FormHelperTest.php 308 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958
  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\Collection\Collection;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Exception\CakeException;
  21. use Cake\Form\Form;
  22. use Cake\Http\ServerRequest;
  23. use Cake\I18n\FrozenDate;
  24. use Cake\I18n\FrozenTime;
  25. use Cake\ORM\Entity;
  26. use Cake\ORM\Table;
  27. use Cake\Routing\Router;
  28. use Cake\TestSuite\TestCase;
  29. use Cake\Utility\Security;
  30. use Cake\Validation\Validator;
  31. use Cake\View\Form\EntityContext;
  32. use Cake\View\Helper\FormHelper;
  33. use Cake\View\View;
  34. use Cake\View\Widget\WidgetLocator;
  35. use InvalidArgumentException;
  36. use ReflectionProperty;
  37. use RuntimeException;
  38. use TestApp\Model\Entity\Article;
  39. use TestApp\Model\Table\ContactsTable;
  40. use TestApp\Model\Table\ValidateUsersTable;
  41. /**
  42. * FormHelperTest class
  43. *
  44. * @property \Cake\View\Helper\FormHelper $Form
  45. * @property \Cake\View\View $View
  46. */
  47. class FormHelperTest extends TestCase
  48. {
  49. /**
  50. * Fixtures to be used
  51. *
  52. * @var array<string>
  53. */
  54. protected $fixtures = ['core.Articles', 'core.Comments'];
  55. /**
  56. * @var array
  57. */
  58. protected $article = [];
  59. /**
  60. * @var string
  61. */
  62. protected $url;
  63. /**
  64. * @var \Cake\View\Helper\FormHelper
  65. */
  66. protected $Form;
  67. /**
  68. * @var \Cake\View\View
  69. */
  70. protected $View;
  71. /**
  72. * setUp method
  73. */
  74. public function setUp(): void
  75. {
  76. parent::setUp();
  77. Configure::write('Config.language', 'eng');
  78. Configure::write('App.base', '');
  79. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  80. $request = new ServerRequest([
  81. 'webroot' => '',
  82. 'base' => '',
  83. 'url' => '/articles/add',
  84. 'params' => [
  85. 'controller' => 'Articles',
  86. 'action' => 'add',
  87. 'plugin' => null,
  88. ],
  89. ]);
  90. $this->View = new View($request);
  91. Router::reload();
  92. Router::setRequest($request);
  93. $this->url = '/articles/add';
  94. $this->Form = new FormHelper($this->View);
  95. $this->article = [
  96. 'schema' => [
  97. 'id' => ['type' => 'integer'],
  98. 'author_id' => ['type' => 'integer', 'null' => true],
  99. 'title' => ['type' => 'string', 'null' => true],
  100. 'body' => 'text',
  101. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  102. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  103. ],
  104. 'required' => [
  105. 'author_id' => true,
  106. 'title' => true,
  107. ],
  108. ];
  109. Security::setSalt('foo!');
  110. $builder = Router::createRouteBuilder('/');
  111. $builder->connect('/{controller}', ['action' => 'index']);
  112. $builder->connect('/{controller}/{action}/*');
  113. }
  114. /**
  115. * tearDown method
  116. */
  117. public function tearDown(): void
  118. {
  119. parent::tearDown();
  120. unset($this->Form, $this->View);
  121. }
  122. /**
  123. * Test construct() with the templates option.
  124. */
  125. public function testConstructTemplatesFile(): void
  126. {
  127. $helper = new FormHelper($this->View, [
  128. 'templates' => 'htmlhelper_tags',
  129. ]);
  130. $result = $helper->control('name');
  131. $this->assertStringContainsString('<input', $result);
  132. }
  133. /**
  134. * Test that when specifying custom widgets the config array for that widget
  135. * is overwritten instead of merged.
  136. */
  137. public function testConstructWithWidgets(): void
  138. {
  139. $config = [
  140. 'widgets' => [
  141. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  142. ],
  143. ];
  144. $helper = new FormHelper($this->View, $config);
  145. $locator = $helper->getWidgetLocator();
  146. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  147. }
  148. /**
  149. * Test that when specifying custom widgets config file and it should be
  150. * added to widgets array. WidgetLocator will load widgets in constructor.
  151. */
  152. public function testConstructWithWidgetsConfig(): void
  153. {
  154. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  155. $locator = $helper->getWidgetLocator();
  156. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  157. }
  158. /**
  159. * Test setting the widget locator
  160. */
  161. public function testSetAndGetWidgetLocator(): void
  162. {
  163. $helper = new FormHelper($this->View);
  164. $locator = new WidgetLocator($helper->templater(), $this->View);
  165. $helper->setWidgetLocator($locator);
  166. $this->assertSame($locator, $helper->getWidgetLocator());
  167. }
  168. /**
  169. * Test overridding grouped input types which controls generation of "for"
  170. * attribute of labels.
  171. */
  172. public function testConstructWithGroupedInputTypes(): void
  173. {
  174. $helper = new FormHelper($this->View, [
  175. 'groupedInputTypes' => ['radio'],
  176. ]);
  177. $result = $helper->control('when', ['type' => 'datetime-local']);
  178. $this->assertStringContainsString('<label for="when">When</label>', $result);
  179. }
  180. /**
  181. * Test registering a new widget class and rendering it.
  182. */
  183. public function testAddWidgetAndRenderWidget(): void
  184. {
  185. $data = [
  186. 'val' => 1,
  187. ];
  188. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  189. $this->Form->addWidget('test', $mock);
  190. $mock->expects($this->once())
  191. ->method('render')
  192. ->with($data)
  193. ->will($this->returnValue('HTML'));
  194. $result = $this->Form->widget('test', $data);
  195. $this->assertSame('HTML', $result);
  196. }
  197. /**
  198. * Test that secureFields() of widget is called after calling render(),
  199. * not before.
  200. */
  201. public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
  202. {
  203. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  204. 'unlockedFields' => [],
  205. ]));
  206. $data = [
  207. 'val' => 1,
  208. 'name' => 'test',
  209. ];
  210. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  211. $this->Form->addWidget('test', $mock);
  212. $mock->expects($this->once())
  213. ->method('render')
  214. ->with($data)
  215. ->will($this->returnValue('HTML'));
  216. $mock->expects($this->once())
  217. ->method('secureFields')
  218. ->with($data)
  219. ->will($this->returnValue(['test']));
  220. $this->Form->create();
  221. $result = $this->Form->widget('test', $data + ['secure' => true]);
  222. $this->assertSame('HTML', $result);
  223. }
  224. /**
  225. * Test that empty string is not added to secure fields list when
  226. * rendering input widget without name.
  227. */
  228. public function testRenderingWidgetWithEmptyName(): void
  229. {
  230. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  231. $this->Form->create();
  232. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  233. $this->assertSame('<select name=""></select>', $result);
  234. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  235. $this->assertEquals([], $result);
  236. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  237. $this->assertSame('<select name="0"></select>', $result);
  238. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  239. $this->assertEquals(['0'], $result);
  240. }
  241. /**
  242. * Test registering an invalid widget class.
  243. */
  244. public function testAddWidgetInvalid(): void
  245. {
  246. $this->expectException(\RuntimeException::class);
  247. $mock = new \stdClass();
  248. $this->Form->addWidget('test', $mock);
  249. $this->Form->widget('test');
  250. }
  251. /**
  252. * Test adding a new context class.
  253. */
  254. public function testAddContextProvider(): void
  255. {
  256. $context = 'My data';
  257. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  258. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  259. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  260. $this->assertSame($context, $data['entity']);
  261. return $stub;
  262. });
  263. $this->Form->create($context);
  264. $result = $this->Form->context();
  265. $this->assertSame($stub, $result);
  266. }
  267. /**
  268. * Test replacing a context class.
  269. */
  270. public function testAddContextProviderReplace(): void
  271. {
  272. $entity = new Article();
  273. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  274. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  275. return $stub;
  276. });
  277. $this->Form->create($entity);
  278. $result = $this->Form->context();
  279. $this->assertSame($stub, $result);
  280. }
  281. /**
  282. * Test overriding a context class.
  283. */
  284. public function testAddContextProviderAdd(): void
  285. {
  286. $entity = new Article();
  287. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  288. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  289. if ($data['entity'] instanceof Entity) {
  290. return $stub;
  291. }
  292. });
  293. $this->Form->create($entity);
  294. $result = $this->Form->context();
  295. $this->assertSame($stub, $result);
  296. }
  297. /**
  298. * Provides context options for create().
  299. *
  300. * @return array
  301. */
  302. public function contextSelectionProvider(): array
  303. {
  304. $entity = new Article();
  305. $collection = new Collection([$entity]);
  306. $emptyCollection = new Collection([]);
  307. $arrayObject = new ArrayObject([]);
  308. $data = [
  309. 'schema' => [
  310. 'title' => ['type' => 'string'],
  311. ],
  312. ];
  313. $form = new Form();
  314. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  315. return [
  316. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  317. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  318. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  319. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  320. 'form' => [$form, 'Cake\View\Form\FormContext'],
  321. 'none' => [null, 'Cake\View\Form\NullContext'],
  322. 'custom' => [$custom, get_class($custom)],
  323. ];
  324. }
  325. /**
  326. * Test default context selection in create()
  327. *
  328. * @dataProvider contextSelectionProvider
  329. * @param mixed $data
  330. */
  331. public function testCreateContextSelectionBuiltIn($data, string $class): void
  332. {
  333. $this->Form->create($data);
  334. $this->assertInstanceOf($class, $this->Form->context());
  335. }
  336. /**
  337. * Data provider for type option.
  338. *
  339. * @return array
  340. */
  341. public static function requestTypeProvider(): array
  342. {
  343. return [
  344. // type, method, override
  345. ['post', 'post', 'POST'],
  346. ['put', 'post', 'PUT'],
  347. ['patch', 'post', 'PATCH'],
  348. ['delete', 'post', 'DELETE'],
  349. ];
  350. }
  351. /**
  352. * Test creating file forms.
  353. */
  354. public function testCreateFile(): void
  355. {
  356. $encoding = strtolower(Configure::read('App.encoding'));
  357. $result = $this->Form->create(null, ['type' => 'file']);
  358. $expected = [
  359. 'form' => [
  360. 'method' => 'post', 'action' => '/articles/add',
  361. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  362. ],
  363. 'div' => ['style' => 'display:none;'],
  364. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  365. '/div',
  366. ];
  367. $this->assertHtml($expected, $result);
  368. }
  369. /**
  370. * Test creating GET forms.
  371. */
  372. public function testCreateGet(): void
  373. {
  374. $encoding = strtolower(Configure::read('App.encoding'));
  375. $result = $this->Form->create(null, ['type' => 'get']);
  376. $expected = ['form' => [
  377. 'method' => 'get', 'action' => '/articles/add',
  378. 'accept-charset' => $encoding,
  379. ]];
  380. $this->assertHtml($expected, $result);
  381. }
  382. /**
  383. * Test explicit method/enctype options.
  384. *
  385. * Explicit method overwrites inferred method from 'type'
  386. */
  387. public function testCreateExplicitMethodEnctype(): void
  388. {
  389. $encoding = strtolower(Configure::read('App.encoding'));
  390. $result = $this->Form->create(null, [
  391. 'type' => 'get',
  392. 'method' => 'put',
  393. 'enctype' => 'multipart/form-data',
  394. ]);
  395. $expected = ['form' => [
  396. 'method' => 'put',
  397. 'action' => '/articles/add',
  398. 'enctype' => 'multipart/form-data',
  399. 'accept-charset' => $encoding,
  400. ]];
  401. $this->assertHtml($expected, $result);
  402. }
  403. /**
  404. * Test create() with the templates option.
  405. */
  406. public function testCreateTemplatesArray(): void
  407. {
  408. $result = $this->Form->create($this->article, [
  409. 'templates' => [
  410. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  411. ],
  412. ]);
  413. $expected = [
  414. 'form' => [
  415. 'class' => 'form-horizontal',
  416. 'method' => 'post',
  417. 'action' => '/articles/add',
  418. 'accept-charset' => 'utf-8',
  419. ],
  420. ];
  421. $this->assertHtml($expected, $result);
  422. }
  423. /**
  424. * Test create() with the templates option.
  425. */
  426. public function testCreateTemplatesFile(): void
  427. {
  428. $result = $this->Form->create($this->article, [
  429. 'templates' => 'htmlhelper_tags',
  430. ]);
  431. $expected = [
  432. 'start form',
  433. ];
  434. $this->assertHtml($expected, $result);
  435. }
  436. /**
  437. * Test that create() and end() restore templates.
  438. */
  439. public function testCreateEndRestoreTemplates(): void
  440. {
  441. $this->Form->create($this->article, [
  442. 'templates' => ['input' => 'custom input element'],
  443. ]);
  444. $this->Form->end();
  445. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  446. }
  447. /**
  448. * Test using template vars in various templates used by control() method.
  449. */
  450. public function testControlTemplateVars(): void
  451. {
  452. $result = $this->Form->control('text', [
  453. 'templates' => [
  454. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  455. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  456. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  457. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  458. ],
  459. 'templateVars' => [
  460. 'forinput' => 'in-input',
  461. 'forlabel' => 'in-label',
  462. 'forgroup' => 'in-group',
  463. 'forcontainer' => 'in-container',
  464. ],
  465. ]);
  466. $expected = [
  467. 'div' => ['class'],
  468. 'label' => ['for'],
  469. 'Text in-label',
  470. '/label',
  471. 'in-group',
  472. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  473. 'in-container',
  474. '/div',
  475. ];
  476. $this->assertHtml($expected, $result);
  477. }
  478. /**
  479. * Test ensuring template variables work in template files loaded
  480. * during control().
  481. */
  482. public function testControlTemplatesFromFile(): void
  483. {
  484. $result = $this->Form->control('title', [
  485. 'templates' => 'test_templates',
  486. 'templateVars' => [
  487. 'forcontainer' => 'container-data',
  488. ],
  489. ]);
  490. $expected = [
  491. 'div' => ['class'],
  492. 'label' => ['for'],
  493. 'Title',
  494. '/label',
  495. 'input' => ['name', 'type' => 'text', 'id'],
  496. 'container-data',
  497. '/div',
  498. ];
  499. $this->assertHtml($expected, $result);
  500. }
  501. /**
  502. * Test using template vars in inputSubmit and submitContainer template.
  503. */
  504. public function testSubmitTemplateVars(): void
  505. {
  506. $this->Form->setTemplates([
  507. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  508. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  509. ]);
  510. $result = $this->Form->submit('Submit', [
  511. 'templateVars' => [
  512. 'forinput' => 'in-input',
  513. 'forcontainer' => 'in-container',
  514. ],
  515. ]);
  516. $expected = [
  517. 'div' => ['class'],
  518. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  519. 'in-container',
  520. '/div',
  521. ];
  522. $this->assertHtml($expected, $result);
  523. }
  524. /**
  525. * test the create() method
  526. *
  527. * @dataProvider requestTypeProvider
  528. */
  529. public function testCreateTypeOptions(string $type, string $method, string $override): void
  530. {
  531. $encoding = strtolower(Configure::read('App.encoding'));
  532. $result = $this->Form->create(null, ['type' => $type]);
  533. $expected = [
  534. 'form' => [
  535. 'method' => $method, 'action' => '/articles/add',
  536. 'accept-charset' => $encoding,
  537. ],
  538. ];
  539. $extra = [
  540. 'div' => ['style' => 'display:none;'],
  541. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  542. '/div',
  543. ];
  544. if ($type !== 'post') {
  545. $expected = array_merge($expected, $extra);
  546. }
  547. $this->assertHtml($expected, $result);
  548. }
  549. /**
  550. * Test using template vars in Create (formStart template)
  551. */
  552. public function testCreateTemplateVars(): void
  553. {
  554. $result = $this->Form->create($this->article, [
  555. 'templates' => [
  556. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  557. ],
  558. 'templateVars' => ['header' => 'headertext'],
  559. ]);
  560. $expected = [
  561. 'h4' => ['class'],
  562. 'headertext',
  563. '/h4',
  564. 'form' => [
  565. 'method' => 'post',
  566. 'action' => '/articles/add',
  567. 'accept-charset' => 'utf-8',
  568. ],
  569. ];
  570. $this->assertHtml($expected, $result);
  571. }
  572. /**
  573. * Test opening a form for an update operation.
  574. */
  575. public function testCreateUpdateForm(): void
  576. {
  577. $encoding = strtolower(Configure::read('App.encoding'));
  578. $this->View->setRequest($this->View->getRequest()
  579. ->withRequestTarget('/articles/edit/1')
  580. ->withParam('action', 'edit'));
  581. $this->article['defaults']['id'] = 1;
  582. $result = $this->Form->create($this->article);
  583. $expected = [
  584. 'form' => [
  585. 'method' => 'post',
  586. 'action' => '/articles/edit/1',
  587. 'accept-charset' => $encoding,
  588. ],
  589. 'div' => ['style' => 'display:none;'],
  590. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  591. '/div',
  592. ];
  593. $this->assertHtml($expected, $result);
  594. }
  595. /**
  596. * test create() with automatic url generation
  597. */
  598. public function testCreateAutoUrl(): void
  599. {
  600. $encoding = strtolower(Configure::read('App.encoding'));
  601. $this->View->setRequest($this->View->getRequest()
  602. ->withRequestTarget('/articles/delete/10')
  603. ->withParam('action', 'delete'));
  604. $result = $this->Form->create($this->article);
  605. $expected = [
  606. 'form' => [
  607. 'method' => 'post', 'action' => '/articles/delete/10',
  608. 'accept-charset' => $encoding,
  609. ],
  610. ];
  611. $this->assertHtml($expected, $result);
  612. $this->article['defaults'] = ['id' => 1];
  613. $this->View->setRequest($this->View->getRequest()
  614. ->withRequestTarget('/Articles/edit/1')
  615. ->withParam('action', 'delete'));
  616. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  617. $expected = [
  618. 'form' => [
  619. 'method' => 'post',
  620. 'action' => '/Articles/edit/1',
  621. 'accept-charset' => $encoding,
  622. ],
  623. 'div' => ['style' => 'display:none;'],
  624. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  625. '/div',
  626. ];
  627. $this->assertHtml($expected, $result);
  628. $this->View->setRequest($this->View->getRequest()
  629. ->withParam('action', 'add'));
  630. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  631. $expected = [
  632. 'form' => [
  633. 'method' => 'post',
  634. 'action' => '/Articles/publish/1',
  635. 'accept-charset' => $encoding,
  636. ],
  637. 'div' => ['style' => 'display:none;'],
  638. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  639. '/div',
  640. ];
  641. $this->assertHtml($expected, $result);
  642. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  643. $expected = [
  644. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  645. 'div' => ['style' => 'display:none;'],
  646. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  647. '/div',
  648. ];
  649. $this->assertHtml($expected, $result);
  650. $this->View->setRequest($this->View->getRequest()
  651. ->withParam('controller', 'Pages'));
  652. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  653. $expected = [
  654. 'form' => [
  655. 'method' => 'post', 'action' => '/Pages/signup/1',
  656. 'accept-charset' => $encoding,
  657. ],
  658. 'div' => ['style' => 'display:none;'],
  659. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  660. '/div',
  661. ];
  662. $this->assertHtml($expected, $result);
  663. }
  664. /**
  665. * Test create() with no URL (no "action" attribute for <form> tag)
  666. */
  667. public function testCreateNoUrl(): void
  668. {
  669. $result = $this->Form->create(null, ['url' => false]);
  670. $expected = [
  671. 'form' => [
  672. 'method' => 'post',
  673. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  674. ],
  675. ];
  676. $this->assertHtml($expected, $result);
  677. }
  678. /**
  679. * test create() with a custom route
  680. */
  681. public function testCreateCustomRoute(): void
  682. {
  683. $builder = Router::createRouteBuilder('/');
  684. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  685. $encoding = strtolower(Configure::read('App.encoding'));
  686. $this->View->setRequest($this->View->getRequest()
  687. ->withParam('controller', 'Users'));
  688. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  689. $expected = [
  690. 'form' => [
  691. 'method' => 'post', 'action' => '/login',
  692. 'accept-charset' => $encoding,
  693. ],
  694. ];
  695. $this->assertHtml($expected, $result);
  696. $builder->connect(
  697. '/new-article',
  698. ['controller' => 'Articles', 'action' => 'myAction'],
  699. ['_name' => 'my-route']
  700. );
  701. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  702. $expected = [
  703. 'form' => [
  704. 'method' => 'post', 'action' => '/new-article',
  705. 'accept-charset' => $encoding,
  706. ],
  707. ];
  708. $this->assertHtml($expected, $result);
  709. }
  710. /**
  711. * test automatic accept-charset overriding
  712. */
  713. public function testCreateWithAcceptCharset(): void
  714. {
  715. $result = $this->Form->create(
  716. $this->article,
  717. [
  718. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  719. ]
  720. );
  721. $expected = [
  722. 'form' => [
  723. 'method' => 'post', 'action' => '/Articles',
  724. 'accept-charset' => 'iso-8859-1',
  725. ],
  726. ];
  727. $this->assertHtml($expected, $result);
  728. }
  729. /**
  730. * Test base form URL when 'url' param is passed with multiple parameters (&)
  731. */
  732. public function testCreateQueryStringRequest(): void
  733. {
  734. $encoding = strtolower(Configure::read('App.encoding'));
  735. $result = $this->Form->create($this->article, [
  736. 'type' => 'post',
  737. 'escape' => false,
  738. 'url' => [
  739. 'controller' => 'Controller',
  740. 'action' => 'action',
  741. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  742. ],
  743. ]);
  744. $expected = [
  745. 'form' => [
  746. 'method' => 'post',
  747. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  748. 'accept-charset' => $encoding,
  749. ],
  750. ];
  751. $this->assertHtml($expected, $result);
  752. $result = $this->Form->create($this->article, [
  753. 'type' => 'post',
  754. 'url' => [
  755. 'controller' => 'Controller',
  756. 'action' => 'action',
  757. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  758. ],
  759. ]);
  760. $this->assertHtml($expected, $result);
  761. }
  762. /**
  763. * test that create() doesn't cause errors by multiple id's being in the primary key
  764. * as could happen with multiple select or checkboxes.
  765. */
  766. public function testCreateWithMultipleIdInData(): void
  767. {
  768. $encoding = strtolower(Configure::read('App.encoding'));
  769. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  770. $result = $this->Form->create($this->article);
  771. $expected = [
  772. 'form' => [
  773. 'method' => 'post',
  774. 'action' => '/articles/add',
  775. 'accept-charset' => $encoding,
  776. ],
  777. ];
  778. $this->assertHtml($expected, $result);
  779. }
  780. /**
  781. * test that create() doesn't add in extra passed params.
  782. */
  783. public function testCreatePassedArgs(): void
  784. {
  785. $encoding = strtolower(Configure::read('App.encoding'));
  786. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  787. $result = $this->Form->create($this->article, [
  788. 'type' => 'post',
  789. 'escape' => false,
  790. 'url' => [
  791. 'action' => 'edit',
  792. 'myparam',
  793. ],
  794. ]);
  795. $expected = [
  796. 'form' => [
  797. 'method' => 'post',
  798. 'action' => '/Articles/edit/myparam',
  799. 'accept-charset' => $encoding,
  800. ],
  801. ];
  802. $this->assertHtml($expected, $result);
  803. }
  804. /**
  805. * test creating a get form, and get form inputs.
  806. */
  807. public function testGetFormCreate(): void
  808. {
  809. $encoding = strtolower(Configure::read('App.encoding'));
  810. $result = $this->Form->create($this->article, ['type' => 'get']);
  811. $expected = ['form' => [
  812. 'method' => 'get', 'action' => '/articles/add',
  813. 'accept-charset' => $encoding,
  814. ]];
  815. $this->assertHtml($expected, $result);
  816. $result = $this->Form->text('title');
  817. $expected = ['input' => [
  818. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  819. ]];
  820. $this->assertHtml($expected, $result);
  821. $result = $this->Form->password('password');
  822. $expected = ['input' => [
  823. 'name' => 'password', 'type' => 'password',
  824. ]];
  825. $this->assertHtml($expected, $result);
  826. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  827. $result = $this->Form->text('user_form');
  828. $expected = ['input' => [
  829. 'name' => 'user_form', 'type' => 'text',
  830. ]];
  831. $this->assertHtml($expected, $result);
  832. }
  833. /**
  834. * test get form, and inputs when the model param is false
  835. */
  836. public function testGetFormWithFalseModel(): void
  837. {
  838. $encoding = strtolower(Configure::read('App.encoding'));
  839. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  840. $result = $this->Form->create(null, [
  841. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  842. ]);
  843. $expected = ['form' => [
  844. 'method' => 'get', 'action' => '/ContactTest/add',
  845. 'accept-charset' => $encoding,
  846. ]];
  847. $this->assertHtml($expected, $result);
  848. $result = $this->Form->text('reason');
  849. $expected = [
  850. 'input' => ['type' => 'text', 'name' => 'reason'],
  851. ];
  852. $this->assertHtml($expected, $result);
  853. }
  854. /**
  855. * testFormCreateWithSecurity method
  856. *
  857. * Test form->create() with security key.
  858. */
  859. public function testCreateWithSecurity(): void
  860. {
  861. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  862. $encoding = strtolower(Configure::read('App.encoding'));
  863. $result = $this->Form->create($this->article, [
  864. 'url' => '/articles/publish',
  865. ]);
  866. $expected = [
  867. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  868. 'div' => ['style' => 'display:none;'],
  869. ['input' => [
  870. 'type' => 'hidden',
  871. 'name' => '_csrfToken',
  872. 'value' => 'testKey',
  873. 'autocomplete' => 'off',
  874. ]],
  875. '/div',
  876. ];
  877. $this->assertHtml($expected, $result);
  878. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  879. $expected['form']['id'] = 'MyForm';
  880. $this->assertHtml($expected, $result);
  881. }
  882. /**
  883. * testFormCreateGetNoSecurity method
  884. *
  885. * Test form->create() with no security key as its a get form
  886. */
  887. public function testCreateEndGetNoSecurity(): void
  888. {
  889. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  890. $article = new Article();
  891. $result = $this->Form->create($article, [
  892. 'type' => 'get',
  893. 'url' => '/contacts/add',
  894. ]);
  895. $this->assertStringNotContainsString('testKey', $result);
  896. $result = $this->Form->end();
  897. $this->assertStringNotContainsString('testKey', $result);
  898. }
  899. /**
  900. * Tests form hash generation with model-less data
  901. */
  902. public function testValidateHashNoModel(): void
  903. {
  904. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  905. $fields = ['anything'];
  906. $this->Form->create();
  907. $result = $this->Form->secure($fields);
  908. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  909. $this->assertStringContainsString($hash, $result);
  910. }
  911. /**
  912. * Tests that hidden fields generated for checkboxes don't get locked
  913. */
  914. public function testNoCheckboxLocking(): void
  915. {
  916. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  917. $this->Form->create();
  918. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  919. $this->Form->checkbox('check', ['value' => '1']);
  920. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  921. }
  922. /**
  923. * testFormSecurityFields method
  924. *
  925. * Test generation of secure form hash generation.
  926. */
  927. public function testFormSecurityFields(): void
  928. {
  929. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  930. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  931. $this->Form->create();
  932. $result = $this->Form->secure($fields);
  933. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  934. $hash .= ':' . 'Model.valid';
  935. $hash = urlencode($hash);
  936. $tokenDebug = urlencode(json_encode([
  937. $this->url,
  938. $fields,
  939. [],
  940. ]));
  941. $expected = [
  942. 'div' => ['style' => 'display:none;'],
  943. ['input' => [
  944. 'type' => 'hidden',
  945. 'name' => '_Token[fields]',
  946. 'value' => $hash,
  947. 'autocomplete' => 'off',
  948. ]],
  949. ['input' => [
  950. 'type' => 'hidden',
  951. 'name' => '_Token[unlocked]',
  952. 'value' => '',
  953. 'autocomplete' => 'off',
  954. ]],
  955. ['input' => [
  956. 'type' => 'hidden',
  957. 'name' => '_Token[debug]',
  958. 'value' => $tokenDebug,
  959. 'autocomplete' => 'off',
  960. ]],
  961. '/div',
  962. ];
  963. $this->assertHtml($expected, $result);
  964. }
  965. /**
  966. * testFormSecurityFields method
  967. *
  968. * Test debug token is not generated if debug is false
  969. */
  970. public function testFormSecurityFieldsNoDebugMode(): void
  971. {
  972. Configure::write('debug', false);
  973. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  974. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  975. $this->Form->create();
  976. $result = $this->Form->secure($fields);
  977. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  978. $hash .= ':' . 'Model.valid';
  979. $hash = urlencode($hash);
  980. $expected = [
  981. 'div' => ['style' => 'display:none;'],
  982. ['input' => [
  983. 'type' => 'hidden',
  984. 'name' => '_Token[fields]',
  985. 'autocomplete' => 'off',
  986. 'value' => $hash,
  987. ]],
  988. ['input' => [
  989. 'type' => 'hidden',
  990. 'name' => '_Token[unlocked]',
  991. 'autocomplete' => 'off',
  992. 'value' => '',
  993. ]],
  994. '/div',
  995. ];
  996. $this->assertHtml($expected, $result);
  997. }
  998. /**
  999. * Tests correct generation of number fields for smallint
  1000. */
  1001. public function testTextFieldGenerationForSmallint(): void
  1002. {
  1003. $this->article['schema'] = [
  1004. 'foo' => [
  1005. 'type' => 'smallinteger',
  1006. 'null' => false,
  1007. 'default' => null,
  1008. 'length' => 10,
  1009. ],
  1010. ];
  1011. $this->Form->create($this->article);
  1012. $result = $this->Form->control('foo');
  1013. $this->assertStringContainsString('class="input number"', $result);
  1014. $this->assertStringContainsString('type="number"', $result);
  1015. }
  1016. /**
  1017. * Tests correct generation of number fields for tinyint
  1018. */
  1019. public function testTextFieldGenerationForTinyint(): void
  1020. {
  1021. $this->article['schema'] = [
  1022. 'foo' => [
  1023. 'type' => 'tinyinteger',
  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 double and float fields
  1036. */
  1037. public function testTextFieldGenerationForFloats(): void
  1038. {
  1039. $this->article['schema'] = [
  1040. 'foo' => [
  1041. 'type' => 'float',
  1042. 'null' => false,
  1043. 'default' => null,
  1044. 'length' => 10,
  1045. ],
  1046. ];
  1047. $this->Form->create($this->article);
  1048. $result = $this->Form->control('foo');
  1049. $expected = [
  1050. 'div' => ['class' => 'input number'],
  1051. 'label' => ['for' => 'foo'],
  1052. 'Foo',
  1053. '/label',
  1054. ['input' => [
  1055. 'type' => 'number',
  1056. 'name' => 'foo',
  1057. 'id' => 'foo',
  1058. 'step' => 'any',
  1059. ]],
  1060. '/div',
  1061. ];
  1062. $this->assertHtml($expected, $result);
  1063. $result = $this->Form->control('foo', ['step' => 0.5]);
  1064. $expected = [
  1065. 'div' => ['class' => 'input number'],
  1066. 'label' => ['for' => 'foo'],
  1067. 'Foo',
  1068. '/label',
  1069. ['input' => [
  1070. 'type' => 'number',
  1071. 'name' => 'foo',
  1072. 'id' => 'foo',
  1073. 'step' => '0.5',
  1074. ]],
  1075. '/div',
  1076. ];
  1077. $this->assertHtml($expected, $result);
  1078. }
  1079. /**
  1080. * Tests correct generation of number fields for integer fields
  1081. */
  1082. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1083. {
  1084. $this->getTableLocator()->get('Contacts', [
  1085. 'className' => ContactsTable::class,
  1086. ]);
  1087. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1088. $result = $this->Form->control('age');
  1089. $expected = [
  1090. 'div' => ['class' => 'input number'],
  1091. 'label' => ['for' => 'age'],
  1092. 'Age',
  1093. '/label',
  1094. ['input' => [
  1095. 'type' => 'number', 'name' => 'age',
  1096. 'id' => 'age',
  1097. ]],
  1098. '/div',
  1099. ];
  1100. $this->assertHtml($expected, $result);
  1101. }
  1102. /**
  1103. * Tests correct generation of file upload fields for binary fields
  1104. */
  1105. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1106. {
  1107. $table = $this->getTableLocator()->get('Contacts', [
  1108. 'className' => ContactsTable::class,
  1109. ]);
  1110. $table->setSchema(['foo' => [
  1111. 'type' => 'binary',
  1112. 'null' => false,
  1113. 'default' => null,
  1114. 'length' => 1024,
  1115. ]]);
  1116. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1117. $result = $this->Form->control('foo');
  1118. $expected = [
  1119. 'div' => ['class' => 'input file'],
  1120. 'label' => ['for' => 'foo'],
  1121. 'Foo',
  1122. '/label',
  1123. ['input' => [
  1124. 'type' => 'file', 'name' => 'foo',
  1125. 'id' => 'foo',
  1126. ]],
  1127. '/div',
  1128. ];
  1129. $this->assertHtml($expected, $result);
  1130. }
  1131. /**
  1132. * testFormSecurityMultipleFields method
  1133. *
  1134. * Test secure() with multiple row form. Ensure hash is correct.
  1135. */
  1136. public function testFormSecurityMultipleFields(): void
  1137. {
  1138. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1139. $this->Form->create();
  1140. $fields = [
  1141. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1142. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1143. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1144. ];
  1145. $result = $this->Form->secure($fields);
  1146. $sortedFields = [
  1147. 'Model.0.password',
  1148. 'Model.0.username',
  1149. 'Model.1.password',
  1150. 'Model.1.username',
  1151. 'Model.0.hidden' => 'value',
  1152. 'Model.0.valid' => '0',
  1153. 'Model.1.hidden' => 'value',
  1154. 'Model.1.valid' => '0',
  1155. ];
  1156. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1157. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1158. $hash = urlencode($hash);
  1159. $tokenDebug = urlencode(json_encode([
  1160. $this->url,
  1161. $fields,
  1162. [],
  1163. ]));
  1164. $expected = [
  1165. 'div' => ['style' => 'display:none;'],
  1166. ['input' => [
  1167. 'type' => 'hidden',
  1168. 'name' => '_Token[fields]',
  1169. 'value' => $hash,
  1170. 'autocomplete' => 'off',
  1171. ]],
  1172. ['input' => [
  1173. 'type' => 'hidden',
  1174. 'name' => '_Token[unlocked]',
  1175. 'autocomplete' => 'off',
  1176. 'value' => '',
  1177. ]],
  1178. ['input' => [
  1179. 'type' => 'hidden',
  1180. 'name' => '_Token[debug]',
  1181. 'value' => $tokenDebug,
  1182. 'autocomplete' => 'off',
  1183. ]],
  1184. '/div',
  1185. ];
  1186. $this->assertHtml($expected, $result);
  1187. }
  1188. /**
  1189. * testFormSecurityMultipleSubmitButtons
  1190. *
  1191. * test form submit generation and ensure that _Token is only created on end()
  1192. */
  1193. public function testFormSecurityMultipleSubmitButtons(): void
  1194. {
  1195. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1196. $this->Form->create($this->article);
  1197. $this->Form->text('Address.title');
  1198. $this->Form->text('Address.first_name');
  1199. $result = $this->Form->submit('Save', ['name' => 'save']);
  1200. $expected = [
  1201. 'div' => ['class' => 'submit'],
  1202. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1203. '/div',
  1204. ];
  1205. $this->assertHtml($expected, $result);
  1206. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1207. $expected = [
  1208. 'div' => ['class' => 'submit'],
  1209. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1210. '/div',
  1211. ];
  1212. $this->assertHtml($expected, $result);
  1213. $result = $this->Form->end();
  1214. $tokenDebug = urlencode(json_encode([
  1215. '/articles/add',
  1216. [
  1217. 'Address.title',
  1218. 'Address.first_name',
  1219. ],
  1220. ['save', 'cancel'],
  1221. ]));
  1222. $expected = [
  1223. 'div' => ['style' => 'display:none;'],
  1224. ['input' => [
  1225. 'type' => 'hidden',
  1226. 'name' => '_Token[fields]',
  1227. 'autocomplete',
  1228. 'value',
  1229. ]],
  1230. ['input' => [
  1231. 'type' => 'hidden',
  1232. 'name' => '_Token[unlocked]',
  1233. 'value' => 'cancel%7Csave',
  1234. 'autocomplete' => 'off',
  1235. ]],
  1236. ['input' => [
  1237. 'type' => 'hidden',
  1238. 'name' => '_Token[debug]',
  1239. 'value' => $tokenDebug,
  1240. 'autocomplete' => 'off',
  1241. ]],
  1242. '/div',
  1243. ];
  1244. $this->assertHtml($expected, $result);
  1245. }
  1246. /**
  1247. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1248. */
  1249. public function testSecurityButtonNestedNamed(): void
  1250. {
  1251. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1252. $this->Form->create();
  1253. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1254. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1255. $this->assertEquals(['Address.button'], $result);
  1256. }
  1257. /**
  1258. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1259. */
  1260. public function testSecuritySubmitNestedNamed(): void
  1261. {
  1262. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1263. $this->Form->create($this->article);
  1264. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1265. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1266. $this->assertEquals(['Address.button'], $result);
  1267. }
  1268. /**
  1269. * Test that the correct fields are unlocked for image submits with no names.
  1270. */
  1271. public function testSecuritySubmitImageNoName(): void
  1272. {
  1273. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1274. $this->Form->create();
  1275. $result = $this->Form->submit('save.png');
  1276. $expected = [
  1277. 'div' => ['class' => 'submit'],
  1278. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1279. '/div',
  1280. ];
  1281. $this->assertHtml($expected, $result);
  1282. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1283. $this->assertEquals(['x', 'y'], $result);
  1284. }
  1285. /**
  1286. * Test that the correct fields are unlocked for image submits with names.
  1287. */
  1288. public function testSecuritySubmitImageName(): void
  1289. {
  1290. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1291. $this->Form->create();
  1292. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1293. $expected = [
  1294. 'div' => ['class' => 'submit'],
  1295. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1296. '/div',
  1297. ];
  1298. $this->assertHtml($expected, $result);
  1299. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1300. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1301. }
  1302. /**
  1303. * testFormSecurityMultipleControlFields method
  1304. *
  1305. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1306. */
  1307. public function testFormSecurityMultipleControlFields(): void
  1308. {
  1309. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1310. $this->Form->create();
  1311. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1312. $this->Form->control('Addresses.0.title');
  1313. $this->Form->control('Addresses.0.first_name');
  1314. $this->Form->control('Addresses.0.last_name');
  1315. $this->Form->control('Addresses.0.address');
  1316. $this->Form->control('Addresses.0.city');
  1317. $this->Form->control('Addresses.0.phone');
  1318. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1319. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1320. $this->Form->control('Addresses.1.title');
  1321. $this->Form->control('Addresses.1.first_name');
  1322. $this->Form->control('Addresses.1.last_name');
  1323. $this->Form->control('Addresses.1.address');
  1324. $this->Form->control('Addresses.1.city');
  1325. $this->Form->control('Addresses.1.phone');
  1326. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1327. $result = $this->Form->secure();
  1328. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1329. $tokenDebug = urlencode(json_encode([
  1330. '/articles/add',
  1331. [
  1332. 'Addresses.0.id' => '123456',
  1333. 'Addresses.0.title',
  1334. 'Addresses.0.first_name',
  1335. 'Addresses.0.last_name',
  1336. 'Addresses.0.address',
  1337. 'Addresses.0.city',
  1338. 'Addresses.0.phone',
  1339. 'Addresses.0.primary',
  1340. 'Addresses.1.id' => '654321',
  1341. 'Addresses.1.title',
  1342. 'Addresses.1.first_name',
  1343. 'Addresses.1.last_name',
  1344. 'Addresses.1.address',
  1345. 'Addresses.1.city',
  1346. 'Addresses.1.phone',
  1347. 'Addresses.1.primary',
  1348. ],
  1349. [],
  1350. ]));
  1351. $expected = [
  1352. 'div' => ['style' => 'display:none;'],
  1353. ['input' => [
  1354. 'type' => 'hidden',
  1355. 'name' => '_Token[fields]',
  1356. 'value' => $hash,
  1357. 'autocomplete' => 'off',
  1358. ]],
  1359. ['input' => [
  1360. 'type' => 'hidden',
  1361. 'name' => '_Token[unlocked]',
  1362. 'autocomplete' => 'off',
  1363. 'value' => '',
  1364. ]],
  1365. ['input' => [
  1366. 'type' => 'hidden',
  1367. 'name' => '_Token[debug]',
  1368. 'value' => $tokenDebug,
  1369. 'autocomplete' => 'off',
  1370. ]],
  1371. '/div',
  1372. ];
  1373. $this->assertHtml($expected, $result);
  1374. }
  1375. /**
  1376. * testFormSecurityArrayFields method
  1377. *
  1378. * Test form security with Model.field.0 style inputs.
  1379. */
  1380. public function testFormSecurityArrayFields(): void
  1381. {
  1382. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1383. $this->Form->create();
  1384. $this->Form->text('Address.primary.1');
  1385. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1386. $this->assertSame('Address.primary', $result[0]);
  1387. $this->Form->text('Address.secondary.1.0');
  1388. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1389. $this->assertSame('Address.secondary', $result[1]);
  1390. }
  1391. /**
  1392. * testFormSecurityMultipleControlDisabledFields method
  1393. *
  1394. * Test secure form generation with multiple records and disabled fields.
  1395. */
  1396. public function testFormSecurityMultipleControlDisabledFields(): void
  1397. {
  1398. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1399. 'unlockedFields' => ['first_name', 'address'],
  1400. ]));
  1401. $this->Form->create();
  1402. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1403. $this->Form->text('Addresses.0.title');
  1404. $this->Form->text('Addresses.0.first_name');
  1405. $this->Form->text('Addresses.0.last_name');
  1406. $this->Form->text('Addresses.0.address');
  1407. $this->Form->text('Addresses.0.city');
  1408. $this->Form->text('Addresses.0.phone');
  1409. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1410. $this->Form->text('Addresses.1.title');
  1411. $this->Form->text('Addresses.1.first_name');
  1412. $this->Form->text('Addresses.1.last_name');
  1413. $this->Form->text('Addresses.1.address');
  1414. $this->Form->text('Addresses.1.city');
  1415. $this->Form->text('Addresses.1.phone');
  1416. $result = $this->Form->secure();
  1417. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1418. $tokenDebug = urlencode(json_encode([
  1419. '/articles/add',
  1420. [
  1421. 'Addresses.0.id' => '123456',
  1422. 'Addresses.0.title',
  1423. 'Addresses.0.last_name',
  1424. 'Addresses.0.city',
  1425. 'Addresses.0.phone',
  1426. 'Addresses.1.id' => '654321',
  1427. 'Addresses.1.title',
  1428. 'Addresses.1.last_name',
  1429. 'Addresses.1.city',
  1430. 'Addresses.1.phone',
  1431. ],
  1432. [
  1433. 'first_name',
  1434. 'address',
  1435. ],
  1436. ]));
  1437. $expected = [
  1438. 'div' => ['style' => 'display:none;'],
  1439. ['input' => [
  1440. 'type' => 'hidden',
  1441. 'name' => '_Token[fields]',
  1442. 'autocomplete' => 'off',
  1443. 'value' => $hash,
  1444. ]],
  1445. ['input' => [
  1446. 'type' => 'hidden',
  1447. 'name' => '_Token[unlocked]',
  1448. 'autocomplete' => 'off',
  1449. 'value' => 'address%7Cfirst_name',
  1450. ]],
  1451. ['input' => [
  1452. 'type' => 'hidden',
  1453. 'name' => '_Token[debug]',
  1454. 'autocomplete' => 'off',
  1455. 'value' => $tokenDebug,
  1456. ]],
  1457. '/div',
  1458. ];
  1459. $this->assertHtml($expected, $result);
  1460. }
  1461. /**
  1462. * testFormSecurityControlDisabledFields method
  1463. *
  1464. * Test single record form with disabled fields.
  1465. */
  1466. public function testFormSecurityControlUnlockedFields(): void
  1467. {
  1468. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1469. 'unlockedFields' => ['first_name', 'address'],
  1470. ]));
  1471. $this->Form->create();
  1472. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1473. $this->assertEquals(
  1474. $this->View->getRequest()->getAttribute('formTokenData'),
  1475. ['unlockedFields' => $result]
  1476. );
  1477. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1478. $this->Form->text('Addresses.title');
  1479. $this->Form->text('Addresses.first_name');
  1480. $this->Form->text('Addresses.last_name');
  1481. $this->Form->text('Addresses.address');
  1482. $this->Form->text('Addresses.city');
  1483. $this->Form->text('Addresses.phone');
  1484. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1485. $expected = [
  1486. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1487. 'Addresses.city', 'Addresses.phone',
  1488. ];
  1489. $this->assertEquals($expected, $result);
  1490. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1491. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1492. $tokenDebug = urlencode(json_encode([
  1493. '/articles/add',
  1494. [
  1495. 'Addresses.id' => '123456',
  1496. 'Addresses.title',
  1497. 'Addresses.last_name',
  1498. 'Addresses.city',
  1499. 'Addresses.phone',
  1500. ],
  1501. [
  1502. 'first_name',
  1503. 'address',
  1504. ],
  1505. ]));
  1506. $expected = [
  1507. 'div' => ['style' => 'display:none;'],
  1508. ['input' => [
  1509. 'type' => 'hidden',
  1510. 'name' => '_Token[fields]',
  1511. 'value' => $hash,
  1512. 'autocomplete' => 'off',
  1513. 'data-foo' => 'bar',
  1514. ]],
  1515. ['input' => [
  1516. 'type' => 'hidden',
  1517. 'name' => '_Token[unlocked]',
  1518. 'value' => 'address%7Cfirst_name',
  1519. 'autocomplete' => 'off',
  1520. 'data-foo' => 'bar',
  1521. ]],
  1522. ['input' => [
  1523. 'type' => 'hidden', 'name' => '_Token[debug]',
  1524. 'value' => $tokenDebug,
  1525. 'autocomplete' => 'off',
  1526. 'data-foo' => 'bar',
  1527. ]],
  1528. '/div',
  1529. ];
  1530. $this->assertHtml($expected, $result);
  1531. }
  1532. /**
  1533. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1534. *
  1535. * Test single record form with debugSecurity param.
  1536. */
  1537. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1538. {
  1539. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1540. 'unlockedFields' => ['first_name', 'address'],
  1541. ]));
  1542. $this->Form->create();
  1543. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1544. $this->assertEquals(
  1545. $this->View->getRequest()->getAttribute('formTokenData'),
  1546. ['unlockedFields' => $result]
  1547. );
  1548. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1549. $this->Form->text('Addresses.title');
  1550. $this->Form->text('Addresses.first_name');
  1551. $this->Form->text('Addresses.last_name');
  1552. $this->Form->text('Addresses.address');
  1553. $this->Form->text('Addresses.city');
  1554. $this->Form->text('Addresses.phone');
  1555. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1556. $expected = [
  1557. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1558. 'Addresses.city', 'Addresses.phone',
  1559. ];
  1560. $this->assertEquals($expected, $result);
  1561. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1562. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1563. $tokenDebug = urlencode(json_encode([
  1564. '/articles/add',
  1565. [
  1566. 'Addresses.id' => '123456',
  1567. 'Addresses.title',
  1568. 'Addresses.last_name',
  1569. 'Addresses.city',
  1570. 'Addresses.phone',
  1571. ],
  1572. [
  1573. 'first_name',
  1574. 'address',
  1575. ],
  1576. ]));
  1577. $expected = [
  1578. 'div' => ['style' => 'display:none;'],
  1579. ['input' => [
  1580. 'type' => 'hidden',
  1581. 'name' => '_Token[fields]',
  1582. 'value' => $hash,
  1583. 'autocomplete' => 'off',
  1584. 'data-foo' => 'bar',
  1585. ]],
  1586. ['input' => [
  1587. 'type' => 'hidden',
  1588. 'name' => '_Token[unlocked]',
  1589. 'value' => 'address%7Cfirst_name',
  1590. 'autocomplete' => 'off',
  1591. 'data-foo' => 'bar',
  1592. ]],
  1593. ['input' => [
  1594. 'type' => 'hidden', 'name' => '_Token[debug]',
  1595. 'value' => $tokenDebug,
  1596. 'autocomplete' => 'off',
  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. 'autocomplete' => 'off',
  1642. 'data-foo' => 'bar',
  1643. ]],
  1644. ['input' => [
  1645. 'type' => 'hidden',
  1646. 'name' => '_Token[unlocked]',
  1647. 'value' => 'address%7Cfirst_name',
  1648. 'autocomplete' => 'off',
  1649. 'data-foo' => 'bar',
  1650. ]],
  1651. '/div',
  1652. ];
  1653. $this->assertHtml($expected, $result);
  1654. }
  1655. /**
  1656. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1657. *
  1658. * Test single record form with debugSecurity param.
  1659. */
  1660. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1661. {
  1662. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1663. 'unlockedFields' => ['first_name', 'address'],
  1664. ]));
  1665. $this->Form->create();
  1666. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1667. $this->assertEquals(
  1668. $this->View->getRequest()->getAttribute('formTokenData'),
  1669. ['unlockedFields' => $result]
  1670. );
  1671. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1672. $this->Form->text('Addresses.title');
  1673. $this->Form->text('Addresses.first_name');
  1674. $this->Form->text('Addresses.last_name');
  1675. $this->Form->text('Addresses.address');
  1676. $this->Form->text('Addresses.city');
  1677. $this->Form->text('Addresses.phone');
  1678. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1679. $expected = [
  1680. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1681. 'Addresses.city', 'Addresses.phone',
  1682. ];
  1683. $this->assertEquals($expected, $result);
  1684. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1685. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1686. $expected = [
  1687. 'div' => ['style' => 'display:none;'],
  1688. ['input' => [
  1689. 'type' => 'hidden',
  1690. 'name' => '_Token[fields]',
  1691. 'value' => $hash,
  1692. 'autocomplete' => 'off',
  1693. 'data-foo' => 'bar',
  1694. ]],
  1695. ['input' => [
  1696. 'type' => 'hidden',
  1697. 'name' => '_Token[unlocked]',
  1698. 'value' => 'address%7Cfirst_name',
  1699. 'autocomplete' => 'off',
  1700. 'data-foo' => 'bar',
  1701. ]],
  1702. '/div',
  1703. ];
  1704. $this->assertHtml($expected, $result);
  1705. }
  1706. /**
  1707. * testFormSecureWithCustomNameAttribute method
  1708. *
  1709. * Test securing inputs with custom name attributes.
  1710. */
  1711. public function testFormSecureWithCustomNameAttribute(): void
  1712. {
  1713. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1714. $this->Form->create();
  1715. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1716. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1717. $this->assertSame('User.custom', $result[0]);
  1718. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1719. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1720. $this->assertSame('User.custom.another.value', $result[1]);
  1721. }
  1722. /**
  1723. * testFormSecuredControl method
  1724. *
  1725. * Test generation of entire secure form, assertions made on control() output.
  1726. */
  1727. public function testFormSecuredControl(): void
  1728. {
  1729. $this->View->setRequest($this->View->getRequest()
  1730. ->withAttribute('formTokenData', [])
  1731. ->withAttribute('csrfToken', 'testKey'));
  1732. $this->article['schema'] = [
  1733. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1734. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1735. ];
  1736. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1737. $encoding = strtolower(Configure::read('App.encoding'));
  1738. $expected = [
  1739. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1740. 'div' => ['style' => 'display:none;'],
  1741. ['input' => [
  1742. 'type' => 'hidden',
  1743. 'name' => '_csrfToken',
  1744. 'value' => 'testKey',
  1745. 'autocomplete' => 'off',
  1746. ]],
  1747. '/div',
  1748. ];
  1749. $this->assertHtml($expected, $result);
  1750. $result = $this->Form->control('ratio');
  1751. $expected = [
  1752. 'div' => ['class'],
  1753. 'label' => ['for'],
  1754. 'Ratio',
  1755. '/label',
  1756. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1757. '/div',
  1758. ];
  1759. $this->assertHtml($expected, $result);
  1760. $result = $this->Form->control('population');
  1761. $expected = [
  1762. 'div' => ['class'],
  1763. 'label' => ['for'],
  1764. 'Population',
  1765. '/label',
  1766. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1767. '/div',
  1768. ];
  1769. $this->assertHtml($expected, $result);
  1770. $result = $this->Form->control('published', ['type' => 'text']);
  1771. $expected = [
  1772. 'div' => ['class' => 'input text'],
  1773. 'label' => ['for' => 'published'],
  1774. 'Published',
  1775. '/label',
  1776. ['input' => [
  1777. 'type' => 'text',
  1778. 'name' => 'published',
  1779. 'id' => 'published',
  1780. ]],
  1781. '/div',
  1782. ];
  1783. $this->assertHtml($expected, $result);
  1784. $result = $this->Form->control('other', ['type' => 'text']);
  1785. $expected = [
  1786. 'div' => ['class' => 'input text'],
  1787. 'label' => ['for' => 'other'],
  1788. 'Other',
  1789. '/label',
  1790. ['input' => [
  1791. 'type' => 'text',
  1792. 'name' => 'other',
  1793. 'id',
  1794. ]],
  1795. '/div',
  1796. ];
  1797. $this->assertHtml($expected, $result);
  1798. $result = $this->Form->hidden('stuff');
  1799. $expected = [
  1800. 'input' => [
  1801. 'type' => 'hidden',
  1802. 'name' => 'stuff',
  1803. ],
  1804. ];
  1805. $this->assertHtml($expected, $result);
  1806. $result = $this->Form->hidden('hidden', ['value' => false]);
  1807. $expected = ['input' => [
  1808. 'type' => 'hidden',
  1809. 'name' => 'hidden',
  1810. 'value' => '0',
  1811. ]];
  1812. $this->assertHtml($expected, $result);
  1813. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1814. $expected = [
  1815. 'div' => ['class' => 'input checkbox'],
  1816. ['input' => [
  1817. 'type' => 'hidden',
  1818. 'name' => 'something',
  1819. 'value' => '0',
  1820. ]],
  1821. 'label' => ['for' => 'something'],
  1822. ['input' => [
  1823. 'type' => 'checkbox',
  1824. 'name' => 'something',
  1825. 'value' => '1',
  1826. 'id' => 'something',
  1827. ]],
  1828. 'Something',
  1829. '/label',
  1830. '/div',
  1831. ];
  1832. $this->assertHtml($expected, $result);
  1833. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1834. $expectedFields = [
  1835. 'ratio',
  1836. 'population',
  1837. 'published',
  1838. 'other',
  1839. 'stuff' => '',
  1840. 'hidden' => '0',
  1841. 'something',
  1842. ];
  1843. $this->assertEquals($expectedFields, $result);
  1844. $result = $this->Form->secure();
  1845. $tokenDebug = urlencode(json_encode([
  1846. '/articles/add',
  1847. $expectedFields,
  1848. [],
  1849. ]));
  1850. $expected = [
  1851. 'div' => ['style' => 'display:none;'],
  1852. ['input' => [
  1853. 'type' => 'hidden',
  1854. 'name' => '_Token[fields]',
  1855. 'value',
  1856. 'autocomplete',
  1857. ]],
  1858. ['input' => [
  1859. 'type' => 'hidden',
  1860. 'name' => '_Token[unlocked]',
  1861. 'value' => '',
  1862. 'autocomplete' => 'off',
  1863. ]],
  1864. ['input' => [
  1865. 'type' => 'hidden', 'name' => '_Token[debug]',
  1866. 'value' => $tokenDebug,
  1867. 'autocomplete' => 'off',
  1868. ]],
  1869. '/div',
  1870. ];
  1871. $this->assertHtml($expected, $result);
  1872. $data = [
  1873. 'ratio' => '',
  1874. 'population' => '',
  1875. 'published' => '',
  1876. 'other' => '',
  1877. 'stuff' => '',
  1878. 'hidden' => '0',
  1879. 'something' => '',
  1880. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1881. ];
  1882. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1883. }
  1884. /**
  1885. * testSecuredControlCustomName method
  1886. *
  1887. * Test secured inputs with custom names.
  1888. */
  1889. public function testSecuredControlCustomName(): void
  1890. {
  1891. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1892. $this->Form->create();
  1893. $this->Form->text('text_input', [
  1894. 'name' => 'Option[General.default_role]',
  1895. ]);
  1896. $expected = ['Option.General.default_role'];
  1897. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1898. $this->assertEquals($expected, $result);
  1899. $this->Form->select('select_box', [1, 2], [
  1900. 'name' => 'Option[General.select_role]',
  1901. ]);
  1902. $expected[] = 'Option.General.select_role';
  1903. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1904. $this->assertEquals($expected, $result);
  1905. $this->Form->text('other.things[]');
  1906. $expected[] = 'other.things';
  1907. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1908. $this->assertEquals($expected, $result);
  1909. }
  1910. /**
  1911. * testSecuredControlDuplicate method
  1912. *
  1913. * Test that a hidden field followed by a visible field
  1914. * undoes the hidden field locking.
  1915. */
  1916. public function testSecuredControlDuplicate(): void
  1917. {
  1918. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1919. $this->Form->create();
  1920. $this->Form->control('text_val', [
  1921. 'type' => 'hidden',
  1922. 'value' => 'some text',
  1923. ]);
  1924. $expected = ['text_val' => 'some text'];
  1925. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1926. $this->assertEquals($expected, $result);
  1927. $this->Form->control('text_val', [
  1928. 'type' => 'text',
  1929. ]);
  1930. $expected = ['text_val'];
  1931. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1932. $this->assertEquals($expected, $result);
  1933. }
  1934. /**
  1935. * testFormSecuredFileControl method
  1936. *
  1937. * Tests that the correct keys are added to the field hash index.
  1938. */
  1939. public function testFormSecuredFileControl(): void
  1940. {
  1941. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1942. $this->Form->create();
  1943. $this->Form->file('Attachment.file');
  1944. $expected = ['Attachment.file'];
  1945. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1946. $this->assertEquals($expected, $result);
  1947. }
  1948. /**
  1949. * testFormSecuredMultipleSelect method
  1950. *
  1951. * Test that multiple selects keys are added to field hash.
  1952. */
  1953. public function testFormSecuredMultipleSelect(): void
  1954. {
  1955. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1956. $this->Form->create();
  1957. $options = ['1' => 'one', '2' => 'two'];
  1958. $this->Form->select('Model.select', $options);
  1959. $expected = ['Model.select'];
  1960. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1961. $this->assertEquals($expected, $result);
  1962. $this->Form->fields = [];
  1963. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1964. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1965. $this->assertEquals($expected, $result);
  1966. }
  1967. /**
  1968. * testFormSecuredRadio method
  1969. */
  1970. public function testFormSecuredRadio(): void
  1971. {
  1972. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1973. $this->Form->create();
  1974. $options = ['1' => 'option1', '2' => 'option2'];
  1975. $this->Form->radio('Test.test', $options);
  1976. $expected = ['Test.test'];
  1977. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1978. $this->assertEquals($expected, $result);
  1979. $this->Form->radio('Test.all', $options, [
  1980. 'disabled' => ['option1', 'option2'],
  1981. ]);
  1982. $expected = ['Test.test', 'Test.all' => ''];
  1983. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1984. $this->assertEquals($expected, $result);
  1985. $this->Form->radio('Test.some', $options, [
  1986. 'disabled' => ['option1'],
  1987. ]);
  1988. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1989. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1990. $this->assertEquals($expected, $result);
  1991. }
  1992. /**
  1993. * testFormSecuredAndDisabledNotAssoc method
  1994. *
  1995. * Test that when disabled is in a list based attribute array it works.
  1996. */
  1997. public function testFormSecuredAndDisabledNotAssoc(): void
  1998. {
  1999. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2000. $this->Form->create();
  2001. $this->deprecated(function () {
  2002. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2003. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2004. $this->Form->text('Model.text', ['disabled']);
  2005. $this->Form->textarea('Model.textarea', ['disabled']);
  2006. $this->Form->password('Model.password', ['disabled']);
  2007. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2008. });
  2009. $expected = [
  2010. 'Model.radio' => '',
  2011. ];
  2012. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2013. $this->assertEquals($expected, $result);
  2014. }
  2015. /**
  2016. * testFormSecuredAndDisabled method
  2017. *
  2018. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2019. * hashing.
  2020. */
  2021. public function testFormSecuredAndDisabled(): void
  2022. {
  2023. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2024. $this->Form->create();
  2025. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2026. $this->Form->text('Model.text', ['disabled' => true]);
  2027. $this->Form->password('Model.text', ['disabled' => true]);
  2028. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2029. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2030. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2031. $this->Form->year('Model.year', ['disabled' => true]);
  2032. $this->Form->month('Model.month', ['disabled' => true]);
  2033. $this->Form->day('Model.day', ['disabled' => true]);
  2034. $this->Form->hour('Model.hour', ['disabled' => true]);
  2035. $this->Form->minute('Model.minute', ['disabled' => true]);
  2036. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2037. $expected = [
  2038. 'Model.radio' => '',
  2039. ];
  2040. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2041. $this->assertEquals($expected, $result);
  2042. }
  2043. /**
  2044. * testUnlockFieldAddsToList method
  2045. *
  2046. * Test disableField.
  2047. */
  2048. public function testUnlockFieldAddsToList(): void
  2049. {
  2050. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2051. 'unlockedFields' => [],
  2052. ]));
  2053. $this->Form->create();
  2054. $this->Form->unlockField('Contact.name');
  2055. $this->Form->text('Contact.name');
  2056. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2057. $this->assertEquals([], $result);
  2058. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2059. $this->assertEquals(['Contact.name'], $result);
  2060. }
  2061. /**
  2062. * testUnlockFieldRemovingFromFields method
  2063. *
  2064. * Test unlockField removing from fields array.
  2065. */
  2066. public function testUnlockFieldRemovingFromFields(): void
  2067. {
  2068. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2069. 'unlockedFields' => [],
  2070. ]));
  2071. $this->Form->create($this->article);
  2072. $this->Form->hidden('Article.id', ['value' => 1]);
  2073. $this->Form->text('Article.title');
  2074. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2075. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2076. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2077. $this->Form->unlockField('Article.title');
  2078. $this->Form->unlockField('Article.id');
  2079. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2080. $this->assertEquals([], $result);
  2081. }
  2082. /**
  2083. * testResetUnlockFields method
  2084. *
  2085. * Test reset unlockFields, when create new form.
  2086. */
  2087. public function testResetUnlockFields(): void
  2088. {
  2089. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2090. 'key' => 'testKey',
  2091. 'unlockedFields' => [],
  2092. ]));
  2093. $this->Form->create();
  2094. $this->Form->unlockField('Contact.id');
  2095. $this->Form->hidden('Contact.id', ['value' => 1]);
  2096. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2097. $this->assertEmpty($result, 'Field should be unlocked');
  2098. $this->Form->end();
  2099. $this->Form->create();
  2100. $this->Form->hidden('Contact.id', ['value' => 1]);
  2101. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2102. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2103. }
  2104. /**
  2105. * testSecuredFormUrlIgnoresHost method
  2106. *
  2107. * Test that only the path + query elements of a form's URL show up in their hash.
  2108. */
  2109. public function testSecuredFormUrlIgnoresHost(): void
  2110. {
  2111. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2112. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2113. $this->Form->create($this->article, [
  2114. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2115. ]);
  2116. $result = $this->Form->secure();
  2117. $this->assertStringContainsString($expected, $result);
  2118. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2119. $result = $this->Form->secure();
  2120. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2121. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2122. $result = $this->Form->secure();
  2123. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2124. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2125. $result = $this->Form->secure();
  2126. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2127. }
  2128. /**
  2129. * testSecuredFormUrlHasHtmlAndIdentifier method
  2130. *
  2131. * Test that URL, HTML and identifier show up in their hashes.
  2132. */
  2133. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2134. {
  2135. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2136. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2137. $this->Form->create($this->article, [
  2138. 'url' => [
  2139. 'controller' => 'articles',
  2140. 'action' => 'view',
  2141. '?' => [
  2142. 'page' => 1,
  2143. 'limit' => 10,
  2144. 'html' => '<>"',
  2145. ],
  2146. '#' => 'result',
  2147. ],
  2148. ]);
  2149. $result = $this->Form->secure();
  2150. $this->assertStringContainsString($expected, $result);
  2151. $this->Form->create($this->article, [
  2152. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2153. ]);
  2154. $result = $this->Form->secure();
  2155. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2156. $this->Form->create($this->article, [
  2157. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2158. ]);
  2159. $result = $this->Form->secure();
  2160. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2161. }
  2162. /**
  2163. * testErrorMessageDisplay method
  2164. *
  2165. * Test error message display.
  2166. */
  2167. public function testErrorMessageDisplay(): void
  2168. {
  2169. $this->article['errors'] = [
  2170. 'Article' => [
  2171. 'title' => 'error message',
  2172. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2173. ],
  2174. ];
  2175. $this->Form->create($this->article);
  2176. $result = $this->Form->control('Article.title');
  2177. $expected = [
  2178. 'div' => ['class' => 'input text error'],
  2179. 'label' => ['for' => 'article-title'],
  2180. 'Title',
  2181. '/label',
  2182. 'input' => [
  2183. 'type' => 'text',
  2184. 'name' => 'Article[title]',
  2185. 'id' => 'article-title',
  2186. 'class' => 'form-error',
  2187. 'aria-invalid' => 'true',
  2188. 'aria-describedby' => 'article-title-error',
  2189. ],
  2190. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2191. 'error message',
  2192. '/div',
  2193. '/div',
  2194. ];
  2195. $this->assertHtml($expected, $result);
  2196. $result = $this->Form->control('Article.title', [
  2197. 'templates' => [
  2198. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2199. ],
  2200. ]);
  2201. $expected = [
  2202. 'div' => ['class' => 'input text error'],
  2203. 'label' => ['for' => 'article-title'],
  2204. 'Title',
  2205. '/label',
  2206. 'input' => [
  2207. 'type' => 'text',
  2208. 'name' => 'Article[title]',
  2209. 'id' => 'article-title',
  2210. 'class' => 'form-error',
  2211. // No aria-describedby because error template is custom
  2212. 'aria-invalid' => 'true',
  2213. ],
  2214. '/div',
  2215. ];
  2216. $this->assertHtml($expected, $result);
  2217. $result = $this->Form->control('Article.content');
  2218. $expected = [
  2219. 'div' => ['class' => 'input text error'],
  2220. 'label' => ['for' => 'article-content'],
  2221. 'Content',
  2222. '/label',
  2223. 'input' => [
  2224. 'type' => 'text',
  2225. 'name' => 'Article[content]',
  2226. 'id' => 'article-content',
  2227. 'class' => 'form-error',
  2228. 'aria-invalid' => 'true',
  2229. 'aria-describedby' => 'article-content-error',
  2230. ],
  2231. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2232. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2233. '/div',
  2234. '/div',
  2235. ];
  2236. $this->assertHtml($expected, $result);
  2237. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2238. $expected = [
  2239. 'div' => ['class' => 'input text error'],
  2240. 'label' => ['for' => 'article-content'],
  2241. 'Content',
  2242. '/label',
  2243. 'input' => [
  2244. 'type' => 'text',
  2245. 'name' => 'Article[content]',
  2246. 'id' => 'article-content',
  2247. 'class' => 'form-error',
  2248. 'aria-invalid' => 'true',
  2249. 'aria-describedby' => 'article-content-error',
  2250. ],
  2251. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2252. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2253. '/div',
  2254. '/div',
  2255. ];
  2256. $this->assertHtml($expected, $result);
  2257. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2258. $expected = [
  2259. 'div' => ['class' => 'input text error'],
  2260. 'label' => ['for' => 'article-content'],
  2261. 'Content',
  2262. '/label',
  2263. 'input' => [
  2264. 'type' => 'text',
  2265. 'name' => 'Article[content]',
  2266. 'id' => 'article-content',
  2267. 'class' => 'form-error',
  2268. 'aria-invalid' => 'true',
  2269. 'aria-describedby' => 'article-content-error',
  2270. ],
  2271. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2272. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2273. '/div',
  2274. '/div',
  2275. ];
  2276. $this->assertHtml($expected, $result);
  2277. }
  2278. /**
  2279. * testEmptyErrorValidation method
  2280. *
  2281. * Test validation errors, when validation message is an empty string.
  2282. */
  2283. public function testEmptyErrorValidation(): void
  2284. {
  2285. $this->article['errors'] = [
  2286. 'Article' => ['title' => ''],
  2287. ];
  2288. $this->Form->create($this->article);
  2289. $result = $this->Form->control('Article.title');
  2290. $expected = [
  2291. 'div' => ['class' => 'input text error'],
  2292. 'label' => ['for' => 'article-title'],
  2293. 'Title',
  2294. '/label',
  2295. 'input' => [
  2296. 'type' => 'text',
  2297. 'name' => 'Article[title]',
  2298. 'id' => 'article-title',
  2299. 'class' => 'form-error',
  2300. 'aria-invalid' => 'true',
  2301. 'aria-describedby' => 'article-title-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. * testEmptyControlErrorValidation method
  2312. *
  2313. * Test validation errors, when calling control() overriding validation message by an empty string.
  2314. */
  2315. public function testEmptyControlErrorValidation(): void
  2316. {
  2317. $this->article['errors'] = [
  2318. 'Article' => ['title' => 'error message'],
  2319. ];
  2320. $this->Form->create($this->article);
  2321. $result = $this->Form->control('Article.title', ['error' => '']);
  2322. $expected = [
  2323. 'div' => ['class' => 'input text error'],
  2324. 'label' => ['for' => 'article-title'],
  2325. 'Title',
  2326. '/label',
  2327. 'input' => [
  2328. 'aria-invalid' => 'true',
  2329. 'aria-describedby' => 'article-title-error',
  2330. 'type' => 'text',
  2331. 'name' => 'Article[title]',
  2332. 'id' => 'article-title',
  2333. 'class' => 'form-error',
  2334. ],
  2335. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2336. [],
  2337. '/div',
  2338. '/div',
  2339. ];
  2340. $this->assertHtml($expected, $result);
  2341. }
  2342. /**
  2343. * testControlErrorMessage method
  2344. *
  2345. * Test validation errors, when calling control() overriding validation messages.
  2346. */
  2347. public function testControlErrorMessage(): void
  2348. {
  2349. $this->article['errors'] = [
  2350. 'title' => ['error message'],
  2351. ];
  2352. $this->Form->create($this->article);
  2353. $result = $this->Form->control('title', [
  2354. 'error' => 'Custom error!',
  2355. ]);
  2356. $expected = [
  2357. 'div' => ['class' => 'input text required error'],
  2358. 'label' => ['for' => 'title'],
  2359. 'Title',
  2360. '/label',
  2361. 'input' => [
  2362. 'type' => 'text',
  2363. 'name' => 'title',
  2364. 'id' => 'title',
  2365. 'class' => 'form-error',
  2366. 'required' => 'required',
  2367. 'data-validity-message' => 'This field cannot be left empty',
  2368. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2369. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2370. 'aria-required' => 'true',
  2371. 'aria-invalid' => 'true',
  2372. 'aria-describedby' => 'title-error',
  2373. ],
  2374. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2375. 'Custom error!',
  2376. '/div',
  2377. '/div',
  2378. ];
  2379. $this->assertHtml($expected, $result);
  2380. $result = $this->Form->control('title', [
  2381. 'error' => ['error message' => 'Custom error!'],
  2382. ]);
  2383. $expected = [
  2384. 'div' => ['class' => 'input text required error'],
  2385. 'label' => ['for' => 'title'],
  2386. 'Title',
  2387. '/label',
  2388. 'input' => [
  2389. 'type' => 'text',
  2390. 'name' => 'title',
  2391. 'id' => 'title',
  2392. 'aria-required' => 'true',
  2393. 'aria-invalid' => 'true',
  2394. 'aria-describedby' => 'title-error',
  2395. 'class' => 'form-error',
  2396. 'required' => 'required',
  2397. 'data-validity-message' => 'This field cannot be left empty',
  2398. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2399. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2400. ],
  2401. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2402. 'Custom error!',
  2403. '/div',
  2404. '/div',
  2405. ];
  2406. $this->assertHtml($expected, $result);
  2407. }
  2408. /**
  2409. * testFormValidationAssociated method
  2410. *
  2411. * Tests displaying errors for nested entities.
  2412. */
  2413. public function testFormValidationAssociated(): void
  2414. {
  2415. $nested = new Entity(['foo' => 'bar']);
  2416. $nested->setError('foo', ['not a valid bar']);
  2417. $entity = new Entity(['nested' => $nested]);
  2418. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2419. $result = $this->Form->error('nested.foo');
  2420. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2421. }
  2422. /**
  2423. * testFormValidationAssociatedSecondLevel method
  2424. *
  2425. * Test form error display with associated model.
  2426. */
  2427. public function testFormValidationAssociatedSecondLevel(): void
  2428. {
  2429. $inner = new Entity(['bar' => 'baz']);
  2430. $nested = new Entity(['foo' => $inner]);
  2431. $entity = new Entity(['nested' => $nested]);
  2432. $inner->setError('bar', ['not a valid one']);
  2433. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2434. $result = $this->Form->error('nested.foo.bar');
  2435. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2436. }
  2437. /**
  2438. * testFormValidationMultiRecord method
  2439. *
  2440. * Test form error display with multiple records.
  2441. */
  2442. public function testFormValidationMultiRecord(): void
  2443. {
  2444. $one = new Entity();
  2445. $two = new Entity();
  2446. $this->getTableLocator()->get('Contacts', [
  2447. 'className' => ContactsTable::class,
  2448. ]);
  2449. $one->set('email', '');
  2450. $one->setError('email', ['invalid email']);
  2451. $two->set('name', '');
  2452. $two->setError('name', ['This is wrong']);
  2453. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2454. $result = $this->Form->control('0.email');
  2455. $expected = [
  2456. 'div' => ['class' => 'input email error'],
  2457. 'label' => ['for' => '0-email'],
  2458. 'Email',
  2459. '/label',
  2460. 'input' => [
  2461. 'type' => 'email',
  2462. 'name' => '0[email]',
  2463. 'id' => '0-email',
  2464. 'class' => 'form-error',
  2465. 'maxlength' => 255,
  2466. 'value' => '',
  2467. 'aria-invalid' => 'true',
  2468. 'aria-describedby' => '0-email-error',
  2469. ],
  2470. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2471. 'invalid email',
  2472. '/div',
  2473. '/div',
  2474. ];
  2475. $this->assertHtml($expected, $result);
  2476. $result = $this->Form->control('1.name');
  2477. $expected = [
  2478. 'div' => ['class' => 'input text error'],
  2479. 'label' => ['for' => '1-name'],
  2480. 'Name',
  2481. '/label',
  2482. 'input' => [
  2483. 'type' => 'text',
  2484. 'name' => '1[name]',
  2485. 'id' => '1-name',
  2486. 'class' => 'form-error',
  2487. 'maxlength' => 255,
  2488. 'value' => '',
  2489. 'aria-invalid' => 'true',
  2490. 'aria-describedby' => '1-name-error',
  2491. ],
  2492. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2493. 'This is wrong',
  2494. '/div',
  2495. '/div',
  2496. ];
  2497. $this->assertHtml($expected, $result);
  2498. }
  2499. /**
  2500. * testControl method
  2501. *
  2502. * Test various incarnations of control().
  2503. */
  2504. public function testControl(): void
  2505. {
  2506. $this->getTableLocator()->get('ValidateUsers', [
  2507. 'className' => ValidateUsersTable::class,
  2508. ]);
  2509. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2510. $result = $this->Form->control('ValidateUsers.balance');
  2511. $expected = [
  2512. 'div' => ['class'],
  2513. 'label' => ['for'],
  2514. 'Balance',
  2515. '/label',
  2516. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2517. '/div',
  2518. ];
  2519. $this->assertHtml($expected, $result);
  2520. $result = $this->Form->control('ValidateUser.cost_decimal');
  2521. $expected = [
  2522. 'div' => ['class'],
  2523. 'label' => ['for'],
  2524. 'Cost Decimal',
  2525. '/label',
  2526. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2527. '/div',
  2528. ];
  2529. $this->assertHtml($expected, $result);
  2530. $result = $this->Form->control('ValidateUser.null_decimal');
  2531. $expected = [
  2532. 'div' => ['class'],
  2533. 'label' => ['for'],
  2534. 'Null Decimal',
  2535. '/label',
  2536. 'input' => ['name', 'type' => 'number', 'id'],
  2537. '/div',
  2538. ];
  2539. $this->assertHtml($expected, $result);
  2540. }
  2541. /**
  2542. * testControlCustomization method
  2543. *
  2544. * Tests the input method and passing custom options.
  2545. */
  2546. public function testControlCustomization(): void
  2547. {
  2548. $this->getTableLocator()->get('Contacts', [
  2549. 'className' => ContactsTable::class,
  2550. ]);
  2551. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2552. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2553. $expected = [
  2554. 'div' => ['class' => 'input email'],
  2555. 'label' => ['for' => 'custom'],
  2556. 'Email',
  2557. '/label',
  2558. ['input' => [
  2559. 'type' => 'email', 'name' => 'Contact[email]',
  2560. 'id' => 'custom', 'maxlength' => 255,
  2561. ]],
  2562. '/div',
  2563. ];
  2564. $this->assertHtml($expected, $result);
  2565. $result = $this->Form->control('Contact.email', [
  2566. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2567. ]);
  2568. $expected = [
  2569. '<div',
  2570. 'label' => ['for' => 'contact-email'],
  2571. 'Email',
  2572. '/label',
  2573. ['input' => [
  2574. 'type' => 'email', 'name' => 'Contact[email]',
  2575. 'id' => 'contact-email', 'maxlength' => 255,
  2576. ]],
  2577. '/div',
  2578. ];
  2579. $this->assertHtml($expected, $result);
  2580. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2581. $expected = [
  2582. 'div' => ['class' => 'input text'],
  2583. 'label' => ['for' => 'contact-email'],
  2584. 'Email',
  2585. '/label',
  2586. ['input' => [
  2587. 'type' => 'text', 'name' => 'Contact[email]',
  2588. 'id' => 'contact-email', 'maxlength' => '255',
  2589. ]],
  2590. '/div',
  2591. ];
  2592. $this->assertHtml($expected, $result);
  2593. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2594. $expected = [
  2595. 'div' => ['class' => 'input text'],
  2596. 'label' => ['for' => 'contact-5-email'],
  2597. 'Email',
  2598. '/label',
  2599. ['input' => [
  2600. 'type' => 'text', 'name' => 'Contact[5][email]',
  2601. 'id' => 'contact-5-email', 'maxlength' => '255',
  2602. ]],
  2603. '/div',
  2604. ];
  2605. $this->assertHtml($expected, $result);
  2606. $result = $this->Form->control('Contact.password');
  2607. $expected = [
  2608. 'div' => ['class' => 'input password'],
  2609. 'label' => ['for' => 'contact-password'],
  2610. 'Password',
  2611. '/label',
  2612. ['input' => [
  2613. 'type' => 'password', 'name' => 'Contact[password]',
  2614. 'id' => 'contact-password',
  2615. ]],
  2616. '/div',
  2617. ];
  2618. $this->assertHtml($expected, $result);
  2619. $result = $this->Form->control('Contact.email', [
  2620. 'type' => 'file', 'class' => 'textbox',
  2621. ]);
  2622. $expected = [
  2623. 'div' => ['class' => 'input file'],
  2624. 'label' => ['for' => 'contact-email'],
  2625. 'Email',
  2626. '/label',
  2627. ['input' => [
  2628. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2629. 'id' => 'contact-email',
  2630. ]],
  2631. '/div',
  2632. ];
  2633. $this->assertHtml($expected, $result);
  2634. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2635. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2636. $result = $this->Form->control('phone');
  2637. $expected = [
  2638. 'div' => ['class' => 'input tel'],
  2639. 'label' => ['for' => 'phone'],
  2640. 'Phone',
  2641. '/label',
  2642. ['input' => [
  2643. 'type' => 'tel', 'name' => 'phone',
  2644. 'value' => 'Hello &amp; World &gt; weird chars',
  2645. 'id' => 'phone', 'maxlength' => 255,
  2646. ]],
  2647. '/div',
  2648. ];
  2649. $this->assertHtml($expected, $result);
  2650. $this->View->setRequest(
  2651. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2652. );
  2653. $this->Form->create();
  2654. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2655. $expected = [
  2656. 'div' => ['class' => 'input text'],
  2657. 'label' => ['for' => 'myId'],
  2658. 'Field',
  2659. '/label',
  2660. 'input' => [
  2661. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2662. 'value' => 'My value', 'id' => 'myId',
  2663. ],
  2664. '/div',
  2665. ];
  2666. $this->assertHtml($expected, $result);
  2667. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2668. $this->Form->create();
  2669. $entity->setError('field', 'Badness!');
  2670. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2671. $result = $this->Form->control('field');
  2672. $expected = [
  2673. 'div' => ['class' => 'input text error'],
  2674. 'label' => ['for' => 'field'],
  2675. 'Field',
  2676. '/label',
  2677. 'input' => [
  2678. 'type' => 'text',
  2679. 'name' => 'field',
  2680. 'id' => 'field',
  2681. 'class' => 'form-error',
  2682. 'aria-invalid' => 'true',
  2683. 'aria-describedby' => 'field-error',
  2684. ],
  2685. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2686. 'Badness!',
  2687. '/div',
  2688. '/div',
  2689. ];
  2690. $this->assertHtml($expected, $result);
  2691. $result = $this->Form->control('field', [
  2692. 'templates' => [
  2693. 'inputContainerError' => '{{content}}{{error}}',
  2694. 'error' => '<span class="error-message">{{content}}</span>',
  2695. ],
  2696. ]);
  2697. $expected = [
  2698. 'label' => ['for' => 'field'],
  2699. 'Field',
  2700. '/label',
  2701. 'input' => [
  2702. 'type' => 'text',
  2703. 'name' => 'field',
  2704. 'id' => 'field',
  2705. 'class' => 'form-error',
  2706. // No aria-describedby because error template is custom
  2707. 'aria-invalid' => 'true',
  2708. ],
  2709. ['span' => ['class' => 'error-message']],
  2710. 'Badness!',
  2711. '/span',
  2712. ];
  2713. $this->assertHtml($expected, $result);
  2714. $entity->setError('field', ['minLength'], true);
  2715. $result = $this->Form->control('field', [
  2716. 'error' => [
  2717. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2718. 'maxLength' => 'login too large',
  2719. ],
  2720. ]);
  2721. $expected = [
  2722. 'div' => ['class' => 'input text error'],
  2723. 'label' => ['for' => 'field'],
  2724. 'Field',
  2725. '/label',
  2726. 'input' => [
  2727. 'type' => 'text',
  2728. 'name' => 'field',
  2729. 'id' => 'field',
  2730. 'class' => 'form-error',
  2731. 'aria-invalid' => 'true',
  2732. 'aria-describedby' => 'field-error',
  2733. ],
  2734. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2735. 'Le login doit contenir au moins 2 caractères',
  2736. '/div',
  2737. '/div',
  2738. ];
  2739. $this->assertHtml($expected, $result);
  2740. $entity->setError('field', ['maxLength'], true);
  2741. $result = $this->Form->control('field', [
  2742. 'error' => [
  2743. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2744. 'maxLength' => 'login too large',
  2745. ],
  2746. ]);
  2747. $expected = [
  2748. 'div' => ['class' => 'input text error'],
  2749. 'label' => ['for' => 'field'],
  2750. 'Field',
  2751. '/label',
  2752. 'input' => [
  2753. 'type' => 'text',
  2754. 'name' => 'field',
  2755. 'id' => 'field',
  2756. 'class' => 'form-error',
  2757. 'aria-invalid' => 'true',
  2758. 'aria-describedby' => 'field-error',
  2759. ],
  2760. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2761. 'login too large',
  2762. '/div',
  2763. '/div',
  2764. ];
  2765. $this->assertHtml($expected, $result);
  2766. }
  2767. /**
  2768. * testControlWithTemplateFile method
  2769. *
  2770. * Test that control() accepts a template file.
  2771. */
  2772. public function testControlWithTemplateFile(): void
  2773. {
  2774. $result = $this->Form->control('field', [
  2775. 'templates' => 'htmlhelper_tags',
  2776. ]);
  2777. $expected = [
  2778. 'label' => ['for' => 'field'],
  2779. 'Field',
  2780. '/label',
  2781. 'input' => [
  2782. 'type' => 'text', 'name' => 'field',
  2783. 'id' => 'field',
  2784. ],
  2785. ];
  2786. $this->assertHtml($expected, $result);
  2787. }
  2788. /**
  2789. * testNestedControlsEndWithBrackets method
  2790. *
  2791. * Test that nested inputs end with brackets.
  2792. */
  2793. public function testNestedControlsEndWithBrackets(): void
  2794. {
  2795. $result = $this->Form->text('nested.text[]');
  2796. $expected = [
  2797. 'input' => [
  2798. 'type' => 'text', 'name' => 'nested[text][]',
  2799. ],
  2800. ];
  2801. $this->assertHtml($expected, $result);
  2802. $result = $this->Form->file('nested.file[]');
  2803. $expected = [
  2804. 'input' => [
  2805. 'type' => 'file', 'name' => 'nested[file][]',
  2806. ],
  2807. ];
  2808. $this->assertHtml($expected, $result);
  2809. }
  2810. /**
  2811. * testCreateIdPrefix method
  2812. *
  2813. * Test id prefix.
  2814. */
  2815. public function testCreateIdPrefix(): void
  2816. {
  2817. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2818. $result = $this->Form->control('field');
  2819. $expected = [
  2820. 'div' => ['class' => 'input text'],
  2821. 'label' => ['for' => 'prefix-field'],
  2822. 'Field',
  2823. '/label',
  2824. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2825. '/div',
  2826. ];
  2827. $this->assertHtml($expected, $result);
  2828. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2829. $expected = [
  2830. 'div' => ['class' => 'input text'],
  2831. 'label' => ['for' => 'custom-id'],
  2832. 'Field',
  2833. '/label',
  2834. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2835. '/div',
  2836. ];
  2837. $this->assertHtml($expected, $result);
  2838. $result = $this->Form->radio('Model.field', ['option A']);
  2839. $expected = [
  2840. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2841. 'label' => ['for' => 'prefix-model-field-0'],
  2842. ['input' => [
  2843. 'type' => 'radio',
  2844. 'name' => 'Model[field]',
  2845. 'value' => '0',
  2846. 'id' => 'prefix-model-field-0',
  2847. ]],
  2848. 'option A',
  2849. '/label',
  2850. ];
  2851. $this->assertHtml($expected, $result);
  2852. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2853. $expected = [
  2854. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
  2855. 'label' => ['for' => 'prefix-model-field-0'],
  2856. ['input' => [
  2857. 'type' => 'radio',
  2858. 'name' => 'Model[field]',
  2859. 'value' => '0',
  2860. 'id' => 'prefix-model-field-0',
  2861. ]],
  2862. 'option A',
  2863. '/label',
  2864. ];
  2865. $this->assertHtml($expected, $result);
  2866. $result = $this->Form->select(
  2867. 'Model.multi_field',
  2868. ['first'],
  2869. ['multiple' => 'checkbox']
  2870. );
  2871. $expected = [
  2872. 'input' => [
  2873. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'prefix-model-multi-field',
  2874. ],
  2875. ['div' => ['class' => 'checkbox']],
  2876. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2877. ['input' => [
  2878. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2879. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2880. ]],
  2881. 'first',
  2882. '/label',
  2883. '/div',
  2884. ];
  2885. $this->assertHtml($expected, $result);
  2886. $this->Form->end();
  2887. $result = $this->Form->control('field');
  2888. $expected = [
  2889. 'div' => ['class' => 'input text'],
  2890. 'label' => ['for' => 'field'],
  2891. 'Field',
  2892. '/label',
  2893. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2894. '/div',
  2895. ];
  2896. $this->assertHtml($expected, $result);
  2897. }
  2898. /**
  2899. * testControlZero method
  2900. *
  2901. * Test that inputs with 0 can be created.
  2902. */
  2903. public function testControlZero(): void
  2904. {
  2905. $this->getTableLocator()->get('Contacts', [
  2906. 'className' => ContactsTable::class,
  2907. ]);
  2908. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2909. $result = $this->Form->control('0');
  2910. $expected = [
  2911. 'div' => ['class' => 'input text'],
  2912. 'label' => ['for' => '0'], '/label',
  2913. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2914. '/div',
  2915. ];
  2916. $this->assertHtml($expected, $result);
  2917. }
  2918. /**
  2919. * testControlCheckbox method
  2920. *
  2921. * Test control() with checkbox creation.
  2922. */
  2923. public function testControlCheckbox(): void
  2924. {
  2925. $articles = $this->getTableLocator()->get('Articles');
  2926. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2927. $article = $articles->newEmptyEntity();
  2928. $this->Form->create($article);
  2929. $result = $this->Form->control('Articles.active');
  2930. $expected = [
  2931. 'div' => ['class' => 'input checkbox'],
  2932. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2933. 'label' => ['for' => 'articles-active'],
  2934. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2935. 'Active',
  2936. '/label',
  2937. '/div',
  2938. ];
  2939. $this->assertHtml($expected, $result);
  2940. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2941. $expected = [
  2942. 'div' => ['class' => 'input checkbox'],
  2943. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2944. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2945. '/div',
  2946. ];
  2947. $this->assertHtml($expected, $result);
  2948. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2949. $expected = [
  2950. 'div' => ['class' => 'input checkbox'],
  2951. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2952. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2953. '/div',
  2954. ];
  2955. $this->assertHtml($expected, $result);
  2956. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2957. $expected = [
  2958. 'div' => ['class' => 'input checkbox'],
  2959. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2960. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2961. '/div',
  2962. ];
  2963. $this->assertHtml($expected, $result);
  2964. $result = $this->Form->control('Articles.disabled', [
  2965. 'label' => 'Disabled',
  2966. 'type' => 'checkbox',
  2967. 'data-foo' => 'disabled',
  2968. ]);
  2969. $expected = [
  2970. 'div' => ['class' => 'input checkbox'],
  2971. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2972. 'label' => ['for' => 'articles-disabled'],
  2973. ['input' => [
  2974. 'type' => 'checkbox',
  2975. 'name' => 'Articles[disabled]',
  2976. 'value' => '1',
  2977. 'id' => 'articles-disabled',
  2978. 'data-foo' => 'disabled',
  2979. ]],
  2980. 'Disabled',
  2981. '/label',
  2982. '/div',
  2983. ];
  2984. $this->assertHtml($expected, $result);
  2985. $result = $this->Form->control('Articles.confirm', [
  2986. 'label' => 'Confirm <b>me</b>!',
  2987. 'type' => 'checkbox',
  2988. 'escape' => false,
  2989. ]);
  2990. $expected = [
  2991. 'div' => ['class' => 'input checkbox'],
  2992. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  2993. 'label' => ['for' => 'articles-confirm'],
  2994. ['input' => [
  2995. 'type' => 'checkbox',
  2996. 'name' => 'Articles[confirm]',
  2997. 'value' => '1',
  2998. 'id' => 'articles-confirm',
  2999. ]],
  3000. 'Confirm <b>me</b>!',
  3001. '/label',
  3002. '/div',
  3003. ];
  3004. $this->assertHtml($expected, $result);
  3005. }
  3006. /**
  3007. * testControlHidden method
  3008. *
  3009. * Test that control() does not create wrapping div and label tag for hidden fields.
  3010. */
  3011. public function testControlHidden(): void
  3012. {
  3013. $this->getTableLocator()->get('ValidateUsers', [
  3014. 'className' => ValidateUsersTable::class,
  3015. ]);
  3016. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3017. $result = $this->Form->control('ValidateUser.id');
  3018. $expected = [
  3019. 'input' => ['name', 'type' => 'hidden', 'id'],
  3020. ];
  3021. $this->assertHtml($expected, $result);
  3022. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3023. $expected = [
  3024. 'input' => ['name', 'type' => 'hidden', 'id'],
  3025. ];
  3026. $this->assertHtml($expected, $result);
  3027. }
  3028. /**
  3029. * testControlDatetime method
  3030. *
  3031. * Test form->control() with datetime.
  3032. */
  3033. public function testControlDatetime(): void
  3034. {
  3035. $result = $this->Form->control('prueba', [
  3036. 'type' => 'datetime',
  3037. 'value' => new FrozenTime('2019-09-27 02:52:43'),
  3038. ]);
  3039. $expected = [
  3040. 'div' => ['class' => 'input datetime'],
  3041. 'label' => ['for' => 'prueba'],
  3042. 'Prueba',
  3043. '/label',
  3044. 'input' => [
  3045. 'name' => 'prueba',
  3046. 'id' => 'prueba',
  3047. 'type' => 'datetime-local',
  3048. 'value' => '2019-09-27T02:52:43',
  3049. 'step' => '1',
  3050. ],
  3051. '/div',
  3052. ];
  3053. $this->assertHtml($expected, $result);
  3054. }
  3055. /**
  3056. * testControlDatetimeIdPrefix method
  3057. *
  3058. * Test form->control() with datetime with id prefix.
  3059. */
  3060. public function testControlDatetimeIdPrefix(): void
  3061. {
  3062. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3063. $result = $this->Form->control('prueba', [
  3064. 'type' => 'datetime',
  3065. ]);
  3066. $expected = [
  3067. 'div' => ['class' => 'input datetime'],
  3068. 'label' => ['for' => 'prefix-prueba'],
  3069. 'Prueba',
  3070. '/label',
  3071. 'input' => [
  3072. 'name' => 'prueba',
  3073. 'id' => 'prefix-prueba',
  3074. 'type' => 'datetime-local',
  3075. 'value' => '',
  3076. 'step' => '1',
  3077. ],
  3078. '/div',
  3079. ];
  3080. $this->assertHtml($expected, $result);
  3081. }
  3082. /**
  3083. * testControlDatetimeStep method
  3084. *
  3085. * Test form->control() with datetime with custom step size.
  3086. */
  3087. public function testControlDatetimeStep(): void
  3088. {
  3089. $result = $this->Form->control('prueba', [
  3090. 'type' => 'datetime',
  3091. 'value' => new FrozenTime('2019-09-27 02:52:43'),
  3092. 'step' => '0.5',
  3093. ]);
  3094. $expected = [
  3095. 'div' => ['class' => 'input datetime'],
  3096. 'label' => ['for' => 'prueba'],
  3097. 'Prueba',
  3098. '/label',
  3099. 'input' => [
  3100. 'name' => 'prueba',
  3101. 'id' => 'prueba',
  3102. 'type' => 'datetime-local',
  3103. 'value' => '2019-09-27T02:52:43.000',
  3104. 'step' => '0.5',
  3105. ],
  3106. '/div',
  3107. ];
  3108. $this->assertHtml($expected, $result);
  3109. }
  3110. /**
  3111. * testControlCheckboxWithDisabledElements method
  3112. *
  3113. * Test generating checkboxes with disabled elements.
  3114. */
  3115. public function testControlCheckboxWithDisabledElements(): void
  3116. {
  3117. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3118. $result = $this->Form->control('Contact.multiple', [
  3119. 'multiple' => 'checkbox',
  3120. 'disabled' => 'disabled',
  3121. 'options' => $options,
  3122. ]);
  3123. $expected = [
  3124. ['div' => ['class' => 'input select']],
  3125. ['label' => ['for' => 'contact-multiple']],
  3126. 'Multiple',
  3127. '/label',
  3128. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '', 'id' => 'contact-multiple']],
  3129. ['div' => ['class' => 'checkbox']],
  3130. ['label' => ['for' => 'contact-multiple-1']],
  3131. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3132. 'One',
  3133. '/label',
  3134. '/div',
  3135. ['div' => ['class' => 'checkbox']],
  3136. ['label' => ['for' => 'contact-multiple-2']],
  3137. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3138. 'Two',
  3139. '/label',
  3140. '/div',
  3141. ['div' => ['class' => 'checkbox']],
  3142. ['label' => ['for' => 'contact-multiple-3']],
  3143. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3144. 'Three',
  3145. '/label',
  3146. '/div',
  3147. '/div',
  3148. ];
  3149. $this->assertHtml($expected, $result);
  3150. // make sure 50 does only disable 50, and not 50f5c0cf
  3151. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3152. $disabled = [50];
  3153. $expected = [
  3154. ['div' => ['class' => 'input select']],
  3155. ['label' => ['for' => 'contact-multiple']],
  3156. 'Multiple',
  3157. '/label',
  3158. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'contact-multiple']],
  3159. ['div' => ['class' => 'checkbox']],
  3160. ['label' => ['for' => 'contact-multiple-50']],
  3161. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3162. 'Fifty',
  3163. '/label',
  3164. '/div',
  3165. ['div' => ['class' => 'checkbox']],
  3166. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3167. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3168. 'Stringy',
  3169. '/label',
  3170. '/div',
  3171. '/div',
  3172. ];
  3173. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3174. $this->assertHtml($expected, $result);
  3175. }
  3176. /**
  3177. * testControlWithLeadingInteger method
  3178. *
  3179. * Test input name with leading integer, ensure attributes are generated correctly.
  3180. */
  3181. public function testControlWithLeadingInteger(): void
  3182. {
  3183. $result = $this->Form->text('0.Node.title');
  3184. $expected = [
  3185. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3186. ];
  3187. $this->assertHtml($expected, $result);
  3188. }
  3189. /**
  3190. * testControlSelectType method
  3191. *
  3192. * Test form->control() with select type inputs.
  3193. */
  3194. public function testControlSelectType(): void
  3195. {
  3196. $result = $this->Form->control(
  3197. 'email',
  3198. [
  3199. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3200. );
  3201. $expected = [
  3202. 'div' => ['class' => 'input select'],
  3203. 'label' => ['for' => 'email'],
  3204. 'Email',
  3205. '/label',
  3206. ['select' => ['name' => 'email', 'id' => 'email']],
  3207. ['option' => ['value' => '']],
  3208. '/option',
  3209. ['option' => ['value' => 'è']],
  3210. 'Firést',
  3211. '/option',
  3212. ['option' => ['value' => 'é']],
  3213. 'Secoènd',
  3214. '/option',
  3215. '/select',
  3216. '/div',
  3217. ];
  3218. $this->assertHtml($expected, $result);
  3219. $result = $this->Form->control(
  3220. 'email',
  3221. [
  3222. 'options' => ['First', 'Second'], 'empty' => true]
  3223. );
  3224. $expected = [
  3225. 'div' => ['class' => 'input select'],
  3226. 'label' => ['for' => 'email'],
  3227. 'Email',
  3228. '/label',
  3229. ['select' => ['name' => 'email', 'id' => 'email']],
  3230. ['option' => ['value' => '']],
  3231. '/option',
  3232. ['option' => ['value' => '0']],
  3233. 'First',
  3234. '/option',
  3235. ['option' => ['value' => '1']],
  3236. 'Second',
  3237. '/option',
  3238. '/select',
  3239. '/div',
  3240. ];
  3241. $this->assertHtml($expected, $result);
  3242. $result = $this->Form->control('email', [
  3243. 'type' => 'select',
  3244. 'options' => new ArrayObject(['First', 'Second']),
  3245. 'empty' => true,
  3246. ]);
  3247. $this->assertHtml($expected, $result);
  3248. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3249. $this->View->setRequest(
  3250. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3251. );
  3252. $this->Form->create();
  3253. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3254. $expected = [
  3255. 'div' => ['class' => 'input select'],
  3256. 'label' => ['for' => 'model-user-id'],
  3257. 'User',
  3258. '/label',
  3259. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3260. ['option' => ['value' => '']],
  3261. '/option',
  3262. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3263. 'good',
  3264. '/option',
  3265. ['option' => ['value' => 'other']],
  3266. 'bad',
  3267. '/option',
  3268. '/select',
  3269. '/div',
  3270. ];
  3271. $this->assertHtml($expected, $result);
  3272. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3273. $this->View->setRequest(
  3274. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3275. );
  3276. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3277. $expected = [
  3278. 'div' => ['class' => 'input select'],
  3279. 'label' => ['for' => 'thing-user-id'],
  3280. 'User',
  3281. '/label',
  3282. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3283. ['option' => ['value' => '']],
  3284. 'Some Empty',
  3285. '/option',
  3286. ['option' => ['value' => 'value']],
  3287. 'good',
  3288. '/option',
  3289. ['option' => ['value' => 'other']],
  3290. 'bad',
  3291. '/option',
  3292. '/select',
  3293. '/div',
  3294. ];
  3295. $this->assertHtml($expected, $result);
  3296. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3297. $this->View->setRequest(
  3298. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3299. );
  3300. $this->Form->create();
  3301. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3302. $expected = [
  3303. 'div' => ['class' => 'input select'],
  3304. 'label' => ['for' => 'thing-user-id'],
  3305. 'User',
  3306. '/label',
  3307. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3308. ['option' => ['value' => '']],
  3309. 'Some Empty',
  3310. '/option',
  3311. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3312. 'good',
  3313. '/option',
  3314. ['option' => ['value' => 'other']],
  3315. 'bad',
  3316. '/option',
  3317. '/select',
  3318. '/div',
  3319. ];
  3320. $this->assertHtml($expected, $result);
  3321. $result = $this->Form->control('Publisher.id', [
  3322. 'label' => 'Publisher',
  3323. 'type' => 'select',
  3324. 'multiple' => 'checkbox',
  3325. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3326. ]);
  3327. $expected = [
  3328. ['div' => ['class' => 'input select']],
  3329. ['label' => ['for' => 'publisher-id']],
  3330. 'Publisher',
  3331. '/label',
  3332. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'publisher-id'],
  3333. ['div' => ['class' => 'checkbox']],
  3334. ['label' => ['for' => 'publisher-id-value-1']],
  3335. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3336. 'Label 1',
  3337. '/label',
  3338. '/div',
  3339. ['div' => ['class' => 'checkbox']],
  3340. ['label' => ['for' => 'publisher-id-value-2']],
  3341. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3342. 'Label 2',
  3343. '/label',
  3344. '/div',
  3345. '/div',
  3346. ];
  3347. $this->assertHtml($expected, $result);
  3348. }
  3349. /**
  3350. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3351. *
  3352. * Test that control() and a non standard primary key makes a hidden input by default.
  3353. */
  3354. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3355. {
  3356. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3357. $this->Form->create($this->article);
  3358. $result = $this->Form->control('title');
  3359. $expected = [
  3360. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3361. ];
  3362. $this->assertHtml($expected, $result);
  3363. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3364. $this->Form->create($this->article);
  3365. $result = $this->Form->control('title');
  3366. $expected = [
  3367. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3368. ];
  3369. $this->assertHtml($expected, $result);
  3370. $result = $this->Form->control('body');
  3371. $expected = [
  3372. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3373. ];
  3374. $this->assertHtml($expected, $result);
  3375. }
  3376. /**
  3377. * testControlOverridingMagicSelectType method
  3378. *
  3379. * Test that overriding the magic select type widget is possible.
  3380. */
  3381. public function testControlOverridingMagicSelectType(): void
  3382. {
  3383. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3384. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3385. $expected = [
  3386. 'div' => ['class' => 'input text'],
  3387. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3388. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3389. '/div',
  3390. ];
  3391. $this->assertHtml($expected, $result);
  3392. //Check that magic types still work for plural/singular vars
  3393. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3394. $result = $this->Form->control('Model.type');
  3395. $expected = [
  3396. 'div' => ['class' => 'input select'],
  3397. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3398. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3399. ['option' => ['value' => 'value']], 'good', '/option',
  3400. ['option' => ['value' => 'other']], 'bad', '/option',
  3401. '/select',
  3402. '/div',
  3403. ];
  3404. $this->assertHtml($expected, $result);
  3405. }
  3406. /**
  3407. * testControlMagicTypeDoesNotOverride method
  3408. *
  3409. * Test that inferred types do not override developer input.
  3410. */
  3411. public function testControlMagicTypeDoesNotOverride(): void
  3412. {
  3413. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3414. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3415. $expected = [
  3416. 'div' => ['class' => 'input checkbox'],
  3417. ['input' => [
  3418. 'type' => 'hidden',
  3419. 'name' => 'Model[user]',
  3420. 'value' => 0,
  3421. ]],
  3422. 'label' => ['for' => 'model-user'],
  3423. ['input' => [
  3424. 'name' => 'Model[user]',
  3425. 'type' => 'checkbox',
  3426. 'id' => 'model-user',
  3427. 'value' => 1,
  3428. ]],
  3429. 'User',
  3430. '/label',
  3431. '/div',
  3432. ];
  3433. $this->assertHtml($expected, $result);
  3434. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3435. $options = [
  3436. 1 => 'blue',
  3437. 2 => 'red',
  3438. ];
  3439. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3440. $expected = [
  3441. 'div' => ['class' => 'input select'],
  3442. 'label' => ['for' => 'tags-ids'],
  3443. 'Tags',
  3444. '/label',
  3445. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => '', 'id' => 'tags-ids'],
  3446. ['div' => ['class' => 'checkbox']],
  3447. ['label' => ['for' => 'tags-ids-1']],
  3448. ['input' => [
  3449. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3450. 'value' => '1', 'name' => 'tags[_ids][]',
  3451. ]],
  3452. 'blue',
  3453. '/label',
  3454. '/div',
  3455. ['div' => ['class' => 'checkbox']],
  3456. ['label' => ['for' => 'tags-ids-2']],
  3457. ['input' => [
  3458. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3459. 'value' => '2', 'name' => 'tags[_ids][]',
  3460. ]],
  3461. 'red',
  3462. '/label',
  3463. '/div',
  3464. '/div',
  3465. ];
  3466. $this->assertHtml($expected, $result);
  3467. }
  3468. /**
  3469. * testControlMagicSelectForTypeNumber method
  3470. *
  3471. * Test that magic control() selects are created for type=number.
  3472. */
  3473. public function testControlMagicSelectForTypeNumber(): void
  3474. {
  3475. $this->getTableLocator()->get('ValidateUsers', [
  3476. 'className' => ValidateUsersTable::class,
  3477. ]);
  3478. $entity = new Entity(['balance' => 1]);
  3479. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3480. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3481. $result = $this->Form->control('balance');
  3482. $expected = [
  3483. 'div' => ['class' => 'input select'],
  3484. 'label' => ['for' => 'balance'],
  3485. 'Balance',
  3486. '/label',
  3487. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3488. ['option' => ['value' => '0']],
  3489. 'nothing',
  3490. '/option',
  3491. ['option' => ['value' => '1', 'selected' => 'selected']],
  3492. 'some',
  3493. '/option',
  3494. ['option' => ['value' => '100']],
  3495. 'a lot',
  3496. '/option',
  3497. '/select',
  3498. '/div',
  3499. ];
  3500. $this->assertHtml($expected, $result);
  3501. }
  3502. /**
  3503. * testInvalidControlTypeOption method
  3504. *
  3505. * Test invalid 'input' type option to control() function.
  3506. */
  3507. public function testInvalidControlTypeOption(): void
  3508. {
  3509. $this->expectException(RuntimeException::class);
  3510. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3511. $this->Form->control('text', ['type' => 'input']);
  3512. }
  3513. /**
  3514. * testControlMagicSelectChangeToRadio method
  3515. *
  3516. * Test that magic control() selects can easily be converted into radio types without error.
  3517. */
  3518. public function testControlMagicSelectChangeToRadio(): void
  3519. {
  3520. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3521. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3522. $this->assertStringContainsString('input type="radio"', $result);
  3523. }
  3524. /**
  3525. * testFormControlSubmit method
  3526. *
  3527. * Test correct results for form::control() and type submit.
  3528. */
  3529. public function testFormControlSubmit(): void
  3530. {
  3531. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3532. $expected = [
  3533. 'div' => ['class' => 'submit'],
  3534. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3535. '/div',
  3536. ];
  3537. $this->assertHtml($expected, $result);
  3538. }
  3539. /**
  3540. * testFormControls method
  3541. *
  3542. * Test correct results from Form::controls().
  3543. */
  3544. public function testFormControlsLegendFieldset(): void
  3545. {
  3546. $this->Form->create($this->article);
  3547. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3548. $expected = [
  3549. '<fieldset',
  3550. '<legend',
  3551. 'The Legend',
  3552. '/legend',
  3553. '*/fieldset',
  3554. ];
  3555. $this->assertHtml($expected, $result);
  3556. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3557. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3558. $this->assertStringContainsString('<fieldset>', $result);
  3559. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3560. $this->assertStringNotContainsString('<legend>', $result);
  3561. $this->assertStringNotContainsString('<fieldset>', $result);
  3562. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3563. $this->assertStringNotContainsString('<legend>', $result);
  3564. $this->assertStringNotContainsString('<fieldset>', $result);
  3565. $this->Form->create($this->article);
  3566. $this->View->setRequest($this->View->getRequest()
  3567. ->withParam('prefix', 'admin')
  3568. ->withParam('action', 'admin_edit')
  3569. ->withParam('controller', 'articles'));
  3570. $result = $this->Form->allControls();
  3571. $expected = [
  3572. '<fieldset',
  3573. '<legend',
  3574. 'New Article',
  3575. '/legend',
  3576. '*/fieldset',
  3577. ];
  3578. $this->assertHtml($expected, $result);
  3579. $this->Form->create($this->article);
  3580. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3581. $expected = [
  3582. '<fieldset',
  3583. '<legend',
  3584. 'The Legend',
  3585. '/legend',
  3586. '*/fieldset',
  3587. ];
  3588. $this->assertHtml($expected, $result);
  3589. $this->Form->create($this->article);
  3590. $result = $this->Form->allControls([], [
  3591. 'fieldset' => [
  3592. 'class' => 'some-class some-other-class',
  3593. 'disabled' => true,
  3594. 'data-param' => 'a-param',
  3595. ],
  3596. 'legend' => 'The Legend',
  3597. ]);
  3598. $expected = [
  3599. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3600. '<legend',
  3601. 'The Legend',
  3602. '/legend',
  3603. '*/fieldset',
  3604. ];
  3605. $this->assertHtml($expected, $result);
  3606. }
  3607. /**
  3608. * testFormControls method
  3609. *
  3610. * Test the controls() method.
  3611. */
  3612. public function testFormControls(): void
  3613. {
  3614. $this->Form->create($this->article);
  3615. $result = $this->Form->allControls();
  3616. $expected = [
  3617. '<fieldset',
  3618. '<legend', 'New Article', '/legend',
  3619. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3620. ['div' => ['class' => 'input select required']],
  3621. '*/div',
  3622. ['div' => ['class' => 'input text required']],
  3623. '*/div',
  3624. ['div' => ['class' => 'input text']],
  3625. '*/div',
  3626. ['div' => ['class' => 'input text']],
  3627. '*/div',
  3628. '/fieldset',
  3629. ];
  3630. $this->assertHtml($expected, $result);
  3631. $result = $this->Form->allControls([
  3632. 'published' => ['type' => 'boolean'],
  3633. ]);
  3634. $expected = [
  3635. '<fieldset',
  3636. '<legend', 'New Article', '/legend',
  3637. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3638. ['div' => ['class' => 'input select required']],
  3639. '*/div',
  3640. ['div' => ['class' => 'input text required']],
  3641. '*/div',
  3642. ['div' => ['class' => 'input text']],
  3643. '*/div',
  3644. ['div' => ['class' => 'input boolean']],
  3645. '*/div',
  3646. '/fieldset',
  3647. ];
  3648. $this->assertHtml($expected, $result);
  3649. $this->Form->create($this->article);
  3650. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3651. $expected = [
  3652. 'fieldset' => [],
  3653. 'legend' => [],
  3654. 'Hello',
  3655. '/legend',
  3656. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3657. ['div' => ['class' => 'input select required']],
  3658. '*/div',
  3659. ['div' => ['class' => 'input text required']],
  3660. '*/div',
  3661. ['div' => ['class' => 'input text']],
  3662. '*/div',
  3663. ['div' => ['class' => 'input text']],
  3664. '*/div',
  3665. '/fieldset',
  3666. ];
  3667. $this->assertHtml($expected, $result);
  3668. $this->Form->create();
  3669. $expected = [
  3670. 'fieldset' => [],
  3671. ['div' => ['class' => 'input text']],
  3672. 'label' => ['for' => 'foo'],
  3673. 'Foo',
  3674. '/label',
  3675. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3676. '*/div',
  3677. '/fieldset',
  3678. ];
  3679. $result = $this->Form->allControls(
  3680. ['foo' => ['type' => 'text']],
  3681. ['legend' => false]
  3682. );
  3683. $this->assertHtml($expected, $result);
  3684. }
  3685. /**
  3686. * testFormControlsBlacklist method
  3687. */
  3688. public function testFormControlsBlacklist(): void
  3689. {
  3690. $this->Form->create($this->article);
  3691. $result = $this->Form->allControls([
  3692. 'id' => false,
  3693. ]);
  3694. $expected = [
  3695. '<fieldset',
  3696. '<legend', 'New Article', '/legend',
  3697. ['div' => ['class' => 'input select required']],
  3698. '*/div',
  3699. ['div' => ['class' => 'input text required']],
  3700. '*/div',
  3701. ['div' => ['class' => 'input text']],
  3702. '*/div',
  3703. ['div' => ['class' => 'input text']],
  3704. '*/div',
  3705. '/fieldset',
  3706. ];
  3707. $this->assertHtml($expected, $result);
  3708. $this->Form->create($this->article);
  3709. $result = $this->Form->allControls([
  3710. 'id' => [],
  3711. ]);
  3712. $expected = [
  3713. '<fieldset',
  3714. '<legend', 'New Article', '/legend',
  3715. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3716. ['div' => ['class' => 'input select required']],
  3717. '*/div',
  3718. ['div' => ['class' => 'input text required']],
  3719. '*/div',
  3720. ['div' => ['class' => 'input text']],
  3721. '*/div',
  3722. ['div' => ['class' => 'input text']],
  3723. '*/div',
  3724. '/fieldset',
  3725. ];
  3726. $this->assertHtml($expected, $result, true);
  3727. }
  3728. /**
  3729. * testSelectAsCheckbox method
  3730. *
  3731. * Test multi-select widget with checkbox formatting.
  3732. */
  3733. public function testSelectAsCheckbox(): void
  3734. {
  3735. $result = $this->Form->select(
  3736. 'Model.multi_field',
  3737. ['first', 'second', 'third'],
  3738. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3739. );
  3740. $expected = [
  3741. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3742. ['div' => ['class' => 'checkbox']],
  3743. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3744. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3745. 'first',
  3746. '/label',
  3747. '/div',
  3748. ['div' => ['class' => 'checkbox']],
  3749. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3750. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3751. 'second',
  3752. '/label',
  3753. '/div',
  3754. ['div' => ['class' => 'checkbox']],
  3755. ['label' => ['for' => 'model-multi-field-2']],
  3756. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3757. 'third',
  3758. '/label',
  3759. '/div',
  3760. ];
  3761. $this->assertHtml($expected, $result);
  3762. $result = $this->Form->select(
  3763. 'Model.multi_field',
  3764. ['1/2' => 'half'],
  3765. ['multiple' => 'checkbox']
  3766. );
  3767. $expected = [
  3768. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  3769. ['div' => ['class' => 'checkbox']],
  3770. ['label' => ['for' => 'model-multi-field-1-2']],
  3771. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3772. 'half',
  3773. '/label',
  3774. '/div',
  3775. ];
  3776. $this->assertHtml($expected, $result);
  3777. }
  3778. /**
  3779. * testLabel method
  3780. *
  3781. * Test label generation.
  3782. */
  3783. public function testLabel(): void
  3784. {
  3785. $result = $this->Form->label('Person.name');
  3786. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3787. $this->assertHtml($expected, $result);
  3788. $result = $this->Form->label('Person.name');
  3789. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3790. $this->assertHtml($expected, $result);
  3791. $result = $this->Form->label('Person.first_name');
  3792. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3793. $this->assertHtml($expected, $result);
  3794. $result = $this->Form->label('Person.first_name', 'Your first name');
  3795. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3796. $this->assertHtml($expected, $result);
  3797. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3798. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3799. $this->assertHtml($expected, $result);
  3800. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3801. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3802. $this->assertHtml($expected, $result);
  3803. $result = $this->Form->label('Person.first_name', '');
  3804. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3805. $this->assertHtml($expected, $result);
  3806. $result = $this->Form->label('Person.2.name', '');
  3807. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3808. $this->assertHtml($expected, $result);
  3809. }
  3810. /**
  3811. * testLabelContainControl method
  3812. *
  3813. * Test that label() can accept an input with the correct template vars.
  3814. */
  3815. public function testLabelContainControl(): void
  3816. {
  3817. $this->Form->setTemplates([
  3818. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3819. ]);
  3820. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3821. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3822. ]);
  3823. $expected = [
  3824. 'label' => ['for' => 'person-accept-terms'],
  3825. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3826. 'Accept',
  3827. '/label',
  3828. ];
  3829. $this->assertHtml($expected, $result);
  3830. }
  3831. /**
  3832. * testTextbox method
  3833. *
  3834. * Test textbox element generation.
  3835. */
  3836. public function testTextbox(): void
  3837. {
  3838. $result = $this->Form->text('Model.field');
  3839. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3840. $this->assertHtml($expected, $result);
  3841. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3842. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3843. $this->assertHtml($expected, $result);
  3844. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3845. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3846. $this->assertHtml($expected, $result);
  3847. }
  3848. /**
  3849. * testTextBoxDataAndError method
  3850. *
  3851. * Test that text() hooks up with request data and error fields.
  3852. */
  3853. public function testTextBoxDataAndError(): void
  3854. {
  3855. $this->article['errors'] = [
  3856. 'Contact' => ['text' => 'wrong'],
  3857. ];
  3858. $this->View->setRequest($this->View->getRequest()
  3859. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3860. ->withData('Contact.text', 'test'));
  3861. $this->Form->create($this->article);
  3862. $result = $this->Form->text('Model.text');
  3863. $expected = [
  3864. 'input' => [
  3865. 'type' => 'text',
  3866. 'name' => 'Model[text]',
  3867. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3868. ],
  3869. ];
  3870. $this->assertHtml($expected, $result);
  3871. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3872. $expected = [
  3873. 'input' => [
  3874. 'type' => 'text',
  3875. 'name' => 'Contact[text]',
  3876. 'value' => 'test',
  3877. 'id' => 'theID',
  3878. 'class' => 'form-error',
  3879. ],
  3880. ];
  3881. $this->assertHtml($expected, $result);
  3882. }
  3883. /**
  3884. * testDefaultValue method
  3885. *
  3886. * Test default value setting.
  3887. */
  3888. public function testTextDefaultValue(): void
  3889. {
  3890. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3891. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3892. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3893. $this->assertHtml($expected, $result);
  3894. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3895. $this->Form->create();
  3896. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3897. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3898. $this->assertHtml($expected, $result);
  3899. $Articles = $this->getTableLocator()->get('Articles');
  3900. $title = $Articles->getSchema()->getColumn('title');
  3901. $Articles->getSchema()->addColumn(
  3902. 'title',
  3903. ['default' => 'default title', 'length' => 255] + $title
  3904. );
  3905. $entity = $Articles->newEmptyEntity();
  3906. $this->Form->create($entity);
  3907. // Get default value from schema
  3908. $result = $this->Form->text('title');
  3909. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3910. $this->assertHtml($expected, $result);
  3911. // Don't get value from schema
  3912. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3913. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3914. $this->assertHtml($expected, $result);
  3915. // Custom default value overrides default value from schema
  3916. $result = $this->Form->text('title', ['default' => 'override default']);
  3917. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  3918. $this->assertHtml($expected, $result);
  3919. // Default value from schema is used only for new entities.
  3920. $entity->setNew(false);
  3921. $result = $this->Form->text('title');
  3922. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3923. $this->assertHtml($expected, $result);
  3924. }
  3925. /**
  3926. * testError method
  3927. *
  3928. * Test field error generation.
  3929. */
  3930. public function testError(): void
  3931. {
  3932. $this->article['errors'] = [
  3933. 'Article' => ['field' => 'email'],
  3934. ];
  3935. $this->Form->create($this->article);
  3936. $result = $this->Form->error('Article.field');
  3937. $expected = [
  3938. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3939. 'email',
  3940. '/div',
  3941. ];
  3942. $this->assertHtml($expected, $result);
  3943. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  3944. $expected = [
  3945. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3946. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3947. '/div',
  3948. ];
  3949. $this->assertHtml($expected, $result);
  3950. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  3951. $expected = [
  3952. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3953. '<strong', 'Badness!', '/strong',
  3954. '/div',
  3955. ];
  3956. $this->assertHtml($expected, $result);
  3957. }
  3958. /**
  3959. * testErrorRuleName method
  3960. *
  3961. * Test error translation can use rule names for translating.
  3962. */
  3963. public function testErrorRuleName(): void
  3964. {
  3965. $this->article['errors'] = [
  3966. 'Article' => [
  3967. 'field' => ['email' => 'Your email was not good'],
  3968. ],
  3969. ];
  3970. $this->Form->create($this->article);
  3971. $result = $this->Form->error('Article.field');
  3972. $expected = [
  3973. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3974. 'Your email was not good',
  3975. '/div',
  3976. ];
  3977. $this->assertHtml($expected, $result);
  3978. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  3979. $expected = [
  3980. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3981. 'Email in use',
  3982. '/div',
  3983. ];
  3984. $this->assertHtml($expected, $result);
  3985. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  3986. $expected = [
  3987. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3988. 'Email in use',
  3989. '/div',
  3990. ];
  3991. $this->assertHtml($expected, $result);
  3992. $result = $this->Form->error('Article.field', [
  3993. 'email' => 'Key is preferred',
  3994. 'Your email was not good' => 'Email in use',
  3995. ]);
  3996. $expected = [
  3997. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3998. 'Key is preferred',
  3999. '/div',
  4000. ];
  4001. $this->assertHtml($expected, $result);
  4002. }
  4003. /**
  4004. * testErrorMessages method
  4005. *
  4006. * Test error with nested lists.
  4007. */
  4008. public function testErrorMessages(): void
  4009. {
  4010. $this->article['errors'] = [
  4011. 'Article' => ['field' => 'email'],
  4012. ];
  4013. $this->Form->create($this->article);
  4014. $result = $this->Form->error('Article.field', [
  4015. 'email' => 'No good!',
  4016. ]);
  4017. $expected = [
  4018. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  4019. 'No good!',
  4020. '/div',
  4021. ];
  4022. $this->assertHtml($expected, $result);
  4023. }
  4024. /**
  4025. * testErrorMultipleMessages method
  4026. *
  4027. * Test error() with multiple messages.
  4028. */
  4029. public function testErrorMultipleMessages(): void
  4030. {
  4031. $this->article['errors'] = [
  4032. 'field' => ['notBlank', 'email', 'Something else'],
  4033. ];
  4034. $this->Form->create($this->article);
  4035. $result = $this->Form->error('field', [
  4036. 'notBlank' => 'Cannot be empty',
  4037. 'email' => 'No good!',
  4038. ]);
  4039. $expected = [
  4040. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  4041. 'ul' => [],
  4042. '<li', 'Cannot be empty', '/li',
  4043. '<li', 'No good!', '/li',
  4044. '<li', 'Something else', '/li',
  4045. '/ul',
  4046. '/div',
  4047. ];
  4048. $this->assertHtml($expected, $result);
  4049. }
  4050. /**
  4051. * testPassword method
  4052. *
  4053. * Test password element generation.
  4054. */
  4055. public function testPassword(): void
  4056. {
  4057. $this->article['errors'] = [
  4058. 'Contact' => [
  4059. 'passwd' => 1,
  4060. ],
  4061. ];
  4062. $this->Form->create($this->article);
  4063. $result = $this->Form->password('Contact.field');
  4064. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4065. $this->assertHtml($expected, $result);
  4066. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4067. $this->Form->create($this->article);
  4068. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4069. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4070. $this->assertHtml($expected, $result);
  4071. }
  4072. /**
  4073. * testRadio method
  4074. *
  4075. * Test radio element set generation.
  4076. */
  4077. public function testRadio(): void
  4078. {
  4079. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4080. $expected = [
  4081. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4082. ['label' => ['for' => 'model-field-0']],
  4083. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4084. 'option A',
  4085. '/label',
  4086. ['label' => ['for' => 'model-field-1']],
  4087. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4088. 'option B',
  4089. '/label',
  4090. ];
  4091. $this->assertHtml($expected, $result);
  4092. $result = $this->Form->radio('Model.field', new Collection(['option A', 'option B']));
  4093. $this->assertHtml($expected, $result);
  4094. $result = $this->Form->radio(
  4095. 'Employee.vegetarian',
  4096. ['yes' => 'Yes', 'no' => 'No'],
  4097. ['form' => 'my-form', 'id' => 'id-veg']
  4098. );
  4099. $expected = [
  4100. 'input' => ['type' => 'hidden', 'name' => 'Employee[vegetarian]', 'value' => '', 'form' => 'my-form', 'id' => 'id-veg'],
  4101. ['label' => ['for' => 'id-veg-yes']],
  4102. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'yes', 'id' => 'id-veg-yes', 'form' => 'my-form']],
  4103. 'Yes',
  4104. '/label',
  4105. ['label' => ['for' => 'id-veg-no']],
  4106. ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'no', 'id' => 'id-veg-no', 'form' => 'my-form']],
  4107. 'No',
  4108. '/label',
  4109. ];
  4110. $this->assertHtml($expected, $result);
  4111. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4112. $expected = [
  4113. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'model-field']],
  4114. ['label' => ['for' => 'model-custom-0']],
  4115. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4116. 'option A',
  4117. '/label',
  4118. ['label' => ['for' => 'model-custom-1']],
  4119. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4120. 'option B',
  4121. '/label',
  4122. ];
  4123. $this->assertHtml($expected, $result);
  4124. $result = $this->Form->radio(
  4125. 'Employee.gender',
  4126. [
  4127. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4128. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4129. ]
  4130. );
  4131. $expected = [
  4132. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'employee-gender'],
  4133. ['label' => ['for' => 'employee-gender-male']],
  4134. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4135. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4136. 'Male',
  4137. '/label',
  4138. ['label' => ['for' => 'employee-gender-female']],
  4139. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4140. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4141. 'Female',
  4142. '/label',
  4143. ];
  4144. $this->assertHtml($expected, $result);
  4145. }
  4146. /**
  4147. * Test radio with complex options and empty disabled data.
  4148. */
  4149. public function testRadioComplexDisabled(): void
  4150. {
  4151. $options = [
  4152. ['value' => 'r', 'text' => 'red'],
  4153. ['value' => 'b', 'text' => 'blue'],
  4154. ];
  4155. $attrs = ['disabled' => []];
  4156. $result = $this->Form->radio('Model.field', $options, $attrs);
  4157. $expected = [
  4158. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4159. ['label' => ['for' => 'model-field-r']],
  4160. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4161. 'red',
  4162. '/label',
  4163. ['label' => ['for' => 'model-field-b']],
  4164. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4165. 'blue',
  4166. '/label',
  4167. ];
  4168. $this->assertHtml($expected, $result);
  4169. $attrs = ['disabled' => ['r']];
  4170. $result = $this->Form->radio('Model.field', $options, $attrs);
  4171. $this->assertStringContainsString('disabled="disabled"', $result);
  4172. }
  4173. /**
  4174. * testRadioDefaultValue method
  4175. *
  4176. * Test default value setting on radio() method.
  4177. */
  4178. public function testRadioDefaultValue(): void
  4179. {
  4180. $Articles = $this->getTableLocator()->get('Articles');
  4181. $title = $Articles->getSchema()->getColumn('title');
  4182. $Articles->getSchema()->addColumn(
  4183. 'title',
  4184. ['default' => '1'] + $title
  4185. );
  4186. $this->Form->create($Articles->newEmptyEntity());
  4187. $result = $this->Form->radio('title', ['option A', 'option B']);
  4188. $expected = [
  4189. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4190. ['label' => ['for' => 'title-0']],
  4191. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4192. 'option A',
  4193. '/label',
  4194. ['label' => ['for' => 'title-1']],
  4195. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4196. 'option B',
  4197. '/label',
  4198. ];
  4199. $this->assertHtml($expected, $result);
  4200. }
  4201. /**
  4202. * Test setting a hiddenField value on radio buttons.
  4203. */
  4204. public function testRadioHiddenFieldValue(): void
  4205. {
  4206. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4207. $expected = [
  4208. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N', 'id' => 'title']],
  4209. 'label' => ['for' => 'title-0'],
  4210. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4211. 'option A',
  4212. '/label',
  4213. ];
  4214. $this->assertHtml($expected, $result);
  4215. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
  4216. $expected = [
  4217. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
  4218. 'label' => ['for' => 'title-0'],
  4219. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4220. 'option A',
  4221. '/label',
  4222. ];
  4223. $this->assertHtml($expected, $result);
  4224. }
  4225. /**
  4226. * testControlRadio method
  4227. *
  4228. * Test that input works with radio types.
  4229. */
  4230. public function testControlRadio(): void
  4231. {
  4232. $result = $this->Form->control('test', [
  4233. 'type' => 'radio',
  4234. 'options' => ['A', 'B'],
  4235. ]);
  4236. $expected = [
  4237. ['div' => ['class' => 'input radio']],
  4238. '<label',
  4239. 'Test',
  4240. '/label',
  4241. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4242. ['label' => ['for' => 'test-0']],
  4243. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4244. 'A',
  4245. '/label',
  4246. ['label' => ['for' => 'test-1']],
  4247. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4248. 'B',
  4249. '/label',
  4250. '/div',
  4251. ];
  4252. $this->assertHtml($expected, $result);
  4253. $result = $this->Form->control('test', [
  4254. 'type' => 'radio',
  4255. 'options' => ['A', 'B'],
  4256. 'value' => '0',
  4257. ]);
  4258. $expected = [
  4259. ['div' => ['class' => 'input radio']],
  4260. '<label',
  4261. 'Test',
  4262. '/label',
  4263. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4264. ['label' => ['for' => 'test-0']],
  4265. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4266. 'A',
  4267. '/label',
  4268. ['label' => ['for' => 'test-1']],
  4269. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4270. 'B',
  4271. '/label',
  4272. '/div',
  4273. ];
  4274. $this->assertHtml($expected, $result);
  4275. $result = $this->Form->control('test', [
  4276. 'type' => 'radio',
  4277. 'options' => ['A', 'B'],
  4278. 'label' => false,
  4279. ]);
  4280. $expected = [
  4281. ['div' => ['class' => 'input radio']],
  4282. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  4283. ['label' => ['for' => 'test-0']],
  4284. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4285. 'A',
  4286. '/label',
  4287. ['label' => ['for' => 'test-1']],
  4288. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4289. 'B',
  4290. '/label',
  4291. '/div',
  4292. ];
  4293. $this->assertHtml($expected, $result);
  4294. $result = $this->Form->control('accept', [
  4295. 'type' => 'radio',
  4296. 'options' => [
  4297. 1 => 'positive',
  4298. -1 => 'negative',
  4299. ],
  4300. 'label' => false,
  4301. ]);
  4302. $expected = [
  4303. ['div' => ['class' => 'input radio']],
  4304. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '', 'id' => 'accept']],
  4305. ['label' => ['for' => 'accept-1']],
  4306. ['input' => [
  4307. 'type' => 'radio',
  4308. 'name' => 'accept',
  4309. 'value' => '1',
  4310. 'id' => 'accept-1',
  4311. ]],
  4312. 'positive',
  4313. '/label',
  4314. ['label' => ['for' => 'accept--1']],
  4315. ['input' => [
  4316. 'type' => 'radio',
  4317. 'name' => 'accept',
  4318. 'value' => '-1',
  4319. 'id' => 'accept--1',
  4320. ]],
  4321. 'negative',
  4322. '/label',
  4323. '/div',
  4324. ];
  4325. $this->assertHtml($expected, $result);
  4326. }
  4327. /**
  4328. * testRadioNoLabel method
  4329. *
  4330. * Test that radio() works with label = false.
  4331. */
  4332. public function testRadioNoLabel(): void
  4333. {
  4334. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4335. $expected = [
  4336. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4337. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4338. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4339. ];
  4340. $this->assertHtml($expected, $result);
  4341. }
  4342. /**
  4343. * testRadioControlInsideLabel method
  4344. *
  4345. * Test generating radio input inside label ala twitter bootstrap.
  4346. */
  4347. public function testRadioControlInsideLabel(): void
  4348. {
  4349. $this->Form->setTemplates([
  4350. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4351. 'radioWrapper' => '{{label}}',
  4352. ]);
  4353. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4354. // phpcs:disable
  4355. $expected = [
  4356. ['input' => [
  4357. 'type' => 'hidden',
  4358. 'name' => 'Model[field]',
  4359. 'value' => '',
  4360. 'id' => 'model-field'
  4361. ]],
  4362. ['label' => ['for' => 'model-field-0']],
  4363. ['input' => [
  4364. 'type' => 'radio',
  4365. 'name' => 'Model[field]',
  4366. 'value' => '0',
  4367. 'id' => 'model-field-0'
  4368. ]],
  4369. 'option A',
  4370. '/label',
  4371. ['label' => ['for' => 'model-field-1']],
  4372. ['input' => [
  4373. 'type' => 'radio',
  4374. 'name' => 'Model[field]',
  4375. 'value' => '1',
  4376. 'id' => 'model-field-1'
  4377. ]],
  4378. 'option B',
  4379. '/label',
  4380. ];
  4381. // phpcs:enable
  4382. $this->assertHtml($expected, $result);
  4383. }
  4384. /**
  4385. * testRadioHiddenControlDisabling method
  4386. *
  4387. * Test disabling the hidden input for radio buttons.
  4388. */
  4389. public function testRadioHiddenControlDisabling(): void
  4390. {
  4391. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4392. $expected = [
  4393. 'label' => ['for' => 'model-1-field-0'],
  4394. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4395. 'option A',
  4396. '/label',
  4397. ];
  4398. $this->assertHtml($expected, $result);
  4399. }
  4400. /**
  4401. * testRadioOutOfRange method
  4402. *
  4403. * Test radio element set generation.
  4404. */
  4405. public function testRadioOutOfRange(): void
  4406. {
  4407. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4408. $expected = [
  4409. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
  4410. 'label' => ['for' => 'model-field-v'],
  4411. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4412. 'value',
  4413. '/label',
  4414. ];
  4415. $this->assertHtml($expected, $result);
  4416. }
  4417. /**
  4418. * testSelect method
  4419. *
  4420. * Test select element generation.
  4421. */
  4422. public function testSelect(): void
  4423. {
  4424. $result = $this->Form->select('Model.field', []);
  4425. $expected = [
  4426. 'select' => ['name' => 'Model[field]'],
  4427. '/select',
  4428. ];
  4429. $this->assertHtml($expected, $result);
  4430. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4431. $this->Form->create();
  4432. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4433. $expected = [
  4434. 'select' => ['name' => 'Model[field]'],
  4435. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4436. 'good',
  4437. '/option',
  4438. ['option' => ['value' => 'other']],
  4439. 'bad',
  4440. '/option',
  4441. '/select',
  4442. ];
  4443. $this->assertHtml($expected, $result);
  4444. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4445. $this->assertHtml($expected, $result);
  4446. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4447. $this->Form->create();
  4448. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4449. $expected = [
  4450. 'select' => ['name' => 'Model[field]'],
  4451. ['option' => ['value' => 'value']],
  4452. 'good',
  4453. '/option',
  4454. ['option' => ['value' => 'other']],
  4455. 'bad',
  4456. '/option',
  4457. '/select',
  4458. ];
  4459. $this->assertHtml($expected, $result);
  4460. $options = [
  4461. ['value' => 'first', 'text' => 'First'],
  4462. ['value' => 'first', 'text' => 'Another First'],
  4463. ];
  4464. $result = $this->Form->select(
  4465. 'Model.field',
  4466. $options,
  4467. ['escape' => false, 'empty' => false]
  4468. );
  4469. $expected = [
  4470. 'select' => ['name' => 'Model[field]'],
  4471. ['option' => ['value' => 'first']],
  4472. 'First',
  4473. '/option',
  4474. ['option' => ['value' => 'first']],
  4475. 'Another First',
  4476. '/option',
  4477. '/select',
  4478. ];
  4479. $this->assertHtml($expected, $result);
  4480. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4481. $this->Form->create();
  4482. $result = $this->Form->select(
  4483. 'Model.contact_id',
  4484. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4485. ['escape' => false, 'empty' => 'pick something']
  4486. );
  4487. $expected = [
  4488. 'select' => ['name' => 'Model[contact_id]'],
  4489. ['option' => ['value' => '']], 'pick something', '/option',
  4490. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4491. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4492. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4493. '/select',
  4494. ];
  4495. $this->assertHtml($expected, $result);
  4496. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4497. $this->Form->create();
  4498. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4499. $expected = [
  4500. 'select' => ['name' => 'Model[field]'],
  4501. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4502. ['option' => ['value' => '1']], 'Yes', '/option',
  4503. '/select',
  4504. ];
  4505. $this->assertHtml($expected, $result);
  4506. }
  4507. /**
  4508. * testSelectEscapeHtml method
  4509. *
  4510. * Test that select() escapes HTML.
  4511. */
  4512. public function testSelectEscapeHtml(): void
  4513. {
  4514. $result = $this->Form->select(
  4515. 'Model.field',
  4516. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4517. ['empty' => false]
  4518. );
  4519. $expected = [
  4520. 'select' => ['name' => 'Model[field]'],
  4521. ['option' => ['value' => 'first']],
  4522. 'first &quot;html&quot; &lt;chars&gt;',
  4523. '/option',
  4524. ['option' => ['value' => 'second']],
  4525. 'value',
  4526. '/option',
  4527. '/select',
  4528. ];
  4529. $this->assertHtml($expected, $result);
  4530. $result = $this->Form->select(
  4531. 'Model.field',
  4532. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4533. ['escape' => false, 'empty' => false]
  4534. );
  4535. $expected = [
  4536. 'select' => ['name' => 'Model[field]'],
  4537. ['option' => ['value' => 'first']],
  4538. 'first "html" <chars>',
  4539. '/option',
  4540. ['option' => ['value' => 'second']],
  4541. 'value',
  4542. '/option',
  4543. '/select',
  4544. ];
  4545. $this->assertHtml($expected, $result);
  4546. }
  4547. /**
  4548. * testSelectRequired method
  4549. *
  4550. * Test select() with required and disabled attributes.
  4551. */
  4552. public function testSelectRequired(): void
  4553. {
  4554. $this->article['required'] = [
  4555. 'user_id' => true,
  4556. ];
  4557. $this->Form->create($this->article);
  4558. $result = $this->Form->select('user_id', ['option A']);
  4559. $expected = [
  4560. 'select' => [
  4561. 'name' => 'user_id',
  4562. 'required' => 'required',
  4563. ],
  4564. ['option' => ['value' => '0']], 'option A', '/option',
  4565. '/select',
  4566. ];
  4567. $this->assertHtml($expected, $result);
  4568. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4569. $expected = [
  4570. 'select' => [
  4571. 'name' => 'user_id',
  4572. 'disabled' => 'disabled',
  4573. ],
  4574. ['option' => ['value' => '0']], 'option A', '/option',
  4575. '/select',
  4576. ];
  4577. $this->assertHtml($expected, $result);
  4578. }
  4579. public function testSelectEmptyWithRequiredFalse(): void
  4580. {
  4581. $Articles = $this->getTableLocator()->get('Articles');
  4582. $validator = $Articles->getValidator('default');
  4583. $validator->allowEmptyString('user_id');
  4584. $Articles->setValidator('default', $validator);
  4585. $entity = $Articles->newEmptyEntity();
  4586. $this->Form->create($entity);
  4587. $result = $this->Form->select('user_id', ['option A']);
  4588. $expected = [
  4589. 'select' => [
  4590. 'name' => 'user_id',
  4591. ],
  4592. ['option' => ['value' => '']], '/option',
  4593. ['option' => ['value' => '0']], 'option A', '/option',
  4594. '/select',
  4595. ];
  4596. $this->assertHtml($expected, $result);
  4597. }
  4598. /**
  4599. * testNestedSelect method
  4600. *
  4601. * Test select element generation with optgroups.
  4602. */
  4603. public function testNestedSelect(): void
  4604. {
  4605. $result = $this->Form->select(
  4606. 'Model.field',
  4607. [1 => 'One', 2 => 'Two', 'Three' => [
  4608. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4609. ]],
  4610. ['empty' => false]
  4611. );
  4612. $expected = [
  4613. 'select' => ['name' => 'Model[field]'],
  4614. ['option' => ['value' => 1]],
  4615. 'One',
  4616. '/option',
  4617. ['option' => ['value' => 2]],
  4618. 'Two',
  4619. '/option',
  4620. ['optgroup' => ['label' => 'Three']],
  4621. ['option' => ['value' => 3]],
  4622. 'Three',
  4623. '/option',
  4624. ['option' => ['value' => 4]],
  4625. 'Four',
  4626. '/option',
  4627. ['option' => ['value' => 5]],
  4628. 'Five',
  4629. '/option',
  4630. '/optgroup',
  4631. '/select',
  4632. ];
  4633. $this->assertHtml($expected, $result);
  4634. }
  4635. /**
  4636. * testSelectMultiple method
  4637. *
  4638. * Test generation of multiple select elements.
  4639. */
  4640. public function testSelectMultiple(): void
  4641. {
  4642. $options = ['first', 'second', 'third'];
  4643. $result = $this->Form->select(
  4644. 'Model.multi_field',
  4645. $options,
  4646. ['form' => 'my-form', 'multiple' => true]
  4647. );
  4648. $expected = [
  4649. 'input' => [
  4650. 'type' => 'hidden',
  4651. 'name' => 'Model[multi_field]',
  4652. 'value' => '',
  4653. 'form' => 'my-form',
  4654. ],
  4655. 'select' => [
  4656. 'name' => 'Model[multi_field][]',
  4657. 'multiple' => 'multiple',
  4658. 'form' => 'my-form',
  4659. ],
  4660. ['option' => ['value' => '0']],
  4661. 'first',
  4662. '/option',
  4663. ['option' => ['value' => '1']],
  4664. 'second',
  4665. '/option',
  4666. ['option' => ['value' => '2']],
  4667. 'third',
  4668. '/option',
  4669. '/select',
  4670. ];
  4671. $this->assertHtml($expected, $result);
  4672. $result = $this->Form->select(
  4673. 'Model.multi_field',
  4674. $options,
  4675. ['multiple' => 'multiple', 'form' => 'my-form']
  4676. );
  4677. $this->assertHtml($expected, $result);
  4678. $result = $this->Form->select(
  4679. 'Model.multi_field',
  4680. $options,
  4681. ['form' => 'my-form', 'multiple' => false]
  4682. );
  4683. $this->assertStringNotContainsString('multiple', $result);
  4684. }
  4685. /**
  4686. * testCheckboxZeroValue method
  4687. *
  4688. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4689. */
  4690. public function testCheckboxZeroValue(): void
  4691. {
  4692. $result = $this->Form->control('User.get_spam', [
  4693. 'type' => 'checkbox',
  4694. 'value' => '0',
  4695. 'hiddenField' => '1',
  4696. ]);
  4697. $expected = [
  4698. 'div' => ['class' => 'input checkbox'],
  4699. 'label' => ['for' => 'user-get-spam'],
  4700. ['input' => [
  4701. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4702. 'value' => '1',
  4703. ]],
  4704. ['input' => [
  4705. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4706. 'value' => '0', 'id' => 'user-get-spam',
  4707. ]],
  4708. 'Get Spam',
  4709. '/label',
  4710. '/div',
  4711. ];
  4712. $this->assertHtml($expected, $result);
  4713. $result = $this->Form->control('User.get_spam', [
  4714. 'type' => 'checkbox',
  4715. 'value' => '0',
  4716. 'hiddenField' => '',
  4717. ]);
  4718. $expected = [
  4719. 'div' => ['class' => 'input checkbox'],
  4720. 'label' => ['for' => 'user-get-spam'],
  4721. ['input' => [
  4722. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4723. 'value' => '',
  4724. ]],
  4725. ['input' => [
  4726. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4727. 'value' => '0', 'id' => 'user-get-spam',
  4728. ]],
  4729. 'Get Spam',
  4730. '/label',
  4731. '/div',
  4732. ];
  4733. $this->assertHtml($expected, $result);
  4734. }
  4735. /**
  4736. * testHabtmSelectBox method
  4737. *
  4738. * Test generation of habtm select boxes.
  4739. */
  4740. public function testHabtmSelectBox(): void
  4741. {
  4742. $options = [
  4743. 1 => 'blue',
  4744. 2 => 'red',
  4745. 3 => 'green',
  4746. ];
  4747. $tags = [
  4748. new Entity(['id' => 1, 'name' => 'blue']),
  4749. new Entity(['id' => 3, 'name' => 'green']),
  4750. ];
  4751. $article = new Article(['tags' => $tags]);
  4752. $this->Form->create($article);
  4753. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4754. $expected = [
  4755. 'div' => ['class' => 'input select'],
  4756. 'label' => ['for' => 'tags-ids'],
  4757. 'Tags',
  4758. '/label',
  4759. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4760. 'select' => [
  4761. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4762. 'multiple' => 'multiple',
  4763. ],
  4764. ['option' => ['value' => '1', 'selected' => 'selected']],
  4765. 'blue',
  4766. '/option',
  4767. ['option' => ['value' => '2']],
  4768. 'red',
  4769. '/option',
  4770. ['option' => ['value' => '3', 'selected' => 'selected']],
  4771. 'green',
  4772. '/option',
  4773. '/select',
  4774. '/div',
  4775. ];
  4776. $this->assertHtml($expected, $result);
  4777. // make sure only 50 is selected, and not 50f5c0cf
  4778. $options = [
  4779. '1' => 'blue',
  4780. '50f5c0cf' => 'red',
  4781. '50' => 'green',
  4782. ];
  4783. $tags = [
  4784. new Entity(['id' => 1, 'name' => 'blue']),
  4785. new Entity(['id' => 50, 'name' => 'green']),
  4786. ];
  4787. $article = new Article(['tags' => $tags]);
  4788. $this->Form->create($article);
  4789. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4790. $expected = [
  4791. 'div' => ['class' => 'input select'],
  4792. 'label' => ['for' => 'tags-ids'],
  4793. 'Tags',
  4794. '/label',
  4795. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4796. 'select' => [
  4797. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4798. 'multiple' => 'multiple',
  4799. ],
  4800. ['option' => ['value' => '1', 'selected' => 'selected']],
  4801. 'blue',
  4802. '/option',
  4803. ['option' => ['value' => '50f5c0cf']],
  4804. 'red',
  4805. '/option',
  4806. ['option' => ['value' => '50', 'selected' => 'selected']],
  4807. 'green',
  4808. '/option',
  4809. '/select',
  4810. '/div',
  4811. ];
  4812. $this->assertHtml($expected, $result);
  4813. $spacecraft = [
  4814. 1 => 'Orion',
  4815. 2 => 'Helios',
  4816. ];
  4817. $this->View->set('spacecraft', $spacecraft);
  4818. $this->Form->create();
  4819. $result = $this->Form->control('spacecraft._ids');
  4820. $expected = [
  4821. 'div' => ['class' => 'input select'],
  4822. 'label' => ['for' => 'spacecraft-ids'],
  4823. 'Spacecraft',
  4824. '/label',
  4825. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4826. 'select' => [
  4827. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4828. 'multiple' => 'multiple',
  4829. ],
  4830. ['option' => ['value' => '1']],
  4831. 'Orion',
  4832. '/option',
  4833. ['option' => ['value' => '2']],
  4834. 'Helios',
  4835. '/option',
  4836. '/select',
  4837. '/div',
  4838. ];
  4839. $this->assertHtml($expected, $result);
  4840. }
  4841. /**
  4842. * testErrorsForBelongsToManySelect method
  4843. *
  4844. * Tests that errors for belongsToMany select fields are being
  4845. * picked up properly.
  4846. */
  4847. public function testErrorsForBelongsToManySelect(): void
  4848. {
  4849. $spacecraft = [
  4850. 1 => 'Orion',
  4851. 2 => 'Helios',
  4852. ];
  4853. $this->View->set('spacecraft', $spacecraft);
  4854. $article = new Article();
  4855. $article->setError('spacecraft', ['Invalid']);
  4856. $this->Form->create($article);
  4857. $result = $this->Form->control('spacecraft._ids');
  4858. $expected = [
  4859. ['div' => ['class' => 'input select error']],
  4860. 'label' => ['for' => 'spacecraft-ids'],
  4861. 'Spacecraft',
  4862. '/label',
  4863. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4864. 'select' => [
  4865. 'name' => 'spacecraft[_ids][]',
  4866. 'id' => 'spacecraft-ids',
  4867. 'multiple' => 'multiple',
  4868. ],
  4869. ['option' => ['value' => '1']],
  4870. 'Orion',
  4871. '/option',
  4872. ['option' => ['value' => '2']],
  4873. 'Helios',
  4874. '/option',
  4875. '/select',
  4876. ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  4877. 'Invalid',
  4878. '/div',
  4879. '/div',
  4880. ];
  4881. $this->assertHtml($expected, $result);
  4882. }
  4883. /**
  4884. * testSelectMultipleCheckboxes method
  4885. *
  4886. * Test generation of multi select elements in checkbox format.
  4887. */
  4888. public function testSelectMultipleCheckboxes(): void
  4889. {
  4890. $result = $this->Form->select(
  4891. 'Model.multi_field',
  4892. ['first', 'second', 'third'],
  4893. ['multiple' => 'checkbox']
  4894. );
  4895. $expected = [
  4896. 'input' => [
  4897. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4898. ],
  4899. ['div' => ['class' => 'checkbox']],
  4900. ['label' => ['for' => 'model-multi-field-0']],
  4901. ['input' => [
  4902. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4903. 'value' => '0', 'id' => 'model-multi-field-0',
  4904. ]],
  4905. 'first',
  4906. '/label',
  4907. '/div',
  4908. ['div' => ['class' => 'checkbox']],
  4909. ['label' => ['for' => 'model-multi-field-1']],
  4910. ['input' => [
  4911. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4912. 'value' => '1', 'id' => 'model-multi-field-1',
  4913. ]],
  4914. 'second',
  4915. '/label',
  4916. '/div',
  4917. ['div' => ['class' => 'checkbox']],
  4918. ['label' => ['for' => 'model-multi-field-2']],
  4919. ['input' => [
  4920. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4921. 'value' => '2', 'id' => 'model-multi-field-2',
  4922. ]],
  4923. 'third',
  4924. '/label',
  4925. '/div',
  4926. ];
  4927. $this->assertHtml($expected, $result);
  4928. $result = $this->Form->select(
  4929. 'Model.multi_field',
  4930. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4931. ['multiple' => 'checkbox']
  4932. );
  4933. $expected = [
  4934. 'input' => [
  4935. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4936. ],
  4937. ['div' => ['class' => 'checkbox']],
  4938. ['label' => ['for' => 'model-multi-field-a+']],
  4939. ['input' => [
  4940. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4941. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  4942. ]],
  4943. 'first',
  4944. '/label',
  4945. '/div',
  4946. ['div' => ['class' => 'checkbox']],
  4947. ['label' => ['for' => 'model-multi-field-a++']],
  4948. ['input' => [
  4949. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4950. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  4951. ]],
  4952. 'second',
  4953. '/label',
  4954. '/div',
  4955. ['div' => ['class' => 'checkbox']],
  4956. ['label' => ['for' => 'model-multi-field-a+++']],
  4957. ['input' => [
  4958. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4959. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  4960. ]],
  4961. 'third',
  4962. '/label',
  4963. '/div',
  4964. ];
  4965. $this->assertHtml($expected, $result);
  4966. $result = $this->Form->select(
  4967. 'Model.multi_field',
  4968. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4969. ['multiple' => 'checkbox']
  4970. );
  4971. $expected = [
  4972. 'input' => [
  4973. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
  4974. ],
  4975. ['div' => ['class' => 'checkbox']],
  4976. ['label' => ['for' => 'model-multi-field-a-b']],
  4977. ['input' => [
  4978. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4979. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  4980. ]],
  4981. 'first',
  4982. '/label',
  4983. '/div',
  4984. ['div' => ['class' => 'checkbox']],
  4985. ['label' => ['for' => 'model-multi-field-a-b1']],
  4986. ['input' => [
  4987. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4988. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  4989. ]],
  4990. 'second',
  4991. '/label',
  4992. '/div',
  4993. ['div' => ['class' => 'checkbox']],
  4994. ['label' => ['for' => 'model-multi-field-a-b2']],
  4995. ['input' => [
  4996. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4997. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  4998. ]],
  4999. 'third',
  5000. '/label',
  5001. '/div',
  5002. ];
  5003. $this->assertHtml($expected, $result);
  5004. }
  5005. /**
  5006. * testSelectMultipleCheckboxRequestData method
  5007. *
  5008. * Ensure that multiCheckbox reads from the request data.
  5009. */
  5010. public function testSelectMultipleCheckboxRequestData(): void
  5011. {
  5012. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5013. $result = $this->Form->select(
  5014. 'Model.tags',
  5015. ['1' => 'first', 'Array' => 'Array'],
  5016. ['multiple' => 'checkbox']
  5017. );
  5018. $expected = [
  5019. 'input' => [
  5020. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'model-tags',
  5021. ],
  5022. ['div' => ['class' => 'checkbox']],
  5023. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5024. ['input' => [
  5025. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5026. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5027. ]],
  5028. 'first',
  5029. '/label',
  5030. '/div',
  5031. ['div' => ['class' => 'checkbox']],
  5032. ['label' => ['for' => 'model-tags-array']],
  5033. ['input' => [
  5034. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5035. 'value' => 'Array', 'id' => 'model-tags-array',
  5036. ]],
  5037. 'Array',
  5038. '/label',
  5039. '/div',
  5040. ];
  5041. $this->assertHtml($expected, $result);
  5042. }
  5043. /**
  5044. * testSelectMultipleCheckboxSecurity method
  5045. *
  5046. * Checks the security hash array generated for multiple-input checkbox elements.
  5047. */
  5048. public function testSelectMultipleCheckboxSecurity(): void
  5049. {
  5050. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5051. $this->Form->create();
  5052. $this->Form->select(
  5053. 'Model.multi_field',
  5054. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5055. ['multiple' => 'checkbox']
  5056. );
  5057. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5058. $this->assertEquals(['Model.multi_field'], $fields);
  5059. $result = $this->Form->secure();
  5060. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5061. $hash = urlencode($hash . ':');
  5062. $this->assertStringContainsString('"' . $hash . '"', $result);
  5063. }
  5064. /**
  5065. * testSelectMultipleSecureWithNoOptions method
  5066. *
  5067. * Multiple select elements should always be secured as they always participate
  5068. * in the POST data.
  5069. */
  5070. public function testSelectMultipleSecureWithNoOptions(): void
  5071. {
  5072. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5073. $this->Form->create();
  5074. $this->Form->select(
  5075. 'Model.select',
  5076. [],
  5077. ['multiple' => true]
  5078. );
  5079. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5080. $this->assertEquals(['Model.select'], $result);
  5081. }
  5082. /**
  5083. * testSelectNoSecureWithNoOptions method
  5084. *
  5085. * When a select box has no options it should not be added to the fields list
  5086. * as it always fail post validation.
  5087. */
  5088. public function testSelectNoSecureWithNoOptions(): void
  5089. {
  5090. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5091. $this->Form->create();
  5092. $this->Form->select(
  5093. 'Model.select',
  5094. []
  5095. );
  5096. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5097. $this->assertEquals([], $result);
  5098. $this->Form->select(
  5099. 'Model.user_id',
  5100. [],
  5101. ['empty' => true]
  5102. );
  5103. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5104. $this->assertEquals(['Model.user_id'], $result);
  5105. }
  5106. /**
  5107. * testControlMultipleCheckboxes method
  5108. *
  5109. * Test control() resulting in multi select elements being generated.
  5110. */
  5111. public function testControlMultipleCheckboxes(): void
  5112. {
  5113. $result = $this->Form->control('Model.multi_field', [
  5114. 'options' => ['first', 'second', 'third'],
  5115. 'multiple' => 'checkbox',
  5116. ]);
  5117. $expected = [
  5118. ['div' => ['class' => 'input select']],
  5119. ['label' => ['for' => 'model-multi-field']],
  5120. 'Multi Field',
  5121. '/label',
  5122. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5123. ['div' => ['class' => 'checkbox']],
  5124. ['label' => ['for' => 'model-multi-field-0']],
  5125. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5126. 'first',
  5127. '/label',
  5128. '/div',
  5129. ['div' => ['class' => 'checkbox']],
  5130. ['label' => ['for' => 'model-multi-field-1']],
  5131. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5132. 'second',
  5133. '/label',
  5134. '/div',
  5135. ['div' => ['class' => 'checkbox']],
  5136. ['label' => ['for' => 'model-multi-field-2']],
  5137. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5138. 'third',
  5139. '/label',
  5140. '/div',
  5141. '/div',
  5142. ];
  5143. $this->assertHtml($expected, $result);
  5144. $result = $this->Form->control('Model.multi_field', [
  5145. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5146. 'multiple' => 'checkbox',
  5147. ]);
  5148. $expected = [
  5149. ['div' => ['class' => 'input select']],
  5150. ['label' => ['for' => 'model-multi-field']],
  5151. 'Multi Field',
  5152. '/label',
  5153. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
  5154. ['div' => ['class' => 'checkbox']],
  5155. ['label' => ['for' => 'model-multi-field-a']],
  5156. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5157. 'first',
  5158. '/label',
  5159. '/div',
  5160. ['div' => ['class' => 'checkbox']],
  5161. ['label' => ['for' => 'model-multi-field-b']],
  5162. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5163. 'second',
  5164. '/label',
  5165. '/div',
  5166. ['div' => ['class' => 'checkbox']],
  5167. ['label' => ['for' => 'model-multi-field-c']],
  5168. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5169. 'third',
  5170. '/label',
  5171. '/div',
  5172. '/div',
  5173. ];
  5174. $this->assertHtml($expected, $result);
  5175. }
  5176. /**
  5177. * testSelectHiddenFieldOmission method
  5178. *
  5179. * Test that select() with 'hiddenField' => false omits the hidden field.
  5180. */
  5181. public function testSelectHiddenFieldOmission(): void
  5182. {
  5183. $result = $this->Form->select(
  5184. 'Model.multi_field',
  5185. ['first', 'second'],
  5186. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5187. );
  5188. $this->assertStringNotContainsString('type="hidden"', $result);
  5189. }
  5190. /**
  5191. * testSelectCheckboxMultipleOverrideName method
  5192. *
  5193. * Test that select() with multiple = checkbox works with overriding name attribute.
  5194. */
  5195. public function testSelectCheckboxMultipleOverrideName(): void
  5196. {
  5197. $result = $this->Form->select('category', ['1', '2'], [
  5198. 'multiple' => 'checkbox',
  5199. 'name' => 'fish',
  5200. ]);
  5201. $expected = [
  5202. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  5203. ['div' => ['class' => 'checkbox']],
  5204. ['label' => ['for' => 'fish-0']],
  5205. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5206. '1',
  5207. '/label',
  5208. '/div',
  5209. ['div' => ['class' => 'checkbox']],
  5210. ['label' => ['for' => 'fish-1']],
  5211. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5212. '2',
  5213. '/label',
  5214. '/div',
  5215. ];
  5216. $this->assertHtml($expected, $result);
  5217. $result = $this->Form->multiCheckbox(
  5218. 'category',
  5219. new Collection(['1', '2']),
  5220. ['name' => 'fish']
  5221. );
  5222. $this->assertHtml($expected, $result);
  5223. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5224. 'name' => 'fish',
  5225. ]);
  5226. $this->assertHtml($expected, $result);
  5227. }
  5228. /**
  5229. * testSelectCheckboxMultipleOverrideName method
  5230. *
  5231. * Test that select() with multiple = checkbox works with overriding name attribute.
  5232. */
  5233. public function testSelectCheckboxMultipleCustomId(): void
  5234. {
  5235. $result = $this->Form->select('category', ['1', '2'], [
  5236. 'multiple' => 'checkbox',
  5237. 'id' => 'cat',
  5238. ]);
  5239. $expected = [
  5240. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'cat'],
  5241. ['div' => ['class' => 'checkbox']],
  5242. ['label' => ['for' => 'cat-0']],
  5243. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'cat-0']],
  5244. '1',
  5245. '/label',
  5246. '/div',
  5247. ['div' => ['class' => 'checkbox']],
  5248. ['label' => ['for' => 'cat-1']],
  5249. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'cat-1']],
  5250. '2',
  5251. '/label',
  5252. '/div',
  5253. ];
  5254. $this->assertHtml($expected, $result);
  5255. $result = $this->Form->multiCheckbox(
  5256. 'category',
  5257. ['1', '2'],
  5258. ['id' => 'cat']
  5259. );
  5260. $this->assertHtml($expected, $result);
  5261. }
  5262. /**
  5263. * testControlMultiCheckbox method
  5264. *
  5265. * Test that control() works with multicheckbox.
  5266. */
  5267. public function testControlMultiCheckbox(): void
  5268. {
  5269. $result = $this->Form->control('category', [
  5270. 'type' => 'multicheckbox',
  5271. 'options' => ['1', '2'],
  5272. ]);
  5273. $expected = [
  5274. ['div' => ['class' => 'input multicheckbox']],
  5275. '<label',
  5276. 'Category',
  5277. '/label',
  5278. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'category'],
  5279. ['div' => ['class' => 'checkbox']],
  5280. ['label' => ['for' => 'category-0']],
  5281. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5282. '1',
  5283. '/label',
  5284. '/div',
  5285. ['div' => ['class' => 'checkbox']],
  5286. ['label' => ['for' => 'category-1']],
  5287. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5288. '2',
  5289. '/label',
  5290. '/div',
  5291. '/div',
  5292. ];
  5293. $this->assertHtml($expected, $result);
  5294. }
  5295. /**
  5296. * testCheckbox method
  5297. *
  5298. * Test generation of checkboxes.
  5299. */
  5300. public function testCheckbox(): void
  5301. {
  5302. $result = $this->Form->checkbox('Model.field');
  5303. $expected = [
  5304. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5305. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5306. ];
  5307. $this->assertHtml($expected, $result);
  5308. $result = $this->Form->checkbox('Model.field', [
  5309. 'id' => 'theID',
  5310. 'value' => 'myvalue',
  5311. 'form' => 'my-form',
  5312. ]);
  5313. $expected = [
  5314. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5315. ['input' => [
  5316. 'type' => 'checkbox', 'name' => 'Model[field]',
  5317. 'value' => 'myvalue', 'id' => 'theID',
  5318. 'form' => 'my-form',
  5319. ]],
  5320. ];
  5321. $this->assertHtml($expected, $result);
  5322. }
  5323. /**
  5324. * testCheckboxDefaultValue method
  5325. *
  5326. * Test default value setting on checkbox() method.
  5327. */
  5328. public function testCheckboxDefaultValue(): void
  5329. {
  5330. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5331. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5332. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5333. $this->assertHtml($expected, $result);
  5334. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5335. $this->Form->create();
  5336. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5337. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5338. $this->assertHtml($expected, $result);
  5339. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5340. $this->Form->create();
  5341. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5342. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5343. $this->assertHtml($expected, $result);
  5344. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5345. $this->Form->create();
  5346. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5347. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5348. $this->assertHtml($expected, $result);
  5349. $Articles = $this->getTableLocator()->get('Articles');
  5350. $Articles->getSchema()->addColumn(
  5351. 'published',
  5352. ['type' => 'boolean', 'null' => false, 'default' => true]
  5353. );
  5354. $this->Form->create($Articles->newEmptyEntity());
  5355. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5356. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5357. $this->assertHtml($expected, $result);
  5358. }
  5359. /**
  5360. * testCheckboxCheckedAndError method
  5361. *
  5362. * Test checkbox being checked or having errors.
  5363. */
  5364. public function testCheckboxCheckedAndError(): void
  5365. {
  5366. $this->article['errors'] = [
  5367. 'published' => true,
  5368. ];
  5369. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5370. $this->Form->create($this->article);
  5371. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5372. $expected = [
  5373. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5374. ['input' => [
  5375. 'type' => 'checkbox',
  5376. 'name' => 'published',
  5377. 'value' => 'myvalue',
  5378. 'id' => 'theID',
  5379. 'checked' => 'checked',
  5380. 'class' => 'form-error',
  5381. ]],
  5382. ];
  5383. $this->assertHtml($expected, $result);
  5384. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5385. $this->Form->create($this->article);
  5386. $result = $this->Form->checkbox('published');
  5387. $expected = [
  5388. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5389. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5390. ];
  5391. $this->assertHtml($expected, $result);
  5392. }
  5393. /**
  5394. * testCheckboxCustomNameAttribute method
  5395. *
  5396. * Test checkbox() with a custom name attribute.
  5397. */
  5398. public function testCheckboxCustomNameAttribute(): void
  5399. {
  5400. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5401. $expected = [
  5402. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5403. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5404. ];
  5405. $this->assertHtml($expected, $result);
  5406. }
  5407. /**
  5408. * testCheckboxHiddenField method
  5409. *
  5410. * Test that the hidden input for checkboxes can be omitted or set to a
  5411. * specific value.
  5412. */
  5413. public function testCheckboxHiddenField(): void
  5414. {
  5415. $result = $this->Form->checkbox('UserForm.something', [
  5416. 'hiddenField' => false,
  5417. ]);
  5418. $expected = [
  5419. 'input' => [
  5420. 'type' => 'checkbox',
  5421. 'name' => 'UserForm[something]',
  5422. 'value' => '1',
  5423. ],
  5424. ];
  5425. $this->assertHtml($expected, $result);
  5426. $result = $this->Form->checkbox('UserForm.something', [
  5427. 'value' => 'Y',
  5428. 'hiddenField' => '',
  5429. ]);
  5430. $expected = [
  5431. ['input' => [
  5432. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5433. 'value' => '',
  5434. ]],
  5435. ['input' => [
  5436. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5437. 'value' => 'Y',
  5438. ]],
  5439. ];
  5440. $this->assertHtml($expected, $result);
  5441. $result = $this->Form->checkbox('UserForm.something', [
  5442. 'value' => 'Y',
  5443. 'hiddenField' => 'N',
  5444. ]);
  5445. $expected = [
  5446. ['input' => [
  5447. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5448. 'value' => 'N',
  5449. ]],
  5450. ['input' => [
  5451. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5452. 'value' => 'Y',
  5453. ]],
  5454. ];
  5455. $this->assertHtml($expected, $result);
  5456. }
  5457. /**
  5458. * testTime method
  5459. *
  5460. * Test the time type.
  5461. */
  5462. public function testTime(): void
  5463. {
  5464. $result = $this->Form->time('start_time', [
  5465. 'value' => '2014-03-08 16:30:00',
  5466. ]);
  5467. $expected = [
  5468. 'input' => [
  5469. 'type' => 'time',
  5470. 'name' => 'start_time',
  5471. 'value' => '16:30:00',
  5472. 'step' => '1',
  5473. ],
  5474. ];
  5475. $this->assertHtml($expected, $result);
  5476. }
  5477. /**
  5478. * testDate method
  5479. *
  5480. * Test the date type.
  5481. */
  5482. public function testDate(): void
  5483. {
  5484. $result = $this->Form->date('start_day', [
  5485. 'value' => '2014-03-08',
  5486. ]);
  5487. $expected = [
  5488. 'input' => [
  5489. 'type' => 'date',
  5490. 'name' => 'start_day',
  5491. 'value' => '2014-03-08',
  5492. ],
  5493. ];
  5494. $this->assertHtml($expected, $result);
  5495. $result = $this->Form->date('start_day', [
  5496. 'value' => new FrozenDate('2014-03-08'),
  5497. ]);
  5498. $this->assertHtml($expected, $result);
  5499. }
  5500. /**
  5501. * testDateTime method
  5502. */
  5503. public function testDateTime(): void
  5504. {
  5505. $result = $this->Form->dateTime('date', ['default' => true]);
  5506. $expected = [
  5507. 'input' => [
  5508. 'type' => 'datetime-local',
  5509. 'name' => 'date',
  5510. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5511. 'step' => '1',
  5512. ],
  5513. ];
  5514. $this->assertHtml($expected, $result);
  5515. }
  5516. /**
  5517. * testDateTimeSecured method
  5518. *
  5519. * Test that datetime fields are added to protected fields list.
  5520. */
  5521. public function testDateTimeSecured(): void
  5522. {
  5523. $this->View->setRequest(
  5524. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5525. );
  5526. $this->Form->create();
  5527. $this->Form->dateTime('date');
  5528. $expected = ['date'];
  5529. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5530. $this->assertEquals($expected, $result);
  5531. $this->Form->fields = [];
  5532. $this->Form->date('published');
  5533. $expected = ['date', 'published'];
  5534. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5535. $this->assertEquals($expected, $result);
  5536. }
  5537. /**
  5538. * testDateTimeSecuredDisabled method
  5539. *
  5540. * Test that datetime fields are added to protected fields list.
  5541. */
  5542. public function testDateTimeSecuredDisabled(): void
  5543. {
  5544. $this->View->setRequest(
  5545. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5546. );
  5547. $this->Form->create();
  5548. $this->Form->dateTime('date', ['secure' => false]);
  5549. $expected = [];
  5550. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5551. $this->assertEquals($expected, $result);
  5552. $this->Form->fields = [];
  5553. $this->Form->date('published', ['secure' => false]);
  5554. $expected = [];
  5555. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5556. $this->assertEquals($expected, $result);
  5557. }
  5558. /**
  5559. * testDatetimeWithDefault method
  5560. *
  5561. * Test that datetime() and default values work.
  5562. */
  5563. public function testDatetimeWithDefault(): void
  5564. {
  5565. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5566. $expected = [
  5567. 'input' => [
  5568. 'type' => 'datetime-local',
  5569. 'name' => 'updated',
  5570. 'value' => '2009-06-01T11:15:30',
  5571. 'step' => '1',
  5572. ],
  5573. ];
  5574. $this->assertHtml($expected, $result);
  5575. $result = $this->Form->dateTime('updated', [
  5576. 'default' => '2009-06-01 11:15:30',
  5577. ]);
  5578. $expected = [
  5579. 'input' => [
  5580. 'type' => 'datetime-local',
  5581. 'name' => 'updated',
  5582. 'value' => '2009-06-01T11:15:30',
  5583. 'step' => '1',
  5584. ],
  5585. ];
  5586. $this->assertHtml($expected, $result);
  5587. }
  5588. /**
  5589. * testMonth method
  5590. *
  5591. * Test generation of a month input.
  5592. */
  5593. public function testMonth(): void
  5594. {
  5595. $result = $this->Form->month('field', ['value' => '']);
  5596. $expected = [
  5597. 'input' => [
  5598. 'type' => 'month',
  5599. 'name' => 'field',
  5600. 'value' => '',
  5601. ],
  5602. ];
  5603. $this->assertHtml($expected, $result);
  5604. $this->View->setRequest(
  5605. $this->View->getRequest()->withData('release', '2050-02-10')
  5606. );
  5607. $this->Form->create();
  5608. $result = $this->Form->month('release');
  5609. $expected = [
  5610. 'input' => [
  5611. 'type' => 'month',
  5612. 'name' => 'release',
  5613. 'value' => '2050-02',
  5614. ],
  5615. ];
  5616. $this->assertHtml($expected, $result);
  5617. $this->View->setRequest(
  5618. $this->View->getRequest()->withData('release', '2050-03')
  5619. );
  5620. $this->Form->create();
  5621. $result = $this->Form->month('release');
  5622. $expected = [
  5623. 'input' => [
  5624. 'type' => 'month',
  5625. 'name' => 'release',
  5626. 'value' => '2050-03',
  5627. ],
  5628. ];
  5629. $this->assertHtml($expected, $result);
  5630. }
  5631. /**
  5632. * testYear method
  5633. *
  5634. * Test generation of a year input.
  5635. */
  5636. public function testYear(): void
  5637. {
  5638. $this->View->setRequest(
  5639. $this->View->getRequest()->withData('published', '2006')
  5640. );
  5641. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5642. $expected = [
  5643. ['select' => ['name' => 'field']],
  5644. ['option' => ['selected' => 'selected', 'value' => '']],
  5645. '/option',
  5646. ['option' => ['value' => '2007']],
  5647. '2007',
  5648. '/option',
  5649. ['option' => ['value' => '2006']],
  5650. '2006',
  5651. '/option',
  5652. '/select',
  5653. ];
  5654. $this->assertHtml($expected, $result);
  5655. $result = $this->Form->year('field', [
  5656. 'value' => '',
  5657. 'min' => 2006,
  5658. 'max' => 2007,
  5659. 'order' => 'asc',
  5660. ]);
  5661. $expected = [
  5662. ['select' => ['name' => 'field']],
  5663. ['option' => ['selected' => 'selected', 'value' => '']],
  5664. '/option',
  5665. ['option' => ['value' => '2006']],
  5666. '2006',
  5667. '/option',
  5668. ['option' => ['value' => '2007']],
  5669. '2007',
  5670. '/option',
  5671. '/select',
  5672. ];
  5673. $this->assertHtml($expected, $result);
  5674. $result = $this->Form->year('published', [
  5675. 'empty' => false,
  5676. 'min' => 2006,
  5677. 'max' => 2007,
  5678. ]);
  5679. $expected = [
  5680. ['select' => ['name' => 'published']],
  5681. ['option' => ['value' => '2007']],
  5682. '2007',
  5683. '/option',
  5684. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5685. '2006',
  5686. '/option',
  5687. '/select',
  5688. ];
  5689. $this->assertHtml($expected, $result);
  5690. $result = $this->Form->year('published', [
  5691. 'empty' => false,
  5692. 'value' => new FrozenDate('2008-01-12'),
  5693. 'min' => 2007,
  5694. 'max' => 2009,
  5695. ]);
  5696. $expected = [
  5697. ['select' => ['name' => 'published']],
  5698. ['option' => ['value' => '2009']],
  5699. '2009',
  5700. '/option',
  5701. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5702. '2008',
  5703. '/option',
  5704. ['option' => ['value' => '2007']],
  5705. '2007',
  5706. '/option',
  5707. '/select',
  5708. ];
  5709. $this->assertHtml($expected, $result);
  5710. $result = $this->Form->year('published', [
  5711. 'empty' => 'Published on',
  5712. ]);
  5713. $this->assertStringContainsString('Published on', $result);
  5714. }
  5715. /**
  5716. * testControlYearPreEpoch method
  5717. *
  5718. * Test minYear being prior to the unix epoch.
  5719. */
  5720. public function testControlYearPreEpoch(): void
  5721. {
  5722. $start = date('Y') - 80;
  5723. $end = date('Y') - 18;
  5724. $result = $this->Form->control('birth_year', [
  5725. 'type' => 'year',
  5726. 'label' => 'Birth Year',
  5727. 'min' => $start,
  5728. 'max' => $end,
  5729. ]);
  5730. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5731. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5732. $this->assertStringNotContainsString('value="00">00', $result);
  5733. }
  5734. /**
  5735. * test control() datetime & required attributes
  5736. */
  5737. public function testControlDatetimeRequired(): void
  5738. {
  5739. $result = $this->Form->control('birthday', [
  5740. 'type' => 'date',
  5741. 'required' => true,
  5742. ]);
  5743. $this->assertStringContainsString(
  5744. '<input type="date" name="birthday" required="required"',
  5745. $result
  5746. );
  5747. }
  5748. /**
  5749. * testYearAutoExpandRange method
  5750. */
  5751. public function testYearAutoExpandRange(): void
  5752. {
  5753. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5754. $result = $this->Form->year('birthday');
  5755. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5756. $result = $matches[1];
  5757. $expected = range(date('Y') + 5, 1930);
  5758. $this->assertEquals($expected, $result);
  5759. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5760. $this->Form->create();
  5761. $result = $this->Form->year('release');
  5762. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5763. $result = $matches[1];
  5764. $expected = range(2050, date('Y') - 5);
  5765. $this->assertEquals($expected, $result);
  5766. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5767. $this->Form->create();
  5768. $result = $this->Form->year('release', [
  5769. 'min' => 1890,
  5770. 'max' => 1900,
  5771. ]);
  5772. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5773. $result = $matches[1];
  5774. $expected = range(1900, 1881);
  5775. $this->assertEquals($expected, $result);
  5776. }
  5777. /**
  5778. * test control placeholder + label
  5779. */
  5780. public function testControlLabelAndPlaceholder(): void
  5781. {
  5782. $this->Form->create($this->article);
  5783. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5784. $expected = [
  5785. 'div' => ['class' => 'input text required'],
  5786. 'label' => ['for' => 'title'],
  5787. 'Title',
  5788. '/label',
  5789. 'input' => [
  5790. 'aria-required' => 'true',
  5791. 'type' => 'text',
  5792. 'required' => 'required',
  5793. 'placeholder' => 'Add title',
  5794. 'id' => 'title',
  5795. 'name' => 'title',
  5796. 'data-validity-message' => 'This field cannot be left empty',
  5797. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5798. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5799. ],
  5800. '/div',
  5801. ];
  5802. $this->assertHtml($expected, $result);
  5803. }
  5804. /**
  5805. * testControlLabelFalse method
  5806. *
  5807. * Test the label option being set to false.
  5808. */
  5809. public function testControlLabelFalse(): void
  5810. {
  5811. $this->Form->create($this->article);
  5812. $result = $this->Form->control('title', ['label' => false]);
  5813. $expected = [
  5814. 'div' => ['class' => 'input text required'],
  5815. 'input' => [
  5816. 'aria-required' => 'true',
  5817. 'type' => 'text',
  5818. 'required' => 'required',
  5819. 'id' => 'title',
  5820. 'name' => 'title',
  5821. 'data-validity-message' => 'This field cannot be left empty',
  5822. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5823. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5824. ],
  5825. '/div',
  5826. ];
  5827. $this->assertHtml($expected, $result);
  5828. $this->Form->create($this->article);
  5829. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5830. $expected['input'] += [
  5831. 'placeholder' => 'Add title',
  5832. 'aria-label' => 'Add title',
  5833. ];
  5834. $this->assertHtml($expected, $result);
  5835. }
  5836. /**
  5837. * testTextArea method
  5838. *
  5839. * Test generation of a textarea input.
  5840. */
  5841. public function testTextArea(): void
  5842. {
  5843. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5844. $result = $this->Form->textarea('field');
  5845. $expected = [
  5846. 'textarea' => ['name' => 'field', 'rows' => 5],
  5847. 'some test data',
  5848. '/textarea',
  5849. ];
  5850. $this->assertHtml($expected, $result);
  5851. $result = $this->Form->textarea('user.bio');
  5852. $expected = [
  5853. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5854. '/textarea',
  5855. ];
  5856. $this->assertHtml($expected, $result);
  5857. $this->View->setRequest($this->View->getRequest()
  5858. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5859. $this->Form->create();
  5860. $result = $this->Form->textarea('field');
  5861. $expected = [
  5862. 'textarea' => ['name' => 'field', 'rows' => 5],
  5863. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5864. '/textarea',
  5865. ];
  5866. $this->assertHtml($expected, $result);
  5867. $this->View->setRequest($this->View->getRequest()->withData(
  5868. 'Model.field',
  5869. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5870. ));
  5871. $this->Form->create();
  5872. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5873. $expected = [
  5874. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5875. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5876. '/textarea',
  5877. ];
  5878. $this->assertHtml($expected, $result);
  5879. $result = $this->Form->textarea('0.OtherModel.field');
  5880. $expected = [
  5881. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5882. '/textarea',
  5883. ];
  5884. $this->assertHtml($expected, $result);
  5885. }
  5886. /**
  5887. * testTextAreaWithStupidCharacters method
  5888. *
  5889. * Test text area with non-ascii characters.
  5890. */
  5891. public function testTextAreaWithStupidCharacters(): void
  5892. {
  5893. $result = $this->Form->textarea('Post.content', [
  5894. 'value' => 'GREAT®',
  5895. 'rows' => '15',
  5896. 'cols' => '75',
  5897. ]);
  5898. $expected = [
  5899. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5900. 'GREAT®',
  5901. '/textarea',
  5902. ];
  5903. $this->assertHtml($expected, $result);
  5904. }
  5905. /**
  5906. * testTextAreaMaxLength method
  5907. *
  5908. * Test textareas maxlength read from schema.
  5909. */
  5910. public function testTextAreaMaxLength(): void
  5911. {
  5912. $this->Form->create([
  5913. 'schema' => [
  5914. 'stuff' => ['type' => 'string', 'length' => 10],
  5915. ],
  5916. ]);
  5917. $result = $this->Form->control('other', ['type' => 'textarea']);
  5918. $expected = [
  5919. 'div' => ['class' => 'input textarea'],
  5920. 'label' => ['for' => 'other'],
  5921. 'Other',
  5922. '/label',
  5923. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  5924. '/textarea',
  5925. '/div',
  5926. ];
  5927. $this->assertHtml($expected, $result);
  5928. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  5929. $expected = [
  5930. 'div' => ['class' => 'input textarea'],
  5931. 'label' => ['for' => 'stuff'],
  5932. 'Stuff',
  5933. '/label',
  5934. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  5935. '/textarea',
  5936. '/div',
  5937. ];
  5938. $this->assertHtml($expected, $result);
  5939. }
  5940. /**
  5941. * testHiddenField method
  5942. *
  5943. * Test generation of a hidden input.
  5944. */
  5945. public function testHidden(): void
  5946. {
  5947. $this->article['errors'] = [
  5948. 'field' => true,
  5949. ];
  5950. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  5951. $this->Form->create($this->article);
  5952. $result = $this->Form->hidden('field', ['id' => 'theID']);
  5953. $expected = [
  5954. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  5955. $this->assertHtml($expected, $result);
  5956. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5957. $expected = [
  5958. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  5959. ];
  5960. $this->assertHtml($expected, $result);
  5961. }
  5962. /**
  5963. * Test hidden() with various boolean values.
  5964. */
  5965. public function testHiddenBooleanValues(): void
  5966. {
  5967. $this->Form->create($this->article);
  5968. $result = $this->Form->hidden('field', ['value' => null]);
  5969. $expected = [
  5970. 'input' => ['type' => 'hidden', 'name' => 'field'],
  5971. ];
  5972. $this->assertHtml($expected, $result);
  5973. $result = $this->Form->hidden('field', ['value' => true]);
  5974. $expected = [
  5975. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  5976. ];
  5977. $this->assertHtml($expected, $result);
  5978. $result = $this->Form->hidden('field', ['value' => false]);
  5979. $expected = [
  5980. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  5981. ];
  5982. $this->assertHtml($expected, $result);
  5983. }
  5984. /**
  5985. * testFileUploadField method
  5986. *
  5987. * Test generation of a file upload input.
  5988. */
  5989. public function testFileUploadField(): void
  5990. {
  5991. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5992. $result = $this->Form->file('Model.upload');
  5993. $this->assertHtml($expected, $result);
  5994. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  5995. 'name' => '', 'type' => '', 'tmp_name' => '',
  5996. 'error' => 4, 'size' => 0,
  5997. ]));
  5998. $result = $this->Form->file('Model.upload');
  5999. $this->assertHtml($expected, $result);
  6000. $this->View->setRequest(
  6001. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6002. );
  6003. $result = $this->Form->file('Model.upload');
  6004. $this->assertHtml($expected, $result);
  6005. }
  6006. /**
  6007. * testFileUploadOnOtherModel method
  6008. *
  6009. * Test File upload input on a model not used in create().
  6010. */
  6011. public function testFileUploadOnOtherModel(): void
  6012. {
  6013. $this->Form->create($this->article, ['type' => 'file']);
  6014. $result = $this->Form->file('ValidateProfile.city');
  6015. $expected = [
  6016. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6017. ];
  6018. $this->assertHtml($expected, $result);
  6019. }
  6020. /**
  6021. * testButton method
  6022. *
  6023. * Test generation of a form button.
  6024. */
  6025. public function testButton(): void
  6026. {
  6027. $result = $this->Form->button('Hi');
  6028. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6029. $this->assertHtml($expected, $result);
  6030. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  6031. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6032. $this->assertHtml($expected, $result);
  6033. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  6034. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6035. $this->assertHtml($expected, $result);
  6036. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  6037. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6038. $this->assertHtml($expected, $result);
  6039. $result = $this->Form->button('No type', ['type' => false]);
  6040. $expected = ['button' => [], 'No type', '/button'];
  6041. $this->assertHtml($expected, $result);
  6042. $result = $this->Form->button('Upload Text', [
  6043. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6044. 'escape' => false,
  6045. ]);
  6046. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  6047. }
  6048. /**
  6049. * testButtonUnlockedByDefault method
  6050. *
  6051. * Test that button() makes unlocked fields by default.
  6052. */
  6053. public function testButtonUnlockedByDefault(): void
  6054. {
  6055. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6056. $this->Form->create();
  6057. $this->Form->button('Save', ['name' => 'save']);
  6058. $this->Form->button('Clear');
  6059. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6060. $this->assertEquals(['save'], $result);
  6061. }
  6062. /**
  6063. * Test generation of a form button with confirm message.
  6064. */
  6065. public function testButtonWithConfirm(): void
  6066. {
  6067. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6068. $expected = ['button' => [
  6069. 'type' => 'submit',
  6070. 'data-confirm-message' => 'Confirm me!',
  6071. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6072. ], 'Hi', '/button'];
  6073. $this->assertHtml($expected, $result);
  6074. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6075. $expected = ['button' => [
  6076. 'type' => 'submit',
  6077. 'data-confirm-message' => 'Confirm "me"!',
  6078. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6079. ], 'Hi', '/button'];
  6080. $this->assertHtml($expected, $result);
  6081. }
  6082. /**
  6083. * testPostButton method
  6084. */
  6085. public function testPostButton(): void
  6086. {
  6087. $result = $this->Form->postButton('Hi', '/controller/action');
  6088. $expected = [
  6089. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6090. 'button' => ['type' => 'submit'],
  6091. 'Hi',
  6092. '/button',
  6093. '/form',
  6094. ];
  6095. $this->assertHtml($expected, $result);
  6096. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6097. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6098. }
  6099. /**
  6100. * testPostButtonMethodType method
  6101. */
  6102. public function testPostButtonMethodType(): void
  6103. {
  6104. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6105. $expected = [
  6106. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6107. 'div' => ['style' => 'display:none;'],
  6108. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6109. '/div',
  6110. 'button' => ['type' => 'submit'],
  6111. 'Hi',
  6112. '/button',
  6113. '/form',
  6114. ];
  6115. $this->assertHtml($expected, $result);
  6116. }
  6117. /**
  6118. * testPostButtonFormOptions method
  6119. */
  6120. public function testPostButtonFormOptions(): void
  6121. {
  6122. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6123. $expected = [
  6124. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6125. 'button' => ['type' => 'submit'],
  6126. 'Hi',
  6127. '/button',
  6128. '/form',
  6129. ];
  6130. $this->assertHtml($expected, $result);
  6131. }
  6132. /**
  6133. * testPostButtonNestedData method
  6134. *
  6135. * Test using postButton with N dimensional data.
  6136. */
  6137. public function testPostButtonNestedData(): void
  6138. {
  6139. $data = [
  6140. 'one' => [
  6141. 'two' => [
  6142. 3, 4, 5,
  6143. ],
  6144. ],
  6145. ];
  6146. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6147. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6148. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6149. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6150. }
  6151. /**
  6152. * testSecurePostButton method
  6153. *
  6154. * Test that postButton adds _Token fields.
  6155. */
  6156. public function testSecurePostButton(): void
  6157. {
  6158. $this->View->setRequest($this->View->getRequest()
  6159. ->withAttribute('csrfToken', 'testkey')
  6160. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6161. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6162. $tokenDebug = urlencode(json_encode([
  6163. '/posts/delete/1',
  6164. [],
  6165. [],
  6166. ]));
  6167. $expected = [
  6168. 'form' => [
  6169. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6170. ],
  6171. ['div' => ['style' => 'display:none;']],
  6172. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6173. '/div',
  6174. 'button' => ['type' => 'submit'],
  6175. 'Delete',
  6176. '/button',
  6177. ['div' => ['style' => 'display:none;']],
  6178. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6179. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6180. ['input' => [
  6181. 'type' => 'hidden',
  6182. 'name' => '_Token[debug]',
  6183. 'value' => $tokenDebug,
  6184. 'autocomplete' => 'off',
  6185. ]],
  6186. '/div',
  6187. '/form',
  6188. ];
  6189. $this->assertHtml($expected, $result);
  6190. }
  6191. /**
  6192. * testPostLink method
  6193. */
  6194. public function testPostLink(): void
  6195. {
  6196. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6197. $expected = [
  6198. 'form' => [
  6199. 'method' => 'post', 'action' => '/posts/delete/1',
  6200. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6201. ],
  6202. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6203. '/form',
  6204. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6205. 'Delete',
  6206. '/a',
  6207. ];
  6208. $this->assertHtml($expected, $result);
  6209. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6210. $expected = [
  6211. 'form' => [
  6212. 'method' => 'post', 'action' => '/posts/delete/1',
  6213. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6214. ],
  6215. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6216. '/form',
  6217. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6218. 'Delete',
  6219. '/a',
  6220. ];
  6221. $this->assertHtml($expected, $result);
  6222. $result = $this->Form->postLink(
  6223. 'Delete',
  6224. '/posts/delete/1',
  6225. ['target' => '_blank', 'class' => 'btn btn-danger']
  6226. );
  6227. $expected = [
  6228. 'form' => [
  6229. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6230. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6231. ],
  6232. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6233. '/form',
  6234. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6235. 'Delete',
  6236. '/a',
  6237. ];
  6238. $this->assertHtml($expected, $result);
  6239. }
  6240. /**
  6241. * testPostLinkWithConfirm method
  6242. *
  6243. * Test the confirm option for postLink().
  6244. */
  6245. public function testPostLinkWithConfirm(): void
  6246. {
  6247. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6248. $expected = [
  6249. 'form' => [
  6250. 'method' => 'post', 'action' => '/posts/delete/1',
  6251. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6252. ],
  6253. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6254. '/form',
  6255. 'a' => [
  6256. 'href' => '#',
  6257. 'data-confirm-message' => 'Confirm?',
  6258. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6259. ],
  6260. 'Delete',
  6261. '/a',
  6262. ];
  6263. $this->assertHtml($expected, $result);
  6264. $result = $this->Form->postLink(
  6265. 'Delete',
  6266. '/posts/delete/1',
  6267. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6268. );
  6269. $expected = [
  6270. 'form' => [
  6271. 'method' => 'post', 'action' => '/posts/delete/1',
  6272. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6273. ],
  6274. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6275. '/form',
  6276. 'a' => [
  6277. 'href' => '#',
  6278. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6279. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6280. ],
  6281. 'Delete',
  6282. '/a',
  6283. ];
  6284. $this->assertHtml($expected, $result);
  6285. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6286. $result = $this->Form->postLink(
  6287. 'Delete',
  6288. '/posts/delete/1',
  6289. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6290. );
  6291. $expected = [
  6292. 'form' => [
  6293. 'method' => 'post', 'action' => '/posts/delete/1',
  6294. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6295. ],
  6296. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6297. '/form',
  6298. 'a' => [
  6299. 'href' => '#',
  6300. 'data-confirm-message' => 'Confirm this deletion?',
  6301. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6302. ],
  6303. 'Delete',
  6304. '/a',
  6305. ];
  6306. $this->assertHtml($expected, $result);
  6307. }
  6308. /**
  6309. * testPostLinkWithQuery method
  6310. *
  6311. * Test postLink() with query string args.
  6312. */
  6313. public function testPostLinkWithQuery(): void
  6314. {
  6315. $result = $this->Form->postLink(
  6316. 'Delete',
  6317. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6318. );
  6319. $expected = [
  6320. 'form' => [
  6321. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6322. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6323. ],
  6324. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6325. '/form',
  6326. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6327. 'Delete',
  6328. '/a',
  6329. ];
  6330. $this->assertHtml($expected, $result);
  6331. }
  6332. /**
  6333. * testPostLinkWithData method
  6334. *
  6335. * Test postLink with additional data.
  6336. */
  6337. public function testPostLinkWithData(): void
  6338. {
  6339. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6340. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6341. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6342. $this->Form->create($entity);
  6343. $this->Form->end();
  6344. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6345. $this->assertStringContainsString(
  6346. '<input type="hidden" name="name" value="show"',
  6347. $result,
  6348. 'should not contain entity data.'
  6349. );
  6350. }
  6351. /**
  6352. * testPostLinkSecurityHash method
  6353. *
  6354. * Test that security hashes for postLink include the url.
  6355. */
  6356. public function testPostLinkSecurityHash(): void
  6357. {
  6358. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6359. $hash .= '%3Aid';
  6360. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6361. $result = $this->Form->postLink(
  6362. 'Delete',
  6363. '/posts/delete/1',
  6364. ['data' => ['id' => 1]]
  6365. );
  6366. $tokenDebug = urlencode(json_encode([
  6367. '/posts/delete/1',
  6368. [
  6369. 'id' => 1,
  6370. ],
  6371. [],
  6372. ]));
  6373. $expected = [
  6374. 'form' => [
  6375. 'method' => 'post', 'action' => '/posts/delete/1',
  6376. 'name', 'style' => 'display:none;',
  6377. ],
  6378. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6379. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6380. 'div' => ['style' => 'display:none;'],
  6381. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6382. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6383. ['input' => [
  6384. 'type' => 'hidden',
  6385. 'name' => '_Token[debug]',
  6386. 'value' => $tokenDebug,
  6387. 'autocomplete' => 'off',
  6388. ]],
  6389. '/div',
  6390. '/form',
  6391. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6392. 'Delete',
  6393. '/a',
  6394. ];
  6395. $this->assertHtml($expected, $result);
  6396. }
  6397. /**
  6398. * testPostLinkSecurityHashBlockMode method
  6399. *
  6400. * Test that postLink doesn't modify the fields in the containing form.
  6401. *
  6402. * postLink() calls inside open forms should not modify the field list
  6403. * for the form.
  6404. */
  6405. public function testPostLinkSecurityHashBlockMode(): void
  6406. {
  6407. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6408. $hash .= '%3A';
  6409. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6410. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6411. $this->Form->control('title');
  6412. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6413. $result = $this->View->fetch('postLink');
  6414. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6415. $this->assertEquals(['title'], $fields);
  6416. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6417. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6418. $reflect->setAccessible(true);
  6419. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6420. }
  6421. /**
  6422. * testPostLinkSecurityHashNoDebugMode method
  6423. *
  6424. * Test that security does not include debug token if debug is false.
  6425. */
  6426. public function testPostLinkSecurityHashNoDebugMode(): void
  6427. {
  6428. Configure::write('debug', false);
  6429. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6430. $hash .= '%3Aid';
  6431. $this->View->setRequest($this->View->getRequest()
  6432. ->withAttribute('formTokenData', ['key' => 'test']));
  6433. $result = $this->Form->postLink(
  6434. 'Delete',
  6435. '/posts/delete/1',
  6436. ['data' => ['id' => 1]]
  6437. );
  6438. $expected = [
  6439. 'form' => [
  6440. 'method' => 'post', 'action' => '/posts/delete/1',
  6441. 'name', 'style' => 'display:none;',
  6442. ],
  6443. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6444. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6445. 'div' => ['style' => 'display:none;'],
  6446. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6447. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6448. '/div',
  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. * testPostLinkNestedData method
  6458. *
  6459. * Test using postLink with N dimensional data.
  6460. */
  6461. public function testPostLinkNestedData(): void
  6462. {
  6463. $data = [
  6464. 'one' => [
  6465. 'two' => [
  6466. 3, 4, 5,
  6467. ],
  6468. ],
  6469. ];
  6470. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6471. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6472. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6473. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6474. }
  6475. /**
  6476. * testPostLinkAfterGetForm method
  6477. *
  6478. * Test creating postLinks after a GET form.
  6479. */
  6480. public function testPostLinkAfterGetForm(): void
  6481. {
  6482. $this->View->setRequest($this->View->getRequest()
  6483. ->withAttribute('csrfToken', 'testkey')
  6484. ->withAttribute('formTokenData', []));
  6485. $this->Form->create($this->article, ['type' => 'get']);
  6486. $this->Form->end();
  6487. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6488. $tokenDebug = urlencode(json_encode([
  6489. '/posts/delete/1',
  6490. [],
  6491. [],
  6492. ]));
  6493. $expected = [
  6494. 'form' => [
  6495. 'method' => 'post', 'action' => '/posts/delete/1',
  6496. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6497. ],
  6498. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6499. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6500. 'div' => ['style' => 'display:none;'],
  6501. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6502. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6503. ['input' => [
  6504. 'type' => 'hidden', 'name' => '_Token[debug]',
  6505. 'value' => $tokenDebug,
  6506. 'autocomplete' => 'off',
  6507. ]],
  6508. '/div',
  6509. '/form',
  6510. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6511. 'Delete',
  6512. '/a',
  6513. ];
  6514. $this->assertHtml($expected, $result);
  6515. }
  6516. /**
  6517. * testPostLinkFormBuffer method
  6518. *
  6519. * Test that postLink adds form tags to view block.
  6520. */
  6521. public function testPostLinkFormBuffer(): void
  6522. {
  6523. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6524. $expected = [
  6525. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6526. 'Delete',
  6527. '/a',
  6528. ];
  6529. $this->assertHtml($expected, $result);
  6530. $result = $this->View->fetch('postLink');
  6531. $expected = [
  6532. 'form' => [
  6533. 'method' => 'post', 'action' => '/posts/delete/1',
  6534. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6535. ],
  6536. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6537. '/form',
  6538. ];
  6539. $this->assertHtml($expected, $result);
  6540. $result = $this->Form->postLink(
  6541. 'Delete',
  6542. '/posts/delete/2',
  6543. ['block' => true, 'method' => 'DELETE']
  6544. );
  6545. $expected = [
  6546. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6547. 'Delete',
  6548. '/a',
  6549. ];
  6550. $this->assertHtml($expected, $result);
  6551. $result = $this->View->fetch('postLink');
  6552. $expected = [
  6553. 'form' => [
  6554. 'method' => 'post', 'action' => '/posts/delete/1',
  6555. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6556. ],
  6557. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6558. '/form',
  6559. [
  6560. 'form' => [
  6561. 'method' => 'post', 'action' => '/posts/delete/2',
  6562. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6563. ],
  6564. ],
  6565. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6566. '/form',
  6567. ];
  6568. $this->assertHtml($expected, $result);
  6569. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6570. $expected = [
  6571. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6572. 'Delete',
  6573. '/a',
  6574. ];
  6575. $this->assertHtml($expected, $result);
  6576. $result = $this->View->fetch('foobar');
  6577. $expected = [
  6578. 'form' => [
  6579. 'method' => 'post', 'action' => '/posts/delete/1',
  6580. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6581. ],
  6582. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6583. '/form',
  6584. ];
  6585. $this->assertHtml($expected, $result);
  6586. }
  6587. /**
  6588. * testSubmitButton method
  6589. */
  6590. public function testSubmitButton(): void
  6591. {
  6592. $result = $this->Form->submit('');
  6593. $expected = [
  6594. 'div' => ['class' => 'submit'],
  6595. 'input' => ['type' => 'submit', 'value' => ''],
  6596. '/div',
  6597. ];
  6598. $this->assertHtml($expected, $result);
  6599. $result = $this->Form->submit('Test Submit');
  6600. $expected = [
  6601. 'div' => ['class' => 'submit'],
  6602. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6603. '/div',
  6604. ];
  6605. $this->assertHtml($expected, $result);
  6606. $result = $this->Form->submit('Next >');
  6607. $expected = [
  6608. 'div' => ['class' => 'submit'],
  6609. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6610. '/div',
  6611. ];
  6612. $this->assertHtml($expected, $result);
  6613. $result = $this->Form->submit('Next >', ['escape' => false]);
  6614. $expected = [
  6615. 'div' => ['class' => 'submit'],
  6616. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6617. '/div',
  6618. ];
  6619. $this->assertHtml($expected, $result);
  6620. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6621. $expected = [
  6622. 'div' => ['class' => 'submit'],
  6623. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6624. '/div',
  6625. ];
  6626. $this->assertHtml($expected, $result);
  6627. }
  6628. /**
  6629. * testSubmitImage method
  6630. *
  6631. * Test image submit types.
  6632. */
  6633. public function testSubmitImage(): void
  6634. {
  6635. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6636. $expected = [
  6637. 'div' => ['class' => 'submit'],
  6638. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6639. '/div',
  6640. ];
  6641. $this->assertHtml($expected, $result);
  6642. $result = $this->Form->submit('/relative/cake.power.gif');
  6643. $expected = [
  6644. 'div' => ['class' => 'submit'],
  6645. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6646. '/div',
  6647. ];
  6648. $this->assertHtml($expected, $result);
  6649. $result = $this->Form->submit('cake.power.gif');
  6650. $expected = [
  6651. 'div' => ['class' => 'submit'],
  6652. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6653. '/div',
  6654. ];
  6655. $this->assertHtml($expected, $result);
  6656. $result = $this->Form->submit('Not.an.image');
  6657. $expected = [
  6658. 'div' => ['class' => 'submit'],
  6659. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6660. '/div',
  6661. ];
  6662. $this->assertHtml($expected, $result);
  6663. }
  6664. /**
  6665. * testSubmitUnlockedByDefault method
  6666. *
  6667. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6668. * be submitted at a time.
  6669. */
  6670. public function testSubmitUnlockedByDefault(): void
  6671. {
  6672. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6673. $this->Form->create();
  6674. $this->Form->submit('Go go');
  6675. $this->Form->submit('Save', ['name' => 'save']);
  6676. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6677. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6678. }
  6679. /**
  6680. * testSubmitImageTimestamp method
  6681. *
  6682. * Test submit image with timestamps.
  6683. */
  6684. public function testSubmitImageTimestamp(): void
  6685. {
  6686. Configure::write('Asset.timestamp', 'force');
  6687. $result = $this->Form->submit('cake.power.gif');
  6688. $expected = [
  6689. 'div' => ['class' => 'submit'],
  6690. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6691. '/div',
  6692. ];
  6693. $this->assertHtml($expected, $result);
  6694. }
  6695. /**
  6696. * testDateTimeWithGetForms method
  6697. *
  6698. * Test that datetime() works with GET style forms.
  6699. */
  6700. public function testDateTimeWithGetForms(): void
  6701. {
  6702. $this->Form->create($this->article, ['type' => 'get']);
  6703. $result = $this->Form->datetime('created');
  6704. $expected = [
  6705. 'input' => [
  6706. 'type' => 'datetime-local',
  6707. 'name' => 'created',
  6708. 'value' => '',
  6709. 'step' => '1',
  6710. ],
  6711. ];
  6712. $this->assertHtml($expected, $result);
  6713. $result = $this->Form->datetime('created', ['default' => true]);
  6714. $expected = [
  6715. 'input' => [
  6716. 'type' => 'datetime-local',
  6717. 'name' => 'created',
  6718. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6719. 'step' => '1',
  6720. ],
  6721. ];
  6722. $this->assertHtml($expected, $result);
  6723. }
  6724. /**
  6725. * Provides fractional schema types
  6726. *
  6727. * @return array
  6728. */
  6729. public function fractionalTypeProvider(): array
  6730. {
  6731. return [
  6732. ['datetimefractional'],
  6733. ['timestampfractional'],
  6734. ['timestamptimezone'],
  6735. ];
  6736. }
  6737. /**
  6738. * testDateTimeWithFractional method
  6739. *
  6740. * Test that datetime() works with datetimefractional.
  6741. *
  6742. * @dataProvider fractionalTypeProvider
  6743. */
  6744. public function testDateTimeWithFractional(string $type): void
  6745. {
  6746. $this->Form->create([
  6747. 'schema' => [
  6748. 'created' => ['type' => $type],
  6749. ],
  6750. ]);
  6751. $result = $this->Form->datetime('created', [
  6752. 'val' => new FrozenTime('2019-09-27 02:52:43.123'),
  6753. ]);
  6754. $expected = [
  6755. 'input' => [
  6756. 'type' => 'datetime-local',
  6757. 'name' => 'created',
  6758. 'value' => '2019-09-27T02:52:43.123',
  6759. 'step' => '0.001',
  6760. ],
  6761. ];
  6762. $this->assertHtml($expected, $result);
  6763. }
  6764. /**
  6765. * testControlWithFractional method
  6766. *
  6767. * Test that control() works with datetimefractional.
  6768. *
  6769. * @dataProvider fractionalTypeProvider
  6770. */
  6771. public function testControlWithFractional(string $type): void
  6772. {
  6773. $this->Form->create([
  6774. 'schema' => [
  6775. 'created' => ['type' => $type],
  6776. ],
  6777. ]);
  6778. $result = $this->Form->control('created', [
  6779. 'val' => new FrozenTime('2019-09-27 02:52:43.123'),
  6780. ]);
  6781. $expected = [
  6782. 'div' => ['class' => 'input datetime'],
  6783. 'label' => ['for' => 'created'],
  6784. 'Created',
  6785. '/label',
  6786. 'input' => [
  6787. 'type' => 'datetime-local',
  6788. 'name' => 'created',
  6789. 'id' => 'created',
  6790. 'value' => '2019-09-27T02:52:43.123',
  6791. 'step' => '0.001',
  6792. ],
  6793. '/div',
  6794. ];
  6795. $this->assertHtml($expected, $result);
  6796. }
  6797. /**
  6798. * testForMagicControlNonExistentNotValidated method
  6799. */
  6800. public function testForMagicControlNonExistentNotValidated(): void
  6801. {
  6802. $this->Form->create($this->article);
  6803. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6804. $result = $this->Form->control('nonexistent_not_validated');
  6805. $expected = [
  6806. 'label' => ['for' => 'nonexistent-not-validated'],
  6807. 'Nonexistent Not Validated',
  6808. '/label',
  6809. 'input' => [
  6810. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6811. 'id' => 'nonexistent-not-validated',
  6812. ],
  6813. ];
  6814. $this->assertHtml($expected, $result);
  6815. $result = $this->Form->control('nonexistent_not_validated', [
  6816. 'val' => 'my value',
  6817. ]);
  6818. $expected = [
  6819. 'label' => ['for' => 'nonexistent-not-validated'],
  6820. 'Nonexistent Not Validated',
  6821. '/label',
  6822. 'input' => [
  6823. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6824. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6825. ],
  6826. ];
  6827. $this->assertHtml($expected, $result);
  6828. $this->View->setRequest(
  6829. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6830. );
  6831. $this->Form->create($this->article);
  6832. $result = $this->Form->control('nonexistent_not_validated');
  6833. $expected = [
  6834. 'label' => ['for' => 'nonexistent-not-validated'],
  6835. 'Nonexistent Not Validated',
  6836. '/label',
  6837. 'input' => [
  6838. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6839. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6840. ],
  6841. ];
  6842. $this->assertHtml($expected, $result);
  6843. }
  6844. /**
  6845. * testFormMagicControlLabel method
  6846. */
  6847. public function testFormMagicControlLabel(): void
  6848. {
  6849. $this->getTableLocator()->get('Contacts', [
  6850. 'className' => ContactsTable::class,
  6851. ]);
  6852. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6853. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6854. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6855. $expected = [
  6856. 'label' => ['for' => 'contacts-name'],
  6857. 'My label',
  6858. '/label',
  6859. 'input' => [
  6860. 'type' => 'text',
  6861. 'name' => 'Contacts[name]',
  6862. 'id' => 'contacts-name',
  6863. 'maxlength' => '255',
  6864. ],
  6865. ];
  6866. $this->assertHtml($expected, $result);
  6867. $result = $this->Form->control('name', [
  6868. 'label' => ['class' => 'mandatory'],
  6869. ]);
  6870. $expected = [
  6871. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6872. 'Name',
  6873. '/label',
  6874. 'input' => [
  6875. 'type' => 'text', 'name' => 'name',
  6876. 'id' => 'name', 'maxlength' => '255',
  6877. ],
  6878. ];
  6879. $this->assertHtml($expected, $result);
  6880. $result = $this->Form->control('name', [
  6881. 'div' => false,
  6882. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6883. ]);
  6884. $expected = [
  6885. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6886. 'My label',
  6887. '/label',
  6888. 'input' => [
  6889. 'type' => 'text', 'name' => 'name',
  6890. 'id' => 'name', 'maxlength' => '255',
  6891. ],
  6892. ];
  6893. $this->assertHtml($expected, $result);
  6894. $result = $this->Form->control('Contact.name', [
  6895. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  6896. ]);
  6897. $expected = [
  6898. 'label' => ['for' => 'my_id'],
  6899. 'Name',
  6900. '/label',
  6901. 'input' => [
  6902. 'type' => 'text', 'name' => 'Contact[name]',
  6903. 'id' => 'my_id', 'maxlength' => '255',
  6904. ],
  6905. ];
  6906. $this->assertHtml($expected, $result);
  6907. $result = $this->Form->control('1.id');
  6908. $expected = ['input' => [
  6909. 'type' => 'hidden', 'name' => '1[id]',
  6910. 'id' => '1-id',
  6911. ]];
  6912. $this->assertHtml($expected, $result);
  6913. $result = $this->Form->control('1.name');
  6914. $expected = [
  6915. 'label' => ['for' => '1-name'],
  6916. 'Name',
  6917. '/label',
  6918. 'input' => [
  6919. 'type' => 'text', 'name' => '1[name]',
  6920. 'id' => '1-name', 'maxlength' => '255',
  6921. ],
  6922. ];
  6923. $this->assertHtml($expected, $result);
  6924. }
  6925. /**
  6926. * testFormEnd method
  6927. */
  6928. public function testFormEnd(): void
  6929. {
  6930. $this->assertSame('</form>', $this->Form->end());
  6931. }
  6932. /**
  6933. * testMultiRecordForm method
  6934. *
  6935. * Test the generation of fields for a multi record form.
  6936. */
  6937. public function testMultiRecordForm(): void
  6938. {
  6939. $articles = $this->getTableLocator()->get('Articles');
  6940. $articles->hasMany('Comments');
  6941. $comment = new Entity(['comment' => 'Value']);
  6942. $article = new Article(['comments' => [$comment]]);
  6943. $this->Form->create([$article]);
  6944. $result = $this->Form->control('0.comments.1.comment');
  6945. // phpcs:disable
  6946. $expected = [
  6947. 'div' => ['class' => 'input textarea'],
  6948. 'label' => ['for' => '0-comments-1-comment'],
  6949. 'Comment',
  6950. '/label',
  6951. 'textarea' => [
  6952. 'name',
  6953. 'id' => '0-comments-1-comment',
  6954. 'rows' => 5
  6955. ],
  6956. '/textarea',
  6957. '/div'
  6958. ];
  6959. // phpcs:enable
  6960. $this->assertHtml($expected, $result);
  6961. $result = $this->Form->control('0.comments.0.comment');
  6962. // phpcs:disable
  6963. $expected = [
  6964. 'div' => ['class' => 'input textarea'],
  6965. 'label' => ['for' => '0-comments-0-comment'],
  6966. 'Comment',
  6967. '/label',
  6968. 'textarea' => [
  6969. 'name',
  6970. 'id' => '0-comments-0-comment',
  6971. 'rows' => 5
  6972. ],
  6973. 'Value',
  6974. '/textarea',
  6975. '/div'
  6976. ];
  6977. // phpcs:enable
  6978. $this->assertHtml($expected, $result);
  6979. $comment->setError('comment', ['Not valid']);
  6980. $result = $this->Form->control('0.comments.0.comment');
  6981. // phpcs:disable
  6982. $expected = [
  6983. 'div' => ['class' => 'input textarea error'],
  6984. 'label' => ['for' => '0-comments-0-comment'],
  6985. 'Comment',
  6986. '/label',
  6987. 'textarea' => [
  6988. 'name',
  6989. 'class' => 'form-error',
  6990. 'id' => '0-comments-0-comment',
  6991. 'aria-invalid' => 'true',
  6992. 'aria-describedby' => '0-comments-0-comment-error',
  6993. 'rows' => 5
  6994. ],
  6995. 'Value',
  6996. '/textarea',
  6997. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  6998. 'Not valid',
  6999. '/div',
  7000. '/div'
  7001. ];
  7002. // phpcs:enable
  7003. $this->assertHtml($expected, $result);
  7004. $this->getTableLocator()->get('Comments')
  7005. ->getValidator('default')
  7006. ->allowEmptyString('comment', null, false);
  7007. $result = $this->Form->control('0.comments.1.comment');
  7008. // phpcs:disable
  7009. $expected = [
  7010. 'div' => ['class' => 'input textarea required'],
  7011. 'label' => ['for' => '0-comments-1-comment'],
  7012. 'Comment',
  7013. '/label',
  7014. 'textarea' => [
  7015. 'name',
  7016. 'aria-required' => 'true',
  7017. 'required' => 'required',
  7018. 'id' => '0-comments-1-comment',
  7019. 'rows' => 5,
  7020. 'data-validity-message' => 'This field cannot be left empty',
  7021. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7022. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7023. ],
  7024. '/textarea',
  7025. '/div'
  7026. ];
  7027. // phpcs:enable
  7028. $this->assertHtml($expected, $result);
  7029. }
  7030. /**
  7031. * testHtml5Controls method
  7032. *
  7033. * Test that some html5 inputs + FormHelper::__call() work.
  7034. */
  7035. public function testHtml5Controls(): void
  7036. {
  7037. $result = $this->Form->email('User.email');
  7038. $expected = [
  7039. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7040. ];
  7041. $this->assertHtml($expected, $result);
  7042. $result = $this->Form->search('User.query');
  7043. $expected = [
  7044. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7045. ];
  7046. $this->assertHtml($expected, $result);
  7047. $result = $this->Form->search('User.query', ['value' => 'test']);
  7048. $expected = [
  7049. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7050. ];
  7051. $this->assertHtml($expected, $result);
  7052. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7053. $expected = [
  7054. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7055. ];
  7056. $this->assertHtml($expected, $result);
  7057. }
  7058. /**
  7059. * testHtml5ControlWithControl method
  7060. *
  7061. * Test accessing html5 inputs through control().
  7062. */
  7063. public function testHtml5ControlWithControl(): void
  7064. {
  7065. $this->Form->create();
  7066. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7067. $result = $this->Form->control('website', [
  7068. 'type' => 'url',
  7069. 'val' => 'http://domain.tld',
  7070. 'label' => false,
  7071. ]);
  7072. $expected = [
  7073. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7074. ];
  7075. $this->assertHtml($expected, $result);
  7076. }
  7077. /**
  7078. * testHtml5ControlException method
  7079. *
  7080. * Test errors when field name is missing.
  7081. */
  7082. public function testHtml5ControlException(): void
  7083. {
  7084. $this->expectException(CakeException::class);
  7085. $this->Form->email();
  7086. }
  7087. /**
  7088. * tests fields that are required use custom validation messages
  7089. */
  7090. public function testHtml5ErrorMessage(): void
  7091. {
  7092. $this->Form->setConfig('autoSetCustomValidity', true);
  7093. $validator = (new Validator())
  7094. ->notEmptyString('email', 'Custom error message')
  7095. ->requirePresence('password')
  7096. ->alphaNumeric('password')
  7097. ->notBlank('phone');
  7098. $table = $this->getTableLocator()->get('Contacts', [
  7099. 'className' => ContactsTable::class,
  7100. ]);
  7101. $table->setValidator('default', $validator);
  7102. $contact = new Entity();
  7103. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7104. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7105. $result = $this->Form->control('password');
  7106. $expected = [
  7107. 'label' => ['for' => 'password'],
  7108. 'Password',
  7109. '/label',
  7110. 'input' => [
  7111. 'aria-required' => 'true',
  7112. 'id' => 'password',
  7113. 'name' => 'password',
  7114. 'type' => 'password',
  7115. 'value' => '',
  7116. 'required' => 'required',
  7117. 'data-validity-message' => 'This field cannot be left empty',
  7118. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7119. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7120. ],
  7121. ];
  7122. $this->assertHtml($expected, $result);
  7123. $result = $this->Form->control('phone');
  7124. $expected = [
  7125. 'label' => ['for' => 'phone'],
  7126. 'Phone',
  7127. '/label',
  7128. 'input' => [
  7129. 'aria-required' => 'true',
  7130. 'id' => 'phone',
  7131. 'name' => 'phone',
  7132. 'type' => 'tel',
  7133. 'value' => '',
  7134. 'maxlength' => 255,
  7135. 'required' => 'required',
  7136. 'data-validity-message' => 'This field cannot be left empty',
  7137. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7138. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7139. ],
  7140. ];
  7141. $this->assertHtml($expected, $result);
  7142. $result = $this->Form->control('email');
  7143. $expected = [
  7144. 'label' => ['for' => 'email'],
  7145. 'Email',
  7146. '/label',
  7147. 'input' => [
  7148. 'aria-required' => 'true',
  7149. 'id' => 'email',
  7150. 'name' => 'email',
  7151. 'type' => 'email',
  7152. 'value' => '',
  7153. 'maxlength' => 255,
  7154. 'required' => 'required',
  7155. 'data-validity-message' => 'Custom error message',
  7156. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7157. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7158. ],
  7159. ];
  7160. $this->assertHtml($expected, $result);
  7161. }
  7162. /**
  7163. * tests that custom validation messages are in templateVars
  7164. */
  7165. public function testHtml5ErrorMessageInTemplateVars(): void
  7166. {
  7167. $validator = (new Validator())
  7168. ->notEmptyString('email', 'Custom error "message" & entities')
  7169. ->requirePresence('password')
  7170. ->alphaNumeric('password')
  7171. ->notBlank('phone');
  7172. $table = $this->getTableLocator()->get('Contacts', [
  7173. 'className' => ContactsTable::class,
  7174. ]);
  7175. $table->setValidator('default', $validator);
  7176. $contact = new Entity();
  7177. $this->Form->setConfig('autoSetCustomValidity', false);
  7178. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7179. $this->Form->setTemplates([
  7180. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7181. 'inputContainer' => '{{content}}',
  7182. ]);
  7183. $result = $this->Form->control('password');
  7184. $expected = [
  7185. 'label' => ['for' => 'password'],
  7186. 'Password',
  7187. '/label',
  7188. 'input' => [
  7189. 'aria-required' => 'true',
  7190. 'id' => 'password',
  7191. 'name' => 'password',
  7192. 'type' => 'password',
  7193. 'value' => '',
  7194. 'required' => 'required',
  7195. 'data-message' => 'This field cannot be left empty',
  7196. ],
  7197. ];
  7198. $this->assertHtml($expected, $result);
  7199. $result = $this->Form->control('phone');
  7200. $expected = [
  7201. 'label' => ['for' => 'phone'],
  7202. 'Phone',
  7203. '/label',
  7204. 'input' => [
  7205. 'aria-required' => 'true',
  7206. 'id' => 'phone',
  7207. 'name' => 'phone',
  7208. 'type' => 'tel',
  7209. 'value' => '',
  7210. 'maxlength' => 255,
  7211. 'required' => 'required',
  7212. 'data-message' => 'This field cannot be left empty',
  7213. ],
  7214. ];
  7215. $this->assertHtml($expected, $result);
  7216. $result = $this->Form->control('email', [
  7217. 'templateVars' => [
  7218. 'custom' => 'data-custom="1"',
  7219. ],
  7220. ]);
  7221. $expected = [
  7222. 'label' => ['for' => 'email'],
  7223. 'Email',
  7224. '/label',
  7225. 'input' => [
  7226. 'aria-required' => 'true',
  7227. 'id' => 'email',
  7228. 'name' => 'email',
  7229. 'type' => 'email',
  7230. 'value' => '',
  7231. 'maxlength' => 255,
  7232. 'required' => 'required',
  7233. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7234. 'data-custom' => '1',
  7235. ],
  7236. ];
  7237. $this->assertHtml($expected, $result);
  7238. $this->Form->setConfig('autoSetCustomValidity', true);
  7239. }
  7240. /**
  7241. * testRequiredAttribute method
  7242. *
  7243. * Tests that formhelper sets required attributes.
  7244. */
  7245. public function testRequiredAttribute(): void
  7246. {
  7247. $this->article['required'] = [
  7248. 'title' => true,
  7249. 'body' => false,
  7250. ];
  7251. $this->Form->create($this->article);
  7252. $result = $this->Form->control('title');
  7253. $expected = [
  7254. 'div' => ['class' => 'input text required'],
  7255. 'label' => ['for' => 'title'],
  7256. 'Title',
  7257. '/label',
  7258. 'input' => [
  7259. 'type' => 'text',
  7260. 'name' => 'title',
  7261. 'id' => 'title',
  7262. 'aria-required' => 'true',
  7263. 'required' => 'required',
  7264. 'data-validity-message' => 'This field cannot be left empty',
  7265. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7266. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7267. ],
  7268. '/div',
  7269. ];
  7270. $this->assertHtml($expected, $result);
  7271. $result = $this->Form->control('title', ['required' => false]);
  7272. $this->assertStringNotContainsString('required', $result);
  7273. $result = $this->Form->control('body');
  7274. $expected = [
  7275. 'div' => ['class' => 'input text'],
  7276. 'label' => ['for' => 'body'],
  7277. 'Body',
  7278. '/label',
  7279. 'input' => [
  7280. 'type' => 'text',
  7281. 'name' => 'body',
  7282. 'id' => 'body',
  7283. ],
  7284. '/div',
  7285. ];
  7286. $this->assertHtml($expected, $result);
  7287. $result = $this->Form->control('body', ['required' => true]);
  7288. $this->assertStringContainsString('required', $result);
  7289. }
  7290. /**
  7291. * testControlsNotNested method
  7292. *
  7293. * Tests that it is possible to put inputs outside of the label.
  7294. */
  7295. public function testControlsNotNested(): void
  7296. {
  7297. $this->Form->setTemplates([
  7298. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7299. 'formGroup' => '{{input}}{{label}}',
  7300. ]);
  7301. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7302. $expected = [
  7303. 'div' => ['class' => 'input checkbox'],
  7304. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7305. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7306. 'label' => ['for' => 'foo'],
  7307. 'Foo',
  7308. '/label',
  7309. '/div',
  7310. ];
  7311. $this->assertHtml($expected, $result);
  7312. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7313. $expected = [
  7314. 'div' => ['class' => 'input checkbox'],
  7315. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7316. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7317. '/div',
  7318. ];
  7319. $this->assertHtml($expected, $result);
  7320. $result = $this->Form->control('confirm', [
  7321. 'type' => 'radio',
  7322. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7323. ]);
  7324. $expected = [
  7325. 'div' => ['class' => 'input radio'],
  7326. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '', 'id' => 'confirm']],
  7327. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7328. ['label' => ['for' => 'confirm-y']],
  7329. 'Yes',
  7330. '/label',
  7331. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7332. ['label' => ['for' => 'confirm-n']],
  7333. 'No',
  7334. '/label',
  7335. '<label',
  7336. 'Confirm',
  7337. '/label',
  7338. '/div',
  7339. ];
  7340. $this->assertHtml($expected, $result);
  7341. $result = $this->Form->select('category', ['1', '2'], [
  7342. 'multiple' => 'checkbox',
  7343. 'name' => 'fish',
  7344. ]);
  7345. $expected = [
  7346. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
  7347. ['div' => ['class' => 'checkbox']],
  7348. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7349. ['label' => ['for' => 'fish-0']],
  7350. '1',
  7351. '/label',
  7352. '/div',
  7353. ['div' => ['class' => 'checkbox']],
  7354. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7355. ['label' => ['for' => 'fish-1']],
  7356. '2',
  7357. '/label',
  7358. '/div',
  7359. ];
  7360. $this->assertHtml($expected, $result);
  7361. }
  7362. /**
  7363. * testControlContainerTemplates method
  7364. *
  7365. * Test that *Container templates are used by input.
  7366. */
  7367. public function testControlContainerTemplates(): void
  7368. {
  7369. $this->Form->setTemplates([
  7370. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7371. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7372. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7373. ]);
  7374. $this->article['errors'] = [
  7375. 'Article' => ['published' => 'error message'],
  7376. ];
  7377. $this->Form->create($this->article);
  7378. $result = $this->Form->control('accept', [
  7379. 'type' => 'checkbox',
  7380. ]);
  7381. $expected = [
  7382. 'div' => ['class' => 'check'],
  7383. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7384. 'label' => ['for' => 'accept'],
  7385. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7386. 'Accept',
  7387. '/label',
  7388. '/div',
  7389. ];
  7390. $this->assertHtml($expected, $result);
  7391. $result = $this->Form->control('accept', [
  7392. 'type' => 'radio',
  7393. 'options' => ['Y', 'N'],
  7394. ]);
  7395. $this->assertStringContainsString('<div class="rad">', $result);
  7396. $result = $this->Form->control('Article.published', [
  7397. 'type' => 'radio',
  7398. 'options' => ['Y', 'N'],
  7399. ]);
  7400. $this->assertStringContainsString('<div class="rad err">', $result);
  7401. }
  7402. /**
  7403. * testFormGroupTemplates method
  7404. *
  7405. * Test that *Container templates are used by input.
  7406. */
  7407. public function testFormGroupTemplates(): void
  7408. {
  7409. $this->Form->setTemplates([
  7410. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7411. ]);
  7412. $this->Form->create($this->article);
  7413. $result = $this->Form->control('accept', [
  7414. 'type' => 'radio',
  7415. 'options' => ['Y', 'N'],
  7416. ]);
  7417. $this->assertStringContainsString('<div class="radio">', $result);
  7418. }
  7419. /**
  7420. * testResetTemplates method
  7421. *
  7422. * Test resetting templates.
  7423. */
  7424. public function testResetTemplates(): void
  7425. {
  7426. $this->Form->setTemplates(['input' => '<input/>']);
  7427. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7428. $this->Form->resetTemplates();
  7429. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7430. }
  7431. /**
  7432. * testContext method
  7433. *
  7434. * Test the context method.
  7435. */
  7436. public function testContext(): void
  7437. {
  7438. $result = $this->Form->context();
  7439. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7440. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7441. $this->assertSame($mock, $this->Form->context($mock));
  7442. $this->assertSame($mock, $this->Form->context());
  7443. }
  7444. /**
  7445. * testAutoDomId method
  7446. */
  7447. public function testAutoDomId(): void
  7448. {
  7449. $result = $this->Form->text('field', ['id' => true]);
  7450. $expected = [
  7451. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7452. ];
  7453. $this->assertHtml($expected, $result);
  7454. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7455. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7456. $expected = [
  7457. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '', 'id' => 'field'],
  7458. ['label' => ['for' => 'field-0']],
  7459. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7460. 'option A',
  7461. '/label',
  7462. ['label' => ['for' => 'field-1']],
  7463. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7464. 'option B',
  7465. '/label',
  7466. ];
  7467. $this->assertHtml($expected, $result);
  7468. $result = $this->Form->select(
  7469. 'multi_field',
  7470. ['first', 'second'],
  7471. ['multiple' => 'checkbox', 'id' => true]
  7472. );
  7473. $expected = [
  7474. 'input' => [
  7475. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '', 'id' => 'multi-field',
  7476. ],
  7477. ['div' => ['class' => 'checkbox']],
  7478. ['label' => ['for' => 'multi-field-0']],
  7479. ['input' => [
  7480. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7481. 'value' => '0', 'id' => 'multi-field-0',
  7482. ]],
  7483. 'first',
  7484. '/label',
  7485. '/div',
  7486. ['div' => ['class' => 'checkbox']],
  7487. ['label' => ['for' => 'multi-field-1']],
  7488. ['input' => [
  7489. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7490. 'value' => '1', 'id' => 'multi-field-1',
  7491. ]],
  7492. 'second',
  7493. '/label',
  7494. '/div',
  7495. ];
  7496. $this->assertHtml($expected, $result);
  7497. }
  7498. /**
  7499. * Test the basic setters and getters for value sources
  7500. */
  7501. public function testFormValueSourcesSettersGetters(): void
  7502. {
  7503. $this->View->setRequest($this->View->getRequest()
  7504. ->withData('id', '1')
  7505. ->withQueryParams(['id' => '2']));
  7506. $expected = ['data', 'context'];
  7507. $result = $this->Form->getValueSources();
  7508. $this->assertEquals($expected, $result);
  7509. $this->Form->setValueSources(['context']);
  7510. $result = $this->Form->getSourceValue('id');
  7511. $this->assertNull($result);
  7512. $this->Form->setValueSources('query');
  7513. $expected = ['query'];
  7514. $result = $this->Form->getValueSources();
  7515. $this->assertEquals($expected, $result);
  7516. $expected = '2';
  7517. $result = $this->Form->getSourceValue('id');
  7518. $this->assertSame($expected, $result);
  7519. $this->Form->setValueSources(['data']);
  7520. $expected = '1';
  7521. $result = $this->Form->getSourceValue('id');
  7522. $this->assertSame($expected, $result);
  7523. $this->Form->setValueSources(['query', 'data']);
  7524. $expected = '2';
  7525. $result = $this->Form->getSourceValue('id');
  7526. $this->assertSame($expected, $result);
  7527. }
  7528. public function testValueSourcesValidation(): void
  7529. {
  7530. $this->expectException(InvalidArgumentException::class);
  7531. $this->expectExceptionMessage('Invalid value source(s): invalid, foo. Valid values are: context, data, query');
  7532. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7533. }
  7534. /**
  7535. * Tests the different input rendering values based on sources values switching
  7536. */
  7537. public function testFormValueSourcesSingleSwitchRendering(): void
  7538. {
  7539. $articles = $this->getTableLocator()->get('Articles');
  7540. $article = new Article();
  7541. $articles->patchEntity($article, ['id' => '3']);
  7542. $this->Form->create($article);
  7543. $this->Form->setValueSources(['context']);
  7544. $result = $this->Form->control('id');
  7545. $expected = [
  7546. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7547. ];
  7548. $this->assertHtml($expected, $result);
  7549. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7550. $this->Form->setValueSources(['query']);
  7551. $result = $this->Form->control('id');
  7552. $expected = [
  7553. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7554. ];
  7555. $this->assertHtml($expected, $result);
  7556. $this->View->setRequest($this->View->getRequest()
  7557. ->withQueryParams(['id' => '5a'])
  7558. ->withData('id', '5b'));
  7559. $this->Form->setValueSources(['context']);
  7560. $this->Form->create($article);
  7561. $result = $this->Form->control('id');
  7562. $expected = [
  7563. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7564. ];
  7565. $this->assertHtml($expected, $result);
  7566. $this->Form->setValueSources(['data']);
  7567. $this->Form->create($article);
  7568. $result = $this->Form->control('id');
  7569. $expected = [
  7570. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7571. ];
  7572. $this->assertHtml($expected, $result);
  7573. $this->Form->setValueSources(['query']);
  7574. $result = $this->Form->control('id');
  7575. $expected = [
  7576. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7577. ];
  7578. $this->assertHtml($expected, $result);
  7579. }
  7580. /**
  7581. * Tests the different input rendering values based on sources values switching while supplying
  7582. * an entity (base context) and multiple sources (such as data, query)
  7583. */
  7584. public function testFormValueSourcesListSwitchRendering(): void
  7585. {
  7586. $articles = $this->getTableLocator()->get('Articles');
  7587. $article = new Article();
  7588. $articles->patchEntity($article, ['id' => '3']);
  7589. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7590. $this->Form->create($article);
  7591. $this->Form->setValueSources(['context', 'query']);
  7592. $result = $this->Form->control('id');
  7593. $expected = [
  7594. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7595. ];
  7596. $this->assertHtml($expected, $result);
  7597. $this->Form->setValueSources(['query', 'context']);
  7598. $result = $this->Form->control('id');
  7599. $expected = [
  7600. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7601. ];
  7602. $this->assertHtml($expected, $result);
  7603. $this->Form->setValueSources(['data', 'query', 'context']);
  7604. $result = $this->Form->control('id');
  7605. $expected = [
  7606. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7607. ];
  7608. $this->assertHtml($expected, $result);
  7609. $this->View->setRequest($this->View->getRequest()
  7610. ->withData('id', '8')
  7611. ->withQueryParams(['id' => '9']));
  7612. $this->Form->setValueSources(['data', 'query', 'context']);
  7613. $result = $this->Form->control('id');
  7614. $expected = [
  7615. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7616. ];
  7617. $this->assertHtml($expected, $result);
  7618. }
  7619. /**
  7620. * Test the different form input renderings based on values sources switchings through form options
  7621. */
  7622. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7623. {
  7624. $articles = $this->getTableLocator()->get('Articles');
  7625. $article = new Article();
  7626. $articles->patchEntity($article, ['id' => '3']);
  7627. $this->View->setRequest(
  7628. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7629. );
  7630. $this->Form->create($article, ['valueSources' => 'query']);
  7631. $result = $this->Form->control('id');
  7632. $expected = [
  7633. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7634. ];
  7635. $this->assertHtml($expected, $result);
  7636. $result = $this->Form->getSourceValue('id');
  7637. $this->assertSame('5', $result);
  7638. $this->Form->setValueSources(['context']);
  7639. $this->Form->create($article, ['valueSources' => 'query']);
  7640. $result = $this->Form->control('id');
  7641. $expected = [
  7642. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7643. ];
  7644. $this->assertHtml($expected, $result);
  7645. $result = $this->Form->getSourceValue('id');
  7646. $this->assertSame('5', $result);
  7647. $this->Form->setValueSources(['query']);
  7648. $this->Form->create($article, ['valueSources' => 'data']);
  7649. $result = $this->Form->control('id');
  7650. $expected = [
  7651. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7652. ];
  7653. $this->assertHtml($expected, $result);
  7654. $result = $this->Form->getSourceValue('id');
  7655. $this->assertSame('4', $result);
  7656. $this->Form->setValueSources(['query']);
  7657. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7658. $result = $this->Form->control('id');
  7659. $expected = [
  7660. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7661. ];
  7662. $this->assertHtml($expected, $result);
  7663. $result = $this->Form->getSourceValue('id');
  7664. $this->assertSame(3, $result);
  7665. }
  7666. /**
  7667. * Test the different form input renderings based on values sources switchings through form options
  7668. */
  7669. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7670. {
  7671. $articles = $this->getTableLocator()->get('Articles');
  7672. $article = new Article();
  7673. $articles->patchEntity($article, ['id' => '3']);
  7674. $this->View->setRequest(
  7675. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7676. );
  7677. $this->Form->setValueSources(['context'])
  7678. ->create($article, ['valueSources' => ['query', 'data']]);
  7679. $result = $this->Form->control('id');
  7680. $expected = [
  7681. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7682. ];
  7683. $this->assertHtml($expected, $result);
  7684. $result = $this->Form->getSourceValue('id');
  7685. $this->assertSame('11', $result);
  7686. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7687. $this->Form->setValueSources(['context'])
  7688. ->create($article, ['valueSources' => ['query', 'data']]);
  7689. $result = $this->Form->control('id');
  7690. $expected = [
  7691. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7692. ];
  7693. $this->assertHtml($expected, $result);
  7694. $result = $this->Form->getSourceValue('id');
  7695. $this->assertSame('10', $result);
  7696. }
  7697. /**
  7698. * Test the different form values sources resetting through From::end();
  7699. */
  7700. public function testFormValueSourcesResetViaEnd(): void
  7701. {
  7702. $expected = ['data', 'context'];
  7703. $result = $this->Form->getValueSources();
  7704. $this->assertEquals($expected, $result);
  7705. $expected = ['query', 'context', 'data'];
  7706. $this->Form->setValueSources(['query', 'context', 'data']);
  7707. $result = $this->Form->getValueSources();
  7708. $this->assertEquals($expected, $result);
  7709. $this->Form->create();
  7710. $result = $this->Form->getValueSources();
  7711. $this->assertEquals($expected, $result);
  7712. $this->Form->end();
  7713. $result = $this->Form->getValueSources();
  7714. $this->assertEquals(['data', 'context'], $result);
  7715. }
  7716. /**
  7717. * Test sources values defaults handling
  7718. */
  7719. public function testFormValueSourcesDefaults(): void
  7720. {
  7721. $this->View->setRequest(
  7722. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7723. );
  7724. $this->Form->create();
  7725. $result = $this->Form->password('password');
  7726. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7727. $this->assertHtml($expected, $result);
  7728. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7729. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7730. $this->assertHtml($expected, $result);
  7731. $this->Form->setValueSources('query');
  7732. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7733. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7734. $this->assertHtml($expected, $result);
  7735. $this->Form->setValueSources('data');
  7736. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7737. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7738. $this->assertHtml($expected, $result);
  7739. }
  7740. /**
  7741. * Test sources values schema defaults handling
  7742. */
  7743. public function testSourcesValueDoesntExistPassThrough(): void
  7744. {
  7745. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7746. $articles = $this->getTableLocator()->get('Articles');
  7747. $entity = $articles->newEmptyEntity();
  7748. $this->Form->create($entity);
  7749. $this->Form->setValueSources(['query', 'context']);
  7750. $result = $this->Form->getSourceValue('category');
  7751. $this->assertSame('sesame-cookies', $result);
  7752. $this->Form->setValueSources(['context', 'query']);
  7753. $result = $this->Form->getSourceValue('category');
  7754. $this->assertSame('sesame-cookies', $result);
  7755. }
  7756. /**
  7757. * testNestedLabelInput method
  7758. *
  7759. * Test the `nestedInput` parameter
  7760. */
  7761. public function testNestedLabelInput(): void
  7762. {
  7763. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7764. $expected = [
  7765. 'div' => ['class' => 'input text'],
  7766. 'label' => ['for' => 'foo'],
  7767. ['input' => [
  7768. 'type' => 'text',
  7769. 'name' => 'foo',
  7770. 'id' => 'foo',
  7771. ]],
  7772. 'Foo',
  7773. '/label',
  7774. '/div',
  7775. ];
  7776. $this->assertHtml($expected, $result);
  7777. }
  7778. /**
  7779. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7780. *
  7781. * This test makes sure `false` excludes the label from the render
  7782. */
  7783. public function testControlLabelManipulationDisableLabels(): void
  7784. {
  7785. $result = $this->Form->control('test', [
  7786. 'type' => 'radio',
  7787. 'options' => ['A', 'B'],
  7788. 'labelOptions' => false,
  7789. ]);
  7790. $expected = [
  7791. ['div' => ['class' => 'input radio']],
  7792. '<label',
  7793. 'Test',
  7794. '/label',
  7795. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7796. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7797. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7798. '/div',
  7799. ];
  7800. $this->assertHtml($expected, $result);
  7801. $result = $this->Form->control('checkbox1', [
  7802. 'label' => 'My checkboxes',
  7803. 'multiple' => 'checkbox',
  7804. 'type' => 'select',
  7805. 'options' => [
  7806. ['text' => 'First Checkbox', 'value' => 1],
  7807. ['text' => 'Second Checkbox', 'value' => 2],
  7808. ],
  7809. 'labelOptions' => false,
  7810. ]);
  7811. $expected = [
  7812. ['div' => ['class' => 'input select']],
  7813. ['label' => ['for' => 'checkbox1']],
  7814. 'My checkboxes',
  7815. '/label',
  7816. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7817. ['div' => ['class' => 'checkbox']],
  7818. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7819. '/div',
  7820. ['div' => ['class' => 'checkbox']],
  7821. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7822. '/div',
  7823. '/div',
  7824. ];
  7825. $this->assertHtml($expected, $result);
  7826. }
  7827. /**
  7828. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7829. *
  7830. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7831. * added to the class if checked.
  7832. *
  7833. * Also checks to make sure any custom attributes are rendered correctly
  7834. */
  7835. public function testControlLabelManipulationRadios(): void
  7836. {
  7837. $result = $this->Form->control('test', [
  7838. 'type' => 'radio',
  7839. 'options' => ['A', 'B'],
  7840. 'labelOptions' => ['class' => 'custom-class'],
  7841. ]);
  7842. $expected = [
  7843. ['div' => ['class' => 'input radio']],
  7844. '<label',
  7845. 'Test',
  7846. '/label',
  7847. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7848. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7849. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7850. 'A',
  7851. '/label',
  7852. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7853. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7854. 'B',
  7855. '/label',
  7856. '/div',
  7857. ];
  7858. $this->assertHtml($expected, $result);
  7859. $result = $this->Form->control('test', [
  7860. 'type' => 'radio',
  7861. 'options' => ['A', 'B'],
  7862. 'value' => 1,
  7863. 'labelOptions' => ['class' => 'custom-class'],
  7864. ]);
  7865. $expected = [
  7866. ['div' => ['class' => 'input radio']],
  7867. '<label',
  7868. 'Test',
  7869. '/label',
  7870. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7871. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7872. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7873. 'A',
  7874. '/label',
  7875. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7876. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7877. 'B',
  7878. '/label',
  7879. '/div',
  7880. ];
  7881. $this->assertHtml($expected, $result);
  7882. $result = $this->Form->control('test', [
  7883. 'type' => 'radio',
  7884. 'options' => ['A', 'B'],
  7885. 'value' => 1,
  7886. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7887. ]);
  7888. $expected = [
  7889. ['div' => ['class' => 'input radio']],
  7890. '<label',
  7891. 'Test',
  7892. '/label',
  7893. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
  7894. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7895. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7896. 'A',
  7897. '/label',
  7898. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7899. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7900. 'B',
  7901. '/label',
  7902. '/div',
  7903. ];
  7904. $this->assertHtml($expected, $result);
  7905. $result = $this->Form->radio('test', ['A', 'B'], [
  7906. 'label' => [
  7907. 'class' => ['custom-class', 'another-class'],
  7908. 'data-name' => 'bob',
  7909. ],
  7910. 'value' => 1,
  7911. ]);
  7912. $expected = [
  7913. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test'],
  7914. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7915. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7916. 'A',
  7917. '/label',
  7918. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  7919. ['input' => [
  7920. 'type' => 'radio',
  7921. 'name' => 'test',
  7922. 'value' => '1',
  7923. 'id' => 'test-1',
  7924. 'checked' => 'checked',
  7925. ]],
  7926. 'B',
  7927. '/label',
  7928. ];
  7929. $this->assertHtml($expected, $result);
  7930. }
  7931. /**
  7932. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  7933. *
  7934. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7935. * added to the class if checked.
  7936. *
  7937. * Also checks to make sure any custom attributes are rendered correctly
  7938. */
  7939. public function testControlLabelManipulationCheckboxes(): void
  7940. {
  7941. $result = $this->Form->control('checkbox1', [
  7942. 'label' => 'My checkboxes',
  7943. 'multiple' => 'checkbox',
  7944. 'type' => 'select',
  7945. 'options' => [
  7946. ['text' => 'First Checkbox', 'value' => 1],
  7947. ['text' => 'Second Checkbox', 'value' => 2],
  7948. ],
  7949. 'labelOptions' => ['class' => 'custom-class'],
  7950. 'value' => ['1'],
  7951. ]);
  7952. $expected = [
  7953. ['div' => ['class' => 'input select']],
  7954. ['label' => ['for' => 'checkbox1']],
  7955. 'My checkboxes',
  7956. '/label',
  7957. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  7958. ['div' => ['class' => 'checkbox']],
  7959. ['label' => [
  7960. 'class' => 'custom-class selected',
  7961. 'for' => 'checkbox1-1',
  7962. ]],
  7963. ['input' => [
  7964. 'type' => 'checkbox',
  7965. 'name' => 'checkbox1[]',
  7966. 'value' => '1',
  7967. 'id' => 'checkbox1-1',
  7968. 'checked' => 'checked',
  7969. ]],
  7970. 'First Checkbox',
  7971. '/label',
  7972. '/div',
  7973. ['div' => ['class' => 'checkbox']],
  7974. ['label' => [
  7975. 'class' => 'custom-class',
  7976. 'for' => 'checkbox1-2',
  7977. ]],
  7978. ['input' => [
  7979. 'type' => 'checkbox',
  7980. 'name' => 'checkbox1[]',
  7981. 'value' => '2',
  7982. 'id' => 'checkbox1-2',
  7983. ]],
  7984. 'Second Checkbox',
  7985. '/label',
  7986. '/div',
  7987. '/div',
  7988. ];
  7989. $this->assertHtml($expected, $result);
  7990. $result = $this->Form->control('checkbox1', [
  7991. 'label' => 'My checkboxes',
  7992. 'multiple' => 'checkbox',
  7993. 'type' => 'select',
  7994. 'options' => [
  7995. ['text' => 'First Checkbox', 'value' => 1],
  7996. ['text' => 'Second Checkbox', 'value' => 2],
  7997. ],
  7998. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  7999. 'value' => ['1'],
  8000. ]);
  8001. $expected = [
  8002. ['div' => ['class' => 'input select']],
  8003. ['label' => ['for' => 'checkbox1']],
  8004. 'My checkboxes',
  8005. '/label',
  8006. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
  8007. ['div' => ['class' => 'checkbox']],
  8008. ['label' => [
  8009. 'class' => 'custom-class another-class selected',
  8010. 'data-name' => 'bob',
  8011. 'for' => 'checkbox1-1',
  8012. ]],
  8013. ['input' => [
  8014. 'type' => 'checkbox',
  8015. 'name' => 'checkbox1[]',
  8016. 'value' => '1',
  8017. 'id' => 'checkbox1-1',
  8018. 'checked' => 'checked',
  8019. ]],
  8020. 'First Checkbox',
  8021. '/label',
  8022. '/div',
  8023. ['div' => ['class' => 'checkbox']],
  8024. ['label' => [
  8025. 'class' => 'custom-class another-class',
  8026. 'data-name' => 'bob',
  8027. 'for' => 'checkbox1-2',
  8028. ]],
  8029. ['input' => [
  8030. 'type' => 'checkbox',
  8031. 'name' => 'checkbox1[]',
  8032. 'value' => '2',
  8033. 'id' => 'checkbox1-2',
  8034. ]],
  8035. 'Second Checkbox',
  8036. '/label',
  8037. '/div',
  8038. '/div',
  8039. ];
  8040. $this->assertHtml($expected, $result);
  8041. }
  8042. /**
  8043. * testControlMaxLengthArrayContext method
  8044. *
  8045. * Test control() with maxlength attribute in Array Context.
  8046. */
  8047. public function testControlMaxLengthArrayContext(): void
  8048. {
  8049. $this->article['schema'] = [
  8050. 'title' => ['length' => 10],
  8051. ];
  8052. $this->Form->create($this->article);
  8053. $result = $this->Form->control('title');
  8054. $expected = [
  8055. 'div' => ['class'],
  8056. 'label' => ['for'],
  8057. 'Title',
  8058. '/label',
  8059. 'input' => [
  8060. 'aria-required' => 'true',
  8061. 'id',
  8062. 'name' => 'title',
  8063. 'type' => 'text',
  8064. 'required' => 'required',
  8065. 'maxlength' => 10,
  8066. 'data-validity-message' => 'This field cannot be left empty',
  8067. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8068. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8069. ],
  8070. '/div',
  8071. ];
  8072. $this->assertHtml($expected, $result);
  8073. }
  8074. /**
  8075. * testControlMaxLengthEntityContext method
  8076. *
  8077. * Test control() with maxlength attribute in Entity Context.
  8078. */
  8079. public function testControlMaxLengthEntityContext(): void
  8080. {
  8081. $this->article['schema']['title']['length'] = 45;
  8082. $validator = new Validator();
  8083. $validator->maxLength('title', 10);
  8084. $article = new EntityContext(
  8085. [
  8086. 'entity' => new Entity($this->article),
  8087. 'table' => new Table([
  8088. 'alias' => 'Articles',
  8089. 'schema' => $this->article['schema'],
  8090. 'validator' => $validator,
  8091. ]),
  8092. ]
  8093. );
  8094. $this->Form->create($article);
  8095. $result = $this->Form->control('title');
  8096. $expected = [
  8097. 'div' => ['class'],
  8098. 'label' => ['for'],
  8099. 'Title',
  8100. '/label',
  8101. 'input' => [
  8102. 'aria-required' => 'true',
  8103. 'id',
  8104. 'name' => 'title',
  8105. 'type' => 'text',
  8106. 'required' => 'required',
  8107. 'maxlength' => 10,
  8108. 'data-validity-message' => 'This field cannot be left empty',
  8109. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8110. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8111. ],
  8112. '/div',
  8113. ];
  8114. $this->assertHtml($expected, $result);
  8115. $this->article['schema']['title']['length'] = 45;
  8116. $validator = new Validator();
  8117. $validator->maxLength('title', 55);
  8118. $article = new EntityContext(
  8119. [
  8120. 'entity' => new Entity($this->article),
  8121. 'table' => new Table([
  8122. 'schema' => $this->article['schema'],
  8123. 'validator' => $validator,
  8124. 'alias' => 'Articles',
  8125. ]),
  8126. ]
  8127. );
  8128. $this->Form->create($article);
  8129. $result = $this->Form->control('title');
  8130. $expected = [
  8131. 'div' => ['class'],
  8132. 'label' => ['for'],
  8133. 'Title',
  8134. '/label',
  8135. 'input' => [
  8136. 'aria-required' => 'true',
  8137. 'id',
  8138. 'name' => 'title',
  8139. 'type' => 'text',
  8140. 'required' => 'required',
  8141. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8142. 'data-validity-message' => 'This field cannot be left empty',
  8143. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8144. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8145. ],
  8146. '/div',
  8147. ];
  8148. $this->assertHtml($expected, $result);
  8149. $this->article['schema']['title']['length'] = 45;
  8150. $validator = new Validator();
  8151. $validator->maxLength('title', 55);
  8152. $article = new EntityContext(
  8153. [
  8154. 'entity' => new Entity($this->article),
  8155. 'table' => new Table([
  8156. 'schema' => $this->article['schema'],
  8157. 'validator' => $validator,
  8158. 'alias' => 'Articles',
  8159. ]),
  8160. ]
  8161. );
  8162. $this->Form->create($article);
  8163. $result = $this->Form->control('title', ['maxlength' => 10]);
  8164. $expected = [
  8165. 'div' => ['class'],
  8166. 'label' => ['for'],
  8167. 'Title',
  8168. '/label',
  8169. 'input' => [
  8170. 'aria-required' => 'true',
  8171. 'id',
  8172. 'name' => 'title',
  8173. 'type' => 'text',
  8174. 'required' => 'required',
  8175. 'maxlength' => 10, // Length set in options should take highest precedence.
  8176. 'data-validity-message' => 'This field cannot be left empty',
  8177. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8178. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8179. ],
  8180. '/div',
  8181. ];
  8182. $this->assertHtml($expected, $result);
  8183. }
  8184. /**
  8185. * testControlMinMaxLengthEntityContext method
  8186. *
  8187. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8188. */
  8189. public function testControlMinMaxLengthEntityContext(): void
  8190. {
  8191. $validator = new Validator();
  8192. $validator->maxLength('title', 10);
  8193. $article = new EntityContext(
  8194. [
  8195. 'entity' => new Entity($this->article),
  8196. 'table' => new Table([
  8197. 'alias' => 'Articles',
  8198. 'schema' => $this->article['schema'],
  8199. 'validator' => $validator,
  8200. ]),
  8201. ]
  8202. );
  8203. $this->Form->create($article);
  8204. $result = $this->Form->control('title');
  8205. $expected = [
  8206. 'div' => ['class'],
  8207. 'label' => ['for'],
  8208. 'Title',
  8209. '/label',
  8210. 'input' => [
  8211. 'aria-required' => 'true',
  8212. 'id',
  8213. 'name' => 'title',
  8214. 'type' => 'text',
  8215. 'required' => 'required',
  8216. 'maxlength' => 10,
  8217. 'data-validity-message' => 'This field cannot be left empty',
  8218. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8219. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8220. ],
  8221. '/div',
  8222. ];
  8223. $this->assertHtml($expected, $result);
  8224. }
  8225. /**
  8226. * testControlMaxLengthFormContext method
  8227. *
  8228. * Test control() with maxlength attribute in Form Context.
  8229. */
  8230. public function testControlMaxLengthFormContext(): void
  8231. {
  8232. $validator = new Validator();
  8233. $validator->maxLength('title', 10);
  8234. $form = new Form();
  8235. $form->setValidator('default', $validator);
  8236. $this->Form->create($form);
  8237. $result = $this->Form->control('title');
  8238. $expected = [
  8239. 'div' => ['class'],
  8240. 'label' => ['for'],
  8241. 'Title',
  8242. '/label',
  8243. 'input' => [
  8244. 'aria-required' => 'true',
  8245. 'id',
  8246. 'name' => 'title',
  8247. 'type' => 'text',
  8248. 'required' => 'required',
  8249. 'maxlength' => 10,
  8250. 'data-validity-message' => 'This field cannot be left empty',
  8251. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8252. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8253. ],
  8254. '/div',
  8255. ];
  8256. $this->assertHtml($expected, $result);
  8257. }
  8258. }