FormHelperTest.php 312 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Helper;
  17. use ArrayObject;
  18. use Cake\Chronos\ChronosTime;
  19. use Cake\Collection\Collection;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Exception\CakeException;
  22. use Cake\Database\Type\EnumType;
  23. use Cake\Form\Form;
  24. use Cake\Http\ServerRequest;
  25. use Cake\I18n\Date;
  26. use Cake\I18n\DateTime;
  27. use Cake\ORM\Entity;
  28. use Cake\ORM\Table;
  29. use Cake\Routing\Router;
  30. use Cake\TestSuite\TestCase;
  31. use Cake\Utility\Security;
  32. use Cake\Validation\Validator;
  33. use Cake\View\Form\EntityContext;
  34. use Cake\View\Helper\FormHelper;
  35. use Cake\View\View;
  36. use Cake\View\Widget\WidgetLocator;
  37. use InvalidArgumentException;
  38. use ReflectionProperty;
  39. use TestApp\Model\Entity\Article;
  40. use TestApp\Model\Enum\ArticleStatus;
  41. use TestApp\Model\Enum\ArticleStatusLabel;
  42. use TestApp\Model\Enum\ArticleStatusLabelInterface;
  43. use TestApp\Model\Table\ContactsTable;
  44. use TestApp\Model\Table\ValidateUsersTable;
  45. use TestApp\View\Form\StubContext;
  46. /**
  47. * FormHelperTest class
  48. *
  49. * @property \Cake\View\Helper\FormHelper $Form
  50. * @property \Cake\View\View $View
  51. */
  52. class FormHelperTest extends TestCase
  53. {
  54. /**
  55. * Fixtures to be used
  56. *
  57. * @var array<string>
  58. */
  59. protected array $fixtures = ['core.Articles', 'core.Comments'];
  60. /**
  61. * @var array
  62. */
  63. protected $article = [];
  64. /**
  65. * @var string
  66. */
  67. protected $url;
  68. /**
  69. * @var \Cake\View\Helper\FormHelper
  70. */
  71. protected $Form;
  72. /**
  73. * @var \Cake\View\View
  74. */
  75. protected $View;
  76. /**
  77. * setUp method
  78. */
  79. public function setUp(): void
  80. {
  81. parent::setUp();
  82. Configure::write('Config.language', 'eng');
  83. Configure::write('App.base', '');
  84. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  85. $request = new ServerRequest([
  86. 'webroot' => '',
  87. 'base' => '',
  88. 'url' => '/articles/add',
  89. 'params' => [
  90. 'controller' => 'Articles',
  91. 'action' => 'add',
  92. 'plugin' => null,
  93. ],
  94. ]);
  95. $this->View = new View($request);
  96. Router::reload();
  97. Router::setRequest($request);
  98. $this->url = '/articles/add';
  99. $this->Form = new FormHelper($this->View);
  100. $this->article = [
  101. 'schema' => [
  102. 'id' => ['type' => 'integer'],
  103. 'author_id' => ['type' => 'integer', 'null' => true],
  104. 'title' => ['type' => 'string', 'null' => true],
  105. 'body' => 'text',
  106. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  107. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  108. ],
  109. 'required' => [
  110. 'author_id' => true,
  111. 'title' => true,
  112. ],
  113. ];
  114. Security::setSalt('foo!');
  115. $builder = Router::createRouteBuilder('/');
  116. $builder->connect('/{controller}', ['action' => 'index']);
  117. $builder->connect('/{controller}/{action}/*');
  118. }
  119. /**
  120. * tearDown method
  121. */
  122. public function tearDown(): void
  123. {
  124. parent::tearDown();
  125. unset($this->Form, $this->View);
  126. }
  127. /**
  128. * Test construct() with the templates option.
  129. */
  130. public function testConstructTemplatesFile(): void
  131. {
  132. $helper = new FormHelper($this->View, [
  133. 'templates' => 'htmlhelper_tags',
  134. ]);
  135. $result = $helper->control('name');
  136. $this->assertStringContainsString('<input', $result);
  137. }
  138. /**
  139. * Test that when specifying custom widgets the config array for that widget
  140. * is overwritten instead of merged.
  141. */
  142. public function testConstructWithWidgets(): void
  143. {
  144. $config = [
  145. 'widgets' => [
  146. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  147. ],
  148. ];
  149. $helper = new FormHelper($this->View, $config);
  150. $locator = $helper->getWidgetLocator();
  151. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  152. }
  153. /**
  154. * Test that when specifying custom widgets config file and it should be
  155. * added to widgets array. WidgetLocator will load widgets in constructor.
  156. */
  157. public function testConstructWithWidgetsConfig(): void
  158. {
  159. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  160. $locator = $helper->getWidgetLocator();
  161. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  162. }
  163. /**
  164. * Test setting the widget locator
  165. */
  166. public function testSetAndGetWidgetLocator(): void
  167. {
  168. $helper = new FormHelper($this->View);
  169. $locator = new WidgetLocator($helper->templater(), $this->View);
  170. $helper->setWidgetLocator($locator);
  171. $this->assertSame($locator, $helper->getWidgetLocator());
  172. }
  173. /**
  174. * Test overridding grouped input types which controls generation of "for"
  175. * attribute of labels.
  176. */
  177. public function testConstructWithGroupedInputTypes(): void
  178. {
  179. $helper = new FormHelper($this->View, [
  180. 'groupedInputTypes' => ['radio'],
  181. ]);
  182. $result = $helper->control('when', ['type' => 'datetime-local']);
  183. $this->assertStringContainsString('<label for="when">When</label>', $result);
  184. }
  185. /**
  186. * Test registering a new widget class and rendering it.
  187. */
  188. public function testAddWidgetAndRenderWidget(): void
  189. {
  190. $data = [
  191. 'val' => 1,
  192. ];
  193. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  194. $this->Form->addWidget('test', $mock);
  195. $mock->expects($this->once())
  196. ->method('render')
  197. ->with($data)
  198. ->willReturn('HTML');
  199. $result = $this->Form->widget('test', $data);
  200. $this->assertSame('HTML', $result);
  201. }
  202. /**
  203. * Test that secureFields() of widget is called after calling render(),
  204. * not before.
  205. */
  206. public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
  207. {
  208. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  209. 'unlockedFields' => [],
  210. ]));
  211. $data = [
  212. 'val' => 1,
  213. 'name' => 'test',
  214. ];
  215. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  216. $this->Form->addWidget('test', $mock);
  217. $mock->expects($this->once())
  218. ->method('render')
  219. ->with($data)
  220. ->willReturn('HTML');
  221. $mock->expects($this->once())
  222. ->method('secureFields')
  223. ->with($data)
  224. ->willReturn(['test']);
  225. $this->Form->create();
  226. $result = $this->Form->widget('test', $data + ['secure' => true]);
  227. $this->assertSame('HTML', $result);
  228. }
  229. /**
  230. * Test that empty string is not added to secure fields list when
  231. * rendering input widget without name.
  232. */
  233. public function testRenderingWidgetWithEmptyName(): void
  234. {
  235. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  236. $this->Form->create();
  237. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  238. $this->assertSame('<select name=""></select>', $result);
  239. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  240. $this->assertEquals([], $result);
  241. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  242. $this->assertSame('<select name="0"></select>', $result);
  243. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  244. $this->assertEquals(['0'], $result);
  245. }
  246. /**
  247. * Test adding a new context class.
  248. */
  249. public function testAddContextProvider(): void
  250. {
  251. $context = 'My data';
  252. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  253. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  254. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  255. $this->assertSame($context, $data['entity']);
  256. return $stub;
  257. });
  258. $this->Form->create($context);
  259. $result = $this->Form->context();
  260. $this->assertSame($stub, $result);
  261. }
  262. /**
  263. * Test replacing a context class.
  264. */
  265. public function testAddContextProviderReplace(): void
  266. {
  267. $entity = new Article();
  268. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  269. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  270. return $stub;
  271. });
  272. $this->Form->create($entity);
  273. $result = $this->Form->context();
  274. $this->assertSame($stub, $result);
  275. }
  276. /**
  277. * Test overriding a context class.
  278. */
  279. public function testAddContextProviderAdd(): void
  280. {
  281. $entity = new Article();
  282. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  283. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  284. if ($data['entity'] instanceof Entity) {
  285. return $stub;
  286. }
  287. });
  288. $this->Form->create($entity);
  289. $result = $this->Form->context();
  290. $this->assertSame($stub, $result);
  291. }
  292. /**
  293. * Provides context options for create().
  294. *
  295. * @return array
  296. */
  297. public static function contextSelectionProvider(): array
  298. {
  299. $entity = new Article();
  300. $collection = new Collection([$entity]);
  301. $emptyCollection = new Collection([]);
  302. $data = [
  303. 'schema' => [
  304. 'title' => ['type' => 'string'],
  305. ],
  306. ];
  307. $form = new Form();
  308. $custom = new StubContext();
  309. return [
  310. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  311. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  312. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  313. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  314. 'form' => [$form, 'Cake\View\Form\FormContext'],
  315. 'none' => [null, 'Cake\View\Form\NullContext'],
  316. 'custom' => [$custom, $custom::class],
  317. ];
  318. }
  319. /**
  320. * Test default context selection in create()
  321. *
  322. * @dataProvider contextSelectionProvider
  323. * @param mixed $data
  324. */
  325. public function testCreateContextSelectionBuiltIn($data, string $class): void
  326. {
  327. $this->Form->create($data);
  328. $this->assertInstanceOf($class, $this->Form->context());
  329. }
  330. /**
  331. * Data provider for type option.
  332. *
  333. * @return array
  334. */
  335. public static function requestTypeProvider(): array
  336. {
  337. return [
  338. // type, method, override
  339. ['post', 'post', 'POST'],
  340. ['put', 'post', 'PUT'],
  341. ['patch', 'post', 'PATCH'],
  342. ['delete', 'post', 'DELETE'],
  343. ];
  344. }
  345. /**
  346. * Test creating file forms.
  347. */
  348. public function testCreateFile(): void
  349. {
  350. $encoding = strtolower(Configure::read('App.encoding'));
  351. $result = $this->Form->create(null, ['type' => 'file']);
  352. $expected = [
  353. 'form' => [
  354. 'method' => 'post', 'action' => '/articles/add',
  355. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  356. ],
  357. 'div' => ['style' => 'display:none;'],
  358. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  359. '/div',
  360. ];
  361. $this->assertHtml($expected, $result);
  362. }
  363. /**
  364. * Test creating GET forms.
  365. */
  366. public function testCreateGet(): void
  367. {
  368. $encoding = strtolower(Configure::read('App.encoding'));
  369. $result = $this->Form->create(null, ['type' => 'get']);
  370. $expected = ['form' => [
  371. 'method' => 'get', 'action' => '/articles/add',
  372. 'accept-charset' => $encoding,
  373. ]];
  374. $this->assertHtml($expected, $result);
  375. }
  376. /**
  377. * Test explicit method/enctype options.
  378. *
  379. * Explicit method overwrites inferred method from 'type'
  380. */
  381. public function testCreateExplicitMethodEnctype(): void
  382. {
  383. $encoding = strtolower(Configure::read('App.encoding'));
  384. $result = $this->Form->create(null, [
  385. 'type' => 'get',
  386. 'method' => 'put',
  387. 'enctype' => 'multipart/form-data',
  388. ]);
  389. $expected = ['form' => [
  390. 'method' => 'put',
  391. 'action' => '/articles/add',
  392. 'enctype' => 'multipart/form-data',
  393. 'accept-charset' => $encoding,
  394. ]];
  395. $this->assertHtml($expected, $result);
  396. }
  397. /**
  398. * Test create() with the templates option.
  399. */
  400. public function testCreateTemplatesArray(): void
  401. {
  402. $result = $this->Form->create($this->article, [
  403. 'templates' => [
  404. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  405. ],
  406. ]);
  407. $expected = [
  408. 'form' => [
  409. 'class' => 'form-horizontal',
  410. 'method' => 'post',
  411. 'action' => '/articles/add',
  412. 'accept-charset' => 'utf-8',
  413. ],
  414. ];
  415. $this->assertHtml($expected, $result);
  416. }
  417. /**
  418. * Test create() with the templates option.
  419. */
  420. public function testCreateTemplatesFile(): void
  421. {
  422. $result = $this->Form->create($this->article, [
  423. 'templates' => 'htmlhelper_tags',
  424. ]);
  425. $expected = [
  426. 'start form',
  427. ];
  428. $this->assertHtml($expected, $result);
  429. }
  430. /**
  431. * Test that create() and end() restore templates.
  432. */
  433. public function testCreateEndRestoreTemplates(): void
  434. {
  435. $this->Form->create($this->article, [
  436. 'templates' => ['input' => 'custom input element'],
  437. ]);
  438. $this->Form->end();
  439. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  440. }
  441. /**
  442. * Test create() with the templates option.
  443. */
  444. public function testCreateTemplatesRequiredClass(): void
  445. {
  446. $this->Form->create($this->article, [
  447. 'templates' => [
  448. 'requiredClass' => 'is-required',
  449. ],
  450. ]);
  451. $result = $this->Form->control('title');
  452. $expected = [
  453. 'div' => ['class' => 'input text is-required'],
  454. 'label' => ['for' => 'title'],
  455. 'Title',
  456. '/label',
  457. 'input' => [
  458. 'type' => 'text',
  459. 'name' => 'title',
  460. 'id' => 'title',
  461. 'required' => 'required',
  462. 'data-validity-message' => 'This field cannot be left empty',
  463. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  464. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  465. 'aria-required' => 'true',
  466. ],
  467. '/div',
  468. ];
  469. $this->assertHtml($expected, $result);
  470. }
  471. /**
  472. * Test using template vars in various templates used by control() method.
  473. */
  474. public function testControlTemplateVars(): void
  475. {
  476. $result = $this->Form->control('text', [
  477. 'templates' => [
  478. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  479. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  480. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  481. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  482. ],
  483. 'templateVars' => [
  484. 'forinput' => 'in-input',
  485. 'forlabel' => 'in-label',
  486. 'forgroup' => 'in-group',
  487. 'forcontainer' => 'in-container',
  488. ],
  489. ]);
  490. $expected = [
  491. 'div' => ['class'],
  492. 'label' => ['for'],
  493. 'Text in-label',
  494. '/label',
  495. 'in-group',
  496. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  497. 'in-container',
  498. '/div',
  499. ];
  500. $this->assertHtml($expected, $result);
  501. }
  502. /**
  503. * Test ensuring template variables work in template files loaded
  504. * during control().
  505. */
  506. public function testControlTemplatesFromFile(): void
  507. {
  508. $result = $this->Form->control('title', [
  509. 'templates' => 'test_templates',
  510. 'templateVars' => [
  511. 'forcontainer' => 'container-data',
  512. ],
  513. ]);
  514. $expected = [
  515. 'div' => ['class'],
  516. 'label' => ['for'],
  517. 'Title',
  518. '/label',
  519. 'input' => ['name', 'type' => 'text', 'id'],
  520. 'container-data',
  521. '/div',
  522. ];
  523. $this->assertHtml($expected, $result);
  524. }
  525. /**
  526. * Test using template vars in inputSubmit and submitContainer template.
  527. */
  528. public function testSubmitTemplateVars(): void
  529. {
  530. $this->Form->setTemplates([
  531. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  532. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  533. ]);
  534. $result = $this->Form->submit('Submit', [
  535. 'templateVars' => [
  536. 'forinput' => 'in-input',
  537. 'forcontainer' => 'in-container',
  538. ],
  539. ]);
  540. $expected = [
  541. 'div' => ['class'],
  542. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  543. 'in-container',
  544. '/div',
  545. ];
  546. $this->assertHtml($expected, $result);
  547. }
  548. /**
  549. * test the create() method
  550. *
  551. * @dataProvider requestTypeProvider
  552. */
  553. public function testCreateTypeOptions(string $type, string $method, string $override): void
  554. {
  555. $encoding = strtolower(Configure::read('App.encoding'));
  556. $result = $this->Form->create(null, ['type' => $type]);
  557. $expected = [
  558. 'form' => [
  559. 'method' => $method, 'action' => '/articles/add',
  560. 'accept-charset' => $encoding,
  561. ],
  562. ];
  563. $extra = [
  564. 'div' => ['style' => 'display:none;'],
  565. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  566. '/div',
  567. ];
  568. if ($type !== 'post') {
  569. $expected = array_merge($expected, $extra);
  570. }
  571. $this->assertHtml($expected, $result);
  572. }
  573. /**
  574. * Test using template vars in Create (formStart template)
  575. */
  576. public function testCreateTemplateVars(): void
  577. {
  578. $result = $this->Form->create($this->article, [
  579. 'templates' => [
  580. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  581. ],
  582. 'templateVars' => ['header' => 'headertext'],
  583. ]);
  584. $expected = [
  585. 'h4' => ['class'],
  586. 'headertext',
  587. '/h4',
  588. 'form' => [
  589. 'method' => 'post',
  590. 'action' => '/articles/add',
  591. 'accept-charset' => 'utf-8',
  592. ],
  593. ];
  594. $this->assertHtml($expected, $result);
  595. }
  596. /**
  597. * Test opening a form for an update operation.
  598. */
  599. public function testCreateUpdateForm(): void
  600. {
  601. $encoding = strtolower(Configure::read('App.encoding'));
  602. $this->View->setRequest($this->View->getRequest()
  603. ->withRequestTarget('/articles/edit/1')
  604. ->withParam('action', 'edit'));
  605. $this->article['defaults']['id'] = 1;
  606. $result = $this->Form->create($this->article);
  607. $expected = [
  608. 'form' => [
  609. 'method' => 'post',
  610. 'action' => '/articles/edit/1',
  611. 'accept-charset' => $encoding,
  612. ],
  613. 'div' => ['style' => 'display:none;'],
  614. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  615. '/div',
  616. ];
  617. $this->assertHtml($expected, $result);
  618. }
  619. /**
  620. * test create() with automatic url generation
  621. */
  622. public function testCreateAutoUrl(): void
  623. {
  624. $encoding = strtolower(Configure::read('App.encoding'));
  625. $this->View->setRequest($this->View->getRequest()
  626. ->withRequestTarget('/articles/delete/10')
  627. ->withParam('action', 'delete'));
  628. $result = $this->Form->create($this->article);
  629. $expected = [
  630. 'form' => [
  631. 'method' => 'post', 'action' => '/articles/delete/10',
  632. 'accept-charset' => $encoding,
  633. ],
  634. ];
  635. $this->assertHtml($expected, $result);
  636. $this->article['defaults'] = ['id' => 1];
  637. $this->View->setRequest($this->View->getRequest()
  638. ->withRequestTarget('/Articles/edit/1')
  639. ->withParam('action', 'delete'));
  640. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  641. $expected = [
  642. 'form' => [
  643. 'method' => 'post',
  644. 'action' => '/Articles/edit/1',
  645. 'accept-charset' => $encoding,
  646. ],
  647. 'div' => ['style' => 'display:none;'],
  648. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  649. '/div',
  650. ];
  651. $this->assertHtml($expected, $result);
  652. $this->View->setRequest($this->View->getRequest()
  653. ->withParam('action', 'add'));
  654. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  655. $expected = [
  656. 'form' => [
  657. 'method' => 'post',
  658. 'action' => '/Articles/publish/1',
  659. 'accept-charset' => $encoding,
  660. ],
  661. 'div' => ['style' => 'display:none;'],
  662. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  663. '/div',
  664. ];
  665. $this->assertHtml($expected, $result);
  666. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  667. $expected = [
  668. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  669. 'div' => ['style' => 'display:none;'],
  670. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  671. '/div',
  672. ];
  673. $this->assertHtml($expected, $result);
  674. $this->View->setRequest($this->View->getRequest()
  675. ->withParam('controller', 'Pages'));
  676. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  677. $expected = [
  678. 'form' => [
  679. 'method' => 'post', 'action' => '/Pages/signup/1',
  680. 'accept-charset' => $encoding,
  681. ],
  682. 'div' => ['style' => 'display:none;'],
  683. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  684. '/div',
  685. ];
  686. $this->assertHtml($expected, $result);
  687. }
  688. /**
  689. * Test create() with no URL (no "action" attribute for <form> tag)
  690. */
  691. public function testCreateNoUrl(): void
  692. {
  693. $result = $this->Form->create(null, ['url' => false]);
  694. $expected = [
  695. 'form' => [
  696. 'method' => 'post',
  697. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  698. ],
  699. ];
  700. $this->assertHtml($expected, $result);
  701. }
  702. /**
  703. * test create() with a custom route
  704. */
  705. public function testCreateCustomRoute(): void
  706. {
  707. $builder = Router::createRouteBuilder('/');
  708. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  709. $encoding = strtolower(Configure::read('App.encoding'));
  710. $this->View->setRequest($this->View->getRequest()
  711. ->withParam('controller', 'Users'));
  712. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  713. $expected = [
  714. 'form' => [
  715. 'method' => 'post', 'action' => '/login',
  716. 'accept-charset' => $encoding,
  717. ],
  718. ];
  719. $this->assertHtml($expected, $result);
  720. $builder->connect(
  721. '/new-article',
  722. ['controller' => 'Articles', 'action' => 'myAction'],
  723. ['_name' => 'my-route']
  724. );
  725. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  726. $expected = [
  727. 'form' => [
  728. 'method' => 'post', 'action' => '/new-article',
  729. 'accept-charset' => $encoding,
  730. ],
  731. ];
  732. $this->assertHtml($expected, $result);
  733. }
  734. /**
  735. * test automatic accept-charset overriding
  736. */
  737. public function testCreateWithAcceptCharset(): void
  738. {
  739. $result = $this->Form->create(
  740. $this->article,
  741. [
  742. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  743. ]
  744. );
  745. $expected = [
  746. 'form' => [
  747. 'method' => 'post', 'action' => '/Articles',
  748. 'accept-charset' => 'iso-8859-1',
  749. ],
  750. ];
  751. $this->assertHtml($expected, $result);
  752. }
  753. /**
  754. * Test base form URL when 'url' param is passed with multiple parameters (&)
  755. */
  756. public function testCreateQueryStringRequest(): void
  757. {
  758. $encoding = strtolower(Configure::read('App.encoding'));
  759. $result = $this->Form->create($this->article, [
  760. 'type' => 'post',
  761. 'escape' => false,
  762. 'url' => [
  763. 'controller' => 'Controller',
  764. 'action' => 'action',
  765. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  766. ],
  767. ]);
  768. $expected = [
  769. 'form' => [
  770. 'method' => 'post',
  771. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  772. 'accept-charset' => $encoding,
  773. ],
  774. ];
  775. $this->assertHtml($expected, $result);
  776. $result = $this->Form->create($this->article, [
  777. 'type' => 'post',
  778. 'url' => [
  779. 'controller' => 'Controller',
  780. 'action' => 'action',
  781. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  782. ],
  783. ]);
  784. $this->assertHtml($expected, $result);
  785. }
  786. /**
  787. * test that create() doesn't cause errors by multiple id's being in the primary key
  788. * as could happen with multiple select or checkboxes.
  789. */
  790. public function testCreateWithMultipleIdInData(): void
  791. {
  792. $encoding = strtolower(Configure::read('App.encoding'));
  793. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  794. $result = $this->Form->create($this->article);
  795. $expected = [
  796. 'form' => [
  797. 'method' => 'post',
  798. 'action' => '/articles/add',
  799. 'accept-charset' => $encoding,
  800. ],
  801. ];
  802. $this->assertHtml($expected, $result);
  803. }
  804. /**
  805. * test that create() doesn't add in extra passed params.
  806. */
  807. public function testCreatePassedArgs(): void
  808. {
  809. $encoding = strtolower(Configure::read('App.encoding'));
  810. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  811. $result = $this->Form->create($this->article, [
  812. 'type' => 'post',
  813. 'escape' => false,
  814. 'url' => [
  815. 'action' => 'edit',
  816. 'myparam',
  817. ],
  818. ]);
  819. $expected = [
  820. 'form' => [
  821. 'method' => 'post',
  822. 'action' => '/Articles/edit/myparam',
  823. 'accept-charset' => $encoding,
  824. ],
  825. ];
  826. $this->assertHtml($expected, $result);
  827. }
  828. /**
  829. * test creating a get form, and get form inputs.
  830. */
  831. public function testGetFormCreate(): void
  832. {
  833. $encoding = strtolower(Configure::read('App.encoding'));
  834. $result = $this->Form->create($this->article, ['type' => 'get']);
  835. $expected = ['form' => [
  836. 'method' => 'get', 'action' => '/articles/add',
  837. 'accept-charset' => $encoding,
  838. ]];
  839. $this->assertHtml($expected, $result);
  840. $result = $this->Form->text('title');
  841. $expected = ['input' => [
  842. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  843. ]];
  844. $this->assertHtml($expected, $result);
  845. $result = $this->Form->password('password');
  846. $expected = ['input' => [
  847. 'name' => 'password', 'type' => 'password',
  848. ]];
  849. $this->assertHtml($expected, $result);
  850. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  851. $result = $this->Form->text('user_form');
  852. $expected = ['input' => [
  853. 'name' => 'user_form', 'type' => 'text',
  854. ]];
  855. $this->assertHtml($expected, $result);
  856. }
  857. /**
  858. * test get form, and inputs when the model param is false
  859. */
  860. public function testGetFormWithFalseModel(): void
  861. {
  862. $encoding = strtolower(Configure::read('App.encoding'));
  863. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  864. $result = $this->Form->create(null, [
  865. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  866. ]);
  867. $expected = ['form' => [
  868. 'method' => 'get', 'action' => '/ContactTest/add',
  869. 'accept-charset' => $encoding,
  870. ]];
  871. $this->assertHtml($expected, $result);
  872. $result = $this->Form->text('reason');
  873. $expected = [
  874. 'input' => ['type' => 'text', 'name' => 'reason'],
  875. ];
  876. $this->assertHtml($expected, $result);
  877. }
  878. /**
  879. * testFormCreateWithSecurity method
  880. *
  881. * Test form->create() with security key.
  882. */
  883. public function testCreateWithSecurity(): void
  884. {
  885. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  886. $encoding = strtolower(Configure::read('App.encoding'));
  887. $result = $this->Form->create($this->article, [
  888. 'url' => '/articles/publish',
  889. ]);
  890. $expected = [
  891. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  892. 'div' => ['style' => 'display:none;'],
  893. ['input' => [
  894. 'type' => 'hidden',
  895. 'name' => '_csrfToken',
  896. 'value' => 'testKey',
  897. ]],
  898. '/div',
  899. ];
  900. $this->assertHtml($expected, $result);
  901. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  902. $expected['form']['id'] = 'MyForm';
  903. $this->assertHtml($expected, $result);
  904. }
  905. /**
  906. * testFormCreateGetNoSecurity method
  907. *
  908. * Test form->create() with no security key as its a get form
  909. */
  910. public function testCreateEndGetNoSecurity(): void
  911. {
  912. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  913. $article = new Article();
  914. $result = $this->Form->create($article, [
  915. 'type' => 'get',
  916. 'url' => '/contacts/add',
  917. ]);
  918. $this->assertStringNotContainsString('testKey', $result);
  919. $result = $this->Form->end();
  920. $this->assertStringNotContainsString('testKey', $result);
  921. }
  922. /**
  923. * Tests form hash generation with model-less data
  924. */
  925. public function testValidateHashNoModel(): void
  926. {
  927. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  928. $fields = ['anything'];
  929. $this->Form->create();
  930. $result = $this->Form->secure($fields);
  931. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  932. $this->assertStringContainsString($hash, $result);
  933. }
  934. /**
  935. * Tests that hidden fields generated for checkboxes don't get locked
  936. */
  937. public function testNoCheckboxLocking(): void
  938. {
  939. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  940. $this->Form->create();
  941. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  942. $this->Form->checkbox('check', ['value' => '1']);
  943. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  944. }
  945. /**
  946. * testFormSecurityFields method
  947. *
  948. * Test generation of secure form hash generation.
  949. */
  950. public function testFormSecurityFields(): void
  951. {
  952. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  953. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  954. $this->Form->create();
  955. $result = $this->Form->secure($fields);
  956. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  957. $hash .= ':' . 'Model.valid';
  958. $hash = urlencode($hash);
  959. $tokenDebug = urlencode(json_encode([
  960. $this->url,
  961. $fields,
  962. [],
  963. ]));
  964. $expected = [
  965. 'div' => ['style' => 'display:none;'],
  966. ['input' => [
  967. 'type' => 'hidden',
  968. 'name' => '_Token[fields]',
  969. 'value' => $hash,
  970. ]],
  971. ['input' => [
  972. 'type' => 'hidden',
  973. 'name' => '_Token[unlocked]',
  974. 'value' => '',
  975. ]],
  976. ['input' => [
  977. 'type' => 'hidden',
  978. 'name' => '_Token[debug]',
  979. 'value' => $tokenDebug,
  980. ]],
  981. '/div',
  982. ];
  983. $this->assertHtml($expected, $result);
  984. }
  985. /**
  986. * testFormSecurityFields method
  987. *
  988. * Test debug token is not generated if debug is false
  989. */
  990. public function testFormSecurityFieldsNoDebugMode(): void
  991. {
  992. Configure::write('debug', false);
  993. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  994. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  995. $this->Form->create();
  996. $result = $this->Form->secure($fields);
  997. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  998. $hash .= ':' . 'Model.valid';
  999. $hash = urlencode($hash);
  1000. $expected = [
  1001. 'div' => ['style' => 'display:none;'],
  1002. ['input' => [
  1003. 'type' => 'hidden',
  1004. 'name' => '_Token[fields]',
  1005. 'value' => $hash,
  1006. ]],
  1007. ['input' => [
  1008. 'type' => 'hidden',
  1009. 'name' => '_Token[unlocked]',
  1010. 'value' => '',
  1011. ]],
  1012. '/div',
  1013. ];
  1014. $this->assertHtml($expected, $result);
  1015. }
  1016. /**
  1017. * Tests correct generation of number fields for smallint
  1018. */
  1019. public function testTextFieldGenerationForSmallint(): void
  1020. {
  1021. $this->article['schema'] = [
  1022. 'foo' => [
  1023. 'type' => 'smallinteger',
  1024. 'null' => false,
  1025. 'default' => null,
  1026. 'length' => 10,
  1027. ],
  1028. ];
  1029. $this->Form->create($this->article);
  1030. $result = $this->Form->control('foo');
  1031. $this->assertStringContainsString('class="input number"', $result);
  1032. $this->assertStringContainsString('type="number"', $result);
  1033. }
  1034. /**
  1035. * Tests correct generation of number fields for tinyint
  1036. */
  1037. public function testTextFieldGenerationForTinyint(): void
  1038. {
  1039. $this->article['schema'] = [
  1040. 'foo' => [
  1041. 'type' => 'tinyinteger',
  1042. 'null' => false,
  1043. 'default' => null,
  1044. 'length' => 10,
  1045. ],
  1046. ];
  1047. $this->Form->create($this->article);
  1048. $result = $this->Form->control('foo');
  1049. $this->assertStringContainsString('class="input number"', $result);
  1050. $this->assertStringContainsString('type="number"', $result);
  1051. }
  1052. /**
  1053. * Tests correct generation of number fields for double and float fields
  1054. */
  1055. public function testTextFieldGenerationForFloats(): void
  1056. {
  1057. $this->article['schema'] = [
  1058. 'foo' => [
  1059. 'type' => 'float',
  1060. 'null' => false,
  1061. 'default' => null,
  1062. 'length' => 10,
  1063. ],
  1064. ];
  1065. $this->Form->create($this->article);
  1066. $result = $this->Form->control('foo');
  1067. $expected = [
  1068. 'div' => ['class' => 'input number'],
  1069. 'label' => ['for' => 'foo'],
  1070. 'Foo',
  1071. '/label',
  1072. ['input' => [
  1073. 'type' => 'number',
  1074. 'name' => 'foo',
  1075. 'id' => 'foo',
  1076. 'step' => 'any',
  1077. ]],
  1078. '/div',
  1079. ];
  1080. $this->assertHtml($expected, $result);
  1081. $result = $this->Form->control('foo', ['step' => 0.5]);
  1082. $expected = [
  1083. 'div' => ['class' => 'input number'],
  1084. 'label' => ['for' => 'foo'],
  1085. 'Foo',
  1086. '/label',
  1087. ['input' => [
  1088. 'type' => 'number',
  1089. 'name' => 'foo',
  1090. 'id' => 'foo',
  1091. 'step' => '0.5',
  1092. ]],
  1093. '/div',
  1094. ];
  1095. $this->assertHtml($expected, $result);
  1096. }
  1097. /**
  1098. * Tests correct generation of number fields for integer fields
  1099. */
  1100. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1101. {
  1102. $this->getTableLocator()->get('Contacts', [
  1103. 'className' => ContactsTable::class,
  1104. ]);
  1105. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1106. $result = $this->Form->control('age');
  1107. $expected = [
  1108. 'div' => ['class' => 'input number'],
  1109. 'label' => ['for' => 'age'],
  1110. 'Age',
  1111. '/label',
  1112. ['input' => [
  1113. 'type' => 'number', 'name' => 'age',
  1114. 'id' => 'age',
  1115. ]],
  1116. '/div',
  1117. ];
  1118. $this->assertHtml($expected, $result);
  1119. }
  1120. /**
  1121. * Tests correct generation of file upload fields for binary fields
  1122. */
  1123. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1124. {
  1125. $table = $this->getTableLocator()->get('Contacts', [
  1126. 'className' => ContactsTable::class,
  1127. ]);
  1128. $table->setSchema(['foo' => [
  1129. 'type' => 'binary',
  1130. 'null' => false,
  1131. 'default' => null,
  1132. 'length' => 1024,
  1133. ]]);
  1134. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1135. $result = $this->Form->control('foo');
  1136. $expected = [
  1137. 'div' => ['class' => 'input file'],
  1138. 'label' => ['for' => 'foo'],
  1139. 'Foo',
  1140. '/label',
  1141. ['input' => [
  1142. 'type' => 'file', 'name' => 'foo',
  1143. 'id' => 'foo',
  1144. ]],
  1145. '/div',
  1146. ];
  1147. $this->assertHtml($expected, $result);
  1148. }
  1149. /**
  1150. * testFormSecurityMultipleFields method
  1151. *
  1152. * Test secure() with multiple row form. Ensure hash is correct.
  1153. */
  1154. public function testFormSecurityMultipleFields(): void
  1155. {
  1156. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1157. $this->Form->create();
  1158. $fields = [
  1159. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1160. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1161. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1162. ];
  1163. $result = $this->Form->secure($fields);
  1164. $sortedFields = [
  1165. 'Model.0.password',
  1166. 'Model.0.username',
  1167. 'Model.1.password',
  1168. 'Model.1.username',
  1169. 'Model.0.hidden' => 'value',
  1170. 'Model.0.valid' => '0',
  1171. 'Model.1.hidden' => 'value',
  1172. 'Model.1.valid' => '0',
  1173. ];
  1174. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1175. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1176. $hash = urlencode($hash);
  1177. $tokenDebug = urlencode(json_encode([
  1178. $this->url,
  1179. $fields,
  1180. [],
  1181. ]));
  1182. $expected = [
  1183. 'div' => ['style' => 'display:none;'],
  1184. ['input' => [
  1185. 'type' => 'hidden',
  1186. 'name' => '_Token[fields]',
  1187. 'value' => $hash,
  1188. ]],
  1189. ['input' => [
  1190. 'type' => 'hidden',
  1191. 'name' => '_Token[unlocked]',
  1192. 'value' => '',
  1193. ]],
  1194. ['input' => [
  1195. 'type' => 'hidden',
  1196. 'name' => '_Token[debug]',
  1197. 'value' => $tokenDebug,
  1198. ]],
  1199. '/div',
  1200. ];
  1201. $this->assertHtml($expected, $result);
  1202. }
  1203. /**
  1204. * testFormSecurityMultipleSubmitButtons
  1205. *
  1206. * test form submit generation and ensure that _Token is only created on end()
  1207. */
  1208. public function testFormSecurityMultipleSubmitButtons(): void
  1209. {
  1210. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1211. $this->Form->create($this->article);
  1212. $this->Form->text('Address.title');
  1213. $this->Form->text('Address.first_name');
  1214. $result = $this->Form->submit('Save', ['name' => 'save']);
  1215. $expected = [
  1216. 'div' => ['class' => 'submit'],
  1217. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1218. '/div',
  1219. ];
  1220. $this->assertHtml($expected, $result);
  1221. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1222. $expected = [
  1223. 'div' => ['class' => 'submit'],
  1224. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1225. '/div',
  1226. ];
  1227. $this->assertHtml($expected, $result);
  1228. $result = $this->Form->end();
  1229. $tokenDebug = urlencode(json_encode([
  1230. '/articles/add',
  1231. [
  1232. 'Address.title',
  1233. 'Address.first_name',
  1234. ],
  1235. ['save', 'cancel'],
  1236. ]));
  1237. $expected = [
  1238. 'div' => ['style' => 'display:none;'],
  1239. ['input' => [
  1240. 'type' => 'hidden',
  1241. 'name' => '_Token[fields]',
  1242. 'value',
  1243. ]],
  1244. ['input' => [
  1245. 'type' => 'hidden',
  1246. 'name' => '_Token[unlocked]',
  1247. 'value' => 'cancel%7Csave',
  1248. ]],
  1249. ['input' => [
  1250. 'type' => 'hidden',
  1251. 'name' => '_Token[debug]',
  1252. 'value' => $tokenDebug,
  1253. ]],
  1254. '/div',
  1255. ];
  1256. $this->assertHtml($expected, $result);
  1257. }
  1258. /**
  1259. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1260. */
  1261. public function testSecurityButtonNestedNamed(): void
  1262. {
  1263. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1264. $this->Form->create();
  1265. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1266. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1267. $this->assertEquals(['Address.button'], $result);
  1268. }
  1269. /**
  1270. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1271. */
  1272. public function testSecuritySubmitNestedNamed(): void
  1273. {
  1274. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1275. $this->Form->create($this->article);
  1276. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1277. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1278. $this->assertEquals(['Address.button'], $result);
  1279. }
  1280. /**
  1281. * Test that the correct fields are unlocked for image submits with no names.
  1282. */
  1283. public function testSecuritySubmitImageNoName(): void
  1284. {
  1285. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1286. $this->Form->create();
  1287. $result = $this->Form->submit('save.png');
  1288. $expected = [
  1289. 'div' => ['class' => 'submit'],
  1290. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1291. '/div',
  1292. ];
  1293. $this->assertHtml($expected, $result);
  1294. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1295. $this->assertEquals(['x', 'y'], $result);
  1296. }
  1297. /**
  1298. * Test that the correct fields are unlocked for image submits with names.
  1299. */
  1300. public function testSecuritySubmitImageName(): void
  1301. {
  1302. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1303. $this->Form->create();
  1304. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1305. $expected = [
  1306. 'div' => ['class' => 'submit'],
  1307. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1308. '/div',
  1309. ];
  1310. $this->assertHtml($expected, $result);
  1311. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1312. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1313. }
  1314. /**
  1315. * testFormSecurityMultipleControlFields method
  1316. *
  1317. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1318. */
  1319. public function testFormSecurityMultipleControlFields(): void
  1320. {
  1321. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1322. $this->Form->create();
  1323. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1324. $this->Form->control('Addresses.0.title');
  1325. $this->Form->control('Addresses.0.first_name');
  1326. $this->Form->control('Addresses.0.last_name');
  1327. $this->Form->control('Addresses.0.address');
  1328. $this->Form->control('Addresses.0.city');
  1329. $this->Form->control('Addresses.0.phone');
  1330. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1331. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1332. $this->Form->control('Addresses.1.title');
  1333. $this->Form->control('Addresses.1.first_name');
  1334. $this->Form->control('Addresses.1.last_name');
  1335. $this->Form->control('Addresses.1.address');
  1336. $this->Form->control('Addresses.1.city');
  1337. $this->Form->control('Addresses.1.phone');
  1338. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1339. $result = $this->Form->secure();
  1340. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1341. $tokenDebug = urlencode(json_encode([
  1342. '/articles/add',
  1343. [
  1344. 'Addresses.0.id' => '123456',
  1345. 'Addresses.0.title',
  1346. 'Addresses.0.first_name',
  1347. 'Addresses.0.last_name',
  1348. 'Addresses.0.address',
  1349. 'Addresses.0.city',
  1350. 'Addresses.0.phone',
  1351. 'Addresses.0.primary',
  1352. 'Addresses.1.id' => '654321',
  1353. 'Addresses.1.title',
  1354. 'Addresses.1.first_name',
  1355. 'Addresses.1.last_name',
  1356. 'Addresses.1.address',
  1357. 'Addresses.1.city',
  1358. 'Addresses.1.phone',
  1359. 'Addresses.1.primary',
  1360. ],
  1361. [],
  1362. ]));
  1363. $expected = [
  1364. 'div' => ['style' => 'display:none;'],
  1365. ['input' => [
  1366. 'type' => 'hidden',
  1367. 'name' => '_Token[fields]',
  1368. 'value' => $hash,
  1369. ]],
  1370. ['input' => [
  1371. 'type' => 'hidden',
  1372. 'name' => '_Token[unlocked]',
  1373. 'value' => '',
  1374. ]],
  1375. ['input' => [
  1376. 'type' => 'hidden',
  1377. 'name' => '_Token[debug]',
  1378. 'value' => $tokenDebug,
  1379. ]],
  1380. '/div',
  1381. ];
  1382. $this->assertHtml($expected, $result);
  1383. }
  1384. /**
  1385. * testFormSecurityArrayFields method
  1386. *
  1387. * Test form security with Model.field.0 style inputs.
  1388. */
  1389. public function testFormSecurityArrayFields(): void
  1390. {
  1391. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1392. $this->Form->create();
  1393. $this->Form->text('Address.primary.1');
  1394. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1395. $this->assertSame('Address.primary', $result[0]);
  1396. $this->Form->text('Address.secondary.1.0');
  1397. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1398. $this->assertSame('Address.secondary', $result[1]);
  1399. }
  1400. /**
  1401. * testFormSecurityMultipleControlDisabledFields method
  1402. *
  1403. * Test secure form generation with multiple records and disabled fields.
  1404. */
  1405. public function testFormSecurityMultipleControlDisabledFields(): void
  1406. {
  1407. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1408. 'unlockedFields' => ['first_name', 'address'],
  1409. ]));
  1410. $this->Form->create();
  1411. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1412. $this->Form->text('Addresses.0.title');
  1413. $this->Form->text('Addresses.0.first_name');
  1414. $this->Form->text('Addresses.0.last_name');
  1415. $this->Form->text('Addresses.0.address');
  1416. $this->Form->text('Addresses.0.city');
  1417. $this->Form->text('Addresses.0.phone');
  1418. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1419. $this->Form->text('Addresses.1.title');
  1420. $this->Form->text('Addresses.1.first_name');
  1421. $this->Form->text('Addresses.1.last_name');
  1422. $this->Form->text('Addresses.1.address');
  1423. $this->Form->text('Addresses.1.city');
  1424. $this->Form->text('Addresses.1.phone');
  1425. $result = $this->Form->secure();
  1426. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1427. $tokenDebug = urlencode(json_encode([
  1428. '/articles/add',
  1429. [
  1430. 'Addresses.0.id' => '123456',
  1431. 'Addresses.0.title',
  1432. 'Addresses.0.last_name',
  1433. 'Addresses.0.city',
  1434. 'Addresses.0.phone',
  1435. 'Addresses.1.id' => '654321',
  1436. 'Addresses.1.title',
  1437. 'Addresses.1.last_name',
  1438. 'Addresses.1.city',
  1439. 'Addresses.1.phone',
  1440. ],
  1441. [
  1442. 'first_name',
  1443. 'address',
  1444. ],
  1445. ]));
  1446. $expected = [
  1447. 'div' => ['style' => 'display:none;'],
  1448. ['input' => [
  1449. 'type' => 'hidden',
  1450. 'name' => '_Token[fields]',
  1451. 'value' => $hash,
  1452. ]],
  1453. ['input' => [
  1454. 'type' => 'hidden',
  1455. 'name' => '_Token[unlocked]',
  1456. 'value' => 'address%7Cfirst_name',
  1457. ]],
  1458. ['input' => [
  1459. 'type' => 'hidden',
  1460. 'name' => '_Token[debug]',
  1461. 'value' => $tokenDebug,
  1462. ]],
  1463. '/div',
  1464. ];
  1465. $this->assertHtml($expected, $result);
  1466. }
  1467. /**
  1468. * testFormSecurityControlDisabledFields method
  1469. *
  1470. * Test single record form with disabled fields.
  1471. */
  1472. public function testFormSecurityControlUnlockedFields(): void
  1473. {
  1474. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1475. 'unlockedFields' => ['first_name', 'address'],
  1476. ]));
  1477. $this->Form->create();
  1478. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1479. $this->assertEquals(
  1480. $this->View->getRequest()->getAttribute('formTokenData'),
  1481. ['unlockedFields' => $result]
  1482. );
  1483. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1484. $this->Form->text('Addresses.title');
  1485. $this->Form->text('Addresses.first_name');
  1486. $this->Form->text('Addresses.last_name');
  1487. $this->Form->text('Addresses.address');
  1488. $this->Form->text('Addresses.city');
  1489. $this->Form->text('Addresses.phone');
  1490. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1491. $expected = [
  1492. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1493. 'Addresses.city', 'Addresses.phone',
  1494. ];
  1495. $this->assertEquals($expected, $result);
  1496. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1497. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1498. $tokenDebug = urlencode(json_encode([
  1499. '/articles/add',
  1500. [
  1501. 'Addresses.id' => '123456',
  1502. 'Addresses.title',
  1503. 'Addresses.last_name',
  1504. 'Addresses.city',
  1505. 'Addresses.phone',
  1506. ],
  1507. [
  1508. 'first_name',
  1509. 'address',
  1510. ],
  1511. ]));
  1512. $expected = [
  1513. 'div' => ['style' => 'display:none;'],
  1514. ['input' => [
  1515. 'type' => 'hidden',
  1516. 'name' => '_Token[fields]',
  1517. 'value' => $hash,
  1518. 'data-foo' => 'bar',
  1519. ]],
  1520. ['input' => [
  1521. 'type' => 'hidden',
  1522. 'name' => '_Token[unlocked]',
  1523. 'value' => 'address%7Cfirst_name',
  1524. 'data-foo' => 'bar',
  1525. ]],
  1526. ['input' => [
  1527. 'type' => 'hidden', 'name' => '_Token[debug]',
  1528. 'value' => $tokenDebug,
  1529. 'data-foo' => 'bar',
  1530. ]],
  1531. '/div',
  1532. ];
  1533. $this->assertHtml($expected, $result);
  1534. }
  1535. /**
  1536. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1537. *
  1538. * Test single record form with debugSecurity param.
  1539. */
  1540. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1541. {
  1542. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1543. 'unlockedFields' => ['first_name', 'address'],
  1544. ]));
  1545. $this->Form->create();
  1546. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1547. $this->assertEquals(
  1548. $this->View->getRequest()->getAttribute('formTokenData'),
  1549. ['unlockedFields' => $result]
  1550. );
  1551. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1552. $this->Form->text('Addresses.title');
  1553. $this->Form->text('Addresses.first_name');
  1554. $this->Form->text('Addresses.last_name');
  1555. $this->Form->text('Addresses.address');
  1556. $this->Form->text('Addresses.city');
  1557. $this->Form->text('Addresses.phone');
  1558. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1559. $expected = [
  1560. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1561. 'Addresses.city', 'Addresses.phone',
  1562. ];
  1563. $this->assertEquals($expected, $result);
  1564. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1565. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1566. $tokenDebug = urlencode(json_encode([
  1567. '/articles/add',
  1568. [
  1569. 'Addresses.id' => '123456',
  1570. 'Addresses.title',
  1571. 'Addresses.last_name',
  1572. 'Addresses.city',
  1573. 'Addresses.phone',
  1574. ],
  1575. [
  1576. 'first_name',
  1577. 'address',
  1578. ],
  1579. ]));
  1580. $expected = [
  1581. 'div' => ['style' => 'display:none;'],
  1582. ['input' => [
  1583. 'type' => 'hidden',
  1584. 'name' => '_Token[fields]',
  1585. 'value' => $hash,
  1586. 'data-foo' => 'bar',
  1587. ]],
  1588. ['input' => [
  1589. 'type' => 'hidden',
  1590. 'name' => '_Token[unlocked]',
  1591. 'value' => 'address%7Cfirst_name',
  1592. 'data-foo' => 'bar',
  1593. ]],
  1594. ['input' => [
  1595. 'type' => 'hidden', 'name' => '_Token[debug]',
  1596. 'value' => $tokenDebug,
  1597. 'data-foo' => 'bar',
  1598. ]],
  1599. '/div',
  1600. ];
  1601. $this->assertHtml($expected, $result);
  1602. }
  1603. /**
  1604. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1605. *
  1606. * Debug is false, debugSecurity is true -> no debug
  1607. */
  1608. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
  1609. {
  1610. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1611. 'unlockedFields' => ['first_name', 'address'],
  1612. ]));
  1613. $this->Form->create();
  1614. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1615. $this->assertEquals(
  1616. $this->View->getRequest()->getAttribute('formTokenData'),
  1617. ['unlockedFields' => $result]
  1618. );
  1619. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1620. $this->Form->text('Addresses.title');
  1621. $this->Form->text('Addresses.first_name');
  1622. $this->Form->text('Addresses.last_name');
  1623. $this->Form->text('Addresses.address');
  1624. $this->Form->text('Addresses.city');
  1625. $this->Form->text('Addresses.phone');
  1626. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1627. $expected = [
  1628. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1629. 'Addresses.city', 'Addresses.phone',
  1630. ];
  1631. $this->assertEquals($expected, $result);
  1632. Configure::write('debug', false);
  1633. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1634. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1635. $expected = [
  1636. 'div' => ['style' => 'display:none;'],
  1637. ['input' => [
  1638. 'type' => 'hidden',
  1639. 'name' => '_Token[fields]',
  1640. 'value' => $hash,
  1641. 'data-foo' => 'bar',
  1642. ]],
  1643. ['input' => [
  1644. 'type' => 'hidden',
  1645. 'name' => '_Token[unlocked]',
  1646. 'value' => 'address%7Cfirst_name',
  1647. 'data-foo' => 'bar',
  1648. ]],
  1649. '/div',
  1650. ];
  1651. $this->assertHtml($expected, $result);
  1652. }
  1653. /**
  1654. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1655. *
  1656. * Test single record form with debugSecurity param.
  1657. */
  1658. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1659. {
  1660. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1661. 'unlockedFields' => ['first_name', 'address'],
  1662. ]));
  1663. $this->Form->create();
  1664. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1665. $this->assertEquals(
  1666. $this->View->getRequest()->getAttribute('formTokenData'),
  1667. ['unlockedFields' => $result]
  1668. );
  1669. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1670. $this->Form->text('Addresses.title');
  1671. $this->Form->text('Addresses.first_name');
  1672. $this->Form->text('Addresses.last_name');
  1673. $this->Form->text('Addresses.address');
  1674. $this->Form->text('Addresses.city');
  1675. $this->Form->text('Addresses.phone');
  1676. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1677. $expected = [
  1678. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1679. 'Addresses.city', 'Addresses.phone',
  1680. ];
  1681. $this->assertEquals($expected, $result);
  1682. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1683. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1684. $expected = [
  1685. 'div' => ['style' => 'display:none;'],
  1686. ['input' => [
  1687. 'type' => 'hidden',
  1688. 'name' => '_Token[fields]',
  1689. 'value' => $hash,
  1690. 'data-foo' => 'bar',
  1691. ]],
  1692. ['input' => [
  1693. 'type' => 'hidden',
  1694. 'name' => '_Token[unlocked]',
  1695. 'value' => 'address%7Cfirst_name',
  1696. 'data-foo' => 'bar',
  1697. ]],
  1698. '/div',
  1699. ];
  1700. $this->assertHtml($expected, $result);
  1701. }
  1702. /**
  1703. * testFormSecureWithCustomNameAttribute method
  1704. *
  1705. * Test securing inputs with custom name attributes.
  1706. */
  1707. public function testFormSecureWithCustomNameAttribute(): void
  1708. {
  1709. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1710. $this->Form->create();
  1711. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1712. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1713. $this->assertSame('User.custom', $result[0]);
  1714. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1715. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1716. $this->assertSame('User.custom.another.value', $result[1]);
  1717. }
  1718. /**
  1719. * testFormSecuredControl method
  1720. *
  1721. * Test generation of entire secure form, assertions made on control() output.
  1722. */
  1723. public function testFormSecuredControl(): void
  1724. {
  1725. $this->View->setRequest($this->View->getRequest()
  1726. ->withAttribute('formTokenData', [])
  1727. ->withAttribute('csrfToken', 'testKey'));
  1728. $this->article['schema'] = [
  1729. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1730. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1731. ];
  1732. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1733. $encoding = strtolower(Configure::read('App.encoding'));
  1734. $expected = [
  1735. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1736. 'div' => ['style' => 'display:none;'],
  1737. ['input' => [
  1738. 'type' => 'hidden',
  1739. 'name' => '_csrfToken',
  1740. 'value' => 'testKey',
  1741. ]],
  1742. '/div',
  1743. ];
  1744. $this->assertHtml($expected, $result);
  1745. $result = $this->Form->control('ratio');
  1746. $expected = [
  1747. 'div' => ['class'],
  1748. 'label' => ['for'],
  1749. 'Ratio',
  1750. '/label',
  1751. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1752. '/div',
  1753. ];
  1754. $this->assertHtml($expected, $result);
  1755. $result = $this->Form->control('population');
  1756. $expected = [
  1757. 'div' => ['class'],
  1758. 'label' => ['for'],
  1759. 'Population',
  1760. '/label',
  1761. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1762. '/div',
  1763. ];
  1764. $this->assertHtml($expected, $result);
  1765. $result = $this->Form->control('published', ['type' => 'text']);
  1766. $expected = [
  1767. 'div' => ['class' => 'input text'],
  1768. 'label' => ['for' => 'published'],
  1769. 'Published',
  1770. '/label',
  1771. ['input' => [
  1772. 'type' => 'text',
  1773. 'name' => 'published',
  1774. 'id' => 'published',
  1775. ]],
  1776. '/div',
  1777. ];
  1778. $this->assertHtml($expected, $result);
  1779. $result = $this->Form->control('other', ['type' => 'text']);
  1780. $expected = [
  1781. 'div' => ['class' => 'input text'],
  1782. 'label' => ['for' => 'other'],
  1783. 'Other',
  1784. '/label',
  1785. ['input' => [
  1786. 'type' => 'text',
  1787. 'name' => 'other',
  1788. 'id',
  1789. ]],
  1790. '/div',
  1791. ];
  1792. $this->assertHtml($expected, $result);
  1793. $result = $this->Form->hidden('stuff');
  1794. $expected = [
  1795. 'input' => [
  1796. 'type' => 'hidden',
  1797. 'name' => 'stuff',
  1798. ],
  1799. ];
  1800. $this->assertHtml($expected, $result);
  1801. $result = $this->Form->hidden('hidden', ['value' => false]);
  1802. $expected = ['input' => [
  1803. 'type' => 'hidden',
  1804. 'name' => 'hidden',
  1805. 'value' => '0',
  1806. ]];
  1807. $this->assertHtml($expected, $result);
  1808. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1809. $expected = [
  1810. 'div' => ['class' => 'input checkbox'],
  1811. ['input' => [
  1812. 'type' => 'hidden',
  1813. 'name' => 'something',
  1814. 'value' => '0',
  1815. ]],
  1816. 'label' => ['for' => 'something'],
  1817. ['input' => [
  1818. 'type' => 'checkbox',
  1819. 'name' => 'something',
  1820. 'value' => '1',
  1821. 'id' => 'something',
  1822. ]],
  1823. 'Something',
  1824. '/label',
  1825. '/div',
  1826. ];
  1827. $this->assertHtml($expected, $result);
  1828. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1829. $expectedFields = [
  1830. 'ratio',
  1831. 'population',
  1832. 'published',
  1833. 'other',
  1834. 'stuff' => '',
  1835. 'hidden' => '0',
  1836. 'something',
  1837. ];
  1838. $this->assertEquals($expectedFields, $result);
  1839. $result = $this->Form->secure();
  1840. $tokenDebug = urlencode(json_encode([
  1841. '/articles/add',
  1842. $expectedFields,
  1843. [],
  1844. ]));
  1845. $expected = [
  1846. 'div' => ['style' => 'display:none;'],
  1847. ['input' => [
  1848. 'type' => 'hidden',
  1849. 'name' => '_Token[fields]',
  1850. 'value',
  1851. ]],
  1852. ['input' => [
  1853. 'type' => 'hidden',
  1854. 'name' => '_Token[unlocked]',
  1855. 'value' => '',
  1856. ]],
  1857. ['input' => [
  1858. 'type' => 'hidden', 'name' => '_Token[debug]',
  1859. 'value' => $tokenDebug,
  1860. ]],
  1861. '/div',
  1862. ];
  1863. $this->assertHtml($expected, $result);
  1864. $data = [
  1865. 'ratio' => '',
  1866. 'population' => '',
  1867. 'published' => '',
  1868. 'other' => '',
  1869. 'stuff' => '',
  1870. 'hidden' => '0',
  1871. 'something' => '',
  1872. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1873. ];
  1874. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1875. }
  1876. /**
  1877. * testSecuredControlCustomName method
  1878. *
  1879. * Test secured inputs with custom names.
  1880. */
  1881. public function testSecuredControlCustomName(): void
  1882. {
  1883. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1884. $this->Form->create();
  1885. $this->Form->text('text_input', [
  1886. 'name' => 'Option[General.default_role]',
  1887. ]);
  1888. $expected = ['Option.General.default_role'];
  1889. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1890. $this->assertEquals($expected, $result);
  1891. $this->Form->select('select_box', [1, 2], [
  1892. 'name' => 'Option[General.select_role]',
  1893. ]);
  1894. $expected[] = 'Option.General.select_role';
  1895. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1896. $this->assertEquals($expected, $result);
  1897. $this->Form->text('other.things[]');
  1898. $expected[] = 'other.things';
  1899. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1900. $this->assertEquals($expected, $result);
  1901. }
  1902. /**
  1903. * testSecuredControlDuplicate method
  1904. *
  1905. * Test that a hidden field followed by a visible field
  1906. * undoes the hidden field locking.
  1907. */
  1908. public function testSecuredControlDuplicate(): void
  1909. {
  1910. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1911. $this->Form->create();
  1912. $this->Form->control('text_val', [
  1913. 'type' => 'hidden',
  1914. 'value' => 'some text',
  1915. ]);
  1916. $expected = ['text_val' => 'some text'];
  1917. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1918. $this->assertEquals($expected, $result);
  1919. $this->Form->control('text_val', [
  1920. 'type' => 'text',
  1921. ]);
  1922. $expected = ['text_val'];
  1923. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1924. $this->assertEquals($expected, $result);
  1925. }
  1926. /**
  1927. * testFormSecuredFileControl method
  1928. *
  1929. * Tests that the correct keys are added to the field hash index.
  1930. */
  1931. public function testFormSecuredFileControl(): void
  1932. {
  1933. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1934. $this->Form->create();
  1935. $this->Form->file('Attachment.file');
  1936. $expected = ['Attachment.file'];
  1937. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1938. $this->assertEquals($expected, $result);
  1939. }
  1940. /**
  1941. * testFormSecuredMultipleSelect method
  1942. *
  1943. * Test that multiple selects keys are added to field hash.
  1944. */
  1945. public function testFormSecuredMultipleSelect(): void
  1946. {
  1947. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1948. $this->Form->create();
  1949. $options = ['1' => 'one', '2' => 'two'];
  1950. $this->Form->select('Model.select', $options);
  1951. $expected = ['Model.select'];
  1952. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1953. $this->assertEquals($expected, $result);
  1954. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1955. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1956. $this->assertEquals($expected, $result);
  1957. }
  1958. /**
  1959. * testFormSecuredRadio method
  1960. */
  1961. public function testFormSecuredRadio(): void
  1962. {
  1963. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1964. $this->Form->create();
  1965. $options = ['1' => 'option1', '2' => 'option2'];
  1966. $this->Form->radio('Test.test', $options);
  1967. $expected = ['Test.test'];
  1968. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1969. $this->assertEquals($expected, $result);
  1970. $this->Form->radio('Test.all', $options, [
  1971. 'disabled' => ['option1', 'option2'],
  1972. ]);
  1973. $expected = ['Test.test', 'Test.all' => ''];
  1974. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1975. $this->assertEquals($expected, $result);
  1976. $this->Form->radio('Test.some', $options, [
  1977. 'disabled' => ['option1'],
  1978. ]);
  1979. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1980. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1981. $this->assertEquals($expected, $result);
  1982. }
  1983. /**
  1984. * testFormSecuredAndDisabled method
  1985. *
  1986. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  1987. * hashing.
  1988. */
  1989. public function testFormSecuredAndDisabled(): void
  1990. {
  1991. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1992. $this->Form->create();
  1993. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  1994. $this->Form->text('Model.text', ['disabled' => true]);
  1995. $this->Form->password('Model.text', ['disabled' => true]);
  1996. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  1997. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  1998. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  1999. $this->Form->year('Model.year', ['disabled' => true]);
  2000. $this->Form->month('Model.month', ['disabled' => true]);
  2001. $this->Form->day('Model.day', ['disabled' => true]);
  2002. $this->Form->hour('Model.hour', ['disabled' => true]);
  2003. $this->Form->minute('Model.minute', ['disabled' => true]);
  2004. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2005. $expected = [
  2006. 'Model.radio' => '',
  2007. ];
  2008. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2009. $this->assertEquals($expected, $result);
  2010. }
  2011. /**
  2012. * testUnlockFieldAddsToList method
  2013. *
  2014. * Test disableField.
  2015. */
  2016. public function testUnlockFieldAddsToList(): void
  2017. {
  2018. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2019. 'unlockedFields' => [],
  2020. ]));
  2021. $this->Form->create();
  2022. $this->Form->unlockField('Contact.name');
  2023. $this->Form->text('Contact.name');
  2024. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2025. $this->assertEquals([], $result);
  2026. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2027. $this->assertEquals(['Contact.name'], $result);
  2028. }
  2029. /**
  2030. * testUnlockFieldRemovingFromFields method
  2031. *
  2032. * Test unlockField removing from fields array.
  2033. */
  2034. public function testUnlockFieldRemovingFromFields(): void
  2035. {
  2036. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2037. 'unlockedFields' => [],
  2038. ]));
  2039. $this->Form->create($this->article);
  2040. $this->Form->hidden('Article.id', ['value' => 1]);
  2041. $this->Form->text('Article.title');
  2042. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2043. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2044. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2045. $this->Form->unlockField('Article.title');
  2046. $this->Form->unlockField('Article.id');
  2047. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2048. $this->assertEquals([], $result);
  2049. }
  2050. /**
  2051. * testResetUnlockFields method
  2052. *
  2053. * Test reset unlockFields, when create new form.
  2054. */
  2055. public function testResetUnlockFields(): void
  2056. {
  2057. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2058. 'key' => 'testKey',
  2059. 'unlockedFields' => [],
  2060. ]));
  2061. $this->Form->create();
  2062. $this->Form->unlockField('Contact.id');
  2063. $this->Form->hidden('Contact.id', ['value' => 1]);
  2064. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2065. $this->assertEmpty($result, 'Field should be unlocked');
  2066. $this->Form->end();
  2067. $this->Form->create();
  2068. $this->Form->hidden('Contact.id', ['value' => 1]);
  2069. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2070. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2071. }
  2072. /**
  2073. * testSecuredFormUrlIgnoresHost method
  2074. *
  2075. * Test that only the path + query elements of a form's URL show up in their hash.
  2076. */
  2077. public function testSecuredFormUrlIgnoresHost(): void
  2078. {
  2079. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2080. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2081. $this->Form->create($this->article, [
  2082. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2083. ]);
  2084. $result = $this->Form->secure();
  2085. $this->assertStringContainsString($expected, $result);
  2086. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2087. $result = $this->Form->secure();
  2088. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2089. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2090. $result = $this->Form->secure();
  2091. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2092. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2093. $result = $this->Form->secure();
  2094. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2095. }
  2096. /**
  2097. * testSecuredFormUrlHasHtmlAndIdentifier method
  2098. *
  2099. * Test that URL, HTML and identifier show up in their hashes.
  2100. */
  2101. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2102. {
  2103. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2104. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2105. $this->Form->create($this->article, [
  2106. 'url' => [
  2107. 'controller' => 'articles',
  2108. 'action' => 'view',
  2109. '?' => [
  2110. 'page' => 1,
  2111. 'limit' => 10,
  2112. 'html' => '<>"',
  2113. ],
  2114. '#' => 'result',
  2115. ],
  2116. ]);
  2117. $result = $this->Form->secure();
  2118. $this->assertStringContainsString($expected, $result);
  2119. $this->Form->create($this->article, [
  2120. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2121. ]);
  2122. $result = $this->Form->secure();
  2123. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2124. $this->Form->create($this->article, [
  2125. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2126. ]);
  2127. $result = $this->Form->secure();
  2128. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2129. }
  2130. /**
  2131. * testErrorMessageDisplay method
  2132. *
  2133. * Test error message display.
  2134. */
  2135. public function testErrorMessageDisplay(): void
  2136. {
  2137. $this->article['errors'] = [
  2138. 'Article' => [
  2139. 'title' => 'error message',
  2140. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2141. ],
  2142. ];
  2143. $this->Form->create($this->article);
  2144. $result = $this->Form->control('Article.title');
  2145. $expected = [
  2146. 'div' => ['class' => 'input text error'],
  2147. 'label' => ['for' => 'article-title'],
  2148. 'Title',
  2149. '/label',
  2150. 'input' => [
  2151. 'type' => 'text',
  2152. 'name' => 'Article[title]',
  2153. 'id' => 'article-title',
  2154. 'class' => 'form-error',
  2155. 'aria-invalid' => 'true',
  2156. 'aria-describedby' => 'article-title-error',
  2157. ],
  2158. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2159. 'error message',
  2160. '/div',
  2161. '/div',
  2162. ];
  2163. $this->assertHtml($expected, $result);
  2164. $result = $this->Form->control('Article.title', [
  2165. 'templates' => [
  2166. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2167. ],
  2168. ]);
  2169. $expected = [
  2170. 'div' => ['class' => 'input text error'],
  2171. 'label' => ['for' => 'article-title'],
  2172. 'Title',
  2173. '/label',
  2174. 'input' => [
  2175. 'type' => 'text',
  2176. 'name' => 'Article[title]',
  2177. 'id' => 'article-title',
  2178. 'class' => 'form-error',
  2179. // No aria-describedby because error template is custom
  2180. 'aria-invalid' => 'true',
  2181. ],
  2182. '/div',
  2183. ];
  2184. $this->assertHtml($expected, $result);
  2185. $result = $this->Form->control('Article.content');
  2186. $expected = [
  2187. 'div' => ['class' => 'input text error'],
  2188. 'label' => ['for' => 'article-content'],
  2189. 'Content',
  2190. '/label',
  2191. 'input' => [
  2192. 'type' => 'text',
  2193. 'name' => 'Article[content]',
  2194. 'id' => 'article-content',
  2195. 'class' => 'form-error',
  2196. 'aria-invalid' => 'true',
  2197. 'aria-describedby' => 'article-content-error',
  2198. ],
  2199. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2200. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2201. '/div',
  2202. '/div',
  2203. ];
  2204. $this->assertHtml($expected, $result);
  2205. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2206. $expected = [
  2207. 'div' => ['class' => 'input text error'],
  2208. 'label' => ['for' => 'article-content'],
  2209. 'Content',
  2210. '/label',
  2211. 'input' => [
  2212. 'type' => 'text',
  2213. 'name' => 'Article[content]',
  2214. 'id' => 'article-content',
  2215. 'class' => 'form-error',
  2216. 'aria-invalid' => 'true',
  2217. 'aria-describedby' => 'article-content-error',
  2218. ],
  2219. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2220. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2221. '/div',
  2222. '/div',
  2223. ];
  2224. $this->assertHtml($expected, $result);
  2225. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2226. $expected = [
  2227. 'div' => ['class' => 'input text error'],
  2228. 'label' => ['for' => 'article-content'],
  2229. 'Content',
  2230. '/label',
  2231. 'input' => [
  2232. 'type' => 'text',
  2233. 'name' => 'Article[content]',
  2234. 'id' => 'article-content',
  2235. 'class' => 'form-error',
  2236. 'aria-invalid' => 'true',
  2237. 'aria-describedby' => 'article-content-error',
  2238. ],
  2239. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2240. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2241. '/div',
  2242. '/div',
  2243. ];
  2244. $this->assertHtml($expected, $result);
  2245. }
  2246. /**
  2247. * testEmptyErrorValidation method
  2248. *
  2249. * Test validation errors, when validation message is an empty string.
  2250. */
  2251. public function testEmptyErrorValidation(): void
  2252. {
  2253. $this->article['errors'] = [
  2254. 'Article' => ['title' => ''],
  2255. ];
  2256. $this->Form->create($this->article);
  2257. $result = $this->Form->control('Article.title');
  2258. $expected = [
  2259. 'div' => ['class' => 'input text error'],
  2260. 'label' => ['for' => 'article-title'],
  2261. 'Title',
  2262. '/label',
  2263. 'input' => [
  2264. 'type' => 'text',
  2265. 'name' => 'Article[title]',
  2266. 'id' => 'article-title',
  2267. 'class' => 'form-error',
  2268. 'aria-invalid' => 'true',
  2269. 'aria-describedby' => 'article-title-error',
  2270. ],
  2271. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2272. [],
  2273. '/div',
  2274. '/div',
  2275. ];
  2276. $this->assertHtml($expected, $result);
  2277. }
  2278. /**
  2279. * testEmptyControlErrorValidation method
  2280. *
  2281. * Test validation errors, when calling control() overriding validation message by an empty string.
  2282. */
  2283. public function testEmptyControlErrorValidation(): void
  2284. {
  2285. $this->article['errors'] = [
  2286. 'Article' => ['title' => 'error message'],
  2287. ];
  2288. $this->Form->create($this->article);
  2289. $result = $this->Form->control('Article.title', ['error' => '']);
  2290. $expected = [
  2291. 'div' => ['class' => 'input text error'],
  2292. 'label' => ['for' => 'article-title'],
  2293. 'Title',
  2294. '/label',
  2295. 'input' => [
  2296. 'aria-invalid' => 'true',
  2297. 'aria-describedby' => 'article-title-error',
  2298. 'type' => 'text',
  2299. 'name' => 'Article[title]',
  2300. 'id' => 'article-title',
  2301. 'class' => 'form-error',
  2302. ],
  2303. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2304. [],
  2305. '/div',
  2306. '/div',
  2307. ];
  2308. $this->assertHtml($expected, $result);
  2309. }
  2310. /**
  2311. * testControlErrorMessage method
  2312. *
  2313. * Test validation errors, when calling control() overriding validation messages.
  2314. */
  2315. public function testControlErrorMessage(): void
  2316. {
  2317. $this->article['errors'] = [
  2318. 'title' => ['error message'],
  2319. ];
  2320. $this->Form->create($this->article);
  2321. $result = $this->Form->control('title', [
  2322. 'error' => 'Custom error!',
  2323. ]);
  2324. $expected = [
  2325. 'div' => ['class' => 'input text required error'],
  2326. 'label' => ['for' => 'title'],
  2327. 'Title',
  2328. '/label',
  2329. 'input' => [
  2330. 'type' => 'text',
  2331. 'name' => 'title',
  2332. 'id' => 'title',
  2333. 'class' => 'form-error',
  2334. 'required' => 'required',
  2335. 'data-validity-message' => 'This field cannot be left empty',
  2336. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2337. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2338. 'aria-required' => 'true',
  2339. 'aria-invalid' => 'true',
  2340. 'aria-describedby' => 'title-error',
  2341. ],
  2342. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2343. 'Custom error!',
  2344. '/div',
  2345. '/div',
  2346. ];
  2347. $this->assertHtml($expected, $result);
  2348. $result = $this->Form->control('title', [
  2349. 'error' => ['error message' => 'Custom error!'],
  2350. ]);
  2351. $expected = [
  2352. 'div' => ['class' => 'input text required error'],
  2353. 'label' => ['for' => 'title'],
  2354. 'Title',
  2355. '/label',
  2356. 'input' => [
  2357. 'type' => 'text',
  2358. 'name' => 'title',
  2359. 'id' => 'title',
  2360. 'aria-required' => 'true',
  2361. 'aria-invalid' => 'true',
  2362. 'aria-describedby' => 'title-error',
  2363. 'class' => 'form-error',
  2364. 'required' => 'required',
  2365. 'data-validity-message' => 'This field cannot be left empty',
  2366. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2367. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2368. ],
  2369. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2370. 'Custom error!',
  2371. '/div',
  2372. '/div',
  2373. ];
  2374. $this->assertHtml($expected, $result);
  2375. }
  2376. /**
  2377. * testFormValidationAssociated method
  2378. *
  2379. * Tests displaying errors for nested entities.
  2380. */
  2381. public function testFormValidationAssociated(): void
  2382. {
  2383. $nested = new Entity(['foo' => 'bar']);
  2384. $nested->setError('foo', ['not a valid bar']);
  2385. $entity = new Entity(['nested' => $nested]);
  2386. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2387. $result = $this->Form->error('nested.foo');
  2388. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2389. }
  2390. /**
  2391. * testFormValidationAssociatedSecondLevel method
  2392. *
  2393. * Test form error display with associated model.
  2394. */
  2395. public function testFormValidationAssociatedSecondLevel(): void
  2396. {
  2397. $inner = new Entity(['bar' => 'baz']);
  2398. $nested = new Entity(['foo' => $inner]);
  2399. $entity = new Entity(['nested' => $nested]);
  2400. $inner->setError('bar', ['not a valid one']);
  2401. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2402. $result = $this->Form->error('nested.foo.bar');
  2403. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2404. }
  2405. /**
  2406. * testFormValidationMultiRecord method
  2407. *
  2408. * Test form error display with multiple records.
  2409. */
  2410. public function testFormValidationMultiRecord(): void
  2411. {
  2412. $one = new Entity();
  2413. $two = new Entity();
  2414. $this->getTableLocator()->get('Contacts', [
  2415. 'className' => ContactsTable::class,
  2416. ]);
  2417. $one->set('email', '');
  2418. $one->setError('email', ['invalid email']);
  2419. $two->set('name', '');
  2420. $two->setError('name', ['This is wrong']);
  2421. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2422. $result = $this->Form->control('0.email');
  2423. $expected = [
  2424. 'div' => ['class' => 'input email error'],
  2425. 'label' => ['for' => '0-email'],
  2426. 'Email',
  2427. '/label',
  2428. 'input' => [
  2429. 'type' => 'email',
  2430. 'name' => '0[email]',
  2431. 'id' => '0-email',
  2432. 'class' => 'form-error',
  2433. 'maxlength' => 255,
  2434. 'value' => '',
  2435. 'aria-invalid' => 'true',
  2436. 'aria-describedby' => '0-email-error',
  2437. ],
  2438. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2439. 'invalid email',
  2440. '/div',
  2441. '/div',
  2442. ];
  2443. $this->assertHtml($expected, $result);
  2444. $result = $this->Form->control('1.name');
  2445. $expected = [
  2446. 'div' => ['class' => 'input text error'],
  2447. 'label' => ['for' => '1-name'],
  2448. 'Name',
  2449. '/label',
  2450. 'input' => [
  2451. 'type' => 'text',
  2452. 'name' => '1[name]',
  2453. 'id' => '1-name',
  2454. 'class' => 'form-error',
  2455. 'maxlength' => 255,
  2456. 'value' => '',
  2457. 'aria-invalid' => 'true',
  2458. 'aria-describedby' => '1-name-error',
  2459. ],
  2460. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2461. 'This is wrong',
  2462. '/div',
  2463. '/div',
  2464. ];
  2465. $this->assertHtml($expected, $result);
  2466. }
  2467. /**
  2468. * testControl method
  2469. *
  2470. * Test various incarnations of control().
  2471. */
  2472. public function testControl(): void
  2473. {
  2474. $this->getTableLocator()->get('ValidateUsers', [
  2475. 'className' => ValidateUsersTable::class,
  2476. ]);
  2477. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2478. $result = $this->Form->control('ValidateUsers.balance');
  2479. $expected = [
  2480. 'div' => ['class'],
  2481. 'label' => ['for'],
  2482. 'Balance',
  2483. '/label',
  2484. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2485. '/div',
  2486. ];
  2487. $this->assertHtml($expected, $result);
  2488. $result = $this->Form->control('ValidateUser.cost_decimal');
  2489. $expected = [
  2490. 'div' => ['class'],
  2491. 'label' => ['for'],
  2492. 'Cost Decimal',
  2493. '/label',
  2494. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2495. '/div',
  2496. ];
  2497. $this->assertHtml($expected, $result);
  2498. $result = $this->Form->control('ValidateUser.null_decimal');
  2499. $expected = [
  2500. 'div' => ['class'],
  2501. 'label' => ['for'],
  2502. 'Null Decimal',
  2503. '/label',
  2504. 'input' => ['name', 'type' => 'number', 'id'],
  2505. '/div',
  2506. ];
  2507. $this->assertHtml($expected, $result);
  2508. }
  2509. /**
  2510. * testControlCustomization method
  2511. *
  2512. * Tests the input method and passing custom options.
  2513. */
  2514. public function testControlCustomization(): void
  2515. {
  2516. $this->getTableLocator()->get('Contacts', [
  2517. 'className' => ContactsTable::class,
  2518. ]);
  2519. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2520. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2521. $expected = [
  2522. 'div' => ['class' => 'input email'],
  2523. 'label' => ['for' => 'custom'],
  2524. 'Email',
  2525. '/label',
  2526. ['input' => [
  2527. 'type' => 'email', 'name' => 'Contact[email]',
  2528. 'id' => 'custom', 'maxlength' => 255,
  2529. ]],
  2530. '/div',
  2531. ];
  2532. $this->assertHtml($expected, $result);
  2533. $result = $this->Form->control('Contact.email', [
  2534. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2535. ]);
  2536. $expected = [
  2537. '<div',
  2538. 'label' => ['for' => 'contact-email'],
  2539. 'Email',
  2540. '/label',
  2541. ['input' => [
  2542. 'type' => 'email', 'name' => 'Contact[email]',
  2543. 'id' => 'contact-email', 'maxlength' => 255,
  2544. ]],
  2545. '/div',
  2546. ];
  2547. $this->assertHtml($expected, $result);
  2548. $result = $this->Form->control('Contact.email', [
  2549. 'templates' => [
  2550. 'formGroup' => '{{input}}',
  2551. 'inputContainer' => '<div><div>{{label}}</div>{{content}}</div>',
  2552. ],
  2553. ]);
  2554. $expected = [
  2555. '<div',
  2556. '<div',
  2557. 'label' => ['for' => 'contact-email'],
  2558. 'Email',
  2559. '/label',
  2560. '/div',
  2561. ['input' => [
  2562. 'type' => 'email', 'name' => 'Contact[email]',
  2563. 'id' => 'contact-email', 'maxlength' => 255,
  2564. ]],
  2565. '/div',
  2566. ];
  2567. $this->assertHtml($expected, $result);
  2568. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2569. $expected = [
  2570. 'div' => ['class' => 'input text'],
  2571. 'label' => ['for' => 'contact-email'],
  2572. 'Email',
  2573. '/label',
  2574. ['input' => [
  2575. 'type' => 'text', 'name' => 'Contact[email]',
  2576. 'id' => 'contact-email', 'maxlength' => '255',
  2577. ]],
  2578. '/div',
  2579. ];
  2580. $this->assertHtml($expected, $result);
  2581. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2582. $expected = [
  2583. 'div' => ['class' => 'input text'],
  2584. 'label' => ['for' => 'contact-5-email'],
  2585. 'Email',
  2586. '/label',
  2587. ['input' => [
  2588. 'type' => 'text', 'name' => 'Contact[5][email]',
  2589. 'id' => 'contact-5-email', 'maxlength' => '255',
  2590. ]],
  2591. '/div',
  2592. ];
  2593. $this->assertHtml($expected, $result);
  2594. $result = $this->Form->control('Contact.password');
  2595. $expected = [
  2596. 'div' => ['class' => 'input password'],
  2597. 'label' => ['for' => 'contact-password'],
  2598. 'Password',
  2599. '/label',
  2600. ['input' => [
  2601. 'type' => 'password', 'name' => 'Contact[password]',
  2602. 'id' => 'contact-password',
  2603. ]],
  2604. '/div',
  2605. ];
  2606. $this->assertHtml($expected, $result);
  2607. $result = $this->Form->control('Contact.email', [
  2608. 'type' => 'file', 'class' => 'textbox',
  2609. ]);
  2610. $expected = [
  2611. 'div' => ['class' => 'input file'],
  2612. 'label' => ['for' => 'contact-email'],
  2613. 'Email',
  2614. '/label',
  2615. ['input' => [
  2616. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2617. 'id' => 'contact-email',
  2618. ]],
  2619. '/div',
  2620. ];
  2621. $this->assertHtml($expected, $result);
  2622. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2623. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2624. $result = $this->Form->control('phone');
  2625. $expected = [
  2626. 'div' => ['class' => 'input tel'],
  2627. 'label' => ['for' => 'phone'],
  2628. 'Phone',
  2629. '/label',
  2630. ['input' => [
  2631. 'type' => 'tel', 'name' => 'phone',
  2632. 'value' => 'Hello &amp; World &gt; weird chars',
  2633. 'id' => 'phone', 'maxlength' => 255,
  2634. ]],
  2635. '/div',
  2636. ];
  2637. $this->assertHtml($expected, $result);
  2638. $this->View->setRequest(
  2639. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2640. );
  2641. $this->Form->create();
  2642. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2643. $expected = [
  2644. 'div' => ['class' => 'input text'],
  2645. 'label' => ['for' => 'myId'],
  2646. 'Field',
  2647. '/label',
  2648. 'input' => [
  2649. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2650. 'value' => 'My value', 'id' => 'myId',
  2651. ],
  2652. '/div',
  2653. ];
  2654. $this->assertHtml($expected, $result);
  2655. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2656. $this->Form->create();
  2657. $entity->setError('field', 'Badness!');
  2658. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2659. $result = $this->Form->control('field');
  2660. $expected = [
  2661. 'div' => ['class' => 'input text error'],
  2662. 'label' => ['for' => 'field'],
  2663. 'Field',
  2664. '/label',
  2665. 'input' => [
  2666. 'type' => 'text',
  2667. 'name' => 'field',
  2668. 'id' => 'field',
  2669. 'class' => 'form-error',
  2670. 'aria-invalid' => 'true',
  2671. 'aria-describedby' => 'field-error',
  2672. ],
  2673. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2674. 'Badness!',
  2675. '/div',
  2676. '/div',
  2677. ];
  2678. $this->assertHtml($expected, $result);
  2679. $result = $this->Form->control('field', [
  2680. 'templates' => [
  2681. 'inputContainerError' => '{{content}}{{error}}',
  2682. 'error' => '<span class="error-message">{{content}}</span>',
  2683. ],
  2684. ]);
  2685. $expected = [
  2686. 'label' => ['for' => 'field'],
  2687. 'Field',
  2688. '/label',
  2689. 'input' => [
  2690. 'type' => 'text',
  2691. 'name' => 'field',
  2692. 'id' => 'field',
  2693. 'class' => 'form-error',
  2694. // No aria-describedby because error template is custom
  2695. 'aria-invalid' => 'true',
  2696. ],
  2697. ['span' => ['class' => 'error-message']],
  2698. 'Badness!',
  2699. '/span',
  2700. ];
  2701. $this->assertHtml($expected, $result);
  2702. $entity->setError('field', ['minLength'], true);
  2703. $result = $this->Form->control('field', [
  2704. 'error' => [
  2705. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2706. 'maxLength' => 'login too large',
  2707. ],
  2708. ]);
  2709. $expected = [
  2710. 'div' => ['class' => 'input text error'],
  2711. 'label' => ['for' => 'field'],
  2712. 'Field',
  2713. '/label',
  2714. 'input' => [
  2715. 'type' => 'text',
  2716. 'name' => 'field',
  2717. 'id' => 'field',
  2718. 'class' => 'form-error',
  2719. 'aria-invalid' => 'true',
  2720. 'aria-describedby' => 'field-error',
  2721. ],
  2722. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2723. 'Le login doit contenir au moins 2 caractères',
  2724. '/div',
  2725. '/div',
  2726. ];
  2727. $this->assertHtml($expected, $result);
  2728. $entity->setError('field', ['maxLength'], true);
  2729. $result = $this->Form->control('field', [
  2730. 'error' => [
  2731. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2732. 'maxLength' => 'login too large',
  2733. ],
  2734. ]);
  2735. $expected = [
  2736. 'div' => ['class' => 'input text error'],
  2737. 'label' => ['for' => 'field'],
  2738. 'Field',
  2739. '/label',
  2740. 'input' => [
  2741. 'type' => 'text',
  2742. 'name' => 'field',
  2743. 'id' => 'field',
  2744. 'class' => 'form-error',
  2745. 'aria-invalid' => 'true',
  2746. 'aria-describedby' => 'field-error',
  2747. ],
  2748. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2749. 'login too large',
  2750. '/div',
  2751. '/div',
  2752. ];
  2753. $this->assertHtml($expected, $result);
  2754. }
  2755. /**
  2756. * testControlWithTemplateFile method
  2757. *
  2758. * Test that control() accepts a template file.
  2759. */
  2760. public function testControlWithTemplateFile(): void
  2761. {
  2762. $result = $this->Form->control('field', [
  2763. 'templates' => 'htmlhelper_tags',
  2764. ]);
  2765. $expected = [
  2766. 'label' => ['for' => 'field'],
  2767. 'Field',
  2768. '/label',
  2769. 'input' => [
  2770. 'type' => 'text', 'name' => 'field',
  2771. 'id' => 'field',
  2772. ],
  2773. ];
  2774. $this->assertHtml($expected, $result);
  2775. }
  2776. /**
  2777. * testNestedControlsEndWithBrackets method
  2778. *
  2779. * Test that nested inputs end with brackets.
  2780. */
  2781. public function testNestedControlsEndWithBrackets(): void
  2782. {
  2783. $result = $this->Form->text('nested.text[]');
  2784. $expected = [
  2785. 'input' => [
  2786. 'type' => 'text', 'name' => 'nested[text][]',
  2787. ],
  2788. ];
  2789. $this->assertHtml($expected, $result);
  2790. $result = $this->Form->file('nested.file[]');
  2791. $expected = [
  2792. 'input' => [
  2793. 'type' => 'file', 'name' => 'nested[file][]',
  2794. ],
  2795. ];
  2796. $this->assertHtml($expected, $result);
  2797. }
  2798. /**
  2799. * testCreateIdPrefix method
  2800. *
  2801. * Test id prefix.
  2802. */
  2803. public function testCreateIdPrefix(): void
  2804. {
  2805. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2806. $result = $this->Form->control('field');
  2807. $expected = [
  2808. 'div' => ['class' => 'input text'],
  2809. 'label' => ['for' => 'prefix-field'],
  2810. 'Field',
  2811. '/label',
  2812. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2813. '/div',
  2814. ];
  2815. $this->assertHtml($expected, $result);
  2816. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2817. $expected = [
  2818. 'div' => ['class' => 'input text'],
  2819. 'label' => ['for' => 'custom-id'],
  2820. 'Field',
  2821. '/label',
  2822. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2823. '/div',
  2824. ];
  2825. $this->assertHtml($expected, $result);
  2826. $result = $this->Form->radio('Model.field', ['option A']);
  2827. $expected = [
  2828. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2829. 'label' => ['for' => 'prefix-model-field-0'],
  2830. ['input' => [
  2831. 'type' => 'radio',
  2832. 'name' => 'Model[field]',
  2833. 'value' => '0',
  2834. 'id' => 'prefix-model-field-0',
  2835. ]],
  2836. 'option A',
  2837. '/label',
  2838. ];
  2839. $this->assertHtml($expected, $result);
  2840. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2841. $expected = [
  2842. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2843. 'label' => ['for' => 'prefix-model-field-0'],
  2844. ['input' => [
  2845. 'type' => 'radio',
  2846. 'name' => 'Model[field]',
  2847. 'value' => '0',
  2848. 'id' => 'prefix-model-field-0',
  2849. ]],
  2850. 'option A',
  2851. '/label',
  2852. ];
  2853. $this->assertHtml($expected, $result);
  2854. $result = $this->Form->select(
  2855. 'Model.multi_field',
  2856. ['first'],
  2857. ['multiple' => 'checkbox']
  2858. );
  2859. $expected = [
  2860. 'input' => [
  2861. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'prefix-model-multi-field',
  2862. ],
  2863. ['div' => ['class' => 'checkbox']],
  2864. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2865. ['input' => [
  2866. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2867. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2868. ]],
  2869. 'first',
  2870. '/label',
  2871. '/div',
  2872. ];
  2873. $this->assertHtml($expected, $result);
  2874. $this->Form->end();
  2875. $result = $this->Form->control('field');
  2876. $expected = [
  2877. 'div' => ['class' => 'input text'],
  2878. 'label' => ['for' => 'field'],
  2879. 'Field',
  2880. '/label',
  2881. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2882. '/div',
  2883. ];
  2884. $this->assertHtml($expected, $result);
  2885. }
  2886. /**
  2887. * testControlZero method
  2888. *
  2889. * Test that inputs with 0 can be created.
  2890. */
  2891. public function testControlZero(): void
  2892. {
  2893. $this->getTableLocator()->get('Contacts', [
  2894. 'className' => ContactsTable::class,
  2895. ]);
  2896. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2897. $result = $this->Form->control('0');
  2898. $expected = [
  2899. 'div' => ['class' => 'input text'],
  2900. 'label' => ['for' => '0'], '/label',
  2901. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2902. '/div',
  2903. ];
  2904. $this->assertHtml($expected, $result);
  2905. }
  2906. /**
  2907. * testControlCheckbox method
  2908. *
  2909. * Test control() with checkbox creation.
  2910. */
  2911. public function testControlCheckbox(): void
  2912. {
  2913. $articles = $this->getTableLocator()->get('Articles');
  2914. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2915. $article = $articles->newEmptyEntity();
  2916. $this->Form->create($article);
  2917. $result = $this->Form->control('Articles.active');
  2918. $expected = [
  2919. 'div' => ['class' => 'input checkbox'],
  2920. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2921. 'label' => ['for' => 'articles-active'],
  2922. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2923. 'Active',
  2924. '/label',
  2925. '/div',
  2926. ];
  2927. $this->assertHtml($expected, $result);
  2928. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2929. $expected = [
  2930. 'div' => ['class' => 'input checkbox'],
  2931. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2932. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2933. '/div',
  2934. ];
  2935. $this->assertHtml($expected, $result);
  2936. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2937. $expected = [
  2938. 'div' => ['class' => 'input checkbox'],
  2939. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2940. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2941. '/div',
  2942. ];
  2943. $this->assertHtml($expected, $result);
  2944. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2945. $expected = [
  2946. 'div' => ['class' => 'input checkbox'],
  2947. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2948. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2949. '/div',
  2950. ];
  2951. $this->assertHtml($expected, $result);
  2952. $result = $this->Form->control('Articles.disabled', [
  2953. 'label' => 'Disabled',
  2954. 'type' => 'checkbox',
  2955. 'data-foo' => 'disabled',
  2956. ]);
  2957. $expected = [
  2958. 'div' => ['class' => 'input checkbox'],
  2959. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2960. 'label' => ['for' => 'articles-disabled'],
  2961. ['input' => [
  2962. 'type' => 'checkbox',
  2963. 'name' => 'Articles[disabled]',
  2964. 'value' => '1',
  2965. 'id' => 'articles-disabled',
  2966. 'data-foo' => 'disabled',
  2967. ]],
  2968. 'Disabled',
  2969. '/label',
  2970. '/div',
  2971. ];
  2972. $this->assertHtml($expected, $result);
  2973. $result = $this->Form->control('Articles.confirm', [
  2974. 'label' => 'Confirm <b>me</b>!',
  2975. 'type' => 'checkbox',
  2976. 'escape' => false,
  2977. ]);
  2978. $expected = [
  2979. 'div' => ['class' => 'input checkbox'],
  2980. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  2981. 'label' => ['for' => 'articles-confirm'],
  2982. ['input' => [
  2983. 'type' => 'checkbox',
  2984. 'name' => 'Articles[confirm]',
  2985. 'value' => '1',
  2986. 'id' => 'articles-confirm',
  2987. ]],
  2988. 'Confirm <b>me</b>!',
  2989. '/label',
  2990. '/div',
  2991. ];
  2992. $this->assertHtml($expected, $result);
  2993. }
  2994. /**
  2995. * testControlHidden method
  2996. *
  2997. * Test that control() does not create wrapping div and label tag for hidden fields.
  2998. */
  2999. public function testControlHidden(): void
  3000. {
  3001. $this->getTableLocator()->get('ValidateUsers', [
  3002. 'className' => ValidateUsersTable::class,
  3003. ]);
  3004. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3005. $result = $this->Form->control('ValidateUser.id');
  3006. $expected = [
  3007. 'input' => ['name', 'type' => 'hidden', 'id'],
  3008. ];
  3009. $this->assertHtml($expected, $result);
  3010. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3011. $expected = [
  3012. 'input' => ['name', 'type' => 'hidden', 'id'],
  3013. ];
  3014. $this->assertHtml($expected, $result);
  3015. }
  3016. /**
  3017. * testControlDatetime method
  3018. *
  3019. * Test form->control() with datetime.
  3020. */
  3021. public function testControlDatetime(): void
  3022. {
  3023. $result = $this->Form->control('prueba', [
  3024. 'type' => 'datetime',
  3025. 'value' => new DateTime('2019-09-27 02:52:43'),
  3026. ]);
  3027. $expected = [
  3028. 'div' => ['class' => 'input datetime'],
  3029. 'label' => ['for' => 'prueba'],
  3030. 'Prueba',
  3031. '/label',
  3032. 'input' => [
  3033. 'name' => 'prueba',
  3034. 'id' => 'prueba',
  3035. 'type' => 'datetime-local',
  3036. 'value' => '2019-09-27T02:52:43',
  3037. 'step' => '1',
  3038. ],
  3039. '/div',
  3040. ];
  3041. $this->assertHtml($expected, $result);
  3042. }
  3043. /**
  3044. * testControlDatetimeIdPrefix method
  3045. *
  3046. * Test form->control() with datetime with id prefix.
  3047. */
  3048. public function testControlDatetimeIdPrefix(): void
  3049. {
  3050. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3051. $result = $this->Form->control('prueba', [
  3052. 'type' => 'datetime',
  3053. ]);
  3054. $expected = [
  3055. 'div' => ['class' => 'input datetime'],
  3056. 'label' => ['for' => 'prefix-prueba'],
  3057. 'Prueba',
  3058. '/label',
  3059. 'input' => [
  3060. 'name' => 'prueba',
  3061. 'id' => 'prefix-prueba',
  3062. 'type' => 'datetime-local',
  3063. 'value' => '',
  3064. 'step' => '1',
  3065. ],
  3066. '/div',
  3067. ];
  3068. $this->assertHtml($expected, $result);
  3069. }
  3070. /**
  3071. * testControlDatetimeStep method
  3072. *
  3073. * Test form->control() with datetime with custom step size.
  3074. */
  3075. public function testControlDatetimeStep(): void
  3076. {
  3077. $result = $this->Form->control('prueba', [
  3078. 'type' => 'datetime',
  3079. 'value' => new DateTime('2019-09-27 02:52:43'),
  3080. 'step' => '0.5',
  3081. ]);
  3082. $expected = [
  3083. 'div' => ['class' => 'input datetime'],
  3084. 'label' => ['for' => 'prueba'],
  3085. 'Prueba',
  3086. '/label',
  3087. 'input' => [
  3088. 'name' => 'prueba',
  3089. 'id' => 'prueba',
  3090. 'type' => 'datetime-local',
  3091. 'value' => '2019-09-27T02:52:43.000',
  3092. 'step' => '0.5',
  3093. ],
  3094. '/div',
  3095. ];
  3096. $this->assertHtml($expected, $result);
  3097. }
  3098. /**
  3099. * testControlCheckboxWithDisabledElements method
  3100. *
  3101. * Test generating checkboxes with disabled elements.
  3102. */
  3103. public function testControlCheckboxWithDisabledElements(): void
  3104. {
  3105. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3106. $result = $this->Form->control('Contact.multiple', [
  3107. 'multiple' => 'checkbox',
  3108. 'disabled' => 'disabled',
  3109. 'options' => $options,
  3110. ]);
  3111. $expected = [
  3112. ['div' => ['class' => 'input select']],
  3113. ['label' => ['for' => 'contact-multiple']],
  3114. 'Multiple',
  3115. '/label',
  3116. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '', 'id' => 'contact-multiple']],
  3117. ['div' => ['class' => 'checkbox']],
  3118. ['label' => ['for' => 'contact-multiple-1']],
  3119. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3120. 'One',
  3121. '/label',
  3122. '/div',
  3123. ['div' => ['class' => 'checkbox']],
  3124. ['label' => ['for' => 'contact-multiple-2']],
  3125. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3126. 'Two',
  3127. '/label',
  3128. '/div',
  3129. ['div' => ['class' => 'checkbox']],
  3130. ['label' => ['for' => 'contact-multiple-3']],
  3131. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3132. 'Three',
  3133. '/label',
  3134. '/div',
  3135. '/div',
  3136. ];
  3137. $this->assertHtml($expected, $result);
  3138. // make sure 50 does only disable 50, and not 50f5c0cf
  3139. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3140. $disabled = [50];
  3141. $expected = [
  3142. ['div' => ['class' => 'input select']],
  3143. ['label' => ['for' => 'contact-multiple']],
  3144. 'Multiple',
  3145. '/label',
  3146. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'contact-multiple']],
  3147. ['div' => ['class' => 'checkbox']],
  3148. ['label' => ['for' => 'contact-multiple-50']],
  3149. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3150. 'Fifty',
  3151. '/label',
  3152. '/div',
  3153. ['div' => ['class' => 'checkbox']],
  3154. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3155. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3156. 'Stringy',
  3157. '/label',
  3158. '/div',
  3159. '/div',
  3160. ];
  3161. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3162. $this->assertHtml($expected, $result);
  3163. }
  3164. /**
  3165. * testControlWithLeadingInteger method
  3166. *
  3167. * Test input name with leading integer, ensure attributes are generated correctly.
  3168. */
  3169. public function testControlWithLeadingInteger(): void
  3170. {
  3171. $result = $this->Form->text('0.Node.title');
  3172. $expected = [
  3173. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3174. ];
  3175. $this->assertHtml($expected, $result);
  3176. }
  3177. /**
  3178. * testControlSelectType method
  3179. *
  3180. * Test form->control() with select type inputs.
  3181. */
  3182. public function testControlSelectType(): void
  3183. {
  3184. $result = $this->Form->control(
  3185. 'email',
  3186. [
  3187. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3188. );
  3189. $expected = [
  3190. 'div' => ['class' => 'input select'],
  3191. 'label' => ['for' => 'email'],
  3192. 'Email',
  3193. '/label',
  3194. ['select' => ['name' => 'email', 'id' => 'email']],
  3195. ['option' => ['value' => '']],
  3196. '/option',
  3197. ['option' => ['value' => 'è']],
  3198. 'Firést',
  3199. '/option',
  3200. ['option' => ['value' => 'é']],
  3201. 'Secoènd',
  3202. '/option',
  3203. '/select',
  3204. '/div',
  3205. ];
  3206. $this->assertHtml($expected, $result);
  3207. $result = $this->Form->control(
  3208. 'email',
  3209. [
  3210. 'options' => ['First', 'Second'], 'empty' => true]
  3211. );
  3212. $expected = [
  3213. 'div' => ['class' => 'input select'],
  3214. 'label' => ['for' => 'email'],
  3215. 'Email',
  3216. '/label',
  3217. ['select' => ['name' => 'email', 'id' => 'email']],
  3218. ['option' => ['value' => '']],
  3219. '/option',
  3220. ['option' => ['value' => '0']],
  3221. 'First',
  3222. '/option',
  3223. ['option' => ['value' => '1']],
  3224. 'Second',
  3225. '/option',
  3226. '/select',
  3227. '/div',
  3228. ];
  3229. $this->assertHtml($expected, $result);
  3230. $result = $this->Form->control('email', [
  3231. 'type' => 'select',
  3232. 'options' => new ArrayObject(['First', 'Second']),
  3233. 'empty' => true,
  3234. ]);
  3235. $this->assertHtml($expected, $result);
  3236. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3237. $this->View->setRequest(
  3238. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3239. );
  3240. $this->Form->create();
  3241. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3242. $expected = [
  3243. 'div' => ['class' => 'input select'],
  3244. 'label' => ['for' => 'model-user-id'],
  3245. 'User',
  3246. '/label',
  3247. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3248. ['option' => ['value' => '']],
  3249. '/option',
  3250. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3251. 'good',
  3252. '/option',
  3253. ['option' => ['value' => 'other']],
  3254. 'bad',
  3255. '/option',
  3256. '/select',
  3257. '/div',
  3258. ];
  3259. $this->assertHtml($expected, $result);
  3260. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3261. $this->View->setRequest(
  3262. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3263. );
  3264. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3265. $expected = [
  3266. 'div' => ['class' => 'input select'],
  3267. 'label' => ['for' => 'thing-user-id'],
  3268. 'User',
  3269. '/label',
  3270. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3271. ['option' => ['value' => '']],
  3272. 'Some Empty',
  3273. '/option',
  3274. ['option' => ['value' => 'value']],
  3275. 'good',
  3276. '/option',
  3277. ['option' => ['value' => 'other']],
  3278. 'bad',
  3279. '/option',
  3280. '/select',
  3281. '/div',
  3282. ];
  3283. $this->assertHtml($expected, $result);
  3284. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3285. $this->View->setRequest(
  3286. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3287. );
  3288. $this->Form->create();
  3289. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3290. $expected = [
  3291. 'div' => ['class' => 'input select'],
  3292. 'label' => ['for' => 'thing-user-id'],
  3293. 'User',
  3294. '/label',
  3295. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3296. ['option' => ['value' => '']],
  3297. 'Some Empty',
  3298. '/option',
  3299. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3300. 'good',
  3301. '/option',
  3302. ['option' => ['value' => 'other']],
  3303. 'bad',
  3304. '/option',
  3305. '/select',
  3306. '/div',
  3307. ];
  3308. $this->assertHtml($expected, $result);
  3309. $result = $this->Form->control('Publisher.id', [
  3310. 'label' => 'Publisher',
  3311. 'type' => 'select',
  3312. 'multiple' => 'checkbox',
  3313. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3314. ]);
  3315. $expected = [
  3316. ['div' => ['class' => 'input select']],
  3317. ['label' => ['for' => 'publisher-id']],
  3318. 'Publisher',
  3319. '/label',
  3320. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'publisher-id'],
  3321. ['div' => ['class' => 'checkbox']],
  3322. ['label' => ['for' => 'publisher-id-value-1']],
  3323. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3324. 'Label 1',
  3325. '/label',
  3326. '/div',
  3327. ['div' => ['class' => 'checkbox']],
  3328. ['label' => ['for' => 'publisher-id-value-2']],
  3329. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3330. 'Label 2',
  3331. '/label',
  3332. '/div',
  3333. '/div',
  3334. ];
  3335. $this->assertHtml($expected, $result);
  3336. $articlesTable = $this->getTableLocator()->get('Articles');
  3337. $articlesTable->getSchema()->setColumnType(
  3338. 'published',
  3339. EnumType::from(ArticleStatus::class)
  3340. );
  3341. $this->Form->create($articlesTable->newEmptyEntity());
  3342. $result = $this->Form->control('published');
  3343. $expected = [
  3344. 'div' => ['class' => 'input select'],
  3345. 'label' => ['for' => 'published'],
  3346. 'Published',
  3347. '/label',
  3348. 'select' => ['name' => 'published', 'id' => 'published'],
  3349. ['option' => ['value' => 'Y']],
  3350. 'PUBLISHED',
  3351. '/option',
  3352. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3353. 'UNPUBLISHED',
  3354. '/option',
  3355. '/select',
  3356. '/div',
  3357. ];
  3358. $this->assertHtml($expected, $result);
  3359. $articlesTable->getSchema()->setColumnType(
  3360. 'published',
  3361. EnumType::from(ArticleStatusLabel::class)
  3362. );
  3363. $this->Form->create($articlesTable->newEmptyEntity());
  3364. $result = $this->Form->control('published');
  3365. $expected = [
  3366. 'div' => ['class' => 'input select'],
  3367. 'label' => ['for' => 'published'],
  3368. 'Published',
  3369. '/label',
  3370. 'select' => ['name' => 'published', 'id' => 'published'],
  3371. ['option' => ['value' => 'Y']],
  3372. 'Is published',
  3373. '/option',
  3374. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3375. 'Is unpublished',
  3376. '/option',
  3377. '/select',
  3378. '/div',
  3379. ];
  3380. $this->assertHtml($expected, $result);
  3381. $articlesTable->getSchema()->setColumnType(
  3382. 'published',
  3383. EnumType::from(ArticleStatusLabelInterface::class)
  3384. );
  3385. $article = $articlesTable->newEmptyEntity();
  3386. $this->Form->create($article);
  3387. $result = $this->Form->control('published');
  3388. $expected = [
  3389. 'div' => ['class' => 'input select'],
  3390. 'label' => ['for' => 'published'],
  3391. 'Published',
  3392. '/label',
  3393. 'select' => ['name' => 'published', 'id' => 'published'],
  3394. ['option' => ['value' => 'Y']],
  3395. 'Is published',
  3396. '/option',
  3397. ['option' => ['value' => 'N', 'selected' => 'selected']],
  3398. 'Is unpublished',
  3399. '/option',
  3400. '/select',
  3401. '/div',
  3402. ];
  3403. $this->assertHtml($expected, $result);
  3404. }
  3405. /**
  3406. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3407. *
  3408. * Test that control() and a non standard primary key makes a hidden input by default.
  3409. */
  3410. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3411. {
  3412. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3413. $this->Form->create($this->article);
  3414. $result = $this->Form->control('title');
  3415. $expected = [
  3416. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3417. ];
  3418. $this->assertHtml($expected, $result);
  3419. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3420. $this->Form->create($this->article);
  3421. $result = $this->Form->control('title');
  3422. $expected = [
  3423. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3424. ];
  3425. $this->assertHtml($expected, $result);
  3426. $result = $this->Form->control('body');
  3427. $expected = [
  3428. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3429. ];
  3430. $this->assertHtml($expected, $result);
  3431. }
  3432. /**
  3433. * testControlOverridingMagicSelectType method
  3434. *
  3435. * Test that overriding the magic select type widget is possible.
  3436. */
  3437. public function testControlOverridingMagicSelectType(): void
  3438. {
  3439. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3440. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3441. $expected = [
  3442. 'div' => ['class' => 'input text'],
  3443. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3444. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3445. '/div',
  3446. ];
  3447. $this->assertHtml($expected, $result);
  3448. //Check that magic types still work for plural/singular vars
  3449. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3450. $result = $this->Form->control('Model.type');
  3451. $expected = [
  3452. 'div' => ['class' => 'input select'],
  3453. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3454. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3455. ['option' => ['value' => 'value']], 'good', '/option',
  3456. ['option' => ['value' => 'other']], 'bad', '/option',
  3457. '/select',
  3458. '/div',
  3459. ];
  3460. $this->assertHtml($expected, $result);
  3461. }
  3462. /**
  3463. * testControlMagicTypeDoesNotOverride method
  3464. *
  3465. * Test that inferred types do not override developer input.
  3466. */
  3467. public function testControlMagicTypeDoesNotOverride(): void
  3468. {
  3469. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3470. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3471. $expected = [
  3472. 'div' => ['class' => 'input checkbox'],
  3473. ['input' => [
  3474. 'type' => 'hidden',
  3475. 'name' => 'Model[user]',
  3476. 'value' => 0,
  3477. ]],
  3478. 'label' => ['for' => 'model-user'],
  3479. ['input' => [
  3480. 'name' => 'Model[user]',
  3481. 'type' => 'checkbox',
  3482. 'id' => 'model-user',
  3483. 'value' => 1,
  3484. ]],
  3485. 'User',
  3486. '/label',
  3487. '/div',
  3488. ];
  3489. $this->assertHtml($expected, $result);
  3490. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3491. $options = [
  3492. 1 => 'blue',
  3493. 2 => 'red',
  3494. ];
  3495. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3496. $expected = [
  3497. 'div' => ['class' => 'input select'],
  3498. 'label' => ['for' => 'tags-ids'],
  3499. 'Tags',
  3500. '/label',
  3501. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => '', 'id' => 'tags-ids'],
  3502. ['div' => ['class' => 'checkbox']],
  3503. ['label' => ['for' => 'tags-ids-1']],
  3504. ['input' => [
  3505. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3506. 'value' => '1', 'name' => 'tags[_ids][]',
  3507. ]],
  3508. 'blue',
  3509. '/label',
  3510. '/div',
  3511. ['div' => ['class' => 'checkbox']],
  3512. ['label' => ['for' => 'tags-ids-2']],
  3513. ['input' => [
  3514. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3515. 'value' => '2', 'name' => 'tags[_ids][]',
  3516. ]],
  3517. 'red',
  3518. '/label',
  3519. '/div',
  3520. '/div',
  3521. ];
  3522. $this->assertHtml($expected, $result);
  3523. }
  3524. /**
  3525. * testControlMagicSelectForTypeNumber method
  3526. *
  3527. * Test that magic control() selects are created for type=number.
  3528. */
  3529. public function testControlMagicSelectForTypeNumber(): void
  3530. {
  3531. $this->getTableLocator()->get('ValidateUsers', [
  3532. 'className' => ValidateUsersTable::class,
  3533. ]);
  3534. $entity = new Entity(['balance' => 1]);
  3535. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3536. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3537. $result = $this->Form->control('balance');
  3538. $expected = [
  3539. 'div' => ['class' => 'input select'],
  3540. 'label' => ['for' => 'balance'],
  3541. 'Balance',
  3542. '/label',
  3543. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3544. ['option' => ['value' => '0']],
  3545. 'nothing',
  3546. '/option',
  3547. ['option' => ['value' => '1', 'selected' => 'selected']],
  3548. 'some',
  3549. '/option',
  3550. ['option' => ['value' => '100']],
  3551. 'a lot',
  3552. '/option',
  3553. '/select',
  3554. '/div',
  3555. ];
  3556. $this->assertHtml($expected, $result);
  3557. }
  3558. /**
  3559. * testInvalidControlTypeOption method
  3560. *
  3561. * Test invalid 'input' type option to control() function.
  3562. */
  3563. public function testInvalidControlTypeOption(): void
  3564. {
  3565. $this->expectException(InvalidArgumentException::class);
  3566. $this->expectExceptionMessage('Invalid type `input` used for field `text`.');
  3567. $this->Form->control('text', ['type' => 'input']);
  3568. }
  3569. /**
  3570. * testControlMagicSelectChangeToRadio method
  3571. *
  3572. * Test that magic control() selects can easily be converted into radio types without error.
  3573. */
  3574. public function testControlMagicSelectChangeToRadio(): void
  3575. {
  3576. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3577. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3578. $this->assertStringContainsString('input type="radio"', $result);
  3579. }
  3580. /**
  3581. * testFormControlSubmit method
  3582. *
  3583. * Test correct results for form::control() and type submit.
  3584. */
  3585. public function testFormControlSubmit(): void
  3586. {
  3587. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3588. $expected = [
  3589. 'div' => ['class' => 'submit'],
  3590. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3591. '/div',
  3592. ];
  3593. $this->assertHtml($expected, $result);
  3594. }
  3595. /**
  3596. * testFormControls method
  3597. *
  3598. * Test correct results from Form::controls().
  3599. */
  3600. public function testFormControlsLegendFieldset(): void
  3601. {
  3602. $this->Form->create($this->article);
  3603. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3604. $expected = [
  3605. '<fieldset',
  3606. '<legend',
  3607. 'The Legend',
  3608. '/legend',
  3609. '*/fieldset',
  3610. ];
  3611. $this->assertHtml($expected, $result);
  3612. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3613. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3614. $this->assertStringContainsString('<fieldset>', $result);
  3615. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3616. $this->assertStringNotContainsString('<legend>', $result);
  3617. $this->assertStringNotContainsString('<fieldset>', $result);
  3618. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3619. $this->assertStringNotContainsString('<legend>', $result);
  3620. $this->assertStringNotContainsString('<fieldset>', $result);
  3621. $this->Form->create($this->article);
  3622. $this->View->setRequest($this->View->getRequest()
  3623. ->withParam('prefix', 'admin')
  3624. ->withParam('action', 'admin_edit')
  3625. ->withParam('controller', 'articles'));
  3626. $result = $this->Form->allControls();
  3627. $expected = [
  3628. '<fieldset',
  3629. '<legend',
  3630. 'New Article',
  3631. '/legend',
  3632. '*/fieldset',
  3633. ];
  3634. $this->assertHtml($expected, $result);
  3635. $this->Form->create($this->article);
  3636. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3637. $expected = [
  3638. '<fieldset',
  3639. '<legend',
  3640. 'The Legend',
  3641. '/legend',
  3642. '*/fieldset',
  3643. ];
  3644. $this->assertHtml($expected, $result);
  3645. $this->Form->create($this->article);
  3646. $result = $this->Form->allControls([], [
  3647. 'fieldset' => [
  3648. 'class' => 'some-class some-other-class',
  3649. 'disabled' => true,
  3650. 'data-param' => 'a-param',
  3651. ],
  3652. 'legend' => 'The Legend',
  3653. ]);
  3654. $expected = [
  3655. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3656. '<legend',
  3657. 'The Legend',
  3658. '/legend',
  3659. '*/fieldset',
  3660. ];
  3661. $this->assertHtml($expected, $result);
  3662. }
  3663. /**
  3664. * testFormControls method
  3665. *
  3666. * Test the controls() method.
  3667. */
  3668. public function testFormControls(): void
  3669. {
  3670. $this->Form->create($this->article);
  3671. $result = $this->Form->allControls();
  3672. $expected = [
  3673. '<fieldset',
  3674. '<legend', 'New Article', '/legend',
  3675. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3676. ['div' => ['class' => 'input select required']],
  3677. '*/div',
  3678. ['div' => ['class' => 'input text required']],
  3679. '*/div',
  3680. ['div' => ['class' => 'input text']],
  3681. '*/div',
  3682. ['div' => ['class' => 'input text']],
  3683. '*/div',
  3684. '/fieldset',
  3685. ];
  3686. $this->assertHtml($expected, $result);
  3687. $result = $this->Form->allControls([
  3688. 'published' => ['type' => 'boolean'],
  3689. ]);
  3690. $expected = [
  3691. '<fieldset',
  3692. '<legend', 'New Article', '/legend',
  3693. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3694. ['div' => ['class' => 'input select required']],
  3695. '*/div',
  3696. ['div' => ['class' => 'input text required']],
  3697. '*/div',
  3698. ['div' => ['class' => 'input text']],
  3699. '*/div',
  3700. ['div' => ['class' => 'input boolean']],
  3701. '*/div',
  3702. '/fieldset',
  3703. ];
  3704. $this->assertHtml($expected, $result);
  3705. $this->Form->create($this->article);
  3706. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3707. $expected = [
  3708. 'fieldset' => [],
  3709. 'legend' => [],
  3710. 'Hello',
  3711. '/legend',
  3712. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3713. ['div' => ['class' => 'input select required']],
  3714. '*/div',
  3715. ['div' => ['class' => 'input text required']],
  3716. '*/div',
  3717. ['div' => ['class' => 'input text']],
  3718. '*/div',
  3719. ['div' => ['class' => 'input text']],
  3720. '*/div',
  3721. '/fieldset',
  3722. ];
  3723. $this->assertHtml($expected, $result);
  3724. $this->Form->create();
  3725. $expected = [
  3726. 'fieldset' => [],
  3727. ['div' => ['class' => 'input text']],
  3728. 'label' => ['for' => 'foo'],
  3729. 'Foo',
  3730. '/label',
  3731. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3732. '*/div',
  3733. '/fieldset',
  3734. ];
  3735. $result = $this->Form->allControls(
  3736. ['foo' => ['type' => 'text']],
  3737. ['legend' => false]
  3738. );
  3739. $this->assertHtml($expected, $result);
  3740. }
  3741. /**
  3742. * testFormControlsBlacklist method
  3743. */
  3744. public function testFormControlsBlacklist(): void
  3745. {
  3746. $this->Form->create($this->article);
  3747. $result = $this->Form->allControls([
  3748. 'id' => false,
  3749. ]);
  3750. $expected = [
  3751. '<fieldset',
  3752. '<legend', 'New Article', '/legend',
  3753. ['div' => ['class' => 'input select required']],
  3754. '*/div',
  3755. ['div' => ['class' => 'input text required']],
  3756. '*/div',
  3757. ['div' => ['class' => 'input text']],
  3758. '*/div',
  3759. ['div' => ['class' => 'input text']],
  3760. '*/div',
  3761. '/fieldset',
  3762. ];
  3763. $this->assertHtml($expected, $result);
  3764. $this->Form->create($this->article);
  3765. $result = $this->Form->allControls([
  3766. 'id' => [],
  3767. ]);
  3768. $expected = [
  3769. '<fieldset',
  3770. '<legend', 'New Article', '/legend',
  3771. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3772. ['div' => ['class' => 'input select required']],
  3773. '*/div',
  3774. ['div' => ['class' => 'input text required']],
  3775. '*/div',
  3776. ['div' => ['class' => 'input text']],
  3777. '*/div',
  3778. ['div' => ['class' => 'input text']],
  3779. '*/div',
  3780. '/fieldset',
  3781. ];
  3782. $this->assertHtml($expected, $result, true);
  3783. }
  3784. /**
  3785. * testSelectAsCheckbox method
  3786. *
  3787. * Test multi-select widget with checkbox formatting.
  3788. */
  3789. public function testSelectAsCheckbox(): void
  3790. {
  3791. $result = $this->Form->select(
  3792. 'Model.multi_field',
  3793. ['first', 'second', 'third'],
  3794. ['multiple' => 'checkbox', 'value' => [0, 1]]
  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-0', 'class' => 'selected']],
  3800. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3801. 'first',
  3802. '/label',
  3803. '/div',
  3804. ['div' => ['class' => 'checkbox']],
  3805. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3806. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3807. 'second',
  3808. '/label',
  3809. '/div',
  3810. ['div' => ['class' => 'checkbox']],
  3811. ['label' => ['for' => 'model-multi-field-2']],
  3812. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3813. 'third',
  3814. '/label',
  3815. '/div',
  3816. ];
  3817. $this->assertHtml($expected, $result);
  3818. $result = $this->Form->select(
  3819. 'Model.multi_field',
  3820. ['1/2' => 'half'],
  3821. ['multiple' => 'checkbox']
  3822. );
  3823. $expected = [
  3824. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3825. ['div' => ['class' => 'checkbox']],
  3826. ['label' => ['for' => 'model-multi-field-1-2']],
  3827. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3828. 'half',
  3829. '/label',
  3830. '/div',
  3831. ];
  3832. $this->assertHtml($expected, $result);
  3833. }
  3834. /**
  3835. * testLabel method
  3836. *
  3837. * Test label generation.
  3838. */
  3839. public function testLabel(): void
  3840. {
  3841. $result = $this->Form->label('Person.name');
  3842. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3843. $this->assertHtml($expected, $result);
  3844. $result = $this->Form->label('Person.name');
  3845. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3846. $this->assertHtml($expected, $result);
  3847. $result = $this->Form->label('Person.first_name');
  3848. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3849. $this->assertHtml($expected, $result);
  3850. $result = $this->Form->label('Person.first_name', 'Your first name');
  3851. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3852. $this->assertHtml($expected, $result);
  3853. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3854. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3855. $this->assertHtml($expected, $result);
  3856. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3857. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3858. $this->assertHtml($expected, $result);
  3859. $result = $this->Form->label('Person.first_name', '');
  3860. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3861. $this->assertHtml($expected, $result);
  3862. $result = $this->Form->label('Person.2.name', '');
  3863. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3864. $this->assertHtml($expected, $result);
  3865. }
  3866. /**
  3867. * testLabelContainControl method
  3868. *
  3869. * Test that label() can accept an input with the correct template vars.
  3870. */
  3871. public function testLabelContainControl(): void
  3872. {
  3873. $this->Form->setTemplates([
  3874. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3875. ]);
  3876. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3877. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3878. ]);
  3879. $expected = [
  3880. 'label' => ['for' => 'person-accept-terms'],
  3881. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3882. 'Accept',
  3883. '/label',
  3884. ];
  3885. $this->assertHtml($expected, $result);
  3886. }
  3887. /**
  3888. * testTextbox method
  3889. *
  3890. * Test textbox element generation.
  3891. */
  3892. public function testTextbox(): void
  3893. {
  3894. $result = $this->Form->text('Model.field');
  3895. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3896. $this->assertHtml($expected, $result);
  3897. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3898. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3899. $this->assertHtml($expected, $result);
  3900. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3901. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3902. $this->assertHtml($expected, $result);
  3903. }
  3904. /**
  3905. * testTextBoxDataAndError method
  3906. *
  3907. * Test that text() hooks up with request data and error fields.
  3908. */
  3909. public function testTextBoxDataAndError(): void
  3910. {
  3911. $this->article['errors'] = [
  3912. 'Contact' => ['text' => 'wrong'],
  3913. ];
  3914. $this->View->setRequest($this->View->getRequest()
  3915. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3916. ->withData('Contact.text', 'test'));
  3917. $this->Form->create($this->article);
  3918. $result = $this->Form->text('Model.text');
  3919. $expected = [
  3920. 'input' => [
  3921. 'type' => 'text',
  3922. 'name' => 'Model[text]',
  3923. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3924. ],
  3925. ];
  3926. $this->assertHtml($expected, $result);
  3927. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3928. $expected = [
  3929. 'input' => [
  3930. 'type' => 'text',
  3931. 'name' => 'Contact[text]',
  3932. 'value' => 'test',
  3933. 'id' => 'theID',
  3934. 'class' => 'form-error',
  3935. ],
  3936. ];
  3937. $this->assertHtml($expected, $result);
  3938. }
  3939. /**
  3940. * testDefaultValue method
  3941. *
  3942. * Test default value setting.
  3943. */
  3944. public function testTextDefaultValue(): void
  3945. {
  3946. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3947. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3948. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3949. $this->assertHtml($expected, $result);
  3950. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3951. $this->Form->create();
  3952. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3953. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3954. $this->assertHtml($expected, $result);
  3955. $Articles = $this->getTableLocator()->get('Articles');
  3956. $title = $Articles->getSchema()->getColumn('title');
  3957. $Articles->getSchema()->addColumn(
  3958. 'title',
  3959. ['default' => 'default title', 'length' => 255] + $title
  3960. );
  3961. $entity = $Articles->newEmptyEntity();
  3962. $this->Form->create($entity);
  3963. // Get default value from schema
  3964. $result = $this->Form->text('title');
  3965. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3966. $this->assertHtml($expected, $result);
  3967. // Don't get value from schema
  3968. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3969. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3970. $this->assertHtml($expected, $result);
  3971. // Custom default value overrides default value from schema
  3972. $result = $this->Form->text('title', ['default' => 'override default']);
  3973. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  3974. $this->assertHtml($expected, $result);
  3975. // Default value from schema is used only for new entities.
  3976. $entity->setNew(false);
  3977. $result = $this->Form->text('title');
  3978. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3979. $this->assertHtml($expected, $result);
  3980. }
  3981. /**
  3982. * testError method
  3983. *
  3984. * Test field error generation.
  3985. */
  3986. public function testError(): void
  3987. {
  3988. $this->article['errors'] = [
  3989. 'Article' => ['field' => 'email'],
  3990. ];
  3991. $this->Form->create($this->article);
  3992. $result = $this->Form->error('Article.field');
  3993. $expected = [
  3994. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3995. 'email',
  3996. '/div',
  3997. ];
  3998. $this->assertHtml($expected, $result);
  3999. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4000. $expected = [
  4001. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4002. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4003. '/div',
  4004. ];
  4005. $this->assertHtml($expected, $result);
  4006. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4007. $expected = [
  4008. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4009. '<strong', 'Badness!', '/strong',
  4010. '/div',
  4011. ];
  4012. $this->assertHtml($expected, $result);
  4013. }
  4014. /**
  4015. * testErrorRuleName method
  4016. *
  4017. * Test error translation can use rule names for translating.
  4018. */
  4019. public function testErrorRuleName(): void
  4020. {
  4021. $this->article['errors'] = [
  4022. 'Article' => [
  4023. 'field' => ['email' => 'Your email was not good'],
  4024. ],
  4025. ];
  4026. $this->Form->create($this->article);
  4027. $result = $this->Form->error('Article.field');
  4028. $expected = [
  4029. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4030. 'Your email was not good',
  4031. '/div',
  4032. ];
  4033. $this->assertHtml($expected, $result);
  4034. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4035. $expected = [
  4036. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4037. 'Email in use',
  4038. '/div',
  4039. ];
  4040. $this->assertHtml($expected, $result);
  4041. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4042. $expected = [
  4043. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4044. 'Email in use',
  4045. '/div',
  4046. ];
  4047. $this->assertHtml($expected, $result);
  4048. $result = $this->Form->error('Article.field', [
  4049. 'email' => 'Key is preferred',
  4050. 'Your email was not good' => 'Email in use',
  4051. ]);
  4052. $expected = [
  4053. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  4054. 'Key is preferred',
  4055. '/div',
  4056. ];
  4057. $this->assertHtml($expected, $result);
  4058. }
  4059. /**
  4060. * testErrorMessages method
  4061. *
  4062. * Test error with nested lists.
  4063. */
  4064. public function testErrorMessages(): void
  4065. {
  4066. $this->article['errors'] = [
  4067. 'Article' => ['field' => 'email'],
  4068. ];
  4069. $this->Form->create($this->article);
  4070. $result = $this->Form->error('Article.field', [
  4071. 'email' => 'No good!',
  4072. ]);
  4073. $expected = [
  4074. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  4075. 'No good!',
  4076. '/div',
  4077. ];
  4078. $this->assertHtml($expected, $result);
  4079. }
  4080. /**
  4081. * testErrorMultipleMessages method
  4082. *
  4083. * Test error() with multiple messages.
  4084. */
  4085. public function testErrorMultipleMessages(): void
  4086. {
  4087. $this->article['errors'] = [
  4088. 'field' => ['notBlank', 'email', 'Something else'],
  4089. ];
  4090. $this->Form->create($this->article);
  4091. $result = $this->Form->error('field', [
  4092. 'notBlank' => 'Cannot be empty',
  4093. 'email' => 'No good!',
  4094. ]);
  4095. $expected = [
  4096. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  4097. 'ul' => [],
  4098. '<li', 'Cannot be empty', '/li',
  4099. '<li', 'No good!', '/li',
  4100. '<li', 'Something else', '/li',
  4101. '/ul',
  4102. '/div',
  4103. ];
  4104. $this->assertHtml($expected, $result);
  4105. }
  4106. /**
  4107. * testPassword method
  4108. *
  4109. * Test password element generation.
  4110. */
  4111. public function testPassword(): void
  4112. {
  4113. $this->article['errors'] = [
  4114. 'Contact' => [
  4115. 'passwd' => 1,
  4116. ],
  4117. ];
  4118. $this->Form->create($this->article);
  4119. $result = $this->Form->password('Contact.field');
  4120. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4121. $this->assertHtml($expected, $result);
  4122. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4123. $this->Form->create($this->article);
  4124. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4125. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4126. $this->assertHtml($expected, $result);
  4127. }
  4128. /**
  4129. * testRadio method
  4130. *
  4131. * Test radio element set generation.
  4132. */
  4133. public function testRadio(): void
  4134. {
  4135. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4136. $expected = [
  4137. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4138. ['label' => ['for' => 'model-field-0']],
  4139. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4140. 'option A',
  4141. '/label',
  4142. ['label' => ['for' => 'model-field-1']],
  4143. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4144. 'option B',
  4145. '/label',
  4146. ];
  4147. $this->assertHtml($expected, $result);
  4148. $result = $this->Form->radio('Model.field', new Collection(['option A', 'option B']));
  4149. $this->assertHtml($expected, $result);
  4150. $result = $this->Form->radio(
  4151. 'Employee.vegetarian',
  4152. ['yes' => 'Yes', 'no' => 'No'],
  4153. ['form' => 'my-form', 'id' => 'id-veg']
  4154. );
  4155. $expected = [
  4156. 'input' => ['type' => 'hidden', 'name' => 'Employee[vegetarian]', 'value' => '', 'form' => 'my-form', 'id' => 'id-veg'],
  4157. ['label' => ['for' => 'id-veg-yes']],
  4158. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'yes', 'id' => 'id-veg-yes', 'form' => 'my-form']],
  4159. 'Yes',
  4160. '/label',
  4161. ['label' => ['for' => 'id-veg-no']],
  4162. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'no', 'id' => 'id-veg-no', 'form' => 'my-form']],
  4163. 'No',
  4164. '/label',
  4165. ];
  4166. $this->assertHtml($expected, $result);
  4167. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4168. $expected = [
  4169. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'model-field']],
  4170. ['label' => ['for' => 'model-custom-0']],
  4171. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4172. 'option A',
  4173. '/label',
  4174. ['label' => ['for' => 'model-custom-1']],
  4175. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4176. 'option B',
  4177. '/label',
  4178. ];
  4179. $this->assertHtml($expected, $result);
  4180. $result = $this->Form->radio(
  4181. 'Employee.gender',
  4182. [
  4183. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4184. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4185. ]
  4186. );
  4187. $expected = [
  4188. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'employee-gender'],
  4189. ['label' => ['for' => 'employee-gender-male']],
  4190. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4191. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4192. 'Male',
  4193. '/label',
  4194. ['label' => ['for' => 'employee-gender-female']],
  4195. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4196. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4197. 'Female',
  4198. '/label',
  4199. ];
  4200. $this->assertHtml($expected, $result);
  4201. $article = new Article([
  4202. 'status' => ArticleStatus::UNPUBLISHED,
  4203. ]);
  4204. $this->Form->create($article);
  4205. $result = $this->Form->radio('status', ['Y' => 'Published', 'N' => 'Unpublished']);
  4206. $expected = [
  4207. 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => '', 'id' => 'status'],
  4208. ['label' => ['for' => 'status-y']],
  4209. ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'Y', 'id' => 'status-y']],
  4210. 'Published',
  4211. '/label',
  4212. ['label' => ['for' => 'status-n']],
  4213. ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'N', 'id' => 'status-n', 'checked' => 'checked']],
  4214. 'Unpublished',
  4215. '/label',
  4216. ];
  4217. $this->assertHtml($expected, $result);
  4218. }
  4219. /**
  4220. * Test radio with complex options and empty disabled data.
  4221. */
  4222. public function testRadioComplexDisabled(): void
  4223. {
  4224. $options = [
  4225. ['value' => 'r', 'text' => 'red'],
  4226. ['value' => 'b', 'text' => 'blue'],
  4227. ];
  4228. $attrs = ['disabled' => []];
  4229. $result = $this->Form->radio('Model.field', $options, $attrs);
  4230. $expected = [
  4231. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4232. ['label' => ['for' => 'model-field-r']],
  4233. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4234. 'red',
  4235. '/label',
  4236. ['label' => ['for' => 'model-field-b']],
  4237. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4238. 'blue',
  4239. '/label',
  4240. ];
  4241. $this->assertHtml($expected, $result);
  4242. $attrs = ['disabled' => ['r']];
  4243. $result = $this->Form->radio('Model.field', $options, $attrs);
  4244. $this->assertStringContainsString('disabled="disabled"', $result);
  4245. }
  4246. /**
  4247. * testRadioDefaultValue method
  4248. *
  4249. * Test default value setting on radio() method.
  4250. */
  4251. public function testRadioDefaultValue(): void
  4252. {
  4253. $Articles = $this->getTableLocator()->get('Articles');
  4254. $title = $Articles->getSchema()->getColumn('title');
  4255. $Articles->getSchema()->addColumn(
  4256. 'title',
  4257. ['default' => '1'] + $title
  4258. );
  4259. $this->Form->create($Articles->newEmptyEntity());
  4260. $result = $this->Form->radio('title', ['option A', 'option B']);
  4261. $expected = [
  4262. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4263. ['label' => ['for' => 'title-0']],
  4264. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4265. 'option A',
  4266. '/label',
  4267. ['label' => ['for' => 'title-1']],
  4268. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4269. 'option B',
  4270. '/label',
  4271. ];
  4272. $this->assertHtml($expected, $result);
  4273. }
  4274. /**
  4275. * Test setting a hiddenField value on radio buttons.
  4276. */
  4277. public function testRadioHiddenFieldValue(): void
  4278. {
  4279. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4280. $expected = [
  4281. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N', 'id' => 'title']],
  4282. 'label' => ['for' => 'title-0'],
  4283. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4284. 'option A',
  4285. '/label',
  4286. ];
  4287. $this->assertHtml($expected, $result);
  4288. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
  4289. $expected = [
  4290. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4291. 'label' => ['for' => 'title-0'],
  4292. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4293. 'option A',
  4294. '/label',
  4295. ];
  4296. $this->assertHtml($expected, $result);
  4297. }
  4298. /**
  4299. * testControlRadio method
  4300. *
  4301. * Test that input works with radio types.
  4302. */
  4303. public function testControlRadio(): void
  4304. {
  4305. $result = $this->Form->control('test', [
  4306. 'type' => 'radio',
  4307. 'options' => ['A', 'B'],
  4308. ]);
  4309. $expected = [
  4310. ['div' => ['class' => 'input radio']],
  4311. '<label',
  4312. 'Test',
  4313. '/label',
  4314. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4315. ['label' => ['for' => 'test-0']],
  4316. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4317. 'A',
  4318. '/label',
  4319. ['label' => ['for' => 'test-1']],
  4320. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4321. 'B',
  4322. '/label',
  4323. '/div',
  4324. ];
  4325. $this->assertHtml($expected, $result);
  4326. $result = $this->Form->control('test', [
  4327. 'type' => 'radio',
  4328. 'options' => ['A', 'B'],
  4329. 'value' => '0',
  4330. ]);
  4331. $expected = [
  4332. ['div' => ['class' => 'input radio']],
  4333. '<label',
  4334. 'Test',
  4335. '/label',
  4336. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4337. ['label' => ['for' => 'test-0']],
  4338. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4339. 'A',
  4340. '/label',
  4341. ['label' => ['for' => 'test-1']],
  4342. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4343. 'B',
  4344. '/label',
  4345. '/div',
  4346. ];
  4347. $this->assertHtml($expected, $result);
  4348. $result = $this->Form->control('test', [
  4349. 'type' => 'radio',
  4350. 'options' => ['A', 'B'],
  4351. 'label' => false,
  4352. ]);
  4353. $expected = [
  4354. ['div' => ['class' => 'input radio']],
  4355. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4356. ['label' => ['for' => 'test-0']],
  4357. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4358. 'A',
  4359. '/label',
  4360. ['label' => ['for' => 'test-1']],
  4361. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4362. 'B',
  4363. '/label',
  4364. '/div',
  4365. ];
  4366. $this->assertHtml($expected, $result);
  4367. $result = $this->Form->control('accept', [
  4368. 'type' => 'radio',
  4369. 'options' => [
  4370. 1 => 'positive',
  4371. -1 => 'negative',
  4372. ],
  4373. 'label' => false,
  4374. ]);
  4375. $expected = [
  4376. ['div' => ['class' => 'input radio']],
  4377. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '', 'id' => 'accept']],
  4378. ['label' => ['for' => 'accept-1']],
  4379. ['input' => [
  4380. 'type' => 'radio',
  4381. 'name' => 'accept',
  4382. 'value' => '1',
  4383. 'id' => 'accept-1',
  4384. ]],
  4385. 'positive',
  4386. '/label',
  4387. ['label' => ['for' => 'accept--1']],
  4388. ['input' => [
  4389. 'type' => 'radio',
  4390. 'name' => 'accept',
  4391. 'value' => '-1',
  4392. 'id' => 'accept--1',
  4393. ]],
  4394. 'negative',
  4395. '/label',
  4396. '/div',
  4397. ];
  4398. $this->assertHtml($expected, $result);
  4399. $articlesTable = $this->getTableLocator()->get('Articles');
  4400. $articlesTable->getSchema()->setColumnType(
  4401. 'published',
  4402. EnumType::from(ArticleStatus::class)
  4403. );
  4404. $this->Form->create($articlesTable->newEmptyEntity());
  4405. $result = $this->Form->control('published', ['type' => 'radio', 'label' => false,]);
  4406. $expected = [
  4407. ['div' => ['class' => 'input radio']],
  4408. 'input' => ['type' => 'hidden', 'name' => 'published', 'value' => '', 'id' => 'published'],
  4409. ['label' => ['for' => 'published-y']],
  4410. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'Y', 'id' => 'published-y']],
  4411. 'PUBLISHED',
  4412. '/label',
  4413. ['label' => ['for' => 'published-n']],
  4414. ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'N', 'id' => 'published-n', 'checked' => 'checked']],
  4415. 'UNPUBLISHED',
  4416. '/label',
  4417. '/div',
  4418. ];
  4419. $this->assertHtml($expected, $result);
  4420. }
  4421. /**
  4422. * testRadioNoLabel method
  4423. *
  4424. * Test that radio() works with label = false.
  4425. */
  4426. public function testRadioNoLabel(): void
  4427. {
  4428. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4429. $expected = [
  4430. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4431. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4432. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4433. ];
  4434. $this->assertHtml($expected, $result);
  4435. }
  4436. /**
  4437. * testRadioControlInsideLabel method
  4438. *
  4439. * Test generating radio input inside label ala twitter bootstrap.
  4440. */
  4441. public function testRadioControlInsideLabel(): void
  4442. {
  4443. $this->Form->setTemplates([
  4444. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4445. 'radioWrapper' => '{{label}}',
  4446. ]);
  4447. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4448. // phpcs:disable
  4449. $expected = [
  4450. ['input' => [
  4451. 'type' => 'hidden',
  4452. 'name' => 'Model[field]',
  4453. 'value' => '',
  4454. 'id' => 'model-field'
  4455. ]],
  4456. ['label' => ['for' => 'model-field-0']],
  4457. ['input' => [
  4458. 'type' => 'radio',
  4459. 'name' => 'Model[field]',
  4460. 'value' => '0',
  4461. 'id' => 'model-field-0'
  4462. ]],
  4463. 'option A',
  4464. '/label',
  4465. ['label' => ['for' => 'model-field-1']],
  4466. ['input' => [
  4467. 'type' => 'radio',
  4468. 'name' => 'Model[field]',
  4469. 'value' => '1',
  4470. 'id' => 'model-field-1'
  4471. ]],
  4472. 'option B',
  4473. '/label',
  4474. ];
  4475. // phpcs:enable
  4476. $this->assertHtml($expected, $result);
  4477. }
  4478. /**
  4479. * testRadioHiddenControlDisabling method
  4480. *
  4481. * Test disabling the hidden input for radio buttons.
  4482. */
  4483. public function testRadioHiddenControlDisabling(): void
  4484. {
  4485. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4486. $expected = [
  4487. 'label' => ['for' => 'model-1-field-0'],
  4488. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4489. 'option A',
  4490. '/label',
  4491. ];
  4492. $this->assertHtml($expected, $result);
  4493. }
  4494. /**
  4495. * testRadioOutOfRange method
  4496. *
  4497. * Test radio element set generation.
  4498. */
  4499. public function testRadioOutOfRange(): void
  4500. {
  4501. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4502. $expected = [
  4503. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4504. 'label' => ['for' => 'model-field-v'],
  4505. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4506. 'value',
  4507. '/label',
  4508. ];
  4509. $this->assertHtml($expected, $result);
  4510. }
  4511. /**
  4512. * testSelect method
  4513. *
  4514. * Test select element generation.
  4515. */
  4516. public function testSelect(): void
  4517. {
  4518. $result = $this->Form->select('Model.field', []);
  4519. $expected = [
  4520. 'select' => ['name' => 'Model[field]'],
  4521. '/select',
  4522. ];
  4523. $this->assertHtml($expected, $result);
  4524. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4525. $this->Form->create();
  4526. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4527. $expected = [
  4528. 'select' => ['name' => 'Model[field]'],
  4529. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4530. 'good',
  4531. '/option',
  4532. ['option' => ['value' => 'other']],
  4533. 'bad',
  4534. '/option',
  4535. '/select',
  4536. ];
  4537. $this->assertHtml($expected, $result);
  4538. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4539. $this->assertHtml($expected, $result);
  4540. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4541. $this->Form->create();
  4542. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4543. $expected = [
  4544. 'select' => ['name' => 'Model[field]'],
  4545. ['option' => ['value' => 'value']],
  4546. 'good',
  4547. '/option',
  4548. ['option' => ['value' => 'other']],
  4549. 'bad',
  4550. '/option',
  4551. '/select',
  4552. ];
  4553. $this->assertHtml($expected, $result);
  4554. $options = [
  4555. ['value' => 'first', 'text' => 'First'],
  4556. ['value' => 'first', 'text' => 'Another First'],
  4557. ];
  4558. $result = $this->Form->select(
  4559. 'Model.field',
  4560. $options,
  4561. ['escape' => false, 'empty' => false]
  4562. );
  4563. $expected = [
  4564. 'select' => ['name' => 'Model[field]'],
  4565. ['option' => ['value' => 'first']],
  4566. 'First',
  4567. '/option',
  4568. ['option' => ['value' => 'first']],
  4569. 'Another First',
  4570. '/option',
  4571. '/select',
  4572. ];
  4573. $this->assertHtml($expected, $result);
  4574. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4575. $this->Form->create();
  4576. $result = $this->Form->select(
  4577. 'Model.contact_id',
  4578. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4579. ['escape' => false, 'empty' => 'pick something']
  4580. );
  4581. $expected = [
  4582. 'select' => ['name' => 'Model[contact_id]'],
  4583. ['option' => ['value' => '']], 'pick something', '/option',
  4584. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4585. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4586. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4587. '/select',
  4588. ];
  4589. $this->assertHtml($expected, $result);
  4590. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4591. $this->Form->create();
  4592. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4593. $expected = [
  4594. 'select' => ['name' => 'Model[field]'],
  4595. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4596. ['option' => ['value' => '1']], 'Yes', '/option',
  4597. '/select',
  4598. ];
  4599. $this->assertHtml($expected, $result);
  4600. $article = new Article([
  4601. 'status' => ArticleStatus::UNPUBLISHED,
  4602. ]);
  4603. $this->Form->create($article);
  4604. $result = $this->Form->select('status', ['Y' => 'Published', 'N' => 'Unpublished']);
  4605. $expected = [
  4606. 'select' => ['name' => 'status'],
  4607. ['option' => ['value' => 'Y']], 'Published', '/option',
  4608. ['option' => ['value' => 'N', 'selected' => 'selected']], 'Unpublished', '/option',
  4609. '/select',
  4610. ];
  4611. $this->assertHtml($expected, $result);
  4612. }
  4613. /**
  4614. * testSelectEscapeHtml method
  4615. *
  4616. * Test that select() escapes HTML.
  4617. */
  4618. public function testSelectEscapeHtml(): void
  4619. {
  4620. $result = $this->Form->select(
  4621. 'Model.field',
  4622. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4623. ['empty' => false]
  4624. );
  4625. $expected = [
  4626. 'select' => ['name' => 'Model[field]'],
  4627. ['option' => ['value' => 'first']],
  4628. 'first &quot;html&quot; &lt;chars&gt;',
  4629. '/option',
  4630. ['option' => ['value' => 'second']],
  4631. 'value',
  4632. '/option',
  4633. '/select',
  4634. ];
  4635. $this->assertHtml($expected, $result);
  4636. $result = $this->Form->select(
  4637. 'Model.field',
  4638. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4639. ['escape' => false, 'empty' => false]
  4640. );
  4641. $expected = [
  4642. 'select' => ['name' => 'Model[field]'],
  4643. ['option' => ['value' => 'first']],
  4644. 'first "html" <chars>',
  4645. '/option',
  4646. ['option' => ['value' => 'second']],
  4647. 'value',
  4648. '/option',
  4649. '/select',
  4650. ];
  4651. $this->assertHtml($expected, $result);
  4652. }
  4653. /**
  4654. * testSelectRequired method
  4655. *
  4656. * Test select() with required and disabled attributes.
  4657. */
  4658. public function testSelectRequired(): void
  4659. {
  4660. $this->article['required'] = [
  4661. 'user_id' => true,
  4662. ];
  4663. $this->Form->create($this->article);
  4664. $result = $this->Form->select('user_id', ['option A']);
  4665. $expected = [
  4666. 'select' => [
  4667. 'name' => 'user_id',
  4668. 'required' => 'required',
  4669. ],
  4670. ['option' => ['value' => '0']], 'option A', '/option',
  4671. '/select',
  4672. ];
  4673. $this->assertHtml($expected, $result);
  4674. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4675. $expected = [
  4676. 'select' => [
  4677. 'name' => 'user_id',
  4678. 'disabled' => 'disabled',
  4679. ],
  4680. ['option' => ['value' => '0']], 'option A', '/option',
  4681. '/select',
  4682. ];
  4683. $this->assertHtml($expected, $result);
  4684. }
  4685. public function testSelectEmptyWithRequiredFalse(): void
  4686. {
  4687. $Articles = $this->getTableLocator()->get('Articles');
  4688. $validator = $Articles->getValidator('default');
  4689. $validator->allowEmptyString('user_id');
  4690. $Articles->setValidator('default', $validator);
  4691. $entity = $Articles->newEmptyEntity();
  4692. $this->Form->create($entity);
  4693. $result = $this->Form->select('user_id', ['option A']);
  4694. $expected = [
  4695. 'select' => [
  4696. 'name' => 'user_id',
  4697. ],
  4698. ['option' => ['value' => '']], '/option',
  4699. ['option' => ['value' => '0']], 'option A', '/option',
  4700. '/select',
  4701. ];
  4702. $this->assertHtml($expected, $result);
  4703. }
  4704. /**
  4705. * testNestedSelect method
  4706. *
  4707. * Test select element generation with optgroups.
  4708. */
  4709. public function testNestedSelect(): void
  4710. {
  4711. $result = $this->Form->select(
  4712. 'Model.field',
  4713. [1 => 'One', 2 => 'Two', 'Three' => [
  4714. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4715. ]],
  4716. ['empty' => false]
  4717. );
  4718. $expected = [
  4719. 'select' => ['name' => 'Model[field]'],
  4720. ['option' => ['value' => 1]],
  4721. 'One',
  4722. '/option',
  4723. ['option' => ['value' => 2]],
  4724. 'Two',
  4725. '/option',
  4726. ['optgroup' => ['label' => 'Three']],
  4727. ['option' => ['value' => 3]],
  4728. 'Three',
  4729. '/option',
  4730. ['option' => ['value' => 4]],
  4731. 'Four',
  4732. '/option',
  4733. ['option' => ['value' => 5]],
  4734. 'Five',
  4735. '/option',
  4736. '/optgroup',
  4737. '/select',
  4738. ];
  4739. $this->assertHtml($expected, $result);
  4740. }
  4741. /**
  4742. * testSelectMultiple method
  4743. *
  4744. * Test generation of multiple select elements.
  4745. */
  4746. public function testSelectMultiple(): void
  4747. {
  4748. $options = ['first', 'second', 'third'];
  4749. $result = $this->Form->select(
  4750. 'Model.multi_field',
  4751. $options,
  4752. ['form' => 'my-form', 'multiple' => true]
  4753. );
  4754. $expected = [
  4755. 'input' => [
  4756. 'type' => 'hidden',
  4757. 'name' => 'Model[multi_field]',
  4758. 'value' => '',
  4759. 'form' => 'my-form',
  4760. ],
  4761. 'select' => [
  4762. 'name' => 'Model[multi_field][]',
  4763. 'multiple' => 'multiple',
  4764. 'form' => 'my-form',
  4765. ],
  4766. ['option' => ['value' => '0']],
  4767. 'first',
  4768. '/option',
  4769. ['option' => ['value' => '1']],
  4770. 'second',
  4771. '/option',
  4772. ['option' => ['value' => '2']],
  4773. 'third',
  4774. '/option',
  4775. '/select',
  4776. ];
  4777. $this->assertHtml($expected, $result);
  4778. $result = $this->Form->select(
  4779. 'Model.multi_field',
  4780. $options,
  4781. ['multiple' => 'multiple', 'form' => 'my-form']
  4782. );
  4783. $this->assertHtml($expected, $result);
  4784. $result = $this->Form->select(
  4785. 'Model.multi_field',
  4786. $options,
  4787. ['form' => 'my-form', 'multiple' => false]
  4788. );
  4789. $this->assertStringNotContainsString('multiple', $result);
  4790. }
  4791. /**
  4792. * testCheckboxZeroValue method
  4793. *
  4794. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4795. */
  4796. public function testCheckboxZeroValue(): void
  4797. {
  4798. $result = $this->Form->control('User.get_spam', [
  4799. 'type' => 'checkbox',
  4800. 'value' => '0',
  4801. 'hiddenField' => '1',
  4802. ]);
  4803. $expected = [
  4804. 'div' => ['class' => 'input checkbox'],
  4805. 'label' => ['for' => 'user-get-spam'],
  4806. ['input' => [
  4807. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4808. 'value' => '1',
  4809. ]],
  4810. ['input' => [
  4811. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4812. 'value' => '0', 'id' => 'user-get-spam',
  4813. ]],
  4814. 'Get Spam',
  4815. '/label',
  4816. '/div',
  4817. ];
  4818. $this->assertHtml($expected, $result);
  4819. $result = $this->Form->control('User.get_spam', [
  4820. 'type' => 'checkbox',
  4821. 'value' => '0',
  4822. 'hiddenField' => '',
  4823. ]);
  4824. $expected = [
  4825. 'div' => ['class' => 'input checkbox'],
  4826. 'label' => ['for' => 'user-get-spam'],
  4827. ['input' => [
  4828. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4829. 'value' => '',
  4830. ]],
  4831. ['input' => [
  4832. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4833. 'value' => '0', 'id' => 'user-get-spam',
  4834. ]],
  4835. 'Get Spam',
  4836. '/label',
  4837. '/div',
  4838. ];
  4839. $this->assertHtml($expected, $result);
  4840. }
  4841. /**
  4842. * testHabtmSelectBox method
  4843. *
  4844. * Test generation of habtm select boxes.
  4845. */
  4846. public function testHabtmSelectBox(): void
  4847. {
  4848. $options = [
  4849. 1 => 'blue',
  4850. 2 => 'red',
  4851. 3 => 'green',
  4852. ];
  4853. $tags = [
  4854. new Entity(['id' => 1, 'name' => 'blue']),
  4855. new Entity(['id' => 3, 'name' => 'green']),
  4856. ];
  4857. $article = new Article(['tags' => $tags]);
  4858. $this->Form->create($article);
  4859. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4860. $expected = [
  4861. 'div' => ['class' => 'input select'],
  4862. 'label' => ['for' => 'tags-ids'],
  4863. 'Tags',
  4864. '/label',
  4865. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4866. 'select' => [
  4867. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4868. 'multiple' => 'multiple',
  4869. ],
  4870. ['option' => ['value' => '1', 'selected' => 'selected']],
  4871. 'blue',
  4872. '/option',
  4873. ['option' => ['value' => '2']],
  4874. 'red',
  4875. '/option',
  4876. ['option' => ['value' => '3', 'selected' => 'selected']],
  4877. 'green',
  4878. '/option',
  4879. '/select',
  4880. '/div',
  4881. ];
  4882. $this->assertHtml($expected, $result);
  4883. // make sure only 50 is selected, and not 50f5c0cf
  4884. $options = [
  4885. '1' => 'blue',
  4886. '50f5c0cf' => 'red',
  4887. '50' => 'green',
  4888. ];
  4889. $tags = [
  4890. new Entity(['id' => 1, 'name' => 'blue']),
  4891. new Entity(['id' => 50, 'name' => 'green']),
  4892. ];
  4893. $article = new Article(['tags' => $tags]);
  4894. $this->Form->create($article);
  4895. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4896. $expected = [
  4897. 'div' => ['class' => 'input select'],
  4898. 'label' => ['for' => 'tags-ids'],
  4899. 'Tags',
  4900. '/label',
  4901. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4902. 'select' => [
  4903. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4904. 'multiple' => 'multiple',
  4905. ],
  4906. ['option' => ['value' => '1', 'selected' => 'selected']],
  4907. 'blue',
  4908. '/option',
  4909. ['option' => ['value' => '50f5c0cf']],
  4910. 'red',
  4911. '/option',
  4912. ['option' => ['value' => '50', 'selected' => 'selected']],
  4913. 'green',
  4914. '/option',
  4915. '/select',
  4916. '/div',
  4917. ];
  4918. $this->assertHtml($expected, $result);
  4919. $spacecraft = [
  4920. 1 => 'Orion',
  4921. 2 => 'Helios',
  4922. ];
  4923. $this->View->set('spacecraft', $spacecraft);
  4924. $this->Form->create();
  4925. $result = $this->Form->control('spacecraft._ids');
  4926. $expected = [
  4927. 'div' => ['class' => 'input select'],
  4928. 'label' => ['for' => 'spacecraft-ids'],
  4929. 'Spacecraft',
  4930. '/label',
  4931. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4932. 'select' => [
  4933. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4934. 'multiple' => 'multiple',
  4935. ],
  4936. ['option' => ['value' => '1']],
  4937. 'Orion',
  4938. '/option',
  4939. ['option' => ['value' => '2']],
  4940. 'Helios',
  4941. '/option',
  4942. '/select',
  4943. '/div',
  4944. ];
  4945. $this->assertHtml($expected, $result);
  4946. }
  4947. /**
  4948. * testErrorsForBelongsToManySelect method
  4949. *
  4950. * Tests that errors for belongsToMany select fields are being
  4951. * picked up properly.
  4952. */
  4953. public function testErrorsForBelongsToManySelect(): void
  4954. {
  4955. $spacecraft = [
  4956. 1 => 'Orion',
  4957. 2 => 'Helios',
  4958. ];
  4959. $this->View->set('spacecraft', $spacecraft);
  4960. $article = new Article();
  4961. $article->setError('spacecraft', ['Invalid']);
  4962. $this->Form->create($article);
  4963. $result = $this->Form->control('spacecraft._ids');
  4964. $expected = [
  4965. ['div' => ['class' => 'input select error']],
  4966. 'label' => ['for' => 'spacecraft-ids'],
  4967. 'Spacecraft',
  4968. '/label',
  4969. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4970. 'select' => [
  4971. 'name' => 'spacecraft[_ids][]',
  4972. 'id' => 'spacecraft-ids',
  4973. 'multiple' => 'multiple',
  4974. ],
  4975. ['option' => ['value' => '1']],
  4976. 'Orion',
  4977. '/option',
  4978. ['option' => ['value' => '2']],
  4979. 'Helios',
  4980. '/option',
  4981. '/select',
  4982. ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  4983. 'Invalid',
  4984. '/div',
  4985. '/div',
  4986. ];
  4987. $this->assertHtml($expected, $result);
  4988. }
  4989. /**
  4990. * testSelectMultipleCheckboxes method
  4991. *
  4992. * Test generation of multi select elements in checkbox format.
  4993. */
  4994. public function testSelectMultipleCheckboxes(): void
  4995. {
  4996. $result = $this->Form->select(
  4997. 'Model.multi_field',
  4998. ['first', 'second', 'third'],
  4999. ['multiple' => 'checkbox']
  5000. );
  5001. $expected = [
  5002. 'input' => [
  5003. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5004. ],
  5005. ['div' => ['class' => 'checkbox']],
  5006. ['label' => ['for' => 'model-multi-field-0']],
  5007. ['input' => [
  5008. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5009. 'value' => '0', 'id' => 'model-multi-field-0',
  5010. ]],
  5011. 'first',
  5012. '/label',
  5013. '/div',
  5014. ['div' => ['class' => 'checkbox']],
  5015. ['label' => ['for' => 'model-multi-field-1']],
  5016. ['input' => [
  5017. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5018. 'value' => '1', 'id' => 'model-multi-field-1',
  5019. ]],
  5020. 'second',
  5021. '/label',
  5022. '/div',
  5023. ['div' => ['class' => 'checkbox']],
  5024. ['label' => ['for' => 'model-multi-field-2']],
  5025. ['input' => [
  5026. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5027. 'value' => '2', 'id' => 'model-multi-field-2',
  5028. ]],
  5029. 'third',
  5030. '/label',
  5031. '/div',
  5032. ];
  5033. $this->assertHtml($expected, $result);
  5034. $result = $this->Form->select(
  5035. 'Model.multi_field',
  5036. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5037. ['multiple' => 'checkbox']
  5038. );
  5039. $expected = [
  5040. 'input' => [
  5041. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5042. ],
  5043. ['div' => ['class' => 'checkbox']],
  5044. ['label' => ['for' => 'model-multi-field-a+']],
  5045. ['input' => [
  5046. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5047. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5048. ]],
  5049. 'first',
  5050. '/label',
  5051. '/div',
  5052. ['div' => ['class' => 'checkbox']],
  5053. ['label' => ['for' => 'model-multi-field-a++']],
  5054. ['input' => [
  5055. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5056. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5057. ]],
  5058. 'second',
  5059. '/label',
  5060. '/div',
  5061. ['div' => ['class' => 'checkbox']],
  5062. ['label' => ['for' => 'model-multi-field-a+++']],
  5063. ['input' => [
  5064. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5065. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5066. ]],
  5067. 'third',
  5068. '/label',
  5069. '/div',
  5070. ];
  5071. $this->assertHtml($expected, $result);
  5072. $result = $this->Form->select(
  5073. 'Model.multi_field',
  5074. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5075. ['multiple' => 'checkbox']
  5076. );
  5077. $expected = [
  5078. 'input' => [
  5079. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  5080. ],
  5081. ['div' => ['class' => 'checkbox']],
  5082. ['label' => ['for' => 'model-multi-field-a-b']],
  5083. ['input' => [
  5084. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5085. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5086. ]],
  5087. 'first',
  5088. '/label',
  5089. '/div',
  5090. ['div' => ['class' => 'checkbox']],
  5091. ['label' => ['for' => 'model-multi-field-a-b1']],
  5092. ['input' => [
  5093. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5094. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5095. ]],
  5096. 'second',
  5097. '/label',
  5098. '/div',
  5099. ['div' => ['class' => 'checkbox']],
  5100. ['label' => ['for' => 'model-multi-field-a-b2']],
  5101. ['input' => [
  5102. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5103. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5104. ]],
  5105. 'third',
  5106. '/label',
  5107. '/div',
  5108. ];
  5109. $this->assertHtml($expected, $result);
  5110. }
  5111. /**
  5112. * testSelectMultipleCheckboxRequestData method
  5113. *
  5114. * Ensure that multiCheckbox reads from the request data.
  5115. */
  5116. public function testSelectMultipleCheckboxRequestData(): void
  5117. {
  5118. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5119. $result = $this->Form->select(
  5120. 'Model.tags',
  5121. ['1' => 'first', 'Array' => 'Array'],
  5122. ['multiple' => 'checkbox']
  5123. );
  5124. $expected = [
  5125. 'input' => [
  5126. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'model-tags',
  5127. ],
  5128. ['div' => ['class' => 'checkbox']],
  5129. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5130. ['input' => [
  5131. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5132. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5133. ]],
  5134. 'first',
  5135. '/label',
  5136. '/div',
  5137. ['div' => ['class' => 'checkbox']],
  5138. ['label' => ['for' => 'model-tags-array']],
  5139. ['input' => [
  5140. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5141. 'value' => 'Array', 'id' => 'model-tags-array',
  5142. ]],
  5143. 'Array',
  5144. '/label',
  5145. '/div',
  5146. ];
  5147. $this->assertHtml($expected, $result);
  5148. }
  5149. /**
  5150. * testSelectMultipleCheckboxSecurity method
  5151. *
  5152. * Checks the security hash array generated for multiple-input checkbox elements.
  5153. */
  5154. public function testSelectMultipleCheckboxSecurity(): void
  5155. {
  5156. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5157. $this->Form->create();
  5158. $this->Form->select(
  5159. 'Model.multi_field',
  5160. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5161. ['multiple' => 'checkbox']
  5162. );
  5163. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5164. $this->assertEquals(['Model.multi_field'], $fields);
  5165. $result = $this->Form->secure();
  5166. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5167. $hash = urlencode($hash . ':');
  5168. $this->assertStringContainsString('"' . $hash . '"', $result);
  5169. }
  5170. /**
  5171. * testSelectMultipleSecureWithNoOptions method
  5172. *
  5173. * Multiple select elements should always be secured as they always participate
  5174. * in the POST data.
  5175. */
  5176. public function testSelectMultipleSecureWithNoOptions(): void
  5177. {
  5178. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5179. $this->Form->create();
  5180. $this->Form->select(
  5181. 'Model.select',
  5182. [],
  5183. ['multiple' => true]
  5184. );
  5185. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5186. $this->assertEquals(['Model.select'], $result);
  5187. }
  5188. /**
  5189. * testSelectNoSecureWithNoOptions method
  5190. *
  5191. * When a select box has no options it should not be added to the fields list
  5192. * as it always fail post validation.
  5193. */
  5194. public function testSelectNoSecureWithNoOptions(): void
  5195. {
  5196. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5197. $this->Form->create();
  5198. $this->Form->select(
  5199. 'Model.select',
  5200. []
  5201. );
  5202. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5203. $this->assertEquals([], $result);
  5204. $this->Form->select(
  5205. 'Model.user_id',
  5206. [],
  5207. ['empty' => true]
  5208. );
  5209. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5210. $this->assertEquals(['Model.user_id'], $result);
  5211. }
  5212. /**
  5213. * testControlMultipleCheckboxes method
  5214. *
  5215. * Test control() resulting in multi select elements being generated.
  5216. */
  5217. public function testControlMultipleCheckboxes(): void
  5218. {
  5219. $result = $this->Form->control('Model.multi_field', [
  5220. 'options' => ['first', 'second', 'third'],
  5221. 'multiple' => 'checkbox',
  5222. ]);
  5223. $expected = [
  5224. ['div' => ['class' => 'input select']],
  5225. ['label' => ['for' => 'model-multi-field']],
  5226. 'Multi Field',
  5227. '/label',
  5228. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5229. ['div' => ['class' => 'checkbox']],
  5230. ['label' => ['for' => 'model-multi-field-0']],
  5231. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5232. 'first',
  5233. '/label',
  5234. '/div',
  5235. ['div' => ['class' => 'checkbox']],
  5236. ['label' => ['for' => 'model-multi-field-1']],
  5237. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5238. 'second',
  5239. '/label',
  5240. '/div',
  5241. ['div' => ['class' => 'checkbox']],
  5242. ['label' => ['for' => 'model-multi-field-2']],
  5243. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5244. 'third',
  5245. '/label',
  5246. '/div',
  5247. '/div',
  5248. ];
  5249. $this->assertHtml($expected, $result);
  5250. $result = $this->Form->control('Model.multi_field', [
  5251. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5252. 'multiple' => 'checkbox',
  5253. ]);
  5254. $expected = [
  5255. ['div' => ['class' => 'input select']],
  5256. ['label' => ['for' => 'model-multi-field']],
  5257. 'Multi Field',
  5258. '/label',
  5259. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5260. ['div' => ['class' => 'checkbox']],
  5261. ['label' => ['for' => 'model-multi-field-a']],
  5262. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5263. 'first',
  5264. '/label',
  5265. '/div',
  5266. ['div' => ['class' => 'checkbox']],
  5267. ['label' => ['for' => 'model-multi-field-b']],
  5268. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5269. 'second',
  5270. '/label',
  5271. '/div',
  5272. ['div' => ['class' => 'checkbox']],
  5273. ['label' => ['for' => 'model-multi-field-c']],
  5274. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5275. 'third',
  5276. '/label',
  5277. '/div',
  5278. '/div',
  5279. ];
  5280. $this->assertHtml($expected, $result);
  5281. }
  5282. /**
  5283. * testSelectHiddenFieldOmission method
  5284. *
  5285. * Test that select() with 'hiddenField' => false omits the hidden field.
  5286. */
  5287. public function testSelectHiddenFieldOmission(): void
  5288. {
  5289. $result = $this->Form->select(
  5290. 'Model.multi_field',
  5291. ['first', 'second'],
  5292. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5293. );
  5294. $this->assertStringNotContainsString('type="hidden"', $result);
  5295. }
  5296. /**
  5297. * testSelectCheckboxMultipleOverrideName method
  5298. *
  5299. * Test that select() with multiple = checkbox works with overriding name attribute.
  5300. */
  5301. public function testSelectCheckboxMultipleOverrideName(): void
  5302. {
  5303. $result = $this->Form->select('category', ['1', '2'], [
  5304. 'multiple' => 'checkbox',
  5305. 'name' => 'fish',
  5306. ]);
  5307. $expected = [
  5308. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  5309. ['div' => ['class' => 'checkbox']],
  5310. ['label' => ['for' => 'fish-0']],
  5311. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5312. '1',
  5313. '/label',
  5314. '/div',
  5315. ['div' => ['class' => 'checkbox']],
  5316. ['label' => ['for' => 'fish-1']],
  5317. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5318. '2',
  5319. '/label',
  5320. '/div',
  5321. ];
  5322. $this->assertHtml($expected, $result);
  5323. $result = $this->Form->multiCheckbox(
  5324. 'category',
  5325. new Collection(['1', '2']),
  5326. ['name' => 'fish']
  5327. );
  5328. $this->assertHtml($expected, $result);
  5329. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5330. 'name' => 'fish',
  5331. ]);
  5332. $this->assertHtml($expected, $result);
  5333. }
  5334. /**
  5335. * testSelectCheckboxMultipleOverrideName method
  5336. *
  5337. * Test that select() with multiple = checkbox works with overriding name attribute.
  5338. */
  5339. public function testSelectCheckboxMultipleCustomId(): void
  5340. {
  5341. $result = $this->Form->select('category', ['1', '2'], [
  5342. 'multiple' => 'checkbox',
  5343. 'id' => 'cat',
  5344. ]);
  5345. $expected = [
  5346. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'cat'],
  5347. ['div' => ['class' => 'checkbox']],
  5348. ['label' => ['for' => 'cat-0']],
  5349. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'cat-0']],
  5350. '1',
  5351. '/label',
  5352. '/div',
  5353. ['div' => ['class' => 'checkbox']],
  5354. ['label' => ['for' => 'cat-1']],
  5355. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'cat-1']],
  5356. '2',
  5357. '/label',
  5358. '/div',
  5359. ];
  5360. $this->assertHtml($expected, $result);
  5361. $result = $this->Form->multiCheckbox(
  5362. 'category',
  5363. ['1', '2'],
  5364. ['id' => 'cat']
  5365. );
  5366. $this->assertHtml($expected, $result);
  5367. }
  5368. /**
  5369. * testControlMultiCheckbox method
  5370. *
  5371. * Test that control() works with multicheckbox.
  5372. */
  5373. public function testControlMultiCheckbox(): void
  5374. {
  5375. $result = $this->Form->control('category', [
  5376. 'type' => 'multicheckbox',
  5377. 'options' => ['1', '2'],
  5378. ]);
  5379. $expected = [
  5380. ['div' => ['class' => 'input multicheckbox']],
  5381. '<label',
  5382. 'Category',
  5383. '/label',
  5384. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'category'],
  5385. ['div' => ['class' => 'checkbox']],
  5386. ['label' => ['for' => 'category-0']],
  5387. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5388. '1',
  5389. '/label',
  5390. '/div',
  5391. ['div' => ['class' => 'checkbox']],
  5392. ['label' => ['for' => 'category-1']],
  5393. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5394. '2',
  5395. '/label',
  5396. '/div',
  5397. '/div',
  5398. ];
  5399. $this->assertHtml($expected, $result);
  5400. }
  5401. /**
  5402. * testCheckbox method
  5403. *
  5404. * Test generation of checkboxes.
  5405. */
  5406. public function testCheckbox(): void
  5407. {
  5408. $result = $this->Form->checkbox('Model.field');
  5409. $expected = [
  5410. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5411. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5412. ];
  5413. $this->assertHtml($expected, $result);
  5414. $result = $this->Form->checkbox('Model.field', [
  5415. 'id' => 'theID',
  5416. 'value' => 'myvalue',
  5417. 'form' => 'my-form',
  5418. ]);
  5419. $expected = [
  5420. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5421. ['input' => [
  5422. 'type' => 'checkbox', 'name' => 'Model[field]',
  5423. 'value' => 'myvalue', 'id' => 'theID',
  5424. 'form' => 'my-form',
  5425. ]],
  5426. ];
  5427. $this->assertHtml($expected, $result);
  5428. }
  5429. /**
  5430. * testCheckboxDefaultValue method
  5431. *
  5432. * Test default value setting on checkbox() method.
  5433. */
  5434. public function testCheckboxDefaultValue(): void
  5435. {
  5436. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5437. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5438. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5439. $this->assertHtml($expected, $result);
  5440. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5441. $this->Form->create();
  5442. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5443. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5444. $this->assertHtml($expected, $result);
  5445. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5446. $this->Form->create();
  5447. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5448. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5449. $this->assertHtml($expected, $result);
  5450. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5451. $this->Form->create();
  5452. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5453. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5454. $this->assertHtml($expected, $result);
  5455. $Articles = $this->getTableLocator()->get('Articles');
  5456. $Articles->getSchema()->addColumn(
  5457. 'published',
  5458. ['type' => 'boolean', 'null' => false, 'default' => true]
  5459. );
  5460. $this->Form->create($Articles->newEmptyEntity());
  5461. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5462. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5463. $this->assertHtml($expected, $result);
  5464. }
  5465. /**
  5466. * testCheckboxCheckedAndError method
  5467. *
  5468. * Test checkbox being checked or having errors.
  5469. */
  5470. public function testCheckboxCheckedAndError(): void
  5471. {
  5472. $this->article['errors'] = [
  5473. 'published' => true,
  5474. ];
  5475. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5476. $this->Form->create($this->article);
  5477. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5478. $expected = [
  5479. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5480. ['input' => [
  5481. 'type' => 'checkbox',
  5482. 'name' => 'published',
  5483. 'value' => 'myvalue',
  5484. 'id' => 'theID',
  5485. 'checked' => 'checked',
  5486. 'class' => 'form-error',
  5487. ]],
  5488. ];
  5489. $this->assertHtml($expected, $result);
  5490. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5491. $this->Form->create($this->article);
  5492. $result = $this->Form->checkbox('published');
  5493. $expected = [
  5494. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5495. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5496. ];
  5497. $this->assertHtml($expected, $result);
  5498. }
  5499. /**
  5500. * testCheckboxCustomNameAttribute method
  5501. *
  5502. * Test checkbox() with a custom name attribute.
  5503. */
  5504. public function testCheckboxCustomNameAttribute(): void
  5505. {
  5506. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5507. $expected = [
  5508. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5509. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5510. ];
  5511. $this->assertHtml($expected, $result);
  5512. }
  5513. /**
  5514. * testCheckboxHiddenField method
  5515. *
  5516. * Test that the hidden input for checkboxes can be omitted or set to a
  5517. * specific value.
  5518. */
  5519. public function testCheckboxHiddenField(): void
  5520. {
  5521. $result = $this->Form->checkbox('UserForm.something', [
  5522. 'hiddenField' => false,
  5523. ]);
  5524. $expected = [
  5525. 'input' => [
  5526. 'type' => 'checkbox',
  5527. 'name' => 'UserForm[something]',
  5528. 'value' => '1',
  5529. ],
  5530. ];
  5531. $this->assertHtml($expected, $result);
  5532. $result = $this->Form->checkbox('UserForm.something', [
  5533. 'value' => 'Y',
  5534. 'hiddenField' => '',
  5535. ]);
  5536. $expected = [
  5537. ['input' => [
  5538. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5539. 'value' => '',
  5540. ]],
  5541. ['input' => [
  5542. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5543. 'value' => 'Y',
  5544. ]],
  5545. ];
  5546. $this->assertHtml($expected, $result);
  5547. $result = $this->Form->checkbox('UserForm.something', [
  5548. 'value' => 'Y',
  5549. 'hiddenField' => 'N',
  5550. ]);
  5551. $expected = [
  5552. ['input' => [
  5553. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5554. 'value' => 'N',
  5555. ]],
  5556. ['input' => [
  5557. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5558. 'value' => 'Y',
  5559. ]],
  5560. ];
  5561. $this->assertHtml($expected, $result);
  5562. }
  5563. /**
  5564. * testTime method
  5565. *
  5566. * Test the time type.
  5567. */
  5568. public function testTime(): void
  5569. {
  5570. $result = $this->Form->time('start_time', [
  5571. 'value' => '2014-03-08 16:30:00',
  5572. ]);
  5573. $expected = [
  5574. 'input' => [
  5575. 'type' => 'time',
  5576. 'name' => 'start_time',
  5577. 'value' => '16:30:00',
  5578. 'step' => '1',
  5579. ],
  5580. ];
  5581. $this->assertHtml($expected, $result);
  5582. $result = $this->Form->time('start_time', [
  5583. 'value' => new ChronosTime('16:30:00'),
  5584. ]);
  5585. $expected = [
  5586. 'input' => [
  5587. 'type' => 'time',
  5588. 'name' => 'start_time',
  5589. 'value' => '16:30:00',
  5590. 'step' => '1',
  5591. ],
  5592. ];
  5593. $this->assertHtml($expected, $result);
  5594. }
  5595. /**
  5596. * testDate method
  5597. *
  5598. * Test the date type.
  5599. */
  5600. public function testDate(): void
  5601. {
  5602. $result = $this->Form->date('start_day', [
  5603. 'value' => '2014-03-08',
  5604. ]);
  5605. $expected = [
  5606. 'input' => [
  5607. 'type' => 'date',
  5608. 'name' => 'start_day',
  5609. 'value' => '2014-03-08',
  5610. ],
  5611. ];
  5612. $this->assertHtml($expected, $result);
  5613. $result = $this->Form->date('start_day', [
  5614. 'value' => new Date('2014-03-08'),
  5615. ]);
  5616. $this->assertHtml($expected, $result);
  5617. }
  5618. /**
  5619. * testDateTime method
  5620. */
  5621. public function testDateTime(): void
  5622. {
  5623. $result = $this->Form->dateTime('date', ['default' => true]);
  5624. $expected = [
  5625. 'input' => [
  5626. 'type' => 'datetime-local',
  5627. 'name' => 'date',
  5628. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5629. 'step' => '1',
  5630. ],
  5631. ];
  5632. $this->assertHtml($expected, $result);
  5633. }
  5634. /**
  5635. * testDateTimeSecured method
  5636. *
  5637. * Test that datetime fields are added to protected fields list.
  5638. */
  5639. public function testDateTimeSecured(): void
  5640. {
  5641. $this->View->setRequest(
  5642. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5643. );
  5644. $this->Form->create();
  5645. $this->Form->dateTime('date');
  5646. $expected = ['date'];
  5647. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5648. $this->assertEquals($expected, $result);
  5649. $this->Form->date('published');
  5650. $expected = ['date', 'published'];
  5651. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5652. $this->assertEquals($expected, $result);
  5653. }
  5654. /**
  5655. * testDateTimeSecuredDisabled method
  5656. *
  5657. * Test that datetime fields are added to protected fields list.
  5658. */
  5659. public function testDateTimeSecuredDisabled(): void
  5660. {
  5661. $this->View->setRequest(
  5662. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5663. );
  5664. $this->Form->create();
  5665. $this->Form->dateTime('date', ['secure' => false]);
  5666. $expected = [];
  5667. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5668. $this->assertEquals($expected, $result);
  5669. $this->Form->date('published', ['secure' => false]);
  5670. $expected = [];
  5671. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5672. $this->assertEquals($expected, $result);
  5673. }
  5674. /**
  5675. * testDatetimeWithDefault method
  5676. *
  5677. * Test that datetime() and default values work.
  5678. */
  5679. public function testDatetimeWithDefault(): void
  5680. {
  5681. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5682. $expected = [
  5683. 'input' => [
  5684. 'type' => 'datetime-local',
  5685. 'name' => 'updated',
  5686. 'value' => '2009-06-01T11:15:30',
  5687. 'step' => '1',
  5688. ],
  5689. ];
  5690. $this->assertHtml($expected, $result);
  5691. $result = $this->Form->dateTime('updated', [
  5692. 'default' => '2009-06-01 11:15:30',
  5693. ]);
  5694. $expected = [
  5695. 'input' => [
  5696. 'type' => 'datetime-local',
  5697. 'name' => 'updated',
  5698. 'value' => '2009-06-01T11:15:30',
  5699. 'step' => '1',
  5700. ],
  5701. ];
  5702. $this->assertHtml($expected, $result);
  5703. }
  5704. /**
  5705. * testMonth method
  5706. *
  5707. * Test generation of a month input.
  5708. */
  5709. public function testMonth(): void
  5710. {
  5711. $result = $this->Form->month('field', ['value' => '']);
  5712. $expected = [
  5713. 'input' => [
  5714. 'type' => 'month',
  5715. 'name' => 'field',
  5716. 'value' => '',
  5717. ],
  5718. ];
  5719. $this->assertHtml($expected, $result);
  5720. $this->View->setRequest(
  5721. $this->View->getRequest()->withData('release', '2050-02-10')
  5722. );
  5723. $this->Form->create();
  5724. $result = $this->Form->month('release');
  5725. $expected = [
  5726. 'input' => [
  5727. 'type' => 'month',
  5728. 'name' => 'release',
  5729. 'value' => '2050-02',
  5730. ],
  5731. ];
  5732. $this->assertHtml($expected, $result);
  5733. $this->View->setRequest(
  5734. $this->View->getRequest()->withData('release', '2050-03')
  5735. );
  5736. $this->Form->create();
  5737. $result = $this->Form->month('release');
  5738. $expected = [
  5739. 'input' => [
  5740. 'type' => 'month',
  5741. 'name' => 'release',
  5742. 'value' => '2050-03',
  5743. ],
  5744. ];
  5745. $this->assertHtml($expected, $result);
  5746. }
  5747. /**
  5748. * testYear method
  5749. *
  5750. * Test generation of a year input.
  5751. */
  5752. public function testYear(): void
  5753. {
  5754. $this->View->setRequest(
  5755. $this->View->getRequest()->withData('published', '2006')
  5756. );
  5757. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5758. $expected = [
  5759. ['select' => ['name' => 'field']],
  5760. ['option' => ['selected' => 'selected', 'value' => '']],
  5761. '/option',
  5762. ['option' => ['value' => '2007']],
  5763. '2007',
  5764. '/option',
  5765. ['option' => ['value' => '2006']],
  5766. '2006',
  5767. '/option',
  5768. '/select',
  5769. ];
  5770. $this->assertHtml($expected, $result);
  5771. $result = $this->Form->year('field', [
  5772. 'value' => '',
  5773. 'min' => 2006,
  5774. 'max' => 2007,
  5775. 'order' => 'asc',
  5776. ]);
  5777. $expected = [
  5778. ['select' => ['name' => 'field']],
  5779. ['option' => ['selected' => 'selected', 'value' => '']],
  5780. '/option',
  5781. ['option' => ['value' => '2006']],
  5782. '2006',
  5783. '/option',
  5784. ['option' => ['value' => '2007']],
  5785. '2007',
  5786. '/option',
  5787. '/select',
  5788. ];
  5789. $this->assertHtml($expected, $result);
  5790. $result = $this->Form->year('published', [
  5791. 'empty' => false,
  5792. 'min' => 2006,
  5793. 'max' => 2007,
  5794. ]);
  5795. $expected = [
  5796. ['select' => ['name' => 'published']],
  5797. ['option' => ['value' => '2007']],
  5798. '2007',
  5799. '/option',
  5800. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5801. '2006',
  5802. '/option',
  5803. '/select',
  5804. ];
  5805. $this->assertHtml($expected, $result);
  5806. $result = $this->Form->year('published', [
  5807. 'empty' => false,
  5808. 'value' => new Date('2008-01-12'),
  5809. 'min' => 2007,
  5810. 'max' => 2009,
  5811. ]);
  5812. $expected = [
  5813. ['select' => ['name' => 'published']],
  5814. ['option' => ['value' => '2009']],
  5815. '2009',
  5816. '/option',
  5817. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5818. '2008',
  5819. '/option',
  5820. ['option' => ['value' => '2007']],
  5821. '2007',
  5822. '/option',
  5823. '/select',
  5824. ];
  5825. $this->assertHtml($expected, $result);
  5826. $result = $this->Form->year('published', [
  5827. 'empty' => 'Published on',
  5828. ]);
  5829. $this->assertStringContainsString('Published on', $result);
  5830. }
  5831. /**
  5832. * testControlYearPreEpoch method
  5833. *
  5834. * Test minYear being prior to the unix epoch.
  5835. */
  5836. public function testControlYearPreEpoch(): void
  5837. {
  5838. $start = date('Y') - 80;
  5839. $end = date('Y') - 18;
  5840. $result = $this->Form->control('birth_year', [
  5841. 'type' => 'year',
  5842. 'label' => 'Birth Year',
  5843. 'min' => $start,
  5844. 'max' => $end,
  5845. ]);
  5846. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5847. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5848. $this->assertStringNotContainsString('value="00">00', $result);
  5849. }
  5850. /**
  5851. * test control() datetime & required attributes
  5852. */
  5853. public function testControlDatetimeRequired(): void
  5854. {
  5855. $result = $this->Form->control('birthday', [
  5856. 'type' => 'date',
  5857. 'required' => true,
  5858. ]);
  5859. $this->assertStringContainsString(
  5860. '<input type="date" name="birthday" required="required"',
  5861. $result
  5862. );
  5863. }
  5864. /**
  5865. * testYearAutoExpandRange method
  5866. */
  5867. public function testYearAutoExpandRange(): void
  5868. {
  5869. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5870. $result = $this->Form->year('birthday');
  5871. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5872. $result = $matches[1];
  5873. $expected = range(date('Y') + 5, 1930);
  5874. $this->assertEquals($expected, $result);
  5875. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5876. $this->Form->create();
  5877. $result = $this->Form->year('release');
  5878. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5879. $result = $matches[1];
  5880. $expected = range(2050, date('Y') - 5);
  5881. $this->assertEquals($expected, $result);
  5882. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5883. $this->Form->create();
  5884. $result = $this->Form->year('release', [
  5885. 'min' => 1890,
  5886. 'max' => 1900,
  5887. ]);
  5888. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5889. $result = $matches[1];
  5890. $expected = range(1900, 1881);
  5891. $this->assertEquals($expected, $result);
  5892. }
  5893. /**
  5894. * test control placeholder + label
  5895. */
  5896. public function testControlLabelAndPlaceholder(): void
  5897. {
  5898. $this->Form->create($this->article);
  5899. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5900. $expected = [
  5901. 'div' => ['class' => 'input text required'],
  5902. 'label' => ['for' => 'title'],
  5903. 'Title',
  5904. '/label',
  5905. 'input' => [
  5906. 'aria-required' => 'true',
  5907. 'type' => 'text',
  5908. 'required' => 'required',
  5909. 'placeholder' => 'Add title',
  5910. 'id' => 'title',
  5911. 'name' => 'title',
  5912. 'data-validity-message' => 'This field cannot be left empty',
  5913. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5914. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5915. ],
  5916. '/div',
  5917. ];
  5918. $this->assertHtml($expected, $result);
  5919. }
  5920. /**
  5921. * testControlLabelFalse method
  5922. *
  5923. * Test the label option being set to false.
  5924. */
  5925. public function testControlLabelFalse(): void
  5926. {
  5927. $this->Form->create($this->article);
  5928. $result = $this->Form->control('title', ['label' => false]);
  5929. $expected = [
  5930. 'div' => ['class' => 'input text required'],
  5931. 'input' => [
  5932. 'aria-required' => 'true',
  5933. 'type' => 'text',
  5934. 'required' => 'required',
  5935. 'id' => 'title',
  5936. 'name' => 'title',
  5937. 'data-validity-message' => 'This field cannot be left empty',
  5938. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5939. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5940. ],
  5941. '/div',
  5942. ];
  5943. $this->assertHtml($expected, $result);
  5944. $this->Form->create($this->article);
  5945. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5946. $expected['input'] += [
  5947. 'placeholder' => 'Add title',
  5948. 'aria-label' => 'Add title',
  5949. ];
  5950. $this->assertHtml($expected, $result);
  5951. }
  5952. /**
  5953. * testTextArea method
  5954. *
  5955. * Test generation of a textarea input.
  5956. */
  5957. public function testTextArea(): void
  5958. {
  5959. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5960. $result = $this->Form->textarea('field');
  5961. $expected = [
  5962. 'textarea' => ['name' => 'field', 'rows' => 5],
  5963. 'some test data',
  5964. '/textarea',
  5965. ];
  5966. $this->assertHtml($expected, $result);
  5967. $result = $this->Form->textarea('user.bio');
  5968. $expected = [
  5969. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5970. '/textarea',
  5971. ];
  5972. $this->assertHtml($expected, $result);
  5973. $this->View->setRequest($this->View->getRequest()
  5974. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5975. $this->Form->create();
  5976. $result = $this->Form->textarea('field');
  5977. $expected = [
  5978. 'textarea' => ['name' => 'field', 'rows' => 5],
  5979. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5980. '/textarea',
  5981. ];
  5982. $this->assertHtml($expected, $result);
  5983. $this->View->setRequest($this->View->getRequest()->withData(
  5984. 'Model.field',
  5985. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5986. ));
  5987. $this->Form->create();
  5988. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5989. $expected = [
  5990. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5991. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5992. '/textarea',
  5993. ];
  5994. $this->assertHtml($expected, $result);
  5995. $result = $this->Form->textarea('0.OtherModel.field');
  5996. $expected = [
  5997. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5998. '/textarea',
  5999. ];
  6000. $this->assertHtml($expected, $result);
  6001. }
  6002. /**
  6003. * testTextAreaWithStupidCharacters method
  6004. *
  6005. * Test text area with non-ascii characters.
  6006. */
  6007. public function testTextAreaWithStupidCharacters(): void
  6008. {
  6009. $result = $this->Form->textarea('Post.content', [
  6010. 'value' => 'GREAT®',
  6011. 'rows' => '15',
  6012. 'cols' => '75',
  6013. ]);
  6014. $expected = [
  6015. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6016. 'GREAT®',
  6017. '/textarea',
  6018. ];
  6019. $this->assertHtml($expected, $result);
  6020. }
  6021. /**
  6022. * testTextAreaMaxLength method
  6023. *
  6024. * Test textareas maxlength read from schema.
  6025. */
  6026. public function testTextAreaMaxLength(): void
  6027. {
  6028. $this->Form->create([
  6029. 'schema' => [
  6030. 'stuff' => ['type' => 'string', 'length' => 10],
  6031. ],
  6032. ]);
  6033. $result = $this->Form->control('other', ['type' => 'textarea']);
  6034. $expected = [
  6035. 'div' => ['class' => 'input textarea'],
  6036. 'label' => ['for' => 'other'],
  6037. 'Other',
  6038. '/label',
  6039. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6040. '/textarea',
  6041. '/div',
  6042. ];
  6043. $this->assertHtml($expected, $result);
  6044. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6045. $expected = [
  6046. 'div' => ['class' => 'input textarea'],
  6047. 'label' => ['for' => 'stuff'],
  6048. 'Stuff',
  6049. '/label',
  6050. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6051. '/textarea',
  6052. '/div',
  6053. ];
  6054. $this->assertHtml($expected, $result);
  6055. }
  6056. /**
  6057. * testHiddenField method
  6058. *
  6059. * Test generation of a hidden input.
  6060. */
  6061. public function testHidden(): void
  6062. {
  6063. $this->article['errors'] = [
  6064. 'field' => true,
  6065. ];
  6066. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6067. $this->Form->create($this->article);
  6068. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6069. $expected = [
  6070. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6071. $this->assertHtml($expected, $result);
  6072. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6073. $expected = [
  6074. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6075. ];
  6076. $this->assertHtml($expected, $result);
  6077. $article = new Article([
  6078. 'status' => ArticleStatus::UNPUBLISHED,
  6079. ]);
  6080. $this->Form->create($article);
  6081. $result = $this->Form->hidden('status');
  6082. $expected = [
  6083. 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => 'N'],
  6084. ];
  6085. $this->assertHtml($expected, $result);
  6086. }
  6087. /**
  6088. * Test hidden() with various boolean values.
  6089. */
  6090. public function testHiddenBooleanValues(): void
  6091. {
  6092. $this->Form->create($this->article);
  6093. $result = $this->Form->hidden('field', ['value' => null]);
  6094. $expected = [
  6095. 'input' => ['type' => 'hidden', 'name' => 'field'],
  6096. ];
  6097. $this->assertHtml($expected, $result);
  6098. $result = $this->Form->hidden('field', ['value' => true]);
  6099. $expected = [
  6100. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  6101. ];
  6102. $this->assertHtml($expected, $result);
  6103. $result = $this->Form->hidden('field', ['value' => false]);
  6104. $expected = [
  6105. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  6106. ];
  6107. $this->assertHtml($expected, $result);
  6108. }
  6109. /**
  6110. * testFileUploadField method
  6111. *
  6112. * Test generation of a file upload input.
  6113. */
  6114. public function testFileUploadField(): void
  6115. {
  6116. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6117. $result = $this->Form->file('Model.upload');
  6118. $this->assertHtml($expected, $result);
  6119. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6120. 'name' => '', 'type' => '', 'tmp_name' => '',
  6121. 'error' => 4, 'size' => 0,
  6122. ]));
  6123. $result = $this->Form->file('Model.upload');
  6124. $this->assertHtml($expected, $result);
  6125. $this->View->setRequest(
  6126. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6127. );
  6128. $result = $this->Form->file('Model.upload');
  6129. $this->assertHtml($expected, $result);
  6130. }
  6131. /**
  6132. * testFileUploadOnOtherModel method
  6133. *
  6134. * Test File upload input on a model not used in create().
  6135. */
  6136. public function testFileUploadOnOtherModel(): void
  6137. {
  6138. $this->Form->create($this->article, ['type' => 'file']);
  6139. $result = $this->Form->file('ValidateProfile.city');
  6140. $expected = [
  6141. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6142. ];
  6143. $this->assertHtml($expected, $result);
  6144. }
  6145. /**
  6146. * testButton method
  6147. *
  6148. * Test generation of a form button.
  6149. */
  6150. public function testButton(): void
  6151. {
  6152. $result = $this->Form->button('Hi');
  6153. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6154. $this->assertHtml($expected, $result);
  6155. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  6156. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6157. $this->assertHtml($expected, $result);
  6158. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  6159. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6160. $this->assertHtml($expected, $result);
  6161. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  6162. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6163. $this->assertHtml($expected, $result);
  6164. $result = $this->Form->button('No type', ['type' => false]);
  6165. $expected = ['button' => [], 'No type', '/button'];
  6166. $this->assertHtml($expected, $result);
  6167. $result = $this->Form->button('Upload Text', [
  6168. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6169. 'escape' => false,
  6170. ]);
  6171. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  6172. }
  6173. /**
  6174. * testButtonUnlockedByDefault method
  6175. *
  6176. * Test that button() makes unlocked fields by default.
  6177. */
  6178. public function testButtonUnlockedByDefault(): void
  6179. {
  6180. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6181. $this->Form->create();
  6182. $this->Form->button('Save', ['name' => 'save']);
  6183. $this->Form->button('Clear');
  6184. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6185. $this->assertEquals(['save'], $result);
  6186. }
  6187. /**
  6188. * Test generation of a form button with confirm message.
  6189. */
  6190. public function testButtonWithConfirm(): void
  6191. {
  6192. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6193. $expected = ['button' => [
  6194. 'type' => 'submit',
  6195. 'data-confirm-message' => 'Confirm me!',
  6196. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6197. ], 'Hi', '/button'];
  6198. $this->assertHtml($expected, $result);
  6199. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6200. $expected = ['button' => [
  6201. 'type' => 'submit',
  6202. 'data-confirm-message' => 'Confirm "me"!',
  6203. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6204. ], 'Hi', '/button'];
  6205. $this->assertHtml($expected, $result);
  6206. }
  6207. /**
  6208. * testPostButton method
  6209. */
  6210. public function testPostButton(): void
  6211. {
  6212. $result = $this->Form->postButton('Hi', '/controller/action');
  6213. $expected = [
  6214. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6215. 'button' => ['type' => 'submit'],
  6216. 'Hi',
  6217. '/button',
  6218. '/form',
  6219. ];
  6220. $this->assertHtml($expected, $result);
  6221. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6222. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6223. }
  6224. /**
  6225. * testPostButtonMethodType method
  6226. */
  6227. public function testPostButtonMethodType(): void
  6228. {
  6229. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6230. $expected = [
  6231. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6232. 'div' => ['style' => 'display:none;'],
  6233. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6234. '/div',
  6235. 'button' => ['type' => 'submit'],
  6236. 'Hi',
  6237. '/button',
  6238. '/form',
  6239. ];
  6240. $this->assertHtml($expected, $result);
  6241. }
  6242. /**
  6243. * testPostButtonFormOptions method
  6244. */
  6245. public function testPostButtonFormOptions(): void
  6246. {
  6247. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6248. $expected = [
  6249. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6250. 'button' => ['type' => 'submit'],
  6251. 'Hi',
  6252. '/button',
  6253. '/form',
  6254. ];
  6255. $this->assertHtml($expected, $result);
  6256. }
  6257. /**
  6258. * testPostButtonNestedData method
  6259. *
  6260. * Test using postButton with N dimensional data.
  6261. */
  6262. public function testPostButtonNestedData(): void
  6263. {
  6264. $data = [
  6265. 'one' => [
  6266. 'two' => [
  6267. 3, 4, 5,
  6268. ],
  6269. ],
  6270. ];
  6271. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6272. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6273. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6274. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6275. }
  6276. /**
  6277. * testSecurePostButton method
  6278. *
  6279. * Test that postButton adds _Token fields.
  6280. */
  6281. public function testSecurePostButton(): void
  6282. {
  6283. $this->View->setRequest($this->View->getRequest()
  6284. ->withAttribute('csrfToken', 'testkey')
  6285. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6286. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6287. $tokenDebug = urlencode(json_encode([
  6288. '/posts/delete/1',
  6289. [],
  6290. [],
  6291. ]));
  6292. $expected = [
  6293. 'form' => [
  6294. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6295. ],
  6296. ['div' => ['style' => 'display:none;']],
  6297. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6298. '/div',
  6299. 'button' => ['type' => 'submit'],
  6300. 'Delete',
  6301. '/button',
  6302. ['div' => ['style' => 'display:none;']],
  6303. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6304. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6305. ['input' => [
  6306. 'type' => 'hidden',
  6307. 'name' => '_Token[debug]',
  6308. 'value' => $tokenDebug,
  6309. ]],
  6310. '/div',
  6311. '/form',
  6312. ];
  6313. $this->assertHtml($expected, $result);
  6314. }
  6315. /**
  6316. * testPostLink method
  6317. */
  6318. public function testPostLink(): void
  6319. {
  6320. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6321. $expected = [
  6322. 'form' => [
  6323. 'method' => 'post', 'action' => '/posts/delete/1',
  6324. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6325. ],
  6326. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6327. '/form',
  6328. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6329. 'Delete',
  6330. '/a',
  6331. ];
  6332. $this->assertHtml($expected, $result);
  6333. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6334. $expected = [
  6335. 'form' => [
  6336. 'method' => 'post', 'action' => '/posts/delete/1',
  6337. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6338. ],
  6339. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6340. '/form',
  6341. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6342. 'Delete',
  6343. '/a',
  6344. ];
  6345. $this->assertHtml($expected, $result);
  6346. $result = $this->Form->postLink(
  6347. 'Delete',
  6348. '/posts/delete/1',
  6349. ['target' => '_blank', 'class' => 'btn btn-danger']
  6350. );
  6351. $expected = [
  6352. 'form' => [
  6353. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6354. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6355. ],
  6356. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6357. '/form',
  6358. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6359. 'Delete',
  6360. '/a',
  6361. ];
  6362. $this->assertHtml($expected, $result);
  6363. }
  6364. /**
  6365. * testPostLinkWithConfirm method
  6366. *
  6367. * Test the confirm option for postLink().
  6368. */
  6369. public function testPostLinkWithConfirm(): void
  6370. {
  6371. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6372. $expected = [
  6373. 'form' => [
  6374. 'method' => 'post', 'action' => '/posts/delete/1',
  6375. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6376. ],
  6377. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6378. '/form',
  6379. 'a' => [
  6380. 'href' => '#',
  6381. 'data-confirm-message' => 'Confirm?',
  6382. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6383. ],
  6384. 'Delete',
  6385. '/a',
  6386. ];
  6387. $this->assertHtml($expected, $result);
  6388. $result = $this->Form->postLink(
  6389. 'Delete',
  6390. '/posts/delete/1',
  6391. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6392. );
  6393. $expected = [
  6394. 'form' => [
  6395. 'method' => 'post', 'action' => '/posts/delete/1',
  6396. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6397. ],
  6398. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6399. '/form',
  6400. 'a' => [
  6401. 'href' => '#',
  6402. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6403. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6404. ],
  6405. 'Delete',
  6406. '/a',
  6407. ];
  6408. $this->assertHtml($expected, $result);
  6409. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6410. $result = $this->Form->postLink(
  6411. 'Delete',
  6412. '/posts/delete/1',
  6413. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6414. );
  6415. $expected = [
  6416. 'form' => [
  6417. 'method' => 'post', 'action' => '/posts/delete/1',
  6418. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6419. ],
  6420. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6421. '/form',
  6422. 'a' => [
  6423. 'href' => '#',
  6424. 'data-confirm-message' => 'Confirm this deletion?',
  6425. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6426. ],
  6427. 'Delete',
  6428. '/a',
  6429. ];
  6430. $this->assertHtml($expected, $result);
  6431. }
  6432. /**
  6433. * testPostLinkWithQuery method
  6434. *
  6435. * Test postLink() with query string args.
  6436. */
  6437. public function testPostLinkWithQuery(): void
  6438. {
  6439. $result = $this->Form->postLink(
  6440. 'Delete',
  6441. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6442. );
  6443. $expected = [
  6444. 'form' => [
  6445. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6446. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6447. ],
  6448. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6449. '/form',
  6450. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6451. 'Delete',
  6452. '/a',
  6453. ];
  6454. $this->assertHtml($expected, $result);
  6455. }
  6456. /**
  6457. * testPostLinkWithData method
  6458. *
  6459. * Test postLink with additional data.
  6460. */
  6461. public function testPostLinkWithData(): void
  6462. {
  6463. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6464. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6465. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6466. $this->Form->create($entity);
  6467. $this->Form->end();
  6468. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6469. $this->assertStringContainsString(
  6470. '<input type="hidden" name="name" value="show"',
  6471. $result,
  6472. 'should not contain entity data.'
  6473. );
  6474. }
  6475. /**
  6476. * testPostLinkSecurityHash method
  6477. *
  6478. * Test that security hashes for postLink include the url.
  6479. */
  6480. public function testPostLinkSecurityHash(): void
  6481. {
  6482. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6483. $hash .= '%3Aid';
  6484. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6485. $result = $this->Form->postLink(
  6486. 'Delete',
  6487. '/posts/delete/1',
  6488. ['data' => ['id' => 1]]
  6489. );
  6490. $tokenDebug = urlencode(json_encode([
  6491. '/posts/delete/1',
  6492. [
  6493. 'id' => 1,
  6494. ],
  6495. [],
  6496. ]));
  6497. $expected = [
  6498. 'form' => [
  6499. 'method' => 'post', 'action' => '/posts/delete/1',
  6500. 'name', 'style' => 'display:none;',
  6501. ],
  6502. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6503. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6504. 'div' => ['style' => 'display:none;'],
  6505. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6506. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6507. ['input' => [
  6508. 'type' => 'hidden',
  6509. 'name' => '_Token[debug]',
  6510. 'value' => $tokenDebug,
  6511. ]],
  6512. '/div',
  6513. '/form',
  6514. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6515. 'Delete',
  6516. '/a',
  6517. ];
  6518. $this->assertHtml($expected, $result);
  6519. }
  6520. /**
  6521. * testPostLinkSecurityHashBlockMode method
  6522. *
  6523. * Test that postLink doesn't modify the fields in the containing form.
  6524. *
  6525. * postLink() calls inside open forms should not modify the field list
  6526. * for the form.
  6527. */
  6528. public function testPostLinkSecurityHashBlockMode(): void
  6529. {
  6530. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6531. $hash .= '%3A';
  6532. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6533. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6534. $this->Form->control('title');
  6535. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6536. $result = $this->View->fetch('postLink');
  6537. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6538. $this->assertEquals(['title'], $fields);
  6539. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6540. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6541. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6542. }
  6543. /**
  6544. * testPostLinkSecurityHashNoDebugMode method
  6545. *
  6546. * Test that security does not include debug token if debug is false.
  6547. */
  6548. public function testPostLinkSecurityHashNoDebugMode(): void
  6549. {
  6550. Configure::write('debug', false);
  6551. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6552. $hash .= '%3Aid';
  6553. $this->View->setRequest($this->View->getRequest()
  6554. ->withAttribute('formTokenData', ['key' => 'test']));
  6555. $result = $this->Form->postLink(
  6556. 'Delete',
  6557. '/posts/delete/1',
  6558. ['data' => ['id' => 1]]
  6559. );
  6560. $expected = [
  6561. 'form' => [
  6562. 'method' => 'post', 'action' => '/posts/delete/1',
  6563. 'name', 'style' => 'display:none;',
  6564. ],
  6565. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6566. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6567. 'div' => ['style' => 'display:none;'],
  6568. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6569. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6570. '/div',
  6571. '/form',
  6572. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6573. 'Delete',
  6574. '/a',
  6575. ];
  6576. $this->assertHtml($expected, $result);
  6577. }
  6578. /**
  6579. * testPostLinkNestedData method
  6580. *
  6581. * Test using postLink with N dimensional data.
  6582. */
  6583. public function testPostLinkNestedData(): void
  6584. {
  6585. $data = [
  6586. 'one' => [
  6587. 'two' => [
  6588. 3, 4, 5,
  6589. ],
  6590. ],
  6591. ];
  6592. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6593. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6594. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6595. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6596. }
  6597. /**
  6598. * testPostLinkAfterGetForm method
  6599. *
  6600. * Test creating postLinks after a GET form.
  6601. */
  6602. public function testPostLinkAfterGetForm(): void
  6603. {
  6604. $this->View->setRequest($this->View->getRequest()
  6605. ->withAttribute('csrfToken', 'testkey')
  6606. ->withAttribute('formTokenData', []));
  6607. $this->Form->create($this->article, ['type' => 'get']);
  6608. $this->Form->end();
  6609. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6610. $tokenDebug = urlencode(json_encode([
  6611. '/posts/delete/1',
  6612. [],
  6613. [],
  6614. ]));
  6615. $expected = [
  6616. 'form' => [
  6617. 'method' => 'post', 'action' => '/posts/delete/1',
  6618. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6619. ],
  6620. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6621. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6622. 'div' => ['style' => 'display:none;'],
  6623. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6624. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6625. ['input' => [
  6626. 'type' => 'hidden', 'name' => '_Token[debug]',
  6627. 'value' => $tokenDebug,
  6628. ]],
  6629. '/div',
  6630. '/form',
  6631. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6632. 'Delete',
  6633. '/a',
  6634. ];
  6635. $this->assertHtml($expected, $result);
  6636. }
  6637. /**
  6638. * testPostLinkFormBuffer method
  6639. *
  6640. * Test that postLink adds form tags to view block.
  6641. */
  6642. public function testPostLinkFormBuffer(): void
  6643. {
  6644. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6645. $expected = [
  6646. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6647. 'Delete',
  6648. '/a',
  6649. ];
  6650. $this->assertHtml($expected, $result);
  6651. $result = $this->View->fetch('postLink');
  6652. $expected = [
  6653. 'form' => [
  6654. 'method' => 'post', 'action' => '/posts/delete/1',
  6655. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6656. ],
  6657. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6658. '/form',
  6659. ];
  6660. $this->assertHtml($expected, $result);
  6661. $result = $this->Form->postLink(
  6662. 'Delete',
  6663. '/posts/delete/2',
  6664. ['block' => true, 'method' => 'DELETE']
  6665. );
  6666. $expected = [
  6667. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6668. 'Delete',
  6669. '/a',
  6670. ];
  6671. $this->assertHtml($expected, $result);
  6672. $result = $this->View->fetch('postLink');
  6673. $expected = [
  6674. 'form' => [
  6675. 'method' => 'post', 'action' => '/posts/delete/1',
  6676. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6677. ],
  6678. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6679. '/form',
  6680. [
  6681. 'form' => [
  6682. 'method' => 'post', 'action' => '/posts/delete/2',
  6683. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6684. ],
  6685. ],
  6686. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6687. '/form',
  6688. ];
  6689. $this->assertHtml($expected, $result);
  6690. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6691. $expected = [
  6692. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6693. 'Delete',
  6694. '/a',
  6695. ];
  6696. $this->assertHtml($expected, $result);
  6697. $result = $this->View->fetch('foobar');
  6698. $expected = [
  6699. 'form' => [
  6700. 'method' => 'post', 'action' => '/posts/delete/1',
  6701. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6702. ],
  6703. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6704. '/form',
  6705. ];
  6706. $this->assertHtml($expected, $result);
  6707. }
  6708. /**
  6709. * testSubmitButton method
  6710. */
  6711. public function testSubmitButton(): void
  6712. {
  6713. $result = $this->Form->submit('');
  6714. $expected = [
  6715. 'div' => ['class' => 'submit'],
  6716. 'input' => ['type' => 'submit', 'value' => ''],
  6717. '/div',
  6718. ];
  6719. $this->assertHtml($expected, $result);
  6720. $result = $this->Form->submit('Test Submit');
  6721. $expected = [
  6722. 'div' => ['class' => 'submit'],
  6723. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6724. '/div',
  6725. ];
  6726. $this->assertHtml($expected, $result);
  6727. $result = $this->Form->submit('Next >');
  6728. $expected = [
  6729. 'div' => ['class' => 'submit'],
  6730. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6731. '/div',
  6732. ];
  6733. $this->assertHtml($expected, $result);
  6734. $result = $this->Form->submit('Next >', ['escape' => false]);
  6735. $expected = [
  6736. 'div' => ['class' => 'submit'],
  6737. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6738. '/div',
  6739. ];
  6740. $this->assertHtml($expected, $result);
  6741. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6742. $expected = [
  6743. 'div' => ['class' => 'submit'],
  6744. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6745. '/div',
  6746. ];
  6747. $this->assertHtml($expected, $result);
  6748. }
  6749. /**
  6750. * testSubmitImage method
  6751. *
  6752. * Test image submit types.
  6753. */
  6754. public function testSubmitImage(): void
  6755. {
  6756. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6757. $expected = [
  6758. 'div' => ['class' => 'submit'],
  6759. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6760. '/div',
  6761. ];
  6762. $this->assertHtml($expected, $result);
  6763. $result = $this->Form->submit('/relative/cake.power.gif');
  6764. $expected = [
  6765. 'div' => ['class' => 'submit'],
  6766. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6767. '/div',
  6768. ];
  6769. $this->assertHtml($expected, $result);
  6770. $result = $this->Form->submit('cake.power.gif');
  6771. $expected = [
  6772. 'div' => ['class' => 'submit'],
  6773. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6774. '/div',
  6775. ];
  6776. $this->assertHtml($expected, $result);
  6777. $result = $this->Form->submit('Not.an.image');
  6778. $expected = [
  6779. 'div' => ['class' => 'submit'],
  6780. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6781. '/div',
  6782. ];
  6783. $this->assertHtml($expected, $result);
  6784. }
  6785. /**
  6786. * testSubmitUnlockedByDefault method
  6787. *
  6788. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6789. * be submitted at a time.
  6790. */
  6791. public function testSubmitUnlockedByDefault(): void
  6792. {
  6793. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6794. $this->Form->create();
  6795. $this->Form->submit('Go go');
  6796. $this->Form->submit('Save', ['name' => 'save']);
  6797. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6798. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6799. }
  6800. /**
  6801. * testSubmitImageTimestamp method
  6802. *
  6803. * Test submit image with timestamps.
  6804. */
  6805. public function testSubmitImageTimestamp(): void
  6806. {
  6807. Configure::write('Asset.timestamp', 'force');
  6808. $result = $this->Form->submit('cake.power.gif');
  6809. $expected = [
  6810. 'div' => ['class' => 'submit'],
  6811. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6812. '/div',
  6813. ];
  6814. $this->assertHtml($expected, $result);
  6815. }
  6816. /**
  6817. * testDateTimeWithGetForms method
  6818. *
  6819. * Test that datetime() works with GET style forms.
  6820. */
  6821. public function testDateTimeWithGetForms(): void
  6822. {
  6823. $this->Form->create($this->article, ['type' => 'get']);
  6824. $result = $this->Form->datetime('created');
  6825. $expected = [
  6826. 'input' => [
  6827. 'type' => 'datetime-local',
  6828. 'name' => 'created',
  6829. 'value' => '',
  6830. 'step' => '1',
  6831. ],
  6832. ];
  6833. $this->assertHtml($expected, $result);
  6834. $result = $this->Form->datetime('created', ['default' => true]);
  6835. $expected = [
  6836. 'input' => [
  6837. 'type' => 'datetime-local',
  6838. 'name' => 'created',
  6839. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6840. 'step' => '1',
  6841. ],
  6842. ];
  6843. $this->assertHtml($expected, $result);
  6844. }
  6845. /**
  6846. * Provides fractional schema types
  6847. *
  6848. * @return array
  6849. */
  6850. public static function fractionalTypeProvider(): array
  6851. {
  6852. return [
  6853. ['datetimefractional'],
  6854. ['timestampfractional'],
  6855. ['timestamptimezone'],
  6856. ];
  6857. }
  6858. /**
  6859. * testDateTimeWithFractional method
  6860. *
  6861. * Test that datetime() works with datetimefractional.
  6862. *
  6863. * @dataProvider fractionalTypeProvider
  6864. */
  6865. public function testDateTimeWithFractional(string $type): void
  6866. {
  6867. $this->Form->create([
  6868. 'schema' => [
  6869. 'created' => ['type' => $type],
  6870. ],
  6871. ]);
  6872. $result = $this->Form->datetime('created', [
  6873. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6874. ]);
  6875. $expected = [
  6876. 'input' => [
  6877. 'type' => 'datetime-local',
  6878. 'name' => 'created',
  6879. 'value' => '2019-09-27T02:52:43.123',
  6880. 'step' => '0.001',
  6881. ],
  6882. ];
  6883. $this->assertHtml($expected, $result);
  6884. }
  6885. /**
  6886. * testControlWithFractional method
  6887. *
  6888. * Test that control() works with datetimefractional.
  6889. *
  6890. * @dataProvider fractionalTypeProvider
  6891. */
  6892. public function testControlWithFractional(string $type): void
  6893. {
  6894. $this->Form->create([
  6895. 'schema' => [
  6896. 'created' => ['type' => $type],
  6897. ],
  6898. ]);
  6899. $result = $this->Form->control('created', [
  6900. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6901. ]);
  6902. $expected = [
  6903. 'div' => ['class' => 'input datetime'],
  6904. 'label' => ['for' => 'created'],
  6905. 'Created',
  6906. '/label',
  6907. 'input' => [
  6908. 'type' => 'datetime-local',
  6909. 'name' => 'created',
  6910. 'id' => 'created',
  6911. 'value' => '2019-09-27T02:52:43.123',
  6912. 'step' => '0.001',
  6913. ],
  6914. '/div',
  6915. ];
  6916. $this->assertHtml($expected, $result);
  6917. }
  6918. /**
  6919. * testForMagicControlNonExistentNotValidated method
  6920. */
  6921. public function testForMagicControlNonExistentNotValidated(): void
  6922. {
  6923. $this->Form->create($this->article);
  6924. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6925. $result = $this->Form->control('nonexistent_not_validated');
  6926. $expected = [
  6927. 'label' => ['for' => 'nonexistent-not-validated'],
  6928. 'Nonexistent Not Validated',
  6929. '/label',
  6930. 'input' => [
  6931. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6932. 'id' => 'nonexistent-not-validated',
  6933. ],
  6934. ];
  6935. $this->assertHtml($expected, $result);
  6936. $result = $this->Form->control('nonexistent_not_validated', [
  6937. 'val' => 'my value',
  6938. ]);
  6939. $expected = [
  6940. 'label' => ['for' => 'nonexistent-not-validated'],
  6941. 'Nonexistent Not Validated',
  6942. '/label',
  6943. 'input' => [
  6944. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6945. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6946. ],
  6947. ];
  6948. $this->assertHtml($expected, $result);
  6949. $this->View->setRequest(
  6950. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6951. );
  6952. $this->Form->create($this->article);
  6953. $result = $this->Form->control('nonexistent_not_validated');
  6954. $expected = [
  6955. 'label' => ['for' => 'nonexistent-not-validated'],
  6956. 'Nonexistent Not Validated',
  6957. '/label',
  6958. 'input' => [
  6959. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6960. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6961. ],
  6962. ];
  6963. $this->assertHtml($expected, $result);
  6964. }
  6965. /**
  6966. * testFormMagicControlLabel method
  6967. */
  6968. public function testFormMagicControlLabel(): void
  6969. {
  6970. $this->getTableLocator()->get('Contacts', [
  6971. 'className' => ContactsTable::class,
  6972. ]);
  6973. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6974. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6975. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6976. $expected = [
  6977. 'label' => ['for' => 'contacts-name'],
  6978. 'My label',
  6979. '/label',
  6980. 'input' => [
  6981. 'type' => 'text',
  6982. 'name' => 'Contacts[name]',
  6983. 'id' => 'contacts-name',
  6984. 'maxlength' => '255',
  6985. ],
  6986. ];
  6987. $this->assertHtml($expected, $result);
  6988. $result = $this->Form->control('name', [
  6989. 'label' => ['class' => 'mandatory'],
  6990. ]);
  6991. $expected = [
  6992. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6993. 'Name',
  6994. '/label',
  6995. 'input' => [
  6996. 'type' => 'text', 'name' => 'name',
  6997. 'id' => 'name', 'maxlength' => '255',
  6998. ],
  6999. ];
  7000. $this->assertHtml($expected, $result);
  7001. $result = $this->Form->control('name', [
  7002. 'div' => false,
  7003. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  7004. ]);
  7005. $expected = [
  7006. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7007. 'My label',
  7008. '/label',
  7009. 'input' => [
  7010. 'type' => 'text', 'name' => 'name',
  7011. 'id' => 'name', 'maxlength' => '255',
  7012. ],
  7013. ];
  7014. $this->assertHtml($expected, $result);
  7015. $result = $this->Form->control('Contact.name', [
  7016. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  7017. ]);
  7018. $expected = [
  7019. 'label' => ['for' => 'my_id'],
  7020. 'Name',
  7021. '/label',
  7022. 'input' => [
  7023. 'type' => 'text', 'name' => 'Contact[name]',
  7024. 'id' => 'my_id', 'maxlength' => '255',
  7025. ],
  7026. ];
  7027. $this->assertHtml($expected, $result);
  7028. $result = $this->Form->control('1.id');
  7029. $expected = ['input' => [
  7030. 'type' => 'hidden', 'name' => '1[id]',
  7031. 'id' => '1-id',
  7032. ]];
  7033. $this->assertHtml($expected, $result);
  7034. $result = $this->Form->control('1.name');
  7035. $expected = [
  7036. 'label' => ['for' => '1-name'],
  7037. 'Name',
  7038. '/label',
  7039. 'input' => [
  7040. 'type' => 'text', 'name' => '1[name]',
  7041. 'id' => '1-name', 'maxlength' => '255',
  7042. ],
  7043. ];
  7044. $this->assertHtml($expected, $result);
  7045. }
  7046. /**
  7047. * testFormEnd method
  7048. */
  7049. public function testFormEnd(): void
  7050. {
  7051. $this->assertSame('</form>', $this->Form->end());
  7052. }
  7053. /**
  7054. * testMultiRecordForm method
  7055. *
  7056. * Test the generation of fields for a multi record form.
  7057. */
  7058. public function testMultiRecordForm(): void
  7059. {
  7060. $articles = $this->getTableLocator()->get('Articles');
  7061. $articles->hasMany('Comments');
  7062. $comment = new Entity(['comment' => 'Value']);
  7063. $article = new Article(['comments' => [$comment]]);
  7064. $this->Form->create([$article]);
  7065. $result = $this->Form->control('0.comments.1.comment');
  7066. // phpcs:disable
  7067. $expected = [
  7068. 'div' => ['class' => 'input textarea'],
  7069. 'label' => ['for' => '0-comments-1-comment'],
  7070. 'Comment',
  7071. '/label',
  7072. 'textarea' => [
  7073. 'name',
  7074. 'id' => '0-comments-1-comment',
  7075. 'rows' => 5
  7076. ],
  7077. '/textarea',
  7078. '/div'
  7079. ];
  7080. // phpcs:enable
  7081. $this->assertHtml($expected, $result);
  7082. $result = $this->Form->control('0.comments.0.comment');
  7083. // phpcs:disable
  7084. $expected = [
  7085. 'div' => ['class' => 'input textarea'],
  7086. 'label' => ['for' => '0-comments-0-comment'],
  7087. 'Comment',
  7088. '/label',
  7089. 'textarea' => [
  7090. 'name',
  7091. 'id' => '0-comments-0-comment',
  7092. 'rows' => 5
  7093. ],
  7094. 'Value',
  7095. '/textarea',
  7096. '/div'
  7097. ];
  7098. // phpcs:enable
  7099. $this->assertHtml($expected, $result);
  7100. $comment->setError('comment', ['Not valid']);
  7101. $result = $this->Form->control('0.comments.0.comment');
  7102. // phpcs:disable
  7103. $expected = [
  7104. 'div' => ['class' => 'input textarea error'],
  7105. 'label' => ['for' => '0-comments-0-comment'],
  7106. 'Comment',
  7107. '/label',
  7108. 'textarea' => [
  7109. 'name',
  7110. 'class' => 'form-error',
  7111. 'id' => '0-comments-0-comment',
  7112. 'aria-invalid' => 'true',
  7113. 'aria-describedby' => '0-comments-0-comment-error',
  7114. 'rows' => 5
  7115. ],
  7116. 'Value',
  7117. '/textarea',
  7118. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  7119. 'Not valid',
  7120. '/div',
  7121. '/div'
  7122. ];
  7123. // phpcs:enable
  7124. $this->assertHtml($expected, $result);
  7125. $this->getTableLocator()->get('Comments')
  7126. ->getValidator('default')
  7127. ->allowEmptyString('comment', null, false);
  7128. $result = $this->Form->control('0.comments.1.comment');
  7129. // phpcs:disable
  7130. $expected = [
  7131. 'div' => ['class' => 'input textarea required'],
  7132. 'label' => ['for' => '0-comments-1-comment'],
  7133. 'Comment',
  7134. '/label',
  7135. 'textarea' => [
  7136. 'name',
  7137. 'aria-required' => 'true',
  7138. 'required' => 'required',
  7139. 'id' => '0-comments-1-comment',
  7140. 'rows' => 5,
  7141. 'data-validity-message' => 'This field cannot be left empty',
  7142. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7143. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7144. ],
  7145. '/textarea',
  7146. '/div'
  7147. ];
  7148. // phpcs:enable
  7149. $this->assertHtml($expected, $result);
  7150. }
  7151. /**
  7152. * testHtml5Controls method
  7153. *
  7154. * Test that some html5 inputs + FormHelper::__call() work.
  7155. */
  7156. public function testHtml5Controls(): void
  7157. {
  7158. $result = $this->Form->email('User.email');
  7159. $expected = [
  7160. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7161. ];
  7162. $this->assertHtml($expected, $result);
  7163. $result = $this->Form->search('User.query');
  7164. $expected = [
  7165. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7166. ];
  7167. $this->assertHtml($expected, $result);
  7168. $result = $this->Form->search('User.query', ['value' => 'test']);
  7169. $expected = [
  7170. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7171. ];
  7172. $this->assertHtml($expected, $result);
  7173. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7174. $expected = [
  7175. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7176. ];
  7177. $this->assertHtml($expected, $result);
  7178. }
  7179. /**
  7180. * testHtml5ControlWithControl method
  7181. *
  7182. * Test accessing html5 inputs through control().
  7183. */
  7184. public function testHtml5ControlWithControl(): void
  7185. {
  7186. $this->Form->create();
  7187. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7188. $result = $this->Form->control('website', [
  7189. 'type' => 'url',
  7190. 'val' => 'http://domain.tld',
  7191. 'label' => false,
  7192. ]);
  7193. $expected = [
  7194. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7195. ];
  7196. $this->assertHtml($expected, $result);
  7197. }
  7198. /**
  7199. * testHtml5ControlException method
  7200. *
  7201. * Test errors when field name is missing.
  7202. */
  7203. public function testHtml5ControlException(): void
  7204. {
  7205. $this->expectException(CakeException::class);
  7206. $this->Form->email();
  7207. }
  7208. /**
  7209. * tests fields that are required use custom validation messages
  7210. */
  7211. public function testHtml5ErrorMessage(): void
  7212. {
  7213. $this->Form->setConfig('autoSetCustomValidity', true);
  7214. $validator = (new Validator())
  7215. ->notEmptyString('email', 'Custom error message')
  7216. ->requirePresence('password')
  7217. ->alphaNumeric('password')
  7218. ->notBlank('phone');
  7219. $table = $this->getTableLocator()->get('Contacts', [
  7220. 'className' => ContactsTable::class,
  7221. ]);
  7222. $table->setValidator('default', $validator);
  7223. $contact = new Entity();
  7224. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7225. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7226. $result = $this->Form->control('password');
  7227. $expected = [
  7228. 'label' => ['for' => 'password'],
  7229. 'Password',
  7230. '/label',
  7231. 'input' => [
  7232. 'aria-required' => 'true',
  7233. 'id' => 'password',
  7234. 'name' => 'password',
  7235. 'type' => 'password',
  7236. 'value' => '',
  7237. 'required' => 'required',
  7238. 'data-validity-message' => 'This field cannot be left empty',
  7239. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7240. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7241. ],
  7242. ];
  7243. $this->assertHtml($expected, $result);
  7244. $result = $this->Form->control('phone');
  7245. $expected = [
  7246. 'label' => ['for' => 'phone'],
  7247. 'Phone',
  7248. '/label',
  7249. 'input' => [
  7250. 'aria-required' => 'true',
  7251. 'id' => 'phone',
  7252. 'name' => 'phone',
  7253. 'type' => 'tel',
  7254. 'value' => '',
  7255. 'maxlength' => 255,
  7256. 'required' => 'required',
  7257. 'data-validity-message' => 'This field cannot be left empty',
  7258. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7259. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7260. ],
  7261. ];
  7262. $this->assertHtml($expected, $result);
  7263. $result = $this->Form->control('email');
  7264. $expected = [
  7265. 'label' => ['for' => 'email'],
  7266. 'Email',
  7267. '/label',
  7268. 'input' => [
  7269. 'aria-required' => 'true',
  7270. 'id' => 'email',
  7271. 'name' => 'email',
  7272. 'type' => 'email',
  7273. 'value' => '',
  7274. 'maxlength' => 255,
  7275. 'required' => 'required',
  7276. 'data-validity-message' => 'Custom error message',
  7277. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7278. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7279. ],
  7280. ];
  7281. $this->assertHtml($expected, $result);
  7282. }
  7283. /**
  7284. * tests that custom validation messages are in templateVars
  7285. */
  7286. public function testHtml5ErrorMessageInTemplateVars(): void
  7287. {
  7288. $validator = (new Validator())
  7289. ->notEmptyString('email', 'Custom error "message" & entities')
  7290. ->requirePresence('password')
  7291. ->alphaNumeric('password')
  7292. ->notBlank('phone');
  7293. $table = $this->getTableLocator()->get('Contacts', [
  7294. 'className' => ContactsTable::class,
  7295. ]);
  7296. $table->setValidator('default', $validator);
  7297. $contact = new Entity();
  7298. $this->Form->setConfig('autoSetCustomValidity', false);
  7299. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7300. $this->Form->setTemplates([
  7301. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7302. 'inputContainer' => '{{content}}',
  7303. ]);
  7304. $result = $this->Form->control('password');
  7305. $expected = [
  7306. 'label' => ['for' => 'password'],
  7307. 'Password',
  7308. '/label',
  7309. 'input' => [
  7310. 'aria-required' => 'true',
  7311. 'id' => 'password',
  7312. 'name' => 'password',
  7313. 'type' => 'password',
  7314. 'value' => '',
  7315. 'required' => 'required',
  7316. 'data-message' => 'This field cannot be left empty',
  7317. ],
  7318. ];
  7319. $this->assertHtml($expected, $result);
  7320. $result = $this->Form->control('phone');
  7321. $expected = [
  7322. 'label' => ['for' => 'phone'],
  7323. 'Phone',
  7324. '/label',
  7325. 'input' => [
  7326. 'aria-required' => 'true',
  7327. 'id' => 'phone',
  7328. 'name' => 'phone',
  7329. 'type' => 'tel',
  7330. 'value' => '',
  7331. 'maxlength' => 255,
  7332. 'required' => 'required',
  7333. 'data-message' => 'This field cannot be left empty',
  7334. ],
  7335. ];
  7336. $this->assertHtml($expected, $result);
  7337. $result = $this->Form->control('email', [
  7338. 'templateVars' => [
  7339. 'custom' => 'data-custom="1"',
  7340. ],
  7341. ]);
  7342. $expected = [
  7343. 'label' => ['for' => 'email'],
  7344. 'Email',
  7345. '/label',
  7346. 'input' => [
  7347. 'aria-required' => 'true',
  7348. 'id' => 'email',
  7349. 'name' => 'email',
  7350. 'type' => 'email',
  7351. 'value' => '',
  7352. 'maxlength' => 255,
  7353. 'required' => 'required',
  7354. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7355. 'data-custom' => '1',
  7356. ],
  7357. ];
  7358. $this->assertHtml($expected, $result);
  7359. $this->Form->setConfig('autoSetCustomValidity', true);
  7360. }
  7361. /**
  7362. * testRequiredAttribute method
  7363. *
  7364. * Tests that formhelper sets required attributes.
  7365. */
  7366. public function testRequiredAttribute(): void
  7367. {
  7368. $this->article['required'] = [
  7369. 'title' => true,
  7370. 'body' => false,
  7371. ];
  7372. $this->Form->create($this->article);
  7373. $result = $this->Form->control('title');
  7374. $expected = [
  7375. 'div' => ['class' => 'input text required'],
  7376. 'label' => ['for' => 'title'],
  7377. 'Title',
  7378. '/label',
  7379. 'input' => [
  7380. 'type' => 'text',
  7381. 'name' => 'title',
  7382. 'id' => 'title',
  7383. 'aria-required' => 'true',
  7384. 'required' => 'required',
  7385. 'data-validity-message' => 'This field cannot be left empty',
  7386. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7387. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7388. ],
  7389. '/div',
  7390. ];
  7391. $this->assertHtml($expected, $result);
  7392. $result = $this->Form->control('title', ['required' => false]);
  7393. $this->assertStringNotContainsString('required', $result);
  7394. $result = $this->Form->control('body');
  7395. $expected = [
  7396. 'div' => ['class' => 'input text'],
  7397. 'label' => ['for' => 'body'],
  7398. 'Body',
  7399. '/label',
  7400. 'input' => [
  7401. 'type' => 'text',
  7402. 'name' => 'body',
  7403. 'id' => 'body',
  7404. ],
  7405. '/div',
  7406. ];
  7407. $this->assertHtml($expected, $result);
  7408. $result = $this->Form->control('body', ['required' => true]);
  7409. $this->assertStringContainsString('required', $result);
  7410. }
  7411. /**
  7412. * testControlsNotNested method
  7413. *
  7414. * Tests that it is possible to put inputs outside of the label.
  7415. */
  7416. public function testControlsNotNested(): void
  7417. {
  7418. $this->Form->setTemplates([
  7419. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7420. 'formGroup' => '{{input}}{{label}}',
  7421. ]);
  7422. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7423. $expected = [
  7424. 'div' => ['class' => 'input checkbox'],
  7425. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7426. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7427. 'label' => ['for' => 'foo'],
  7428. 'Foo',
  7429. '/label',
  7430. '/div',
  7431. ];
  7432. $this->assertHtml($expected, $result);
  7433. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7434. $expected = [
  7435. 'div' => ['class' => 'input checkbox'],
  7436. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7437. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7438. '/div',
  7439. ];
  7440. $this->assertHtml($expected, $result);
  7441. $result = $this->Form->control('confirm', [
  7442. 'type' => 'radio',
  7443. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7444. ]);
  7445. $expected = [
  7446. 'div' => ['class' => 'input radio'],
  7447. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '', 'id' => 'confirm']],
  7448. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7449. ['label' => ['for' => 'confirm-y']],
  7450. 'Yes',
  7451. '/label',
  7452. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7453. ['label' => ['for' => 'confirm-n']],
  7454. 'No',
  7455. '/label',
  7456. '<label',
  7457. 'Confirm',
  7458. '/label',
  7459. '/div',
  7460. ];
  7461. $this->assertHtml($expected, $result);
  7462. $result = $this->Form->select('category', ['1', '2'], [
  7463. 'multiple' => 'checkbox',
  7464. 'name' => 'fish',
  7465. ]);
  7466. $expected = [
  7467. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  7468. ['div' => ['class' => 'checkbox']],
  7469. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7470. ['label' => ['for' => 'fish-0']],
  7471. '1',
  7472. '/label',
  7473. '/div',
  7474. ['div' => ['class' => 'checkbox']],
  7475. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7476. ['label' => ['for' => 'fish-1']],
  7477. '2',
  7478. '/label',
  7479. '/div',
  7480. ];
  7481. $this->assertHtml($expected, $result);
  7482. }
  7483. /**
  7484. * testControlContainerTemplates method
  7485. *
  7486. * Test that *Container templates are used by input.
  7487. */
  7488. public function testControlContainerTemplates(): void
  7489. {
  7490. $this->Form->setTemplates([
  7491. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7492. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7493. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7494. ]);
  7495. $this->article['errors'] = [
  7496. 'Article' => ['published' => 'error message'],
  7497. ];
  7498. $this->Form->create($this->article);
  7499. $result = $this->Form->control('accept', [
  7500. 'type' => 'checkbox',
  7501. ]);
  7502. $expected = [
  7503. 'div' => ['class' => 'check'],
  7504. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7505. 'label' => ['for' => 'accept'],
  7506. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7507. 'Accept',
  7508. '/label',
  7509. '/div',
  7510. ];
  7511. $this->assertHtml($expected, $result);
  7512. $result = $this->Form->control('accept', [
  7513. 'type' => 'radio',
  7514. 'options' => ['Y', 'N'],
  7515. ]);
  7516. $this->assertStringContainsString('<div class="rad">', $result);
  7517. $result = $this->Form->control('Article.published', [
  7518. 'type' => 'radio',
  7519. 'options' => ['Y', 'N'],
  7520. ]);
  7521. $this->assertStringContainsString('<div class="rad err">', $result);
  7522. }
  7523. /**
  7524. * testFormGroupTemplates method
  7525. *
  7526. * Test that *Container templates are used by input.
  7527. */
  7528. public function testFormGroupTemplates(): void
  7529. {
  7530. $this->Form->setTemplates([
  7531. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7532. ]);
  7533. $this->Form->create($this->article);
  7534. $result = $this->Form->control('accept', [
  7535. 'type' => 'radio',
  7536. 'options' => ['Y', 'N'],
  7537. ]);
  7538. $this->assertStringContainsString('<div class="radio">', $result);
  7539. }
  7540. /**
  7541. * testResetTemplates method
  7542. *
  7543. * Test resetting templates.
  7544. */
  7545. public function testResetTemplates(): void
  7546. {
  7547. $this->Form->setTemplates(['input' => '<input/>']);
  7548. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7549. $this->Form->resetTemplates();
  7550. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7551. }
  7552. /**
  7553. * testContext method
  7554. *
  7555. * Test the context method.
  7556. */
  7557. public function testContext(): void
  7558. {
  7559. $result = $this->Form->context();
  7560. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7561. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7562. $this->assertSame($mock, $this->Form->context($mock));
  7563. $this->assertSame($mock, $this->Form->context());
  7564. }
  7565. /**
  7566. * testAutoDomId method
  7567. */
  7568. public function testAutoDomId(): void
  7569. {
  7570. $result = $this->Form->text('field', ['id' => true]);
  7571. $expected = [
  7572. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7573. ];
  7574. $this->assertHtml($expected, $result);
  7575. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7576. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7577. $expected = [
  7578. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '', 'id' => 'field'],
  7579. ['label' => ['for' => 'field-0']],
  7580. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7581. 'option A',
  7582. '/label',
  7583. ['label' => ['for' => 'field-1']],
  7584. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7585. 'option B',
  7586. '/label',
  7587. ];
  7588. $this->assertHtml($expected, $result);
  7589. $result = $this->Form->select(
  7590. 'multi_field',
  7591. ['first', 'second'],
  7592. ['multiple' => 'checkbox', 'id' => true]
  7593. );
  7594. $expected = [
  7595. 'input' => [
  7596. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '', 'id' => 'multi-field',
  7597. ],
  7598. ['div' => ['class' => 'checkbox']],
  7599. ['label' => ['for' => 'multi-field-0']],
  7600. ['input' => [
  7601. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7602. 'value' => '0', 'id' => 'multi-field-0',
  7603. ]],
  7604. 'first',
  7605. '/label',
  7606. '/div',
  7607. ['div' => ['class' => 'checkbox']],
  7608. ['label' => ['for' => 'multi-field-1']],
  7609. ['input' => [
  7610. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7611. 'value' => '1', 'id' => 'multi-field-1',
  7612. ]],
  7613. 'second',
  7614. '/label',
  7615. '/div',
  7616. ];
  7617. $this->assertHtml($expected, $result);
  7618. }
  7619. /**
  7620. * Test the basic setters and getters for value sources
  7621. */
  7622. public function testFormValueSourcesSettersGetters(): void
  7623. {
  7624. $this->View->setRequest($this->View->getRequest()
  7625. ->withData('id', '1')
  7626. ->withQueryParams(['id' => '2']));
  7627. $expected = ['data', 'context'];
  7628. $result = $this->Form->getValueSources();
  7629. $this->assertEquals($expected, $result);
  7630. $this->Form->setValueSources(['context']);
  7631. $result = $this->Form->getSourceValue('id');
  7632. $this->assertNull($result);
  7633. $this->Form->setValueSources('query');
  7634. $expected = ['query'];
  7635. $result = $this->Form->getValueSources();
  7636. $this->assertEquals($expected, $result);
  7637. $expected = '2';
  7638. $result = $this->Form->getSourceValue('id');
  7639. $this->assertSame($expected, $result);
  7640. $this->Form->setValueSources(['data']);
  7641. $expected = '1';
  7642. $result = $this->Form->getSourceValue('id');
  7643. $this->assertSame($expected, $result);
  7644. $this->Form->setValueSources(['query', 'data']);
  7645. $expected = '2';
  7646. $result = $this->Form->getSourceValue('id');
  7647. $this->assertSame($expected, $result);
  7648. }
  7649. public function testValueSourcesValidation(): void
  7650. {
  7651. $this->expectException(InvalidArgumentException::class);
  7652. $this->expectExceptionMessage('Invalid value source(s): `invalid`, `foo`. Valid values are: `context`, `data`, `query`.');
  7653. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7654. }
  7655. /**
  7656. * Tests the different input rendering values based on sources values switching
  7657. */
  7658. public function testFormValueSourcesSingleSwitchRendering(): void
  7659. {
  7660. $articles = $this->getTableLocator()->get('Articles');
  7661. $article = new Article();
  7662. $articles->patchEntity($article, ['id' => '3']);
  7663. $this->Form->create($article);
  7664. $this->Form->setValueSources(['context']);
  7665. $result = $this->Form->control('id');
  7666. $expected = [
  7667. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7668. ];
  7669. $this->assertHtml($expected, $result);
  7670. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7671. $this->Form->setValueSources(['query']);
  7672. $result = $this->Form->control('id');
  7673. $expected = [
  7674. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7675. ];
  7676. $this->assertHtml($expected, $result);
  7677. $this->View->setRequest($this->View->getRequest()
  7678. ->withQueryParams(['id' => '5a'])
  7679. ->withData('id', '5b'));
  7680. $this->Form->setValueSources(['context']);
  7681. $this->Form->create($article);
  7682. $result = $this->Form->control('id');
  7683. $expected = [
  7684. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7685. ];
  7686. $this->assertHtml($expected, $result);
  7687. $this->Form->setValueSources(['data']);
  7688. $this->Form->create($article);
  7689. $result = $this->Form->control('id');
  7690. $expected = [
  7691. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7692. ];
  7693. $this->assertHtml($expected, $result);
  7694. $this->Form->setValueSources(['query']);
  7695. $result = $this->Form->control('id');
  7696. $expected = [
  7697. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7698. ];
  7699. $this->assertHtml($expected, $result);
  7700. }
  7701. /**
  7702. * Tests the different input rendering values based on sources values switching while supplying
  7703. * an entity (base context) and multiple sources (such as data, query)
  7704. */
  7705. public function testFormValueSourcesListSwitchRendering(): void
  7706. {
  7707. $articles = $this->getTableLocator()->get('Articles');
  7708. $article = new Article();
  7709. $articles->patchEntity($article, ['id' => '3']);
  7710. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7711. $this->Form->create($article);
  7712. $this->Form->setValueSources(['context', 'query']);
  7713. $result = $this->Form->control('id');
  7714. $expected = [
  7715. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7716. ];
  7717. $this->assertHtml($expected, $result);
  7718. $this->Form->setValueSources(['query', 'context']);
  7719. $result = $this->Form->control('id');
  7720. $expected = [
  7721. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7722. ];
  7723. $this->assertHtml($expected, $result);
  7724. $this->Form->setValueSources(['data', 'query', 'context']);
  7725. $result = $this->Form->control('id');
  7726. $expected = [
  7727. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7728. ];
  7729. $this->assertHtml($expected, $result);
  7730. $this->View->setRequest($this->View->getRequest()
  7731. ->withData('id', '8')
  7732. ->withQueryParams(['id' => '9']));
  7733. $this->Form->setValueSources(['data', 'query', 'context']);
  7734. $result = $this->Form->control('id');
  7735. $expected = [
  7736. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7737. ];
  7738. $this->assertHtml($expected, $result);
  7739. }
  7740. /**
  7741. * Test the different form input renderings based on values sources switchings through form options
  7742. */
  7743. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7744. {
  7745. $articles = $this->getTableLocator()->get('Articles');
  7746. $article = new Article();
  7747. $articles->patchEntity($article, ['id' => '3']);
  7748. $this->View->setRequest(
  7749. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7750. );
  7751. $this->Form->create($article, ['valueSources' => 'query']);
  7752. $result = $this->Form->control('id');
  7753. $expected = [
  7754. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7755. ];
  7756. $this->assertHtml($expected, $result);
  7757. $result = $this->Form->getSourceValue('id');
  7758. $this->assertSame('5', $result);
  7759. $this->Form->setValueSources(['context']);
  7760. $this->Form->create($article, ['valueSources' => 'query']);
  7761. $result = $this->Form->control('id');
  7762. $expected = [
  7763. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7764. ];
  7765. $this->assertHtml($expected, $result);
  7766. $result = $this->Form->getSourceValue('id');
  7767. $this->assertSame('5', $result);
  7768. $this->Form->setValueSources(['query']);
  7769. $this->Form->create($article, ['valueSources' => 'data']);
  7770. $result = $this->Form->control('id');
  7771. $expected = [
  7772. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7773. ];
  7774. $this->assertHtml($expected, $result);
  7775. $result = $this->Form->getSourceValue('id');
  7776. $this->assertSame('4', $result);
  7777. $this->Form->setValueSources(['query']);
  7778. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7779. $result = $this->Form->control('id');
  7780. $expected = [
  7781. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7782. ];
  7783. $this->assertHtml($expected, $result);
  7784. $result = $this->Form->getSourceValue('id');
  7785. $this->assertSame(3, $result);
  7786. }
  7787. /**
  7788. * Test the different form input renderings based on values sources switchings through form options
  7789. */
  7790. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7791. {
  7792. $articles = $this->getTableLocator()->get('Articles');
  7793. $article = new Article();
  7794. $articles->patchEntity($article, ['id' => '3']);
  7795. $this->View->setRequest(
  7796. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7797. );
  7798. $this->Form->setValueSources(['context'])
  7799. ->create($article, ['valueSources' => ['query', 'data']]);
  7800. $result = $this->Form->control('id');
  7801. $expected = [
  7802. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7803. ];
  7804. $this->assertHtml($expected, $result);
  7805. $result = $this->Form->getSourceValue('id');
  7806. $this->assertSame('11', $result);
  7807. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7808. $this->Form->setValueSources(['context'])
  7809. ->create($article, ['valueSources' => ['query', 'data']]);
  7810. $result = $this->Form->control('id');
  7811. $expected = [
  7812. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7813. ];
  7814. $this->assertHtml($expected, $result);
  7815. $result = $this->Form->getSourceValue('id');
  7816. $this->assertSame('10', $result);
  7817. }
  7818. /**
  7819. * Test the different form values sources resetting through From::end();
  7820. */
  7821. public function testFormValueSourcesResetViaEnd(): void
  7822. {
  7823. $expected = ['data', 'context'];
  7824. $result = $this->Form->getValueSources();
  7825. $this->assertEquals($expected, $result);
  7826. $expected = ['query', 'context', 'data'];
  7827. $this->Form->setValueSources(['query', 'context', 'data']);
  7828. $result = $this->Form->getValueSources();
  7829. $this->assertEquals($expected, $result);
  7830. $this->Form->create();
  7831. $result = $this->Form->getValueSources();
  7832. $this->assertEquals($expected, $result);
  7833. $this->Form->end();
  7834. $result = $this->Form->getValueSources();
  7835. $this->assertEquals(['data', 'context'], $result);
  7836. }
  7837. /**
  7838. * Test sources values defaults handling
  7839. */
  7840. public function testFormValueSourcesDefaults(): void
  7841. {
  7842. $this->View->setRequest(
  7843. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7844. );
  7845. $this->Form->create();
  7846. $result = $this->Form->password('password');
  7847. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7848. $this->assertHtml($expected, $result);
  7849. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7850. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7851. $this->assertHtml($expected, $result);
  7852. $this->Form->setValueSources('query');
  7853. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7854. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7855. $this->assertHtml($expected, $result);
  7856. $this->Form->setValueSources('data');
  7857. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7858. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7859. $this->assertHtml($expected, $result);
  7860. }
  7861. /**
  7862. * Test sources values schema defaults handling
  7863. */
  7864. public function testSourcesValueDoesntExistPassThrough(): void
  7865. {
  7866. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7867. $articles = $this->getTableLocator()->get('Articles');
  7868. $entity = $articles->newEmptyEntity();
  7869. $this->Form->create($entity);
  7870. $this->Form->setValueSources(['query', 'context']);
  7871. $result = $this->Form->getSourceValue('category');
  7872. $this->assertSame('sesame-cookies', $result);
  7873. $this->Form->setValueSources(['context', 'query']);
  7874. $result = $this->Form->getSourceValue('category');
  7875. $this->assertSame('sesame-cookies', $result);
  7876. }
  7877. /**
  7878. * testNestedLabelInput method
  7879. *
  7880. * Test the `nestedInput` parameter
  7881. */
  7882. public function testNestedLabelInput(): void
  7883. {
  7884. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7885. $expected = [
  7886. 'div' => ['class' => 'input text'],
  7887. 'label' => ['for' => 'foo'],
  7888. ['input' => [
  7889. 'type' => 'text',
  7890. 'name' => 'foo',
  7891. 'id' => 'foo',
  7892. ]],
  7893. 'Foo',
  7894. '/label',
  7895. '/div',
  7896. ];
  7897. $this->assertHtml($expected, $result);
  7898. }
  7899. /**
  7900. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7901. *
  7902. * This test makes sure `false` excludes the label from the render
  7903. */
  7904. public function testControlLabelManipulationDisableLabels(): void
  7905. {
  7906. $result = $this->Form->control('test', [
  7907. 'type' => 'radio',
  7908. 'options' => ['A', 'B'],
  7909. 'labelOptions' => false,
  7910. ]);
  7911. $expected = [
  7912. ['div' => ['class' => 'input radio']],
  7913. '<label',
  7914. 'Test',
  7915. '/label',
  7916. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7917. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7918. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7919. '/div',
  7920. ];
  7921. $this->assertHtml($expected, $result);
  7922. $result = $this->Form->control('checkbox1', [
  7923. 'label' => 'My checkboxes',
  7924. 'multiple' => 'checkbox',
  7925. 'type' => 'select',
  7926. 'options' => [
  7927. ['text' => 'First Checkbox', 'value' => 1],
  7928. ['text' => 'Second Checkbox', 'value' => 2],
  7929. ],
  7930. 'labelOptions' => false,
  7931. ]);
  7932. $expected = [
  7933. ['div' => ['class' => 'input select']],
  7934. ['label' => ['for' => 'checkbox1']],
  7935. 'My checkboxes',
  7936. '/label',
  7937. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7938. ['div' => ['class' => 'checkbox']],
  7939. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7940. '/div',
  7941. ['div' => ['class' => 'checkbox']],
  7942. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7943. '/div',
  7944. '/div',
  7945. ];
  7946. $this->assertHtml($expected, $result);
  7947. }
  7948. /**
  7949. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7950. *
  7951. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7952. * added to the class if checked.
  7953. *
  7954. * Also checks to make sure any custom attributes are rendered correctly
  7955. */
  7956. public function testControlLabelManipulationRadios(): void
  7957. {
  7958. $result = $this->Form->control('test', [
  7959. 'type' => 'radio',
  7960. 'options' => ['A', 'B'],
  7961. 'labelOptions' => ['class' => 'custom-class'],
  7962. ]);
  7963. $expected = [
  7964. ['div' => ['class' => 'input radio']],
  7965. '<label',
  7966. 'Test',
  7967. '/label',
  7968. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7969. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7970. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7971. 'A',
  7972. '/label',
  7973. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7974. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7975. 'B',
  7976. '/label',
  7977. '/div',
  7978. ];
  7979. $this->assertHtml($expected, $result);
  7980. $result = $this->Form->control('test', [
  7981. 'type' => 'radio',
  7982. 'options' => ['A', 'B'],
  7983. 'value' => 1,
  7984. 'labelOptions' => ['class' => 'custom-class'],
  7985. ]);
  7986. $expected = [
  7987. ['div' => ['class' => 'input radio']],
  7988. '<label',
  7989. 'Test',
  7990. '/label',
  7991. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7992. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7993. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7994. 'A',
  7995. '/label',
  7996. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7997. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7998. 'B',
  7999. '/label',
  8000. '/div',
  8001. ];
  8002. $this->assertHtml($expected, $result);
  8003. $result = $this->Form->control('test', [
  8004. 'type' => 'radio',
  8005. 'options' => ['A', 'B'],
  8006. 'value' => 1,
  8007. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  8008. ]);
  8009. $expected = [
  8010. ['div' => ['class' => 'input radio']],
  8011. '<label',
  8012. 'Test',
  8013. '/label',
  8014. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  8015. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8016. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8017. 'A',
  8018. '/label',
  8019. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8020. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8021. 'B',
  8022. '/label',
  8023. '/div',
  8024. ];
  8025. $this->assertHtml($expected, $result);
  8026. $result = $this->Form->radio('test', ['A', 'B'], [
  8027. 'label' => [
  8028. 'class' => ['custom-class', 'another-class'],
  8029. 'data-name' => 'bob',
  8030. ],
  8031. 'value' => 1,
  8032. ]);
  8033. $expected = [
  8034. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test'],
  8035. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8036. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8037. 'A',
  8038. '/label',
  8039. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8040. ['input' => [
  8041. 'type' => 'radio',
  8042. 'name' => 'test',
  8043. 'value' => '1',
  8044. 'id' => 'test-1',
  8045. 'checked' => 'checked',
  8046. ]],
  8047. 'B',
  8048. '/label',
  8049. ];
  8050. $this->assertHtml($expected, $result);
  8051. }
  8052. /**
  8053. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8054. *
  8055. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8056. * added to the class if checked.
  8057. *
  8058. * Also checks to make sure any custom attributes are rendered correctly
  8059. */
  8060. public function testControlLabelManipulationCheckboxes(): void
  8061. {
  8062. $result = $this->Form->control('checkbox1', [
  8063. 'label' => 'My checkboxes',
  8064. 'multiple' => 'checkbox',
  8065. 'type' => 'select',
  8066. 'options' => [
  8067. ['text' => 'First Checkbox', 'value' => 1],
  8068. ['text' => 'Second Checkbox', 'value' => 2],
  8069. ],
  8070. 'labelOptions' => ['class' => 'custom-class'],
  8071. 'value' => ['1'],
  8072. ]);
  8073. $expected = [
  8074. ['div' => ['class' => 'input select']],
  8075. ['label' => ['for' => 'checkbox1']],
  8076. 'My checkboxes',
  8077. '/label',
  8078. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8079. ['div' => ['class' => 'checkbox']],
  8080. ['label' => [
  8081. 'class' => 'custom-class selected',
  8082. 'for' => 'checkbox1-1',
  8083. ]],
  8084. ['input' => [
  8085. 'type' => 'checkbox',
  8086. 'name' => 'checkbox1[]',
  8087. 'value' => '1',
  8088. 'id' => 'checkbox1-1',
  8089. 'checked' => 'checked',
  8090. ]],
  8091. 'First Checkbox',
  8092. '/label',
  8093. '/div',
  8094. ['div' => ['class' => 'checkbox']],
  8095. ['label' => [
  8096. 'class' => 'custom-class',
  8097. 'for' => 'checkbox1-2',
  8098. ]],
  8099. ['input' => [
  8100. 'type' => 'checkbox',
  8101. 'name' => 'checkbox1[]',
  8102. 'value' => '2',
  8103. 'id' => 'checkbox1-2',
  8104. ]],
  8105. 'Second Checkbox',
  8106. '/label',
  8107. '/div',
  8108. '/div',
  8109. ];
  8110. $this->assertHtml($expected, $result);
  8111. $result = $this->Form->control('checkbox1', [
  8112. 'label' => 'My checkboxes',
  8113. 'multiple' => 'checkbox',
  8114. 'type' => 'select',
  8115. 'options' => [
  8116. ['text' => 'First Checkbox', 'value' => 1],
  8117. ['text' => 'Second Checkbox', 'value' => 2],
  8118. ],
  8119. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8120. 'value' => ['1'],
  8121. ]);
  8122. $expected = [
  8123. ['div' => ['class' => 'input select']],
  8124. ['label' => ['for' => 'checkbox1']],
  8125. 'My checkboxes',
  8126. '/label',
  8127. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8128. ['div' => ['class' => 'checkbox']],
  8129. ['label' => [
  8130. 'class' => 'custom-class another-class selected',
  8131. 'data-name' => 'bob',
  8132. 'for' => 'checkbox1-1',
  8133. ]],
  8134. ['input' => [
  8135. 'type' => 'checkbox',
  8136. 'name' => 'checkbox1[]',
  8137. 'value' => '1',
  8138. 'id' => 'checkbox1-1',
  8139. 'checked' => 'checked',
  8140. ]],
  8141. 'First Checkbox',
  8142. '/label',
  8143. '/div',
  8144. ['div' => ['class' => 'checkbox']],
  8145. ['label' => [
  8146. 'class' => 'custom-class another-class',
  8147. 'data-name' => 'bob',
  8148. 'for' => 'checkbox1-2',
  8149. ]],
  8150. ['input' => [
  8151. 'type' => 'checkbox',
  8152. 'name' => 'checkbox1[]',
  8153. 'value' => '2',
  8154. 'id' => 'checkbox1-2',
  8155. ]],
  8156. 'Second Checkbox',
  8157. '/label',
  8158. '/div',
  8159. '/div',
  8160. ];
  8161. $this->assertHtml($expected, $result);
  8162. }
  8163. /**
  8164. * testControlMaxLengthArrayContext method
  8165. *
  8166. * Test control() with maxlength attribute in Array Context.
  8167. */
  8168. public function testControlMaxLengthArrayContext(): void
  8169. {
  8170. $this->article['schema'] = [
  8171. 'title' => ['length' => 10],
  8172. ];
  8173. $this->Form->create($this->article);
  8174. $result = $this->Form->control('title');
  8175. $expected = [
  8176. 'div' => ['class'],
  8177. 'label' => ['for'],
  8178. 'Title',
  8179. '/label',
  8180. 'input' => [
  8181. 'aria-required' => 'true',
  8182. 'id',
  8183. 'name' => 'title',
  8184. 'type' => 'text',
  8185. 'required' => 'required',
  8186. 'maxlength' => 10,
  8187. 'data-validity-message' => 'This field cannot be left empty',
  8188. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8189. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8190. ],
  8191. '/div',
  8192. ];
  8193. $this->assertHtml($expected, $result);
  8194. }
  8195. /**
  8196. * testControlMaxLengthEntityContext method
  8197. *
  8198. * Test control() with maxlength attribute in Entity Context.
  8199. */
  8200. public function testControlMaxLengthEntityContext(): void
  8201. {
  8202. $this->article['schema']['title']['length'] = 45;
  8203. $validator = new Validator();
  8204. $validator->maxLength('title', 10);
  8205. $article = new EntityContext(
  8206. [
  8207. 'entity' => new Entity($this->article),
  8208. 'table' => new Table([
  8209. 'alias' => 'Articles',
  8210. 'schema' => $this->article['schema'],
  8211. 'validator' => $validator,
  8212. ]),
  8213. ]
  8214. );
  8215. $this->Form->create($article);
  8216. $result = $this->Form->control('title');
  8217. $expected = [
  8218. 'div' => ['class'],
  8219. 'label' => ['for'],
  8220. 'Title',
  8221. '/label',
  8222. 'input' => [
  8223. 'aria-required' => 'true',
  8224. 'id',
  8225. 'name' => 'title',
  8226. 'type' => 'text',
  8227. 'required' => 'required',
  8228. 'maxlength' => 10,
  8229. 'data-validity-message' => 'This field cannot be left empty',
  8230. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8231. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8232. ],
  8233. '/div',
  8234. ];
  8235. $this->assertHtml($expected, $result);
  8236. $this->article['schema']['title']['length'] = 45;
  8237. $validator = new Validator();
  8238. $validator->maxLength('title', 55);
  8239. $article = new EntityContext(
  8240. [
  8241. 'entity' => new Entity($this->article),
  8242. 'table' => new Table([
  8243. 'schema' => $this->article['schema'],
  8244. 'validator' => $validator,
  8245. 'alias' => 'Articles',
  8246. ]),
  8247. ]
  8248. );
  8249. $this->Form->create($article);
  8250. $result = $this->Form->control('title');
  8251. $expected = [
  8252. 'div' => ['class'],
  8253. 'label' => ['for'],
  8254. 'Title',
  8255. '/label',
  8256. 'input' => [
  8257. 'aria-required' => 'true',
  8258. 'id',
  8259. 'name' => 'title',
  8260. 'type' => 'text',
  8261. 'required' => 'required',
  8262. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8263. 'data-validity-message' => 'This field cannot be left empty',
  8264. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8265. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8266. ],
  8267. '/div',
  8268. ];
  8269. $this->assertHtml($expected, $result);
  8270. $this->article['schema']['title']['length'] = 45;
  8271. $validator = new Validator();
  8272. $validator->maxLength('title', 55);
  8273. $article = new EntityContext(
  8274. [
  8275. 'entity' => new Entity($this->article),
  8276. 'table' => new Table([
  8277. 'schema' => $this->article['schema'],
  8278. 'validator' => $validator,
  8279. 'alias' => 'Articles',
  8280. ]),
  8281. ]
  8282. );
  8283. $this->Form->create($article);
  8284. $result = $this->Form->control('title', ['maxlength' => 10]);
  8285. $expected = [
  8286. 'div' => ['class'],
  8287. 'label' => ['for'],
  8288. 'Title',
  8289. '/label',
  8290. 'input' => [
  8291. 'aria-required' => 'true',
  8292. 'id',
  8293. 'name' => 'title',
  8294. 'type' => 'text',
  8295. 'required' => 'required',
  8296. 'maxlength' => 10, // Length set in options should take highest precedence.
  8297. 'data-validity-message' => 'This field cannot be left empty',
  8298. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8299. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8300. ],
  8301. '/div',
  8302. ];
  8303. $this->assertHtml($expected, $result);
  8304. }
  8305. /**
  8306. * testControlMinMaxLengthEntityContext method
  8307. *
  8308. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8309. */
  8310. public function testControlMinMaxLengthEntityContext(): void
  8311. {
  8312. $validator = new Validator();
  8313. $validator->maxLength('title', 10);
  8314. $article = new EntityContext(
  8315. [
  8316. 'entity' => new Entity($this->article),
  8317. 'table' => new Table([
  8318. 'alias' => 'Articles',
  8319. 'schema' => $this->article['schema'],
  8320. 'validator' => $validator,
  8321. ]),
  8322. ]
  8323. );
  8324. $this->Form->create($article);
  8325. $result = $this->Form->control('title');
  8326. $expected = [
  8327. 'div' => ['class'],
  8328. 'label' => ['for'],
  8329. 'Title',
  8330. '/label',
  8331. 'input' => [
  8332. 'aria-required' => 'true',
  8333. 'id',
  8334. 'name' => 'title',
  8335. 'type' => 'text',
  8336. 'required' => 'required',
  8337. 'maxlength' => 10,
  8338. 'data-validity-message' => 'This field cannot be left empty',
  8339. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8340. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8341. ],
  8342. '/div',
  8343. ];
  8344. $this->assertHtml($expected, $result);
  8345. }
  8346. /**
  8347. * testControlMaxLengthFormContext method
  8348. *
  8349. * Test control() with maxlength attribute in Form Context.
  8350. */
  8351. public function testControlMaxLengthFormContext(): void
  8352. {
  8353. $validator = new Validator();
  8354. $validator->maxLength('title', 10);
  8355. $form = new Form();
  8356. $form->setValidator('default', $validator);
  8357. $this->Form->create($form);
  8358. $result = $this->Form->control('title');
  8359. $expected = [
  8360. 'div' => ['class'],
  8361. 'label' => ['for'],
  8362. 'Title',
  8363. '/label',
  8364. 'input' => [
  8365. 'aria-required' => 'true',
  8366. 'id',
  8367. 'name' => 'title',
  8368. 'type' => 'text',
  8369. 'required' => 'required',
  8370. 'maxlength' => 10,
  8371. 'data-validity-message' => 'This field cannot be left empty',
  8372. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8373. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8374. ],
  8375. '/div',
  8376. ];
  8377. $this->assertHtml($expected, $result);
  8378. }
  8379. }