FormHelperTest.php 302 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117
  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\ORM\TableRegistry;
  26. use Cake\Routing\Router;
  27. use Cake\TestSuite\TestCase;
  28. use Cake\Utility\Security;
  29. use Cake\Validation\Validator;
  30. use Cake\View\Form\EntityContext;
  31. use Cake\View\Helper\FormHelper;
  32. use Cake\View\View;
  33. use Cake\View\Widget\WidgetLocator;
  34. use ReflectionProperty;
  35. use TestApp\Model\Entity\Article;
  36. use TestApp\Model\Table\ContactsTable;
  37. use TestApp\Model\Table\ValidateUsersTable;
  38. /**
  39. * FormHelperTest class
  40. *
  41. * @property \Cake\View\Helper\FormHelper $Form
  42. * @property \Cake\View\View $View
  43. */
  44. class FormHelperTest extends TestCase
  45. {
  46. /**
  47. * Fixtures to be used
  48. *
  49. * @var array
  50. */
  51. protected $fixtures = ['core.Articles', 'core.Comments'];
  52. /**
  53. * Do not load the fixtures by default
  54. *
  55. * @var bool
  56. */
  57. public $autoFixtures = false;
  58. /**
  59. * @var array
  60. */
  61. protected $article = [];
  62. /**
  63. * @var string
  64. */
  65. protected $url;
  66. /**
  67. * setUp method
  68. *
  69. * @return void
  70. */
  71. public function setUp(): void
  72. {
  73. parent::setUp();
  74. Configure::write('Config.language', 'eng');
  75. Configure::write('App.base', '');
  76. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  77. $request = new ServerRequest([
  78. 'webroot' => '',
  79. 'base' => '',
  80. 'url' => '/articles/add',
  81. 'params' => [
  82. 'controller' => 'articles',
  83. 'action' => 'add',
  84. 'plugin' => null,
  85. ],
  86. ]);
  87. $this->View = new View($request);
  88. Router::reload();
  89. Router::setRequest($request);
  90. $this->url = '/articles/add';
  91. $this->Form = new FormHelper($this->View);
  92. $this->dateRegex = [
  93. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  94. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  95. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  96. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  97. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  98. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  99. ];
  100. $this->article = [
  101. 'schema' => [
  102. 'id' => ['type' => 'integer'],
  103. 'author_id' => ['type' => 'integer', 'null' => true],
  104. 'title' => ['type' => 'string', 'null' => true],
  105. 'body' => 'text',
  106. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  107. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  108. ],
  109. 'required' => [
  110. 'author_id' => true,
  111. 'title' => true,
  112. ],
  113. ];
  114. Security::setSalt('foo!');
  115. Router::connect('/:controller', ['action' => 'index']);
  116. Router::connect('/:controller/:action/*');
  117. }
  118. /**
  119. * tearDown method
  120. *
  121. * @return void
  122. */
  123. public function tearDown(): void
  124. {
  125. parent::tearDown();
  126. unset($this->Form, $this->Controller, $this->View);
  127. $this->getTableLocator()->clear();
  128. }
  129. /**
  130. * Test construct() with the templates option.
  131. *
  132. * @return void
  133. */
  134. public function testConstructTemplatesFile()
  135. {
  136. $helper = new FormHelper($this->View, [
  137. 'templates' => 'htmlhelper_tags',
  138. ]);
  139. $result = $helper->control('name');
  140. $this->assertStringContainsString('<input', $result);
  141. }
  142. /**
  143. * Test that when specifying custom widgets the config array for that widget
  144. * is overwritten instead of merged.
  145. *
  146. * @return void
  147. */
  148. public function testConstructWithWidgets()
  149. {
  150. $config = [
  151. 'widgets' => [
  152. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  153. ],
  154. ];
  155. $helper = new FormHelper($this->View, $config);
  156. $locator = $helper->getWidgetLocator();
  157. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  158. }
  159. /**
  160. * Test that when specifying custom widgets config file and it should be
  161. * added to widgets array. WidgetLocator will load widgets in constructor.
  162. *
  163. * @return void
  164. */
  165. public function testConstructWithWidgetsConfig()
  166. {
  167. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  168. $locator = $helper->getWidgetLocator();
  169. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  170. }
  171. /**
  172. * Test setting the widget locator
  173. *
  174. * @return void
  175. */
  176. public function testSetAndGetWidgetLocator()
  177. {
  178. $helper = new FormHelper($this->View);
  179. $locator = new WidgetLocator($helper->templater(), $this->View);
  180. $helper->setWidgetLocator($locator);
  181. $this->assertSame($locator, $helper->getWidgetLocator());
  182. }
  183. /**
  184. * Test overridding grouped input types which controls generation of "for"
  185. * attribute of labels.
  186. *
  187. * @return void
  188. */
  189. public function testConstructWithGroupedInputTypes()
  190. {
  191. $helper = new FormHelper($this->View, [
  192. 'groupedInputTypes' => ['radio'],
  193. ]);
  194. $result = $helper->control('when', ['type' => 'datetime-local']);
  195. $this->assertStringContainsString('<label for="when">When</label>', $result);
  196. }
  197. /**
  198. * Test registering a new widget class and rendering it.
  199. *
  200. * @return void
  201. */
  202. public function testAddWidgetAndRenderWidget()
  203. {
  204. $data = [
  205. 'val' => 1,
  206. ];
  207. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  208. $this->Form->addWidget('test', $mock);
  209. $mock->expects($this->once())
  210. ->method('render')
  211. ->with($data)
  212. ->will($this->returnValue('HTML'));
  213. $result = $this->Form->widget('test', $data);
  214. $this->assertSame('HTML', $result);
  215. }
  216. /**
  217. * Test that secureFields() of widget is called after calling render(),
  218. * not before.
  219. *
  220. * @return void
  221. */
  222. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  223. {
  224. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  225. 'unlockedFields' => [],
  226. ]));
  227. $data = [
  228. 'val' => 1,
  229. 'name' => 'test',
  230. ];
  231. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  232. $this->Form->addWidget('test', $mock);
  233. $mock->expects($this->at(0))
  234. ->method('render')
  235. ->with($data)
  236. ->will($this->returnValue('HTML'));
  237. $mock->expects($this->at(1))
  238. ->method('secureFields')
  239. ->with($data)
  240. ->will($this->returnValue(['test']));
  241. $this->Form->create();
  242. $result = $this->Form->widget('test', $data + ['secure' => true]);
  243. $this->assertSame('HTML', $result);
  244. }
  245. /**
  246. * Test that empty string is not added to secure fields list when
  247. * rendering input widget without name.
  248. *
  249. * @return void
  250. */
  251. public function testRenderingWidgetWithEmptyName()
  252. {
  253. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  254. $this->Form->create();
  255. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  256. $this->assertSame('<select name=""></select>', $result);
  257. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  258. $this->assertEquals([], $result);
  259. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  260. $this->assertSame('<select name="0"></select>', $result);
  261. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  262. $this->assertEquals(['0'], $result);
  263. }
  264. /**
  265. * Test registering an invalid widget class.
  266. *
  267. * @return void
  268. */
  269. public function testAddWidgetInvalid()
  270. {
  271. $this->expectException(\RuntimeException::class);
  272. $mock = new \StdClass();
  273. $this->Form->addWidget('test', $mock);
  274. $this->Form->widget('test');
  275. }
  276. /**
  277. * Test adding a new context class.
  278. *
  279. * @return void
  280. */
  281. public function testAddContextProvider()
  282. {
  283. $context = 'My data';
  284. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  285. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  286. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  287. $this->assertEquals($context, $data['entity']);
  288. return $stub;
  289. });
  290. $this->Form->create($context);
  291. $result = $this->Form->context();
  292. $this->assertSame($stub, $result);
  293. }
  294. /**
  295. * Test replacing a context class.
  296. *
  297. * @return void
  298. */
  299. public function testAddContextProviderReplace()
  300. {
  301. $entity = new Article();
  302. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  303. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  304. return $stub;
  305. });
  306. $this->Form->create($entity);
  307. $result = $this->Form->context();
  308. $this->assertSame($stub, $result);
  309. }
  310. /**
  311. * Test overriding a context class.
  312. *
  313. * @return void
  314. */
  315. public function testAddContextProviderAdd()
  316. {
  317. $entity = new Article();
  318. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  319. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  320. if ($data['entity'] instanceof Entity) {
  321. return $stub;
  322. }
  323. });
  324. $this->Form->create($entity);
  325. $result = $this->Form->context();
  326. $this->assertSame($stub, $result);
  327. }
  328. /**
  329. * Test adding an invalid context class.
  330. *
  331. * @return void
  332. */
  333. public function testAddContextProviderInvalid()
  334. {
  335. $this->expectException(\TypeError::class);
  336. $this->expectExceptionMessage('Return value of Cake\View\Form\ContextFactory::get() must implement interface Cake\View\Form\ContextInterface, instance of stdClass returned');
  337. $context = 'My data';
  338. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  339. return new \StdClass();
  340. });
  341. $this->Form->create($context);
  342. }
  343. /**
  344. * Provides context options for create().
  345. *
  346. * @return array
  347. */
  348. public function contextSelectionProvider()
  349. {
  350. $entity = new Article();
  351. $collection = new Collection([$entity]);
  352. $emptyCollection = new Collection([]);
  353. $arrayObject = new \ArrayObject([]);
  354. $data = [
  355. 'schema' => [
  356. 'title' => ['type' => 'string'],
  357. ],
  358. ];
  359. $form = new Form();
  360. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  361. return [
  362. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  363. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  364. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  365. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  366. 'form' => [$form, 'Cake\View\Form\FormContext'],
  367. 'none' => [null, 'Cake\View\Form\NullContext'],
  368. 'custom' => [$custom, get_class($custom)],
  369. ];
  370. }
  371. /**
  372. * Test default context selection in create()
  373. *
  374. * @dataProvider contextSelectionProvider
  375. * @return void
  376. */
  377. public function testCreateContextSelectionBuiltIn($data, $class)
  378. {
  379. $this->loadFixtures('Articles');
  380. $this->Form->create($data);
  381. $this->assertInstanceOf($class, $this->Form->context());
  382. }
  383. /**
  384. * Data provider for type option.
  385. *
  386. * @return array
  387. */
  388. public static function requestTypeProvider()
  389. {
  390. return [
  391. // type, method, override
  392. ['post', 'post', 'POST'],
  393. ['put', 'post', 'PUT'],
  394. ['patch', 'post', 'PATCH'],
  395. ['delete', 'post', 'DELETE'],
  396. ];
  397. }
  398. /**
  399. * Test creating file forms.
  400. *
  401. * @return void
  402. */
  403. public function testCreateFile()
  404. {
  405. $encoding = strtolower(Configure::read('App.encoding'));
  406. $result = $this->Form->create(null, ['type' => 'file']);
  407. $expected = [
  408. 'form' => [
  409. 'method' => 'post', 'action' => '/articles/add',
  410. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  411. ],
  412. 'div' => ['style' => 'display:none;'],
  413. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  414. '/div',
  415. ];
  416. $this->assertHtml($expected, $result);
  417. }
  418. /**
  419. * Test creating GET forms.
  420. *
  421. * @return void
  422. */
  423. public function testCreateGet()
  424. {
  425. $encoding = strtolower(Configure::read('App.encoding'));
  426. $result = $this->Form->create(null, ['type' => 'get']);
  427. $expected = ['form' => [
  428. 'method' => 'get', 'action' => '/articles/add',
  429. 'accept-charset' => $encoding,
  430. ]];
  431. $this->assertHtml($expected, $result);
  432. }
  433. /**
  434. * Test explicit method/enctype options.
  435. *
  436. * Explicit method overwrites inferred method from 'type'
  437. *
  438. * @return void
  439. */
  440. public function testCreateExplicitMethodEnctype()
  441. {
  442. $encoding = strtolower(Configure::read('App.encoding'));
  443. $result = $this->Form->create(null, [
  444. 'type' => 'get',
  445. 'method' => 'put',
  446. 'enctype' => 'multipart/form-data',
  447. ]);
  448. $expected = ['form' => [
  449. 'method' => 'put',
  450. 'action' => '/articles/add',
  451. 'enctype' => 'multipart/form-data',
  452. 'accept-charset' => $encoding,
  453. ]];
  454. $this->assertHtml($expected, $result);
  455. }
  456. /**
  457. * Test create() with the templates option.
  458. *
  459. * @return void
  460. */
  461. public function testCreateTemplatesArray()
  462. {
  463. $result = $this->Form->create($this->article, [
  464. 'templates' => [
  465. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  466. ],
  467. ]);
  468. $expected = [
  469. 'form' => [
  470. 'class' => 'form-horizontal',
  471. 'method' => 'post',
  472. 'action' => '/articles/add',
  473. 'accept-charset' => 'utf-8',
  474. ],
  475. ];
  476. $this->assertHtml($expected, $result);
  477. }
  478. /**
  479. * Test create() with the templates option.
  480. *
  481. * @return void
  482. */
  483. public function testCreateTemplatesFile()
  484. {
  485. $result = $this->Form->create($this->article, [
  486. 'templates' => 'htmlhelper_tags',
  487. ]);
  488. $expected = [
  489. 'start form',
  490. ];
  491. $this->assertHtml($expected, $result);
  492. }
  493. /**
  494. * Test that create() and end() restore templates.
  495. *
  496. * @return void
  497. */
  498. public function testCreateEndRestoreTemplates()
  499. {
  500. $this->Form->create($this->article, [
  501. 'templates' => ['input' => 'custom input element'],
  502. ]);
  503. $this->Form->end();
  504. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  505. }
  506. /**
  507. * Test using template vars in various templates used by control() method.
  508. *
  509. * @return void
  510. */
  511. public function testControlTemplateVars()
  512. {
  513. $result = $this->Form->control('text', [
  514. 'templates' => [
  515. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  516. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  517. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  518. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  519. ],
  520. 'templateVars' => [
  521. 'forinput' => 'in-input',
  522. 'forlabel' => 'in-label',
  523. 'forgroup' => 'in-group',
  524. 'forcontainer' => 'in-container',
  525. ],
  526. ]);
  527. $expected = [
  528. 'div' => ['class'],
  529. 'label' => ['for'],
  530. 'Text in-label',
  531. '/label',
  532. 'in-group',
  533. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  534. 'in-container',
  535. '/div',
  536. ];
  537. $this->assertHtml($expected, $result);
  538. }
  539. /**
  540. * Test ensuring template variables work in template files loaded
  541. * during control().
  542. *
  543. * @return void
  544. */
  545. public function testControlTemplatesFromFile()
  546. {
  547. $result = $this->Form->control('title', [
  548. 'templates' => 'test_templates',
  549. 'templateVars' => [
  550. 'forcontainer' => 'container-data',
  551. ],
  552. ]);
  553. $expected = [
  554. 'div' => ['class'],
  555. 'label' => ['for'],
  556. 'Title',
  557. '/label',
  558. 'input' => ['name', 'type' => 'text', 'id'],
  559. 'container-data',
  560. '/div',
  561. ];
  562. $this->assertHtml($expected, $result);
  563. }
  564. /**
  565. * Test using template vars in inputSubmit and submitContainer template.
  566. *
  567. * @return void
  568. */
  569. public function testSubmitTemplateVars()
  570. {
  571. $this->Form->setTemplates([
  572. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  573. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  574. ]);
  575. $result = $this->Form->submit('Submit', [
  576. 'templateVars' => [
  577. 'forinput' => 'in-input',
  578. 'forcontainer' => 'in-container',
  579. ],
  580. ]);
  581. $expected = [
  582. 'div' => ['class'],
  583. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  584. 'in-container',
  585. '/div',
  586. ];
  587. $this->assertHtml($expected, $result);
  588. }
  589. /**
  590. * test the create() method
  591. *
  592. * @dataProvider requestTypeProvider
  593. * @return void
  594. */
  595. public function testCreateTypeOptions($type, $method, $override)
  596. {
  597. $encoding = strtolower(Configure::read('App.encoding'));
  598. $result = $this->Form->create(null, ['type' => $type]);
  599. $expected = [
  600. 'form' => [
  601. 'method' => $method, 'action' => '/articles/add',
  602. 'accept-charset' => $encoding,
  603. ],
  604. ];
  605. $extra = [
  606. 'div' => ['style' => 'display:none;'],
  607. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  608. '/div',
  609. ];
  610. if ($type !== 'post') {
  611. $expected = array_merge($expected, $extra);
  612. }
  613. $this->assertHtml($expected, $result);
  614. }
  615. /**
  616. * Test using template vars in Create (formStart template)
  617. *
  618. * @return void
  619. */
  620. public function testCreateTemplateVars()
  621. {
  622. $result = $this->Form->create($this->article, [
  623. 'templates' => [
  624. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  625. ],
  626. 'templateVars' => ['header' => 'headertext'],
  627. ]);
  628. $expected = [
  629. 'h4' => ['class'],
  630. 'headertext',
  631. '/h4',
  632. 'form' => [
  633. 'method' => 'post',
  634. 'action' => '/articles/add',
  635. 'accept-charset' => 'utf-8',
  636. ],
  637. ];
  638. $this->assertHtml($expected, $result);
  639. }
  640. /**
  641. * Test opening a form for an update operation.
  642. *
  643. * @return void
  644. */
  645. public function testCreateUpdateForm()
  646. {
  647. $encoding = strtolower(Configure::read('App.encoding'));
  648. $this->View->setRequest($this->View->getRequest()
  649. ->withRequestTarget('/articles/edit/1')
  650. ->withParam('action', 'edit'));
  651. $this->article['defaults']['id'] = 1;
  652. $result = $this->Form->create($this->article);
  653. $expected = [
  654. 'form' => [
  655. 'method' => 'post',
  656. 'action' => '/articles/edit/1',
  657. 'accept-charset' => $encoding,
  658. ],
  659. 'div' => ['style' => 'display:none;'],
  660. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  661. '/div',
  662. ];
  663. $this->assertHtml($expected, $result);
  664. }
  665. /**
  666. * test create() with automatic url generation
  667. *
  668. * @return void
  669. */
  670. public function testCreateAutoUrl()
  671. {
  672. $encoding = strtolower(Configure::read('App.encoding'));
  673. $this->View->setRequest($this->View->getRequest()
  674. ->withRequestTarget('/articles/delete/10')
  675. ->withParam('action', 'delete'));
  676. $result = $this->Form->create($this->article);
  677. $expected = [
  678. 'form' => [
  679. 'method' => 'post', 'action' => '/articles/delete/10',
  680. 'accept-charset' => $encoding,
  681. ],
  682. ];
  683. $this->assertHtml($expected, $result);
  684. $this->article['defaults'] = ['id' => 1];
  685. $this->View->setRequest($this->View->getRequest()
  686. ->withRequestTarget('/articles/edit/1')
  687. ->withParam('action', 'delete'));
  688. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit', 1]]);
  689. $expected = [
  690. 'form' => [
  691. 'method' => 'post',
  692. 'action' => '/articles/edit/1',
  693. 'accept-charset' => $encoding,
  694. ],
  695. 'div' => ['style' => 'display:none;'],
  696. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  697. '/div',
  698. ];
  699. $this->assertHtml($expected, $result);
  700. $this->View->setRequest($this->View->getRequest()
  701. ->withParam('action', 'add'));
  702. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish', 1]]);
  703. $expected = [
  704. 'form' => [
  705. 'method' => 'post',
  706. 'action' => '/articles/publish/1',
  707. 'accept-charset' => $encoding,
  708. ],
  709. 'div' => ['style' => 'display:none;'],
  710. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  711. '/div',
  712. ];
  713. $this->assertHtml($expected, $result);
  714. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  715. $expected = [
  716. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  717. 'div' => ['style' => 'display:none;'],
  718. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  719. '/div',
  720. ];
  721. $this->assertHtml($expected, $result);
  722. $this->View->setRequest($this->View->getRequest()
  723. ->withParam('controller', 'Pages'));
  724. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup', 1]]);
  725. $expected = [
  726. 'form' => [
  727. 'method' => 'post', 'action' => '/Pages/signup/1',
  728. 'accept-charset' => $encoding,
  729. ],
  730. 'div' => ['style' => 'display:none;'],
  731. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  732. '/div',
  733. ];
  734. $this->assertHtml($expected, $result);
  735. }
  736. /**
  737. * Test create() with no URL (no "action" attribute for <form> tag)
  738. *
  739. * @return void
  740. */
  741. public function testCreateNoUrl()
  742. {
  743. $result = $this->Form->create(null, ['url' => false]);
  744. $expected = [
  745. 'form' => [
  746. 'method' => 'post',
  747. 'accept-charset' => strtolower(Configure::read('App.encoding')),
  748. ],
  749. ];
  750. $this->assertHtml($expected, $result);
  751. }
  752. /**
  753. * test create() with a custom route
  754. *
  755. * @return void
  756. */
  757. public function testCreateCustomRoute()
  758. {
  759. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  760. $encoding = strtolower(Configure::read('App.encoding'));
  761. $this->View->setRequest($this->View->getRequest()
  762. ->withParam('controller', 'users'));
  763. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  764. $expected = [
  765. 'form' => [
  766. 'method' => 'post', 'action' => '/login',
  767. 'accept-charset' => $encoding,
  768. ],
  769. ];
  770. $this->assertHtml($expected, $result);
  771. Router::connect(
  772. '/new-article',
  773. ['controller' => 'articles', 'action' => 'myaction'],
  774. ['_name' => 'my-route']
  775. );
  776. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  777. $expected = [
  778. 'form' => [
  779. 'method' => 'post', 'action' => '/new-article',
  780. 'accept-charset' => $encoding,
  781. ],
  782. ];
  783. $this->assertHtml($expected, $result);
  784. }
  785. /**
  786. * test automatic accept-charset overriding
  787. *
  788. * @return void
  789. */
  790. public function testCreateWithAcceptCharset()
  791. {
  792. $result = $this->Form->create(
  793. $this->article,
  794. [
  795. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  796. ]
  797. );
  798. $expected = [
  799. 'form' => [
  800. 'method' => 'post', 'action' => '/articles',
  801. 'accept-charset' => 'iso-8859-1',
  802. ],
  803. ];
  804. $this->assertHtml($expected, $result);
  805. }
  806. /**
  807. * Test base form URL when url param is passed with multiple parameters (&)
  808. *
  809. */
  810. public function testCreateQueryStringRequest()
  811. {
  812. $encoding = strtolower(Configure::read('App.encoding'));
  813. $result = $this->Form->create($this->article, [
  814. 'type' => 'post',
  815. 'escape' => false,
  816. 'url' => [
  817. 'controller' => 'controller',
  818. 'action' => 'action',
  819. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  820. ],
  821. ]);
  822. $expected = [
  823. 'form' => [
  824. 'method' => 'post',
  825. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  826. 'accept-charset' => $encoding,
  827. ],
  828. ];
  829. $this->assertHtml($expected, $result);
  830. $result = $this->Form->create($this->article, [
  831. 'type' => 'post',
  832. 'url' => [
  833. 'controller' => 'controller',
  834. 'action' => 'action',
  835. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  836. ],
  837. ]);
  838. $this->assertHtml($expected, $result);
  839. }
  840. /**
  841. * test that create() doesn't cause errors by multiple id's being in the primary key
  842. * as could happen with multiple select or checkboxes.
  843. *
  844. * @return void
  845. */
  846. public function testCreateWithMultipleIdInData()
  847. {
  848. $encoding = strtolower(Configure::read('App.encoding'));
  849. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  850. $result = $this->Form->create($this->article);
  851. $expected = [
  852. 'form' => [
  853. 'method' => 'post',
  854. 'action' => '/articles/add',
  855. 'accept-charset' => $encoding,
  856. ],
  857. ];
  858. $this->assertHtml($expected, $result);
  859. }
  860. /**
  861. * test that create() doesn't add in extra passed params.
  862. *
  863. * @return void
  864. */
  865. public function testCreatePassedArgs()
  866. {
  867. $encoding = strtolower(Configure::read('App.encoding'));
  868. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  869. $result = $this->Form->create($this->article, [
  870. 'type' => 'post',
  871. 'escape' => false,
  872. 'url' => [
  873. 'action' => 'edit',
  874. 'myparam',
  875. ],
  876. ]);
  877. $expected = [
  878. 'form' => [
  879. 'method' => 'post',
  880. 'action' => '/articles/edit/myparam',
  881. 'accept-charset' => $encoding,
  882. ],
  883. ];
  884. $this->assertHtml($expected, $result);
  885. }
  886. /**
  887. * test creating a get form, and get form inputs.
  888. *
  889. * @return void
  890. */
  891. public function testGetFormCreate()
  892. {
  893. $encoding = strtolower(Configure::read('App.encoding'));
  894. $result = $this->Form->create($this->article, ['type' => 'get']);
  895. $expected = ['form' => [
  896. 'method' => 'get', 'action' => '/articles/add',
  897. 'accept-charset' => $encoding,
  898. ]];
  899. $this->assertHtml($expected, $result);
  900. $result = $this->Form->text('title');
  901. $expected = ['input' => [
  902. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  903. ]];
  904. $this->assertHtml($expected, $result);
  905. $result = $this->Form->password('password');
  906. $expected = ['input' => [
  907. 'name' => 'password', 'type' => 'password',
  908. ]];
  909. $this->assertHtml($expected, $result);
  910. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  911. $result = $this->Form->text('user_form');
  912. $expected = ['input' => [
  913. 'name' => 'user_form', 'type' => 'text',
  914. ]];
  915. $this->assertHtml($expected, $result);
  916. }
  917. /**
  918. * test get form, and inputs when the model param is false
  919. *
  920. * @return void
  921. */
  922. public function testGetFormWithFalseModel()
  923. {
  924. $encoding = strtolower(Configure::read('App.encoding'));
  925. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'contact_test'));
  926. $result = $this->Form->create(null, [
  927. 'type' => 'get', 'url' => ['controller' => 'contact_test'],
  928. ]);
  929. $expected = ['form' => [
  930. 'method' => 'get', 'action' => '/contact_test/add',
  931. 'accept-charset' => $encoding,
  932. ]];
  933. $this->assertHtml($expected, $result);
  934. $result = $this->Form->text('reason');
  935. $expected = [
  936. 'input' => ['type' => 'text', 'name' => 'reason'],
  937. ];
  938. $this->assertHtml($expected, $result);
  939. }
  940. /**
  941. * testFormCreateWithSecurity method
  942. *
  943. * Test form->create() with security key.
  944. *
  945. * @return void
  946. */
  947. public function testCreateWithSecurity()
  948. {
  949. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  950. $encoding = strtolower(Configure::read('App.encoding'));
  951. $result = $this->Form->create($this->article, [
  952. 'url' => '/articles/publish',
  953. ]);
  954. $expected = [
  955. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  956. 'div' => ['style' => 'display:none;'],
  957. ['input' => [
  958. 'type' => 'hidden',
  959. 'name' => '_csrfToken',
  960. 'value' => 'testKey',
  961. 'autocomplete' => 'off',
  962. ]],
  963. '/div',
  964. ];
  965. $this->assertHtml($expected, $result);
  966. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  967. $expected['form']['id'] = 'MyForm';
  968. $this->assertHtml($expected, $result);
  969. }
  970. /**
  971. * testFormCreateGetNoSecurity method
  972. *
  973. * Test form->create() with no security key as its a get form
  974. *
  975. * @return void
  976. */
  977. public function testCreateEndGetNoSecurity()
  978. {
  979. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  980. $article = new Article();
  981. $result = $this->Form->create($article, [
  982. 'type' => 'get',
  983. 'url' => '/contacts/add',
  984. ]);
  985. $this->assertStringNotContainsString('testKey', $result);
  986. $result = $this->Form->end();
  987. $this->assertStringNotContainsString('testKey', $result);
  988. }
  989. /**
  990. * Tests form hash generation with model-less data
  991. *
  992. * @return void
  993. */
  994. public function testValidateHashNoModel()
  995. {
  996. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  997. $fields = ['anything'];
  998. $this->Form->create();
  999. $result = $this->Form->secure($fields);
  1000. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  1001. $this->assertStringContainsString($hash, $result);
  1002. }
  1003. /**
  1004. * Tests that hidden fields generated for checkboxes don't get locked
  1005. *
  1006. * @return void
  1007. */
  1008. public function testNoCheckboxLocking()
  1009. {
  1010. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1011. $this->Form->create();
  1012. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  1013. $this->Form->checkbox('check', ['value' => '1']);
  1014. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  1015. }
  1016. /**
  1017. * testFormSecurityFields method
  1018. *
  1019. * Test generation of secure form hash generation.
  1020. *
  1021. * @return void
  1022. */
  1023. public function testFormSecurityFields()
  1024. {
  1025. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1026. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1027. $this->Form->create();
  1028. $result = $this->Form->secure($fields);
  1029. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  1030. $hash .= ':' . 'Model.valid';
  1031. $hash = urlencode($hash);
  1032. $tokenDebug = urlencode(json_encode([
  1033. $this->url,
  1034. $fields,
  1035. [],
  1036. ]));
  1037. $expected = [
  1038. 'div' => ['style' => 'display:none;'],
  1039. ['input' => [
  1040. 'type' => 'hidden',
  1041. 'name' => '_Token[fields]',
  1042. 'value' => $hash,
  1043. 'autocomplete' => 'off',
  1044. ]],
  1045. ['input' => [
  1046. 'type' => 'hidden',
  1047. 'name' => '_Token[unlocked]',
  1048. 'value' => '',
  1049. 'autocomplete' => 'off',
  1050. ]],
  1051. ['input' => [
  1052. 'type' => 'hidden',
  1053. 'name' => '_Token[debug]',
  1054. 'value' => $tokenDebug,
  1055. 'autocomplete' => 'off',
  1056. ]],
  1057. '/div',
  1058. ];
  1059. $this->assertHtml($expected, $result);
  1060. }
  1061. /**
  1062. * testFormSecurityFields method
  1063. *
  1064. * Test debug token is not generated if debug is false
  1065. *
  1066. * @return void
  1067. */
  1068. public function testFormSecurityFieldsNoDebugMode()
  1069. {
  1070. Configure::write('debug', false);
  1071. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1072. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1073. $this->Form->create();
  1074. $result = $this->Form->secure($fields);
  1075. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  1076. $hash .= ':' . 'Model.valid';
  1077. $hash = urlencode($hash);
  1078. $expected = [
  1079. 'div' => ['style' => 'display:none;'],
  1080. ['input' => [
  1081. 'type' => 'hidden',
  1082. 'name' => '_Token[fields]',
  1083. 'autocomplete' => 'off',
  1084. 'value' => $hash,
  1085. ]],
  1086. ['input' => [
  1087. 'type' => 'hidden',
  1088. 'name' => '_Token[unlocked]',
  1089. 'autocomplete' => 'off',
  1090. 'value' => '',
  1091. ]],
  1092. '/div',
  1093. ];
  1094. $this->assertHtml($expected, $result);
  1095. }
  1096. /**
  1097. * Tests correct generation of number fields for smallint
  1098. *
  1099. * @return void
  1100. */
  1101. public function testTextFieldGenerationForSmallint()
  1102. {
  1103. $this->article['schema'] = [
  1104. 'foo' => [
  1105. 'type' => 'smallinteger',
  1106. 'null' => false,
  1107. 'default' => null,
  1108. 'length' => 10,
  1109. ],
  1110. ];
  1111. $this->Form->create($this->article);
  1112. $result = $this->Form->control('foo');
  1113. $this->assertStringContainsString('class="input number"', $result);
  1114. $this->assertStringContainsString('type="number"', $result);
  1115. }
  1116. /**
  1117. * Tests correct generation of number fields for tinyint
  1118. *
  1119. * @return void
  1120. */
  1121. public function testTextFieldGenerationForTinyint()
  1122. {
  1123. $this->article['schema'] = [
  1124. 'foo' => [
  1125. 'type' => 'tinyinteger',
  1126. 'null' => false,
  1127. 'default' => null,
  1128. 'length' => 10,
  1129. ],
  1130. ];
  1131. $this->Form->create($this->article);
  1132. $result = $this->Form->control('foo');
  1133. $this->assertStringContainsString('class="input number"', $result);
  1134. $this->assertStringContainsString('type="number"', $result);
  1135. }
  1136. /**
  1137. * Tests correct generation of number fields for double and float fields
  1138. *
  1139. * @return void
  1140. */
  1141. public function testTextFieldGenerationForFloats()
  1142. {
  1143. $this->article['schema'] = [
  1144. 'foo' => [
  1145. 'type' => 'float',
  1146. 'null' => false,
  1147. 'default' => null,
  1148. 'length' => 10,
  1149. ],
  1150. ];
  1151. $this->Form->create($this->article);
  1152. $result = $this->Form->control('foo');
  1153. $expected = [
  1154. 'div' => ['class' => 'input number'],
  1155. 'label' => ['for' => 'foo'],
  1156. 'Foo',
  1157. '/label',
  1158. ['input' => [
  1159. 'type' => 'number',
  1160. 'name' => 'foo',
  1161. 'id' => 'foo',
  1162. 'step' => 'any',
  1163. ]],
  1164. '/div',
  1165. ];
  1166. $this->assertHtml($expected, $result);
  1167. $result = $this->Form->control('foo', ['step' => 0.5]);
  1168. $expected = [
  1169. 'div' => ['class' => 'input number'],
  1170. 'label' => ['for' => 'foo'],
  1171. 'Foo',
  1172. '/label',
  1173. ['input' => [
  1174. 'type' => 'number',
  1175. 'name' => 'foo',
  1176. 'id' => 'foo',
  1177. 'step' => '0.5',
  1178. ]],
  1179. '/div',
  1180. ];
  1181. $this->assertHtml($expected, $result);
  1182. }
  1183. /**
  1184. * Tests correct generation of number fields for integer fields
  1185. *
  1186. * @return void
  1187. */
  1188. public function testTextFieldTypeNumberGenerationForIntegers()
  1189. {
  1190. $this->getTableLocator()->get('Contacts', [
  1191. 'className' => ContactsTable::class,
  1192. ]);
  1193. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1194. $result = $this->Form->control('age');
  1195. $expected = [
  1196. 'div' => ['class' => 'input number'],
  1197. 'label' => ['for' => 'age'],
  1198. 'Age',
  1199. '/label',
  1200. ['input' => [
  1201. 'type' => 'number', 'name' => 'age',
  1202. 'id' => 'age',
  1203. ]],
  1204. '/div',
  1205. ];
  1206. $this->assertHtml($expected, $result);
  1207. }
  1208. /**
  1209. * Tests correct generation of file upload fields for binary fields
  1210. *
  1211. * @return void
  1212. */
  1213. public function testFileUploadFieldTypeGenerationForBinaries()
  1214. {
  1215. $table = $this->getTableLocator()->get('Contacts', [
  1216. 'className' => ContactsTable::class,
  1217. ]);
  1218. $table->setSchema(['foo' => [
  1219. 'type' => 'binary',
  1220. 'null' => false,
  1221. 'default' => null,
  1222. 'length' => 1024,
  1223. ]]);
  1224. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1225. $result = $this->Form->control('foo');
  1226. $expected = [
  1227. 'div' => ['class' => 'input file'],
  1228. 'label' => ['for' => 'foo'],
  1229. 'Foo',
  1230. '/label',
  1231. ['input' => [
  1232. 'type' => 'file', 'name' => 'foo',
  1233. 'id' => 'foo',
  1234. ]],
  1235. '/div',
  1236. ];
  1237. $this->assertHtml($expected, $result);
  1238. }
  1239. /**
  1240. * testFormSecurityMultipleFields method
  1241. *
  1242. * Test secure() with multiple row form. Ensure hash is correct.
  1243. *
  1244. * @return void
  1245. */
  1246. public function testFormSecurityMultipleFields()
  1247. {
  1248. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1249. $this->Form->create();
  1250. $fields = [
  1251. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1252. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1253. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1254. ];
  1255. $result = $this->Form->secure($fields);
  1256. $sortedFields = [
  1257. 'Model.0.password',
  1258. 'Model.0.username',
  1259. 'Model.1.password',
  1260. 'Model.1.username',
  1261. 'Model.0.hidden' => 'value',
  1262. 'Model.0.valid' => '0',
  1263. 'Model.1.hidden' => 'value',
  1264. 'Model.1.valid' => '0',
  1265. ];
  1266. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1267. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1268. $hash = urlencode($hash);
  1269. $tokenDebug = urlencode(json_encode([
  1270. $this->url,
  1271. $fields,
  1272. [],
  1273. ]));
  1274. $expected = [
  1275. 'div' => ['style' => 'display:none;'],
  1276. ['input' => [
  1277. 'type' => 'hidden',
  1278. 'name' => '_Token[fields]',
  1279. 'value' => $hash,
  1280. 'autocomplete' => 'off',
  1281. ]],
  1282. ['input' => [
  1283. 'type' => 'hidden',
  1284. 'name' => '_Token[unlocked]',
  1285. 'autocomplete' => 'off',
  1286. 'value' => '',
  1287. ]],
  1288. ['input' => [
  1289. 'type' => 'hidden',
  1290. 'name' => '_Token[debug]',
  1291. 'value' => $tokenDebug,
  1292. 'autocomplete' => 'off',
  1293. ]],
  1294. '/div',
  1295. ];
  1296. $this->assertHtml($expected, $result);
  1297. }
  1298. /**
  1299. * testFormSecurityMultipleSubmitButtons
  1300. *
  1301. * test form submit generation and ensure that _Token is only created on end()
  1302. *
  1303. * @return void
  1304. */
  1305. public function testFormSecurityMultipleSubmitButtons()
  1306. {
  1307. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1308. $this->Form->create($this->article);
  1309. $this->Form->text('Address.title');
  1310. $this->Form->text('Address.first_name');
  1311. $result = $this->Form->submit('Save', ['name' => 'save']);
  1312. $expected = [
  1313. 'div' => ['class' => 'submit'],
  1314. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1315. '/div',
  1316. ];
  1317. $this->assertHtml($expected, $result);
  1318. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1319. $expected = [
  1320. 'div' => ['class' => 'submit'],
  1321. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1322. '/div',
  1323. ];
  1324. $this->assertHtml($expected, $result);
  1325. $result = $this->Form->end();
  1326. $tokenDebug = urlencode(json_encode([
  1327. '/articles/add',
  1328. [
  1329. 'Address.title',
  1330. 'Address.first_name',
  1331. ],
  1332. ['save', 'cancel'],
  1333. ]));
  1334. $expected = [
  1335. 'div' => ['style' => 'display:none;'],
  1336. ['input' => [
  1337. 'type' => 'hidden',
  1338. 'name' => '_Token[fields]',
  1339. 'autocomplete',
  1340. 'value',
  1341. ]],
  1342. ['input' => [
  1343. 'type' => 'hidden',
  1344. 'name' => '_Token[unlocked]',
  1345. 'value' => 'cancel%7Csave',
  1346. 'autocomplete' => 'off',
  1347. ]],
  1348. ['input' => [
  1349. 'type' => 'hidden',
  1350. 'name' => '_Token[debug]',
  1351. 'value' => $tokenDebug,
  1352. 'autocomplete' => 'off',
  1353. ]],
  1354. '/div',
  1355. ];
  1356. $this->assertHtml($expected, $result);
  1357. }
  1358. /**
  1359. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1360. *
  1361. * @return void
  1362. */
  1363. public function testSecurityButtonNestedNamed()
  1364. {
  1365. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1366. $this->Form->create();
  1367. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1368. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1369. $this->assertEquals(['Address.button'], $result);
  1370. }
  1371. /**
  1372. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1373. *
  1374. * @return void
  1375. */
  1376. public function testSecuritySubmitNestedNamed()
  1377. {
  1378. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1379. $this->Form->create($this->article);
  1380. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1381. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1382. $this->assertEquals(['Address.button'], $result);
  1383. }
  1384. /**
  1385. * Test that the correct fields are unlocked for image submits with no names.
  1386. *
  1387. * @return void
  1388. */
  1389. public function testSecuritySubmitImageNoName()
  1390. {
  1391. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1392. $this->Form->create();
  1393. $result = $this->Form->submit('save.png');
  1394. $expected = [
  1395. 'div' => ['class' => 'submit'],
  1396. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1397. '/div',
  1398. ];
  1399. $this->assertHtml($expected, $result);
  1400. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1401. $this->assertEquals(['x', 'y'], $result);
  1402. }
  1403. /**
  1404. * Test that the correct fields are unlocked for image submits with names.
  1405. *
  1406. * @return void
  1407. */
  1408. public function testSecuritySubmitImageName()
  1409. {
  1410. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1411. $this->Form->create();
  1412. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1413. $expected = [
  1414. 'div' => ['class' => 'submit'],
  1415. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1416. '/div',
  1417. ];
  1418. $this->assertHtml($expected, $result);
  1419. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1420. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1421. }
  1422. /**
  1423. * testFormSecurityMultipleControlFields method
  1424. *
  1425. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1426. *
  1427. * @return void
  1428. */
  1429. public function testFormSecurityMultipleControlFields()
  1430. {
  1431. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1432. $this->Form->create();
  1433. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1434. $this->Form->control('Addresses.0.title');
  1435. $this->Form->control('Addresses.0.first_name');
  1436. $this->Form->control('Addresses.0.last_name');
  1437. $this->Form->control('Addresses.0.address');
  1438. $this->Form->control('Addresses.0.city');
  1439. $this->Form->control('Addresses.0.phone');
  1440. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1441. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1442. $this->Form->control('Addresses.1.title');
  1443. $this->Form->control('Addresses.1.first_name');
  1444. $this->Form->control('Addresses.1.last_name');
  1445. $this->Form->control('Addresses.1.address');
  1446. $this->Form->control('Addresses.1.city');
  1447. $this->Form->control('Addresses.1.phone');
  1448. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1449. $result = $this->Form->secure();
  1450. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1451. $tokenDebug = urlencode(json_encode([
  1452. '/articles/add',
  1453. [
  1454. 'Addresses.0.id' => '123456',
  1455. 'Addresses.0.title',
  1456. 'Addresses.0.first_name',
  1457. 'Addresses.0.last_name',
  1458. 'Addresses.0.address',
  1459. 'Addresses.0.city',
  1460. 'Addresses.0.phone',
  1461. 'Addresses.0.primary',
  1462. 'Addresses.1.id' => '654321',
  1463. 'Addresses.1.title',
  1464. 'Addresses.1.first_name',
  1465. 'Addresses.1.last_name',
  1466. 'Addresses.1.address',
  1467. 'Addresses.1.city',
  1468. 'Addresses.1.phone',
  1469. 'Addresses.1.primary',
  1470. ],
  1471. [],
  1472. ]));
  1473. $expected = [
  1474. 'div' => ['style' => 'display:none;'],
  1475. ['input' => [
  1476. 'type' => 'hidden',
  1477. 'name' => '_Token[fields]',
  1478. 'value' => $hash,
  1479. 'autocomplete' => 'off',
  1480. ]],
  1481. ['input' => [
  1482. 'type' => 'hidden',
  1483. 'name' => '_Token[unlocked]',
  1484. 'autocomplete' => 'off',
  1485. 'value' => '',
  1486. ]],
  1487. ['input' => [
  1488. 'type' => 'hidden',
  1489. 'name' => '_Token[debug]',
  1490. 'value' => $tokenDebug,
  1491. 'autocomplete' => 'off',
  1492. ]],
  1493. '/div',
  1494. ];
  1495. $this->assertHtml($expected, $result);
  1496. }
  1497. /**
  1498. * testFormSecurityArrayFields method
  1499. *
  1500. * Test form security with Model.field.0 style inputs.
  1501. *
  1502. * @return void
  1503. */
  1504. public function testFormSecurityArrayFields()
  1505. {
  1506. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1507. $this->Form->create();
  1508. $this->Form->text('Address.primary.1');
  1509. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1510. $this->assertSame('Address.primary', $result[0]);
  1511. $this->Form->text('Address.secondary.1.0');
  1512. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1513. $this->assertSame('Address.secondary', $result[1]);
  1514. }
  1515. /**
  1516. * testFormSecurityMultipleControlDisabledFields method
  1517. *
  1518. * Test secure form generation with multiple records and disabled fields.
  1519. *
  1520. * @return void
  1521. */
  1522. public function testFormSecurityMultipleControlDisabledFields()
  1523. {
  1524. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1525. 'unlockedFields' => ['first_name', 'address'],
  1526. ]));
  1527. $this->Form->create();
  1528. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1529. $this->Form->text('Addresses.0.title');
  1530. $this->Form->text('Addresses.0.first_name');
  1531. $this->Form->text('Addresses.0.last_name');
  1532. $this->Form->text('Addresses.0.address');
  1533. $this->Form->text('Addresses.0.city');
  1534. $this->Form->text('Addresses.0.phone');
  1535. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1536. $this->Form->text('Addresses.1.title');
  1537. $this->Form->text('Addresses.1.first_name');
  1538. $this->Form->text('Addresses.1.last_name');
  1539. $this->Form->text('Addresses.1.address');
  1540. $this->Form->text('Addresses.1.city');
  1541. $this->Form->text('Addresses.1.phone');
  1542. $result = $this->Form->secure();
  1543. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1544. $tokenDebug = urlencode(json_encode([
  1545. '/articles/add',
  1546. [
  1547. 'Addresses.0.id' => '123456',
  1548. 'Addresses.0.title',
  1549. 'Addresses.0.last_name',
  1550. 'Addresses.0.city',
  1551. 'Addresses.0.phone',
  1552. 'Addresses.1.id' => '654321',
  1553. 'Addresses.1.title',
  1554. 'Addresses.1.last_name',
  1555. 'Addresses.1.city',
  1556. 'Addresses.1.phone',
  1557. ],
  1558. [
  1559. 'first_name',
  1560. 'address',
  1561. ],
  1562. ]));
  1563. $expected = [
  1564. 'div' => ['style' => 'display:none;'],
  1565. ['input' => [
  1566. 'type' => 'hidden',
  1567. 'name' => '_Token[fields]',
  1568. 'autocomplete' => 'off',
  1569. 'value' => $hash,
  1570. ]],
  1571. ['input' => [
  1572. 'type' => 'hidden',
  1573. 'name' => '_Token[unlocked]',
  1574. 'autocomplete' => 'off',
  1575. 'value' => 'address%7Cfirst_name',
  1576. ]],
  1577. ['input' => [
  1578. 'type' => 'hidden',
  1579. 'name' => '_Token[debug]',
  1580. 'autocomplete' => 'off',
  1581. 'value' => $tokenDebug,
  1582. ]],
  1583. '/div',
  1584. ];
  1585. $this->assertHtml($expected, $result);
  1586. }
  1587. /**
  1588. * testFormSecurityControlDisabledFields method
  1589. *
  1590. * Test single record form with disabled fields.
  1591. *
  1592. * @return void
  1593. */
  1594. public function testFormSecurityControlUnlockedFields()
  1595. {
  1596. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1597. 'unlockedFields' => ['first_name', 'address'],
  1598. ]));
  1599. $this->Form->create();
  1600. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1601. $this->assertEquals(
  1602. $this->View->getRequest()->getAttribute('formTokenData'),
  1603. ['unlockedFields' => $result]
  1604. );
  1605. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1606. $this->Form->text('Addresses.title');
  1607. $this->Form->text('Addresses.first_name');
  1608. $this->Form->text('Addresses.last_name');
  1609. $this->Form->text('Addresses.address');
  1610. $this->Form->text('Addresses.city');
  1611. $this->Form->text('Addresses.phone');
  1612. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1613. $expected = [
  1614. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1615. 'Addresses.city', 'Addresses.phone',
  1616. ];
  1617. $this->assertEquals($expected, $result);
  1618. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1619. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1620. $tokenDebug = urlencode(json_encode([
  1621. '/articles/add',
  1622. [
  1623. 'Addresses.id' => '123456',
  1624. 'Addresses.title',
  1625. 'Addresses.last_name',
  1626. 'Addresses.city',
  1627. 'Addresses.phone',
  1628. ],
  1629. [
  1630. 'first_name',
  1631. 'address',
  1632. ],
  1633. ]));
  1634. $expected = [
  1635. 'div' => ['style' => 'display:none;'],
  1636. ['input' => [
  1637. 'type' => 'hidden',
  1638. 'name' => '_Token[fields]',
  1639. 'value' => $hash,
  1640. 'autocomplete' => 'off',
  1641. 'data-foo' => 'bar',
  1642. ]],
  1643. ['input' => [
  1644. 'type' => 'hidden',
  1645. 'name' => '_Token[unlocked]',
  1646. 'value' => 'address%7Cfirst_name',
  1647. 'autocomplete' => 'off',
  1648. 'data-foo' => 'bar',
  1649. ]],
  1650. ['input' => [
  1651. 'type' => 'hidden', 'name' => '_Token[debug]',
  1652. 'value' => $tokenDebug,
  1653. 'autocomplete' => 'off',
  1654. 'data-foo' => 'bar',
  1655. ]],
  1656. '/div',
  1657. ];
  1658. $this->assertHtml($expected, $result);
  1659. }
  1660. /**
  1661. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1662. *
  1663. * Test single record form with debugSecurity param.
  1664. *
  1665. * @return void
  1666. */
  1667. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1668. {
  1669. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1670. 'unlockedFields' => ['first_name', 'address'],
  1671. ]));
  1672. $this->Form->create();
  1673. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1674. $this->assertEquals(
  1675. $this->View->getRequest()->getAttribute('formTokenData'),
  1676. ['unlockedFields' => $result]
  1677. );
  1678. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1679. $this->Form->text('Addresses.title');
  1680. $this->Form->text('Addresses.first_name');
  1681. $this->Form->text('Addresses.last_name');
  1682. $this->Form->text('Addresses.address');
  1683. $this->Form->text('Addresses.city');
  1684. $this->Form->text('Addresses.phone');
  1685. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1686. $expected = [
  1687. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1688. 'Addresses.city', 'Addresses.phone',
  1689. ];
  1690. $this->assertEquals($expected, $result);
  1691. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1692. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1693. $tokenDebug = urlencode(json_encode([
  1694. '/articles/add',
  1695. [
  1696. 'Addresses.id' => '123456',
  1697. 'Addresses.title',
  1698. 'Addresses.last_name',
  1699. 'Addresses.city',
  1700. 'Addresses.phone',
  1701. ],
  1702. [
  1703. 'first_name',
  1704. 'address',
  1705. ],
  1706. ]));
  1707. $expected = [
  1708. 'div' => ['style' => 'display:none;'],
  1709. ['input' => [
  1710. 'type' => 'hidden',
  1711. 'name' => '_Token[fields]',
  1712. 'value' => $hash,
  1713. 'autocomplete' => 'off',
  1714. 'data-foo' => 'bar',
  1715. ]],
  1716. ['input' => [
  1717. 'type' => 'hidden',
  1718. 'name' => '_Token[unlocked]',
  1719. 'value' => 'address%7Cfirst_name',
  1720. 'autocomplete' => 'off',
  1721. 'data-foo' => 'bar',
  1722. ]],
  1723. ['input' => [
  1724. 'type' => 'hidden', 'name' => '_Token[debug]',
  1725. 'value' => $tokenDebug,
  1726. 'autocomplete' => 'off',
  1727. 'data-foo' => 'bar',
  1728. ]],
  1729. '/div',
  1730. ];
  1731. $this->assertHtml($expected, $result);
  1732. }
  1733. /**
  1734. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1735. *
  1736. * Debug is false, debugSecurity is true -> no debug
  1737. *
  1738. * @return void
  1739. */
  1740. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1741. {
  1742. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1743. 'unlockedFields' => ['first_name', 'address'],
  1744. ]));
  1745. $this->Form->create();
  1746. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1747. $this->assertEquals(
  1748. $this->View->getRequest()->getAttribute('formTokenData'),
  1749. ['unlockedFields' => $result]
  1750. );
  1751. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1752. $this->Form->text('Addresses.title');
  1753. $this->Form->text('Addresses.first_name');
  1754. $this->Form->text('Addresses.last_name');
  1755. $this->Form->text('Addresses.address');
  1756. $this->Form->text('Addresses.city');
  1757. $this->Form->text('Addresses.phone');
  1758. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1759. $expected = [
  1760. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1761. 'Addresses.city', 'Addresses.phone',
  1762. ];
  1763. $this->assertEquals($expected, $result);
  1764. Configure::write('debug', false);
  1765. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1766. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1767. $expected = [
  1768. 'div' => ['style' => 'display:none;'],
  1769. ['input' => [
  1770. 'type' => 'hidden',
  1771. 'name' => '_Token[fields]',
  1772. 'value' => $hash,
  1773. 'autocomplete' => 'off',
  1774. 'data-foo' => 'bar',
  1775. ]],
  1776. ['input' => [
  1777. 'type' => 'hidden',
  1778. 'name' => '_Token[unlocked]',
  1779. 'value' => 'address%7Cfirst_name',
  1780. 'autocomplete' => 'off',
  1781. 'data-foo' => 'bar',
  1782. ]],
  1783. '/div',
  1784. ];
  1785. $this->assertHtml($expected, $result);
  1786. }
  1787. /**
  1788. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1789. *
  1790. * Test single record form with debugSecurity param.
  1791. *
  1792. * @return void
  1793. */
  1794. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1795. {
  1796. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1797. 'unlockedFields' => ['first_name', 'address'],
  1798. ]));
  1799. $this->Form->create();
  1800. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1801. $this->assertEquals(
  1802. $this->View->getRequest()->getAttribute('formTokenData'),
  1803. ['unlockedFields' => $result]
  1804. );
  1805. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1806. $this->Form->text('Addresses.title');
  1807. $this->Form->text('Addresses.first_name');
  1808. $this->Form->text('Addresses.last_name');
  1809. $this->Form->text('Addresses.address');
  1810. $this->Form->text('Addresses.city');
  1811. $this->Form->text('Addresses.phone');
  1812. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1813. $expected = [
  1814. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1815. 'Addresses.city', 'Addresses.phone',
  1816. ];
  1817. $this->assertEquals($expected, $result);
  1818. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1819. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1820. $expected = [
  1821. 'div' => ['style' => 'display:none;'],
  1822. ['input' => [
  1823. 'type' => 'hidden',
  1824. 'name' => '_Token[fields]',
  1825. 'value' => $hash,
  1826. 'autocomplete' => 'off',
  1827. 'data-foo' => 'bar',
  1828. ]],
  1829. ['input' => [
  1830. 'type' => 'hidden',
  1831. 'name' => '_Token[unlocked]',
  1832. 'value' => 'address%7Cfirst_name',
  1833. 'autocomplete' => 'off',
  1834. 'data-foo' => 'bar',
  1835. ]],
  1836. '/div',
  1837. ];
  1838. $this->assertHtml($expected, $result);
  1839. }
  1840. /**
  1841. * testFormSecureWithCustomNameAttribute method
  1842. *
  1843. * Test securing inputs with custom name attributes.
  1844. *
  1845. * @return void
  1846. */
  1847. public function testFormSecureWithCustomNameAttribute()
  1848. {
  1849. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1850. $this->Form->create();
  1851. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1852. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1853. $this->assertSame('User.custom', $result[0]);
  1854. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1855. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1856. $this->assertSame('User.custom.another.value', $result[1]);
  1857. }
  1858. /**
  1859. * testFormSecuredControl method
  1860. *
  1861. * Test generation of entire secure form, assertions made on control() output.
  1862. *
  1863. * @return void
  1864. */
  1865. public function testFormSecuredControl()
  1866. {
  1867. $this->View->setRequest($this->View->getRequest()
  1868. ->withAttribute('formTokenData', [])
  1869. ->withAttribute('csrfToken', 'testKey'));
  1870. $this->article['schema'] = [
  1871. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1872. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1873. ];
  1874. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1875. $encoding = strtolower(Configure::read('App.encoding'));
  1876. $expected = [
  1877. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1878. 'div' => ['style' => 'display:none;'],
  1879. ['input' => [
  1880. 'type' => 'hidden',
  1881. 'name' => '_csrfToken',
  1882. 'value' => 'testKey',
  1883. 'autocomplete' => 'off',
  1884. ]],
  1885. '/div',
  1886. ];
  1887. $this->assertHtml($expected, $result);
  1888. $result = $this->Form->control('ratio');
  1889. $expected = [
  1890. 'div' => ['class'],
  1891. 'label' => ['for'],
  1892. 'Ratio',
  1893. '/label',
  1894. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1895. '/div',
  1896. ];
  1897. $this->assertHtml($expected, $result);
  1898. $result = $this->Form->control('population');
  1899. $expected = [
  1900. 'div' => ['class'],
  1901. 'label' => ['for'],
  1902. 'Population',
  1903. '/label',
  1904. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1905. '/div',
  1906. ];
  1907. $this->assertHtml($expected, $result);
  1908. $result = $this->Form->control('published', ['type' => 'text']);
  1909. $expected = [
  1910. 'div' => ['class' => 'input text'],
  1911. 'label' => ['for' => 'published'],
  1912. 'Published',
  1913. '/label',
  1914. ['input' => [
  1915. 'type' => 'text',
  1916. 'name' => 'published',
  1917. 'id' => 'published',
  1918. ]],
  1919. '/div',
  1920. ];
  1921. $this->assertHtml($expected, $result);
  1922. $result = $this->Form->control('other', ['type' => 'text']);
  1923. $expected = [
  1924. 'div' => ['class' => 'input text'],
  1925. 'label' => ['for' => 'other'],
  1926. 'Other',
  1927. '/label',
  1928. ['input' => [
  1929. 'type' => 'text',
  1930. 'name' => 'other',
  1931. 'id',
  1932. ]],
  1933. '/div',
  1934. ];
  1935. $this->assertHtml($expected, $result);
  1936. $result = $this->Form->hidden('stuff');
  1937. $expected = [
  1938. 'input' => [
  1939. 'type' => 'hidden',
  1940. 'name' => 'stuff',
  1941. ],
  1942. ];
  1943. $this->assertHtml($expected, $result);
  1944. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1945. $expected = ['input' => [
  1946. 'type' => 'hidden',
  1947. 'name' => 'hidden',
  1948. 'value' => '0',
  1949. ]];
  1950. $this->assertHtml($expected, $result);
  1951. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1952. $expected = [
  1953. 'div' => ['class' => 'input checkbox'],
  1954. ['input' => [
  1955. 'type' => 'hidden',
  1956. 'name' => 'something',
  1957. 'value' => '0',
  1958. ]],
  1959. 'label' => ['for' => 'something'],
  1960. ['input' => [
  1961. 'type' => 'checkbox',
  1962. 'name' => 'something',
  1963. 'value' => '1',
  1964. 'id' => 'something',
  1965. ]],
  1966. 'Something',
  1967. '/label',
  1968. '/div',
  1969. ];
  1970. $this->assertHtml($expected, $result);
  1971. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1972. $expectedFields = [
  1973. 'ratio',
  1974. 'population',
  1975. 'published',
  1976. 'other',
  1977. 'stuff' => '',
  1978. 'hidden' => '0',
  1979. 'something',
  1980. ];
  1981. $this->assertEquals($expectedFields, $result);
  1982. $result = $this->Form->secure();
  1983. $tokenDebug = urlencode(json_encode([
  1984. '/articles/add',
  1985. $expectedFields,
  1986. [],
  1987. ]));
  1988. $expected = [
  1989. 'div' => ['style' => 'display:none;'],
  1990. ['input' => [
  1991. 'type' => 'hidden',
  1992. 'name' => '_Token[fields]',
  1993. 'value',
  1994. 'autocomplete',
  1995. ]],
  1996. ['input' => [
  1997. 'type' => 'hidden',
  1998. 'name' => '_Token[unlocked]',
  1999. 'value' => '',
  2000. 'autocomplete' => 'off',
  2001. ]],
  2002. ['input' => [
  2003. 'type' => 'hidden', 'name' => '_Token[debug]',
  2004. 'value' => $tokenDebug,
  2005. 'autocomplete' => 'off',
  2006. ]],
  2007. '/div',
  2008. ];
  2009. $this->assertHtml($expected, $result);
  2010. }
  2011. /**
  2012. * testSecuredControlCustomName method
  2013. *
  2014. * Test secured inputs with custom names.
  2015. *
  2016. * @return void
  2017. */
  2018. public function testSecuredControlCustomName()
  2019. {
  2020. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2021. $this->Form->create();
  2022. $this->Form->text('text_input', [
  2023. 'name' => 'Option[General.default_role]',
  2024. ]);
  2025. $expected = ['Option.General.default_role'];
  2026. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2027. $this->assertEquals($expected, $result);
  2028. $this->Form->select('select_box', [1, 2], [
  2029. 'name' => 'Option[General.select_role]',
  2030. ]);
  2031. $expected[] = 'Option.General.select_role';
  2032. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2033. $this->assertEquals($expected, $result);
  2034. $this->Form->text('other.things[]');
  2035. $expected[] = 'other.things';
  2036. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2037. $this->assertEquals($expected, $result);
  2038. }
  2039. /**
  2040. * testSecuredControlDuplicate method
  2041. *
  2042. * Test that a hidden field followed by a visible field
  2043. * undoes the hidden field locking.
  2044. *
  2045. * @return void
  2046. */
  2047. public function testSecuredControlDuplicate()
  2048. {
  2049. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2050. $this->Form->create();
  2051. $this->Form->control('text_val', [
  2052. 'type' => 'hidden',
  2053. 'value' => 'some text',
  2054. ]);
  2055. $expected = ['text_val' => 'some text'];
  2056. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2057. $this->assertEquals($expected, $result);
  2058. $this->Form->control('text_val', [
  2059. 'type' => 'text',
  2060. ]);
  2061. $expected = ['text_val'];
  2062. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2063. $this->assertEquals($expected, $result);
  2064. }
  2065. /**
  2066. * testFormSecuredFileControl method
  2067. *
  2068. * Tests that the correct keys are added to the field hash index.
  2069. *
  2070. * @return void
  2071. */
  2072. public function testFormSecuredFileControl()
  2073. {
  2074. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2075. $this->Form->create();
  2076. $this->Form->file('Attachment.file');
  2077. $expected = [
  2078. 'Attachment.file.name', 'Attachment.file.type',
  2079. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2080. 'Attachment.file.size',
  2081. ];
  2082. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2083. $this->assertEquals($expected, $result);
  2084. }
  2085. /**
  2086. * testFormSecuredMultipleSelect method
  2087. *
  2088. * Test that multiple selects keys are added to field hash.
  2089. *
  2090. * @return void
  2091. */
  2092. public function testFormSecuredMultipleSelect()
  2093. {
  2094. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2095. $this->Form->create();
  2096. $options = ['1' => 'one', '2' => 'two'];
  2097. $this->Form->select('Model.select', $options);
  2098. $expected = ['Model.select'];
  2099. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2100. $this->assertEquals($expected, $result);
  2101. $this->Form->fields = [];
  2102. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2103. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2104. $this->assertEquals($expected, $result);
  2105. }
  2106. /**
  2107. * testFormSecuredRadio method
  2108. *
  2109. * @return void
  2110. */
  2111. public function testFormSecuredRadio()
  2112. {
  2113. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2114. $this->Form->create();
  2115. $options = ['1' => 'option1', '2' => 'option2'];
  2116. $this->Form->radio('Test.test', $options);
  2117. $expected = ['Test.test'];
  2118. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2119. $this->assertEquals($expected, $result);
  2120. $this->Form->radio('Test.all', $options, [
  2121. 'disabled' => ['option1', 'option2'],
  2122. ]);
  2123. $expected = ['Test.test', 'Test.all' => ''];
  2124. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2125. $this->assertEquals($expected, $result);
  2126. $this->Form->radio('Test.some', $options, [
  2127. 'disabled' => ['option1'],
  2128. ]);
  2129. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2130. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2131. $this->assertEquals($expected, $result);
  2132. }
  2133. /**
  2134. * testFormSecuredAndDisabledNotAssoc method
  2135. *
  2136. * Test that when disabled is in a list based attribute array it works.
  2137. *
  2138. * @return void
  2139. */
  2140. public function testFormSecuredAndDisabledNotAssoc()
  2141. {
  2142. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2143. $this->Form->create();
  2144. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2145. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2146. $this->Form->text('Model.text', ['disabled']);
  2147. $this->Form->textarea('Model.textarea', ['disabled']);
  2148. $this->Form->password('Model.password', ['disabled']);
  2149. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2150. $expected = [
  2151. 'Model.radio' => '',
  2152. ];
  2153. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2154. $this->assertEquals($expected, $result);
  2155. }
  2156. /**
  2157. * testFormSecuredAndDisabled method
  2158. *
  2159. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2160. * hashing.
  2161. *
  2162. * @return void
  2163. */
  2164. public function testFormSecuredAndDisabled()
  2165. {
  2166. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2167. $this->Form->create();
  2168. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2169. $this->Form->text('Model.text', ['disabled' => true]);
  2170. $this->Form->password('Model.text', ['disabled' => true]);
  2171. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2172. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2173. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2174. $this->Form->year('Model.year', ['disabled' => true]);
  2175. $this->Form->month('Model.month', ['disabled' => true]);
  2176. $this->Form->day('Model.day', ['disabled' => true]);
  2177. $this->Form->hour('Model.hour', ['disabled' => true]);
  2178. $this->Form->minute('Model.minute', ['disabled' => true]);
  2179. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2180. $expected = [
  2181. 'Model.radio' => '',
  2182. ];
  2183. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2184. $this->assertEquals($expected, $result);
  2185. }
  2186. /**
  2187. * testUnlockFieldAddsToList method
  2188. *
  2189. * Test disableField.
  2190. *
  2191. * @return void
  2192. */
  2193. public function testUnlockFieldAddsToList()
  2194. {
  2195. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2196. 'unlockedFields' => [],
  2197. ]));
  2198. $this->Form->create();
  2199. $this->Form->unlockField('Contact.name');
  2200. $this->Form->text('Contact.name');
  2201. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2202. $this->assertEquals([], $result);
  2203. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2204. $this->assertEquals(['Contact.name'], $result);
  2205. }
  2206. /**
  2207. * testUnlockFieldRemovingFromFields method
  2208. *
  2209. * Test unlockField removing from fields array.
  2210. *
  2211. * @return void
  2212. */
  2213. public function testUnlockFieldRemovingFromFields()
  2214. {
  2215. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2216. 'unlockedFields' => [],
  2217. ]));
  2218. $this->Form->create($this->article);
  2219. $this->Form->hidden('Article.id', ['value' => 1]);
  2220. $this->Form->text('Article.title');
  2221. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2222. $this->assertEquals(1, $result['Article.id'], 'Hidden input should be secured.');
  2223. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2224. $this->Form->unlockField('Article.title');
  2225. $this->Form->unlockField('Article.id');
  2226. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2227. $this->assertEquals([], $result);
  2228. }
  2229. /**
  2230. * testResetUnlockFields method
  2231. *
  2232. * Test reset unlockFields, when create new form.
  2233. *
  2234. * @return void
  2235. */
  2236. public function testResetUnlockFields()
  2237. {
  2238. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2239. 'key' => 'testKey',
  2240. 'unlockedFields' => [],
  2241. ]));
  2242. $this->Form->create();
  2243. $this->Form->unlockField('Contact.id');
  2244. $this->Form->hidden('Contact.id', ['value' => 1]);
  2245. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2246. $this->assertEmpty($result, 'Field should be unlocked');
  2247. $this->Form->end();
  2248. $this->Form->create();
  2249. $this->Form->hidden('Contact.id', ['value' => 1]);
  2250. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2251. $this->assertEquals(1, $result['Contact.id'], 'Hidden input should be secured.');
  2252. }
  2253. /**
  2254. * testSecuredFormUrlIgnoresHost method
  2255. *
  2256. * Test that only the path + query elements of a form's URL show up in their hash.
  2257. *
  2258. * @return void
  2259. */
  2260. public function testSecuredFormUrlIgnoresHost()
  2261. {
  2262. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2263. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2264. $this->Form->create($this->article, [
  2265. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2266. ]);
  2267. $result = $this->Form->secure();
  2268. $this->assertStringContainsString($expected, $result);
  2269. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2270. $result = $this->Form->secure();
  2271. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2272. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2273. $result = $this->Form->secure();
  2274. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2275. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2276. $result = $this->Form->secure();
  2277. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2278. }
  2279. /**
  2280. * testSecuredFormUrlHasHtmlAndIdentifier method
  2281. *
  2282. * Test that URL, HTML and identifier show up in their hashes.
  2283. *
  2284. * @return void
  2285. */
  2286. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2287. {
  2288. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2289. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2290. $this->Form->create($this->article, [
  2291. 'url' => [
  2292. 'controller' => 'articles',
  2293. 'action' => 'view',
  2294. '?' => [
  2295. 'page' => 1,
  2296. 'limit' => 10,
  2297. 'html' => '<>"',
  2298. ],
  2299. '#' => 'result',
  2300. ],
  2301. ]);
  2302. $result = $this->Form->secure();
  2303. $this->assertStringContainsString($expected, $result);
  2304. $this->Form->create($this->article, [
  2305. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2306. ]);
  2307. $result = $this->Form->secure();
  2308. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2309. $this->Form->create($this->article, [
  2310. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2311. ]);
  2312. $result = $this->Form->secure();
  2313. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2314. }
  2315. /**
  2316. * testErrorMessageDisplay method
  2317. *
  2318. * Test error message display.
  2319. *
  2320. * @return void
  2321. */
  2322. public function testErrorMessageDisplay()
  2323. {
  2324. $this->article['errors'] = [
  2325. 'Article' => [
  2326. 'title' => 'error message',
  2327. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2328. ],
  2329. ];
  2330. $this->Form->create($this->article);
  2331. $result = $this->Form->control('Article.title');
  2332. $expected = [
  2333. 'div' => ['class' => 'input text error'],
  2334. 'label' => ['for' => 'article-title'],
  2335. 'Title',
  2336. '/label',
  2337. 'input' => [
  2338. 'type' => 'text', 'name' => 'Article[title]',
  2339. 'id' => 'article-title', 'class' => 'form-error',
  2340. ],
  2341. ['div' => ['class' => 'error-message']],
  2342. 'error message',
  2343. '/div',
  2344. '/div',
  2345. ];
  2346. $this->assertHtml($expected, $result);
  2347. $result = $this->Form->control('Article.title', [
  2348. 'templates' => [
  2349. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2350. ],
  2351. ]);
  2352. $expected = [
  2353. 'div' => ['class' => 'input text error'],
  2354. 'label' => ['for' => 'article-title'],
  2355. 'Title',
  2356. '/label',
  2357. 'input' => [
  2358. 'type' => 'text', 'name' => 'Article[title]',
  2359. 'id' => 'article-title', 'class' => 'form-error',
  2360. ],
  2361. '/div',
  2362. ];
  2363. $this->assertHtml($expected, $result);
  2364. $result = $this->Form->control('Article.content');
  2365. $expected = [
  2366. 'div' => ['class' => 'input text error'],
  2367. 'label' => ['for' => 'article-content'],
  2368. 'Content',
  2369. '/label',
  2370. 'input' => [
  2371. 'type' => 'text', 'name' => 'Article[content]',
  2372. 'id' => 'article-content', 'class' => 'form-error',
  2373. ],
  2374. ['div' => ['class' => 'error-message']],
  2375. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2376. '/div',
  2377. '/div',
  2378. ];
  2379. $this->assertHtml($expected, $result);
  2380. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2381. $expected = [
  2382. 'div' => ['class' => 'input text error'],
  2383. 'label' => ['for' => 'article-content'],
  2384. 'Content',
  2385. '/label',
  2386. 'input' => [
  2387. 'type' => 'text', 'name' => 'Article[content]',
  2388. 'id' => 'article-content', 'class' => 'form-error',
  2389. ],
  2390. ['div' => ['class' => 'error-message']],
  2391. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2392. '/div',
  2393. '/div',
  2394. ];
  2395. $this->assertHtml($expected, $result);
  2396. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2397. $expected = [
  2398. 'div' => ['class' => 'input text error'],
  2399. 'label' => ['for' => 'article-content'],
  2400. 'Content',
  2401. '/label',
  2402. 'input' => [
  2403. 'type' => 'text', 'name' => 'Article[content]',
  2404. 'id' => 'article-content', 'class' => 'form-error',
  2405. ],
  2406. ['div' => ['class' => 'error-message']],
  2407. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2408. '/div',
  2409. '/div',
  2410. ];
  2411. $this->assertHtml($expected, $result);
  2412. }
  2413. /**
  2414. * testEmptyErrorValidation method
  2415. *
  2416. * Test validation errors, when validation message is an empty string.
  2417. *
  2418. * @return void
  2419. */
  2420. public function testEmptyErrorValidation()
  2421. {
  2422. $this->article['errors'] = [
  2423. 'Article' => ['title' => ''],
  2424. ];
  2425. $this->Form->create($this->article);
  2426. $result = $this->Form->control('Article.title');
  2427. $expected = [
  2428. 'div' => ['class' => 'input text error'],
  2429. 'label' => ['for' => 'article-title'],
  2430. 'Title',
  2431. '/label',
  2432. 'input' => [
  2433. 'type' => 'text', 'name' => 'Article[title]',
  2434. 'id' => 'article-title', 'class' => 'form-error',
  2435. ],
  2436. ['div' => ['class' => 'error-message']],
  2437. [],
  2438. '/div',
  2439. '/div',
  2440. ];
  2441. $this->assertHtml($expected, $result);
  2442. }
  2443. /**
  2444. * testEmptyControlErrorValidation method
  2445. *
  2446. * Test validation errors, when calling control() overriding validation message by an empty string.
  2447. *
  2448. * @return void
  2449. */
  2450. public function testEmptyControlErrorValidation()
  2451. {
  2452. $this->article['errors'] = [
  2453. 'Article' => ['title' => 'error message'],
  2454. ];
  2455. $this->Form->create($this->article);
  2456. $result = $this->Form->control('Article.title', ['error' => '']);
  2457. $expected = [
  2458. 'div' => ['class' => 'input text error'],
  2459. 'label' => ['for' => 'article-title'],
  2460. 'Title',
  2461. '/label',
  2462. 'input' => [
  2463. 'type' => 'text', 'name' => 'Article[title]',
  2464. 'id' => 'article-title', 'class' => 'form-error',
  2465. ],
  2466. ['div' => ['class' => 'error-message']],
  2467. [],
  2468. '/div',
  2469. '/div',
  2470. ];
  2471. $this->assertHtml($expected, $result);
  2472. }
  2473. /**
  2474. * testControlErrorMessage method
  2475. *
  2476. * Test validation errors, when calling control() overriding validation messages.
  2477. *
  2478. * @return void
  2479. */
  2480. public function testControlErrorMessage()
  2481. {
  2482. $this->article['errors'] = [
  2483. 'title' => ['error message'],
  2484. ];
  2485. $this->Form->create($this->article);
  2486. $result = $this->Form->control('title', [
  2487. 'error' => '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', 'name' => 'title',
  2496. 'id' => 'title', 'class' => 'form-error',
  2497. 'required' => 'required',
  2498. 'data-validity-message' => 'This field cannot be left empty',
  2499. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2500. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2501. ],
  2502. ['div' => ['class' => 'error-message']],
  2503. 'Custom error!',
  2504. '/div',
  2505. '/div',
  2506. ];
  2507. $this->assertHtml($expected, $result);
  2508. $result = $this->Form->control('title', [
  2509. 'error' => ['error message' => 'Custom error!'],
  2510. ]);
  2511. $expected = [
  2512. 'div' => ['class' => 'input text required error'],
  2513. 'label' => ['for' => 'title'],
  2514. 'Title',
  2515. '/label',
  2516. 'input' => [
  2517. 'type' => 'text',
  2518. 'name' => 'title',
  2519. 'id' => 'title',
  2520. 'class' => 'form-error',
  2521. 'required' => 'required',
  2522. 'data-validity-message' => 'This field cannot be left empty',
  2523. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2524. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2525. ],
  2526. ['div' => ['class' => 'error-message']],
  2527. 'Custom error!',
  2528. '/div',
  2529. '/div',
  2530. ];
  2531. $this->assertHtml($expected, $result);
  2532. }
  2533. /**
  2534. * testFormValidationAssociated method
  2535. *
  2536. * Tests displaying errors for nested entities.
  2537. *
  2538. * @return void
  2539. */
  2540. public function testFormValidationAssociated()
  2541. {
  2542. $nested = new Entity(['foo' => 'bar']);
  2543. $nested->setError('foo', ['not a valid bar']);
  2544. $entity = new Entity(['nested' => $nested]);
  2545. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2546. $result = $this->Form->error('nested.foo');
  2547. $this->assertSame('<div class="error-message">not a valid bar</div>', $result);
  2548. }
  2549. /**
  2550. * testFormValidationAssociatedSecondLevel method
  2551. *
  2552. * Test form error display with associated model.
  2553. *
  2554. * @return void
  2555. */
  2556. public function testFormValidationAssociatedSecondLevel()
  2557. {
  2558. $inner = new Entity(['bar' => 'baz']);
  2559. $nested = new Entity(['foo' => $inner]);
  2560. $entity = new Entity(['nested' => $nested]);
  2561. $inner->setError('bar', ['not a valid one']);
  2562. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2563. $result = $this->Form->error('nested.foo.bar');
  2564. $this->assertSame('<div class="error-message">not a valid one</div>', $result);
  2565. }
  2566. /**
  2567. * testFormValidationMultiRecord method
  2568. *
  2569. * Test form error display with multiple records.
  2570. *
  2571. * @return void
  2572. */
  2573. public function testFormValidationMultiRecord()
  2574. {
  2575. $one = new Entity();
  2576. $two = new Entity();
  2577. $this->getTableLocator()->get('Contacts', [
  2578. 'className' => ContactsTable::class,
  2579. ]);
  2580. $one->set('email', '');
  2581. $one->setError('email', ['invalid email']);
  2582. $two->set('name', '');
  2583. $two->setError('name', ['This is wrong']);
  2584. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2585. $result = $this->Form->control('0.email');
  2586. $expected = [
  2587. 'div' => ['class' => 'input email error'],
  2588. 'label' => ['for' => '0-email'],
  2589. 'Email',
  2590. '/label',
  2591. 'input' => [
  2592. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2593. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2594. ],
  2595. ['div' => ['class' => 'error-message']],
  2596. 'invalid email',
  2597. '/div',
  2598. '/div',
  2599. ];
  2600. $this->assertHtml($expected, $result);
  2601. $result = $this->Form->control('1.name');
  2602. $expected = [
  2603. 'div' => ['class' => 'input text error'],
  2604. 'label' => ['for' => '1-name'],
  2605. 'Name',
  2606. '/label',
  2607. 'input' => [
  2608. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2609. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2610. ],
  2611. ['div' => ['class' => 'error-message']],
  2612. 'This is wrong',
  2613. '/div',
  2614. '/div',
  2615. ];
  2616. $this->assertHtml($expected, $result);
  2617. }
  2618. /**
  2619. * testControl method
  2620. *
  2621. * Test various incarnations of control().
  2622. *
  2623. * @return void
  2624. */
  2625. public function testControl()
  2626. {
  2627. $this->getTableLocator()->get('ValidateUsers', [
  2628. 'className' => ValidateUsersTable::class,
  2629. ]);
  2630. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2631. $result = $this->Form->control('ValidateUsers.balance');
  2632. $expected = [
  2633. 'div' => ['class'],
  2634. 'label' => ['for'],
  2635. 'Balance',
  2636. '/label',
  2637. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2638. '/div',
  2639. ];
  2640. $this->assertHtml($expected, $result);
  2641. $result = $this->Form->control('ValidateUser.cost_decimal');
  2642. $expected = [
  2643. 'div' => ['class'],
  2644. 'label' => ['for'],
  2645. 'Cost Decimal',
  2646. '/label',
  2647. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2648. '/div',
  2649. ];
  2650. $this->assertHtml($expected, $result);
  2651. $result = $this->Form->control('ValidateUser.null_decimal');
  2652. $expected = [
  2653. 'div' => ['class'],
  2654. 'label' => ['for'],
  2655. 'Null Decimal',
  2656. '/label',
  2657. 'input' => ['name', 'type' => 'number', 'id'],
  2658. '/div',
  2659. ];
  2660. $this->assertHtml($expected, $result);
  2661. }
  2662. /**
  2663. * testControlCustomization method
  2664. *
  2665. * Tests the input method and passing custom options.
  2666. *
  2667. * @return void
  2668. */
  2669. public function testControlCustomization()
  2670. {
  2671. $this->getTableLocator()->get('Contacts', [
  2672. 'className' => ContactsTable::class,
  2673. ]);
  2674. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2675. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2676. $expected = [
  2677. 'div' => ['class' => 'input email'],
  2678. 'label' => ['for' => 'custom'],
  2679. 'Email',
  2680. '/label',
  2681. ['input' => [
  2682. 'type' => 'email', 'name' => 'Contact[email]',
  2683. 'id' => 'custom', 'maxlength' => 255,
  2684. ]],
  2685. '/div',
  2686. ];
  2687. $this->assertHtml($expected, $result);
  2688. $result = $this->Form->control('Contact.email', [
  2689. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2690. ]);
  2691. $expected = [
  2692. '<div',
  2693. 'label' => ['for' => 'contact-email'],
  2694. 'Email',
  2695. '/label',
  2696. ['input' => [
  2697. 'type' => 'email', 'name' => 'Contact[email]',
  2698. 'id' => 'contact-email', 'maxlength' => 255,
  2699. ]],
  2700. '/div',
  2701. ];
  2702. $this->assertHtml($expected, $result);
  2703. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2704. $expected = [
  2705. 'div' => ['class' => 'input text'],
  2706. 'label' => ['for' => 'contact-email'],
  2707. 'Email',
  2708. '/label',
  2709. ['input' => [
  2710. 'type' => 'text', 'name' => 'Contact[email]',
  2711. 'id' => 'contact-email', 'maxlength' => '255',
  2712. ]],
  2713. '/div',
  2714. ];
  2715. $this->assertHtml($expected, $result);
  2716. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2717. $expected = [
  2718. 'div' => ['class' => 'input text'],
  2719. 'label' => ['for' => 'contact-5-email'],
  2720. 'Email',
  2721. '/label',
  2722. ['input' => [
  2723. 'type' => 'text', 'name' => 'Contact[5][email]',
  2724. 'id' => 'contact-5-email', 'maxlength' => '255',
  2725. ]],
  2726. '/div',
  2727. ];
  2728. $this->assertHtml($expected, $result);
  2729. $result = $this->Form->control('Contact.password');
  2730. $expected = [
  2731. 'div' => ['class' => 'input password'],
  2732. 'label' => ['for' => 'contact-password'],
  2733. 'Password',
  2734. '/label',
  2735. ['input' => [
  2736. 'type' => 'password', 'name' => 'Contact[password]',
  2737. 'id' => 'contact-password',
  2738. ]],
  2739. '/div',
  2740. ];
  2741. $this->assertHtml($expected, $result);
  2742. $result = $this->Form->control('Contact.email', [
  2743. 'type' => 'file', 'class' => 'textbox',
  2744. ]);
  2745. $expected = [
  2746. 'div' => ['class' => 'input file'],
  2747. 'label' => ['for' => 'contact-email'],
  2748. 'Email',
  2749. '/label',
  2750. ['input' => [
  2751. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2752. 'id' => 'contact-email',
  2753. ]],
  2754. '/div',
  2755. ];
  2756. $this->assertHtml($expected, $result);
  2757. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2758. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2759. $result = $this->Form->control('phone');
  2760. $expected = [
  2761. 'div' => ['class' => 'input tel'],
  2762. 'label' => ['for' => 'phone'],
  2763. 'Phone',
  2764. '/label',
  2765. ['input' => [
  2766. 'type' => 'tel', 'name' => 'phone',
  2767. 'value' => 'Hello &amp; World &gt; weird chars',
  2768. 'id' => 'phone', 'maxlength' => 255,
  2769. ]],
  2770. '/div',
  2771. ];
  2772. $this->assertHtml($expected, $result);
  2773. $this->View->setRequest(
  2774. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2775. );
  2776. $this->Form->create();
  2777. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2778. $expected = [
  2779. 'div' => ['class' => 'input text'],
  2780. 'label' => ['for' => 'myId'],
  2781. 'Field',
  2782. '/label',
  2783. 'input' => [
  2784. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2785. 'value' => 'My value', 'id' => 'myId',
  2786. ],
  2787. '/div',
  2788. ];
  2789. $this->assertHtml($expected, $result);
  2790. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2791. $this->Form->create();
  2792. $entity->setError('field', 'Badness!');
  2793. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2794. $result = $this->Form->control('field');
  2795. $expected = [
  2796. 'div' => ['class' => 'input text error'],
  2797. 'label' => ['for' => 'field'],
  2798. 'Field',
  2799. '/label',
  2800. 'input' => [
  2801. 'type' => 'text', 'name' => 'field',
  2802. 'id' => 'field', 'class' => 'form-error',
  2803. ],
  2804. ['div' => ['class' => 'error-message']],
  2805. 'Badness!',
  2806. '/div',
  2807. '/div',
  2808. ];
  2809. $this->assertHtml($expected, $result);
  2810. $result = $this->Form->control('field', [
  2811. 'templates' => [
  2812. 'inputContainerError' => '{{content}}{{error}}',
  2813. 'error' => '<span class="error-message">{{content}}</span>',
  2814. ],
  2815. ]);
  2816. $expected = [
  2817. 'label' => ['for' => 'field'],
  2818. 'Field',
  2819. '/label',
  2820. 'input' => [
  2821. 'type' => 'text', 'name' => 'field',
  2822. 'id' => 'field', 'class' => 'form-error',
  2823. ],
  2824. ['span' => ['class' => 'error-message']],
  2825. 'Badness!',
  2826. '/span',
  2827. ];
  2828. $this->assertHtml($expected, $result);
  2829. $entity->setError('field', ['minLength'], true);
  2830. $result = $this->Form->control('field', [
  2831. 'error' => [
  2832. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2833. 'maxLength' => 'login too large',
  2834. ],
  2835. ]);
  2836. $expected = [
  2837. 'div' => ['class' => 'input text error'],
  2838. 'label' => ['for' => 'field'],
  2839. 'Field',
  2840. '/label',
  2841. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2842. ['div' => ['class' => 'error-message']],
  2843. 'Le login doit contenir au moins 2 caractères',
  2844. '/div',
  2845. '/div',
  2846. ];
  2847. $this->assertHtml($expected, $result);
  2848. $entity->setError('field', ['maxLength'], true);
  2849. $result = $this->Form->control('field', [
  2850. 'error' => [
  2851. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2852. 'maxLength' => 'login too large',
  2853. ],
  2854. ]);
  2855. $expected = [
  2856. 'div' => ['class' => 'input text error'],
  2857. 'label' => ['for' => 'field'],
  2858. 'Field',
  2859. '/label',
  2860. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2861. ['div' => ['class' => 'error-message']],
  2862. 'login too large',
  2863. '/div',
  2864. '/div',
  2865. ];
  2866. $this->assertHtml($expected, $result);
  2867. }
  2868. /**
  2869. * testControlWithTemplateFile method
  2870. *
  2871. * Test that control() accepts a template file.
  2872. *
  2873. * @return void
  2874. */
  2875. public function testControlWithTemplateFile()
  2876. {
  2877. $result = $this->Form->control('field', [
  2878. 'templates' => 'htmlhelper_tags',
  2879. ]);
  2880. $expected = [
  2881. 'label' => ['for' => 'field'],
  2882. 'Field',
  2883. '/label',
  2884. 'input' => [
  2885. 'type' => 'text', 'name' => 'field',
  2886. 'id' => 'field',
  2887. ],
  2888. ];
  2889. $this->assertHtml($expected, $result);
  2890. }
  2891. /**
  2892. * testNestedControlsEndWithBrackets method
  2893. *
  2894. * Test that nested inputs end with brackets.
  2895. *
  2896. * @return void
  2897. */
  2898. public function testNestedControlsEndWithBrackets()
  2899. {
  2900. $result = $this->Form->text('nested.text[]');
  2901. $expected = [
  2902. 'input' => [
  2903. 'type' => 'text', 'name' => 'nested[text][]',
  2904. ],
  2905. ];
  2906. $this->assertHtml($expected, $result);
  2907. $result = $this->Form->file('nested.file[]');
  2908. $expected = [
  2909. 'input' => [
  2910. 'type' => 'file', 'name' => 'nested[file][]',
  2911. ],
  2912. ];
  2913. $this->assertHtml($expected, $result);
  2914. }
  2915. /**
  2916. * testCreateIdPrefix method
  2917. *
  2918. * Test id prefix.
  2919. *
  2920. * @return void
  2921. */
  2922. public function testCreateIdPrefix()
  2923. {
  2924. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2925. $result = $this->Form->control('field');
  2926. $expected = [
  2927. 'div' => ['class' => 'input text'],
  2928. 'label' => ['for' => 'prefix-field'],
  2929. 'Field',
  2930. '/label',
  2931. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2932. '/div',
  2933. ];
  2934. $this->assertHtml($expected, $result);
  2935. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2936. $expected = [
  2937. 'div' => ['class' => 'input text'],
  2938. 'label' => ['for' => 'custom-id'],
  2939. 'Field',
  2940. '/label',
  2941. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2942. '/div',
  2943. ];
  2944. $this->assertHtml($expected, $result);
  2945. $result = $this->Form->radio('Model.field', ['option A']);
  2946. $expected = [
  2947. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2948. 'label' => ['for' => 'prefix-model-field-0'],
  2949. ['input' => [
  2950. 'type' => 'radio',
  2951. 'name' => 'Model[field]',
  2952. 'value' => '0',
  2953. 'id' => 'prefix-model-field-0',
  2954. ]],
  2955. 'option A',
  2956. '/label',
  2957. ];
  2958. $this->assertHtml($expected, $result);
  2959. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2960. $expected = [
  2961. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2962. 'label' => ['for' => 'prefix-model-field-0'],
  2963. ['input' => [
  2964. 'type' => 'radio',
  2965. 'name' => 'Model[field]',
  2966. 'value' => '0',
  2967. 'id' => 'prefix-model-field-0',
  2968. ]],
  2969. 'option A',
  2970. '/label',
  2971. ];
  2972. $this->assertHtml($expected, $result);
  2973. $result = $this->Form->select(
  2974. 'Model.multi_field',
  2975. ['first'],
  2976. ['multiple' => 'checkbox']
  2977. );
  2978. $expected = [
  2979. 'input' => [
  2980. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  2981. ],
  2982. ['div' => ['class' => 'checkbox']],
  2983. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2984. ['input' => [
  2985. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2986. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  2987. ]],
  2988. 'first',
  2989. '/label',
  2990. '/div',
  2991. ];
  2992. $this->assertHtml($expected, $result);
  2993. $this->Form->end();
  2994. $result = $this->Form->control('field');
  2995. $expected = [
  2996. 'div' => ['class' => 'input text'],
  2997. 'label' => ['for' => 'field'],
  2998. 'Field',
  2999. '/label',
  3000. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3001. '/div',
  3002. ];
  3003. $this->assertHtml($expected, $result);
  3004. }
  3005. /**
  3006. * testControlZero method
  3007. *
  3008. * Test that inputs with 0 can be created.
  3009. *
  3010. * @return void
  3011. */
  3012. public function testControlZero()
  3013. {
  3014. $this->getTableLocator()->get('Contacts', [
  3015. 'className' => ContactsTable::class,
  3016. ]);
  3017. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3018. $result = $this->Form->control('0');
  3019. $expected = [
  3020. 'div' => ['class' => 'input text'],
  3021. 'label' => ['for' => '0'], '/label',
  3022. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3023. '/div',
  3024. ];
  3025. $this->assertHtml($expected, $result);
  3026. }
  3027. /**
  3028. * testControlCheckbox method
  3029. *
  3030. * Test control() with checkbox creation.
  3031. *
  3032. * @return void
  3033. */
  3034. public function testControlCheckbox()
  3035. {
  3036. $articles = $this->getTableLocator()->get('Articles');
  3037. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3038. $article = $articles->newEmptyEntity();
  3039. $this->Form->create($article);
  3040. $result = $this->Form->control('Articles.active');
  3041. $expected = [
  3042. 'div' => ['class' => 'input checkbox'],
  3043. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3044. 'label' => ['for' => 'articles-active'],
  3045. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3046. 'Active',
  3047. '/label',
  3048. '/div',
  3049. ];
  3050. $this->assertHtml($expected, $result);
  3051. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3052. $expected = [
  3053. 'div' => ['class' => 'input checkbox'],
  3054. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3055. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3056. '/div',
  3057. ];
  3058. $this->assertHtml($expected, $result);
  3059. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3060. $expected = [
  3061. 'div' => ['class' => 'input checkbox'],
  3062. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3063. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3064. '/div',
  3065. ];
  3066. $this->assertHtml($expected, $result);
  3067. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3068. $expected = [
  3069. 'div' => ['class' => 'input checkbox'],
  3070. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3071. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3072. '/div',
  3073. ];
  3074. $this->assertHtml($expected, $result);
  3075. $result = $this->Form->control('Articles.disabled', [
  3076. 'label' => 'Disabled',
  3077. 'type' => 'checkbox',
  3078. 'data-foo' => 'disabled',
  3079. ]);
  3080. $expected = [
  3081. 'div' => ['class' => 'input checkbox'],
  3082. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3083. 'label' => ['for' => 'articles-disabled'],
  3084. ['input' => [
  3085. 'type' => 'checkbox',
  3086. 'name' => 'Articles[disabled]',
  3087. 'value' => '1',
  3088. 'id' => 'articles-disabled',
  3089. 'data-foo' => 'disabled',
  3090. ]],
  3091. 'Disabled',
  3092. '/label',
  3093. '/div',
  3094. ];
  3095. $this->assertHtml($expected, $result);
  3096. $result = $this->Form->control('Articles.confirm', [
  3097. 'label' => 'Confirm <b>me</b>!',
  3098. 'type' => 'checkbox',
  3099. 'escape' => false,
  3100. ]);
  3101. $expected = [
  3102. 'div' => ['class' => 'input checkbox'],
  3103. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3104. 'label' => ['for' => 'articles-confirm'],
  3105. ['input' => [
  3106. 'type' => 'checkbox',
  3107. 'name' => 'Articles[confirm]',
  3108. 'value' => '1',
  3109. 'id' => 'articles-confirm',
  3110. ]],
  3111. 'Confirm <b>me</b>!',
  3112. '/label',
  3113. '/div',
  3114. ];
  3115. $this->assertHtml($expected, $result);
  3116. }
  3117. /**
  3118. * testControlHidden method
  3119. *
  3120. * Test that control() does not create wrapping div and label tag for hidden fields.
  3121. *
  3122. * @return void
  3123. */
  3124. public function testControlHidden()
  3125. {
  3126. $this->getTableLocator()->get('ValidateUsers', [
  3127. 'className' => ValidateUsersTable::class,
  3128. ]);
  3129. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3130. $result = $this->Form->control('ValidateUser.id');
  3131. $expected = [
  3132. 'input' => ['name', 'type' => 'hidden', 'id'],
  3133. ];
  3134. $this->assertHtml($expected, $result);
  3135. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3136. $expected = [
  3137. 'input' => ['name', 'type' => 'hidden', 'id'],
  3138. ];
  3139. $this->assertHtml($expected, $result);
  3140. }
  3141. /**
  3142. * testControlDatetime method
  3143. *
  3144. * Test form->control() with datetime.
  3145. *
  3146. * @return void
  3147. */
  3148. public function testControlDatetime()
  3149. {
  3150. $result = $this->Form->control('prueba', [
  3151. 'type' => 'datetime',
  3152. 'value' => new FrozenTime('2019-09-27 02:52:43'),
  3153. ]);
  3154. $expected = [
  3155. 'div' => ['class' => 'input datetime'],
  3156. 'label' => ['for' => 'prueba'],
  3157. 'Prueba',
  3158. '/label',
  3159. 'input' => [
  3160. 'name' => 'prueba',
  3161. 'id' => 'prueba',
  3162. 'type' => 'datetime-local',
  3163. 'value' => '2019-09-27T02:52:43.000',
  3164. 'step' => '1',
  3165. ],
  3166. '/div',
  3167. ];
  3168. $this->assertHtml($expected, $result);
  3169. }
  3170. /**
  3171. * testControlDatetimeIdPrefix method
  3172. *
  3173. * Test form->control() with datetime with id prefix.
  3174. *
  3175. * @return void
  3176. */
  3177. public function testControlDatetimeIdPrefix()
  3178. {
  3179. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3180. $result = $this->Form->control('prueba', [
  3181. 'type' => 'datetime',
  3182. ]);
  3183. $expected = [
  3184. 'div' => ['class' => 'input datetime'],
  3185. 'label' => ['for' => 'prefix-prueba'],
  3186. 'Prueba',
  3187. '/label',
  3188. 'input' => [
  3189. 'name' => 'prueba',
  3190. 'id' => 'prefix-prueba',
  3191. 'type' => 'datetime-local',
  3192. 'value' => '',
  3193. 'step' => '1',
  3194. ],
  3195. '/div',
  3196. ];
  3197. $this->assertHtml($expected, $result);
  3198. }
  3199. /**
  3200. * testControlDatetimeStep method
  3201. *
  3202. * Test form->control() with datetime with custom step size.
  3203. *
  3204. * @return void
  3205. */
  3206. public function testControlDatetimeStep()
  3207. {
  3208. $result = $this->Form->control('prueba', [
  3209. 'type' => 'datetime',
  3210. 'value' => new FrozenTime('2019-09-27 02:52:43'),
  3211. 'step' => '0.5',
  3212. ]);
  3213. $expected = [
  3214. 'div' => ['class' => 'input datetime'],
  3215. 'label' => ['for' => 'prueba'],
  3216. 'Prueba',
  3217. '/label',
  3218. 'input' => [
  3219. 'name' => 'prueba',
  3220. 'id' => 'prueba',
  3221. 'type' => 'datetime-local',
  3222. 'value' => '2019-09-27T02:52:43.000',
  3223. 'step' => '0.5',
  3224. ],
  3225. '/div',
  3226. ];
  3227. $this->assertHtml($expected, $result);
  3228. }
  3229. /**
  3230. * testControlCheckboxWithDisabledElements method
  3231. *
  3232. * Test generating checkboxes with disabled elements.
  3233. *
  3234. * @return void
  3235. */
  3236. public function testControlCheckboxWithDisabledElements()
  3237. {
  3238. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3239. $result = $this->Form->control('Contact.multiple', [
  3240. 'multiple' => 'checkbox',
  3241. 'disabled' => 'disabled',
  3242. 'options' => $options,
  3243. ]);
  3244. $expected = [
  3245. ['div' => ['class' => 'input select']],
  3246. ['label' => ['for' => 'contact-multiple']],
  3247. 'Multiple',
  3248. '/label',
  3249. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3250. ['div' => ['class' => 'checkbox']],
  3251. ['label' => ['for' => 'contact-multiple-1']],
  3252. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3253. 'One',
  3254. '/label',
  3255. '/div',
  3256. ['div' => ['class' => 'checkbox']],
  3257. ['label' => ['for' => 'contact-multiple-2']],
  3258. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3259. 'Two',
  3260. '/label',
  3261. '/div',
  3262. ['div' => ['class' => 'checkbox']],
  3263. ['label' => ['for' => 'contact-multiple-3']],
  3264. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3265. 'Three',
  3266. '/label',
  3267. '/div',
  3268. '/div',
  3269. ];
  3270. $this->assertHtml($expected, $result);
  3271. // make sure 50 does only disable 50, and not 50f5c0cf
  3272. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3273. $disabled = [50];
  3274. $expected = [
  3275. ['div' => ['class' => 'input select']],
  3276. ['label' => ['for' => 'contact-multiple']],
  3277. 'Multiple',
  3278. '/label',
  3279. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3280. ['div' => ['class' => 'checkbox']],
  3281. ['label' => ['for' => 'contact-multiple-50']],
  3282. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3283. 'Fifty',
  3284. '/label',
  3285. '/div',
  3286. ['div' => ['class' => 'checkbox']],
  3287. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3288. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3289. 'Stringy',
  3290. '/label',
  3291. '/div',
  3292. '/div',
  3293. ];
  3294. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3295. $this->assertHtml($expected, $result);
  3296. }
  3297. /**
  3298. * testControlWithLeadingInteger method
  3299. *
  3300. * Test input name with leading integer, ensure attributes are generated correctly.
  3301. *
  3302. * @return void
  3303. */
  3304. public function testControlWithLeadingInteger()
  3305. {
  3306. $result = $this->Form->text('0.Node.title');
  3307. $expected = [
  3308. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3309. ];
  3310. $this->assertHtml($expected, $result);
  3311. }
  3312. /**
  3313. * testControlSelectType method
  3314. *
  3315. * Test form->control() with select type inputs.
  3316. *
  3317. * @return void
  3318. */
  3319. public function testControlSelectType()
  3320. {
  3321. $result = $this->Form->control(
  3322. 'email',
  3323. [
  3324. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3325. );
  3326. $expected = [
  3327. 'div' => ['class' => 'input select'],
  3328. 'label' => ['for' => 'email'],
  3329. 'Email',
  3330. '/label',
  3331. ['select' => ['name' => 'email', 'id' => 'email']],
  3332. ['option' => ['value' => '']],
  3333. '/option',
  3334. ['option' => ['value' => 'è']],
  3335. 'Firést',
  3336. '/option',
  3337. ['option' => ['value' => 'é']],
  3338. 'Secoènd',
  3339. '/option',
  3340. '/select',
  3341. '/div',
  3342. ];
  3343. $this->assertHtml($expected, $result);
  3344. $result = $this->Form->control(
  3345. 'email',
  3346. [
  3347. 'options' => ['First', 'Second'], 'empty' => true]
  3348. );
  3349. $expected = [
  3350. 'div' => ['class' => 'input select'],
  3351. 'label' => ['for' => 'email'],
  3352. 'Email',
  3353. '/label',
  3354. ['select' => ['name' => 'email', 'id' => 'email']],
  3355. ['option' => ['value' => '']],
  3356. '/option',
  3357. ['option' => ['value' => '0']],
  3358. 'First',
  3359. '/option',
  3360. ['option' => ['value' => '1']],
  3361. 'Second',
  3362. '/option',
  3363. '/select',
  3364. '/div',
  3365. ];
  3366. $this->assertHtml($expected, $result);
  3367. $result = $this->Form->control('email', [
  3368. 'type' => 'select',
  3369. 'options' => new \ArrayObject(['First', 'Second']),
  3370. 'empty' => true,
  3371. ]);
  3372. $this->assertHtml($expected, $result);
  3373. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3374. $this->View->setRequest(
  3375. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3376. );
  3377. $this->Form->create();
  3378. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3379. $expected = [
  3380. 'div' => ['class' => 'input select'],
  3381. 'label' => ['for' => 'model-user-id'],
  3382. 'User',
  3383. '/label',
  3384. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3385. ['option' => ['value' => '']],
  3386. '/option',
  3387. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3388. 'good',
  3389. '/option',
  3390. ['option' => ['value' => 'other']],
  3391. 'bad',
  3392. '/option',
  3393. '/select',
  3394. '/div',
  3395. ];
  3396. $this->assertHtml($expected, $result);
  3397. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3398. $this->View->setRequest(
  3399. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3400. );
  3401. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3402. $expected = [
  3403. 'div' => ['class' => 'input select'],
  3404. 'label' => ['for' => 'thing-user-id'],
  3405. 'User',
  3406. '/label',
  3407. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3408. ['option' => ['value' => '']],
  3409. 'Some Empty',
  3410. '/option',
  3411. ['option' => ['value' => 'value']],
  3412. 'good',
  3413. '/option',
  3414. ['option' => ['value' => 'other']],
  3415. 'bad',
  3416. '/option',
  3417. '/select',
  3418. '/div',
  3419. ];
  3420. $this->assertHtml($expected, $result);
  3421. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3422. $this->View->setRequest(
  3423. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3424. );
  3425. $this->Form->create();
  3426. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3427. $expected = [
  3428. 'div' => ['class' => 'input select'],
  3429. 'label' => ['for' => 'thing-user-id'],
  3430. 'User',
  3431. '/label',
  3432. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3433. ['option' => ['value' => '']],
  3434. 'Some Empty',
  3435. '/option',
  3436. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3437. 'good',
  3438. '/option',
  3439. ['option' => ['value' => 'other']],
  3440. 'bad',
  3441. '/option',
  3442. '/select',
  3443. '/div',
  3444. ];
  3445. $this->assertHtml($expected, $result);
  3446. $result = $this->Form->control('Publisher.id', [
  3447. 'label' => 'Publisher',
  3448. 'type' => 'select',
  3449. 'multiple' => 'checkbox',
  3450. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3451. ]);
  3452. $expected = [
  3453. ['div' => ['class' => 'input select']],
  3454. ['label' => ['for' => 'publisher-id']],
  3455. 'Publisher',
  3456. '/label',
  3457. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3458. ['div' => ['class' => 'checkbox']],
  3459. ['label' => ['for' => 'publisher-id-value-1']],
  3460. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3461. 'Label 1',
  3462. '/label',
  3463. '/div',
  3464. ['div' => ['class' => 'checkbox']],
  3465. ['label' => ['for' => 'publisher-id-value-2']],
  3466. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3467. 'Label 2',
  3468. '/label',
  3469. '/div',
  3470. '/div',
  3471. ];
  3472. $this->assertHtml($expected, $result);
  3473. }
  3474. /**
  3475. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3476. *
  3477. * Test that control() and a non standard primary key makes a hidden input by default.
  3478. *
  3479. * @return void
  3480. */
  3481. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3482. {
  3483. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3484. $this->Form->create($this->article);
  3485. $result = $this->Form->control('title');
  3486. $expected = [
  3487. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3488. ];
  3489. $this->assertHtml($expected, $result);
  3490. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3491. $this->Form->create($this->article);
  3492. $result = $this->Form->control('title');
  3493. $expected = [
  3494. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3495. ];
  3496. $this->assertHtml($expected, $result);
  3497. $result = $this->Form->control('body');
  3498. $expected = [
  3499. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3500. ];
  3501. $this->assertHtml($expected, $result);
  3502. }
  3503. /**
  3504. * testControlOverridingMagicSelectType method
  3505. *
  3506. * Test that overriding the magic select type widget is possible.
  3507. *
  3508. * @return void
  3509. */
  3510. public function testControlOverridingMagicSelectType()
  3511. {
  3512. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3513. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3514. $expected = [
  3515. 'div' => ['class' => 'input text'],
  3516. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3517. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3518. '/div',
  3519. ];
  3520. $this->assertHtml($expected, $result);
  3521. //Check that magic types still work for plural/singular vars
  3522. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3523. $result = $this->Form->control('Model.type');
  3524. $expected = [
  3525. 'div' => ['class' => 'input select'],
  3526. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3527. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3528. ['option' => ['value' => 'value']], 'good', '/option',
  3529. ['option' => ['value' => 'other']], 'bad', '/option',
  3530. '/select',
  3531. '/div',
  3532. ];
  3533. $this->assertHtml($expected, $result);
  3534. }
  3535. /**
  3536. * testControlMagicTypeDoesNotOverride method
  3537. *
  3538. * Test that inferred types do not override developer input.
  3539. *
  3540. * @return void
  3541. */
  3542. public function testControlMagicTypeDoesNotOverride()
  3543. {
  3544. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3545. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3546. $expected = [
  3547. 'div' => ['class' => 'input checkbox'],
  3548. ['input' => [
  3549. 'type' => 'hidden',
  3550. 'name' => 'Model[user]',
  3551. 'value' => 0,
  3552. ]],
  3553. 'label' => ['for' => 'model-user'],
  3554. ['input' => [
  3555. 'name' => 'Model[user]',
  3556. 'type' => 'checkbox',
  3557. 'id' => 'model-user',
  3558. 'value' => 1,
  3559. ]],
  3560. 'User',
  3561. '/label',
  3562. '/div',
  3563. ];
  3564. $this->assertHtml($expected, $result);
  3565. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3566. $options = [
  3567. 1 => 'blue',
  3568. 2 => 'red',
  3569. ];
  3570. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3571. $expected = [
  3572. 'div' => ['class' => 'input select'],
  3573. 'label' => ['for' => 'tags-ids'],
  3574. 'Tags',
  3575. '/label',
  3576. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3577. ['div' => ['class' => 'checkbox']],
  3578. ['label' => ['for' => 'tags-ids-1']],
  3579. ['input' => [
  3580. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3581. 'value' => '1', 'name' => 'tags[_ids][]',
  3582. ]],
  3583. 'blue',
  3584. '/label',
  3585. '/div',
  3586. ['div' => ['class' => 'checkbox']],
  3587. ['label' => ['for' => 'tags-ids-2']],
  3588. ['input' => [
  3589. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3590. 'value' => '2', 'name' => 'tags[_ids][]',
  3591. ]],
  3592. 'red',
  3593. '/label',
  3594. '/div',
  3595. '/div',
  3596. ];
  3597. $this->assertHtml($expected, $result);
  3598. }
  3599. /**
  3600. * testControlMagicSelectForTypeNumber method
  3601. *
  3602. * Test that magic control() selects are created for type=number.
  3603. *
  3604. * @return void
  3605. */
  3606. public function testControlMagicSelectForTypeNumber()
  3607. {
  3608. $this->getTableLocator()->get('ValidateUsers', [
  3609. 'className' => ValidateUsersTable::class,
  3610. ]);
  3611. $entity = new Entity(['balance' => 1]);
  3612. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3613. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3614. $result = $this->Form->control('balance');
  3615. $expected = [
  3616. 'div' => ['class' => 'input select'],
  3617. 'label' => ['for' => 'balance'],
  3618. 'Balance',
  3619. '/label',
  3620. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3621. ['option' => ['value' => '0']],
  3622. 'nothing',
  3623. '/option',
  3624. ['option' => ['value' => '1', 'selected' => 'selected']],
  3625. 'some',
  3626. '/option',
  3627. ['option' => ['value' => '100']],
  3628. 'a lot',
  3629. '/option',
  3630. '/select',
  3631. '/div',
  3632. ];
  3633. $this->assertHtml($expected, $result);
  3634. }
  3635. /**
  3636. * testInvalidControlTypeOption method
  3637. *
  3638. * Test invalid 'input' type option to control() function.
  3639. *
  3640. * @return void
  3641. */
  3642. public function testInvalidControlTypeOption()
  3643. {
  3644. $this->expectException(\RuntimeException::class);
  3645. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3646. $this->Form->control('text', ['type' => 'input']);
  3647. }
  3648. /**
  3649. * testControlMagicSelectChangeToRadio method
  3650. *
  3651. * Test that magic control() selects can easily be converted into radio types without error.
  3652. *
  3653. * @return void
  3654. */
  3655. public function testControlMagicSelectChangeToRadio()
  3656. {
  3657. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3658. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3659. $this->assertStringContainsString('input type="radio"', $result);
  3660. }
  3661. /**
  3662. * testFormControlSubmit method
  3663. *
  3664. * Test correct results for form::control() and type submit.
  3665. *
  3666. * @return void
  3667. */
  3668. public function testFormControlSubmit()
  3669. {
  3670. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3671. $expected = [
  3672. 'div' => ['class' => 'submit'],
  3673. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3674. '/div',
  3675. ];
  3676. $this->assertHtml($expected, $result);
  3677. }
  3678. /**
  3679. * testFormControls method
  3680. *
  3681. * Test correct results from Form::controls().
  3682. *
  3683. * @return void
  3684. */
  3685. public function testFormControlsLegendFieldset()
  3686. {
  3687. $this->Form->create($this->article);
  3688. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3689. $expected = [
  3690. '<fieldset',
  3691. '<legend',
  3692. 'The Legend',
  3693. '/legend',
  3694. '*/fieldset',
  3695. ];
  3696. $this->assertHtml($expected, $result);
  3697. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3698. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3699. $this->assertStringContainsString('<fieldset>', $result);
  3700. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3701. $this->assertStringNotContainsString('<legend>', $result);
  3702. $this->assertStringNotContainsString('<fieldset>', $result);
  3703. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3704. $this->assertStringNotContainsString('<legend>', $result);
  3705. $this->assertStringNotContainsString('<fieldset>', $result);
  3706. $this->Form->create($this->article);
  3707. $this->View->setRequest($this->View->getRequest()
  3708. ->withParam('prefix', 'admin')
  3709. ->withParam('action', 'admin_edit')
  3710. ->withParam('controller', 'articles'));
  3711. $result = $this->Form->allControls();
  3712. $expected = [
  3713. '<fieldset',
  3714. '<legend',
  3715. 'New Article',
  3716. '/legend',
  3717. '*/fieldset',
  3718. ];
  3719. $this->assertHtml($expected, $result);
  3720. $this->Form->create($this->article);
  3721. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3722. $expected = [
  3723. '<fieldset',
  3724. '<legend',
  3725. 'The Legend',
  3726. '/legend',
  3727. '*/fieldset',
  3728. ];
  3729. $this->assertHtml($expected, $result);
  3730. $this->Form->create($this->article);
  3731. $result = $this->Form->allControls([], [
  3732. 'fieldset' => [
  3733. 'class' => 'some-class some-other-class',
  3734. 'disabled' => true,
  3735. 'data-param' => 'a-param',
  3736. ],
  3737. 'legend' => 'The Legend',
  3738. ]);
  3739. $expected = [
  3740. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3741. '<legend',
  3742. 'The Legend',
  3743. '/legend',
  3744. '*/fieldset',
  3745. ];
  3746. $this->assertHtml($expected, $result);
  3747. }
  3748. /**
  3749. * testFormControls method
  3750. *
  3751. * Test the controls() method.
  3752. *
  3753. * @return void
  3754. */
  3755. public function testFormControls()
  3756. {
  3757. $this->Form->create($this->article);
  3758. $result = $this->Form->allControls();
  3759. $expected = [
  3760. '<fieldset',
  3761. '<legend', 'New Article', '/legend',
  3762. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3763. ['div' => ['class' => 'input select required']],
  3764. '*/div',
  3765. ['div' => ['class' => 'input text required']],
  3766. '*/div',
  3767. ['div' => ['class' => 'input text']],
  3768. '*/div',
  3769. ['div' => ['class' => 'input text']],
  3770. '*/div',
  3771. '/fieldset',
  3772. ];
  3773. $this->assertHtml($expected, $result);
  3774. $result = $this->Form->allControls([
  3775. 'published' => ['type' => 'boolean'],
  3776. ]);
  3777. $expected = [
  3778. '<fieldset',
  3779. '<legend', 'New Article', '/legend',
  3780. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3781. ['div' => ['class' => 'input select required']],
  3782. '*/div',
  3783. ['div' => ['class' => 'input text required']],
  3784. '*/div',
  3785. ['div' => ['class' => 'input text']],
  3786. '*/div',
  3787. ['div' => ['class' => 'input boolean']],
  3788. '*/div',
  3789. '/fieldset',
  3790. ];
  3791. $this->assertHtml($expected, $result);
  3792. $this->Form->create($this->article);
  3793. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3794. $expected = [
  3795. 'fieldset' => [],
  3796. 'legend' => [],
  3797. 'Hello',
  3798. '/legend',
  3799. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3800. ['div' => ['class' => 'input select required']],
  3801. '*/div',
  3802. ['div' => ['class' => 'input text required']],
  3803. '*/div',
  3804. ['div' => ['class' => 'input text']],
  3805. '*/div',
  3806. ['div' => ['class' => 'input text']],
  3807. '*/div',
  3808. '/fieldset',
  3809. ];
  3810. $this->assertHtml($expected, $result);
  3811. $this->Form->create();
  3812. $expected = [
  3813. 'fieldset' => [],
  3814. ['div' => ['class' => 'input text']],
  3815. 'label' => ['for' => 'foo'],
  3816. 'Foo',
  3817. '/label',
  3818. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3819. '*/div',
  3820. '/fieldset',
  3821. ];
  3822. $result = $this->Form->allControls(
  3823. ['foo' => ['type' => 'text']],
  3824. ['legend' => false]
  3825. );
  3826. $this->assertHtml($expected, $result);
  3827. }
  3828. /**
  3829. * testFormControlsBlacklist method
  3830. *
  3831. * @return void
  3832. */
  3833. public function testFormControlsBlacklist()
  3834. {
  3835. $this->Form->create($this->article);
  3836. $result = $this->Form->allControls([
  3837. 'id' => false,
  3838. ]);
  3839. $expected = [
  3840. '<fieldset',
  3841. '<legend', 'New Article', '/legend',
  3842. ['div' => ['class' => 'input select required']],
  3843. '*/div',
  3844. ['div' => ['class' => 'input text required']],
  3845. '*/div',
  3846. ['div' => ['class' => 'input text']],
  3847. '*/div',
  3848. ['div' => ['class' => 'input text']],
  3849. '*/div',
  3850. '/fieldset',
  3851. ];
  3852. $this->assertHtml($expected, $result);
  3853. $this->Form->create($this->article);
  3854. $result = $this->Form->allControls([
  3855. 'id' => [],
  3856. ]);
  3857. $expected = [
  3858. '<fieldset',
  3859. '<legend', 'New Article', '/legend',
  3860. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3861. ['div' => ['class' => 'input select required']],
  3862. '*/div',
  3863. ['div' => ['class' => 'input text required']],
  3864. '*/div',
  3865. ['div' => ['class' => 'input text']],
  3866. '*/div',
  3867. ['div' => ['class' => 'input text']],
  3868. '*/div',
  3869. '/fieldset',
  3870. ];
  3871. $this->assertHtml($expected, $result, true);
  3872. }
  3873. /**
  3874. * testSelectAsCheckbox method
  3875. *
  3876. * Test multi-select widget with checkbox formatting.
  3877. *
  3878. * @return void
  3879. */
  3880. public function testSelectAsCheckbox()
  3881. {
  3882. $result = $this->Form->select(
  3883. 'Model.multi_field',
  3884. ['first', 'second', 'third'],
  3885. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3886. );
  3887. $expected = [
  3888. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3889. ['div' => ['class' => 'checkbox']],
  3890. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3891. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3892. 'first',
  3893. '/label',
  3894. '/div',
  3895. ['div' => ['class' => 'checkbox']],
  3896. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3897. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3898. 'second',
  3899. '/label',
  3900. '/div',
  3901. ['div' => ['class' => 'checkbox']],
  3902. ['label' => ['for' => 'model-multi-field-2']],
  3903. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3904. 'third',
  3905. '/label',
  3906. '/div',
  3907. ];
  3908. $this->assertHtml($expected, $result);
  3909. $result = $this->Form->select(
  3910. 'Model.multi_field',
  3911. ['1/2' => 'half'],
  3912. ['multiple' => 'checkbox']
  3913. );
  3914. $expected = [
  3915. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3916. ['div' => ['class' => 'checkbox']],
  3917. ['label' => ['for' => 'model-multi-field-1-2']],
  3918. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3919. 'half',
  3920. '/label',
  3921. '/div',
  3922. ];
  3923. $this->assertHtml($expected, $result);
  3924. }
  3925. /**
  3926. * testLabel method
  3927. *
  3928. * Test label generation.
  3929. *
  3930. * @return void
  3931. */
  3932. public function testLabel()
  3933. {
  3934. $result = $this->Form->label('Person.name');
  3935. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3936. $this->assertHtml($expected, $result);
  3937. $result = $this->Form->label('Person.name');
  3938. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3939. $this->assertHtml($expected, $result);
  3940. $result = $this->Form->label('Person.first_name');
  3941. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3942. $this->assertHtml($expected, $result);
  3943. $result = $this->Form->label('Person.first_name', 'Your first name');
  3944. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3945. $this->assertHtml($expected, $result);
  3946. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3947. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3948. $this->assertHtml($expected, $result);
  3949. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3950. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3951. $this->assertHtml($expected, $result);
  3952. $result = $this->Form->label('Person.first_name', '');
  3953. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3954. $this->assertHtml($expected, $result);
  3955. $result = $this->Form->label('Person.2.name', '');
  3956. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3957. $this->assertHtml($expected, $result);
  3958. }
  3959. /**
  3960. * testLabelContainControl method
  3961. *
  3962. * Test that label() can accept an input with the correct template vars.
  3963. *
  3964. * @return void
  3965. */
  3966. public function testLabelContainControl()
  3967. {
  3968. $this->Form->setTemplates([
  3969. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3970. ]);
  3971. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3972. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3973. ]);
  3974. $expected = [
  3975. 'label' => ['for' => 'person-accept-terms'],
  3976. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3977. 'Accept',
  3978. '/label',
  3979. ];
  3980. $this->assertHtml($expected, $result);
  3981. }
  3982. /**
  3983. * testTextbox method
  3984. *
  3985. * Test textbox element generation.
  3986. *
  3987. * @return void
  3988. */
  3989. public function testTextbox()
  3990. {
  3991. $result = $this->Form->text('Model.field');
  3992. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3993. $this->assertHtml($expected, $result);
  3994. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3995. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3996. $this->assertHtml($expected, $result);
  3997. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3998. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3999. $this->assertHtml($expected, $result);
  4000. }
  4001. /**
  4002. * testTextBoxDataAndError method
  4003. *
  4004. * Test that text() hooks up with request data and error fields.
  4005. *
  4006. * @return void
  4007. */
  4008. public function testTextBoxDataAndError()
  4009. {
  4010. $this->article['errors'] = [
  4011. 'Contact' => ['text' => 'wrong'],
  4012. ];
  4013. $this->View->setRequest($this->View->getRequest()
  4014. ->withData('Model.text', 'test <strong>HTML</strong> values')
  4015. ->withData('Contact.text', 'test'));
  4016. $this->Form->create($this->article);
  4017. $result = $this->Form->text('Model.text');
  4018. $expected = [
  4019. 'input' => [
  4020. 'type' => 'text',
  4021. 'name' => 'Model[text]',
  4022. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4023. ],
  4024. ];
  4025. $this->assertHtml($expected, $result);
  4026. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4027. $expected = [
  4028. 'input' => [
  4029. 'type' => 'text',
  4030. 'name' => 'Contact[text]',
  4031. 'value' => 'test',
  4032. 'id' => 'theID',
  4033. 'class' => 'form-error',
  4034. ],
  4035. ];
  4036. $this->assertHtml($expected, $result);
  4037. }
  4038. /**
  4039. * testDefaultValue method
  4040. *
  4041. * Test default value setting.
  4042. *
  4043. * @return void
  4044. */
  4045. public function testTextDefaultValue()
  4046. {
  4047. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  4048. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4049. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4050. $this->assertHtml($expected, $result);
  4051. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4052. $this->Form->create();
  4053. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4054. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4055. $this->assertHtml($expected, $result);
  4056. $Articles = $this->getTableLocator()->get('Articles');
  4057. $title = $Articles->getSchema()->getColumn('title');
  4058. $Articles->getSchema()->addColumn(
  4059. 'title',
  4060. ['default' => 'default title', 'length' => 255] + $title
  4061. );
  4062. $entity = $Articles->newEmptyEntity();
  4063. $this->Form->create($entity);
  4064. // Get default value from schema
  4065. $result = $this->Form->text('title');
  4066. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title', 'maxlength' => '255']];
  4067. $this->assertHtml($expected, $result);
  4068. // Don't get value from schema
  4069. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4070. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  4071. $this->assertHtml($expected, $result);
  4072. // Custom default value overrides default value from schema
  4073. $result = $this->Form->text('title', ['default' => 'override default']);
  4074. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default', 'maxlength' => '255']];
  4075. $this->assertHtml($expected, $result);
  4076. // Default value from schema is used only for new entities.
  4077. $entity->setNew(false);
  4078. $result = $this->Form->text('title');
  4079. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'maxlength' => '255']];
  4080. $this->assertHtml($expected, $result);
  4081. }
  4082. /**
  4083. * testError method
  4084. *
  4085. * Test field error generation.
  4086. *
  4087. * @return void
  4088. */
  4089. public function testError()
  4090. {
  4091. $this->article['errors'] = [
  4092. 'Article' => ['field' => 'email'],
  4093. ];
  4094. $this->Form->create($this->article);
  4095. $result = $this->Form->error('Article.field');
  4096. $expected = [
  4097. ['div' => ['class' => 'error-message']],
  4098. 'email',
  4099. '/div',
  4100. ];
  4101. $this->assertHtml($expected, $result);
  4102. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4103. $expected = [
  4104. ['div' => ['class' => 'error-message']],
  4105. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4106. '/div',
  4107. ];
  4108. $this->assertHtml($expected, $result);
  4109. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4110. $expected = [
  4111. ['div' => ['class' => 'error-message']],
  4112. '<strong', 'Badness!', '/strong',
  4113. '/div',
  4114. ];
  4115. $this->assertHtml($expected, $result);
  4116. }
  4117. /**
  4118. * testErrorRuleName method
  4119. *
  4120. * Test error translation can use rule names for translating.
  4121. *
  4122. * @return void
  4123. */
  4124. public function testErrorRuleName()
  4125. {
  4126. $this->article['errors'] = [
  4127. 'Article' => [
  4128. 'field' => ['email' => 'Your email was not good'],
  4129. ],
  4130. ];
  4131. $this->Form->create($this->article);
  4132. $result = $this->Form->error('Article.field');
  4133. $expected = [
  4134. ['div' => ['class' => 'error-message']],
  4135. 'Your email was not good',
  4136. '/div',
  4137. ];
  4138. $this->assertHtml($expected, $result);
  4139. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4140. $expected = [
  4141. ['div' => ['class' => 'error-message']],
  4142. 'Email in use',
  4143. '/div',
  4144. ];
  4145. $this->assertHtml($expected, $result);
  4146. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4147. $expected = [
  4148. ['div' => ['class' => 'error-message']],
  4149. 'Email in use',
  4150. '/div',
  4151. ];
  4152. $this->assertHtml($expected, $result);
  4153. $result = $this->Form->error('Article.field', [
  4154. 'email' => 'Key is preferred',
  4155. 'Your email was not good' => 'Email in use',
  4156. ]);
  4157. $expected = [
  4158. ['div' => ['class' => 'error-message']],
  4159. 'Key is preferred',
  4160. '/div',
  4161. ];
  4162. $this->assertHtml($expected, $result);
  4163. }
  4164. /**
  4165. * testErrorMessages method
  4166. *
  4167. * Test error with nested lists.
  4168. *
  4169. * @return void
  4170. */
  4171. public function testErrorMessages()
  4172. {
  4173. $this->article['errors'] = [
  4174. 'Article' => ['field' => 'email'],
  4175. ];
  4176. $this->Form->create($this->article);
  4177. $result = $this->Form->error('Article.field', [
  4178. 'email' => 'No good!',
  4179. ]);
  4180. $expected = [
  4181. 'div' => ['class' => 'error-message'],
  4182. 'No good!',
  4183. '/div',
  4184. ];
  4185. $this->assertHtml($expected, $result);
  4186. }
  4187. /**
  4188. * testErrorMultipleMessages method
  4189. *
  4190. * Test error() with multiple messages.
  4191. *
  4192. * @return void
  4193. */
  4194. public function testErrorMultipleMessages()
  4195. {
  4196. $this->article['errors'] = [
  4197. 'field' => ['notBlank', 'email', 'Something else'],
  4198. ];
  4199. $this->Form->create($this->article);
  4200. $result = $this->Form->error('field', [
  4201. 'notBlank' => 'Cannot be empty',
  4202. 'email' => 'No good!',
  4203. ]);
  4204. $expected = [
  4205. 'div' => ['class' => 'error-message'],
  4206. 'ul' => [],
  4207. '<li', 'Cannot be empty', '/li',
  4208. '<li', 'No good!', '/li',
  4209. '<li', 'Something else', '/li',
  4210. '/ul',
  4211. '/div',
  4212. ];
  4213. $this->assertHtml($expected, $result);
  4214. }
  4215. /**
  4216. * testPassword method
  4217. *
  4218. * Test password element generation.
  4219. *
  4220. * @return void
  4221. */
  4222. public function testPassword()
  4223. {
  4224. $this->article['errors'] = [
  4225. 'Contact' => [
  4226. 'passwd' => 1,
  4227. ],
  4228. ];
  4229. $this->Form->create($this->article);
  4230. $result = $this->Form->password('Contact.field');
  4231. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4232. $this->assertHtml($expected, $result);
  4233. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4234. $this->Form->create($this->article);
  4235. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4236. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4237. $this->assertHtml($expected, $result);
  4238. }
  4239. /**
  4240. * testRadio method
  4241. *
  4242. * Test radio element set generation.
  4243. *
  4244. * @return void
  4245. */
  4246. public function testRadio()
  4247. {
  4248. $result = $this->Form->radio('Model.field', ['option A']);
  4249. $expected = [
  4250. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4251. 'label' => ['for' => 'model-field-0'],
  4252. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4253. 'option A',
  4254. '/label',
  4255. ];
  4256. $this->assertHtml($expected, $result);
  4257. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4258. $this->assertHtml($expected, $result);
  4259. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4260. $expected = [
  4261. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4262. ['label' => ['for' => 'model-field-0']],
  4263. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4264. 'option A',
  4265. '/label',
  4266. ['label' => ['for' => 'model-field-1']],
  4267. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4268. 'option B',
  4269. '/label',
  4270. ];
  4271. $this->assertHtml($expected, $result);
  4272. $result = $this->Form->radio(
  4273. 'Employee.gender',
  4274. ['male' => 'Male', 'female' => 'Female'],
  4275. ['form' => 'my-form']
  4276. );
  4277. $expected = [
  4278. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4279. ['label' => ['for' => 'employee-gender-male']],
  4280. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4281. 'Male',
  4282. '/label',
  4283. ['label' => ['for' => 'employee-gender-female']],
  4284. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4285. 'Female',
  4286. '/label',
  4287. ];
  4288. $this->assertHtml($expected, $result);
  4289. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4290. $expected = [
  4291. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4292. ['label' => ['for' => 'model-custom-0']],
  4293. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4294. 'option A',
  4295. '/label',
  4296. ['label' => ['for' => 'model-custom-1']],
  4297. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4298. 'option B',
  4299. '/label',
  4300. ];
  4301. $this->assertHtml($expected, $result);
  4302. $result = $this->Form->radio(
  4303. 'Employee.gender',
  4304. [
  4305. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4306. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4307. ]
  4308. );
  4309. $expected = [
  4310. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4311. ['label' => ['for' => 'employee-gender-male']],
  4312. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4313. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4314. 'Male',
  4315. '/label',
  4316. ['label' => ['for' => 'employee-gender-female']],
  4317. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4318. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4319. 'Female',
  4320. '/label',
  4321. ];
  4322. $this->assertHtml($expected, $result);
  4323. }
  4324. /**
  4325. * Test radio with complex options and empty disabled data.
  4326. *
  4327. * @return void
  4328. */
  4329. public function testRadioComplexDisabled()
  4330. {
  4331. $options = [
  4332. ['value' => 'r', 'text' => 'red'],
  4333. ['value' => 'b', 'text' => 'blue'],
  4334. ];
  4335. $attrs = ['disabled' => []];
  4336. $result = $this->Form->radio('Model.field', $options, $attrs);
  4337. $expected = [
  4338. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4339. ['label' => ['for' => 'model-field-r']],
  4340. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4341. 'red',
  4342. '/label',
  4343. ['label' => ['for' => 'model-field-b']],
  4344. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4345. 'blue',
  4346. '/label',
  4347. ];
  4348. $this->assertHtml($expected, $result);
  4349. $attrs = ['disabled' => ['r']];
  4350. $result = $this->Form->radio('Model.field', $options, $attrs);
  4351. $this->assertStringContainsString('disabled="disabled"', $result);
  4352. }
  4353. /**
  4354. * testRadioDefaultValue method
  4355. *
  4356. * Test default value setting on radio() method.
  4357. *
  4358. * @return void
  4359. */
  4360. public function testRadioDefaultValue()
  4361. {
  4362. $Articles = $this->getTableLocator()->get('Articles');
  4363. $title = $Articles->getSchema()->getColumn('title');
  4364. $Articles->getSchema()->addColumn(
  4365. 'title',
  4366. ['default' => '1'] + $title
  4367. );
  4368. $this->Form->create($Articles->newEmptyEntity());
  4369. $result = $this->Form->radio('title', ['option A', 'option B']);
  4370. $expected = [
  4371. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4372. ['label' => ['for' => 'title-0']],
  4373. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4374. 'option A',
  4375. '/label',
  4376. ['label' => ['for' => 'title-1']],
  4377. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4378. 'option B',
  4379. '/label',
  4380. ];
  4381. $this->assertHtml($expected, $result);
  4382. }
  4383. /**
  4384. * Test setting a hiddenField value on radio buttons.
  4385. *
  4386. * @return void
  4387. */
  4388. public function testRadioHiddenFieldValue()
  4389. {
  4390. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4391. $expected = [
  4392. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4393. 'label' => ['for' => 'title-0'],
  4394. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4395. 'option A',
  4396. '/label',
  4397. ];
  4398. $this->assertHtml($expected, $result);
  4399. }
  4400. /**
  4401. * testControlRadio method
  4402. *
  4403. * Test that input works with radio types.
  4404. *
  4405. * @return void
  4406. */
  4407. public function testControlRadio()
  4408. {
  4409. $result = $this->Form->control('test', [
  4410. 'type' => 'radio',
  4411. 'options' => ['A', 'B'],
  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', '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. 'value' => '0',
  4434. ]);
  4435. $expected = [
  4436. ['div' => ['class' => 'input radio']],
  4437. '<label',
  4438. 'Test',
  4439. '/label',
  4440. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4441. ['label' => ['for' => 'test-0']],
  4442. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4443. 'A',
  4444. '/label',
  4445. ['label' => ['for' => 'test-1']],
  4446. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4447. 'B',
  4448. '/label',
  4449. '/div',
  4450. ];
  4451. $this->assertHtml($expected, $result);
  4452. $result = $this->Form->control('test', [
  4453. 'type' => 'radio',
  4454. 'options' => ['A', 'B'],
  4455. 'label' => false,
  4456. ]);
  4457. $expected = [
  4458. ['div' => ['class' => 'input radio']],
  4459. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4460. ['label' => ['for' => 'test-0']],
  4461. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4462. 'A',
  4463. '/label',
  4464. ['label' => ['for' => 'test-1']],
  4465. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4466. 'B',
  4467. '/label',
  4468. '/div',
  4469. ];
  4470. $this->assertHtml($expected, $result);
  4471. }
  4472. /**
  4473. * testRadioNoLabel method
  4474. *
  4475. * Test that radio() works with label = false.
  4476. *
  4477. * @return void
  4478. */
  4479. public function testRadioNoLabel()
  4480. {
  4481. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4482. $expected = [
  4483. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4484. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4485. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4486. ];
  4487. $this->assertHtml($expected, $result);
  4488. }
  4489. /**
  4490. * testRadioControlInsideLabel method
  4491. *
  4492. * Test generating radio input inside label ala twitter bootstrap.
  4493. *
  4494. * @return void
  4495. */
  4496. public function testRadioControlInsideLabel()
  4497. {
  4498. $this->Form->setTemplates([
  4499. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4500. 'radioWrapper' => '{{label}}',
  4501. ]);
  4502. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4503. // phpcs:disable
  4504. $expected = [
  4505. ['input' => [
  4506. 'type' => 'hidden',
  4507. 'name' => 'Model[field]',
  4508. 'value' => ''
  4509. ]],
  4510. ['label' => ['for' => 'model-field-0']],
  4511. ['input' => [
  4512. 'type' => 'radio',
  4513. 'name' => 'Model[field]',
  4514. 'value' => '0',
  4515. 'id' => 'model-field-0'
  4516. ]],
  4517. 'option A',
  4518. '/label',
  4519. ['label' => ['for' => 'model-field-1']],
  4520. ['input' => [
  4521. 'type' => 'radio',
  4522. 'name' => 'Model[field]',
  4523. 'value' => '1',
  4524. 'id' => 'model-field-1'
  4525. ]],
  4526. 'option B',
  4527. '/label',
  4528. ];
  4529. // phpcs:enable
  4530. $this->assertHtml($expected, $result);
  4531. }
  4532. /**
  4533. * testRadioHiddenControlDisabling method
  4534. *
  4535. * Test disabling the hidden input for radio buttons.
  4536. *
  4537. * @return void
  4538. */
  4539. public function testRadioHiddenControlDisabling()
  4540. {
  4541. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4542. $expected = [
  4543. 'label' => ['for' => 'model-1-field-0'],
  4544. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4545. 'option A',
  4546. '/label',
  4547. ];
  4548. $this->assertHtml($expected, $result);
  4549. }
  4550. /**
  4551. * testRadioOutOfRange method
  4552. *
  4553. * Test radio element set generation.
  4554. *
  4555. * @return void
  4556. */
  4557. public function testRadioOutOfRange()
  4558. {
  4559. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4560. $expected = [
  4561. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4562. 'label' => ['for' => 'model-field-v'],
  4563. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4564. 'value',
  4565. '/label',
  4566. ];
  4567. $this->assertHtml($expected, $result);
  4568. }
  4569. /**
  4570. * testSelect method
  4571. *
  4572. * Test select element generation.
  4573. *
  4574. * @return void
  4575. */
  4576. public function testSelect()
  4577. {
  4578. $result = $this->Form->select('Model.field', []);
  4579. $expected = [
  4580. 'select' => ['name' => 'Model[field]'],
  4581. '/select',
  4582. ];
  4583. $this->assertHtml($expected, $result);
  4584. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4585. $this->Form->create();
  4586. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4587. $expected = [
  4588. 'select' => ['name' => 'Model[field]'],
  4589. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4590. 'good',
  4591. '/option',
  4592. ['option' => ['value' => 'other']],
  4593. 'bad',
  4594. '/option',
  4595. '/select',
  4596. ];
  4597. $this->assertHtml($expected, $result);
  4598. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4599. $this->assertHtml($expected, $result);
  4600. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4601. $this->Form->create();
  4602. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4603. $expected = [
  4604. 'select' => ['name' => 'Model[field]'],
  4605. ['option' => ['value' => 'value']],
  4606. 'good',
  4607. '/option',
  4608. ['option' => ['value' => 'other']],
  4609. 'bad',
  4610. '/option',
  4611. '/select',
  4612. ];
  4613. $this->assertHtml($expected, $result);
  4614. $options = [
  4615. ['value' => 'first', 'text' => 'First'],
  4616. ['value' => 'first', 'text' => 'Another First'],
  4617. ];
  4618. $result = $this->Form->select(
  4619. 'Model.field',
  4620. $options,
  4621. ['escape' => false, 'empty' => false]
  4622. );
  4623. $expected = [
  4624. 'select' => ['name' => 'Model[field]'],
  4625. ['option' => ['value' => 'first']],
  4626. 'First',
  4627. '/option',
  4628. ['option' => ['value' => 'first']],
  4629. 'Another First',
  4630. '/option',
  4631. '/select',
  4632. ];
  4633. $this->assertHtml($expected, $result);
  4634. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4635. $this->Form->create();
  4636. $result = $this->Form->select(
  4637. 'Model.contact_id',
  4638. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4639. ['escape' => false, 'empty' => 'pick something']
  4640. );
  4641. $expected = [
  4642. 'select' => ['name' => 'Model[contact_id]'],
  4643. ['option' => ['value' => '']], 'pick something', '/option',
  4644. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4645. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4646. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4647. '/select',
  4648. ];
  4649. $this->assertHtml($expected, $result);
  4650. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4651. $this->Form->create();
  4652. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4653. $expected = [
  4654. 'select' => ['name' => 'Model[field]'],
  4655. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4656. ['option' => ['value' => '1']], 'Yes', '/option',
  4657. '/select',
  4658. ];
  4659. $this->assertHtml($expected, $result);
  4660. }
  4661. /**
  4662. * testSelectEscapeHtml method
  4663. *
  4664. * Test that select() escapes HTML.
  4665. *
  4666. * @return void
  4667. */
  4668. public function testSelectEscapeHtml()
  4669. {
  4670. $result = $this->Form->select(
  4671. 'Model.field',
  4672. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4673. ['empty' => false]
  4674. );
  4675. $expected = [
  4676. 'select' => ['name' => 'Model[field]'],
  4677. ['option' => ['value' => 'first']],
  4678. 'first &quot;html&quot; &lt;chars&gt;',
  4679. '/option',
  4680. ['option' => ['value' => 'second']],
  4681. 'value',
  4682. '/option',
  4683. '/select',
  4684. ];
  4685. $this->assertHtml($expected, $result);
  4686. $result = $this->Form->select(
  4687. 'Model.field',
  4688. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4689. ['escape' => false, 'empty' => false]
  4690. );
  4691. $expected = [
  4692. 'select' => ['name' => 'Model[field]'],
  4693. ['option' => ['value' => 'first']],
  4694. 'first "html" <chars>',
  4695. '/option',
  4696. ['option' => ['value' => 'second']],
  4697. 'value',
  4698. '/option',
  4699. '/select',
  4700. ];
  4701. $this->assertHtml($expected, $result);
  4702. }
  4703. /**
  4704. * testSelectRequired method
  4705. *
  4706. * Test select() with required and disabled attributes.
  4707. *
  4708. * @return void
  4709. */
  4710. public function testSelectRequired()
  4711. {
  4712. $this->article['required'] = [
  4713. 'user_id' => true,
  4714. ];
  4715. $this->Form->create($this->article);
  4716. $result = $this->Form->select('user_id', ['option A']);
  4717. $expected = [
  4718. 'select' => [
  4719. 'name' => 'user_id',
  4720. 'required' => 'required',
  4721. ],
  4722. ['option' => ['value' => '0']], 'option A', '/option',
  4723. '/select',
  4724. ];
  4725. $this->assertHtml($expected, $result);
  4726. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4727. $expected = [
  4728. 'select' => [
  4729. 'name' => 'user_id',
  4730. 'disabled' => 'disabled',
  4731. ],
  4732. ['option' => ['value' => '0']], 'option A', '/option',
  4733. '/select',
  4734. ];
  4735. $this->assertHtml($expected, $result);
  4736. }
  4737. public function testSelectEmptyWithRequiredFalse()
  4738. {
  4739. $this->loadFixtures();
  4740. $Articles = TableRegistry::getTableLocator()->get('Articles');
  4741. $validator = $Articles->getValidator('default');
  4742. $validator->allowEmptyString('user_id');
  4743. $Articles->setValidator('default', $validator);
  4744. $entity = $Articles->newEmptyEntity();
  4745. $this->Form->create($entity);
  4746. $result = $this->Form->select('user_id', ['option A']);
  4747. $expected = [
  4748. 'select' => [
  4749. 'name' => 'user_id',
  4750. ],
  4751. ['option' => ['value' => '']], '/option',
  4752. ['option' => ['value' => '0']], 'option A', '/option',
  4753. '/select',
  4754. ];
  4755. $this->assertHtml($expected, $result);
  4756. }
  4757. /**
  4758. * testNestedSelect method
  4759. *
  4760. * Test select element generation with optgroups.
  4761. *
  4762. * @return void
  4763. */
  4764. public function testNestedSelect()
  4765. {
  4766. $result = $this->Form->select(
  4767. 'Model.field',
  4768. [1 => 'One', 2 => 'Two', 'Three' => [
  4769. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4770. ]],
  4771. ['empty' => false]
  4772. );
  4773. $expected = [
  4774. 'select' => ['name' => 'Model[field]'],
  4775. ['option' => ['value' => 1]],
  4776. 'One',
  4777. '/option',
  4778. ['option' => ['value' => 2]],
  4779. 'Two',
  4780. '/option',
  4781. ['optgroup' => ['label' => 'Three']],
  4782. ['option' => ['value' => 3]],
  4783. 'Three',
  4784. '/option',
  4785. ['option' => ['value' => 4]],
  4786. 'Four',
  4787. '/option',
  4788. ['option' => ['value' => 5]],
  4789. 'Five',
  4790. '/option',
  4791. '/optgroup',
  4792. '/select',
  4793. ];
  4794. $this->assertHtml($expected, $result);
  4795. }
  4796. /**
  4797. * testSelectMultiple method
  4798. *
  4799. * Test generation of multiple select elements.
  4800. *
  4801. * @return void
  4802. */
  4803. public function testSelectMultiple()
  4804. {
  4805. $options = ['first', 'second', 'third'];
  4806. $result = $this->Form->select(
  4807. 'Model.multi_field',
  4808. $options,
  4809. ['form' => 'my-form', 'multiple' => true]
  4810. );
  4811. $expected = [
  4812. 'input' => [
  4813. 'type' => 'hidden',
  4814. 'name' => 'Model[multi_field]',
  4815. 'value' => '',
  4816. 'form' => 'my-form',
  4817. ],
  4818. 'select' => [
  4819. 'name' => 'Model[multi_field][]',
  4820. 'multiple' => 'multiple',
  4821. 'form' => 'my-form',
  4822. ],
  4823. ['option' => ['value' => '0']],
  4824. 'first',
  4825. '/option',
  4826. ['option' => ['value' => '1']],
  4827. 'second',
  4828. '/option',
  4829. ['option' => ['value' => '2']],
  4830. 'third',
  4831. '/option',
  4832. '/select',
  4833. ];
  4834. $this->assertHtml($expected, $result);
  4835. $result = $this->Form->select(
  4836. 'Model.multi_field',
  4837. $options,
  4838. ['multiple' => 'multiple', 'form' => 'my-form']
  4839. );
  4840. $this->assertHtml($expected, $result);
  4841. $result = $this->Form->select(
  4842. 'Model.multi_field',
  4843. $options,
  4844. ['form' => 'my-form', 'multiple' => false]
  4845. );
  4846. $this->assertStringNotContainsString('multiple', $result);
  4847. }
  4848. /**
  4849. * testCheckboxZeroValue method
  4850. *
  4851. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4852. *
  4853. * @return void
  4854. */
  4855. public function testCheckboxZeroValue()
  4856. {
  4857. $result = $this->Form->control('User.get_spam', [
  4858. 'type' => 'checkbox',
  4859. 'value' => '0',
  4860. 'hiddenField' => '1',
  4861. ]);
  4862. $expected = [
  4863. 'div' => ['class' => 'input checkbox'],
  4864. 'label' => ['for' => 'user-get-spam'],
  4865. ['input' => [
  4866. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4867. 'value' => '1',
  4868. ]],
  4869. ['input' => [
  4870. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4871. 'value' => '0', 'id' => 'user-get-spam',
  4872. ]],
  4873. 'Get Spam',
  4874. '/label',
  4875. '/div',
  4876. ];
  4877. $this->assertHtml($expected, $result);
  4878. }
  4879. /**
  4880. * testHabtmSelectBox method
  4881. *
  4882. * Test generation of habtm select boxes.
  4883. *
  4884. * @return void
  4885. */
  4886. public function testHabtmSelectBox()
  4887. {
  4888. $this->loadFixtures('Articles');
  4889. $options = [
  4890. 1 => 'blue',
  4891. 2 => 'red',
  4892. 3 => 'green',
  4893. ];
  4894. $tags = [
  4895. new Entity(['id' => 1, 'name' => 'blue']),
  4896. new Entity(['id' => 3, 'name' => 'green']),
  4897. ];
  4898. $article = new Article(['tags' => $tags]);
  4899. $this->Form->create($article);
  4900. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4901. $expected = [
  4902. 'div' => ['class' => 'input select'],
  4903. 'label' => ['for' => 'tags-ids'],
  4904. 'Tags',
  4905. '/label',
  4906. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4907. 'select' => [
  4908. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4909. 'multiple' => 'multiple',
  4910. ],
  4911. ['option' => ['value' => '1', 'selected' => 'selected']],
  4912. 'blue',
  4913. '/option',
  4914. ['option' => ['value' => '2']],
  4915. 'red',
  4916. '/option',
  4917. ['option' => ['value' => '3', 'selected' => 'selected']],
  4918. 'green',
  4919. '/option',
  4920. '/select',
  4921. '/div',
  4922. ];
  4923. $this->assertHtml($expected, $result);
  4924. // make sure only 50 is selected, and not 50f5c0cf
  4925. $options = [
  4926. '1' => 'blue',
  4927. '50f5c0cf' => 'red',
  4928. '50' => 'green',
  4929. ];
  4930. $tags = [
  4931. new Entity(['id' => 1, 'name' => 'blue']),
  4932. new Entity(['id' => 50, 'name' => 'green']),
  4933. ];
  4934. $article = new Article(['tags' => $tags]);
  4935. $this->Form->create($article);
  4936. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4937. $expected = [
  4938. 'div' => ['class' => 'input select'],
  4939. 'label' => ['for' => 'tags-ids'],
  4940. 'Tags',
  4941. '/label',
  4942. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4943. 'select' => [
  4944. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4945. 'multiple' => 'multiple',
  4946. ],
  4947. ['option' => ['value' => '1', 'selected' => 'selected']],
  4948. 'blue',
  4949. '/option',
  4950. ['option' => ['value' => '50f5c0cf']],
  4951. 'red',
  4952. '/option',
  4953. ['option' => ['value' => '50', 'selected' => 'selected']],
  4954. 'green',
  4955. '/option',
  4956. '/select',
  4957. '/div',
  4958. ];
  4959. $this->assertHtml($expected, $result);
  4960. $spacecraft = [
  4961. 1 => 'Orion',
  4962. 2 => 'Helios',
  4963. ];
  4964. $this->View->set('spacecraft', $spacecraft);
  4965. $this->Form->create();
  4966. $result = $this->Form->control('spacecraft._ids');
  4967. $expected = [
  4968. 'div' => ['class' => 'input select'],
  4969. 'label' => ['for' => 'spacecraft-ids'],
  4970. 'Spacecraft',
  4971. '/label',
  4972. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4973. 'select' => [
  4974. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4975. 'multiple' => 'multiple',
  4976. ],
  4977. ['option' => ['value' => '1']],
  4978. 'Orion',
  4979. '/option',
  4980. ['option' => ['value' => '2']],
  4981. 'Helios',
  4982. '/option',
  4983. '/select',
  4984. '/div',
  4985. ];
  4986. $this->assertHtml($expected, $result);
  4987. }
  4988. /**
  4989. * testErrorsForBelongsToManySelect method
  4990. *
  4991. * Tests that errors for belongsToMany select fields are being
  4992. * picked up properly.
  4993. *
  4994. * @return void
  4995. */
  4996. public function testErrorsForBelongsToManySelect()
  4997. {
  4998. $spacecraft = [
  4999. 1 => 'Orion',
  5000. 2 => 'Helios',
  5001. ];
  5002. $this->View->set('spacecraft', $spacecraft);
  5003. $article = new Article();
  5004. $article->setError('spacecraft', ['Invalid']);
  5005. $this->Form->create($article);
  5006. $result = $this->Form->control('spacecraft._ids');
  5007. $expected = [
  5008. ['div' => ['class' => 'input select error']],
  5009. 'label' => ['for' => 'spacecraft-ids'],
  5010. 'Spacecraft',
  5011. '/label',
  5012. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  5013. 'select' => [
  5014. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  5015. 'multiple' => 'multiple',
  5016. ],
  5017. ['option' => ['value' => '1']],
  5018. 'Orion',
  5019. '/option',
  5020. ['option' => ['value' => '2']],
  5021. 'Helios',
  5022. '/option',
  5023. '/select',
  5024. ['div' => ['class' => 'error-message']],
  5025. 'Invalid',
  5026. '/div',
  5027. '/div',
  5028. ];
  5029. $this->assertHtml($expected, $result);
  5030. }
  5031. /**
  5032. * testSelectMultipleCheckboxes method
  5033. *
  5034. * Test generation of multi select elements in checkbox format.
  5035. *
  5036. * @return void
  5037. */
  5038. public function testSelectMultipleCheckboxes()
  5039. {
  5040. $result = $this->Form->select(
  5041. 'Model.multi_field',
  5042. ['first', 'second', 'third'],
  5043. ['multiple' => 'checkbox']
  5044. );
  5045. $expected = [
  5046. 'input' => [
  5047. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5048. ],
  5049. ['div' => ['class' => 'checkbox']],
  5050. ['label' => ['for' => 'model-multi-field-0']],
  5051. ['input' => [
  5052. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5053. 'value' => '0', 'id' => 'model-multi-field-0',
  5054. ]],
  5055. 'first',
  5056. '/label',
  5057. '/div',
  5058. ['div' => ['class' => 'checkbox']],
  5059. ['label' => ['for' => 'model-multi-field-1']],
  5060. ['input' => [
  5061. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5062. 'value' => '1', 'id' => 'model-multi-field-1',
  5063. ]],
  5064. 'second',
  5065. '/label',
  5066. '/div',
  5067. ['div' => ['class' => 'checkbox']],
  5068. ['label' => ['for' => 'model-multi-field-2']],
  5069. ['input' => [
  5070. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5071. 'value' => '2', 'id' => 'model-multi-field-2',
  5072. ]],
  5073. 'third',
  5074. '/label',
  5075. '/div',
  5076. ];
  5077. $this->assertHtml($expected, $result);
  5078. $result = $this->Form->select(
  5079. 'Model.multi_field',
  5080. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5081. ['multiple' => 'checkbox']
  5082. );
  5083. $expected = [
  5084. 'input' => [
  5085. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5086. ],
  5087. ['div' => ['class' => 'checkbox']],
  5088. ['label' => ['for' => 'model-multi-field-a+']],
  5089. ['input' => [
  5090. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5091. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5092. ]],
  5093. 'first',
  5094. '/label',
  5095. '/div',
  5096. ['div' => ['class' => 'checkbox']],
  5097. ['label' => ['for' => 'model-multi-field-a++']],
  5098. ['input' => [
  5099. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5100. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5101. ]],
  5102. 'second',
  5103. '/label',
  5104. '/div',
  5105. ['div' => ['class' => 'checkbox']],
  5106. ['label' => ['for' => 'model-multi-field-a+++']],
  5107. ['input' => [
  5108. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5109. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5110. ]],
  5111. 'third',
  5112. '/label',
  5113. '/div',
  5114. ];
  5115. $this->assertHtml($expected, $result);
  5116. $result = $this->Form->select(
  5117. 'Model.multi_field',
  5118. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5119. ['multiple' => 'checkbox']
  5120. );
  5121. $expected = [
  5122. 'input' => [
  5123. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5124. ],
  5125. ['div' => ['class' => 'checkbox']],
  5126. ['label' => ['for' => 'model-multi-field-a-b']],
  5127. ['input' => [
  5128. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5129. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5130. ]],
  5131. 'first',
  5132. '/label',
  5133. '/div',
  5134. ['div' => ['class' => 'checkbox']],
  5135. ['label' => ['for' => 'model-multi-field-a-b1']],
  5136. ['input' => [
  5137. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5138. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5139. ]],
  5140. 'second',
  5141. '/label',
  5142. '/div',
  5143. ['div' => ['class' => 'checkbox']],
  5144. ['label' => ['for' => 'model-multi-field-a-b2']],
  5145. ['input' => [
  5146. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5147. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5148. ]],
  5149. 'third',
  5150. '/label',
  5151. '/div',
  5152. ];
  5153. $this->assertHtml($expected, $result);
  5154. }
  5155. /**
  5156. * testSelectMultipleCheckboxRequestData method
  5157. *
  5158. * Ensure that multiCheckbox reads from the request data.
  5159. *
  5160. * @return void
  5161. */
  5162. public function testSelectMultipleCheckboxRequestData()
  5163. {
  5164. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5165. $result = $this->Form->select(
  5166. 'Model.tags',
  5167. ['1' => 'first', 'Array' => 'Array'],
  5168. ['multiple' => 'checkbox']
  5169. );
  5170. $expected = [
  5171. 'input' => [
  5172. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '',
  5173. ],
  5174. ['div' => ['class' => 'checkbox']],
  5175. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5176. ['input' => [
  5177. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5178. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5179. ]],
  5180. 'first',
  5181. '/label',
  5182. '/div',
  5183. ['div' => ['class' => 'checkbox']],
  5184. ['label' => ['for' => 'model-tags-array']],
  5185. ['input' => [
  5186. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5187. 'value' => 'Array', 'id' => 'model-tags-array',
  5188. ]],
  5189. 'Array',
  5190. '/label',
  5191. '/div',
  5192. ];
  5193. $this->assertHtml($expected, $result);
  5194. }
  5195. /**
  5196. * testSelectMultipleCheckboxSecurity method
  5197. *
  5198. * Checks the security hash array generated for multiple-input checkbox elements.
  5199. *
  5200. * @return void
  5201. */
  5202. public function testSelectMultipleCheckboxSecurity()
  5203. {
  5204. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5205. $this->Form->create();
  5206. $this->Form->select(
  5207. 'Model.multi_field',
  5208. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5209. ['multiple' => 'checkbox']
  5210. );
  5211. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5212. $this->assertEquals(['Model.multi_field'], $fields);
  5213. $result = $this->Form->secure();
  5214. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5215. $hash = urlencode($hash . ':');
  5216. $this->assertStringContainsString('"' . $hash . '"', $result);
  5217. }
  5218. /**
  5219. * testSelectMultipleSecureWithNoOptions method
  5220. *
  5221. * Multiple select elements should always be secured as they always participate
  5222. * in the POST data.
  5223. *
  5224. * @return void
  5225. */
  5226. public function testSelectMultipleSecureWithNoOptions()
  5227. {
  5228. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5229. $this->Form->create();
  5230. $this->Form->select(
  5231. 'Model.select',
  5232. [],
  5233. ['multiple' => true]
  5234. );
  5235. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5236. $this->assertEquals(['Model.select'], $result);
  5237. }
  5238. /**
  5239. * testSelectNoSecureWithNoOptions method
  5240. *
  5241. * When a select box has no options it should not be added to the fields list
  5242. * as it always fail post validation.
  5243. *
  5244. * @return void
  5245. */
  5246. public function testSelectNoSecureWithNoOptions()
  5247. {
  5248. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5249. $this->Form->create();
  5250. $this->Form->select(
  5251. 'Model.select',
  5252. []
  5253. );
  5254. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5255. $this->assertEquals([], $result);
  5256. $this->Form->select(
  5257. 'Model.user_id',
  5258. [],
  5259. ['empty' => true]
  5260. );
  5261. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5262. $this->assertEquals(['Model.user_id'], $result);
  5263. }
  5264. /**
  5265. * testControlMultipleCheckboxes method
  5266. *
  5267. * Test control() resulting in multi select elements being generated.
  5268. *
  5269. * @return void
  5270. */
  5271. public function testControlMultipleCheckboxes()
  5272. {
  5273. $result = $this->Form->control('Model.multi_field', [
  5274. 'options' => ['first', 'second', 'third'],
  5275. 'multiple' => 'checkbox',
  5276. ]);
  5277. $expected = [
  5278. ['div' => ['class' => 'input select']],
  5279. ['label' => ['for' => 'model-multi-field']],
  5280. 'Multi Field',
  5281. '/label',
  5282. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5283. ['div' => ['class' => 'checkbox']],
  5284. ['label' => ['for' => 'model-multi-field-0']],
  5285. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5286. 'first',
  5287. '/label',
  5288. '/div',
  5289. ['div' => ['class' => 'checkbox']],
  5290. ['label' => ['for' => 'model-multi-field-1']],
  5291. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5292. 'second',
  5293. '/label',
  5294. '/div',
  5295. ['div' => ['class' => 'checkbox']],
  5296. ['label' => ['for' => 'model-multi-field-2']],
  5297. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5298. 'third',
  5299. '/label',
  5300. '/div',
  5301. '/div',
  5302. ];
  5303. $this->assertHtml($expected, $result);
  5304. $result = $this->Form->control('Model.multi_field', [
  5305. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5306. 'multiple' => 'checkbox',
  5307. ]);
  5308. $expected = [
  5309. ['div' => ['class' => 'input select']],
  5310. ['label' => ['for' => 'model-multi-field']],
  5311. 'Multi Field',
  5312. '/label',
  5313. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5314. ['div' => ['class' => 'checkbox']],
  5315. ['label' => ['for' => 'model-multi-field-a']],
  5316. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5317. 'first',
  5318. '/label',
  5319. '/div',
  5320. ['div' => ['class' => 'checkbox']],
  5321. ['label' => ['for' => 'model-multi-field-b']],
  5322. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5323. 'second',
  5324. '/label',
  5325. '/div',
  5326. ['div' => ['class' => 'checkbox']],
  5327. ['label' => ['for' => 'model-multi-field-c']],
  5328. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5329. 'third',
  5330. '/label',
  5331. '/div',
  5332. '/div',
  5333. ];
  5334. $this->assertHtml($expected, $result);
  5335. }
  5336. /**
  5337. * testSelectHiddenFieldOmission method
  5338. *
  5339. * Test that select() with 'hiddenField' => false omits the hidden field.
  5340. *
  5341. * @return void
  5342. */
  5343. public function testSelectHiddenFieldOmission()
  5344. {
  5345. $result = $this->Form->select(
  5346. 'Model.multi_field',
  5347. ['first', 'second'],
  5348. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5349. );
  5350. $this->assertStringNotContainsString('type="hidden"', $result);
  5351. }
  5352. /**
  5353. * testSelectCheckboxMultipleOverrideName method
  5354. *
  5355. * Test that select() with multiple = checkbox works with overriding name attribute.
  5356. *
  5357. * @return void
  5358. */
  5359. public function testSelectCheckboxMultipleOverrideName()
  5360. {
  5361. $result = $this->Form->select('category', ['1', '2'], [
  5362. 'multiple' => 'checkbox',
  5363. 'name' => 'fish',
  5364. ]);
  5365. $expected = [
  5366. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5367. ['div' => ['class' => 'checkbox']],
  5368. ['label' => ['for' => 'fish-0']],
  5369. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5370. '1',
  5371. '/label',
  5372. '/div',
  5373. ['div' => ['class' => 'checkbox']],
  5374. ['label' => ['for' => 'fish-1']],
  5375. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5376. '2',
  5377. '/label',
  5378. '/div',
  5379. ];
  5380. $this->assertHtml($expected, $result);
  5381. $result = $this->Form->multiCheckbox(
  5382. 'category',
  5383. new Collection(['1', '2']),
  5384. ['name' => 'fish']
  5385. );
  5386. $this->assertHtml($expected, $result);
  5387. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5388. 'name' => 'fish',
  5389. ]);
  5390. $this->assertHtml($expected, $result);
  5391. }
  5392. /**
  5393. * testControlMultiCheckbox method
  5394. *
  5395. * Test that control() works with multicheckbox.
  5396. *
  5397. * @return void
  5398. */
  5399. public function testControlMultiCheckbox()
  5400. {
  5401. $result = $this->Form->control('category', [
  5402. 'type' => 'multicheckbox',
  5403. 'options' => ['1', '2'],
  5404. ]);
  5405. $expected = [
  5406. ['div' => ['class' => 'input multicheckbox']],
  5407. '<label',
  5408. 'Category',
  5409. '/label',
  5410. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5411. ['div' => ['class' => 'checkbox']],
  5412. ['label' => ['for' => 'category-0']],
  5413. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5414. '1',
  5415. '/label',
  5416. '/div',
  5417. ['div' => ['class' => 'checkbox']],
  5418. ['label' => ['for' => 'category-1']],
  5419. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5420. '2',
  5421. '/label',
  5422. '/div',
  5423. '/div',
  5424. ];
  5425. $this->assertHtml($expected, $result);
  5426. }
  5427. /**
  5428. * testCheckbox method
  5429. *
  5430. * Test generation of checkboxes.
  5431. *
  5432. * @return void
  5433. */
  5434. public function testCheckbox()
  5435. {
  5436. $result = $this->Form->checkbox('Model.field');
  5437. $expected = [
  5438. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5439. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5440. ];
  5441. $this->assertHtml($expected, $result);
  5442. $result = $this->Form->checkbox('Model.field', [
  5443. 'id' => 'theID',
  5444. 'value' => 'myvalue',
  5445. 'form' => 'my-form',
  5446. ]);
  5447. $expected = [
  5448. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5449. ['input' => [
  5450. 'type' => 'checkbox', 'name' => 'Model[field]',
  5451. 'value' => 'myvalue', 'id' => 'theID',
  5452. 'form' => 'my-form',
  5453. ]],
  5454. ];
  5455. $this->assertHtml($expected, $result);
  5456. }
  5457. /**
  5458. * testCheckboxDefaultValue method
  5459. *
  5460. * Test default value setting on checkbox() method.
  5461. *
  5462. * @return void
  5463. */
  5464. public function testCheckboxDefaultValue()
  5465. {
  5466. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5467. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5468. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5469. $this->assertHtml($expected, $result);
  5470. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5471. $this->Form->create();
  5472. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5473. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5474. $this->assertHtml($expected, $result);
  5475. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5476. $this->Form->create();
  5477. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5478. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5479. $this->assertHtml($expected, $result);
  5480. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5481. $this->Form->create();
  5482. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5483. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5484. $this->assertHtml($expected, $result);
  5485. $Articles = $this->getTableLocator()->get('Articles');
  5486. $Articles->getSchema()->addColumn(
  5487. 'published',
  5488. ['type' => 'boolean', 'null' => false, 'default' => true]
  5489. );
  5490. $this->Form->create($Articles->newEmptyEntity());
  5491. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5492. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5493. $this->assertHtml($expected, $result);
  5494. }
  5495. /**
  5496. * testCheckboxCheckedAndError method
  5497. *
  5498. * Test checkbox being checked or having errors.
  5499. *
  5500. * @return void
  5501. */
  5502. public function testCheckboxCheckedAndError()
  5503. {
  5504. $this->article['errors'] = [
  5505. 'published' => true,
  5506. ];
  5507. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5508. $this->Form->create($this->article);
  5509. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5510. $expected = [
  5511. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5512. ['input' => [
  5513. 'type' => 'checkbox',
  5514. 'name' => 'published',
  5515. 'value' => 'myvalue',
  5516. 'id' => 'theID',
  5517. 'checked' => 'checked',
  5518. 'class' => 'form-error',
  5519. ]],
  5520. ];
  5521. $this->assertHtml($expected, $result);
  5522. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5523. $this->Form->create($this->article);
  5524. $result = $this->Form->checkbox('published');
  5525. $expected = [
  5526. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5527. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5528. ];
  5529. $this->assertHtml($expected, $result);
  5530. }
  5531. /**
  5532. * testCheckboxCustomNameAttribute method
  5533. *
  5534. * Test checkbox() with a custom name attribute.
  5535. *
  5536. * @return void
  5537. */
  5538. public function testCheckboxCustomNameAttribute()
  5539. {
  5540. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5541. $expected = [
  5542. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5543. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5544. ];
  5545. $this->assertHtml($expected, $result);
  5546. }
  5547. /**
  5548. * testCheckboxHiddenField method
  5549. *
  5550. * Test that the hidden input for checkboxes can be omitted or set to a
  5551. * specific value.
  5552. *
  5553. * @return void
  5554. */
  5555. public function testCheckboxHiddenField()
  5556. {
  5557. $result = $this->Form->checkbox('UserForm.something', [
  5558. 'hiddenField' => false,
  5559. ]);
  5560. $expected = [
  5561. 'input' => [
  5562. 'type' => 'checkbox',
  5563. 'name' => 'UserForm[something]',
  5564. 'value' => '1',
  5565. ],
  5566. ];
  5567. $this->assertHtml($expected, $result);
  5568. $result = $this->Form->checkbox('UserForm.something', [
  5569. 'value' => 'Y',
  5570. 'hiddenField' => 'N',
  5571. ]);
  5572. $expected = [
  5573. ['input' => [
  5574. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5575. 'value' => 'N',
  5576. ]],
  5577. ['input' => [
  5578. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5579. 'value' => 'Y',
  5580. ]],
  5581. ];
  5582. $this->assertHtml($expected, $result);
  5583. }
  5584. /**
  5585. * testTime method
  5586. *
  5587. * Test the time type.
  5588. *
  5589. * @return void
  5590. */
  5591. public function testTime()
  5592. {
  5593. $result = $this->Form->time('start_time', [
  5594. 'value' => '2014-03-08 16:30:00',
  5595. ]);
  5596. $expected = [
  5597. 'input' => [
  5598. 'type' => 'time',
  5599. 'name' => 'start_time',
  5600. 'value' => '16:30:00',
  5601. 'step' => '1',
  5602. ],
  5603. ];
  5604. $this->assertHtml($expected, $result);
  5605. }
  5606. /**
  5607. * testDate method
  5608. *
  5609. * Test the date type.
  5610. *
  5611. * @return void
  5612. */
  5613. public function testDate()
  5614. {
  5615. $result = $this->Form->date('start_day', [
  5616. 'value' => '2014-03-08',
  5617. ]);
  5618. $expected = [
  5619. 'input' => [
  5620. 'type' => 'date',
  5621. 'name' => 'start_day',
  5622. 'value' => '2014-03-08',
  5623. ],
  5624. ];
  5625. $this->assertHtml($expected, $result);
  5626. $result = $this->Form->date('start_day', [
  5627. 'value' => new Date('2014-03-08'),
  5628. ]);
  5629. $this->assertHtml($expected, $result);
  5630. }
  5631. /**
  5632. * testDateTime method
  5633. *
  5634. * @return void
  5635. */
  5636. public function testDateTime()
  5637. {
  5638. $result = $this->Form->dateTime('date', ['default' => true]);
  5639. $expected = [
  5640. 'input' => [
  5641. 'type' => 'datetime-local',
  5642. 'name' => 'date',
  5643. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}\.\d{3}/',
  5644. 'step' => '1',
  5645. ],
  5646. ];
  5647. $this->assertHtml($expected, $result);
  5648. }
  5649. /**
  5650. * testDateTimeSecured method
  5651. *
  5652. * Test that datetime fields are added to protected fields list.
  5653. *
  5654. * @return void
  5655. */
  5656. public function testDateTimeSecured()
  5657. {
  5658. $this->View->setRequest(
  5659. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5660. );
  5661. $this->Form->create();
  5662. $this->Form->dateTime('date');
  5663. $expected = ['date'];
  5664. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5665. $this->assertEquals($expected, $result);
  5666. $this->Form->fields = [];
  5667. $this->Form->date('published');
  5668. $expected = ['date', 'published'];
  5669. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5670. $this->assertEquals($expected, $result);
  5671. }
  5672. /**
  5673. * testDateTimeSecuredDisabled method
  5674. *
  5675. * Test that datetime fields are added to protected fields list.
  5676. *
  5677. * @return void
  5678. */
  5679. public function testDateTimeSecuredDisabled()
  5680. {
  5681. $this->View->setRequest(
  5682. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5683. );
  5684. $this->Form->create();
  5685. $this->Form->dateTime('date', ['secure' => false]);
  5686. $expected = [];
  5687. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5688. $this->assertEquals($expected, $result);
  5689. $this->Form->fields = [];
  5690. $this->Form->date('published', ['secure' => false]);
  5691. $expected = [];
  5692. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5693. $this->assertEquals($expected, $result);
  5694. }
  5695. /**
  5696. * testDatetimeWithDefault method
  5697. *
  5698. * Test that datetime() and default values work.
  5699. *
  5700. * @return void
  5701. */
  5702. public function testDatetimeWithDefault()
  5703. {
  5704. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5705. $expected = [
  5706. 'input' => [
  5707. 'type' => 'datetime-local',
  5708. 'name' => 'updated',
  5709. 'value' => '2009-06-01T11:15:30.000',
  5710. 'step' => '1',
  5711. ],
  5712. ];
  5713. $this->assertHtml($expected, $result);
  5714. $result = $this->Form->dateTime('updated', [
  5715. 'default' => '2009-06-01 11:15:30',
  5716. ]);
  5717. $expected = [
  5718. 'input' => [
  5719. 'type' => 'datetime-local',
  5720. 'name' => 'updated',
  5721. 'value' => '2009-06-01T11:15:30.000',
  5722. 'step' => '1',
  5723. ],
  5724. ];
  5725. $this->assertHtml($expected, $result);
  5726. }
  5727. /**
  5728. * testMonth method
  5729. *
  5730. * Test generation of a month input.
  5731. *
  5732. * @return void
  5733. */
  5734. public function testMonth()
  5735. {
  5736. $result = $this->Form->month('field', ['value' => '']);
  5737. $expected = [
  5738. 'input' => [
  5739. 'type' => 'month',
  5740. 'name' => 'field',
  5741. 'value' => '',
  5742. ],
  5743. ];
  5744. $this->assertHtml($expected, $result);
  5745. $this->View->setRequest(
  5746. $this->View->getRequest()->withData('release', '2050-02-10')
  5747. );
  5748. $this->Form->create();
  5749. $result = $this->Form->month('release');
  5750. $expected = [
  5751. 'input' => [
  5752. 'type' => 'month',
  5753. 'name' => 'release',
  5754. 'value' => '2050-02',
  5755. ],
  5756. ];
  5757. $this->assertHtml($expected, $result);
  5758. $this->View->setRequest(
  5759. $this->View->getRequest()->withData('release', '2050-03')
  5760. );
  5761. $this->Form->create();
  5762. $result = $this->Form->month('release');
  5763. $expected = [
  5764. 'input' => [
  5765. 'type' => 'month',
  5766. 'name' => 'release',
  5767. 'value' => '2050-03',
  5768. ],
  5769. ];
  5770. $this->assertHtml($expected, $result);
  5771. }
  5772. /**
  5773. * testYear method
  5774. *
  5775. * Test generation of a year input.
  5776. *
  5777. * @return void
  5778. */
  5779. public function testYear()
  5780. {
  5781. $this->View->setRequest(
  5782. $this->View->getRequest()->withData('published', '2006')
  5783. );
  5784. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5785. $expected = [
  5786. ['select' => ['name' => 'field']],
  5787. ['option' => ['selected' => 'selected', 'value' => '']],
  5788. '/option',
  5789. ['option' => ['value' => '2007']],
  5790. '2007',
  5791. '/option',
  5792. ['option' => ['value' => '2006']],
  5793. '2006',
  5794. '/option',
  5795. '/select',
  5796. ];
  5797. $this->assertHtml($expected, $result);
  5798. $result = $this->Form->year('field', [
  5799. 'value' => '',
  5800. 'min' => 2006,
  5801. 'max' => 2007,
  5802. 'order' => 'asc',
  5803. ]);
  5804. $expected = [
  5805. ['select' => ['name' => 'field']],
  5806. ['option' => ['selected' => 'selected', 'value' => '']],
  5807. '/option',
  5808. ['option' => ['value' => '2006']],
  5809. '2006',
  5810. '/option',
  5811. ['option' => ['value' => '2007']],
  5812. '2007',
  5813. '/option',
  5814. '/select',
  5815. ];
  5816. $this->assertHtml($expected, $result);
  5817. $result = $this->Form->year('published', [
  5818. 'empty' => false,
  5819. 'min' => 2006,
  5820. 'max' => 2007,
  5821. ]);
  5822. $expected = [
  5823. ['select' => ['name' => 'published']],
  5824. ['option' => ['value' => '2007']],
  5825. '2007',
  5826. '/option',
  5827. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5828. '2006',
  5829. '/option',
  5830. '/select',
  5831. ];
  5832. $this->assertHtml($expected, $result);
  5833. $result = $this->Form->year('published', [
  5834. 'empty' => 'Published on',
  5835. ]);
  5836. $this->assertStringContainsString('Published on', $result);
  5837. }
  5838. /**
  5839. * testControlYearPreEpoch method
  5840. *
  5841. * Test minYear being prior to the unix epoch.
  5842. *
  5843. * @return void
  5844. */
  5845. public function testControlYearPreEpoch()
  5846. {
  5847. $start = date('Y') - 80;
  5848. $end = date('Y') - 18;
  5849. $result = $this->Form->control('birth_year', [
  5850. 'type' => 'year',
  5851. 'label' => 'Birth Year',
  5852. 'min' => $start,
  5853. 'max' => $end,
  5854. ]);
  5855. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5856. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5857. $this->assertStringNotContainsString('value="00">00', $result);
  5858. }
  5859. /**
  5860. * test control() datetime & required attributes
  5861. *
  5862. * @return void
  5863. */
  5864. public function testControlDatetimeRequired()
  5865. {
  5866. $result = $this->Form->control('birthday', [
  5867. 'type' => 'date',
  5868. 'required' => true,
  5869. ]);
  5870. $this->assertStringContainsString(
  5871. '<input type="date" name="birthday" required="required"',
  5872. $result
  5873. );
  5874. }
  5875. /**
  5876. * testYearAutoExpandRange method
  5877. *
  5878. * @return void
  5879. */
  5880. public function testYearAutoExpandRange()
  5881. {
  5882. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5883. $result = $this->Form->year('birthday');
  5884. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5885. $result = $matches[1];
  5886. $expected = range(date('Y') + 5, 1930);
  5887. $this->assertEquals($expected, $result);
  5888. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5889. $this->Form->create();
  5890. $result = $this->Form->year('release');
  5891. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5892. $result = $matches[1];
  5893. $expected = range(2050, date('Y') - 5);
  5894. $this->assertEquals($expected, $result);
  5895. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5896. $this->Form->create();
  5897. $result = $this->Form->year('release', [
  5898. 'min' => 1890,
  5899. 'max' => 1900,
  5900. ]);
  5901. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5902. $result = $matches[1];
  5903. $expected = range(1900, 1881);
  5904. $this->assertEquals($expected, $result);
  5905. }
  5906. /**
  5907. * testControlLabelFalse method
  5908. *
  5909. * Test the label option being set to false.
  5910. *
  5911. * @return void
  5912. */
  5913. public function testControlLabelFalse()
  5914. {
  5915. $this->Form->create($this->article);
  5916. $result = $this->Form->control('title', ['label' => false]);
  5917. $expected = [
  5918. 'div' => ['class' => 'input text required'],
  5919. 'input' => [
  5920. 'type' => 'text',
  5921. 'required' => 'required',
  5922. 'id' => 'title',
  5923. 'name' => 'title',
  5924. 'data-validity-message' => 'This field cannot be left empty',
  5925. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5926. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5927. ],
  5928. '/div',
  5929. ];
  5930. $this->assertHtml($expected, $result);
  5931. }
  5932. /**
  5933. * testTextArea method
  5934. *
  5935. * Test generation of a textarea input.
  5936. *
  5937. * @return void
  5938. */
  5939. public function testTextArea()
  5940. {
  5941. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5942. $result = $this->Form->textarea('field');
  5943. $expected = [
  5944. 'textarea' => ['name' => 'field', 'rows' => 5],
  5945. 'some test data',
  5946. '/textarea',
  5947. ];
  5948. $this->assertHtml($expected, $result);
  5949. $result = $this->Form->textarea('user.bio');
  5950. $expected = [
  5951. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5952. '/textarea',
  5953. ];
  5954. $this->assertHtml($expected, $result);
  5955. $this->View->setRequest($this->View->getRequest()
  5956. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5957. $this->Form->create();
  5958. $result = $this->Form->textarea('field');
  5959. $expected = [
  5960. 'textarea' => ['name' => 'field', 'rows' => 5],
  5961. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5962. '/textarea',
  5963. ];
  5964. $this->assertHtml($expected, $result);
  5965. $this->View->setRequest($this->View->getRequest()->withData(
  5966. 'Model.field',
  5967. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5968. ));
  5969. $this->Form->create();
  5970. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5971. $expected = [
  5972. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5973. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5974. '/textarea',
  5975. ];
  5976. $this->assertHtml($expected, $result);
  5977. $result = $this->Form->textarea('0.OtherModel.field');
  5978. $expected = [
  5979. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5980. '/textarea',
  5981. ];
  5982. $this->assertHtml($expected, $result);
  5983. }
  5984. /**
  5985. * testTextAreaWithStupidCharacters method
  5986. *
  5987. * Test text area with non-ascii characters.
  5988. *
  5989. * @return void
  5990. */
  5991. public function testTextAreaWithStupidCharacters()
  5992. {
  5993. $result = $this->Form->textarea('Post.content', [
  5994. 'value' => 'GREAT®',
  5995. 'rows' => '15',
  5996. 'cols' => '75',
  5997. ]);
  5998. $expected = [
  5999. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6000. 'GREAT®',
  6001. '/textarea',
  6002. ];
  6003. $this->assertHtml($expected, $result);
  6004. }
  6005. /**
  6006. * testTextAreaMaxLength method
  6007. *
  6008. * Test textareas maxlength read from schema.
  6009. *
  6010. * @return void
  6011. */
  6012. public function testTextAreaMaxLength()
  6013. {
  6014. $this->Form->create([
  6015. 'schema' => [
  6016. 'stuff' => ['type' => 'string', 'length' => 10],
  6017. ],
  6018. ]);
  6019. $result = $this->Form->control('other', ['type' => 'textarea']);
  6020. $expected = [
  6021. 'div' => ['class' => 'input textarea'],
  6022. 'label' => ['for' => 'other'],
  6023. 'Other',
  6024. '/label',
  6025. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6026. '/textarea',
  6027. '/div',
  6028. ];
  6029. $this->assertHtml($expected, $result);
  6030. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6031. $expected = [
  6032. 'div' => ['class' => 'input textarea'],
  6033. 'label' => ['for' => 'stuff'],
  6034. 'Stuff',
  6035. '/label',
  6036. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6037. '/textarea',
  6038. '/div',
  6039. ];
  6040. $this->assertHtml($expected, $result);
  6041. }
  6042. /**
  6043. * testHiddenField method
  6044. *
  6045. * Test generation of a hidden input.
  6046. *
  6047. * @return void
  6048. */
  6049. public function testHiddenField()
  6050. {
  6051. $this->article['errors'] = [
  6052. 'field' => true,
  6053. ];
  6054. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6055. $this->Form->create($this->article);
  6056. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6057. $expected = [
  6058. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6059. $this->assertHtml($expected, $result);
  6060. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6061. $expected = [
  6062. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6063. ];
  6064. $this->assertHtml($expected, $result);
  6065. }
  6066. /**
  6067. * testFileUploadField method
  6068. *
  6069. * Test generation of a file upload input.
  6070. *
  6071. * @return void
  6072. */
  6073. public function testFileUploadField()
  6074. {
  6075. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6076. $result = $this->Form->file('Model.upload');
  6077. $this->assertHtml($expected, $result);
  6078. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6079. 'name' => '', 'type' => '', 'tmp_name' => '',
  6080. 'error' => 4, 'size' => 0,
  6081. ]));
  6082. $result = $this->Form->file('Model.upload');
  6083. $this->assertHtml($expected, $result);
  6084. $this->View->setRequest(
  6085. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6086. );
  6087. $result = $this->Form->file('Model.upload');
  6088. $this->assertHtml($expected, $result);
  6089. }
  6090. /**
  6091. * testFileUploadOnOtherModel method
  6092. *
  6093. * Test File upload input on a model not used in create().
  6094. *
  6095. * @return void
  6096. */
  6097. public function testFileUploadOnOtherModel()
  6098. {
  6099. $this->Form->create($this->article, ['type' => 'file']);
  6100. $result = $this->Form->file('ValidateProfile.city');
  6101. $expected = [
  6102. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6103. ];
  6104. $this->assertHtml($expected, $result);
  6105. }
  6106. /**
  6107. * testButton method
  6108. *
  6109. * Test generation of a form button.
  6110. *
  6111. * @return void
  6112. */
  6113. public function testButton()
  6114. {
  6115. $result = $this->Form->button('Hi');
  6116. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6117. $this->assertHtml($expected, $result);
  6118. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  6119. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6120. $this->assertHtml($expected, $result);
  6121. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  6122. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6123. $this->assertHtml($expected, $result);
  6124. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  6125. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6126. $this->assertHtml($expected, $result);
  6127. $result = $this->Form->button('No type', ['type' => false]);
  6128. $expected = ['button' => [], 'No type', '/button'];
  6129. $this->assertHtml($expected, $result);
  6130. $result = $this->Form->button('Upload Text', [
  6131. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6132. 'escape' => false,
  6133. ]);
  6134. $this->assertNotRegExp('/\&039/', $result);
  6135. }
  6136. /**
  6137. * testButtonUnlockedByDefault method
  6138. *
  6139. * Test that button() makes unlocked fields by default.
  6140. *
  6141. * @return void
  6142. */
  6143. public function testButtonUnlockedByDefault()
  6144. {
  6145. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6146. $this->Form->create();
  6147. $this->Form->button('Save', ['name' => 'save']);
  6148. $this->Form->button('Clear');
  6149. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6150. $this->assertEquals(['save'], $result);
  6151. }
  6152. /**
  6153. * Test generation of a form button with confirm message.
  6154. *
  6155. * @return void
  6156. */
  6157. public function testButtonWithConfirm()
  6158. {
  6159. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6160. $expected = ['button' => [
  6161. 'type' => 'submit',
  6162. 'data-confirm-message' => 'Confirm me!',
  6163. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6164. ], 'Hi', '/button'];
  6165. $this->assertHtml($expected, $result);
  6166. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6167. $expected = ['button' => [
  6168. 'type' => 'submit',
  6169. 'data-confirm-message' => 'Confirm "me"!',
  6170. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6171. ], 'Hi', '/button'];
  6172. $this->assertHtml($expected, $result);
  6173. }
  6174. /**
  6175. * testPostButton method
  6176. *
  6177. * @return void
  6178. */
  6179. public function testPostButton()
  6180. {
  6181. $result = $this->Form->postButton('Hi', '/controller/action');
  6182. $expected = [
  6183. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6184. 'button' => ['type' => 'submit'],
  6185. 'Hi',
  6186. '/button',
  6187. '/form',
  6188. ];
  6189. $this->assertHtml($expected, $result);
  6190. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6191. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6192. }
  6193. /**
  6194. * testPostButtonMethodType method
  6195. *
  6196. * @return void
  6197. */
  6198. public function testPostButtonMethodType()
  6199. {
  6200. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6201. $expected = [
  6202. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6203. 'div' => ['style' => 'display:none;'],
  6204. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6205. '/div',
  6206. 'button' => ['type' => 'submit'],
  6207. 'Hi',
  6208. '/button',
  6209. '/form',
  6210. ];
  6211. $this->assertHtml($expected, $result);
  6212. }
  6213. /**
  6214. * testPostButtonFormOptions method
  6215. *
  6216. * @return void
  6217. */
  6218. public function testPostButtonFormOptions()
  6219. {
  6220. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6221. $expected = [
  6222. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6223. 'button' => ['type' => 'submit'],
  6224. 'Hi',
  6225. '/button',
  6226. '/form',
  6227. ];
  6228. $this->assertHtml($expected, $result);
  6229. }
  6230. /**
  6231. * testPostButtonNestedData method
  6232. *
  6233. * Test using postButton with N dimensional data.
  6234. *
  6235. * @return void
  6236. */
  6237. public function testPostButtonNestedData()
  6238. {
  6239. $data = [
  6240. 'one' => [
  6241. 'two' => [
  6242. 3, 4, 5,
  6243. ],
  6244. ],
  6245. ];
  6246. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6247. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6248. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6249. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6250. }
  6251. /**
  6252. * testSecurePostButton method
  6253. *
  6254. * Test that postButton adds _Token fields.
  6255. *
  6256. * @return void
  6257. */
  6258. public function testSecurePostButton()
  6259. {
  6260. $this->View->setRequest($this->View->getRequest()
  6261. ->withAttribute('csrfToken', 'testkey')
  6262. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6263. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6264. $tokenDebug = urlencode(json_encode([
  6265. '/posts/delete/1',
  6266. [],
  6267. [],
  6268. ]));
  6269. $expected = [
  6270. 'form' => [
  6271. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6272. ],
  6273. ['div' => ['style' => 'display:none;']],
  6274. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6275. '/div',
  6276. 'button' => ['type' => 'submit'],
  6277. 'Delete',
  6278. '/button',
  6279. ['div' => ['style' => 'display:none;']],
  6280. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6281. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6282. ['input' => [
  6283. 'type' => 'hidden',
  6284. 'name' => '_Token[debug]',
  6285. 'value' => $tokenDebug,
  6286. 'autocomplete' => 'off',
  6287. ]],
  6288. '/div',
  6289. '/form',
  6290. ];
  6291. $this->assertHtml($expected, $result);
  6292. }
  6293. /**
  6294. * testPostLink method
  6295. *
  6296. * @return void
  6297. */
  6298. public function testPostLink()
  6299. {
  6300. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6301. $expected = [
  6302. 'form' => [
  6303. 'method' => 'post', 'action' => '/posts/delete/1',
  6304. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6305. ],
  6306. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6307. '/form',
  6308. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6309. 'Delete',
  6310. '/a',
  6311. ];
  6312. $this->assertHtml($expected, $result);
  6313. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6314. $expected = [
  6315. 'form' => [
  6316. 'method' => 'post', 'action' => '/posts/delete/1',
  6317. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6318. ],
  6319. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6320. '/form',
  6321. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6322. 'Delete',
  6323. '/a',
  6324. ];
  6325. $this->assertHtml($expected, $result);
  6326. $result = $this->Form->postLink(
  6327. 'Delete',
  6328. '/posts/delete/1',
  6329. ['target' => '_blank', 'class' => 'btn btn-danger']
  6330. );
  6331. $expected = [
  6332. 'form' => [
  6333. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6334. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6335. ],
  6336. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6337. '/form',
  6338. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6339. 'Delete',
  6340. '/a',
  6341. ];
  6342. $this->assertHtml($expected, $result);
  6343. }
  6344. /**
  6345. * testPostLinkWithConfirm method
  6346. *
  6347. * Test the confirm option for postLink().
  6348. *
  6349. * @return void
  6350. */
  6351. public function testPostLinkWithConfirm()
  6352. {
  6353. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6354. $expected = [
  6355. 'form' => [
  6356. 'method' => 'post', 'action' => '/posts/delete/1',
  6357. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6358. ],
  6359. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6360. '/form',
  6361. 'a' => [
  6362. 'href' => '#',
  6363. 'data-confirm-message' => 'Confirm?',
  6364. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6365. ],
  6366. 'Delete',
  6367. '/a',
  6368. ];
  6369. $this->assertHtml($expected, $result);
  6370. $result = $this->Form->postLink(
  6371. 'Delete',
  6372. '/posts/delete/1',
  6373. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6374. );
  6375. $expected = [
  6376. 'form' => [
  6377. 'method' => 'post', 'action' => '/posts/delete/1',
  6378. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6379. ],
  6380. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6381. '/form',
  6382. 'a' => [
  6383. 'href' => '#',
  6384. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6385. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6386. ],
  6387. 'Delete',
  6388. '/a',
  6389. ];
  6390. $this->assertHtml($expected, $result);
  6391. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6392. $result = $this->Form->postLink(
  6393. 'Delete',
  6394. '/posts/delete/1',
  6395. ['escape' => false, 'confirm' => 'Confirm this 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' => 'Confirm this deletion?',
  6407. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6408. ],
  6409. 'Delete',
  6410. '/a',
  6411. ];
  6412. $this->assertHtml($expected, $result);
  6413. }
  6414. /**
  6415. * testPostLinkWithQuery method
  6416. *
  6417. * Test postLink() with query string args.
  6418. *
  6419. * @return void
  6420. */
  6421. public function testPostLinkWithQuery()
  6422. {
  6423. $result = $this->Form->postLink(
  6424. 'Delete',
  6425. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6426. );
  6427. $expected = [
  6428. 'form' => [
  6429. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6430. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6431. ],
  6432. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6433. '/form',
  6434. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6435. 'Delete',
  6436. '/a',
  6437. ];
  6438. $this->assertHtml($expected, $result);
  6439. }
  6440. /**
  6441. * testPostLinkWithData method
  6442. *
  6443. * Test postLink with additional data.
  6444. *
  6445. * @return void
  6446. */
  6447. public function testPostLinkWithData()
  6448. {
  6449. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6450. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6451. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6452. $this->Form->create($entity);
  6453. $this->Form->end();
  6454. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6455. $this->assertStringContainsString(
  6456. '<input type="hidden" name="name" value="show"',
  6457. $result,
  6458. 'should not contain entity data.'
  6459. );
  6460. }
  6461. /**
  6462. * testPostLinkSecurityHash method
  6463. *
  6464. * Test that security hashes for postLink include the url.
  6465. *
  6466. * @return void
  6467. */
  6468. public function testPostLinkSecurityHash()
  6469. {
  6470. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6471. $hash .= '%3Aid';
  6472. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6473. $result = $this->Form->postLink(
  6474. 'Delete',
  6475. '/posts/delete/1',
  6476. ['data' => ['id' => 1]]
  6477. );
  6478. $tokenDebug = urlencode(json_encode([
  6479. '/posts/delete/1',
  6480. [
  6481. 'id' => 1,
  6482. ],
  6483. [],
  6484. ]));
  6485. $expected = [
  6486. 'form' => [
  6487. 'method' => 'post', 'action' => '/posts/delete/1',
  6488. 'name', 'style' => 'display:none;',
  6489. ],
  6490. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6491. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6492. 'div' => ['style' => 'display:none;'],
  6493. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6494. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6495. ['input' => [
  6496. 'type' => 'hidden',
  6497. 'name' => '_Token[debug]',
  6498. 'value' => $tokenDebug,
  6499. 'autocomplete' => 'off',
  6500. ]],
  6501. '/div',
  6502. '/form',
  6503. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6504. 'Delete',
  6505. '/a',
  6506. ];
  6507. $this->assertHtml($expected, $result);
  6508. }
  6509. /**
  6510. * testPostLinkSecurityHashBlockMode method
  6511. *
  6512. * Test that postLink doesn't modify the fields in the containing form.
  6513. *
  6514. * postLink() calls inside open forms should not modify the field list
  6515. * for the form.
  6516. *
  6517. * @return void
  6518. */
  6519. public function testPostLinkSecurityHashBlockMode()
  6520. {
  6521. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6522. $hash .= '%3A';
  6523. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6524. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6525. $this->Form->control('title');
  6526. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6527. $result = $this->View->fetch('postLink');
  6528. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6529. $this->assertEquals(['title'], $fields);
  6530. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6531. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6532. $reflect->setAccessible(true);
  6533. $this->assertEquals('/articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6534. }
  6535. /**
  6536. * testPostLinkSecurityHashNoDebugMode method
  6537. *
  6538. * Test that security does not include debug token if debug is false.
  6539. *
  6540. * @return void
  6541. */
  6542. public function testPostLinkSecurityHashNoDebugMode()
  6543. {
  6544. Configure::write('debug', false);
  6545. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6546. $hash .= '%3Aid';
  6547. $this->View->setRequest($this->View->getRequest()
  6548. ->withAttribute('formTokenData', ['key' => 'test']));
  6549. $result = $this->Form->postLink(
  6550. 'Delete',
  6551. '/posts/delete/1',
  6552. ['data' => ['id' => 1]]
  6553. );
  6554. $expected = [
  6555. 'form' => [
  6556. 'method' => 'post', 'action' => '/posts/delete/1',
  6557. 'name', 'style' => 'display:none;',
  6558. ],
  6559. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6560. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6561. 'div' => ['style' => 'display:none;'],
  6562. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6563. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6564. '/div',
  6565. '/form',
  6566. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6567. 'Delete',
  6568. '/a',
  6569. ];
  6570. $this->assertHtml($expected, $result);
  6571. }
  6572. /**
  6573. * testPostLinkNestedData method
  6574. *
  6575. * Test using postLink with N dimensional data.
  6576. *
  6577. * @return void
  6578. */
  6579. public function testPostLinkNestedData()
  6580. {
  6581. $data = [
  6582. 'one' => [
  6583. 'two' => [
  6584. 3, 4, 5,
  6585. ],
  6586. ],
  6587. ];
  6588. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6589. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6590. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6591. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6592. }
  6593. /**
  6594. * testPostLinkAfterGetForm method
  6595. *
  6596. * Test creating postLinks after a GET form.
  6597. *
  6598. * @return void
  6599. */
  6600. public function testPostLinkAfterGetForm()
  6601. {
  6602. $this->View->setRequest($this->View->getRequest()
  6603. ->withAttribute('csrfToken', 'testkey')
  6604. ->withAttribute('formTokenData', []));
  6605. $this->Form->create($this->article, ['type' => 'get']);
  6606. $this->Form->end();
  6607. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6608. $tokenDebug = urlencode(json_encode([
  6609. '/posts/delete/1',
  6610. [],
  6611. [],
  6612. ]));
  6613. $expected = [
  6614. 'form' => [
  6615. 'method' => 'post', 'action' => '/posts/delete/1',
  6616. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6617. ],
  6618. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6619. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6620. 'div' => ['style' => 'display:none;'],
  6621. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6622. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6623. ['input' => [
  6624. 'type' => 'hidden', 'name' => '_Token[debug]',
  6625. 'value' => $tokenDebug,
  6626. 'autocomplete' => 'off',
  6627. ]],
  6628. '/div',
  6629. '/form',
  6630. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6631. 'Delete',
  6632. '/a',
  6633. ];
  6634. $this->assertHtml($expected, $result);
  6635. }
  6636. /**
  6637. * testPostLinkFormBuffer method
  6638. *
  6639. * Test that postLink adds form tags to view block.
  6640. *
  6641. * @return void
  6642. */
  6643. public function testPostLinkFormBuffer()
  6644. {
  6645. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6646. $expected = [
  6647. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6648. 'Delete',
  6649. '/a',
  6650. ];
  6651. $this->assertHtml($expected, $result);
  6652. $result = $this->View->fetch('postLink');
  6653. $expected = [
  6654. 'form' => [
  6655. 'method' => 'post', 'action' => '/posts/delete/1',
  6656. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6657. ],
  6658. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6659. '/form',
  6660. ];
  6661. $this->assertHtml($expected, $result);
  6662. $result = $this->Form->postLink(
  6663. 'Delete',
  6664. '/posts/delete/2',
  6665. ['block' => true, 'method' => 'DELETE']
  6666. );
  6667. $expected = [
  6668. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6669. 'Delete',
  6670. '/a',
  6671. ];
  6672. $this->assertHtml($expected, $result);
  6673. $result = $this->View->fetch('postLink');
  6674. $expected = [
  6675. 'form' => [
  6676. 'method' => 'post', 'action' => '/posts/delete/1',
  6677. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6678. ],
  6679. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6680. '/form',
  6681. [
  6682. 'form' => [
  6683. 'method' => 'post', 'action' => '/posts/delete/2',
  6684. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6685. ],
  6686. ],
  6687. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6688. '/form',
  6689. ];
  6690. $this->assertHtml($expected, $result);
  6691. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6692. $expected = [
  6693. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6694. 'Delete',
  6695. '/a',
  6696. ];
  6697. $this->assertHtml($expected, $result);
  6698. $result = $this->View->fetch('foobar');
  6699. $expected = [
  6700. 'form' => [
  6701. 'method' => 'post', 'action' => '/posts/delete/1',
  6702. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6703. ],
  6704. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6705. '/form',
  6706. ];
  6707. $this->assertHtml($expected, $result);
  6708. }
  6709. /**
  6710. * testSubmitButton method
  6711. *
  6712. * @return void
  6713. */
  6714. public function testSubmitButton()
  6715. {
  6716. $result = $this->Form->submit('');
  6717. $expected = [
  6718. 'div' => ['class' => 'submit'],
  6719. 'input' => ['type' => 'submit', 'value' => ''],
  6720. '/div',
  6721. ];
  6722. $this->assertHtml($expected, $result);
  6723. $result = $this->Form->submit('Test Submit');
  6724. $expected = [
  6725. 'div' => ['class' => 'submit'],
  6726. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6727. '/div',
  6728. ];
  6729. $this->assertHtml($expected, $result);
  6730. $result = $this->Form->submit('Next >');
  6731. $expected = [
  6732. 'div' => ['class' => 'submit'],
  6733. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6734. '/div',
  6735. ];
  6736. $this->assertHtml($expected, $result);
  6737. $result = $this->Form->submit('Next >', ['escape' => false]);
  6738. $expected = [
  6739. 'div' => ['class' => 'submit'],
  6740. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6741. '/div',
  6742. ];
  6743. $this->assertHtml($expected, $result);
  6744. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6745. $expected = [
  6746. 'div' => ['class' => 'submit'],
  6747. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6748. '/div',
  6749. ];
  6750. $this->assertHtml($expected, $result);
  6751. }
  6752. /**
  6753. * testSubmitImage method
  6754. *
  6755. * Test image submit types.
  6756. *
  6757. * @return void
  6758. */
  6759. public function testSubmitImage()
  6760. {
  6761. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6762. $expected = [
  6763. 'div' => ['class' => 'submit'],
  6764. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6765. '/div',
  6766. ];
  6767. $this->assertHtml($expected, $result);
  6768. $result = $this->Form->submit('/relative/cake.power.gif');
  6769. $expected = [
  6770. 'div' => ['class' => 'submit'],
  6771. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6772. '/div',
  6773. ];
  6774. $this->assertHtml($expected, $result);
  6775. $result = $this->Form->submit('cake.power.gif');
  6776. $expected = [
  6777. 'div' => ['class' => 'submit'],
  6778. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6779. '/div',
  6780. ];
  6781. $this->assertHtml($expected, $result);
  6782. $result = $this->Form->submit('Not.an.image');
  6783. $expected = [
  6784. 'div' => ['class' => 'submit'],
  6785. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6786. '/div',
  6787. ];
  6788. $this->assertHtml($expected, $result);
  6789. }
  6790. /**
  6791. * testSubmitUnlockedByDefault method
  6792. *
  6793. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6794. * be submitted at a time.
  6795. *
  6796. * @return void
  6797. */
  6798. public function testSubmitUnlockedByDefault()
  6799. {
  6800. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6801. $this->Form->create();
  6802. $this->Form->submit('Go go');
  6803. $this->Form->submit('Save', ['name' => 'save']);
  6804. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6805. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6806. }
  6807. /**
  6808. * testSubmitImageTimestamp method
  6809. *
  6810. * Test submit image with timestamps.
  6811. *
  6812. * @return void
  6813. */
  6814. public function testSubmitImageTimestamp()
  6815. {
  6816. Configure::write('Asset.timestamp', 'force');
  6817. $result = $this->Form->submit('cake.power.gif');
  6818. $expected = [
  6819. 'div' => ['class' => 'submit'],
  6820. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6821. '/div',
  6822. ];
  6823. $this->assertHtml($expected, $result);
  6824. }
  6825. /**
  6826. * testDateTimeWithGetForms method
  6827. *
  6828. * Test that datetime() works with GET style forms.
  6829. *
  6830. * @return void
  6831. */
  6832. public function testDateTimeWithGetForms()
  6833. {
  6834. $this->Form->create($this->article, ['type' => 'get']);
  6835. $result = $this->Form->datetime('created');
  6836. $expected = [
  6837. 'input' => [
  6838. 'type' => 'datetime-local',
  6839. 'name' => 'created',
  6840. 'value' => '',
  6841. 'step' => '1',
  6842. ],
  6843. ];
  6844. $this->assertHtml($expected, $result);
  6845. $result = $this->Form->datetime('created', ['default' => true]);
  6846. $expected = [
  6847. 'input' => [
  6848. 'type' => 'datetime-local',
  6849. 'name' => 'created',
  6850. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}\.\d{3}/',
  6851. 'step' => '1',
  6852. ],
  6853. ];
  6854. $this->assertHtml($expected, $result);
  6855. }
  6856. /**
  6857. * testDateTimeWithFractional method
  6858. *
  6859. * Test that datetime() works with datetimefractional.
  6860. *
  6861. * @return void
  6862. */
  6863. public function testDateTimeWithFractional()
  6864. {
  6865. $this->Form->create([
  6866. 'schema' => [
  6867. 'created' => ['type' => 'datetimefractional'],
  6868. ],
  6869. ]);
  6870. $result = $this->Form->datetime('created', [
  6871. 'val' => new FrozenTime('2019-09-27 02:52:43.123'),
  6872. ]);
  6873. $expected = [
  6874. 'input' => [
  6875. 'type' => 'datetime-local',
  6876. 'name' => 'created',
  6877. 'value' => '2019-09-27T02:52:43.123',
  6878. 'step' => '0.001',
  6879. ],
  6880. ];
  6881. $this->assertHtml($expected, $result);
  6882. }
  6883. /**
  6884. * testControlWithFractional method
  6885. *
  6886. * Test that control() works with datetimefractional.
  6887. *
  6888. * @return void
  6889. */
  6890. public function testControlWithFractional()
  6891. {
  6892. $this->Form->create([
  6893. 'schema' => [
  6894. 'created' => ['type' => 'datetimefractional'],
  6895. ],
  6896. ]);
  6897. $result = $this->Form->control('created', [
  6898. 'val' => new FrozenTime('2019-09-27 02:52:43.123'),
  6899. ]);
  6900. $expected = [
  6901. 'div' => ['class' => 'input datetime'],
  6902. 'label' => ['for' => 'created'],
  6903. 'Created',
  6904. '/label',
  6905. 'input' => [
  6906. 'type' => 'datetime-local',
  6907. 'name' => 'created',
  6908. 'id' => 'created',
  6909. 'value' => '2019-09-27T02:52:43.123',
  6910. 'step' => '0.001',
  6911. ],
  6912. '/div',
  6913. ];
  6914. $this->assertHtml($expected, $result);
  6915. }
  6916. /**
  6917. * testForMagicControlNonExistingNorValidated method
  6918. *
  6919. * @return void
  6920. */
  6921. public function testForMagicControlNonExistingNorValidated()
  6922. {
  6923. $this->Form->create($this->article);
  6924. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6925. $result = $this->Form->control('non_existing_nor_validated');
  6926. $expected = [
  6927. 'label' => ['for' => 'non-existing-nor-validated'],
  6928. 'Non Existing Nor Validated',
  6929. '/label',
  6930. 'input' => [
  6931. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6932. 'id' => 'non-existing-nor-validated',
  6933. ],
  6934. ];
  6935. $this->assertHtml($expected, $result);
  6936. $result = $this->Form->control('non_existing_nor_validated', [
  6937. 'val' => 'my value',
  6938. ]);
  6939. $expected = [
  6940. 'label' => ['for' => 'non-existing-nor-validated'],
  6941. 'Non Existing Nor Validated',
  6942. '/label',
  6943. 'input' => [
  6944. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6945. 'value' => 'my value', 'id' => 'non-existing-nor-validated',
  6946. ],
  6947. ];
  6948. $this->assertHtml($expected, $result);
  6949. $this->View->setRequest(
  6950. $this->View->getRequest()->withData('non_existing_nor_validated', 'CakePHP magic')
  6951. );
  6952. $this->Form->create($this->article);
  6953. $result = $this->Form->control('non_existing_nor_validated');
  6954. $expected = [
  6955. 'label' => ['for' => 'non-existing-nor-validated'],
  6956. 'Non Existing Nor Validated',
  6957. '/label',
  6958. 'input' => [
  6959. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6960. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated',
  6961. ],
  6962. ];
  6963. $this->assertHtml($expected, $result);
  6964. }
  6965. /**
  6966. * testFormMagicControlLabel method
  6967. *
  6968. * @return void
  6969. */
  6970. public function testFormMagicControlLabel()
  6971. {
  6972. $this->getTableLocator()->get('Contacts', [
  6973. 'className' => ContactsTable::class,
  6974. ]);
  6975. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6976. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6977. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6978. $expected = [
  6979. 'label' => ['for' => 'contacts-name'],
  6980. 'My label',
  6981. '/label',
  6982. 'input' => [
  6983. 'type' => 'text', 'name' => 'Contacts[name]',
  6984. 'id' => 'contacts-name', 'maxlength' => '255',
  6985. ],
  6986. ];
  6987. $this->assertHtml($expected, $result);
  6988. $result = $this->Form->control('name', [
  6989. 'label' => ['class' => 'mandatory'],
  6990. ]);
  6991. $expected = [
  6992. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6993. 'Name',
  6994. '/label',
  6995. 'input' => [
  6996. 'type' => 'text', 'name' => 'name',
  6997. 'id' => 'name', 'maxlength' => '255',
  6998. ],
  6999. ];
  7000. $this->assertHtml($expected, $result);
  7001. $result = $this->Form->control('name', [
  7002. 'div' => false,
  7003. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  7004. ]);
  7005. $expected = [
  7006. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7007. 'My label',
  7008. '/label',
  7009. 'input' => [
  7010. 'type' => 'text', 'name' => 'name',
  7011. 'id' => 'name', 'maxlength' => '255',
  7012. ],
  7013. ];
  7014. $this->assertHtml($expected, $result);
  7015. $result = $this->Form->control('Contact.name', [
  7016. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  7017. ]);
  7018. $expected = [
  7019. 'label' => ['for' => 'my_id'],
  7020. 'Name',
  7021. '/label',
  7022. 'input' => [
  7023. 'type' => 'text', 'name' => 'Contact[name]',
  7024. 'id' => 'my_id', 'maxlength' => '255',
  7025. ],
  7026. ];
  7027. $this->assertHtml($expected, $result);
  7028. $result = $this->Form->control('1.id');
  7029. $expected = ['input' => [
  7030. 'type' => 'hidden', 'name' => '1[id]',
  7031. 'id' => '1-id',
  7032. ]];
  7033. $this->assertHtml($expected, $result);
  7034. $result = $this->Form->control('1.name');
  7035. $expected = [
  7036. 'label' => ['for' => '1-name'],
  7037. 'Name',
  7038. '/label',
  7039. 'input' => [
  7040. 'type' => 'text', 'name' => '1[name]',
  7041. 'id' => '1-name', 'maxlength' => '255',
  7042. ],
  7043. ];
  7044. $this->assertHtml($expected, $result);
  7045. }
  7046. /**
  7047. * testFormEnd method
  7048. *
  7049. * @return void
  7050. */
  7051. public function testFormEnd()
  7052. {
  7053. $this->assertSame('</form>', $this->Form->end());
  7054. }
  7055. /**
  7056. * testMultiRecordForm method
  7057. *
  7058. * Test the generation of fields for a multi record form.
  7059. *
  7060. * @return void
  7061. */
  7062. public function testMultiRecordForm()
  7063. {
  7064. $this->loadFixtures('Articles', 'Comments');
  7065. $articles = $this->getTableLocator()->get('Articles');
  7066. $articles->hasMany('Comments');
  7067. $comment = new Entity(['comment' => 'Value']);
  7068. $article = new Article(['comments' => [$comment]]);
  7069. $this->Form->create([$article]);
  7070. $result = $this->Form->control('0.comments.1.comment');
  7071. // phpcs:disable
  7072. $expected = [
  7073. 'div' => ['class' => 'input textarea'],
  7074. 'label' => ['for' => '0-comments-1-comment'],
  7075. 'Comment',
  7076. '/label',
  7077. 'textarea' => [
  7078. 'name',
  7079. 'id' => '0-comments-1-comment',
  7080. 'rows' => 5
  7081. ],
  7082. '/textarea',
  7083. '/div'
  7084. ];
  7085. // phpcs:enable
  7086. $this->assertHtml($expected, $result);
  7087. $result = $this->Form->control('0.comments.0.comment');
  7088. // phpcs:disable
  7089. $expected = [
  7090. 'div' => ['class' => 'input textarea'],
  7091. 'label' => ['for' => '0-comments-0-comment'],
  7092. 'Comment',
  7093. '/label',
  7094. 'textarea' => [
  7095. 'name',
  7096. 'id' => '0-comments-0-comment',
  7097. 'rows' => 5
  7098. ],
  7099. 'Value',
  7100. '/textarea',
  7101. '/div'
  7102. ];
  7103. // phpcs:enable
  7104. $this->assertHtml($expected, $result);
  7105. $comment->setError('comment', ['Not valid']);
  7106. $result = $this->Form->control('0.comments.0.comment');
  7107. // phpcs:disable
  7108. $expected = [
  7109. 'div' => ['class' => 'input textarea error'],
  7110. 'label' => ['for' => '0-comments-0-comment'],
  7111. 'Comment',
  7112. '/label',
  7113. 'textarea' => [
  7114. 'name',
  7115. 'class' => 'form-error',
  7116. 'id' => '0-comments-0-comment',
  7117. 'rows' => 5
  7118. ],
  7119. 'Value',
  7120. '/textarea',
  7121. ['div' => ['class' => 'error-message']],
  7122. 'Not valid',
  7123. '/div',
  7124. '/div'
  7125. ];
  7126. // phpcs:enable
  7127. $this->assertHtml($expected, $result);
  7128. $this->getTableLocator()->get('Comments')
  7129. ->getValidator('default')
  7130. ->allowEmptyString('comment', null, false);
  7131. $result = $this->Form->control('0.comments.1.comment');
  7132. // phpcs:disable
  7133. $expected = [
  7134. 'div' => ['class' => 'input textarea required'],
  7135. 'label' => ['for' => '0-comments-1-comment'],
  7136. 'Comment',
  7137. '/label',
  7138. 'textarea' => [
  7139. 'name',
  7140. 'required' => 'required',
  7141. 'id' => '0-comments-1-comment',
  7142. 'rows' => 5,
  7143. 'data-validity-message' => 'This field cannot be left empty',
  7144. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7145. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7146. ],
  7147. '/textarea',
  7148. '/div'
  7149. ];
  7150. // phpcs:enable
  7151. $this->assertHtml($expected, $result);
  7152. }
  7153. /**
  7154. * testHtml5Controls method
  7155. *
  7156. * Test that some html5 inputs + FormHelper::__call() work.
  7157. *
  7158. * @return void
  7159. */
  7160. public function testHtml5Controls()
  7161. {
  7162. $result = $this->Form->email('User.email');
  7163. $expected = [
  7164. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7165. ];
  7166. $this->assertHtml($expected, $result);
  7167. $result = $this->Form->search('User.query');
  7168. $expected = [
  7169. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7170. ];
  7171. $this->assertHtml($expected, $result);
  7172. $result = $this->Form->search('User.query', ['value' => 'test']);
  7173. $expected = [
  7174. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7175. ];
  7176. $this->assertHtml($expected, $result);
  7177. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7178. $expected = [
  7179. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7180. ];
  7181. $this->assertHtml($expected, $result);
  7182. }
  7183. /**
  7184. * testHtml5ControlWithControl method
  7185. *
  7186. * Test accessing html5 inputs through control().
  7187. *
  7188. * @return void
  7189. */
  7190. public function testHtml5ControlWithControl()
  7191. {
  7192. $this->Form->create();
  7193. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7194. $result = $this->Form->control('website', [
  7195. 'type' => 'url',
  7196. 'val' => 'http://domain.tld',
  7197. 'label' => false,
  7198. ]);
  7199. $expected = [
  7200. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7201. ];
  7202. $this->assertHtml($expected, $result);
  7203. }
  7204. /**
  7205. * testHtml5ControlException method
  7206. *
  7207. * Test errors when field name is missing.
  7208. *
  7209. * @return void
  7210. */
  7211. public function testHtml5ControlException()
  7212. {
  7213. $this->expectException(\Cake\Core\Exception\Exception::class);
  7214. $this->Form->email();
  7215. }
  7216. /**
  7217. * tests fields that are required use custom validation messages
  7218. *
  7219. * @return void
  7220. */
  7221. public function testHtml5ErrorMessage()
  7222. {
  7223. $this->Form->setConfig('autoSetCustomValidity', true);
  7224. $validator = (new \Cake\Validation\Validator())
  7225. ->notEmptyString('email', 'Custom error message')
  7226. ->requirePresence('password')
  7227. ->alphaNumeric('password')
  7228. ->notBlank('phone');
  7229. $table = $this->getTableLocator()->get('Contacts', [
  7230. 'className' => ContactsTable::class,
  7231. ]);
  7232. $table->setValidator('default', $validator);
  7233. $contact = new Entity();
  7234. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7235. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7236. $result = $this->Form->control('password');
  7237. $expected = [
  7238. 'label' => ['for' => 'password'],
  7239. 'Password',
  7240. '/label',
  7241. 'input' => [
  7242. 'id' => 'password',
  7243. 'name' => 'password',
  7244. 'type' => 'password',
  7245. 'value' => '',
  7246. 'required' => 'required',
  7247. 'data-validity-message' => 'This field cannot be left empty',
  7248. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7249. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7250. ],
  7251. ];
  7252. $this->assertHtml($expected, $result);
  7253. $result = $this->Form->control('phone');
  7254. $expected = [
  7255. 'label' => ['for' => 'phone'],
  7256. 'Phone',
  7257. '/label',
  7258. 'input' => [
  7259. 'id' => 'phone',
  7260. 'name' => 'phone',
  7261. 'type' => 'tel',
  7262. 'value' => '',
  7263. 'maxlength' => 255,
  7264. 'required' => 'required',
  7265. 'data-validity-message' => 'This field cannot be left empty',
  7266. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7267. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7268. ],
  7269. ];
  7270. $this->assertHtml($expected, $result);
  7271. $result = $this->Form->control('email');
  7272. $expected = [
  7273. 'label' => ['for' => 'email'],
  7274. 'Email',
  7275. '/label',
  7276. 'input' => [
  7277. 'id' => 'email',
  7278. 'name' => 'email',
  7279. 'type' => 'email',
  7280. 'value' => '',
  7281. 'maxlength' => 255,
  7282. 'required' => 'required',
  7283. 'data-validity-message' => 'Custom error message',
  7284. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7285. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7286. ],
  7287. ];
  7288. $this->assertHtml($expected, $result);
  7289. }
  7290. /**
  7291. * tests that custom validation messages are in templateVars
  7292. *
  7293. * @return void
  7294. */
  7295. public function testHtml5ErrorMessageInTemplateVars()
  7296. {
  7297. $validator = (new \Cake\Validation\Validator())
  7298. ->notEmptyString('email', 'Custom error "message" & entities')
  7299. ->requirePresence('password')
  7300. ->alphaNumeric('password')
  7301. ->notBlank('phone');
  7302. $table = $this->getTableLocator()->get('Contacts', [
  7303. 'className' => ContactsTable::class,
  7304. ]);
  7305. $table->setValidator('default', $validator);
  7306. $contact = new Entity();
  7307. $this->Form->setConfig('autoSetCustomValidity', false);
  7308. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7309. $this->Form->setTemplates([
  7310. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7311. 'inputContainer' => '{{content}}',
  7312. ]);
  7313. $result = $this->Form->control('password');
  7314. $expected = [
  7315. 'label' => ['for' => 'password'],
  7316. 'Password',
  7317. '/label',
  7318. 'input' => [
  7319. 'id' => 'password',
  7320. 'name' => 'password',
  7321. 'type' => 'password',
  7322. 'value' => '',
  7323. 'required' => 'required',
  7324. 'data-message' => 'This field cannot be left empty',
  7325. ],
  7326. ];
  7327. $this->assertHtml($expected, $result);
  7328. $result = $this->Form->control('phone');
  7329. $expected = [
  7330. 'label' => ['for' => 'phone'],
  7331. 'Phone',
  7332. '/label',
  7333. 'input' => [
  7334. 'id' => 'phone',
  7335. 'name' => 'phone',
  7336. 'type' => 'tel',
  7337. 'value' => '',
  7338. 'maxlength' => 255,
  7339. 'required' => 'required',
  7340. 'data-message' => 'This field cannot be left empty',
  7341. ],
  7342. ];
  7343. $this->assertHtml($expected, $result);
  7344. $result = $this->Form->control('email', [
  7345. 'templateVars' => [
  7346. 'custom' => 'data-custom="1"',
  7347. ],
  7348. ]);
  7349. $expected = [
  7350. 'label' => ['for' => 'email'],
  7351. 'Email',
  7352. '/label',
  7353. 'input' => [
  7354. 'id' => 'email',
  7355. 'name' => 'email',
  7356. 'type' => 'email',
  7357. 'value' => '',
  7358. 'maxlength' => 255,
  7359. 'required' => 'required',
  7360. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7361. 'data-custom' => '1',
  7362. ],
  7363. ];
  7364. $this->assertHtml($expected, $result);
  7365. $this->Form->setConfig('autoSetCustomValidity', true);
  7366. }
  7367. /**
  7368. * testRequiredAttribute method
  7369. *
  7370. * Tests that formhelper sets required attributes.
  7371. *
  7372. * @return void
  7373. */
  7374. public function testRequiredAttribute()
  7375. {
  7376. $this->article['required'] = [
  7377. 'title' => true,
  7378. 'body' => false,
  7379. ];
  7380. $this->Form->create($this->article);
  7381. $result = $this->Form->control('title');
  7382. $expected = [
  7383. 'div' => ['class' => 'input text required'],
  7384. 'label' => ['for' => 'title'],
  7385. 'Title',
  7386. '/label',
  7387. 'input' => [
  7388. 'type' => 'text',
  7389. 'name' => 'title',
  7390. 'id' => 'title',
  7391. 'required' => 'required',
  7392. 'data-validity-message' => 'This field cannot be left empty',
  7393. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7394. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7395. ],
  7396. '/div',
  7397. ];
  7398. $this->assertHtml($expected, $result);
  7399. $result = $this->Form->control('title', ['required' => false]);
  7400. $this->assertStringNotContainsString('required', $result);
  7401. $result = $this->Form->control('body');
  7402. $expected = [
  7403. 'div' => ['class' => 'input text'],
  7404. 'label' => ['for' => 'body'],
  7405. 'Body',
  7406. '/label',
  7407. 'input' => [
  7408. 'type' => 'text',
  7409. 'name' => 'body',
  7410. 'id' => 'body',
  7411. ],
  7412. '/div',
  7413. ];
  7414. $this->assertHtml($expected, $result);
  7415. $result = $this->Form->control('body', ['required' => true]);
  7416. $this->assertStringContainsString('required', $result);
  7417. }
  7418. /**
  7419. * testControlsNotNested method
  7420. *
  7421. * Tests that it is possible to put inputs outside of the label.
  7422. *
  7423. * @return void
  7424. */
  7425. public function testControlsNotNested()
  7426. {
  7427. $this->Form->setTemplates([
  7428. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7429. 'formGroup' => '{{input}}{{label}}',
  7430. ]);
  7431. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7432. $expected = [
  7433. 'div' => ['class' => 'input checkbox'],
  7434. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7435. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7436. 'label' => ['for' => 'foo'],
  7437. 'Foo',
  7438. '/label',
  7439. '/div',
  7440. ];
  7441. $this->assertHtml($expected, $result);
  7442. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7443. $expected = [
  7444. 'div' => ['class' => 'input checkbox'],
  7445. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7446. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7447. '/div',
  7448. ];
  7449. $this->assertHtml($expected, $result);
  7450. $result = $this->Form->control('confirm', [
  7451. 'type' => 'radio',
  7452. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7453. ]);
  7454. $expected = [
  7455. 'div' => ['class' => 'input radio'],
  7456. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7457. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7458. ['label' => ['for' => 'confirm-y']],
  7459. 'Yes',
  7460. '/label',
  7461. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7462. ['label' => ['for' => 'confirm-n']],
  7463. 'No',
  7464. '/label',
  7465. '<label',
  7466. 'Confirm',
  7467. '/label',
  7468. '/div',
  7469. ];
  7470. $this->assertHtml($expected, $result);
  7471. $result = $this->Form->select('category', ['1', '2'], [
  7472. 'multiple' => 'checkbox',
  7473. 'name' => 'fish',
  7474. ]);
  7475. $expected = [
  7476. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7477. ['div' => ['class' => 'checkbox']],
  7478. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7479. ['label' => ['for' => 'fish-0']],
  7480. '1',
  7481. '/label',
  7482. '/div',
  7483. ['div' => ['class' => 'checkbox']],
  7484. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7485. ['label' => ['for' => 'fish-1']],
  7486. '2',
  7487. '/label',
  7488. '/div',
  7489. ];
  7490. $this->assertHtml($expected, $result);
  7491. }
  7492. /**
  7493. * testControlContainerTemplates method
  7494. *
  7495. * Test that *Container templates are used by input.
  7496. *
  7497. * @return void
  7498. */
  7499. public function testControlContainerTemplates()
  7500. {
  7501. $this->Form->setTemplates([
  7502. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7503. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7504. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7505. ]);
  7506. $this->article['errors'] = [
  7507. 'Article' => ['published' => 'error message'],
  7508. ];
  7509. $this->Form->create($this->article);
  7510. $result = $this->Form->control('accept', [
  7511. 'type' => 'checkbox',
  7512. ]);
  7513. $expected = [
  7514. 'div' => ['class' => 'check'],
  7515. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7516. 'label' => ['for' => 'accept'],
  7517. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7518. 'Accept',
  7519. '/label',
  7520. '/div',
  7521. ];
  7522. $this->assertHtml($expected, $result);
  7523. $result = $this->Form->control('accept', [
  7524. 'type' => 'radio',
  7525. 'options' => ['Y', 'N'],
  7526. ]);
  7527. $this->assertStringContainsString('<div class="rad">', $result);
  7528. $result = $this->Form->control('Article.published', [
  7529. 'type' => 'radio',
  7530. 'options' => ['Y', 'N'],
  7531. ]);
  7532. $this->assertStringContainsString('<div class="rad err">', $result);
  7533. }
  7534. /**
  7535. * testFormGroupTemplates method
  7536. *
  7537. * Test that *Container templates are used by input.
  7538. *
  7539. * @return void
  7540. */
  7541. public function testFormGroupTemplates()
  7542. {
  7543. $this->Form->setTemplates([
  7544. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7545. ]);
  7546. $this->Form->create($this->article);
  7547. $result = $this->Form->control('accept', [
  7548. 'type' => 'radio',
  7549. 'options' => ['Y', 'N'],
  7550. ]);
  7551. $this->assertStringContainsString('<div class="radio">', $result);
  7552. }
  7553. /**
  7554. * testResetTemplates method
  7555. *
  7556. * Test resetting templates.
  7557. *
  7558. * @return void
  7559. */
  7560. public function testResetTemplates()
  7561. {
  7562. $this->Form->setTemplates(['input' => '<input/>']);
  7563. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7564. $this->Form->resetTemplates();
  7565. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7566. }
  7567. /**
  7568. * testContext method
  7569. *
  7570. * Test the context method.
  7571. *
  7572. * @return void
  7573. */
  7574. public function testContext()
  7575. {
  7576. $result = $this->Form->context();
  7577. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7578. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7579. $this->assertSame($mock, $this->Form->context($mock));
  7580. $this->assertSame($mock, $this->Form->context());
  7581. }
  7582. /**
  7583. * testAutoDomId method
  7584. *
  7585. * @return void
  7586. */
  7587. public function testAutoDomId()
  7588. {
  7589. $result = $this->Form->text('field', ['id' => true]);
  7590. $expected = [
  7591. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7592. ];
  7593. $this->assertHtml($expected, $result);
  7594. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7595. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7596. $expected = [
  7597. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7598. ['label' => ['for' => 'field-0']],
  7599. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7600. 'option A',
  7601. '/label',
  7602. ['label' => ['for' => 'field-1']],
  7603. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7604. 'option B',
  7605. '/label',
  7606. ];
  7607. $this->assertHtml($expected, $result);
  7608. $result = $this->Form->select(
  7609. 'multi_field',
  7610. ['first', 'second'],
  7611. ['multiple' => 'checkbox', 'id' => true]
  7612. );
  7613. $expected = [
  7614. 'input' => [
  7615. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '',
  7616. ],
  7617. ['div' => ['class' => 'checkbox']],
  7618. ['label' => ['for' => 'multi-field-0']],
  7619. ['input' => [
  7620. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7621. 'value' => '0', 'id' => 'multi-field-0',
  7622. ]],
  7623. 'first',
  7624. '/label',
  7625. '/div',
  7626. ['div' => ['class' => 'checkbox']],
  7627. ['label' => ['for' => 'multi-field-1']],
  7628. ['input' => [
  7629. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7630. 'value' => '1', 'id' => 'multi-field-1',
  7631. ]],
  7632. 'second',
  7633. '/label',
  7634. '/div',
  7635. ];
  7636. $this->assertHtml($expected, $result);
  7637. }
  7638. /**
  7639. * Test the basic setters and getters for value sources
  7640. *
  7641. * @return void
  7642. */
  7643. public function testFormValueSourcesSettersGetters()
  7644. {
  7645. $this->View->setRequest($this->View->getRequest()
  7646. ->withData('id', '1')
  7647. ->withQueryParams(['id' => '2']));
  7648. $expected = ['context'];
  7649. $result = $this->Form->getValueSources();
  7650. $this->assertEquals($expected, $result);
  7651. $expected = ['query', 'data', 'context'];
  7652. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7653. $result = $this->Form->getValueSources();
  7654. $this->assertEquals($expected, $result);
  7655. $this->Form->setValueSources(['context']);
  7656. $expected = '1';
  7657. $result = $this->Form->getSourceValue('id');
  7658. $this->assertEquals($expected, $result);
  7659. $this->Form->setValueSources('query');
  7660. $expected = ['query'];
  7661. $result = $this->Form->getValueSources();
  7662. $this->assertEquals($expected, $result);
  7663. $expected = '2';
  7664. $result = $this->Form->getSourceValue('id');
  7665. $this->assertEquals($expected, $result);
  7666. $this->Form->setValueSources(['data']);
  7667. $expected = '1';
  7668. $result = $this->Form->getSourceValue('id');
  7669. $this->assertEquals($expected, $result);
  7670. $this->Form->setValueSources(['query', 'data']);
  7671. $expected = '2';
  7672. $result = $this->Form->getSourceValue('id');
  7673. $this->assertEquals($expected, $result);
  7674. }
  7675. /**
  7676. * Tests the different input rendering values based on sources values switching
  7677. *
  7678. * @return void
  7679. */
  7680. public function testFormValueSourcesSingleSwitchRendering()
  7681. {
  7682. $this->loadFixtures('Articles');
  7683. $articles = $this->getTableLocator()->get('Articles');
  7684. $article = new Article();
  7685. $articles->patchEntity($article, ['id' => '3']);
  7686. $this->Form->create($article);
  7687. $this->Form->setValueSources(['context']);
  7688. $result = $this->Form->control('id');
  7689. $expected = [
  7690. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7691. ];
  7692. $this->assertHtml($expected, $result);
  7693. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7694. $this->Form->setValueSources(['query']);
  7695. $result = $this->Form->control('id');
  7696. $expected = [
  7697. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7698. ];
  7699. $this->assertHtml($expected, $result);
  7700. $this->View->setRequest($this->View->getRequest()
  7701. ->withQueryParams(['id' => '5a'])
  7702. ->withData('id', '5b'));
  7703. $this->Form->setValueSources(['context']);
  7704. $this->Form->create($article);
  7705. $result = $this->Form->control('id');
  7706. $expected = [
  7707. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7708. ];
  7709. $this->assertHtml($expected, $result);
  7710. $this->Form->setValueSources(['data']);
  7711. $this->Form->create($article);
  7712. $result = $this->Form->control('id');
  7713. $expected = [
  7714. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7715. ];
  7716. $this->assertHtml($expected, $result);
  7717. $this->Form->setValueSources(['query']);
  7718. $result = $this->Form->control('id');
  7719. $expected = [
  7720. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7721. ];
  7722. $this->assertHtml($expected, $result);
  7723. }
  7724. /**
  7725. * Tests the different input rendering values based on sources values switching while supplying
  7726. * an entity (base context) and multiple sources (such as data, query)
  7727. *
  7728. * @return void
  7729. */
  7730. public function testFormValueSourcesListSwitchRendering()
  7731. {
  7732. $this->loadFixtures('Articles');
  7733. $articles = $this->getTableLocator()->get('Articles');
  7734. $article = new Article();
  7735. $articles->patchEntity($article, ['id' => '3']);
  7736. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7737. $this->Form->create($article);
  7738. $this->Form->setValueSources(['context', 'query']);
  7739. $result = $this->Form->control('id');
  7740. $expected = [
  7741. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7742. ];
  7743. $this->assertHtml($expected, $result);
  7744. $this->Form->setValueSources(['query', 'context']);
  7745. $result = $this->Form->control('id');
  7746. $expected = [
  7747. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7748. ];
  7749. $this->assertHtml($expected, $result);
  7750. $this->Form->setValueSources(['data', 'query', 'context']);
  7751. $result = $this->Form->control('id');
  7752. $expected = [
  7753. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7754. ];
  7755. $this->assertHtml($expected, $result);
  7756. $this->View->setRequest($this->View->getRequest()
  7757. ->withData('id', '8')
  7758. ->withQueryParams(['id' => '9']));
  7759. $this->Form->setValueSources(['data', 'query', 'context']);
  7760. $result = $this->Form->control('id');
  7761. $expected = [
  7762. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7763. ];
  7764. $this->assertHtml($expected, $result);
  7765. }
  7766. /**
  7767. * Test the different form input renderings based on values sources switchings through form options
  7768. *
  7769. * @return void
  7770. */
  7771. public function testFormValueSourcesSwitchViaOptionsRendering()
  7772. {
  7773. $this->loadFixtures('Articles');
  7774. $articles = $this->getTableLocator()->get('Articles');
  7775. $article = new Article();
  7776. $articles->patchEntity($article, ['id' => '3']);
  7777. $this->View->setRequest(
  7778. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7779. );
  7780. $this->Form->create($article, ['valueSources' => 'query']);
  7781. $result = $this->Form->control('id');
  7782. $expected = [
  7783. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7784. ];
  7785. $this->assertHtml($expected, $result);
  7786. $result = $this->Form->getSourceValue('id');
  7787. $this->assertSame('5', $result);
  7788. $this->Form->setValueSources(['context']);
  7789. $this->Form->create($article, ['valueSources' => 'query']);
  7790. $result = $this->Form->control('id');
  7791. $expected = [
  7792. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7793. ];
  7794. $this->assertHtml($expected, $result);
  7795. $result = $this->Form->getSourceValue('id');
  7796. $this->assertSame('5', $result);
  7797. $this->Form->setValueSources(['query']);
  7798. $this->Form->create($article, ['valueSources' => 'data']);
  7799. $result = $this->Form->control('id');
  7800. $expected = [
  7801. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7802. ];
  7803. $this->assertHtml($expected, $result);
  7804. $result = $this->Form->getSourceValue('id');
  7805. $this->assertSame('4', $result);
  7806. $this->Form->setValueSources(['query']);
  7807. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7808. $result = $this->Form->control('id');
  7809. $expected = [
  7810. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7811. ];
  7812. $this->assertHtml($expected, $result);
  7813. $result = $this->Form->getSourceValue('id');
  7814. $this->assertSame('4', $result);
  7815. }
  7816. /**
  7817. * Test the different form input renderings based on values sources switchings through form options
  7818. *
  7819. * @return void
  7820. */
  7821. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  7822. {
  7823. $this->loadFixtures('Articles');
  7824. $articles = $this->getTableLocator()->get('Articles');
  7825. $article = new Article();
  7826. $articles->patchEntity($article, ['id' => '3']);
  7827. $this->View->setRequest(
  7828. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7829. );
  7830. $this->Form->setValueSources(['context'])
  7831. ->create($article, ['valueSources' => ['query', 'data']]);
  7832. $result = $this->Form->control('id');
  7833. $expected = [
  7834. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7835. ];
  7836. $this->assertHtml($expected, $result);
  7837. $result = $this->Form->getSourceValue('id');
  7838. $this->assertSame('11', $result);
  7839. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7840. $this->Form->setValueSources(['context'])
  7841. ->create($article, ['valueSources' => ['query', 'data']]);
  7842. $result = $this->Form->control('id');
  7843. $expected = [
  7844. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7845. ];
  7846. $this->assertHtml($expected, $result);
  7847. $result = $this->Form->getSourceValue('id');
  7848. $this->assertSame('10', $result);
  7849. }
  7850. /**
  7851. * Test the different form values sources resetting through From::end();
  7852. *
  7853. * @return void
  7854. */
  7855. public function testFormValueSourcesResetViaEnd()
  7856. {
  7857. $expected = ['context'];
  7858. $result = $this->Form->getValueSources();
  7859. $this->assertEquals($expected, $result);
  7860. $expected = ['query', 'context', 'data'];
  7861. $this->Form->setValueSources(['query', 'context', 'data']);
  7862. $result = $this->Form->getValueSources();
  7863. $this->assertEquals($expected, $result);
  7864. $this->Form->create();
  7865. $result = $this->Form->getValueSources();
  7866. $this->assertEquals($expected, $result);
  7867. $this->Form->end();
  7868. $result = $this->Form->getValueSources();
  7869. $this->assertEquals(['context'], $result);
  7870. }
  7871. /**
  7872. * Test sources values defaults handling
  7873. *
  7874. * @return void
  7875. */
  7876. public function testFormValueSourcesDefaults()
  7877. {
  7878. $this->View->setRequest(
  7879. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7880. );
  7881. $this->Form->create();
  7882. $result = $this->Form->password('password');
  7883. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7884. $this->assertHtml($expected, $result);
  7885. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7886. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7887. $this->assertHtml($expected, $result);
  7888. $this->Form->setValueSources('query');
  7889. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7890. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7891. $this->assertHtml($expected, $result);
  7892. $this->Form->setValueSources('data');
  7893. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7894. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7895. $this->assertHtml($expected, $result);
  7896. }
  7897. /**
  7898. * Test sources values schema defaults handling
  7899. *
  7900. * @return void
  7901. */
  7902. public function testSourcesValueDoesntExistPassThrough()
  7903. {
  7904. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7905. $articles = $this->getTableLocator()->get('Articles');
  7906. $entity = $articles->newEmptyEntity();
  7907. $this->Form->create($entity);
  7908. $this->Form->setValueSources(['query', 'context']);
  7909. $result = $this->Form->getSourceValue('category');
  7910. $this->assertSame('sesame-cookies', $result);
  7911. $this->Form->setValueSources(['context', 'query']);
  7912. $result = $this->Form->getSourceValue('category');
  7913. $this->assertSame('sesame-cookies', $result);
  7914. }
  7915. /**
  7916. * testNestedLabelInput method
  7917. *
  7918. * Test the `nestedInput` parameter
  7919. *
  7920. * @return void
  7921. */
  7922. public function testNestedLabelInput()
  7923. {
  7924. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7925. $expected = [
  7926. 'div' => ['class' => 'input text'],
  7927. 'label' => ['for' => 'foo'],
  7928. ['input' => [
  7929. 'type' => 'text',
  7930. 'name' => 'foo',
  7931. 'id' => 'foo',
  7932. ]],
  7933. 'Foo',
  7934. '/label',
  7935. '/div',
  7936. ];
  7937. $this->assertHtml($expected, $result);
  7938. }
  7939. /**
  7940. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7941. *
  7942. * This test makes sure `false` excludes the label from the render
  7943. *
  7944. * @return void
  7945. */
  7946. public function testControlLabelManipulationDisableLabels()
  7947. {
  7948. $result = $this->Form->control('test', [
  7949. 'type' => 'radio',
  7950. 'options' => ['A', 'B'],
  7951. 'labelOptions' => false,
  7952. ]);
  7953. $expected = [
  7954. ['div' => ['class' => 'input radio']],
  7955. '<label',
  7956. 'Test',
  7957. '/label',
  7958. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7959. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7960. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7961. '/div',
  7962. ];
  7963. $this->assertHtml($expected, $result);
  7964. $result = $this->Form->control('checkbox1', [
  7965. 'label' => 'My checkboxes',
  7966. 'multiple' => 'checkbox',
  7967. 'type' => 'select',
  7968. 'options' => [
  7969. ['text' => 'First Checkbox', 'value' => 1],
  7970. ['text' => 'Second Checkbox', 'value' => 2],
  7971. ],
  7972. 'labelOptions' => false,
  7973. ]);
  7974. $expected = [
  7975. ['div' => ['class' => 'input select']],
  7976. ['label' => ['for' => 'checkbox1']],
  7977. 'My checkboxes',
  7978. '/label',
  7979. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7980. ['div' => ['class' => 'checkbox']],
  7981. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7982. '/div',
  7983. ['div' => ['class' => 'checkbox']],
  7984. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7985. '/div',
  7986. '/div',
  7987. ];
  7988. $this->assertHtml($expected, $result);
  7989. }
  7990. /**
  7991. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7992. *
  7993. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7994. * added to the class if checked.
  7995. *
  7996. * Also checks to make sure any custom attributes are rendered correctly
  7997. *
  7998. * @return void
  7999. */
  8000. public function testControlLabelManipulationRadios()
  8001. {
  8002. $result = $this->Form->control('test', [
  8003. 'type' => 'radio',
  8004. 'options' => ['A', 'B'],
  8005. 'labelOptions' => ['class' => 'custom-class'],
  8006. ]);
  8007. $expected = [
  8008. ['div' => ['class' => 'input radio']],
  8009. '<label',
  8010. 'Test',
  8011. '/label',
  8012. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8013. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8014. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8015. 'A',
  8016. '/label',
  8017. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8018. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8019. 'B',
  8020. '/label',
  8021. '/div',
  8022. ];
  8023. $this->assertHtml($expected, $result);
  8024. $result = $this->Form->control('test', [
  8025. 'type' => 'radio',
  8026. 'options' => ['A', 'B'],
  8027. 'value' => 1,
  8028. 'labelOptions' => ['class' => 'custom-class'],
  8029. ]);
  8030. $expected = [
  8031. ['div' => ['class' => 'input radio']],
  8032. '<label',
  8033. 'Test',
  8034. '/label',
  8035. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8036. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8037. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8038. 'A',
  8039. '/label',
  8040. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8041. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8042. 'B',
  8043. '/label',
  8044. '/div',
  8045. ];
  8046. $this->assertHtml($expected, $result);
  8047. $result = $this->Form->control('test', [
  8048. 'type' => 'radio',
  8049. 'options' => ['A', 'B'],
  8050. 'value' => 1,
  8051. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  8052. ]);
  8053. $expected = [
  8054. ['div' => ['class' => 'input radio']],
  8055. '<label',
  8056. 'Test',
  8057. '/label',
  8058. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8059. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8060. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8061. 'A',
  8062. '/label',
  8063. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8064. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8065. 'B',
  8066. '/label',
  8067. '/div',
  8068. ];
  8069. $this->assertHtml($expected, $result);
  8070. $result = $this->Form->radio('test', ['A', 'B'], [
  8071. 'label' => [
  8072. 'class' => ['custom-class', 'another-class'],
  8073. 'data-name' => 'bob',
  8074. ],
  8075. 'value' => 1,
  8076. ]);
  8077. $expected = [
  8078. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8079. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8080. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8081. 'A',
  8082. '/label',
  8083. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8084. ['input' => [
  8085. 'type' => 'radio',
  8086. 'name' => 'test',
  8087. 'value' => '1',
  8088. 'id' => 'test-1',
  8089. 'checked' => 'checked',
  8090. ]],
  8091. 'B',
  8092. '/label',
  8093. ];
  8094. $this->assertHtml($expected, $result);
  8095. }
  8096. /**
  8097. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8098. *
  8099. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8100. * added to the class if checked.
  8101. *
  8102. * Also checks to make sure any custom attributes are rendered correctly
  8103. *
  8104. * @return void
  8105. */
  8106. public function testControlLabelManipulationCheckboxes()
  8107. {
  8108. $result = $this->Form->control('checkbox1', [
  8109. 'label' => 'My checkboxes',
  8110. 'multiple' => 'checkbox',
  8111. 'type' => 'select',
  8112. 'options' => [
  8113. ['text' => 'First Checkbox', 'value' => 1],
  8114. ['text' => 'Second Checkbox', 'value' => 2],
  8115. ],
  8116. 'labelOptions' => ['class' => 'custom-class'],
  8117. 'value' => ['1'],
  8118. ]);
  8119. $expected = [
  8120. ['div' => ['class' => 'input select']],
  8121. ['label' => ['for' => 'checkbox1']],
  8122. 'My checkboxes',
  8123. '/label',
  8124. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8125. ['div' => ['class' => 'checkbox']],
  8126. ['label' => [
  8127. 'class' => 'custom-class selected',
  8128. 'for' => 'checkbox1-1',
  8129. ]],
  8130. ['input' => [
  8131. 'type' => 'checkbox',
  8132. 'name' => 'checkbox1[]',
  8133. 'value' => '1',
  8134. 'id' => 'checkbox1-1',
  8135. 'checked' => 'checked',
  8136. ]],
  8137. 'First Checkbox',
  8138. '/label',
  8139. '/div',
  8140. ['div' => ['class' => 'checkbox']],
  8141. ['label' => [
  8142. 'class' => 'custom-class',
  8143. 'for' => 'checkbox1-2',
  8144. ]],
  8145. ['input' => [
  8146. 'type' => 'checkbox',
  8147. 'name' => 'checkbox1[]',
  8148. 'value' => '2',
  8149. 'id' => 'checkbox1-2',
  8150. ]],
  8151. 'Second Checkbox',
  8152. '/label',
  8153. '/div',
  8154. '/div',
  8155. ];
  8156. $this->assertHtml($expected, $result);
  8157. $result = $this->Form->control('checkbox1', [
  8158. 'label' => 'My checkboxes',
  8159. 'multiple' => 'checkbox',
  8160. 'type' => 'select',
  8161. 'options' => [
  8162. ['text' => 'First Checkbox', 'value' => 1],
  8163. ['text' => 'Second Checkbox', 'value' => 2],
  8164. ],
  8165. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8166. 'value' => ['1'],
  8167. ]);
  8168. $expected = [
  8169. ['div' => ['class' => 'input select']],
  8170. ['label' => ['for' => 'checkbox1']],
  8171. 'My checkboxes',
  8172. '/label',
  8173. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8174. ['div' => ['class' => 'checkbox']],
  8175. ['label' => [
  8176. 'class' => 'custom-class another-class selected',
  8177. 'data-name' => 'bob',
  8178. 'for' => 'checkbox1-1',
  8179. ]],
  8180. ['input' => [
  8181. 'type' => 'checkbox',
  8182. 'name' => 'checkbox1[]',
  8183. 'value' => '1',
  8184. 'id' => 'checkbox1-1',
  8185. 'checked' => 'checked',
  8186. ]],
  8187. 'First Checkbox',
  8188. '/label',
  8189. '/div',
  8190. ['div' => ['class' => 'checkbox']],
  8191. ['label' => [
  8192. 'class' => 'custom-class another-class',
  8193. 'data-name' => 'bob',
  8194. 'for' => 'checkbox1-2',
  8195. ]],
  8196. ['input' => [
  8197. 'type' => 'checkbox',
  8198. 'name' => 'checkbox1[]',
  8199. 'value' => '2',
  8200. 'id' => 'checkbox1-2',
  8201. ]],
  8202. 'Second Checkbox',
  8203. '/label',
  8204. '/div',
  8205. '/div',
  8206. ];
  8207. $this->assertHtml($expected, $result);
  8208. }
  8209. /**
  8210. * testControlMaxLengthArrayContext method
  8211. *
  8212. * Test control() with maxlength attribute in Array Context.
  8213. *
  8214. * @return void
  8215. */
  8216. public function testControlMaxLengthArrayContext()
  8217. {
  8218. $this->article['schema'] = [
  8219. 'title' => ['length' => 10],
  8220. ];
  8221. $this->Form->create($this->article);
  8222. $result = $this->Form->control('title');
  8223. $expected = [
  8224. 'div' => ['class'],
  8225. 'label' => ['for'],
  8226. 'Title',
  8227. '/label',
  8228. 'input' => [
  8229. 'id',
  8230. 'name' => 'title',
  8231. 'type' => 'text',
  8232. 'required' => 'required',
  8233. 'maxlength' => 10,
  8234. 'data-validity-message' => 'This field cannot be left empty',
  8235. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8236. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8237. ],
  8238. '/div',
  8239. ];
  8240. $this->assertHtml($expected, $result);
  8241. }
  8242. /**
  8243. * testControlMaxLengthEntityContext method
  8244. *
  8245. * Test control() with maxlength attribute in Entity Context.
  8246. *
  8247. * @return void
  8248. */
  8249. public function testControlMaxLengthEntityContext()
  8250. {
  8251. $this->article['schema']['title']['length'] = 45;
  8252. $validator = new Validator();
  8253. $validator->maxLength('title', 10);
  8254. $article = new EntityContext(
  8255. new ServerRequest(),
  8256. [
  8257. 'entity' => new Entity($this->article),
  8258. 'table' => new Table([
  8259. 'alias' => 'Articles',
  8260. 'schema' => $this->article['schema'],
  8261. 'validator' => $validator,
  8262. ]),
  8263. ]
  8264. );
  8265. $this->Form->create($article);
  8266. $result = $this->Form->control('title');
  8267. $expected = [
  8268. 'div' => ['class'],
  8269. 'label' => ['for'],
  8270. 'Title',
  8271. '/label',
  8272. 'input' => [
  8273. 'id',
  8274. 'name' => 'title',
  8275. 'type' => 'text',
  8276. 'required' => 'required',
  8277. 'maxlength' => 10,
  8278. 'data-validity-message' => 'This field cannot be left empty',
  8279. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8280. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8281. ],
  8282. '/div',
  8283. ];
  8284. $this->assertHtml($expected, $result);
  8285. $this->article['schema']['title']['length'] = 45;
  8286. $validator = new Validator();
  8287. $validator->maxLength('title', 55);
  8288. $article = new EntityContext(
  8289. new ServerRequest(),
  8290. [
  8291. 'entity' => new Entity($this->article),
  8292. 'table' => new Table([
  8293. 'schema' => $this->article['schema'],
  8294. 'validator' => $validator,
  8295. 'alias' => 'Articles',
  8296. ]),
  8297. ]
  8298. );
  8299. $this->Form->create($article);
  8300. $result = $this->Form->control('title');
  8301. $expected = [
  8302. 'div' => ['class'],
  8303. 'label' => ['for'],
  8304. 'Title',
  8305. '/label',
  8306. 'input' => [
  8307. 'id',
  8308. 'name' => 'title',
  8309. 'type' => 'text',
  8310. 'required' => 'required',
  8311. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8312. 'data-validity-message' => 'This field cannot be left empty',
  8313. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8314. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8315. ],
  8316. '/div',
  8317. ];
  8318. $this->assertHtml($expected, $result);
  8319. $this->article['schema']['title']['length'] = 45;
  8320. $validator = new Validator();
  8321. $validator->maxLength('title', 55);
  8322. $article = new EntityContext(
  8323. new ServerRequest(),
  8324. [
  8325. 'entity' => new Entity($this->article),
  8326. 'table' => new Table([
  8327. 'schema' => $this->article['schema'],
  8328. 'validator' => $validator,
  8329. 'alias' => 'Articles',
  8330. ]),
  8331. ]
  8332. );
  8333. $this->Form->create($article);
  8334. $result = $this->Form->control('title', ['maxlength' => 10]);
  8335. $expected = [
  8336. 'div' => ['class'],
  8337. 'label' => ['for'],
  8338. 'Title',
  8339. '/label',
  8340. 'input' => [
  8341. 'id',
  8342. 'name' => 'title',
  8343. 'type' => 'text',
  8344. 'required' => 'required',
  8345. 'maxlength' => 10, // Length set in options should take highest precedence.
  8346. 'data-validity-message' => 'This field cannot be left empty',
  8347. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8348. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8349. ],
  8350. '/div',
  8351. ];
  8352. $this->assertHtml($expected, $result);
  8353. }
  8354. /**
  8355. * testControlMinMaxLengthEntityContext method
  8356. *
  8357. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8358. *
  8359. * @return void
  8360. */
  8361. public function testControlMinMaxLengthEntityContext()
  8362. {
  8363. $validator = new Validator();
  8364. $validator->maxLength('title', 10);
  8365. $article = new EntityContext(
  8366. new ServerRequest(),
  8367. [
  8368. 'entity' => new Entity($this->article),
  8369. 'table' => new Table([
  8370. 'alias' => 'Articles',
  8371. 'schema' => $this->article['schema'],
  8372. 'validator' => $validator,
  8373. ]),
  8374. ]
  8375. );
  8376. $this->Form->create($article);
  8377. $result = $this->Form->control('title');
  8378. $expected = [
  8379. 'div' => ['class'],
  8380. 'label' => ['for'],
  8381. 'Title',
  8382. '/label',
  8383. 'input' => [
  8384. 'id',
  8385. 'name' => 'title',
  8386. 'type' => 'text',
  8387. 'required' => 'required',
  8388. 'maxlength' => 10,
  8389. 'data-validity-message' => 'This field cannot be left empty',
  8390. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8391. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8392. ],
  8393. '/div',
  8394. ];
  8395. $this->assertHtml($expected, $result);
  8396. }
  8397. /**
  8398. * testControlMaxLengthFormContext method
  8399. *
  8400. * Test control() with maxlength attribute in Form Context.
  8401. *
  8402. * @return void
  8403. */
  8404. public function testControlMaxLengthFormContext()
  8405. {
  8406. $validator = new Validator();
  8407. $validator->maxLength('title', 10);
  8408. $form = new Form();
  8409. $form->setValidator('default', $validator);
  8410. $this->Form->create($form);
  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. }