FormHelperTest.php 303 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850
  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\Date;
  24. use Cake\I18n\DateTime;
  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
  53. */
  54. protected array $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. * setUp method
  69. */
  70. public function setUp(): void
  71. {
  72. parent::setUp();
  73. Configure::write('Config.language', 'eng');
  74. Configure::write('App.base', '');
  75. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  76. $request = new ServerRequest([
  77. 'webroot' => '',
  78. 'base' => '',
  79. 'url' => '/articles/add',
  80. 'params' => [
  81. 'controller' => 'Articles',
  82. 'action' => 'add',
  83. 'plugin' => null,
  84. ],
  85. ]);
  86. $this->View = new View($request);
  87. Router::reload();
  88. Router::setRequest($request);
  89. $this->url = '/articles/add';
  90. $this->Form = new FormHelper($this->View);
  91. $this->dateRegex = [
  92. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  93. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  94. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  95. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  96. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  97. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  98. ];
  99. $this->article = [
  100. 'schema' => [
  101. 'id' => ['type' => 'integer'],
  102. 'author_id' => ['type' => 'integer', 'null' => true],
  103. 'title' => ['type' => 'string', 'null' => true],
  104. 'body' => 'text',
  105. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  106. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  107. ],
  108. 'required' => [
  109. 'author_id' => true,
  110. 'title' => true,
  111. ],
  112. ];
  113. Security::setSalt('foo!');
  114. $builder = Router::createRouteBuilder('/');
  115. $builder->connect('/{controller}', ['action' => 'index']);
  116. $builder->connect('/{controller}/{action}/*');
  117. }
  118. /**
  119. * tearDown method
  120. */
  121. public function tearDown(): void
  122. {
  123. parent::tearDown();
  124. unset($this->Form, $this->Controller, $this->View);
  125. }
  126. /**
  127. * Test construct() with the templates option.
  128. */
  129. public function testConstructTemplatesFile(): void
  130. {
  131. $helper = new FormHelper($this->View, [
  132. 'templates' => 'htmlhelper_tags',
  133. ]);
  134. $result = $helper->control('name');
  135. $this->assertStringContainsString('<input', $result);
  136. }
  137. /**
  138. * Test that when specifying custom widgets the config array for that widget
  139. * is overwritten instead of merged.
  140. */
  141. public function testConstructWithWidgets(): void
  142. {
  143. $config = [
  144. 'widgets' => [
  145. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  146. ],
  147. ];
  148. $helper = new FormHelper($this->View, $config);
  149. $locator = $helper->getWidgetLocator();
  150. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  151. }
  152. /**
  153. * Test that when specifying custom widgets config file and it should be
  154. * added to widgets array. WidgetLocator will load widgets in constructor.
  155. */
  156. public function testConstructWithWidgetsConfig(): void
  157. {
  158. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  159. $locator = $helper->getWidgetLocator();
  160. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  161. }
  162. /**
  163. * Test setting the widget locator
  164. */
  165. public function testSetAndGetWidgetLocator(): void
  166. {
  167. $helper = new FormHelper($this->View);
  168. $locator = new WidgetLocator($helper->templater(), $this->View);
  169. $helper->setWidgetLocator($locator);
  170. $this->assertSame($locator, $helper->getWidgetLocator());
  171. }
  172. /**
  173. * Test overridding grouped input types which controls generation of "for"
  174. * attribute of labels.
  175. */
  176. public function testConstructWithGroupedInputTypes(): void
  177. {
  178. $helper = new FormHelper($this->View, [
  179. 'groupedInputTypes' => ['radio'],
  180. ]);
  181. $result = $helper->control('when', ['type' => 'datetime-local']);
  182. $this->assertStringContainsString('<label for="when">When</label>', $result);
  183. }
  184. /**
  185. * Test registering a new widget class and rendering it.
  186. */
  187. public function testAddWidgetAndRenderWidget(): void
  188. {
  189. $data = [
  190. 'val' => 1,
  191. ];
  192. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  193. $this->Form->addWidget('test', $mock);
  194. $mock->expects($this->once())
  195. ->method('render')
  196. ->with($data)
  197. ->will($this->returnValue('HTML'));
  198. $result = $this->Form->widget('test', $data);
  199. $this->assertSame('HTML', $result);
  200. }
  201. /**
  202. * Test that secureFields() of widget is called after calling render(),
  203. * not before.
  204. */
  205. public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
  206. {
  207. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  208. 'unlockedFields' => [],
  209. ]));
  210. $data = [
  211. 'val' => 1,
  212. 'name' => 'test',
  213. ];
  214. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  215. $this->Form->addWidget('test', $mock);
  216. $mock->expects($this->once())
  217. ->method('render')
  218. ->with($data)
  219. ->will($this->returnValue('HTML'));
  220. $mock->expects($this->once())
  221. ->method('secureFields')
  222. ->with($data)
  223. ->will($this->returnValue(['test']));
  224. $this->Form->create();
  225. $result = $this->Form->widget('test', $data + ['secure' => true]);
  226. $this->assertSame('HTML', $result);
  227. }
  228. /**
  229. * Test that empty string is not added to secure fields list when
  230. * rendering input widget without name.
  231. */
  232. public function testRenderingWidgetWithEmptyName(): void
  233. {
  234. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  235. $this->Form->create();
  236. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  237. $this->assertSame('<select name=""></select>', $result);
  238. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  239. $this->assertEquals([], $result);
  240. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  241. $this->assertSame('<select name="0"></select>', $result);
  242. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  243. $this->assertEquals(['0'], $result);
  244. }
  245. /**
  246. * Test adding a new context class.
  247. */
  248. public function testAddContextProvider(): void
  249. {
  250. $context = 'My data';
  251. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  252. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  253. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  254. $this->assertSame($context, $data['entity']);
  255. return $stub;
  256. });
  257. $this->Form->create($context);
  258. $result = $this->Form->context();
  259. $this->assertSame($stub, $result);
  260. }
  261. /**
  262. * Test replacing a context class.
  263. */
  264. public function testAddContextProviderReplace(): void
  265. {
  266. $entity = new Article();
  267. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  268. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  269. return $stub;
  270. });
  271. $this->Form->create($entity);
  272. $result = $this->Form->context();
  273. $this->assertSame($stub, $result);
  274. }
  275. /**
  276. * Test overriding a context class.
  277. */
  278. public function testAddContextProviderAdd(): void
  279. {
  280. $entity = new Article();
  281. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  282. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  283. if ($data['entity'] instanceof Entity) {
  284. return $stub;
  285. }
  286. });
  287. $this->Form->create($entity);
  288. $result = $this->Form->context();
  289. $this->assertSame($stub, $result);
  290. }
  291. /**
  292. * Provides context options for create().
  293. *
  294. * @return array
  295. */
  296. public function contextSelectionProvider(): array
  297. {
  298. $entity = new Article();
  299. $collection = new Collection([$entity]);
  300. $emptyCollection = new Collection([]);
  301. $arrayObject = new ArrayObject([]);
  302. $data = [
  303. 'schema' => [
  304. 'title' => ['type' => 'string'],
  305. ],
  306. ];
  307. $form = new Form();
  308. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  309. return [
  310. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  311. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  312. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  313. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  314. 'form' => [$form, 'Cake\View\Form\FormContext'],
  315. 'none' => [null, 'Cake\View\Form\NullContext'],
  316. 'custom' => [$custom, get_class($custom)],
  317. ];
  318. }
  319. /**
  320. * Test default context selection in create()
  321. *
  322. * @dataProvider contextSelectionProvider
  323. * @param mixed $data
  324. */
  325. public function testCreateContextSelectionBuiltIn($data, string $class): void
  326. {
  327. $this->Form->create($data);
  328. $this->assertInstanceOf($class, $this->Form->context());
  329. }
  330. /**
  331. * Data provider for type option.
  332. *
  333. * @return array
  334. */
  335. public static function requestTypeProvider(): array
  336. {
  337. return [
  338. // type, method, override
  339. ['post', 'post', 'POST'],
  340. ['put', 'post', 'PUT'],
  341. ['patch', 'post', 'PATCH'],
  342. ['delete', 'post', 'DELETE'],
  343. ];
  344. }
  345. /**
  346. * Test creating file forms.
  347. */
  348. public function testCreateFile(): void
  349. {
  350. $encoding = strtolower(Configure::read('App.encoding'));
  351. $result = $this->Form->create(null, ['type' => 'file']);
  352. $expected = [
  353. 'form' => [
  354. 'method' => 'post', 'action' => '/articles/add',
  355. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  356. ],
  357. 'div' => ['style' => 'display:none;'],
  358. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  359. '/div',
  360. ];
  361. $this->assertHtml($expected, $result);
  362. }
  363. /**
  364. * Test creating GET forms.
  365. */
  366. public function testCreateGet(): void
  367. {
  368. $encoding = strtolower(Configure::read('App.encoding'));
  369. $result = $this->Form->create(null, ['type' => 'get']);
  370. $expected = ['form' => [
  371. 'method' => 'get', 'action' => '/articles/add',
  372. 'accept-charset' => $encoding,
  373. ]];
  374. $this->assertHtml($expected, $result);
  375. }
  376. /**
  377. * Test explicit method/enctype options.
  378. *
  379. * Explicit method overwrites inferred method from 'type'
  380. */
  381. public function testCreateExplicitMethodEnctype(): void
  382. {
  383. $encoding = strtolower(Configure::read('App.encoding'));
  384. $result = $this->Form->create(null, [
  385. 'type' => 'get',
  386. 'method' => 'put',
  387. 'enctype' => 'multipart/form-data',
  388. ]);
  389. $expected = ['form' => [
  390. 'method' => 'put',
  391. 'action' => '/articles/add',
  392. 'enctype' => 'multipart/form-data',
  393. 'accept-charset' => $encoding,
  394. ]];
  395. $this->assertHtml($expected, $result);
  396. }
  397. /**
  398. * Test create() with the templates option.
  399. */
  400. public function testCreateTemplatesArray(): void
  401. {
  402. $result = $this->Form->create($this->article, [
  403. 'templates' => [
  404. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  405. ],
  406. ]);
  407. $expected = [
  408. 'form' => [
  409. 'class' => 'form-horizontal',
  410. 'method' => 'post',
  411. 'action' => '/articles/add',
  412. 'accept-charset' => 'utf-8',
  413. ],
  414. ];
  415. $this->assertHtml($expected, $result);
  416. }
  417. /**
  418. * Test create() with the templates option.
  419. */
  420. public function testCreateTemplatesFile(): void
  421. {
  422. $result = $this->Form->create($this->article, [
  423. 'templates' => 'htmlhelper_tags',
  424. ]);
  425. $expected = [
  426. 'start form',
  427. ];
  428. $this->assertHtml($expected, $result);
  429. }
  430. /**
  431. * Test that create() and end() restore templates.
  432. */
  433. public function testCreateEndRestoreTemplates(): void
  434. {
  435. $this->Form->create($this->article, [
  436. 'templates' => ['input' => 'custom input element'],
  437. ]);
  438. $this->Form->end();
  439. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  440. }
  441. /**
  442. * Test using template vars in various templates used by control() method.
  443. */
  444. public function testControlTemplateVars(): void
  445. {
  446. $result = $this->Form->control('text', [
  447. 'templates' => [
  448. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  449. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  450. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  451. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  452. ],
  453. 'templateVars' => [
  454. 'forinput' => 'in-input',
  455. 'forlabel' => 'in-label',
  456. 'forgroup' => 'in-group',
  457. 'forcontainer' => 'in-container',
  458. ],
  459. ]);
  460. $expected = [
  461. 'div' => ['class'],
  462. 'label' => ['for'],
  463. 'Text in-label',
  464. '/label',
  465. 'in-group',
  466. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  467. 'in-container',
  468. '/div',
  469. ];
  470. $this->assertHtml($expected, $result);
  471. }
  472. /**
  473. * Test ensuring template variables work in template files loaded
  474. * during control().
  475. */
  476. public function testControlTemplatesFromFile(): void
  477. {
  478. $result = $this->Form->control('title', [
  479. 'templates' => 'test_templates',
  480. 'templateVars' => [
  481. 'forcontainer' => 'container-data',
  482. ],
  483. ]);
  484. $expected = [
  485. 'div' => ['class'],
  486. 'label' => ['for'],
  487. 'Title',
  488. '/label',
  489. 'input' => ['name', 'type' => 'text', 'id'],
  490. 'container-data',
  491. '/div',
  492. ];
  493. $this->assertHtml($expected, $result);
  494. }
  495. /**
  496. * Test using template vars in inputSubmit and submitContainer template.
  497. */
  498. public function testSubmitTemplateVars(): void
  499. {
  500. $this->Form->setTemplates([
  501. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  502. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  503. ]);
  504. $result = $this->Form->submit('Submit', [
  505. 'templateVars' => [
  506. 'forinput' => 'in-input',
  507. 'forcontainer' => 'in-container',
  508. ],
  509. ]);
  510. $expected = [
  511. 'div' => ['class'],
  512. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  513. 'in-container',
  514. '/div',
  515. ];
  516. $this->assertHtml($expected, $result);
  517. }
  518. /**
  519. * test the create() method
  520. *
  521. * @dataProvider requestTypeProvider
  522. */
  523. public function testCreateTypeOptions(string $type, string $method, string $override): void
  524. {
  525. $encoding = strtolower(Configure::read('App.encoding'));
  526. $result = $this->Form->create(null, ['type' => $type]);
  527. $expected = [
  528. 'form' => [
  529. 'method' => $method, 'action' => '/articles/add',
  530. 'accept-charset' => $encoding,
  531. ],
  532. ];
  533. $extra = [
  534. 'div' => ['style' => 'display:none;'],
  535. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  536. '/div',
  537. ];
  538. if ($type !== 'post') {
  539. $expected = array_merge($expected, $extra);
  540. }
  541. $this->assertHtml($expected, $result);
  542. }
  543. /**
  544. * Test using template vars in Create (formStart template)
  545. */
  546. public function testCreateTemplateVars(): void
  547. {
  548. $result = $this->Form->create($this->article, [
  549. 'templates' => [
  550. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  551. ],
  552. 'templateVars' => ['header' => 'headertext'],
  553. ]);
  554. $expected = [
  555. 'h4' => ['class'],
  556. 'headertext',
  557. '/h4',
  558. 'form' => [
  559. 'method' => 'post',
  560. 'action' => '/articles/add',
  561. 'accept-charset' => 'utf-8',
  562. ],
  563. ];
  564. $this->assertHtml($expected, $result);
  565. }
  566. /**
  567. * Test opening a form for an update operation.
  568. */
  569. public function testCreateUpdateForm(): void
  570. {
  571. $encoding = strtolower(Configure::read('App.encoding'));
  572. $this->View->setRequest($this->View->getRequest()
  573. ->withRequestTarget('/articles/edit/1')
  574. ->withParam('action', 'edit'));
  575. $this->article['defaults']['id'] = 1;
  576. $result = $this->Form->create($this->article);
  577. $expected = [
  578. 'form' => [
  579. 'method' => 'post',
  580. 'action' => '/articles/edit/1',
  581. 'accept-charset' => $encoding,
  582. ],
  583. 'div' => ['style' => 'display:none;'],
  584. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  585. '/div',
  586. ];
  587. $this->assertHtml($expected, $result);
  588. }
  589. /**
  590. * test create() with automatic url generation
  591. */
  592. public function testCreateAutoUrl(): void
  593. {
  594. $encoding = strtolower(Configure::read('App.encoding'));
  595. $this->View->setRequest($this->View->getRequest()
  596. ->withRequestTarget('/articles/delete/10')
  597. ->withParam('action', 'delete'));
  598. $result = $this->Form->create($this->article);
  599. $expected = [
  600. 'form' => [
  601. 'method' => 'post', 'action' => '/articles/delete/10',
  602. 'accept-charset' => $encoding,
  603. ],
  604. ];
  605. $this->assertHtml($expected, $result);
  606. $this->article['defaults'] = ['id' => 1];
  607. $this->View->setRequest($this->View->getRequest()
  608. ->withRequestTarget('/Articles/edit/1')
  609. ->withParam('action', 'delete'));
  610. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  611. $expected = [
  612. 'form' => [
  613. 'method' => 'post',
  614. 'action' => '/Articles/edit/1',
  615. 'accept-charset' => $encoding,
  616. ],
  617. 'div' => ['style' => 'display:none;'],
  618. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  619. '/div',
  620. ];
  621. $this->assertHtml($expected, $result);
  622. $this->View->setRequest($this->View->getRequest()
  623. ->withParam('action', 'add'));
  624. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  625. $expected = [
  626. 'form' => [
  627. 'method' => 'post',
  628. 'action' => '/Articles/publish/1',
  629. 'accept-charset' => $encoding,
  630. ],
  631. 'div' => ['style' => 'display:none;'],
  632. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  633. '/div',
  634. ];
  635. $this->assertHtml($expected, $result);
  636. $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
  637. $expected = [
  638. 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
  639. 'div' => ['style' => 'display:none;'],
  640. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  641. '/div',
  642. ];
  643. $this->assertHtml($expected, $result);
  644. $this->View->setRequest($this->View->getRequest()
  645. ->withParam('controller', 'Pages'));
  646. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  647. $expected = [
  648. 'form' => [
  649. 'method' => 'post', 'action' => '/Pages/signup/1',
  650. 'accept-charset' => $encoding,
  651. ],
  652. 'div' => ['style' => 'display:none;'],
  653. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  654. '/div',
  655. ];
  656. $this->assertHtml($expected, $result);
  657. }
  658. /**
  659. * Test create() with no URL (no "action" attribute for <form> tag)
  660. */
  661. public function testCreateNoUrl(): void
  662. {
  663. $result = $this->Form->create(null, ['url' => false]);
  664. $expected = [
  665. 'form' => [
  666. 'method' => 'post',
  667. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  668. ],
  669. ];
  670. $this->assertHtml($expected, $result);
  671. }
  672. /**
  673. * test create() with a custom route
  674. */
  675. public function testCreateCustomRoute(): void
  676. {
  677. $builder = Router::createRouteBuilder('/');
  678. $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
  679. $encoding = strtolower(Configure::read('App.encoding'));
  680. $this->View->setRequest($this->View->getRequest()
  681. ->withParam('controller', 'Users'));
  682. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  683. $expected = [
  684. 'form' => [
  685. 'method' => 'post', 'action' => '/login',
  686. 'accept-charset' => $encoding,
  687. ],
  688. ];
  689. $this->assertHtml($expected, $result);
  690. $builder->connect(
  691. '/new-article',
  692. ['controller' => 'Articles', 'action' => 'myAction'],
  693. ['_name' => 'my-route']
  694. );
  695. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  696. $expected = [
  697. 'form' => [
  698. 'method' => 'post', 'action' => '/new-article',
  699. 'accept-charset' => $encoding,
  700. ],
  701. ];
  702. $this->assertHtml($expected, $result);
  703. }
  704. /**
  705. * test automatic accept-charset overriding
  706. */
  707. public function testCreateWithAcceptCharset(): void
  708. {
  709. $result = $this->Form->create(
  710. $this->article,
  711. [
  712. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  713. ]
  714. );
  715. $expected = [
  716. 'form' => [
  717. 'method' => 'post', 'action' => '/Articles',
  718. 'accept-charset' => 'iso-8859-1',
  719. ],
  720. ];
  721. $this->assertHtml($expected, $result);
  722. }
  723. /**
  724. * Test base form URL when 'url' param is passed with multiple parameters (&)
  725. */
  726. public function testCreateQueryStringRequest(): void
  727. {
  728. $encoding = strtolower(Configure::read('App.encoding'));
  729. $result = $this->Form->create($this->article, [
  730. 'type' => 'post',
  731. 'escape' => false,
  732. 'url' => [
  733. 'controller' => 'Controller',
  734. 'action' => 'action',
  735. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  736. ],
  737. ]);
  738. $expected = [
  739. 'form' => [
  740. 'method' => 'post',
  741. 'action' => '/Controller/action?param1=value1&amp;param2=value2',
  742. 'accept-charset' => $encoding,
  743. ],
  744. ];
  745. $this->assertHtml($expected, $result);
  746. $result = $this->Form->create($this->article, [
  747. 'type' => 'post',
  748. 'url' => [
  749. 'controller' => 'Controller',
  750. 'action' => 'action',
  751. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  752. ],
  753. ]);
  754. $this->assertHtml($expected, $result);
  755. }
  756. /**
  757. * test that create() doesn't cause errors by multiple id's being in the primary key
  758. * as could happen with multiple select or checkboxes.
  759. */
  760. public function testCreateWithMultipleIdInData(): void
  761. {
  762. $encoding = strtolower(Configure::read('App.encoding'));
  763. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  764. $result = $this->Form->create($this->article);
  765. $expected = [
  766. 'form' => [
  767. 'method' => 'post',
  768. 'action' => '/articles/add',
  769. 'accept-charset' => $encoding,
  770. ],
  771. ];
  772. $this->assertHtml($expected, $result);
  773. }
  774. /**
  775. * test that create() doesn't add in extra passed params.
  776. */
  777. public function testCreatePassedArgs(): void
  778. {
  779. $encoding = strtolower(Configure::read('App.encoding'));
  780. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  781. $result = $this->Form->create($this->article, [
  782. 'type' => 'post',
  783. 'escape' => false,
  784. 'url' => [
  785. 'action' => 'edit',
  786. 'myparam',
  787. ],
  788. ]);
  789. $expected = [
  790. 'form' => [
  791. 'method' => 'post',
  792. 'action' => '/Articles/edit/myparam',
  793. 'accept-charset' => $encoding,
  794. ],
  795. ];
  796. $this->assertHtml($expected, $result);
  797. }
  798. /**
  799. * test creating a get form, and get form inputs.
  800. */
  801. public function testGetFormCreate(): void
  802. {
  803. $encoding = strtolower(Configure::read('App.encoding'));
  804. $result = $this->Form->create($this->article, ['type' => 'get']);
  805. $expected = ['form' => [
  806. 'method' => 'get', 'action' => '/articles/add',
  807. 'accept-charset' => $encoding,
  808. ]];
  809. $this->assertHtml($expected, $result);
  810. $result = $this->Form->text('title');
  811. $expected = ['input' => [
  812. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  813. ]];
  814. $this->assertHtml($expected, $result);
  815. $result = $this->Form->password('password');
  816. $expected = ['input' => [
  817. 'name' => 'password', 'type' => 'password',
  818. ]];
  819. $this->assertHtml($expected, $result);
  820. $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  821. $result = $this->Form->text('user_form');
  822. $expected = ['input' => [
  823. 'name' => 'user_form', 'type' => 'text',
  824. ]];
  825. $this->assertHtml($expected, $result);
  826. }
  827. /**
  828. * test get form, and inputs when the model param is false
  829. */
  830. public function testGetFormWithFalseModel(): void
  831. {
  832. $encoding = strtolower(Configure::read('App.encoding'));
  833. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
  834. $result = $this->Form->create(null, [
  835. 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
  836. ]);
  837. $expected = ['form' => [
  838. 'method' => 'get', 'action' => '/ContactTest/add',
  839. 'accept-charset' => $encoding,
  840. ]];
  841. $this->assertHtml($expected, $result);
  842. $result = $this->Form->text('reason');
  843. $expected = [
  844. 'input' => ['type' => 'text', 'name' => 'reason'],
  845. ];
  846. $this->assertHtml($expected, $result);
  847. }
  848. /**
  849. * testFormCreateWithSecurity method
  850. *
  851. * Test form->create() with security key.
  852. */
  853. public function testCreateWithSecurity(): void
  854. {
  855. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  856. $encoding = strtolower(Configure::read('App.encoding'));
  857. $result = $this->Form->create($this->article, [
  858. 'url' => '/articles/publish',
  859. ]);
  860. $expected = [
  861. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  862. 'div' => ['style' => 'display:none;'],
  863. ['input' => [
  864. 'type' => 'hidden',
  865. 'name' => '_csrfToken',
  866. 'value' => 'testKey',
  867. 'autocomplete' => 'off',
  868. ]],
  869. '/div',
  870. ];
  871. $this->assertHtml($expected, $result);
  872. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  873. $expected['form']['id'] = 'MyForm';
  874. $this->assertHtml($expected, $result);
  875. }
  876. /**
  877. * testFormCreateGetNoSecurity method
  878. *
  879. * Test form->create() with no security key as its a get form
  880. */
  881. public function testCreateEndGetNoSecurity(): void
  882. {
  883. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  884. $article = new Article();
  885. $result = $this->Form->create($article, [
  886. 'type' => 'get',
  887. 'url' => '/contacts/add',
  888. ]);
  889. $this->assertStringNotContainsString('testKey', $result);
  890. $result = $this->Form->end();
  891. $this->assertStringNotContainsString('testKey', $result);
  892. }
  893. /**
  894. * Tests form hash generation with model-less data
  895. */
  896. public function testValidateHashNoModel(): void
  897. {
  898. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  899. $fields = ['anything'];
  900. $this->Form->create();
  901. $result = $this->Form->secure($fields);
  902. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  903. $this->assertStringContainsString($hash, $result);
  904. }
  905. /**
  906. * Tests that hidden fields generated for checkboxes don't get locked
  907. */
  908. public function testNoCheckboxLocking(): void
  909. {
  910. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  911. $this->Form->create();
  912. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  913. $this->Form->checkbox('check', ['value' => '1']);
  914. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  915. }
  916. /**
  917. * testFormSecurityFields method
  918. *
  919. * Test generation of secure form hash generation.
  920. */
  921. public function testFormSecurityFields(): void
  922. {
  923. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  924. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  925. $this->Form->create();
  926. $result = $this->Form->secure($fields);
  927. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  928. $hash .= ':' . 'Model.valid';
  929. $hash = urlencode($hash);
  930. $tokenDebug = urlencode(json_encode([
  931. $this->url,
  932. $fields,
  933. [],
  934. ]));
  935. $expected = [
  936. 'div' => ['style' => 'display:none;'],
  937. ['input' => [
  938. 'type' => 'hidden',
  939. 'name' => '_Token[fields]',
  940. 'value' => $hash,
  941. 'autocomplete' => 'off',
  942. ]],
  943. ['input' => [
  944. 'type' => 'hidden',
  945. 'name' => '_Token[unlocked]',
  946. 'value' => '',
  947. 'autocomplete' => 'off',
  948. ]],
  949. ['input' => [
  950. 'type' => 'hidden',
  951. 'name' => '_Token[debug]',
  952. 'value' => $tokenDebug,
  953. 'autocomplete' => 'off',
  954. ]],
  955. '/div',
  956. ];
  957. $this->assertHtml($expected, $result);
  958. }
  959. /**
  960. * testFormSecurityFields method
  961. *
  962. * Test debug token is not generated if debug is false
  963. */
  964. public function testFormSecurityFieldsNoDebugMode(): void
  965. {
  966. Configure::write('debug', false);
  967. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  968. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  969. $this->Form->create();
  970. $result = $this->Form->secure($fields);
  971. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  972. $hash .= ':' . 'Model.valid';
  973. $hash = urlencode($hash);
  974. $expected = [
  975. 'div' => ['style' => 'display:none;'],
  976. ['input' => [
  977. 'type' => 'hidden',
  978. 'name' => '_Token[fields]',
  979. 'autocomplete' => 'off',
  980. 'value' => $hash,
  981. ]],
  982. ['input' => [
  983. 'type' => 'hidden',
  984. 'name' => '_Token[unlocked]',
  985. 'autocomplete' => 'off',
  986. 'value' => '',
  987. ]],
  988. '/div',
  989. ];
  990. $this->assertHtml($expected, $result);
  991. }
  992. /**
  993. * Tests correct generation of number fields for smallint
  994. */
  995. public function testTextFieldGenerationForSmallint(): void
  996. {
  997. $this->article['schema'] = [
  998. 'foo' => [
  999. 'type' => 'smallinteger',
  1000. 'null' => false,
  1001. 'default' => null,
  1002. 'length' => 10,
  1003. ],
  1004. ];
  1005. $this->Form->create($this->article);
  1006. $result = $this->Form->control('foo');
  1007. $this->assertStringContainsString('class="input number"', $result);
  1008. $this->assertStringContainsString('type="number"', $result);
  1009. }
  1010. /**
  1011. * Tests correct generation of number fields for tinyint
  1012. */
  1013. public function testTextFieldGenerationForTinyint(): void
  1014. {
  1015. $this->article['schema'] = [
  1016. 'foo' => [
  1017. 'type' => 'tinyinteger',
  1018. 'null' => false,
  1019. 'default' => null,
  1020. 'length' => 10,
  1021. ],
  1022. ];
  1023. $this->Form->create($this->article);
  1024. $result = $this->Form->control('foo');
  1025. $this->assertStringContainsString('class="input number"', $result);
  1026. $this->assertStringContainsString('type="number"', $result);
  1027. }
  1028. /**
  1029. * Tests correct generation of number fields for double and float fields
  1030. */
  1031. public function testTextFieldGenerationForFloats(): void
  1032. {
  1033. $this->article['schema'] = [
  1034. 'foo' => [
  1035. 'type' => 'float',
  1036. 'null' => false,
  1037. 'default' => null,
  1038. 'length' => 10,
  1039. ],
  1040. ];
  1041. $this->Form->create($this->article);
  1042. $result = $this->Form->control('foo');
  1043. $expected = [
  1044. 'div' => ['class' => 'input number'],
  1045. 'label' => ['for' => 'foo'],
  1046. 'Foo',
  1047. '/label',
  1048. ['input' => [
  1049. 'type' => 'number',
  1050. 'name' => 'foo',
  1051. 'id' => 'foo',
  1052. 'step' => 'any',
  1053. ]],
  1054. '/div',
  1055. ];
  1056. $this->assertHtml($expected, $result);
  1057. $result = $this->Form->control('foo', ['step' => 0.5]);
  1058. $expected = [
  1059. 'div' => ['class' => 'input number'],
  1060. 'label' => ['for' => 'foo'],
  1061. 'Foo',
  1062. '/label',
  1063. ['input' => [
  1064. 'type' => 'number',
  1065. 'name' => 'foo',
  1066. 'id' => 'foo',
  1067. 'step' => '0.5',
  1068. ]],
  1069. '/div',
  1070. ];
  1071. $this->assertHtml($expected, $result);
  1072. }
  1073. /**
  1074. * Tests correct generation of number fields for integer fields
  1075. */
  1076. public function testTextFieldTypeNumberGenerationForIntegers(): void
  1077. {
  1078. $this->getTableLocator()->get('Contacts', [
  1079. 'className' => ContactsTable::class,
  1080. ]);
  1081. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1082. $result = $this->Form->control('age');
  1083. $expected = [
  1084. 'div' => ['class' => 'input number'],
  1085. 'label' => ['for' => 'age'],
  1086. 'Age',
  1087. '/label',
  1088. ['input' => [
  1089. 'type' => 'number', 'name' => 'age',
  1090. 'id' => 'age',
  1091. ]],
  1092. '/div',
  1093. ];
  1094. $this->assertHtml($expected, $result);
  1095. }
  1096. /**
  1097. * Tests correct generation of file upload fields for binary fields
  1098. */
  1099. public function testFileUploadFieldTypeGenerationForBinaries(): void
  1100. {
  1101. $table = $this->getTableLocator()->get('Contacts', [
  1102. 'className' => ContactsTable::class,
  1103. ]);
  1104. $table->setSchema(['foo' => [
  1105. 'type' => 'binary',
  1106. 'null' => false,
  1107. 'default' => null,
  1108. 'length' => 1024,
  1109. ]]);
  1110. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1111. $result = $this->Form->control('foo');
  1112. $expected = [
  1113. 'div' => ['class' => 'input file'],
  1114. 'label' => ['for' => 'foo'],
  1115. 'Foo',
  1116. '/label',
  1117. ['input' => [
  1118. 'type' => 'file', 'name' => 'foo',
  1119. 'id' => 'foo',
  1120. ]],
  1121. '/div',
  1122. ];
  1123. $this->assertHtml($expected, $result);
  1124. }
  1125. /**
  1126. * testFormSecurityMultipleFields method
  1127. *
  1128. * Test secure() with multiple row form. Ensure hash is correct.
  1129. */
  1130. public function testFormSecurityMultipleFields(): void
  1131. {
  1132. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1133. $this->Form->create();
  1134. $fields = [
  1135. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1136. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1137. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1138. ];
  1139. $result = $this->Form->secure($fields);
  1140. $sortedFields = [
  1141. 'Model.0.password',
  1142. 'Model.0.username',
  1143. 'Model.1.password',
  1144. 'Model.1.username',
  1145. 'Model.0.hidden' => 'value',
  1146. 'Model.0.valid' => '0',
  1147. 'Model.1.hidden' => 'value',
  1148. 'Model.1.valid' => '0',
  1149. ];
  1150. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1151. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1152. $hash = urlencode($hash);
  1153. $tokenDebug = urlencode(json_encode([
  1154. $this->url,
  1155. $fields,
  1156. [],
  1157. ]));
  1158. $expected = [
  1159. 'div' => ['style' => 'display:none;'],
  1160. ['input' => [
  1161. 'type' => 'hidden',
  1162. 'name' => '_Token[fields]',
  1163. 'value' => $hash,
  1164. 'autocomplete' => 'off',
  1165. ]],
  1166. ['input' => [
  1167. 'type' => 'hidden',
  1168. 'name' => '_Token[unlocked]',
  1169. 'autocomplete' => 'off',
  1170. 'value' => '',
  1171. ]],
  1172. ['input' => [
  1173. 'type' => 'hidden',
  1174. 'name' => '_Token[debug]',
  1175. 'value' => $tokenDebug,
  1176. 'autocomplete' => 'off',
  1177. ]],
  1178. '/div',
  1179. ];
  1180. $this->assertHtml($expected, $result);
  1181. }
  1182. /**
  1183. * testFormSecurityMultipleSubmitButtons
  1184. *
  1185. * test form submit generation and ensure that _Token is only created on end()
  1186. */
  1187. public function testFormSecurityMultipleSubmitButtons(): void
  1188. {
  1189. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1190. $this->Form->create($this->article);
  1191. $this->Form->text('Address.title');
  1192. $this->Form->text('Address.first_name');
  1193. $result = $this->Form->submit('Save', ['name' => 'save']);
  1194. $expected = [
  1195. 'div' => ['class' => 'submit'],
  1196. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1197. '/div',
  1198. ];
  1199. $this->assertHtml($expected, $result);
  1200. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1201. $expected = [
  1202. 'div' => ['class' => 'submit'],
  1203. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1204. '/div',
  1205. ];
  1206. $this->assertHtml($expected, $result);
  1207. $result = $this->Form->end();
  1208. $tokenDebug = urlencode(json_encode([
  1209. '/articles/add',
  1210. [
  1211. 'Address.title',
  1212. 'Address.first_name',
  1213. ],
  1214. ['save', 'cancel'],
  1215. ]));
  1216. $expected = [
  1217. 'div' => ['style' => 'display:none;'],
  1218. ['input' => [
  1219. 'type' => 'hidden',
  1220. 'name' => '_Token[fields]',
  1221. 'autocomplete',
  1222. 'value',
  1223. ]],
  1224. ['input' => [
  1225. 'type' => 'hidden',
  1226. 'name' => '_Token[unlocked]',
  1227. 'value' => 'cancel%7Csave',
  1228. 'autocomplete' => 'off',
  1229. ]],
  1230. ['input' => [
  1231. 'type' => 'hidden',
  1232. 'name' => '_Token[debug]',
  1233. 'value' => $tokenDebug,
  1234. 'autocomplete' => 'off',
  1235. ]],
  1236. '/div',
  1237. ];
  1238. $this->assertHtml($expected, $result);
  1239. }
  1240. /**
  1241. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1242. */
  1243. public function testSecurityButtonNestedNamed(): void
  1244. {
  1245. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1246. $this->Form->create();
  1247. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1248. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1249. $this->assertEquals(['Address.button'], $result);
  1250. }
  1251. /**
  1252. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1253. */
  1254. public function testSecuritySubmitNestedNamed(): void
  1255. {
  1256. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1257. $this->Form->create($this->article);
  1258. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1259. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1260. $this->assertEquals(['Address.button'], $result);
  1261. }
  1262. /**
  1263. * Test that the correct fields are unlocked for image submits with no names.
  1264. */
  1265. public function testSecuritySubmitImageNoName(): void
  1266. {
  1267. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1268. $this->Form->create();
  1269. $result = $this->Form->submit('save.png');
  1270. $expected = [
  1271. 'div' => ['class' => 'submit'],
  1272. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1273. '/div',
  1274. ];
  1275. $this->assertHtml($expected, $result);
  1276. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1277. $this->assertEquals(['x', 'y'], $result);
  1278. }
  1279. /**
  1280. * Test that the correct fields are unlocked for image submits with names.
  1281. */
  1282. public function testSecuritySubmitImageName(): void
  1283. {
  1284. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1285. $this->Form->create();
  1286. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1287. $expected = [
  1288. 'div' => ['class' => 'submit'],
  1289. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1290. '/div',
  1291. ];
  1292. $this->assertHtml($expected, $result);
  1293. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1294. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1295. }
  1296. /**
  1297. * testFormSecurityMultipleControlFields method
  1298. *
  1299. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1300. */
  1301. public function testFormSecurityMultipleControlFields(): void
  1302. {
  1303. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1304. $this->Form->create();
  1305. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1306. $this->Form->control('Addresses.0.title');
  1307. $this->Form->control('Addresses.0.first_name');
  1308. $this->Form->control('Addresses.0.last_name');
  1309. $this->Form->control('Addresses.0.address');
  1310. $this->Form->control('Addresses.0.city');
  1311. $this->Form->control('Addresses.0.phone');
  1312. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1313. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1314. $this->Form->control('Addresses.1.title');
  1315. $this->Form->control('Addresses.1.first_name');
  1316. $this->Form->control('Addresses.1.last_name');
  1317. $this->Form->control('Addresses.1.address');
  1318. $this->Form->control('Addresses.1.city');
  1319. $this->Form->control('Addresses.1.phone');
  1320. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1321. $result = $this->Form->secure();
  1322. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1323. $tokenDebug = urlencode(json_encode([
  1324. '/articles/add',
  1325. [
  1326. 'Addresses.0.id' => '123456',
  1327. 'Addresses.0.title',
  1328. 'Addresses.0.first_name',
  1329. 'Addresses.0.last_name',
  1330. 'Addresses.0.address',
  1331. 'Addresses.0.city',
  1332. 'Addresses.0.phone',
  1333. 'Addresses.0.primary',
  1334. 'Addresses.1.id' => '654321',
  1335. 'Addresses.1.title',
  1336. 'Addresses.1.first_name',
  1337. 'Addresses.1.last_name',
  1338. 'Addresses.1.address',
  1339. 'Addresses.1.city',
  1340. 'Addresses.1.phone',
  1341. 'Addresses.1.primary',
  1342. ],
  1343. [],
  1344. ]));
  1345. $expected = [
  1346. 'div' => ['style' => 'display:none;'],
  1347. ['input' => [
  1348. 'type' => 'hidden',
  1349. 'name' => '_Token[fields]',
  1350. 'value' => $hash,
  1351. 'autocomplete' => 'off',
  1352. ]],
  1353. ['input' => [
  1354. 'type' => 'hidden',
  1355. 'name' => '_Token[unlocked]',
  1356. 'autocomplete' => 'off',
  1357. 'value' => '',
  1358. ]],
  1359. ['input' => [
  1360. 'type' => 'hidden',
  1361. 'name' => '_Token[debug]',
  1362. 'value' => $tokenDebug,
  1363. 'autocomplete' => 'off',
  1364. ]],
  1365. '/div',
  1366. ];
  1367. $this->assertHtml($expected, $result);
  1368. }
  1369. /**
  1370. * testFormSecurityArrayFields method
  1371. *
  1372. * Test form security with Model.field.0 style inputs.
  1373. */
  1374. public function testFormSecurityArrayFields(): void
  1375. {
  1376. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1377. $this->Form->create();
  1378. $this->Form->text('Address.primary.1');
  1379. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1380. $this->assertSame('Address.primary', $result[0]);
  1381. $this->Form->text('Address.secondary.1.0');
  1382. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1383. $this->assertSame('Address.secondary', $result[1]);
  1384. }
  1385. /**
  1386. * testFormSecurityMultipleControlDisabledFields method
  1387. *
  1388. * Test secure form generation with multiple records and disabled fields.
  1389. */
  1390. public function testFormSecurityMultipleControlDisabledFields(): void
  1391. {
  1392. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1393. 'unlockedFields' => ['first_name', 'address'],
  1394. ]));
  1395. $this->Form->create();
  1396. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1397. $this->Form->text('Addresses.0.title');
  1398. $this->Form->text('Addresses.0.first_name');
  1399. $this->Form->text('Addresses.0.last_name');
  1400. $this->Form->text('Addresses.0.address');
  1401. $this->Form->text('Addresses.0.city');
  1402. $this->Form->text('Addresses.0.phone');
  1403. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1404. $this->Form->text('Addresses.1.title');
  1405. $this->Form->text('Addresses.1.first_name');
  1406. $this->Form->text('Addresses.1.last_name');
  1407. $this->Form->text('Addresses.1.address');
  1408. $this->Form->text('Addresses.1.city');
  1409. $this->Form->text('Addresses.1.phone');
  1410. $result = $this->Form->secure();
  1411. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1412. $tokenDebug = urlencode(json_encode([
  1413. '/articles/add',
  1414. [
  1415. 'Addresses.0.id' => '123456',
  1416. 'Addresses.0.title',
  1417. 'Addresses.0.last_name',
  1418. 'Addresses.0.city',
  1419. 'Addresses.0.phone',
  1420. 'Addresses.1.id' => '654321',
  1421. 'Addresses.1.title',
  1422. 'Addresses.1.last_name',
  1423. 'Addresses.1.city',
  1424. 'Addresses.1.phone',
  1425. ],
  1426. [
  1427. 'first_name',
  1428. 'address',
  1429. ],
  1430. ]));
  1431. $expected = [
  1432. 'div' => ['style' => 'display:none;'],
  1433. ['input' => [
  1434. 'type' => 'hidden',
  1435. 'name' => '_Token[fields]',
  1436. 'autocomplete' => 'off',
  1437. 'value' => $hash,
  1438. ]],
  1439. ['input' => [
  1440. 'type' => 'hidden',
  1441. 'name' => '_Token[unlocked]',
  1442. 'autocomplete' => 'off',
  1443. 'value' => 'address%7Cfirst_name',
  1444. ]],
  1445. ['input' => [
  1446. 'type' => 'hidden',
  1447. 'name' => '_Token[debug]',
  1448. 'autocomplete' => 'off',
  1449. 'value' => $tokenDebug,
  1450. ]],
  1451. '/div',
  1452. ];
  1453. $this->assertHtml($expected, $result);
  1454. }
  1455. /**
  1456. * testFormSecurityControlDisabledFields method
  1457. *
  1458. * Test single record form with disabled fields.
  1459. */
  1460. public function testFormSecurityControlUnlockedFields(): void
  1461. {
  1462. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1463. 'unlockedFields' => ['first_name', 'address'],
  1464. ]));
  1465. $this->Form->create();
  1466. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1467. $this->assertEquals(
  1468. $this->View->getRequest()->getAttribute('formTokenData'),
  1469. ['unlockedFields' => $result]
  1470. );
  1471. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1472. $this->Form->text('Addresses.title');
  1473. $this->Form->text('Addresses.first_name');
  1474. $this->Form->text('Addresses.last_name');
  1475. $this->Form->text('Addresses.address');
  1476. $this->Form->text('Addresses.city');
  1477. $this->Form->text('Addresses.phone');
  1478. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1479. $expected = [
  1480. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1481. 'Addresses.city', 'Addresses.phone',
  1482. ];
  1483. $this->assertEquals($expected, $result);
  1484. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1485. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1486. $tokenDebug = urlencode(json_encode([
  1487. '/articles/add',
  1488. [
  1489. 'Addresses.id' => '123456',
  1490. 'Addresses.title',
  1491. 'Addresses.last_name',
  1492. 'Addresses.city',
  1493. 'Addresses.phone',
  1494. ],
  1495. [
  1496. 'first_name',
  1497. 'address',
  1498. ],
  1499. ]));
  1500. $expected = [
  1501. 'div' => ['style' => 'display:none;'],
  1502. ['input' => [
  1503. 'type' => 'hidden',
  1504. 'name' => '_Token[fields]',
  1505. 'value' => $hash,
  1506. 'autocomplete' => 'off',
  1507. 'data-foo' => 'bar',
  1508. ]],
  1509. ['input' => [
  1510. 'type' => 'hidden',
  1511. 'name' => '_Token[unlocked]',
  1512. 'value' => 'address%7Cfirst_name',
  1513. 'autocomplete' => 'off',
  1514. 'data-foo' => 'bar',
  1515. ]],
  1516. ['input' => [
  1517. 'type' => 'hidden', 'name' => '_Token[debug]',
  1518. 'value' => $tokenDebug,
  1519. 'autocomplete' => 'off',
  1520. 'data-foo' => 'bar',
  1521. ]],
  1522. '/div',
  1523. ];
  1524. $this->assertHtml($expected, $result);
  1525. }
  1526. /**
  1527. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1528. *
  1529. * Test single record form with debugSecurity param.
  1530. */
  1531. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
  1532. {
  1533. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1534. 'unlockedFields' => ['first_name', 'address'],
  1535. ]));
  1536. $this->Form->create();
  1537. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1538. $this->assertEquals(
  1539. $this->View->getRequest()->getAttribute('formTokenData'),
  1540. ['unlockedFields' => $result]
  1541. );
  1542. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1543. $this->Form->text('Addresses.title');
  1544. $this->Form->text('Addresses.first_name');
  1545. $this->Form->text('Addresses.last_name');
  1546. $this->Form->text('Addresses.address');
  1547. $this->Form->text('Addresses.city');
  1548. $this->Form->text('Addresses.phone');
  1549. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1550. $expected = [
  1551. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1552. 'Addresses.city', 'Addresses.phone',
  1553. ];
  1554. $this->assertEquals($expected, $result);
  1555. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1556. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1557. $tokenDebug = urlencode(json_encode([
  1558. '/articles/add',
  1559. [
  1560. 'Addresses.id' => '123456',
  1561. 'Addresses.title',
  1562. 'Addresses.last_name',
  1563. 'Addresses.city',
  1564. 'Addresses.phone',
  1565. ],
  1566. [
  1567. 'first_name',
  1568. 'address',
  1569. ],
  1570. ]));
  1571. $expected = [
  1572. 'div' => ['style' => 'display:none;'],
  1573. ['input' => [
  1574. 'type' => 'hidden',
  1575. 'name' => '_Token[fields]',
  1576. 'value' => $hash,
  1577. 'autocomplete' => 'off',
  1578. 'data-foo' => 'bar',
  1579. ]],
  1580. ['input' => [
  1581. 'type' => 'hidden',
  1582. 'name' => '_Token[unlocked]',
  1583. 'value' => 'address%7Cfirst_name',
  1584. 'autocomplete' => 'off',
  1585. 'data-foo' => 'bar',
  1586. ]],
  1587. ['input' => [
  1588. 'type' => 'hidden', 'name' => '_Token[debug]',
  1589. 'value' => $tokenDebug,
  1590. 'autocomplete' => 'off',
  1591. 'data-foo' => 'bar',
  1592. ]],
  1593. '/div',
  1594. ];
  1595. $this->assertHtml($expected, $result);
  1596. }
  1597. /**
  1598. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1599. *
  1600. * Debug is false, debugSecurity is true -> no debug
  1601. */
  1602. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
  1603. {
  1604. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1605. 'unlockedFields' => ['first_name', 'address'],
  1606. ]));
  1607. $this->Form->create();
  1608. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1609. $this->assertEquals(
  1610. $this->View->getRequest()->getAttribute('formTokenData'),
  1611. ['unlockedFields' => $result]
  1612. );
  1613. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1614. $this->Form->text('Addresses.title');
  1615. $this->Form->text('Addresses.first_name');
  1616. $this->Form->text('Addresses.last_name');
  1617. $this->Form->text('Addresses.address');
  1618. $this->Form->text('Addresses.city');
  1619. $this->Form->text('Addresses.phone');
  1620. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1621. $expected = [
  1622. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1623. 'Addresses.city', 'Addresses.phone',
  1624. ];
  1625. $this->assertEquals($expected, $result);
  1626. Configure::write('debug', false);
  1627. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1628. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1629. $expected = [
  1630. 'div' => ['style' => 'display:none;'],
  1631. ['input' => [
  1632. 'type' => 'hidden',
  1633. 'name' => '_Token[fields]',
  1634. 'value' => $hash,
  1635. 'autocomplete' => 'off',
  1636. 'data-foo' => 'bar',
  1637. ]],
  1638. ['input' => [
  1639. 'type' => 'hidden',
  1640. 'name' => '_Token[unlocked]',
  1641. 'value' => 'address%7Cfirst_name',
  1642. 'autocomplete' => 'off',
  1643. 'data-foo' => 'bar',
  1644. ]],
  1645. '/div',
  1646. ];
  1647. $this->assertHtml($expected, $result);
  1648. }
  1649. /**
  1650. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1651. *
  1652. * Test single record form with debugSecurity param.
  1653. */
  1654. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
  1655. {
  1656. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1657. 'unlockedFields' => ['first_name', 'address'],
  1658. ]));
  1659. $this->Form->create();
  1660. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1661. $this->assertEquals(
  1662. $this->View->getRequest()->getAttribute('formTokenData'),
  1663. ['unlockedFields' => $result]
  1664. );
  1665. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1666. $this->Form->text('Addresses.title');
  1667. $this->Form->text('Addresses.first_name');
  1668. $this->Form->text('Addresses.last_name');
  1669. $this->Form->text('Addresses.address');
  1670. $this->Form->text('Addresses.city');
  1671. $this->Form->text('Addresses.phone');
  1672. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1673. $expected = [
  1674. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1675. 'Addresses.city', 'Addresses.phone',
  1676. ];
  1677. $this->assertEquals($expected, $result);
  1678. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1679. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1680. $expected = [
  1681. 'div' => ['style' => 'display:none;'],
  1682. ['input' => [
  1683. 'type' => 'hidden',
  1684. 'name' => '_Token[fields]',
  1685. 'value' => $hash,
  1686. 'autocomplete' => 'off',
  1687. 'data-foo' => 'bar',
  1688. ]],
  1689. ['input' => [
  1690. 'type' => 'hidden',
  1691. 'name' => '_Token[unlocked]',
  1692. 'value' => 'address%7Cfirst_name',
  1693. 'autocomplete' => 'off',
  1694. 'data-foo' => 'bar',
  1695. ]],
  1696. '/div',
  1697. ];
  1698. $this->assertHtml($expected, $result);
  1699. }
  1700. /**
  1701. * testFormSecureWithCustomNameAttribute method
  1702. *
  1703. * Test securing inputs with custom name attributes.
  1704. */
  1705. public function testFormSecureWithCustomNameAttribute(): void
  1706. {
  1707. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1708. $this->Form->create();
  1709. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1710. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1711. $this->assertSame('User.custom', $result[0]);
  1712. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1713. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1714. $this->assertSame('User.custom.another.value', $result[1]);
  1715. }
  1716. /**
  1717. * testFormSecuredControl method
  1718. *
  1719. * Test generation of entire secure form, assertions made on control() output.
  1720. */
  1721. public function testFormSecuredControl(): void
  1722. {
  1723. $this->View->setRequest($this->View->getRequest()
  1724. ->withAttribute('formTokenData', [])
  1725. ->withAttribute('csrfToken', 'testKey'));
  1726. $this->article['schema'] = [
  1727. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1728. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1729. ];
  1730. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1731. $encoding = strtolower(Configure::read('App.encoding'));
  1732. $expected = [
  1733. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1734. 'div' => ['style' => 'display:none;'],
  1735. ['input' => [
  1736. 'type' => 'hidden',
  1737. 'name' => '_csrfToken',
  1738. 'value' => 'testKey',
  1739. 'autocomplete' => 'off',
  1740. ]],
  1741. '/div',
  1742. ];
  1743. $this->assertHtml($expected, $result);
  1744. $result = $this->Form->control('ratio');
  1745. $expected = [
  1746. 'div' => ['class'],
  1747. 'label' => ['for'],
  1748. 'Ratio',
  1749. '/label',
  1750. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1751. '/div',
  1752. ];
  1753. $this->assertHtml($expected, $result);
  1754. $result = $this->Form->control('population');
  1755. $expected = [
  1756. 'div' => ['class'],
  1757. 'label' => ['for'],
  1758. 'Population',
  1759. '/label',
  1760. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1761. '/div',
  1762. ];
  1763. $this->assertHtml($expected, $result);
  1764. $result = $this->Form->control('published', ['type' => 'text']);
  1765. $expected = [
  1766. 'div' => ['class' => 'input text'],
  1767. 'label' => ['for' => 'published'],
  1768. 'Published',
  1769. '/label',
  1770. ['input' => [
  1771. 'type' => 'text',
  1772. 'name' => 'published',
  1773. 'id' => 'published',
  1774. ]],
  1775. '/div',
  1776. ];
  1777. $this->assertHtml($expected, $result);
  1778. $result = $this->Form->control('other', ['type' => 'text']);
  1779. $expected = [
  1780. 'div' => ['class' => 'input text'],
  1781. 'label' => ['for' => 'other'],
  1782. 'Other',
  1783. '/label',
  1784. ['input' => [
  1785. 'type' => 'text',
  1786. 'name' => 'other',
  1787. 'id',
  1788. ]],
  1789. '/div',
  1790. ];
  1791. $this->assertHtml($expected, $result);
  1792. $result = $this->Form->hidden('stuff');
  1793. $expected = [
  1794. 'input' => [
  1795. 'type' => 'hidden',
  1796. 'name' => 'stuff',
  1797. ],
  1798. ];
  1799. $this->assertHtml($expected, $result);
  1800. $result = $this->Form->hidden('hidden', ['value' => false]);
  1801. $expected = ['input' => [
  1802. 'type' => 'hidden',
  1803. 'name' => 'hidden',
  1804. 'value' => '0',
  1805. ]];
  1806. $this->assertHtml($expected, $result);
  1807. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1808. $expected = [
  1809. 'div' => ['class' => 'input checkbox'],
  1810. ['input' => [
  1811. 'type' => 'hidden',
  1812. 'name' => 'something',
  1813. 'value' => '0',
  1814. ]],
  1815. 'label' => ['for' => 'something'],
  1816. ['input' => [
  1817. 'type' => 'checkbox',
  1818. 'name' => 'something',
  1819. 'value' => '1',
  1820. 'id' => 'something',
  1821. ]],
  1822. 'Something',
  1823. '/label',
  1824. '/div',
  1825. ];
  1826. $this->assertHtml($expected, $result);
  1827. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1828. $expectedFields = [
  1829. 'ratio',
  1830. 'population',
  1831. 'published',
  1832. 'other',
  1833. 'stuff' => '',
  1834. 'hidden' => '0',
  1835. 'something',
  1836. ];
  1837. $this->assertEquals($expectedFields, $result);
  1838. $result = $this->Form->secure();
  1839. $tokenDebug = urlencode(json_encode([
  1840. '/articles/add',
  1841. $expectedFields,
  1842. [],
  1843. ]));
  1844. $expected = [
  1845. 'div' => ['style' => 'display:none;'],
  1846. ['input' => [
  1847. 'type' => 'hidden',
  1848. 'name' => '_Token[fields]',
  1849. 'value',
  1850. 'autocomplete',
  1851. ]],
  1852. ['input' => [
  1853. 'type' => 'hidden',
  1854. 'name' => '_Token[unlocked]',
  1855. 'value' => '',
  1856. 'autocomplete' => 'off',
  1857. ]],
  1858. ['input' => [
  1859. 'type' => 'hidden', 'name' => '_Token[debug]',
  1860. 'value' => $tokenDebug,
  1861. 'autocomplete' => 'off',
  1862. ]],
  1863. '/div',
  1864. ];
  1865. $this->assertHtml($expected, $result);
  1866. $data = [
  1867. 'ratio' => '',
  1868. 'population' => '',
  1869. 'published' => '',
  1870. 'other' => '',
  1871. 'stuff' => '',
  1872. 'hidden' => '0',
  1873. 'something' => '',
  1874. '_Token' => $this->Form->getFormProtector()->buildTokenData(),
  1875. ];
  1876. $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
  1877. }
  1878. /**
  1879. * testSecuredControlCustomName method
  1880. *
  1881. * Test secured inputs with custom names.
  1882. */
  1883. public function testSecuredControlCustomName(): void
  1884. {
  1885. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1886. $this->Form->create();
  1887. $this->Form->text('text_input', [
  1888. 'name' => 'Option[General.default_role]',
  1889. ]);
  1890. $expected = ['Option.General.default_role'];
  1891. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1892. $this->assertEquals($expected, $result);
  1893. $this->Form->select('select_box', [1, 2], [
  1894. 'name' => 'Option[General.select_role]',
  1895. ]);
  1896. $expected[] = 'Option.General.select_role';
  1897. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1898. $this->assertEquals($expected, $result);
  1899. $this->Form->text('other.things[]');
  1900. $expected[] = 'other.things';
  1901. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1902. $this->assertEquals($expected, $result);
  1903. }
  1904. /**
  1905. * testSecuredControlDuplicate method
  1906. *
  1907. * Test that a hidden field followed by a visible field
  1908. * undoes the hidden field locking.
  1909. */
  1910. public function testSecuredControlDuplicate(): void
  1911. {
  1912. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1913. $this->Form->create();
  1914. $this->Form->control('text_val', [
  1915. 'type' => 'hidden',
  1916. 'value' => 'some text',
  1917. ]);
  1918. $expected = ['text_val' => 'some text'];
  1919. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1920. $this->assertEquals($expected, $result);
  1921. $this->Form->control('text_val', [
  1922. 'type' => 'text',
  1923. ]);
  1924. $expected = ['text_val'];
  1925. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1926. $this->assertEquals($expected, $result);
  1927. }
  1928. /**
  1929. * testFormSecuredFileControl method
  1930. *
  1931. * Tests that the correct keys are added to the field hash index.
  1932. */
  1933. public function testFormSecuredFileControl(): void
  1934. {
  1935. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1936. $this->Form->create();
  1937. $this->Form->file('Attachment.file');
  1938. $expected = ['Attachment.file'];
  1939. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1940. $this->assertEquals($expected, $result);
  1941. }
  1942. /**
  1943. * testFormSecuredMultipleSelect method
  1944. *
  1945. * Test that multiple selects keys are added to field hash.
  1946. */
  1947. public function testFormSecuredMultipleSelect(): void
  1948. {
  1949. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1950. $this->Form->create();
  1951. $options = ['1' => 'one', '2' => 'two'];
  1952. $this->Form->select('Model.select', $options);
  1953. $expected = ['Model.select'];
  1954. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1955. $this->assertEquals($expected, $result);
  1956. $this->Form->fields = [];
  1957. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1958. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1959. $this->assertEquals($expected, $result);
  1960. }
  1961. /**
  1962. * testFormSecuredRadio method
  1963. */
  1964. public function testFormSecuredRadio(): void
  1965. {
  1966. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1967. $this->Form->create();
  1968. $options = ['1' => 'option1', '2' => 'option2'];
  1969. $this->Form->radio('Test.test', $options);
  1970. $expected = ['Test.test'];
  1971. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1972. $this->assertEquals($expected, $result);
  1973. $this->Form->radio('Test.all', $options, [
  1974. 'disabled' => ['option1', 'option2'],
  1975. ]);
  1976. $expected = ['Test.test', 'Test.all' => ''];
  1977. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1978. $this->assertEquals($expected, $result);
  1979. $this->Form->radio('Test.some', $options, [
  1980. 'disabled' => ['option1'],
  1981. ]);
  1982. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1983. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1984. $this->assertEquals($expected, $result);
  1985. }
  1986. /**
  1987. * testFormSecuredAndDisabled method
  1988. *
  1989. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  1990. * hashing.
  1991. */
  1992. public function testFormSecuredAndDisabled(): void
  1993. {
  1994. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1995. $this->Form->create();
  1996. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  1997. $this->Form->text('Model.text', ['disabled' => true]);
  1998. $this->Form->password('Model.text', ['disabled' => true]);
  1999. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2000. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2001. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2002. $this->Form->year('Model.year', ['disabled' => true]);
  2003. $this->Form->month('Model.month', ['disabled' => true]);
  2004. $this->Form->day('Model.day', ['disabled' => true]);
  2005. $this->Form->hour('Model.hour', ['disabled' => true]);
  2006. $this->Form->minute('Model.minute', ['disabled' => true]);
  2007. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2008. $expected = [
  2009. 'Model.radio' => '',
  2010. ];
  2011. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2012. $this->assertEquals($expected, $result);
  2013. }
  2014. /**
  2015. * testUnlockFieldAddsToList method
  2016. *
  2017. * Test disableField.
  2018. */
  2019. public function testUnlockFieldAddsToList(): void
  2020. {
  2021. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2022. 'unlockedFields' => [],
  2023. ]));
  2024. $this->Form->create();
  2025. $this->Form->unlockField('Contact.name');
  2026. $this->Form->text('Contact.name');
  2027. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2028. $this->assertEquals([], $result);
  2029. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2030. $this->assertEquals(['Contact.name'], $result);
  2031. }
  2032. /**
  2033. * testUnlockFieldRemovingFromFields method
  2034. *
  2035. * Test unlockField removing from fields array.
  2036. */
  2037. public function testUnlockFieldRemovingFromFields(): void
  2038. {
  2039. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2040. 'unlockedFields' => [],
  2041. ]));
  2042. $this->Form->create($this->article);
  2043. $this->Form->hidden('Article.id', ['value' => 1]);
  2044. $this->Form->text('Article.title');
  2045. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2046. $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
  2047. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2048. $this->Form->unlockField('Article.title');
  2049. $this->Form->unlockField('Article.id');
  2050. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2051. $this->assertEquals([], $result);
  2052. }
  2053. /**
  2054. * testResetUnlockFields method
  2055. *
  2056. * Test reset unlockFields, when create new form.
  2057. */
  2058. public function testResetUnlockFields(): void
  2059. {
  2060. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2061. 'key' => 'testKey',
  2062. 'unlockedFields' => [],
  2063. ]));
  2064. $this->Form->create();
  2065. $this->Form->unlockField('Contact.id');
  2066. $this->Form->hidden('Contact.id', ['value' => 1]);
  2067. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2068. $this->assertEmpty($result, 'Field should be unlocked');
  2069. $this->Form->end();
  2070. $this->Form->create();
  2071. $this->Form->hidden('Contact.id', ['value' => 1]);
  2072. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2073. $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
  2074. }
  2075. /**
  2076. * testSecuredFormUrlIgnoresHost method
  2077. *
  2078. * Test that only the path + query elements of a form's URL show up in their hash.
  2079. */
  2080. public function testSecuredFormUrlIgnoresHost(): void
  2081. {
  2082. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2083. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2084. $this->Form->create($this->article, [
  2085. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2086. ]);
  2087. $result = $this->Form->secure();
  2088. $this->assertStringContainsString($expected, $result);
  2089. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2090. $result = $this->Form->secure();
  2091. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2092. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2093. $result = $this->Form->secure();
  2094. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2095. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2096. $result = $this->Form->secure();
  2097. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2098. }
  2099. /**
  2100. * testSecuredFormUrlHasHtmlAndIdentifier method
  2101. *
  2102. * Test that URL, HTML and identifier show up in their hashes.
  2103. */
  2104. public function testSecuredFormUrlHasHtmlAndIdentifier(): void
  2105. {
  2106. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2107. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2108. $this->Form->create($this->article, [
  2109. 'url' => [
  2110. 'controller' => 'articles',
  2111. 'action' => 'view',
  2112. '?' => [
  2113. 'page' => 1,
  2114. 'limit' => 10,
  2115. 'html' => '<>"',
  2116. ],
  2117. '#' => 'result',
  2118. ],
  2119. ]);
  2120. $result = $this->Form->secure();
  2121. $this->assertStringContainsString($expected, $result);
  2122. $this->Form->create($this->article, [
  2123. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2124. ]);
  2125. $result = $this->Form->secure();
  2126. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2127. $this->Form->create($this->article, [
  2128. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2129. ]);
  2130. $result = $this->Form->secure();
  2131. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2132. }
  2133. /**
  2134. * testErrorMessageDisplay method
  2135. *
  2136. * Test error message display.
  2137. */
  2138. public function testErrorMessageDisplay(): void
  2139. {
  2140. $this->article['errors'] = [
  2141. 'Article' => [
  2142. 'title' => 'error message',
  2143. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2144. ],
  2145. ];
  2146. $this->Form->create($this->article);
  2147. $result = $this->Form->control('Article.title');
  2148. $expected = [
  2149. 'div' => ['class' => 'input text error'],
  2150. 'label' => ['for' => 'article-title'],
  2151. 'Title',
  2152. '/label',
  2153. 'input' => [
  2154. 'type' => 'text',
  2155. 'name' => 'Article[title]',
  2156. 'id' => 'article-title',
  2157. 'class' => 'form-error',
  2158. 'aria-invalid' => 'true',
  2159. 'aria-describedby' => 'article-title-error',
  2160. ],
  2161. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2162. 'error message',
  2163. '/div',
  2164. '/div',
  2165. ];
  2166. $this->assertHtml($expected, $result);
  2167. $result = $this->Form->control('Article.title', [
  2168. 'templates' => [
  2169. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2170. ],
  2171. ]);
  2172. $expected = [
  2173. 'div' => ['class' => 'input text error'],
  2174. 'label' => ['for' => 'article-title'],
  2175. 'Title',
  2176. '/label',
  2177. 'input' => [
  2178. 'type' => 'text',
  2179. 'name' => 'Article[title]',
  2180. 'id' => 'article-title',
  2181. 'class' => 'form-error',
  2182. // No aria-describedby because error template is custom
  2183. 'aria-invalid' => 'true',
  2184. ],
  2185. '/div',
  2186. ];
  2187. $this->assertHtml($expected, $result);
  2188. $result = $this->Form->control('Article.content');
  2189. $expected = [
  2190. 'div' => ['class' => 'input text error'],
  2191. 'label' => ['for' => 'article-content'],
  2192. 'Content',
  2193. '/label',
  2194. 'input' => [
  2195. 'type' => 'text',
  2196. 'name' => 'Article[content]',
  2197. 'id' => 'article-content',
  2198. 'class' => 'form-error',
  2199. 'aria-invalid' => 'true',
  2200. 'aria-describedby' => 'article-content-error',
  2201. ],
  2202. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2203. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2204. '/div',
  2205. '/div',
  2206. ];
  2207. $this->assertHtml($expected, $result);
  2208. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2209. $expected = [
  2210. 'div' => ['class' => 'input text error'],
  2211. 'label' => ['for' => 'article-content'],
  2212. 'Content',
  2213. '/label',
  2214. 'input' => [
  2215. 'type' => 'text',
  2216. 'name' => 'Article[content]',
  2217. 'id' => 'article-content',
  2218. 'class' => 'form-error',
  2219. 'aria-invalid' => 'true',
  2220. 'aria-describedby' => 'article-content-error',
  2221. ],
  2222. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2223. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2224. '/div',
  2225. '/div',
  2226. ];
  2227. $this->assertHtml($expected, $result);
  2228. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2229. $expected = [
  2230. 'div' => ['class' => 'input text error'],
  2231. 'label' => ['for' => 'article-content'],
  2232. 'Content',
  2233. '/label',
  2234. 'input' => [
  2235. 'type' => 'text',
  2236. 'name' => 'Article[content]',
  2237. 'id' => 'article-content',
  2238. 'class' => 'form-error',
  2239. 'aria-invalid' => 'true',
  2240. 'aria-describedby' => 'article-content-error',
  2241. ],
  2242. ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
  2243. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2244. '/div',
  2245. '/div',
  2246. ];
  2247. $this->assertHtml($expected, $result);
  2248. }
  2249. /**
  2250. * testEmptyErrorValidation method
  2251. *
  2252. * Test validation errors, when validation message is an empty string.
  2253. */
  2254. public function testEmptyErrorValidation(): void
  2255. {
  2256. $this->article['errors'] = [
  2257. 'Article' => ['title' => ''],
  2258. ];
  2259. $this->Form->create($this->article);
  2260. $result = $this->Form->control('Article.title');
  2261. $expected = [
  2262. 'div' => ['class' => 'input text error'],
  2263. 'label' => ['for' => 'article-title'],
  2264. 'Title',
  2265. '/label',
  2266. 'input' => [
  2267. 'type' => 'text',
  2268. 'name' => 'Article[title]',
  2269. 'id' => 'article-title',
  2270. 'class' => 'form-error',
  2271. 'aria-invalid' => 'true',
  2272. 'aria-describedby' => 'article-title-error',
  2273. ],
  2274. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2275. [],
  2276. '/div',
  2277. '/div',
  2278. ];
  2279. $this->assertHtml($expected, $result);
  2280. }
  2281. /**
  2282. * testEmptyControlErrorValidation method
  2283. *
  2284. * Test validation errors, when calling control() overriding validation message by an empty string.
  2285. */
  2286. public function testEmptyControlErrorValidation(): void
  2287. {
  2288. $this->article['errors'] = [
  2289. 'Article' => ['title' => 'error message'],
  2290. ];
  2291. $this->Form->create($this->article);
  2292. $result = $this->Form->control('Article.title', ['error' => '']);
  2293. $expected = [
  2294. 'div' => ['class' => 'input text error'],
  2295. 'label' => ['for' => 'article-title'],
  2296. 'Title',
  2297. '/label',
  2298. 'input' => [
  2299. 'aria-invalid' => 'true',
  2300. 'aria-describedby' => 'article-title-error',
  2301. 'type' => 'text',
  2302. 'name' => 'Article[title]',
  2303. 'id' => 'article-title',
  2304. 'class' => 'form-error',
  2305. ],
  2306. ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
  2307. [],
  2308. '/div',
  2309. '/div',
  2310. ];
  2311. $this->assertHtml($expected, $result);
  2312. }
  2313. /**
  2314. * testControlErrorMessage method
  2315. *
  2316. * Test validation errors, when calling control() overriding validation messages.
  2317. */
  2318. public function testControlErrorMessage(): void
  2319. {
  2320. $this->article['errors'] = [
  2321. 'title' => ['error message'],
  2322. ];
  2323. $this->Form->create($this->article);
  2324. $result = $this->Form->control('title', [
  2325. 'error' => 'Custom error!',
  2326. ]);
  2327. $expected = [
  2328. 'div' => ['class' => 'input text required error'],
  2329. 'label' => ['for' => 'title'],
  2330. 'Title',
  2331. '/label',
  2332. 'input' => [
  2333. 'type' => 'text',
  2334. 'name' => 'title',
  2335. 'id' => 'title',
  2336. 'class' => 'form-error',
  2337. 'required' => 'required',
  2338. 'data-validity-message' => 'This field cannot be left empty',
  2339. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2340. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2341. 'aria-required' => 'true',
  2342. 'aria-invalid' => 'true',
  2343. 'aria-describedby' => 'title-error',
  2344. ],
  2345. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2346. 'Custom error!',
  2347. '/div',
  2348. '/div',
  2349. ];
  2350. $this->assertHtml($expected, $result);
  2351. $result = $this->Form->control('title', [
  2352. 'error' => ['error message' => 'Custom error!'],
  2353. ]);
  2354. $expected = [
  2355. 'div' => ['class' => 'input text required error'],
  2356. 'label' => ['for' => 'title'],
  2357. 'Title',
  2358. '/label',
  2359. 'input' => [
  2360. 'type' => 'text',
  2361. 'name' => 'title',
  2362. 'id' => 'title',
  2363. 'aria-required' => 'true',
  2364. 'aria-invalid' => 'true',
  2365. 'aria-describedby' => 'title-error',
  2366. 'class' => 'form-error',
  2367. 'required' => 'required',
  2368. 'data-validity-message' => 'This field cannot be left empty',
  2369. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2370. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2371. ],
  2372. ['div' => ['class' => 'error-message', 'id' => 'title-error']],
  2373. 'Custom error!',
  2374. '/div',
  2375. '/div',
  2376. ];
  2377. $this->assertHtml($expected, $result);
  2378. }
  2379. /**
  2380. * testFormValidationAssociated method
  2381. *
  2382. * Tests displaying errors for nested entities.
  2383. */
  2384. public function testFormValidationAssociated(): void
  2385. {
  2386. $nested = new Entity(['foo' => 'bar']);
  2387. $nested->setError('foo', ['not a valid bar']);
  2388. $entity = new Entity(['nested' => $nested]);
  2389. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2390. $result = $this->Form->error('nested.foo');
  2391. $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
  2392. }
  2393. /**
  2394. * testFormValidationAssociatedSecondLevel method
  2395. *
  2396. * Test form error display with associated model.
  2397. */
  2398. public function testFormValidationAssociatedSecondLevel(): void
  2399. {
  2400. $inner = new Entity(['bar' => 'baz']);
  2401. $nested = new Entity(['foo' => $inner]);
  2402. $entity = new Entity(['nested' => $nested]);
  2403. $inner->setError('bar', ['not a valid one']);
  2404. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2405. $result = $this->Form->error('nested.foo.bar');
  2406. $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
  2407. }
  2408. /**
  2409. * testFormValidationMultiRecord method
  2410. *
  2411. * Test form error display with multiple records.
  2412. */
  2413. public function testFormValidationMultiRecord(): void
  2414. {
  2415. $one = new Entity();
  2416. $two = new Entity();
  2417. $this->getTableLocator()->get('Contacts', [
  2418. 'className' => ContactsTable::class,
  2419. ]);
  2420. $one->set('email', '');
  2421. $one->setError('email', ['invalid email']);
  2422. $two->set('name', '');
  2423. $two->setError('name', ['This is wrong']);
  2424. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2425. $result = $this->Form->control('0.email');
  2426. $expected = [
  2427. 'div' => ['class' => 'input email error'],
  2428. 'label' => ['for' => '0-email'],
  2429. 'Email',
  2430. '/label',
  2431. 'input' => [
  2432. 'type' => 'email',
  2433. 'name' => '0[email]',
  2434. 'id' => '0-email',
  2435. 'class' => 'form-error',
  2436. 'maxlength' => 255,
  2437. 'value' => '',
  2438. 'aria-invalid' => 'true',
  2439. 'aria-describedby' => '0-email-error',
  2440. ],
  2441. ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
  2442. 'invalid email',
  2443. '/div',
  2444. '/div',
  2445. ];
  2446. $this->assertHtml($expected, $result);
  2447. $result = $this->Form->control('1.name');
  2448. $expected = [
  2449. 'div' => ['class' => 'input text error'],
  2450. 'label' => ['for' => '1-name'],
  2451. 'Name',
  2452. '/label',
  2453. 'input' => [
  2454. 'type' => 'text',
  2455. 'name' => '1[name]',
  2456. 'id' => '1-name',
  2457. 'class' => 'form-error',
  2458. 'maxlength' => 255,
  2459. 'value' => '',
  2460. 'aria-invalid' => 'true',
  2461. 'aria-describedby' => '1-name-error',
  2462. ],
  2463. ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
  2464. 'This is wrong',
  2465. '/div',
  2466. '/div',
  2467. ];
  2468. $this->assertHtml($expected, $result);
  2469. }
  2470. /**
  2471. * testControl method
  2472. *
  2473. * Test various incarnations of control().
  2474. */
  2475. public function testControl(): void
  2476. {
  2477. $this->getTableLocator()->get('ValidateUsers', [
  2478. 'className' => ValidateUsersTable::class,
  2479. ]);
  2480. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2481. $result = $this->Form->control('ValidateUsers.balance');
  2482. $expected = [
  2483. 'div' => ['class'],
  2484. 'label' => ['for'],
  2485. 'Balance',
  2486. '/label',
  2487. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2488. '/div',
  2489. ];
  2490. $this->assertHtml($expected, $result);
  2491. $result = $this->Form->control('ValidateUser.cost_decimal');
  2492. $expected = [
  2493. 'div' => ['class'],
  2494. 'label' => ['for'],
  2495. 'Cost Decimal',
  2496. '/label',
  2497. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2498. '/div',
  2499. ];
  2500. $this->assertHtml($expected, $result);
  2501. $result = $this->Form->control('ValidateUser.null_decimal');
  2502. $expected = [
  2503. 'div' => ['class'],
  2504. 'label' => ['for'],
  2505. 'Null Decimal',
  2506. '/label',
  2507. 'input' => ['name', 'type' => 'number', 'id'],
  2508. '/div',
  2509. ];
  2510. $this->assertHtml($expected, $result);
  2511. }
  2512. /**
  2513. * testControlCustomization method
  2514. *
  2515. * Tests the input method and passing custom options.
  2516. */
  2517. public function testControlCustomization(): void
  2518. {
  2519. $this->getTableLocator()->get('Contacts', [
  2520. 'className' => ContactsTable::class,
  2521. ]);
  2522. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2523. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2524. $expected = [
  2525. 'div' => ['class' => 'input email'],
  2526. 'label' => ['for' => 'custom'],
  2527. 'Email',
  2528. '/label',
  2529. ['input' => [
  2530. 'type' => 'email', 'name' => 'Contact[email]',
  2531. 'id' => 'custom', 'maxlength' => 255,
  2532. ]],
  2533. '/div',
  2534. ];
  2535. $this->assertHtml($expected, $result);
  2536. $result = $this->Form->control('Contact.email', [
  2537. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2538. ]);
  2539. $expected = [
  2540. '<div',
  2541. 'label' => ['for' => 'contact-email'],
  2542. 'Email',
  2543. '/label',
  2544. ['input' => [
  2545. 'type' => 'email', 'name' => 'Contact[email]',
  2546. 'id' => 'contact-email', 'maxlength' => 255,
  2547. ]],
  2548. '/div',
  2549. ];
  2550. $this->assertHtml($expected, $result);
  2551. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2552. $expected = [
  2553. 'div' => ['class' => 'input text'],
  2554. 'label' => ['for' => 'contact-email'],
  2555. 'Email',
  2556. '/label',
  2557. ['input' => [
  2558. 'type' => 'text', 'name' => 'Contact[email]',
  2559. 'id' => 'contact-email', 'maxlength' => '255',
  2560. ]],
  2561. '/div',
  2562. ];
  2563. $this->assertHtml($expected, $result);
  2564. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2565. $expected = [
  2566. 'div' => ['class' => 'input text'],
  2567. 'label' => ['for' => 'contact-5-email'],
  2568. 'Email',
  2569. '/label',
  2570. ['input' => [
  2571. 'type' => 'text', 'name' => 'Contact[5][email]',
  2572. 'id' => 'contact-5-email', 'maxlength' => '255',
  2573. ]],
  2574. '/div',
  2575. ];
  2576. $this->assertHtml($expected, $result);
  2577. $result = $this->Form->control('Contact.password');
  2578. $expected = [
  2579. 'div' => ['class' => 'input password'],
  2580. 'label' => ['for' => 'contact-password'],
  2581. 'Password',
  2582. '/label',
  2583. ['input' => [
  2584. 'type' => 'password', 'name' => 'Contact[password]',
  2585. 'id' => 'contact-password',
  2586. ]],
  2587. '/div',
  2588. ];
  2589. $this->assertHtml($expected, $result);
  2590. $result = $this->Form->control('Contact.email', [
  2591. 'type' => 'file', 'class' => 'textbox',
  2592. ]);
  2593. $expected = [
  2594. 'div' => ['class' => 'input file'],
  2595. 'label' => ['for' => 'contact-email'],
  2596. 'Email',
  2597. '/label',
  2598. ['input' => [
  2599. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2600. 'id' => 'contact-email',
  2601. ]],
  2602. '/div',
  2603. ];
  2604. $this->assertHtml($expected, $result);
  2605. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2606. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2607. $result = $this->Form->control('phone');
  2608. $expected = [
  2609. 'div' => ['class' => 'input tel'],
  2610. 'label' => ['for' => 'phone'],
  2611. 'Phone',
  2612. '/label',
  2613. ['input' => [
  2614. 'type' => 'tel', 'name' => 'phone',
  2615. 'value' => 'Hello &amp; World &gt; weird chars',
  2616. 'id' => 'phone', 'maxlength' => 255,
  2617. ]],
  2618. '/div',
  2619. ];
  2620. $this->assertHtml($expected, $result);
  2621. $this->View->setRequest(
  2622. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2623. );
  2624. $this->Form->create();
  2625. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2626. $expected = [
  2627. 'div' => ['class' => 'input text'],
  2628. 'label' => ['for' => 'myId'],
  2629. 'Field',
  2630. '/label',
  2631. 'input' => [
  2632. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2633. 'value' => 'My value', 'id' => 'myId',
  2634. ],
  2635. '/div',
  2636. ];
  2637. $this->assertHtml($expected, $result);
  2638. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2639. $this->Form->create();
  2640. $entity->setError('field', 'Badness!');
  2641. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2642. $result = $this->Form->control('field');
  2643. $expected = [
  2644. 'div' => ['class' => 'input text error'],
  2645. 'label' => ['for' => 'field'],
  2646. 'Field',
  2647. '/label',
  2648. 'input' => [
  2649. 'type' => 'text',
  2650. 'name' => 'field',
  2651. 'id' => 'field',
  2652. 'class' => 'form-error',
  2653. 'aria-invalid' => 'true',
  2654. 'aria-describedby' => 'field-error',
  2655. ],
  2656. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2657. 'Badness!',
  2658. '/div',
  2659. '/div',
  2660. ];
  2661. $this->assertHtml($expected, $result);
  2662. $result = $this->Form->control('field', [
  2663. 'templates' => [
  2664. 'inputContainerError' => '{{content}}{{error}}',
  2665. 'error' => '<span class="error-message">{{content}}</span>',
  2666. ],
  2667. ]);
  2668. $expected = [
  2669. 'label' => ['for' => 'field'],
  2670. 'Field',
  2671. '/label',
  2672. 'input' => [
  2673. 'type' => 'text',
  2674. 'name' => 'field',
  2675. 'id' => 'field',
  2676. 'class' => 'form-error',
  2677. // No aria-describedby because error template is custom
  2678. 'aria-invalid' => 'true',
  2679. ],
  2680. ['span' => ['class' => 'error-message']],
  2681. 'Badness!',
  2682. '/span',
  2683. ];
  2684. $this->assertHtml($expected, $result);
  2685. $entity->setError('field', ['minLength'], true);
  2686. $result = $this->Form->control('field', [
  2687. 'error' => [
  2688. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2689. 'maxLength' => 'login too large',
  2690. ],
  2691. ]);
  2692. $expected = [
  2693. 'div' => ['class' => 'input text error'],
  2694. 'label' => ['for' => 'field'],
  2695. 'Field',
  2696. '/label',
  2697. 'input' => [
  2698. 'type' => 'text',
  2699. 'name' => 'field',
  2700. 'id' => 'field',
  2701. 'class' => 'form-error',
  2702. 'aria-invalid' => 'true',
  2703. 'aria-describedby' => 'field-error',
  2704. ],
  2705. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2706. 'Le login doit contenir au moins 2 caractères',
  2707. '/div',
  2708. '/div',
  2709. ];
  2710. $this->assertHtml($expected, $result);
  2711. $entity->setError('field', ['maxLength'], true);
  2712. $result = $this->Form->control('field', [
  2713. 'error' => [
  2714. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2715. 'maxLength' => 'login too large',
  2716. ],
  2717. ]);
  2718. $expected = [
  2719. 'div' => ['class' => 'input text error'],
  2720. 'label' => ['for' => 'field'],
  2721. 'Field',
  2722. '/label',
  2723. 'input' => [
  2724. 'type' => 'text',
  2725. 'name' => 'field',
  2726. 'id' => 'field',
  2727. 'class' => 'form-error',
  2728. 'aria-invalid' => 'true',
  2729. 'aria-describedby' => 'field-error',
  2730. ],
  2731. ['div' => ['class' => 'error-message', 'id' => 'field-error']],
  2732. 'login too large',
  2733. '/div',
  2734. '/div',
  2735. ];
  2736. $this->assertHtml($expected, $result);
  2737. }
  2738. /**
  2739. * testControlWithTemplateFile method
  2740. *
  2741. * Test that control() accepts a template file.
  2742. */
  2743. public function testControlWithTemplateFile(): void
  2744. {
  2745. $result = $this->Form->control('field', [
  2746. 'templates' => 'htmlhelper_tags',
  2747. ]);
  2748. $expected = [
  2749. 'label' => ['for' => 'field'],
  2750. 'Field',
  2751. '/label',
  2752. 'input' => [
  2753. 'type' => 'text', 'name' => 'field',
  2754. 'id' => 'field',
  2755. ],
  2756. ];
  2757. $this->assertHtml($expected, $result);
  2758. }
  2759. /**
  2760. * testNestedControlsEndWithBrackets method
  2761. *
  2762. * Test that nested inputs end with brackets.
  2763. */
  2764. public function testNestedControlsEndWithBrackets(): void
  2765. {
  2766. $result = $this->Form->text('nested.text[]');
  2767. $expected = [
  2768. 'input' => [
  2769. 'type' => 'text', 'name' => 'nested[text][]',
  2770. ],
  2771. ];
  2772. $this->assertHtml($expected, $result);
  2773. $result = $this->Form->file('nested.file[]');
  2774. $expected = [
  2775. 'input' => [
  2776. 'type' => 'file', 'name' => 'nested[file][]',
  2777. ],
  2778. ];
  2779. $this->assertHtml($expected, $result);
  2780. }
  2781. /**
  2782. * testCreateIdPrefix method
  2783. *
  2784. * Test id prefix.
  2785. */
  2786. public function testCreateIdPrefix(): void
  2787. {
  2788. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2789. $result = $this->Form->control('field');
  2790. $expected = [
  2791. 'div' => ['class' => 'input text'],
  2792. 'label' => ['for' => 'prefix-field'],
  2793. 'Field',
  2794. '/label',
  2795. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2796. '/div',
  2797. ];
  2798. $this->assertHtml($expected, $result);
  2799. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2800. $expected = [
  2801. 'div' => ['class' => 'input text'],
  2802. 'label' => ['for' => 'custom-id'],
  2803. 'Field',
  2804. '/label',
  2805. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2806. '/div',
  2807. ];
  2808. $this->assertHtml($expected, $result);
  2809. $result = $this->Form->radio('Model.field', ['option A']);
  2810. $expected = [
  2811. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2812. 'label' => ['for' => 'prefix-model-field-0'],
  2813. ['input' => [
  2814. 'type' => 'radio',
  2815. 'name' => 'Model[field]',
  2816. 'value' => '0',
  2817. 'id' => 'prefix-model-field-0',
  2818. ]],
  2819. 'option A',
  2820. '/label',
  2821. ];
  2822. $this->assertHtml($expected, $result);
  2823. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2824. $expected = [
  2825. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2826. 'label' => ['for' => 'prefix-model-field-0'],
  2827. ['input' => [
  2828. 'type' => 'radio',
  2829. 'name' => 'Model[field]',
  2830. 'value' => '0',
  2831. 'id' => 'prefix-model-field-0',
  2832. ]],
  2833. 'option A',
  2834. '/label',
  2835. ];
  2836. $this->assertHtml($expected, $result);
  2837. $result = $this->Form->select(
  2838. 'Model.multi_field',
  2839. ['first'],
  2840. ['multiple' => 'checkbox']
  2841. );
  2842. $expected = [
  2843. 'input' => [
  2844. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  2845. ],
  2846. ['div' => ['class' => 'checkbox']],
  2847. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2848. ['input' => [
  2849. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2850. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2851. ]],
  2852. 'first',
  2853. '/label',
  2854. '/div',
  2855. ];
  2856. $this->assertHtml($expected, $result);
  2857. $this->Form->end();
  2858. $result = $this->Form->control('field');
  2859. $expected = [
  2860. 'div' => ['class' => 'input text'],
  2861. 'label' => ['for' => 'field'],
  2862. 'Field',
  2863. '/label',
  2864. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2865. '/div',
  2866. ];
  2867. $this->assertHtml($expected, $result);
  2868. }
  2869. /**
  2870. * testControlZero method
  2871. *
  2872. * Test that inputs with 0 can be created.
  2873. */
  2874. public function testControlZero(): void
  2875. {
  2876. $this->getTableLocator()->get('Contacts', [
  2877. 'className' => ContactsTable::class,
  2878. ]);
  2879. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2880. $result = $this->Form->control('0');
  2881. $expected = [
  2882. 'div' => ['class' => 'input text'],
  2883. 'label' => ['for' => '0'], '/label',
  2884. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2885. '/div',
  2886. ];
  2887. $this->assertHtml($expected, $result);
  2888. }
  2889. /**
  2890. * testControlCheckbox method
  2891. *
  2892. * Test control() with checkbox creation.
  2893. */
  2894. public function testControlCheckbox(): void
  2895. {
  2896. $articles = $this->getTableLocator()->get('Articles');
  2897. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2898. $article = $articles->newEmptyEntity();
  2899. $this->Form->create($article);
  2900. $result = $this->Form->control('Articles.active');
  2901. $expected = [
  2902. 'div' => ['class' => 'input checkbox'],
  2903. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2904. 'label' => ['for' => 'articles-active'],
  2905. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  2906. 'Active',
  2907. '/label',
  2908. '/div',
  2909. ];
  2910. $this->assertHtml($expected, $result);
  2911. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  2912. $expected = [
  2913. 'div' => ['class' => 'input checkbox'],
  2914. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2915. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2916. '/div',
  2917. ];
  2918. $this->assertHtml($expected, $result);
  2919. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  2920. $expected = [
  2921. 'div' => ['class' => 'input checkbox'],
  2922. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2923. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2924. '/div',
  2925. ];
  2926. $this->assertHtml($expected, $result);
  2927. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  2928. $expected = [
  2929. 'div' => ['class' => 'input checkbox'],
  2930. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2931. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  2932. '/div',
  2933. ];
  2934. $this->assertHtml($expected, $result);
  2935. $result = $this->Form->control('Articles.disabled', [
  2936. 'label' => 'Disabled',
  2937. 'type' => 'checkbox',
  2938. 'data-foo' => 'disabled',
  2939. ]);
  2940. $expected = [
  2941. 'div' => ['class' => 'input checkbox'],
  2942. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  2943. 'label' => ['for' => 'articles-disabled'],
  2944. ['input' => [
  2945. 'type' => 'checkbox',
  2946. 'name' => 'Articles[disabled]',
  2947. 'value' => '1',
  2948. 'id' => 'articles-disabled',
  2949. 'data-foo' => 'disabled',
  2950. ]],
  2951. 'Disabled',
  2952. '/label',
  2953. '/div',
  2954. ];
  2955. $this->assertHtml($expected, $result);
  2956. $result = $this->Form->control('Articles.confirm', [
  2957. 'label' => 'Confirm <b>me</b>!',
  2958. 'type' => 'checkbox',
  2959. 'escape' => false,
  2960. ]);
  2961. $expected = [
  2962. 'div' => ['class' => 'input checkbox'],
  2963. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  2964. 'label' => ['for' => 'articles-confirm'],
  2965. ['input' => [
  2966. 'type' => 'checkbox',
  2967. 'name' => 'Articles[confirm]',
  2968. 'value' => '1',
  2969. 'id' => 'articles-confirm',
  2970. ]],
  2971. 'Confirm <b>me</b>!',
  2972. '/label',
  2973. '/div',
  2974. ];
  2975. $this->assertHtml($expected, $result);
  2976. }
  2977. /**
  2978. * testControlHidden method
  2979. *
  2980. * Test that control() does not create wrapping div and label tag for hidden fields.
  2981. */
  2982. public function testControlHidden(): void
  2983. {
  2984. $this->getTableLocator()->get('ValidateUsers', [
  2985. 'className' => ValidateUsersTable::class,
  2986. ]);
  2987. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2988. $result = $this->Form->control('ValidateUser.id');
  2989. $expected = [
  2990. 'input' => ['name', 'type' => 'hidden', 'id'],
  2991. ];
  2992. $this->assertHtml($expected, $result);
  2993. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  2994. $expected = [
  2995. 'input' => ['name', 'type' => 'hidden', 'id'],
  2996. ];
  2997. $this->assertHtml($expected, $result);
  2998. }
  2999. /**
  3000. * testControlDatetime method
  3001. *
  3002. * Test form->control() with datetime.
  3003. */
  3004. public function testControlDatetime(): void
  3005. {
  3006. $result = $this->Form->control('prueba', [
  3007. 'type' => 'datetime',
  3008. 'value' => new DateTime('2019-09-27 02:52:43'),
  3009. ]);
  3010. $expected = [
  3011. 'div' => ['class' => 'input datetime'],
  3012. 'label' => ['for' => 'prueba'],
  3013. 'Prueba',
  3014. '/label',
  3015. 'input' => [
  3016. 'name' => 'prueba',
  3017. 'id' => 'prueba',
  3018. 'type' => 'datetime-local',
  3019. 'value' => '2019-09-27T02:52:43',
  3020. 'step' => '1',
  3021. ],
  3022. '/div',
  3023. ];
  3024. $this->assertHtml($expected, $result);
  3025. }
  3026. /**
  3027. * testControlDatetimeIdPrefix method
  3028. *
  3029. * Test form->control() with datetime with id prefix.
  3030. */
  3031. public function testControlDatetimeIdPrefix(): void
  3032. {
  3033. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3034. $result = $this->Form->control('prueba', [
  3035. 'type' => 'datetime',
  3036. ]);
  3037. $expected = [
  3038. 'div' => ['class' => 'input datetime'],
  3039. 'label' => ['for' => 'prefix-prueba'],
  3040. 'Prueba',
  3041. '/label',
  3042. 'input' => [
  3043. 'name' => 'prueba',
  3044. 'id' => 'prefix-prueba',
  3045. 'type' => 'datetime-local',
  3046. 'value' => '',
  3047. 'step' => '1',
  3048. ],
  3049. '/div',
  3050. ];
  3051. $this->assertHtml($expected, $result);
  3052. }
  3053. /**
  3054. * testControlDatetimeStep method
  3055. *
  3056. * Test form->control() with datetime with custom step size.
  3057. */
  3058. public function testControlDatetimeStep(): void
  3059. {
  3060. $result = $this->Form->control('prueba', [
  3061. 'type' => 'datetime',
  3062. 'value' => new DateTime('2019-09-27 02:52:43'),
  3063. 'step' => '0.5',
  3064. ]);
  3065. $expected = [
  3066. 'div' => ['class' => 'input datetime'],
  3067. 'label' => ['for' => 'prueba'],
  3068. 'Prueba',
  3069. '/label',
  3070. 'input' => [
  3071. 'name' => 'prueba',
  3072. 'id' => 'prueba',
  3073. 'type' => 'datetime-local',
  3074. 'value' => '2019-09-27T02:52:43.000',
  3075. 'step' => '0.5',
  3076. ],
  3077. '/div',
  3078. ];
  3079. $this->assertHtml($expected, $result);
  3080. }
  3081. /**
  3082. * testControlCheckboxWithDisabledElements method
  3083. *
  3084. * Test generating checkboxes with disabled elements.
  3085. */
  3086. public function testControlCheckboxWithDisabledElements(): void
  3087. {
  3088. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3089. $result = $this->Form->control('Contact.multiple', [
  3090. 'multiple' => 'checkbox',
  3091. 'disabled' => 'disabled',
  3092. 'options' => $options,
  3093. ]);
  3094. $expected = [
  3095. ['div' => ['class' => 'input select']],
  3096. ['label' => ['for' => 'contact-multiple']],
  3097. 'Multiple',
  3098. '/label',
  3099. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3100. ['div' => ['class' => 'checkbox']],
  3101. ['label' => ['for' => 'contact-multiple-1']],
  3102. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3103. 'One',
  3104. '/label',
  3105. '/div',
  3106. ['div' => ['class' => 'checkbox']],
  3107. ['label' => ['for' => 'contact-multiple-2']],
  3108. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3109. 'Two',
  3110. '/label',
  3111. '/div',
  3112. ['div' => ['class' => 'checkbox']],
  3113. ['label' => ['for' => 'contact-multiple-3']],
  3114. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3115. 'Three',
  3116. '/label',
  3117. '/div',
  3118. '/div',
  3119. ];
  3120. $this->assertHtml($expected, $result);
  3121. // make sure 50 does only disable 50, and not 50f5c0cf
  3122. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3123. $disabled = [50];
  3124. $expected = [
  3125. ['div' => ['class' => 'input select']],
  3126. ['label' => ['for' => 'contact-multiple']],
  3127. 'Multiple',
  3128. '/label',
  3129. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3130. ['div' => ['class' => 'checkbox']],
  3131. ['label' => ['for' => 'contact-multiple-50']],
  3132. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3133. 'Fifty',
  3134. '/label',
  3135. '/div',
  3136. ['div' => ['class' => 'checkbox']],
  3137. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3138. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3139. 'Stringy',
  3140. '/label',
  3141. '/div',
  3142. '/div',
  3143. ];
  3144. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3145. $this->assertHtml($expected, $result);
  3146. }
  3147. /**
  3148. * testControlWithLeadingInteger method
  3149. *
  3150. * Test input name with leading integer, ensure attributes are generated correctly.
  3151. */
  3152. public function testControlWithLeadingInteger(): void
  3153. {
  3154. $result = $this->Form->text('0.Node.title');
  3155. $expected = [
  3156. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3157. ];
  3158. $this->assertHtml($expected, $result);
  3159. }
  3160. /**
  3161. * testControlSelectType method
  3162. *
  3163. * Test form->control() with select type inputs.
  3164. */
  3165. public function testControlSelectType(): void
  3166. {
  3167. $result = $this->Form->control(
  3168. 'email',
  3169. [
  3170. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3171. );
  3172. $expected = [
  3173. 'div' => ['class' => 'input select'],
  3174. 'label' => ['for' => 'email'],
  3175. 'Email',
  3176. '/label',
  3177. ['select' => ['name' => 'email', 'id' => 'email']],
  3178. ['option' => ['value' => '']],
  3179. '/option',
  3180. ['option' => ['value' => 'è']],
  3181. 'Firést',
  3182. '/option',
  3183. ['option' => ['value' => 'é']],
  3184. 'Secoènd',
  3185. '/option',
  3186. '/select',
  3187. '/div',
  3188. ];
  3189. $this->assertHtml($expected, $result);
  3190. $result = $this->Form->control(
  3191. 'email',
  3192. [
  3193. 'options' => ['First', 'Second'], 'empty' => true]
  3194. );
  3195. $expected = [
  3196. 'div' => ['class' => 'input select'],
  3197. 'label' => ['for' => 'email'],
  3198. 'Email',
  3199. '/label',
  3200. ['select' => ['name' => 'email', 'id' => 'email']],
  3201. ['option' => ['value' => '']],
  3202. '/option',
  3203. ['option' => ['value' => '0']],
  3204. 'First',
  3205. '/option',
  3206. ['option' => ['value' => '1']],
  3207. 'Second',
  3208. '/option',
  3209. '/select',
  3210. '/div',
  3211. ];
  3212. $this->assertHtml($expected, $result);
  3213. $result = $this->Form->control('email', [
  3214. 'type' => 'select',
  3215. 'options' => new ArrayObject(['First', 'Second']),
  3216. 'empty' => true,
  3217. ]);
  3218. $this->assertHtml($expected, $result);
  3219. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3220. $this->View->setRequest(
  3221. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3222. );
  3223. $this->Form->create();
  3224. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3225. $expected = [
  3226. 'div' => ['class' => 'input select'],
  3227. 'label' => ['for' => 'model-user-id'],
  3228. 'User',
  3229. '/label',
  3230. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3231. ['option' => ['value' => '']],
  3232. '/option',
  3233. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3234. 'good',
  3235. '/option',
  3236. ['option' => ['value' => 'other']],
  3237. 'bad',
  3238. '/option',
  3239. '/select',
  3240. '/div',
  3241. ];
  3242. $this->assertHtml($expected, $result);
  3243. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3244. $this->View->setRequest(
  3245. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3246. );
  3247. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3248. $expected = [
  3249. 'div' => ['class' => 'input select'],
  3250. 'label' => ['for' => 'thing-user-id'],
  3251. 'User',
  3252. '/label',
  3253. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3254. ['option' => ['value' => '']],
  3255. 'Some Empty',
  3256. '/option',
  3257. ['option' => ['value' => 'value']],
  3258. 'good',
  3259. '/option',
  3260. ['option' => ['value' => 'other']],
  3261. 'bad',
  3262. '/option',
  3263. '/select',
  3264. '/div',
  3265. ];
  3266. $this->assertHtml($expected, $result);
  3267. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3268. $this->View->setRequest(
  3269. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3270. );
  3271. $this->Form->create();
  3272. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3273. $expected = [
  3274. 'div' => ['class' => 'input select'],
  3275. 'label' => ['for' => 'thing-user-id'],
  3276. 'User',
  3277. '/label',
  3278. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3279. ['option' => ['value' => '']],
  3280. 'Some Empty',
  3281. '/option',
  3282. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3283. 'good',
  3284. '/option',
  3285. ['option' => ['value' => 'other']],
  3286. 'bad',
  3287. '/option',
  3288. '/select',
  3289. '/div',
  3290. ];
  3291. $this->assertHtml($expected, $result);
  3292. $result = $this->Form->control('Publisher.id', [
  3293. 'label' => 'Publisher',
  3294. 'type' => 'select',
  3295. 'multiple' => 'checkbox',
  3296. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3297. ]);
  3298. $expected = [
  3299. ['div' => ['class' => 'input select']],
  3300. ['label' => ['for' => 'publisher-id']],
  3301. 'Publisher',
  3302. '/label',
  3303. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3304. ['div' => ['class' => 'checkbox']],
  3305. ['label' => ['for' => 'publisher-id-value-1']],
  3306. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3307. 'Label 1',
  3308. '/label',
  3309. '/div',
  3310. ['div' => ['class' => 'checkbox']],
  3311. ['label' => ['for' => 'publisher-id-value-2']],
  3312. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3313. 'Label 2',
  3314. '/label',
  3315. '/div',
  3316. '/div',
  3317. ];
  3318. $this->assertHtml($expected, $result);
  3319. }
  3320. /**
  3321. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3322. *
  3323. * Test that control() and a non standard primary key makes a hidden input by default.
  3324. */
  3325. public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
  3326. {
  3327. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3328. $this->Form->create($this->article);
  3329. $result = $this->Form->control('title');
  3330. $expected = [
  3331. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3332. ];
  3333. $this->assertHtml($expected, $result);
  3334. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3335. $this->Form->create($this->article);
  3336. $result = $this->Form->control('title');
  3337. $expected = [
  3338. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3339. ];
  3340. $this->assertHtml($expected, $result);
  3341. $result = $this->Form->control('body');
  3342. $expected = [
  3343. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3344. ];
  3345. $this->assertHtml($expected, $result);
  3346. }
  3347. /**
  3348. * testControlOverridingMagicSelectType method
  3349. *
  3350. * Test that overriding the magic select type widget is possible.
  3351. */
  3352. public function testControlOverridingMagicSelectType(): void
  3353. {
  3354. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3355. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3356. $expected = [
  3357. 'div' => ['class' => 'input text'],
  3358. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3359. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3360. '/div',
  3361. ];
  3362. $this->assertHtml($expected, $result);
  3363. //Check that magic types still work for plural/singular vars
  3364. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3365. $result = $this->Form->control('Model.type');
  3366. $expected = [
  3367. 'div' => ['class' => 'input select'],
  3368. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3369. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3370. ['option' => ['value' => 'value']], 'good', '/option',
  3371. ['option' => ['value' => 'other']], 'bad', '/option',
  3372. '/select',
  3373. '/div',
  3374. ];
  3375. $this->assertHtml($expected, $result);
  3376. }
  3377. /**
  3378. * testControlMagicTypeDoesNotOverride method
  3379. *
  3380. * Test that inferred types do not override developer input.
  3381. */
  3382. public function testControlMagicTypeDoesNotOverride(): void
  3383. {
  3384. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3385. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3386. $expected = [
  3387. 'div' => ['class' => 'input checkbox'],
  3388. ['input' => [
  3389. 'type' => 'hidden',
  3390. 'name' => 'Model[user]',
  3391. 'value' => 0,
  3392. ]],
  3393. 'label' => ['for' => 'model-user'],
  3394. ['input' => [
  3395. 'name' => 'Model[user]',
  3396. 'type' => 'checkbox',
  3397. 'id' => 'model-user',
  3398. 'value' => 1,
  3399. ]],
  3400. 'User',
  3401. '/label',
  3402. '/div',
  3403. ];
  3404. $this->assertHtml($expected, $result);
  3405. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3406. $options = [
  3407. 1 => 'blue',
  3408. 2 => 'red',
  3409. ];
  3410. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3411. $expected = [
  3412. 'div' => ['class' => 'input select'],
  3413. 'label' => ['for' => 'tags-ids'],
  3414. 'Tags',
  3415. '/label',
  3416. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3417. ['div' => ['class' => 'checkbox']],
  3418. ['label' => ['for' => 'tags-ids-1']],
  3419. ['input' => [
  3420. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3421. 'value' => '1', 'name' => 'tags[_ids][]',
  3422. ]],
  3423. 'blue',
  3424. '/label',
  3425. '/div',
  3426. ['div' => ['class' => 'checkbox']],
  3427. ['label' => ['for' => 'tags-ids-2']],
  3428. ['input' => [
  3429. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3430. 'value' => '2', 'name' => 'tags[_ids][]',
  3431. ]],
  3432. 'red',
  3433. '/label',
  3434. '/div',
  3435. '/div',
  3436. ];
  3437. $this->assertHtml($expected, $result);
  3438. }
  3439. /**
  3440. * testControlMagicSelectForTypeNumber method
  3441. *
  3442. * Test that magic control() selects are created for type=number.
  3443. */
  3444. public function testControlMagicSelectForTypeNumber(): void
  3445. {
  3446. $this->getTableLocator()->get('ValidateUsers', [
  3447. 'className' => ValidateUsersTable::class,
  3448. ]);
  3449. $entity = new Entity(['balance' => 1]);
  3450. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3451. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3452. $result = $this->Form->control('balance');
  3453. $expected = [
  3454. 'div' => ['class' => 'input select'],
  3455. 'label' => ['for' => 'balance'],
  3456. 'Balance',
  3457. '/label',
  3458. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3459. ['option' => ['value' => '0']],
  3460. 'nothing',
  3461. '/option',
  3462. ['option' => ['value' => '1', 'selected' => 'selected']],
  3463. 'some',
  3464. '/option',
  3465. ['option' => ['value' => '100']],
  3466. 'a lot',
  3467. '/option',
  3468. '/select',
  3469. '/div',
  3470. ];
  3471. $this->assertHtml($expected, $result);
  3472. }
  3473. /**
  3474. * testInvalidControlTypeOption method
  3475. *
  3476. * Test invalid 'input' type option to control() function.
  3477. */
  3478. public function testInvalidControlTypeOption(): void
  3479. {
  3480. $this->expectException(RuntimeException::class);
  3481. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3482. $this->Form->control('text', ['type' => 'input']);
  3483. }
  3484. /**
  3485. * testControlMagicSelectChangeToRadio method
  3486. *
  3487. * Test that magic control() selects can easily be converted into radio types without error.
  3488. */
  3489. public function testControlMagicSelectChangeToRadio(): void
  3490. {
  3491. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3492. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3493. $this->assertStringContainsString('input type="radio"', $result);
  3494. }
  3495. /**
  3496. * testFormControlSubmit method
  3497. *
  3498. * Test correct results for form::control() and type submit.
  3499. */
  3500. public function testFormControlSubmit(): void
  3501. {
  3502. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3503. $expected = [
  3504. 'div' => ['class' => 'submit'],
  3505. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3506. '/div',
  3507. ];
  3508. $this->assertHtml($expected, $result);
  3509. }
  3510. /**
  3511. * testFormControls method
  3512. *
  3513. * Test correct results from Form::controls().
  3514. */
  3515. public function testFormControlsLegendFieldset(): void
  3516. {
  3517. $this->Form->create($this->article);
  3518. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3519. $expected = [
  3520. '<fieldset',
  3521. '<legend',
  3522. 'The Legend',
  3523. '/legend',
  3524. '*/fieldset',
  3525. ];
  3526. $this->assertHtml($expected, $result);
  3527. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3528. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3529. $this->assertStringContainsString('<fieldset>', $result);
  3530. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3531. $this->assertStringNotContainsString('<legend>', $result);
  3532. $this->assertStringNotContainsString('<fieldset>', $result);
  3533. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3534. $this->assertStringNotContainsString('<legend>', $result);
  3535. $this->assertStringNotContainsString('<fieldset>', $result);
  3536. $this->Form->create($this->article);
  3537. $this->View->setRequest($this->View->getRequest()
  3538. ->withParam('prefix', 'admin')
  3539. ->withParam('action', 'admin_edit')
  3540. ->withParam('controller', 'articles'));
  3541. $result = $this->Form->allControls();
  3542. $expected = [
  3543. '<fieldset',
  3544. '<legend',
  3545. 'New Article',
  3546. '/legend',
  3547. '*/fieldset',
  3548. ];
  3549. $this->assertHtml($expected, $result);
  3550. $this->Form->create($this->article);
  3551. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3552. $expected = [
  3553. '<fieldset',
  3554. '<legend',
  3555. 'The Legend',
  3556. '/legend',
  3557. '*/fieldset',
  3558. ];
  3559. $this->assertHtml($expected, $result);
  3560. $this->Form->create($this->article);
  3561. $result = $this->Form->allControls([], [
  3562. 'fieldset' => [
  3563. 'class' => 'some-class some-other-class',
  3564. 'disabled' => true,
  3565. 'data-param' => 'a-param',
  3566. ],
  3567. 'legend' => 'The Legend',
  3568. ]);
  3569. $expected = [
  3570. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3571. '<legend',
  3572. 'The Legend',
  3573. '/legend',
  3574. '*/fieldset',
  3575. ];
  3576. $this->assertHtml($expected, $result);
  3577. }
  3578. /**
  3579. * testFormControls method
  3580. *
  3581. * Test the controls() method.
  3582. */
  3583. public function testFormControls(): void
  3584. {
  3585. $this->Form->create($this->article);
  3586. $result = $this->Form->allControls();
  3587. $expected = [
  3588. '<fieldset',
  3589. '<legend', 'New Article', '/legend',
  3590. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3591. ['div' => ['class' => 'input select required']],
  3592. '*/div',
  3593. ['div' => ['class' => 'input text required']],
  3594. '*/div',
  3595. ['div' => ['class' => 'input text']],
  3596. '*/div',
  3597. ['div' => ['class' => 'input text']],
  3598. '*/div',
  3599. '/fieldset',
  3600. ];
  3601. $this->assertHtml($expected, $result);
  3602. $result = $this->Form->allControls([
  3603. 'published' => ['type' => 'boolean'],
  3604. ]);
  3605. $expected = [
  3606. '<fieldset',
  3607. '<legend', 'New Article', '/legend',
  3608. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3609. ['div' => ['class' => 'input select required']],
  3610. '*/div',
  3611. ['div' => ['class' => 'input text required']],
  3612. '*/div',
  3613. ['div' => ['class' => 'input text']],
  3614. '*/div',
  3615. ['div' => ['class' => 'input boolean']],
  3616. '*/div',
  3617. '/fieldset',
  3618. ];
  3619. $this->assertHtml($expected, $result);
  3620. $this->Form->create($this->article);
  3621. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3622. $expected = [
  3623. 'fieldset' => [],
  3624. 'legend' => [],
  3625. 'Hello',
  3626. '/legend',
  3627. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3628. ['div' => ['class' => 'input select required']],
  3629. '*/div',
  3630. ['div' => ['class' => 'input text required']],
  3631. '*/div',
  3632. ['div' => ['class' => 'input text']],
  3633. '*/div',
  3634. ['div' => ['class' => 'input text']],
  3635. '*/div',
  3636. '/fieldset',
  3637. ];
  3638. $this->assertHtml($expected, $result);
  3639. $this->Form->create();
  3640. $expected = [
  3641. 'fieldset' => [],
  3642. ['div' => ['class' => 'input text']],
  3643. 'label' => ['for' => 'foo'],
  3644. 'Foo',
  3645. '/label',
  3646. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3647. '*/div',
  3648. '/fieldset',
  3649. ];
  3650. $result = $this->Form->allControls(
  3651. ['foo' => ['type' => 'text']],
  3652. ['legend' => false]
  3653. );
  3654. $this->assertHtml($expected, $result);
  3655. }
  3656. /**
  3657. * testFormControlsBlacklist method
  3658. */
  3659. public function testFormControlsBlacklist(): void
  3660. {
  3661. $this->Form->create($this->article);
  3662. $result = $this->Form->allControls([
  3663. 'id' => false,
  3664. ]);
  3665. $expected = [
  3666. '<fieldset',
  3667. '<legend', 'New Article', '/legend',
  3668. ['div' => ['class' => 'input select required']],
  3669. '*/div',
  3670. ['div' => ['class' => 'input text required']],
  3671. '*/div',
  3672. ['div' => ['class' => 'input text']],
  3673. '*/div',
  3674. ['div' => ['class' => 'input text']],
  3675. '*/div',
  3676. '/fieldset',
  3677. ];
  3678. $this->assertHtml($expected, $result);
  3679. $this->Form->create($this->article);
  3680. $result = $this->Form->allControls([
  3681. 'id' => [],
  3682. ]);
  3683. $expected = [
  3684. '<fieldset',
  3685. '<legend', 'New Article', '/legend',
  3686. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3687. ['div' => ['class' => 'input select required']],
  3688. '*/div',
  3689. ['div' => ['class' => 'input text required']],
  3690. '*/div',
  3691. ['div' => ['class' => 'input text']],
  3692. '*/div',
  3693. ['div' => ['class' => 'input text']],
  3694. '*/div',
  3695. '/fieldset',
  3696. ];
  3697. $this->assertHtml($expected, $result, true);
  3698. }
  3699. /**
  3700. * testSelectAsCheckbox method
  3701. *
  3702. * Test multi-select widget with checkbox formatting.
  3703. */
  3704. public function testSelectAsCheckbox(): void
  3705. {
  3706. $result = $this->Form->select(
  3707. 'Model.multi_field',
  3708. ['first', 'second', 'third'],
  3709. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3710. );
  3711. $expected = [
  3712. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3713. ['div' => ['class' => 'checkbox']],
  3714. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3715. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3716. 'first',
  3717. '/label',
  3718. '/div',
  3719. ['div' => ['class' => 'checkbox']],
  3720. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3721. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3722. 'second',
  3723. '/label',
  3724. '/div',
  3725. ['div' => ['class' => 'checkbox']],
  3726. ['label' => ['for' => 'model-multi-field-2']],
  3727. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3728. 'third',
  3729. '/label',
  3730. '/div',
  3731. ];
  3732. $this->assertHtml($expected, $result);
  3733. $result = $this->Form->select(
  3734. 'Model.multi_field',
  3735. ['1/2' => 'half'],
  3736. ['multiple' => 'checkbox']
  3737. );
  3738. $expected = [
  3739. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3740. ['div' => ['class' => 'checkbox']],
  3741. ['label' => ['for' => 'model-multi-field-1-2']],
  3742. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3743. 'half',
  3744. '/label',
  3745. '/div',
  3746. ];
  3747. $this->assertHtml($expected, $result);
  3748. }
  3749. /**
  3750. * testLabel method
  3751. *
  3752. * Test label generation.
  3753. */
  3754. public function testLabel(): void
  3755. {
  3756. $result = $this->Form->label('Person.name');
  3757. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3758. $this->assertHtml($expected, $result);
  3759. $result = $this->Form->label('Person.name');
  3760. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3761. $this->assertHtml($expected, $result);
  3762. $result = $this->Form->label('Person.first_name');
  3763. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3764. $this->assertHtml($expected, $result);
  3765. $result = $this->Form->label('Person.first_name', 'Your first name');
  3766. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3767. $this->assertHtml($expected, $result);
  3768. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3769. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3770. $this->assertHtml($expected, $result);
  3771. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3772. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3773. $this->assertHtml($expected, $result);
  3774. $result = $this->Form->label('Person.first_name', '');
  3775. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3776. $this->assertHtml($expected, $result);
  3777. $result = $this->Form->label('Person.2.name', '');
  3778. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3779. $this->assertHtml($expected, $result);
  3780. }
  3781. /**
  3782. * testLabelContainControl method
  3783. *
  3784. * Test that label() can accept an input with the correct template vars.
  3785. */
  3786. public function testLabelContainControl(): void
  3787. {
  3788. $this->Form->setTemplates([
  3789. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3790. ]);
  3791. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3792. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3793. ]);
  3794. $expected = [
  3795. 'label' => ['for' => 'person-accept-terms'],
  3796. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3797. 'Accept',
  3798. '/label',
  3799. ];
  3800. $this->assertHtml($expected, $result);
  3801. }
  3802. /**
  3803. * testTextbox method
  3804. *
  3805. * Test textbox element generation.
  3806. */
  3807. public function testTextbox(): void
  3808. {
  3809. $result = $this->Form->text('Model.field');
  3810. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3811. $this->assertHtml($expected, $result);
  3812. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3813. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3814. $this->assertHtml($expected, $result);
  3815. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3816. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3817. $this->assertHtml($expected, $result);
  3818. }
  3819. /**
  3820. * testTextBoxDataAndError method
  3821. *
  3822. * Test that text() hooks up with request data and error fields.
  3823. */
  3824. public function testTextBoxDataAndError(): void
  3825. {
  3826. $this->article['errors'] = [
  3827. 'Contact' => ['text' => 'wrong'],
  3828. ];
  3829. $this->View->setRequest($this->View->getRequest()
  3830. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3831. ->withData('Contact.text', 'test'));
  3832. $this->Form->create($this->article);
  3833. $result = $this->Form->text('Model.text');
  3834. $expected = [
  3835. 'input' => [
  3836. 'type' => 'text',
  3837. 'name' => 'Model[text]',
  3838. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3839. ],
  3840. ];
  3841. $this->assertHtml($expected, $result);
  3842. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3843. $expected = [
  3844. 'input' => [
  3845. 'type' => 'text',
  3846. 'name' => 'Contact[text]',
  3847. 'value' => 'test',
  3848. 'id' => 'theID',
  3849. 'class' => 'form-error',
  3850. ],
  3851. ];
  3852. $this->assertHtml($expected, $result);
  3853. }
  3854. /**
  3855. * testDefaultValue method
  3856. *
  3857. * Test default value setting.
  3858. */
  3859. public function testTextDefaultValue(): void
  3860. {
  3861. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  3862. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3863. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3864. $this->assertHtml($expected, $result);
  3865. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  3866. $this->Form->create();
  3867. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3868. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3869. $this->assertHtml($expected, $result);
  3870. $Articles = $this->getTableLocator()->get('Articles');
  3871. $title = $Articles->getSchema()->getColumn('title');
  3872. $Articles->getSchema()->addColumn(
  3873. 'title',
  3874. ['default' => 'default title', 'length' => 255] + $title
  3875. );
  3876. $entity = $Articles->newEmptyEntity();
  3877. $this->Form->create($entity);
  3878. // Get default value from schema
  3879. $result = $this->Form->text('title');
  3880. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  3881. $this->assertHtml($expected, $result);
  3882. // Don't get value from schema
  3883. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3884. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3885. $this->assertHtml($expected, $result);
  3886. // Custom default value overrides default value from schema
  3887. $result = $this->Form->text('title', ['default' => 'override default']);
  3888. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  3889. $this->assertHtml($expected, $result);
  3890. // Default value from schema is used only for new entities.
  3891. $entity->setNew(false);
  3892. $result = $this->Form->text('title');
  3893. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  3894. $this->assertHtml($expected, $result);
  3895. }
  3896. /**
  3897. * testError method
  3898. *
  3899. * Test field error generation.
  3900. */
  3901. public function testError(): void
  3902. {
  3903. $this->article['errors'] = [
  3904. 'Article' => ['field' => 'email'],
  3905. ];
  3906. $this->Form->create($this->article);
  3907. $result = $this->Form->error('Article.field');
  3908. $expected = [
  3909. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3910. 'email',
  3911. '/div',
  3912. ];
  3913. $this->assertHtml($expected, $result);
  3914. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  3915. $expected = [
  3916. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3917. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3918. '/div',
  3919. ];
  3920. $this->assertHtml($expected, $result);
  3921. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  3922. $expected = [
  3923. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3924. '<strong', 'Badness!', '/strong',
  3925. '/div',
  3926. ];
  3927. $this->assertHtml($expected, $result);
  3928. }
  3929. /**
  3930. * testErrorRuleName method
  3931. *
  3932. * Test error translation can use rule names for translating.
  3933. */
  3934. public function testErrorRuleName(): void
  3935. {
  3936. $this->article['errors'] = [
  3937. 'Article' => [
  3938. 'field' => ['email' => 'Your email was not good'],
  3939. ],
  3940. ];
  3941. $this->Form->create($this->article);
  3942. $result = $this->Form->error('Article.field');
  3943. $expected = [
  3944. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3945. 'Your email was not good',
  3946. '/div',
  3947. ];
  3948. $this->assertHtml($expected, $result);
  3949. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  3950. $expected = [
  3951. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3952. 'Email in use',
  3953. '/div',
  3954. ];
  3955. $this->assertHtml($expected, $result);
  3956. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  3957. $expected = [
  3958. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3959. 'Email in use',
  3960. '/div',
  3961. ];
  3962. $this->assertHtml($expected, $result);
  3963. $result = $this->Form->error('Article.field', [
  3964. 'email' => 'Key is preferred',
  3965. 'Your email was not good' => 'Email in use',
  3966. ]);
  3967. $expected = [
  3968. ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
  3969. 'Key is preferred',
  3970. '/div',
  3971. ];
  3972. $this->assertHtml($expected, $result);
  3973. }
  3974. /**
  3975. * testErrorMessages method
  3976. *
  3977. * Test error with nested lists.
  3978. */
  3979. public function testErrorMessages(): void
  3980. {
  3981. $this->article['errors'] = [
  3982. 'Article' => ['field' => 'email'],
  3983. ];
  3984. $this->Form->create($this->article);
  3985. $result = $this->Form->error('Article.field', [
  3986. 'email' => 'No good!',
  3987. ]);
  3988. $expected = [
  3989. 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
  3990. 'No good!',
  3991. '/div',
  3992. ];
  3993. $this->assertHtml($expected, $result);
  3994. }
  3995. /**
  3996. * testErrorMultipleMessages method
  3997. *
  3998. * Test error() with multiple messages.
  3999. */
  4000. public function testErrorMultipleMessages(): void
  4001. {
  4002. $this->article['errors'] = [
  4003. 'field' => ['notBlank', 'email', 'Something else'],
  4004. ];
  4005. $this->Form->create($this->article);
  4006. $result = $this->Form->error('field', [
  4007. 'notBlank' => 'Cannot be empty',
  4008. 'email' => 'No good!',
  4009. ]);
  4010. $expected = [
  4011. 'div' => ['class' => 'error-message', 'id' => 'field-error'],
  4012. 'ul' => [],
  4013. '<li', 'Cannot be empty', '/li',
  4014. '<li', 'No good!', '/li',
  4015. '<li', 'Something else', '/li',
  4016. '/ul',
  4017. '/div',
  4018. ];
  4019. $this->assertHtml($expected, $result);
  4020. }
  4021. /**
  4022. * testPassword method
  4023. *
  4024. * Test password element generation.
  4025. */
  4026. public function testPassword(): void
  4027. {
  4028. $this->article['errors'] = [
  4029. 'Contact' => [
  4030. 'passwd' => 1,
  4031. ],
  4032. ];
  4033. $this->Form->create($this->article);
  4034. $result = $this->Form->password('Contact.field');
  4035. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4036. $this->assertHtml($expected, $result);
  4037. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4038. $this->Form->create($this->article);
  4039. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4040. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4041. $this->assertHtml($expected, $result);
  4042. }
  4043. /**
  4044. * testRadio method
  4045. *
  4046. * Test radio element set generation.
  4047. */
  4048. public function testRadio(): void
  4049. {
  4050. $result = $this->Form->radio('Model.field', ['option A']);
  4051. $expected = [
  4052. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4053. 'label' => ['for' => 'model-field-0'],
  4054. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4055. 'option A',
  4056. '/label',
  4057. ];
  4058. $this->assertHtml($expected, $result);
  4059. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4060. $this->assertHtml($expected, $result);
  4061. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4062. $expected = [
  4063. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4064. ['label' => ['for' => 'model-field-0']],
  4065. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4066. 'option A',
  4067. '/label',
  4068. ['label' => ['for' => 'model-field-1']],
  4069. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4070. 'option B',
  4071. '/label',
  4072. ];
  4073. $this->assertHtml($expected, $result);
  4074. $result = $this->Form->radio(
  4075. 'Employee.gender',
  4076. ['male' => 'Male', 'female' => 'Female'],
  4077. ['form' => 'my-form']
  4078. );
  4079. $expected = [
  4080. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4081. ['label' => ['for' => 'employee-gender-male']],
  4082. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4083. 'Male',
  4084. '/label',
  4085. ['label' => ['for' => 'employee-gender-female']],
  4086. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4087. 'Female',
  4088. '/label',
  4089. ];
  4090. $this->assertHtml($expected, $result);
  4091. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4092. $expected = [
  4093. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4094. ['label' => ['for' => 'model-custom-0']],
  4095. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4096. 'option A',
  4097. '/label',
  4098. ['label' => ['for' => 'model-custom-1']],
  4099. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4100. 'option B',
  4101. '/label',
  4102. ];
  4103. $this->assertHtml($expected, $result);
  4104. $result = $this->Form->radio(
  4105. 'Employee.gender',
  4106. [
  4107. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4108. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4109. ]
  4110. );
  4111. $expected = [
  4112. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4113. ['label' => ['for' => 'employee-gender-male']],
  4114. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4115. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4116. 'Male',
  4117. '/label',
  4118. ['label' => ['for' => 'employee-gender-female']],
  4119. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4120. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4121. 'Female',
  4122. '/label',
  4123. ];
  4124. $this->assertHtml($expected, $result);
  4125. }
  4126. /**
  4127. * Test radio with complex options and empty disabled data.
  4128. */
  4129. public function testRadioComplexDisabled(): void
  4130. {
  4131. $options = [
  4132. ['value' => 'r', 'text' => 'red'],
  4133. ['value' => 'b', 'text' => 'blue'],
  4134. ];
  4135. $attrs = ['disabled' => []];
  4136. $result = $this->Form->radio('Model.field', $options, $attrs);
  4137. $expected = [
  4138. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4139. ['label' => ['for' => 'model-field-r']],
  4140. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4141. 'red',
  4142. '/label',
  4143. ['label' => ['for' => 'model-field-b']],
  4144. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4145. 'blue',
  4146. '/label',
  4147. ];
  4148. $this->assertHtml($expected, $result);
  4149. $attrs = ['disabled' => ['r']];
  4150. $result = $this->Form->radio('Model.field', $options, $attrs);
  4151. $this->assertStringContainsString('disabled="disabled"', $result);
  4152. }
  4153. /**
  4154. * testRadioDefaultValue method
  4155. *
  4156. * Test default value setting on radio() method.
  4157. */
  4158. public function testRadioDefaultValue(): void
  4159. {
  4160. $Articles = $this->getTableLocator()->get('Articles');
  4161. $title = $Articles->getSchema()->getColumn('title');
  4162. $Articles->getSchema()->addColumn(
  4163. 'title',
  4164. ['default' => '1'] + $title
  4165. );
  4166. $this->Form->create($Articles->newEmptyEntity());
  4167. $result = $this->Form->radio('title', ['option A', 'option B']);
  4168. $expected = [
  4169. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4170. ['label' => ['for' => 'title-0']],
  4171. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4172. 'option A',
  4173. '/label',
  4174. ['label' => ['for' => 'title-1']],
  4175. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4176. 'option B',
  4177. '/label',
  4178. ];
  4179. $this->assertHtml($expected, $result);
  4180. }
  4181. /**
  4182. * Test setting a hiddenField value on radio buttons.
  4183. */
  4184. public function testRadioHiddenFieldValue(): void
  4185. {
  4186. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4187. $expected = [
  4188. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4189. 'label' => ['for' => 'title-0'],
  4190. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4191. 'option A',
  4192. '/label',
  4193. ];
  4194. $this->assertHtml($expected, $result);
  4195. }
  4196. /**
  4197. * testControlRadio method
  4198. *
  4199. * Test that input works with radio types.
  4200. */
  4201. public function testControlRadio(): void
  4202. {
  4203. $result = $this->Form->control('test', [
  4204. 'type' => 'radio',
  4205. 'options' => ['A', 'B'],
  4206. ]);
  4207. $expected = [
  4208. ['div' => ['class' => 'input radio']],
  4209. '<label',
  4210. 'Test',
  4211. '/label',
  4212. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4213. ['label' => ['for' => 'test-0']],
  4214. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4215. 'A',
  4216. '/label',
  4217. ['label' => ['for' => 'test-1']],
  4218. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4219. 'B',
  4220. '/label',
  4221. '/div',
  4222. ];
  4223. $this->assertHtml($expected, $result);
  4224. $result = $this->Form->control('test', [
  4225. 'type' => 'radio',
  4226. 'options' => ['A', 'B'],
  4227. 'value' => '0',
  4228. ]);
  4229. $expected = [
  4230. ['div' => ['class' => 'input radio']],
  4231. '<label',
  4232. 'Test',
  4233. '/label',
  4234. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4235. ['label' => ['for' => 'test-0']],
  4236. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4237. 'A',
  4238. '/label',
  4239. ['label' => ['for' => 'test-1']],
  4240. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4241. 'B',
  4242. '/label',
  4243. '/div',
  4244. ];
  4245. $this->assertHtml($expected, $result);
  4246. $result = $this->Form->control('test', [
  4247. 'type' => 'radio',
  4248. 'options' => ['A', 'B'],
  4249. 'label' => false,
  4250. ]);
  4251. $expected = [
  4252. ['div' => ['class' => 'input radio']],
  4253. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4254. ['label' => ['for' => 'test-0']],
  4255. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4256. 'A',
  4257. '/label',
  4258. ['label' => ['for' => 'test-1']],
  4259. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4260. 'B',
  4261. '/label',
  4262. '/div',
  4263. ];
  4264. $this->assertHtml($expected, $result);
  4265. $result = $this->Form->control('accept', [
  4266. 'type' => 'radio',
  4267. 'options' => [
  4268. 1 => 'positive',
  4269. -1 => 'negative',
  4270. ],
  4271. 'label' => false,
  4272. ]);
  4273. $expected = [
  4274. ['div' => ['class' => 'input radio']],
  4275. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '']],
  4276. ['label' => ['for' => 'accept-1']],
  4277. ['input' => [
  4278. 'type' => 'radio',
  4279. 'name' => 'accept',
  4280. 'value' => '1',
  4281. 'id' => 'accept-1',
  4282. ]],
  4283. 'positive',
  4284. '/label',
  4285. ['label' => ['for' => 'accept--1']],
  4286. ['input' => [
  4287. 'type' => 'radio',
  4288. 'name' => 'accept',
  4289. 'value' => '-1',
  4290. 'id' => 'accept--1',
  4291. ]],
  4292. 'negative',
  4293. '/label',
  4294. '/div',
  4295. ];
  4296. $this->assertHtml($expected, $result);
  4297. }
  4298. /**
  4299. * testRadioNoLabel method
  4300. *
  4301. * Test that radio() works with label = false.
  4302. */
  4303. public function testRadioNoLabel(): void
  4304. {
  4305. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4306. $expected = [
  4307. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4308. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4309. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4310. ];
  4311. $this->assertHtml($expected, $result);
  4312. }
  4313. /**
  4314. * testRadioControlInsideLabel method
  4315. *
  4316. * Test generating radio input inside label ala twitter bootstrap.
  4317. */
  4318. public function testRadioControlInsideLabel(): void
  4319. {
  4320. $this->Form->setTemplates([
  4321. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4322. 'radioWrapper' => '{{label}}',
  4323. ]);
  4324. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4325. // phpcs:disable
  4326. $expected = [
  4327. ['input' => [
  4328. 'type' => 'hidden',
  4329. 'name' => 'Model[field]',
  4330. 'value' => ''
  4331. ]],
  4332. ['label' => ['for' => 'model-field-0']],
  4333. ['input' => [
  4334. 'type' => 'radio',
  4335. 'name' => 'Model[field]',
  4336. 'value' => '0',
  4337. 'id' => 'model-field-0'
  4338. ]],
  4339. 'option A',
  4340. '/label',
  4341. ['label' => ['for' => 'model-field-1']],
  4342. ['input' => [
  4343. 'type' => 'radio',
  4344. 'name' => 'Model[field]',
  4345. 'value' => '1',
  4346. 'id' => 'model-field-1'
  4347. ]],
  4348. 'option B',
  4349. '/label',
  4350. ];
  4351. // phpcs:enable
  4352. $this->assertHtml($expected, $result);
  4353. }
  4354. /**
  4355. * testRadioHiddenControlDisabling method
  4356. *
  4357. * Test disabling the hidden input for radio buttons.
  4358. */
  4359. public function testRadioHiddenControlDisabling(): void
  4360. {
  4361. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4362. $expected = [
  4363. 'label' => ['for' => 'model-1-field-0'],
  4364. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4365. 'option A',
  4366. '/label',
  4367. ];
  4368. $this->assertHtml($expected, $result);
  4369. }
  4370. /**
  4371. * testRadioOutOfRange method
  4372. *
  4373. * Test radio element set generation.
  4374. */
  4375. public function testRadioOutOfRange(): void
  4376. {
  4377. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4378. $expected = [
  4379. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4380. 'label' => ['for' => 'model-field-v'],
  4381. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4382. 'value',
  4383. '/label',
  4384. ];
  4385. $this->assertHtml($expected, $result);
  4386. }
  4387. /**
  4388. * testSelect method
  4389. *
  4390. * Test select element generation.
  4391. */
  4392. public function testSelect(): void
  4393. {
  4394. $result = $this->Form->select('Model.field', []);
  4395. $expected = [
  4396. 'select' => ['name' => 'Model[field]'],
  4397. '/select',
  4398. ];
  4399. $this->assertHtml($expected, $result);
  4400. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4401. $this->Form->create();
  4402. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4403. $expected = [
  4404. 'select' => ['name' => 'Model[field]'],
  4405. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4406. 'good',
  4407. '/option',
  4408. ['option' => ['value' => 'other']],
  4409. 'bad',
  4410. '/option',
  4411. '/select',
  4412. ];
  4413. $this->assertHtml($expected, $result);
  4414. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4415. $this->assertHtml($expected, $result);
  4416. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4417. $this->Form->create();
  4418. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4419. $expected = [
  4420. 'select' => ['name' => 'Model[field]'],
  4421. ['option' => ['value' => 'value']],
  4422. 'good',
  4423. '/option',
  4424. ['option' => ['value' => 'other']],
  4425. 'bad',
  4426. '/option',
  4427. '/select',
  4428. ];
  4429. $this->assertHtml($expected, $result);
  4430. $options = [
  4431. ['value' => 'first', 'text' => 'First'],
  4432. ['value' => 'first', 'text' => 'Another First'],
  4433. ];
  4434. $result = $this->Form->select(
  4435. 'Model.field',
  4436. $options,
  4437. ['escape' => false, 'empty' => false]
  4438. );
  4439. $expected = [
  4440. 'select' => ['name' => 'Model[field]'],
  4441. ['option' => ['value' => 'first']],
  4442. 'First',
  4443. '/option',
  4444. ['option' => ['value' => 'first']],
  4445. 'Another First',
  4446. '/option',
  4447. '/select',
  4448. ];
  4449. $this->assertHtml($expected, $result);
  4450. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4451. $this->Form->create();
  4452. $result = $this->Form->select(
  4453. 'Model.contact_id',
  4454. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4455. ['escape' => false, 'empty' => 'pick something']
  4456. );
  4457. $expected = [
  4458. 'select' => ['name' => 'Model[contact_id]'],
  4459. ['option' => ['value' => '']], 'pick something', '/option',
  4460. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4461. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4462. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4463. '/select',
  4464. ];
  4465. $this->assertHtml($expected, $result);
  4466. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4467. $this->Form->create();
  4468. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4469. $expected = [
  4470. 'select' => ['name' => 'Model[field]'],
  4471. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4472. ['option' => ['value' => '1']], 'Yes', '/option',
  4473. '/select',
  4474. ];
  4475. $this->assertHtml($expected, $result);
  4476. }
  4477. /**
  4478. * testSelectEscapeHtml method
  4479. *
  4480. * Test that select() escapes HTML.
  4481. */
  4482. public function testSelectEscapeHtml(): void
  4483. {
  4484. $result = $this->Form->select(
  4485. 'Model.field',
  4486. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4487. ['empty' => false]
  4488. );
  4489. $expected = [
  4490. 'select' => ['name' => 'Model[field]'],
  4491. ['option' => ['value' => 'first']],
  4492. 'first &quot;html&quot; &lt;chars&gt;',
  4493. '/option',
  4494. ['option' => ['value' => 'second']],
  4495. 'value',
  4496. '/option',
  4497. '/select',
  4498. ];
  4499. $this->assertHtml($expected, $result);
  4500. $result = $this->Form->select(
  4501. 'Model.field',
  4502. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4503. ['escape' => false, 'empty' => false]
  4504. );
  4505. $expected = [
  4506. 'select' => ['name' => 'Model[field]'],
  4507. ['option' => ['value' => 'first']],
  4508. 'first "html" <chars>',
  4509. '/option',
  4510. ['option' => ['value' => 'second']],
  4511. 'value',
  4512. '/option',
  4513. '/select',
  4514. ];
  4515. $this->assertHtml($expected, $result);
  4516. }
  4517. /**
  4518. * testSelectRequired method
  4519. *
  4520. * Test select() with required and disabled attributes.
  4521. */
  4522. public function testSelectRequired(): void
  4523. {
  4524. $this->article['required'] = [
  4525. 'user_id' => true,
  4526. ];
  4527. $this->Form->create($this->article);
  4528. $result = $this->Form->select('user_id', ['option A']);
  4529. $expected = [
  4530. 'select' => [
  4531. 'name' => 'user_id',
  4532. 'required' => 'required',
  4533. ],
  4534. ['option' => ['value' => '0']], 'option A', '/option',
  4535. '/select',
  4536. ];
  4537. $this->assertHtml($expected, $result);
  4538. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4539. $expected = [
  4540. 'select' => [
  4541. 'name' => 'user_id',
  4542. 'disabled' => 'disabled',
  4543. ],
  4544. ['option' => ['value' => '0']], 'option A', '/option',
  4545. '/select',
  4546. ];
  4547. $this->assertHtml($expected, $result);
  4548. }
  4549. public function testSelectEmptyWithRequiredFalse(): void
  4550. {
  4551. $Articles = $this->getTableLocator()->get('Articles');
  4552. $validator = $Articles->getValidator('default');
  4553. $validator->allowEmptyString('user_id');
  4554. $Articles->setValidator('default', $validator);
  4555. $entity = $Articles->newEmptyEntity();
  4556. $this->Form->create($entity);
  4557. $result = $this->Form->select('user_id', ['option A']);
  4558. $expected = [
  4559. 'select' => [
  4560. 'name' => 'user_id',
  4561. ],
  4562. ['option' => ['value' => '']], '/option',
  4563. ['option' => ['value' => '0']], 'option A', '/option',
  4564. '/select',
  4565. ];
  4566. $this->assertHtml($expected, $result);
  4567. }
  4568. /**
  4569. * testNestedSelect method
  4570. *
  4571. * Test select element generation with optgroups.
  4572. */
  4573. public function testNestedSelect(): void
  4574. {
  4575. $result = $this->Form->select(
  4576. 'Model.field',
  4577. [1 => 'One', 2 => 'Two', 'Three' => [
  4578. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4579. ]],
  4580. ['empty' => false]
  4581. );
  4582. $expected = [
  4583. 'select' => ['name' => 'Model[field]'],
  4584. ['option' => ['value' => 1]],
  4585. 'One',
  4586. '/option',
  4587. ['option' => ['value' => 2]],
  4588. 'Two',
  4589. '/option',
  4590. ['optgroup' => ['label' => 'Three']],
  4591. ['option' => ['value' => 3]],
  4592. 'Three',
  4593. '/option',
  4594. ['option' => ['value' => 4]],
  4595. 'Four',
  4596. '/option',
  4597. ['option' => ['value' => 5]],
  4598. 'Five',
  4599. '/option',
  4600. '/optgroup',
  4601. '/select',
  4602. ];
  4603. $this->assertHtml($expected, $result);
  4604. }
  4605. /**
  4606. * testSelectMultiple method
  4607. *
  4608. * Test generation of multiple select elements.
  4609. */
  4610. public function testSelectMultiple(): void
  4611. {
  4612. $options = ['first', 'second', 'third'];
  4613. $result = $this->Form->select(
  4614. 'Model.multi_field',
  4615. $options,
  4616. ['form' => 'my-form', 'multiple' => true]
  4617. );
  4618. $expected = [
  4619. 'input' => [
  4620. 'type' => 'hidden',
  4621. 'name' => 'Model[multi_field]',
  4622. 'value' => '',
  4623. 'form' => 'my-form',
  4624. ],
  4625. 'select' => [
  4626. 'name' => 'Model[multi_field][]',
  4627. 'multiple' => 'multiple',
  4628. 'form' => 'my-form',
  4629. ],
  4630. ['option' => ['value' => '0']],
  4631. 'first',
  4632. '/option',
  4633. ['option' => ['value' => '1']],
  4634. 'second',
  4635. '/option',
  4636. ['option' => ['value' => '2']],
  4637. 'third',
  4638. '/option',
  4639. '/select',
  4640. ];
  4641. $this->assertHtml($expected, $result);
  4642. $result = $this->Form->select(
  4643. 'Model.multi_field',
  4644. $options,
  4645. ['multiple' => 'multiple', 'form' => 'my-form']
  4646. );
  4647. $this->assertHtml($expected, $result);
  4648. $result = $this->Form->select(
  4649. 'Model.multi_field',
  4650. $options,
  4651. ['form' => 'my-form', 'multiple' => false]
  4652. );
  4653. $this->assertStringNotContainsString('multiple', $result);
  4654. }
  4655. /**
  4656. * testCheckboxZeroValue method
  4657. *
  4658. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4659. */
  4660. public function testCheckboxZeroValue(): void
  4661. {
  4662. $result = $this->Form->control('User.get_spam', [
  4663. 'type' => 'checkbox',
  4664. 'value' => '0',
  4665. 'hiddenField' => '1',
  4666. ]);
  4667. $expected = [
  4668. 'div' => ['class' => 'input checkbox'],
  4669. 'label' => ['for' => 'user-get-spam'],
  4670. ['input' => [
  4671. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4672. 'value' => '1',
  4673. ]],
  4674. ['input' => [
  4675. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4676. 'value' => '0', 'id' => 'user-get-spam',
  4677. ]],
  4678. 'Get Spam',
  4679. '/label',
  4680. '/div',
  4681. ];
  4682. $this->assertHtml($expected, $result);
  4683. }
  4684. /**
  4685. * testHabtmSelectBox method
  4686. *
  4687. * Test generation of habtm select boxes.
  4688. */
  4689. public function testHabtmSelectBox(): void
  4690. {
  4691. $options = [
  4692. 1 => 'blue',
  4693. 2 => 'red',
  4694. 3 => 'green',
  4695. ];
  4696. $tags = [
  4697. new Entity(['id' => 1, 'name' => 'blue']),
  4698. new Entity(['id' => 3, 'name' => 'green']),
  4699. ];
  4700. $article = new Article(['tags' => $tags]);
  4701. $this->Form->create($article);
  4702. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4703. $expected = [
  4704. 'div' => ['class' => 'input select'],
  4705. 'label' => ['for' => 'tags-ids'],
  4706. 'Tags',
  4707. '/label',
  4708. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4709. 'select' => [
  4710. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4711. 'multiple' => 'multiple',
  4712. ],
  4713. ['option' => ['value' => '1', 'selected' => 'selected']],
  4714. 'blue',
  4715. '/option',
  4716. ['option' => ['value' => '2']],
  4717. 'red',
  4718. '/option',
  4719. ['option' => ['value' => '3', 'selected' => 'selected']],
  4720. 'green',
  4721. '/option',
  4722. '/select',
  4723. '/div',
  4724. ];
  4725. $this->assertHtml($expected, $result);
  4726. // make sure only 50 is selected, and not 50f5c0cf
  4727. $options = [
  4728. '1' => 'blue',
  4729. '50f5c0cf' => 'red',
  4730. '50' => 'green',
  4731. ];
  4732. $tags = [
  4733. new Entity(['id' => 1, 'name' => 'blue']),
  4734. new Entity(['id' => 50, 'name' => 'green']),
  4735. ];
  4736. $article = new Article(['tags' => $tags]);
  4737. $this->Form->create($article);
  4738. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4739. $expected = [
  4740. 'div' => ['class' => 'input select'],
  4741. 'label' => ['for' => 'tags-ids'],
  4742. 'Tags',
  4743. '/label',
  4744. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4745. 'select' => [
  4746. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4747. 'multiple' => 'multiple',
  4748. ],
  4749. ['option' => ['value' => '1', 'selected' => 'selected']],
  4750. 'blue',
  4751. '/option',
  4752. ['option' => ['value' => '50f5c0cf']],
  4753. 'red',
  4754. '/option',
  4755. ['option' => ['value' => '50', 'selected' => 'selected']],
  4756. 'green',
  4757. '/option',
  4758. '/select',
  4759. '/div',
  4760. ];
  4761. $this->assertHtml($expected, $result);
  4762. $spacecraft = [
  4763. 1 => 'Orion',
  4764. 2 => 'Helios',
  4765. ];
  4766. $this->View->set('spacecraft', $spacecraft);
  4767. $this->Form->create();
  4768. $result = $this->Form->control('spacecraft._ids');
  4769. $expected = [
  4770. 'div' => ['class' => 'input select'],
  4771. 'label' => ['for' => 'spacecraft-ids'],
  4772. 'Spacecraft',
  4773. '/label',
  4774. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4775. 'select' => [
  4776. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4777. 'multiple' => 'multiple',
  4778. ],
  4779. ['option' => ['value' => '1']],
  4780. 'Orion',
  4781. '/option',
  4782. ['option' => ['value' => '2']],
  4783. 'Helios',
  4784. '/option',
  4785. '/select',
  4786. '/div',
  4787. ];
  4788. $this->assertHtml($expected, $result);
  4789. }
  4790. /**
  4791. * testErrorsForBelongsToManySelect method
  4792. *
  4793. * Tests that errors for belongsToMany select fields are being
  4794. * picked up properly.
  4795. */
  4796. public function testErrorsForBelongsToManySelect(): void
  4797. {
  4798. $spacecraft = [
  4799. 1 => 'Orion',
  4800. 2 => 'Helios',
  4801. ];
  4802. $this->View->set('spacecraft', $spacecraft);
  4803. $article = new Article();
  4804. $article->setError('spacecraft', ['Invalid']);
  4805. $this->Form->create($article);
  4806. $result = $this->Form->control('spacecraft._ids');
  4807. $expected = [
  4808. ['div' => ['class' => 'input select error']],
  4809. 'label' => ['for' => 'spacecraft-ids'],
  4810. 'Spacecraft',
  4811. '/label',
  4812. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4813. 'select' => [
  4814. 'name' => 'spacecraft[_ids][]',
  4815. 'id' => 'spacecraft-ids',
  4816. 'multiple' => 'multiple',
  4817. ],
  4818. ['option' => ['value' => '1']],
  4819. 'Orion',
  4820. '/option',
  4821. ['option' => ['value' => '2']],
  4822. 'Helios',
  4823. '/option',
  4824. '/select',
  4825. ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
  4826. 'Invalid',
  4827. '/div',
  4828. '/div',
  4829. ];
  4830. $this->assertHtml($expected, $result);
  4831. }
  4832. /**
  4833. * testSelectMultipleCheckboxes method
  4834. *
  4835. * Test generation of multi select elements in checkbox format.
  4836. */
  4837. public function testSelectMultipleCheckboxes(): void
  4838. {
  4839. $result = $this->Form->select(
  4840. 'Model.multi_field',
  4841. ['first', 'second', 'third'],
  4842. ['multiple' => 'checkbox']
  4843. );
  4844. $expected = [
  4845. 'input' => [
  4846. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  4847. ],
  4848. ['div' => ['class' => 'checkbox']],
  4849. ['label' => ['for' => 'model-multi-field-0']],
  4850. ['input' => [
  4851. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4852. 'value' => '0', 'id' => 'model-multi-field-0',
  4853. ]],
  4854. 'first',
  4855. '/label',
  4856. '/div',
  4857. ['div' => ['class' => 'checkbox']],
  4858. ['label' => ['for' => 'model-multi-field-1']],
  4859. ['input' => [
  4860. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4861. 'value' => '1', 'id' => 'model-multi-field-1',
  4862. ]],
  4863. 'second',
  4864. '/label',
  4865. '/div',
  4866. ['div' => ['class' => 'checkbox']],
  4867. ['label' => ['for' => 'model-multi-field-2']],
  4868. ['input' => [
  4869. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4870. 'value' => '2', 'id' => 'model-multi-field-2',
  4871. ]],
  4872. 'third',
  4873. '/label',
  4874. '/div',
  4875. ];
  4876. $this->assertHtml($expected, $result);
  4877. $result = $this->Form->select(
  4878. 'Model.multi_field',
  4879. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4880. ['multiple' => 'checkbox']
  4881. );
  4882. $expected = [
  4883. 'input' => [
  4884. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  4885. ],
  4886. ['div' => ['class' => 'checkbox']],
  4887. ['label' => ['for' => 'model-multi-field-a+']],
  4888. ['input' => [
  4889. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4890. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  4891. ]],
  4892. 'first',
  4893. '/label',
  4894. '/div',
  4895. ['div' => ['class' => 'checkbox']],
  4896. ['label' => ['for' => 'model-multi-field-a++']],
  4897. ['input' => [
  4898. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4899. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  4900. ]],
  4901. 'second',
  4902. '/label',
  4903. '/div',
  4904. ['div' => ['class' => 'checkbox']],
  4905. ['label' => ['for' => 'model-multi-field-a+++']],
  4906. ['input' => [
  4907. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4908. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  4909. ]],
  4910. 'third',
  4911. '/label',
  4912. '/div',
  4913. ];
  4914. $this->assertHtml($expected, $result);
  4915. $result = $this->Form->select(
  4916. 'Model.multi_field',
  4917. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4918. ['multiple' => 'checkbox']
  4919. );
  4920. $expected = [
  4921. 'input' => [
  4922. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  4923. ],
  4924. ['div' => ['class' => 'checkbox']],
  4925. ['label' => ['for' => 'model-multi-field-a-b']],
  4926. ['input' => [
  4927. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4928. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  4929. ]],
  4930. 'first',
  4931. '/label',
  4932. '/div',
  4933. ['div' => ['class' => 'checkbox']],
  4934. ['label' => ['for' => 'model-multi-field-a-b1']],
  4935. ['input' => [
  4936. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4937. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  4938. ]],
  4939. 'second',
  4940. '/label',
  4941. '/div',
  4942. ['div' => ['class' => 'checkbox']],
  4943. ['label' => ['for' => 'model-multi-field-a-b2']],
  4944. ['input' => [
  4945. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4946. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  4947. ]],
  4948. 'third',
  4949. '/label',
  4950. '/div',
  4951. ];
  4952. $this->assertHtml($expected, $result);
  4953. }
  4954. /**
  4955. * testSelectMultipleCheckboxRequestData method
  4956. *
  4957. * Ensure that multiCheckbox reads from the request data.
  4958. */
  4959. public function testSelectMultipleCheckboxRequestData(): void
  4960. {
  4961. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  4962. $result = $this->Form->select(
  4963. 'Model.tags',
  4964. ['1' => 'first', 'Array' => 'Array'],
  4965. ['multiple' => 'checkbox']
  4966. );
  4967. $expected = [
  4968. 'input' => [
  4969. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '',
  4970. ],
  4971. ['div' => ['class' => 'checkbox']],
  4972. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4973. ['input' => [
  4974. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4975. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  4976. ]],
  4977. 'first',
  4978. '/label',
  4979. '/div',
  4980. ['div' => ['class' => 'checkbox']],
  4981. ['label' => ['for' => 'model-tags-array']],
  4982. ['input' => [
  4983. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4984. 'value' => 'Array', 'id' => 'model-tags-array',
  4985. ]],
  4986. 'Array',
  4987. '/label',
  4988. '/div',
  4989. ];
  4990. $this->assertHtml($expected, $result);
  4991. }
  4992. /**
  4993. * testSelectMultipleCheckboxSecurity method
  4994. *
  4995. * Checks the security hash array generated for multiple-input checkbox elements.
  4996. */
  4997. public function testSelectMultipleCheckboxSecurity(): void
  4998. {
  4999. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5000. $this->Form->create();
  5001. $this->Form->select(
  5002. 'Model.multi_field',
  5003. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5004. ['multiple' => 'checkbox']
  5005. );
  5006. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5007. $this->assertEquals(['Model.multi_field'], $fields);
  5008. $result = $this->Form->secure();
  5009. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5010. $hash = urlencode($hash . ':');
  5011. $this->assertStringContainsString('"' . $hash . '"', $result);
  5012. }
  5013. /**
  5014. * testSelectMultipleSecureWithNoOptions method
  5015. *
  5016. * Multiple select elements should always be secured as they always participate
  5017. * in the POST data.
  5018. */
  5019. public function testSelectMultipleSecureWithNoOptions(): void
  5020. {
  5021. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5022. $this->Form->create();
  5023. $this->Form->select(
  5024. 'Model.select',
  5025. [],
  5026. ['multiple' => true]
  5027. );
  5028. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5029. $this->assertEquals(['Model.select'], $result);
  5030. }
  5031. /**
  5032. * testSelectNoSecureWithNoOptions method
  5033. *
  5034. * When a select box has no options it should not be added to the fields list
  5035. * as it always fail post validation.
  5036. */
  5037. public function testSelectNoSecureWithNoOptions(): void
  5038. {
  5039. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5040. $this->Form->create();
  5041. $this->Form->select(
  5042. 'Model.select',
  5043. []
  5044. );
  5045. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5046. $this->assertEquals([], $result);
  5047. $this->Form->select(
  5048. 'Model.user_id',
  5049. [],
  5050. ['empty' => true]
  5051. );
  5052. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5053. $this->assertEquals(['Model.user_id'], $result);
  5054. }
  5055. /**
  5056. * testControlMultipleCheckboxes method
  5057. *
  5058. * Test control() resulting in multi select elements being generated.
  5059. */
  5060. public function testControlMultipleCheckboxes(): void
  5061. {
  5062. $result = $this->Form->control('Model.multi_field', [
  5063. 'options' => ['first', 'second', 'third'],
  5064. 'multiple' => 'checkbox',
  5065. ]);
  5066. $expected = [
  5067. ['div' => ['class' => 'input select']],
  5068. ['label' => ['for' => 'model-multi-field']],
  5069. 'Multi Field',
  5070. '/label',
  5071. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5072. ['div' => ['class' => 'checkbox']],
  5073. ['label' => ['for' => 'model-multi-field-0']],
  5074. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5075. 'first',
  5076. '/label',
  5077. '/div',
  5078. ['div' => ['class' => 'checkbox']],
  5079. ['label' => ['for' => 'model-multi-field-1']],
  5080. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5081. 'second',
  5082. '/label',
  5083. '/div',
  5084. ['div' => ['class' => 'checkbox']],
  5085. ['label' => ['for' => 'model-multi-field-2']],
  5086. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5087. 'third',
  5088. '/label',
  5089. '/div',
  5090. '/div',
  5091. ];
  5092. $this->assertHtml($expected, $result);
  5093. $result = $this->Form->control('Model.multi_field', [
  5094. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5095. 'multiple' => 'checkbox',
  5096. ]);
  5097. $expected = [
  5098. ['div' => ['class' => 'input select']],
  5099. ['label' => ['for' => 'model-multi-field']],
  5100. 'Multi Field',
  5101. '/label',
  5102. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5103. ['div' => ['class' => 'checkbox']],
  5104. ['label' => ['for' => 'model-multi-field-a']],
  5105. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5106. 'first',
  5107. '/label',
  5108. '/div',
  5109. ['div' => ['class' => 'checkbox']],
  5110. ['label' => ['for' => 'model-multi-field-b']],
  5111. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5112. 'second',
  5113. '/label',
  5114. '/div',
  5115. ['div' => ['class' => 'checkbox']],
  5116. ['label' => ['for' => 'model-multi-field-c']],
  5117. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5118. 'third',
  5119. '/label',
  5120. '/div',
  5121. '/div',
  5122. ];
  5123. $this->assertHtml($expected, $result);
  5124. }
  5125. /**
  5126. * testSelectHiddenFieldOmission method
  5127. *
  5128. * Test that select() with 'hiddenField' => false omits the hidden field.
  5129. */
  5130. public function testSelectHiddenFieldOmission(): void
  5131. {
  5132. $result = $this->Form->select(
  5133. 'Model.multi_field',
  5134. ['first', 'second'],
  5135. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5136. );
  5137. $this->assertStringNotContainsString('type="hidden"', $result);
  5138. }
  5139. /**
  5140. * testSelectCheckboxMultipleOverrideName method
  5141. *
  5142. * Test that select() with multiple = checkbox works with overriding name attribute.
  5143. */
  5144. public function testSelectCheckboxMultipleOverrideName(): void
  5145. {
  5146. $result = $this->Form->select('category', ['1', '2'], [
  5147. 'multiple' => 'checkbox',
  5148. 'name' => 'fish',
  5149. ]);
  5150. $expected = [
  5151. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5152. ['div' => ['class' => 'checkbox']],
  5153. ['label' => ['for' => 'fish-0']],
  5154. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5155. '1',
  5156. '/label',
  5157. '/div',
  5158. ['div' => ['class' => 'checkbox']],
  5159. ['label' => ['for' => 'fish-1']],
  5160. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5161. '2',
  5162. '/label',
  5163. '/div',
  5164. ];
  5165. $this->assertHtml($expected, $result);
  5166. $result = $this->Form->multiCheckbox(
  5167. 'category',
  5168. new Collection(['1', '2']),
  5169. ['name' => 'fish']
  5170. );
  5171. $this->assertHtml($expected, $result);
  5172. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5173. 'name' => 'fish',
  5174. ]);
  5175. $this->assertHtml($expected, $result);
  5176. }
  5177. /**
  5178. * testControlMultiCheckbox method
  5179. *
  5180. * Test that control() works with multicheckbox.
  5181. */
  5182. public function testControlMultiCheckbox(): void
  5183. {
  5184. $result = $this->Form->control('category', [
  5185. 'type' => 'multicheckbox',
  5186. 'options' => ['1', '2'],
  5187. ]);
  5188. $expected = [
  5189. ['div' => ['class' => 'input multicheckbox']],
  5190. '<label',
  5191. 'Category',
  5192. '/label',
  5193. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5194. ['div' => ['class' => 'checkbox']],
  5195. ['label' => ['for' => 'category-0']],
  5196. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5197. '1',
  5198. '/label',
  5199. '/div',
  5200. ['div' => ['class' => 'checkbox']],
  5201. ['label' => ['for' => 'category-1']],
  5202. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5203. '2',
  5204. '/label',
  5205. '/div',
  5206. '/div',
  5207. ];
  5208. $this->assertHtml($expected, $result);
  5209. }
  5210. /**
  5211. * testCheckbox method
  5212. *
  5213. * Test generation of checkboxes.
  5214. */
  5215. public function testCheckbox(): void
  5216. {
  5217. $result = $this->Form->checkbox('Model.field');
  5218. $expected = [
  5219. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5220. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5221. ];
  5222. $this->assertHtml($expected, $result);
  5223. $result = $this->Form->checkbox('Model.field', [
  5224. 'id' => 'theID',
  5225. 'value' => 'myvalue',
  5226. 'form' => 'my-form',
  5227. ]);
  5228. $expected = [
  5229. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5230. ['input' => [
  5231. 'type' => 'checkbox', 'name' => 'Model[field]',
  5232. 'value' => 'myvalue', 'id' => 'theID',
  5233. 'form' => 'my-form',
  5234. ]],
  5235. ];
  5236. $this->assertHtml($expected, $result);
  5237. }
  5238. /**
  5239. * testCheckboxDefaultValue method
  5240. *
  5241. * Test default value setting on checkbox() method.
  5242. */
  5243. public function testCheckboxDefaultValue(): void
  5244. {
  5245. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5246. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5247. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5248. $this->assertHtml($expected, $result);
  5249. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5250. $this->Form->create();
  5251. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5252. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5253. $this->assertHtml($expected, $result);
  5254. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5255. $this->Form->create();
  5256. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5257. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5258. $this->assertHtml($expected, $result);
  5259. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5260. $this->Form->create();
  5261. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5262. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5263. $this->assertHtml($expected, $result);
  5264. $Articles = $this->getTableLocator()->get('Articles');
  5265. $Articles->getSchema()->addColumn(
  5266. 'published',
  5267. ['type' => 'boolean', 'null' => false, 'default' => true]
  5268. );
  5269. $this->Form->create($Articles->newEmptyEntity());
  5270. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5271. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5272. $this->assertHtml($expected, $result);
  5273. }
  5274. /**
  5275. * testCheckboxCheckedAndError method
  5276. *
  5277. * Test checkbox being checked or having errors.
  5278. */
  5279. public function testCheckboxCheckedAndError(): void
  5280. {
  5281. $this->article['errors'] = [
  5282. 'published' => true,
  5283. ];
  5284. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5285. $this->Form->create($this->article);
  5286. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5287. $expected = [
  5288. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5289. ['input' => [
  5290. 'type' => 'checkbox',
  5291. 'name' => 'published',
  5292. 'value' => 'myvalue',
  5293. 'id' => 'theID',
  5294. 'checked' => 'checked',
  5295. 'class' => 'form-error',
  5296. ]],
  5297. ];
  5298. $this->assertHtml($expected, $result);
  5299. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5300. $this->Form->create($this->article);
  5301. $result = $this->Form->checkbox('published');
  5302. $expected = [
  5303. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5304. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5305. ];
  5306. $this->assertHtml($expected, $result);
  5307. }
  5308. /**
  5309. * testCheckboxCustomNameAttribute method
  5310. *
  5311. * Test checkbox() with a custom name attribute.
  5312. */
  5313. public function testCheckboxCustomNameAttribute(): void
  5314. {
  5315. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5316. $expected = [
  5317. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5318. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5319. ];
  5320. $this->assertHtml($expected, $result);
  5321. }
  5322. /**
  5323. * testCheckboxHiddenField method
  5324. *
  5325. * Test that the hidden input for checkboxes can be omitted or set to a
  5326. * specific value.
  5327. */
  5328. public function testCheckboxHiddenField(): void
  5329. {
  5330. $result = $this->Form->checkbox('UserForm.something', [
  5331. 'hiddenField' => false,
  5332. ]);
  5333. $expected = [
  5334. 'input' => [
  5335. 'type' => 'checkbox',
  5336. 'name' => 'UserForm[something]',
  5337. 'value' => '1',
  5338. ],
  5339. ];
  5340. $this->assertHtml($expected, $result);
  5341. $result = $this->Form->checkbox('UserForm.something', [
  5342. 'value' => 'Y',
  5343. 'hiddenField' => 'N',
  5344. ]);
  5345. $expected = [
  5346. ['input' => [
  5347. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5348. 'value' => 'N',
  5349. ]],
  5350. ['input' => [
  5351. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5352. 'value' => 'Y',
  5353. ]],
  5354. ];
  5355. $this->assertHtml($expected, $result);
  5356. }
  5357. /**
  5358. * testTime method
  5359. *
  5360. * Test the time type.
  5361. */
  5362. public function testTime(): void
  5363. {
  5364. $result = $this->Form->time('start_time', [
  5365. 'value' => '2014-03-08 16:30:00',
  5366. ]);
  5367. $expected = [
  5368. 'input' => [
  5369. 'type' => 'time',
  5370. 'name' => 'start_time',
  5371. 'value' => '16:30:00',
  5372. 'step' => '1',
  5373. ],
  5374. ];
  5375. $this->assertHtml($expected, $result);
  5376. }
  5377. /**
  5378. * testDate method
  5379. *
  5380. * Test the date type.
  5381. */
  5382. public function testDate(): void
  5383. {
  5384. $result = $this->Form->date('start_day', [
  5385. 'value' => '2014-03-08',
  5386. ]);
  5387. $expected = [
  5388. 'input' => [
  5389. 'type' => 'date',
  5390. 'name' => 'start_day',
  5391. 'value' => '2014-03-08',
  5392. ],
  5393. ];
  5394. $this->assertHtml($expected, $result);
  5395. $result = $this->Form->date('start_day', [
  5396. 'value' => new Date('2014-03-08'),
  5397. ]);
  5398. $this->assertHtml($expected, $result);
  5399. }
  5400. /**
  5401. * testDateTime method
  5402. */
  5403. public function testDateTime(): void
  5404. {
  5405. $result = $this->Form->dateTime('date', ['default' => true]);
  5406. $expected = [
  5407. 'input' => [
  5408. 'type' => 'datetime-local',
  5409. 'name' => 'date',
  5410. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5411. 'step' => '1',
  5412. ],
  5413. ];
  5414. $this->assertHtml($expected, $result);
  5415. }
  5416. /**
  5417. * testDateTimeSecured method
  5418. *
  5419. * Test that datetime fields are added to protected fields list.
  5420. */
  5421. public function testDateTimeSecured(): void
  5422. {
  5423. $this->View->setRequest(
  5424. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5425. );
  5426. $this->Form->create();
  5427. $this->Form->dateTime('date');
  5428. $expected = ['date'];
  5429. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5430. $this->assertEquals($expected, $result);
  5431. $this->Form->fields = [];
  5432. $this->Form->date('published');
  5433. $expected = ['date', 'published'];
  5434. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5435. $this->assertEquals($expected, $result);
  5436. }
  5437. /**
  5438. * testDateTimeSecuredDisabled method
  5439. *
  5440. * Test that datetime fields are added to protected fields list.
  5441. */
  5442. public function testDateTimeSecuredDisabled(): void
  5443. {
  5444. $this->View->setRequest(
  5445. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5446. );
  5447. $this->Form->create();
  5448. $this->Form->dateTime('date', ['secure' => false]);
  5449. $expected = [];
  5450. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5451. $this->assertEquals($expected, $result);
  5452. $this->Form->fields = [];
  5453. $this->Form->date('published', ['secure' => false]);
  5454. $expected = [];
  5455. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5456. $this->assertEquals($expected, $result);
  5457. }
  5458. /**
  5459. * testDatetimeWithDefault method
  5460. *
  5461. * Test that datetime() and default values work.
  5462. */
  5463. public function testDatetimeWithDefault(): void
  5464. {
  5465. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5466. $expected = [
  5467. 'input' => [
  5468. 'type' => 'datetime-local',
  5469. 'name' => 'updated',
  5470. 'value' => '2009-06-01T11:15:30',
  5471. 'step' => '1',
  5472. ],
  5473. ];
  5474. $this->assertHtml($expected, $result);
  5475. $result = $this->Form->dateTime('updated', [
  5476. 'default' => '2009-06-01 11:15:30',
  5477. ]);
  5478. $expected = [
  5479. 'input' => [
  5480. 'type' => 'datetime-local',
  5481. 'name' => 'updated',
  5482. 'value' => '2009-06-01T11:15:30',
  5483. 'step' => '1',
  5484. ],
  5485. ];
  5486. $this->assertHtml($expected, $result);
  5487. }
  5488. /**
  5489. * testMonth method
  5490. *
  5491. * Test generation of a month input.
  5492. */
  5493. public function testMonth(): void
  5494. {
  5495. $result = $this->Form->month('field', ['value' => '']);
  5496. $expected = [
  5497. 'input' => [
  5498. 'type' => 'month',
  5499. 'name' => 'field',
  5500. 'value' => '',
  5501. ],
  5502. ];
  5503. $this->assertHtml($expected, $result);
  5504. $this->View->setRequest(
  5505. $this->View->getRequest()->withData('release', '2050-02-10')
  5506. );
  5507. $this->Form->create();
  5508. $result = $this->Form->month('release');
  5509. $expected = [
  5510. 'input' => [
  5511. 'type' => 'month',
  5512. 'name' => 'release',
  5513. 'value' => '2050-02',
  5514. ],
  5515. ];
  5516. $this->assertHtml($expected, $result);
  5517. $this->View->setRequest(
  5518. $this->View->getRequest()->withData('release', '2050-03')
  5519. );
  5520. $this->Form->create();
  5521. $result = $this->Form->month('release');
  5522. $expected = [
  5523. 'input' => [
  5524. 'type' => 'month',
  5525. 'name' => 'release',
  5526. 'value' => '2050-03',
  5527. ],
  5528. ];
  5529. $this->assertHtml($expected, $result);
  5530. }
  5531. /**
  5532. * testYear method
  5533. *
  5534. * Test generation of a year input.
  5535. */
  5536. public function testYear(): void
  5537. {
  5538. $this->View->setRequest(
  5539. $this->View->getRequest()->withData('published', '2006')
  5540. );
  5541. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5542. $expected = [
  5543. ['select' => ['name' => 'field']],
  5544. ['option' => ['selected' => 'selected', 'value' => '']],
  5545. '/option',
  5546. ['option' => ['value' => '2007']],
  5547. '2007',
  5548. '/option',
  5549. ['option' => ['value' => '2006']],
  5550. '2006',
  5551. '/option',
  5552. '/select',
  5553. ];
  5554. $this->assertHtml($expected, $result);
  5555. $result = $this->Form->year('field', [
  5556. 'value' => '',
  5557. 'min' => 2006,
  5558. 'max' => 2007,
  5559. 'order' => 'asc',
  5560. ]);
  5561. $expected = [
  5562. ['select' => ['name' => 'field']],
  5563. ['option' => ['selected' => 'selected', 'value' => '']],
  5564. '/option',
  5565. ['option' => ['value' => '2006']],
  5566. '2006',
  5567. '/option',
  5568. ['option' => ['value' => '2007']],
  5569. '2007',
  5570. '/option',
  5571. '/select',
  5572. ];
  5573. $this->assertHtml($expected, $result);
  5574. $result = $this->Form->year('published', [
  5575. 'empty' => false,
  5576. 'min' => 2006,
  5577. 'max' => 2007,
  5578. ]);
  5579. $expected = [
  5580. ['select' => ['name' => 'published']],
  5581. ['option' => ['value' => '2007']],
  5582. '2007',
  5583. '/option',
  5584. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5585. '2006',
  5586. '/option',
  5587. '/select',
  5588. ];
  5589. $this->assertHtml($expected, $result);
  5590. $result = $this->Form->year('published', [
  5591. 'empty' => false,
  5592. 'value' => new Date('2008-01-12'),
  5593. 'min' => 2007,
  5594. 'max' => 2009,
  5595. ]);
  5596. $expected = [
  5597. ['select' => ['name' => 'published']],
  5598. ['option' => ['value' => '2009']],
  5599. '2009',
  5600. '/option',
  5601. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5602. '2008',
  5603. '/option',
  5604. ['option' => ['value' => '2007']],
  5605. '2007',
  5606. '/option',
  5607. '/select',
  5608. ];
  5609. $this->assertHtml($expected, $result);
  5610. $result = $this->Form->year('published', [
  5611. 'empty' => 'Published on',
  5612. ]);
  5613. $this->assertStringContainsString('Published on', $result);
  5614. }
  5615. /**
  5616. * testControlYearPreEpoch method
  5617. *
  5618. * Test minYear being prior to the unix epoch.
  5619. */
  5620. public function testControlYearPreEpoch(): void
  5621. {
  5622. $start = date('Y') - 80;
  5623. $end = date('Y') - 18;
  5624. $result = $this->Form->control('birth_year', [
  5625. 'type' => 'year',
  5626. 'label' => 'Birth Year',
  5627. 'min' => $start,
  5628. 'max' => $end,
  5629. ]);
  5630. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5631. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5632. $this->assertStringNotContainsString('value="00">00', $result);
  5633. }
  5634. /**
  5635. * test control() datetime & required attributes
  5636. */
  5637. public function testControlDatetimeRequired(): void
  5638. {
  5639. $result = $this->Form->control('birthday', [
  5640. 'type' => 'date',
  5641. 'required' => true,
  5642. ]);
  5643. $this->assertStringContainsString(
  5644. '<input type="date" name="birthday" required="required"',
  5645. $result
  5646. );
  5647. }
  5648. /**
  5649. * testYearAutoExpandRange method
  5650. */
  5651. public function testYearAutoExpandRange(): void
  5652. {
  5653. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5654. $result = $this->Form->year('birthday');
  5655. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5656. $result = $matches[1];
  5657. $expected = range(date('Y') + 5, 1930);
  5658. $this->assertEquals($expected, $result);
  5659. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5660. $this->Form->create();
  5661. $result = $this->Form->year('release');
  5662. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5663. $result = $matches[1];
  5664. $expected = range(2050, date('Y') - 5);
  5665. $this->assertEquals($expected, $result);
  5666. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5667. $this->Form->create();
  5668. $result = $this->Form->year('release', [
  5669. 'min' => 1890,
  5670. 'max' => 1900,
  5671. ]);
  5672. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5673. $result = $matches[1];
  5674. $expected = range(1900, 1881);
  5675. $this->assertEquals($expected, $result);
  5676. }
  5677. /**
  5678. * test control placeholder + label
  5679. */
  5680. public function testControlLabelAndPlaceholder(): void
  5681. {
  5682. $this->Form->create($this->article);
  5683. $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
  5684. $expected = [
  5685. 'div' => ['class' => 'input text required'],
  5686. 'label' => ['for' => 'title'],
  5687. 'Title',
  5688. '/label',
  5689. 'input' => [
  5690. 'aria-required' => 'true',
  5691. 'type' => 'text',
  5692. 'required' => 'required',
  5693. 'placeholder' => 'Add title',
  5694. 'id' => 'title',
  5695. 'name' => 'title',
  5696. 'data-validity-message' => 'This field cannot be left empty',
  5697. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5698. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5699. ],
  5700. '/div',
  5701. ];
  5702. $this->assertHtml($expected, $result);
  5703. }
  5704. /**
  5705. * testControlLabelFalse method
  5706. *
  5707. * Test the label option being set to false.
  5708. */
  5709. public function testControlLabelFalse(): void
  5710. {
  5711. $this->Form->create($this->article);
  5712. $result = $this->Form->control('title', ['label' => false]);
  5713. $expected = [
  5714. 'div' => ['class' => 'input text required'],
  5715. 'input' => [
  5716. 'aria-required' => 'true',
  5717. 'type' => 'text',
  5718. 'required' => 'required',
  5719. 'id' => 'title',
  5720. 'name' => 'title',
  5721. 'data-validity-message' => 'This field cannot be left empty',
  5722. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5723. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5724. ],
  5725. '/div',
  5726. ];
  5727. $this->assertHtml($expected, $result);
  5728. $this->Form->create($this->article);
  5729. $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
  5730. $expected['input'] += [
  5731. 'placeholder' => 'Add title',
  5732. 'aria-label' => 'Add title',
  5733. ];
  5734. $this->assertHtml($expected, $result);
  5735. }
  5736. /**
  5737. * testTextArea method
  5738. *
  5739. * Test generation of a textarea input.
  5740. */
  5741. public function testTextArea(): void
  5742. {
  5743. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5744. $result = $this->Form->textarea('field');
  5745. $expected = [
  5746. 'textarea' => ['name' => 'field', 'rows' => 5],
  5747. 'some test data',
  5748. '/textarea',
  5749. ];
  5750. $this->assertHtml($expected, $result);
  5751. $result = $this->Form->textarea('user.bio');
  5752. $expected = [
  5753. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5754. '/textarea',
  5755. ];
  5756. $this->assertHtml($expected, $result);
  5757. $this->View->setRequest($this->View->getRequest()
  5758. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5759. $this->Form->create();
  5760. $result = $this->Form->textarea('field');
  5761. $expected = [
  5762. 'textarea' => ['name' => 'field', 'rows' => 5],
  5763. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5764. '/textarea',
  5765. ];
  5766. $this->assertHtml($expected, $result);
  5767. $this->View->setRequest($this->View->getRequest()->withData(
  5768. 'Model.field',
  5769. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5770. ));
  5771. $this->Form->create();
  5772. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5773. $expected = [
  5774. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5775. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5776. '/textarea',
  5777. ];
  5778. $this->assertHtml($expected, $result);
  5779. $result = $this->Form->textarea('0.OtherModel.field');
  5780. $expected = [
  5781. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5782. '/textarea',
  5783. ];
  5784. $this->assertHtml($expected, $result);
  5785. }
  5786. /**
  5787. * testTextAreaWithStupidCharacters method
  5788. *
  5789. * Test text area with non-ascii characters.
  5790. */
  5791. public function testTextAreaWithStupidCharacters(): void
  5792. {
  5793. $result = $this->Form->textarea('Post.content', [
  5794. 'value' => 'GREAT®',
  5795. 'rows' => '15',
  5796. 'cols' => '75',
  5797. ]);
  5798. $expected = [
  5799. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5800. 'GREAT®',
  5801. '/textarea',
  5802. ];
  5803. $this->assertHtml($expected, $result);
  5804. }
  5805. /**
  5806. * testTextAreaMaxLength method
  5807. *
  5808. * Test textareas maxlength read from schema.
  5809. */
  5810. public function testTextAreaMaxLength(): void
  5811. {
  5812. $this->Form->create([
  5813. 'schema' => [
  5814. 'stuff' => ['type' => 'string', 'length' => 10],
  5815. ],
  5816. ]);
  5817. $result = $this->Form->control('other', ['type' => 'textarea']);
  5818. $expected = [
  5819. 'div' => ['class' => 'input textarea'],
  5820. 'label' => ['for' => 'other'],
  5821. 'Other',
  5822. '/label',
  5823. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  5824. '/textarea',
  5825. '/div',
  5826. ];
  5827. $this->assertHtml($expected, $result);
  5828. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  5829. $expected = [
  5830. 'div' => ['class' => 'input textarea'],
  5831. 'label' => ['for' => 'stuff'],
  5832. 'Stuff',
  5833. '/label',
  5834. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  5835. '/textarea',
  5836. '/div',
  5837. ];
  5838. $this->assertHtml($expected, $result);
  5839. }
  5840. /**
  5841. * testHiddenField method
  5842. *
  5843. * Test generation of a hidden input.
  5844. */
  5845. public function testHidden(): void
  5846. {
  5847. $this->article['errors'] = [
  5848. 'field' => true,
  5849. ];
  5850. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  5851. $this->Form->create($this->article);
  5852. $result = $this->Form->hidden('field', ['id' => 'theID']);
  5853. $expected = [
  5854. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  5855. $this->assertHtml($expected, $result);
  5856. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5857. $expected = [
  5858. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  5859. ];
  5860. $this->assertHtml($expected, $result);
  5861. }
  5862. /**
  5863. * Test hidden() with various boolean values.
  5864. */
  5865. public function testHiddenBooleanValues(): void
  5866. {
  5867. $this->Form->create($this->article);
  5868. $result = $this->Form->hidden('field', ['value' => null]);
  5869. $expected = [
  5870. 'input' => ['type' => 'hidden', 'name' => 'field'],
  5871. ];
  5872. $this->assertHtml($expected, $result);
  5873. $result = $this->Form->hidden('field', ['value' => true]);
  5874. $expected = [
  5875. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  5876. ];
  5877. $this->assertHtml($expected, $result);
  5878. $result = $this->Form->hidden('field', ['value' => false]);
  5879. $expected = [
  5880. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  5881. ];
  5882. $this->assertHtml($expected, $result);
  5883. }
  5884. /**
  5885. * testFileUploadField method
  5886. *
  5887. * Test generation of a file upload input.
  5888. */
  5889. public function testFileUploadField(): void
  5890. {
  5891. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5892. $result = $this->Form->file('Model.upload');
  5893. $this->assertHtml($expected, $result);
  5894. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  5895. 'name' => '', 'type' => '', 'tmp_name' => '',
  5896. 'error' => 4, 'size' => 0,
  5897. ]));
  5898. $result = $this->Form->file('Model.upload');
  5899. $this->assertHtml($expected, $result);
  5900. $this->View->setRequest(
  5901. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  5902. );
  5903. $result = $this->Form->file('Model.upload');
  5904. $this->assertHtml($expected, $result);
  5905. }
  5906. /**
  5907. * testFileUploadOnOtherModel method
  5908. *
  5909. * Test File upload input on a model not used in create().
  5910. */
  5911. public function testFileUploadOnOtherModel(): void
  5912. {
  5913. $this->Form->create($this->article, ['type' => 'file']);
  5914. $result = $this->Form->file('ValidateProfile.city');
  5915. $expected = [
  5916. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  5917. ];
  5918. $this->assertHtml($expected, $result);
  5919. }
  5920. /**
  5921. * testButton method
  5922. *
  5923. * Test generation of a form button.
  5924. */
  5925. public function testButton(): void
  5926. {
  5927. $result = $this->Form->button('Hi');
  5928. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  5929. $this->assertHtml($expected, $result);
  5930. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  5931. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  5932. $this->assertHtml($expected, $result);
  5933. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  5934. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  5935. $this->assertHtml($expected, $result);
  5936. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  5937. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  5938. $this->assertHtml($expected, $result);
  5939. $result = $this->Form->button('No type', ['type' => false]);
  5940. $expected = ['button' => [], 'No type', '/button'];
  5941. $this->assertHtml($expected, $result);
  5942. $result = $this->Form->button('Upload Text', [
  5943. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5944. 'escape' => false,
  5945. ]);
  5946. $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
  5947. }
  5948. /**
  5949. * testButtonUnlockedByDefault method
  5950. *
  5951. * Test that button() makes unlocked fields by default.
  5952. */
  5953. public function testButtonUnlockedByDefault(): void
  5954. {
  5955. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5956. $this->Form->create();
  5957. $this->Form->button('Save', ['name' => 'save']);
  5958. $this->Form->button('Clear');
  5959. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  5960. $this->assertEquals(['save'], $result);
  5961. }
  5962. /**
  5963. * Test generation of a form button with confirm message.
  5964. */
  5965. public function testButtonWithConfirm(): void
  5966. {
  5967. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  5968. $expected = ['button' => [
  5969. 'type' => 'submit',
  5970. 'data-confirm-message' => 'Confirm me!',
  5971. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  5972. ], 'Hi', '/button'];
  5973. $this->assertHtml($expected, $result);
  5974. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  5975. $expected = ['button' => [
  5976. 'type' => 'submit',
  5977. 'data-confirm-message' => 'Confirm "me"!',
  5978. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  5979. ], 'Hi', '/button'];
  5980. $this->assertHtml($expected, $result);
  5981. }
  5982. /**
  5983. * testPostButton method
  5984. */
  5985. public function testPostButton(): void
  5986. {
  5987. $result = $this->Form->postButton('Hi', '/controller/action');
  5988. $expected = [
  5989. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  5990. 'button' => ['type' => 'submit'],
  5991. 'Hi',
  5992. '/button',
  5993. '/form',
  5994. ];
  5995. $this->assertHtml($expected, $result);
  5996. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  5997. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  5998. }
  5999. /**
  6000. * testPostButtonMethodType method
  6001. */
  6002. public function testPostButtonMethodType(): void
  6003. {
  6004. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6005. $expected = [
  6006. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6007. 'div' => ['style' => 'display:none;'],
  6008. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6009. '/div',
  6010. 'button' => ['type' => 'submit'],
  6011. 'Hi',
  6012. '/button',
  6013. '/form',
  6014. ];
  6015. $this->assertHtml($expected, $result);
  6016. }
  6017. /**
  6018. * testPostButtonFormOptions method
  6019. */
  6020. public function testPostButtonFormOptions(): void
  6021. {
  6022. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6023. $expected = [
  6024. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6025. 'button' => ['type' => 'submit'],
  6026. 'Hi',
  6027. '/button',
  6028. '/form',
  6029. ];
  6030. $this->assertHtml($expected, $result);
  6031. }
  6032. /**
  6033. * testPostButtonNestedData method
  6034. *
  6035. * Test using postButton with N dimensional data.
  6036. */
  6037. public function testPostButtonNestedData(): void
  6038. {
  6039. $data = [
  6040. 'one' => [
  6041. 'two' => [
  6042. 3, 4, 5,
  6043. ],
  6044. ],
  6045. ];
  6046. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6047. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6048. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6049. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6050. }
  6051. /**
  6052. * testSecurePostButton method
  6053. *
  6054. * Test that postButton adds _Token fields.
  6055. */
  6056. public function testSecurePostButton(): void
  6057. {
  6058. $this->View->setRequest($this->View->getRequest()
  6059. ->withAttribute('csrfToken', 'testkey')
  6060. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6061. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6062. $tokenDebug = urlencode(json_encode([
  6063. '/posts/delete/1',
  6064. [],
  6065. [],
  6066. ]));
  6067. $expected = [
  6068. 'form' => [
  6069. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6070. ],
  6071. ['div' => ['style' => 'display:none;']],
  6072. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6073. '/div',
  6074. 'button' => ['type' => 'submit'],
  6075. 'Delete',
  6076. '/button',
  6077. ['div' => ['style' => 'display:none;']],
  6078. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6079. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6080. ['input' => [
  6081. 'type' => 'hidden',
  6082. 'name' => '_Token[debug]',
  6083. 'value' => $tokenDebug,
  6084. 'autocomplete' => 'off',
  6085. ]],
  6086. '/div',
  6087. '/form',
  6088. ];
  6089. $this->assertHtml($expected, $result);
  6090. }
  6091. /**
  6092. * testPostLink method
  6093. */
  6094. public function testPostLink(): void
  6095. {
  6096. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6097. $expected = [
  6098. 'form' => [
  6099. 'method' => 'post', 'action' => '/posts/delete/1',
  6100. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6101. ],
  6102. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6103. '/form',
  6104. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6105. 'Delete',
  6106. '/a',
  6107. ];
  6108. $this->assertHtml($expected, $result);
  6109. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6110. $expected = [
  6111. 'form' => [
  6112. 'method' => 'post', 'action' => '/posts/delete/1',
  6113. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6114. ],
  6115. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6116. '/form',
  6117. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6118. 'Delete',
  6119. '/a',
  6120. ];
  6121. $this->assertHtml($expected, $result);
  6122. $result = $this->Form->postLink(
  6123. 'Delete',
  6124. '/posts/delete/1',
  6125. ['target' => '_blank', 'class' => 'btn btn-danger']
  6126. );
  6127. $expected = [
  6128. 'form' => [
  6129. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6130. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6131. ],
  6132. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6133. '/form',
  6134. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6135. 'Delete',
  6136. '/a',
  6137. ];
  6138. $this->assertHtml($expected, $result);
  6139. }
  6140. /**
  6141. * testPostLinkWithConfirm method
  6142. *
  6143. * Test the confirm option for postLink().
  6144. */
  6145. public function testPostLinkWithConfirm(): void
  6146. {
  6147. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6148. $expected = [
  6149. 'form' => [
  6150. 'method' => 'post', 'action' => '/posts/delete/1',
  6151. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6152. ],
  6153. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6154. '/form',
  6155. 'a' => [
  6156. 'href' => '#',
  6157. 'data-confirm-message' => 'Confirm?',
  6158. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6159. ],
  6160. 'Delete',
  6161. '/a',
  6162. ];
  6163. $this->assertHtml($expected, $result);
  6164. $result = $this->Form->postLink(
  6165. 'Delete',
  6166. '/posts/delete/1',
  6167. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6168. );
  6169. $expected = [
  6170. 'form' => [
  6171. 'method' => 'post', 'action' => '/posts/delete/1',
  6172. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6173. ],
  6174. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6175. '/form',
  6176. 'a' => [
  6177. 'href' => '#',
  6178. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6179. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6180. ],
  6181. 'Delete',
  6182. '/a',
  6183. ];
  6184. $this->assertHtml($expected, $result);
  6185. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6186. $result = $this->Form->postLink(
  6187. 'Delete',
  6188. '/posts/delete/1',
  6189. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6190. );
  6191. $expected = [
  6192. 'form' => [
  6193. 'method' => 'post', 'action' => '/posts/delete/1',
  6194. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6195. ],
  6196. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6197. '/form',
  6198. 'a' => [
  6199. 'href' => '#',
  6200. 'data-confirm-message' => 'Confirm this deletion?',
  6201. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6202. ],
  6203. 'Delete',
  6204. '/a',
  6205. ];
  6206. $this->assertHtml($expected, $result);
  6207. }
  6208. /**
  6209. * testPostLinkWithQuery method
  6210. *
  6211. * Test postLink() with query string args.
  6212. */
  6213. public function testPostLinkWithQuery(): void
  6214. {
  6215. $result = $this->Form->postLink(
  6216. 'Delete',
  6217. ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6218. );
  6219. $expected = [
  6220. 'form' => [
  6221. 'method' => 'post', 'action' => '/Posts/delete/1?a=b&amp;c=d',
  6222. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6223. ],
  6224. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6225. '/form',
  6226. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6227. 'Delete',
  6228. '/a',
  6229. ];
  6230. $this->assertHtml($expected, $result);
  6231. }
  6232. /**
  6233. * testPostLinkWithData method
  6234. *
  6235. * Test postLink with additional data.
  6236. */
  6237. public function testPostLinkWithData(): void
  6238. {
  6239. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6240. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6241. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6242. $this->Form->create($entity);
  6243. $this->Form->end();
  6244. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6245. $this->assertStringContainsString(
  6246. '<input type="hidden" name="name" value="show"',
  6247. $result,
  6248. 'should not contain entity data.'
  6249. );
  6250. }
  6251. /**
  6252. * testPostLinkSecurityHash method
  6253. *
  6254. * Test that security hashes for postLink include the url.
  6255. */
  6256. public function testPostLinkSecurityHash(): void
  6257. {
  6258. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6259. $hash .= '%3Aid';
  6260. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6261. $result = $this->Form->postLink(
  6262. 'Delete',
  6263. '/posts/delete/1',
  6264. ['data' => ['id' => 1]]
  6265. );
  6266. $tokenDebug = urlencode(json_encode([
  6267. '/posts/delete/1',
  6268. [
  6269. 'id' => 1,
  6270. ],
  6271. [],
  6272. ]));
  6273. $expected = [
  6274. 'form' => [
  6275. 'method' => 'post', 'action' => '/posts/delete/1',
  6276. 'name', 'style' => 'display:none;',
  6277. ],
  6278. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6279. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6280. 'div' => ['style' => 'display:none;'],
  6281. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6282. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6283. ['input' => [
  6284. 'type' => 'hidden',
  6285. 'name' => '_Token[debug]',
  6286. 'value' => $tokenDebug,
  6287. 'autocomplete' => 'off',
  6288. ]],
  6289. '/div',
  6290. '/form',
  6291. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6292. 'Delete',
  6293. '/a',
  6294. ];
  6295. $this->assertHtml($expected, $result);
  6296. }
  6297. /**
  6298. * testPostLinkSecurityHashBlockMode method
  6299. *
  6300. * Test that postLink doesn't modify the fields in the containing form.
  6301. *
  6302. * postLink() calls inside open forms should not modify the field list
  6303. * for the form.
  6304. */
  6305. public function testPostLinkSecurityHashBlockMode(): void
  6306. {
  6307. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6308. $hash .= '%3A';
  6309. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6310. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6311. $this->Form->control('title');
  6312. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6313. $result = $this->View->fetch('postLink');
  6314. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6315. $this->assertEquals(['title'], $fields);
  6316. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6317. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6318. $reflect->setAccessible(true);
  6319. $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6320. }
  6321. /**
  6322. * testPostLinkSecurityHashNoDebugMode method
  6323. *
  6324. * Test that security does not include debug token if debug is false.
  6325. */
  6326. public function testPostLinkSecurityHashNoDebugMode(): void
  6327. {
  6328. Configure::write('debug', false);
  6329. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6330. $hash .= '%3Aid';
  6331. $this->View->setRequest($this->View->getRequest()
  6332. ->withAttribute('formTokenData', ['key' => 'test']));
  6333. $result = $this->Form->postLink(
  6334. 'Delete',
  6335. '/posts/delete/1',
  6336. ['data' => ['id' => 1]]
  6337. );
  6338. $expected = [
  6339. 'form' => [
  6340. 'method' => 'post', 'action' => '/posts/delete/1',
  6341. 'name', 'style' => 'display:none;',
  6342. ],
  6343. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6344. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6345. 'div' => ['style' => 'display:none;'],
  6346. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6347. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6348. '/div',
  6349. '/form',
  6350. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6351. 'Delete',
  6352. '/a',
  6353. ];
  6354. $this->assertHtml($expected, $result);
  6355. }
  6356. /**
  6357. * testPostLinkNestedData method
  6358. *
  6359. * Test using postLink with N dimensional data.
  6360. */
  6361. public function testPostLinkNestedData(): void
  6362. {
  6363. $data = [
  6364. 'one' => [
  6365. 'two' => [
  6366. 3, 4, 5,
  6367. ],
  6368. ],
  6369. ];
  6370. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6371. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6372. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6373. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6374. }
  6375. /**
  6376. * testPostLinkAfterGetForm method
  6377. *
  6378. * Test creating postLinks after a GET form.
  6379. */
  6380. public function testPostLinkAfterGetForm(): void
  6381. {
  6382. $this->View->setRequest($this->View->getRequest()
  6383. ->withAttribute('csrfToken', 'testkey')
  6384. ->withAttribute('formTokenData', []));
  6385. $this->Form->create($this->article, ['type' => 'get']);
  6386. $this->Form->end();
  6387. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6388. $tokenDebug = urlencode(json_encode([
  6389. '/posts/delete/1',
  6390. [],
  6391. [],
  6392. ]));
  6393. $expected = [
  6394. 'form' => [
  6395. 'method' => 'post', 'action' => '/posts/delete/1',
  6396. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6397. ],
  6398. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6399. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6400. 'div' => ['style' => 'display:none;'],
  6401. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6402. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6403. ['input' => [
  6404. 'type' => 'hidden', 'name' => '_Token[debug]',
  6405. 'value' => $tokenDebug,
  6406. 'autocomplete' => 'off',
  6407. ]],
  6408. '/div',
  6409. '/form',
  6410. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6411. 'Delete',
  6412. '/a',
  6413. ];
  6414. $this->assertHtml($expected, $result);
  6415. }
  6416. /**
  6417. * testPostLinkFormBuffer method
  6418. *
  6419. * Test that postLink adds form tags to view block.
  6420. */
  6421. public function testPostLinkFormBuffer(): void
  6422. {
  6423. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6424. $expected = [
  6425. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6426. 'Delete',
  6427. '/a',
  6428. ];
  6429. $this->assertHtml($expected, $result);
  6430. $result = $this->View->fetch('postLink');
  6431. $expected = [
  6432. 'form' => [
  6433. 'method' => 'post', 'action' => '/posts/delete/1',
  6434. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6435. ],
  6436. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6437. '/form',
  6438. ];
  6439. $this->assertHtml($expected, $result);
  6440. $result = $this->Form->postLink(
  6441. 'Delete',
  6442. '/posts/delete/2',
  6443. ['block' => true, 'method' => 'DELETE']
  6444. );
  6445. $expected = [
  6446. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6447. 'Delete',
  6448. '/a',
  6449. ];
  6450. $this->assertHtml($expected, $result);
  6451. $result = $this->View->fetch('postLink');
  6452. $expected = [
  6453. 'form' => [
  6454. 'method' => 'post', 'action' => '/posts/delete/1',
  6455. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6456. ],
  6457. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6458. '/form',
  6459. [
  6460. 'form' => [
  6461. 'method' => 'post', 'action' => '/posts/delete/2',
  6462. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6463. ],
  6464. ],
  6465. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6466. '/form',
  6467. ];
  6468. $this->assertHtml($expected, $result);
  6469. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6470. $expected = [
  6471. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6472. 'Delete',
  6473. '/a',
  6474. ];
  6475. $this->assertHtml($expected, $result);
  6476. $result = $this->View->fetch('foobar');
  6477. $expected = [
  6478. 'form' => [
  6479. 'method' => 'post', 'action' => '/posts/delete/1',
  6480. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6481. ],
  6482. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6483. '/form',
  6484. ];
  6485. $this->assertHtml($expected, $result);
  6486. }
  6487. /**
  6488. * testSubmitButton method
  6489. */
  6490. public function testSubmitButton(): void
  6491. {
  6492. $result = $this->Form->submit('');
  6493. $expected = [
  6494. 'div' => ['class' => 'submit'],
  6495. 'input' => ['type' => 'submit', 'value' => ''],
  6496. '/div',
  6497. ];
  6498. $this->assertHtml($expected, $result);
  6499. $result = $this->Form->submit('Test Submit');
  6500. $expected = [
  6501. 'div' => ['class' => 'submit'],
  6502. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6503. '/div',
  6504. ];
  6505. $this->assertHtml($expected, $result);
  6506. $result = $this->Form->submit('Next >');
  6507. $expected = [
  6508. 'div' => ['class' => 'submit'],
  6509. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6510. '/div',
  6511. ];
  6512. $this->assertHtml($expected, $result);
  6513. $result = $this->Form->submit('Next >', ['escape' => false]);
  6514. $expected = [
  6515. 'div' => ['class' => 'submit'],
  6516. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6517. '/div',
  6518. ];
  6519. $this->assertHtml($expected, $result);
  6520. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6521. $expected = [
  6522. 'div' => ['class' => 'submit'],
  6523. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6524. '/div',
  6525. ];
  6526. $this->assertHtml($expected, $result);
  6527. }
  6528. /**
  6529. * testSubmitImage method
  6530. *
  6531. * Test image submit types.
  6532. */
  6533. public function testSubmitImage(): void
  6534. {
  6535. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6536. $expected = [
  6537. 'div' => ['class' => 'submit'],
  6538. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6539. '/div',
  6540. ];
  6541. $this->assertHtml($expected, $result);
  6542. $result = $this->Form->submit('/relative/cake.power.gif');
  6543. $expected = [
  6544. 'div' => ['class' => 'submit'],
  6545. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6546. '/div',
  6547. ];
  6548. $this->assertHtml($expected, $result);
  6549. $result = $this->Form->submit('cake.power.gif');
  6550. $expected = [
  6551. 'div' => ['class' => 'submit'],
  6552. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6553. '/div',
  6554. ];
  6555. $this->assertHtml($expected, $result);
  6556. $result = $this->Form->submit('Not.an.image');
  6557. $expected = [
  6558. 'div' => ['class' => 'submit'],
  6559. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6560. '/div',
  6561. ];
  6562. $this->assertHtml($expected, $result);
  6563. }
  6564. /**
  6565. * testSubmitUnlockedByDefault method
  6566. *
  6567. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6568. * be submitted at a time.
  6569. */
  6570. public function testSubmitUnlockedByDefault(): void
  6571. {
  6572. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6573. $this->Form->create();
  6574. $this->Form->submit('Go go');
  6575. $this->Form->submit('Save', ['name' => 'save']);
  6576. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6577. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6578. }
  6579. /**
  6580. * testSubmitImageTimestamp method
  6581. *
  6582. * Test submit image with timestamps.
  6583. */
  6584. public function testSubmitImageTimestamp(): void
  6585. {
  6586. Configure::write('Asset.timestamp', 'force');
  6587. $result = $this->Form->submit('cake.power.gif');
  6588. $expected = [
  6589. 'div' => ['class' => 'submit'],
  6590. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6591. '/div',
  6592. ];
  6593. $this->assertHtml($expected, $result);
  6594. }
  6595. /**
  6596. * testDateTimeWithGetForms method
  6597. *
  6598. * Test that datetime() works with GET style forms.
  6599. */
  6600. public function testDateTimeWithGetForms(): void
  6601. {
  6602. $this->Form->create($this->article, ['type' => 'get']);
  6603. $result = $this->Form->datetime('created');
  6604. $expected = [
  6605. 'input' => [
  6606. 'type' => 'datetime-local',
  6607. 'name' => 'created',
  6608. 'value' => '',
  6609. 'step' => '1',
  6610. ],
  6611. ];
  6612. $this->assertHtml($expected, $result);
  6613. $result = $this->Form->datetime('created', ['default' => true]);
  6614. $expected = [
  6615. 'input' => [
  6616. 'type' => 'datetime-local',
  6617. 'name' => 'created',
  6618. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6619. 'step' => '1',
  6620. ],
  6621. ];
  6622. $this->assertHtml($expected, $result);
  6623. }
  6624. /**
  6625. * Provides fractional schema types
  6626. *
  6627. * @return array
  6628. */
  6629. public function fractionalTypeProvider(): array
  6630. {
  6631. return [
  6632. ['datetimefractional'],
  6633. ['timestampfractional'],
  6634. ['timestamptimezone'],
  6635. ];
  6636. }
  6637. /**
  6638. * testDateTimeWithFractional method
  6639. *
  6640. * Test that datetime() works with datetimefractional.
  6641. *
  6642. * @dataProvider fractionalTypeProvider
  6643. */
  6644. public function testDateTimeWithFractional(string $type): void
  6645. {
  6646. $this->Form->create([
  6647. 'schema' => [
  6648. 'created' => ['type' => $type],
  6649. ],
  6650. ]);
  6651. $result = $this->Form->datetime('created', [
  6652. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6653. ]);
  6654. $expected = [
  6655. 'input' => [
  6656. 'type' => 'datetime-local',
  6657. 'name' => 'created',
  6658. 'value' => '2019-09-27T02:52:43.123',
  6659. 'step' => '0.001',
  6660. ],
  6661. ];
  6662. $this->assertHtml($expected, $result);
  6663. }
  6664. /**
  6665. * testControlWithFractional method
  6666. *
  6667. * Test that control() works with datetimefractional.
  6668. *
  6669. * @dataProvider fractionalTypeProvider
  6670. */
  6671. public function testControlWithFractional(string $type): void
  6672. {
  6673. $this->Form->create([
  6674. 'schema' => [
  6675. 'created' => ['type' => $type],
  6676. ],
  6677. ]);
  6678. $result = $this->Form->control('created', [
  6679. 'val' => new DateTime('2019-09-27 02:52:43.123'),
  6680. ]);
  6681. $expected = [
  6682. 'div' => ['class' => 'input datetime'],
  6683. 'label' => ['for' => 'created'],
  6684. 'Created',
  6685. '/label',
  6686. 'input' => [
  6687. 'type' => 'datetime-local',
  6688. 'name' => 'created',
  6689. 'id' => 'created',
  6690. 'value' => '2019-09-27T02:52:43.123',
  6691. 'step' => '0.001',
  6692. ],
  6693. '/div',
  6694. ];
  6695. $this->assertHtml($expected, $result);
  6696. }
  6697. /**
  6698. * testForMagicControlNonExistentNotValidated method
  6699. */
  6700. public function testForMagicControlNonExistentNotValidated(): void
  6701. {
  6702. $this->Form->create($this->article);
  6703. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6704. $result = $this->Form->control('nonexistent_not_validated');
  6705. $expected = [
  6706. 'label' => ['for' => 'nonexistent-not-validated'],
  6707. 'Nonexistent Not Validated',
  6708. '/label',
  6709. 'input' => [
  6710. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6711. 'id' => 'nonexistent-not-validated',
  6712. ],
  6713. ];
  6714. $this->assertHtml($expected, $result);
  6715. $result = $this->Form->control('nonexistent_not_validated', [
  6716. 'val' => 'my value',
  6717. ]);
  6718. $expected = [
  6719. 'label' => ['for' => 'nonexistent-not-validated'],
  6720. 'Nonexistent Not Validated',
  6721. '/label',
  6722. 'input' => [
  6723. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6724. 'value' => 'my value', 'id' => 'nonexistent-not-validated',
  6725. ],
  6726. ];
  6727. $this->assertHtml($expected, $result);
  6728. $this->View->setRequest(
  6729. $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
  6730. );
  6731. $this->Form->create($this->article);
  6732. $result = $this->Form->control('nonexistent_not_validated');
  6733. $expected = [
  6734. 'label' => ['for' => 'nonexistent-not-validated'],
  6735. 'Nonexistent Not Validated',
  6736. '/label',
  6737. 'input' => [
  6738. 'type' => 'text', 'name' => 'nonexistent_not_validated',
  6739. 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
  6740. ],
  6741. ];
  6742. $this->assertHtml($expected, $result);
  6743. }
  6744. /**
  6745. * testFormMagicControlLabel method
  6746. */
  6747. public function testFormMagicControlLabel(): void
  6748. {
  6749. $this->getTableLocator()->get('Contacts', [
  6750. 'className' => ContactsTable::class,
  6751. ]);
  6752. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6753. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6754. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6755. $expected = [
  6756. 'label' => ['for' => 'contacts-name'],
  6757. 'My label',
  6758. '/label',
  6759. 'input' => [
  6760. 'type' => 'text',
  6761. 'name' => 'Contacts[name]',
  6762. 'id' => 'contacts-name',
  6763. 'maxlength' => '255',
  6764. ],
  6765. ];
  6766. $this->assertHtml($expected, $result);
  6767. $result = $this->Form->control('name', [
  6768. 'label' => ['class' => 'mandatory'],
  6769. ]);
  6770. $expected = [
  6771. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6772. 'Name',
  6773. '/label',
  6774. 'input' => [
  6775. 'type' => 'text', 'name' => 'name',
  6776. 'id' => 'name', 'maxlength' => '255',
  6777. ],
  6778. ];
  6779. $this->assertHtml($expected, $result);
  6780. $result = $this->Form->control('name', [
  6781. 'div' => false,
  6782. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6783. ]);
  6784. $expected = [
  6785. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6786. 'My label',
  6787. '/label',
  6788. 'input' => [
  6789. 'type' => 'text', 'name' => 'name',
  6790. 'id' => 'name', 'maxlength' => '255',
  6791. ],
  6792. ];
  6793. $this->assertHtml($expected, $result);
  6794. $result = $this->Form->control('Contact.name', [
  6795. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  6796. ]);
  6797. $expected = [
  6798. 'label' => ['for' => 'my_id'],
  6799. 'Name',
  6800. '/label',
  6801. 'input' => [
  6802. 'type' => 'text', 'name' => 'Contact[name]',
  6803. 'id' => 'my_id', 'maxlength' => '255',
  6804. ],
  6805. ];
  6806. $this->assertHtml($expected, $result);
  6807. $result = $this->Form->control('1.id');
  6808. $expected = ['input' => [
  6809. 'type' => 'hidden', 'name' => '1[id]',
  6810. 'id' => '1-id',
  6811. ]];
  6812. $this->assertHtml($expected, $result);
  6813. $result = $this->Form->control('1.name');
  6814. $expected = [
  6815. 'label' => ['for' => '1-name'],
  6816. 'Name',
  6817. '/label',
  6818. 'input' => [
  6819. 'type' => 'text', 'name' => '1[name]',
  6820. 'id' => '1-name', 'maxlength' => '255',
  6821. ],
  6822. ];
  6823. $this->assertHtml($expected, $result);
  6824. }
  6825. /**
  6826. * testFormEnd method
  6827. */
  6828. public function testFormEnd(): void
  6829. {
  6830. $this->assertSame('</form>', $this->Form->end());
  6831. }
  6832. /**
  6833. * testMultiRecordForm method
  6834. *
  6835. * Test the generation of fields for a multi record form.
  6836. */
  6837. public function testMultiRecordForm(): void
  6838. {
  6839. $articles = $this->getTableLocator()->get('Articles');
  6840. $articles->hasMany('Comments');
  6841. $comment = new Entity(['comment' => 'Value']);
  6842. $article = new Article(['comments' => [$comment]]);
  6843. $this->Form->create([$article]);
  6844. $result = $this->Form->control('0.comments.1.comment');
  6845. // phpcs:disable
  6846. $expected = [
  6847. 'div' => ['class' => 'input textarea'],
  6848. 'label' => ['for' => '0-comments-1-comment'],
  6849. 'Comment',
  6850. '/label',
  6851. 'textarea' => [
  6852. 'name',
  6853. 'id' => '0-comments-1-comment',
  6854. 'rows' => 5
  6855. ],
  6856. '/textarea',
  6857. '/div'
  6858. ];
  6859. // phpcs:enable
  6860. $this->assertHtml($expected, $result);
  6861. $result = $this->Form->control('0.comments.0.comment');
  6862. // phpcs:disable
  6863. $expected = [
  6864. 'div' => ['class' => 'input textarea'],
  6865. 'label' => ['for' => '0-comments-0-comment'],
  6866. 'Comment',
  6867. '/label',
  6868. 'textarea' => [
  6869. 'name',
  6870. 'id' => '0-comments-0-comment',
  6871. 'rows' => 5
  6872. ],
  6873. 'Value',
  6874. '/textarea',
  6875. '/div'
  6876. ];
  6877. // phpcs:enable
  6878. $this->assertHtml($expected, $result);
  6879. $comment->setError('comment', ['Not valid']);
  6880. $result = $this->Form->control('0.comments.0.comment');
  6881. // phpcs:disable
  6882. $expected = [
  6883. 'div' => ['class' => 'input textarea error'],
  6884. 'label' => ['for' => '0-comments-0-comment'],
  6885. 'Comment',
  6886. '/label',
  6887. 'textarea' => [
  6888. 'name',
  6889. 'class' => 'form-error',
  6890. 'id' => '0-comments-0-comment',
  6891. 'aria-invalid' => 'true',
  6892. 'aria-describedby' => '0-comments-0-comment-error',
  6893. 'rows' => 5
  6894. ],
  6895. 'Value',
  6896. '/textarea',
  6897. ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
  6898. 'Not valid',
  6899. '/div',
  6900. '/div'
  6901. ];
  6902. // phpcs:enable
  6903. $this->assertHtml($expected, $result);
  6904. $this->getTableLocator()->get('Comments')
  6905. ->getValidator('default')
  6906. ->allowEmptyString('comment', null, false);
  6907. $result = $this->Form->control('0.comments.1.comment');
  6908. // phpcs:disable
  6909. $expected = [
  6910. 'div' => ['class' => 'input textarea required'],
  6911. 'label' => ['for' => '0-comments-1-comment'],
  6912. 'Comment',
  6913. '/label',
  6914. 'textarea' => [
  6915. 'name',
  6916. 'aria-required' => 'true',
  6917. 'required' => 'required',
  6918. 'id' => '0-comments-1-comment',
  6919. 'rows' => 5,
  6920. 'data-validity-message' => 'This field cannot be left empty',
  6921. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  6922. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  6923. ],
  6924. '/textarea',
  6925. '/div'
  6926. ];
  6927. // phpcs:enable
  6928. $this->assertHtml($expected, $result);
  6929. }
  6930. /**
  6931. * testHtml5Controls method
  6932. *
  6933. * Test that some html5 inputs + FormHelper::__call() work.
  6934. */
  6935. public function testHtml5Controls(): void
  6936. {
  6937. $result = $this->Form->email('User.email');
  6938. $expected = [
  6939. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  6940. ];
  6941. $this->assertHtml($expected, $result);
  6942. $result = $this->Form->search('User.query');
  6943. $expected = [
  6944. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  6945. ];
  6946. $this->assertHtml($expected, $result);
  6947. $result = $this->Form->search('User.query', ['value' => 'test']);
  6948. $expected = [
  6949. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  6950. ];
  6951. $this->assertHtml($expected, $result);
  6952. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  6953. $expected = [
  6954. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  6955. ];
  6956. $this->assertHtml($expected, $result);
  6957. }
  6958. /**
  6959. * testHtml5ControlWithControl method
  6960. *
  6961. * Test accessing html5 inputs through control().
  6962. */
  6963. public function testHtml5ControlWithControl(): void
  6964. {
  6965. $this->Form->create();
  6966. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6967. $result = $this->Form->control('website', [
  6968. 'type' => 'url',
  6969. 'val' => 'http://domain.tld',
  6970. 'label' => false,
  6971. ]);
  6972. $expected = [
  6973. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  6974. ];
  6975. $this->assertHtml($expected, $result);
  6976. }
  6977. /**
  6978. * testHtml5ControlException method
  6979. *
  6980. * Test errors when field name is missing.
  6981. */
  6982. public function testHtml5ControlException(): void
  6983. {
  6984. $this->expectException(CakeException::class);
  6985. $this->Form->email();
  6986. }
  6987. /**
  6988. * tests fields that are required use custom validation messages
  6989. */
  6990. public function testHtml5ErrorMessage(): void
  6991. {
  6992. $this->Form->setConfig('autoSetCustomValidity', true);
  6993. $validator = (new Validator())
  6994. ->notEmptyString('email', 'Custom error message')
  6995. ->requirePresence('password')
  6996. ->alphaNumeric('password')
  6997. ->notBlank('phone');
  6998. $table = $this->getTableLocator()->get('Contacts', [
  6999. 'className' => ContactsTable::class,
  7000. ]);
  7001. $table->setValidator('default', $validator);
  7002. $contact = new Entity();
  7003. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7004. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7005. $result = $this->Form->control('password');
  7006. $expected = [
  7007. 'label' => ['for' => 'password'],
  7008. 'Password',
  7009. '/label',
  7010. 'input' => [
  7011. 'aria-required' => 'true',
  7012. 'id' => 'password',
  7013. 'name' => 'password',
  7014. 'type' => 'password',
  7015. 'value' => '',
  7016. 'required' => 'required',
  7017. 'data-validity-message' => 'This field cannot be left empty',
  7018. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7019. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7020. ],
  7021. ];
  7022. $this->assertHtml($expected, $result);
  7023. $result = $this->Form->control('phone');
  7024. $expected = [
  7025. 'label' => ['for' => 'phone'],
  7026. 'Phone',
  7027. '/label',
  7028. 'input' => [
  7029. 'aria-required' => 'true',
  7030. 'id' => 'phone',
  7031. 'name' => 'phone',
  7032. 'type' => 'tel',
  7033. 'value' => '',
  7034. 'maxlength' => 255,
  7035. 'required' => 'required',
  7036. 'data-validity-message' => 'This field cannot be left empty',
  7037. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7038. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7039. ],
  7040. ];
  7041. $this->assertHtml($expected, $result);
  7042. $result = $this->Form->control('email');
  7043. $expected = [
  7044. 'label' => ['for' => 'email'],
  7045. 'Email',
  7046. '/label',
  7047. 'input' => [
  7048. 'aria-required' => 'true',
  7049. 'id' => 'email',
  7050. 'name' => 'email',
  7051. 'type' => 'email',
  7052. 'value' => '',
  7053. 'maxlength' => 255,
  7054. 'required' => 'required',
  7055. 'data-validity-message' => 'Custom error message',
  7056. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7057. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7058. ],
  7059. ];
  7060. $this->assertHtml($expected, $result);
  7061. }
  7062. /**
  7063. * tests that custom validation messages are in templateVars
  7064. */
  7065. public function testHtml5ErrorMessageInTemplateVars(): void
  7066. {
  7067. $validator = (new Validator())
  7068. ->notEmptyString('email', 'Custom error "message" & entities')
  7069. ->requirePresence('password')
  7070. ->alphaNumeric('password')
  7071. ->notBlank('phone');
  7072. $table = $this->getTableLocator()->get('Contacts', [
  7073. 'className' => ContactsTable::class,
  7074. ]);
  7075. $table->setValidator('default', $validator);
  7076. $contact = new Entity();
  7077. $this->Form->setConfig('autoSetCustomValidity', false);
  7078. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7079. $this->Form->setTemplates([
  7080. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7081. 'inputContainer' => '{{content}}',
  7082. ]);
  7083. $result = $this->Form->control('password');
  7084. $expected = [
  7085. 'label' => ['for' => 'password'],
  7086. 'Password',
  7087. '/label',
  7088. 'input' => [
  7089. 'aria-required' => 'true',
  7090. 'id' => 'password',
  7091. 'name' => 'password',
  7092. 'type' => 'password',
  7093. 'value' => '',
  7094. 'required' => 'required',
  7095. 'data-message' => 'This field cannot be left empty',
  7096. ],
  7097. ];
  7098. $this->assertHtml($expected, $result);
  7099. $result = $this->Form->control('phone');
  7100. $expected = [
  7101. 'label' => ['for' => 'phone'],
  7102. 'Phone',
  7103. '/label',
  7104. 'input' => [
  7105. 'aria-required' => 'true',
  7106. 'id' => 'phone',
  7107. 'name' => 'phone',
  7108. 'type' => 'tel',
  7109. 'value' => '',
  7110. 'maxlength' => 255,
  7111. 'required' => 'required',
  7112. 'data-message' => 'This field cannot be left empty',
  7113. ],
  7114. ];
  7115. $this->assertHtml($expected, $result);
  7116. $result = $this->Form->control('email', [
  7117. 'templateVars' => [
  7118. 'custom' => 'data-custom="1"',
  7119. ],
  7120. ]);
  7121. $expected = [
  7122. 'label' => ['for' => 'email'],
  7123. 'Email',
  7124. '/label',
  7125. 'input' => [
  7126. 'aria-required' => 'true',
  7127. 'id' => 'email',
  7128. 'name' => 'email',
  7129. 'type' => 'email',
  7130. 'value' => '',
  7131. 'maxlength' => 255,
  7132. 'required' => 'required',
  7133. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7134. 'data-custom' => '1',
  7135. ],
  7136. ];
  7137. $this->assertHtml($expected, $result);
  7138. $this->Form->setConfig('autoSetCustomValidity', true);
  7139. }
  7140. /**
  7141. * testRequiredAttribute method
  7142. *
  7143. * Tests that formhelper sets required attributes.
  7144. */
  7145. public function testRequiredAttribute(): void
  7146. {
  7147. $this->article['required'] = [
  7148. 'title' => true,
  7149. 'body' => false,
  7150. ];
  7151. $this->Form->create($this->article);
  7152. $result = $this->Form->control('title');
  7153. $expected = [
  7154. 'div' => ['class' => 'input text required'],
  7155. 'label' => ['for' => 'title'],
  7156. 'Title',
  7157. '/label',
  7158. 'input' => [
  7159. 'type' => 'text',
  7160. 'name' => 'title',
  7161. 'id' => 'title',
  7162. 'aria-required' => 'true',
  7163. 'required' => 'required',
  7164. 'data-validity-message' => 'This field cannot be left empty',
  7165. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7166. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7167. ],
  7168. '/div',
  7169. ];
  7170. $this->assertHtml($expected, $result);
  7171. $result = $this->Form->control('title', ['required' => false]);
  7172. $this->assertStringNotContainsString('required', $result);
  7173. $result = $this->Form->control('body');
  7174. $expected = [
  7175. 'div' => ['class' => 'input text'],
  7176. 'label' => ['for' => 'body'],
  7177. 'Body',
  7178. '/label',
  7179. 'input' => [
  7180. 'type' => 'text',
  7181. 'name' => 'body',
  7182. 'id' => 'body',
  7183. ],
  7184. '/div',
  7185. ];
  7186. $this->assertHtml($expected, $result);
  7187. $result = $this->Form->control('body', ['required' => true]);
  7188. $this->assertStringContainsString('required', $result);
  7189. }
  7190. /**
  7191. * testControlsNotNested method
  7192. *
  7193. * Tests that it is possible to put inputs outside of the label.
  7194. */
  7195. public function testControlsNotNested(): void
  7196. {
  7197. $this->Form->setTemplates([
  7198. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7199. 'formGroup' => '{{input}}{{label}}',
  7200. ]);
  7201. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7202. $expected = [
  7203. 'div' => ['class' => 'input checkbox'],
  7204. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7205. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7206. 'label' => ['for' => 'foo'],
  7207. 'Foo',
  7208. '/label',
  7209. '/div',
  7210. ];
  7211. $this->assertHtml($expected, $result);
  7212. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7213. $expected = [
  7214. 'div' => ['class' => 'input checkbox'],
  7215. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7216. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7217. '/div',
  7218. ];
  7219. $this->assertHtml($expected, $result);
  7220. $result = $this->Form->control('confirm', [
  7221. 'type' => 'radio',
  7222. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7223. ]);
  7224. $expected = [
  7225. 'div' => ['class' => 'input radio'],
  7226. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7227. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7228. ['label' => ['for' => 'confirm-y']],
  7229. 'Yes',
  7230. '/label',
  7231. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7232. ['label' => ['for' => 'confirm-n']],
  7233. 'No',
  7234. '/label',
  7235. '<label',
  7236. 'Confirm',
  7237. '/label',
  7238. '/div',
  7239. ];
  7240. $this->assertHtml($expected, $result);
  7241. $result = $this->Form->select('category', ['1', '2'], [
  7242. 'multiple' => 'checkbox',
  7243. 'name' => 'fish',
  7244. ]);
  7245. $expected = [
  7246. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7247. ['div' => ['class' => 'checkbox']],
  7248. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7249. ['label' => ['for' => 'fish-0']],
  7250. '1',
  7251. '/label',
  7252. '/div',
  7253. ['div' => ['class' => 'checkbox']],
  7254. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7255. ['label' => ['for' => 'fish-1']],
  7256. '2',
  7257. '/label',
  7258. '/div',
  7259. ];
  7260. $this->assertHtml($expected, $result);
  7261. }
  7262. /**
  7263. * testControlContainerTemplates method
  7264. *
  7265. * Test that *Container templates are used by input.
  7266. */
  7267. public function testControlContainerTemplates(): void
  7268. {
  7269. $this->Form->setTemplates([
  7270. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7271. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7272. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7273. ]);
  7274. $this->article['errors'] = [
  7275. 'Article' => ['published' => 'error message'],
  7276. ];
  7277. $this->Form->create($this->article);
  7278. $result = $this->Form->control('accept', [
  7279. 'type' => 'checkbox',
  7280. ]);
  7281. $expected = [
  7282. 'div' => ['class' => 'check'],
  7283. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7284. 'label' => ['for' => 'accept'],
  7285. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7286. 'Accept',
  7287. '/label',
  7288. '/div',
  7289. ];
  7290. $this->assertHtml($expected, $result);
  7291. $result = $this->Form->control('accept', [
  7292. 'type' => 'radio',
  7293. 'options' => ['Y', 'N'],
  7294. ]);
  7295. $this->assertStringContainsString('<div class="rad">', $result);
  7296. $result = $this->Form->control('Article.published', [
  7297. 'type' => 'radio',
  7298. 'options' => ['Y', 'N'],
  7299. ]);
  7300. $this->assertStringContainsString('<div class="rad err">', $result);
  7301. }
  7302. /**
  7303. * testFormGroupTemplates method
  7304. *
  7305. * Test that *Container templates are used by input.
  7306. */
  7307. public function testFormGroupTemplates(): void
  7308. {
  7309. $this->Form->setTemplates([
  7310. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7311. ]);
  7312. $this->Form->create($this->article);
  7313. $result = $this->Form->control('accept', [
  7314. 'type' => 'radio',
  7315. 'options' => ['Y', 'N'],
  7316. ]);
  7317. $this->assertStringContainsString('<div class="radio">', $result);
  7318. }
  7319. /**
  7320. * testResetTemplates method
  7321. *
  7322. * Test resetting templates.
  7323. */
  7324. public function testResetTemplates(): void
  7325. {
  7326. $this->Form->setTemplates(['input' => '<input/>']);
  7327. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7328. $this->Form->resetTemplates();
  7329. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7330. }
  7331. /**
  7332. * testContext method
  7333. *
  7334. * Test the context method.
  7335. */
  7336. public function testContext(): void
  7337. {
  7338. $result = $this->Form->context();
  7339. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7340. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7341. $this->assertSame($mock, $this->Form->context($mock));
  7342. $this->assertSame($mock, $this->Form->context());
  7343. }
  7344. /**
  7345. * testAutoDomId method
  7346. */
  7347. public function testAutoDomId(): void
  7348. {
  7349. $result = $this->Form->text('field', ['id' => true]);
  7350. $expected = [
  7351. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7352. ];
  7353. $this->assertHtml($expected, $result);
  7354. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7355. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7356. $expected = [
  7357. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7358. ['label' => ['for' => 'field-0']],
  7359. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7360. 'option A',
  7361. '/label',
  7362. ['label' => ['for' => 'field-1']],
  7363. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7364. 'option B',
  7365. '/label',
  7366. ];
  7367. $this->assertHtml($expected, $result);
  7368. $result = $this->Form->select(
  7369. 'multi_field',
  7370. ['first', 'second'],
  7371. ['multiple' => 'checkbox', 'id' => true]
  7372. );
  7373. $expected = [
  7374. 'input' => [
  7375. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '',
  7376. ],
  7377. ['div' => ['class' => 'checkbox']],
  7378. ['label' => ['for' => 'multi-field-0']],
  7379. ['input' => [
  7380. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7381. 'value' => '0', 'id' => 'multi-field-0',
  7382. ]],
  7383. 'first',
  7384. '/label',
  7385. '/div',
  7386. ['div' => ['class' => 'checkbox']],
  7387. ['label' => ['for' => 'multi-field-1']],
  7388. ['input' => [
  7389. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7390. 'value' => '1', 'id' => 'multi-field-1',
  7391. ]],
  7392. 'second',
  7393. '/label',
  7394. '/div',
  7395. ];
  7396. $this->assertHtml($expected, $result);
  7397. }
  7398. /**
  7399. * Test the basic setters and getters for value sources
  7400. */
  7401. public function testFormValueSourcesSettersGetters(): void
  7402. {
  7403. $this->View->setRequest($this->View->getRequest()
  7404. ->withData('id', '1')
  7405. ->withQueryParams(['id' => '2']));
  7406. $expected = ['data', 'context'];
  7407. $result = $this->Form->getValueSources();
  7408. $this->assertEquals($expected, $result);
  7409. $this->Form->setValueSources(['context']);
  7410. $result = $this->Form->getSourceValue('id');
  7411. $this->assertNull($result);
  7412. $this->Form->setValueSources('query');
  7413. $expected = ['query'];
  7414. $result = $this->Form->getValueSources();
  7415. $this->assertEquals($expected, $result);
  7416. $expected = '2';
  7417. $result = $this->Form->getSourceValue('id');
  7418. $this->assertSame($expected, $result);
  7419. $this->Form->setValueSources(['data']);
  7420. $expected = '1';
  7421. $result = $this->Form->getSourceValue('id');
  7422. $this->assertSame($expected, $result);
  7423. $this->Form->setValueSources(['query', 'data']);
  7424. $expected = '2';
  7425. $result = $this->Form->getSourceValue('id');
  7426. $this->assertSame($expected, $result);
  7427. }
  7428. public function testValueSourcesValidation(): void
  7429. {
  7430. $this->expectException(InvalidArgumentException::class);
  7431. $this->expectExceptionMessage('Invalid value source(s): invalid, foo. Valid values are: context, data, query');
  7432. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7433. }
  7434. /**
  7435. * Tests the different input rendering values based on sources values switching
  7436. */
  7437. public function testFormValueSourcesSingleSwitchRendering(): void
  7438. {
  7439. $articles = $this->getTableLocator()->get('Articles');
  7440. $article = new Article();
  7441. $articles->patchEntity($article, ['id' => '3']);
  7442. $this->Form->create($article);
  7443. $this->Form->setValueSources(['context']);
  7444. $result = $this->Form->control('id');
  7445. $expected = [
  7446. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7447. ];
  7448. $this->assertHtml($expected, $result);
  7449. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7450. $this->Form->setValueSources(['query']);
  7451. $result = $this->Form->control('id');
  7452. $expected = [
  7453. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7454. ];
  7455. $this->assertHtml($expected, $result);
  7456. $this->View->setRequest($this->View->getRequest()
  7457. ->withQueryParams(['id' => '5a'])
  7458. ->withData('id', '5b'));
  7459. $this->Form->setValueSources(['context']);
  7460. $this->Form->create($article);
  7461. $result = $this->Form->control('id');
  7462. $expected = [
  7463. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7464. ];
  7465. $this->assertHtml($expected, $result);
  7466. $this->Form->setValueSources(['data']);
  7467. $this->Form->create($article);
  7468. $result = $this->Form->control('id');
  7469. $expected = [
  7470. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7471. ];
  7472. $this->assertHtml($expected, $result);
  7473. $this->Form->setValueSources(['query']);
  7474. $result = $this->Form->control('id');
  7475. $expected = [
  7476. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7477. ];
  7478. $this->assertHtml($expected, $result);
  7479. }
  7480. /**
  7481. * Tests the different input rendering values based on sources values switching while supplying
  7482. * an entity (base context) and multiple sources (such as data, query)
  7483. */
  7484. public function testFormValueSourcesListSwitchRendering(): void
  7485. {
  7486. $articles = $this->getTableLocator()->get('Articles');
  7487. $article = new Article();
  7488. $articles->patchEntity($article, ['id' => '3']);
  7489. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7490. $this->Form->create($article);
  7491. $this->Form->setValueSources(['context', 'query']);
  7492. $result = $this->Form->control('id');
  7493. $expected = [
  7494. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7495. ];
  7496. $this->assertHtml($expected, $result);
  7497. $this->Form->setValueSources(['query', 'context']);
  7498. $result = $this->Form->control('id');
  7499. $expected = [
  7500. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7501. ];
  7502. $this->assertHtml($expected, $result);
  7503. $this->Form->setValueSources(['data', 'query', 'context']);
  7504. $result = $this->Form->control('id');
  7505. $expected = [
  7506. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7507. ];
  7508. $this->assertHtml($expected, $result);
  7509. $this->View->setRequest($this->View->getRequest()
  7510. ->withData('id', '8')
  7511. ->withQueryParams(['id' => '9']));
  7512. $this->Form->setValueSources(['data', 'query', 'context']);
  7513. $result = $this->Form->control('id');
  7514. $expected = [
  7515. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7516. ];
  7517. $this->assertHtml($expected, $result);
  7518. }
  7519. /**
  7520. * Test the different form input renderings based on values sources switchings through form options
  7521. */
  7522. public function testFormValueSourcesSwitchViaOptionsRendering(): void
  7523. {
  7524. $articles = $this->getTableLocator()->get('Articles');
  7525. $article = new Article();
  7526. $articles->patchEntity($article, ['id' => '3']);
  7527. $this->View->setRequest(
  7528. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7529. );
  7530. $this->Form->create($article, ['valueSources' => 'query']);
  7531. $result = $this->Form->control('id');
  7532. $expected = [
  7533. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7534. ];
  7535. $this->assertHtml($expected, $result);
  7536. $result = $this->Form->getSourceValue('id');
  7537. $this->assertSame('5', $result);
  7538. $this->Form->setValueSources(['context']);
  7539. $this->Form->create($article, ['valueSources' => 'query']);
  7540. $result = $this->Form->control('id');
  7541. $expected = [
  7542. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7543. ];
  7544. $this->assertHtml($expected, $result);
  7545. $result = $this->Form->getSourceValue('id');
  7546. $this->assertSame('5', $result);
  7547. $this->Form->setValueSources(['query']);
  7548. $this->Form->create($article, ['valueSources' => 'data']);
  7549. $result = $this->Form->control('id');
  7550. $expected = [
  7551. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7552. ];
  7553. $this->assertHtml($expected, $result);
  7554. $result = $this->Form->getSourceValue('id');
  7555. $this->assertSame('4', $result);
  7556. $this->Form->setValueSources(['query']);
  7557. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7558. $result = $this->Form->control('id');
  7559. $expected = [
  7560. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7561. ];
  7562. $this->assertHtml($expected, $result);
  7563. $result = $this->Form->getSourceValue('id');
  7564. $this->assertSame(3, $result);
  7565. }
  7566. /**
  7567. * Test the different form input renderings based on values sources switchings through form options
  7568. */
  7569. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
  7570. {
  7571. $articles = $this->getTableLocator()->get('Articles');
  7572. $article = new Article();
  7573. $articles->patchEntity($article, ['id' => '3']);
  7574. $this->View->setRequest(
  7575. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7576. );
  7577. $this->Form->setValueSources(['context'])
  7578. ->create($article, ['valueSources' => ['query', 'data']]);
  7579. $result = $this->Form->control('id');
  7580. $expected = [
  7581. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7582. ];
  7583. $this->assertHtml($expected, $result);
  7584. $result = $this->Form->getSourceValue('id');
  7585. $this->assertSame('11', $result);
  7586. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7587. $this->Form->setValueSources(['context'])
  7588. ->create($article, ['valueSources' => ['query', 'data']]);
  7589. $result = $this->Form->control('id');
  7590. $expected = [
  7591. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7592. ];
  7593. $this->assertHtml($expected, $result);
  7594. $result = $this->Form->getSourceValue('id');
  7595. $this->assertSame('10', $result);
  7596. }
  7597. /**
  7598. * Test the different form values sources resetting through From::end();
  7599. */
  7600. public function testFormValueSourcesResetViaEnd(): void
  7601. {
  7602. $expected = ['data', 'context'];
  7603. $result = $this->Form->getValueSources();
  7604. $this->assertEquals($expected, $result);
  7605. $expected = ['query', 'context', 'data'];
  7606. $this->Form->setValueSources(['query', 'context', 'data']);
  7607. $result = $this->Form->getValueSources();
  7608. $this->assertEquals($expected, $result);
  7609. $this->Form->create();
  7610. $result = $this->Form->getValueSources();
  7611. $this->assertEquals($expected, $result);
  7612. $this->Form->end();
  7613. $result = $this->Form->getValueSources();
  7614. $this->assertEquals(['data', 'context'], $result);
  7615. }
  7616. /**
  7617. * Test sources values defaults handling
  7618. */
  7619. public function testFormValueSourcesDefaults(): void
  7620. {
  7621. $this->View->setRequest(
  7622. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7623. );
  7624. $this->Form->create();
  7625. $result = $this->Form->password('password');
  7626. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7627. $this->assertHtml($expected, $result);
  7628. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7629. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7630. $this->assertHtml($expected, $result);
  7631. $this->Form->setValueSources('query');
  7632. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7633. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7634. $this->assertHtml($expected, $result);
  7635. $this->Form->setValueSources('data');
  7636. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7637. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7638. $this->assertHtml($expected, $result);
  7639. }
  7640. /**
  7641. * Test sources values schema defaults handling
  7642. */
  7643. public function testSourcesValueDoesntExistPassThrough(): void
  7644. {
  7645. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7646. $articles = $this->getTableLocator()->get('Articles');
  7647. $entity = $articles->newEmptyEntity();
  7648. $this->Form->create($entity);
  7649. $this->Form->setValueSources(['query', 'context']);
  7650. $result = $this->Form->getSourceValue('category');
  7651. $this->assertSame('sesame-cookies', $result);
  7652. $this->Form->setValueSources(['context', 'query']);
  7653. $result = $this->Form->getSourceValue('category');
  7654. $this->assertSame('sesame-cookies', $result);
  7655. }
  7656. /**
  7657. * testNestedLabelInput method
  7658. *
  7659. * Test the `nestedInput` parameter
  7660. */
  7661. public function testNestedLabelInput(): void
  7662. {
  7663. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7664. $expected = [
  7665. 'div' => ['class' => 'input text'],
  7666. 'label' => ['for' => 'foo'],
  7667. ['input' => [
  7668. 'type' => 'text',
  7669. 'name' => 'foo',
  7670. 'id' => 'foo',
  7671. ]],
  7672. 'Foo',
  7673. '/label',
  7674. '/div',
  7675. ];
  7676. $this->assertHtml($expected, $result);
  7677. }
  7678. /**
  7679. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7680. *
  7681. * This test makes sure `false` excludes the label from the render
  7682. */
  7683. public function testControlLabelManipulationDisableLabels(): void
  7684. {
  7685. $result = $this->Form->control('test', [
  7686. 'type' => 'radio',
  7687. 'options' => ['A', 'B'],
  7688. 'labelOptions' => false,
  7689. ]);
  7690. $expected = [
  7691. ['div' => ['class' => 'input radio']],
  7692. '<label',
  7693. 'Test',
  7694. '/label',
  7695. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7696. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7697. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7698. '/div',
  7699. ];
  7700. $this->assertHtml($expected, $result);
  7701. $result = $this->Form->control('checkbox1', [
  7702. 'label' => 'My checkboxes',
  7703. 'multiple' => 'checkbox',
  7704. 'type' => 'select',
  7705. 'options' => [
  7706. ['text' => 'First Checkbox', 'value' => 1],
  7707. ['text' => 'Second Checkbox', 'value' => 2],
  7708. ],
  7709. 'labelOptions' => false,
  7710. ]);
  7711. $expected = [
  7712. ['div' => ['class' => 'input select']],
  7713. ['label' => ['for' => 'checkbox1']],
  7714. 'My checkboxes',
  7715. '/label',
  7716. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7717. ['div' => ['class' => 'checkbox']],
  7718. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7719. '/div',
  7720. ['div' => ['class' => 'checkbox']],
  7721. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7722. '/div',
  7723. '/div',
  7724. ];
  7725. $this->assertHtml($expected, $result);
  7726. }
  7727. /**
  7728. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7729. *
  7730. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7731. * added to the class if checked.
  7732. *
  7733. * Also checks to make sure any custom attributes are rendered correctly
  7734. */
  7735. public function testControlLabelManipulationRadios(): void
  7736. {
  7737. $result = $this->Form->control('test', [
  7738. 'type' => 'radio',
  7739. 'options' => ['A', 'B'],
  7740. 'labelOptions' => ['class' => 'custom-class'],
  7741. ]);
  7742. $expected = [
  7743. ['div' => ['class' => 'input radio']],
  7744. '<label',
  7745. 'Test',
  7746. '/label',
  7747. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7748. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7749. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7750. 'A',
  7751. '/label',
  7752. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7753. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7754. 'B',
  7755. '/label',
  7756. '/div',
  7757. ];
  7758. $this->assertHtml($expected, $result);
  7759. $result = $this->Form->control('test', [
  7760. 'type' => 'radio',
  7761. 'options' => ['A', 'B'],
  7762. 'value' => 1,
  7763. 'labelOptions' => ['class' => 'custom-class'],
  7764. ]);
  7765. $expected = [
  7766. ['div' => ['class' => 'input radio']],
  7767. '<label',
  7768. 'Test',
  7769. '/label',
  7770. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7771. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7772. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7773. 'A',
  7774. '/label',
  7775. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7776. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7777. 'B',
  7778. '/label',
  7779. '/div',
  7780. ];
  7781. $this->assertHtml($expected, $result);
  7782. $result = $this->Form->control('test', [
  7783. 'type' => 'radio',
  7784. 'options' => ['A', 'B'],
  7785. 'value' => 1,
  7786. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7787. ]);
  7788. $expected = [
  7789. ['div' => ['class' => 'input radio']],
  7790. '<label',
  7791. 'Test',
  7792. '/label',
  7793. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7794. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7795. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7796. 'A',
  7797. '/label',
  7798. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7799. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7800. 'B',
  7801. '/label',
  7802. '/div',
  7803. ];
  7804. $this->assertHtml($expected, $result);
  7805. $result = $this->Form->radio('test', ['A', 'B'], [
  7806. 'label' => [
  7807. 'class' => ['custom-class', 'another-class'],
  7808. 'data-name' => 'bob',
  7809. ],
  7810. 'value' => 1,
  7811. ]);
  7812. $expected = [
  7813. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  7814. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7815. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7816. 'A',
  7817. '/label',
  7818. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  7819. ['input' => [
  7820. 'type' => 'radio',
  7821. 'name' => 'test',
  7822. 'value' => '1',
  7823. 'id' => 'test-1',
  7824. 'checked' => 'checked',
  7825. ]],
  7826. 'B',
  7827. '/label',
  7828. ];
  7829. $this->assertHtml($expected, $result);
  7830. }
  7831. /**
  7832. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  7833. *
  7834. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7835. * added to the class if checked.
  7836. *
  7837. * Also checks to make sure any custom attributes are rendered correctly
  7838. */
  7839. public function testControlLabelManipulationCheckboxes(): void
  7840. {
  7841. $result = $this->Form->control('checkbox1', [
  7842. 'label' => 'My checkboxes',
  7843. 'multiple' => 'checkbox',
  7844. 'type' => 'select',
  7845. 'options' => [
  7846. ['text' => 'First Checkbox', 'value' => 1],
  7847. ['text' => 'Second Checkbox', 'value' => 2],
  7848. ],
  7849. 'labelOptions' => ['class' => 'custom-class'],
  7850. 'value' => ['1'],
  7851. ]);
  7852. $expected = [
  7853. ['div' => ['class' => 'input select']],
  7854. ['label' => ['for' => 'checkbox1']],
  7855. 'My checkboxes',
  7856. '/label',
  7857. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7858. ['div' => ['class' => 'checkbox']],
  7859. ['label' => [
  7860. 'class' => 'custom-class selected',
  7861. 'for' => 'checkbox1-1',
  7862. ]],
  7863. ['input' => [
  7864. 'type' => 'checkbox',
  7865. 'name' => 'checkbox1[]',
  7866. 'value' => '1',
  7867. 'id' => 'checkbox1-1',
  7868. 'checked' => 'checked',
  7869. ]],
  7870. 'First Checkbox',
  7871. '/label',
  7872. '/div',
  7873. ['div' => ['class' => 'checkbox']],
  7874. ['label' => [
  7875. 'class' => 'custom-class',
  7876. 'for' => 'checkbox1-2',
  7877. ]],
  7878. ['input' => [
  7879. 'type' => 'checkbox',
  7880. 'name' => 'checkbox1[]',
  7881. 'value' => '2',
  7882. 'id' => 'checkbox1-2',
  7883. ]],
  7884. 'Second Checkbox',
  7885. '/label',
  7886. '/div',
  7887. '/div',
  7888. ];
  7889. $this->assertHtml($expected, $result);
  7890. $result = $this->Form->control('checkbox1', [
  7891. 'label' => 'My checkboxes',
  7892. 'multiple' => 'checkbox',
  7893. 'type' => 'select',
  7894. 'options' => [
  7895. ['text' => 'First Checkbox', 'value' => 1],
  7896. ['text' => 'Second Checkbox', 'value' => 2],
  7897. ],
  7898. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  7899. 'value' => ['1'],
  7900. ]);
  7901. $expected = [
  7902. ['div' => ['class' => 'input select']],
  7903. ['label' => ['for' => 'checkbox1']],
  7904. 'My checkboxes',
  7905. '/label',
  7906. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7907. ['div' => ['class' => 'checkbox']],
  7908. ['label' => [
  7909. 'class' => 'custom-class another-class selected',
  7910. 'data-name' => 'bob',
  7911. 'for' => 'checkbox1-1',
  7912. ]],
  7913. ['input' => [
  7914. 'type' => 'checkbox',
  7915. 'name' => 'checkbox1[]',
  7916. 'value' => '1',
  7917. 'id' => 'checkbox1-1',
  7918. 'checked' => 'checked',
  7919. ]],
  7920. 'First Checkbox',
  7921. '/label',
  7922. '/div',
  7923. ['div' => ['class' => 'checkbox']],
  7924. ['label' => [
  7925. 'class' => 'custom-class another-class',
  7926. 'data-name' => 'bob',
  7927. 'for' => 'checkbox1-2',
  7928. ]],
  7929. ['input' => [
  7930. 'type' => 'checkbox',
  7931. 'name' => 'checkbox1[]',
  7932. 'value' => '2',
  7933. 'id' => 'checkbox1-2',
  7934. ]],
  7935. 'Second Checkbox',
  7936. '/label',
  7937. '/div',
  7938. '/div',
  7939. ];
  7940. $this->assertHtml($expected, $result);
  7941. }
  7942. /**
  7943. * testControlMaxLengthArrayContext method
  7944. *
  7945. * Test control() with maxlength attribute in Array Context.
  7946. */
  7947. public function testControlMaxLengthArrayContext(): void
  7948. {
  7949. $this->article['schema'] = [
  7950. 'title' => ['length' => 10],
  7951. ];
  7952. $this->Form->create($this->article);
  7953. $result = $this->Form->control('title');
  7954. $expected = [
  7955. 'div' => ['class'],
  7956. 'label' => ['for'],
  7957. 'Title',
  7958. '/label',
  7959. 'input' => [
  7960. 'aria-required' => 'true',
  7961. 'id',
  7962. 'name' => 'title',
  7963. 'type' => 'text',
  7964. 'required' => 'required',
  7965. 'maxlength' => 10,
  7966. 'data-validity-message' => 'This field cannot be left empty',
  7967. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7968. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7969. ],
  7970. '/div',
  7971. ];
  7972. $this->assertHtml($expected, $result);
  7973. }
  7974. /**
  7975. * testControlMaxLengthEntityContext method
  7976. *
  7977. * Test control() with maxlength attribute in Entity Context.
  7978. */
  7979. public function testControlMaxLengthEntityContext(): void
  7980. {
  7981. $this->article['schema']['title']['length'] = 45;
  7982. $validator = new Validator();
  7983. $validator->maxLength('title', 10);
  7984. $article = new EntityContext(
  7985. [
  7986. 'entity' => new Entity($this->article),
  7987. 'table' => new Table([
  7988. 'alias' => 'Articles',
  7989. 'schema' => $this->article['schema'],
  7990. 'validator' => $validator,
  7991. ]),
  7992. ]
  7993. );
  7994. $this->Form->create($article);
  7995. $result = $this->Form->control('title');
  7996. $expected = [
  7997. 'div' => ['class'],
  7998. 'label' => ['for'],
  7999. 'Title',
  8000. '/label',
  8001. 'input' => [
  8002. 'aria-required' => 'true',
  8003. 'id',
  8004. 'name' => 'title',
  8005. 'type' => 'text',
  8006. 'required' => 'required',
  8007. 'maxlength' => 10,
  8008. 'data-validity-message' => 'This field cannot be left empty',
  8009. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8010. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8011. ],
  8012. '/div',
  8013. ];
  8014. $this->assertHtml($expected, $result);
  8015. $this->article['schema']['title']['length'] = 45;
  8016. $validator = new Validator();
  8017. $validator->maxLength('title', 55);
  8018. $article = new EntityContext(
  8019. [
  8020. 'entity' => new Entity($this->article),
  8021. 'table' => new Table([
  8022. 'schema' => $this->article['schema'],
  8023. 'validator' => $validator,
  8024. 'alias' => 'Articles',
  8025. ]),
  8026. ]
  8027. );
  8028. $this->Form->create($article);
  8029. $result = $this->Form->control('title');
  8030. $expected = [
  8031. 'div' => ['class'],
  8032. 'label' => ['for'],
  8033. 'Title',
  8034. '/label',
  8035. 'input' => [
  8036. 'aria-required' => 'true',
  8037. 'id',
  8038. 'name' => 'title',
  8039. 'type' => 'text',
  8040. 'required' => 'required',
  8041. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8042. 'data-validity-message' => 'This field cannot be left empty',
  8043. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8044. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8045. ],
  8046. '/div',
  8047. ];
  8048. $this->assertHtml($expected, $result);
  8049. $this->article['schema']['title']['length'] = 45;
  8050. $validator = new Validator();
  8051. $validator->maxLength('title', 55);
  8052. $article = new EntityContext(
  8053. [
  8054. 'entity' => new Entity($this->article),
  8055. 'table' => new Table([
  8056. 'schema' => $this->article['schema'],
  8057. 'validator' => $validator,
  8058. 'alias' => 'Articles',
  8059. ]),
  8060. ]
  8061. );
  8062. $this->Form->create($article);
  8063. $result = $this->Form->control('title', ['maxlength' => 10]);
  8064. $expected = [
  8065. 'div' => ['class'],
  8066. 'label' => ['for'],
  8067. 'Title',
  8068. '/label',
  8069. 'input' => [
  8070. 'aria-required' => 'true',
  8071. 'id',
  8072. 'name' => 'title',
  8073. 'type' => 'text',
  8074. 'required' => 'required',
  8075. 'maxlength' => 10, // Length set in options should take highest precedence.
  8076. 'data-validity-message' => 'This field cannot be left empty',
  8077. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8078. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8079. ],
  8080. '/div',
  8081. ];
  8082. $this->assertHtml($expected, $result);
  8083. }
  8084. /**
  8085. * testControlMinMaxLengthEntityContext method
  8086. *
  8087. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8088. */
  8089. public function testControlMinMaxLengthEntityContext(): void
  8090. {
  8091. $validator = new Validator();
  8092. $validator->maxLength('title', 10);
  8093. $article = new EntityContext(
  8094. [
  8095. 'entity' => new Entity($this->article),
  8096. 'table' => new Table([
  8097. 'alias' => 'Articles',
  8098. 'schema' => $this->article['schema'],
  8099. 'validator' => $validator,
  8100. ]),
  8101. ]
  8102. );
  8103. $this->Form->create($article);
  8104. $result = $this->Form->control('title');
  8105. $expected = [
  8106. 'div' => ['class'],
  8107. 'label' => ['for'],
  8108. 'Title',
  8109. '/label',
  8110. 'input' => [
  8111. 'aria-required' => 'true',
  8112. 'id',
  8113. 'name' => 'title',
  8114. 'type' => 'text',
  8115. 'required' => 'required',
  8116. 'maxlength' => 10,
  8117. 'data-validity-message' => 'This field cannot be left empty',
  8118. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8119. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8120. ],
  8121. '/div',
  8122. ];
  8123. $this->assertHtml($expected, $result);
  8124. }
  8125. /**
  8126. * testControlMaxLengthFormContext method
  8127. *
  8128. * Test control() with maxlength attribute in Form Context.
  8129. */
  8130. public function testControlMaxLengthFormContext(): void
  8131. {
  8132. $validator = new Validator();
  8133. $validator->maxLength('title', 10);
  8134. $form = new Form();
  8135. $form->setValidator('default', $validator);
  8136. $this->Form->create($form);
  8137. $result = $this->Form->control('title');
  8138. $expected = [
  8139. 'div' => ['class'],
  8140. 'label' => ['for'],
  8141. 'Title',
  8142. '/label',
  8143. 'input' => [
  8144. 'aria-required' => 'true',
  8145. 'id',
  8146. 'name' => 'title',
  8147. 'type' => 'text',
  8148. 'required' => 'required',
  8149. 'maxlength' => 10,
  8150. 'data-validity-message' => 'This field cannot be left empty',
  8151. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8152. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8153. ],
  8154. '/div',
  8155. ];
  8156. $this->assertHtml($expected, $result);
  8157. }
  8158. }