| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108 |
- <?php
- declare(strict_types=1);
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 1.2.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\View\Helper;
- use ArrayObject;
- use Cake\Chronos\ChronosTime;
- use Cake\Collection\Collection;
- use Cake\Core\Configure;
- use Cake\Core\Exception\CakeException;
- use Cake\Database\Type\EnumType;
- use Cake\Form\Form;
- use Cake\Http\ServerRequest;
- use Cake\I18n\Date;
- use Cake\I18n\DateTime;
- use Cake\ORM\Entity;
- use Cake\ORM\Table;
- use Cake\Routing\Router;
- use Cake\TestSuite\TestCase;
- use Cake\Utility\Security;
- use Cake\Validation\Validator;
- use Cake\View\Form\EntityContext;
- use Cake\View\Helper\FormHelper;
- use Cake\View\View;
- use Cake\View\Widget\WidgetLocator;
- use InvalidArgumentException;
- use ReflectionProperty;
- use TestApp\Model\Entity\Article;
- use TestApp\Model\Enum\ArticleStatus;
- use TestApp\Model\Enum\ArticleStatusLabel;
- use TestApp\Model\Enum\ArticleStatusLabelInterface;
- use TestApp\Model\Enum\Priority;
- use TestApp\Model\Table\ContactsTable;
- use TestApp\Model\Table\ValidateUsersTable;
- use TestApp\View\Form\StubContext;
- /**
- * FormHelperTest class
- *
- * @property \Cake\View\Helper\FormHelper $Form
- * @property \Cake\View\View $View
- */
- class FormHelperTest extends TestCase
- {
- /**
- * Fixtures to be used
- *
- * @var list<string>
- */
- protected array $fixtures = ['core.Articles', 'core.Comments'];
- /**
- * @var array
- */
- protected $article = [];
- /**
- * @var string
- */
- protected $url;
- /**
- * @var \Cake\View\Helper\FormHelper
- */
- protected $Form;
- /**
- * @var \Cake\View\View
- */
- protected $View;
- /**
- * setUp method
- */
- public function setUp(): void
- {
- parent::setUp();
- Configure::write('Config.language', 'eng');
- Configure::write('App.base', '');
- static::setAppNamespace('Cake\Test\TestCase\View\Helper');
- $request = new ServerRequest([
- 'webroot' => '',
- 'base' => '',
- 'url' => '/articles/add',
- 'params' => [
- 'controller' => 'Articles',
- 'action' => 'add',
- 'plugin' => null,
- ],
- ]);
- $this->View = new View($request);
- Router::reload();
- Router::setRequest($request);
- $this->url = '/articles/add';
- $this->Form = new FormHelper($this->View);
- $this->article = [
- 'schema' => [
- 'id' => ['type' => 'integer'],
- 'author_id' => ['type' => 'integer', 'null' => true],
- 'title' => ['type' => 'string', 'null' => true],
- 'body' => 'text',
- 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
- '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
- ],
- 'required' => [
- 'author_id' => true,
- 'title' => true,
- ],
- ];
- Security::setSalt('foo!');
- $builder = Router::createRouteBuilder('/');
- $builder->connect('/{controller}', ['action' => 'index']);
- $builder->connect('/{controller}/{action}/*');
- }
- /**
- * tearDown method
- */
- public function tearDown(): void
- {
- parent::tearDown();
- unset($this->Form, $this->View);
- }
- /**
- * Test construct() with the templates option.
- */
- public function testConstructTemplatesFile(): void
- {
- $helper = new FormHelper($this->View, [
- 'templates' => 'htmlhelper_tags',
- ]);
- $result = $helper->control('name');
- $this->assertStringContainsString('<input', $result);
- }
- /**
- * Test that when specifying custom widgets the config array for that widget
- * is overwritten instead of merged.
- */
- public function testConstructWithWidgets(): void
- {
- $config = [
- 'widgets' => [
- 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
- ],
- ];
- $helper = new FormHelper($this->View, $config);
- $locator = $helper->getWidgetLocator();
- $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
- }
- /**
- * Test that when specifying custom widgets config file and it should be
- * added to widgets array. WidgetLocator will load widgets in constructor.
- */
- public function testConstructWithWidgetsConfig(): void
- {
- $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
- $locator = $helper->getWidgetLocator();
- $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
- }
- /**
- * Test setting the widget locator
- */
- public function testSetAndGetWidgetLocator(): void
- {
- $helper = new FormHelper($this->View);
- $locator = new WidgetLocator($helper->templater(), $this->View);
- $helper->setWidgetLocator($locator);
- $this->assertSame($locator, $helper->getWidgetLocator());
- }
- /**
- * Test overridding grouped input types which controls generation of "for"
- * attribute of labels.
- */
- public function testConstructWithGroupedInputTypes(): void
- {
- $helper = new FormHelper($this->View, [
- 'groupedInputTypes' => ['radio'],
- ]);
- $result = $helper->control('when', ['type' => 'datetime-local']);
- $this->assertStringContainsString('<label for="when">When</label>', $result);
- }
- /**
- * Test registering a new widget class and rendering it.
- */
- public function testAddWidgetAndRenderWidget(): void
- {
- $data = [
- 'val' => 1,
- ];
- $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
- $this->Form->addWidget('test', $mock);
- $mock->expects($this->once())
- ->method('render')
- ->with($data)
- ->willReturn('HTML');
- $result = $this->Form->widget('test', $data);
- $this->assertSame('HTML', $result);
- }
- /**
- * Test that secureFields() of widget is called after calling render(),
- * not before.
- */
- public function testOrderForRenderingWidgetAndFetchingSecureFields(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
- 'unlockedFields' => [],
- ]));
- $data = [
- 'val' => 1,
- 'name' => 'test',
- ];
- $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
- $this->Form->addWidget('test', $mock);
- $mock->expects($this->once())
- ->method('render')
- ->with($data)
- ->willReturn('HTML');
- $mock->expects($this->once())
- ->method('secureFields')
- ->with($data)
- ->willReturn(['test']);
- $this->Form->create();
- $result = $this->Form->widget('test', $data + ['secure' => true]);
- $this->assertSame('HTML', $result);
- }
- /**
- * Test that empty string is not added to secure fields list when
- * rendering input widget without name.
- */
- public function testRenderingWidgetWithEmptyName(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
- $this->assertSame('<select name=""></select>', $result);
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals([], $result);
- $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
- $this->assertSame('<select name="0"></select>', $result);
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals(['0'], $result);
- }
- /**
- * Test adding a new context class.
- */
- public function testAddContextProvider(): void
- {
- $context = 'My data';
- $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
- $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
- $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
- $this->assertSame($context, $data['entity']);
- return $stub;
- });
- $this->Form->create($context);
- $result = $this->Form->context();
- $this->assertSame($stub, $result);
- }
- /**
- * Test replacing a context class.
- */
- public function testAddContextProviderReplace(): void
- {
- $entity = new Article();
- $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
- $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
- return $stub;
- });
- $this->Form->create($entity);
- $result = $this->Form->context();
- $this->assertSame($stub, $result);
- }
- /**
- * Test overriding a context class.
- */
- public function testAddContextProviderAdd(): void
- {
- $entity = new Article();
- $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
- $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
- if ($data['entity'] instanceof Entity) {
- return $stub;
- }
- });
- $this->Form->create($entity);
- $result = $this->Form->context();
- $this->assertSame($stub, $result);
- }
- /**
- * Provides context options for create().
- *
- * @return array
- */
- public static function contextSelectionProvider(): array
- {
- $entity = new Article();
- $collection = new Collection([$entity]);
- $emptyCollection = new Collection([]);
- $data = [
- 'schema' => [
- 'title' => ['type' => 'string'],
- ],
- ];
- $form = new Form();
- $custom = new StubContext();
- return [
- 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
- 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
- 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
- 'array' => [$data, 'Cake\View\Form\ArrayContext'],
- 'form' => [$form, 'Cake\View\Form\FormContext'],
- 'none' => [null, 'Cake\View\Form\NullContext'],
- 'custom' => [$custom, $custom::class],
- ];
- }
- /**
- * Test default context selection in create()
- *
- * @dataProvider contextSelectionProvider
- * @param mixed $data
- */
- public function testCreateContextSelectionBuiltIn($data, string $class): void
- {
- $this->Form->create($data);
- $this->assertInstanceOf($class, $this->Form->context());
- }
- /**
- * Data provider for type option.
- *
- * @return array
- */
- public static function requestTypeProvider(): array
- {
- return [
- // type, method, override
- ['post', 'post', 'POST'],
- ['put', 'post', 'PUT'],
- ['patch', 'post', 'PATCH'],
- ['delete', 'post', 'DELETE'],
- ];
- }
- /**
- * Test creating file forms.
- */
- public function testCreateFile(): void
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create(null, ['type' => 'file']);
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/articles/add',
- 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test creating GET forms.
- */
- public function testCreateGet(): void
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create(null, ['type' => 'get']);
- $expected = ['form' => [
- 'method' => 'get', 'action' => '/articles/add',
- 'accept-charset' => $encoding,
- ]];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test explicit method/enctype options.
- *
- * Explicit method overwrites inferred method from 'type'
- */
- public function testCreateExplicitMethodEnctype(): void
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create(null, [
- 'type' => 'get',
- 'method' => 'put',
- 'enctype' => 'multipart/form-data',
- ]);
- $expected = ['form' => [
- 'method' => 'put',
- 'action' => '/articles/add',
- 'enctype' => 'multipart/form-data',
- 'accept-charset' => $encoding,
- ]];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test create() with the templates option.
- */
- public function testCreateTemplatesArray(): void
- {
- $result = $this->Form->create($this->article, [
- 'templates' => [
- 'formStart' => '<form class="form-horizontal"{{attrs}}>',
- ],
- ]);
- $expected = [
- 'form' => [
- 'class' => 'form-horizontal',
- 'method' => 'post',
- 'action' => '/articles/add',
- 'accept-charset' => 'utf-8',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test create() with the templates option.
- */
- public function testCreateTemplatesFile(): void
- {
- $result = $this->Form->create($this->article, [
- 'templates' => 'htmlhelper_tags',
- ]);
- $expected = [
- 'start form',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test that create() and end() restore templates.
- */
- public function testCreateEndRestoreTemplates(): void
- {
- $this->Form->create($this->article, [
- 'templates' => ['input' => 'custom input element'],
- ]);
- $this->Form->end();
- $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
- }
- /**
- * Test create() with the templates option.
- */
- public function testCreateTemplatesRequiredClass(): void
- {
- $this->Form->create($this->article, [
- 'templates' => [
- 'requiredClass' => 'is-required',
- ],
- ]);
- $result = $this->Form->control('title');
- $expected = [
- 'div' => ['class' => 'input text is-required'],
- 'label' => ['for' => 'title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'title',
- 'id' => 'title',
- 'required' => 'required',
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- 'oninput' => 'this.setCustomValidity('')',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test using template vars in various templates used by control() method.
- */
- public function testControlTemplateVars(): void
- {
- $result = $this->Form->control('text', [
- 'templates' => [
- 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
- 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
- 'formGroup' => '{{label}}{{forgroup}}{{input}}',
- 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
- ],
- 'templateVars' => [
- 'forinput' => 'in-input',
- 'forlabel' => 'in-label',
- 'forgroup' => 'in-group',
- 'forcontainer' => 'in-container',
- ],
- ]);
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Text in-label',
- '/label',
- 'in-group',
- 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
- 'in-container',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test ensuring template variables work in template files loaded
- * during control().
- */
- public function testControlTemplatesFromFile(): void
- {
- $result = $this->Form->control('title', [
- 'templates' => 'test_templates',
- 'templateVars' => [
- 'forcontainer' => 'container-data',
- ],
- ]);
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Title',
- '/label',
- 'input' => ['name', 'type' => 'text', 'id'],
- 'container-data',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test using template vars in inputSubmit and submitContainer template.
- */
- public function testSubmitTemplateVars(): void
- {
- $this->Form->setTemplates([
- 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
- 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
- ]);
- $result = $this->Form->submit('Submit', [
- 'templateVars' => [
- 'forinput' => 'in-input',
- 'forcontainer' => 'in-container',
- ],
- ]);
- $expected = [
- 'div' => ['class'],
- 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
- 'in-container',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test the create() method
- *
- * @dataProvider requestTypeProvider
- */
- public function testCreateTypeOptions(string $type, string $method, string $override): void
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create(null, ['type' => $type]);
- $expected = [
- 'form' => [
- 'method' => $method, 'action' => '/articles/add',
- 'accept-charset' => $encoding,
- ],
- ];
- $extra = [
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
- '/div',
- ];
- if ($type !== 'post') {
- $expected = array_merge($expected, $extra);
- }
- $this->assertHtml($expected, $result);
- }
- /**
- * Test using template vars in Create (formStart template)
- */
- public function testCreateTemplateVars(): void
- {
- $result = $this->Form->create($this->article, [
- 'templates' => [
- 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
- ],
- 'templateVars' => ['header' => 'headertext'],
- ]);
- $expected = [
- 'h4' => ['class'],
- 'headertext',
- '/h4',
- 'form' => [
- 'method' => 'post',
- 'action' => '/articles/add',
- 'accept-charset' => 'utf-8',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test opening a form for an update operation.
- */
- public function testCreateUpdateForm(): void
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $this->View->setRequest($this->View->getRequest()
- ->withRequestTarget('/articles/edit/1')
- ->withParam('action', 'edit'));
- $this->article['defaults']['id'] = 1;
- $result = $this->Form->create($this->article);
- $expected = [
- 'form' => [
- 'method' => 'post',
- 'action' => '/articles/edit/1',
- 'accept-charset' => $encoding,
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test create() with automatic url generation
- */
- public function testCreateAutoUrl(): void
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $this->View->setRequest($this->View->getRequest()
- ->withRequestTarget('/articles/delete/10')
- ->withParam('action', 'delete'));
- $result = $this->Form->create($this->article);
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/articles/delete/10',
- 'accept-charset' => $encoding,
- ],
- ];
- $this->assertHtml($expected, $result);
- $this->article['defaults'] = ['id' => 1];
- $this->View->setRequest($this->View->getRequest()
- ->withRequestTarget('/Articles/edit/1')
- ->withParam('action', 'delete'));
- $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
- $expected = [
- 'form' => [
- 'method' => 'post',
- 'action' => '/Articles/edit/1',
- 'accept-charset' => $encoding,
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()
- ->withParam('action', 'add'));
- $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
- $expected = [
- 'form' => [
- 'method' => 'post',
- 'action' => '/Articles/publish/1',
- 'accept-charset' => $encoding,
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->create($this->article, ['url' => '/Articles/publish']);
- $expected = [
- 'form' => ['method' => 'post', 'action' => '/Articles/publish', 'accept-charset' => $encoding],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()
- ->withParam('controller', 'Pages'));
- $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/Pages/signup/1',
- 'accept-charset' => $encoding,
- ],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test create() with no URL (no "action" attribute for <form> tag)
- */
- public function testCreateNoUrl(): void
- {
- $result = $this->Form->create(null, ['url' => false]);
- $expected = [
- 'form' => [
- 'method' => 'post',
- 'accept-charset' => strtolower(Configure::read('App.encoding')),
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test create() with a custom route
- */
- public function testCreateCustomRoute(): void
- {
- $builder = Router::createRouteBuilder('/');
- $builder->connect('/login', ['controller' => 'Users', 'action' => 'login']);
- $encoding = strtolower(Configure::read('App.encoding'));
- $this->View->setRequest($this->View->getRequest()
- ->withParam('controller', 'Users'));
- $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/login',
- 'accept-charset' => $encoding,
- ],
- ];
- $this->assertHtml($expected, $result);
- $builder->connect(
- '/new-article',
- ['controller' => 'Articles', 'action' => 'myAction'],
- ['_name' => 'my-route']
- );
- $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/new-article',
- 'accept-charset' => $encoding,
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test automatic accept-charset overriding
- */
- public function testCreateWithAcceptCharset(): void
- {
- $result = $this->Form->create(
- $this->article,
- [
- 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
- ]
- );
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/Articles',
- 'accept-charset' => 'iso-8859-1',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test base form URL when 'url' param is passed with multiple parameters (&)
- */
- public function testCreateQueryStringRequest(): void
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create($this->article, [
- 'type' => 'post',
- 'escape' => false,
- 'url' => [
- 'controller' => 'Controller',
- 'action' => 'action',
- '?' => ['param1' => 'value1', 'param2' => 'value2'],
- ],
- ]);
- $expected = [
- 'form' => [
- 'method' => 'post',
- 'action' => '/Controller/action?param1=value1&param2=value2',
- 'accept-charset' => $encoding,
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->create($this->article, [
- 'type' => 'post',
- 'url' => [
- 'controller' => 'Controller',
- 'action' => 'action',
- '?' => ['param1' => 'value1', 'param2' => 'value2'],
- ],
- ]);
- $this->assertHtml($expected, $result);
- }
- /**
- * test that create() doesn't cause errors by multiple id's being in the primary key
- * as could happen with multiple select or checkboxes.
- */
- public function testCreateWithMultipleIdInData(): void
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
- $result = $this->Form->create($this->article);
- $expected = [
- 'form' => [
- 'method' => 'post',
- 'action' => '/articles/add',
- 'accept-charset' => $encoding,
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test that create() doesn't add in extra passed params.
- */
- public function testCreatePassedArgs(): void
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
- $result = $this->Form->create($this->article, [
- 'type' => 'post',
- 'escape' => false,
- 'url' => [
- 'action' => 'edit',
- 'myparam',
- ],
- ]);
- $expected = [
- 'form' => [
- 'method' => 'post',
- 'action' => '/Articles/edit/myparam',
- 'accept-charset' => $encoding,
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * test creating a get form, and get form inputs.
- */
- public function testGetFormCreate(): void
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create($this->article, ['type' => 'get']);
- $expected = ['form' => [
- 'method' => 'get', 'action' => '/articles/add',
- 'accept-charset' => $encoding,
- ]];
- $this->assertHtml($expected, $result);
- $result = $this->Form->text('title');
- $expected = ['input' => [
- 'name' => 'title', 'type' => 'text', 'required' => 'required',
- ]];
- $this->assertHtml($expected, $result);
- $result = $this->Form->password('password');
- $expected = ['input' => [
- 'name' => 'password', 'type' => 'password',
- ]];
- $this->assertHtml($expected, $result);
- $this->assertDoesNotMatchRegularExpression('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
- $result = $this->Form->text('user_form');
- $expected = ['input' => [
- 'name' => 'user_form', 'type' => 'text',
- ]];
- $this->assertHtml($expected, $result);
- }
- /**
- * test get form, and inputs when the model param is false
- */
- public function testGetFormWithFalseModel(): void
- {
- $encoding = strtolower(Configure::read('App.encoding'));
- $this->View->setRequest($this->View->getRequest()->withParam('controller', 'ContactTest'));
- $result = $this->Form->create(null, [
- 'type' => 'get', 'url' => ['controller' => 'ContactTest'],
- ]);
- $expected = ['form' => [
- 'method' => 'get', 'action' => '/ContactTest/add',
- 'accept-charset' => $encoding,
- ]];
- $this->assertHtml($expected, $result);
- $result = $this->Form->text('reason');
- $expected = [
- 'input' => ['type' => 'text', 'name' => 'reason'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormCreateWithSecurity method
- *
- * Test form->create() with security key.
- */
- public function testCreateWithSecurity(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
- $encoding = strtolower(Configure::read('App.encoding'));
- $result = $this->Form->create($this->article, [
- 'url' => '/articles/publish',
- ]);
- $expected = [
- 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_csrfToken',
- 'value' => 'testKey',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
- $expected['form']['id'] = 'MyForm';
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormCreateGetNoSecurity method
- *
- * Test form->create() with no security key as its a get form
- */
- public function testCreateEndGetNoSecurity(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
- $article = new Article();
- $result = $this->Form->create($article, [
- 'type' => 'get',
- 'url' => '/contacts/add',
- ]);
- $this->assertStringNotContainsString('testKey', $result);
- $result = $this->Form->end();
- $this->assertStringNotContainsString('testKey', $result);
- }
- /**
- * Tests form hash generation with model-less data
- */
- public function testValidateHashNoModel(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $fields = ['anything'];
- $this->Form->create();
- $result = $this->Form->secure($fields);
- $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
- $this->assertStringContainsString($hash, $result);
- }
- /**
- * Tests that hidden fields generated for checkboxes don't get locked
- */
- public function testNoCheckboxLocking(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
- $this->Form->checkbox('check', ['value' => '1']);
- $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
- }
- /**
- * testFormSecurityFields method
- *
- * Test generation of secure form hash generation.
- */
- public function testFormSecurityFields(): void
- {
- $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $result = $this->Form->secure($fields);
- $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
- $hash .= ':' . 'Model.valid';
- $hash = urlencode($hash);
- $tokenDebug = urlencode(json_encode([
- $this->url,
- $fields,
- [],
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => '',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityFields method
- *
- * Test debug token is not generated if debug is false
- */
- public function testFormSecurityFieldsNoDebugMode(): void
- {
- Configure::write('debug', false);
- $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $result = $this->Form->secure($fields);
- $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
- $hash .= ':' . 'Model.valid';
- $hash = urlencode($hash);
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => '',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Tests correct generation of number fields for smallint
- */
- public function testTextFieldGenerationForSmallint(): void
- {
- $this->article['schema'] = [
- 'foo' => [
- 'type' => 'smallinteger',
- 'null' => false,
- 'default' => null,
- 'length' => 10,
- ],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('foo');
- $this->assertStringContainsString('class="input number"', $result);
- $this->assertStringContainsString('type="number"', $result);
- }
- /**
- * Tests correct generation of number fields for tinyint
- */
- public function testTextFieldGenerationForTinyint(): void
- {
- $this->article['schema'] = [
- 'foo' => [
- 'type' => 'tinyinteger',
- 'null' => false,
- 'default' => null,
- 'length' => 10,
- ],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('foo');
- $this->assertStringContainsString('class="input number"', $result);
- $this->assertStringContainsString('type="number"', $result);
- }
- /**
- * Tests correct generation of number fields for double and float fields
- */
- public function testTextFieldGenerationForFloats(): void
- {
- $this->article['schema'] = [
- 'foo' => [
- 'type' => 'float',
- 'null' => false,
- 'default' => null,
- 'length' => 10,
- ],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('foo');
- $expected = [
- 'div' => ['class' => 'input number'],
- 'label' => ['for' => 'foo'],
- 'Foo',
- '/label',
- ['input' => [
- 'type' => 'number',
- 'name' => 'foo',
- 'id' => 'foo',
- 'step' => 'any',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('foo', ['step' => 0.5]);
- $expected = [
- 'div' => ['class' => 'input number'],
- 'label' => ['for' => 'foo'],
- 'Foo',
- '/label',
- ['input' => [
- 'type' => 'number',
- 'name' => 'foo',
- 'id' => 'foo',
- 'step' => '0.5',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Tests correct generation of number fields for integer fields
- */
- public function testTextFieldTypeNumberGenerationForIntegers(): void
- {
- $this->getTableLocator()->get('Contacts', [
- 'className' => ContactsTable::class,
- ]);
- $this->Form->create([], ['context' => ['table' => 'Contacts']]);
- $result = $this->Form->control('age');
- $expected = [
- 'div' => ['class' => 'input number'],
- 'label' => ['for' => 'age'],
- 'Age',
- '/label',
- ['input' => [
- 'type' => 'number', 'name' => 'age',
- 'id' => 'age',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Tests correct generation of file upload fields for binary fields
- */
- public function testFileUploadFieldTypeGenerationForBinaries(): void
- {
- $table = $this->getTableLocator()->get('Contacts', [
- 'className' => ContactsTable::class,
- ]);
- $table->setSchema(['foo' => [
- 'type' => 'binary',
- 'null' => false,
- 'default' => null,
- 'length' => 1024,
- ]]);
- $this->Form->create([], ['context' => ['table' => 'Contacts']]);
- $result = $this->Form->control('foo');
- $expected = [
- 'div' => ['class' => 'input file'],
- 'label' => ['for' => 'foo'],
- 'Foo',
- '/label',
- ['input' => [
- 'type' => 'file', 'name' => 'foo',
- 'id' => 'foo',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityMultipleFields method
- *
- * Test secure() with multiple row form. Ensure hash is correct.
- */
- public function testFormSecurityMultipleFields(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $fields = [
- 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
- 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
- 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
- ];
- $result = $this->Form->secure($fields);
- $sortedFields = [
- 'Model.0.password',
- 'Model.0.username',
- 'Model.1.password',
- 'Model.1.username',
- 'Model.0.hidden' => 'value',
- 'Model.0.valid' => '0',
- 'Model.1.hidden' => 'value',
- 'Model.1.valid' => '0',
- ];
- $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
- $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
- $hash = urlencode($hash);
- $tokenDebug = urlencode(json_encode([
- $this->url,
- $fields,
- [],
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => '',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityMultipleSubmitButtons
- *
- * test form submit generation and ensure that _Token is only created on end()
- */
- public function testFormSecurityMultipleSubmitButtons(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create($this->article);
- $this->Form->text('Address.title');
- $this->Form->text('Address.first_name');
- $result = $this->Form->submit('Save', ['name' => 'save']);
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->end();
- $tokenDebug = urlencode(json_encode([
- '/articles/add',
- [
- 'Address.title',
- 'Address.first_name',
- ],
- ['save', 'cancel'],
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => 'cancel%7Csave',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test that buttons created with foo[bar] name attributes are unlocked correctly.
- */
- public function testSecurityButtonNestedNamed(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
- $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
- $this->assertEquals(['Address.button'], $result);
- }
- /**
- * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
- */
- public function testSecuritySubmitNestedNamed(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create($this->article);
- $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
- $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
- $this->assertEquals(['Address.button'], $result);
- }
- /**
- * Test that the correct fields are unlocked for image submits with no names.
- */
- public function testSecuritySubmitImageNoName(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $result = $this->Form->submit('save.png');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'image', 'src' => 'img/save.png'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
- $this->assertEquals(['x', 'y'], $result);
- }
- /**
- * Test that the correct fields are unlocked for image submits with names.
- */
- public function testSecuritySubmitImageName(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $result = $this->Form->submit('save.png', ['name' => 'test']);
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
- $this->assertEquals(['test', 'test_x', 'test_y'], $result);
- }
- /**
- * testFormSecurityMultipleControlFields method
- *
- * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
- */
- public function testFormSecurityMultipleControlFields(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
- $this->Form->control('Addresses.0.title');
- $this->Form->control('Addresses.0.first_name');
- $this->Form->control('Addresses.0.last_name');
- $this->Form->control('Addresses.0.address');
- $this->Form->control('Addresses.0.city');
- $this->Form->control('Addresses.0.phone');
- $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
- $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
- $this->Form->control('Addresses.1.title');
- $this->Form->control('Addresses.1.first_name');
- $this->Form->control('Addresses.1.last_name');
- $this->Form->control('Addresses.1.address');
- $this->Form->control('Addresses.1.city');
- $this->Form->control('Addresses.1.phone');
- $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
- $result = $this->Form->secure();
- $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
- $tokenDebug = urlencode(json_encode([
- '/articles/add',
- [
- 'Addresses.0.id' => '123456',
- 'Addresses.0.title',
- 'Addresses.0.first_name',
- 'Addresses.0.last_name',
- 'Addresses.0.address',
- 'Addresses.0.city',
- 'Addresses.0.phone',
- 'Addresses.0.primary',
- 'Addresses.1.id' => '654321',
- 'Addresses.1.title',
- 'Addresses.1.first_name',
- 'Addresses.1.last_name',
- 'Addresses.1.address',
- 'Addresses.1.city',
- 'Addresses.1.phone',
- 'Addresses.1.primary',
- ],
- [],
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => '',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityArrayFields method
- *
- * Test form security with Model.field.0 style inputs.
- */
- public function testFormSecurityArrayFields(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $this->Form->text('Address.primary.1');
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertSame('Address.primary', $result[0]);
- $this->Form->text('Address.secondary.1.0');
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertSame('Address.secondary', $result[1]);
- }
- /**
- * testFormSecurityMultipleControlDisabledFields method
- *
- * Test secure form generation with multiple records and disabled fields.
- */
- public function testFormSecurityMultipleControlDisabledFields(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
- 'unlockedFields' => ['first_name', 'address'],
- ]));
- $this->Form->create();
- $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
- $this->Form->text('Addresses.0.title');
- $this->Form->text('Addresses.0.first_name');
- $this->Form->text('Addresses.0.last_name');
- $this->Form->text('Addresses.0.address');
- $this->Form->text('Addresses.0.city');
- $this->Form->text('Addresses.0.phone');
- $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
- $this->Form->text('Addresses.1.title');
- $this->Form->text('Addresses.1.first_name');
- $this->Form->text('Addresses.1.last_name');
- $this->Form->text('Addresses.1.address');
- $this->Form->text('Addresses.1.city');
- $this->Form->text('Addresses.1.phone');
- $result = $this->Form->secure();
- $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
- $tokenDebug = urlencode(json_encode([
- '/articles/add',
- [
- 'Addresses.0.id' => '123456',
- 'Addresses.0.title',
- 'Addresses.0.last_name',
- 'Addresses.0.city',
- 'Addresses.0.phone',
- 'Addresses.1.id' => '654321',
- 'Addresses.1.title',
- 'Addresses.1.last_name',
- 'Addresses.1.city',
- 'Addresses.1.phone',
- ],
- [
- 'first_name',
- 'address',
- ],
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => 'address%7Cfirst_name',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityControlDisabledFields method
- *
- * Test single record form with disabled fields.
- */
- public function testFormSecurityControlUnlockedFields(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
- 'unlockedFields' => ['first_name', 'address'],
- ]));
- $this->Form->create();
- $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
- $this->assertEquals(
- $this->View->getRequest()->getAttribute('formTokenData'),
- ['unlockedFields' => $result]
- );
- $this->Form->hidden('Addresses.id', ['value' => '123456']);
- $this->Form->text('Addresses.title');
- $this->Form->text('Addresses.first_name');
- $this->Form->text('Addresses.last_name');
- $this->Form->text('Addresses.address');
- $this->Form->text('Addresses.city');
- $this->Form->text('Addresses.phone');
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $expected = [
- 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
- 'Addresses.city', 'Addresses.phone',
- ];
- $this->assertEquals($expected, $result);
- $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
- $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
- $tokenDebug = urlencode(json_encode([
- '/articles/add',
- [
- 'Addresses.id' => '123456',
- 'Addresses.title',
- 'Addresses.last_name',
- 'Addresses.city',
- 'Addresses.phone',
- ],
- [
- 'first_name',
- 'address',
- ],
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- 'data-foo' => 'bar',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => 'address%7Cfirst_name',
- 'data-foo' => 'bar',
- ]],
- ['input' => [
- 'type' => 'hidden', 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- 'data-foo' => 'bar',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
- *
- * Test single record form with debugSecurity param.
- */
- public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
- 'unlockedFields' => ['first_name', 'address'],
- ]));
- $this->Form->create();
- $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
- $this->assertEquals(
- $this->View->getRequest()->getAttribute('formTokenData'),
- ['unlockedFields' => $result]
- );
- $this->Form->hidden('Addresses.id', ['value' => '123456']);
- $this->Form->text('Addresses.title');
- $this->Form->text('Addresses.first_name');
- $this->Form->text('Addresses.last_name');
- $this->Form->text('Addresses.address');
- $this->Form->text('Addresses.city');
- $this->Form->text('Addresses.phone');
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $expected = [
- 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
- 'Addresses.city', 'Addresses.phone',
- ];
- $this->assertEquals($expected, $result);
- $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
- $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
- $tokenDebug = urlencode(json_encode([
- '/articles/add',
- [
- 'Addresses.id' => '123456',
- 'Addresses.title',
- 'Addresses.last_name',
- 'Addresses.city',
- 'Addresses.phone',
- ],
- [
- 'first_name',
- 'address',
- ],
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- 'data-foo' => 'bar',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => 'address%7Cfirst_name',
- 'data-foo' => 'bar',
- ]],
- ['input' => [
- 'type' => 'hidden', 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- 'data-foo' => 'bar',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
- *
- * Debug is false, debugSecurity is true -> no debug
- */
- public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
- 'unlockedFields' => ['first_name', 'address'],
- ]));
- $this->Form->create();
- $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
- $this->assertEquals(
- $this->View->getRequest()->getAttribute('formTokenData'),
- ['unlockedFields' => $result]
- );
- $this->Form->hidden('Addresses.id', ['value' => '123456']);
- $this->Form->text('Addresses.title');
- $this->Form->text('Addresses.first_name');
- $this->Form->text('Addresses.last_name');
- $this->Form->text('Addresses.address');
- $this->Form->text('Addresses.city');
- $this->Form->text('Addresses.phone');
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $expected = [
- 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
- 'Addresses.city', 'Addresses.phone',
- ];
- $this->assertEquals($expected, $result);
- Configure::write('debug', false);
- $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
- $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- 'data-foo' => 'bar',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => 'address%7Cfirst_name',
- 'data-foo' => 'bar',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
- *
- * Test single record form with debugSecurity param.
- */
- public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
- 'unlockedFields' => ['first_name', 'address'],
- ]));
- $this->Form->create();
- $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
- $this->assertEquals(
- $this->View->getRequest()->getAttribute('formTokenData'),
- ['unlockedFields' => $result]
- );
- $this->Form->hidden('Addresses.id', ['value' => '123456']);
- $this->Form->text('Addresses.title');
- $this->Form->text('Addresses.first_name');
- $this->Form->text('Addresses.last_name');
- $this->Form->text('Addresses.address');
- $this->Form->text('Addresses.city');
- $this->Form->text('Addresses.phone');
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $expected = [
- 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
- 'Addresses.city', 'Addresses.phone',
- ];
- $this->assertEquals($expected, $result);
- $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
- $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value' => $hash,
- 'data-foo' => 'bar',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => 'address%7Cfirst_name',
- 'data-foo' => 'bar',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormSecureWithCustomNameAttribute method
- *
- * Test securing inputs with custom name attributes.
- */
- public function testFormSecureWithCustomNameAttribute(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertSame('User.custom', $result[0]);
- $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertSame('User.custom.another.value', $result[1]);
- }
- /**
- * testFormSecuredControl method
- *
- * Test generation of entire secure form, assertions made on control() output.
- */
- public function testFormSecuredControl(): void
- {
- $this->View->setRequest($this->View->getRequest()
- ->withAttribute('formTokenData', [])
- ->withAttribute('csrfToken', 'testKey'));
- $this->article['schema'] = [
- 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
- 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
- ];
- $result = $this->Form->create($this->article, ['url' => '/articles/add']);
- $encoding = strtolower(Configure::read('App.encoding'));
- $expected = [
- 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_csrfToken',
- 'value' => 'testKey',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('ratio');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Ratio',
- '/label',
- 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('population');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Population',
- '/label',
- 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('published', ['type' => 'text']);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'published'],
- 'Published',
- '/label',
- ['input' => [
- 'type' => 'text',
- 'name' => 'published',
- 'id' => 'published',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('other', ['type' => 'text']);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'other'],
- 'Other',
- '/label',
- ['input' => [
- 'type' => 'text',
- 'name' => 'other',
- 'id',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->hidden('stuff');
- $expected = [
- 'input' => [
- 'type' => 'hidden',
- 'name' => 'stuff',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->hidden('hidden', ['value' => false]);
- $expected = ['input' => [
- 'type' => 'hidden',
- 'name' => 'hidden',
- 'value' => '0',
- ]];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('something', ['type' => 'checkbox']);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => 'something',
- 'value' => '0',
- ]],
- 'label' => ['for' => 'something'],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'something',
- 'value' => '1',
- 'id' => 'something',
- ]],
- 'Something',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $expectedFields = [
- 'ratio',
- 'population',
- 'published',
- 'other',
- 'stuff' => '',
- 'hidden' => '0',
- 'something',
- ];
- $this->assertEquals($expectedFields, $result);
- $result = $this->Form->secure();
- $tokenDebug = urlencode(json_encode([
- '/articles/add',
- $expectedFields,
- [],
- ]));
- $expected = [
- 'div' => ['style' => 'display:none;'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[fields]',
- 'value',
- ]],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[unlocked]',
- 'value' => '',
- ]],
- ['input' => [
- 'type' => 'hidden', 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $data = [
- 'ratio' => '',
- 'population' => '',
- 'published' => '',
- 'other' => '',
- 'stuff' => '',
- 'hidden' => '0',
- 'something' => '',
- '_Token' => $this->Form->getFormProtector()->buildTokenData(),
- ];
- $this->assertTrue($this->Form->getFormProtector()->validate($data, '', ''));
- }
- /**
- * testSecuredControlCustomName method
- *
- * Test secured inputs with custom names.
- */
- public function testSecuredControlCustomName(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $this->Form->text('text_input', [
- 'name' => 'Option[General.default_role]',
- ]);
- $expected = ['Option.General.default_role'];
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- $this->Form->select('select_box', [1, 2], [
- 'name' => 'Option[General.select_role]',
- ]);
- $expected[] = 'Option.General.select_role';
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- $this->Form->text('other.things[]');
- $expected[] = 'other.things';
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- }
- /**
- * testSecuredControlDuplicate method
- *
- * Test that a hidden field followed by a visible field
- * undoes the hidden field locking.
- */
- public function testSecuredControlDuplicate(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $this->Form->control('text_val', [
- 'type' => 'hidden',
- 'value' => 'some text',
- ]);
- $expected = ['text_val' => 'some text'];
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- $this->Form->control('text_val', [
- 'type' => 'text',
- ]);
- $expected = ['text_val'];
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- }
- /**
- * testFormSecuredFileControl method
- *
- * Tests that the correct keys are added to the field hash index.
- */
- public function testFormSecuredFileControl(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $this->Form->file('Attachment.file');
- $expected = ['Attachment.file'];
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- }
- /**
- * testFormSecuredMultipleSelect method
- *
- * Test that multiple selects keys are added to field hash.
- */
- public function testFormSecuredMultipleSelect(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $options = ['1' => 'one', '2' => 'two'];
- $this->Form->select('Model.select', $options);
- $expected = ['Model.select'];
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- $this->Form->select('Model.select', $options, ['multiple' => true]);
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- }
- /**
- * testFormSecuredRadio method
- */
- public function testFormSecuredRadio(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $options = ['1' => 'option1', '2' => 'option2'];
- $this->Form->radio('Test.test', $options);
- $expected = ['Test.test'];
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- $this->Form->radio('Test.all', $options, [
- 'disabled' => ['option1', 'option2'],
- ]);
- $expected = ['Test.test', 'Test.all' => ''];
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- $this->Form->radio('Test.some', $options, [
- 'disabled' => ['option1'],
- ]);
- $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- }
- /**
- * testFormSecuredAndDisabled method
- *
- * Test that forms with disabled inputs + secured forms leave off the inputs from the form
- * hashing.
- */
- public function testFormSecuredAndDisabled(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
- $this->Form->textarea('Model.textarea', ['disabled' => true]);
- $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
- $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
- $this->Form->year('Model.year', ['disabled' => true]);
- $this->Form->month('Model.month', ['disabled' => true]);
- $this->Form->text('Model.text', ['disabled' => true]);
- $this->Form->password('Model.text', ['disabled' => true]);
- $this->Form->day('Model.day', ['disabled' => true]);
- $this->Form->hour('Model.hour', ['disabled' => true]);
- $this->Form->minute('Model.minute', ['disabled' => true]);
- $this->Form->meridian('Model.meridian', ['disabled' => true]);
- $expected = [
- 'Model.radio' => '',
- ];
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- }
- /**
- * testUnlockFieldAddsToList method
- *
- * Test disableField.
- */
- public function testUnlockFieldAddsToList(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
- 'unlockedFields' => [],
- ]));
- $this->Form->create();
- $this->Form->unlockField('Contact.name');
- $this->Form->text('Contact.name');
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals([], $result);
- $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
- $this->assertEquals(['Contact.name'], $result);
- }
- /**
- * testUnlockFieldRemovingFromFields method
- *
- * Test unlockField removing from fields array.
- */
- public function testUnlockFieldRemovingFromFields(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
- 'unlockedFields' => [],
- ]));
- $this->Form->create($this->article);
- $this->Form->hidden('Article.id', ['value' => 1]);
- $this->Form->text('Article.title');
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertSame('1', $result['Article.id'], 'Hidden input should be secured.');
- $this->assertContains('Article.title', $result, 'Field should be secured.');
- $this->Form->unlockField('Article.title');
- $this->Form->unlockField('Article.id');
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals([], $result);
- }
- /**
- * testResetUnlockFields method
- *
- * Test reset unlockFields, when create new form.
- */
- public function testResetUnlockFields(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
- 'key' => 'testKey',
- 'unlockedFields' => [],
- ]));
- $this->Form->create();
- $this->Form->unlockField('Contact.id');
- $this->Form->hidden('Contact.id', ['value' => 1]);
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEmpty($result, 'Field should be unlocked');
- $this->Form->end();
- $this->Form->create();
- $this->Form->hidden('Contact.id', ['value' => 1]);
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertSame('1', $result['Contact.id'], 'Hidden input should be secured.');
- }
- /**
- * testSecuredFormUrlIgnoresHost method
- *
- * Test that only the path + query elements of a form's URL show up in their hash.
- */
- public function testSecuredFormUrlIgnoresHost(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
- $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
- $this->Form->create($this->article, [
- 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
- ]);
- $result = $this->Form->secure();
- $this->assertStringContainsString($expected, $result);
- $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
- $result = $this->Form->secure();
- $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
- $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
- $result = $this->Form->secure();
- $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
- $this->Form->create($this->article, ['url' => '/articles/view/1']);
- $result = $this->Form->secure();
- $this->assertStringNotContainsString($expected, $result, 'URL is different');
- }
- /**
- * testSecuredFormUrlHasHtmlAndIdentifier method
- *
- * Test that URL, HTML and identifier show up in their hashes.
- */
- public function testSecuredFormUrlHasHtmlAndIdentifier(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
- $this->Form->create($this->article, [
- 'url' => [
- 'controller' => 'articles',
- 'action' => 'view',
- '?' => [
- 'page' => 1,
- 'limit' => 10,
- 'html' => '<>"',
- ],
- '#' => 'result',
- ],
- ]);
- $result = $this->Form->secure();
- $this->assertStringContainsString($expected, $result);
- $this->Form->create($this->article, [
- 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
- ]);
- $result = $this->Form->secure();
- $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
- $this->Form->create($this->article, [
- 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
- ]);
- $result = $this->Form->secure();
- $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
- }
- /**
- * testErrorMessageDisplay method
- *
- * Test error message display.
- */
- public function testErrorMessageDisplay(): void
- {
- $this->article['errors'] = [
- 'Article' => [
- 'title' => 'error message',
- 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
- ],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('Article.title');
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'article-title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'Article[title]',
- 'id' => 'article-title',
- 'class' => 'form-error',
- 'aria-invalid' => 'true',
- 'aria-describedby' => 'article-title-error',
- ],
- ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
- 'error message',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Article.title', [
- 'templates' => [
- 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
- ],
- ]);
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'article-title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'Article[title]',
- 'id' => 'article-title',
- 'class' => 'form-error',
- // No aria-describedby because error template is custom
- 'aria-invalid' => 'true',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Article.content');
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'article-content'],
- 'Content',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'Article[content]',
- 'id' => 'article-content',
- 'class' => 'form-error',
- 'aria-invalid' => 'true',
- 'aria-describedby' => 'article-content-error',
- ],
- ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
- 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'article-content'],
- 'Content',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'Article[content]',
- 'id' => 'article-content',
- 'class' => 'form-error',
- 'aria-invalid' => 'true',
- 'aria-describedby' => 'article-content-error',
- ],
- ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
- 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'article-content'],
- 'Content',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'Article[content]',
- 'id' => 'article-content',
- 'class' => 'form-error',
- 'aria-invalid' => 'true',
- 'aria-describedby' => 'article-content-error',
- ],
- ['div' => ['class' => 'error-message', 'id' => 'article-content-error']],
- 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testEmptyErrorValidation method
- *
- * Test validation errors, when validation message is an empty string.
- */
- public function testEmptyErrorValidation(): void
- {
- $this->article['errors'] = [
- 'Article' => ['title' => ''],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('Article.title');
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'article-title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'Article[title]',
- 'id' => 'article-title',
- 'class' => 'form-error',
- 'aria-invalid' => 'true',
- 'aria-describedby' => 'article-title-error',
- ],
- ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
- [],
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testEmptyControlErrorValidation method
- *
- * Test validation errors, when calling control() overriding validation message by an empty string.
- */
- public function testEmptyControlErrorValidation(): void
- {
- $this->article['errors'] = [
- 'Article' => ['title' => 'error message'],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('Article.title', ['error' => '']);
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'article-title'],
- 'Title',
- '/label',
- 'input' => [
- 'aria-invalid' => 'true',
- 'aria-describedby' => 'article-title-error',
- 'type' => 'text',
- 'name' => 'Article[title]',
- 'id' => 'article-title',
- 'class' => 'form-error',
- ],
- ['div' => ['class' => 'error-message', 'id' => 'article-title-error']],
- [],
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlErrorMessage method
- *
- * Test validation errors, when calling control() overriding validation messages.
- */
- public function testControlErrorMessage(): void
- {
- $this->article['errors'] = [
- 'title' => ['error message'],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('title', [
- 'error' => 'Custom error!',
- ]);
- $expected = [
- 'div' => ['class' => 'input text required error'],
- 'label' => ['for' => 'title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'title',
- 'id' => 'title',
- 'class' => 'form-error',
- 'required' => 'required',
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- 'oninput' => 'this.setCustomValidity('')',
- 'aria-invalid' => 'true',
- 'aria-describedby' => 'title-error',
- ],
- ['div' => ['class' => 'error-message', 'id' => 'title-error']],
- 'Custom error!',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('title', [
- 'error' => ['error message' => 'Custom error!'],
- ]);
- $expected = [
- 'div' => ['class' => 'input text required error'],
- 'label' => ['for' => 'title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'title',
- 'id' => 'title',
- 'aria-invalid' => 'true',
- 'aria-describedby' => 'title-error',
- 'class' => 'form-error',
- 'required' => 'required',
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- 'oninput' => 'this.setCustomValidity('')',
- ],
- ['div' => ['class' => 'error-message', 'id' => 'title-error']],
- 'Custom error!',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormValidationAssociated method
- *
- * Tests displaying errors for nested entities.
- */
- public function testFormValidationAssociated(): void
- {
- $nested = new Entity(['foo' => 'bar']);
- $nested->setError('foo', ['not a valid bar']);
- $entity = new Entity(['nested' => $nested]);
- $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
- $result = $this->Form->error('nested.foo');
- $this->assertSame('<div class="error-message" id="nested-foo-error">not a valid bar</div>', $result);
- }
- /**
- * testFormValidationAssociatedSecondLevel method
- *
- * Test form error display with associated model.
- */
- public function testFormValidationAssociatedSecondLevel(): void
- {
- $inner = new Entity(['bar' => 'baz']);
- $nested = new Entity(['foo' => $inner]);
- $entity = new Entity(['nested' => $nested]);
- $inner->setError('bar', ['not a valid one']);
- $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
- $result = $this->Form->error('nested.foo.bar');
- $this->assertSame('<div class="error-message" id="nested-foo-bar-error">not a valid one</div>', $result);
- }
- /**
- * testFormValidationMultiRecord method
- *
- * Test form error display with multiple records.
- */
- public function testFormValidationMultiRecord(): void
- {
- $one = new Entity();
- $two = new Entity();
- $this->getTableLocator()->get('Contacts', [
- 'className' => ContactsTable::class,
- ]);
- $one->set('email', '');
- $one->setError('email', ['invalid email']);
- $two->set('name', '');
- $two->setError('name', ['This is wrong']);
- $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
- $result = $this->Form->control('0.email');
- $expected = [
- 'div' => ['class' => 'input email error'],
- 'label' => ['for' => '0-email'],
- 'Email',
- '/label',
- 'input' => [
- 'type' => 'email',
- 'name' => '0[email]',
- 'id' => '0-email',
- 'class' => 'form-error',
- 'maxlength' => 255,
- 'value' => '',
- 'aria-invalid' => 'true',
- 'aria-describedby' => '0-email-error',
- ],
- ['div' => ['class' => 'error-message', 'id' => '0-email-error']],
- 'invalid email',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('1.name');
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => '1-name'],
- 'Name',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => '1[name]',
- 'id' => '1-name',
- 'class' => 'form-error',
- 'maxlength' => 255,
- 'value' => '',
- 'aria-invalid' => 'true',
- 'aria-describedby' => '1-name-error',
- ],
- ['div' => ['class' => 'error-message', 'id' => '1-name-error']],
- 'This is wrong',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControl method
- *
- * Test various incarnations of control().
- */
- public function testControl(): void
- {
- $this->getTableLocator()->get('ValidateUsers', [
- 'className' => ValidateUsersTable::class,
- ]);
- $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
- $result = $this->Form->control('ValidateUsers.balance');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Balance',
- '/label',
- 'input' => ['name', 'type' => 'number', 'id', 'step'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('ValidateUser.cost_decimal');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Cost Decimal',
- '/label',
- 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('ValidateUser.null_decimal');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Null Decimal',
- '/label',
- 'input' => ['name', 'type' => 'number', 'id'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlCustomization method
- *
- * Tests the input method and passing custom options.
- */
- public function testControlCustomization(): void
- {
- $this->getTableLocator()->get('Contacts', [
- 'className' => ContactsTable::class,
- ]);
- $this->Form->create([], ['context' => ['table' => 'Contacts']]);
- $result = $this->Form->control('Contact.email', ['id' => 'custom']);
- $expected = [
- 'div' => ['class' => 'input email'],
- 'label' => ['for' => 'custom'],
- 'Email',
- '/label',
- ['input' => [
- 'type' => 'email', 'name' => 'Contact[email]',
- 'id' => 'custom', 'maxlength' => 255,
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Contact.email', [
- 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
- ]);
- $expected = [
- '<div',
- 'label' => ['for' => 'contact-email'],
- 'Email',
- '/label',
- ['input' => [
- 'type' => 'email', 'name' => 'Contact[email]',
- 'id' => 'contact-email', 'maxlength' => 255,
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Contact.email', [
- 'templates' => [
- 'formGroup' => '{{input}}',
- 'inputContainer' => '<div><div>{{label}}</div>{{content}}</div>',
- ],
- ]);
- $expected = [
- '<div',
- '<div',
- 'label' => ['for' => 'contact-email'],
- 'Email',
- '/label',
- '/div',
- ['input' => [
- 'type' => 'email', 'name' => 'Contact[email]',
- 'id' => 'contact-email', 'maxlength' => 255,
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Contact.email', ['type' => 'text']);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'contact-email'],
- 'Email',
- '/label',
- ['input' => [
- 'type' => 'text', 'name' => 'Contact[email]',
- 'id' => 'contact-email', 'maxlength' => '255',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'contact-5-email'],
- 'Email',
- '/label',
- ['input' => [
- 'type' => 'text', 'name' => 'Contact[5][email]',
- 'id' => 'contact-5-email', 'maxlength' => '255',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Contact.password');
- $expected = [
- 'div' => ['class' => 'input password'],
- 'label' => ['for' => 'contact-password'],
- 'Password',
- '/label',
- ['input' => [
- 'type' => 'password', 'name' => 'Contact[password]',
- 'id' => 'contact-password',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Contact.email', [
- 'type' => 'file', 'class' => 'textbox',
- ]);
- $expected = [
- 'div' => ['class' => 'input file'],
- 'label' => ['for' => 'contact-email'],
- 'Email',
- '/label',
- ['input' => [
- 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
- 'id' => 'contact-email',
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $entity = new Entity(['phone' => 'Hello & World > weird chars']);
- $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
- $result = $this->Form->control('phone');
- $expected = [
- 'div' => ['class' => 'input tel'],
- 'label' => ['for' => 'phone'],
- 'Phone',
- '/label',
- ['input' => [
- 'type' => 'tel', 'name' => 'phone',
- 'value' => 'Hello & World > weird chars',
- 'id' => 'phone', 'maxlength' => 255,
- ]],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest(
- $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
- );
- $this->Form->create();
- $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'myId'],
- 'Field',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
- 'value' => 'My value', 'id' => 'myId',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
- $this->Form->create();
- $entity->setError('field', 'Badness!');
- $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
- $result = $this->Form->control('field');
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'field'],
- 'Field',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'field',
- 'id' => 'field',
- 'class' => 'form-error',
- 'aria-invalid' => 'true',
- 'aria-describedby' => 'field-error',
- ],
- ['div' => ['class' => 'error-message', 'id' => 'field-error']],
- 'Badness!',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('field', [
- 'templates' => [
- 'inputContainerError' => '{{content}}{{error}}',
- 'error' => '<span class="error-message">{{content}}</span>',
- ],
- ]);
- $expected = [
- 'label' => ['for' => 'field'],
- 'Field',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'field',
- 'id' => 'field',
- 'class' => 'form-error',
- // No aria-describedby because error template is custom
- 'aria-invalid' => 'true',
- ],
- ['span' => ['class' => 'error-message']],
- 'Badness!',
- '/span',
- ];
- $this->assertHtml($expected, $result);
- $entity->setError('field', ['minLength'], true);
- $result = $this->Form->control('field', [
- 'error' => [
- 'minLength' => 'Le login doit contenir au moins 2 caractères',
- 'maxLength' => 'login too large',
- ],
- ]);
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'field'],
- 'Field',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'field',
- 'id' => 'field',
- 'class' => 'form-error',
- 'aria-invalid' => 'true',
- 'aria-describedby' => 'field-error',
- ],
- ['div' => ['class' => 'error-message', 'id' => 'field-error']],
- 'Le login doit contenir au moins 2 caractères',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $entity->setError('field', ['maxLength'], true);
- $result = $this->Form->control('field', [
- 'error' => [
- 'minLength' => 'Le login doit contenir au moins 2 caractères',
- 'maxLength' => 'login too large',
- ],
- ]);
- $expected = [
- 'div' => ['class' => 'input text error'],
- 'label' => ['for' => 'field'],
- 'Field',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'field',
- 'id' => 'field',
- 'class' => 'form-error',
- 'aria-invalid' => 'true',
- 'aria-describedby' => 'field-error',
- ],
- ['div' => ['class' => 'error-message', 'id' => 'field-error']],
- 'login too large',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlWithTemplateFile method
- *
- * Test that control() accepts a template file.
- */
- public function testControlWithTemplateFile(): void
- {
- $result = $this->Form->control('field', [
- 'templates' => 'htmlhelper_tags',
- ]);
- $expected = [
- 'label' => ['for' => 'field'],
- 'Field',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'field',
- 'id' => 'field',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testNestedControlsEndWithBrackets method
- *
- * Test that nested inputs end with brackets.
- */
- public function testNestedControlsEndWithBrackets(): void
- {
- $result = $this->Form->text('nested.text[]');
- $expected = [
- 'input' => [
- 'type' => 'text', 'name' => 'nested[text][]',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->file('nested.file[]');
- $expected = [
- 'input' => [
- 'type' => 'file', 'name' => 'nested[file][]',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testCreateIdPrefix method
- *
- * Test id prefix.
- */
- public function testCreateIdPrefix(): void
- {
- $this->Form->create(null, ['idPrefix' => 'prefix']);
- $result = $this->Form->control('field');
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'prefix-field'],
- 'Field',
- '/label',
- 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('field', ['id' => 'custom-id']);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'custom-id'],
- 'Field',
- '/label',
- 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio('Model.field', ['option A']);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
- 'label' => ['for' => 'prefix-model-field-0'],
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Model[field]',
- 'value' => '0',
- 'id' => 'prefix-model-field-0',
- ]],
- 'option A',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio('Model.field', ['option A', 'option']);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'prefix-model-field'],
- 'label' => ['for' => 'prefix-model-field-0'],
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Model[field]',
- 'value' => '0',
- 'id' => 'prefix-model-field-0',
- ]],
- 'option A',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'Model.multi_field',
- ['first'],
- ['multiple' => 'checkbox']
- );
- $expected = [
- 'input' => [
- 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'prefix-model-multi-field',
- ],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'prefix-model-multi-field-0']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => '0', 'id' => 'prefix-model-multi-field-0',
- ]],
- 'first',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->end();
- $result = $this->Form->control('field');
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'field'],
- 'Field',
- '/label',
- 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlZero method
- *
- * Test that inputs with 0 can be created.
- */
- public function testControlZero(): void
- {
- $this->getTableLocator()->get('Contacts', [
- 'className' => ContactsTable::class,
- ]);
- $this->Form->create([], ['context' => ['table' => 'Contacts']]);
- $result = $this->Form->control('0');
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => '0'], '/label',
- 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlCheckbox method
- *
- * Test control() with checkbox creation.
- */
- public function testControlCheckbox(): void
- {
- $articles = $this->getTableLocator()->get('Articles');
- $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
- $article = $articles->newEmptyEntity();
- $this->Form->create($article);
- $result = $this->Form->control('Articles.active');
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
- 'label' => ['for' => 'articles-active'],
- ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
- 'Active',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
- ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
- ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
- ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Articles.disabled', [
- 'label' => 'Disabled',
- 'type' => 'checkbox',
- 'data-foo' => 'disabled',
- ]);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
- 'label' => ['for' => 'articles-disabled'],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Articles[disabled]',
- 'value' => '1',
- 'id' => 'articles-disabled',
- 'data-foo' => 'disabled',
- ]],
- 'Disabled',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Articles.confirm', [
- 'label' => 'Confirm <b>me</b>!',
- 'type' => 'checkbox',
- 'escape' => false,
- ]);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
- 'label' => ['for' => 'articles-confirm'],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'Articles[confirm]',
- 'value' => '1',
- 'id' => 'articles-confirm',
- ]],
- 'Confirm <b>me</b>!',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlHidden method
- *
- * Test that control() does not create wrapping div and label tag for hidden fields.
- */
- public function testControlHidden(): void
- {
- $this->getTableLocator()->get('ValidateUsers', [
- 'className' => ValidateUsersTable::class,
- ]);
- $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
- $result = $this->Form->control('ValidateUser.id');
- $expected = [
- 'input' => ['name', 'type' => 'hidden', 'id'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
- $expected = [
- 'input' => ['name', 'type' => 'hidden', 'id'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlDatetime method
- *
- * Test form->control() with datetime.
- */
- public function testControlDatetime(): void
- {
- $result = $this->Form->control('prueba', [
- 'type' => 'datetime',
- 'value' => new DateTime('2019-09-27 02:52:43'),
- ]);
- $expected = [
- 'div' => ['class' => 'input datetime'],
- 'label' => ['for' => 'prueba'],
- 'Prueba',
- '/label',
- 'input' => [
- 'name' => 'prueba',
- 'id' => 'prueba',
- 'type' => 'datetime-local',
- 'value' => '2019-09-27T02:52:43',
- 'step' => '1',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlDatetimeIdPrefix method
- *
- * Test form->control() with datetime with id prefix.
- */
- public function testControlDatetimeIdPrefix(): void
- {
- $this->Form->create(null, ['idPrefix' => 'prefix']);
- $result = $this->Form->control('prueba', [
- 'type' => 'datetime',
- ]);
- $expected = [
- 'div' => ['class' => 'input datetime'],
- 'label' => ['for' => 'prefix-prueba'],
- 'Prueba',
- '/label',
- 'input' => [
- 'name' => 'prueba',
- 'id' => 'prefix-prueba',
- 'type' => 'datetime-local',
- 'value' => '',
- 'step' => '1',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlDatetimeStep method
- *
- * Test form->control() with datetime with custom step size.
- */
- public function testControlDatetimeStep(): void
- {
- $result = $this->Form->control('prueba', [
- 'type' => 'datetime',
- 'value' => new DateTime('2019-09-27 02:52:43'),
- 'step' => '0.5',
- ]);
- $expected = [
- 'div' => ['class' => 'input datetime'],
- 'label' => ['for' => 'prueba'],
- 'Prueba',
- '/label',
- 'input' => [
- 'name' => 'prueba',
- 'id' => 'prueba',
- 'type' => 'datetime-local',
- 'value' => '2019-09-27T02:52:43.000',
- 'step' => '0.5',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlCheckboxWithDisabledElements method
- *
- * Test generating checkboxes with disabled elements.
- */
- public function testControlCheckboxWithDisabledElements(): void
- {
- $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
- $result = $this->Form->control('Contact.multiple', [
- 'multiple' => 'checkbox',
- 'disabled' => 'disabled',
- 'options' => $options,
- ]);
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'contact-multiple']],
- 'Multiple',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '', 'id' => 'contact-multiple']],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'contact-multiple-1']],
- ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
- 'One',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'contact-multiple-2']],
- ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
- 'Two',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'contact-multiple-3']],
- ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
- 'Three',
- '/label',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- // make sure 50 does only disable 50, and not 50f5c0cf
- $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
- $disabled = [50];
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'contact-multiple']],
- 'Multiple',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'contact-multiple']],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'contact-multiple-50']],
- ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
- 'Fifty',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'contact-multiple-50f5c0cf']],
- ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
- 'Stringy',
- '/label',
- '/div',
- '/div',
- ];
- $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlWithLeadingInteger method
- *
- * Test input name with leading integer, ensure attributes are generated correctly.
- */
- public function testControlWithLeadingInteger(): void
- {
- $result = $this->Form->text('0.Node.title');
- $expected = [
- 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlSelectType method
- *
- * Test form->control() with select type inputs.
- */
- public function testControlSelectType(): void
- {
- $result = $this->Form->control(
- 'email',
- [
- 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
- );
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'email'],
- 'Email',
- '/label',
- ['select' => ['name' => 'email', 'id' => 'email']],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => 'è']],
- 'Firést',
- '/option',
- ['option' => ['value' => 'é']],
- 'Secoènd',
- '/option',
- '/select',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control(
- 'email',
- [
- 'options' => ['First', 'Second'], 'empty' => true]
- );
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'email'],
- 'Email',
- '/label',
- ['select' => ['name' => 'email', 'id' => 'email']],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => '0']],
- 'First',
- '/option',
- ['option' => ['value' => '1']],
- 'Second',
- '/option',
- '/select',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('email', [
- 'type' => 'select',
- 'options' => new ArrayObject(['First', 'Second']),
- 'empty' => true,
- ]);
- $this->assertHtml($expected, $result);
- $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
- $this->View->setRequest(
- $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
- );
- $this->Form->create();
- $result = $this->Form->control('Model.user_id', ['empty' => true]);
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'model-user-id'],
- 'User',
- '/label',
- 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
- ['option' => ['value' => '']],
- '/option',
- ['option' => ['value' => 'value', 'selected' => 'selected']],
- 'good',
- '/option',
- ['option' => ['value' => 'other']],
- 'bad',
- '/option',
- '/select',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
- $this->View->setRequest(
- $this->View->getRequest()->withData('Thing', ['user_id' => null])
- );
- $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'thing-user-id'],
- 'User',
- '/label',
- 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
- ['option' => ['value' => '']],
- 'Some Empty',
- '/option',
- ['option' => ['value' => 'value']],
- 'good',
- '/option',
- ['option' => ['value' => 'other']],
- 'bad',
- '/option',
- '/select',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
- $this->View->setRequest(
- $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
- );
- $this->Form->create();
- $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'thing-user-id'],
- 'User',
- '/label',
- 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
- ['option' => ['value' => '']],
- 'Some Empty',
- '/option',
- ['option' => ['value' => 'value', 'selected' => 'selected']],
- 'good',
- '/option',
- ['option' => ['value' => 'other']],
- 'bad',
- '/option',
- '/select',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Publisher.id', [
- 'label' => 'Publisher',
- 'type' => 'select',
- 'multiple' => 'checkbox',
- 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
- ]);
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'publisher-id']],
- 'Publisher',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'publisher-id'],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'publisher-id-value-1']],
- ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
- 'Label 1',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'publisher-id-value-2']],
- ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
- 'Label 2',
- '/label',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $articlesTable = $this->getTableLocator()->get('Articles');
- $articlesTable->getSchema()->setColumnType(
- 'published',
- EnumType::from(ArticleStatus::class)
- );
- $this->Form->create($articlesTable->newEmptyEntity());
- $result = $this->Form->control('published');
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'published'],
- 'Published',
- '/label',
- 'select' => ['name' => 'published', 'id' => 'published'],
- ['option' => ['value' => 'Y']],
- 'Published',
- '/option',
- ['option' => ['value' => 'N', 'selected' => 'selected']],
- 'Unpublished',
- '/option',
- '/select',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $articlesTable->getSchema()->setColumnType(
- 'published',
- EnumType::from(ArticleStatusLabel::class)
- );
- $this->Form->create($articlesTable->newEmptyEntity());
- $result = $this->Form->control('published');
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'published'],
- 'Published',
- '/label',
- 'select' => ['name' => 'published', 'id' => 'published'],
- ['option' => ['value' => 'Y']],
- 'Is Published',
- '/option',
- ['option' => ['value' => 'N', 'selected' => 'selected']],
- 'Is Unpublished',
- '/option',
- '/select',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $articlesTable->getSchema()->setColumnType(
- 'published',
- EnumType::from(ArticleStatusLabelInterface::class)
- );
- $this->Form->create($articlesTable->newEmptyEntity());
- $result = $this->Form->control('published');
- $this->assertHtml($expected, $result);
- $articlePriosTable = $this->getTableLocator()->get('ArticlePrios');
- $articlePriosTable->getSchema()->setColumnType(
- 'priority',
- EnumType::from(Priority::class)
- );
- $this->Form->create($articlePriosTable->newEmptyEntity());
- // Select empty by default
- $result = $this->Form->control('priority', ['empty' => ['' => ' - please select - '], 'default' => '']);
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'priority'],
- 'Priority',
- '/label',
- 'select' => ['name' => 'priority', 'id' => 'priority'],
- ['option' => ['value' => '', 'selected' => 'selected']],
- ' - please select - ',
- '/option',
- ['option' => ['value' => '1']],
- 'Is Low',
- '/option',
- ['option' => ['value' => '2']],
- 'Is Medium',
- '/option',
- ['option' => ['value' => '3']],
- 'Is High',
- '/option',
- '/select',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlWithNonStandardPrimaryKeyMakesHidden method
- *
- * Test that control() and a non standard primary key makes a hidden input by default.
- */
- public function testControlWithNonStandardPrimaryKeyMakesHidden(): void
- {
- $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
- $this->Form->create($this->article);
- $result = $this->Form->control('title');
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
- ];
- $this->assertHtml($expected, $result);
- $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
- $this->Form->create($this->article);
- $result = $this->Form->control('title');
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('body');
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlOverridingMagicSelectType method
- *
- * Test that overriding the magic select type widget is possible.
- */
- public function testControlOverridingMagicSelectType(): void
- {
- $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
- $result = $this->Form->control('Model.user_id', ['type' => 'text']);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'model-user-id'], 'User', '/label',
- 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- //Check that magic types still work for plural/singular vars
- $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
- $result = $this->Form->control('Model.type');
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'model-type'], 'Type', '/label',
- 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
- ['option' => ['value' => 'value']], 'good', '/option',
- ['option' => ['value' => 'other']], 'bad', '/option',
- '/select',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlMagicTypeDoesNotOverride method
- *
- * Test that inferred types do not override developer input.
- */
- public function testControlMagicTypeDoesNotOverride(): void
- {
- $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
- $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- ['input' => [
- 'type' => 'hidden',
- 'name' => 'Model[user]',
- 'value' => 0,
- ]],
- 'label' => ['for' => 'model-user'],
- ['input' => [
- 'name' => 'Model[user]',
- 'type' => 'checkbox',
- 'id' => 'model-user',
- 'value' => 1,
- ]],
- 'User',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- // make sure that for HABTM the multiple option is not being overwritten in case it's truly
- $options = [
- 1 => 'blue',
- 2 => 'red',
- ];
- $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'tags-ids'],
- 'Tags',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => '', 'id' => 'tags-ids'],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'tags-ids-1']],
- ['input' => [
- 'id' => 'tags-ids-1', 'type' => 'checkbox',
- 'value' => '1', 'name' => 'tags[_ids][]',
- ]],
- 'blue',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'tags-ids-2']],
- ['input' => [
- 'id' => 'tags-ids-2', 'type' => 'checkbox',
- 'value' => '2', 'name' => 'tags[_ids][]',
- ]],
- 'red',
- '/label',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlMagicSelectForTypeNumber method
- *
- * Test that magic control() selects are created for type=number.
- */
- public function testControlMagicSelectForTypeNumber(): void
- {
- $this->getTableLocator()->get('ValidateUsers', [
- 'className' => ValidateUsersTable::class,
- ]);
- $entity = new Entity(['balance' => 1]);
- $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
- $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
- $result = $this->Form->control('balance');
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'balance'],
- 'Balance',
- '/label',
- 'select' => ['name' => 'balance', 'id' => 'balance'],
- ['option' => ['value' => '0']],
- 'nothing',
- '/option',
- ['option' => ['value' => '1', 'selected' => 'selected']],
- 'some',
- '/option',
- ['option' => ['value' => '100']],
- 'a lot',
- '/option',
- '/select',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testInvalidControlTypeOption method
- *
- * Test invalid 'input' type option to control() function.
- */
- public function testInvalidControlTypeOption(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid type `input` used for field `text`.');
- $this->Form->control('text', ['type' => 'input']);
- }
- /**
- * testControlMagicSelectChangeToRadio method
- *
- * Test that magic control() selects can easily be converted into radio types without error.
- */
- public function testControlMagicSelectChangeToRadio(): void
- {
- $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
- $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
- $this->assertStringContainsString('input type="radio"', $result);
- }
- /**
- * testFormControlSubmit method
- *
- * Test correct results for form::control() and type submit.
- */
- public function testFormControlSubmit(): void
- {
- $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormControls method
- *
- * Test correct results from Form::controls().
- */
- public function testFormControlsLegendFieldset(): void
- {
- $this->Form->create($this->article);
- $result = $this->Form->allControls([], ['legend' => 'The Legend']);
- $expected = [
- '<fieldset',
- '<legend',
- 'The Legend',
- '/legend',
- '*/fieldset',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
- $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
- $this->assertStringContainsString('<fieldset>', $result);
- $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
- $this->assertStringNotContainsString('<legend>', $result);
- $this->assertStringNotContainsString('<fieldset>', $result);
- $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
- $this->assertStringNotContainsString('<legend>', $result);
- $this->assertStringNotContainsString('<fieldset>', $result);
- $this->Form->create($this->article);
- $this->View->setRequest($this->View->getRequest()
- ->withParam('prefix', 'admin')
- ->withParam('action', 'admin_edit')
- ->withParam('controller', 'articles'));
- $result = $this->Form->allControls();
- $expected = [
- '<fieldset',
- '<legend',
- 'New Article',
- '/legend',
- '*/fieldset',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->create($this->article);
- $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
- $expected = [
- '<fieldset',
- '<legend',
- 'The Legend',
- '/legend',
- '*/fieldset',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->create($this->article);
- $result = $this->Form->allControls([], [
- 'fieldset' => [
- 'class' => 'some-class some-other-class',
- 'disabled' => true,
- 'data-param' => 'a-param',
- ],
- 'legend' => 'The Legend',
- ]);
- $expected = [
- '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
- '<legend',
- 'The Legend',
- '/legend',
- '*/fieldset',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormControls method
- *
- * Test the controls() method.
- */
- public function testFormControls(): void
- {
- $this->Form->create($this->article);
- $result = $this->Form->allControls();
- $expected = [
- '<fieldset',
- '<legend', 'New Article', '/legend',
- 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
- ['div' => ['class' => 'input select required']],
- '*/div',
- ['div' => ['class' => 'input text required']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- '/fieldset',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->allControls([
- 'published' => ['type' => 'boolean'],
- ]);
- $expected = [
- '<fieldset',
- '<legend', 'New Article', '/legend',
- 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
- ['div' => ['class' => 'input select required']],
- '*/div',
- ['div' => ['class' => 'input text required']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- ['div' => ['class' => 'input boolean']],
- '*/div',
- '/fieldset',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->create($this->article);
- $result = $this->Form->allControls([], ['legend' => 'Hello']);
- $expected = [
- 'fieldset' => [],
- 'legend' => [],
- 'Hello',
- '/legend',
- 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
- ['div' => ['class' => 'input select required']],
- '*/div',
- ['div' => ['class' => 'input text required']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- '/fieldset',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->create();
- $expected = [
- 'fieldset' => [],
- ['div' => ['class' => 'input text']],
- 'label' => ['for' => 'foo'],
- 'Foo',
- '/label',
- 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
- '*/div',
- '/fieldset',
- ];
- $result = $this->Form->allControls(
- ['foo' => ['type' => 'text']],
- ['legend' => false]
- );
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormControlsBlacklist method
- */
- public function testFormControlsBlacklist(): void
- {
- $this->Form->create($this->article);
- $result = $this->Form->allControls([
- 'id' => false,
- ]);
- $expected = [
- '<fieldset',
- '<legend', 'New Article', '/legend',
- ['div' => ['class' => 'input select required']],
- '*/div',
- ['div' => ['class' => 'input text required']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- '/fieldset',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->create($this->article);
- $result = $this->Form->allControls([
- 'id' => [],
- ]);
- $expected = [
- '<fieldset',
- '<legend', 'New Article', '/legend',
- 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
- ['div' => ['class' => 'input select required']],
- '*/div',
- ['div' => ['class' => 'input text required']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- ['div' => ['class' => 'input text']],
- '*/div',
- '/fieldset',
- ];
- $this->assertHtml($expected, $result, true);
- }
- /**
- * testSelectAsCheckbox method
- *
- * Test multi-select widget with checkbox formatting.
- */
- public function testSelectAsCheckbox(): void
- {
- $result = $this->Form->select(
- 'Model.multi_field',
- ['first', 'second', 'third'],
- ['multiple' => 'checkbox', 'value' => [0, 1]]
- );
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
- 'second',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-2']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
- 'third',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'Model.multi_field',
- ['1/2' => 'half'],
- ['multiple' => 'checkbox']
- );
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-1-2']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
- 'half',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testLabel method
- *
- * Test label generation.
- */
- public function testLabel(): void
- {
- $result = $this->Form->label('Person.name');
- $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->label('Person.name');
- $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->label('Person.first_name');
- $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->label('Person.first_name', 'Your first name');
- $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
- $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
- $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->label('Person.first_name', '');
- $expected = ['label' => ['for' => 'person-first-name'], '/label'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->label('Person.2.name', '');
- $expected = ['label' => ['for' => 'person-2-name'], '/label'];
- $this->assertHtml($expected, $result);
- }
- /**
- * testLabelContainControl method
- *
- * Test that label() can accept an input with the correct template vars.
- */
- public function testLabelContainControl(): void
- {
- $this->Form->setTemplates([
- 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
- ]);
- $result = $this->Form->label('Person.accept_terms', 'Accept', [
- 'input' => '<input type="checkbox" name="accept_tos"/>',
- ]);
- $expected = [
- 'label' => ['for' => 'person-accept-terms'],
- 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
- 'Accept',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testTextbox method
- *
- * Test textbox element generation.
- */
- public function testTextbox(): void
- {
- $result = $this->Form->text('Model.field');
- $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
- $this->assertHtml($expected, $result);
- $result = $this->Form->text('Model.field', ['type' => 'password']);
- $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
- $this->assertHtml($expected, $result);
- $result = $this->Form->text('Model.field', ['id' => 'theID']);
- $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
- $this->assertHtml($expected, $result);
- }
- /**
- * testTextBoxDataAndError method
- *
- * Test that text() hooks up with request data and error fields.
- */
- public function testTextBoxDataAndError(): void
- {
- $this->article['errors'] = [
- 'Contact' => ['text' => 'wrong'],
- ];
- $this->View->setRequest($this->View->getRequest()
- ->withData('Model.text', 'test <strong>HTML</strong> values')
- ->withData('Contact.text', 'test'));
- $this->Form->create($this->article);
- $result = $this->Form->text('Model.text');
- $expected = [
- 'input' => [
- 'type' => 'text',
- 'name' => 'Model[text]',
- 'value' => 'test <strong>HTML</strong> values',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->text('Contact.text', ['id' => 'theID']);
- $expected = [
- 'input' => [
- 'type' => 'text',
- 'name' => 'Contact[text]',
- 'value' => 'test',
- 'id' => 'theID',
- 'class' => 'form-error',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testDefaultValue method
- *
- * Test default value setting.
- */
- public function testTextDefaultValue(): void
- {
- $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
- $result = $this->Form->text('Model.field', ['default' => 'default value']);
- $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
- $this->Form->create();
- $result = $this->Form->text('Model.field', ['default' => 'default value']);
- $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
- $this->assertHtml($expected, $result);
- $Articles = $this->getTableLocator()->get('Articles');
- $title = $Articles->getSchema()->getColumn('title');
- $Articles->getSchema()->addColumn(
- 'title',
- ['default' => 'default title', 'length' => 255] + $title
- );
- $entity = $Articles->newEmptyEntity();
- $this->Form->create($entity);
- // Get default value from schema
- $result = $this->Form->text('title');
- $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
- $this->assertHtml($expected, $result);
- // Don't get value from schema
- $result = $this->Form->text('title', ['schemaDefault' => false]);
- $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
- $this->assertHtml($expected, $result);
- // Custom default value overrides default value from schema
- $result = $this->Form->text('title', ['default' => 'override default']);
- $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
- $this->assertHtml($expected, $result);
- // Default value from schema is used only for new entities.
- $entity->setNew(false);
- $result = $this->Form->text('title');
- $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
- $this->assertHtml($expected, $result);
- }
- /**
- * testError method
- *
- * Test field error generation.
- */
- public function testError(): void
- {
- $this->article['errors'] = [
- 'Article' => ['field' => 'email'],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->error('Article.field');
- $expected = [
- ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
- 'email',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
- $expected = [
- ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
- '<strong>Badness!</strong>',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
- $expected = [
- ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
- '<strong', 'Badness!', '/strong',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testErrorRuleName method
- *
- * Test error translation can use rule names for translating.
- */
- public function testErrorRuleName(): void
- {
- $this->article['errors'] = [
- 'Article' => [
- 'field' => ['email' => 'Your email was not good'],
- ],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->error('Article.field');
- $expected = [
- ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
- 'Your email was not good',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
- $expected = [
- ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
- 'Email in use',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
- $expected = [
- ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
- 'Email in use',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->error('Article.field', [
- 'email' => 'Key is preferred',
- 'Your email was not good' => 'Email in use',
- ]);
- $expected = [
- ['div' => ['class' => 'error-message', 'id' => 'article-field-error']],
- 'Key is preferred',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testErrorMessages method
- *
- * Test error with nested lists.
- */
- public function testErrorMessages(): void
- {
- $this->article['errors'] = [
- 'Article' => ['field' => 'email'],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->error('Article.field', [
- 'email' => 'No good!',
- ]);
- $expected = [
- 'div' => ['class' => 'error-message', 'id' => 'article-field-error'],
- 'No good!',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testErrorMultipleMessages method
- *
- * Test error() with multiple messages.
- */
- public function testErrorMultipleMessages(): void
- {
- $this->article['errors'] = [
- 'field' => ['notBlank', 'email', 'Something else'],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->error('field', [
- 'notBlank' => 'Cannot be empty',
- 'email' => 'No good!',
- ]);
- $expected = [
- 'div' => ['class' => 'error-message', 'id' => 'field-error'],
- 'ul' => [],
- '<li', 'Cannot be empty', '/li',
- '<li', 'No good!', '/li',
- '<li', 'Something else', '/li',
- '/ul',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPassword method
- *
- * Test password element generation.
- */
- public function testPassword(): void
- {
- $this->article['errors'] = [
- 'Contact' => [
- 'passwd' => 1,
- ],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->password('Contact.field');
- $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
- $this->Form->create($this->article);
- $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
- $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
- $this->assertHtml($expected, $result);
- }
- /**
- * testRadio method
- *
- * Test radio element set generation.
- */
- public function testRadio(): void
- {
- $result = $this->Form->radio('Model.field', ['option A', 'option B']);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
- ['label' => ['for' => 'model-field-0']],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
- 'option A',
- '/label',
- ['label' => ['for' => 'model-field-1']],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
- 'option B',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio('Model.field', new Collection(['option A', 'option B']));
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio(
- 'Employee.vegetarian',
- ['yes' => 'Yes', 'no' => 'No'],
- ['form' => 'my-form', 'id' => 'id-veg']
- );
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Employee[vegetarian]', 'value' => '', 'form' => 'my-form', 'id' => 'id-veg'],
- ['label' => ['for' => 'id-veg-yes']],
- ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'yes', 'id' => 'id-veg-yes', 'form' => 'my-form']],
- 'Yes',
- '/label',
- ['label' => ['for' => 'id-veg-no']],
- ['input' => ['type' => 'radio', 'name' => 'Employee[vegetarian]', 'value' => 'no', 'id' => 'id-veg-no', 'form' => 'my-form']],
- 'No',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'model-field']],
- ['label' => ['for' => 'model-custom-0']],
- ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
- 'option A',
- '/label',
- ['label' => ['for' => 'model-custom-1']],
- ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
- 'option B',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio(
- 'Employee.gender',
- [
- ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
- ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
- ]
- );
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'employee-gender'],
- ['label' => ['for' => 'employee-gender-male']],
- ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
- 'id' => 'employee-gender-male', 'style' => 'width:20px']],
- 'Male',
- '/label',
- ['label' => ['for' => 'employee-gender-female']],
- ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
- 'id' => 'employee-gender-female', 'style' => 'width:20px']],
- 'Female',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- $article = new Article([
- 'status' => ArticleStatus::Unpublished,
- ]);
- $this->Form->create($article);
- $result = $this->Form->radio('status', ['Y' => 'Published', 'N' => 'Unpublished']);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => '', 'id' => 'status'],
- ['label' => ['for' => 'status-y']],
- ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'Y', 'id' => 'status-y']],
- 'Published',
- '/label',
- ['label' => ['for' => 'status-n']],
- ['input' => ['type' => 'radio', 'name' => 'status', 'value' => 'N', 'id' => 'status-n', 'checked' => 'checked']],
- 'Unpublished',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test radio with complex options and empty disabled data.
- */
- public function testRadioComplexDisabled(): void
- {
- $options = [
- ['value' => 'r', 'text' => 'red'],
- ['value' => 'b', 'text' => 'blue'],
- ];
- $attrs = ['disabled' => []];
- $result = $this->Form->radio('Model.field', $options, $attrs);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
- ['label' => ['for' => 'model-field-r']],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
- 'red',
- '/label',
- ['label' => ['for' => 'model-field-b']],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
- 'blue',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- $attrs = ['disabled' => ['r']];
- $result = $this->Form->radio('Model.field', $options, $attrs);
- $this->assertStringContainsString('disabled="disabled"', $result);
- }
- /**
- * testRadioDefaultValue method
- *
- * Test default value setting on radio() method.
- */
- public function testRadioDefaultValue(): void
- {
- $Articles = $this->getTableLocator()->get('Articles');
- $title = $Articles->getSchema()->getColumn('title');
- $Articles->getSchema()->addColumn(
- 'title',
- ['default' => '1'] + $title
- );
- $this->Form->create($Articles->newEmptyEntity());
- $result = $this->Form->radio('title', ['option A', 'option B']);
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
- ['label' => ['for' => 'title-0']],
- ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
- 'option A',
- '/label',
- ['label' => ['for' => 'title-1']],
- ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
- 'option B',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test setting a hiddenField value on radio buttons.
- */
- public function testRadioHiddenFieldValue(): void
- {
- $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N', 'id' => 'title']],
- 'label' => ['for' => 'title-0'],
- ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
- 'option A',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio('title', ['option A'], ['hiddenField' => '']);
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '', 'id' => 'title']],
- 'label' => ['for' => 'title-0'],
- ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
- 'option A',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlRadio method
- *
- * Test that input works with radio types.
- */
- public function testControlRadio(): void
- {
- $result = $this->Form->control('test', [
- 'type' => 'radio',
- 'options' => ['A', 'B'],
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- '<label',
- 'Test',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
- ['label' => ['for' => 'test-0']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- 'A',
- '/label',
- ['label' => ['for' => 'test-1']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
- 'B',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('test', [
- 'type' => 'radio',
- 'options' => ['A', 'B'],
- 'value' => '0',
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- '<label',
- 'Test',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
- ['label' => ['for' => 'test-0']],
- ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- 'A',
- '/label',
- ['label' => ['for' => 'test-1']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
- 'B',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('test', [
- 'type' => 'radio',
- 'options' => ['A', 'B'],
- 'label' => false,
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
- ['label' => ['for' => 'test-0']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- 'A',
- '/label',
- ['label' => ['for' => 'test-1']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
- 'B',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('accept', [
- 'type' => 'radio',
- 'options' => [
- 1 => 'positive',
- -1 => 'negative',
- ],
- 'label' => false,
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => '', 'id' => 'accept']],
- ['label' => ['for' => 'accept-1']],
- ['input' => [
- 'type' => 'radio',
- 'name' => 'accept',
- 'value' => '1',
- 'id' => 'accept-1',
- ]],
- 'positive',
- '/label',
- ['label' => ['for' => 'accept--1']],
- ['input' => [
- 'type' => 'radio',
- 'name' => 'accept',
- 'value' => '-1',
- 'id' => 'accept--1',
- ]],
- 'negative',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $articlesTable = $this->getTableLocator()->get('Articles');
- $articlesTable->getSchema()->setColumnType(
- 'published',
- EnumType::from(ArticleStatus::class)
- );
- $this->Form->create($articlesTable->newEmptyEntity());
- $result = $this->Form->control('published', ['type' => 'radio', 'label' => false,]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- 'input' => ['type' => 'hidden', 'name' => 'published', 'value' => '', 'id' => 'published'],
- ['label' => ['for' => 'published-y']],
- ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'Y', 'id' => 'published-y']],
- 'Published',
- '/label',
- ['label' => ['for' => 'published-n']],
- ['input' => ['type' => 'radio', 'name' => 'published', 'value' => 'N', 'id' => 'published-n', 'checked' => 'checked']],
- 'Unpublished',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testRadioNoLabel method
- *
- * Test that radio() works with label = false.
- */
- public function testRadioNoLabel(): void
- {
- $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testRadioControlInsideLabel method
- *
- * Test generating radio input inside label ala twitter bootstrap.
- */
- public function testRadioControlInsideLabel(): void
- {
- $this->Form->setTemplates([
- 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
- 'radioWrapper' => '{{label}}',
- ]);
- $result = $this->Form->radio('Model.field', ['option A', 'option B']);
- // phpcs:disable
- $expected = [
- ['input' => [
- 'type' => 'hidden',
- 'name' => 'Model[field]',
- 'value' => '',
- 'id' => 'model-field'
- ]],
- ['label' => ['for' => 'model-field-0']],
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Model[field]',
- 'value' => '0',
- 'id' => 'model-field-0'
- ]],
- 'option A',
- '/label',
- ['label' => ['for' => 'model-field-1']],
- ['input' => [
- 'type' => 'radio',
- 'name' => 'Model[field]',
- 'value' => '1',
- 'id' => 'model-field-1'
- ]],
- 'option B',
- '/label',
- ];
- // phpcs:enable
- $this->assertHtml($expected, $result);
- }
- /**
- * testRadioHiddenControlDisabling method
- *
- * Test disabling the hidden input for radio buttons.
- */
- public function testRadioHiddenControlDisabling(): void
- {
- $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
- $expected = [
- 'label' => ['for' => 'model-1-field-0'],
- 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
- 'option A',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testRadioOutOfRange method
- *
- * Test radio element set generation.
- */
- public function testRadioOutOfRange(): void
- {
- $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'model-field'],
- 'label' => ['for' => 'model-field-v'],
- ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
- 'value',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelect method
- *
- * Test select element generation.
- */
- public function testSelect(): void
- {
- $result = $this->Form->select('Model.field', []);
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
- $this->Form->create();
- $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- ['option' => ['value' => 'value', 'selected' => 'selected']],
- 'good',
- '/option',
- ['option' => ['value' => 'other']],
- 'bad',
- '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
- $this->Form->create();
- $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- ['option' => ['value' => 'value']],
- 'good',
- '/option',
- ['option' => ['value' => 'other']],
- 'bad',
- '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $options = [
- ['value' => 'first', 'text' => 'First'],
- ['value' => 'first', 'text' => 'Another First'],
- ];
- $result = $this->Form->select(
- 'Model.field',
- $options,
- ['escape' => false, 'empty' => false]
- );
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- ['option' => ['value' => 'first']],
- 'First',
- '/option',
- ['option' => ['value' => 'first']],
- 'Another First',
- '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
- $this->Form->create();
- $result = $this->Form->select(
- 'Model.contact_id',
- ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
- ['escape' => false, 'empty' => 'pick something']
- );
- $expected = [
- 'select' => ['name' => 'Model[contact_id]'],
- ['option' => ['value' => '']], 'pick something', '/option',
- ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
- ['option' => ['value' => '228-1']], '228-1 value', '/option',
- ['option' => ['value' => '228-2']], '228-2 value', '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
- $this->Form->create();
- $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
- ['option' => ['value' => '1']], 'Yes', '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $article = new Article([
- 'status' => ArticleStatus::Unpublished,
- ]);
- $this->Form->create($article);
- $result = $this->Form->select('status', ['Y' => 'Published', 'N' => 'Unpublished']);
- $expected = [
- 'select' => ['name' => 'status'],
- ['option' => ['value' => 'Y']], 'Published', '/option',
- ['option' => ['value' => 'N', 'selected' => 'selected']], 'Unpublished', '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectEscapeHtml method
- *
- * Test that select() escapes HTML.
- */
- public function testSelectEscapeHtml(): void
- {
- $result = $this->Form->select(
- 'Model.field',
- ['first' => 'first "html" <chars>', 'second' => 'value'],
- ['empty' => false]
- );
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- ['option' => ['value' => 'first']],
- 'first "html" <chars>',
- '/option',
- ['option' => ['value' => 'second']],
- 'value',
- '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'Model.field',
- ['first' => 'first "html" <chars>', 'second' => 'value'],
- ['escape' => false, 'empty' => false]
- );
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- ['option' => ['value' => 'first']],
- 'first "html" <chars>',
- '/option',
- ['option' => ['value' => 'second']],
- 'value',
- '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectRequired method
- *
- * Test select() with required and disabled attributes.
- */
- public function testSelectRequired(): void
- {
- $this->article['required'] = [
- 'user_id' => true,
- ];
- $this->Form->create($this->article);
- $result = $this->Form->select('user_id', ['option A']);
- $expected = [
- 'select' => [
- 'name' => 'user_id',
- 'required' => 'required',
- ],
- ['option' => ['value' => '0']], 'option A', '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
- $expected = [
- 'select' => [
- 'name' => 'user_id',
- 'disabled' => 'disabled',
- ],
- ['option' => ['value' => '0']], 'option A', '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- }
- public function testSelectEmptyWithRequiredFalse(): void
- {
- $Articles = $this->getTableLocator()->get('Articles');
- $validator = $Articles->getValidator('default');
- $validator->allowEmptyString('user_id');
- $Articles->setValidator('default', $validator);
- $entity = $Articles->newEmptyEntity();
- $this->Form->create($entity);
- $result = $this->Form->select('user_id', ['option A']);
- $expected = [
- 'select' => [
- 'name' => 'user_id',
- ],
- ['option' => ['value' => '']], '/option',
- ['option' => ['value' => '0']], 'option A', '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testNestedSelect method
- *
- * Test select element generation with optgroups.
- */
- public function testNestedSelect(): void
- {
- $result = $this->Form->select(
- 'Model.field',
- [1 => 'One', 2 => 'Two', 'Three' => [
- 3 => 'Three', 4 => 'Four', 5 => 'Five',
- ]],
- ['empty' => false]
- );
- $expected = [
- 'select' => ['name' => 'Model[field]'],
- ['option' => ['value' => 1]],
- 'One',
- '/option',
- ['option' => ['value' => 2]],
- 'Two',
- '/option',
- ['optgroup' => ['label' => 'Three']],
- ['option' => ['value' => 3]],
- 'Three',
- '/option',
- ['option' => ['value' => 4]],
- 'Four',
- '/option',
- ['option' => ['value' => 5]],
- 'Five',
- '/option',
- '/optgroup',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectMultiple method
- *
- * Test generation of multiple select elements.
- */
- public function testSelectMultiple(): void
- {
- $options = ['first', 'second', 'third'];
- $result = $this->Form->select(
- 'Model.multi_field',
- $options,
- ['form' => 'my-form', 'multiple' => true]
- );
- $expected = [
- 'input' => [
- 'type' => 'hidden',
- 'name' => 'Model[multi_field]',
- 'value' => '',
- 'form' => 'my-form',
- ],
- 'select' => [
- 'name' => 'Model[multi_field][]',
- 'multiple' => 'multiple',
- 'form' => 'my-form',
- ],
- ['option' => ['value' => '0']],
- 'first',
- '/option',
- ['option' => ['value' => '1']],
- 'second',
- '/option',
- ['option' => ['value' => '2']],
- 'third',
- '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'Model.multi_field',
- $options,
- ['multiple' => 'multiple', 'form' => 'my-form']
- );
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'Model.multi_field',
- $options,
- ['form' => 'my-form', 'multiple' => false]
- );
- $this->assertStringNotContainsString('multiple', $result);
- }
- /**
- * testCheckboxZeroValue method
- *
- * Test that a checkbox can have 0 for the value and 1 for the hidden input.
- */
- public function testCheckboxZeroValue(): void
- {
- $result = $this->Form->control('User.get_spam', [
- 'type' => 'checkbox',
- 'value' => '0',
- 'hiddenField' => '1',
- ]);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'label' => ['for' => 'user-get-spam'],
- ['input' => [
- 'type' => 'hidden', 'name' => 'User[get_spam]',
- 'value' => '1',
- ]],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'User[get_spam]',
- 'value' => '0', 'id' => 'user-get-spam',
- ]],
- 'Get Spam',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('User.get_spam', [
- 'type' => 'checkbox',
- 'value' => '0',
- 'hiddenField' => '',
- ]);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- 'label' => ['for' => 'user-get-spam'],
- ['input' => [
- 'type' => 'hidden', 'name' => 'User[get_spam]',
- 'value' => '',
- ]],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'User[get_spam]',
- 'value' => '0', 'id' => 'user-get-spam',
- ]],
- 'Get Spam',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testHabtmSelectBox method
- *
- * Test generation of habtm select boxes.
- */
- public function testHabtmSelectBox(): void
- {
- $options = [
- 1 => 'blue',
- 2 => 'red',
- 3 => 'green',
- ];
- $tags = [
- new Entity(['id' => 1, 'name' => 'blue']),
- new Entity(['id' => 3, 'name' => 'green']),
- ];
- $article = new Article(['tags' => $tags]);
- $this->Form->create($article);
- $result = $this->Form->control('tags._ids', ['options' => $options]);
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'tags-ids'],
- 'Tags',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
- 'select' => [
- 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
- 'multiple' => 'multiple',
- ],
- ['option' => ['value' => '1', 'selected' => 'selected']],
- 'blue',
- '/option',
- ['option' => ['value' => '2']],
- 'red',
- '/option',
- ['option' => ['value' => '3', 'selected' => 'selected']],
- 'green',
- '/option',
- '/select',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- // make sure only 50 is selected, and not 50f5c0cf
- $options = [
- '1' => 'blue',
- '50f5c0cf' => 'red',
- '50' => 'green',
- ];
- $tags = [
- new Entity(['id' => 1, 'name' => 'blue']),
- new Entity(['id' => 50, 'name' => 'green']),
- ];
- $article = new Article(['tags' => $tags]);
- $this->Form->create($article);
- $result = $this->Form->control('tags._ids', ['options' => $options]);
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'tags-ids'],
- 'Tags',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
- 'select' => [
- 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
- 'multiple' => 'multiple',
- ],
- ['option' => ['value' => '1', 'selected' => 'selected']],
- 'blue',
- '/option',
- ['option' => ['value' => '50f5c0cf']],
- 'red',
- '/option',
- ['option' => ['value' => '50', 'selected' => 'selected']],
- 'green',
- '/option',
- '/select',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $spacecraft = [
- 1 => 'Orion',
- 2 => 'Helios',
- ];
- $this->View->set('spacecraft', $spacecraft);
- $this->Form->create();
- $result = $this->Form->control('spacecraft._ids');
- $expected = [
- 'div' => ['class' => 'input select'],
- 'label' => ['for' => 'spacecraft-ids'],
- 'Spacecraft',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
- 'select' => [
- 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
- 'multiple' => 'multiple',
- ],
- ['option' => ['value' => '1']],
- 'Orion',
- '/option',
- ['option' => ['value' => '2']],
- 'Helios',
- '/option',
- '/select',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testErrorsForBelongsToManySelect method
- *
- * Tests that errors for belongsToMany select fields are being
- * picked up properly.
- */
- public function testErrorsForBelongsToManySelect(): void
- {
- $spacecraft = [
- 1 => 'Orion',
- 2 => 'Helios',
- ];
- $this->View->set('spacecraft', $spacecraft);
- $article = new Article();
- $article->setError('spacecraft', ['Invalid']);
- $this->Form->create($article);
- $result = $this->Form->control('spacecraft._ids');
- $expected = [
- ['div' => ['class' => 'input select error']],
- 'label' => ['for' => 'spacecraft-ids'],
- 'Spacecraft',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
- 'select' => [
- 'name' => 'spacecraft[_ids][]',
- 'id' => 'spacecraft-ids',
- 'multiple' => 'multiple',
- ],
- ['option' => ['value' => '1']],
- 'Orion',
- '/option',
- ['option' => ['value' => '2']],
- 'Helios',
- '/option',
- '/select',
- ['div' => ['class' => 'error-message', 'id' => 'spacecraft-error']],
- 'Invalid',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectMultipleCheckboxes method
- *
- * Test generation of multi select elements in checkbox format.
- */
- public function testSelectMultipleCheckboxes(): void
- {
- $result = $this->Form->select(
- 'Model.multi_field',
- ['first', 'second', 'third'],
- ['multiple' => 'checkbox']
- );
- $expected = [
- 'input' => [
- 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
- ],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-0']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => '0', 'id' => 'model-multi-field-0',
- ]],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-1']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => '1', 'id' => 'model-multi-field-1',
- ]],
- 'second',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-2']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => '2', 'id' => 'model-multi-field-2',
- ]],
- 'third',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'Model.multi_field',
- ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
- ['multiple' => 'checkbox']
- );
- $expected = [
- 'input' => [
- 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
- ],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-a+']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => 'a+', 'id' => 'model-multi-field-a+',
- ]],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-a++']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => 'a++', 'id' => 'model-multi-field-a++',
- ]],
- 'second',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-a+++']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
- ]],
- 'third',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'Model.multi_field',
- ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
- ['multiple' => 'checkbox']
- );
- $expected = [
- 'input' => [
- 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field',
- ],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-a-b']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => 'a>b', 'id' => 'model-multi-field-a-b',
- ]],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-a-b1']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => 'a<b', 'id' => 'model-multi-field-a-b1',
- ]],
- 'second',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-a-b2']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
- 'value' => 'a"b', 'id' => 'model-multi-field-a-b2',
- ]],
- 'third',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectMultipleCheckboxRequestData method
- *
- * Ensure that multiCheckbox reads from the request data.
- */
- public function testSelectMultipleCheckboxRequestData(): void
- {
- $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
- $result = $this->Form->select(
- 'Model.tags',
- ['1' => 'first', 'Array' => 'Array'],
- ['multiple' => 'checkbox']
- );
- $expected = [
- 'input' => [
- 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'model-tags',
- ],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[tags][]',
- 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
- ]],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-tags-array']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[tags][]',
- 'value' => 'Array', 'id' => 'model-tags-array',
- ]],
- 'Array',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectMultipleCheckboxSecurity method
- *
- * Checks the security hash array generated for multiple-input checkbox elements.
- */
- public function testSelectMultipleCheckboxSecurity(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $this->Form->select(
- 'Model.multi_field',
- ['1' => 'first', '2' => 'second', '3' => 'third'],
- ['multiple' => 'checkbox']
- );
- $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals(['Model.multi_field'], $fields);
- $result = $this->Form->secure();
- $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
- $hash = urlencode($hash . ':');
- $this->assertStringContainsString('"' . $hash . '"', $result);
- }
- /**
- * testSelectMultipleSecureWithNoOptions method
- *
- * Multiple select elements should always be secured as they always participate
- * in the POST data.
- */
- public function testSelectMultipleSecureWithNoOptions(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $this->Form->select(
- 'Model.select',
- [],
- ['multiple' => true]
- );
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals(['Model.select'], $result);
- }
- /**
- * testSelectNoSecureWithNoOptions method
- *
- * When a select box has no options it should not be added to the fields list
- * as it always fail post validation.
- */
- public function testSelectNoSecureWithNoOptions(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $this->Form->select(
- 'Model.select',
- []
- );
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals([], $result);
- $this->Form->select(
- 'Model.user_id',
- [],
- ['empty' => true]
- );
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals(['Model.user_id'], $result);
- }
- /**
- * testControlMultipleCheckboxes method
- *
- * Test control() resulting in multi select elements being generated.
- */
- public function testControlMultipleCheckboxes(): void
- {
- $result = $this->Form->control('Model.multi_field', [
- 'options' => ['first', 'second', 'third'],
- 'multiple' => 'checkbox',
- ]);
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'model-multi-field']],
- 'Multi Field',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-0']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-1']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
- 'second',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-2']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
- 'third',
- '/label',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Model.multi_field', [
- 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
- 'multiple' => 'checkbox',
- ]);
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'model-multi-field']],
- 'Multi Field',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'model-multi-field'],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-a']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-b']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
- 'second',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'model-multi-field-c']],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
- 'third',
- '/label',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectHiddenFieldOmission method
- *
- * Test that select() with 'hiddenField' => false omits the hidden field.
- */
- public function testSelectHiddenFieldOmission(): void
- {
- $result = $this->Form->select(
- 'Model.multi_field',
- ['first', 'second'],
- ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
- );
- $this->assertStringNotContainsString('type="hidden"', $result);
- }
- /**
- * testSelectCheckboxMultipleOverrideName method
- *
- * Test that select() with multiple = checkbox works with overriding name attribute.
- */
- public function testSelectCheckboxMultipleOverrideName(): void
- {
- $result = $this->Form->select('category', ['1', '2'], [
- 'multiple' => 'checkbox',
- 'name' => 'fish',
- ]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'fish-0']],
- ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
- '1',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'fish-1']],
- ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
- '2',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->multiCheckbox(
- 'category',
- new Collection(['1', '2']),
- ['name' => 'fish']
- );
- $this->assertHtml($expected, $result);
- $result = $this->Form->multiCheckbox('category', ['1', '2'], [
- 'name' => 'fish',
- ]);
- $this->assertHtml($expected, $result);
- }
- /**
- * testSelectCheckboxMultipleOverrideName method
- *
- * Test that select() with multiple = checkbox works with overriding name attribute.
- */
- public function testSelectCheckboxMultipleCustomId(): void
- {
- $result = $this->Form->select('category', ['1', '2'], [
- 'multiple' => 'checkbox',
- 'id' => 'cat',
- ]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'cat'],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'cat-0']],
- ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'cat-0']],
- '1',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'cat-1']],
- ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'cat-1']],
- '2',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->multiCheckbox(
- 'category',
- ['1', '2'],
- ['id' => 'cat']
- );
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlMultiCheckbox method
- *
- * Test that control() works with multicheckbox.
- */
- public function testControlMultiCheckbox(): void
- {
- $result = $this->Form->control('category', [
- 'type' => 'multicheckbox',
- 'options' => ['1', '2'],
- ]);
- $expected = [
- ['div' => ['class' => 'input multicheckbox']],
- '<label',
- 'Category',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => '', 'id' => 'category'],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'category-0']],
- ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
- '1',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'category-1']],
- ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
- '2',
- '/label',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testCheckbox method
- *
- * Test generation of checkboxes.
- */
- public function testCheckbox(): void
- {
- $result = $this->Form->checkbox('Model.field');
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
- ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->checkbox('Model.field', [
- 'id' => 'theID',
- 'value' => 'myvalue',
- 'form' => 'my-form',
- ]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'Model[field]',
- 'value' => 'myvalue', 'id' => 'theID',
- 'form' => 'my-form',
- ]],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testCheckboxDefaultValue method
- *
- * Test default value setting on checkbox() method.
- */
- public function testCheckboxDefaultValue(): void
- {
- $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
- $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
- $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
- $this->Form->create();
- $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
- $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
- $this->Form->create();
- $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
- $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
- $this->Form->create();
- $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
- $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
- $this->assertHtml($expected, $result);
- $Articles = $this->getTableLocator()->get('Articles');
- $Articles->getSchema()->addColumn(
- 'published',
- ['type' => 'boolean', 'null' => false, 'default' => true]
- );
- $this->Form->create($Articles->newEmptyEntity());
- $result = $this->Form->checkbox('published', ['hiddenField' => false]);
- $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
- $this->assertHtml($expected, $result);
- }
- /**
- * testCheckboxCheckedAndError method
- *
- * Test checkbox being checked or having errors.
- */
- public function testCheckboxCheckedAndError(): void
- {
- $this->article['errors'] = [
- 'published' => true,
- ];
- $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
- $this->Form->create($this->article);
- $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
- $expected = [
- 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'published',
- 'value' => 'myvalue',
- 'id' => 'theID',
- 'checked' => 'checked',
- 'class' => 'form-error',
- ]],
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withData('published', ''));
- $this->Form->create($this->article);
- $result = $this->Form->checkbox('published');
- $expected = [
- 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
- ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testCheckboxCustomNameAttribute method
- *
- * Test checkbox() with a custom name attribute.
- */
- public function testCheckboxCustomNameAttribute(): void
- {
- $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
- ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testCheckboxHiddenField method
- *
- * Test that the hidden input for checkboxes can be omitted or set to a
- * specific value.
- */
- public function testCheckboxHiddenField(): void
- {
- $result = $this->Form->checkbox('UserForm.something', [
- 'hiddenField' => false,
- ]);
- $expected = [
- 'input' => [
- 'type' => 'checkbox',
- 'name' => 'UserForm[something]',
- 'value' => '1',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->checkbox('UserForm.something', [
- 'value' => 'Y',
- 'hiddenField' => '',
- ]);
- $expected = [
- ['input' => [
- 'type' => 'hidden', 'name' => 'UserForm[something]',
- 'value' => '',
- ]],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'UserForm[something]',
- 'value' => 'Y',
- ]],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->checkbox('UserForm.something', [
- 'value' => 'Y',
- 'hiddenField' => 'N',
- ]);
- $expected = [
- ['input' => [
- 'type' => 'hidden', 'name' => 'UserForm[something]',
- 'value' => 'N',
- ]],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'UserForm[something]',
- 'value' => 'Y',
- ]],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testTime method
- *
- * Test the time type.
- */
- public function testTime(): void
- {
- $result = $this->Form->time('start_time', [
- 'value' => '2014-03-08 16:30:00',
- ]);
- $expected = [
- 'input' => [
- 'type' => 'time',
- 'name' => 'start_time',
- 'value' => '16:30:00',
- 'step' => '1',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->time('start_time', [
- 'value' => new ChronosTime('16:30:00'),
- ]);
- $expected = [
- 'input' => [
- 'type' => 'time',
- 'name' => 'start_time',
- 'value' => '16:30:00',
- 'step' => '1',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testDate method
- *
- * Test the date type.
- */
- public function testDate(): void
- {
- $result = $this->Form->date('start_day', [
- 'value' => '2014-03-08',
- ]);
- $expected = [
- 'input' => [
- 'type' => 'date',
- 'name' => 'start_day',
- 'value' => '2014-03-08',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->date('start_day', [
- 'value' => new Date('2014-03-08'),
- ]);
- $this->assertHtml($expected, $result);
- }
- /**
- * testDateTime method
- */
- public function testDateTime(): void
- {
- $result = $this->Form->dateTime('date', ['default' => true]);
- $expected = [
- 'input' => [
- 'type' => 'datetime-local',
- 'name' => 'date',
- 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
- 'step' => '1',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testDateTimeSecured method
- *
- * Test that datetime fields are added to protected fields list.
- */
- public function testDateTimeSecured(): void
- {
- $this->View->setRequest(
- $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
- );
- $this->Form->create();
- $this->Form->dateTime('date');
- $expected = ['date'];
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- $this->Form->date('published');
- $expected = ['date', 'published'];
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- }
- /**
- * testDateTimeSecuredDisabled method
- *
- * Test that datetime fields are added to protected fields list.
- */
- public function testDateTimeSecuredDisabled(): void
- {
- $this->View->setRequest(
- $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
- );
- $this->Form->create();
- $this->Form->dateTime('date', ['secure' => false]);
- $expected = [];
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- $this->Form->date('published', ['secure' => false]);
- $expected = [];
- $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals($expected, $result);
- }
- /**
- * testDatetimeWithDefault method
- *
- * Test that datetime() and default values work.
- */
- public function testDatetimeWithDefault(): void
- {
- $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
- $expected = [
- 'input' => [
- 'type' => 'datetime-local',
- 'name' => 'updated',
- 'value' => '2009-06-01T11:15:30',
- 'step' => '1',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->dateTime('updated', [
- 'default' => '2009-06-01 11:15:30',
- ]);
- $expected = [
- 'input' => [
- 'type' => 'datetime-local',
- 'name' => 'updated',
- 'value' => '2009-06-01T11:15:30',
- 'step' => '1',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testMonth method
- *
- * Test generation of a month input.
- */
- public function testMonth(): void
- {
- $result = $this->Form->month('field', ['value' => '']);
- $expected = [
- 'input' => [
- 'type' => 'month',
- 'name' => 'field',
- 'value' => '',
- ],
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest(
- $this->View->getRequest()->withData('release', '2050-02-10')
- );
- $this->Form->create();
- $result = $this->Form->month('release');
- $expected = [
- 'input' => [
- 'type' => 'month',
- 'name' => 'release',
- 'value' => '2050-02',
- ],
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest(
- $this->View->getRequest()->withData('release', '2050-03')
- );
- $this->Form->create();
- $result = $this->Form->month('release');
- $expected = [
- 'input' => [
- 'type' => 'month',
- 'name' => 'release',
- 'value' => '2050-03',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testYear method
- *
- * Test generation of a year input.
- */
- public function testYear(): void
- {
- $this->View->setRequest(
- $this->View->getRequest()->withData('published', '2006')
- );
- $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
- $expected = [
- ['select' => ['name' => 'field']],
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- ['option' => ['value' => '2007']],
- '2007',
- '/option',
- ['option' => ['value' => '2006']],
- '2006',
- '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->year('field', [
- 'value' => '',
- 'min' => 2006,
- 'max' => 2007,
- 'order' => 'asc',
- ]);
- $expected = [
- ['select' => ['name' => 'field']],
- ['option' => ['selected' => 'selected', 'value' => '']],
- '/option',
- ['option' => ['value' => '2006']],
- '2006',
- '/option',
- ['option' => ['value' => '2007']],
- '2007',
- '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->year('published', [
- 'empty' => false,
- 'min' => 2006,
- 'max' => 2007,
- ]);
- $expected = [
- ['select' => ['name' => 'published']],
- ['option' => ['value' => '2007']],
- '2007',
- '/option',
- ['option' => ['value' => '2006', 'selected' => 'selected']],
- '2006',
- '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->year('published', [
- 'empty' => false,
- 'value' => new Date('2008-01-12'),
- 'min' => 2007,
- 'max' => 2009,
- ]);
- $expected = [
- ['select' => ['name' => 'published']],
- ['option' => ['value' => '2009']],
- '2009',
- '/option',
- ['option' => ['value' => '2008', 'selected' => 'selected']],
- '2008',
- '/option',
- ['option' => ['value' => '2007']],
- '2007',
- '/option',
- '/select',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->year('published', [
- 'empty' => 'Published on',
- ]);
- $this->assertStringContainsString('Published on', $result);
- }
- /**
- * testControlYearPreEpoch method
- *
- * Test minYear being prior to the unix epoch.
- */
- public function testControlYearPreEpoch(): void
- {
- $start = date('Y') - 80;
- $end = date('Y') - 18;
- $result = $this->Form->control('birth_year', [
- 'type' => 'year',
- 'label' => 'Birth Year',
- 'min' => $start,
- 'max' => $end,
- ]);
- $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
- $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
- $this->assertStringNotContainsString('value="00">00', $result);
- }
- /**
- * test control() datetime & required attributes
- */
- public function testControlDatetimeRequired(): void
- {
- $result = $this->Form->control('birthday', [
- 'type' => 'date',
- 'required' => true,
- ]);
- $this->assertStringContainsString(
- '<input type="date" name="birthday" required="required"',
- $result
- );
- }
- /**
- * testYearAutoExpandRange method
- */
- public function testYearAutoExpandRange(): void
- {
- $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
- $result = $this->Form->year('birthday');
- preg_match_all('/<option value="([\d]+)"/', $result, $matches);
- $result = $matches[1];
- $expected = range(date('Y') + 5, 1930);
- $this->assertEquals($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
- $this->Form->create();
- $result = $this->Form->year('release');
- preg_match_all('/<option value="([\d]+)"/', $result, $matches);
- $result = $matches[1];
- $expected = range(2050, date('Y') - 5);
- $this->assertEquals($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
- $this->Form->create();
- $result = $this->Form->year('release', [
- 'min' => 1890,
- 'max' => 1900,
- ]);
- preg_match_all('/<option value="([\d]+)"/', $result, $matches);
- $result = $matches[1];
- $expected = range(1900, 1881);
- $this->assertEquals($expected, $result);
- }
- /**
- * test control placeholder + label
- */
- public function testControlLabelAndPlaceholder(): void
- {
- $this->Form->create($this->article);
- $result = $this->Form->control('title', ['label' => 'Title', 'placeholder' => 'Add title']);
- $expected = [
- 'div' => ['class' => 'input text required'],
- 'label' => ['for' => 'title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'required' => 'required',
- 'placeholder' => 'Add title',
- 'id' => 'title',
- 'name' => 'title',
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- 'oninput' => 'this.setCustomValidity('')',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlLabelFalse method
- *
- * Test the label option being set to false.
- */
- public function testControlLabelFalse(): void
- {
- $this->Form->create($this->article);
- $result = $this->Form->control('title', ['label' => false]);
- $expected = [
- 'div' => ['class' => 'input text required'],
- 'input' => [
- 'type' => 'text',
- 'required' => 'required',
- 'id' => 'title',
- 'name' => 'title',
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- 'oninput' => 'this.setCustomValidity('')',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->create($this->article);
- $result = $this->Form->control('title', ['label' => false, 'placeholder' => 'Add title']);
- $expected['input'] += [
- 'placeholder' => 'Add title',
- 'aria-label' => 'Add title',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testTextArea method
- *
- * Test generation of a textarea input.
- */
- public function testTextArea(): void
- {
- $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
- $result = $this->Form->textarea('field');
- $expected = [
- 'textarea' => ['name' => 'field', 'rows' => 5],
- 'some test data',
- '/textarea',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->textarea('user.bio');
- $expected = [
- 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
- '/textarea',
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()
- ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
- $this->Form->create();
- $result = $this->Form->textarea('field');
- $expected = [
- 'textarea' => ['name' => 'field', 'rows' => 5],
- htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
- '/textarea',
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withData(
- 'Model.field',
- 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
- ));
- $this->Form->create();
- $result = $this->Form->textarea('Model.field', ['escape' => false]);
- $expected = [
- 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
- 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
- '/textarea',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->textarea('0.OtherModel.field');
- $expected = [
- 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
- '/textarea',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testTextAreaWithStupidCharacters method
- *
- * Test text area with non-ascii characters.
- */
- public function testTextAreaWithStupidCharacters(): void
- {
- $result = $this->Form->textarea('Post.content', [
- 'value' => 'GREAT®',
- 'rows' => '15',
- 'cols' => '75',
- ]);
- $expected = [
- 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
- 'GREAT®',
- '/textarea',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testTextAreaMaxLength method
- *
- * Test textareas maxlength read from schema.
- */
- public function testTextAreaMaxLength(): void
- {
- $this->Form->create([
- 'schema' => [
- 'stuff' => ['type' => 'string', 'length' => 10],
- ],
- ]);
- $result = $this->Form->control('other', ['type' => 'textarea']);
- $expected = [
- 'div' => ['class' => 'input textarea'],
- 'label' => ['for' => 'other'],
- 'Other',
- '/label',
- 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
- '/textarea',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('stuff', ['type' => 'textarea']);
- $expected = [
- 'div' => ['class' => 'input textarea'],
- 'label' => ['for' => 'stuff'],
- 'Stuff',
- '/label',
- 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
- '/textarea',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testHiddenField method
- *
- * Test generation of a hidden input.
- */
- public function testHidden(): void
- {
- $this->article['errors'] = [
- 'field' => true,
- ];
- $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
- $this->Form->create($this->article);
- $result = $this->Form->hidden('field', ['id' => 'theID']);
- $expected = [
- 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
- $this->assertHtml($expected, $result);
- $result = $this->Form->hidden('field', ['value' => 'my value']);
- $expected = [
- 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
- ];
- $this->assertHtml($expected, $result);
- $article = new Article([
- 'status' => ArticleStatus::Unpublished,
- ]);
- $this->Form->create($article);
- $result = $this->Form->hidden('status');
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'status', 'value' => 'N'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test hidden() with various boolean values.
- */
- public function testHiddenBooleanValues(): void
- {
- $this->Form->create($this->article);
- $result = $this->Form->hidden('field', ['value' => null]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'field'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->hidden('field', ['value' => true]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->hidden('field', ['value' => false]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFileUploadField method
- *
- * Test generation of a file upload input.
- */
- public function testFileUploadField(): void
- {
- $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
- $result = $this->Form->file('Model.upload');
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
- 'name' => '', 'type' => '', 'tmp_name' => '',
- 'error' => 4, 'size' => 0,
- ]));
- $result = $this->Form->file('Model.upload');
- $this->assertHtml($expected, $result);
- $this->View->setRequest(
- $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
- );
- $result = $this->Form->file('Model.upload');
- $this->assertHtml($expected, $result);
- }
- /**
- * testFileUploadOnOtherModel method
- *
- * Test File upload input on a model not used in create().
- */
- public function testFileUploadOnOtherModel(): void
- {
- $this->Form->create($this->article, ['type' => 'file']);
- $result = $this->Form->file('ValidateProfile.city');
- $expected = [
- 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testButton method
- *
- * Test generation of a form button.
- */
- public function testButton(): void
- {
- $result = $this->Form->button('Hi');
- $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
- $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
- $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
- $expected = ['button' => ['type' => 'reset'], '<Clear Form>', '/button'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->button('No type', ['type' => false]);
- $expected = ['button' => [], 'No type', '/button'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->button('Upload Text', [
- 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
- 'escape' => false,
- ]);
- $this->assertDoesNotMatchRegularExpression('/\&039/', $result);
- }
- /**
- * testButtonUnlockedByDefault method
- *
- * Test that button() makes unlocked fields by default.
- */
- public function testButtonUnlockedByDefault(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $this->Form->button('Save', ['name' => 'save']);
- $this->Form->button('Clear');
- $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
- $this->assertEquals(['save'], $result);
- }
- /**
- * Test generation of a form button with confirm message.
- */
- public function testButtonWithConfirm(): void
- {
- $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
- $expected = ['button' => [
- 'type' => 'submit',
- 'data-confirm-message' => 'Confirm me!',
- 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
- ], 'Hi', '/button'];
- $this->assertHtml($expected, $result);
- $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
- $expected = ['button' => [
- 'type' => 'submit',
- 'data-confirm-message' => 'Confirm "me"!',
- 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
- ], 'Hi', '/button'];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostButton method
- */
- public function testPostButton(): void
- {
- $result = $this->Form->postButton('Hi', '/controller/action');
- $expected = [
- 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
- 'button' => ['type' => 'submit'],
- 'Hi',
- '/button',
- '/form',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
- $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
- }
- /**
- * testPostButtonMethodType method
- */
- public function testPostButtonMethodType(): void
- {
- $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
- $expected = [
- 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
- 'div' => ['style' => 'display:none;'],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
- '/div',
- 'button' => ['type' => 'submit'],
- 'Hi',
- '/button',
- '/form',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostButtonFormOptions method
- */
- public function testPostButtonFormOptions(): void
- {
- $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
- $expected = [
- 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
- 'button' => ['type' => 'submit'],
- 'Hi',
- '/button',
- '/form',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostButtonNestedData method
- *
- * Test using postButton with N dimensional data.
- */
- public function testPostButtonNestedData(): void
- {
- $data = [
- 'one' => [
- 'two' => [
- 3, 4, 5,
- ],
- ],
- ];
- $result = $this->Form->postButton('Send', '/', ['data' => $data]);
- $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
- $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
- $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
- }
- /**
- * testSecurePostButton method
- *
- * Test that postButton adds _Token fields.
- */
- public function testSecurePostButton(): void
- {
- $this->View->setRequest($this->View->getRequest()
- ->withAttribute('csrfToken', 'testkey')
- ->withAttribute('formTokenData', ['unlockedFields' => []]));
- $result = $this->Form->postButton('Delete', '/posts/delete/1');
- $tokenDebug = urlencode(json_encode([
- '/posts/delete/1',
- [],
- [],
- ]));
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
- ],
- ['div' => ['style' => 'display:none;']],
- ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
- '/div',
- 'button' => ['type' => 'submit'],
- 'Delete',
- '/button',
- ['div' => ['style' => 'display:none;']],
- ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
- ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- ]],
- '/div',
- '/form',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostLink method
- */
- public function testPostLink(): void
- {
- $result = $this->Form->postLink('Delete', '/posts/delete/1');
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
- '/form',
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->postLink(
- 'Delete',
- '/posts/delete/1',
- ['target' => '_blank', 'class' => 'btn btn-danger']
- );
- $expected = [
- 'form' => [
- 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostLinkWithConfirm method
- *
- * Test the confirm option for postLink().
- */
- public function testPostLinkWithConfirm(): void
- {
- $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- 'a' => [
- 'href' => '#',
- 'data-confirm-message' => 'Confirm?',
- 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
- ],
- 'Delete',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->postLink(
- 'Delete',
- '/posts/delete/1',
- ['confirm' => "'Confirm'\nthis \"deletion\"?"]
- );
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- 'a' => [
- 'href' => '#',
- 'data-confirm-message' => "'Confirm'\nthis "deletion"?",
- 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
- ],
- 'Delete',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
- $result = $this->Form->postLink(
- 'Delete',
- '/posts/delete/1',
- ['escape' => false, 'confirm' => 'Confirm this deletion?']
- );
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- 'a' => [
- 'href' => '#',
- 'data-confirm-message' => 'Confirm this deletion?',
- 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
- ],
- 'Delete',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostLinkWithQuery method
- *
- * Test postLink() with query string args.
- */
- public function testPostLinkWithQuery(): void
- {
- $result = $this->Form->postLink(
- 'Delete',
- ['controller' => 'Posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
- );
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/Posts/delete/1?a=b&c=d',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostLinkWithData method
- *
- * Test postLink with additional data.
- */
- public function testPostLinkWithData(): void
- {
- $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
- $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
- $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
- $this->Form->create($entity);
- $this->Form->end();
- $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
- $this->assertStringContainsString(
- '<input type="hidden" name="name" value="show"',
- $result,
- 'should not contain entity data.'
- );
- }
- /**
- * testPostLinkSecurityHash method
- *
- * Test that security hashes for postLink include the url.
- */
- public function testPostLinkSecurityHash(): void
- {
- $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
- $hash .= '%3Aid';
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
- $result = $this->Form->postLink(
- 'Delete',
- '/posts/delete/1',
- ['data' => ['id' => 1]]
- );
- $tokenDebug = urlencode(json_encode([
- '/posts/delete/1',
- [
- 'id' => 1,
- ],
- [],
- ]));
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name', 'style' => 'display:none;',
- ],
- ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
- ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
- 'div' => ['style' => 'display:none;'],
- ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
- ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
- ['input' => [
- 'type' => 'hidden',
- 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- ]],
- '/div',
- '/form',
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostLinkSecurityHashBlockMode method
- *
- * Test that postLink doesn't modify the fields in the containing form.
- *
- * postLink() calls inside open forms should not modify the field list
- * for the form.
- */
- public function testPostLinkSecurityHashBlockMode(): void
- {
- $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
- $hash .= '%3A';
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
- $this->Form->create(null, ['url' => ['action' => 'add']]);
- $this->Form->control('title');
- $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
- $result = $this->View->fetch('postLink');
- $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
- $this->assertEquals(['title'], $fields);
- $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
- $reflect = new ReflectionProperty($this->Form, '_lastAction');
- $this->assertSame('/Articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
- }
- /**
- * testPostLinkSecurityHashNoDebugMode method
- *
- * Test that security does not include debug token if debug is false.
- */
- public function testPostLinkSecurityHashNoDebugMode(): void
- {
- Configure::write('debug', false);
- $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
- $hash .= '%3Aid';
- $this->View->setRequest($this->View->getRequest()
- ->withAttribute('formTokenData', ['key' => 'test']));
- $result = $this->Form->postLink(
- 'Delete',
- '/posts/delete/1',
- ['data' => ['id' => 1]]
- );
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name', 'style' => 'display:none;',
- ],
- ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
- ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
- 'div' => ['style' => 'display:none;'],
- ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
- ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
- '/div',
- '/form',
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostLinkNestedData method
- *
- * Test using postLink with N dimensional data.
- */
- public function testPostLinkNestedData(): void
- {
- $data = [
- 'one' => [
- 'two' => [
- 3, 4, 5,
- ],
- ],
- ];
- $result = $this->Form->postLink('Send', '/', ['data' => $data]);
- $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
- $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
- $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
- }
- /**
- * testPostLinkAfterGetForm method
- *
- * Test creating postLinks after a GET form.
- */
- public function testPostLinkAfterGetForm(): void
- {
- $this->View->setRequest($this->View->getRequest()
- ->withAttribute('csrfToken', 'testkey')
- ->withAttribute('formTokenData', []));
- $this->Form->create($this->article, ['type' => 'get']);
- $this->Form->end();
- $result = $this->Form->postLink('Delete', '/posts/delete/1');
- $tokenDebug = urlencode(json_encode([
- '/posts/delete/1',
- [],
- [],
- ]));
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
- ],
- ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
- ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
- 'div' => ['style' => 'display:none;'],
- ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
- ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
- ['input' => [
- 'type' => 'hidden', 'name' => '_Token[debug]',
- 'value' => $tokenDebug,
- ]],
- '/div',
- '/form',
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testPostLinkFormBuffer method
- *
- * Test that postLink adds form tags to view block.
- */
- public function testPostLinkFormBuffer(): void
- {
- $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
- $expected = [
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->View->fetch('postLink');
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->postLink(
- 'Delete',
- '/posts/delete/2',
- ['block' => true, 'method' => 'DELETE']
- );
- $expected = [
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->View->fetch('postLink');
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/2',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
- ],
- ],
- ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
- '/form',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
- $expected = [
- 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
- 'Delete',
- '/a',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->View->fetch('foobar');
- $expected = [
- 'form' => [
- 'method' => 'post', 'action' => '/posts/delete/1',
- 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
- ],
- 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
- '/form',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSubmitButton method
- */
- public function testSubmitButton(): void
- {
- $result = $this->Form->submit('');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'value' => ''],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('Test Submit');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('Next >');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'value' => 'Next >'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('Next >', ['escape' => false]);
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'value' => 'Next >'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('Reset!', ['type' => 'reset']);
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'reset', 'value' => 'Reset!'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSubmitImage method
- *
- * Test image submit types.
- */
- public function testSubmitImage(): void
- {
- $result = $this->Form->submit('http://example.com/cake.power.gif');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('/relative/cake.power.gif');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('cake.power.gif');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->submit('Not.an.image');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testSubmitUnlockedByDefault method
- *
- * Submit buttons should be unlocked by default as there could be multiples, and only one will
- * be submitted at a time.
- */
- public function testSubmitUnlockedByDefault(): void
- {
- $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
- $this->Form->create();
- $this->Form->submit('Go go');
- $this->Form->submit('Save', ['name' => 'save']);
- $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
- $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
- }
- /**
- * testSubmitImageTimestamp method
- *
- * Test submit image with timestamps.
- */
- public function testSubmitImageTimestamp(): void
- {
- Configure::write('Asset.timestamp', 'force');
- $result = $this->Form->submit('cake.power.gif');
- $expected = [
- 'div' => ['class' => 'submit'],
- 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testDateTimeWithGetForms method
- *
- * Test that datetime() works with GET style forms.
- */
- public function testDateTimeWithGetForms(): void
- {
- $this->Form->create($this->article, ['type' => 'get']);
- $result = $this->Form->datetime('created');
- $expected = [
- 'input' => [
- 'type' => 'datetime-local',
- 'name' => 'created',
- 'value' => '',
- 'step' => '1',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->datetime('created', ['default' => true]);
- $expected = [
- 'input' => [
- 'type' => 'datetime-local',
- 'name' => 'created',
- 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
- 'step' => '1',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Provides fractional schema types
- *
- * @return array
- */
- public static function fractionalTypeProvider(): array
- {
- return [
- ['datetimefractional'],
- ['timestampfractional'],
- ['timestamptimezone'],
- ];
- }
- /**
- * testDateTimeWithFractional method
- *
- * Test that datetime() works with datetimefractional.
- *
- * @dataProvider fractionalTypeProvider
- */
- public function testDateTimeWithFractional(string $type): void
- {
- $this->Form->create([
- 'schema' => [
- 'created' => ['type' => $type],
- ],
- ]);
- $result = $this->Form->datetime('created', [
- 'val' => new DateTime('2019-09-27 02:52:43.123'),
- ]);
- $expected = [
- 'input' => [
- 'type' => 'datetime-local',
- 'name' => 'created',
- 'value' => '2019-09-27T02:52:43.123',
- 'step' => '0.001',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlWithFractional method
- *
- * Test that control() works with datetimefractional.
- *
- * @dataProvider fractionalTypeProvider
- */
- public function testControlWithFractional(string $type): void
- {
- $this->Form->create([
- 'schema' => [
- 'created' => ['type' => $type],
- ],
- ]);
- $result = $this->Form->control('created', [
- 'val' => new DateTime('2019-09-27 02:52:43.123'),
- ]);
- $expected = [
- 'div' => ['class' => 'input datetime'],
- 'label' => ['for' => 'created'],
- 'Created',
- '/label',
- 'input' => [
- 'type' => 'datetime-local',
- 'name' => 'created',
- 'id' => 'created',
- 'value' => '2019-09-27T02:52:43.123',
- 'step' => '0.001',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testForMagicControlNonExistentNotValidated method
- */
- public function testForMagicControlNonExistentNotValidated(): void
- {
- $this->Form->create($this->article);
- $this->Form->setTemplates(['inputContainer' => '{{content}}']);
- $result = $this->Form->control('nonexistent_not_validated');
- $expected = [
- 'label' => ['for' => 'nonexistent-not-validated'],
- 'Nonexistent Not Validated',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'nonexistent_not_validated',
- 'id' => 'nonexistent-not-validated',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('nonexistent_not_validated', [
- 'val' => 'my value',
- ]);
- $expected = [
- 'label' => ['for' => 'nonexistent-not-validated'],
- 'Nonexistent Not Validated',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'nonexistent_not_validated',
- 'value' => 'my value', 'id' => 'nonexistent-not-validated',
- ],
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest(
- $this->View->getRequest()->withData('nonexistent_not_validated', 'CakePHP magic')
- );
- $this->Form->create($this->article);
- $result = $this->Form->control('nonexistent_not_validated');
- $expected = [
- 'label' => ['for' => 'nonexistent-not-validated'],
- 'Nonexistent Not Validated',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'nonexistent_not_validated',
- 'value' => 'CakePHP magic', 'id' => 'nonexistent-not-validated',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormMagicControlLabel method
- */
- public function testFormMagicControlLabel(): void
- {
- $this->getTableLocator()->get('Contacts', [
- 'className' => ContactsTable::class,
- ]);
- $this->Form->create([], ['context' => ['table' => 'Contacts']]);
- $this->Form->setTemplates(['inputContainer' => '{{content}}']);
- $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
- $expected = [
- 'label' => ['for' => 'contacts-name'],
- 'My label',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'Contacts[name]',
- 'id' => 'contacts-name',
- 'maxlength' => '255',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('name', [
- 'label' => ['class' => 'mandatory'],
- ]);
- $expected = [
- 'label' => ['for' => 'name', 'class' => 'mandatory'],
- 'Name',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'name',
- 'id' => 'name', 'maxlength' => '255',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('name', [
- 'div' => false,
- 'label' => ['class' => 'mandatory', 'text' => 'My label'],
- ]);
- $expected = [
- 'label' => ['for' => 'name', 'class' => 'mandatory'],
- 'My label',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'name',
- 'id' => 'name', 'maxlength' => '255',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('Contact.name', [
- 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
- ]);
- $expected = [
- 'label' => ['for' => 'my_id'],
- 'Name',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => 'Contact[name]',
- 'id' => 'my_id', 'maxlength' => '255',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('1.id');
- $expected = ['input' => [
- 'type' => 'hidden', 'name' => '1[id]',
- 'id' => '1-id',
- ]];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('1.name');
- $expected = [
- 'label' => ['for' => '1-name'],
- 'Name',
- '/label',
- 'input' => [
- 'type' => 'text', 'name' => '1[name]',
- 'id' => '1-name', 'maxlength' => '255',
- ],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testFormEnd method
- */
- public function testFormEnd(): void
- {
- $this->assertSame('</form>', $this->Form->end());
- }
- /**
- * testMultiRecordForm method
- *
- * Test the generation of fields for a multi record form.
- */
- public function testMultiRecordForm(): void
- {
- $articles = $this->getTableLocator()->get('Articles');
- $articles->hasMany('Comments');
- $comment = new Entity(['comment' => 'Value']);
- $article = new Article(['comments' => [$comment]]);
- $this->Form->create([$article]);
- $result = $this->Form->control('0.comments.1.comment');
- // phpcs:disable
- $expected = [
- 'div' => ['class' => 'input textarea'],
- 'label' => ['for' => '0-comments-1-comment'],
- 'Comment',
- '/label',
- 'textarea' => [
- 'name',
- 'id' => '0-comments-1-comment',
- 'rows' => 5
- ],
- '/textarea',
- '/div'
- ];
- // phpcs:enable
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('0.comments.0.comment');
- // phpcs:disable
- $expected = [
- 'div' => ['class' => 'input textarea'],
- 'label' => ['for' => '0-comments-0-comment'],
- 'Comment',
- '/label',
- 'textarea' => [
- 'name',
- 'id' => '0-comments-0-comment',
- 'rows' => 5
- ],
- 'Value',
- '/textarea',
- '/div'
- ];
- // phpcs:enable
- $this->assertHtml($expected, $result);
- $comment->setError('comment', ['Not valid']);
- $result = $this->Form->control('0.comments.0.comment');
- // phpcs:disable
- $expected = [
- 'div' => ['class' => 'input textarea error'],
- 'label' => ['for' => '0-comments-0-comment'],
- 'Comment',
- '/label',
- 'textarea' => [
- 'name',
- 'class' => 'form-error',
- 'id' => '0-comments-0-comment',
- 'aria-invalid' => 'true',
- 'aria-describedby' => '0-comments-0-comment-error',
- 'rows' => 5
- ],
- 'Value',
- '/textarea',
- ['div' => ['class' => 'error-message', 'id' => '0-comments-0-comment-error']],
- 'Not valid',
- '/div',
- '/div'
- ];
- // phpcs:enable
- $this->assertHtml($expected, $result);
- $this->getTableLocator()->get('Comments')
- ->getValidator('default')
- ->allowEmptyString('comment', null, false);
- $result = $this->Form->control('0.comments.1.comment');
- // phpcs:disable
- $expected = [
- 'div' => ['class' => 'input textarea required'],
- 'label' => ['for' => '0-comments-1-comment'],
- 'Comment',
- '/label',
- 'textarea' => [
- 'name',
- 'required' => 'required',
- 'id' => '0-comments-1-comment',
- 'rows' => 5,
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- 'oninput' => 'this.setCustomValidity('')',
- ],
- '/textarea',
- '/div'
- ];
- // phpcs:enable
- $this->assertHtml($expected, $result);
- }
- /**
- * testHtml5Controls method
- *
- * Test that some html5 inputs + FormHelper::__call() work.
- */
- public function testHtml5Controls(): void
- {
- $result = $this->Form->email('User.email');
- $expected = [
- 'input' => ['type' => 'email', 'name' => 'User[email]'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->search('User.query');
- $expected = [
- 'input' => ['type' => 'search', 'name' => 'User[query]'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->search('User.query', ['value' => 'test']);
- $expected = [
- 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
- $expected = [
- 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testHtml5ControlWithControl method
- *
- * Test accessing html5 inputs through control().
- */
- public function testHtml5ControlWithControl(): void
- {
- $this->Form->create();
- $this->Form->setTemplates(['inputContainer' => '{{content}}']);
- $result = $this->Form->control('website', [
- 'type' => 'url',
- 'val' => 'http://domain.tld',
- 'label' => false,
- ]);
- $expected = [
- 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testHtml5ControlException method
- *
- * Test errors when field name is missing.
- */
- public function testHtml5ControlException(): void
- {
- $this->expectException(CakeException::class);
- $this->Form->email();
- }
- /**
- * tests fields that are required use custom validation messages
- */
- public function testHtml5ErrorMessage(): void
- {
- $this->Form->setConfig('autoSetCustomValidity', true);
- $validator = (new Validator())
- ->notEmptyString('email', 'Custom error message')
- ->requirePresence('password')
- ->alphaNumeric('password')
- ->requirePresence('accept_tos')
- ->notBlank('phone');
- $table = $this->getTableLocator()->get('Contacts', [
- 'className' => ContactsTable::class,
- ]);
- $table->setValidator('default', $validator);
- $contact = new Entity();
- $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
- $this->Form->setTemplates(['inputContainer' => '{{content}}']);
- $result = $this->Form->control('password');
- $expected = [
- 'label' => ['for' => 'password'],
- 'Password',
- '/label',
- 'input' => [
- 'id' => 'password',
- 'name' => 'password',
- 'type' => 'password',
- 'value' => '',
- 'required' => 'required',
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninput' => 'this.setCustomValidity('')',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('phone');
- $expected = [
- 'label' => ['for' => 'phone'],
- 'Phone',
- '/label',
- 'input' => [
- 'id' => 'phone',
- 'name' => 'phone',
- 'type' => 'tel',
- 'value' => '',
- 'maxlength' => 255,
- 'required' => 'required',
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninput' => 'this.setCustomValidity('')',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('email');
- $expected = [
- 'label' => ['for' => 'email'],
- 'Email',
- '/label',
- 'input' => [
- 'id' => 'email',
- 'name' => 'email',
- 'type' => 'email',
- 'value' => '',
- 'maxlength' => 255,
- 'required' => 'required',
- 'data-validity-message' => 'Custom error message',
- 'oninput' => 'this.setCustomValidity('')',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('accept_tos', ['type' => 'checkbox']);
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'accept_tos', 'value' => '0']],
- 'label' => ['for' => 'accept-tos'],
- [
- 'input' => [
- 'aria-required' => 'true',
- 'required' => 'required',
- 'type' => 'checkbox',
- 'name' => 'accept_tos',
- 'id' => 'accept-tos',
- 'value' => '1',
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninput' => 'this.setCustomValidity('')',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.checked) this.setCustomValidity(this.dataset.validityMessage)',
- ],
- ],
- 'Accept Tos',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * tests that custom validation messages are in templateVars
- */
- public function testHtml5ErrorMessageInTemplateVars(): void
- {
- $validator = (new Validator())
- ->notEmptyString('email', 'Custom error "message" & entities')
- ->requirePresence('password')
- ->alphaNumeric('password')
- ->notBlank('phone');
- $table = $this->getTableLocator()->get('Contacts', [
- 'className' => ContactsTable::class,
- ]);
- $table->setValidator('default', $validator);
- $contact = new Entity();
- $this->Form->setConfig('autoSetCustomValidity', false);
- $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
- $this->Form->setTemplates([
- 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
- 'inputContainer' => '{{content}}',
- ]);
- $result = $this->Form->control('password');
- $expected = [
- 'label' => ['for' => 'password'],
- 'Password',
- '/label',
- 'input' => [
- 'id' => 'password',
- 'name' => 'password',
- 'type' => 'password',
- 'value' => '',
- 'required' => 'required',
- 'data-message' => 'This field cannot be left empty',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('phone');
- $expected = [
- 'label' => ['for' => 'phone'],
- 'Phone',
- '/label',
- 'input' => [
- 'id' => 'phone',
- 'name' => 'phone',
- 'type' => 'tel',
- 'value' => '',
- 'maxlength' => 255,
- 'required' => 'required',
- 'data-message' => 'This field cannot be left empty',
- ],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('email', [
- 'templateVars' => [
- 'custom' => 'data-custom="1"',
- ],
- ]);
- $expected = [
- 'label' => ['for' => 'email'],
- 'Email',
- '/label',
- 'input' => [
- 'id' => 'email',
- 'name' => 'email',
- 'type' => 'email',
- 'value' => '',
- 'maxlength' => 255,
- 'required' => 'required',
- 'data-message' => 'Custom error "message" & entities',
- 'data-custom' => '1',
- ],
- ];
- $this->assertHtml($expected, $result);
- $this->Form->setConfig('autoSetCustomValidity', true);
- }
- /**
- * testRequiredAttribute method
- *
- * Tests that formhelper sets required attributes.
- */
- public function testRequiredAttribute(): void
- {
- $this->article['required'] = [
- 'title' => true,
- 'body' => false,
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('title');
- $expected = [
- 'div' => ['class' => 'input text required'],
- 'label' => ['for' => 'title'],
- 'Title',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'title',
- 'id' => 'title',
- 'required' => 'required',
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- 'oninput' => 'this.setCustomValidity('')',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('title', ['required' => false]);
- $this->assertStringNotContainsString('required', $result);
- $result = $this->Form->control('body');
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'body'],
- 'Body',
- '/label',
- 'input' => [
- 'type' => 'text',
- 'name' => 'body',
- 'id' => 'body',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('body', ['required' => true]);
- $this->assertStringContainsString('required', $result);
- }
- /**
- * testControlsNotNested method
- *
- * Tests that it is possible to put inputs outside of the label.
- */
- public function testControlsNotNested(): void
- {
- $this->Form->setTemplates([
- 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
- 'formGroup' => '{{input}}{{label}}',
- ]);
- $result = $this->Form->control('foo', ['type' => 'checkbox']);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
- ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
- 'label' => ['for' => 'foo'],
- 'Foo',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
- $expected = [
- 'div' => ['class' => 'input checkbox'],
- ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
- ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('confirm', [
- 'type' => 'radio',
- 'options' => ['Y' => 'Yes', 'N' => 'No'],
- ]);
- $expected = [
- 'div' => ['class' => 'input radio'],
- ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '', 'id' => 'confirm']],
- ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
- ['label' => ['for' => 'confirm-y']],
- 'Yes',
- '/label',
- ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
- ['label' => ['for' => 'confirm-n']],
- 'No',
- '/label',
- '<label',
- 'Confirm',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select('category', ['1', '2'], [
- 'multiple' => 'checkbox',
- 'name' => 'fish',
- ]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'],
- ['div' => ['class' => 'checkbox']],
- ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
- ['label' => ['for' => 'fish-0']],
- '1',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
- ['label' => ['for' => 'fish-1']],
- '2',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlContainerTemplates method
- *
- * Test that *Container templates are used by input.
- */
- public function testControlContainerTemplates(): void
- {
- $this->Form->setTemplates([
- 'checkboxContainer' => '<div class="check">{{content}}</div>',
- 'radioContainer' => '<div class="rad">{{content}}</div>',
- 'radioContainerError' => '<div class="rad err">{{content}}</div>',
- ]);
- $this->article['errors'] = [
- 'Article' => ['published' => 'error message'],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('accept', [
- 'type' => 'checkbox',
- ]);
- $expected = [
- 'div' => ['class' => 'check'],
- ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
- 'label' => ['for' => 'accept'],
- ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
- 'Accept',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('accept', [
- 'type' => 'radio',
- 'options' => ['Y', 'N'],
- ]);
- $this->assertStringContainsString('<div class="rad">', $result);
- $result = $this->Form->control('Article.published', [
- 'type' => 'radio',
- 'options' => ['Y', 'N'],
- ]);
- $this->assertStringContainsString('<div class="rad err">', $result);
- }
- /**
- * testFormGroupTemplates method
- *
- * Test that *Container templates are used by input.
- */
- public function testFormGroupTemplates(): void
- {
- $this->Form->setTemplates([
- 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
- ]);
- $this->Form->create($this->article);
- $result = $this->Form->control('accept', [
- 'type' => 'radio',
- 'options' => ['Y', 'N'],
- ]);
- $this->assertStringContainsString('<div class="radio">', $result);
- }
- /**
- * testResetTemplates method
- *
- * Test resetting templates.
- */
- public function testResetTemplates(): void
- {
- $this->Form->setTemplates(['input' => '<input/>']);
- $this->assertSame('<input/>', $this->Form->templater()->get('input'));
- $this->Form->resetTemplates();
- $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
- }
- /**
- * testContext method
- *
- * Test the context method.
- */
- public function testContext(): void
- {
- $result = $this->Form->context();
- $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
- $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
- $this->assertSame($mock, $this->Form->context($mock));
- $this->assertSame($mock, $this->Form->context());
- }
- /**
- * testAutoDomId method
- */
- public function testAutoDomId(): void
- {
- $result = $this->Form->text('field', ['id' => true]);
- $expected = [
- 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
- ];
- $this->assertHtml($expected, $result);
- // Ensure id => doesn't cause problem when multiple inputs are generated.
- $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '', 'id' => 'field'],
- ['label' => ['for' => 'field-0']],
- ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
- 'option A',
- '/label',
- ['label' => ['for' => 'field-1']],
- ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
- 'option B',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->select(
- 'multi_field',
- ['first', 'second'],
- ['multiple' => 'checkbox', 'id' => true]
- );
- $expected = [
- 'input' => [
- 'type' => 'hidden', 'name' => 'multi_field', 'value' => '', 'id' => 'multi-field',
- ],
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'multi-field-0']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'multi_field[]',
- 'value' => '0', 'id' => 'multi-field-0',
- ]],
- 'first',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => ['for' => 'multi-field-1']],
- ['input' => [
- 'type' => 'checkbox', 'name' => 'multi_field[]',
- 'value' => '1', 'id' => 'multi-field-1',
- ]],
- 'second',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test the basic setters and getters for value sources
- */
- public function testFormValueSourcesSettersGetters(): void
- {
- $this->View->setRequest($this->View->getRequest()
- ->withData('id', '1')
- ->withQueryParams(['id' => '2']));
- $expected = ['data', 'context'];
- $result = $this->Form->getValueSources();
- $this->assertEquals($expected, $result);
- $this->Form->setValueSources(['context']);
- $result = $this->Form->getSourceValue('id');
- $this->assertNull($result);
- $this->Form->setValueSources('query');
- $expected = ['query'];
- $result = $this->Form->getValueSources();
- $this->assertEquals($expected, $result);
- $expected = '2';
- $result = $this->Form->getSourceValue('id');
- $this->assertSame($expected, $result);
- $this->Form->setValueSources(['data']);
- $expected = '1';
- $result = $this->Form->getSourceValue('id');
- $this->assertSame($expected, $result);
- $this->Form->setValueSources(['query', 'data']);
- $expected = '2';
- $result = $this->Form->getSourceValue('id');
- $this->assertSame($expected, $result);
- }
- public function testValueSourcesValidation(): void
- {
- $this->expectException(InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid value source(s): `invalid`, `foo`. Valid values are: `context`, `data`, `query`.');
- $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
- }
- /**
- * Tests the different input rendering values based on sources values switching
- */
- public function testFormValueSourcesSingleSwitchRendering(): void
- {
- $articles = $this->getTableLocator()->get('Articles');
- $article = new Article();
- $articles->patchEntity($article, ['id' => '3']);
- $this->Form->create($article);
- $this->Form->setValueSources(['context']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
- $this->Form->setValueSources(['query']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()
- ->withQueryParams(['id' => '5a'])
- ->withData('id', '5b'));
- $this->Form->setValueSources(['context']);
- $this->Form->create($article);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
- ];
- $this->assertHtml($expected, $result);
- $this->Form->setValueSources(['data']);
- $this->Form->create($article);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
- ];
- $this->assertHtml($expected, $result);
- $this->Form->setValueSources(['query']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Tests the different input rendering values based on sources values switching while supplying
- * an entity (base context) and multiple sources (such as data, query)
- */
- public function testFormValueSourcesListSwitchRendering(): void
- {
- $articles = $this->getTableLocator()->get('Articles');
- $article = new Article();
- $articles->patchEntity($article, ['id' => '3']);
- $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
- $this->Form->create($article);
- $this->Form->setValueSources(['context', 'query']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
- ];
- $this->assertHtml($expected, $result);
- $this->Form->setValueSources(['query', 'context']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
- ];
- $this->assertHtml($expected, $result);
- $this->Form->setValueSources(['data', 'query', 'context']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
- ];
- $this->assertHtml($expected, $result);
- $this->View->setRequest($this->View->getRequest()
- ->withData('id', '8')
- ->withQueryParams(['id' => '9']));
- $this->Form->setValueSources(['data', 'query', 'context']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test the different form input renderings based on values sources switchings through form options
- */
- public function testFormValueSourcesSwitchViaOptionsRendering(): void
- {
- $articles = $this->getTableLocator()->get('Articles');
- $article = new Article();
- $articles->patchEntity($article, ['id' => '3']);
- $this->View->setRequest(
- $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
- );
- $this->Form->create($article, ['valueSources' => 'query']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getSourceValue('id');
- $this->assertSame('5', $result);
- $this->Form->setValueSources(['context']);
- $this->Form->create($article, ['valueSources' => 'query']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getSourceValue('id');
- $this->assertSame('5', $result);
- $this->Form->setValueSources(['query']);
- $this->Form->create($article, ['valueSources' => 'data']);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getSourceValue('id');
- $this->assertSame('4', $result);
- $this->Form->setValueSources(['query']);
- $this->Form->create($article, ['valueSources' => ['context', 'data']]);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getSourceValue('id');
- $this->assertSame(3, $result);
- }
- /**
- * Test the different form input renderings based on values sources switchings through form options
- */
- public function testFormValueSourcesSwitchViaOptionsAndSetterRendering(): void
- {
- $articles = $this->getTableLocator()->get('Articles');
- $article = new Article();
- $articles->patchEntity($article, ['id' => '3']);
- $this->View->setRequest(
- $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
- );
- $this->Form->setValueSources(['context'])
- ->create($article, ['valueSources' => ['query', 'data']]);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getSourceValue('id');
- $this->assertSame('11', $result);
- $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
- $this->Form->setValueSources(['context'])
- ->create($article, ['valueSources' => ['query', 'data']]);
- $result = $this->Form->control('id');
- $expected = [
- ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->getSourceValue('id');
- $this->assertSame('10', $result);
- }
- /**
- * Test the different form values sources resetting through From::end();
- */
- public function testFormValueSourcesResetViaEnd(): void
- {
- $expected = ['data', 'context'];
- $result = $this->Form->getValueSources();
- $this->assertEquals($expected, $result);
- $expected = ['query', 'context', 'data'];
- $this->Form->setValueSources(['query', 'context', 'data']);
- $result = $this->Form->getValueSources();
- $this->assertEquals($expected, $result);
- $this->Form->create();
- $result = $this->Form->getValueSources();
- $this->assertEquals($expected, $result);
- $this->Form->end();
- $result = $this->Form->getValueSources();
- $this->assertEquals(['data', 'context'], $result);
- }
- /**
- * Test sources values defaults handling
- */
- public function testFormValueSourcesDefaults(): void
- {
- $this->View->setRequest(
- $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
- );
- $this->Form->create();
- $result = $this->Form->password('password');
- $expected = ['input' => ['type' => 'password', 'name' => 'password']];
- $this->assertHtml($expected, $result);
- $result = $this->Form->password('password', ['default' => 'helloworld']);
- $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
- $this->assertHtml($expected, $result);
- $this->Form->setValueSources('query');
- $result = $this->Form->password('password', ['default' => 'helloworld']);
- $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
- $this->assertHtml($expected, $result);
- $this->Form->setValueSources('data');
- $result = $this->Form->password('password', ['default' => 'helloworld']);
- $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
- $this->assertHtml($expected, $result);
- }
- /**
- * Test sources values schema defaults handling
- */
- public function testSourcesValueDoesntExistPassThrough(): void
- {
- $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
- $articles = $this->getTableLocator()->get('Articles');
- $entity = $articles->newEmptyEntity();
- $this->Form->create($entity);
- $this->Form->setValueSources(['query', 'context']);
- $result = $this->Form->getSourceValue('category');
- $this->assertSame('sesame-cookies', $result);
- $this->Form->setValueSources(['context', 'query']);
- $result = $this->Form->getSourceValue('category');
- $this->assertSame('sesame-cookies', $result);
- }
- /**
- * testNestedLabelInput method
- *
- * Test the `nestedInput` parameter
- */
- public function testNestedLabelInput(): void
- {
- $result = $this->Form->control('foo', ['nestedInput' => true]);
- $expected = [
- 'div' => ['class' => 'input text'],
- 'label' => ['for' => 'foo'],
- ['input' => [
- 'type' => 'text',
- 'name' => 'foo',
- 'id' => 'foo',
- ]],
- 'Foo',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
- *
- * This test makes sure `false` excludes the label from the render
- */
- public function testControlLabelManipulationDisableLabels(): void
- {
- $result = $this->Form->control('test', [
- 'type' => 'radio',
- 'options' => ['A', 'B'],
- 'labelOptions' => false,
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- '<label',
- 'Test',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('checkbox1', [
- 'label' => 'My checkboxes',
- 'multiple' => 'checkbox',
- 'type' => 'select',
- 'options' => [
- ['text' => 'First Checkbox', 'value' => 1],
- ['text' => 'Second Checkbox', 'value' => 2],
- ],
- 'labelOptions' => false,
- ]);
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'checkbox1']],
- 'My checkboxes',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
- ['div' => ['class' => 'checkbox']],
- ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
- *
- * This test checks rendering of class (as string and array) also makes sure 'selected' is
- * added to the class if checked.
- *
- * Also checks to make sure any custom attributes are rendered correctly
- */
- public function testControlLabelManipulationRadios(): void
- {
- $result = $this->Form->control('test', [
- 'type' => 'radio',
- 'options' => ['A', 'B'],
- 'labelOptions' => ['class' => 'custom-class'],
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- '<label',
- 'Test',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
- ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- 'A',
- '/label',
- ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
- 'B',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('test', [
- 'type' => 'radio',
- 'options' => ['A', 'B'],
- 'value' => 1,
- 'labelOptions' => ['class' => 'custom-class'],
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- '<label',
- 'Test',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
- ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- 'A',
- '/label',
- ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
- 'B',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('test', [
- 'type' => 'radio',
- 'options' => ['A', 'B'],
- 'value' => 1,
- 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
- ]);
- $expected = [
- ['div' => ['class' => 'input radio']],
- '<label',
- 'Test',
- '/label',
- ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test']],
- ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- 'A',
- '/label',
- ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
- 'B',
- '/label',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->radio('test', ['A', 'B'], [
- 'label' => [
- 'class' => ['custom-class', 'another-class'],
- 'data-name' => 'bob',
- ],
- 'value' => 1,
- ]);
- $expected = [
- 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => '', 'id' => 'test'],
- ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
- ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
- 'A',
- '/label',
- ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
- ['input' => [
- 'type' => 'radio',
- 'name' => 'test',
- 'value' => '1',
- 'id' => 'test-1',
- 'checked' => 'checked',
- ]],
- 'B',
- '/label',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
- *
- * This test checks rendering of class (as string and array) also makes sure 'selected' is
- * added to the class if checked.
- *
- * Also checks to make sure any custom attributes are rendered correctly
- */
- public function testControlLabelManipulationCheckboxes(): void
- {
- $result = $this->Form->control('checkbox1', [
- 'label' => 'My checkboxes',
- 'multiple' => 'checkbox',
- 'type' => 'select',
- 'options' => [
- ['text' => 'First Checkbox', 'value' => 1],
- ['text' => 'Second Checkbox', 'value' => 2],
- ],
- 'labelOptions' => ['class' => 'custom-class'],
- 'value' => ['1'],
- ]);
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'checkbox1']],
- 'My checkboxes',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
- ['div' => ['class' => 'checkbox']],
- ['label' => [
- 'class' => 'custom-class selected',
- 'for' => 'checkbox1-1',
- ]],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'checkbox1[]',
- 'value' => '1',
- 'id' => 'checkbox1-1',
- 'checked' => 'checked',
- ]],
- 'First Checkbox',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => [
- 'class' => 'custom-class',
- 'for' => 'checkbox1-2',
- ]],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'checkbox1[]',
- 'value' => '2',
- 'id' => 'checkbox1-2',
- ]],
- 'Second Checkbox',
- '/label',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $result = $this->Form->control('checkbox1', [
- 'label' => 'My checkboxes',
- 'multiple' => 'checkbox',
- 'type' => 'select',
- 'options' => [
- ['text' => 'First Checkbox', 'value' => 1],
- ['text' => 'Second Checkbox', 'value' => 2],
- ],
- 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
- 'value' => ['1'],
- ]);
- $expected = [
- ['div' => ['class' => 'input select']],
- ['label' => ['for' => 'checkbox1']],
- 'My checkboxes',
- '/label',
- 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => '', 'id' => 'checkbox1'],
- ['div' => ['class' => 'checkbox']],
- ['label' => [
- 'class' => 'custom-class another-class selected',
- 'data-name' => 'bob',
- 'for' => 'checkbox1-1',
- ]],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'checkbox1[]',
- 'value' => '1',
- 'id' => 'checkbox1-1',
- 'checked' => 'checked',
- ]],
- 'First Checkbox',
- '/label',
- '/div',
- ['div' => ['class' => 'checkbox']],
- ['label' => [
- 'class' => 'custom-class another-class',
- 'data-name' => 'bob',
- 'for' => 'checkbox1-2',
- ]],
- ['input' => [
- 'type' => 'checkbox',
- 'name' => 'checkbox1[]',
- 'value' => '2',
- 'id' => 'checkbox1-2',
- ]],
- 'Second Checkbox',
- '/label',
- '/div',
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlMaxLengthArrayContext method
- *
- * Test control() with maxlength attribute in Array Context.
- */
- public function testControlMaxLengthArrayContext(): void
- {
- $this->article['schema'] = [
- 'title' => ['length' => 10],
- ];
- $this->Form->create($this->article);
- $result = $this->Form->control('title');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Title',
- '/label',
- 'input' => [
- 'id',
- 'name' => 'title',
- 'type' => 'text',
- 'required' => 'required',
- 'maxlength' => 10,
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- 'oninput' => 'this.setCustomValidity('')',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlMaxLengthEntityContext method
- *
- * Test control() with maxlength attribute in Entity Context.
- */
- public function testControlMaxLengthEntityContext(): void
- {
- $this->article['schema']['title']['length'] = 45;
- $validator = new Validator();
- $validator->maxLength('title', 10);
- $article = new EntityContext(
- [
- 'entity' => new Entity($this->article),
- 'table' => new Table([
- 'alias' => 'Articles',
- 'schema' => $this->article['schema'],
- 'validator' => $validator,
- ]),
- ]
- );
- $this->Form->create($article);
- $result = $this->Form->control('title');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Title',
- '/label',
- 'input' => [
- 'id',
- 'name' => 'title',
- 'type' => 'text',
- 'required' => 'required',
- 'maxlength' => 10,
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- 'oninput' => 'this.setCustomValidity('')',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $this->article['schema']['title']['length'] = 45;
- $validator = new Validator();
- $validator->maxLength('title', 55);
- $article = new EntityContext(
- [
- 'entity' => new Entity($this->article),
- 'table' => new Table([
- 'schema' => $this->article['schema'],
- 'validator' => $validator,
- 'alias' => 'Articles',
- ]),
- ]
- );
- $this->Form->create($article);
- $result = $this->Form->control('title');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Title',
- '/label',
- 'input' => [
- 'id',
- 'name' => 'title',
- 'type' => 'text',
- 'required' => 'required',
- 'maxlength' => 55, // Length set in validator should take precedence over schema.
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- 'oninput' => 'this.setCustomValidity('')',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- $this->article['schema']['title']['length'] = 45;
- $validator = new Validator();
- $validator->maxLength('title', 55);
- $article = new EntityContext(
- [
- 'entity' => new Entity($this->article),
- 'table' => new Table([
- 'schema' => $this->article['schema'],
- 'validator' => $validator,
- 'alias' => 'Articles',
- ]),
- ]
- );
- $this->Form->create($article);
- $result = $this->Form->control('title', ['maxlength' => 10]);
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Title',
- '/label',
- 'input' => [
- 'id',
- 'name' => 'title',
- 'type' => 'text',
- 'required' => 'required',
- 'maxlength' => 10, // Length set in options should take highest precedence.
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- 'oninput' => 'this.setCustomValidity('')',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlMinMaxLengthEntityContext method
- *
- * Test control() with maxlength attribute in Entity Context sets the minimum val.
- */
- public function testControlMinMaxLengthEntityContext(): void
- {
- $validator = new Validator();
- $validator->maxLength('title', 10);
- $article = new EntityContext(
- [
- 'entity' => new Entity($this->article),
- 'table' => new Table([
- 'alias' => 'Articles',
- 'schema' => $this->article['schema'],
- 'validator' => $validator,
- ]),
- ]
- );
- $this->Form->create($article);
- $result = $this->Form->control('title');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Title',
- '/label',
- 'input' => [
- 'id',
- 'name' => 'title',
- 'type' => 'text',
- 'required' => 'required',
- 'maxlength' => 10,
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- 'oninput' => 'this.setCustomValidity('')',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- /**
- * testControlMaxLengthFormContext method
- *
- * Test control() with maxlength attribute in Form Context.
- */
- public function testControlMaxLengthFormContext(): void
- {
- $validator = new Validator();
- $validator->maxLength('title', 10);
- $form = new Form();
- $form->setValidator('default', $validator);
- $this->Form->create($form);
- $result = $this->Form->control('title');
- $expected = [
- 'div' => ['class'],
- 'label' => ['for'],
- 'Title',
- '/label',
- 'input' => [
- 'id',
- 'name' => 'title',
- 'type' => 'text',
- 'required' => 'required',
- 'maxlength' => 10,
- 'data-validity-message' => 'This field cannot be left empty',
- 'oninvalid' => 'this.setCustomValidity(''); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
- 'oninput' => 'this.setCustomValidity('')',
- ],
- '/div',
- ];
- $this->assertHtml($expected, $result);
- }
- }
|