FormHelperTest.php 303 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Helper;
  17. use Cake\Collection\Collection;
  18. use Cake\Core\Configure;
  19. use Cake\Form\Form;
  20. use Cake\Http\ServerRequest;
  21. use Cake\I18n\Date;
  22. use Cake\I18n\FrozenTime;
  23. use Cake\ORM\Entity;
  24. use Cake\ORM\Table;
  25. use Cake\Routing\Router;
  26. use Cake\TestSuite\TestCase;
  27. use Cake\Utility\Security;
  28. use Cake\Validation\Validator;
  29. use Cake\View\Form\EntityContext;
  30. use Cake\View\Helper\FormHelper;
  31. use Cake\View\View;
  32. use Cake\View\Widget\WidgetLocator;
  33. use ReflectionProperty;
  34. use TestApp\Model\Entity\Article;
  35. use TestApp\Model\Table\ContactsTable;
  36. use TestApp\Model\Table\ValidateUsersTable;
  37. /**
  38. * FormHelperTest class
  39. *
  40. * @property \Cake\View\Helper\FormHelper $Form
  41. * @property \Cake\View\View $View
  42. */
  43. class FormHelperTest extends TestCase
  44. {
  45. /**
  46. * Fixtures to be used
  47. *
  48. * @var array
  49. */
  50. protected $fixtures = ['core.Articles', 'core.Comments'];
  51. /**
  52. * Do not load the fixtures by default
  53. *
  54. * @var bool
  55. */
  56. public $autoFixtures = false;
  57. /**
  58. * @var array
  59. */
  60. protected $article = [];
  61. /**
  62. * @var string
  63. */
  64. protected $url;
  65. /**
  66. * setUp method
  67. *
  68. * @return void
  69. */
  70. public function setUp(): void
  71. {
  72. parent::setUp();
  73. Configure::write('Config.language', 'eng');
  74. Configure::write('App.base', '');
  75. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  76. $request = new ServerRequest([
  77. 'webroot' => '',
  78. 'base' => '',
  79. 'url' => '/articles/add',
  80. 'params' => [
  81. 'controller' => 'articles',
  82. 'action' => 'add',
  83. 'plugin' => null,
  84. ],
  85. ]);
  86. $this->View = new View($request);
  87. Router::reload();
  88. Router::setRequest($request);
  89. $this->url = '/articles/add';
  90. $this->Form = new FormHelper($this->View);
  91. $this->dateRegex = [
  92. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  93. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  94. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  95. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  96. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  97. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  98. ];
  99. $this->article = [
  100. 'schema' => [
  101. 'id' => ['type' => 'integer'],
  102. 'author_id' => ['type' => 'integer', 'null' => true],
  103. 'title' => ['type' => 'string', 'null' => true],
  104. 'body' => 'text',
  105. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  106. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  107. ],
  108. 'required' => [
  109. 'author_id' => true,
  110. 'title' => true,
  111. ],
  112. ];
  113. Security::setSalt('foo!');
  114. Router::connect('/:controller', ['action' => 'index']);
  115. Router::connect('/:controller/:action/*');
  116. }
  117. /**
  118. * tearDown method
  119. *
  120. * @return void
  121. */
  122. public function tearDown(): void
  123. {
  124. parent::tearDown();
  125. unset($this->Form, $this->Controller, $this->View);
  126. }
  127. /**
  128. * Test construct() with the templates option.
  129. *
  130. * @return void
  131. */
  132. public function testConstructTemplatesFile()
  133. {
  134. $helper = new FormHelper($this->View, [
  135. 'templates' => 'htmlhelper_tags',
  136. ]);
  137. $result = $helper->control('name');
  138. $this->assertStringContainsString('<input', $result);
  139. }
  140. /**
  141. * Test that when specifying custom widgets the config array for that widget
  142. * is overwritten instead of merged.
  143. *
  144. * @return void
  145. */
  146. public function testConstructWithWidgets()
  147. {
  148. $config = [
  149. 'widgets' => [
  150. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  151. ],
  152. ];
  153. $helper = new FormHelper($this->View, $config);
  154. $locator = $helper->getWidgetLocator();
  155. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  156. }
  157. /**
  158. * Test that when specifying custom widgets config file and it should be
  159. * added to widgets array. WidgetLocator will load widgets in constructor.
  160. *
  161. * @return void
  162. */
  163. public function testConstructWithWidgetsConfig()
  164. {
  165. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  166. $locator = $helper->getWidgetLocator();
  167. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  168. }
  169. /**
  170. * Test setting the widget locator
  171. *
  172. * @return void
  173. */
  174. public function testSetAndGetWidgetLocator()
  175. {
  176. $helper = new FormHelper($this->View);
  177. $locator = new WidgetLocator($helper->templater(), $this->View);
  178. $helper->setWidgetLocator($locator);
  179. $this->assertSame($locator, $helper->getWidgetLocator());
  180. }
  181. /**
  182. * Test overridding grouped input types which controls generation of "for"
  183. * attribute of labels.
  184. *
  185. * @return void
  186. */
  187. public function testConstructWithGroupedInputTypes()
  188. {
  189. $helper = new FormHelper($this->View, [
  190. 'groupedInputTypes' => ['radio'],
  191. ]);
  192. $result = $helper->control('when', ['type' => 'datetime-local']);
  193. $this->assertStringContainsString('<label for="when">When</label>', $result);
  194. }
  195. /**
  196. * Test registering a new widget class and rendering it.
  197. *
  198. * @return void
  199. */
  200. public function testAddWidgetAndRenderWidget()
  201. {
  202. $data = [
  203. 'val' => 1,
  204. ];
  205. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  206. $this->Form->addWidget('test', $mock);
  207. $mock->expects($this->once())
  208. ->method('render')
  209. ->with($data)
  210. ->will($this->returnValue('HTML'));
  211. $result = $this->Form->widget('test', $data);
  212. $this->assertSame('HTML', $result);
  213. }
  214. /**
  215. * Test that secureFields() of widget is called after calling render(),
  216. * not before.
  217. *
  218. * @return void
  219. */
  220. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  221. {
  222. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  223. 'unlockedFields' => [],
  224. ]));
  225. $data = [
  226. 'val' => 1,
  227. 'name' => 'test',
  228. ];
  229. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  230. $this->Form->addWidget('test', $mock);
  231. $mock->expects($this->at(0))
  232. ->method('render')
  233. ->with($data)
  234. ->will($this->returnValue('HTML'));
  235. $mock->expects($this->at(1))
  236. ->method('secureFields')
  237. ->with($data)
  238. ->will($this->returnValue(['test']));
  239. $this->Form->create();
  240. $result = $this->Form->widget('test', $data + ['secure' => true]);
  241. $this->assertSame('HTML', $result);
  242. }
  243. /**
  244. * Test that empty string is not added to secure fields list when
  245. * rendering input widget without name.
  246. *
  247. * @return void
  248. */
  249. public function testRenderingWidgetWithEmptyName()
  250. {
  251. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  252. $this->Form->create();
  253. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  254. $this->assertSame('<select name=""></select>', $result);
  255. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  256. $this->assertEquals([], $result);
  257. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  258. $this->assertSame('<select name="0"></select>', $result);
  259. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  260. $this->assertEquals(['0'], $result);
  261. }
  262. /**
  263. * Test registering an invalid widget class.
  264. *
  265. * @return void
  266. */
  267. public function testAddWidgetInvalid()
  268. {
  269. $this->expectException(\RuntimeException::class);
  270. $mock = new \stdClass();
  271. $this->Form->addWidget('test', $mock);
  272. $this->Form->widget('test');
  273. }
  274. /**
  275. * Test adding a new context class.
  276. *
  277. * @return void
  278. */
  279. public function testAddContextProvider()
  280. {
  281. $context = 'My data';
  282. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  283. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  284. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  285. $this->assertEquals($context, $data['entity']);
  286. return $stub;
  287. });
  288. $this->Form->create($context);
  289. $result = $this->Form->context();
  290. $this->assertSame($stub, $result);
  291. }
  292. /**
  293. * Test replacing a context class.
  294. *
  295. * @return void
  296. */
  297. public function testAddContextProviderReplace()
  298. {
  299. $entity = new Article();
  300. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  301. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  302. return $stub;
  303. });
  304. $this->Form->create($entity);
  305. $result = $this->Form->context();
  306. $this->assertSame($stub, $result);
  307. }
  308. /**
  309. * Test overriding a context class.
  310. *
  311. * @return void
  312. */
  313. public function testAddContextProviderAdd()
  314. {
  315. $entity = new Article();
  316. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  317. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  318. if ($data['entity'] instanceof Entity) {
  319. return $stub;
  320. }
  321. });
  322. $this->Form->create($entity);
  323. $result = $this->Form->context();
  324. $this->assertSame($stub, $result);
  325. }
  326. /**
  327. * Provides context options for create().
  328. *
  329. * @return array
  330. */
  331. public function contextSelectionProvider()
  332. {
  333. $entity = new Article();
  334. $collection = new Collection([$entity]);
  335. $emptyCollection = new Collection([]);
  336. $arrayObject = new \ArrayObject([]);
  337. $data = [
  338. 'schema' => [
  339. 'title' => ['type' => 'string'],
  340. ],
  341. ];
  342. $form = new Form();
  343. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  344. return [
  345. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  346. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  347. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  348. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  349. 'form' => [$form, 'Cake\View\Form\FormContext'],
  350. 'none' => [null, 'Cake\View\Form\NullContext'],
  351. 'custom' => [$custom, get_class($custom)],
  352. ];
  353. }
  354. /**
  355. * Test default context selection in create()
  356. *
  357. * @dataProvider contextSelectionProvider
  358. * @return void
  359. */
  360. public function testCreateContextSelectionBuiltIn($data, $class)
  361. {
  362. $this->loadFixtures('Articles');
  363. $this->Form->create($data);
  364. $this->assertInstanceOf($class, $this->Form->context());
  365. }
  366. /**
  367. * Data provider for type option.
  368. *
  369. * @return array
  370. */
  371. public static function requestTypeProvider()
  372. {
  373. return [
  374. // type, method, override
  375. ['post', 'post', 'POST'],
  376. ['put', 'post', 'PUT'],
  377. ['patch', 'post', 'PATCH'],
  378. ['delete', 'post', 'DELETE'],
  379. ];
  380. }
  381. /**
  382. * Test creating file forms.
  383. *
  384. * @return void
  385. */
  386. public function testCreateFile()
  387. {
  388. $encoding = strtolower(Configure::read('App.encoding'));
  389. $result = $this->Form->create(null, ['type' => 'file']);
  390. $expected = [
  391. 'form' => [
  392. 'method' => 'post', 'action' => '/articles/add',
  393. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  394. ],
  395. 'div' => ['style' => 'display:none;'],
  396. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  397. '/div',
  398. ];
  399. $this->assertHtml($expected, $result);
  400. }
  401. /**
  402. * Test creating GET forms.
  403. *
  404. * @return void
  405. */
  406. public function testCreateGet()
  407. {
  408. $encoding = strtolower(Configure::read('App.encoding'));
  409. $result = $this->Form->create(null, ['type' => 'get']);
  410. $expected = ['form' => [
  411. 'method' => 'get', 'action' => '/articles/add',
  412. 'accept-charset' => $encoding,
  413. ]];
  414. $this->assertHtml($expected, $result);
  415. }
  416. /**
  417. * Test explicit method/enctype options.
  418. *
  419. * Explicit method overwrites inferred method from 'type'
  420. *
  421. * @return void
  422. */
  423. public function testCreateExplicitMethodEnctype()
  424. {
  425. $encoding = strtolower(Configure::read('App.encoding'));
  426. $result = $this->Form->create(null, [
  427. 'type' => 'get',
  428. 'method' => 'put',
  429. 'enctype' => 'multipart/form-data',
  430. ]);
  431. $expected = ['form' => [
  432. 'method' => 'put',
  433. 'action' => '/articles/add',
  434. 'enctype' => 'multipart/form-data',
  435. 'accept-charset' => $encoding,
  436. ]];
  437. $this->assertHtml($expected, $result);
  438. }
  439. /**
  440. * Test create() with the templates option.
  441. *
  442. * @return void
  443. */
  444. public function testCreateTemplatesArray()
  445. {
  446. $result = $this->Form->create($this->article, [
  447. 'templates' => [
  448. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  449. ],
  450. ]);
  451. $expected = [
  452. 'form' => [
  453. 'class' => 'form-horizontal',
  454. 'method' => 'post',
  455. 'action' => '/articles/add',
  456. 'accept-charset' => 'utf-8',
  457. ],
  458. ];
  459. $this->assertHtml($expected, $result);
  460. }
  461. /**
  462. * Test create() with the templates option.
  463. *
  464. * @return void
  465. */
  466. public function testCreateTemplatesFile()
  467. {
  468. $result = $this->Form->create($this->article, [
  469. 'templates' => 'htmlhelper_tags',
  470. ]);
  471. $expected = [
  472. 'start form',
  473. ];
  474. $this->assertHtml($expected, $result);
  475. }
  476. /**
  477. * Test that create() and end() restore templates.
  478. *
  479. * @return void
  480. */
  481. public function testCreateEndRestoreTemplates()
  482. {
  483. $this->Form->create($this->article, [
  484. 'templates' => ['input' => 'custom input element'],
  485. ]);
  486. $this->Form->end();
  487. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  488. }
  489. /**
  490. * Test using template vars in various templates used by control() method.
  491. *
  492. * @return void
  493. */
  494. public function testControlTemplateVars()
  495. {
  496. $result = $this->Form->control('text', [
  497. 'templates' => [
  498. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  499. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  500. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  501. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  502. ],
  503. 'templateVars' => [
  504. 'forinput' => 'in-input',
  505. 'forlabel' => 'in-label',
  506. 'forgroup' => 'in-group',
  507. 'forcontainer' => 'in-container',
  508. ],
  509. ]);
  510. $expected = [
  511. 'div' => ['class'],
  512. 'label' => ['for'],
  513. 'Text in-label',
  514. '/label',
  515. 'in-group',
  516. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  517. 'in-container',
  518. '/div',
  519. ];
  520. $this->assertHtml($expected, $result);
  521. }
  522. /**
  523. * Test ensuring template variables work in template files loaded
  524. * during control().
  525. *
  526. * @return void
  527. */
  528. public function testControlTemplatesFromFile()
  529. {
  530. $result = $this->Form->control('title', [
  531. 'templates' => 'test_templates',
  532. 'templateVars' => [
  533. 'forcontainer' => 'container-data',
  534. ],
  535. ]);
  536. $expected = [
  537. 'div' => ['class'],
  538. 'label' => ['for'],
  539. 'Title',
  540. '/label',
  541. 'input' => ['name', 'type' => 'text', 'id'],
  542. 'container-data',
  543. '/div',
  544. ];
  545. $this->assertHtml($expected, $result);
  546. }
  547. /**
  548. * Test using template vars in inputSubmit and submitContainer template.
  549. *
  550. * @return void
  551. */
  552. public function testSubmitTemplateVars()
  553. {
  554. $this->Form->setTemplates([
  555. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  556. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  557. ]);
  558. $result = $this->Form->submit('Submit', [
  559. 'templateVars' => [
  560. 'forinput' => 'in-input',
  561. 'forcontainer' => 'in-container',
  562. ],
  563. ]);
  564. $expected = [
  565. 'div' => ['class'],
  566. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  567. 'in-container',
  568. '/div',
  569. ];
  570. $this->assertHtml($expected, $result);
  571. }
  572. /**
  573. * test the create() method
  574. *
  575. * @dataProvider requestTypeProvider
  576. * @return void
  577. */
  578. public function testCreateTypeOptions($type, $method, $override)
  579. {
  580. $encoding = strtolower(Configure::read('App.encoding'));
  581. $result = $this->Form->create(null, ['type' => $type]);
  582. $expected = [
  583. 'form' => [
  584. 'method' => $method, 'action' => '/articles/add',
  585. 'accept-charset' => $encoding,
  586. ],
  587. ];
  588. $extra = [
  589. 'div' => ['style' => 'display:none;'],
  590. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  591. '/div',
  592. ];
  593. if ($type !== 'post') {
  594. $expected = array_merge($expected, $extra);
  595. }
  596. $this->assertHtml($expected, $result);
  597. }
  598. /**
  599. * Test using template vars in Create (formStart template)
  600. *
  601. * @return void
  602. */
  603. public function testCreateTemplateVars()
  604. {
  605. $result = $this->Form->create($this->article, [
  606. 'templates' => [
  607. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  608. ],
  609. 'templateVars' => ['header' => 'headertext'],
  610. ]);
  611. $expected = [
  612. 'h4' => ['class'],
  613. 'headertext',
  614. '/h4',
  615. 'form' => [
  616. 'method' => 'post',
  617. 'action' => '/articles/add',
  618. 'accept-charset' => 'utf-8',
  619. ],
  620. ];
  621. $this->assertHtml($expected, $result);
  622. }
  623. /**
  624. * Test opening a form for an update operation.
  625. *
  626. * @return void
  627. */
  628. public function testCreateUpdateForm()
  629. {
  630. $encoding = strtolower(Configure::read('App.encoding'));
  631. $this->View->setRequest($this->View->getRequest()
  632. ->withRequestTarget('/articles/edit/1')
  633. ->withParam('action', 'edit'));
  634. $this->article['defaults']['id'] = 1;
  635. $result = $this->Form->create($this->article);
  636. $expected = [
  637. 'form' => [
  638. 'method' => 'post',
  639. 'action' => '/articles/edit/1',
  640. 'accept-charset' => $encoding,
  641. ],
  642. 'div' => ['style' => 'display:none;'],
  643. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  644. '/div',
  645. ];
  646. $this->assertHtml($expected, $result);
  647. }
  648. /**
  649. * test create() with automatic url generation
  650. *
  651. * @return void
  652. */
  653. public function testCreateAutoUrl()
  654. {
  655. $encoding = strtolower(Configure::read('App.encoding'));
  656. $this->View->setRequest($this->View->getRequest()
  657. ->withRequestTarget('/articles/delete/10')
  658. ->withParam('action', 'delete'));
  659. $result = $this->Form->create($this->article);
  660. $expected = [
  661. 'form' => [
  662. 'method' => 'post', 'action' => '/articles/delete/10',
  663. 'accept-charset' => $encoding,
  664. ],
  665. ];
  666. $this->assertHtml($expected, $result);
  667. $this->article['defaults'] = ['id' => 1];
  668. $this->View->setRequest($this->View->getRequest()
  669. ->withRequestTarget('/articles/edit/1')
  670. ->withParam('action', 'delete'));
  671. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  672. $expected = [
  673. 'form' => [
  674. 'method' => 'post',
  675. 'action' => '/articles/edit/1',
  676. 'accept-charset' => $encoding,
  677. ],
  678. 'div' => ['style' => 'display:none;'],
  679. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  680. '/div',
  681. ];
  682. $this->assertHtml($expected, $result);
  683. $this->View->setRequest($this->View->getRequest()
  684. ->withParam('action', 'add'));
  685. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  686. $expected = [
  687. 'form' => [
  688. 'method' => 'post',
  689. 'action' => '/articles/publish/1',
  690. 'accept-charset' => $encoding,
  691. ],
  692. 'div' => ['style' => 'display:none;'],
  693. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  694. '/div',
  695. ];
  696. $this->assertHtml($expected, $result);
  697. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  698. $expected = [
  699. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  700. 'div' => ['style' => 'display:none;'],
  701. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  702. '/div',
  703. ];
  704. $this->assertHtml($expected, $result);
  705. $this->View->setRequest($this->View->getRequest()
  706. ->withParam('controller', 'Pages'));
  707. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  708. $expected = [
  709. 'form' => [
  710. 'method' => 'post', 'action' => '/Pages/signup/1',
  711. 'accept-charset' => $encoding,
  712. ],
  713. 'div' => ['style' => 'display:none;'],
  714. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  715. '/div',
  716. ];
  717. $this->assertHtml($expected, $result);
  718. }
  719. /**
  720. * Test create() with no URL (no "action" attribute for <form> tag)
  721. *
  722. * @return void
  723. */
  724. public function testCreateNoUrl()
  725. {
  726. $result = $this->Form->create(null, ['url' => false]);
  727. $expected = [
  728. 'form' => [
  729. 'method' => 'post',
  730. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  731. ],
  732. ];
  733. $this->assertHtml($expected, $result);
  734. }
  735. /**
  736. * test create() with a custom route
  737. *
  738. * @return void
  739. */
  740. public function testCreateCustomRoute()
  741. {
  742. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  743. $encoding = strtolower(Configure::read('App.encoding'));
  744. $this->View->setRequest($this->View->getRequest()
  745. ->withParam('controller', 'users'));
  746. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  747. $expected = [
  748. 'form' => [
  749. 'method' => 'post', 'action' => '/login',
  750. 'accept-charset' => $encoding,
  751. ],
  752. ];
  753. $this->assertHtml($expected, $result);
  754. Router::connect(
  755. '/new-article',
  756. ['controller' => 'articles', 'action' => 'myaction'],
  757. ['_name' => 'my-route']
  758. );
  759. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  760. $expected = [
  761. 'form' => [
  762. 'method' => 'post', 'action' => '/new-article',
  763. 'accept-charset' => $encoding,
  764. ],
  765. ];
  766. $this->assertHtml($expected, $result);
  767. }
  768. /**
  769. * test automatic accept-charset overriding
  770. *
  771. * @return void
  772. */
  773. public function testCreateWithAcceptCharset()
  774. {
  775. $result = $this->Form->create(
  776. $this->article,
  777. [
  778. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  779. ]
  780. );
  781. $expected = [
  782. 'form' => [
  783. 'method' => 'post', 'action' => '/articles',
  784. 'accept-charset' => 'iso-8859-1',
  785. ],
  786. ];
  787. $this->assertHtml($expected, $result);
  788. }
  789. /**
  790. * Test base form URL when url param is passed with multiple parameters (&)
  791. */
  792. public function testCreateQueryStringRequest()
  793. {
  794. $encoding = strtolower(Configure::read('App.encoding'));
  795. $result = $this->Form->create($this->article, [
  796. 'type' => 'post',
  797. 'escape' => false,
  798. 'url' => [
  799. 'controller' => 'controller',
  800. 'action' => 'action',
  801. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  802. ],
  803. ]);
  804. $expected = [
  805. 'form' => [
  806. 'method' => 'post',
  807. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  808. 'accept-charset' => $encoding,
  809. ],
  810. ];
  811. $this->assertHtml($expected, $result);
  812. $result = $this->Form->create($this->article, [
  813. 'type' => 'post',
  814. 'url' => [
  815. 'controller' => 'controller',
  816. 'action' => 'action',
  817. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  818. ],
  819. ]);
  820. $this->assertHtml($expected, $result);
  821. }
  822. /**
  823. * test that create() doesn't cause errors by multiple id's being in the primary key
  824. * as could happen with multiple select or checkboxes.
  825. *
  826. * @return void
  827. */
  828. public function testCreateWithMultipleIdInData()
  829. {
  830. $encoding = strtolower(Configure::read('App.encoding'));
  831. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  832. $result = $this->Form->create($this->article);
  833. $expected = [
  834. 'form' => [
  835. 'method' => 'post',
  836. 'action' => '/articles/add',
  837. 'accept-charset' => $encoding,
  838. ],
  839. ];
  840. $this->assertHtml($expected, $result);
  841. }
  842. /**
  843. * test that create() doesn't add in extra passed params.
  844. *
  845. * @return void
  846. */
  847. public function testCreatePassedArgs()
  848. {
  849. $encoding = strtolower(Configure::read('App.encoding'));
  850. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  851. $result = $this->Form->create($this->article, [
  852. 'type' => 'post',
  853. 'escape' => false,
  854. 'url' => [
  855. 'action' => 'edit',
  856. 'myparam',
  857. ],
  858. ]);
  859. $expected = [
  860. 'form' => [
  861. 'method' => 'post',
  862. 'action' => '/articles/edit/myparam',
  863. 'accept-charset' => $encoding,
  864. ],
  865. ];
  866. $this->assertHtml($expected, $result);
  867. }
  868. /**
  869. * test creating a get form, and get form inputs.
  870. *
  871. * @return void
  872. */
  873. public function testGetFormCreate()
  874. {
  875. $encoding = strtolower(Configure::read('App.encoding'));
  876. $result = $this->Form->create($this->article, ['type' => 'get']);
  877. $expected = ['form' => [
  878. 'method' => 'get', 'action' => '/articles/add',
  879. 'accept-charset' => $encoding,
  880. ]];
  881. $this->assertHtml($expected, $result);
  882. $result = $this->Form->text('title');
  883. $expected = ['input' => [
  884. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  885. ]];
  886. $this->assertHtml($expected, $result);
  887. $result = $this->Form->password('password');
  888. $expected = ['input' => [
  889. 'name' => 'password', 'type' => 'password',
  890. ]];
  891. $this->assertHtml($expected, $result);
  892. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  893. $result = $this->Form->text('user_form');
  894. $expected = ['input' => [
  895. 'name' => 'user_form', 'type' => 'text',
  896. ]];
  897. $this->assertHtml($expected, $result);
  898. }
  899. /**
  900. * test get form, and inputs when the model param is false
  901. *
  902. * @return void
  903. */
  904. public function testGetFormWithFalseModel()
  905. {
  906. $encoding = strtolower(Configure::read('App.encoding'));
  907. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'contact_test'));
  908. $result = $this->Form->create(null, [
  909. 'type' => 'get', 'url' => ['controller' => 'contact_test'],
  910. ]);
  911. $expected = ['form' => [
  912. 'method' => 'get', 'action' => '/contact_test/add',
  913. 'accept-charset' => $encoding,
  914. ]];
  915. $this->assertHtml($expected, $result);
  916. $result = $this->Form->text('reason');
  917. $expected = [
  918. 'input' => ['type' => 'text', 'name' => 'reason'],
  919. ];
  920. $this->assertHtml($expected, $result);
  921. }
  922. /**
  923. * testFormCreateWithSecurity method
  924. *
  925. * Test form->create() with security key.
  926. *
  927. * @return void
  928. */
  929. public function testCreateWithSecurity()
  930. {
  931. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  932. $encoding = strtolower(Configure::read('App.encoding'));
  933. $result = $this->Form->create($this->article, [
  934. 'url' => '/articles/publish',
  935. ]);
  936. $expected = [
  937. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  938. 'div' => ['style' => 'display:none;'],
  939. ['input' => [
  940. 'type' => 'hidden',
  941. 'name' => '_csrfToken',
  942. 'value' => 'testKey',
  943. 'autocomplete' => 'off',
  944. ]],
  945. '/div',
  946. ];
  947. $this->assertHtml($expected, $result);
  948. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  949. $expected['form']['id'] = 'MyForm';
  950. $this->assertHtml($expected, $result);
  951. }
  952. /**
  953. * testFormCreateGetNoSecurity method
  954. *
  955. * Test form->create() with no security key as its a get form
  956. *
  957. * @return void
  958. */
  959. public function testCreateEndGetNoSecurity()
  960. {
  961. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  962. $article = new Article();
  963. $result = $this->Form->create($article, [
  964. 'type' => 'get',
  965. 'url' => '/contacts/add',
  966. ]);
  967. $this->assertStringNotContainsString('testKey', $result);
  968. $result = $this->Form->end();
  969. $this->assertStringNotContainsString('testKey', $result);
  970. }
  971. /**
  972. * Tests form hash generation with model-less data
  973. *
  974. * @return void
  975. */
  976. public function testValidateHashNoModel()
  977. {
  978. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  979. $fields = ['anything'];
  980. $this->Form->create();
  981. $result = $this->Form->secure($fields);
  982. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  983. $this->assertStringContainsString($hash, $result);
  984. }
  985. /**
  986. * Tests that hidden fields generated for checkboxes don't get locked
  987. *
  988. * @return void
  989. */
  990. public function testNoCheckboxLocking()
  991. {
  992. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  993. $this->Form->create();
  994. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  995. $this->Form->checkbox('check', ['value' => '1']);
  996. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  997. }
  998. /**
  999. * testFormSecurityFields method
  1000. *
  1001. * Test generation of secure form hash generation.
  1002. *
  1003. * @return void
  1004. */
  1005. public function testFormSecurityFields()
  1006. {
  1007. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1008. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1009. $this->Form->create();
  1010. $result = $this->Form->secure($fields);
  1011. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  1012. $hash .= ':' . 'Model.valid';
  1013. $hash = urlencode($hash);
  1014. $tokenDebug = urlencode(json_encode([
  1015. $this->url,
  1016. $fields,
  1017. [],
  1018. ]));
  1019. $expected = [
  1020. 'div' => ['style' => 'display:none;'],
  1021. ['input' => [
  1022. 'type' => 'hidden',
  1023. 'name' => '_Token[fields]',
  1024. 'value' => $hash,
  1025. 'autocomplete' => 'off',
  1026. ]],
  1027. ['input' => [
  1028. 'type' => 'hidden',
  1029. 'name' => '_Token[unlocked]',
  1030. 'value' => '',
  1031. 'autocomplete' => 'off',
  1032. ]],
  1033. ['input' => [
  1034. 'type' => 'hidden',
  1035. 'name' => '_Token[debug]',
  1036. 'value' => $tokenDebug,
  1037. 'autocomplete' => 'off',
  1038. ]],
  1039. '/div',
  1040. ];
  1041. $this->assertHtml($expected, $result);
  1042. }
  1043. /**
  1044. * testFormSecurityFields method
  1045. *
  1046. * Test debug token is not generated if debug is false
  1047. *
  1048. * @return void
  1049. */
  1050. public function testFormSecurityFieldsNoDebugMode()
  1051. {
  1052. Configure::write('debug', false);
  1053. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1054. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1055. $this->Form->create();
  1056. $result = $this->Form->secure($fields);
  1057. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  1058. $hash .= ':' . 'Model.valid';
  1059. $hash = urlencode($hash);
  1060. $expected = [
  1061. 'div' => ['style' => 'display:none;'],
  1062. ['input' => [
  1063. 'type' => 'hidden',
  1064. 'name' => '_Token[fields]',
  1065. 'autocomplete' => 'off',
  1066. 'value' => $hash,
  1067. ]],
  1068. ['input' => [
  1069. 'type' => 'hidden',
  1070. 'name' => '_Token[unlocked]',
  1071. 'autocomplete' => 'off',
  1072. 'value' => '',
  1073. ]],
  1074. '/div',
  1075. ];
  1076. $this->assertHtml($expected, $result);
  1077. }
  1078. /**
  1079. * Tests correct generation of number fields for smallint
  1080. *
  1081. * @return void
  1082. */
  1083. public function testTextFieldGenerationForSmallint()
  1084. {
  1085. $this->article['schema'] = [
  1086. 'foo' => [
  1087. 'type' => 'smallinteger',
  1088. 'null' => false,
  1089. 'default' => null,
  1090. 'length' => 10,
  1091. ],
  1092. ];
  1093. $this->Form->create($this->article);
  1094. $result = $this->Form->control('foo');
  1095. $this->assertStringContainsString('class="input number"', $result);
  1096. $this->assertStringContainsString('type="number"', $result);
  1097. }
  1098. /**
  1099. * Tests correct generation of number fields for tinyint
  1100. *
  1101. * @return void
  1102. */
  1103. public function testTextFieldGenerationForTinyint()
  1104. {
  1105. $this->article['schema'] = [
  1106. 'foo' => [
  1107. 'type' => 'tinyinteger',
  1108. 'null' => false,
  1109. 'default' => null,
  1110. 'length' => 10,
  1111. ],
  1112. ];
  1113. $this->Form->create($this->article);
  1114. $result = $this->Form->control('foo');
  1115. $this->assertStringContainsString('class="input number"', $result);
  1116. $this->assertStringContainsString('type="number"', $result);
  1117. }
  1118. /**
  1119. * Tests correct generation of number fields for double and float fields
  1120. *
  1121. * @return void
  1122. */
  1123. public function testTextFieldGenerationForFloats()
  1124. {
  1125. $this->article['schema'] = [
  1126. 'foo' => [
  1127. 'type' => 'float',
  1128. 'null' => false,
  1129. 'default' => null,
  1130. 'length' => 10,
  1131. ],
  1132. ];
  1133. $this->Form->create($this->article);
  1134. $result = $this->Form->control('foo');
  1135. $expected = [
  1136. 'div' => ['class' => 'input number'],
  1137. 'label' => ['for' => 'foo'],
  1138. 'Foo',
  1139. '/label',
  1140. ['input' => [
  1141. 'type' => 'number',
  1142. 'name' => 'foo',
  1143. 'id' => 'foo',
  1144. 'step' => 'any',
  1145. ]],
  1146. '/div',
  1147. ];
  1148. $this->assertHtml($expected, $result);
  1149. $result = $this->Form->control('foo', ['step' => 0.5]);
  1150. $expected = [
  1151. 'div' => ['class' => 'input number'],
  1152. 'label' => ['for' => 'foo'],
  1153. 'Foo',
  1154. '/label',
  1155. ['input' => [
  1156. 'type' => 'number',
  1157. 'name' => 'foo',
  1158. 'id' => 'foo',
  1159. 'step' => '0.5',
  1160. ]],
  1161. '/div',
  1162. ];
  1163. $this->assertHtml($expected, $result);
  1164. }
  1165. /**
  1166. * Tests correct generation of number fields for integer fields
  1167. *
  1168. * @return void
  1169. */
  1170. public function testTextFieldTypeNumberGenerationForIntegers()
  1171. {
  1172. $this->getTableLocator()->get('Contacts', [
  1173. 'className' => ContactsTable::class,
  1174. ]);
  1175. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1176. $result = $this->Form->control('age');
  1177. $expected = [
  1178. 'div' => ['class' => 'input number'],
  1179. 'label' => ['for' => 'age'],
  1180. 'Age',
  1181. '/label',
  1182. ['input' => [
  1183. 'type' => 'number', 'name' => 'age',
  1184. 'id' => 'age',
  1185. ]],
  1186. '/div',
  1187. ];
  1188. $this->assertHtml($expected, $result);
  1189. }
  1190. /**
  1191. * Tests correct generation of file upload fields for binary fields
  1192. *
  1193. * @return void
  1194. */
  1195. public function testFileUploadFieldTypeGenerationForBinaries()
  1196. {
  1197. $table = $this->getTableLocator()->get('Contacts', [
  1198. 'className' => ContactsTable::class,
  1199. ]);
  1200. $table->setSchema(['foo' => [
  1201. 'type' => 'binary',
  1202. 'null' => false,
  1203. 'default' => null,
  1204. 'length' => 1024,
  1205. ]]);
  1206. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1207. $result = $this->Form->control('foo');
  1208. $expected = [
  1209. 'div' => ['class' => 'input file'],
  1210. 'label' => ['for' => 'foo'],
  1211. 'Foo',
  1212. '/label',
  1213. ['input' => [
  1214. 'type' => 'file', 'name' => 'foo',
  1215. 'id' => 'foo',
  1216. ]],
  1217. '/div',
  1218. ];
  1219. $this->assertHtml($expected, $result);
  1220. }
  1221. /**
  1222. * testFormSecurityMultipleFields method
  1223. *
  1224. * Test secure() with multiple row form. Ensure hash is correct.
  1225. *
  1226. * @return void
  1227. */
  1228. public function testFormSecurityMultipleFields()
  1229. {
  1230. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1231. $this->Form->create();
  1232. $fields = [
  1233. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1234. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1235. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1236. ];
  1237. $result = $this->Form->secure($fields);
  1238. $sortedFields = [
  1239. 'Model.0.password',
  1240. 'Model.0.username',
  1241. 'Model.1.password',
  1242. 'Model.1.username',
  1243. 'Model.0.hidden' => 'value',
  1244. 'Model.0.valid' => '0',
  1245. 'Model.1.hidden' => 'value',
  1246. 'Model.1.valid' => '0',
  1247. ];
  1248. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1249. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1250. $hash = urlencode($hash);
  1251. $tokenDebug = urlencode(json_encode([
  1252. $this->url,
  1253. $fields,
  1254. [],
  1255. ]));
  1256. $expected = [
  1257. 'div' => ['style' => 'display:none;'],
  1258. ['input' => [
  1259. 'type' => 'hidden',
  1260. 'name' => '_Token[fields]',
  1261. 'value' => $hash,
  1262. 'autocomplete' => 'off',
  1263. ]],
  1264. ['input' => [
  1265. 'type' => 'hidden',
  1266. 'name' => '_Token[unlocked]',
  1267. 'autocomplete' => 'off',
  1268. 'value' => '',
  1269. ]],
  1270. ['input' => [
  1271. 'type' => 'hidden',
  1272. 'name' => '_Token[debug]',
  1273. 'value' => $tokenDebug,
  1274. 'autocomplete' => 'off',
  1275. ]],
  1276. '/div',
  1277. ];
  1278. $this->assertHtml($expected, $result);
  1279. }
  1280. /**
  1281. * testFormSecurityMultipleSubmitButtons
  1282. *
  1283. * test form submit generation and ensure that _Token is only created on end()
  1284. *
  1285. * @return void
  1286. */
  1287. public function testFormSecurityMultipleSubmitButtons()
  1288. {
  1289. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1290. $this->Form->create($this->article);
  1291. $this->Form->text('Address.title');
  1292. $this->Form->text('Address.first_name');
  1293. $result = $this->Form->submit('Save', ['name' => 'save']);
  1294. $expected = [
  1295. 'div' => ['class' => 'submit'],
  1296. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1297. '/div',
  1298. ];
  1299. $this->assertHtml($expected, $result);
  1300. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1301. $expected = [
  1302. 'div' => ['class' => 'submit'],
  1303. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1304. '/div',
  1305. ];
  1306. $this->assertHtml($expected, $result);
  1307. $result = $this->Form->end();
  1308. $tokenDebug = urlencode(json_encode([
  1309. '/articles/add',
  1310. [
  1311. 'Address.title',
  1312. 'Address.first_name',
  1313. ],
  1314. ['save', 'cancel'],
  1315. ]));
  1316. $expected = [
  1317. 'div' => ['style' => 'display:none;'],
  1318. ['input' => [
  1319. 'type' => 'hidden',
  1320. 'name' => '_Token[fields]',
  1321. 'autocomplete',
  1322. 'value',
  1323. ]],
  1324. ['input' => [
  1325. 'type' => 'hidden',
  1326. 'name' => '_Token[unlocked]',
  1327. 'value' => 'cancel%7Csave',
  1328. 'autocomplete' => 'off',
  1329. ]],
  1330. ['input' => [
  1331. 'type' => 'hidden',
  1332. 'name' => '_Token[debug]',
  1333. 'value' => $tokenDebug,
  1334. 'autocomplete' => 'off',
  1335. ]],
  1336. '/div',
  1337. ];
  1338. $this->assertHtml($expected, $result);
  1339. }
  1340. /**
  1341. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1342. *
  1343. * @return void
  1344. */
  1345. public function testSecurityButtonNestedNamed()
  1346. {
  1347. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1348. $this->Form->create();
  1349. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1350. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1351. $this->assertEquals(['Address.button'], $result);
  1352. }
  1353. /**
  1354. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1355. *
  1356. * @return void
  1357. */
  1358. public function testSecuritySubmitNestedNamed()
  1359. {
  1360. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1361. $this->Form->create($this->article);
  1362. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1363. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1364. $this->assertEquals(['Address.button'], $result);
  1365. }
  1366. /**
  1367. * Test that the correct fields are unlocked for image submits with no names.
  1368. *
  1369. * @return void
  1370. */
  1371. public function testSecuritySubmitImageNoName()
  1372. {
  1373. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1374. $this->Form->create();
  1375. $result = $this->Form->submit('save.png');
  1376. $expected = [
  1377. 'div' => ['class' => 'submit'],
  1378. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1379. '/div',
  1380. ];
  1381. $this->assertHtml($expected, $result);
  1382. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1383. $this->assertEquals(['x', 'y'], $result);
  1384. }
  1385. /**
  1386. * Test that the correct fields are unlocked for image submits with names.
  1387. *
  1388. * @return void
  1389. */
  1390. public function testSecuritySubmitImageName()
  1391. {
  1392. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1393. $this->Form->create();
  1394. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1395. $expected = [
  1396. 'div' => ['class' => 'submit'],
  1397. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1398. '/div',
  1399. ];
  1400. $this->assertHtml($expected, $result);
  1401. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1402. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1403. }
  1404. /**
  1405. * testFormSecurityMultipleControlFields method
  1406. *
  1407. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1408. *
  1409. * @return void
  1410. */
  1411. public function testFormSecurityMultipleControlFields()
  1412. {
  1413. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1414. $this->Form->create();
  1415. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1416. $this->Form->control('Addresses.0.title');
  1417. $this->Form->control('Addresses.0.first_name');
  1418. $this->Form->control('Addresses.0.last_name');
  1419. $this->Form->control('Addresses.0.address');
  1420. $this->Form->control('Addresses.0.city');
  1421. $this->Form->control('Addresses.0.phone');
  1422. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1423. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1424. $this->Form->control('Addresses.1.title');
  1425. $this->Form->control('Addresses.1.first_name');
  1426. $this->Form->control('Addresses.1.last_name');
  1427. $this->Form->control('Addresses.1.address');
  1428. $this->Form->control('Addresses.1.city');
  1429. $this->Form->control('Addresses.1.phone');
  1430. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1431. $result = $this->Form->secure();
  1432. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1433. $tokenDebug = urlencode(json_encode([
  1434. '/articles/add',
  1435. [
  1436. 'Addresses.0.id' => '123456',
  1437. 'Addresses.0.title',
  1438. 'Addresses.0.first_name',
  1439. 'Addresses.0.last_name',
  1440. 'Addresses.0.address',
  1441. 'Addresses.0.city',
  1442. 'Addresses.0.phone',
  1443. 'Addresses.0.primary',
  1444. 'Addresses.1.id' => '654321',
  1445. 'Addresses.1.title',
  1446. 'Addresses.1.first_name',
  1447. 'Addresses.1.last_name',
  1448. 'Addresses.1.address',
  1449. 'Addresses.1.city',
  1450. 'Addresses.1.phone',
  1451. 'Addresses.1.primary',
  1452. ],
  1453. [],
  1454. ]));
  1455. $expected = [
  1456. 'div' => ['style' => 'display:none;'],
  1457. ['input' => [
  1458. 'type' => 'hidden',
  1459. 'name' => '_Token[fields]',
  1460. 'value' => $hash,
  1461. 'autocomplete' => 'off',
  1462. ]],
  1463. ['input' => [
  1464. 'type' => 'hidden',
  1465. 'name' => '_Token[unlocked]',
  1466. 'autocomplete' => 'off',
  1467. 'value' => '',
  1468. ]],
  1469. ['input' => [
  1470. 'type' => 'hidden',
  1471. 'name' => '_Token[debug]',
  1472. 'value' => $tokenDebug,
  1473. 'autocomplete' => 'off',
  1474. ]],
  1475. '/div',
  1476. ];
  1477. $this->assertHtml($expected, $result);
  1478. }
  1479. /**
  1480. * testFormSecurityArrayFields method
  1481. *
  1482. * Test form security with Model.field.0 style inputs.
  1483. *
  1484. * @return void
  1485. */
  1486. public function testFormSecurityArrayFields()
  1487. {
  1488. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1489. $this->Form->create();
  1490. $this->Form->text('Address.primary.1');
  1491. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1492. $this->assertSame('Address.primary', $result[0]);
  1493. $this->Form->text('Address.secondary.1.0');
  1494. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1495. $this->assertSame('Address.secondary', $result[1]);
  1496. }
  1497. /**
  1498. * testFormSecurityMultipleControlDisabledFields method
  1499. *
  1500. * Test secure form generation with multiple records and disabled fields.
  1501. *
  1502. * @return void
  1503. */
  1504. public function testFormSecurityMultipleControlDisabledFields()
  1505. {
  1506. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1507. 'unlockedFields' => ['first_name', 'address'],
  1508. ]));
  1509. $this->Form->create();
  1510. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1511. $this->Form->text('Addresses.0.title');
  1512. $this->Form->text('Addresses.0.first_name');
  1513. $this->Form->text('Addresses.0.last_name');
  1514. $this->Form->text('Addresses.0.address');
  1515. $this->Form->text('Addresses.0.city');
  1516. $this->Form->text('Addresses.0.phone');
  1517. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1518. $this->Form->text('Addresses.1.title');
  1519. $this->Form->text('Addresses.1.first_name');
  1520. $this->Form->text('Addresses.1.last_name');
  1521. $this->Form->text('Addresses.1.address');
  1522. $this->Form->text('Addresses.1.city');
  1523. $this->Form->text('Addresses.1.phone');
  1524. $result = $this->Form->secure();
  1525. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1526. $tokenDebug = urlencode(json_encode([
  1527. '/articles/add',
  1528. [
  1529. 'Addresses.0.id' => '123456',
  1530. 'Addresses.0.title',
  1531. 'Addresses.0.last_name',
  1532. 'Addresses.0.city',
  1533. 'Addresses.0.phone',
  1534. 'Addresses.1.id' => '654321',
  1535. 'Addresses.1.title',
  1536. 'Addresses.1.last_name',
  1537. 'Addresses.1.city',
  1538. 'Addresses.1.phone',
  1539. ],
  1540. [
  1541. 'first_name',
  1542. 'address',
  1543. ],
  1544. ]));
  1545. $expected = [
  1546. 'div' => ['style' => 'display:none;'],
  1547. ['input' => [
  1548. 'type' => 'hidden',
  1549. 'name' => '_Token[fields]',
  1550. 'autocomplete' => 'off',
  1551. 'value' => $hash,
  1552. ]],
  1553. ['input' => [
  1554. 'type' => 'hidden',
  1555. 'name' => '_Token[unlocked]',
  1556. 'autocomplete' => 'off',
  1557. 'value' => 'address%7Cfirst_name',
  1558. ]],
  1559. ['input' => [
  1560. 'type' => 'hidden',
  1561. 'name' => '_Token[debug]',
  1562. 'autocomplete' => 'off',
  1563. 'value' => $tokenDebug,
  1564. ]],
  1565. '/div',
  1566. ];
  1567. $this->assertHtml($expected, $result);
  1568. }
  1569. /**
  1570. * testFormSecurityControlDisabledFields method
  1571. *
  1572. * Test single record form with disabled fields.
  1573. *
  1574. * @return void
  1575. */
  1576. public function testFormSecurityControlUnlockedFields()
  1577. {
  1578. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1579. 'unlockedFields' => ['first_name', 'address'],
  1580. ]));
  1581. $this->Form->create();
  1582. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1583. $this->assertEquals(
  1584. $this->View->getRequest()->getAttribute('formTokenData'),
  1585. ['unlockedFields' => $result]
  1586. );
  1587. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1588. $this->Form->text('Addresses.title');
  1589. $this->Form->text('Addresses.first_name');
  1590. $this->Form->text('Addresses.last_name');
  1591. $this->Form->text('Addresses.address');
  1592. $this->Form->text('Addresses.city');
  1593. $this->Form->text('Addresses.phone');
  1594. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1595. $expected = [
  1596. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1597. 'Addresses.city', 'Addresses.phone',
  1598. ];
  1599. $this->assertEquals($expected, $result);
  1600. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1601. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1602. $tokenDebug = urlencode(json_encode([
  1603. '/articles/add',
  1604. [
  1605. 'Addresses.id' => '123456',
  1606. 'Addresses.title',
  1607. 'Addresses.last_name',
  1608. 'Addresses.city',
  1609. 'Addresses.phone',
  1610. ],
  1611. [
  1612. 'first_name',
  1613. 'address',
  1614. ],
  1615. ]));
  1616. $expected = [
  1617. 'div' => ['style' => 'display:none;'],
  1618. ['input' => [
  1619. 'type' => 'hidden',
  1620. 'name' => '_Token[fields]',
  1621. 'value' => $hash,
  1622. 'autocomplete' => 'off',
  1623. 'data-foo' => 'bar',
  1624. ]],
  1625. ['input' => [
  1626. 'type' => 'hidden',
  1627. 'name' => '_Token[unlocked]',
  1628. 'value' => 'address%7Cfirst_name',
  1629. 'autocomplete' => 'off',
  1630. 'data-foo' => 'bar',
  1631. ]],
  1632. ['input' => [
  1633. 'type' => 'hidden', 'name' => '_Token[debug]',
  1634. 'value' => $tokenDebug,
  1635. 'autocomplete' => 'off',
  1636. 'data-foo' => 'bar',
  1637. ]],
  1638. '/div',
  1639. ];
  1640. $this->assertHtml($expected, $result);
  1641. }
  1642. /**
  1643. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1644. *
  1645. * Test single record form with debugSecurity param.
  1646. *
  1647. * @return void
  1648. */
  1649. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1650. {
  1651. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1652. 'unlockedFields' => ['first_name', 'address'],
  1653. ]));
  1654. $this->Form->create();
  1655. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1656. $this->assertEquals(
  1657. $this->View->getRequest()->getAttribute('formTokenData'),
  1658. ['unlockedFields' => $result]
  1659. );
  1660. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1661. $this->Form->text('Addresses.title');
  1662. $this->Form->text('Addresses.first_name');
  1663. $this->Form->text('Addresses.last_name');
  1664. $this->Form->text('Addresses.address');
  1665. $this->Form->text('Addresses.city');
  1666. $this->Form->text('Addresses.phone');
  1667. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1668. $expected = [
  1669. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1670. 'Addresses.city', 'Addresses.phone',
  1671. ];
  1672. $this->assertEquals($expected, $result);
  1673. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1674. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1675. $tokenDebug = urlencode(json_encode([
  1676. '/articles/add',
  1677. [
  1678. 'Addresses.id' => '123456',
  1679. 'Addresses.title',
  1680. 'Addresses.last_name',
  1681. 'Addresses.city',
  1682. 'Addresses.phone',
  1683. ],
  1684. [
  1685. 'first_name',
  1686. 'address',
  1687. ],
  1688. ]));
  1689. $expected = [
  1690. 'div' => ['style' => 'display:none;'],
  1691. ['input' => [
  1692. 'type' => 'hidden',
  1693. 'name' => '_Token[fields]',
  1694. 'value' => $hash,
  1695. 'autocomplete' => 'off',
  1696. 'data-foo' => 'bar',
  1697. ]],
  1698. ['input' => [
  1699. 'type' => 'hidden',
  1700. 'name' => '_Token[unlocked]',
  1701. 'value' => 'address%7Cfirst_name',
  1702. 'autocomplete' => 'off',
  1703. 'data-foo' => 'bar',
  1704. ]],
  1705. ['input' => [
  1706. 'type' => 'hidden', 'name' => '_Token[debug]',
  1707. 'value' => $tokenDebug,
  1708. 'autocomplete' => 'off',
  1709. 'data-foo' => 'bar',
  1710. ]],
  1711. '/div',
  1712. ];
  1713. $this->assertHtml($expected, $result);
  1714. }
  1715. /**
  1716. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1717. *
  1718. * Debug is false, debugSecurity is true -> no debug
  1719. *
  1720. * @return void
  1721. */
  1722. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1723. {
  1724. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1725. 'unlockedFields' => ['first_name', 'address'],
  1726. ]));
  1727. $this->Form->create();
  1728. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1729. $this->assertEquals(
  1730. $this->View->getRequest()->getAttribute('formTokenData'),
  1731. ['unlockedFields' => $result]
  1732. );
  1733. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1734. $this->Form->text('Addresses.title');
  1735. $this->Form->text('Addresses.first_name');
  1736. $this->Form->text('Addresses.last_name');
  1737. $this->Form->text('Addresses.address');
  1738. $this->Form->text('Addresses.city');
  1739. $this->Form->text('Addresses.phone');
  1740. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1741. $expected = [
  1742. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1743. 'Addresses.city', 'Addresses.phone',
  1744. ];
  1745. $this->assertEquals($expected, $result);
  1746. Configure::write('debug', false);
  1747. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1748. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1749. $expected = [
  1750. 'div' => ['style' => 'display:none;'],
  1751. ['input' => [
  1752. 'type' => 'hidden',
  1753. 'name' => '_Token[fields]',
  1754. 'value' => $hash,
  1755. 'autocomplete' => 'off',
  1756. 'data-foo' => 'bar',
  1757. ]],
  1758. ['input' => [
  1759. 'type' => 'hidden',
  1760. 'name' => '_Token[unlocked]',
  1761. 'value' => 'address%7Cfirst_name',
  1762. 'autocomplete' => 'off',
  1763. 'data-foo' => 'bar',
  1764. ]],
  1765. '/div',
  1766. ];
  1767. $this->assertHtml($expected, $result);
  1768. }
  1769. /**
  1770. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1771. *
  1772. * Test single record form with debugSecurity param.
  1773. *
  1774. * @return void
  1775. */
  1776. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1777. {
  1778. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1779. 'unlockedFields' => ['first_name', 'address'],
  1780. ]));
  1781. $this->Form->create();
  1782. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1783. $this->assertEquals(
  1784. $this->View->getRequest()->getAttribute('formTokenData'),
  1785. ['unlockedFields' => $result]
  1786. );
  1787. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1788. $this->Form->text('Addresses.title');
  1789. $this->Form->text('Addresses.first_name');
  1790. $this->Form->text('Addresses.last_name');
  1791. $this->Form->text('Addresses.address');
  1792. $this->Form->text('Addresses.city');
  1793. $this->Form->text('Addresses.phone');
  1794. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1795. $expected = [
  1796. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1797. 'Addresses.city', 'Addresses.phone',
  1798. ];
  1799. $this->assertEquals($expected, $result);
  1800. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1801. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1802. $expected = [
  1803. 'div' => ['style' => 'display:none;'],
  1804. ['input' => [
  1805. 'type' => 'hidden',
  1806. 'name' => '_Token[fields]',
  1807. 'value' => $hash,
  1808. 'autocomplete' => 'off',
  1809. 'data-foo' => 'bar',
  1810. ]],
  1811. ['input' => [
  1812. 'type' => 'hidden',
  1813. 'name' => '_Token[unlocked]',
  1814. 'value' => 'address%7Cfirst_name',
  1815. 'autocomplete' => 'off',
  1816. 'data-foo' => 'bar',
  1817. ]],
  1818. '/div',
  1819. ];
  1820. $this->assertHtml($expected, $result);
  1821. }
  1822. /**
  1823. * testFormSecureWithCustomNameAttribute method
  1824. *
  1825. * Test securing inputs with custom name attributes.
  1826. *
  1827. * @return void
  1828. */
  1829. public function testFormSecureWithCustomNameAttribute()
  1830. {
  1831. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1832. $this->Form->create();
  1833. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1834. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1835. $this->assertSame('User.custom', $result[0]);
  1836. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1837. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1838. $this->assertSame('User.custom.another.value', $result[1]);
  1839. }
  1840. /**
  1841. * testFormSecuredControl method
  1842. *
  1843. * Test generation of entire secure form, assertions made on control() output.
  1844. *
  1845. * @return void
  1846. */
  1847. public function testFormSecuredControl()
  1848. {
  1849. $this->View->setRequest($this->View->getRequest()
  1850. ->withAttribute('formTokenData', [])
  1851. ->withAttribute('csrfToken', 'testKey'));
  1852. $this->article['schema'] = [
  1853. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1854. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1855. ];
  1856. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1857. $encoding = strtolower(Configure::read('App.encoding'));
  1858. $expected = [
  1859. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1860. 'div' => ['style' => 'display:none;'],
  1861. ['input' => [
  1862. 'type' => 'hidden',
  1863. 'name' => '_csrfToken',
  1864. 'value' => 'testKey',
  1865. 'autocomplete' => 'off',
  1866. ]],
  1867. '/div',
  1868. ];
  1869. $this->assertHtml($expected, $result);
  1870. $result = $this->Form->control('ratio');
  1871. $expected = [
  1872. 'div' => ['class'],
  1873. 'label' => ['for'],
  1874. 'Ratio',
  1875. '/label',
  1876. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1877. '/div',
  1878. ];
  1879. $this->assertHtml($expected, $result);
  1880. $result = $this->Form->control('population');
  1881. $expected = [
  1882. 'div' => ['class'],
  1883. 'label' => ['for'],
  1884. 'Population',
  1885. '/label',
  1886. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1887. '/div',
  1888. ];
  1889. $this->assertHtml($expected, $result);
  1890. $result = $this->Form->control('published', ['type' => 'text']);
  1891. $expected = [
  1892. 'div' => ['class' => 'input text'],
  1893. 'label' => ['for' => 'published'],
  1894. 'Published',
  1895. '/label',
  1896. ['input' => [
  1897. 'type' => 'text',
  1898. 'name' => 'published',
  1899. 'id' => 'published',
  1900. ]],
  1901. '/div',
  1902. ];
  1903. $this->assertHtml($expected, $result);
  1904. $result = $this->Form->control('other', ['type' => 'text']);
  1905. $expected = [
  1906. 'div' => ['class' => 'input text'],
  1907. 'label' => ['for' => 'other'],
  1908. 'Other',
  1909. '/label',
  1910. ['input' => [
  1911. 'type' => 'text',
  1912. 'name' => 'other',
  1913. 'id',
  1914. ]],
  1915. '/div',
  1916. ];
  1917. $this->assertHtml($expected, $result);
  1918. $result = $this->Form->hidden('stuff');
  1919. $expected = [
  1920. 'input' => [
  1921. 'type' => 'hidden',
  1922. 'name' => 'stuff',
  1923. ],
  1924. ];
  1925. $this->assertHtml($expected, $result);
  1926. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1927. $expected = ['input' => [
  1928. 'type' => 'hidden',
  1929. 'name' => 'hidden',
  1930. 'value' => '0',
  1931. ]];
  1932. $this->assertHtml($expected, $result);
  1933. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1934. $expected = [
  1935. 'div' => ['class' => 'input checkbox'],
  1936. ['input' => [
  1937. 'type' => 'hidden',
  1938. 'name' => 'something',
  1939. 'value' => '0',
  1940. ]],
  1941. 'label' => ['for' => 'something'],
  1942. ['input' => [
  1943. 'type' => 'checkbox',
  1944. 'name' => 'something',
  1945. 'value' => '1',
  1946. 'id' => 'something',
  1947. ]],
  1948. 'Something',
  1949. '/label',
  1950. '/div',
  1951. ];
  1952. $this->assertHtml($expected, $result);
  1953. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1954. $expectedFields = [
  1955. 'ratio',
  1956. 'population',
  1957. 'published',
  1958. 'other',
  1959. 'stuff' => '',
  1960. 'hidden' => '0',
  1961. 'something',
  1962. ];
  1963. $this->assertEquals($expectedFields, $result);
  1964. $result = $this->Form->secure();
  1965. $tokenDebug = urlencode(json_encode([
  1966. '/articles/add',
  1967. $expectedFields,
  1968. [],
  1969. ]));
  1970. $expected = [
  1971. 'div' => ['style' => 'display:none;'],
  1972. ['input' => [
  1973. 'type' => 'hidden',
  1974. 'name' => '_Token[fields]',
  1975. 'value',
  1976. 'autocomplete',
  1977. ]],
  1978. ['input' => [
  1979. 'type' => 'hidden',
  1980. 'name' => '_Token[unlocked]',
  1981. 'value' => '',
  1982. 'autocomplete' => 'off',
  1983. ]],
  1984. ['input' => [
  1985. 'type' => 'hidden', 'name' => '_Token[debug]',
  1986. 'value' => $tokenDebug,
  1987. 'autocomplete' => 'off',
  1988. ]],
  1989. '/div',
  1990. ];
  1991. $this->assertHtml($expected, $result);
  1992. }
  1993. /**
  1994. * testSecuredControlCustomName method
  1995. *
  1996. * Test secured inputs with custom names.
  1997. *
  1998. * @return void
  1999. */
  2000. public function testSecuredControlCustomName()
  2001. {
  2002. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2003. $this->Form->create();
  2004. $this->Form->text('text_input', [
  2005. 'name' => 'Option[General.default_role]',
  2006. ]);
  2007. $expected = ['Option.General.default_role'];
  2008. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2009. $this->assertEquals($expected, $result);
  2010. $this->Form->select('select_box', [1, 2], [
  2011. 'name' => 'Option[General.select_role]',
  2012. ]);
  2013. $expected[] = 'Option.General.select_role';
  2014. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2015. $this->assertEquals($expected, $result);
  2016. $this->Form->text('other.things[]');
  2017. $expected[] = 'other.things';
  2018. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2019. $this->assertEquals($expected, $result);
  2020. }
  2021. /**
  2022. * testSecuredControlDuplicate method
  2023. *
  2024. * Test that a hidden field followed by a visible field
  2025. * undoes the hidden field locking.
  2026. *
  2027. * @return void
  2028. */
  2029. public function testSecuredControlDuplicate()
  2030. {
  2031. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2032. $this->Form->create();
  2033. $this->Form->control('text_val', [
  2034. 'type' => 'hidden',
  2035. 'value' => 'some text',
  2036. ]);
  2037. $expected = ['text_val' => 'some text'];
  2038. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2039. $this->assertEquals($expected, $result);
  2040. $this->Form->control('text_val', [
  2041. 'type' => 'text',
  2042. ]);
  2043. $expected = ['text_val'];
  2044. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2045. $this->assertEquals($expected, $result);
  2046. }
  2047. /**
  2048. * testFormSecuredFileControl method
  2049. *
  2050. * Tests that the correct keys are added to the field hash index.
  2051. *
  2052. * @return void
  2053. */
  2054. public function testFormSecuredFileControl()
  2055. {
  2056. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2057. $this->Form->create();
  2058. $this->Form->file('Attachment.file');
  2059. $expected = ['Attachment.file'];
  2060. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2061. $this->assertEquals($expected, $result);
  2062. }
  2063. /**
  2064. * testFormSecuredMultipleSelect method
  2065. *
  2066. * Test that multiple selects keys are added to field hash.
  2067. *
  2068. * @return void
  2069. */
  2070. public function testFormSecuredMultipleSelect()
  2071. {
  2072. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2073. $this->Form->create();
  2074. $options = ['1' => 'one', '2' => 'two'];
  2075. $this->Form->select('Model.select', $options);
  2076. $expected = ['Model.select'];
  2077. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2078. $this->assertEquals($expected, $result);
  2079. $this->Form->fields = [];
  2080. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2081. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2082. $this->assertEquals($expected, $result);
  2083. }
  2084. /**
  2085. * testFormSecuredRadio method
  2086. *
  2087. * @return void
  2088. */
  2089. public function testFormSecuredRadio()
  2090. {
  2091. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2092. $this->Form->create();
  2093. $options = ['1' => 'option1', '2' => 'option2'];
  2094. $this->Form->radio('Test.test', $options);
  2095. $expected = ['Test.test'];
  2096. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2097. $this->assertEquals($expected, $result);
  2098. $this->Form->radio('Test.all', $options, [
  2099. 'disabled' => ['option1', 'option2'],
  2100. ]);
  2101. $expected = ['Test.test', 'Test.all' => ''];
  2102. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2103. $this->assertEquals($expected, $result);
  2104. $this->Form->radio('Test.some', $options, [
  2105. 'disabled' => ['option1'],
  2106. ]);
  2107. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2108. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2109. $this->assertEquals($expected, $result);
  2110. }
  2111. /**
  2112. * testFormSecuredAndDisabledNotAssoc method
  2113. *
  2114. * Test that when disabled is in a list based attribute array it works.
  2115. *
  2116. * @return void
  2117. */
  2118. public function testFormSecuredAndDisabledNotAssoc()
  2119. {
  2120. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2121. $this->Form->create();
  2122. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2123. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2124. $this->Form->text('Model.text', ['disabled']);
  2125. $this->Form->textarea('Model.textarea', ['disabled']);
  2126. $this->Form->password('Model.password', ['disabled']);
  2127. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2128. $expected = [
  2129. 'Model.radio' => '',
  2130. ];
  2131. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2132. $this->assertEquals($expected, $result);
  2133. }
  2134. /**
  2135. * testFormSecuredAndDisabled method
  2136. *
  2137. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2138. * hashing.
  2139. *
  2140. * @return void
  2141. */
  2142. public function testFormSecuredAndDisabled()
  2143. {
  2144. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2145. $this->Form->create();
  2146. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2147. $this->Form->text('Model.text', ['disabled' => true]);
  2148. $this->Form->password('Model.text', ['disabled' => true]);
  2149. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2150. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2151. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2152. $this->Form->year('Model.year', ['disabled' => true]);
  2153. $this->Form->month('Model.month', ['disabled' => true]);
  2154. $this->Form->day('Model.day', ['disabled' => true]);
  2155. $this->Form->hour('Model.hour', ['disabled' => true]);
  2156. $this->Form->minute('Model.minute', ['disabled' => true]);
  2157. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2158. $expected = [
  2159. 'Model.radio' => '',
  2160. ];
  2161. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2162. $this->assertEquals($expected, $result);
  2163. }
  2164. /**
  2165. * testUnlockFieldAddsToList method
  2166. *
  2167. * Test disableField.
  2168. *
  2169. * @return void
  2170. */
  2171. public function testUnlockFieldAddsToList()
  2172. {
  2173. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2174. 'unlockedFields' => [],
  2175. ]));
  2176. $this->Form->create();
  2177. $this->Form->unlockField('Contact.name');
  2178. $this->Form->text('Contact.name');
  2179. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2180. $this->assertEquals([], $result);
  2181. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2182. $this->assertEquals(['Contact.name'], $result);
  2183. }
  2184. /**
  2185. * testUnlockFieldRemovingFromFields method
  2186. *
  2187. * Test unlockField removing from fields array.
  2188. *
  2189. * @return void
  2190. */
  2191. public function testUnlockFieldRemovingFromFields()
  2192. {
  2193. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2194. 'unlockedFields' => [],
  2195. ]));
  2196. $this->Form->create($this->article);
  2197. $this->Form->hidden('Article.id', ['value' => 1]);
  2198. $this->Form->text('Article.title');
  2199. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2200. $this->assertEquals(1, $result['Article.id'], 'Hidden input should be secured.');
  2201. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2202. $this->Form->unlockField('Article.title');
  2203. $this->Form->unlockField('Article.id');
  2204. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2205. $this->assertEquals([], $result);
  2206. }
  2207. /**
  2208. * testResetUnlockFields method
  2209. *
  2210. * Test reset unlockFields, when create new form.
  2211. *
  2212. * @return void
  2213. */
  2214. public function testResetUnlockFields()
  2215. {
  2216. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2217. 'key' => 'testKey',
  2218. 'unlockedFields' => [],
  2219. ]));
  2220. $this->Form->create();
  2221. $this->Form->unlockField('Contact.id');
  2222. $this->Form->hidden('Contact.id', ['value' => 1]);
  2223. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2224. $this->assertEmpty($result, 'Field should be unlocked');
  2225. $this->Form->end();
  2226. $this->Form->create();
  2227. $this->Form->hidden('Contact.id', ['value' => 1]);
  2228. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2229. $this->assertEquals(1, $result['Contact.id'], 'Hidden input should be secured.');
  2230. }
  2231. /**
  2232. * testSecuredFormUrlIgnoresHost method
  2233. *
  2234. * Test that only the path + query elements of a form's URL show up in their hash.
  2235. *
  2236. * @return void
  2237. */
  2238. public function testSecuredFormUrlIgnoresHost()
  2239. {
  2240. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2241. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2242. $this->Form->create($this->article, [
  2243. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2244. ]);
  2245. $result = $this->Form->secure();
  2246. $this->assertStringContainsString($expected, $result);
  2247. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2248. $result = $this->Form->secure();
  2249. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2250. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2251. $result = $this->Form->secure();
  2252. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2253. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2254. $result = $this->Form->secure();
  2255. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2256. }
  2257. /**
  2258. * testSecuredFormUrlHasHtmlAndIdentifier method
  2259. *
  2260. * Test that URL, HTML and identifier show up in their hashes.
  2261. *
  2262. * @return void
  2263. */
  2264. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2265. {
  2266. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2267. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2268. $this->Form->create($this->article, [
  2269. 'url' => [
  2270. 'controller' => 'articles',
  2271. 'action' => 'view',
  2272. '?' => [
  2273. 'page' => 1,
  2274. 'limit' => 10,
  2275. 'html' => '<>"',
  2276. ],
  2277. '#' => 'result',
  2278. ],
  2279. ]);
  2280. $result = $this->Form->secure();
  2281. $this->assertStringContainsString($expected, $result);
  2282. $this->Form->create($this->article, [
  2283. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2284. ]);
  2285. $result = $this->Form->secure();
  2286. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2287. $this->Form->create($this->article, [
  2288. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2289. ]);
  2290. $result = $this->Form->secure();
  2291. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2292. }
  2293. /**
  2294. * testErrorMessageDisplay method
  2295. *
  2296. * Test error message display.
  2297. *
  2298. * @return void
  2299. */
  2300. public function testErrorMessageDisplay()
  2301. {
  2302. $this->article['errors'] = [
  2303. 'Article' => [
  2304. 'title' => 'error message',
  2305. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2306. ],
  2307. ];
  2308. $this->Form->create($this->article);
  2309. $result = $this->Form->control('Article.title');
  2310. $expected = [
  2311. 'div' => ['class' => 'input text error'],
  2312. 'label' => ['for' => 'article-title'],
  2313. 'Title',
  2314. '/label',
  2315. 'input' => [
  2316. 'type' => 'text', 'name' => 'Article[title]',
  2317. 'id' => 'article-title', 'class' => 'form-error',
  2318. ],
  2319. ['div' => ['class' => 'error-message']],
  2320. 'error message',
  2321. '/div',
  2322. '/div',
  2323. ];
  2324. $this->assertHtml($expected, $result);
  2325. $result = $this->Form->control('Article.title', [
  2326. 'templates' => [
  2327. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2328. ],
  2329. ]);
  2330. $expected = [
  2331. 'div' => ['class' => 'input text error'],
  2332. 'label' => ['for' => 'article-title'],
  2333. 'Title',
  2334. '/label',
  2335. 'input' => [
  2336. 'type' => 'text', 'name' => 'Article[title]',
  2337. 'id' => 'article-title', 'class' => 'form-error',
  2338. ],
  2339. '/div',
  2340. ];
  2341. $this->assertHtml($expected, $result);
  2342. $result = $this->Form->control('Article.content');
  2343. $expected = [
  2344. 'div' => ['class' => 'input text error'],
  2345. 'label' => ['for' => 'article-content'],
  2346. 'Content',
  2347. '/label',
  2348. 'input' => [
  2349. 'type' => 'text', 'name' => 'Article[content]',
  2350. 'id' => 'article-content', 'class' => 'form-error',
  2351. ],
  2352. ['div' => ['class' => 'error-message']],
  2353. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2354. '/div',
  2355. '/div',
  2356. ];
  2357. $this->assertHtml($expected, $result);
  2358. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2359. $expected = [
  2360. 'div' => ['class' => 'input text error'],
  2361. 'label' => ['for' => 'article-content'],
  2362. 'Content',
  2363. '/label',
  2364. 'input' => [
  2365. 'type' => 'text', 'name' => 'Article[content]',
  2366. 'id' => 'article-content', 'class' => 'form-error',
  2367. ],
  2368. ['div' => ['class' => 'error-message']],
  2369. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2370. '/div',
  2371. '/div',
  2372. ];
  2373. $this->assertHtml($expected, $result);
  2374. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2375. $expected = [
  2376. 'div' => ['class' => 'input text error'],
  2377. 'label' => ['for' => 'article-content'],
  2378. 'Content',
  2379. '/label',
  2380. 'input' => [
  2381. 'type' => 'text', 'name' => 'Article[content]',
  2382. 'id' => 'article-content', 'class' => 'form-error',
  2383. ],
  2384. ['div' => ['class' => 'error-message']],
  2385. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2386. '/div',
  2387. '/div',
  2388. ];
  2389. $this->assertHtml($expected, $result);
  2390. }
  2391. /**
  2392. * testEmptyErrorValidation method
  2393. *
  2394. * Test validation errors, when validation message is an empty string.
  2395. *
  2396. * @return void
  2397. */
  2398. public function testEmptyErrorValidation()
  2399. {
  2400. $this->article['errors'] = [
  2401. 'Article' => ['title' => ''],
  2402. ];
  2403. $this->Form->create($this->article);
  2404. $result = $this->Form->control('Article.title');
  2405. $expected = [
  2406. 'div' => ['class' => 'input text error'],
  2407. 'label' => ['for' => 'article-title'],
  2408. 'Title',
  2409. '/label',
  2410. 'input' => [
  2411. 'type' => 'text', 'name' => 'Article[title]',
  2412. 'id' => 'article-title', 'class' => 'form-error',
  2413. ],
  2414. ['div' => ['class' => 'error-message']],
  2415. [],
  2416. '/div',
  2417. '/div',
  2418. ];
  2419. $this->assertHtml($expected, $result);
  2420. }
  2421. /**
  2422. * testEmptyControlErrorValidation method
  2423. *
  2424. * Test validation errors, when calling control() overriding validation message by an empty string.
  2425. *
  2426. * @return void
  2427. */
  2428. public function testEmptyControlErrorValidation()
  2429. {
  2430. $this->article['errors'] = [
  2431. 'Article' => ['title' => 'error message'],
  2432. ];
  2433. $this->Form->create($this->article);
  2434. $result = $this->Form->control('Article.title', ['error' => '']);
  2435. $expected = [
  2436. 'div' => ['class' => 'input text error'],
  2437. 'label' => ['for' => 'article-title'],
  2438. 'Title',
  2439. '/label',
  2440. 'input' => [
  2441. 'type' => 'text', 'name' => 'Article[title]',
  2442. 'id' => 'article-title', 'class' => 'form-error',
  2443. ],
  2444. ['div' => ['class' => 'error-message']],
  2445. [],
  2446. '/div',
  2447. '/div',
  2448. ];
  2449. $this->assertHtml($expected, $result);
  2450. }
  2451. /**
  2452. * testControlErrorMessage method
  2453. *
  2454. * Test validation errors, when calling control() overriding validation messages.
  2455. *
  2456. * @return void
  2457. */
  2458. public function testControlErrorMessage()
  2459. {
  2460. $this->article['errors'] = [
  2461. 'title' => ['error message'],
  2462. ];
  2463. $this->Form->create($this->article);
  2464. $result = $this->Form->control('title', [
  2465. 'error' => 'Custom error!',
  2466. ]);
  2467. $expected = [
  2468. 'div' => ['class' => 'input text required error'],
  2469. 'label' => ['for' => 'title'],
  2470. 'Title',
  2471. '/label',
  2472. 'input' => [
  2473. 'type' => 'text', 'name' => 'title',
  2474. 'id' => 'title', 'class' => 'form-error',
  2475. 'required' => 'required',
  2476. 'data-validity-message' => 'This field cannot be left empty',
  2477. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2478. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2479. ],
  2480. ['div' => ['class' => 'error-message']],
  2481. 'Custom error!',
  2482. '/div',
  2483. '/div',
  2484. ];
  2485. $this->assertHtml($expected, $result);
  2486. $result = $this->Form->control('title', [
  2487. 'error' => ['error message' => 'Custom error!'],
  2488. ]);
  2489. $expected = [
  2490. 'div' => ['class' => 'input text required error'],
  2491. 'label' => ['for' => 'title'],
  2492. 'Title',
  2493. '/label',
  2494. 'input' => [
  2495. 'type' => 'text',
  2496. 'name' => 'title',
  2497. 'id' => 'title',
  2498. 'class' => 'form-error',
  2499. 'required' => 'required',
  2500. 'data-validity-message' => 'This field cannot be left empty',
  2501. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2502. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2503. ],
  2504. ['div' => ['class' => 'error-message']],
  2505. 'Custom error!',
  2506. '/div',
  2507. '/div',
  2508. ];
  2509. $this->assertHtml($expected, $result);
  2510. }
  2511. /**
  2512. * testFormValidationAssociated method
  2513. *
  2514. * Tests displaying errors for nested entities.
  2515. *
  2516. * @return void
  2517. */
  2518. public function testFormValidationAssociated()
  2519. {
  2520. $nested = new Entity(['foo' => 'bar']);
  2521. $nested->setError('foo', ['not a valid bar']);
  2522. $entity = new Entity(['nested' => $nested]);
  2523. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2524. $result = $this->Form->error('nested.foo');
  2525. $this->assertSame('<div class="error-message">not a valid bar</div>', $result);
  2526. }
  2527. /**
  2528. * testFormValidationAssociatedSecondLevel method
  2529. *
  2530. * Test form error display with associated model.
  2531. *
  2532. * @return void
  2533. */
  2534. public function testFormValidationAssociatedSecondLevel()
  2535. {
  2536. $inner = new Entity(['bar' => 'baz']);
  2537. $nested = new Entity(['foo' => $inner]);
  2538. $entity = new Entity(['nested' => $nested]);
  2539. $inner->setError('bar', ['not a valid one']);
  2540. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2541. $result = $this->Form->error('nested.foo.bar');
  2542. $this->assertSame('<div class="error-message">not a valid one</div>', $result);
  2543. }
  2544. /**
  2545. * testFormValidationMultiRecord method
  2546. *
  2547. * Test form error display with multiple records.
  2548. *
  2549. * @return void
  2550. */
  2551. public function testFormValidationMultiRecord()
  2552. {
  2553. $one = new Entity();
  2554. $two = new Entity();
  2555. $this->getTableLocator()->get('Contacts', [
  2556. 'className' => ContactsTable::class,
  2557. ]);
  2558. $one->set('email', '');
  2559. $one->setError('email', ['invalid email']);
  2560. $two->set('name', '');
  2561. $two->setError('name', ['This is wrong']);
  2562. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2563. $result = $this->Form->control('0.email');
  2564. $expected = [
  2565. 'div' => ['class' => 'input email error'],
  2566. 'label' => ['for' => '0-email'],
  2567. 'Email',
  2568. '/label',
  2569. 'input' => [
  2570. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2571. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2572. ],
  2573. ['div' => ['class' => 'error-message']],
  2574. 'invalid email',
  2575. '/div',
  2576. '/div',
  2577. ];
  2578. $this->assertHtml($expected, $result);
  2579. $result = $this->Form->control('1.name');
  2580. $expected = [
  2581. 'div' => ['class' => 'input text error'],
  2582. 'label' => ['for' => '1-name'],
  2583. 'Name',
  2584. '/label',
  2585. 'input' => [
  2586. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2587. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2588. ],
  2589. ['div' => ['class' => 'error-message']],
  2590. 'This is wrong',
  2591. '/div',
  2592. '/div',
  2593. ];
  2594. $this->assertHtml($expected, $result);
  2595. }
  2596. /**
  2597. * testControl method
  2598. *
  2599. * Test various incarnations of control().
  2600. *
  2601. * @return void
  2602. */
  2603. public function testControl()
  2604. {
  2605. $this->getTableLocator()->get('ValidateUsers', [
  2606. 'className' => ValidateUsersTable::class,
  2607. ]);
  2608. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2609. $result = $this->Form->control('ValidateUsers.balance');
  2610. $expected = [
  2611. 'div' => ['class'],
  2612. 'label' => ['for'],
  2613. 'Balance',
  2614. '/label',
  2615. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2616. '/div',
  2617. ];
  2618. $this->assertHtml($expected, $result);
  2619. $result = $this->Form->control('ValidateUser.cost_decimal');
  2620. $expected = [
  2621. 'div' => ['class'],
  2622. 'label' => ['for'],
  2623. 'Cost Decimal',
  2624. '/label',
  2625. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2626. '/div',
  2627. ];
  2628. $this->assertHtml($expected, $result);
  2629. $result = $this->Form->control('ValidateUser.null_decimal');
  2630. $expected = [
  2631. 'div' => ['class'],
  2632. 'label' => ['for'],
  2633. 'Null Decimal',
  2634. '/label',
  2635. 'input' => ['name', 'type' => 'number', 'id'],
  2636. '/div',
  2637. ];
  2638. $this->assertHtml($expected, $result);
  2639. }
  2640. /**
  2641. * testControlCustomization method
  2642. *
  2643. * Tests the input method and passing custom options.
  2644. *
  2645. * @return void
  2646. */
  2647. public function testControlCustomization()
  2648. {
  2649. $this->getTableLocator()->get('Contacts', [
  2650. 'className' => ContactsTable::class,
  2651. ]);
  2652. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2653. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2654. $expected = [
  2655. 'div' => ['class' => 'input email'],
  2656. 'label' => ['for' => 'custom'],
  2657. 'Email',
  2658. '/label',
  2659. ['input' => [
  2660. 'type' => 'email', 'name' => 'Contact[email]',
  2661. 'id' => 'custom', 'maxlength' => 255,
  2662. ]],
  2663. '/div',
  2664. ];
  2665. $this->assertHtml($expected, $result);
  2666. $result = $this->Form->control('Contact.email', [
  2667. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2668. ]);
  2669. $expected = [
  2670. '<div',
  2671. 'label' => ['for' => 'contact-email'],
  2672. 'Email',
  2673. '/label',
  2674. ['input' => [
  2675. 'type' => 'email', 'name' => 'Contact[email]',
  2676. 'id' => 'contact-email', 'maxlength' => 255,
  2677. ]],
  2678. '/div',
  2679. ];
  2680. $this->assertHtml($expected, $result);
  2681. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2682. $expected = [
  2683. 'div' => ['class' => 'input text'],
  2684. 'label' => ['for' => 'contact-email'],
  2685. 'Email',
  2686. '/label',
  2687. ['input' => [
  2688. 'type' => 'text', 'name' => 'Contact[email]',
  2689. 'id' => 'contact-email', 'maxlength' => '255',
  2690. ]],
  2691. '/div',
  2692. ];
  2693. $this->assertHtml($expected, $result);
  2694. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2695. $expected = [
  2696. 'div' => ['class' => 'input text'],
  2697. 'label' => ['for' => 'contact-5-email'],
  2698. 'Email',
  2699. '/label',
  2700. ['input' => [
  2701. 'type' => 'text', 'name' => 'Contact[5][email]',
  2702. 'id' => 'contact-5-email', 'maxlength' => '255',
  2703. ]],
  2704. '/div',
  2705. ];
  2706. $this->assertHtml($expected, $result);
  2707. $result = $this->Form->control('Contact.password');
  2708. $expected = [
  2709. 'div' => ['class' => 'input password'],
  2710. 'label' => ['for' => 'contact-password'],
  2711. 'Password',
  2712. '/label',
  2713. ['input' => [
  2714. 'type' => 'password', 'name' => 'Contact[password]',
  2715. 'id' => 'contact-password',
  2716. ]],
  2717. '/div',
  2718. ];
  2719. $this->assertHtml($expected, $result);
  2720. $result = $this->Form->control('Contact.email', [
  2721. 'type' => 'file', 'class' => 'textbox',
  2722. ]);
  2723. $expected = [
  2724. 'div' => ['class' => 'input file'],
  2725. 'label' => ['for' => 'contact-email'],
  2726. 'Email',
  2727. '/label',
  2728. ['input' => [
  2729. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2730. 'id' => 'contact-email',
  2731. ]],
  2732. '/div',
  2733. ];
  2734. $this->assertHtml($expected, $result);
  2735. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2736. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2737. $result = $this->Form->control('phone');
  2738. $expected = [
  2739. 'div' => ['class' => 'input tel'],
  2740. 'label' => ['for' => 'phone'],
  2741. 'Phone',
  2742. '/label',
  2743. ['input' => [
  2744. 'type' => 'tel', 'name' => 'phone',
  2745. 'value' => 'Hello &amp; World &gt; weird chars',
  2746. 'id' => 'phone', 'maxlength' => 255,
  2747. ]],
  2748. '/div',
  2749. ];
  2750. $this->assertHtml($expected, $result);
  2751. $this->View->setRequest(
  2752. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2753. );
  2754. $this->Form->create();
  2755. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2756. $expected = [
  2757. 'div' => ['class' => 'input text'],
  2758. 'label' => ['for' => 'myId'],
  2759. 'Field',
  2760. '/label',
  2761. 'input' => [
  2762. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2763. 'value' => 'My value', 'id' => 'myId',
  2764. ],
  2765. '/div',
  2766. ];
  2767. $this->assertHtml($expected, $result);
  2768. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2769. $this->Form->create();
  2770. $entity->setError('field', 'Badness!');
  2771. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2772. $result = $this->Form->control('field');
  2773. $expected = [
  2774. 'div' => ['class' => 'input text error'],
  2775. 'label' => ['for' => 'field'],
  2776. 'Field',
  2777. '/label',
  2778. 'input' => [
  2779. 'type' => 'text', 'name' => 'field',
  2780. 'id' => 'field', 'class' => 'form-error',
  2781. ],
  2782. ['div' => ['class' => 'error-message']],
  2783. 'Badness!',
  2784. '/div',
  2785. '/div',
  2786. ];
  2787. $this->assertHtml($expected, $result);
  2788. $result = $this->Form->control('field', [
  2789. 'templates' => [
  2790. 'inputContainerError' => '{{content}}{{error}}',
  2791. 'error' => '<span class="error-message">{{content}}</span>',
  2792. ],
  2793. ]);
  2794. $expected = [
  2795. 'label' => ['for' => 'field'],
  2796. 'Field',
  2797. '/label',
  2798. 'input' => [
  2799. 'type' => 'text', 'name' => 'field',
  2800. 'id' => 'field', 'class' => 'form-error',
  2801. ],
  2802. ['span' => ['class' => 'error-message']],
  2803. 'Badness!',
  2804. '/span',
  2805. ];
  2806. $this->assertHtml($expected, $result);
  2807. $entity->setError('field', ['minLength'], true);
  2808. $result = $this->Form->control('field', [
  2809. 'error' => [
  2810. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2811. 'maxLength' => 'login too large',
  2812. ],
  2813. ]);
  2814. $expected = [
  2815. 'div' => ['class' => 'input text error'],
  2816. 'label' => ['for' => 'field'],
  2817. 'Field',
  2818. '/label',
  2819. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2820. ['div' => ['class' => 'error-message']],
  2821. 'Le login doit contenir au moins 2 caractères',
  2822. '/div',
  2823. '/div',
  2824. ];
  2825. $this->assertHtml($expected, $result);
  2826. $entity->setError('field', ['maxLength'], true);
  2827. $result = $this->Form->control('field', [
  2828. 'error' => [
  2829. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2830. 'maxLength' => 'login too large',
  2831. ],
  2832. ]);
  2833. $expected = [
  2834. 'div' => ['class' => 'input text error'],
  2835. 'label' => ['for' => 'field'],
  2836. 'Field',
  2837. '/label',
  2838. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2839. ['div' => ['class' => 'error-message']],
  2840. 'login too large',
  2841. '/div',
  2842. '/div',
  2843. ];
  2844. $this->assertHtml($expected, $result);
  2845. }
  2846. /**
  2847. * testControlWithTemplateFile method
  2848. *
  2849. * Test that control() accepts a template file.
  2850. *
  2851. * @return void
  2852. */
  2853. public function testControlWithTemplateFile()
  2854. {
  2855. $result = $this->Form->control('field', [
  2856. 'templates' => 'htmlhelper_tags',
  2857. ]);
  2858. $expected = [
  2859. 'label' => ['for' => 'field'],
  2860. 'Field',
  2861. '/label',
  2862. 'input' => [
  2863. 'type' => 'text', 'name' => 'field',
  2864. 'id' => 'field',
  2865. ],
  2866. ];
  2867. $this->assertHtml($expected, $result);
  2868. }
  2869. /**
  2870. * testNestedControlsEndWithBrackets method
  2871. *
  2872. * Test that nested inputs end with brackets.
  2873. *
  2874. * @return void
  2875. */
  2876. public function testNestedControlsEndWithBrackets()
  2877. {
  2878. $result = $this->Form->text('nested.text[]');
  2879. $expected = [
  2880. 'input' => [
  2881. 'type' => 'text', 'name' => 'nested[text][]',
  2882. ],
  2883. ];
  2884. $this->assertHtml($expected, $result);
  2885. $result = $this->Form->file('nested.file[]');
  2886. $expected = [
  2887. 'input' => [
  2888. 'type' => 'file', 'name' => 'nested[file][]',
  2889. ],
  2890. ];
  2891. $this->assertHtml($expected, $result);
  2892. }
  2893. /**
  2894. * testCreateIdPrefix method
  2895. *
  2896. * Test id prefix.
  2897. *
  2898. * @return void
  2899. */
  2900. public function testCreateIdPrefix()
  2901. {
  2902. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2903. $result = $this->Form->control('field');
  2904. $expected = [
  2905. 'div' => ['class' => 'input text'],
  2906. 'label' => ['for' => 'prefix-field'],
  2907. 'Field',
  2908. '/label',
  2909. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2910. '/div',
  2911. ];
  2912. $this->assertHtml($expected, $result);
  2913. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2914. $expected = [
  2915. 'div' => ['class' => 'input text'],
  2916. 'label' => ['for' => 'custom-id'],
  2917. 'Field',
  2918. '/label',
  2919. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2920. '/div',
  2921. ];
  2922. $this->assertHtml($expected, $result);
  2923. $result = $this->Form->radio('Model.field', ['option A']);
  2924. $expected = [
  2925. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2926. 'label' => ['for' => 'prefix-model-field-0'],
  2927. ['input' => [
  2928. 'type' => 'radio',
  2929. 'name' => 'Model[field]',
  2930. 'value' => '0',
  2931. 'id' => 'prefix-model-field-0',
  2932. ]],
  2933. 'option A',
  2934. '/label',
  2935. ];
  2936. $this->assertHtml($expected, $result);
  2937. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2938. $expected = [
  2939. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2940. 'label' => ['for' => 'prefix-model-field-0'],
  2941. ['input' => [
  2942. 'type' => 'radio',
  2943. 'name' => 'Model[field]',
  2944. 'value' => '0',
  2945. 'id' => 'prefix-model-field-0',
  2946. ]],
  2947. 'option A',
  2948. '/label',
  2949. ];
  2950. $this->assertHtml($expected, $result);
  2951. $result = $this->Form->select(
  2952. 'Model.multi_field',
  2953. ['first'],
  2954. ['multiple' => 'checkbox']
  2955. );
  2956. $expected = [
  2957. 'input' => [
  2958. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  2959. ],
  2960. ['div' => ['class' => 'checkbox']],
  2961. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2962. ['input' => [
  2963. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2964. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2965. ]],
  2966. 'first',
  2967. '/label',
  2968. '/div',
  2969. ];
  2970. $this->assertHtml($expected, $result);
  2971. $this->Form->end();
  2972. $result = $this->Form->control('field');
  2973. $expected = [
  2974. 'div' => ['class' => 'input text'],
  2975. 'label' => ['for' => 'field'],
  2976. 'Field',
  2977. '/label',
  2978. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2979. '/div',
  2980. ];
  2981. $this->assertHtml($expected, $result);
  2982. }
  2983. /**
  2984. * testControlZero method
  2985. *
  2986. * Test that inputs with 0 can be created.
  2987. *
  2988. * @return void
  2989. */
  2990. public function testControlZero()
  2991. {
  2992. $this->getTableLocator()->get('Contacts', [
  2993. 'className' => ContactsTable::class,
  2994. ]);
  2995. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2996. $result = $this->Form->control('0');
  2997. $expected = [
  2998. 'div' => ['class' => 'input text'],
  2999. 'label' => ['for' => '0'], '/label',
  3000. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3001. '/div',
  3002. ];
  3003. $this->assertHtml($expected, $result);
  3004. }
  3005. /**
  3006. * testControlCheckbox method
  3007. *
  3008. * Test control() with checkbox creation.
  3009. *
  3010. * @return void
  3011. */
  3012. public function testControlCheckbox()
  3013. {
  3014. $articles = $this->getTableLocator()->get('Articles');
  3015. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3016. $article = $articles->newEmptyEntity();
  3017. $this->Form->create($article);
  3018. $result = $this->Form->control('Articles.active');
  3019. $expected = [
  3020. 'div' => ['class' => 'input checkbox'],
  3021. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3022. 'label' => ['for' => 'articles-active'],
  3023. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3024. 'Active',
  3025. '/label',
  3026. '/div',
  3027. ];
  3028. $this->assertHtml($expected, $result);
  3029. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3030. $expected = [
  3031. 'div' => ['class' => 'input checkbox'],
  3032. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3033. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3034. '/div',
  3035. ];
  3036. $this->assertHtml($expected, $result);
  3037. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3038. $expected = [
  3039. 'div' => ['class' => 'input checkbox'],
  3040. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3041. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3042. '/div',
  3043. ];
  3044. $this->assertHtml($expected, $result);
  3045. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3046. $expected = [
  3047. 'div' => ['class' => 'input checkbox'],
  3048. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3049. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3050. '/div',
  3051. ];
  3052. $this->assertHtml($expected, $result);
  3053. $result = $this->Form->control('Articles.disabled', [
  3054. 'label' => 'Disabled',
  3055. 'type' => 'checkbox',
  3056. 'data-foo' => 'disabled',
  3057. ]);
  3058. $expected = [
  3059. 'div' => ['class' => 'input checkbox'],
  3060. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3061. 'label' => ['for' => 'articles-disabled'],
  3062. ['input' => [
  3063. 'type' => 'checkbox',
  3064. 'name' => 'Articles[disabled]',
  3065. 'value' => '1',
  3066. 'id' => 'articles-disabled',
  3067. 'data-foo' => 'disabled',
  3068. ]],
  3069. 'Disabled',
  3070. '/label',
  3071. '/div',
  3072. ];
  3073. $this->assertHtml($expected, $result);
  3074. $result = $this->Form->control('Articles.confirm', [
  3075. 'label' => 'Confirm <b>me</b>!',
  3076. 'type' => 'checkbox',
  3077. 'escape' => false,
  3078. ]);
  3079. $expected = [
  3080. 'div' => ['class' => 'input checkbox'],
  3081. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3082. 'label' => ['for' => 'articles-confirm'],
  3083. ['input' => [
  3084. 'type' => 'checkbox',
  3085. 'name' => 'Articles[confirm]',
  3086. 'value' => '1',
  3087. 'id' => 'articles-confirm',
  3088. ]],
  3089. 'Confirm <b>me</b>!',
  3090. '/label',
  3091. '/div',
  3092. ];
  3093. $this->assertHtml($expected, $result);
  3094. }
  3095. /**
  3096. * testControlHidden method
  3097. *
  3098. * Test that control() does not create wrapping div and label tag for hidden fields.
  3099. *
  3100. * @return void
  3101. */
  3102. public function testControlHidden()
  3103. {
  3104. $this->getTableLocator()->get('ValidateUsers', [
  3105. 'className' => ValidateUsersTable::class,
  3106. ]);
  3107. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3108. $result = $this->Form->control('ValidateUser.id');
  3109. $expected = [
  3110. 'input' => ['name', 'type' => 'hidden', 'id'],
  3111. ];
  3112. $this->assertHtml($expected, $result);
  3113. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3114. $expected = [
  3115. 'input' => ['name', 'type' => 'hidden', 'id'],
  3116. ];
  3117. $this->assertHtml($expected, $result);
  3118. }
  3119. /**
  3120. * testControlDatetime method
  3121. *
  3122. * Test form->control() with datetime.
  3123. *
  3124. * @return void
  3125. */
  3126. public function testControlDatetime()
  3127. {
  3128. $result = $this->Form->control('prueba', [
  3129. 'type' => 'datetime',
  3130. 'value' => new FrozenTime('2019-09-27 02:52:43'),
  3131. ]);
  3132. $expected = [
  3133. 'div' => ['class' => 'input datetime'],
  3134. 'label' => ['for' => 'prueba'],
  3135. 'Prueba',
  3136. '/label',
  3137. 'input' => [
  3138. 'name' => 'prueba',
  3139. 'id' => 'prueba',
  3140. 'type' => 'datetime-local',
  3141. 'value' => '2019-09-27T02:52:43',
  3142. 'step' => '1',
  3143. ],
  3144. '/div',
  3145. ];
  3146. $this->assertHtml($expected, $result);
  3147. }
  3148. /**
  3149. * testControlDatetimeIdPrefix method
  3150. *
  3151. * Test form->control() with datetime with id prefix.
  3152. *
  3153. * @return void
  3154. */
  3155. public function testControlDatetimeIdPrefix()
  3156. {
  3157. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3158. $result = $this->Form->control('prueba', [
  3159. 'type' => 'datetime',
  3160. ]);
  3161. $expected = [
  3162. 'div' => ['class' => 'input datetime'],
  3163. 'label' => ['for' => 'prefix-prueba'],
  3164. 'Prueba',
  3165. '/label',
  3166. 'input' => [
  3167. 'name' => 'prueba',
  3168. 'id' => 'prefix-prueba',
  3169. 'type' => 'datetime-local',
  3170. 'value' => '',
  3171. 'step' => '1',
  3172. ],
  3173. '/div',
  3174. ];
  3175. $this->assertHtml($expected, $result);
  3176. }
  3177. /**
  3178. * testControlDatetimeStep method
  3179. *
  3180. * Test form->control() with datetime with custom step size.
  3181. *
  3182. * @return void
  3183. */
  3184. public function testControlDatetimeStep()
  3185. {
  3186. $result = $this->Form->control('prueba', [
  3187. 'type' => 'datetime',
  3188. 'value' => new FrozenTime('2019-09-27 02:52:43'),
  3189. 'step' => '0.5',
  3190. ]);
  3191. $expected = [
  3192. 'div' => ['class' => 'input datetime'],
  3193. 'label' => ['for' => 'prueba'],
  3194. 'Prueba',
  3195. '/label',
  3196. 'input' => [
  3197. 'name' => 'prueba',
  3198. 'id' => 'prueba',
  3199. 'type' => 'datetime-local',
  3200. 'value' => '2019-09-27T02:52:43.000',
  3201. 'step' => '0.5',
  3202. ],
  3203. '/div',
  3204. ];
  3205. $this->assertHtml($expected, $result);
  3206. }
  3207. /**
  3208. * testControlCheckboxWithDisabledElements method
  3209. *
  3210. * Test generating checkboxes with disabled elements.
  3211. *
  3212. * @return void
  3213. */
  3214. public function testControlCheckboxWithDisabledElements()
  3215. {
  3216. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3217. $result = $this->Form->control('Contact.multiple', [
  3218. 'multiple' => 'checkbox',
  3219. 'disabled' => 'disabled',
  3220. 'options' => $options,
  3221. ]);
  3222. $expected = [
  3223. ['div' => ['class' => 'input select']],
  3224. ['label' => ['for' => 'contact-multiple']],
  3225. 'Multiple',
  3226. '/label',
  3227. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3228. ['div' => ['class' => 'checkbox']],
  3229. ['label' => ['for' => 'contact-multiple-1']],
  3230. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3231. 'One',
  3232. '/label',
  3233. '/div',
  3234. ['div' => ['class' => 'checkbox']],
  3235. ['label' => ['for' => 'contact-multiple-2']],
  3236. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3237. 'Two',
  3238. '/label',
  3239. '/div',
  3240. ['div' => ['class' => 'checkbox']],
  3241. ['label' => ['for' => 'contact-multiple-3']],
  3242. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3243. 'Three',
  3244. '/label',
  3245. '/div',
  3246. '/div',
  3247. ];
  3248. $this->assertHtml($expected, $result);
  3249. // make sure 50 does only disable 50, and not 50f5c0cf
  3250. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3251. $disabled = [50];
  3252. $expected = [
  3253. ['div' => ['class' => 'input select']],
  3254. ['label' => ['for' => 'contact-multiple']],
  3255. 'Multiple',
  3256. '/label',
  3257. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3258. ['div' => ['class' => 'checkbox']],
  3259. ['label' => ['for' => 'contact-multiple-50']],
  3260. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3261. 'Fifty',
  3262. '/label',
  3263. '/div',
  3264. ['div' => ['class' => 'checkbox']],
  3265. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3266. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3267. 'Stringy',
  3268. '/label',
  3269. '/div',
  3270. '/div',
  3271. ];
  3272. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3273. $this->assertHtml($expected, $result);
  3274. }
  3275. /**
  3276. * testControlWithLeadingInteger method
  3277. *
  3278. * Test input name with leading integer, ensure attributes are generated correctly.
  3279. *
  3280. * @return void
  3281. */
  3282. public function testControlWithLeadingInteger()
  3283. {
  3284. $result = $this->Form->text('0.Node.title');
  3285. $expected = [
  3286. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3287. ];
  3288. $this->assertHtml($expected, $result);
  3289. }
  3290. /**
  3291. * testControlSelectType method
  3292. *
  3293. * Test form->control() with select type inputs.
  3294. *
  3295. * @return void
  3296. */
  3297. public function testControlSelectType()
  3298. {
  3299. $result = $this->Form->control(
  3300. 'email',
  3301. [
  3302. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3303. );
  3304. $expected = [
  3305. 'div' => ['class' => 'input select'],
  3306. 'label' => ['for' => 'email'],
  3307. 'Email',
  3308. '/label',
  3309. ['select' => ['name' => 'email', 'id' => 'email']],
  3310. ['option' => ['value' => '']],
  3311. '/option',
  3312. ['option' => ['value' => 'è']],
  3313. 'Firést',
  3314. '/option',
  3315. ['option' => ['value' => 'é']],
  3316. 'Secoènd',
  3317. '/option',
  3318. '/select',
  3319. '/div',
  3320. ];
  3321. $this->assertHtml($expected, $result);
  3322. $result = $this->Form->control(
  3323. 'email',
  3324. [
  3325. 'options' => ['First', 'Second'], 'empty' => true]
  3326. );
  3327. $expected = [
  3328. 'div' => ['class' => 'input select'],
  3329. 'label' => ['for' => 'email'],
  3330. 'Email',
  3331. '/label',
  3332. ['select' => ['name' => 'email', 'id' => 'email']],
  3333. ['option' => ['value' => '']],
  3334. '/option',
  3335. ['option' => ['value' => '0']],
  3336. 'First',
  3337. '/option',
  3338. ['option' => ['value' => '1']],
  3339. 'Second',
  3340. '/option',
  3341. '/select',
  3342. '/div',
  3343. ];
  3344. $this->assertHtml($expected, $result);
  3345. $result = $this->Form->control('email', [
  3346. 'type' => 'select',
  3347. 'options' => new \ArrayObject(['First', 'Second']),
  3348. 'empty' => true,
  3349. ]);
  3350. $this->assertHtml($expected, $result);
  3351. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3352. $this->View->setRequest(
  3353. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3354. );
  3355. $this->Form->create();
  3356. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3357. $expected = [
  3358. 'div' => ['class' => 'input select'],
  3359. 'label' => ['for' => 'model-user-id'],
  3360. 'User',
  3361. '/label',
  3362. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3363. ['option' => ['value' => '']],
  3364. '/option',
  3365. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3366. 'good',
  3367. '/option',
  3368. ['option' => ['value' => 'other']],
  3369. 'bad',
  3370. '/option',
  3371. '/select',
  3372. '/div',
  3373. ];
  3374. $this->assertHtml($expected, $result);
  3375. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3376. $this->View->setRequest(
  3377. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3378. );
  3379. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3380. $expected = [
  3381. 'div' => ['class' => 'input select'],
  3382. 'label' => ['for' => 'thing-user-id'],
  3383. 'User',
  3384. '/label',
  3385. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3386. ['option' => ['value' => '']],
  3387. 'Some Empty',
  3388. '/option',
  3389. ['option' => ['value' => 'value']],
  3390. 'good',
  3391. '/option',
  3392. ['option' => ['value' => 'other']],
  3393. 'bad',
  3394. '/option',
  3395. '/select',
  3396. '/div',
  3397. ];
  3398. $this->assertHtml($expected, $result);
  3399. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3400. $this->View->setRequest(
  3401. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3402. );
  3403. $this->Form->create();
  3404. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3405. $expected = [
  3406. 'div' => ['class' => 'input select'],
  3407. 'label' => ['for' => 'thing-user-id'],
  3408. 'User',
  3409. '/label',
  3410. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3411. ['option' => ['value' => '']],
  3412. 'Some Empty',
  3413. '/option',
  3414. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3415. 'good',
  3416. '/option',
  3417. ['option' => ['value' => 'other']],
  3418. 'bad',
  3419. '/option',
  3420. '/select',
  3421. '/div',
  3422. ];
  3423. $this->assertHtml($expected, $result);
  3424. $result = $this->Form->control('Publisher.id', [
  3425. 'label' => 'Publisher',
  3426. 'type' => 'select',
  3427. 'multiple' => 'checkbox',
  3428. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3429. ]);
  3430. $expected = [
  3431. ['div' => ['class' => 'input select']],
  3432. ['label' => ['for' => 'publisher-id']],
  3433. 'Publisher',
  3434. '/label',
  3435. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3436. ['div' => ['class' => 'checkbox']],
  3437. ['label' => ['for' => 'publisher-id-value-1']],
  3438. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3439. 'Label 1',
  3440. '/label',
  3441. '/div',
  3442. ['div' => ['class' => 'checkbox']],
  3443. ['label' => ['for' => 'publisher-id-value-2']],
  3444. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3445. 'Label 2',
  3446. '/label',
  3447. '/div',
  3448. '/div',
  3449. ];
  3450. $this->assertHtml($expected, $result);
  3451. }
  3452. /**
  3453. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3454. *
  3455. * Test that control() and a non standard primary key makes a hidden input by default.
  3456. *
  3457. * @return void
  3458. */
  3459. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3460. {
  3461. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3462. $this->Form->create($this->article);
  3463. $result = $this->Form->control('title');
  3464. $expected = [
  3465. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3466. ];
  3467. $this->assertHtml($expected, $result);
  3468. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3469. $this->Form->create($this->article);
  3470. $result = $this->Form->control('title');
  3471. $expected = [
  3472. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3473. ];
  3474. $this->assertHtml($expected, $result);
  3475. $result = $this->Form->control('body');
  3476. $expected = [
  3477. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3478. ];
  3479. $this->assertHtml($expected, $result);
  3480. }
  3481. /**
  3482. * testControlOverridingMagicSelectType method
  3483. *
  3484. * Test that overriding the magic select type widget is possible.
  3485. *
  3486. * @return void
  3487. */
  3488. public function testControlOverridingMagicSelectType()
  3489. {
  3490. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3491. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3492. $expected = [
  3493. 'div' => ['class' => 'input text'],
  3494. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3495. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3496. '/div',
  3497. ];
  3498. $this->assertHtml($expected, $result);
  3499. //Check that magic types still work for plural/singular vars
  3500. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3501. $result = $this->Form->control('Model.type');
  3502. $expected = [
  3503. 'div' => ['class' => 'input select'],
  3504. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3505. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3506. ['option' => ['value' => 'value']], 'good', '/option',
  3507. ['option' => ['value' => 'other']], 'bad', '/option',
  3508. '/select',
  3509. '/div',
  3510. ];
  3511. $this->assertHtml($expected, $result);
  3512. }
  3513. /**
  3514. * testControlMagicTypeDoesNotOverride method
  3515. *
  3516. * Test that inferred types do not override developer input.
  3517. *
  3518. * @return void
  3519. */
  3520. public function testControlMagicTypeDoesNotOverride()
  3521. {
  3522. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3523. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3524. $expected = [
  3525. 'div' => ['class' => 'input checkbox'],
  3526. ['input' => [
  3527. 'type' => 'hidden',
  3528. 'name' => 'Model[user]',
  3529. 'value' => 0,
  3530. ]],
  3531. 'label' => ['for' => 'model-user'],
  3532. ['input' => [
  3533. 'name' => 'Model[user]',
  3534. 'type' => 'checkbox',
  3535. 'id' => 'model-user',
  3536. 'value' => 1,
  3537. ]],
  3538. 'User',
  3539. '/label',
  3540. '/div',
  3541. ];
  3542. $this->assertHtml($expected, $result);
  3543. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3544. $options = [
  3545. 1 => 'blue',
  3546. 2 => 'red',
  3547. ];
  3548. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3549. $expected = [
  3550. 'div' => ['class' => 'input select'],
  3551. 'label' => ['for' => 'tags-ids'],
  3552. 'Tags',
  3553. '/label',
  3554. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3555. ['div' => ['class' => 'checkbox']],
  3556. ['label' => ['for' => 'tags-ids-1']],
  3557. ['input' => [
  3558. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3559. 'value' => '1', 'name' => 'tags[_ids][]',
  3560. ]],
  3561. 'blue',
  3562. '/label',
  3563. '/div',
  3564. ['div' => ['class' => 'checkbox']],
  3565. ['label' => ['for' => 'tags-ids-2']],
  3566. ['input' => [
  3567. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3568. 'value' => '2', 'name' => 'tags[_ids][]',
  3569. ]],
  3570. 'red',
  3571. '/label',
  3572. '/div',
  3573. '/div',
  3574. ];
  3575. $this->assertHtml($expected, $result);
  3576. }
  3577. /**
  3578. * testControlMagicSelectForTypeNumber method
  3579. *
  3580. * Test that magic control() selects are created for type=number.
  3581. *
  3582. * @return void
  3583. */
  3584. public function testControlMagicSelectForTypeNumber()
  3585. {
  3586. $this->getTableLocator()->get('ValidateUsers', [
  3587. 'className' => ValidateUsersTable::class,
  3588. ]);
  3589. $entity = new Entity(['balance' => 1]);
  3590. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3591. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3592. $result = $this->Form->control('balance');
  3593. $expected = [
  3594. 'div' => ['class' => 'input select'],
  3595. 'label' => ['for' => 'balance'],
  3596. 'Balance',
  3597. '/label',
  3598. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3599. ['option' => ['value' => '0']],
  3600. 'nothing',
  3601. '/option',
  3602. ['option' => ['value' => '1', 'selected' => 'selected']],
  3603. 'some',
  3604. '/option',
  3605. ['option' => ['value' => '100']],
  3606. 'a lot',
  3607. '/option',
  3608. '/select',
  3609. '/div',
  3610. ];
  3611. $this->assertHtml($expected, $result);
  3612. }
  3613. /**
  3614. * testInvalidControlTypeOption method
  3615. *
  3616. * Test invalid 'input' type option to control() function.
  3617. *
  3618. * @return void
  3619. */
  3620. public function testInvalidControlTypeOption()
  3621. {
  3622. $this->expectException(\RuntimeException::class);
  3623. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3624. $this->Form->control('text', ['type' => 'input']);
  3625. }
  3626. /**
  3627. * testControlMagicSelectChangeToRadio method
  3628. *
  3629. * Test that magic control() selects can easily be converted into radio types without error.
  3630. *
  3631. * @return void
  3632. */
  3633. public function testControlMagicSelectChangeToRadio()
  3634. {
  3635. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3636. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3637. $this->assertStringContainsString('input type="radio"', $result);
  3638. }
  3639. /**
  3640. * testFormControlSubmit method
  3641. *
  3642. * Test correct results for form::control() and type submit.
  3643. *
  3644. * @return void
  3645. */
  3646. public function testFormControlSubmit()
  3647. {
  3648. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3649. $expected = [
  3650. 'div' => ['class' => 'submit'],
  3651. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3652. '/div',
  3653. ];
  3654. $this->assertHtml($expected, $result);
  3655. }
  3656. /**
  3657. * testFormControls method
  3658. *
  3659. * Test correct results from Form::controls().
  3660. *
  3661. * @return void
  3662. */
  3663. public function testFormControlsLegendFieldset()
  3664. {
  3665. $this->Form->create($this->article);
  3666. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3667. $expected = [
  3668. '<fieldset',
  3669. '<legend',
  3670. 'The Legend',
  3671. '/legend',
  3672. '*/fieldset',
  3673. ];
  3674. $this->assertHtml($expected, $result);
  3675. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3676. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3677. $this->assertStringContainsString('<fieldset>', $result);
  3678. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3679. $this->assertStringNotContainsString('<legend>', $result);
  3680. $this->assertStringNotContainsString('<fieldset>', $result);
  3681. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3682. $this->assertStringNotContainsString('<legend>', $result);
  3683. $this->assertStringNotContainsString('<fieldset>', $result);
  3684. $this->Form->create($this->article);
  3685. $this->View->setRequest($this->View->getRequest()
  3686. ->withParam('prefix', 'admin')
  3687. ->withParam('action', 'admin_edit')
  3688. ->withParam('controller', 'articles'));
  3689. $result = $this->Form->allControls();
  3690. $expected = [
  3691. '<fieldset',
  3692. '<legend',
  3693. 'New Article',
  3694. '/legend',
  3695. '*/fieldset',
  3696. ];
  3697. $this->assertHtml($expected, $result);
  3698. $this->Form->create($this->article);
  3699. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3700. $expected = [
  3701. '<fieldset',
  3702. '<legend',
  3703. 'The Legend',
  3704. '/legend',
  3705. '*/fieldset',
  3706. ];
  3707. $this->assertHtml($expected, $result);
  3708. $this->Form->create($this->article);
  3709. $result = $this->Form->allControls([], [
  3710. 'fieldset' => [
  3711. 'class' => 'some-class some-other-class',
  3712. 'disabled' => true,
  3713. 'data-param' => 'a-param',
  3714. ],
  3715. 'legend' => 'The Legend',
  3716. ]);
  3717. $expected = [
  3718. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3719. '<legend',
  3720. 'The Legend',
  3721. '/legend',
  3722. '*/fieldset',
  3723. ];
  3724. $this->assertHtml($expected, $result);
  3725. }
  3726. /**
  3727. * testFormControls method
  3728. *
  3729. * Test the controls() method.
  3730. *
  3731. * @return void
  3732. */
  3733. public function testFormControls()
  3734. {
  3735. $this->Form->create($this->article);
  3736. $result = $this->Form->allControls();
  3737. $expected = [
  3738. '<fieldset',
  3739. '<legend', 'New Article', '/legend',
  3740. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3741. ['div' => ['class' => 'input select required']],
  3742. '*/div',
  3743. ['div' => ['class' => 'input text required']],
  3744. '*/div',
  3745. ['div' => ['class' => 'input text']],
  3746. '*/div',
  3747. ['div' => ['class' => 'input text']],
  3748. '*/div',
  3749. '/fieldset',
  3750. ];
  3751. $this->assertHtml($expected, $result);
  3752. $result = $this->Form->allControls([
  3753. 'published' => ['type' => 'boolean'],
  3754. ]);
  3755. $expected = [
  3756. '<fieldset',
  3757. '<legend', 'New Article', '/legend',
  3758. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3759. ['div' => ['class' => 'input select required']],
  3760. '*/div',
  3761. ['div' => ['class' => 'input text required']],
  3762. '*/div',
  3763. ['div' => ['class' => 'input text']],
  3764. '*/div',
  3765. ['div' => ['class' => 'input boolean']],
  3766. '*/div',
  3767. '/fieldset',
  3768. ];
  3769. $this->assertHtml($expected, $result);
  3770. $this->Form->create($this->article);
  3771. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3772. $expected = [
  3773. 'fieldset' => [],
  3774. 'legend' => [],
  3775. 'Hello',
  3776. '/legend',
  3777. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3778. ['div' => ['class' => 'input select required']],
  3779. '*/div',
  3780. ['div' => ['class' => 'input text required']],
  3781. '*/div',
  3782. ['div' => ['class' => 'input text']],
  3783. '*/div',
  3784. ['div' => ['class' => 'input text']],
  3785. '*/div',
  3786. '/fieldset',
  3787. ];
  3788. $this->assertHtml($expected, $result);
  3789. $this->Form->create();
  3790. $expected = [
  3791. 'fieldset' => [],
  3792. ['div' => ['class' => 'input text']],
  3793. 'label' => ['for' => 'foo'],
  3794. 'Foo',
  3795. '/label',
  3796. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3797. '*/div',
  3798. '/fieldset',
  3799. ];
  3800. $result = $this->Form->allControls(
  3801. ['foo' => ['type' => 'text']],
  3802. ['legend' => false]
  3803. );
  3804. $this->assertHtml($expected, $result);
  3805. }
  3806. /**
  3807. * testFormControlsBlacklist method
  3808. *
  3809. * @return void
  3810. */
  3811. public function testFormControlsBlacklist()
  3812. {
  3813. $this->Form->create($this->article);
  3814. $result = $this->Form->allControls([
  3815. 'id' => false,
  3816. ]);
  3817. $expected = [
  3818. '<fieldset',
  3819. '<legend', 'New Article', '/legend',
  3820. ['div' => ['class' => 'input select required']],
  3821. '*/div',
  3822. ['div' => ['class' => 'input text required']],
  3823. '*/div',
  3824. ['div' => ['class' => 'input text']],
  3825. '*/div',
  3826. ['div' => ['class' => 'input text']],
  3827. '*/div',
  3828. '/fieldset',
  3829. ];
  3830. $this->assertHtml($expected, $result);
  3831. $this->Form->create($this->article);
  3832. $result = $this->Form->allControls([
  3833. 'id' => [],
  3834. ]);
  3835. $expected = [
  3836. '<fieldset',
  3837. '<legend', 'New Article', '/legend',
  3838. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3839. ['div' => ['class' => 'input select required']],
  3840. '*/div',
  3841. ['div' => ['class' => 'input text required']],
  3842. '*/div',
  3843. ['div' => ['class' => 'input text']],
  3844. '*/div',
  3845. ['div' => ['class' => 'input text']],
  3846. '*/div',
  3847. '/fieldset',
  3848. ];
  3849. $this->assertHtml($expected, $result, true);
  3850. }
  3851. /**
  3852. * testSelectAsCheckbox method
  3853. *
  3854. * Test multi-select widget with checkbox formatting.
  3855. *
  3856. * @return void
  3857. */
  3858. public function testSelectAsCheckbox()
  3859. {
  3860. $result = $this->Form->select(
  3861. 'Model.multi_field',
  3862. ['first', 'second', 'third'],
  3863. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3864. );
  3865. $expected = [
  3866. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3867. ['div' => ['class' => 'checkbox']],
  3868. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3869. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3870. 'first',
  3871. '/label',
  3872. '/div',
  3873. ['div' => ['class' => 'checkbox']],
  3874. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3875. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3876. 'second',
  3877. '/label',
  3878. '/div',
  3879. ['div' => ['class' => 'checkbox']],
  3880. ['label' => ['for' => 'model-multi-field-2']],
  3881. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3882. 'third',
  3883. '/label',
  3884. '/div',
  3885. ];
  3886. $this->assertHtml($expected, $result);
  3887. $result = $this->Form->select(
  3888. 'Model.multi_field',
  3889. ['1/2' => 'half'],
  3890. ['multiple' => 'checkbox']
  3891. );
  3892. $expected = [
  3893. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3894. ['div' => ['class' => 'checkbox']],
  3895. ['label' => ['for' => 'model-multi-field-1-2']],
  3896. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3897. 'half',
  3898. '/label',
  3899. '/div',
  3900. ];
  3901. $this->assertHtml($expected, $result);
  3902. }
  3903. /**
  3904. * testLabel method
  3905. *
  3906. * Test label generation.
  3907. *
  3908. * @return void
  3909. */
  3910. public function testLabel()
  3911. {
  3912. $result = $this->Form->label('Person.name');
  3913. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3914. $this->assertHtml($expected, $result);
  3915. $result = $this->Form->label('Person.name');
  3916. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3917. $this->assertHtml($expected, $result);
  3918. $result = $this->Form->label('Person.first_name');
  3919. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3920. $this->assertHtml($expected, $result);
  3921. $result = $this->Form->label('Person.first_name', 'Your first name');
  3922. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3923. $this->assertHtml($expected, $result);
  3924. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3925. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3926. $this->assertHtml($expected, $result);
  3927. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3928. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3929. $this->assertHtml($expected, $result);
  3930. $result = $this->Form->label('Person.first_name', '');
  3931. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3932. $this->assertHtml($expected, $result);
  3933. $result = $this->Form->label('Person.2.name', '');
  3934. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3935. $this->assertHtml($expected, $result);
  3936. }
  3937. /**
  3938. * testLabelContainControl method
  3939. *
  3940. * Test that label() can accept an input with the correct template vars.
  3941. *
  3942. * @return void
  3943. */
  3944. public function testLabelContainControl()
  3945. {
  3946. $this->Form->setTemplates([
  3947. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3948. ]);
  3949. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3950. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3951. ]);
  3952. $expected = [
  3953. 'label' => ['for' => 'person-accept-terms'],
  3954. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3955. 'Accept',
  3956. '/label',
  3957. ];
  3958. $this->assertHtml($expected, $result);
  3959. }
  3960. /**
  3961. * testTextbox method
  3962. *
  3963. * Test textbox element generation.
  3964. *
  3965. * @return void
  3966. */
  3967. public function testTextbox()
  3968. {
  3969. $result = $this->Form->text('Model.field');
  3970. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3971. $this->assertHtml($expected, $result);
  3972. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3973. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3974. $this->assertHtml($expected, $result);
  3975. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3976. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3977. $this->assertHtml($expected, $result);
  3978. }
  3979. /**
  3980. * testTextBoxDataAndError method
  3981. *
  3982. * Test that text() hooks up with request data and error fields.
  3983. *
  3984. * @return void
  3985. */
  3986. public function testTextBoxDataAndError()
  3987. {
  3988. $this->article['errors'] = [
  3989. 'Contact' => ['text' => 'wrong'],
  3990. ];
  3991. $this->View->setRequest($this->View->getRequest()
  3992. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3993. ->withData('Contact.text', 'test'));
  3994. $this->Form->create($this->article);
  3995. $result = $this->Form->text('Model.text');
  3996. $expected = [
  3997. 'input' => [
  3998. 'type' => 'text',
  3999. 'name' => 'Model[text]',
  4000. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4001. ],
  4002. ];
  4003. $this->assertHtml($expected, $result);
  4004. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4005. $expected = [
  4006. 'input' => [
  4007. 'type' => 'text',
  4008. 'name' => 'Contact[text]',
  4009. 'value' => 'test',
  4010. 'id' => 'theID',
  4011. 'class' => 'form-error',
  4012. ],
  4013. ];
  4014. $this->assertHtml($expected, $result);
  4015. }
  4016. /**
  4017. * testDefaultValue method
  4018. *
  4019. * Test default value setting.
  4020. *
  4021. * @return void
  4022. */
  4023. public function testTextDefaultValue()
  4024. {
  4025. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  4026. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4027. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4028. $this->assertHtml($expected, $result);
  4029. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4030. $this->Form->create();
  4031. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4032. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4033. $this->assertHtml($expected, $result);
  4034. $Articles = $this->getTableLocator()->get('Articles');
  4035. $title = $Articles->getSchema()->getColumn('title');
  4036. $Articles->getSchema()->addColumn(
  4037. 'title',
  4038. ['default' => 'default title', 'length' => 255] + $title
  4039. );
  4040. $entity = $Articles->newEmptyEntity();
  4041. $this->Form->create($entity);
  4042. // Get default value from schema
  4043. $result = $this->Form->text('title');
  4044. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  4045. $this->assertHtml($expected, $result);
  4046. // Don't get value from schema
  4047. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4048. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  4049. $this->assertHtml($expected, $result);
  4050. // Custom default value overrides default value from schema
  4051. $result = $this->Form->text('title', ['default' => 'override default']);
  4052. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  4053. $this->assertHtml($expected, $result);
  4054. // Default value from schema is used only for new entities.
  4055. $entity->setNew(false);
  4056. $result = $this->Form->text('title');
  4057. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  4058. $this->assertHtml($expected, $result);
  4059. }
  4060. /**
  4061. * testError method
  4062. *
  4063. * Test field error generation.
  4064. *
  4065. * @return void
  4066. */
  4067. public function testError()
  4068. {
  4069. $this->article['errors'] = [
  4070. 'Article' => ['field' => 'email'],
  4071. ];
  4072. $this->Form->create($this->article);
  4073. $result = $this->Form->error('Article.field');
  4074. $expected = [
  4075. ['div' => ['class' => 'error-message']],
  4076. 'email',
  4077. '/div',
  4078. ];
  4079. $this->assertHtml($expected, $result);
  4080. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4081. $expected = [
  4082. ['div' => ['class' => 'error-message']],
  4083. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4084. '/div',
  4085. ];
  4086. $this->assertHtml($expected, $result);
  4087. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4088. $expected = [
  4089. ['div' => ['class' => 'error-message']],
  4090. '<strong', 'Badness!', '/strong',
  4091. '/div',
  4092. ];
  4093. $this->assertHtml($expected, $result);
  4094. }
  4095. /**
  4096. * testErrorRuleName method
  4097. *
  4098. * Test error translation can use rule names for translating.
  4099. *
  4100. * @return void
  4101. */
  4102. public function testErrorRuleName()
  4103. {
  4104. $this->article['errors'] = [
  4105. 'Article' => [
  4106. 'field' => ['email' => 'Your email was not good'],
  4107. ],
  4108. ];
  4109. $this->Form->create($this->article);
  4110. $result = $this->Form->error('Article.field');
  4111. $expected = [
  4112. ['div' => ['class' => 'error-message']],
  4113. 'Your email was not good',
  4114. '/div',
  4115. ];
  4116. $this->assertHtml($expected, $result);
  4117. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4118. $expected = [
  4119. ['div' => ['class' => 'error-message']],
  4120. 'Email in use',
  4121. '/div',
  4122. ];
  4123. $this->assertHtml($expected, $result);
  4124. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4125. $expected = [
  4126. ['div' => ['class' => 'error-message']],
  4127. 'Email in use',
  4128. '/div',
  4129. ];
  4130. $this->assertHtml($expected, $result);
  4131. $result = $this->Form->error('Article.field', [
  4132. 'email' => 'Key is preferred',
  4133. 'Your email was not good' => 'Email in use',
  4134. ]);
  4135. $expected = [
  4136. ['div' => ['class' => 'error-message']],
  4137. 'Key is preferred',
  4138. '/div',
  4139. ];
  4140. $this->assertHtml($expected, $result);
  4141. }
  4142. /**
  4143. * testErrorMessages method
  4144. *
  4145. * Test error with nested lists.
  4146. *
  4147. * @return void
  4148. */
  4149. public function testErrorMessages()
  4150. {
  4151. $this->article['errors'] = [
  4152. 'Article' => ['field' => 'email'],
  4153. ];
  4154. $this->Form->create($this->article);
  4155. $result = $this->Form->error('Article.field', [
  4156. 'email' => 'No good!',
  4157. ]);
  4158. $expected = [
  4159. 'div' => ['class' => 'error-message'],
  4160. 'No good!',
  4161. '/div',
  4162. ];
  4163. $this->assertHtml($expected, $result);
  4164. }
  4165. /**
  4166. * testErrorMultipleMessages method
  4167. *
  4168. * Test error() with multiple messages.
  4169. *
  4170. * @return void
  4171. */
  4172. public function testErrorMultipleMessages()
  4173. {
  4174. $this->article['errors'] = [
  4175. 'field' => ['notBlank', 'email', 'Something else'],
  4176. ];
  4177. $this->Form->create($this->article);
  4178. $result = $this->Form->error('field', [
  4179. 'notBlank' => 'Cannot be empty',
  4180. 'email' => 'No good!',
  4181. ]);
  4182. $expected = [
  4183. 'div' => ['class' => 'error-message'],
  4184. 'ul' => [],
  4185. '<li', 'Cannot be empty', '/li',
  4186. '<li', 'No good!', '/li',
  4187. '<li', 'Something else', '/li',
  4188. '/ul',
  4189. '/div',
  4190. ];
  4191. $this->assertHtml($expected, $result);
  4192. }
  4193. /**
  4194. * testPassword method
  4195. *
  4196. * Test password element generation.
  4197. *
  4198. * @return void
  4199. */
  4200. public function testPassword()
  4201. {
  4202. $this->article['errors'] = [
  4203. 'Contact' => [
  4204. 'passwd' => 1,
  4205. ],
  4206. ];
  4207. $this->Form->create($this->article);
  4208. $result = $this->Form->password('Contact.field');
  4209. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4210. $this->assertHtml($expected, $result);
  4211. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4212. $this->Form->create($this->article);
  4213. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4214. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4215. $this->assertHtml($expected, $result);
  4216. }
  4217. /**
  4218. * testRadio method
  4219. *
  4220. * Test radio element set generation.
  4221. *
  4222. * @return void
  4223. */
  4224. public function testRadio()
  4225. {
  4226. $result = $this->Form->radio('Model.field', ['option A']);
  4227. $expected = [
  4228. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4229. 'label' => ['for' => 'model-field-0'],
  4230. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4231. 'option A',
  4232. '/label',
  4233. ];
  4234. $this->assertHtml($expected, $result);
  4235. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4236. $this->assertHtml($expected, $result);
  4237. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4238. $expected = [
  4239. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4240. ['label' => ['for' => 'model-field-0']],
  4241. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4242. 'option A',
  4243. '/label',
  4244. ['label' => ['for' => 'model-field-1']],
  4245. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4246. 'option B',
  4247. '/label',
  4248. ];
  4249. $this->assertHtml($expected, $result);
  4250. $result = $this->Form->radio(
  4251. 'Employee.gender',
  4252. ['male' => 'Male', 'female' => 'Female'],
  4253. ['form' => 'my-form']
  4254. );
  4255. $expected = [
  4256. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4257. ['label' => ['for' => 'employee-gender-male']],
  4258. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4259. 'Male',
  4260. '/label',
  4261. ['label' => ['for' => 'employee-gender-female']],
  4262. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4263. 'Female',
  4264. '/label',
  4265. ];
  4266. $this->assertHtml($expected, $result);
  4267. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4268. $expected = [
  4269. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4270. ['label' => ['for' => 'model-custom-0']],
  4271. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4272. 'option A',
  4273. '/label',
  4274. ['label' => ['for' => 'model-custom-1']],
  4275. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4276. 'option B',
  4277. '/label',
  4278. ];
  4279. $this->assertHtml($expected, $result);
  4280. $result = $this->Form->radio(
  4281. 'Employee.gender',
  4282. [
  4283. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4284. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4285. ]
  4286. );
  4287. $expected = [
  4288. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4289. ['label' => ['for' => 'employee-gender-male']],
  4290. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4291. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4292. 'Male',
  4293. '/label',
  4294. ['label' => ['for' => 'employee-gender-female']],
  4295. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4296. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4297. 'Female',
  4298. '/label',
  4299. ];
  4300. $this->assertHtml($expected, $result);
  4301. }
  4302. /**
  4303. * Test radio with complex options and empty disabled data.
  4304. *
  4305. * @return void
  4306. */
  4307. public function testRadioComplexDisabled()
  4308. {
  4309. $options = [
  4310. ['value' => 'r', 'text' => 'red'],
  4311. ['value' => 'b', 'text' => 'blue'],
  4312. ];
  4313. $attrs = ['disabled' => []];
  4314. $result = $this->Form->radio('Model.field', $options, $attrs);
  4315. $expected = [
  4316. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4317. ['label' => ['for' => 'model-field-r']],
  4318. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4319. 'red',
  4320. '/label',
  4321. ['label' => ['for' => 'model-field-b']],
  4322. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4323. 'blue',
  4324. '/label',
  4325. ];
  4326. $this->assertHtml($expected, $result);
  4327. $attrs = ['disabled' => ['r']];
  4328. $result = $this->Form->radio('Model.field', $options, $attrs);
  4329. $this->assertStringContainsString('disabled="disabled"', $result);
  4330. }
  4331. /**
  4332. * testRadioDefaultValue method
  4333. *
  4334. * Test default value setting on radio() method.
  4335. *
  4336. * @return void
  4337. */
  4338. public function testRadioDefaultValue()
  4339. {
  4340. $Articles = $this->getTableLocator()->get('Articles');
  4341. $title = $Articles->getSchema()->getColumn('title');
  4342. $Articles->getSchema()->addColumn(
  4343. 'title',
  4344. ['default' => '1'] + $title
  4345. );
  4346. $this->Form->create($Articles->newEmptyEntity());
  4347. $result = $this->Form->radio('title', ['option A', 'option B']);
  4348. $expected = [
  4349. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4350. ['label' => ['for' => 'title-0']],
  4351. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4352. 'option A',
  4353. '/label',
  4354. ['label' => ['for' => 'title-1']],
  4355. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4356. 'option B',
  4357. '/label',
  4358. ];
  4359. $this->assertHtml($expected, $result);
  4360. }
  4361. /**
  4362. * Test setting a hiddenField value on radio buttons.
  4363. *
  4364. * @return void
  4365. */
  4366. public function testRadioHiddenFieldValue()
  4367. {
  4368. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4369. $expected = [
  4370. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4371. 'label' => ['for' => 'title-0'],
  4372. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4373. 'option A',
  4374. '/label',
  4375. ];
  4376. $this->assertHtml($expected, $result);
  4377. }
  4378. /**
  4379. * testControlRadio method
  4380. *
  4381. * Test that input works with radio types.
  4382. *
  4383. * @return void
  4384. */
  4385. public function testControlRadio()
  4386. {
  4387. $result = $this->Form->control('test', [
  4388. 'type' => 'radio',
  4389. 'options' => ['A', 'B'],
  4390. ]);
  4391. $expected = [
  4392. ['div' => ['class' => 'input radio']],
  4393. '<label',
  4394. 'Test',
  4395. '/label',
  4396. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4397. ['label' => ['for' => 'test-0']],
  4398. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4399. 'A',
  4400. '/label',
  4401. ['label' => ['for' => 'test-1']],
  4402. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4403. 'B',
  4404. '/label',
  4405. '/div',
  4406. ];
  4407. $this->assertHtml($expected, $result);
  4408. $result = $this->Form->control('test', [
  4409. 'type' => 'radio',
  4410. 'options' => ['A', 'B'],
  4411. 'value' => '0',
  4412. ]);
  4413. $expected = [
  4414. ['div' => ['class' => 'input radio']],
  4415. '<label',
  4416. 'Test',
  4417. '/label',
  4418. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4419. ['label' => ['for' => 'test-0']],
  4420. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4421. 'A',
  4422. '/label',
  4423. ['label' => ['for' => 'test-1']],
  4424. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4425. 'B',
  4426. '/label',
  4427. '/div',
  4428. ];
  4429. $this->assertHtml($expected, $result);
  4430. $result = $this->Form->control('test', [
  4431. 'type' => 'radio',
  4432. 'options' => ['A', 'B'],
  4433. 'label' => false,
  4434. ]);
  4435. $expected = [
  4436. ['div' => ['class' => 'input radio']],
  4437. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4438. ['label' => ['for' => 'test-0']],
  4439. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4440. 'A',
  4441. '/label',
  4442. ['label' => ['for' => 'test-1']],
  4443. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4444. 'B',
  4445. '/label',
  4446. '/div',
  4447. ];
  4448. $this->assertHtml($expected, $result);
  4449. }
  4450. /**
  4451. * testRadioNoLabel method
  4452. *
  4453. * Test that radio() works with label = false.
  4454. *
  4455. * @return void
  4456. */
  4457. public function testRadioNoLabel()
  4458. {
  4459. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4460. $expected = [
  4461. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4462. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4463. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4464. ];
  4465. $this->assertHtml($expected, $result);
  4466. }
  4467. /**
  4468. * testRadioControlInsideLabel method
  4469. *
  4470. * Test generating radio input inside label ala twitter bootstrap.
  4471. *
  4472. * @return void
  4473. */
  4474. public function testRadioControlInsideLabel()
  4475. {
  4476. $this->Form->setTemplates([
  4477. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4478. 'radioWrapper' => '{{label}}',
  4479. ]);
  4480. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4481. // phpcs:disable
  4482. $expected = [
  4483. ['input' => [
  4484. 'type' => 'hidden',
  4485. 'name' => 'Model[field]',
  4486. 'value' => ''
  4487. ]],
  4488. ['label' => ['for' => 'model-field-0']],
  4489. ['input' => [
  4490. 'type' => 'radio',
  4491. 'name' => 'Model[field]',
  4492. 'value' => '0',
  4493. 'id' => 'model-field-0'
  4494. ]],
  4495. 'option A',
  4496. '/label',
  4497. ['label' => ['for' => 'model-field-1']],
  4498. ['input' => [
  4499. 'type' => 'radio',
  4500. 'name' => 'Model[field]',
  4501. 'value' => '1',
  4502. 'id' => 'model-field-1'
  4503. ]],
  4504. 'option B',
  4505. '/label',
  4506. ];
  4507. // phpcs:enable
  4508. $this->assertHtml($expected, $result);
  4509. }
  4510. /**
  4511. * testRadioHiddenControlDisabling method
  4512. *
  4513. * Test disabling the hidden input for radio buttons.
  4514. *
  4515. * @return void
  4516. */
  4517. public function testRadioHiddenControlDisabling()
  4518. {
  4519. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4520. $expected = [
  4521. 'label' => ['for' => 'model-1-field-0'],
  4522. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4523. 'option A',
  4524. '/label',
  4525. ];
  4526. $this->assertHtml($expected, $result);
  4527. }
  4528. /**
  4529. * testRadioOutOfRange method
  4530. *
  4531. * Test radio element set generation.
  4532. *
  4533. * @return void
  4534. */
  4535. public function testRadioOutOfRange()
  4536. {
  4537. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4538. $expected = [
  4539. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4540. 'label' => ['for' => 'model-field-v'],
  4541. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4542. 'value',
  4543. '/label',
  4544. ];
  4545. $this->assertHtml($expected, $result);
  4546. }
  4547. /**
  4548. * testSelect method
  4549. *
  4550. * Test select element generation.
  4551. *
  4552. * @return void
  4553. */
  4554. public function testSelect()
  4555. {
  4556. $result = $this->Form->select('Model.field', []);
  4557. $expected = [
  4558. 'select' => ['name' => 'Model[field]'],
  4559. '/select',
  4560. ];
  4561. $this->assertHtml($expected, $result);
  4562. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4563. $this->Form->create();
  4564. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4565. $expected = [
  4566. 'select' => ['name' => 'Model[field]'],
  4567. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4568. 'good',
  4569. '/option',
  4570. ['option' => ['value' => 'other']],
  4571. 'bad',
  4572. '/option',
  4573. '/select',
  4574. ];
  4575. $this->assertHtml($expected, $result);
  4576. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4577. $this->assertHtml($expected, $result);
  4578. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4579. $this->Form->create();
  4580. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4581. $expected = [
  4582. 'select' => ['name' => 'Model[field]'],
  4583. ['option' => ['value' => 'value']],
  4584. 'good',
  4585. '/option',
  4586. ['option' => ['value' => 'other']],
  4587. 'bad',
  4588. '/option',
  4589. '/select',
  4590. ];
  4591. $this->assertHtml($expected, $result);
  4592. $options = [
  4593. ['value' => 'first', 'text' => 'First'],
  4594. ['value' => 'first', 'text' => 'Another First'],
  4595. ];
  4596. $result = $this->Form->select(
  4597. 'Model.field',
  4598. $options,
  4599. ['escape' => false, 'empty' => false]
  4600. );
  4601. $expected = [
  4602. 'select' => ['name' => 'Model[field]'],
  4603. ['option' => ['value' => 'first']],
  4604. 'First',
  4605. '/option',
  4606. ['option' => ['value' => 'first']],
  4607. 'Another First',
  4608. '/option',
  4609. '/select',
  4610. ];
  4611. $this->assertHtml($expected, $result);
  4612. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4613. $this->Form->create();
  4614. $result = $this->Form->select(
  4615. 'Model.contact_id',
  4616. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4617. ['escape' => false, 'empty' => 'pick something']
  4618. );
  4619. $expected = [
  4620. 'select' => ['name' => 'Model[contact_id]'],
  4621. ['option' => ['value' => '']], 'pick something', '/option',
  4622. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4623. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4624. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4625. '/select',
  4626. ];
  4627. $this->assertHtml($expected, $result);
  4628. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4629. $this->Form->create();
  4630. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4631. $expected = [
  4632. 'select' => ['name' => 'Model[field]'],
  4633. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4634. ['option' => ['value' => '1']], 'Yes', '/option',
  4635. '/select',
  4636. ];
  4637. $this->assertHtml($expected, $result);
  4638. }
  4639. /**
  4640. * testSelectEscapeHtml method
  4641. *
  4642. * Test that select() escapes HTML.
  4643. *
  4644. * @return void
  4645. */
  4646. public function testSelectEscapeHtml()
  4647. {
  4648. $result = $this->Form->select(
  4649. 'Model.field',
  4650. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4651. ['empty' => false]
  4652. );
  4653. $expected = [
  4654. 'select' => ['name' => 'Model[field]'],
  4655. ['option' => ['value' => 'first']],
  4656. 'first &quot;html&quot; &lt;chars&gt;',
  4657. '/option',
  4658. ['option' => ['value' => 'second']],
  4659. 'value',
  4660. '/option',
  4661. '/select',
  4662. ];
  4663. $this->assertHtml($expected, $result);
  4664. $result = $this->Form->select(
  4665. 'Model.field',
  4666. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4667. ['escape' => false, 'empty' => false]
  4668. );
  4669. $expected = [
  4670. 'select' => ['name' => 'Model[field]'],
  4671. ['option' => ['value' => 'first']],
  4672. 'first "html" <chars>',
  4673. '/option',
  4674. ['option' => ['value' => 'second']],
  4675. 'value',
  4676. '/option',
  4677. '/select',
  4678. ];
  4679. $this->assertHtml($expected, $result);
  4680. }
  4681. /**
  4682. * testSelectRequired method
  4683. *
  4684. * Test select() with required and disabled attributes.
  4685. *
  4686. * @return void
  4687. */
  4688. public function testSelectRequired()
  4689. {
  4690. $this->article['required'] = [
  4691. 'user_id' => true,
  4692. ];
  4693. $this->Form->create($this->article);
  4694. $result = $this->Form->select('user_id', ['option A']);
  4695. $expected = [
  4696. 'select' => [
  4697. 'name' => 'user_id',
  4698. 'required' => 'required',
  4699. ],
  4700. ['option' => ['value' => '0']], 'option A', '/option',
  4701. '/select',
  4702. ];
  4703. $this->assertHtml($expected, $result);
  4704. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4705. $expected = [
  4706. 'select' => [
  4707. 'name' => 'user_id',
  4708. 'disabled' => 'disabled',
  4709. ],
  4710. ['option' => ['value' => '0']], 'option A', '/option',
  4711. '/select',
  4712. ];
  4713. $this->assertHtml($expected, $result);
  4714. }
  4715. public function testSelectEmptyWithRequiredFalse()
  4716. {
  4717. $this->loadFixtures();
  4718. $Articles = $this->getTableLocator()->get('Articles');
  4719. $validator = $Articles->getValidator('default');
  4720. $validator->allowEmptyString('user_id');
  4721. $Articles->setValidator('default', $validator);
  4722. $entity = $Articles->newEmptyEntity();
  4723. $this->Form->create($entity);
  4724. $result = $this->Form->select('user_id', ['option A']);
  4725. $expected = [
  4726. 'select' => [
  4727. 'name' => 'user_id',
  4728. ],
  4729. ['option' => ['value' => '']], '/option',
  4730. ['option' => ['value' => '0']], 'option A', '/option',
  4731. '/select',
  4732. ];
  4733. $this->assertHtml($expected, $result);
  4734. }
  4735. /**
  4736. * testNestedSelect method
  4737. *
  4738. * Test select element generation with optgroups.
  4739. *
  4740. * @return void
  4741. */
  4742. public function testNestedSelect()
  4743. {
  4744. $result = $this->Form->select(
  4745. 'Model.field',
  4746. [1 => 'One', 2 => 'Two', 'Three' => [
  4747. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4748. ]],
  4749. ['empty' => false]
  4750. );
  4751. $expected = [
  4752. 'select' => ['name' => 'Model[field]'],
  4753. ['option' => ['value' => 1]],
  4754. 'One',
  4755. '/option',
  4756. ['option' => ['value' => 2]],
  4757. 'Two',
  4758. '/option',
  4759. ['optgroup' => ['label' => 'Three']],
  4760. ['option' => ['value' => 3]],
  4761. 'Three',
  4762. '/option',
  4763. ['option' => ['value' => 4]],
  4764. 'Four',
  4765. '/option',
  4766. ['option' => ['value' => 5]],
  4767. 'Five',
  4768. '/option',
  4769. '/optgroup',
  4770. '/select',
  4771. ];
  4772. $this->assertHtml($expected, $result);
  4773. }
  4774. /**
  4775. * testSelectMultiple method
  4776. *
  4777. * Test generation of multiple select elements.
  4778. *
  4779. * @return void
  4780. */
  4781. public function testSelectMultiple()
  4782. {
  4783. $options = ['first', 'second', 'third'];
  4784. $result = $this->Form->select(
  4785. 'Model.multi_field',
  4786. $options,
  4787. ['form' => 'my-form', 'multiple' => true]
  4788. );
  4789. $expected = [
  4790. 'input' => [
  4791. 'type' => 'hidden',
  4792. 'name' => 'Model[multi_field]',
  4793. 'value' => '',
  4794. 'form' => 'my-form',
  4795. ],
  4796. 'select' => [
  4797. 'name' => 'Model[multi_field][]',
  4798. 'multiple' => 'multiple',
  4799. 'form' => 'my-form',
  4800. ],
  4801. ['option' => ['value' => '0']],
  4802. 'first',
  4803. '/option',
  4804. ['option' => ['value' => '1']],
  4805. 'second',
  4806. '/option',
  4807. ['option' => ['value' => '2']],
  4808. 'third',
  4809. '/option',
  4810. '/select',
  4811. ];
  4812. $this->assertHtml($expected, $result);
  4813. $result = $this->Form->select(
  4814. 'Model.multi_field',
  4815. $options,
  4816. ['multiple' => 'multiple', 'form' => 'my-form']
  4817. );
  4818. $this->assertHtml($expected, $result);
  4819. $result = $this->Form->select(
  4820. 'Model.multi_field',
  4821. $options,
  4822. ['form' => 'my-form', 'multiple' => false]
  4823. );
  4824. $this->assertStringNotContainsString('multiple', $result);
  4825. }
  4826. /**
  4827. * testCheckboxZeroValue method
  4828. *
  4829. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4830. *
  4831. * @return void
  4832. */
  4833. public function testCheckboxZeroValue()
  4834. {
  4835. $result = $this->Form->control('User.get_spam', [
  4836. 'type' => 'checkbox',
  4837. 'value' => '0',
  4838. 'hiddenField' => '1',
  4839. ]);
  4840. $expected = [
  4841. 'div' => ['class' => 'input checkbox'],
  4842. 'label' => ['for' => 'user-get-spam'],
  4843. ['input' => [
  4844. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4845. 'value' => '1',
  4846. ]],
  4847. ['input' => [
  4848. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4849. 'value' => '0', 'id' => 'user-get-spam',
  4850. ]],
  4851. 'Get Spam',
  4852. '/label',
  4853. '/div',
  4854. ];
  4855. $this->assertHtml($expected, $result);
  4856. }
  4857. /**
  4858. * testHabtmSelectBox method
  4859. *
  4860. * Test generation of habtm select boxes.
  4861. *
  4862. * @return void
  4863. */
  4864. public function testHabtmSelectBox()
  4865. {
  4866. $this->loadFixtures('Articles');
  4867. $options = [
  4868. 1 => 'blue',
  4869. 2 => 'red',
  4870. 3 => 'green',
  4871. ];
  4872. $tags = [
  4873. new Entity(['id' => 1, 'name' => 'blue']),
  4874. new Entity(['id' => 3, 'name' => 'green']),
  4875. ];
  4876. $article = new Article(['tags' => $tags]);
  4877. $this->Form->create($article);
  4878. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4879. $expected = [
  4880. 'div' => ['class' => 'input select'],
  4881. 'label' => ['for' => 'tags-ids'],
  4882. 'Tags',
  4883. '/label',
  4884. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4885. 'select' => [
  4886. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4887. 'multiple' => 'multiple',
  4888. ],
  4889. ['option' => ['value' => '1', 'selected' => 'selected']],
  4890. 'blue',
  4891. '/option',
  4892. ['option' => ['value' => '2']],
  4893. 'red',
  4894. '/option',
  4895. ['option' => ['value' => '3', 'selected' => 'selected']],
  4896. 'green',
  4897. '/option',
  4898. '/select',
  4899. '/div',
  4900. ];
  4901. $this->assertHtml($expected, $result);
  4902. // make sure only 50 is selected, and not 50f5c0cf
  4903. $options = [
  4904. '1' => 'blue',
  4905. '50f5c0cf' => 'red',
  4906. '50' => 'green',
  4907. ];
  4908. $tags = [
  4909. new Entity(['id' => 1, 'name' => 'blue']),
  4910. new Entity(['id' => 50, 'name' => 'green']),
  4911. ];
  4912. $article = new Article(['tags' => $tags]);
  4913. $this->Form->create($article);
  4914. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4915. $expected = [
  4916. 'div' => ['class' => 'input select'],
  4917. 'label' => ['for' => 'tags-ids'],
  4918. 'Tags',
  4919. '/label',
  4920. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4921. 'select' => [
  4922. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4923. 'multiple' => 'multiple',
  4924. ],
  4925. ['option' => ['value' => '1', 'selected' => 'selected']],
  4926. 'blue',
  4927. '/option',
  4928. ['option' => ['value' => '50f5c0cf']],
  4929. 'red',
  4930. '/option',
  4931. ['option' => ['value' => '50', 'selected' => 'selected']],
  4932. 'green',
  4933. '/option',
  4934. '/select',
  4935. '/div',
  4936. ];
  4937. $this->assertHtml($expected, $result);
  4938. $spacecraft = [
  4939. 1 => 'Orion',
  4940. 2 => 'Helios',
  4941. ];
  4942. $this->View->set('spacecraft', $spacecraft);
  4943. $this->Form->create();
  4944. $result = $this->Form->control('spacecraft._ids');
  4945. $expected = [
  4946. 'div' => ['class' => 'input select'],
  4947. 'label' => ['for' => 'spacecraft-ids'],
  4948. 'Spacecraft',
  4949. '/label',
  4950. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4951. 'select' => [
  4952. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4953. 'multiple' => 'multiple',
  4954. ],
  4955. ['option' => ['value' => '1']],
  4956. 'Orion',
  4957. '/option',
  4958. ['option' => ['value' => '2']],
  4959. 'Helios',
  4960. '/option',
  4961. '/select',
  4962. '/div',
  4963. ];
  4964. $this->assertHtml($expected, $result);
  4965. }
  4966. /**
  4967. * testErrorsForBelongsToManySelect method
  4968. *
  4969. * Tests that errors for belongsToMany select fields are being
  4970. * picked up properly.
  4971. *
  4972. * @return void
  4973. */
  4974. public function testErrorsForBelongsToManySelect()
  4975. {
  4976. $spacecraft = [
  4977. 1 => 'Orion',
  4978. 2 => 'Helios',
  4979. ];
  4980. $this->View->set('spacecraft', $spacecraft);
  4981. $article = new Article();
  4982. $article->setError('spacecraft', ['Invalid']);
  4983. $this->Form->create($article);
  4984. $result = $this->Form->control('spacecraft._ids');
  4985. $expected = [
  4986. ['div' => ['class' => 'input select error']],
  4987. 'label' => ['for' => 'spacecraft-ids'],
  4988. 'Spacecraft',
  4989. '/label',
  4990. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4991. 'select' => [
  4992. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4993. 'multiple' => 'multiple',
  4994. ],
  4995. ['option' => ['value' => '1']],
  4996. 'Orion',
  4997. '/option',
  4998. ['option' => ['value' => '2']],
  4999. 'Helios',
  5000. '/option',
  5001. '/select',
  5002. ['div' => ['class' => 'error-message']],
  5003. 'Invalid',
  5004. '/div',
  5005. '/div',
  5006. ];
  5007. $this->assertHtml($expected, $result);
  5008. }
  5009. /**
  5010. * testSelectMultipleCheckboxes method
  5011. *
  5012. * Test generation of multi select elements in checkbox format.
  5013. *
  5014. * @return void
  5015. */
  5016. public function testSelectMultipleCheckboxes()
  5017. {
  5018. $result = $this->Form->select(
  5019. 'Model.multi_field',
  5020. ['first', 'second', 'third'],
  5021. ['multiple' => 'checkbox']
  5022. );
  5023. $expected = [
  5024. 'input' => [
  5025. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5026. ],
  5027. ['div' => ['class' => 'checkbox']],
  5028. ['label' => ['for' => 'model-multi-field-0']],
  5029. ['input' => [
  5030. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5031. 'value' => '0', 'id' => 'model-multi-field-0',
  5032. ]],
  5033. 'first',
  5034. '/label',
  5035. '/div',
  5036. ['div' => ['class' => 'checkbox']],
  5037. ['label' => ['for' => 'model-multi-field-1']],
  5038. ['input' => [
  5039. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5040. 'value' => '1', 'id' => 'model-multi-field-1',
  5041. ]],
  5042. 'second',
  5043. '/label',
  5044. '/div',
  5045. ['div' => ['class' => 'checkbox']],
  5046. ['label' => ['for' => 'model-multi-field-2']],
  5047. ['input' => [
  5048. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5049. 'value' => '2', 'id' => 'model-multi-field-2',
  5050. ]],
  5051. 'third',
  5052. '/label',
  5053. '/div',
  5054. ];
  5055. $this->assertHtml($expected, $result);
  5056. $result = $this->Form->select(
  5057. 'Model.multi_field',
  5058. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5059. ['multiple' => 'checkbox']
  5060. );
  5061. $expected = [
  5062. 'input' => [
  5063. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5064. ],
  5065. ['div' => ['class' => 'checkbox']],
  5066. ['label' => ['for' => 'model-multi-field-a+']],
  5067. ['input' => [
  5068. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5069. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5070. ]],
  5071. 'first',
  5072. '/label',
  5073. '/div',
  5074. ['div' => ['class' => 'checkbox']],
  5075. ['label' => ['for' => 'model-multi-field-a++']],
  5076. ['input' => [
  5077. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5078. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5079. ]],
  5080. 'second',
  5081. '/label',
  5082. '/div',
  5083. ['div' => ['class' => 'checkbox']],
  5084. ['label' => ['for' => 'model-multi-field-a+++']],
  5085. ['input' => [
  5086. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5087. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5088. ]],
  5089. 'third',
  5090. '/label',
  5091. '/div',
  5092. ];
  5093. $this->assertHtml($expected, $result);
  5094. $result = $this->Form->select(
  5095. 'Model.multi_field',
  5096. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5097. ['multiple' => 'checkbox']
  5098. );
  5099. $expected = [
  5100. 'input' => [
  5101. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5102. ],
  5103. ['div' => ['class' => 'checkbox']],
  5104. ['label' => ['for' => 'model-multi-field-a-b']],
  5105. ['input' => [
  5106. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5107. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5108. ]],
  5109. 'first',
  5110. '/label',
  5111. '/div',
  5112. ['div' => ['class' => 'checkbox']],
  5113. ['label' => ['for' => 'model-multi-field-a-b1']],
  5114. ['input' => [
  5115. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5116. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5117. ]],
  5118. 'second',
  5119. '/label',
  5120. '/div',
  5121. ['div' => ['class' => 'checkbox']],
  5122. ['label' => ['for' => 'model-multi-field-a-b2']],
  5123. ['input' => [
  5124. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5125. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5126. ]],
  5127. 'third',
  5128. '/label',
  5129. '/div',
  5130. ];
  5131. $this->assertHtml($expected, $result);
  5132. }
  5133. /**
  5134. * testSelectMultipleCheckboxRequestData method
  5135. *
  5136. * Ensure that multiCheckbox reads from the request data.
  5137. *
  5138. * @return void
  5139. */
  5140. public function testSelectMultipleCheckboxRequestData()
  5141. {
  5142. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5143. $result = $this->Form->select(
  5144. 'Model.tags',
  5145. ['1' => 'first', 'Array' => 'Array'],
  5146. ['multiple' => 'checkbox']
  5147. );
  5148. $expected = [
  5149. 'input' => [
  5150. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '',
  5151. ],
  5152. ['div' => ['class' => 'checkbox']],
  5153. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5154. ['input' => [
  5155. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5156. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5157. ]],
  5158. 'first',
  5159. '/label',
  5160. '/div',
  5161. ['div' => ['class' => 'checkbox']],
  5162. ['label' => ['for' => 'model-tags-array']],
  5163. ['input' => [
  5164. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5165. 'value' => 'Array', 'id' => 'model-tags-array',
  5166. ]],
  5167. 'Array',
  5168. '/label',
  5169. '/div',
  5170. ];
  5171. $this->assertHtml($expected, $result);
  5172. }
  5173. /**
  5174. * testSelectMultipleCheckboxSecurity method
  5175. *
  5176. * Checks the security hash array generated for multiple-input checkbox elements.
  5177. *
  5178. * @return void
  5179. */
  5180. public function testSelectMultipleCheckboxSecurity()
  5181. {
  5182. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5183. $this->Form->create();
  5184. $this->Form->select(
  5185. 'Model.multi_field',
  5186. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5187. ['multiple' => 'checkbox']
  5188. );
  5189. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5190. $this->assertEquals(['Model.multi_field'], $fields);
  5191. $result = $this->Form->secure();
  5192. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5193. $hash = urlencode($hash . ':');
  5194. $this->assertStringContainsString('"' . $hash . '"', $result);
  5195. }
  5196. /**
  5197. * testSelectMultipleSecureWithNoOptions method
  5198. *
  5199. * Multiple select elements should always be secured as they always participate
  5200. * in the POST data.
  5201. *
  5202. * @return void
  5203. */
  5204. public function testSelectMultipleSecureWithNoOptions()
  5205. {
  5206. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5207. $this->Form->create();
  5208. $this->Form->select(
  5209. 'Model.select',
  5210. [],
  5211. ['multiple' => true]
  5212. );
  5213. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5214. $this->assertEquals(['Model.select'], $result);
  5215. }
  5216. /**
  5217. * testSelectNoSecureWithNoOptions method
  5218. *
  5219. * When a select box has no options it should not be added to the fields list
  5220. * as it always fail post validation.
  5221. *
  5222. * @return void
  5223. */
  5224. public function testSelectNoSecureWithNoOptions()
  5225. {
  5226. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5227. $this->Form->create();
  5228. $this->Form->select(
  5229. 'Model.select',
  5230. []
  5231. );
  5232. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5233. $this->assertEquals([], $result);
  5234. $this->Form->select(
  5235. 'Model.user_id',
  5236. [],
  5237. ['empty' => true]
  5238. );
  5239. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5240. $this->assertEquals(['Model.user_id'], $result);
  5241. }
  5242. /**
  5243. * testControlMultipleCheckboxes method
  5244. *
  5245. * Test control() resulting in multi select elements being generated.
  5246. *
  5247. * @return void
  5248. */
  5249. public function testControlMultipleCheckboxes()
  5250. {
  5251. $result = $this->Form->control('Model.multi_field', [
  5252. 'options' => ['first', 'second', 'third'],
  5253. 'multiple' => 'checkbox',
  5254. ]);
  5255. $expected = [
  5256. ['div' => ['class' => 'input select']],
  5257. ['label' => ['for' => 'model-multi-field']],
  5258. 'Multi Field',
  5259. '/label',
  5260. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5261. ['div' => ['class' => 'checkbox']],
  5262. ['label' => ['for' => 'model-multi-field-0']],
  5263. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5264. 'first',
  5265. '/label',
  5266. '/div',
  5267. ['div' => ['class' => 'checkbox']],
  5268. ['label' => ['for' => 'model-multi-field-1']],
  5269. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5270. 'second',
  5271. '/label',
  5272. '/div',
  5273. ['div' => ['class' => 'checkbox']],
  5274. ['label' => ['for' => 'model-multi-field-2']],
  5275. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5276. 'third',
  5277. '/label',
  5278. '/div',
  5279. '/div',
  5280. ];
  5281. $this->assertHtml($expected, $result);
  5282. $result = $this->Form->control('Model.multi_field', [
  5283. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5284. 'multiple' => 'checkbox',
  5285. ]);
  5286. $expected = [
  5287. ['div' => ['class' => 'input select']],
  5288. ['label' => ['for' => 'model-multi-field']],
  5289. 'Multi Field',
  5290. '/label',
  5291. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5292. ['div' => ['class' => 'checkbox']],
  5293. ['label' => ['for' => 'model-multi-field-a']],
  5294. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5295. 'first',
  5296. '/label',
  5297. '/div',
  5298. ['div' => ['class' => 'checkbox']],
  5299. ['label' => ['for' => 'model-multi-field-b']],
  5300. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5301. 'second',
  5302. '/label',
  5303. '/div',
  5304. ['div' => ['class' => 'checkbox']],
  5305. ['label' => ['for' => 'model-multi-field-c']],
  5306. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5307. 'third',
  5308. '/label',
  5309. '/div',
  5310. '/div',
  5311. ];
  5312. $this->assertHtml($expected, $result);
  5313. }
  5314. /**
  5315. * testSelectHiddenFieldOmission method
  5316. *
  5317. * Test that select() with 'hiddenField' => false omits the hidden field.
  5318. *
  5319. * @return void
  5320. */
  5321. public function testSelectHiddenFieldOmission()
  5322. {
  5323. $result = $this->Form->select(
  5324. 'Model.multi_field',
  5325. ['first', 'second'],
  5326. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5327. );
  5328. $this->assertStringNotContainsString('type="hidden"', $result);
  5329. }
  5330. /**
  5331. * testSelectCheckboxMultipleOverrideName method
  5332. *
  5333. * Test that select() with multiple = checkbox works with overriding name attribute.
  5334. *
  5335. * @return void
  5336. */
  5337. public function testSelectCheckboxMultipleOverrideName()
  5338. {
  5339. $result = $this->Form->select('category', ['1', '2'], [
  5340. 'multiple' => 'checkbox',
  5341. 'name' => 'fish',
  5342. ]);
  5343. $expected = [
  5344. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5345. ['div' => ['class' => 'checkbox']],
  5346. ['label' => ['for' => 'fish-0']],
  5347. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5348. '1',
  5349. '/label',
  5350. '/div',
  5351. ['div' => ['class' => 'checkbox']],
  5352. ['label' => ['for' => 'fish-1']],
  5353. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5354. '2',
  5355. '/label',
  5356. '/div',
  5357. ];
  5358. $this->assertHtml($expected, $result);
  5359. $result = $this->Form->multiCheckbox(
  5360. 'category',
  5361. new Collection(['1', '2']),
  5362. ['name' => 'fish']
  5363. );
  5364. $this->assertHtml($expected, $result);
  5365. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5366. 'name' => 'fish',
  5367. ]);
  5368. $this->assertHtml($expected, $result);
  5369. }
  5370. /**
  5371. * testControlMultiCheckbox method
  5372. *
  5373. * Test that control() works with multicheckbox.
  5374. *
  5375. * @return void
  5376. */
  5377. public function testControlMultiCheckbox()
  5378. {
  5379. $result = $this->Form->control('category', [
  5380. 'type' => 'multicheckbox',
  5381. 'options' => ['1', '2'],
  5382. ]);
  5383. $expected = [
  5384. ['div' => ['class' => 'input multicheckbox']],
  5385. '<label',
  5386. 'Category',
  5387. '/label',
  5388. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5389. ['div' => ['class' => 'checkbox']],
  5390. ['label' => ['for' => 'category-0']],
  5391. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5392. '1',
  5393. '/label',
  5394. '/div',
  5395. ['div' => ['class' => 'checkbox']],
  5396. ['label' => ['for' => 'category-1']],
  5397. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5398. '2',
  5399. '/label',
  5400. '/div',
  5401. '/div',
  5402. ];
  5403. $this->assertHtml($expected, $result);
  5404. }
  5405. /**
  5406. * testCheckbox method
  5407. *
  5408. * Test generation of checkboxes.
  5409. *
  5410. * @return void
  5411. */
  5412. public function testCheckbox()
  5413. {
  5414. $result = $this->Form->checkbox('Model.field');
  5415. $expected = [
  5416. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5417. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5418. ];
  5419. $this->assertHtml($expected, $result);
  5420. $result = $this->Form->checkbox('Model.field', [
  5421. 'id' => 'theID',
  5422. 'value' => 'myvalue',
  5423. 'form' => 'my-form',
  5424. ]);
  5425. $expected = [
  5426. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5427. ['input' => [
  5428. 'type' => 'checkbox', 'name' => 'Model[field]',
  5429. 'value' => 'myvalue', 'id' => 'theID',
  5430. 'form' => 'my-form',
  5431. ]],
  5432. ];
  5433. $this->assertHtml($expected, $result);
  5434. }
  5435. /**
  5436. * testCheckboxDefaultValue method
  5437. *
  5438. * Test default value setting on checkbox() method.
  5439. *
  5440. * @return void
  5441. */
  5442. public function testCheckboxDefaultValue()
  5443. {
  5444. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5445. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5446. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5447. $this->assertHtml($expected, $result);
  5448. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5449. $this->Form->create();
  5450. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5451. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5452. $this->assertHtml($expected, $result);
  5453. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5454. $this->Form->create();
  5455. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5456. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5457. $this->assertHtml($expected, $result);
  5458. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5459. $this->Form->create();
  5460. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5461. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5462. $this->assertHtml($expected, $result);
  5463. $Articles = $this->getTableLocator()->get('Articles');
  5464. $Articles->getSchema()->addColumn(
  5465. 'published',
  5466. ['type' => 'boolean', 'null' => false, 'default' => true]
  5467. );
  5468. $this->Form->create($Articles->newEmptyEntity());
  5469. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5470. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5471. $this->assertHtml($expected, $result);
  5472. }
  5473. /**
  5474. * testCheckboxCheckedAndError method
  5475. *
  5476. * Test checkbox being checked or having errors.
  5477. *
  5478. * @return void
  5479. */
  5480. public function testCheckboxCheckedAndError()
  5481. {
  5482. $this->article['errors'] = [
  5483. 'published' => true,
  5484. ];
  5485. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5486. $this->Form->create($this->article);
  5487. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5488. $expected = [
  5489. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5490. ['input' => [
  5491. 'type' => 'checkbox',
  5492. 'name' => 'published',
  5493. 'value' => 'myvalue',
  5494. 'id' => 'theID',
  5495. 'checked' => 'checked',
  5496. 'class' => 'form-error',
  5497. ]],
  5498. ];
  5499. $this->assertHtml($expected, $result);
  5500. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5501. $this->Form->create($this->article);
  5502. $result = $this->Form->checkbox('published');
  5503. $expected = [
  5504. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5505. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5506. ];
  5507. $this->assertHtml($expected, $result);
  5508. }
  5509. /**
  5510. * testCheckboxCustomNameAttribute method
  5511. *
  5512. * Test checkbox() with a custom name attribute.
  5513. *
  5514. * @return void
  5515. */
  5516. public function testCheckboxCustomNameAttribute()
  5517. {
  5518. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5519. $expected = [
  5520. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5521. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5522. ];
  5523. $this->assertHtml($expected, $result);
  5524. }
  5525. /**
  5526. * testCheckboxHiddenField method
  5527. *
  5528. * Test that the hidden input for checkboxes can be omitted or set to a
  5529. * specific value.
  5530. *
  5531. * @return void
  5532. */
  5533. public function testCheckboxHiddenField()
  5534. {
  5535. $result = $this->Form->checkbox('UserForm.something', [
  5536. 'hiddenField' => false,
  5537. ]);
  5538. $expected = [
  5539. 'input' => [
  5540. 'type' => 'checkbox',
  5541. 'name' => 'UserForm[something]',
  5542. 'value' => '1',
  5543. ],
  5544. ];
  5545. $this->assertHtml($expected, $result);
  5546. $result = $this->Form->checkbox('UserForm.something', [
  5547. 'value' => 'Y',
  5548. 'hiddenField' => 'N',
  5549. ]);
  5550. $expected = [
  5551. ['input' => [
  5552. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5553. 'value' => 'N',
  5554. ]],
  5555. ['input' => [
  5556. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5557. 'value' => 'Y',
  5558. ]],
  5559. ];
  5560. $this->assertHtml($expected, $result);
  5561. }
  5562. /**
  5563. * testTime method
  5564. *
  5565. * Test the time type.
  5566. *
  5567. * @return void
  5568. */
  5569. public function testTime()
  5570. {
  5571. $result = $this->Form->time('start_time', [
  5572. 'value' => '2014-03-08 16:30:00',
  5573. ]);
  5574. $expected = [
  5575. 'input' => [
  5576. 'type' => 'time',
  5577. 'name' => 'start_time',
  5578. 'value' => '16:30:00',
  5579. 'step' => '1',
  5580. ],
  5581. ];
  5582. $this->assertHtml($expected, $result);
  5583. }
  5584. /**
  5585. * testDate method
  5586. *
  5587. * Test the date type.
  5588. *
  5589. * @return void
  5590. */
  5591. public function testDate()
  5592. {
  5593. $result = $this->Form->date('start_day', [
  5594. 'value' => '2014-03-08',
  5595. ]);
  5596. $expected = [
  5597. 'input' => [
  5598. 'type' => 'date',
  5599. 'name' => 'start_day',
  5600. 'value' => '2014-03-08',
  5601. ],
  5602. ];
  5603. $this->assertHtml($expected, $result);
  5604. $result = $this->Form->date('start_day', [
  5605. 'value' => new Date('2014-03-08'),
  5606. ]);
  5607. $this->assertHtml($expected, $result);
  5608. }
  5609. /**
  5610. * testDateTime method
  5611. *
  5612. * @return void
  5613. */
  5614. public function testDateTime()
  5615. {
  5616. $result = $this->Form->dateTime('date', ['default' => true]);
  5617. $expected = [
  5618. 'input' => [
  5619. 'type' => 'datetime-local',
  5620. 'name' => 'date',
  5621. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  5622. 'step' => '1',
  5623. ],
  5624. ];
  5625. $this->assertHtml($expected, $result);
  5626. }
  5627. /**
  5628. * testDateTimeSecured method
  5629. *
  5630. * Test that datetime fields are added to protected fields list.
  5631. *
  5632. * @return void
  5633. */
  5634. public function testDateTimeSecured()
  5635. {
  5636. $this->View->setRequest(
  5637. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5638. );
  5639. $this->Form->create();
  5640. $this->Form->dateTime('date');
  5641. $expected = ['date'];
  5642. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5643. $this->assertEquals($expected, $result);
  5644. $this->Form->fields = [];
  5645. $this->Form->date('published');
  5646. $expected = ['date', 'published'];
  5647. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5648. $this->assertEquals($expected, $result);
  5649. }
  5650. /**
  5651. * testDateTimeSecuredDisabled method
  5652. *
  5653. * Test that datetime fields are added to protected fields list.
  5654. *
  5655. * @return void
  5656. */
  5657. public function testDateTimeSecuredDisabled()
  5658. {
  5659. $this->View->setRequest(
  5660. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5661. );
  5662. $this->Form->create();
  5663. $this->Form->dateTime('date', ['secure' => false]);
  5664. $expected = [];
  5665. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5666. $this->assertEquals($expected, $result);
  5667. $this->Form->fields = [];
  5668. $this->Form->date('published', ['secure' => false]);
  5669. $expected = [];
  5670. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5671. $this->assertEquals($expected, $result);
  5672. }
  5673. /**
  5674. * testDatetimeWithDefault method
  5675. *
  5676. * Test that datetime() and default values work.
  5677. *
  5678. * @return void
  5679. */
  5680. public function testDatetimeWithDefault()
  5681. {
  5682. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5683. $expected = [
  5684. 'input' => [
  5685. 'type' => 'datetime-local',
  5686. 'name' => 'updated',
  5687. 'value' => '2009-06-01T11:15:30',
  5688. 'step' => '1',
  5689. ],
  5690. ];
  5691. $this->assertHtml($expected, $result);
  5692. $result = $this->Form->dateTime('updated', [
  5693. 'default' => '2009-06-01 11:15:30',
  5694. ]);
  5695. $expected = [
  5696. 'input' => [
  5697. 'type' => 'datetime-local',
  5698. 'name' => 'updated',
  5699. 'value' => '2009-06-01T11:15:30',
  5700. 'step' => '1',
  5701. ],
  5702. ];
  5703. $this->assertHtml($expected, $result);
  5704. }
  5705. /**
  5706. * testMonth method
  5707. *
  5708. * Test generation of a month input.
  5709. *
  5710. * @return void
  5711. */
  5712. public function testMonth()
  5713. {
  5714. $result = $this->Form->month('field', ['value' => '']);
  5715. $expected = [
  5716. 'input' => [
  5717. 'type' => 'month',
  5718. 'name' => 'field',
  5719. 'value' => '',
  5720. ],
  5721. ];
  5722. $this->assertHtml($expected, $result);
  5723. $this->View->setRequest(
  5724. $this->View->getRequest()->withData('release', '2050-02-10')
  5725. );
  5726. $this->Form->create();
  5727. $result = $this->Form->month('release');
  5728. $expected = [
  5729. 'input' => [
  5730. 'type' => 'month',
  5731. 'name' => 'release',
  5732. 'value' => '2050-02',
  5733. ],
  5734. ];
  5735. $this->assertHtml($expected, $result);
  5736. $this->View->setRequest(
  5737. $this->View->getRequest()->withData('release', '2050-03')
  5738. );
  5739. $this->Form->create();
  5740. $result = $this->Form->month('release');
  5741. $expected = [
  5742. 'input' => [
  5743. 'type' => 'month',
  5744. 'name' => 'release',
  5745. 'value' => '2050-03',
  5746. ],
  5747. ];
  5748. $this->assertHtml($expected, $result);
  5749. }
  5750. /**
  5751. * testYear method
  5752. *
  5753. * Test generation of a year input.
  5754. *
  5755. * @return void
  5756. */
  5757. public function testYear()
  5758. {
  5759. $this->View->setRequest(
  5760. $this->View->getRequest()->withData('published', '2006')
  5761. );
  5762. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5763. $expected = [
  5764. ['select' => ['name' => 'field']],
  5765. ['option' => ['selected' => 'selected', 'value' => '']],
  5766. '/option',
  5767. ['option' => ['value' => '2007']],
  5768. '2007',
  5769. '/option',
  5770. ['option' => ['value' => '2006']],
  5771. '2006',
  5772. '/option',
  5773. '/select',
  5774. ];
  5775. $this->assertHtml($expected, $result);
  5776. $result = $this->Form->year('field', [
  5777. 'value' => '',
  5778. 'min' => 2006,
  5779. 'max' => 2007,
  5780. 'order' => 'asc',
  5781. ]);
  5782. $expected = [
  5783. ['select' => ['name' => 'field']],
  5784. ['option' => ['selected' => 'selected', 'value' => '']],
  5785. '/option',
  5786. ['option' => ['value' => '2006']],
  5787. '2006',
  5788. '/option',
  5789. ['option' => ['value' => '2007']],
  5790. '2007',
  5791. '/option',
  5792. '/select',
  5793. ];
  5794. $this->assertHtml($expected, $result);
  5795. $result = $this->Form->year('published', [
  5796. 'empty' => false,
  5797. 'min' => 2006,
  5798. 'max' => 2007,
  5799. ]);
  5800. $expected = [
  5801. ['select' => ['name' => 'published']],
  5802. ['option' => ['value' => '2007']],
  5803. '2007',
  5804. '/option',
  5805. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5806. '2006',
  5807. '/option',
  5808. '/select',
  5809. ];
  5810. $this->assertHtml($expected, $result);
  5811. $result = $this->Form->year('published', [
  5812. 'empty' => false,
  5813. 'value' => new Date('2008-01-12'),
  5814. 'min' => 2007,
  5815. 'max' => 2009,
  5816. ]);
  5817. $expected = [
  5818. ['select' => ['name' => 'published']],
  5819. ['option' => ['value' => '2009']],
  5820. '2009',
  5821. '/option',
  5822. ['option' => ['value' => '2008', 'selected' => 'selected']],
  5823. '2008',
  5824. '/option',
  5825. ['option' => ['value' => '2007']],
  5826. '2007',
  5827. '/option',
  5828. '/select',
  5829. ];
  5830. $this->assertHtml($expected, $result);
  5831. $result = $this->Form->year('published', [
  5832. 'empty' => 'Published on',
  5833. ]);
  5834. $this->assertStringContainsString('Published on', $result);
  5835. }
  5836. /**
  5837. * testControlYearPreEpoch method
  5838. *
  5839. * Test minYear being prior to the unix epoch.
  5840. *
  5841. * @return void
  5842. */
  5843. public function testControlYearPreEpoch()
  5844. {
  5845. $start = date('Y') - 80;
  5846. $end = date('Y') - 18;
  5847. $result = $this->Form->control('birth_year', [
  5848. 'type' => 'year',
  5849. 'label' => 'Birth Year',
  5850. 'min' => $start,
  5851. 'max' => $end,
  5852. ]);
  5853. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5854. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5855. $this->assertStringNotContainsString('value="00">00', $result);
  5856. }
  5857. /**
  5858. * test control() datetime & required attributes
  5859. *
  5860. * @return void
  5861. */
  5862. public function testControlDatetimeRequired()
  5863. {
  5864. $result = $this->Form->control('birthday', [
  5865. 'type' => 'date',
  5866. 'required' => true,
  5867. ]);
  5868. $this->assertStringContainsString(
  5869. '<input type="date" name="birthday" required="required"',
  5870. $result
  5871. );
  5872. }
  5873. /**
  5874. * testYearAutoExpandRange method
  5875. *
  5876. * @return void
  5877. */
  5878. public function testYearAutoExpandRange()
  5879. {
  5880. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5881. $result = $this->Form->year('birthday');
  5882. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5883. $result = $matches[1];
  5884. $expected = range(date('Y') + 5, 1930);
  5885. $this->assertEquals($expected, $result);
  5886. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5887. $this->Form->create();
  5888. $result = $this->Form->year('release');
  5889. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5890. $result = $matches[1];
  5891. $expected = range(2050, date('Y') - 5);
  5892. $this->assertEquals($expected, $result);
  5893. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5894. $this->Form->create();
  5895. $result = $this->Form->year('release', [
  5896. 'min' => 1890,
  5897. 'max' => 1900,
  5898. ]);
  5899. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5900. $result = $matches[1];
  5901. $expected = range(1900, 1881);
  5902. $this->assertEquals($expected, $result);
  5903. }
  5904. /**
  5905. * testControlLabelFalse method
  5906. *
  5907. * Test the label option being set to false.
  5908. *
  5909. * @return void
  5910. */
  5911. public function testControlLabelFalse()
  5912. {
  5913. $this->Form->create($this->article);
  5914. $result = $this->Form->control('title', ['label' => false]);
  5915. $expected = [
  5916. 'div' => ['class' => 'input text required'],
  5917. 'input' => [
  5918. 'type' => 'text',
  5919. 'required' => 'required',
  5920. 'id' => 'title',
  5921. 'name' => 'title',
  5922. 'data-validity-message' => 'This field cannot be left empty',
  5923. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5924. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5925. ],
  5926. '/div',
  5927. ];
  5928. $this->assertHtml($expected, $result);
  5929. }
  5930. /**
  5931. * testTextArea method
  5932. *
  5933. * Test generation of a textarea input.
  5934. *
  5935. * @return void
  5936. */
  5937. public function testTextArea()
  5938. {
  5939. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5940. $result = $this->Form->textarea('field');
  5941. $expected = [
  5942. 'textarea' => ['name' => 'field', 'rows' => 5],
  5943. 'some test data',
  5944. '/textarea',
  5945. ];
  5946. $this->assertHtml($expected, $result);
  5947. $result = $this->Form->textarea('user.bio');
  5948. $expected = [
  5949. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5950. '/textarea',
  5951. ];
  5952. $this->assertHtml($expected, $result);
  5953. $this->View->setRequest($this->View->getRequest()
  5954. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5955. $this->Form->create();
  5956. $result = $this->Form->textarea('field');
  5957. $expected = [
  5958. 'textarea' => ['name' => 'field', 'rows' => 5],
  5959. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5960. '/textarea',
  5961. ];
  5962. $this->assertHtml($expected, $result);
  5963. $this->View->setRequest($this->View->getRequest()->withData(
  5964. 'Model.field',
  5965. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5966. ));
  5967. $this->Form->create();
  5968. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5969. $expected = [
  5970. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5971. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5972. '/textarea',
  5973. ];
  5974. $this->assertHtml($expected, $result);
  5975. $result = $this->Form->textarea('0.OtherModel.field');
  5976. $expected = [
  5977. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5978. '/textarea',
  5979. ];
  5980. $this->assertHtml($expected, $result);
  5981. }
  5982. /**
  5983. * testTextAreaWithStupidCharacters method
  5984. *
  5985. * Test text area with non-ascii characters.
  5986. *
  5987. * @return void
  5988. */
  5989. public function testTextAreaWithStupidCharacters()
  5990. {
  5991. $result = $this->Form->textarea('Post.content', [
  5992. 'value' => 'GREAT®',
  5993. 'rows' => '15',
  5994. 'cols' => '75',
  5995. ]);
  5996. $expected = [
  5997. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5998. 'GREAT®',
  5999. '/textarea',
  6000. ];
  6001. $this->assertHtml($expected, $result);
  6002. }
  6003. /**
  6004. * testTextAreaMaxLength method
  6005. *
  6006. * Test textareas maxlength read from schema.
  6007. *
  6008. * @return void
  6009. */
  6010. public function testTextAreaMaxLength()
  6011. {
  6012. $this->Form->create([
  6013. 'schema' => [
  6014. 'stuff' => ['type' => 'string', 'length' => 10],
  6015. ],
  6016. ]);
  6017. $result = $this->Form->control('other', ['type' => 'textarea']);
  6018. $expected = [
  6019. 'div' => ['class' => 'input textarea'],
  6020. 'label' => ['for' => 'other'],
  6021. 'Other',
  6022. '/label',
  6023. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6024. '/textarea',
  6025. '/div',
  6026. ];
  6027. $this->assertHtml($expected, $result);
  6028. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6029. $expected = [
  6030. 'div' => ['class' => 'input textarea'],
  6031. 'label' => ['for' => 'stuff'],
  6032. 'Stuff',
  6033. '/label',
  6034. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6035. '/textarea',
  6036. '/div',
  6037. ];
  6038. $this->assertHtml($expected, $result);
  6039. }
  6040. /**
  6041. * testHiddenField method
  6042. *
  6043. * Test generation of a hidden input.
  6044. *
  6045. * @return void
  6046. */
  6047. public function testHidden()
  6048. {
  6049. $this->article['errors'] = [
  6050. 'field' => true,
  6051. ];
  6052. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6053. $this->Form->create($this->article);
  6054. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6055. $expected = [
  6056. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6057. $this->assertHtml($expected, $result);
  6058. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6059. $expected = [
  6060. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6061. ];
  6062. $this->assertHtml($expected, $result);
  6063. }
  6064. /**
  6065. * Test hidden() with various boolean values.
  6066. *
  6067. * @return void
  6068. */
  6069. public function testHiddenBooleanValues()
  6070. {
  6071. $this->Form->create($this->article);
  6072. $result = $this->Form->hidden('field', ['value' => null]);
  6073. $expected = [
  6074. 'input' => ['type' => 'hidden', 'name' => 'field'],
  6075. ];
  6076. $this->assertHtml($expected, $result);
  6077. $result = $this->Form->hidden('field', ['value' => true]);
  6078. $expected = [
  6079. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '1'],
  6080. ];
  6081. $this->assertHtml($expected, $result);
  6082. $result = $this->Form->hidden('field', ['value' => false]);
  6083. $expected = [
  6084. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => '0'],
  6085. ];
  6086. $this->assertHtml($expected, $result);
  6087. }
  6088. /**
  6089. * testFileUploadField method
  6090. *
  6091. * Test generation of a file upload input.
  6092. *
  6093. * @return void
  6094. */
  6095. public function testFileUploadField()
  6096. {
  6097. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6098. $result = $this->Form->file('Model.upload');
  6099. $this->assertHtml($expected, $result);
  6100. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6101. 'name' => '', 'type' => '', 'tmp_name' => '',
  6102. 'error' => 4, 'size' => 0,
  6103. ]));
  6104. $result = $this->Form->file('Model.upload');
  6105. $this->assertHtml($expected, $result);
  6106. $this->View->setRequest(
  6107. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6108. );
  6109. $result = $this->Form->file('Model.upload');
  6110. $this->assertHtml($expected, $result);
  6111. }
  6112. /**
  6113. * testFileUploadOnOtherModel method
  6114. *
  6115. * Test File upload input on a model not used in create().
  6116. *
  6117. * @return void
  6118. */
  6119. public function testFileUploadOnOtherModel()
  6120. {
  6121. $this->Form->create($this->article, ['type' => 'file']);
  6122. $result = $this->Form->file('ValidateProfile.city');
  6123. $expected = [
  6124. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6125. ];
  6126. $this->assertHtml($expected, $result);
  6127. }
  6128. /**
  6129. * testButton method
  6130. *
  6131. * Test generation of a form button.
  6132. *
  6133. * @return void
  6134. */
  6135. public function testButton()
  6136. {
  6137. $result = $this->Form->button('Hi');
  6138. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6139. $this->assertHtml($expected, $result);
  6140. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  6141. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6142. $this->assertHtml($expected, $result);
  6143. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  6144. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6145. $this->assertHtml($expected, $result);
  6146. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  6147. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6148. $this->assertHtml($expected, $result);
  6149. $result = $this->Form->button('No type', ['type' => false]);
  6150. $expected = ['button' => [], 'No type', '/button'];
  6151. $this->assertHtml($expected, $result);
  6152. $result = $this->Form->button('Upload Text', [
  6153. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6154. 'escape' => false,
  6155. ]);
  6156. $this->assertNotRegExp('/\&039/', $result);
  6157. }
  6158. /**
  6159. * testButtonUnlockedByDefault method
  6160. *
  6161. * Test that button() makes unlocked fields by default.
  6162. *
  6163. * @return void
  6164. */
  6165. public function testButtonUnlockedByDefault()
  6166. {
  6167. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6168. $this->Form->create();
  6169. $this->Form->button('Save', ['name' => 'save']);
  6170. $this->Form->button('Clear');
  6171. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6172. $this->assertEquals(['save'], $result);
  6173. }
  6174. /**
  6175. * Test generation of a form button with confirm message.
  6176. *
  6177. * @return void
  6178. */
  6179. public function testButtonWithConfirm()
  6180. {
  6181. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6182. $expected = ['button' => [
  6183. 'type' => 'submit',
  6184. 'data-confirm-message' => 'Confirm me!',
  6185. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6186. ], 'Hi', '/button'];
  6187. $this->assertHtml($expected, $result);
  6188. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6189. $expected = ['button' => [
  6190. 'type' => 'submit',
  6191. 'data-confirm-message' => 'Confirm "me"!',
  6192. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6193. ], 'Hi', '/button'];
  6194. $this->assertHtml($expected, $result);
  6195. }
  6196. /**
  6197. * testPostButton method
  6198. *
  6199. * @return void
  6200. */
  6201. public function testPostButton()
  6202. {
  6203. $result = $this->Form->postButton('Hi', '/controller/action');
  6204. $expected = [
  6205. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6206. 'button' => ['type' => 'submit'],
  6207. 'Hi',
  6208. '/button',
  6209. '/form',
  6210. ];
  6211. $this->assertHtml($expected, $result);
  6212. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6213. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6214. }
  6215. /**
  6216. * testPostButtonMethodType method
  6217. *
  6218. * @return void
  6219. */
  6220. public function testPostButtonMethodType()
  6221. {
  6222. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6223. $expected = [
  6224. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6225. 'div' => ['style' => 'display:none;'],
  6226. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6227. '/div',
  6228. 'button' => ['type' => 'submit'],
  6229. 'Hi',
  6230. '/button',
  6231. '/form',
  6232. ];
  6233. $this->assertHtml($expected, $result);
  6234. }
  6235. /**
  6236. * testPostButtonFormOptions method
  6237. *
  6238. * @return void
  6239. */
  6240. public function testPostButtonFormOptions()
  6241. {
  6242. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6243. $expected = [
  6244. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6245. 'button' => ['type' => 'submit'],
  6246. 'Hi',
  6247. '/button',
  6248. '/form',
  6249. ];
  6250. $this->assertHtml($expected, $result);
  6251. }
  6252. /**
  6253. * testPostButtonNestedData method
  6254. *
  6255. * Test using postButton with N dimensional data.
  6256. *
  6257. * @return void
  6258. */
  6259. public function testPostButtonNestedData()
  6260. {
  6261. $data = [
  6262. 'one' => [
  6263. 'two' => [
  6264. 3, 4, 5,
  6265. ],
  6266. ],
  6267. ];
  6268. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6269. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6270. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6271. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6272. }
  6273. /**
  6274. * testSecurePostButton method
  6275. *
  6276. * Test that postButton adds _Token fields.
  6277. *
  6278. * @return void
  6279. */
  6280. public function testSecurePostButton()
  6281. {
  6282. $this->View->setRequest($this->View->getRequest()
  6283. ->withAttribute('csrfToken', 'testkey')
  6284. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6285. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6286. $tokenDebug = urlencode(json_encode([
  6287. '/posts/delete/1',
  6288. [],
  6289. [],
  6290. ]));
  6291. $expected = [
  6292. 'form' => [
  6293. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6294. ],
  6295. ['div' => ['style' => 'display:none;']],
  6296. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6297. '/div',
  6298. 'button' => ['type' => 'submit'],
  6299. 'Delete',
  6300. '/button',
  6301. ['div' => ['style' => 'display:none;']],
  6302. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6303. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6304. ['input' => [
  6305. 'type' => 'hidden',
  6306. 'name' => '_Token[debug]',
  6307. 'value' => $tokenDebug,
  6308. 'autocomplete' => 'off',
  6309. ]],
  6310. '/div',
  6311. '/form',
  6312. ];
  6313. $this->assertHtml($expected, $result);
  6314. }
  6315. /**
  6316. * testPostLink method
  6317. *
  6318. * @return void
  6319. */
  6320. public function testPostLink()
  6321. {
  6322. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6323. $expected = [
  6324. 'form' => [
  6325. 'method' => 'post', 'action' => '/posts/delete/1',
  6326. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6327. ],
  6328. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6329. '/form',
  6330. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6331. 'Delete',
  6332. '/a',
  6333. ];
  6334. $this->assertHtml($expected, $result);
  6335. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6336. $expected = [
  6337. 'form' => [
  6338. 'method' => 'post', 'action' => '/posts/delete/1',
  6339. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6340. ],
  6341. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6342. '/form',
  6343. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6344. 'Delete',
  6345. '/a',
  6346. ];
  6347. $this->assertHtml($expected, $result);
  6348. $result = $this->Form->postLink(
  6349. 'Delete',
  6350. '/posts/delete/1',
  6351. ['target' => '_blank', 'class' => 'btn btn-danger']
  6352. );
  6353. $expected = [
  6354. 'form' => [
  6355. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6356. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6357. ],
  6358. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6359. '/form',
  6360. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6361. 'Delete',
  6362. '/a',
  6363. ];
  6364. $this->assertHtml($expected, $result);
  6365. }
  6366. /**
  6367. * testPostLinkWithConfirm method
  6368. *
  6369. * Test the confirm option for postLink().
  6370. *
  6371. * @return void
  6372. */
  6373. public function testPostLinkWithConfirm()
  6374. {
  6375. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6376. $expected = [
  6377. 'form' => [
  6378. 'method' => 'post', 'action' => '/posts/delete/1',
  6379. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6380. ],
  6381. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6382. '/form',
  6383. 'a' => [
  6384. 'href' => '#',
  6385. 'data-confirm-message' => 'Confirm?',
  6386. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6387. ],
  6388. 'Delete',
  6389. '/a',
  6390. ];
  6391. $this->assertHtml($expected, $result);
  6392. $result = $this->Form->postLink(
  6393. 'Delete',
  6394. '/posts/delete/1',
  6395. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6396. );
  6397. $expected = [
  6398. 'form' => [
  6399. 'method' => 'post', 'action' => '/posts/delete/1',
  6400. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6401. ],
  6402. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6403. '/form',
  6404. 'a' => [
  6405. 'href' => '#',
  6406. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6407. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6408. ],
  6409. 'Delete',
  6410. '/a',
  6411. ];
  6412. $this->assertHtml($expected, $result);
  6413. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6414. $result = $this->Form->postLink(
  6415. 'Delete',
  6416. '/posts/delete/1',
  6417. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6418. );
  6419. $expected = [
  6420. 'form' => [
  6421. 'method' => 'post', 'action' => '/posts/delete/1',
  6422. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6423. ],
  6424. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6425. '/form',
  6426. 'a' => [
  6427. 'href' => '#',
  6428. 'data-confirm-message' => 'Confirm this deletion?',
  6429. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6430. ],
  6431. 'Delete',
  6432. '/a',
  6433. ];
  6434. $this->assertHtml($expected, $result);
  6435. }
  6436. /**
  6437. * testPostLinkWithQuery method
  6438. *
  6439. * Test postLink() with query string args.
  6440. *
  6441. * @return void
  6442. */
  6443. public function testPostLinkWithQuery()
  6444. {
  6445. $result = $this->Form->postLink(
  6446. 'Delete',
  6447. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6448. );
  6449. $expected = [
  6450. 'form' => [
  6451. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6452. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6453. ],
  6454. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6455. '/form',
  6456. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6457. 'Delete',
  6458. '/a',
  6459. ];
  6460. $this->assertHtml($expected, $result);
  6461. }
  6462. /**
  6463. * testPostLinkWithData method
  6464. *
  6465. * Test postLink with additional data.
  6466. *
  6467. * @return void
  6468. */
  6469. public function testPostLinkWithData()
  6470. {
  6471. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6472. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6473. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6474. $this->Form->create($entity);
  6475. $this->Form->end();
  6476. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6477. $this->assertStringContainsString(
  6478. '<input type="hidden" name="name" value="show"',
  6479. $result,
  6480. 'should not contain entity data.'
  6481. );
  6482. }
  6483. /**
  6484. * testPostLinkSecurityHash method
  6485. *
  6486. * Test that security hashes for postLink include the url.
  6487. *
  6488. * @return void
  6489. */
  6490. public function testPostLinkSecurityHash()
  6491. {
  6492. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6493. $hash .= '%3Aid';
  6494. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6495. $result = $this->Form->postLink(
  6496. 'Delete',
  6497. '/posts/delete/1',
  6498. ['data' => ['id' => 1]]
  6499. );
  6500. $tokenDebug = urlencode(json_encode([
  6501. '/posts/delete/1',
  6502. [
  6503. 'id' => 1,
  6504. ],
  6505. [],
  6506. ]));
  6507. $expected = [
  6508. 'form' => [
  6509. 'method' => 'post', 'action' => '/posts/delete/1',
  6510. 'name', 'style' => 'display:none;',
  6511. ],
  6512. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6513. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6514. 'div' => ['style' => 'display:none;'],
  6515. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6516. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6517. ['input' => [
  6518. 'type' => 'hidden',
  6519. 'name' => '_Token[debug]',
  6520. 'value' => $tokenDebug,
  6521. 'autocomplete' => 'off',
  6522. ]],
  6523. '/div',
  6524. '/form',
  6525. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6526. 'Delete',
  6527. '/a',
  6528. ];
  6529. $this->assertHtml($expected, $result);
  6530. }
  6531. /**
  6532. * testPostLinkSecurityHashBlockMode method
  6533. *
  6534. * Test that postLink doesn't modify the fields in the containing form.
  6535. *
  6536. * postLink() calls inside open forms should not modify the field list
  6537. * for the form.
  6538. *
  6539. * @return void
  6540. */
  6541. public function testPostLinkSecurityHashBlockMode()
  6542. {
  6543. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6544. $hash .= '%3A';
  6545. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6546. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6547. $this->Form->control('title');
  6548. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6549. $result = $this->View->fetch('postLink');
  6550. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6551. $this->assertEquals(['title'], $fields);
  6552. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6553. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6554. $reflect->setAccessible(true);
  6555. $this->assertEquals('/articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6556. }
  6557. /**
  6558. * testPostLinkSecurityHashNoDebugMode method
  6559. *
  6560. * Test that security does not include debug token if debug is false.
  6561. *
  6562. * @return void
  6563. */
  6564. public function testPostLinkSecurityHashNoDebugMode()
  6565. {
  6566. Configure::write('debug', false);
  6567. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6568. $hash .= '%3Aid';
  6569. $this->View->setRequest($this->View->getRequest()
  6570. ->withAttribute('formTokenData', ['key' => 'test']));
  6571. $result = $this->Form->postLink(
  6572. 'Delete',
  6573. '/posts/delete/1',
  6574. ['data' => ['id' => 1]]
  6575. );
  6576. $expected = [
  6577. 'form' => [
  6578. 'method' => 'post', 'action' => '/posts/delete/1',
  6579. 'name', 'style' => 'display:none;',
  6580. ],
  6581. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6582. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6583. 'div' => ['style' => 'display:none;'],
  6584. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6585. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6586. '/div',
  6587. '/form',
  6588. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6589. 'Delete',
  6590. '/a',
  6591. ];
  6592. $this->assertHtml($expected, $result);
  6593. }
  6594. /**
  6595. * testPostLinkNestedData method
  6596. *
  6597. * Test using postLink with N dimensional data.
  6598. *
  6599. * @return void
  6600. */
  6601. public function testPostLinkNestedData()
  6602. {
  6603. $data = [
  6604. 'one' => [
  6605. 'two' => [
  6606. 3, 4, 5,
  6607. ],
  6608. ],
  6609. ];
  6610. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6611. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6612. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6613. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6614. }
  6615. /**
  6616. * testPostLinkAfterGetForm method
  6617. *
  6618. * Test creating postLinks after a GET form.
  6619. *
  6620. * @return void
  6621. */
  6622. public function testPostLinkAfterGetForm()
  6623. {
  6624. $this->View->setRequest($this->View->getRequest()
  6625. ->withAttribute('csrfToken', 'testkey')
  6626. ->withAttribute('formTokenData', []));
  6627. $this->Form->create($this->article, ['type' => 'get']);
  6628. $this->Form->end();
  6629. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6630. $tokenDebug = urlencode(json_encode([
  6631. '/posts/delete/1',
  6632. [],
  6633. [],
  6634. ]));
  6635. $expected = [
  6636. 'form' => [
  6637. 'method' => 'post', 'action' => '/posts/delete/1',
  6638. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6639. ],
  6640. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6641. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6642. 'div' => ['style' => 'display:none;'],
  6643. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6644. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6645. ['input' => [
  6646. 'type' => 'hidden', 'name' => '_Token[debug]',
  6647. 'value' => $tokenDebug,
  6648. 'autocomplete' => 'off',
  6649. ]],
  6650. '/div',
  6651. '/form',
  6652. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6653. 'Delete',
  6654. '/a',
  6655. ];
  6656. $this->assertHtml($expected, $result);
  6657. }
  6658. /**
  6659. * testPostLinkFormBuffer method
  6660. *
  6661. * Test that postLink adds form tags to view block.
  6662. *
  6663. * @return void
  6664. */
  6665. public function testPostLinkFormBuffer()
  6666. {
  6667. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6668. $expected = [
  6669. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6670. 'Delete',
  6671. '/a',
  6672. ];
  6673. $this->assertHtml($expected, $result);
  6674. $result = $this->View->fetch('postLink');
  6675. $expected = [
  6676. 'form' => [
  6677. 'method' => 'post', 'action' => '/posts/delete/1',
  6678. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6679. ],
  6680. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6681. '/form',
  6682. ];
  6683. $this->assertHtml($expected, $result);
  6684. $result = $this->Form->postLink(
  6685. 'Delete',
  6686. '/posts/delete/2',
  6687. ['block' => true, 'method' => 'DELETE']
  6688. );
  6689. $expected = [
  6690. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6691. 'Delete',
  6692. '/a',
  6693. ];
  6694. $this->assertHtml($expected, $result);
  6695. $result = $this->View->fetch('postLink');
  6696. $expected = [
  6697. 'form' => [
  6698. 'method' => 'post', 'action' => '/posts/delete/1',
  6699. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6700. ],
  6701. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6702. '/form',
  6703. [
  6704. 'form' => [
  6705. 'method' => 'post', 'action' => '/posts/delete/2',
  6706. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6707. ],
  6708. ],
  6709. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6710. '/form',
  6711. ];
  6712. $this->assertHtml($expected, $result);
  6713. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6714. $expected = [
  6715. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6716. 'Delete',
  6717. '/a',
  6718. ];
  6719. $this->assertHtml($expected, $result);
  6720. $result = $this->View->fetch('foobar');
  6721. $expected = [
  6722. 'form' => [
  6723. 'method' => 'post', 'action' => '/posts/delete/1',
  6724. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6725. ],
  6726. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6727. '/form',
  6728. ];
  6729. $this->assertHtml($expected, $result);
  6730. }
  6731. /**
  6732. * testSubmitButton method
  6733. *
  6734. * @return void
  6735. */
  6736. public function testSubmitButton()
  6737. {
  6738. $result = $this->Form->submit('');
  6739. $expected = [
  6740. 'div' => ['class' => 'submit'],
  6741. 'input' => ['type' => 'submit', 'value' => ''],
  6742. '/div',
  6743. ];
  6744. $this->assertHtml($expected, $result);
  6745. $result = $this->Form->submit('Test Submit');
  6746. $expected = [
  6747. 'div' => ['class' => 'submit'],
  6748. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6749. '/div',
  6750. ];
  6751. $this->assertHtml($expected, $result);
  6752. $result = $this->Form->submit('Next >');
  6753. $expected = [
  6754. 'div' => ['class' => 'submit'],
  6755. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6756. '/div',
  6757. ];
  6758. $this->assertHtml($expected, $result);
  6759. $result = $this->Form->submit('Next >', ['escape' => false]);
  6760. $expected = [
  6761. 'div' => ['class' => 'submit'],
  6762. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6763. '/div',
  6764. ];
  6765. $this->assertHtml($expected, $result);
  6766. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6767. $expected = [
  6768. 'div' => ['class' => 'submit'],
  6769. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6770. '/div',
  6771. ];
  6772. $this->assertHtml($expected, $result);
  6773. }
  6774. /**
  6775. * testSubmitImage method
  6776. *
  6777. * Test image submit types.
  6778. *
  6779. * @return void
  6780. */
  6781. public function testSubmitImage()
  6782. {
  6783. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6784. $expected = [
  6785. 'div' => ['class' => 'submit'],
  6786. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6787. '/div',
  6788. ];
  6789. $this->assertHtml($expected, $result);
  6790. $result = $this->Form->submit('/relative/cake.power.gif');
  6791. $expected = [
  6792. 'div' => ['class' => 'submit'],
  6793. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6794. '/div',
  6795. ];
  6796. $this->assertHtml($expected, $result);
  6797. $result = $this->Form->submit('cake.power.gif');
  6798. $expected = [
  6799. 'div' => ['class' => 'submit'],
  6800. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6801. '/div',
  6802. ];
  6803. $this->assertHtml($expected, $result);
  6804. $result = $this->Form->submit('Not.an.image');
  6805. $expected = [
  6806. 'div' => ['class' => 'submit'],
  6807. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6808. '/div',
  6809. ];
  6810. $this->assertHtml($expected, $result);
  6811. }
  6812. /**
  6813. * testSubmitUnlockedByDefault method
  6814. *
  6815. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6816. * be submitted at a time.
  6817. *
  6818. * @return void
  6819. */
  6820. public function testSubmitUnlockedByDefault()
  6821. {
  6822. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6823. $this->Form->create();
  6824. $this->Form->submit('Go go');
  6825. $this->Form->submit('Save', ['name' => 'save']);
  6826. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6827. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6828. }
  6829. /**
  6830. * testSubmitImageTimestamp method
  6831. *
  6832. * Test submit image with timestamps.
  6833. *
  6834. * @return void
  6835. */
  6836. public function testSubmitImageTimestamp()
  6837. {
  6838. Configure::write('Asset.timestamp', 'force');
  6839. $result = $this->Form->submit('cake.power.gif');
  6840. $expected = [
  6841. 'div' => ['class' => 'submit'],
  6842. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6843. '/div',
  6844. ];
  6845. $this->assertHtml($expected, $result);
  6846. }
  6847. /**
  6848. * testDateTimeWithGetForms method
  6849. *
  6850. * Test that datetime() works with GET style forms.
  6851. *
  6852. * @return void
  6853. */
  6854. public function testDateTimeWithGetForms()
  6855. {
  6856. $this->Form->create($this->article, ['type' => 'get']);
  6857. $result = $this->Form->datetime('created');
  6858. $expected = [
  6859. 'input' => [
  6860. 'type' => 'datetime-local',
  6861. 'name' => 'created',
  6862. 'value' => '',
  6863. 'step' => '1',
  6864. ],
  6865. ];
  6866. $this->assertHtml($expected, $result);
  6867. $result = $this->Form->datetime('created', ['default' => true]);
  6868. $expected = [
  6869. 'input' => [
  6870. 'type' => 'datetime-local',
  6871. 'name' => 'created',
  6872. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
  6873. 'step' => '1',
  6874. ],
  6875. ];
  6876. $this->assertHtml($expected, $result);
  6877. }
  6878. /**
  6879. * Provides fractional schema types
  6880. *
  6881. * @return array
  6882. */
  6883. public function fractionalTypeProvider()
  6884. {
  6885. return [
  6886. ['datetimefractional'],
  6887. ['timestampfractional'],
  6888. ['timestamptimezone'],
  6889. ];
  6890. }
  6891. /**
  6892. * testDateTimeWithFractional method
  6893. *
  6894. * Test that datetime() works with datetimefractional.
  6895. *
  6896. * @return void
  6897. * @dataProvider fractionalTypeProvider
  6898. */
  6899. public function testDateTimeWithFractional(string $type)
  6900. {
  6901. $this->Form->create([
  6902. 'schema' => [
  6903. 'created' => ['type' => $type],
  6904. ],
  6905. ]);
  6906. $result = $this->Form->datetime('created', [
  6907. 'val' => new FrozenTime('2019-09-27 02:52:43.123'),
  6908. ]);
  6909. $expected = [
  6910. 'input' => [
  6911. 'type' => 'datetime-local',
  6912. 'name' => 'created',
  6913. 'value' => '2019-09-27T02:52:43.123',
  6914. 'step' => '0.001',
  6915. ],
  6916. ];
  6917. $this->assertHtml($expected, $result);
  6918. }
  6919. /**
  6920. * testControlWithFractional method
  6921. *
  6922. * Test that control() works with datetimefractional.
  6923. *
  6924. * @return void
  6925. * @dataProvider fractionalTypeProvider
  6926. */
  6927. public function testControlWithFractional(string $type)
  6928. {
  6929. $this->Form->create([
  6930. 'schema' => [
  6931. 'created' => ['type' => $type],
  6932. ],
  6933. ]);
  6934. $result = $this->Form->control('created', [
  6935. 'val' => new FrozenTime('2019-09-27 02:52:43.123'),
  6936. ]);
  6937. $expected = [
  6938. 'div' => ['class' => 'input datetime'],
  6939. 'label' => ['for' => 'created'],
  6940. 'Created',
  6941. '/label',
  6942. 'input' => [
  6943. 'type' => 'datetime-local',
  6944. 'name' => 'created',
  6945. 'id' => 'created',
  6946. 'value' => '2019-09-27T02:52:43.123',
  6947. 'step' => '0.001',
  6948. ],
  6949. '/div',
  6950. ];
  6951. $this->assertHtml($expected, $result);
  6952. }
  6953. /**
  6954. * testForMagicControlNonExistingNorValidated method
  6955. *
  6956. * @return void
  6957. */
  6958. public function testForMagicControlNonExistingNorValidated()
  6959. {
  6960. $this->Form->create($this->article);
  6961. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6962. $result = $this->Form->control('non_existing_nor_validated');
  6963. $expected = [
  6964. 'label' => ['for' => 'non-existing-nor-validated'],
  6965. 'Non Existing Nor Validated',
  6966. '/label',
  6967. 'input' => [
  6968. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6969. 'id' => 'non-existing-nor-validated',
  6970. ],
  6971. ];
  6972. $this->assertHtml($expected, $result);
  6973. $result = $this->Form->control('non_existing_nor_validated', [
  6974. 'val' => 'my value',
  6975. ]);
  6976. $expected = [
  6977. 'label' => ['for' => 'non-existing-nor-validated'],
  6978. 'Non Existing Nor Validated',
  6979. '/label',
  6980. 'input' => [
  6981. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6982. 'value' => 'my value', 'id' => 'non-existing-nor-validated',
  6983. ],
  6984. ];
  6985. $this->assertHtml($expected, $result);
  6986. $this->View->setRequest(
  6987. $this->View->getRequest()->withData('non_existing_nor_validated', 'CakePHP magic')
  6988. );
  6989. $this->Form->create($this->article);
  6990. $result = $this->Form->control('non_existing_nor_validated');
  6991. $expected = [
  6992. 'label' => ['for' => 'non-existing-nor-validated'],
  6993. 'Non Existing Nor Validated',
  6994. '/label',
  6995. 'input' => [
  6996. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6997. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated',
  6998. ],
  6999. ];
  7000. $this->assertHtml($expected, $result);
  7001. }
  7002. /**
  7003. * testFormMagicControlLabel method
  7004. *
  7005. * @return void
  7006. */
  7007. public function testFormMagicControlLabel()
  7008. {
  7009. $this->getTableLocator()->get('Contacts', [
  7010. 'className' => ContactsTable::class,
  7011. ]);
  7012. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7013. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7014. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7015. $expected = [
  7016. 'label' => ['for' => 'contacts-name'],
  7017. 'My label',
  7018. '/label',
  7019. 'input' => [
  7020. 'type' => 'text', 'name' => 'Contacts[name]',
  7021. 'id' => 'contacts-name', 'maxlength' => '255',
  7022. ],
  7023. ];
  7024. $this->assertHtml($expected, $result);
  7025. $result = $this->Form->control('name', [
  7026. 'label' => ['class' => 'mandatory'],
  7027. ]);
  7028. $expected = [
  7029. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7030. 'Name',
  7031. '/label',
  7032. 'input' => [
  7033. 'type' => 'text', 'name' => 'name',
  7034. 'id' => 'name', 'maxlength' => '255',
  7035. ],
  7036. ];
  7037. $this->assertHtml($expected, $result);
  7038. $result = $this->Form->control('name', [
  7039. 'div' => false,
  7040. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  7041. ]);
  7042. $expected = [
  7043. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7044. 'My label',
  7045. '/label',
  7046. 'input' => [
  7047. 'type' => 'text', 'name' => 'name',
  7048. 'id' => 'name', 'maxlength' => '255',
  7049. ],
  7050. ];
  7051. $this->assertHtml($expected, $result);
  7052. $result = $this->Form->control('Contact.name', [
  7053. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  7054. ]);
  7055. $expected = [
  7056. 'label' => ['for' => 'my_id'],
  7057. 'Name',
  7058. '/label',
  7059. 'input' => [
  7060. 'type' => 'text', 'name' => 'Contact[name]',
  7061. 'id' => 'my_id', 'maxlength' => '255',
  7062. ],
  7063. ];
  7064. $this->assertHtml($expected, $result);
  7065. $result = $this->Form->control('1.id');
  7066. $expected = ['input' => [
  7067. 'type' => 'hidden', 'name' => '1[id]',
  7068. 'id' => '1-id',
  7069. ]];
  7070. $this->assertHtml($expected, $result);
  7071. $result = $this->Form->control('1.name');
  7072. $expected = [
  7073. 'label' => ['for' => '1-name'],
  7074. 'Name',
  7075. '/label',
  7076. 'input' => [
  7077. 'type' => 'text', 'name' => '1[name]',
  7078. 'id' => '1-name', 'maxlength' => '255',
  7079. ],
  7080. ];
  7081. $this->assertHtml($expected, $result);
  7082. }
  7083. /**
  7084. * testFormEnd method
  7085. *
  7086. * @return void
  7087. */
  7088. public function testFormEnd()
  7089. {
  7090. $this->assertSame('</form>', $this->Form->end());
  7091. }
  7092. /**
  7093. * testMultiRecordForm method
  7094. *
  7095. * Test the generation of fields for a multi record form.
  7096. *
  7097. * @return void
  7098. */
  7099. public function testMultiRecordForm()
  7100. {
  7101. $this->loadFixtures('Articles', 'Comments');
  7102. $articles = $this->getTableLocator()->get('Articles');
  7103. $articles->hasMany('Comments');
  7104. $comment = new Entity(['comment' => 'Value']);
  7105. $article = new Article(['comments' => [$comment]]);
  7106. $this->Form->create([$article]);
  7107. $result = $this->Form->control('0.comments.1.comment');
  7108. // phpcs:disable
  7109. $expected = [
  7110. 'div' => ['class' => 'input textarea'],
  7111. 'label' => ['for' => '0-comments-1-comment'],
  7112. 'Comment',
  7113. '/label',
  7114. 'textarea' => [
  7115. 'name',
  7116. 'id' => '0-comments-1-comment',
  7117. 'rows' => 5
  7118. ],
  7119. '/textarea',
  7120. '/div'
  7121. ];
  7122. // phpcs:enable
  7123. $this->assertHtml($expected, $result);
  7124. $result = $this->Form->control('0.comments.0.comment');
  7125. // phpcs:disable
  7126. $expected = [
  7127. 'div' => ['class' => 'input textarea'],
  7128. 'label' => ['for' => '0-comments-0-comment'],
  7129. 'Comment',
  7130. '/label',
  7131. 'textarea' => [
  7132. 'name',
  7133. 'id' => '0-comments-0-comment',
  7134. 'rows' => 5
  7135. ],
  7136. 'Value',
  7137. '/textarea',
  7138. '/div'
  7139. ];
  7140. // phpcs:enable
  7141. $this->assertHtml($expected, $result);
  7142. $comment->setError('comment', ['Not valid']);
  7143. $result = $this->Form->control('0.comments.0.comment');
  7144. // phpcs:disable
  7145. $expected = [
  7146. 'div' => ['class' => 'input textarea error'],
  7147. 'label' => ['for' => '0-comments-0-comment'],
  7148. 'Comment',
  7149. '/label',
  7150. 'textarea' => [
  7151. 'name',
  7152. 'class' => 'form-error',
  7153. 'id' => '0-comments-0-comment',
  7154. 'rows' => 5
  7155. ],
  7156. 'Value',
  7157. '/textarea',
  7158. ['div' => ['class' => 'error-message']],
  7159. 'Not valid',
  7160. '/div',
  7161. '/div'
  7162. ];
  7163. // phpcs:enable
  7164. $this->assertHtml($expected, $result);
  7165. $this->getTableLocator()->get('Comments')
  7166. ->getValidator('default')
  7167. ->allowEmptyString('comment', null, false);
  7168. $result = $this->Form->control('0.comments.1.comment');
  7169. // phpcs:disable
  7170. $expected = [
  7171. 'div' => ['class' => 'input textarea required'],
  7172. 'label' => ['for' => '0-comments-1-comment'],
  7173. 'Comment',
  7174. '/label',
  7175. 'textarea' => [
  7176. 'name',
  7177. 'required' => 'required',
  7178. 'id' => '0-comments-1-comment',
  7179. 'rows' => 5,
  7180. 'data-validity-message' => 'This field cannot be left empty',
  7181. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7182. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7183. ],
  7184. '/textarea',
  7185. '/div'
  7186. ];
  7187. // phpcs:enable
  7188. $this->assertHtml($expected, $result);
  7189. }
  7190. /**
  7191. * testHtml5Controls method
  7192. *
  7193. * Test that some html5 inputs + FormHelper::__call() work.
  7194. *
  7195. * @return void
  7196. */
  7197. public function testHtml5Controls()
  7198. {
  7199. $result = $this->Form->email('User.email');
  7200. $expected = [
  7201. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7202. ];
  7203. $this->assertHtml($expected, $result);
  7204. $result = $this->Form->search('User.query');
  7205. $expected = [
  7206. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7207. ];
  7208. $this->assertHtml($expected, $result);
  7209. $result = $this->Form->search('User.query', ['value' => 'test']);
  7210. $expected = [
  7211. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7212. ];
  7213. $this->assertHtml($expected, $result);
  7214. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7215. $expected = [
  7216. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7217. ];
  7218. $this->assertHtml($expected, $result);
  7219. }
  7220. /**
  7221. * testHtml5ControlWithControl method
  7222. *
  7223. * Test accessing html5 inputs through control().
  7224. *
  7225. * @return void
  7226. */
  7227. public function testHtml5ControlWithControl()
  7228. {
  7229. $this->Form->create();
  7230. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7231. $result = $this->Form->control('website', [
  7232. 'type' => 'url',
  7233. 'val' => 'http://domain.tld',
  7234. 'label' => false,
  7235. ]);
  7236. $expected = [
  7237. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7238. ];
  7239. $this->assertHtml($expected, $result);
  7240. }
  7241. /**
  7242. * testHtml5ControlException method
  7243. *
  7244. * Test errors when field name is missing.
  7245. *
  7246. * @return void
  7247. */
  7248. public function testHtml5ControlException()
  7249. {
  7250. $this->expectException(\Cake\Core\Exception\Exception::class);
  7251. $this->Form->email();
  7252. }
  7253. /**
  7254. * tests fields that are required use custom validation messages
  7255. *
  7256. * @return void
  7257. */
  7258. public function testHtml5ErrorMessage()
  7259. {
  7260. $this->Form->setConfig('autoSetCustomValidity', true);
  7261. $validator = (new \Cake\Validation\Validator())
  7262. ->notEmptyString('email', 'Custom error message')
  7263. ->requirePresence('password')
  7264. ->alphaNumeric('password')
  7265. ->notBlank('phone');
  7266. $table = $this->getTableLocator()->get('Contacts', [
  7267. 'className' => ContactsTable::class,
  7268. ]);
  7269. $table->setValidator('default', $validator);
  7270. $contact = new Entity();
  7271. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7272. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7273. $result = $this->Form->control('password');
  7274. $expected = [
  7275. 'label' => ['for' => 'password'],
  7276. 'Password',
  7277. '/label',
  7278. 'input' => [
  7279. 'id' => 'password',
  7280. 'name' => 'password',
  7281. 'type' => 'password',
  7282. 'value' => '',
  7283. 'required' => 'required',
  7284. 'data-validity-message' => 'This field cannot be left empty',
  7285. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7286. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7287. ],
  7288. ];
  7289. $this->assertHtml($expected, $result);
  7290. $result = $this->Form->control('phone');
  7291. $expected = [
  7292. 'label' => ['for' => 'phone'],
  7293. 'Phone',
  7294. '/label',
  7295. 'input' => [
  7296. 'id' => 'phone',
  7297. 'name' => 'phone',
  7298. 'type' => 'tel',
  7299. 'value' => '',
  7300. 'maxlength' => 255,
  7301. 'required' => 'required',
  7302. 'data-validity-message' => 'This field cannot be left empty',
  7303. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7304. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7305. ],
  7306. ];
  7307. $this->assertHtml($expected, $result);
  7308. $result = $this->Form->control('email');
  7309. $expected = [
  7310. 'label' => ['for' => 'email'],
  7311. 'Email',
  7312. '/label',
  7313. 'input' => [
  7314. 'id' => 'email',
  7315. 'name' => 'email',
  7316. 'type' => 'email',
  7317. 'value' => '',
  7318. 'maxlength' => 255,
  7319. 'required' => 'required',
  7320. 'data-validity-message' => 'Custom error message',
  7321. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7322. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7323. ],
  7324. ];
  7325. $this->assertHtml($expected, $result);
  7326. }
  7327. /**
  7328. * tests that custom validation messages are in templateVars
  7329. *
  7330. * @return void
  7331. */
  7332. public function testHtml5ErrorMessageInTemplateVars()
  7333. {
  7334. $validator = (new \Cake\Validation\Validator())
  7335. ->notEmptyString('email', 'Custom error "message" & entities')
  7336. ->requirePresence('password')
  7337. ->alphaNumeric('password')
  7338. ->notBlank('phone');
  7339. $table = $this->getTableLocator()->get('Contacts', [
  7340. 'className' => ContactsTable::class,
  7341. ]);
  7342. $table->setValidator('default', $validator);
  7343. $contact = new Entity();
  7344. $this->Form->setConfig('autoSetCustomValidity', false);
  7345. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7346. $this->Form->setTemplates([
  7347. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7348. 'inputContainer' => '{{content}}',
  7349. ]);
  7350. $result = $this->Form->control('password');
  7351. $expected = [
  7352. 'label' => ['for' => 'password'],
  7353. 'Password',
  7354. '/label',
  7355. 'input' => [
  7356. 'id' => 'password',
  7357. 'name' => 'password',
  7358. 'type' => 'password',
  7359. 'value' => '',
  7360. 'required' => 'required',
  7361. 'data-message' => 'This field cannot be left empty',
  7362. ],
  7363. ];
  7364. $this->assertHtml($expected, $result);
  7365. $result = $this->Form->control('phone');
  7366. $expected = [
  7367. 'label' => ['for' => 'phone'],
  7368. 'Phone',
  7369. '/label',
  7370. 'input' => [
  7371. 'id' => 'phone',
  7372. 'name' => 'phone',
  7373. 'type' => 'tel',
  7374. 'value' => '',
  7375. 'maxlength' => 255,
  7376. 'required' => 'required',
  7377. 'data-message' => 'This field cannot be left empty',
  7378. ],
  7379. ];
  7380. $this->assertHtml($expected, $result);
  7381. $result = $this->Form->control('email', [
  7382. 'templateVars' => [
  7383. 'custom' => 'data-custom="1"',
  7384. ],
  7385. ]);
  7386. $expected = [
  7387. 'label' => ['for' => 'email'],
  7388. 'Email',
  7389. '/label',
  7390. 'input' => [
  7391. 'id' => 'email',
  7392. 'name' => 'email',
  7393. 'type' => 'email',
  7394. 'value' => '',
  7395. 'maxlength' => 255,
  7396. 'required' => 'required',
  7397. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7398. 'data-custom' => '1',
  7399. ],
  7400. ];
  7401. $this->assertHtml($expected, $result);
  7402. $this->Form->setConfig('autoSetCustomValidity', true);
  7403. }
  7404. /**
  7405. * testRequiredAttribute method
  7406. *
  7407. * Tests that formhelper sets required attributes.
  7408. *
  7409. * @return void
  7410. */
  7411. public function testRequiredAttribute()
  7412. {
  7413. $this->article['required'] = [
  7414. 'title' => true,
  7415. 'body' => false,
  7416. ];
  7417. $this->Form->create($this->article);
  7418. $result = $this->Form->control('title');
  7419. $expected = [
  7420. 'div' => ['class' => 'input text required'],
  7421. 'label' => ['for' => 'title'],
  7422. 'Title',
  7423. '/label',
  7424. 'input' => [
  7425. 'type' => 'text',
  7426. 'name' => 'title',
  7427. 'id' => 'title',
  7428. 'required' => 'required',
  7429. 'data-validity-message' => 'This field cannot be left empty',
  7430. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7431. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7432. ],
  7433. '/div',
  7434. ];
  7435. $this->assertHtml($expected, $result);
  7436. $result = $this->Form->control('title', ['required' => false]);
  7437. $this->assertStringNotContainsString('required', $result);
  7438. $result = $this->Form->control('body');
  7439. $expected = [
  7440. 'div' => ['class' => 'input text'],
  7441. 'label' => ['for' => 'body'],
  7442. 'Body',
  7443. '/label',
  7444. 'input' => [
  7445. 'type' => 'text',
  7446. 'name' => 'body',
  7447. 'id' => 'body',
  7448. ],
  7449. '/div',
  7450. ];
  7451. $this->assertHtml($expected, $result);
  7452. $result = $this->Form->control('body', ['required' => true]);
  7453. $this->assertStringContainsString('required', $result);
  7454. }
  7455. /**
  7456. * testControlsNotNested method
  7457. *
  7458. * Tests that it is possible to put inputs outside of the label.
  7459. *
  7460. * @return void
  7461. */
  7462. public function testControlsNotNested()
  7463. {
  7464. $this->Form->setTemplates([
  7465. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7466. 'formGroup' => '{{input}}{{label}}',
  7467. ]);
  7468. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7469. $expected = [
  7470. 'div' => ['class' => 'input checkbox'],
  7471. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7472. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7473. 'label' => ['for' => 'foo'],
  7474. 'Foo',
  7475. '/label',
  7476. '/div',
  7477. ];
  7478. $this->assertHtml($expected, $result);
  7479. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7480. $expected = [
  7481. 'div' => ['class' => 'input checkbox'],
  7482. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7483. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7484. '/div',
  7485. ];
  7486. $this->assertHtml($expected, $result);
  7487. $result = $this->Form->control('confirm', [
  7488. 'type' => 'radio',
  7489. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7490. ]);
  7491. $expected = [
  7492. 'div' => ['class' => 'input radio'],
  7493. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7494. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7495. ['label' => ['for' => 'confirm-y']],
  7496. 'Yes',
  7497. '/label',
  7498. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7499. ['label' => ['for' => 'confirm-n']],
  7500. 'No',
  7501. '/label',
  7502. '<label',
  7503. 'Confirm',
  7504. '/label',
  7505. '/div',
  7506. ];
  7507. $this->assertHtml($expected, $result);
  7508. $result = $this->Form->select('category', ['1', '2'], [
  7509. 'multiple' => 'checkbox',
  7510. 'name' => 'fish',
  7511. ]);
  7512. $expected = [
  7513. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7514. ['div' => ['class' => 'checkbox']],
  7515. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7516. ['label' => ['for' => 'fish-0']],
  7517. '1',
  7518. '/label',
  7519. '/div',
  7520. ['div' => ['class' => 'checkbox']],
  7521. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7522. ['label' => ['for' => 'fish-1']],
  7523. '2',
  7524. '/label',
  7525. '/div',
  7526. ];
  7527. $this->assertHtml($expected, $result);
  7528. }
  7529. /**
  7530. * testControlContainerTemplates method
  7531. *
  7532. * Test that *Container templates are used by input.
  7533. *
  7534. * @return void
  7535. */
  7536. public function testControlContainerTemplates()
  7537. {
  7538. $this->Form->setTemplates([
  7539. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7540. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7541. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7542. ]);
  7543. $this->article['errors'] = [
  7544. 'Article' => ['published' => 'error message'],
  7545. ];
  7546. $this->Form->create($this->article);
  7547. $result = $this->Form->control('accept', [
  7548. 'type' => 'checkbox',
  7549. ]);
  7550. $expected = [
  7551. 'div' => ['class' => 'check'],
  7552. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7553. 'label' => ['for' => 'accept'],
  7554. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7555. 'Accept',
  7556. '/label',
  7557. '/div',
  7558. ];
  7559. $this->assertHtml($expected, $result);
  7560. $result = $this->Form->control('accept', [
  7561. 'type' => 'radio',
  7562. 'options' => ['Y', 'N'],
  7563. ]);
  7564. $this->assertStringContainsString('<div class="rad">', $result);
  7565. $result = $this->Form->control('Article.published', [
  7566. 'type' => 'radio',
  7567. 'options' => ['Y', 'N'],
  7568. ]);
  7569. $this->assertStringContainsString('<div class="rad err">', $result);
  7570. }
  7571. /**
  7572. * testFormGroupTemplates method
  7573. *
  7574. * Test that *Container templates are used by input.
  7575. *
  7576. * @return void
  7577. */
  7578. public function testFormGroupTemplates()
  7579. {
  7580. $this->Form->setTemplates([
  7581. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7582. ]);
  7583. $this->Form->create($this->article);
  7584. $result = $this->Form->control('accept', [
  7585. 'type' => 'radio',
  7586. 'options' => ['Y', 'N'],
  7587. ]);
  7588. $this->assertStringContainsString('<div class="radio">', $result);
  7589. }
  7590. /**
  7591. * testResetTemplates method
  7592. *
  7593. * Test resetting templates.
  7594. *
  7595. * @return void
  7596. */
  7597. public function testResetTemplates()
  7598. {
  7599. $this->Form->setTemplates(['input' => '<input/>']);
  7600. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7601. $this->Form->resetTemplates();
  7602. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7603. }
  7604. /**
  7605. * testContext method
  7606. *
  7607. * Test the context method.
  7608. *
  7609. * @return void
  7610. */
  7611. public function testContext()
  7612. {
  7613. $result = $this->Form->context();
  7614. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7615. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7616. $this->assertSame($mock, $this->Form->context($mock));
  7617. $this->assertSame($mock, $this->Form->context());
  7618. }
  7619. /**
  7620. * testAutoDomId method
  7621. *
  7622. * @return void
  7623. */
  7624. public function testAutoDomId()
  7625. {
  7626. $result = $this->Form->text('field', ['id' => true]);
  7627. $expected = [
  7628. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7629. ];
  7630. $this->assertHtml($expected, $result);
  7631. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7632. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7633. $expected = [
  7634. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7635. ['label' => ['for' => 'field-0']],
  7636. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7637. 'option A',
  7638. '/label',
  7639. ['label' => ['for' => 'field-1']],
  7640. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7641. 'option B',
  7642. '/label',
  7643. ];
  7644. $this->assertHtml($expected, $result);
  7645. $result = $this->Form->select(
  7646. 'multi_field',
  7647. ['first', 'second'],
  7648. ['multiple' => 'checkbox', 'id' => true]
  7649. );
  7650. $expected = [
  7651. 'input' => [
  7652. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '',
  7653. ],
  7654. ['div' => ['class' => 'checkbox']],
  7655. ['label' => ['for' => 'multi-field-0']],
  7656. ['input' => [
  7657. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7658. 'value' => '0', 'id' => 'multi-field-0',
  7659. ]],
  7660. 'first',
  7661. '/label',
  7662. '/div',
  7663. ['div' => ['class' => 'checkbox']],
  7664. ['label' => ['for' => 'multi-field-1']],
  7665. ['input' => [
  7666. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7667. 'value' => '1', 'id' => 'multi-field-1',
  7668. ]],
  7669. 'second',
  7670. '/label',
  7671. '/div',
  7672. ];
  7673. $this->assertHtml($expected, $result);
  7674. }
  7675. /**
  7676. * Test the basic setters and getters for value sources
  7677. *
  7678. * @return void
  7679. */
  7680. public function testFormValueSourcesSettersGetters()
  7681. {
  7682. $this->View->setRequest($this->View->getRequest()
  7683. ->withData('id', '1')
  7684. ->withQueryParams(['id' => '2']));
  7685. $expected = ['data', 'context'];
  7686. $result = $this->Form->getValueSources();
  7687. $this->assertEquals($expected, $result);
  7688. $this->Form->setValueSources(['context']);
  7689. $result = $this->Form->getSourceValue('id');
  7690. $this->assertNull($result);
  7691. $this->Form->setValueSources('query');
  7692. $expected = ['query'];
  7693. $result = $this->Form->getValueSources();
  7694. $this->assertEquals($expected, $result);
  7695. $expected = '2';
  7696. $result = $this->Form->getSourceValue('id');
  7697. $this->assertEquals($expected, $result);
  7698. $this->Form->setValueSources(['data']);
  7699. $expected = '1';
  7700. $result = $this->Form->getSourceValue('id');
  7701. $this->assertEquals($expected, $result);
  7702. $this->Form->setValueSources(['query', 'data']);
  7703. $expected = '2';
  7704. $result = $this->Form->getSourceValue('id');
  7705. $this->assertEquals($expected, $result);
  7706. }
  7707. public function testValueSourcesValidation()
  7708. {
  7709. $this->expectException(\InvalidArgumentException::class);
  7710. $this->expectExceptionMessage('Invalid value source(s): invalid, foo. Valid values are: context, data, query');
  7711. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7712. }
  7713. /**
  7714. * Tests the different input rendering values based on sources values switching
  7715. *
  7716. * @return void
  7717. */
  7718. public function testFormValueSourcesSingleSwitchRendering()
  7719. {
  7720. $this->loadFixtures('Articles');
  7721. $articles = $this->getTableLocator()->get('Articles');
  7722. $article = new Article();
  7723. $articles->patchEntity($article, ['id' => '3']);
  7724. $this->Form->create($article);
  7725. $this->Form->setValueSources(['context']);
  7726. $result = $this->Form->control('id');
  7727. $expected = [
  7728. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7729. ];
  7730. $this->assertHtml($expected, $result);
  7731. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7732. $this->Form->setValueSources(['query']);
  7733. $result = $this->Form->control('id');
  7734. $expected = [
  7735. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7736. ];
  7737. $this->assertHtml($expected, $result);
  7738. $this->View->setRequest($this->View->getRequest()
  7739. ->withQueryParams(['id' => '5a'])
  7740. ->withData('id', '5b'));
  7741. $this->Form->setValueSources(['context']);
  7742. $this->Form->create($article);
  7743. $result = $this->Form->control('id');
  7744. $expected = [
  7745. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7746. ];
  7747. $this->assertHtml($expected, $result);
  7748. $this->Form->setValueSources(['data']);
  7749. $this->Form->create($article);
  7750. $result = $this->Form->control('id');
  7751. $expected = [
  7752. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7753. ];
  7754. $this->assertHtml($expected, $result);
  7755. $this->Form->setValueSources(['query']);
  7756. $result = $this->Form->control('id');
  7757. $expected = [
  7758. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7759. ];
  7760. $this->assertHtml($expected, $result);
  7761. }
  7762. /**
  7763. * Tests the different input rendering values based on sources values switching while supplying
  7764. * an entity (base context) and multiple sources (such as data, query)
  7765. *
  7766. * @return void
  7767. */
  7768. public function testFormValueSourcesListSwitchRendering()
  7769. {
  7770. $this->loadFixtures('Articles');
  7771. $articles = $this->getTableLocator()->get('Articles');
  7772. $article = new Article();
  7773. $articles->patchEntity($article, ['id' => '3']);
  7774. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7775. $this->Form->create($article);
  7776. $this->Form->setValueSources(['context', 'query']);
  7777. $result = $this->Form->control('id');
  7778. $expected = [
  7779. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7780. ];
  7781. $this->assertHtml($expected, $result);
  7782. $this->Form->setValueSources(['query', 'context']);
  7783. $result = $this->Form->control('id');
  7784. $expected = [
  7785. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7786. ];
  7787. $this->assertHtml($expected, $result);
  7788. $this->Form->setValueSources(['data', 'query', 'context']);
  7789. $result = $this->Form->control('id');
  7790. $expected = [
  7791. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7792. ];
  7793. $this->assertHtml($expected, $result);
  7794. $this->View->setRequest($this->View->getRequest()
  7795. ->withData('id', '8')
  7796. ->withQueryParams(['id' => '9']));
  7797. $this->Form->setValueSources(['data', 'query', 'context']);
  7798. $result = $this->Form->control('id');
  7799. $expected = [
  7800. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7801. ];
  7802. $this->assertHtml($expected, $result);
  7803. }
  7804. /**
  7805. * Test the different form input renderings based on values sources switchings through form options
  7806. *
  7807. * @return void
  7808. */
  7809. public function testFormValueSourcesSwitchViaOptionsRendering()
  7810. {
  7811. $this->loadFixtures('Articles');
  7812. $articles = $this->getTableLocator()->get('Articles');
  7813. $article = new Article();
  7814. $articles->patchEntity($article, ['id' => '3']);
  7815. $this->View->setRequest(
  7816. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7817. );
  7818. $this->Form->create($article, ['valueSources' => 'query']);
  7819. $result = $this->Form->control('id');
  7820. $expected = [
  7821. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7822. ];
  7823. $this->assertHtml($expected, $result);
  7824. $result = $this->Form->getSourceValue('id');
  7825. $this->assertSame('5', $result);
  7826. $this->Form->setValueSources(['context']);
  7827. $this->Form->create($article, ['valueSources' => 'query']);
  7828. $result = $this->Form->control('id');
  7829. $expected = [
  7830. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7831. ];
  7832. $this->assertHtml($expected, $result);
  7833. $result = $this->Form->getSourceValue('id');
  7834. $this->assertSame('5', $result);
  7835. $this->Form->setValueSources(['query']);
  7836. $this->Form->create($article, ['valueSources' => 'data']);
  7837. $result = $this->Form->control('id');
  7838. $expected = [
  7839. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7840. ];
  7841. $this->assertHtml($expected, $result);
  7842. $result = $this->Form->getSourceValue('id');
  7843. $this->assertSame('4', $result);
  7844. $this->Form->setValueSources(['query']);
  7845. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7846. $result = $this->Form->control('id');
  7847. $expected = [
  7848. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7849. ];
  7850. $this->assertHtml($expected, $result);
  7851. $result = $this->Form->getSourceValue('id');
  7852. $this->assertSame(3, $result);
  7853. }
  7854. /**
  7855. * Test the different form input renderings based on values sources switchings through form options
  7856. *
  7857. * @return void
  7858. */
  7859. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  7860. {
  7861. $this->loadFixtures('Articles');
  7862. $articles = $this->getTableLocator()->get('Articles');
  7863. $article = new Article();
  7864. $articles->patchEntity($article, ['id' => '3']);
  7865. $this->View->setRequest(
  7866. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7867. );
  7868. $this->Form->setValueSources(['context'])
  7869. ->create($article, ['valueSources' => ['query', 'data']]);
  7870. $result = $this->Form->control('id');
  7871. $expected = [
  7872. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7873. ];
  7874. $this->assertHtml($expected, $result);
  7875. $result = $this->Form->getSourceValue('id');
  7876. $this->assertSame('11', $result);
  7877. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7878. $this->Form->setValueSources(['context'])
  7879. ->create($article, ['valueSources' => ['query', 'data']]);
  7880. $result = $this->Form->control('id');
  7881. $expected = [
  7882. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7883. ];
  7884. $this->assertHtml($expected, $result);
  7885. $result = $this->Form->getSourceValue('id');
  7886. $this->assertSame('10', $result);
  7887. }
  7888. /**
  7889. * Test the different form values sources resetting through From::end();
  7890. *
  7891. * @return void
  7892. */
  7893. public function testFormValueSourcesResetViaEnd()
  7894. {
  7895. $expected = ['data', 'context'];
  7896. $result = $this->Form->getValueSources();
  7897. $this->assertEquals($expected, $result);
  7898. $expected = ['query', 'context', 'data'];
  7899. $this->Form->setValueSources(['query', 'context', 'data']);
  7900. $result = $this->Form->getValueSources();
  7901. $this->assertEquals($expected, $result);
  7902. $this->Form->create();
  7903. $result = $this->Form->getValueSources();
  7904. $this->assertEquals($expected, $result);
  7905. $this->Form->end();
  7906. $result = $this->Form->getValueSources();
  7907. $this->assertEquals(['data', 'context'], $result);
  7908. }
  7909. /**
  7910. * Test sources values defaults handling
  7911. *
  7912. * @return void
  7913. */
  7914. public function testFormValueSourcesDefaults()
  7915. {
  7916. $this->View->setRequest(
  7917. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7918. );
  7919. $this->Form->create();
  7920. $result = $this->Form->password('password');
  7921. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7922. $this->assertHtml($expected, $result);
  7923. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7924. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7925. $this->assertHtml($expected, $result);
  7926. $this->Form->setValueSources('query');
  7927. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7928. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7929. $this->assertHtml($expected, $result);
  7930. $this->Form->setValueSources('data');
  7931. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7932. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7933. $this->assertHtml($expected, $result);
  7934. }
  7935. /**
  7936. * Test sources values schema defaults handling
  7937. *
  7938. * @return void
  7939. */
  7940. public function testSourcesValueDoesntExistPassThrough()
  7941. {
  7942. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7943. $articles = $this->getTableLocator()->get('Articles');
  7944. $entity = $articles->newEmptyEntity();
  7945. $this->Form->create($entity);
  7946. $this->Form->setValueSources(['query', 'context']);
  7947. $result = $this->Form->getSourceValue('category');
  7948. $this->assertSame('sesame-cookies', $result);
  7949. $this->Form->setValueSources(['context', 'query']);
  7950. $result = $this->Form->getSourceValue('category');
  7951. $this->assertSame('sesame-cookies', $result);
  7952. }
  7953. /**
  7954. * testNestedLabelInput method
  7955. *
  7956. * Test the `nestedInput` parameter
  7957. *
  7958. * @return void
  7959. */
  7960. public function testNestedLabelInput()
  7961. {
  7962. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7963. $expected = [
  7964. 'div' => ['class' => 'input text'],
  7965. 'label' => ['for' => 'foo'],
  7966. ['input' => [
  7967. 'type' => 'text',
  7968. 'name' => 'foo',
  7969. 'id' => 'foo',
  7970. ]],
  7971. 'Foo',
  7972. '/label',
  7973. '/div',
  7974. ];
  7975. $this->assertHtml($expected, $result);
  7976. }
  7977. /**
  7978. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7979. *
  7980. * This test makes sure `false` excludes the label from the render
  7981. *
  7982. * @return void
  7983. */
  7984. public function testControlLabelManipulationDisableLabels()
  7985. {
  7986. $result = $this->Form->control('test', [
  7987. 'type' => 'radio',
  7988. 'options' => ['A', 'B'],
  7989. 'labelOptions' => false,
  7990. ]);
  7991. $expected = [
  7992. ['div' => ['class' => 'input radio']],
  7993. '<label',
  7994. 'Test',
  7995. '/label',
  7996. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7997. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7998. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7999. '/div',
  8000. ];
  8001. $this->assertHtml($expected, $result);
  8002. $result = $this->Form->control('checkbox1', [
  8003. 'label' => 'My checkboxes',
  8004. 'multiple' => 'checkbox',
  8005. 'type' => 'select',
  8006. 'options' => [
  8007. ['text' => 'First Checkbox', 'value' => 1],
  8008. ['text' => 'Second Checkbox', 'value' => 2],
  8009. ],
  8010. 'labelOptions' => false,
  8011. ]);
  8012. $expected = [
  8013. ['div' => ['class' => 'input select']],
  8014. ['label' => ['for' => 'checkbox1']],
  8015. 'My checkboxes',
  8016. '/label',
  8017. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8018. ['div' => ['class' => 'checkbox']],
  8019. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8020. '/div',
  8021. ['div' => ['class' => 'checkbox']],
  8022. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8023. '/div',
  8024. '/div',
  8025. ];
  8026. $this->assertHtml($expected, $result);
  8027. }
  8028. /**
  8029. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8030. *
  8031. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8032. * added to the class if checked.
  8033. *
  8034. * Also checks to make sure any custom attributes are rendered correctly
  8035. *
  8036. * @return void
  8037. */
  8038. public function testControlLabelManipulationRadios()
  8039. {
  8040. $result = $this->Form->control('test', [
  8041. 'type' => 'radio',
  8042. 'options' => ['A', 'B'],
  8043. 'labelOptions' => ['class' => 'custom-class'],
  8044. ]);
  8045. $expected = [
  8046. ['div' => ['class' => 'input radio']],
  8047. '<label',
  8048. 'Test',
  8049. '/label',
  8050. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8051. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8052. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8053. 'A',
  8054. '/label',
  8055. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8056. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8057. 'B',
  8058. '/label',
  8059. '/div',
  8060. ];
  8061. $this->assertHtml($expected, $result);
  8062. $result = $this->Form->control('test', [
  8063. 'type' => 'radio',
  8064. 'options' => ['A', 'B'],
  8065. 'value' => 1,
  8066. 'labelOptions' => ['class' => 'custom-class'],
  8067. ]);
  8068. $expected = [
  8069. ['div' => ['class' => 'input radio']],
  8070. '<label',
  8071. 'Test',
  8072. '/label',
  8073. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8074. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8075. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8076. 'A',
  8077. '/label',
  8078. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8079. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8080. 'B',
  8081. '/label',
  8082. '/div',
  8083. ];
  8084. $this->assertHtml($expected, $result);
  8085. $result = $this->Form->control('test', [
  8086. 'type' => 'radio',
  8087. 'options' => ['A', 'B'],
  8088. 'value' => 1,
  8089. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  8090. ]);
  8091. $expected = [
  8092. ['div' => ['class' => 'input radio']],
  8093. '<label',
  8094. 'Test',
  8095. '/label',
  8096. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8097. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8098. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8099. 'A',
  8100. '/label',
  8101. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8102. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8103. 'B',
  8104. '/label',
  8105. '/div',
  8106. ];
  8107. $this->assertHtml($expected, $result);
  8108. $result = $this->Form->radio('test', ['A', 'B'], [
  8109. 'label' => [
  8110. 'class' => ['custom-class', 'another-class'],
  8111. 'data-name' => 'bob',
  8112. ],
  8113. 'value' => 1,
  8114. ]);
  8115. $expected = [
  8116. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8117. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8118. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8119. 'A',
  8120. '/label',
  8121. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8122. ['input' => [
  8123. 'type' => 'radio',
  8124. 'name' => 'test',
  8125. 'value' => '1',
  8126. 'id' => 'test-1',
  8127. 'checked' => 'checked',
  8128. ]],
  8129. 'B',
  8130. '/label',
  8131. ];
  8132. $this->assertHtml($expected, $result);
  8133. }
  8134. /**
  8135. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8136. *
  8137. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8138. * added to the class if checked.
  8139. *
  8140. * Also checks to make sure any custom attributes are rendered correctly
  8141. *
  8142. * @return void
  8143. */
  8144. public function testControlLabelManipulationCheckboxes()
  8145. {
  8146. $result = $this->Form->control('checkbox1', [
  8147. 'label' => 'My checkboxes',
  8148. 'multiple' => 'checkbox',
  8149. 'type' => 'select',
  8150. 'options' => [
  8151. ['text' => 'First Checkbox', 'value' => 1],
  8152. ['text' => 'Second Checkbox', 'value' => 2],
  8153. ],
  8154. 'labelOptions' => ['class' => 'custom-class'],
  8155. 'value' => ['1'],
  8156. ]);
  8157. $expected = [
  8158. ['div' => ['class' => 'input select']],
  8159. ['label' => ['for' => 'checkbox1']],
  8160. 'My checkboxes',
  8161. '/label',
  8162. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8163. ['div' => ['class' => 'checkbox']],
  8164. ['label' => [
  8165. 'class' => 'custom-class selected',
  8166. 'for' => 'checkbox1-1',
  8167. ]],
  8168. ['input' => [
  8169. 'type' => 'checkbox',
  8170. 'name' => 'checkbox1[]',
  8171. 'value' => '1',
  8172. 'id' => 'checkbox1-1',
  8173. 'checked' => 'checked',
  8174. ]],
  8175. 'First Checkbox',
  8176. '/label',
  8177. '/div',
  8178. ['div' => ['class' => 'checkbox']],
  8179. ['label' => [
  8180. 'class' => 'custom-class',
  8181. 'for' => 'checkbox1-2',
  8182. ]],
  8183. ['input' => [
  8184. 'type' => 'checkbox',
  8185. 'name' => 'checkbox1[]',
  8186. 'value' => '2',
  8187. 'id' => 'checkbox1-2',
  8188. ]],
  8189. 'Second Checkbox',
  8190. '/label',
  8191. '/div',
  8192. '/div',
  8193. ];
  8194. $this->assertHtml($expected, $result);
  8195. $result = $this->Form->control('checkbox1', [
  8196. 'label' => 'My checkboxes',
  8197. 'multiple' => 'checkbox',
  8198. 'type' => 'select',
  8199. 'options' => [
  8200. ['text' => 'First Checkbox', 'value' => 1],
  8201. ['text' => 'Second Checkbox', 'value' => 2],
  8202. ],
  8203. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8204. 'value' => ['1'],
  8205. ]);
  8206. $expected = [
  8207. ['div' => ['class' => 'input select']],
  8208. ['label' => ['for' => 'checkbox1']],
  8209. 'My checkboxes',
  8210. '/label',
  8211. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8212. ['div' => ['class' => 'checkbox']],
  8213. ['label' => [
  8214. 'class' => 'custom-class another-class selected',
  8215. 'data-name' => 'bob',
  8216. 'for' => 'checkbox1-1',
  8217. ]],
  8218. ['input' => [
  8219. 'type' => 'checkbox',
  8220. 'name' => 'checkbox1[]',
  8221. 'value' => '1',
  8222. 'id' => 'checkbox1-1',
  8223. 'checked' => 'checked',
  8224. ]],
  8225. 'First Checkbox',
  8226. '/label',
  8227. '/div',
  8228. ['div' => ['class' => 'checkbox']],
  8229. ['label' => [
  8230. 'class' => 'custom-class another-class',
  8231. 'data-name' => 'bob',
  8232. 'for' => 'checkbox1-2',
  8233. ]],
  8234. ['input' => [
  8235. 'type' => 'checkbox',
  8236. 'name' => 'checkbox1[]',
  8237. 'value' => '2',
  8238. 'id' => 'checkbox1-2',
  8239. ]],
  8240. 'Second Checkbox',
  8241. '/label',
  8242. '/div',
  8243. '/div',
  8244. ];
  8245. $this->assertHtml($expected, $result);
  8246. }
  8247. /**
  8248. * testControlMaxLengthArrayContext method
  8249. *
  8250. * Test control() with maxlength attribute in Array Context.
  8251. *
  8252. * @return void
  8253. */
  8254. public function testControlMaxLengthArrayContext()
  8255. {
  8256. $this->article['schema'] = [
  8257. 'title' => ['length' => 10],
  8258. ];
  8259. $this->Form->create($this->article);
  8260. $result = $this->Form->control('title');
  8261. $expected = [
  8262. 'div' => ['class'],
  8263. 'label' => ['for'],
  8264. 'Title',
  8265. '/label',
  8266. 'input' => [
  8267. 'id',
  8268. 'name' => 'title',
  8269. 'type' => 'text',
  8270. 'required' => 'required',
  8271. 'maxlength' => 10,
  8272. 'data-validity-message' => 'This field cannot be left empty',
  8273. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8274. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8275. ],
  8276. '/div',
  8277. ];
  8278. $this->assertHtml($expected, $result);
  8279. }
  8280. /**
  8281. * testControlMaxLengthEntityContext method
  8282. *
  8283. * Test control() with maxlength attribute in Entity Context.
  8284. *
  8285. * @return void
  8286. */
  8287. public function testControlMaxLengthEntityContext()
  8288. {
  8289. $this->article['schema']['title']['length'] = 45;
  8290. $validator = new Validator();
  8291. $validator->maxLength('title', 10);
  8292. $article = new EntityContext(
  8293. [
  8294. 'entity' => new Entity($this->article),
  8295. 'table' => new Table([
  8296. 'alias' => 'Articles',
  8297. 'schema' => $this->article['schema'],
  8298. 'validator' => $validator,
  8299. ]),
  8300. ]
  8301. );
  8302. $this->Form->create($article);
  8303. $result = $this->Form->control('title');
  8304. $expected = [
  8305. 'div' => ['class'],
  8306. 'label' => ['for'],
  8307. 'Title',
  8308. '/label',
  8309. 'input' => [
  8310. 'id',
  8311. 'name' => 'title',
  8312. 'type' => 'text',
  8313. 'required' => 'required',
  8314. 'maxlength' => 10,
  8315. 'data-validity-message' => 'This field cannot be left empty',
  8316. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8317. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8318. ],
  8319. '/div',
  8320. ];
  8321. $this->assertHtml($expected, $result);
  8322. $this->article['schema']['title']['length'] = 45;
  8323. $validator = new Validator();
  8324. $validator->maxLength('title', 55);
  8325. $article = new EntityContext(
  8326. [
  8327. 'entity' => new Entity($this->article),
  8328. 'table' => new Table([
  8329. 'schema' => $this->article['schema'],
  8330. 'validator' => $validator,
  8331. 'alias' => 'Articles',
  8332. ]),
  8333. ]
  8334. );
  8335. $this->Form->create($article);
  8336. $result = $this->Form->control('title');
  8337. $expected = [
  8338. 'div' => ['class'],
  8339. 'label' => ['for'],
  8340. 'Title',
  8341. '/label',
  8342. 'input' => [
  8343. 'id',
  8344. 'name' => 'title',
  8345. 'type' => 'text',
  8346. 'required' => 'required',
  8347. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8348. 'data-validity-message' => 'This field cannot be left empty',
  8349. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8350. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8351. ],
  8352. '/div',
  8353. ];
  8354. $this->assertHtml($expected, $result);
  8355. $this->article['schema']['title']['length'] = 45;
  8356. $validator = new Validator();
  8357. $validator->maxLength('title', 55);
  8358. $article = new EntityContext(
  8359. [
  8360. 'entity' => new Entity($this->article),
  8361. 'table' => new Table([
  8362. 'schema' => $this->article['schema'],
  8363. 'validator' => $validator,
  8364. 'alias' => 'Articles',
  8365. ]),
  8366. ]
  8367. );
  8368. $this->Form->create($article);
  8369. $result = $this->Form->control('title', ['maxlength' => 10]);
  8370. $expected = [
  8371. 'div' => ['class'],
  8372. 'label' => ['for'],
  8373. 'Title',
  8374. '/label',
  8375. 'input' => [
  8376. 'id',
  8377. 'name' => 'title',
  8378. 'type' => 'text',
  8379. 'required' => 'required',
  8380. 'maxlength' => 10, // Length set in options should take highest precedence.
  8381. 'data-validity-message' => 'This field cannot be left empty',
  8382. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8383. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8384. ],
  8385. '/div',
  8386. ];
  8387. $this->assertHtml($expected, $result);
  8388. }
  8389. /**
  8390. * testControlMinMaxLengthEntityContext method
  8391. *
  8392. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8393. *
  8394. * @return void
  8395. */
  8396. public function testControlMinMaxLengthEntityContext()
  8397. {
  8398. $validator = new Validator();
  8399. $validator->maxLength('title', 10);
  8400. $article = new EntityContext(
  8401. [
  8402. 'entity' => new Entity($this->article),
  8403. 'table' => new Table([
  8404. 'alias' => 'Articles',
  8405. 'schema' => $this->article['schema'],
  8406. 'validator' => $validator,
  8407. ]),
  8408. ]
  8409. );
  8410. $this->Form->create($article);
  8411. $result = $this->Form->control('title');
  8412. $expected = [
  8413. 'div' => ['class'],
  8414. 'label' => ['for'],
  8415. 'Title',
  8416. '/label',
  8417. 'input' => [
  8418. 'id',
  8419. 'name' => 'title',
  8420. 'type' => 'text',
  8421. 'required' => 'required',
  8422. 'maxlength' => 10,
  8423. 'data-validity-message' => 'This field cannot be left empty',
  8424. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8425. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8426. ],
  8427. '/div',
  8428. ];
  8429. $this->assertHtml($expected, $result);
  8430. }
  8431. /**
  8432. * testControlMaxLengthFormContext method
  8433. *
  8434. * Test control() with maxlength attribute in Form Context.
  8435. *
  8436. * @return void
  8437. */
  8438. public function testControlMaxLengthFormContext()
  8439. {
  8440. $validator = new Validator();
  8441. $validator->maxLength('title', 10);
  8442. $form = new Form();
  8443. $form->setValidator('default', $validator);
  8444. $this->Form->create($form);
  8445. $result = $this->Form->control('title');
  8446. $expected = [
  8447. 'div' => ['class'],
  8448. 'label' => ['for'],
  8449. 'Title',
  8450. '/label',
  8451. 'input' => [
  8452. 'id',
  8453. 'name' => 'title',
  8454. 'type' => 'text',
  8455. 'required' => 'required',
  8456. 'maxlength' => 10,
  8457. 'data-validity-message' => 'This field cannot be left empty',
  8458. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8459. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8460. ],
  8461. '/div',
  8462. ];
  8463. $this->assertHtml($expected, $result);
  8464. }
  8465. }