FormHelperTest.php 300 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View\Helper;
  17. use Cake\Collection\Collection;
  18. use Cake\Core\Configure;
  19. use Cake\Form\Form;
  20. use Cake\Http\ServerRequest;
  21. use Cake\I18n\Date;
  22. use Cake\I18n\FrozenTime;
  23. use Cake\ORM\Entity;
  24. use Cake\ORM\Table;
  25. use Cake\Routing\Router;
  26. use Cake\TestSuite\TestCase;
  27. use Cake\Utility\Security;
  28. use Cake\Validation\Validator;
  29. use Cake\View\Form\EntityContext;
  30. use Cake\View\Helper\FormHelper;
  31. use Cake\View\View;
  32. use Cake\View\Widget\WidgetLocator;
  33. use ReflectionProperty;
  34. use TestApp\Model\Entity\Article;
  35. use TestApp\Model\Table\ContactsTable;
  36. use TestApp\Model\Table\ValidateUsersTable;
  37. /**
  38. * FormHelperTest class
  39. *
  40. * @property \Cake\View\Helper\FormHelper $Form
  41. * @property \Cake\View\View $View
  42. */
  43. class FormHelperTest extends TestCase
  44. {
  45. /**
  46. * Fixtures to be used
  47. *
  48. * @var array
  49. */
  50. protected $fixtures = ['core.Articles', 'core.Comments'];
  51. /**
  52. * Do not load the fixtures by default
  53. *
  54. * @var bool
  55. */
  56. public $autoFixtures = false;
  57. /**
  58. * @var array
  59. */
  60. protected $article = [];
  61. /**
  62. * @var string
  63. */
  64. protected $url;
  65. /**
  66. * setUp method
  67. *
  68. * @return void
  69. */
  70. public function setUp(): void
  71. {
  72. parent::setUp();
  73. Configure::write('Config.language', 'eng');
  74. Configure::write('App.base', '');
  75. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  76. $request = new ServerRequest([
  77. 'webroot' => '',
  78. 'base' => '',
  79. 'url' => '/articles/add',
  80. 'params' => [
  81. 'controller' => 'articles',
  82. 'action' => 'add',
  83. 'plugin' => null,
  84. ],
  85. ]);
  86. $this->View = new View($request);
  87. Router::reload();
  88. Router::setRequest($request);
  89. $this->url = '/articles/add';
  90. $this->Form = new FormHelper($this->View);
  91. $this->dateRegex = [
  92. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  93. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  94. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  95. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  96. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  97. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  98. ];
  99. $this->article = [
  100. 'schema' => [
  101. 'id' => ['type' => 'integer'],
  102. 'author_id' => ['type' => 'integer', 'null' => true],
  103. 'title' => ['type' => 'string', 'null' => true],
  104. 'body' => 'text',
  105. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  106. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
  107. ],
  108. 'required' => [
  109. 'author_id' => true,
  110. 'title' => true,
  111. ],
  112. ];
  113. Security::setSalt('foo!');
  114. Router::connect('/:controller', ['action' => 'index']);
  115. Router::connect('/:controller/:action/*');
  116. }
  117. /**
  118. * tearDown method
  119. *
  120. * @return void
  121. */
  122. public function tearDown(): void
  123. {
  124. parent::tearDown();
  125. unset($this->Form, $this->Controller, $this->View);
  126. $this->getTableLocator()->clear();
  127. }
  128. /**
  129. * Test construct() with the templates option.
  130. *
  131. * @return void
  132. */
  133. public function testConstructTemplatesFile()
  134. {
  135. $helper = new FormHelper($this->View, [
  136. 'templates' => 'htmlhelper_tags',
  137. ]);
  138. $result = $helper->control('name');
  139. $this->assertStringContainsString('<input', $result);
  140. }
  141. /**
  142. * Test that when specifying custom widgets the config array for that widget
  143. * is overwritten instead of merged.
  144. *
  145. * @return void
  146. */
  147. public function testConstructWithWidgets()
  148. {
  149. $config = [
  150. 'widgets' => [
  151. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select'],
  152. ],
  153. ];
  154. $helper = new FormHelper($this->View, $config);
  155. $locator = $helper->getWidgetLocator();
  156. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  157. }
  158. /**
  159. * Test that when specifying custom widgets config file and it should be
  160. * added to widgets array. WidgetLocator will load widgets in constructor.
  161. *
  162. * @return void
  163. */
  164. public function testConstructWithWidgetsConfig()
  165. {
  166. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  167. $locator = $helper->getWidgetLocator();
  168. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  169. }
  170. /**
  171. * Test setting the widget locator
  172. *
  173. * @return void
  174. */
  175. public function testSetAndGetWidgetLocator()
  176. {
  177. $helper = new FormHelper($this->View);
  178. $locator = new WidgetLocator($helper->templater(), $this->View);
  179. $helper->setWidgetLocator($locator);
  180. $this->assertSame($locator, $helper->getWidgetLocator());
  181. }
  182. /**
  183. * Test overridding grouped input types which controls generation of "for"
  184. * attribute of labels.
  185. *
  186. * @return void
  187. */
  188. public function testConstructWithGroupedInputTypes()
  189. {
  190. $helper = new FormHelper($this->View, [
  191. 'groupedInputTypes' => ['radio'],
  192. ]);
  193. $result = $helper->control('when', ['type' => 'datetime-local']);
  194. $this->assertStringContainsString('<label for="when">When</label>', $result);
  195. }
  196. /**
  197. * Test registering a new widget class and rendering it.
  198. *
  199. * @return void
  200. */
  201. public function testAddWidgetAndRenderWidget()
  202. {
  203. $data = [
  204. 'val' => 1,
  205. ];
  206. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  207. $this->Form->addWidget('test', $mock);
  208. $mock->expects($this->once())
  209. ->method('render')
  210. ->with($data)
  211. ->will($this->returnValue('HTML'));
  212. $result = $this->Form->widget('test', $data);
  213. $this->assertSame('HTML', $result);
  214. }
  215. /**
  216. * Test that secureFields() of widget is called after calling render(),
  217. * not before.
  218. *
  219. * @return void
  220. */
  221. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  222. {
  223. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  224. 'unlockedFields' => [],
  225. ]));
  226. $data = [
  227. 'val' => 1,
  228. 'name' => 'test',
  229. ];
  230. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  231. $this->Form->addWidget('test', $mock);
  232. $mock->expects($this->at(0))
  233. ->method('render')
  234. ->with($data)
  235. ->will($this->returnValue('HTML'));
  236. $mock->expects($this->at(1))
  237. ->method('secureFields')
  238. ->with($data)
  239. ->will($this->returnValue(['test']));
  240. $this->Form->create();
  241. $result = $this->Form->widget('test', $data + ['secure' => true]);
  242. $this->assertSame('HTML', $result);
  243. }
  244. /**
  245. * Test that empty string is not added to secure fields list when
  246. * rendering input widget without name.
  247. *
  248. * @return void
  249. */
  250. public function testRenderingWidgetWithEmptyName()
  251. {
  252. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  253. $this->Form->create();
  254. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  255. $this->assertSame('<select name=""></select>', $result);
  256. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  257. $this->assertEquals([], $result);
  258. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  259. $this->assertSame('<select name="0"></select>', $result);
  260. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  261. $this->assertEquals(['0'], $result);
  262. }
  263. /**
  264. * Test registering an invalid widget class.
  265. *
  266. * @return void
  267. */
  268. public function testAddWidgetInvalid()
  269. {
  270. $this->expectException(\RuntimeException::class);
  271. $mock = new \StdClass();
  272. $this->Form->addWidget('test', $mock);
  273. $this->Form->widget('test');
  274. }
  275. /**
  276. * Test adding a new context class.
  277. *
  278. * @return void
  279. */
  280. public function testAddContextProvider()
  281. {
  282. $context = 'My data';
  283. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  284. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  285. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  286. $this->assertEquals($context, $data['entity']);
  287. return $stub;
  288. });
  289. $this->Form->create($context);
  290. $result = $this->Form->context();
  291. $this->assertSame($stub, $result);
  292. }
  293. /**
  294. * Test replacing a context class.
  295. *
  296. * @return void
  297. */
  298. public function testAddContextProviderReplace()
  299. {
  300. $entity = new Article();
  301. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  302. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  303. return $stub;
  304. });
  305. $this->Form->create($entity);
  306. $result = $this->Form->context();
  307. $this->assertSame($stub, $result);
  308. }
  309. /**
  310. * Test overriding a context class.
  311. *
  312. * @return void
  313. */
  314. public function testAddContextProviderAdd()
  315. {
  316. $entity = new Article();
  317. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  318. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  319. if ($data['entity'] instanceof Entity) {
  320. return $stub;
  321. }
  322. });
  323. $this->Form->create($entity);
  324. $result = $this->Form->context();
  325. $this->assertSame($stub, $result);
  326. }
  327. /**
  328. * Test adding an invalid context class.
  329. *
  330. * @return void
  331. */
  332. public function testAddContextProviderInvalid()
  333. {
  334. $this->expectException(\TypeError::class);
  335. $this->expectExceptionMessage('Return value of Cake\View\Form\ContextFactory::get() must implement interface Cake\View\Form\ContextInterface, instance of stdClass returned');
  336. $context = 'My data';
  337. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  338. return new \StdClass();
  339. });
  340. $this->Form->create($context);
  341. }
  342. /**
  343. * Provides context options for create().
  344. *
  345. * @return array
  346. */
  347. public function contextSelectionProvider()
  348. {
  349. $entity = new Article();
  350. $collection = new Collection([$entity]);
  351. $emptyCollection = new Collection([]);
  352. $arrayObject = new \ArrayObject([]);
  353. $data = [
  354. 'schema' => [
  355. 'title' => ['type' => 'string'],
  356. ],
  357. ];
  358. $form = new Form();
  359. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  360. return [
  361. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  362. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  363. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  364. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  365. 'form' => [$form, 'Cake\View\Form\FormContext'],
  366. 'none' => [null, 'Cake\View\Form\NullContext'],
  367. 'custom' => [$custom, get_class($custom)],
  368. ];
  369. }
  370. /**
  371. * Test default context selection in create()
  372. *
  373. * @dataProvider contextSelectionProvider
  374. * @return void
  375. */
  376. public function testCreateContextSelectionBuiltIn($data, $class)
  377. {
  378. $this->loadFixtures('Articles');
  379. $this->Form->create($data);
  380. $this->assertInstanceOf($class, $this->Form->context());
  381. }
  382. /**
  383. * Data provider for type option.
  384. *
  385. * @return array
  386. */
  387. public static function requestTypeProvider()
  388. {
  389. return [
  390. // type, method, override
  391. ['post', 'post', 'POST'],
  392. ['put', 'post', 'PUT'],
  393. ['patch', 'post', 'PATCH'],
  394. ['delete', 'post', 'DELETE'],
  395. ];
  396. }
  397. /**
  398. * Test creating file forms.
  399. *
  400. * @return void
  401. */
  402. public function testCreateFile()
  403. {
  404. $encoding = strtolower(Configure::read('App.encoding'));
  405. $result = $this->Form->create(null, ['type' => 'file']);
  406. $expected = [
  407. 'form' => [
  408. 'method' => 'post', 'action' => '/articles/add',
  409. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data',
  410. ],
  411. 'div' => ['style' => 'display:none;'],
  412. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  413. '/div',
  414. ];
  415. $this->assertHtml($expected, $result);
  416. }
  417. /**
  418. * Test creating GET forms.
  419. *
  420. * @return void
  421. */
  422. public function testCreateGet()
  423. {
  424. $encoding = strtolower(Configure::read('App.encoding'));
  425. $result = $this->Form->create(null, ['type' => 'get']);
  426. $expected = ['form' => [
  427. 'method' => 'get', 'action' => '/articles/add',
  428. 'accept-charset' => $encoding,
  429. ]];
  430. $this->assertHtml($expected, $result);
  431. }
  432. /**
  433. * Test explicit method/enctype options.
  434. *
  435. * Explicit method overwrites inferred method from 'type'
  436. *
  437. * @return void
  438. */
  439. public function testCreateExplicitMethodEnctype()
  440. {
  441. $encoding = strtolower(Configure::read('App.encoding'));
  442. $result = $this->Form->create(null, [
  443. 'type' => 'get',
  444. 'method' => 'put',
  445. 'enctype' => 'multipart/form-data',
  446. ]);
  447. $expected = ['form' => [
  448. 'method' => 'put',
  449. 'action' => '/articles/add',
  450. 'enctype' => 'multipart/form-data',
  451. 'accept-charset' => $encoding,
  452. ]];
  453. $this->assertHtml($expected, $result);
  454. }
  455. /**
  456. * Test create() with the templates option.
  457. *
  458. * @return void
  459. */
  460. public function testCreateTemplatesArray()
  461. {
  462. $result = $this->Form->create($this->article, [
  463. 'templates' => [
  464. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  465. ],
  466. ]);
  467. $expected = [
  468. 'form' => [
  469. 'class' => 'form-horizontal',
  470. 'method' => 'post',
  471. 'action' => '/articles/add',
  472. 'accept-charset' => 'utf-8',
  473. ],
  474. ];
  475. $this->assertHtml($expected, $result);
  476. }
  477. /**
  478. * Test create() with the templates option.
  479. *
  480. * @return void
  481. */
  482. public function testCreateTemplatesFile()
  483. {
  484. $result = $this->Form->create($this->article, [
  485. 'templates' => 'htmlhelper_tags',
  486. ]);
  487. $expected = [
  488. 'start form',
  489. 'div' => ['class' => 'hidden'],
  490. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  491. '/div',
  492. ];
  493. $this->assertHtml($expected, $result);
  494. }
  495. /**
  496. * Test that create() and end() restore templates.
  497. *
  498. * @return void
  499. */
  500. public function testCreateEndRestoreTemplates()
  501. {
  502. $this->Form->create($this->article, [
  503. 'templates' => ['input' => 'custom input element'],
  504. ]);
  505. $this->Form->end();
  506. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  507. }
  508. /**
  509. * Test using template vars in various templates used by control() method.
  510. *
  511. * @return void
  512. */
  513. public function testControlTemplateVars()
  514. {
  515. $result = $this->Form->control('text', [
  516. 'templates' => [
  517. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  518. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  519. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  520. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  521. ],
  522. 'templateVars' => [
  523. 'forinput' => 'in-input',
  524. 'forlabel' => 'in-label',
  525. 'forgroup' => 'in-group',
  526. 'forcontainer' => 'in-container',
  527. ],
  528. ]);
  529. $expected = [
  530. 'div' => ['class'],
  531. 'label' => ['for'],
  532. 'Text in-label',
  533. '/label',
  534. 'in-group',
  535. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  536. 'in-container',
  537. '/div',
  538. ];
  539. $this->assertHtml($expected, $result);
  540. }
  541. /**
  542. * Test ensuring template variables work in template files loaded
  543. * during control().
  544. *
  545. * @return void
  546. */
  547. public function testControlTemplatesFromFile()
  548. {
  549. $result = $this->Form->control('title', [
  550. 'templates' => 'test_templates',
  551. 'templateVars' => [
  552. 'forcontainer' => 'container-data',
  553. ],
  554. ]);
  555. $expected = [
  556. 'div' => ['class'],
  557. 'label' => ['for'],
  558. 'Title',
  559. '/label',
  560. 'input' => ['name', 'type' => 'text', 'id'],
  561. 'container-data',
  562. '/div',
  563. ];
  564. $this->assertHtml($expected, $result);
  565. }
  566. /**
  567. * Test using template vars in inputSubmit and submitContainer template.
  568. *
  569. * @return void
  570. */
  571. public function testSubmitTemplateVars()
  572. {
  573. $this->Form->setTemplates([
  574. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  575. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>',
  576. ]);
  577. $result = $this->Form->submit('Submit', [
  578. 'templateVars' => [
  579. 'forinput' => 'in-input',
  580. 'forcontainer' => 'in-container',
  581. ],
  582. ]);
  583. $expected = [
  584. 'div' => ['class'],
  585. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  586. 'in-container',
  587. '/div',
  588. ];
  589. $this->assertHtml($expected, $result);
  590. }
  591. /**
  592. * test the create() method
  593. *
  594. * @dataProvider requestTypeProvider
  595. * @return void
  596. */
  597. public function testCreateTypeOptions($type, $method, $override)
  598. {
  599. $encoding = strtolower(Configure::read('App.encoding'));
  600. $result = $this->Form->create(null, ['type' => $type]);
  601. $expected = [
  602. 'form' => [
  603. 'method' => $method, 'action' => '/articles/add',
  604. 'accept-charset' => $encoding,
  605. ],
  606. 'div' => ['style' => 'display:none;'],
  607. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  608. '/div',
  609. ];
  610. $this->assertHtml($expected, $result);
  611. }
  612. /**
  613. * Test using template vars in Create (formStart template)
  614. *
  615. * @return void
  616. */
  617. public function testCreateTemplateVars()
  618. {
  619. $result = $this->Form->create($this->article, [
  620. 'templates' => [
  621. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  622. ],
  623. 'templateVars' => ['header' => 'headertext'],
  624. ]);
  625. $expected = [
  626. 'h4' => ['class'],
  627. 'headertext',
  628. '/h4',
  629. 'form' => [
  630. 'method' => 'post',
  631. 'action' => '/articles/add',
  632. 'accept-charset' => 'utf-8',
  633. ],
  634. ];
  635. $this->assertHtml($expected, $result);
  636. }
  637. /**
  638. * Test opening a form for an update operation.
  639. *
  640. * @return void
  641. */
  642. public function testCreateUpdateForm()
  643. {
  644. $encoding = strtolower(Configure::read('App.encoding'));
  645. $this->View->setRequest($this->View->getRequest()
  646. ->withRequestTarget('/articles/edit/1')
  647. ->withParam('action', 'edit'));
  648. $this->article['defaults']['id'] = 1;
  649. $result = $this->Form->create($this->article);
  650. $expected = [
  651. 'form' => [
  652. 'method' => 'post',
  653. 'action' => '/articles/edit/1',
  654. 'accept-charset' => $encoding,
  655. ],
  656. 'div' => ['style' => 'display:none;'],
  657. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  658. '/div',
  659. ];
  660. $this->assertHtml($expected, $result);
  661. }
  662. /**
  663. * test create() with automatic url generation
  664. *
  665. * @return void
  666. */
  667. public function testCreateAutoUrl()
  668. {
  669. $encoding = strtolower(Configure::read('App.encoding'));
  670. $this->View->setRequest($this->View->getRequest()
  671. ->withRequestTarget('/articles/delete/10')
  672. ->withParam('action', 'delete'));
  673. $result = $this->Form->create($this->article);
  674. $expected = [
  675. 'form' => [
  676. 'method' => 'post', 'action' => '/articles/delete/10',
  677. 'accept-charset' => $encoding,
  678. ],
  679. 'div' => ['style' => 'display:none;'],
  680. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  681. '/div',
  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. 'div' => ['style' => 'display:none;'],
  750. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  751. '/div',
  752. ];
  753. $this->assertHtml($expected, $result);
  754. }
  755. /**
  756. * test create() with a custom route
  757. *
  758. * @return void
  759. */
  760. public function testCreateCustomRoute()
  761. {
  762. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  763. $encoding = strtolower(Configure::read('App.encoding'));
  764. $this->View->setRequest($this->View->getRequest()
  765. ->withParam('controller', 'users'));
  766. $result = $this->Form->create(null, ['url' => ['action' => 'login']]);
  767. $expected = [
  768. 'form' => [
  769. 'method' => 'post', 'action' => '/login',
  770. 'accept-charset' => $encoding,
  771. ],
  772. 'div' => ['style' => 'display:none;'],
  773. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  774. '/div',
  775. ];
  776. $this->assertHtml($expected, $result);
  777. Router::connect(
  778. '/new-article',
  779. ['controller' => 'articles', 'action' => 'myaction'],
  780. ['_name' => 'my-route']
  781. );
  782. $result = $this->Form->create(null, ['url' => ['_name' => 'my-route']]);
  783. $expected = [
  784. 'form' => [
  785. 'method' => 'post', 'action' => '/new-article',
  786. 'accept-charset' => $encoding,
  787. ],
  788. 'div' => ['style' => 'display:none;'],
  789. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  790. '/div',
  791. ];
  792. $this->assertHtml($expected, $result);
  793. }
  794. /**
  795. * test automatic accept-charset overriding
  796. *
  797. * @return void
  798. */
  799. public function testCreateWithAcceptCharset()
  800. {
  801. $result = $this->Form->create(
  802. $this->article,
  803. [
  804. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1',
  805. ]
  806. );
  807. $expected = [
  808. 'form' => [
  809. 'method' => 'post', 'action' => '/articles',
  810. 'accept-charset' => 'iso-8859-1',
  811. ],
  812. 'div' => ['style' => 'display:none;'],
  813. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  814. '/div',
  815. ];
  816. $this->assertHtml($expected, $result);
  817. }
  818. /**
  819. * Test base form URL when url param is passed with multiple parameters (&)
  820. *
  821. */
  822. public function testCreateQueryStringRequest()
  823. {
  824. $encoding = strtolower(Configure::read('App.encoding'));
  825. $result = $this->Form->create($this->article, [
  826. 'type' => 'post',
  827. 'escape' => false,
  828. 'url' => [
  829. 'controller' => 'controller',
  830. 'action' => 'action',
  831. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  832. ],
  833. ]);
  834. $expected = [
  835. 'form' => [
  836. 'method' => 'post',
  837. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  838. 'accept-charset' => $encoding,
  839. ],
  840. 'div' => ['style' => 'display:none;'],
  841. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  842. '/div',
  843. ];
  844. $this->assertHtml($expected, $result);
  845. $result = $this->Form->create($this->article, [
  846. 'type' => 'post',
  847. 'url' => [
  848. 'controller' => 'controller',
  849. 'action' => 'action',
  850. '?' => ['param1' => 'value1', 'param2' => 'value2'],
  851. ],
  852. ]);
  853. $this->assertHtml($expected, $result);
  854. }
  855. /**
  856. * test that create() doesn't cause errors by multiple id's being in the primary key
  857. * as could happen with multiple select or checkboxes.
  858. *
  859. * @return void
  860. */
  861. public function testCreateWithMultipleIdInData()
  862. {
  863. $encoding = strtolower(Configure::read('App.encoding'));
  864. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  865. $result = $this->Form->create($this->article);
  866. $expected = [
  867. 'form' => [
  868. 'method' => 'post',
  869. 'action' => '/articles/add',
  870. 'accept-charset' => $encoding,
  871. ],
  872. 'div' => ['style' => 'display:none;'],
  873. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  874. '/div',
  875. ];
  876. $this->assertHtml($expected, $result);
  877. }
  878. /**
  879. * test that create() doesn't add in extra passed params.
  880. *
  881. * @return void
  882. */
  883. public function testCreatePassedArgs()
  884. {
  885. $encoding = strtolower(Configure::read('App.encoding'));
  886. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  887. $result = $this->Form->create($this->article, [
  888. 'type' => 'post',
  889. 'escape' => false,
  890. 'url' => [
  891. 'action' => 'edit',
  892. 'myparam',
  893. ],
  894. ]);
  895. $expected = [
  896. 'form' => [
  897. 'method' => 'post',
  898. 'action' => '/articles/edit/myparam',
  899. 'accept-charset' => $encoding,
  900. ],
  901. 'div' => ['style' => 'display:none;'],
  902. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  903. '/div',
  904. ];
  905. $this->assertHtml($expected, $result);
  906. }
  907. /**
  908. * test creating a get form, and get form inputs.
  909. *
  910. * @return void
  911. */
  912. public function testGetFormCreate()
  913. {
  914. $encoding = strtolower(Configure::read('App.encoding'));
  915. $result = $this->Form->create($this->article, ['type' => 'get']);
  916. $expected = ['form' => [
  917. 'method' => 'get', 'action' => '/articles/add',
  918. 'accept-charset' => $encoding,
  919. ]];
  920. $this->assertHtml($expected, $result);
  921. $result = $this->Form->text('title');
  922. $expected = ['input' => [
  923. 'name' => 'title', 'type' => 'text', 'required' => 'required',
  924. ]];
  925. $this->assertHtml($expected, $result);
  926. $result = $this->Form->password('password');
  927. $expected = ['input' => [
  928. 'name' => 'password', 'type' => 'password',
  929. ]];
  930. $this->assertHtml($expected, $result);
  931. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  932. $result = $this->Form->text('user_form');
  933. $expected = ['input' => [
  934. 'name' => 'user_form', 'type' => 'text',
  935. ]];
  936. $this->assertHtml($expected, $result);
  937. }
  938. /**
  939. * test get form, and inputs when the model param is false
  940. *
  941. * @return void
  942. */
  943. public function testGetFormWithFalseModel()
  944. {
  945. $encoding = strtolower(Configure::read('App.encoding'));
  946. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'contact_test'));
  947. $result = $this->Form->create(null, [
  948. 'type' => 'get', 'url' => ['controller' => 'contact_test'],
  949. ]);
  950. $expected = ['form' => [
  951. 'method' => 'get', 'action' => '/contact_test/add',
  952. 'accept-charset' => $encoding,
  953. ]];
  954. $this->assertHtml($expected, $result);
  955. $result = $this->Form->text('reason');
  956. $expected = [
  957. 'input' => ['type' => 'text', 'name' => 'reason'],
  958. ];
  959. $this->assertHtml($expected, $result);
  960. }
  961. /**
  962. * testFormCreateWithSecurity method
  963. *
  964. * Test form->create() with security key.
  965. *
  966. * @return void
  967. */
  968. public function testCreateWithSecurity()
  969. {
  970. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  971. $encoding = strtolower(Configure::read('App.encoding'));
  972. $result = $this->Form->create($this->article, [
  973. 'url' => '/articles/publish',
  974. ]);
  975. $expected = [
  976. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  977. 'div' => ['style' => 'display:none;'],
  978. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  979. ['input' => [
  980. 'type' => 'hidden',
  981. 'name' => '_csrfToken',
  982. 'value' => 'testKey',
  983. 'autocomplete' => 'off',
  984. ]],
  985. '/div',
  986. ];
  987. $this->assertHtml($expected, $result);
  988. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  989. $expected['form']['id'] = 'MyForm';
  990. $this->assertHtml($expected, $result);
  991. }
  992. /**
  993. * testFormCreateGetNoSecurity method
  994. *
  995. * Test form->create() with no security key as its a get form
  996. *
  997. * @return void
  998. */
  999. public function testCreateEndGetNoSecurity()
  1000. {
  1001. $this->View->setRequest($this->View->getRequest()->withAttribute('csrfToken', 'testKey'));
  1002. $article = new Article();
  1003. $result = $this->Form->create($article, [
  1004. 'type' => 'get',
  1005. 'url' => '/contacts/add',
  1006. ]);
  1007. $this->assertStringNotContainsString('testKey', $result);
  1008. $result = $this->Form->end();
  1009. $this->assertStringNotContainsString('testKey', $result);
  1010. }
  1011. /**
  1012. * Tests form hash generation with model-less data
  1013. *
  1014. * @return void
  1015. */
  1016. public function testValidateHashNoModel()
  1017. {
  1018. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1019. $fields = ['anything'];
  1020. $this->Form->create();
  1021. $result = $this->Form->secure($fields);
  1022. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  1023. $this->assertStringContainsString($hash, $result);
  1024. }
  1025. /**
  1026. * Tests that hidden fields generated for checkboxes don't get locked
  1027. *
  1028. * @return void
  1029. */
  1030. public function testNoCheckboxLocking()
  1031. {
  1032. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1033. $this->Form->create();
  1034. $this->assertSame([], $this->Form->getFormProtector()->__debugInfo()['fields']);
  1035. $this->Form->checkbox('check', ['value' => '1']);
  1036. $this->assertSame(['check'], $this->Form->getFormProtector()->__debugInfo()['fields']);
  1037. }
  1038. /**
  1039. * testFormSecurityFields method
  1040. *
  1041. * Test generation of secure form hash generation.
  1042. *
  1043. * @return void
  1044. */
  1045. public function testFormSecurityFields()
  1046. {
  1047. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1048. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1049. $this->Form->create();
  1050. $result = $this->Form->secure($fields);
  1051. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  1052. $hash .= ':' . 'Model.valid';
  1053. $hash = urlencode($hash);
  1054. $tokenDebug = urlencode(json_encode([
  1055. $this->url,
  1056. $fields,
  1057. [],
  1058. ]));
  1059. $expected = [
  1060. 'div' => ['style' => 'display:none;'],
  1061. ['input' => [
  1062. 'type' => 'hidden',
  1063. 'name' => '_Token[fields]',
  1064. 'value' => $hash,
  1065. 'autocomplete' => 'off',
  1066. ]],
  1067. ['input' => [
  1068. 'type' => 'hidden',
  1069. 'name' => '_Token[unlocked]',
  1070. 'value' => '',
  1071. 'autocomplete' => 'off',
  1072. ]],
  1073. ['input' => [
  1074. 'type' => 'hidden',
  1075. 'name' => '_Token[debug]',
  1076. 'value' => $tokenDebug,
  1077. 'autocomplete' => 'off',
  1078. ]],
  1079. '/div',
  1080. ];
  1081. $this->assertHtml($expected, $result);
  1082. }
  1083. /**
  1084. * testFormSecurityFields method
  1085. *
  1086. * Test debug token is not generated if debug is false
  1087. *
  1088. * @return void
  1089. */
  1090. public function testFormSecurityFieldsNoDebugMode()
  1091. {
  1092. Configure::write('debug', false);
  1093. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1094. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1095. $this->Form->create();
  1096. $result = $this->Form->secure($fields);
  1097. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  1098. $hash .= ':' . 'Model.valid';
  1099. $hash = urlencode($hash);
  1100. $expected = [
  1101. 'div' => ['style' => 'display:none;'],
  1102. ['input' => [
  1103. 'type' => 'hidden',
  1104. 'name' => '_Token[fields]',
  1105. 'autocomplete' => 'off',
  1106. 'value' => $hash,
  1107. ]],
  1108. ['input' => [
  1109. 'type' => 'hidden',
  1110. 'name' => '_Token[unlocked]',
  1111. 'autocomplete' => 'off',
  1112. 'value' => '',
  1113. ]],
  1114. '/div',
  1115. ];
  1116. $this->assertHtml($expected, $result);
  1117. }
  1118. /**
  1119. * Tests correct generation of number fields for smallint
  1120. *
  1121. * @return void
  1122. */
  1123. public function testTextFieldGenerationForSmallint()
  1124. {
  1125. $this->article['schema'] = [
  1126. 'foo' => [
  1127. 'type' => 'smallinteger',
  1128. 'null' => false,
  1129. 'default' => null,
  1130. 'length' => 10,
  1131. ],
  1132. ];
  1133. $this->Form->create($this->article);
  1134. $result = $this->Form->control('foo');
  1135. $this->assertStringContainsString('class="input number"', $result);
  1136. $this->assertStringContainsString('type="number"', $result);
  1137. }
  1138. /**
  1139. * Tests correct generation of number fields for tinyint
  1140. *
  1141. * @return void
  1142. */
  1143. public function testTextFieldGenerationForTinyint()
  1144. {
  1145. $this->article['schema'] = [
  1146. 'foo' => [
  1147. 'type' => 'tinyinteger',
  1148. 'null' => false,
  1149. 'default' => null,
  1150. 'length' => 10,
  1151. ],
  1152. ];
  1153. $this->Form->create($this->article);
  1154. $result = $this->Form->control('foo');
  1155. $this->assertStringContainsString('class="input number"', $result);
  1156. $this->assertStringContainsString('type="number"', $result);
  1157. }
  1158. /**
  1159. * Tests correct generation of number fields for double and float fields
  1160. *
  1161. * @return void
  1162. */
  1163. public function testTextFieldGenerationForFloats()
  1164. {
  1165. $this->article['schema'] = [
  1166. 'foo' => [
  1167. 'type' => 'float',
  1168. 'null' => false,
  1169. 'default' => null,
  1170. 'length' => 10,
  1171. ],
  1172. ];
  1173. $this->Form->create($this->article);
  1174. $result = $this->Form->control('foo');
  1175. $expected = [
  1176. 'div' => ['class' => 'input number'],
  1177. 'label' => ['for' => 'foo'],
  1178. 'Foo',
  1179. '/label',
  1180. ['input' => [
  1181. 'type' => 'number',
  1182. 'name' => 'foo',
  1183. 'id' => 'foo',
  1184. 'step' => 'any',
  1185. ]],
  1186. '/div',
  1187. ];
  1188. $this->assertHtml($expected, $result);
  1189. $result = $this->Form->control('foo', ['step' => 0.5]);
  1190. $expected = [
  1191. 'div' => ['class' => 'input number'],
  1192. 'label' => ['for' => 'foo'],
  1193. 'Foo',
  1194. '/label',
  1195. ['input' => [
  1196. 'type' => 'number',
  1197. 'name' => 'foo',
  1198. 'id' => 'foo',
  1199. 'step' => '0.5',
  1200. ]],
  1201. '/div',
  1202. ];
  1203. $this->assertHtml($expected, $result);
  1204. }
  1205. /**
  1206. * Tests correct generation of number fields for integer fields
  1207. *
  1208. * @return void
  1209. */
  1210. public function testTextFieldTypeNumberGenerationForIntegers()
  1211. {
  1212. $this->getTableLocator()->get('Contacts', [
  1213. 'className' => ContactsTable::class,
  1214. ]);
  1215. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1216. $result = $this->Form->control('age');
  1217. $expected = [
  1218. 'div' => ['class' => 'input number'],
  1219. 'label' => ['for' => 'age'],
  1220. 'Age',
  1221. '/label',
  1222. ['input' => [
  1223. 'type' => 'number', 'name' => 'age',
  1224. 'id' => 'age',
  1225. ]],
  1226. '/div',
  1227. ];
  1228. $this->assertHtml($expected, $result);
  1229. }
  1230. /**
  1231. * Tests correct generation of file upload fields for binary fields
  1232. *
  1233. * @return void
  1234. */
  1235. public function testFileUploadFieldTypeGenerationForBinaries()
  1236. {
  1237. $table = $this->getTableLocator()->get('Contacts', [
  1238. 'className' => ContactsTable::class,
  1239. ]);
  1240. $table->setSchema(['foo' => [
  1241. 'type' => 'binary',
  1242. 'null' => false,
  1243. 'default' => null,
  1244. 'length' => 1024,
  1245. ]]);
  1246. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1247. $result = $this->Form->control('foo');
  1248. $expected = [
  1249. 'div' => ['class' => 'input file'],
  1250. 'label' => ['for' => 'foo'],
  1251. 'Foo',
  1252. '/label',
  1253. ['input' => [
  1254. 'type' => 'file', 'name' => 'foo',
  1255. 'id' => 'foo',
  1256. ]],
  1257. '/div',
  1258. ];
  1259. $this->assertHtml($expected, $result);
  1260. }
  1261. /**
  1262. * testFormSecurityMultipleFields method
  1263. *
  1264. * Test secure() with multiple row form. Ensure hash is correct.
  1265. *
  1266. * @return void
  1267. */
  1268. public function testFormSecurityMultipleFields()
  1269. {
  1270. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1271. $this->Form->create();
  1272. $fields = [
  1273. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1274. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1275. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0',
  1276. ];
  1277. $result = $this->Form->secure($fields);
  1278. $sortedFields = [
  1279. 'Model.0.password',
  1280. 'Model.0.username',
  1281. 'Model.1.password',
  1282. 'Model.1.username',
  1283. 'Model.0.hidden' => 'value',
  1284. 'Model.0.valid' => '0',
  1285. 'Model.1.hidden' => 'value',
  1286. 'Model.1.valid' => '0',
  1287. ];
  1288. $hash = hash_hmac('sha1', $this->url . serialize($sortedFields) . session_id(), Security::getSalt());
  1289. $hash .= ':Model.0.hidden|Model.0.valid|Model.1.hidden|Model.1.valid';
  1290. $hash = urlencode($hash);
  1291. $tokenDebug = urlencode(json_encode([
  1292. $this->url,
  1293. $fields,
  1294. [],
  1295. ]));
  1296. $expected = [
  1297. 'div' => ['style' => 'display:none;'],
  1298. ['input' => [
  1299. 'type' => 'hidden',
  1300. 'name' => '_Token[fields]',
  1301. 'value' => $hash,
  1302. 'autocomplete' => 'off',
  1303. ]],
  1304. ['input' => [
  1305. 'type' => 'hidden',
  1306. 'name' => '_Token[unlocked]',
  1307. 'autocomplete' => 'off',
  1308. 'value' => '',
  1309. ]],
  1310. ['input' => [
  1311. 'type' => 'hidden',
  1312. 'name' => '_Token[debug]',
  1313. 'value' => $tokenDebug,
  1314. 'autocomplete' => 'off',
  1315. ]],
  1316. '/div',
  1317. ];
  1318. $this->assertHtml($expected, $result);
  1319. }
  1320. /**
  1321. * testFormSecurityMultipleSubmitButtons
  1322. *
  1323. * test form submit generation and ensure that _Token is only created on end()
  1324. *
  1325. * @return void
  1326. */
  1327. public function testFormSecurityMultipleSubmitButtons()
  1328. {
  1329. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1330. $this->Form->create($this->article);
  1331. $this->Form->text('Address.title');
  1332. $this->Form->text('Address.first_name');
  1333. $result = $this->Form->submit('Save', ['name' => 'save']);
  1334. $expected = [
  1335. 'div' => ['class' => 'submit'],
  1336. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1337. '/div',
  1338. ];
  1339. $this->assertHtml($expected, $result);
  1340. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1341. $expected = [
  1342. 'div' => ['class' => 'submit'],
  1343. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1344. '/div',
  1345. ];
  1346. $this->assertHtml($expected, $result);
  1347. $result = $this->Form->end();
  1348. $tokenDebug = urlencode(json_encode([
  1349. '/articles/add',
  1350. [
  1351. 'Address.title',
  1352. 'Address.first_name',
  1353. ],
  1354. ['save', 'cancel'],
  1355. ]));
  1356. $expected = [
  1357. 'div' => ['style' => 'display:none;'],
  1358. ['input' => [
  1359. 'type' => 'hidden',
  1360. 'name' => '_Token[fields]',
  1361. 'autocomplete',
  1362. 'value',
  1363. ]],
  1364. ['input' => [
  1365. 'type' => 'hidden',
  1366. 'name' => '_Token[unlocked]',
  1367. 'value' => 'cancel%7Csave',
  1368. 'autocomplete' => 'off',
  1369. ]],
  1370. ['input' => [
  1371. 'type' => 'hidden',
  1372. 'name' => '_Token[debug]',
  1373. 'value' => $tokenDebug,
  1374. 'autocomplete' => 'off',
  1375. ]],
  1376. '/div',
  1377. ];
  1378. $this->assertHtml($expected, $result);
  1379. }
  1380. /**
  1381. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1382. *
  1383. * @return void
  1384. */
  1385. public function testSecurityButtonNestedNamed()
  1386. {
  1387. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1388. $this->Form->create();
  1389. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1390. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1391. $this->assertEquals(['Address.button'], $result);
  1392. }
  1393. /**
  1394. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1395. *
  1396. * @return void
  1397. */
  1398. public function testSecuritySubmitNestedNamed()
  1399. {
  1400. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1401. $this->Form->create($this->article);
  1402. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1403. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1404. $this->assertEquals(['Address.button'], $result);
  1405. }
  1406. /**
  1407. * Test that the correct fields are unlocked for image submits with no names.
  1408. *
  1409. * @return void
  1410. */
  1411. public function testSecuritySubmitImageNoName()
  1412. {
  1413. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1414. $this->Form->create();
  1415. $result = $this->Form->submit('save.png');
  1416. $expected = [
  1417. 'div' => ['class' => 'submit'],
  1418. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1419. '/div',
  1420. ];
  1421. $this->assertHtml($expected, $result);
  1422. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1423. $this->assertEquals(['x', 'y'], $result);
  1424. }
  1425. /**
  1426. * Test that the correct fields are unlocked for image submits with names.
  1427. *
  1428. * @return void
  1429. */
  1430. public function testSecuritySubmitImageName()
  1431. {
  1432. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1433. $this->Form->create();
  1434. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1435. $expected = [
  1436. 'div' => ['class' => 'submit'],
  1437. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1438. '/div',
  1439. ];
  1440. $this->assertHtml($expected, $result);
  1441. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1442. $this->assertEquals(['test', 'test_x', 'test_y'], $result);
  1443. }
  1444. /**
  1445. * testFormSecurityMultipleControlFields method
  1446. *
  1447. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1448. *
  1449. * @return void
  1450. */
  1451. public function testFormSecurityMultipleControlFields()
  1452. {
  1453. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1454. $this->Form->create();
  1455. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1456. $this->Form->control('Addresses.0.title');
  1457. $this->Form->control('Addresses.0.first_name');
  1458. $this->Form->control('Addresses.0.last_name');
  1459. $this->Form->control('Addresses.0.address');
  1460. $this->Form->control('Addresses.0.city');
  1461. $this->Form->control('Addresses.0.phone');
  1462. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1463. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1464. $this->Form->control('Addresses.1.title');
  1465. $this->Form->control('Addresses.1.first_name');
  1466. $this->Form->control('Addresses.1.last_name');
  1467. $this->Form->control('Addresses.1.address');
  1468. $this->Form->control('Addresses.1.city');
  1469. $this->Form->control('Addresses.1.phone');
  1470. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1471. $result = $this->Form->secure();
  1472. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1473. $tokenDebug = urlencode(json_encode([
  1474. '/articles/add',
  1475. [
  1476. 'Addresses.0.id' => '123456',
  1477. 'Addresses.0.title',
  1478. 'Addresses.0.first_name',
  1479. 'Addresses.0.last_name',
  1480. 'Addresses.0.address',
  1481. 'Addresses.0.city',
  1482. 'Addresses.0.phone',
  1483. 'Addresses.0.primary',
  1484. 'Addresses.1.id' => '654321',
  1485. 'Addresses.1.title',
  1486. 'Addresses.1.first_name',
  1487. 'Addresses.1.last_name',
  1488. 'Addresses.1.address',
  1489. 'Addresses.1.city',
  1490. 'Addresses.1.phone',
  1491. 'Addresses.1.primary',
  1492. ],
  1493. [],
  1494. ]));
  1495. $expected = [
  1496. 'div' => ['style' => 'display:none;'],
  1497. ['input' => [
  1498. 'type' => 'hidden',
  1499. 'name' => '_Token[fields]',
  1500. 'value' => $hash,
  1501. 'autocomplete' => 'off',
  1502. ]],
  1503. ['input' => [
  1504. 'type' => 'hidden',
  1505. 'name' => '_Token[unlocked]',
  1506. 'autocomplete' => 'off',
  1507. 'value' => '',
  1508. ]],
  1509. ['input' => [
  1510. 'type' => 'hidden',
  1511. 'name' => '_Token[debug]',
  1512. 'value' => $tokenDebug,
  1513. 'autocomplete' => 'off',
  1514. ]],
  1515. '/div',
  1516. ];
  1517. $this->assertHtml($expected, $result);
  1518. }
  1519. /**
  1520. * testFormSecurityArrayFields method
  1521. *
  1522. * Test form security with Model.field.0 style inputs.
  1523. *
  1524. * @return void
  1525. */
  1526. public function testFormSecurityArrayFields()
  1527. {
  1528. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1529. $this->Form->create();
  1530. $this->Form->text('Address.primary.1');
  1531. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1532. $this->assertSame('Address.primary', $result[0]);
  1533. $this->Form->text('Address.secondary.1.0');
  1534. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1535. $this->assertSame('Address.secondary', $result[1]);
  1536. }
  1537. /**
  1538. * testFormSecurityMultipleControlDisabledFields method
  1539. *
  1540. * Test secure form generation with multiple records and disabled fields.
  1541. *
  1542. * @return void
  1543. */
  1544. public function testFormSecurityMultipleControlDisabledFields()
  1545. {
  1546. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1547. 'unlockedFields' => ['first_name', 'address'],
  1548. ]));
  1549. $this->Form->create();
  1550. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1551. $this->Form->text('Addresses.0.title');
  1552. $this->Form->text('Addresses.0.first_name');
  1553. $this->Form->text('Addresses.0.last_name');
  1554. $this->Form->text('Addresses.0.address');
  1555. $this->Form->text('Addresses.0.city');
  1556. $this->Form->text('Addresses.0.phone');
  1557. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1558. $this->Form->text('Addresses.1.title');
  1559. $this->Form->text('Addresses.1.first_name');
  1560. $this->Form->text('Addresses.1.last_name');
  1561. $this->Form->text('Addresses.1.address');
  1562. $this->Form->text('Addresses.1.city');
  1563. $this->Form->text('Addresses.1.phone');
  1564. $result = $this->Form->secure();
  1565. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1566. $tokenDebug = urlencode(json_encode([
  1567. '/articles/add',
  1568. [
  1569. 'Addresses.0.id' => '123456',
  1570. 'Addresses.0.title',
  1571. 'Addresses.0.last_name',
  1572. 'Addresses.0.city',
  1573. 'Addresses.0.phone',
  1574. 'Addresses.1.id' => '654321',
  1575. 'Addresses.1.title',
  1576. 'Addresses.1.last_name',
  1577. 'Addresses.1.city',
  1578. 'Addresses.1.phone',
  1579. ],
  1580. [
  1581. 'first_name',
  1582. 'address',
  1583. ],
  1584. ]));
  1585. $expected = [
  1586. 'div' => ['style' => 'display:none;'],
  1587. ['input' => [
  1588. 'type' => 'hidden',
  1589. 'name' => '_Token[fields]',
  1590. 'autocomplete' => 'off',
  1591. 'value' => $hash,
  1592. ]],
  1593. ['input' => [
  1594. 'type' => 'hidden',
  1595. 'name' => '_Token[unlocked]',
  1596. 'autocomplete' => 'off',
  1597. 'value' => 'address%7Cfirst_name',
  1598. ]],
  1599. ['input' => [
  1600. 'type' => 'hidden',
  1601. 'name' => '_Token[debug]',
  1602. 'autocomplete' => 'off',
  1603. 'value' => $tokenDebug,
  1604. ]],
  1605. '/div',
  1606. ];
  1607. $this->assertHtml($expected, $result);
  1608. }
  1609. /**
  1610. * testFormSecurityControlDisabledFields method
  1611. *
  1612. * Test single record form with disabled fields.
  1613. *
  1614. * @return void
  1615. */
  1616. public function testFormSecurityControlUnlockedFields()
  1617. {
  1618. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1619. 'unlockedFields' => ['first_name', 'address'],
  1620. ]));
  1621. $this->Form->create();
  1622. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1623. $this->assertEquals(
  1624. $this->View->getRequest()->getAttribute('formTokenData'),
  1625. ['unlockedFields' => $result]
  1626. );
  1627. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1628. $this->Form->text('Addresses.title');
  1629. $this->Form->text('Addresses.first_name');
  1630. $this->Form->text('Addresses.last_name');
  1631. $this->Form->text('Addresses.address');
  1632. $this->Form->text('Addresses.city');
  1633. $this->Form->text('Addresses.phone');
  1634. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1635. $expected = [
  1636. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1637. 'Addresses.city', 'Addresses.phone',
  1638. ];
  1639. $this->assertEquals($expected, $result);
  1640. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1641. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1642. $tokenDebug = urlencode(json_encode([
  1643. '/articles/add',
  1644. [
  1645. 'Addresses.id' => '123456',
  1646. 'Addresses.title',
  1647. 'Addresses.last_name',
  1648. 'Addresses.city',
  1649. 'Addresses.phone',
  1650. ],
  1651. [
  1652. 'first_name',
  1653. 'address',
  1654. ],
  1655. ]));
  1656. $expected = [
  1657. 'div' => ['style' => 'display:none;'],
  1658. ['input' => [
  1659. 'type' => 'hidden',
  1660. 'name' => '_Token[fields]',
  1661. 'value' => $hash,
  1662. 'autocomplete' => 'off',
  1663. 'data-foo' => 'bar',
  1664. ]],
  1665. ['input' => [
  1666. 'type' => 'hidden',
  1667. 'name' => '_Token[unlocked]',
  1668. 'value' => 'address%7Cfirst_name',
  1669. 'autocomplete' => 'off',
  1670. 'data-foo' => 'bar',
  1671. ]],
  1672. ['input' => [
  1673. 'type' => 'hidden', 'name' => '_Token[debug]',
  1674. 'value' => $tokenDebug,
  1675. 'autocomplete' => 'off',
  1676. 'data-foo' => 'bar',
  1677. ]],
  1678. '/div',
  1679. ];
  1680. $this->assertHtml($expected, $result);
  1681. }
  1682. /**
  1683. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1684. *
  1685. * Test single record form with debugSecurity param.
  1686. *
  1687. * @return void
  1688. */
  1689. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1690. {
  1691. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1692. 'unlockedFields' => ['first_name', 'address'],
  1693. ]));
  1694. $this->Form->create();
  1695. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1696. $this->assertEquals(
  1697. $this->View->getRequest()->getAttribute('formTokenData'),
  1698. ['unlockedFields' => $result]
  1699. );
  1700. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1701. $this->Form->text('Addresses.title');
  1702. $this->Form->text('Addresses.first_name');
  1703. $this->Form->text('Addresses.last_name');
  1704. $this->Form->text('Addresses.address');
  1705. $this->Form->text('Addresses.city');
  1706. $this->Form->text('Addresses.phone');
  1707. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1708. $expected = [
  1709. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1710. 'Addresses.city', 'Addresses.phone',
  1711. ];
  1712. $this->assertEquals($expected, $result);
  1713. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1714. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1715. $tokenDebug = urlencode(json_encode([
  1716. '/articles/add',
  1717. [
  1718. 'Addresses.id' => '123456',
  1719. 'Addresses.title',
  1720. 'Addresses.last_name',
  1721. 'Addresses.city',
  1722. 'Addresses.phone',
  1723. ],
  1724. [
  1725. 'first_name',
  1726. 'address',
  1727. ],
  1728. ]));
  1729. $expected = [
  1730. 'div' => ['style' => 'display:none;'],
  1731. ['input' => [
  1732. 'type' => 'hidden',
  1733. 'name' => '_Token[fields]',
  1734. 'value' => $hash,
  1735. 'autocomplete' => 'off',
  1736. 'data-foo' => 'bar',
  1737. ]],
  1738. ['input' => [
  1739. 'type' => 'hidden',
  1740. 'name' => '_Token[unlocked]',
  1741. 'value' => 'address%7Cfirst_name',
  1742. 'autocomplete' => 'off',
  1743. 'data-foo' => 'bar',
  1744. ]],
  1745. ['input' => [
  1746. 'type' => 'hidden', 'name' => '_Token[debug]',
  1747. 'value' => $tokenDebug,
  1748. 'autocomplete' => 'off',
  1749. 'data-foo' => 'bar',
  1750. ]],
  1751. '/div',
  1752. ];
  1753. $this->assertHtml($expected, $result);
  1754. }
  1755. /**
  1756. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1757. *
  1758. * Debug is false, debugSecurity is true -> no debug
  1759. *
  1760. * @return void
  1761. */
  1762. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1763. {
  1764. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1765. 'unlockedFields' => ['first_name', 'address'],
  1766. ]));
  1767. $this->Form->create();
  1768. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1769. $this->assertEquals(
  1770. $this->View->getRequest()->getAttribute('formTokenData'),
  1771. ['unlockedFields' => $result]
  1772. );
  1773. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1774. $this->Form->text('Addresses.title');
  1775. $this->Form->text('Addresses.first_name');
  1776. $this->Form->text('Addresses.last_name');
  1777. $this->Form->text('Addresses.address');
  1778. $this->Form->text('Addresses.city');
  1779. $this->Form->text('Addresses.phone');
  1780. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1781. $expected = [
  1782. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1783. 'Addresses.city', 'Addresses.phone',
  1784. ];
  1785. $this->assertEquals($expected, $result);
  1786. Configure::write('debug', false);
  1787. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1788. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1789. $expected = [
  1790. 'div' => ['style' => 'display:none;'],
  1791. ['input' => [
  1792. 'type' => 'hidden',
  1793. 'name' => '_Token[fields]',
  1794. 'value' => $hash,
  1795. 'autocomplete' => 'off',
  1796. 'data-foo' => 'bar',
  1797. ]],
  1798. ['input' => [
  1799. 'type' => 'hidden',
  1800. 'name' => '_Token[unlocked]',
  1801. 'value' => 'address%7Cfirst_name',
  1802. 'autocomplete' => 'off',
  1803. 'data-foo' => 'bar',
  1804. ]],
  1805. '/div',
  1806. ];
  1807. $this->assertHtml($expected, $result);
  1808. }
  1809. /**
  1810. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1811. *
  1812. * Test single record form with debugSecurity param.
  1813. *
  1814. * @return void
  1815. */
  1816. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1817. {
  1818. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  1819. 'unlockedFields' => ['first_name', 'address'],
  1820. ]));
  1821. $this->Form->create();
  1822. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  1823. $this->assertEquals(
  1824. $this->View->getRequest()->getAttribute('formTokenData'),
  1825. ['unlockedFields' => $result]
  1826. );
  1827. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1828. $this->Form->text('Addresses.title');
  1829. $this->Form->text('Addresses.first_name');
  1830. $this->Form->text('Addresses.last_name');
  1831. $this->Form->text('Addresses.address');
  1832. $this->Form->text('Addresses.city');
  1833. $this->Form->text('Addresses.phone');
  1834. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1835. $expected = [
  1836. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1837. 'Addresses.city', 'Addresses.phone',
  1838. ];
  1839. $this->assertEquals($expected, $result);
  1840. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1841. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1842. $expected = [
  1843. 'div' => ['style' => 'display:none;'],
  1844. ['input' => [
  1845. 'type' => 'hidden',
  1846. 'name' => '_Token[fields]',
  1847. 'value' => $hash,
  1848. 'autocomplete' => 'off',
  1849. 'data-foo' => 'bar',
  1850. ]],
  1851. ['input' => [
  1852. 'type' => 'hidden',
  1853. 'name' => '_Token[unlocked]',
  1854. 'value' => 'address%7Cfirst_name',
  1855. 'autocomplete' => 'off',
  1856. 'data-foo' => 'bar',
  1857. ]],
  1858. '/div',
  1859. ];
  1860. $this->assertHtml($expected, $result);
  1861. }
  1862. /**
  1863. * testFormSecureWithCustomNameAttribute method
  1864. *
  1865. * Test securing inputs with custom name attributes.
  1866. *
  1867. * @return void
  1868. */
  1869. public function testFormSecureWithCustomNameAttribute()
  1870. {
  1871. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  1872. $this->Form->create();
  1873. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1874. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1875. $this->assertSame('User.custom', $result[0]);
  1876. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1877. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1878. $this->assertSame('User.custom.another.value', $result[1]);
  1879. }
  1880. /**
  1881. * testFormSecuredControl method
  1882. *
  1883. * Test generation of entire secure form, assertions made on control() output.
  1884. *
  1885. * @return void
  1886. */
  1887. public function testFormSecuredControl()
  1888. {
  1889. $this->View->setRequest($this->View->getRequest()
  1890. ->withAttribute('formTokenData', [])
  1891. ->withAttribute('csrfToken', 'testKey'));
  1892. $this->article['schema'] = [
  1893. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1894. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1895. ];
  1896. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1897. $encoding = strtolower(Configure::read('App.encoding'));
  1898. $expected = [
  1899. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1900. 'div' => ['style' => 'display:none;'],
  1901. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1902. ['input' => [
  1903. 'type' => 'hidden',
  1904. 'name' => '_csrfToken',
  1905. 'value' => 'testKey',
  1906. 'autocomplete' => 'off',
  1907. ]],
  1908. '/div',
  1909. ];
  1910. $this->assertHtml($expected, $result);
  1911. $result = $this->Form->control('ratio');
  1912. $expected = [
  1913. 'div' => ['class'],
  1914. 'label' => ['for'],
  1915. 'Ratio',
  1916. '/label',
  1917. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1918. '/div',
  1919. ];
  1920. $this->assertHtml($expected, $result);
  1921. $result = $this->Form->control('population');
  1922. $expected = [
  1923. 'div' => ['class'],
  1924. 'label' => ['for'],
  1925. 'Population',
  1926. '/label',
  1927. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1928. '/div',
  1929. ];
  1930. $this->assertHtml($expected, $result);
  1931. $result = $this->Form->control('published', ['type' => 'text']);
  1932. $expected = [
  1933. 'div' => ['class' => 'input text'],
  1934. 'label' => ['for' => 'published'],
  1935. 'Published',
  1936. '/label',
  1937. ['input' => [
  1938. 'type' => 'text',
  1939. 'name' => 'published',
  1940. 'id' => 'published',
  1941. ]],
  1942. '/div',
  1943. ];
  1944. $this->assertHtml($expected, $result);
  1945. $result = $this->Form->control('other', ['type' => 'text']);
  1946. $expected = [
  1947. 'div' => ['class' => 'input text'],
  1948. 'label' => ['for' => 'other'],
  1949. 'Other',
  1950. '/label',
  1951. ['input' => [
  1952. 'type' => 'text',
  1953. 'name' => 'other',
  1954. 'id',
  1955. ]],
  1956. '/div',
  1957. ];
  1958. $this->assertHtml($expected, $result);
  1959. $result = $this->Form->hidden('stuff');
  1960. $expected = [
  1961. 'input' => [
  1962. 'type' => 'hidden',
  1963. 'name' => 'stuff',
  1964. ],
  1965. ];
  1966. $this->assertHtml($expected, $result);
  1967. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1968. $expected = ['input' => [
  1969. 'type' => 'hidden',
  1970. 'name' => 'hidden',
  1971. 'value' => '0',
  1972. ]];
  1973. $this->assertHtml($expected, $result);
  1974. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1975. $expected = [
  1976. 'div' => ['class' => 'input checkbox'],
  1977. ['input' => [
  1978. 'type' => 'hidden',
  1979. 'name' => 'something',
  1980. 'value' => '0',
  1981. ]],
  1982. 'label' => ['for' => 'something'],
  1983. ['input' => [
  1984. 'type' => 'checkbox',
  1985. 'name' => 'something',
  1986. 'value' => '1',
  1987. 'id' => 'something',
  1988. ]],
  1989. 'Something',
  1990. '/label',
  1991. '/div',
  1992. ];
  1993. $this->assertHtml($expected, $result);
  1994. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  1995. $expectedFields = [
  1996. 'ratio',
  1997. 'population',
  1998. 'published',
  1999. 'other',
  2000. 'stuff' => '',
  2001. 'hidden' => '0',
  2002. 'something',
  2003. ];
  2004. $this->assertEquals($expectedFields, $result);
  2005. $result = $this->Form->secure();
  2006. $tokenDebug = urlencode(json_encode([
  2007. '/articles/add',
  2008. $expectedFields,
  2009. [],
  2010. ]));
  2011. $expected = [
  2012. 'div' => ['style' => 'display:none;'],
  2013. ['input' => [
  2014. 'type' => 'hidden',
  2015. 'name' => '_Token[fields]',
  2016. 'value',
  2017. 'autocomplete',
  2018. ]],
  2019. ['input' => [
  2020. 'type' => 'hidden',
  2021. 'name' => '_Token[unlocked]',
  2022. 'value' => '',
  2023. 'autocomplete' => 'off',
  2024. ]],
  2025. ['input' => [
  2026. 'type' => 'hidden', 'name' => '_Token[debug]',
  2027. 'value' => $tokenDebug,
  2028. 'autocomplete' => 'off',
  2029. ]],
  2030. '/div',
  2031. ];
  2032. $this->assertHtml($expected, $result);
  2033. }
  2034. /**
  2035. * testSecuredControlCustomName method
  2036. *
  2037. * Test secured inputs with custom names.
  2038. *
  2039. * @return void
  2040. */
  2041. public function testSecuredControlCustomName()
  2042. {
  2043. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2044. $this->Form->create();
  2045. $this->Form->text('text_input', [
  2046. 'name' => 'Option[General.default_role]',
  2047. ]);
  2048. $expected = ['Option.General.default_role'];
  2049. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2050. $this->assertEquals($expected, $result);
  2051. $this->Form->select('select_box', [1, 2], [
  2052. 'name' => 'Option[General.select_role]',
  2053. ]);
  2054. $expected[] = 'Option.General.select_role';
  2055. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2056. $this->assertEquals($expected, $result);
  2057. $this->Form->text('other.things[]');
  2058. $expected[] = 'other.things';
  2059. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2060. $this->assertEquals($expected, $result);
  2061. }
  2062. /**
  2063. * testSecuredControlDuplicate method
  2064. *
  2065. * Test that a hidden field followed by a visible field
  2066. * undoes the hidden field locking.
  2067. *
  2068. * @return void
  2069. */
  2070. public function testSecuredControlDuplicate()
  2071. {
  2072. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2073. $this->Form->create();
  2074. $this->Form->control('text_val', [
  2075. 'type' => 'hidden',
  2076. 'value' => 'some text',
  2077. ]);
  2078. $expected = ['text_val' => 'some text'];
  2079. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2080. $this->assertEquals($expected, $result);
  2081. $this->Form->control('text_val', [
  2082. 'type' => 'text',
  2083. ]);
  2084. $expected = ['text_val'];
  2085. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2086. $this->assertEquals($expected, $result);
  2087. }
  2088. /**
  2089. * testFormSecuredFileControl method
  2090. *
  2091. * Tests that the correct keys are added to the field hash index.
  2092. *
  2093. * @return void
  2094. */
  2095. public function testFormSecuredFileControl()
  2096. {
  2097. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2098. $this->Form->create();
  2099. $this->Form->file('Attachment.file');
  2100. $expected = [
  2101. 'Attachment.file.name', 'Attachment.file.type',
  2102. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2103. 'Attachment.file.size',
  2104. ];
  2105. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2106. $this->assertEquals($expected, $result);
  2107. }
  2108. /**
  2109. * testFormSecuredMultipleSelect method
  2110. *
  2111. * Test that multiple selects keys are added to field hash.
  2112. *
  2113. * @return void
  2114. */
  2115. public function testFormSecuredMultipleSelect()
  2116. {
  2117. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2118. $this->Form->create();
  2119. $options = ['1' => 'one', '2' => 'two'];
  2120. $this->Form->select('Model.select', $options);
  2121. $expected = ['Model.select'];
  2122. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2123. $this->assertEquals($expected, $result);
  2124. $this->Form->fields = [];
  2125. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2126. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2127. $this->assertEquals($expected, $result);
  2128. }
  2129. /**
  2130. * testFormSecuredRadio method
  2131. *
  2132. * @return void
  2133. */
  2134. public function testFormSecuredRadio()
  2135. {
  2136. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2137. $this->Form->create();
  2138. $options = ['1' => 'option1', '2' => 'option2'];
  2139. $this->Form->radio('Test.test', $options);
  2140. $expected = ['Test.test'];
  2141. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2142. $this->assertEquals($expected, $result);
  2143. $this->Form->radio('Test.all', $options, [
  2144. 'disabled' => ['option1', 'option2'],
  2145. ]);
  2146. $expected = ['Test.test', 'Test.all' => ''];
  2147. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2148. $this->assertEquals($expected, $result);
  2149. $this->Form->radio('Test.some', $options, [
  2150. 'disabled' => ['option1'],
  2151. ]);
  2152. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2153. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2154. $this->assertEquals($expected, $result);
  2155. }
  2156. /**
  2157. * testFormSecuredAndDisabledNotAssoc method
  2158. *
  2159. * Test that when disabled is in a list based attribute array it works.
  2160. *
  2161. * @return void
  2162. */
  2163. public function testFormSecuredAndDisabledNotAssoc()
  2164. {
  2165. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2166. $this->Form->create();
  2167. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2168. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2169. $this->Form->text('Model.text', ['disabled']);
  2170. $this->Form->textarea('Model.textarea', ['disabled']);
  2171. $this->Form->password('Model.password', ['disabled']);
  2172. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2173. $expected = [
  2174. 'Model.radio' => '',
  2175. ];
  2176. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2177. $this->assertEquals($expected, $result);
  2178. }
  2179. /**
  2180. * testFormSecuredAndDisabled method
  2181. *
  2182. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2183. * hashing.
  2184. *
  2185. * @return void
  2186. */
  2187. public function testFormSecuredAndDisabled()
  2188. {
  2189. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2190. $this->Form->create();
  2191. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2192. $this->Form->text('Model.text', ['disabled' => true]);
  2193. $this->Form->password('Model.text', ['disabled' => true]);
  2194. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2195. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2196. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2197. $this->Form->year('Model.year', ['disabled' => true]);
  2198. $this->Form->month('Model.month', ['disabled' => true]);
  2199. $this->Form->day('Model.day', ['disabled' => true]);
  2200. $this->Form->hour('Model.hour', ['disabled' => true]);
  2201. $this->Form->minute('Model.minute', ['disabled' => true]);
  2202. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2203. $expected = [
  2204. 'Model.radio' => '',
  2205. ];
  2206. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2207. $this->assertEquals($expected, $result);
  2208. }
  2209. /**
  2210. * testUnlockFieldAddsToList method
  2211. *
  2212. * Test disableField.
  2213. *
  2214. * @return void
  2215. */
  2216. public function testUnlockFieldAddsToList()
  2217. {
  2218. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2219. 'unlockedFields' => [],
  2220. ]));
  2221. $this->Form->create();
  2222. $this->Form->unlockField('Contact.name');
  2223. $this->Form->text('Contact.name');
  2224. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2225. $this->assertEquals([], $result);
  2226. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  2227. $this->assertEquals(['Contact.name'], $result);
  2228. }
  2229. /**
  2230. * testUnlockFieldRemovingFromFields method
  2231. *
  2232. * Test unlockField removing from fields array.
  2233. *
  2234. * @return void
  2235. */
  2236. public function testUnlockFieldRemovingFromFields()
  2237. {
  2238. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2239. 'unlockedFields' => [],
  2240. ]));
  2241. $this->Form->create($this->article);
  2242. $this->Form->hidden('Article.id', ['value' => 1]);
  2243. $this->Form->text('Article.title');
  2244. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2245. $this->assertEquals(1, $result['Article.id'], 'Hidden input should be secured.');
  2246. $this->assertContains('Article.title', $result, 'Field should be secured.');
  2247. $this->Form->unlockField('Article.title');
  2248. $this->Form->unlockField('Article.id');
  2249. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2250. $this->assertEquals([], $result);
  2251. }
  2252. /**
  2253. * testResetUnlockFields method
  2254. *
  2255. * Test reset unlockFields, when create new form.
  2256. *
  2257. * @return void
  2258. */
  2259. public function testResetUnlockFields()
  2260. {
  2261. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', [
  2262. 'key' => 'testKey',
  2263. 'unlockedFields' => [],
  2264. ]));
  2265. $this->Form->create();
  2266. $this->Form->unlockField('Contact.id');
  2267. $this->Form->hidden('Contact.id', ['value' => 1]);
  2268. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2269. $this->assertEmpty($result, 'Field should be unlocked');
  2270. $this->Form->end();
  2271. $this->Form->create();
  2272. $this->Form->hidden('Contact.id', ['value' => 1]);
  2273. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  2274. $this->assertEquals(1, $result['Contact.id'], 'Hidden input should be secured.');
  2275. }
  2276. /**
  2277. * testSecuredFormUrlIgnoresHost method
  2278. *
  2279. * Test that only the path + query elements of a form's URL show up in their hash.
  2280. *
  2281. * @return void
  2282. */
  2283. public function testSecuredFormUrlIgnoresHost()
  2284. {
  2285. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'testKey']));
  2286. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2287. $this->Form->create($this->article, [
  2288. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]],
  2289. ]);
  2290. $result = $this->Form->secure();
  2291. $this->assertStringContainsString($expected, $result);
  2292. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2293. $result = $this->Form->secure();
  2294. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2295. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2296. $result = $this->Form->secure();
  2297. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2298. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2299. $result = $this->Form->secure();
  2300. $this->assertStringNotContainsString($expected, $result, 'URL is different');
  2301. }
  2302. /**
  2303. * testSecuredFormUrlHasHtmlAndIdentifier method
  2304. *
  2305. * Test that URL, HTML and identifier show up in their hashes.
  2306. *
  2307. * @return void
  2308. */
  2309. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2310. {
  2311. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  2312. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2313. $this->Form->create($this->article, [
  2314. 'url' => [
  2315. 'controller' => 'articles',
  2316. 'action' => 'view',
  2317. '?' => [
  2318. 'page' => 1,
  2319. 'limit' => 10,
  2320. 'html' => '<>"',
  2321. ],
  2322. '#' => 'result',
  2323. ],
  2324. ]);
  2325. $result = $this->Form->secure();
  2326. $this->assertStringContainsString($expected, $result);
  2327. $this->Form->create($this->article, [
  2328. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2329. ]);
  2330. $result = $this->Form->secure();
  2331. $this->assertStringContainsString($expected, $result, 'Full URL should only use path and query.');
  2332. $this->Form->create($this->article, [
  2333. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result',
  2334. ]);
  2335. $result = $this->Form->secure();
  2336. $this->assertStringContainsString($expected, $result, 'URL path + query should work.');
  2337. }
  2338. /**
  2339. * testErrorMessageDisplay method
  2340. *
  2341. * Test error message display.
  2342. *
  2343. * @return void
  2344. */
  2345. public function testErrorMessageDisplay()
  2346. {
  2347. $this->article['errors'] = [
  2348. 'Article' => [
  2349. 'title' => 'error message',
  2350. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2351. ],
  2352. ];
  2353. $this->Form->create($this->article);
  2354. $result = $this->Form->control('Article.title');
  2355. $expected = [
  2356. 'div' => ['class' => 'input text error'],
  2357. 'label' => ['for' => 'article-title'],
  2358. 'Title',
  2359. '/label',
  2360. 'input' => [
  2361. 'type' => 'text', 'name' => 'Article[title]',
  2362. 'id' => 'article-title', 'class' => 'form-error',
  2363. ],
  2364. ['div' => ['class' => 'error-message']],
  2365. 'error message',
  2366. '/div',
  2367. '/div',
  2368. ];
  2369. $this->assertHtml($expected, $result);
  2370. $result = $this->Form->control('Article.title', [
  2371. 'templates' => [
  2372. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>',
  2373. ],
  2374. ]);
  2375. $expected = [
  2376. 'div' => ['class' => 'input text error'],
  2377. 'label' => ['for' => 'article-title'],
  2378. 'Title',
  2379. '/label',
  2380. 'input' => [
  2381. 'type' => 'text', 'name' => 'Article[title]',
  2382. 'id' => 'article-title', 'class' => 'form-error',
  2383. ],
  2384. '/div',
  2385. ];
  2386. $this->assertHtml($expected, $result);
  2387. $result = $this->Form->control('Article.content');
  2388. $expected = [
  2389. 'div' => ['class' => 'input text error'],
  2390. 'label' => ['for' => 'article-content'],
  2391. 'Content',
  2392. '/label',
  2393. 'input' => [
  2394. 'type' => 'text', 'name' => 'Article[content]',
  2395. 'id' => 'article-content', 'class' => 'form-error',
  2396. ],
  2397. ['div' => ['class' => 'error-message']],
  2398. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2399. '/div',
  2400. '/div',
  2401. ];
  2402. $this->assertHtml($expected, $result);
  2403. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2404. $expected = [
  2405. 'div' => ['class' => 'input text error'],
  2406. 'label' => ['for' => 'article-content'],
  2407. 'Content',
  2408. '/label',
  2409. 'input' => [
  2410. 'type' => 'text', 'name' => 'Article[content]',
  2411. 'id' => 'article-content', 'class' => 'form-error',
  2412. ],
  2413. ['div' => ['class' => 'error-message']],
  2414. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2415. '/div',
  2416. '/div',
  2417. ];
  2418. $this->assertHtml($expected, $result);
  2419. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2420. $expected = [
  2421. 'div' => ['class' => 'input text error'],
  2422. 'label' => ['for' => 'article-content'],
  2423. 'Content',
  2424. '/label',
  2425. 'input' => [
  2426. 'type' => 'text', 'name' => 'Article[content]',
  2427. 'id' => 'article-content', 'class' => 'form-error',
  2428. ],
  2429. ['div' => ['class' => 'error-message']],
  2430. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2431. '/div',
  2432. '/div',
  2433. ];
  2434. $this->assertHtml($expected, $result);
  2435. }
  2436. /**
  2437. * testEmptyErrorValidation method
  2438. *
  2439. * Test validation errors, when validation message is an empty string.
  2440. *
  2441. * @return void
  2442. */
  2443. public function testEmptyErrorValidation()
  2444. {
  2445. $this->article['errors'] = [
  2446. 'Article' => ['title' => ''],
  2447. ];
  2448. $this->Form->create($this->article);
  2449. $result = $this->Form->control('Article.title');
  2450. $expected = [
  2451. 'div' => ['class' => 'input text error'],
  2452. 'label' => ['for' => 'article-title'],
  2453. 'Title',
  2454. '/label',
  2455. 'input' => [
  2456. 'type' => 'text', 'name' => 'Article[title]',
  2457. 'id' => 'article-title', 'class' => 'form-error',
  2458. ],
  2459. ['div' => ['class' => 'error-message']],
  2460. [],
  2461. '/div',
  2462. '/div',
  2463. ];
  2464. $this->assertHtml($expected, $result);
  2465. }
  2466. /**
  2467. * testEmptyControlErrorValidation method
  2468. *
  2469. * Test validation errors, when calling control() overriding validation message by an empty string.
  2470. *
  2471. * @return void
  2472. */
  2473. public function testEmptyControlErrorValidation()
  2474. {
  2475. $this->article['errors'] = [
  2476. 'Article' => ['title' => 'error message'],
  2477. ];
  2478. $this->Form->create($this->article);
  2479. $result = $this->Form->control('Article.title', ['error' => '']);
  2480. $expected = [
  2481. 'div' => ['class' => 'input text error'],
  2482. 'label' => ['for' => 'article-title'],
  2483. 'Title',
  2484. '/label',
  2485. 'input' => [
  2486. 'type' => 'text', 'name' => 'Article[title]',
  2487. 'id' => 'article-title', 'class' => 'form-error',
  2488. ],
  2489. ['div' => ['class' => 'error-message']],
  2490. [],
  2491. '/div',
  2492. '/div',
  2493. ];
  2494. $this->assertHtml($expected, $result);
  2495. }
  2496. /**
  2497. * testControlErrorMessage method
  2498. *
  2499. * Test validation errors, when calling control() overriding validation messages.
  2500. *
  2501. * @return void
  2502. */
  2503. public function testControlErrorMessage()
  2504. {
  2505. $this->article['errors'] = [
  2506. 'title' => ['error message'],
  2507. ];
  2508. $this->Form->create($this->article);
  2509. $result = $this->Form->control('title', [
  2510. 'error' => 'Custom error!',
  2511. ]);
  2512. $expected = [
  2513. 'div' => ['class' => 'input text required error'],
  2514. 'label' => ['for' => 'title'],
  2515. 'Title',
  2516. '/label',
  2517. 'input' => [
  2518. 'type' => 'text', 'name' => 'title',
  2519. 'id' => 'title', 'class' => 'form-error',
  2520. 'required' => 'required',
  2521. 'data-validity-message' => 'This field cannot be left empty',
  2522. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2523. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2524. ],
  2525. ['div' => ['class' => 'error-message']],
  2526. 'Custom error!',
  2527. '/div',
  2528. '/div',
  2529. ];
  2530. $this->assertHtml($expected, $result);
  2531. $result = $this->Form->control('title', [
  2532. 'error' => ['error message' => 'Custom error!'],
  2533. ]);
  2534. $expected = [
  2535. 'div' => ['class' => 'input text required error'],
  2536. 'label' => ['for' => 'title'],
  2537. 'Title',
  2538. '/label',
  2539. 'input' => [
  2540. 'type' => 'text',
  2541. 'name' => 'title',
  2542. 'id' => 'title',
  2543. 'class' => 'form-error',
  2544. 'required' => 'required',
  2545. 'data-validity-message' => 'This field cannot be left empty',
  2546. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  2547. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  2548. ],
  2549. ['div' => ['class' => 'error-message']],
  2550. 'Custom error!',
  2551. '/div',
  2552. '/div',
  2553. ];
  2554. $this->assertHtml($expected, $result);
  2555. }
  2556. /**
  2557. * testFormValidationAssociated method
  2558. *
  2559. * Tests displaying errors for nested entities.
  2560. *
  2561. * @return void
  2562. */
  2563. public function testFormValidationAssociated()
  2564. {
  2565. $nested = new Entity(['foo' => 'bar']);
  2566. $nested->setError('foo', ['not a valid bar']);
  2567. $entity = new Entity(['nested' => $nested]);
  2568. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2569. $result = $this->Form->error('nested.foo');
  2570. $this->assertSame('<div class="error-message">not a valid bar</div>', $result);
  2571. }
  2572. /**
  2573. * testFormValidationAssociatedSecondLevel method
  2574. *
  2575. * Test form error display with associated model.
  2576. *
  2577. * @return void
  2578. */
  2579. public function testFormValidationAssociatedSecondLevel()
  2580. {
  2581. $inner = new Entity(['bar' => 'baz']);
  2582. $nested = new Entity(['foo' => $inner]);
  2583. $entity = new Entity(['nested' => $nested]);
  2584. $inner->setError('bar', ['not a valid one']);
  2585. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2586. $result = $this->Form->error('nested.foo.bar');
  2587. $this->assertSame('<div class="error-message">not a valid one</div>', $result);
  2588. }
  2589. /**
  2590. * testFormValidationMultiRecord method
  2591. *
  2592. * Test form error display with multiple records.
  2593. *
  2594. * @return void
  2595. */
  2596. public function testFormValidationMultiRecord()
  2597. {
  2598. $one = new Entity();
  2599. $two = new Entity();
  2600. $this->getTableLocator()->get('Contacts', [
  2601. 'className' => ContactsTable::class,
  2602. ]);
  2603. $one->set('email', '');
  2604. $one->setError('email', ['invalid email']);
  2605. $two->set('name', '');
  2606. $two->setError('name', ['This is wrong']);
  2607. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2608. $result = $this->Form->control('0.email');
  2609. $expected = [
  2610. 'div' => ['class' => 'input email error'],
  2611. 'label' => ['for' => '0-email'],
  2612. 'Email',
  2613. '/label',
  2614. 'input' => [
  2615. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2616. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2617. ],
  2618. ['div' => ['class' => 'error-message']],
  2619. 'invalid email',
  2620. '/div',
  2621. '/div',
  2622. ];
  2623. $this->assertHtml($expected, $result);
  2624. $result = $this->Form->control('1.name');
  2625. $expected = [
  2626. 'div' => ['class' => 'input text error'],
  2627. 'label' => ['for' => '1-name'],
  2628. 'Name',
  2629. '/label',
  2630. 'input' => [
  2631. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2632. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2633. ],
  2634. ['div' => ['class' => 'error-message']],
  2635. 'This is wrong',
  2636. '/div',
  2637. '/div',
  2638. ];
  2639. $this->assertHtml($expected, $result);
  2640. }
  2641. /**
  2642. * testControl method
  2643. *
  2644. * Test various incarnations of control().
  2645. *
  2646. * @return void
  2647. */
  2648. public function testControl()
  2649. {
  2650. $this->getTableLocator()->get('ValidateUsers', [
  2651. 'className' => ValidateUsersTable::class,
  2652. ]);
  2653. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2654. $result = $this->Form->control('ValidateUsers.balance');
  2655. $expected = [
  2656. 'div' => ['class'],
  2657. 'label' => ['for'],
  2658. 'Balance',
  2659. '/label',
  2660. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2661. '/div',
  2662. ];
  2663. $this->assertHtml($expected, $result);
  2664. $result = $this->Form->control('ValidateUser.cost_decimal');
  2665. $expected = [
  2666. 'div' => ['class'],
  2667. 'label' => ['for'],
  2668. 'Cost Decimal',
  2669. '/label',
  2670. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2671. '/div',
  2672. ];
  2673. $this->assertHtml($expected, $result);
  2674. $result = $this->Form->control('ValidateUser.null_decimal');
  2675. $expected = [
  2676. 'div' => ['class'],
  2677. 'label' => ['for'],
  2678. 'Null Decimal',
  2679. '/label',
  2680. 'input' => ['name', 'type' => 'number', 'id'],
  2681. '/div',
  2682. ];
  2683. $this->assertHtml($expected, $result);
  2684. }
  2685. /**
  2686. * testControlCustomization method
  2687. *
  2688. * Tests the input method and passing custom options.
  2689. *
  2690. * @return void
  2691. */
  2692. public function testControlCustomization()
  2693. {
  2694. $this->getTableLocator()->get('Contacts', [
  2695. 'className' => ContactsTable::class,
  2696. ]);
  2697. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2698. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2699. $expected = [
  2700. 'div' => ['class' => 'input email'],
  2701. 'label' => ['for' => 'custom'],
  2702. 'Email',
  2703. '/label',
  2704. ['input' => [
  2705. 'type' => 'email', 'name' => 'Contact[email]',
  2706. 'id' => 'custom', 'maxlength' => 255,
  2707. ]],
  2708. '/div',
  2709. ];
  2710. $this->assertHtml($expected, $result);
  2711. $result = $this->Form->control('Contact.email', [
  2712. 'templates' => ['inputContainer' => '<div>{{content}}</div>'],
  2713. ]);
  2714. $expected = [
  2715. '<div',
  2716. 'label' => ['for' => 'contact-email'],
  2717. 'Email',
  2718. '/label',
  2719. ['input' => [
  2720. 'type' => 'email', 'name' => 'Contact[email]',
  2721. 'id' => 'contact-email', 'maxlength' => 255,
  2722. ]],
  2723. '/div',
  2724. ];
  2725. $this->assertHtml($expected, $result);
  2726. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2727. $expected = [
  2728. 'div' => ['class' => 'input text'],
  2729. 'label' => ['for' => 'contact-email'],
  2730. 'Email',
  2731. '/label',
  2732. ['input' => [
  2733. 'type' => 'text', 'name' => 'Contact[email]',
  2734. 'id' => 'contact-email', 'maxlength' => '255',
  2735. ]],
  2736. '/div',
  2737. ];
  2738. $this->assertHtml($expected, $result);
  2739. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2740. $expected = [
  2741. 'div' => ['class' => 'input text'],
  2742. 'label' => ['for' => 'contact-5-email'],
  2743. 'Email',
  2744. '/label',
  2745. ['input' => [
  2746. 'type' => 'text', 'name' => 'Contact[5][email]',
  2747. 'id' => 'contact-5-email', 'maxlength' => '255',
  2748. ]],
  2749. '/div',
  2750. ];
  2751. $this->assertHtml($expected, $result);
  2752. $result = $this->Form->control('Contact.password');
  2753. $expected = [
  2754. 'div' => ['class' => 'input password'],
  2755. 'label' => ['for' => 'contact-password'],
  2756. 'Password',
  2757. '/label',
  2758. ['input' => [
  2759. 'type' => 'password', 'name' => 'Contact[password]',
  2760. 'id' => 'contact-password',
  2761. ]],
  2762. '/div',
  2763. ];
  2764. $this->assertHtml($expected, $result);
  2765. $result = $this->Form->control('Contact.email', [
  2766. 'type' => 'file', 'class' => 'textbox',
  2767. ]);
  2768. $expected = [
  2769. 'div' => ['class' => 'input file'],
  2770. 'label' => ['for' => 'contact-email'],
  2771. 'Email',
  2772. '/label',
  2773. ['input' => [
  2774. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2775. 'id' => 'contact-email',
  2776. ]],
  2777. '/div',
  2778. ];
  2779. $this->assertHtml($expected, $result);
  2780. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2781. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2782. $result = $this->Form->control('phone');
  2783. $expected = [
  2784. 'div' => ['class' => 'input tel'],
  2785. 'label' => ['for' => 'phone'],
  2786. 'Phone',
  2787. '/label',
  2788. ['input' => [
  2789. 'type' => 'tel', 'name' => 'phone',
  2790. 'value' => 'Hello &amp; World &gt; weird chars',
  2791. 'id' => 'phone', 'maxlength' => 255,
  2792. ]],
  2793. '/div',
  2794. ];
  2795. $this->assertHtml($expected, $result);
  2796. $this->View->setRequest(
  2797. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2798. );
  2799. $this->Form->create();
  2800. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2801. $expected = [
  2802. 'div' => ['class' => 'input text'],
  2803. 'label' => ['for' => 'myId'],
  2804. 'Field',
  2805. '/label',
  2806. 'input' => [
  2807. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2808. 'value' => 'My value', 'id' => 'myId',
  2809. ],
  2810. '/div',
  2811. ];
  2812. $this->assertHtml($expected, $result);
  2813. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2814. $this->Form->create();
  2815. $entity->setError('field', 'Badness!');
  2816. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2817. $result = $this->Form->control('field');
  2818. $expected = [
  2819. 'div' => ['class' => 'input text error'],
  2820. 'label' => ['for' => 'field'],
  2821. 'Field',
  2822. '/label',
  2823. 'input' => [
  2824. 'type' => 'text', 'name' => 'field',
  2825. 'id' => 'field', 'class' => 'form-error',
  2826. ],
  2827. ['div' => ['class' => 'error-message']],
  2828. 'Badness!',
  2829. '/div',
  2830. '/div',
  2831. ];
  2832. $this->assertHtml($expected, $result);
  2833. $result = $this->Form->control('field', [
  2834. 'templates' => [
  2835. 'inputContainerError' => '{{content}}{{error}}',
  2836. 'error' => '<span class="error-message">{{content}}</span>',
  2837. ],
  2838. ]);
  2839. $expected = [
  2840. 'label' => ['for' => 'field'],
  2841. 'Field',
  2842. '/label',
  2843. 'input' => [
  2844. 'type' => 'text', 'name' => 'field',
  2845. 'id' => 'field', 'class' => 'form-error',
  2846. ],
  2847. ['span' => ['class' => 'error-message']],
  2848. 'Badness!',
  2849. '/span',
  2850. ];
  2851. $this->assertHtml($expected, $result);
  2852. $entity->setError('field', ['minLength'], true);
  2853. $result = $this->Form->control('field', [
  2854. 'error' => [
  2855. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2856. 'maxLength' => 'login too large',
  2857. ],
  2858. ]);
  2859. $expected = [
  2860. 'div' => ['class' => 'input text error'],
  2861. 'label' => ['for' => 'field'],
  2862. 'Field',
  2863. '/label',
  2864. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2865. ['div' => ['class' => 'error-message']],
  2866. 'Le login doit contenir au moins 2 caractères',
  2867. '/div',
  2868. '/div',
  2869. ];
  2870. $this->assertHtml($expected, $result);
  2871. $entity->setError('field', ['maxLength'], true);
  2872. $result = $this->Form->control('field', [
  2873. 'error' => [
  2874. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2875. 'maxLength' => 'login too large',
  2876. ],
  2877. ]);
  2878. $expected = [
  2879. 'div' => ['class' => 'input text error'],
  2880. 'label' => ['for' => 'field'],
  2881. 'Field',
  2882. '/label',
  2883. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2884. ['div' => ['class' => 'error-message']],
  2885. 'login too large',
  2886. '/div',
  2887. '/div',
  2888. ];
  2889. $this->assertHtml($expected, $result);
  2890. }
  2891. /**
  2892. * testControlWithTemplateFile method
  2893. *
  2894. * Test that control() accepts a template file.
  2895. *
  2896. * @return void
  2897. */
  2898. public function testControlWithTemplateFile()
  2899. {
  2900. $result = $this->Form->control('field', [
  2901. 'templates' => 'htmlhelper_tags',
  2902. ]);
  2903. $expected = [
  2904. 'label' => ['for' => 'field'],
  2905. 'Field',
  2906. '/label',
  2907. 'input' => [
  2908. 'type' => 'text', 'name' => 'field',
  2909. 'id' => 'field',
  2910. ],
  2911. ];
  2912. $this->assertHtml($expected, $result);
  2913. }
  2914. /**
  2915. * testNestedControlsEndWithBrackets method
  2916. *
  2917. * Test that nested inputs end with brackets.
  2918. *
  2919. * @return void
  2920. */
  2921. public function testNestedControlsEndWithBrackets()
  2922. {
  2923. $result = $this->Form->text('nested.text[]');
  2924. $expected = [
  2925. 'input' => [
  2926. 'type' => 'text', 'name' => 'nested[text][]',
  2927. ],
  2928. ];
  2929. $this->assertHtml($expected, $result);
  2930. $result = $this->Form->file('nested.file[]');
  2931. $expected = [
  2932. 'input' => [
  2933. 'type' => 'file', 'name' => 'nested[file][]',
  2934. ],
  2935. ];
  2936. $this->assertHtml($expected, $result);
  2937. }
  2938. /**
  2939. * testCreateIdPrefix method
  2940. *
  2941. * Test id prefix.
  2942. *
  2943. * @return void
  2944. */
  2945. public function testCreateIdPrefix()
  2946. {
  2947. $this->Form->create(null, ['idPrefix' => 'prefix']);
  2948. $result = $this->Form->control('field');
  2949. $expected = [
  2950. 'div' => ['class' => 'input text'],
  2951. 'label' => ['for' => 'prefix-field'],
  2952. 'Field',
  2953. '/label',
  2954. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2955. '/div',
  2956. ];
  2957. $this->assertHtml($expected, $result);
  2958. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2959. $expected = [
  2960. 'div' => ['class' => 'input text'],
  2961. 'label' => ['for' => 'custom-id'],
  2962. 'Field',
  2963. '/label',
  2964. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2965. '/div',
  2966. ];
  2967. $this->assertHtml($expected, $result);
  2968. $result = $this->Form->radio('Model.field', ['option A']);
  2969. $expected = [
  2970. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2971. 'label' => ['for' => 'prefix-model-field-0'],
  2972. ['input' => [
  2973. 'type' => 'radio',
  2974. 'name' => 'Model[field]',
  2975. 'value' => '0',
  2976. 'id' => 'prefix-model-field-0',
  2977. ]],
  2978. 'option A',
  2979. '/label',
  2980. ];
  2981. $this->assertHtml($expected, $result);
  2982. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2983. $expected = [
  2984. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2985. 'label' => ['for' => 'prefix-model-field-0'],
  2986. ['input' => [
  2987. 'type' => 'radio',
  2988. 'name' => 'Model[field]',
  2989. 'value' => '0',
  2990. 'id' => 'prefix-model-field-0',
  2991. ]],
  2992. 'option A',
  2993. '/label',
  2994. ];
  2995. $this->assertHtml($expected, $result);
  2996. $result = $this->Form->select(
  2997. 'Model.multi_field',
  2998. ['first'],
  2999. ['multiple' => 'checkbox']
  3000. );
  3001. $expected = [
  3002. 'input' => [
  3003. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  3004. ],
  3005. ['div' => ['class' => 'checkbox']],
  3006. ['label' => ['for' => 'prefix-model-multi-field-0']],
  3007. ['input' => [
  3008. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3009. 'value' => '0', 'id' => 'prefix-model-multi-field-0',
  3010. ]],
  3011. 'first',
  3012. '/label',
  3013. '/div',
  3014. ];
  3015. $this->assertHtml($expected, $result);
  3016. $this->Form->end();
  3017. $result = $this->Form->control('field');
  3018. $expected = [
  3019. 'div' => ['class' => 'input text'],
  3020. 'label' => ['for' => 'field'],
  3021. 'Field',
  3022. '/label',
  3023. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3024. '/div',
  3025. ];
  3026. $this->assertHtml($expected, $result);
  3027. }
  3028. /**
  3029. * testControlZero method
  3030. *
  3031. * Test that inputs with 0 can be created.
  3032. *
  3033. * @return void
  3034. */
  3035. public function testControlZero()
  3036. {
  3037. $this->getTableLocator()->get('Contacts', [
  3038. 'className' => ContactsTable::class,
  3039. ]);
  3040. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3041. $result = $this->Form->control('0');
  3042. $expected = [
  3043. 'div' => ['class' => 'input text'],
  3044. 'label' => ['for' => '0'], '/label',
  3045. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3046. '/div',
  3047. ];
  3048. $this->assertHtml($expected, $result);
  3049. }
  3050. /**
  3051. * testControlCheckbox method
  3052. *
  3053. * Test control() with checkbox creation.
  3054. *
  3055. * @return void
  3056. */
  3057. public function testControlCheckbox()
  3058. {
  3059. $articles = $this->getTableLocator()->get('Articles');
  3060. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3061. $article = $articles->newEmptyEntity();
  3062. $this->Form->create($article);
  3063. $result = $this->Form->control('Articles.active');
  3064. $expected = [
  3065. 'div' => ['class' => 'input checkbox'],
  3066. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3067. 'label' => ['for' => 'articles-active'],
  3068. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3069. 'Active',
  3070. '/label',
  3071. '/div',
  3072. ];
  3073. $this->assertHtml($expected, $result);
  3074. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3075. $expected = [
  3076. 'div' => ['class' => 'input checkbox'],
  3077. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3078. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3079. '/div',
  3080. ];
  3081. $this->assertHtml($expected, $result);
  3082. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3083. $expected = [
  3084. 'div' => ['class' => 'input checkbox'],
  3085. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3086. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3087. '/div',
  3088. ];
  3089. $this->assertHtml($expected, $result);
  3090. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3091. $expected = [
  3092. 'div' => ['class' => 'input checkbox'],
  3093. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3094. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3095. '/div',
  3096. ];
  3097. $this->assertHtml($expected, $result);
  3098. $result = $this->Form->control('Articles.disabled', [
  3099. 'label' => 'Disabled',
  3100. 'type' => 'checkbox',
  3101. 'data-foo' => 'disabled',
  3102. ]);
  3103. $expected = [
  3104. 'div' => ['class' => 'input checkbox'],
  3105. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3106. 'label' => ['for' => 'articles-disabled'],
  3107. ['input' => [
  3108. 'type' => 'checkbox',
  3109. 'name' => 'Articles[disabled]',
  3110. 'value' => '1',
  3111. 'id' => 'articles-disabled',
  3112. 'data-foo' => 'disabled',
  3113. ]],
  3114. 'Disabled',
  3115. '/label',
  3116. '/div',
  3117. ];
  3118. $this->assertHtml($expected, $result);
  3119. $result = $this->Form->control('Articles.confirm', [
  3120. 'label' => 'Confirm <b>me</b>!',
  3121. 'type' => 'checkbox',
  3122. 'escape' => false,
  3123. ]);
  3124. $expected = [
  3125. 'div' => ['class' => 'input checkbox'],
  3126. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3127. 'label' => ['for' => 'articles-confirm'],
  3128. ['input' => [
  3129. 'type' => 'checkbox',
  3130. 'name' => 'Articles[confirm]',
  3131. 'value' => '1',
  3132. 'id' => 'articles-confirm',
  3133. ]],
  3134. 'Confirm <b>me</b>!',
  3135. '/label',
  3136. '/div',
  3137. ];
  3138. $this->assertHtml($expected, $result);
  3139. }
  3140. /**
  3141. * testControlHidden method
  3142. *
  3143. * Test that control() does not create wrapping div and label tag for hidden fields.
  3144. *
  3145. * @return void
  3146. */
  3147. public function testControlHidden()
  3148. {
  3149. $this->getTableLocator()->get('ValidateUsers', [
  3150. 'className' => ValidateUsersTable::class,
  3151. ]);
  3152. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3153. $result = $this->Form->control('ValidateUser.id');
  3154. $expected = [
  3155. 'input' => ['name', 'type' => 'hidden', 'id'],
  3156. ];
  3157. $this->assertHtml($expected, $result);
  3158. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3159. $expected = [
  3160. 'input' => ['name', 'type' => 'hidden', 'id'],
  3161. ];
  3162. $this->assertHtml($expected, $result);
  3163. }
  3164. /**
  3165. * testControlDatetime method
  3166. *
  3167. * Test form->control() with datetime.
  3168. *
  3169. * @return void
  3170. */
  3171. public function testControlDatetime()
  3172. {
  3173. $result = $this->Form->control('prueba', [
  3174. 'type' => 'datetime',
  3175. 'value' => new FrozenTime('2019-09-27 02:52:43'),
  3176. ]);
  3177. $expected = [
  3178. 'div' => ['class' => 'input datetime'],
  3179. 'label' => ['for' => 'prueba'],
  3180. 'Prueba',
  3181. '/label',
  3182. 'input' => ['name' => 'prueba', 'id' => 'prueba', 'type' => 'datetime-local', 'value' => '2019-09-27T02:52:43'],
  3183. '/div',
  3184. ];
  3185. $this->assertHtml($expected, $result);
  3186. }
  3187. /**
  3188. * testControlDatetimeIdPrefix method
  3189. *
  3190. * Test form->control() with datetime with id prefix.
  3191. *
  3192. * @return void
  3193. */
  3194. public function testControlDatetimeIdPrefix()
  3195. {
  3196. $this->Form->create(null, ['idPrefix' => 'prefix']);
  3197. $result = $this->Form->control('prueba', [
  3198. 'type' => 'datetime',
  3199. ]);
  3200. $expected = [
  3201. 'div' => ['class' => 'input datetime'],
  3202. 'label' => ['for' => 'prefix-prueba'],
  3203. 'Prueba',
  3204. '/label',
  3205. 'input' => ['name' => 'prueba', 'id' => 'prefix-prueba', 'type' => 'datetime-local', 'value' => ''],
  3206. '/div',
  3207. ];
  3208. $this->assertHtml($expected, $result);
  3209. }
  3210. /**
  3211. * testControlCheckboxWithDisabledElements method
  3212. *
  3213. * Test generating checkboxes with disabled elements.
  3214. *
  3215. * @return void
  3216. */
  3217. public function testControlCheckboxWithDisabledElements()
  3218. {
  3219. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3220. $result = $this->Form->control('Contact.multiple', [
  3221. 'multiple' => 'checkbox',
  3222. 'disabled' => 'disabled',
  3223. 'options' => $options,
  3224. ]);
  3225. $expected = [
  3226. ['div' => ['class' => 'input select']],
  3227. ['label' => ['for' => 'contact-multiple']],
  3228. 'Multiple',
  3229. '/label',
  3230. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3231. ['div' => ['class' => 'checkbox']],
  3232. ['label' => ['for' => 'contact-multiple-1']],
  3233. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3234. 'One',
  3235. '/label',
  3236. '/div',
  3237. ['div' => ['class' => 'checkbox']],
  3238. ['label' => ['for' => 'contact-multiple-2']],
  3239. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3240. 'Two',
  3241. '/label',
  3242. '/div',
  3243. ['div' => ['class' => 'checkbox']],
  3244. ['label' => ['for' => 'contact-multiple-3']],
  3245. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3246. 'Three',
  3247. '/label',
  3248. '/div',
  3249. '/div',
  3250. ];
  3251. $this->assertHtml($expected, $result);
  3252. // make sure 50 does only disable 50, and not 50f5c0cf
  3253. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3254. $disabled = [50];
  3255. $expected = [
  3256. ['div' => ['class' => 'input select']],
  3257. ['label' => ['for' => 'contact-multiple']],
  3258. 'Multiple',
  3259. '/label',
  3260. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3261. ['div' => ['class' => 'checkbox']],
  3262. ['label' => ['for' => 'contact-multiple-50']],
  3263. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3264. 'Fifty',
  3265. '/label',
  3266. '/div',
  3267. ['div' => ['class' => 'checkbox']],
  3268. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3269. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3270. 'Stringy',
  3271. '/label',
  3272. '/div',
  3273. '/div',
  3274. ];
  3275. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3276. $this->assertHtml($expected, $result);
  3277. }
  3278. /**
  3279. * testControlWithLeadingInteger method
  3280. *
  3281. * Test input name with leading integer, ensure attributes are generated correctly.
  3282. *
  3283. * @return void
  3284. */
  3285. public function testControlWithLeadingInteger()
  3286. {
  3287. $result = $this->Form->text('0.Node.title');
  3288. $expected = [
  3289. 'input' => ['name' => '0[Node][title]', 'type' => 'text'],
  3290. ];
  3291. $this->assertHtml($expected, $result);
  3292. }
  3293. /**
  3294. * testControlSelectType method
  3295. *
  3296. * Test form->control() with select type inputs.
  3297. *
  3298. * @return void
  3299. */
  3300. public function testControlSelectType()
  3301. {
  3302. $result = $this->Form->control(
  3303. 'email',
  3304. [
  3305. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3306. );
  3307. $expected = [
  3308. 'div' => ['class' => 'input select'],
  3309. 'label' => ['for' => 'email'],
  3310. 'Email',
  3311. '/label',
  3312. ['select' => ['name' => 'email', 'id' => 'email']],
  3313. ['option' => ['value' => '']],
  3314. '/option',
  3315. ['option' => ['value' => 'è']],
  3316. 'Firést',
  3317. '/option',
  3318. ['option' => ['value' => 'é']],
  3319. 'Secoènd',
  3320. '/option',
  3321. '/select',
  3322. '/div',
  3323. ];
  3324. $this->assertHtml($expected, $result);
  3325. $result = $this->Form->control(
  3326. 'email',
  3327. [
  3328. 'options' => ['First', 'Second'], 'empty' => true]
  3329. );
  3330. $expected = [
  3331. 'div' => ['class' => 'input select'],
  3332. 'label' => ['for' => 'email'],
  3333. 'Email',
  3334. '/label',
  3335. ['select' => ['name' => 'email', 'id' => 'email']],
  3336. ['option' => ['value' => '']],
  3337. '/option',
  3338. ['option' => ['value' => '0']],
  3339. 'First',
  3340. '/option',
  3341. ['option' => ['value' => '1']],
  3342. 'Second',
  3343. '/option',
  3344. '/select',
  3345. '/div',
  3346. ];
  3347. $this->assertHtml($expected, $result);
  3348. $result = $this->Form->control('email', [
  3349. 'type' => 'select',
  3350. 'options' => new \ArrayObject(['First', 'Second']),
  3351. 'empty' => true,
  3352. ]);
  3353. $this->assertHtml($expected, $result);
  3354. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3355. $this->View->setRequest(
  3356. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3357. );
  3358. $this->Form->create();
  3359. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3360. $expected = [
  3361. 'div' => ['class' => 'input select'],
  3362. 'label' => ['for' => 'model-user-id'],
  3363. 'User',
  3364. '/label',
  3365. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3366. ['option' => ['value' => '']],
  3367. '/option',
  3368. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3369. 'good',
  3370. '/option',
  3371. ['option' => ['value' => 'other']],
  3372. 'bad',
  3373. '/option',
  3374. '/select',
  3375. '/div',
  3376. ];
  3377. $this->assertHtml($expected, $result);
  3378. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3379. $this->View->setRequest(
  3380. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3381. );
  3382. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3383. $expected = [
  3384. 'div' => ['class' => 'input select'],
  3385. 'label' => ['for' => 'thing-user-id'],
  3386. 'User',
  3387. '/label',
  3388. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3389. ['option' => ['value' => '']],
  3390. 'Some Empty',
  3391. '/option',
  3392. ['option' => ['value' => 'value']],
  3393. 'good',
  3394. '/option',
  3395. ['option' => ['value' => 'other']],
  3396. 'bad',
  3397. '/option',
  3398. '/select',
  3399. '/div',
  3400. ];
  3401. $this->assertHtml($expected, $result);
  3402. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3403. $this->View->setRequest(
  3404. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3405. );
  3406. $this->Form->create();
  3407. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3408. $expected = [
  3409. 'div' => ['class' => 'input select'],
  3410. 'label' => ['for' => 'thing-user-id'],
  3411. 'User',
  3412. '/label',
  3413. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3414. ['option' => ['value' => '']],
  3415. 'Some Empty',
  3416. '/option',
  3417. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3418. 'good',
  3419. '/option',
  3420. ['option' => ['value' => 'other']],
  3421. 'bad',
  3422. '/option',
  3423. '/select',
  3424. '/div',
  3425. ];
  3426. $this->assertHtml($expected, $result);
  3427. $result = $this->Form->control('Publisher.id', [
  3428. 'label' => 'Publisher',
  3429. 'type' => 'select',
  3430. 'multiple' => 'checkbox',
  3431. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2'],
  3432. ]);
  3433. $expected = [
  3434. ['div' => ['class' => 'input select']],
  3435. ['label' => ['for' => 'publisher-id']],
  3436. 'Publisher',
  3437. '/label',
  3438. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3439. ['div' => ['class' => 'checkbox']],
  3440. ['label' => ['for' => 'publisher-id-value-1']],
  3441. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3442. 'Label 1',
  3443. '/label',
  3444. '/div',
  3445. ['div' => ['class' => 'checkbox']],
  3446. ['label' => ['for' => 'publisher-id-value-2']],
  3447. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3448. 'Label 2',
  3449. '/label',
  3450. '/div',
  3451. '/div',
  3452. ];
  3453. $this->assertHtml($expected, $result);
  3454. }
  3455. /**
  3456. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3457. *
  3458. * Test that control() and a non standard primary key makes a hidden input by default.
  3459. *
  3460. * @return void
  3461. */
  3462. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3463. {
  3464. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3465. $this->Form->create($this->article);
  3466. $result = $this->Form->control('title');
  3467. $expected = [
  3468. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3469. ];
  3470. $this->assertHtml($expected, $result);
  3471. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3472. $this->Form->create($this->article);
  3473. $result = $this->Form->control('title');
  3474. $expected = [
  3475. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3476. ];
  3477. $this->assertHtml($expected, $result);
  3478. $result = $this->Form->control('body');
  3479. $expected = [
  3480. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3481. ];
  3482. $this->assertHtml($expected, $result);
  3483. }
  3484. /**
  3485. * testControlOverridingMagicSelectType method
  3486. *
  3487. * Test that overriding the magic select type widget is possible.
  3488. *
  3489. * @return void
  3490. */
  3491. public function testControlOverridingMagicSelectType()
  3492. {
  3493. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3494. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3495. $expected = [
  3496. 'div' => ['class' => 'input text'],
  3497. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3498. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3499. '/div',
  3500. ];
  3501. $this->assertHtml($expected, $result);
  3502. //Check that magic types still work for plural/singular vars
  3503. $this->View->set('types', ['value' => 'good', 'other' => 'bad']);
  3504. $result = $this->Form->control('Model.type');
  3505. $expected = [
  3506. 'div' => ['class' => 'input select'],
  3507. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3508. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3509. ['option' => ['value' => 'value']], 'good', '/option',
  3510. ['option' => ['value' => 'other']], 'bad', '/option',
  3511. '/select',
  3512. '/div',
  3513. ];
  3514. $this->assertHtml($expected, $result);
  3515. }
  3516. /**
  3517. * testControlMagicTypeDoesNotOverride method
  3518. *
  3519. * Test that inferred types do not override developer input.
  3520. *
  3521. * @return void
  3522. */
  3523. public function testControlMagicTypeDoesNotOverride()
  3524. {
  3525. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3526. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3527. $expected = [
  3528. 'div' => ['class' => 'input checkbox'],
  3529. ['input' => [
  3530. 'type' => 'hidden',
  3531. 'name' => 'Model[user]',
  3532. 'value' => 0,
  3533. ]],
  3534. 'label' => ['for' => 'model-user'],
  3535. ['input' => [
  3536. 'name' => 'Model[user]',
  3537. 'type' => 'checkbox',
  3538. 'id' => 'model-user',
  3539. 'value' => 1,
  3540. ]],
  3541. 'User',
  3542. '/label',
  3543. '/div',
  3544. ];
  3545. $this->assertHtml($expected, $result);
  3546. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3547. $options = [
  3548. 1 => 'blue',
  3549. 2 => 'red',
  3550. ];
  3551. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3552. $expected = [
  3553. 'div' => ['class' => 'input select'],
  3554. 'label' => ['for' => 'tags-ids'],
  3555. 'Tags',
  3556. '/label',
  3557. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3558. ['div' => ['class' => 'checkbox']],
  3559. ['label' => ['for' => 'tags-ids-1']],
  3560. ['input' => [
  3561. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3562. 'value' => '1', 'name' => 'tags[_ids][]',
  3563. ]],
  3564. 'blue',
  3565. '/label',
  3566. '/div',
  3567. ['div' => ['class' => 'checkbox']],
  3568. ['label' => ['for' => 'tags-ids-2']],
  3569. ['input' => [
  3570. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3571. 'value' => '2', 'name' => 'tags[_ids][]',
  3572. ]],
  3573. 'red',
  3574. '/label',
  3575. '/div',
  3576. '/div',
  3577. ];
  3578. $this->assertHtml($expected, $result);
  3579. }
  3580. /**
  3581. * testControlMagicSelectForTypeNumber method
  3582. *
  3583. * Test that magic control() selects are created for type=number.
  3584. *
  3585. * @return void
  3586. */
  3587. public function testControlMagicSelectForTypeNumber()
  3588. {
  3589. $this->getTableLocator()->get('ValidateUsers', [
  3590. 'className' => ValidateUsersTable::class,
  3591. ]);
  3592. $entity = new Entity(['balance' => 1]);
  3593. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3594. $this->View->set('balances', [0 => 'nothing', 1 => 'some', 100 => 'a lot']);
  3595. $result = $this->Form->control('balance');
  3596. $expected = [
  3597. 'div' => ['class' => 'input select'],
  3598. 'label' => ['for' => 'balance'],
  3599. 'Balance',
  3600. '/label',
  3601. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3602. ['option' => ['value' => '0']],
  3603. 'nothing',
  3604. '/option',
  3605. ['option' => ['value' => '1', 'selected' => 'selected']],
  3606. 'some',
  3607. '/option',
  3608. ['option' => ['value' => '100']],
  3609. 'a lot',
  3610. '/option',
  3611. '/select',
  3612. '/div',
  3613. ];
  3614. $this->assertHtml($expected, $result);
  3615. }
  3616. /**
  3617. * testInvalidControlTypeOption method
  3618. *
  3619. * Test invalid 'input' type option to control() function.
  3620. *
  3621. * @return void
  3622. */
  3623. public function testInvalidControlTypeOption()
  3624. {
  3625. $this->expectException(\RuntimeException::class);
  3626. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3627. $this->Form->control('text', ['type' => 'input']);
  3628. }
  3629. /**
  3630. * testControlMagicSelectChangeToRadio method
  3631. *
  3632. * Test that magic control() selects can easily be converted into radio types without error.
  3633. *
  3634. * @return void
  3635. */
  3636. public function testControlMagicSelectChangeToRadio()
  3637. {
  3638. $this->View->set('users', ['value' => 'good', 'other' => 'bad']);
  3639. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3640. $this->assertStringContainsString('input type="radio"', $result);
  3641. }
  3642. /**
  3643. * testFormControlSubmit method
  3644. *
  3645. * Test correct results for form::control() and type submit.
  3646. *
  3647. * @return void
  3648. */
  3649. public function testFormControlSubmit()
  3650. {
  3651. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3652. $expected = [
  3653. 'div' => ['class' => 'submit'],
  3654. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3655. '/div',
  3656. ];
  3657. $this->assertHtml($expected, $result);
  3658. }
  3659. /**
  3660. * testFormControls method
  3661. *
  3662. * Test correct results from Form::controls().
  3663. *
  3664. * @return void
  3665. */
  3666. public function testFormControlsLegendFieldset()
  3667. {
  3668. $this->Form->create($this->article);
  3669. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3670. $expected = [
  3671. '<fieldset',
  3672. '<legend',
  3673. 'The Legend',
  3674. '/legend',
  3675. '*/fieldset',
  3676. ];
  3677. $this->assertHtml($expected, $result);
  3678. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3679. $this->assertStringContainsString('<legend>Field of Dreams</legend>', $result);
  3680. $this->assertStringContainsString('<fieldset>', $result);
  3681. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3682. $this->assertStringNotContainsString('<legend>', $result);
  3683. $this->assertStringNotContainsString('<fieldset>', $result);
  3684. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3685. $this->assertStringNotContainsString('<legend>', $result);
  3686. $this->assertStringNotContainsString('<fieldset>', $result);
  3687. $this->Form->create($this->article);
  3688. $this->View->setRequest($this->View->getRequest()
  3689. ->withParam('prefix', 'admin')
  3690. ->withParam('action', 'admin_edit')
  3691. ->withParam('controller', 'articles'));
  3692. $result = $this->Form->allControls();
  3693. $expected = [
  3694. '<fieldset',
  3695. '<legend',
  3696. 'New Article',
  3697. '/legend',
  3698. '*/fieldset',
  3699. ];
  3700. $this->assertHtml($expected, $result);
  3701. $this->Form->create($this->article);
  3702. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3703. $expected = [
  3704. '<fieldset',
  3705. '<legend',
  3706. 'The Legend',
  3707. '/legend',
  3708. '*/fieldset',
  3709. ];
  3710. $this->assertHtml($expected, $result);
  3711. $this->Form->create($this->article);
  3712. $result = $this->Form->allControls([], [
  3713. 'fieldset' => [
  3714. 'class' => 'some-class some-other-class',
  3715. 'disabled' => true,
  3716. 'data-param' => 'a-param',
  3717. ],
  3718. 'legend' => 'The Legend',
  3719. ]);
  3720. $expected = [
  3721. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3722. '<legend',
  3723. 'The Legend',
  3724. '/legend',
  3725. '*/fieldset',
  3726. ];
  3727. $this->assertHtml($expected, $result);
  3728. }
  3729. /**
  3730. * testFormControls method
  3731. *
  3732. * Test the controls() method.
  3733. *
  3734. * @return void
  3735. */
  3736. public function testFormControls()
  3737. {
  3738. $this->Form->create($this->article);
  3739. $result = $this->Form->allControls();
  3740. $expected = [
  3741. '<fieldset',
  3742. '<legend', 'New Article', '/legend',
  3743. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3744. ['div' => ['class' => 'input select required']],
  3745. '*/div',
  3746. ['div' => ['class' => 'input text required']],
  3747. '*/div',
  3748. ['div' => ['class' => 'input text']],
  3749. '*/div',
  3750. ['div' => ['class' => 'input text']],
  3751. '*/div',
  3752. '/fieldset',
  3753. ];
  3754. $this->assertHtml($expected, $result);
  3755. $result = $this->Form->allControls([
  3756. 'published' => ['type' => 'boolean'],
  3757. ]);
  3758. $expected = [
  3759. '<fieldset',
  3760. '<legend', 'New Article', '/legend',
  3761. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3762. ['div' => ['class' => 'input select required']],
  3763. '*/div',
  3764. ['div' => ['class' => 'input text required']],
  3765. '*/div',
  3766. ['div' => ['class' => 'input text']],
  3767. '*/div',
  3768. ['div' => ['class' => 'input boolean']],
  3769. '*/div',
  3770. '/fieldset',
  3771. ];
  3772. $this->assertHtml($expected, $result);
  3773. $this->Form->create($this->article);
  3774. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3775. $expected = [
  3776. 'fieldset' => [],
  3777. 'legend' => [],
  3778. 'Hello',
  3779. '/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 text']],
  3788. '*/div',
  3789. '/fieldset',
  3790. ];
  3791. $this->assertHtml($expected, $result);
  3792. $this->Form->create();
  3793. $expected = [
  3794. 'fieldset' => [],
  3795. ['div' => ['class' => 'input text']],
  3796. 'label' => ['for' => 'foo'],
  3797. 'Foo',
  3798. '/label',
  3799. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3800. '*/div',
  3801. '/fieldset',
  3802. ];
  3803. $result = $this->Form->allControls(
  3804. ['foo' => ['type' => 'text']],
  3805. ['legend' => false]
  3806. );
  3807. $this->assertHtml($expected, $result);
  3808. }
  3809. /**
  3810. * testFormControlsBlacklist method
  3811. *
  3812. * @return void
  3813. */
  3814. public function testFormControlsBlacklist()
  3815. {
  3816. $this->Form->create($this->article);
  3817. $result = $this->Form->allControls([
  3818. 'id' => false,
  3819. ]);
  3820. $expected = [
  3821. '<fieldset',
  3822. '<legend', 'New Article', '/legend',
  3823. ['div' => ['class' => 'input select required']],
  3824. '*/div',
  3825. ['div' => ['class' => 'input text required']],
  3826. '*/div',
  3827. ['div' => ['class' => 'input text']],
  3828. '*/div',
  3829. ['div' => ['class' => 'input text']],
  3830. '*/div',
  3831. '/fieldset',
  3832. ];
  3833. $this->assertHtml($expected, $result);
  3834. $this->Form->create($this->article);
  3835. $result = $this->Form->allControls([
  3836. 'id' => [],
  3837. ]);
  3838. $expected = [
  3839. '<fieldset',
  3840. '<legend', 'New Article', '/legend',
  3841. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  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, true);
  3853. }
  3854. /**
  3855. * testSelectAsCheckbox method
  3856. *
  3857. * Test multi-select widget with checkbox formatting.
  3858. *
  3859. * @return void
  3860. */
  3861. public function testSelectAsCheckbox()
  3862. {
  3863. $result = $this->Form->select(
  3864. 'Model.multi_field',
  3865. ['first', 'second', 'third'],
  3866. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3867. );
  3868. $expected = [
  3869. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3870. ['div' => ['class' => 'checkbox']],
  3871. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3872. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3873. 'first',
  3874. '/label',
  3875. '/div',
  3876. ['div' => ['class' => 'checkbox']],
  3877. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3878. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3879. 'second',
  3880. '/label',
  3881. '/div',
  3882. ['div' => ['class' => 'checkbox']],
  3883. ['label' => ['for' => 'model-multi-field-2']],
  3884. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3885. 'third',
  3886. '/label',
  3887. '/div',
  3888. ];
  3889. $this->assertHtml($expected, $result);
  3890. $result = $this->Form->select(
  3891. 'Model.multi_field',
  3892. ['1/2' => 'half'],
  3893. ['multiple' => 'checkbox']
  3894. );
  3895. $expected = [
  3896. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3897. ['div' => ['class' => 'checkbox']],
  3898. ['label' => ['for' => 'model-multi-field-1-2']],
  3899. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3900. 'half',
  3901. '/label',
  3902. '/div',
  3903. ];
  3904. $this->assertHtml($expected, $result);
  3905. }
  3906. /**
  3907. * testLabel method
  3908. *
  3909. * Test label generation.
  3910. *
  3911. * @return void
  3912. */
  3913. public function testLabel()
  3914. {
  3915. $result = $this->Form->label('Person.name');
  3916. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3917. $this->assertHtml($expected, $result);
  3918. $result = $this->Form->label('Person.name');
  3919. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3920. $this->assertHtml($expected, $result);
  3921. $result = $this->Form->label('Person.first_name');
  3922. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3923. $this->assertHtml($expected, $result);
  3924. $result = $this->Form->label('Person.first_name', 'Your first name');
  3925. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3926. $this->assertHtml($expected, $result);
  3927. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3928. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3929. $this->assertHtml($expected, $result);
  3930. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3931. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3932. $this->assertHtml($expected, $result);
  3933. $result = $this->Form->label('Person.first_name', '');
  3934. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3935. $this->assertHtml($expected, $result);
  3936. $result = $this->Form->label('Person.2.name', '');
  3937. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3938. $this->assertHtml($expected, $result);
  3939. }
  3940. /**
  3941. * testLabelContainControl method
  3942. *
  3943. * Test that label() can accept an input with the correct template vars.
  3944. *
  3945. * @return void
  3946. */
  3947. public function testLabelContainControl()
  3948. {
  3949. $this->Form->setTemplates([
  3950. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3951. ]);
  3952. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3953. 'input' => '<input type="checkbox" name="accept_tos"/>',
  3954. ]);
  3955. $expected = [
  3956. 'label' => ['for' => 'person-accept-terms'],
  3957. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3958. 'Accept',
  3959. '/label',
  3960. ];
  3961. $this->assertHtml($expected, $result);
  3962. }
  3963. /**
  3964. * testTextbox method
  3965. *
  3966. * Test textbox element generation.
  3967. *
  3968. * @return void
  3969. */
  3970. public function testTextbox()
  3971. {
  3972. $result = $this->Form->text('Model.field');
  3973. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3974. $this->assertHtml($expected, $result);
  3975. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3976. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3977. $this->assertHtml($expected, $result);
  3978. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3979. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3980. $this->assertHtml($expected, $result);
  3981. }
  3982. /**
  3983. * testTextBoxDataAndError method
  3984. *
  3985. * Test that text() hooks up with request data and error fields.
  3986. *
  3987. * @return void
  3988. */
  3989. public function testTextBoxDataAndError()
  3990. {
  3991. $this->article['errors'] = [
  3992. 'Contact' => ['text' => 'wrong'],
  3993. ];
  3994. $this->View->setRequest($this->View->getRequest()
  3995. ->withData('Model.text', 'test <strong>HTML</strong> values')
  3996. ->withData('Contact.text', 'test'));
  3997. $this->Form->create($this->article);
  3998. $result = $this->Form->text('Model.text');
  3999. $expected = [
  4000. 'input' => [
  4001. 'type' => 'text',
  4002. 'name' => 'Model[text]',
  4003. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4004. ],
  4005. ];
  4006. $this->assertHtml($expected, $result);
  4007. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4008. $expected = [
  4009. 'input' => [
  4010. 'type' => 'text',
  4011. 'name' => 'Contact[text]',
  4012. 'value' => 'test',
  4013. 'id' => 'theID',
  4014. 'class' => 'form-error',
  4015. ],
  4016. ];
  4017. $this->assertHtml($expected, $result);
  4018. }
  4019. /**
  4020. * testDefaultValue method
  4021. *
  4022. * Test default value setting.
  4023. *
  4024. * @return void
  4025. */
  4026. public function testTextDefaultValue()
  4027. {
  4028. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  4029. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4030. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4031. $this->assertHtml($expected, $result);
  4032. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4033. $this->Form->create();
  4034. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4035. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4036. $this->assertHtml($expected, $result);
  4037. $Articles = $this->getTableLocator()->get('Articles');
  4038. $title = $Articles->getSchema()->getColumn('title');
  4039. $Articles->getSchema()->addColumn(
  4040. 'title',
  4041. ['default' => 'default title'] + $title
  4042. );
  4043. $entity = $Articles->newEmptyEntity();
  4044. $this->Form->create($entity);
  4045. // Get default value from schema
  4046. $result = $this->Form->text('title');
  4047. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4048. $this->assertHtml($expected, $result);
  4049. // Don't get value from schema
  4050. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4051. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4052. $this->assertHtml($expected, $result);
  4053. // Custom default value overrides default value from schema
  4054. $result = $this->Form->text('title', ['default' => 'override default']);
  4055. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4056. $this->assertHtml($expected, $result);
  4057. // Default value from schema is used only for new entities.
  4058. $entity->setNew(false);
  4059. $result = $this->Form->text('title');
  4060. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4061. $this->assertHtml($expected, $result);
  4062. }
  4063. /**
  4064. * testError method
  4065. *
  4066. * Test field error generation.
  4067. *
  4068. * @return void
  4069. */
  4070. public function testError()
  4071. {
  4072. $this->article['errors'] = [
  4073. 'Article' => ['field' => 'email'],
  4074. ];
  4075. $this->Form->create($this->article);
  4076. $result = $this->Form->error('Article.field');
  4077. $expected = [
  4078. ['div' => ['class' => 'error-message']],
  4079. 'email',
  4080. '/div',
  4081. ];
  4082. $this->assertHtml($expected, $result);
  4083. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4084. $expected = [
  4085. ['div' => ['class' => 'error-message']],
  4086. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4087. '/div',
  4088. ];
  4089. $this->assertHtml($expected, $result);
  4090. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4091. $expected = [
  4092. ['div' => ['class' => 'error-message']],
  4093. '<strong', 'Badness!', '/strong',
  4094. '/div',
  4095. ];
  4096. $this->assertHtml($expected, $result);
  4097. }
  4098. /**
  4099. * testErrorRuleName method
  4100. *
  4101. * Test error translation can use rule names for translating.
  4102. *
  4103. * @return void
  4104. */
  4105. public function testErrorRuleName()
  4106. {
  4107. $this->article['errors'] = [
  4108. 'Article' => [
  4109. 'field' => ['email' => 'Your email was not good'],
  4110. ],
  4111. ];
  4112. $this->Form->create($this->article);
  4113. $result = $this->Form->error('Article.field');
  4114. $expected = [
  4115. ['div' => ['class' => 'error-message']],
  4116. 'Your email was not good',
  4117. '/div',
  4118. ];
  4119. $this->assertHtml($expected, $result);
  4120. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4121. $expected = [
  4122. ['div' => ['class' => 'error-message']],
  4123. 'Email in use',
  4124. '/div',
  4125. ];
  4126. $this->assertHtml($expected, $result);
  4127. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4128. $expected = [
  4129. ['div' => ['class' => 'error-message']],
  4130. 'Email in use',
  4131. '/div',
  4132. ];
  4133. $this->assertHtml($expected, $result);
  4134. $result = $this->Form->error('Article.field', [
  4135. 'email' => 'Key is preferred',
  4136. 'Your email was not good' => 'Email in use',
  4137. ]);
  4138. $expected = [
  4139. ['div' => ['class' => 'error-message']],
  4140. 'Key is preferred',
  4141. '/div',
  4142. ];
  4143. $this->assertHtml($expected, $result);
  4144. }
  4145. /**
  4146. * testErrorMessages method
  4147. *
  4148. * Test error with nested lists.
  4149. *
  4150. * @return void
  4151. */
  4152. public function testErrorMessages()
  4153. {
  4154. $this->article['errors'] = [
  4155. 'Article' => ['field' => 'email'],
  4156. ];
  4157. $this->Form->create($this->article);
  4158. $result = $this->Form->error('Article.field', [
  4159. 'email' => 'No good!',
  4160. ]);
  4161. $expected = [
  4162. 'div' => ['class' => 'error-message'],
  4163. 'No good!',
  4164. '/div',
  4165. ];
  4166. $this->assertHtml($expected, $result);
  4167. }
  4168. /**
  4169. * testErrorMultipleMessages method
  4170. *
  4171. * Test error() with multiple messages.
  4172. *
  4173. * @return void
  4174. */
  4175. public function testErrorMultipleMessages()
  4176. {
  4177. $this->article['errors'] = [
  4178. 'field' => ['notBlank', 'email', 'Something else'],
  4179. ];
  4180. $this->Form->create($this->article);
  4181. $result = $this->Form->error('field', [
  4182. 'notBlank' => 'Cannot be empty',
  4183. 'email' => 'No good!',
  4184. ]);
  4185. $expected = [
  4186. 'div' => ['class' => 'error-message'],
  4187. 'ul' => [],
  4188. '<li', 'Cannot be empty', '/li',
  4189. '<li', 'No good!', '/li',
  4190. '<li', 'Something else', '/li',
  4191. '/ul',
  4192. '/div',
  4193. ];
  4194. $this->assertHtml($expected, $result);
  4195. }
  4196. /**
  4197. * testPassword method
  4198. *
  4199. * Test password element generation.
  4200. *
  4201. * @return void
  4202. */
  4203. public function testPassword()
  4204. {
  4205. $this->article['errors'] = [
  4206. 'Contact' => [
  4207. 'passwd' => 1,
  4208. ],
  4209. ];
  4210. $this->Form->create($this->article);
  4211. $result = $this->Form->password('Contact.field');
  4212. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4213. $this->assertHtml($expected, $result);
  4214. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4215. $this->Form->create($this->article);
  4216. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4217. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4218. $this->assertHtml($expected, $result);
  4219. }
  4220. /**
  4221. * testRadio method
  4222. *
  4223. * Test radio element set generation.
  4224. *
  4225. * @return void
  4226. */
  4227. public function testRadio()
  4228. {
  4229. $result = $this->Form->radio('Model.field', ['option A']);
  4230. $expected = [
  4231. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4232. 'label' => ['for' => 'model-field-0'],
  4233. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4234. 'option A',
  4235. '/label',
  4236. ];
  4237. $this->assertHtml($expected, $result);
  4238. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4239. $this->assertHtml($expected, $result);
  4240. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4241. $expected = [
  4242. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4243. ['label' => ['for' => 'model-field-0']],
  4244. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4245. 'option A',
  4246. '/label',
  4247. ['label' => ['for' => 'model-field-1']],
  4248. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4249. 'option B',
  4250. '/label',
  4251. ];
  4252. $this->assertHtml($expected, $result);
  4253. $result = $this->Form->radio(
  4254. 'Employee.gender',
  4255. ['male' => 'Male', 'female' => 'Female'],
  4256. ['form' => 'my-form']
  4257. );
  4258. $expected = [
  4259. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4260. ['label' => ['for' => 'employee-gender-male']],
  4261. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4262. 'Male',
  4263. '/label',
  4264. ['label' => ['for' => 'employee-gender-female']],
  4265. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4266. 'Female',
  4267. '/label',
  4268. ];
  4269. $this->assertHtml($expected, $result);
  4270. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4271. $expected = [
  4272. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4273. ['label' => ['for' => 'model-custom-0']],
  4274. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4275. 'option A',
  4276. '/label',
  4277. ['label' => ['for' => 'model-custom-1']],
  4278. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4279. 'option B',
  4280. '/label',
  4281. ];
  4282. $this->assertHtml($expected, $result);
  4283. $result = $this->Form->radio(
  4284. 'Employee.gender',
  4285. [
  4286. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4287. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4288. ]
  4289. );
  4290. $expected = [
  4291. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4292. ['label' => ['for' => 'employee-gender-male']],
  4293. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4294. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4295. 'Male',
  4296. '/label',
  4297. ['label' => ['for' => 'employee-gender-female']],
  4298. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4299. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4300. 'Female',
  4301. '/label',
  4302. ];
  4303. $this->assertHtml($expected, $result);
  4304. }
  4305. /**
  4306. * Test radio with complex options and empty disabled data.
  4307. *
  4308. * @return void
  4309. */
  4310. public function testRadioComplexDisabled()
  4311. {
  4312. $options = [
  4313. ['value' => 'r', 'text' => 'red'],
  4314. ['value' => 'b', 'text' => 'blue'],
  4315. ];
  4316. $attrs = ['disabled' => []];
  4317. $result = $this->Form->radio('Model.field', $options, $attrs);
  4318. $expected = [
  4319. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4320. ['label' => ['for' => 'model-field-r']],
  4321. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4322. 'red',
  4323. '/label',
  4324. ['label' => ['for' => 'model-field-b']],
  4325. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4326. 'blue',
  4327. '/label',
  4328. ];
  4329. $this->assertHtml($expected, $result);
  4330. $attrs = ['disabled' => ['r']];
  4331. $result = $this->Form->radio('Model.field', $options, $attrs);
  4332. $this->assertStringContainsString('disabled="disabled"', $result);
  4333. }
  4334. /**
  4335. * testRadioDefaultValue method
  4336. *
  4337. * Test default value setting on radio() method.
  4338. *
  4339. * @return void
  4340. */
  4341. public function testRadioDefaultValue()
  4342. {
  4343. $Articles = $this->getTableLocator()->get('Articles');
  4344. $title = $Articles->getSchema()->getColumn('title');
  4345. $Articles->getSchema()->addColumn(
  4346. 'title',
  4347. ['default' => '1'] + $title
  4348. );
  4349. $this->Form->create($Articles->newEmptyEntity());
  4350. $result = $this->Form->radio('title', ['option A', 'option B']);
  4351. $expected = [
  4352. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4353. ['label' => ['for' => 'title-0']],
  4354. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4355. 'option A',
  4356. '/label',
  4357. ['label' => ['for' => 'title-1']],
  4358. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4359. 'option B',
  4360. '/label',
  4361. ];
  4362. $this->assertHtml($expected, $result);
  4363. }
  4364. /**
  4365. * Test setting a hiddenField value on radio buttons.
  4366. *
  4367. * @return void
  4368. */
  4369. public function testRadioHiddenFieldValue()
  4370. {
  4371. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4372. $expected = [
  4373. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4374. 'label' => ['for' => 'title-0'],
  4375. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4376. 'option A',
  4377. '/label',
  4378. ];
  4379. $this->assertHtml($expected, $result);
  4380. }
  4381. /**
  4382. * testControlRadio method
  4383. *
  4384. * Test that input works with radio types.
  4385. *
  4386. * @return void
  4387. */
  4388. public function testControlRadio()
  4389. {
  4390. $result = $this->Form->control('test', [
  4391. 'type' => 'radio',
  4392. 'options' => ['A', 'B'],
  4393. ]);
  4394. $expected = [
  4395. ['div' => ['class' => 'input radio']],
  4396. '<label',
  4397. 'Test',
  4398. '/label',
  4399. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4400. ['label' => ['for' => 'test-0']],
  4401. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4402. 'A',
  4403. '/label',
  4404. ['label' => ['for' => 'test-1']],
  4405. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4406. 'B',
  4407. '/label',
  4408. '/div',
  4409. ];
  4410. $this->assertHtml($expected, $result);
  4411. $result = $this->Form->control('test', [
  4412. 'type' => 'radio',
  4413. 'options' => ['A', 'B'],
  4414. 'value' => '0',
  4415. ]);
  4416. $expected = [
  4417. ['div' => ['class' => 'input radio']],
  4418. '<label',
  4419. 'Test',
  4420. '/label',
  4421. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4422. ['label' => ['for' => 'test-0']],
  4423. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4424. 'A',
  4425. '/label',
  4426. ['label' => ['for' => 'test-1']],
  4427. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4428. 'B',
  4429. '/label',
  4430. '/div',
  4431. ];
  4432. $this->assertHtml($expected, $result);
  4433. $result = $this->Form->control('test', [
  4434. 'type' => 'radio',
  4435. 'options' => ['A', 'B'],
  4436. 'label' => false,
  4437. ]);
  4438. $expected = [
  4439. ['div' => ['class' => 'input radio']],
  4440. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4441. ['label' => ['for' => 'test-0']],
  4442. ['input' => ['type' => 'radio', '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. }
  4453. /**
  4454. * testRadioNoLabel method
  4455. *
  4456. * Test that radio() works with label = false.
  4457. *
  4458. * @return void
  4459. */
  4460. public function testRadioNoLabel()
  4461. {
  4462. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4463. $expected = [
  4464. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4465. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4466. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4467. ];
  4468. $this->assertHtml($expected, $result);
  4469. }
  4470. /**
  4471. * testRadioControlInsideLabel method
  4472. *
  4473. * Test generating radio input inside label ala twitter bootstrap.
  4474. *
  4475. * @return void
  4476. */
  4477. public function testRadioControlInsideLabel()
  4478. {
  4479. $this->Form->setTemplates([
  4480. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4481. 'radioWrapper' => '{{label}}',
  4482. ]);
  4483. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4484. // phpcs:disable
  4485. $expected = [
  4486. ['input' => [
  4487. 'type' => 'hidden',
  4488. 'name' => 'Model[field]',
  4489. 'value' => ''
  4490. ]],
  4491. ['label' => ['for' => 'model-field-0']],
  4492. ['input' => [
  4493. 'type' => 'radio',
  4494. 'name' => 'Model[field]',
  4495. 'value' => '0',
  4496. 'id' => 'model-field-0'
  4497. ]],
  4498. 'option A',
  4499. '/label',
  4500. ['label' => ['for' => 'model-field-1']],
  4501. ['input' => [
  4502. 'type' => 'radio',
  4503. 'name' => 'Model[field]',
  4504. 'value' => '1',
  4505. 'id' => 'model-field-1'
  4506. ]],
  4507. 'option B',
  4508. '/label',
  4509. ];
  4510. // phpcs:enable
  4511. $this->assertHtml($expected, $result);
  4512. }
  4513. /**
  4514. * testRadioHiddenControlDisabling method
  4515. *
  4516. * Test disabling the hidden input for radio buttons.
  4517. *
  4518. * @return void
  4519. */
  4520. public function testRadioHiddenControlDisabling()
  4521. {
  4522. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4523. $expected = [
  4524. 'label' => ['for' => 'model-1-field-0'],
  4525. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4526. 'option A',
  4527. '/label',
  4528. ];
  4529. $this->assertHtml($expected, $result);
  4530. }
  4531. /**
  4532. * testRadioOutOfRange method
  4533. *
  4534. * Test radio element set generation.
  4535. *
  4536. * @return void
  4537. */
  4538. public function testRadioOutOfRange()
  4539. {
  4540. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4541. $expected = [
  4542. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4543. 'label' => ['for' => 'model-field-v'],
  4544. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4545. 'value',
  4546. '/label',
  4547. ];
  4548. $this->assertHtml($expected, $result);
  4549. }
  4550. /**
  4551. * testSelect method
  4552. *
  4553. * Test select element generation.
  4554. *
  4555. * @return void
  4556. */
  4557. public function testSelect()
  4558. {
  4559. $result = $this->Form->select('Model.field', []);
  4560. $expected = [
  4561. 'select' => ['name' => 'Model[field]'],
  4562. '/select',
  4563. ];
  4564. $this->assertHtml($expected, $result);
  4565. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4566. $this->Form->create();
  4567. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4568. $expected = [
  4569. 'select' => ['name' => 'Model[field]'],
  4570. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4571. 'good',
  4572. '/option',
  4573. ['option' => ['value' => 'other']],
  4574. 'bad',
  4575. '/option',
  4576. '/select',
  4577. ];
  4578. $this->assertHtml($expected, $result);
  4579. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4580. $this->assertHtml($expected, $result);
  4581. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4582. $this->Form->create();
  4583. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4584. $expected = [
  4585. 'select' => ['name' => 'Model[field]'],
  4586. ['option' => ['value' => 'value']],
  4587. 'good',
  4588. '/option',
  4589. ['option' => ['value' => 'other']],
  4590. 'bad',
  4591. '/option',
  4592. '/select',
  4593. ];
  4594. $this->assertHtml($expected, $result);
  4595. $options = [
  4596. ['value' => 'first', 'text' => 'First'],
  4597. ['value' => 'first', 'text' => 'Another First'],
  4598. ];
  4599. $result = $this->Form->select(
  4600. 'Model.field',
  4601. $options,
  4602. ['escape' => false, 'empty' => false]
  4603. );
  4604. $expected = [
  4605. 'select' => ['name' => 'Model[field]'],
  4606. ['option' => ['value' => 'first']],
  4607. 'First',
  4608. '/option',
  4609. ['option' => ['value' => 'first']],
  4610. 'Another First',
  4611. '/option',
  4612. '/select',
  4613. ];
  4614. $this->assertHtml($expected, $result);
  4615. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4616. $this->Form->create();
  4617. $result = $this->Form->select(
  4618. 'Model.contact_id',
  4619. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4620. ['escape' => false, 'empty' => 'pick something']
  4621. );
  4622. $expected = [
  4623. 'select' => ['name' => 'Model[contact_id]'],
  4624. ['option' => ['value' => '']], 'pick something', '/option',
  4625. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4626. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4627. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4628. '/select',
  4629. ];
  4630. $this->assertHtml($expected, $result);
  4631. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4632. $this->Form->create();
  4633. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4634. $expected = [
  4635. 'select' => ['name' => 'Model[field]'],
  4636. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4637. ['option' => ['value' => '1']], 'Yes', '/option',
  4638. '/select',
  4639. ];
  4640. $this->assertHtml($expected, $result);
  4641. }
  4642. /**
  4643. * testSelectEscapeHtml method
  4644. *
  4645. * Test that select() escapes HTML.
  4646. *
  4647. * @return void
  4648. */
  4649. public function testSelectEscapeHtml()
  4650. {
  4651. $result = $this->Form->select(
  4652. 'Model.field',
  4653. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4654. ['empty' => false]
  4655. );
  4656. $expected = [
  4657. 'select' => ['name' => 'Model[field]'],
  4658. ['option' => ['value' => 'first']],
  4659. 'first &quot;html&quot; &lt;chars&gt;',
  4660. '/option',
  4661. ['option' => ['value' => 'second']],
  4662. 'value',
  4663. '/option',
  4664. '/select',
  4665. ];
  4666. $this->assertHtml($expected, $result);
  4667. $result = $this->Form->select(
  4668. 'Model.field',
  4669. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4670. ['escape' => false, 'empty' => false]
  4671. );
  4672. $expected = [
  4673. 'select' => ['name' => 'Model[field]'],
  4674. ['option' => ['value' => 'first']],
  4675. 'first "html" <chars>',
  4676. '/option',
  4677. ['option' => ['value' => 'second']],
  4678. 'value',
  4679. '/option',
  4680. '/select',
  4681. ];
  4682. $this->assertHtml($expected, $result);
  4683. }
  4684. /**
  4685. * testSelectRequired method
  4686. *
  4687. * Test select() with required and disabled attributes.
  4688. *
  4689. * @return void
  4690. */
  4691. public function testSelectRequired()
  4692. {
  4693. $this->article['required'] = [
  4694. 'user_id' => true,
  4695. ];
  4696. $this->Form->create($this->article);
  4697. $result = $this->Form->select('user_id', ['option A']);
  4698. $expected = [
  4699. 'select' => [
  4700. 'name' => 'user_id',
  4701. 'required' => 'required',
  4702. ],
  4703. ['option' => ['value' => '0']], 'option A', '/option',
  4704. '/select',
  4705. ];
  4706. $this->assertHtml($expected, $result);
  4707. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4708. $expected = [
  4709. 'select' => [
  4710. 'name' => 'user_id',
  4711. 'disabled' => 'disabled',
  4712. ],
  4713. ['option' => ['value' => '0']], 'option A', '/option',
  4714. '/select',
  4715. ];
  4716. $this->assertHtml($expected, $result);
  4717. }
  4718. /**
  4719. * testNestedSelect method
  4720. *
  4721. * Test select element generation with optgroups.
  4722. *
  4723. * @return void
  4724. */
  4725. public function testNestedSelect()
  4726. {
  4727. $result = $this->Form->select(
  4728. 'Model.field',
  4729. [1 => 'One', 2 => 'Two', 'Three' => [
  4730. 3 => 'Three', 4 => 'Four', 5 => 'Five',
  4731. ]],
  4732. ['empty' => false]
  4733. );
  4734. $expected = [
  4735. 'select' => ['name' => 'Model[field]'],
  4736. ['option' => ['value' => 1]],
  4737. 'One',
  4738. '/option',
  4739. ['option' => ['value' => 2]],
  4740. 'Two',
  4741. '/option',
  4742. ['optgroup' => ['label' => 'Three']],
  4743. ['option' => ['value' => 3]],
  4744. 'Three',
  4745. '/option',
  4746. ['option' => ['value' => 4]],
  4747. 'Four',
  4748. '/option',
  4749. ['option' => ['value' => 5]],
  4750. 'Five',
  4751. '/option',
  4752. '/optgroup',
  4753. '/select',
  4754. ];
  4755. $this->assertHtml($expected, $result);
  4756. }
  4757. /**
  4758. * testSelectMultiple method
  4759. *
  4760. * Test generation of multiple select elements.
  4761. *
  4762. * @return void
  4763. */
  4764. public function testSelectMultiple()
  4765. {
  4766. $options = ['first', 'second', 'third'];
  4767. $result = $this->Form->select(
  4768. 'Model.multi_field',
  4769. $options,
  4770. ['form' => 'my-form', 'multiple' => true]
  4771. );
  4772. $expected = [
  4773. 'input' => [
  4774. 'type' => 'hidden',
  4775. 'name' => 'Model[multi_field]',
  4776. 'value' => '',
  4777. 'form' => 'my-form',
  4778. ],
  4779. 'select' => [
  4780. 'name' => 'Model[multi_field][]',
  4781. 'multiple' => 'multiple',
  4782. 'form' => 'my-form',
  4783. ],
  4784. ['option' => ['value' => '0']],
  4785. 'first',
  4786. '/option',
  4787. ['option' => ['value' => '1']],
  4788. 'second',
  4789. '/option',
  4790. ['option' => ['value' => '2']],
  4791. 'third',
  4792. '/option',
  4793. '/select',
  4794. ];
  4795. $this->assertHtml($expected, $result);
  4796. $result = $this->Form->select(
  4797. 'Model.multi_field',
  4798. $options,
  4799. ['multiple' => 'multiple', 'form' => 'my-form']
  4800. );
  4801. $this->assertHtml($expected, $result);
  4802. $result = $this->Form->select(
  4803. 'Model.multi_field',
  4804. $options,
  4805. ['form' => 'my-form', 'multiple' => false]
  4806. );
  4807. $this->assertStringNotContainsString('multiple', $result);
  4808. }
  4809. /**
  4810. * testCheckboxZeroValue method
  4811. *
  4812. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4813. *
  4814. * @return void
  4815. */
  4816. public function testCheckboxZeroValue()
  4817. {
  4818. $result = $this->Form->control('User.get_spam', [
  4819. 'type' => 'checkbox',
  4820. 'value' => '0',
  4821. 'hiddenField' => '1',
  4822. ]);
  4823. $expected = [
  4824. 'div' => ['class' => 'input checkbox'],
  4825. 'label' => ['for' => 'user-get-spam'],
  4826. ['input' => [
  4827. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4828. 'value' => '1',
  4829. ]],
  4830. ['input' => [
  4831. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4832. 'value' => '0', 'id' => 'user-get-spam',
  4833. ]],
  4834. 'Get Spam',
  4835. '/label',
  4836. '/div',
  4837. ];
  4838. $this->assertHtml($expected, $result);
  4839. }
  4840. /**
  4841. * testHabtmSelectBox method
  4842. *
  4843. * Test generation of habtm select boxes.
  4844. *
  4845. * @return void
  4846. */
  4847. public function testHabtmSelectBox()
  4848. {
  4849. $this->loadFixtures('Articles');
  4850. $options = [
  4851. 1 => 'blue',
  4852. 2 => 'red',
  4853. 3 => 'green',
  4854. ];
  4855. $tags = [
  4856. new Entity(['id' => 1, 'name' => 'blue']),
  4857. new Entity(['id' => 3, 'name' => 'green']),
  4858. ];
  4859. $article = new Article(['tags' => $tags]);
  4860. $this->Form->create($article);
  4861. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4862. $expected = [
  4863. 'div' => ['class' => 'input select'],
  4864. 'label' => ['for' => 'tags-ids'],
  4865. 'Tags',
  4866. '/label',
  4867. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4868. 'select' => [
  4869. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4870. 'multiple' => 'multiple',
  4871. ],
  4872. ['option' => ['value' => '1', 'selected' => 'selected']],
  4873. 'blue',
  4874. '/option',
  4875. ['option' => ['value' => '2']],
  4876. 'red',
  4877. '/option',
  4878. ['option' => ['value' => '3', 'selected' => 'selected']],
  4879. 'green',
  4880. '/option',
  4881. '/select',
  4882. '/div',
  4883. ];
  4884. $this->assertHtml($expected, $result);
  4885. // make sure only 50 is selected, and not 50f5c0cf
  4886. $options = [
  4887. '1' => 'blue',
  4888. '50f5c0cf' => 'red',
  4889. '50' => 'green',
  4890. ];
  4891. $tags = [
  4892. new Entity(['id' => 1, 'name' => 'blue']),
  4893. new Entity(['id' => 50, 'name' => 'green']),
  4894. ];
  4895. $article = new Article(['tags' => $tags]);
  4896. $this->Form->create($article);
  4897. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4898. $expected = [
  4899. 'div' => ['class' => 'input select'],
  4900. 'label' => ['for' => 'tags-ids'],
  4901. 'Tags',
  4902. '/label',
  4903. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4904. 'select' => [
  4905. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4906. 'multiple' => 'multiple',
  4907. ],
  4908. ['option' => ['value' => '1', 'selected' => 'selected']],
  4909. 'blue',
  4910. '/option',
  4911. ['option' => ['value' => '50f5c0cf']],
  4912. 'red',
  4913. '/option',
  4914. ['option' => ['value' => '50', 'selected' => 'selected']],
  4915. 'green',
  4916. '/option',
  4917. '/select',
  4918. '/div',
  4919. ];
  4920. $this->assertHtml($expected, $result);
  4921. $spacecraft = [
  4922. 1 => 'Orion',
  4923. 2 => 'Helios',
  4924. ];
  4925. $this->View->set('spacecraft', $spacecraft);
  4926. $this->Form->create();
  4927. $result = $this->Form->control('spacecraft._ids');
  4928. $expected = [
  4929. 'div' => ['class' => 'input select'],
  4930. 'label' => ['for' => 'spacecraft-ids'],
  4931. 'Spacecraft',
  4932. '/label',
  4933. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4934. 'select' => [
  4935. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4936. 'multiple' => 'multiple',
  4937. ],
  4938. ['option' => ['value' => '1']],
  4939. 'Orion',
  4940. '/option',
  4941. ['option' => ['value' => '2']],
  4942. 'Helios',
  4943. '/option',
  4944. '/select',
  4945. '/div',
  4946. ];
  4947. $this->assertHtml($expected, $result);
  4948. }
  4949. /**
  4950. * testErrorsForBelongsToManySelect method
  4951. *
  4952. * Tests that errors for belongsToMany select fields are being
  4953. * picked up properly.
  4954. *
  4955. * @return void
  4956. */
  4957. public function testErrorsForBelongsToManySelect()
  4958. {
  4959. $spacecraft = [
  4960. 1 => 'Orion',
  4961. 2 => 'Helios',
  4962. ];
  4963. $this->View->set('spacecraft', $spacecraft);
  4964. $article = new Article();
  4965. $article->setError('spacecraft', ['Invalid']);
  4966. $this->Form->create($article);
  4967. $result = $this->Form->control('spacecraft._ids');
  4968. $expected = [
  4969. ['div' => ['class' => 'input select error']],
  4970. 'label' => ['for' => 'spacecraft-ids'],
  4971. 'Spacecraft',
  4972. '/label',
  4973. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4974. 'select' => [
  4975. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4976. 'multiple' => 'multiple',
  4977. ],
  4978. ['option' => ['value' => '1']],
  4979. 'Orion',
  4980. '/option',
  4981. ['option' => ['value' => '2']],
  4982. 'Helios',
  4983. '/option',
  4984. '/select',
  4985. ['div' => ['class' => 'error-message']],
  4986. 'Invalid',
  4987. '/div',
  4988. '/div',
  4989. ];
  4990. $this->assertHtml($expected, $result);
  4991. }
  4992. /**
  4993. * testSelectMultipleCheckboxes method
  4994. *
  4995. * Test generation of multi select elements in checkbox format.
  4996. *
  4997. * @return void
  4998. */
  4999. public function testSelectMultipleCheckboxes()
  5000. {
  5001. $result = $this->Form->select(
  5002. 'Model.multi_field',
  5003. ['first', 'second', 'third'],
  5004. ['multiple' => 'checkbox']
  5005. );
  5006. $expected = [
  5007. 'input' => [
  5008. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5009. ],
  5010. ['div' => ['class' => 'checkbox']],
  5011. ['label' => ['for' => 'model-multi-field-0']],
  5012. ['input' => [
  5013. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5014. 'value' => '0', 'id' => 'model-multi-field-0',
  5015. ]],
  5016. 'first',
  5017. '/label',
  5018. '/div',
  5019. ['div' => ['class' => 'checkbox']],
  5020. ['label' => ['for' => 'model-multi-field-1']],
  5021. ['input' => [
  5022. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5023. 'value' => '1', 'id' => 'model-multi-field-1',
  5024. ]],
  5025. 'second',
  5026. '/label',
  5027. '/div',
  5028. ['div' => ['class' => 'checkbox']],
  5029. ['label' => ['for' => 'model-multi-field-2']],
  5030. ['input' => [
  5031. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5032. 'value' => '2', 'id' => 'model-multi-field-2',
  5033. ]],
  5034. 'third',
  5035. '/label',
  5036. '/div',
  5037. ];
  5038. $this->assertHtml($expected, $result);
  5039. $result = $this->Form->select(
  5040. 'Model.multi_field',
  5041. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5042. ['multiple' => 'checkbox']
  5043. );
  5044. $expected = [
  5045. 'input' => [
  5046. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5047. ],
  5048. ['div' => ['class' => 'checkbox']],
  5049. ['label' => ['for' => 'model-multi-field-a+']],
  5050. ['input' => [
  5051. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5052. 'value' => 'a+', 'id' => 'model-multi-field-a+',
  5053. ]],
  5054. 'first',
  5055. '/label',
  5056. '/div',
  5057. ['div' => ['class' => 'checkbox']],
  5058. ['label' => ['for' => 'model-multi-field-a++']],
  5059. ['input' => [
  5060. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5061. 'value' => 'a++', 'id' => 'model-multi-field-a++',
  5062. ]],
  5063. 'second',
  5064. '/label',
  5065. '/div',
  5066. ['div' => ['class' => 'checkbox']],
  5067. ['label' => ['for' => 'model-multi-field-a+++']],
  5068. ['input' => [
  5069. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5070. 'value' => 'a+++', 'id' => 'model-multi-field-a+++',
  5071. ]],
  5072. 'third',
  5073. '/label',
  5074. '/div',
  5075. ];
  5076. $this->assertHtml($expected, $result);
  5077. $result = $this->Form->select(
  5078. 'Model.multi_field',
  5079. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5080. ['multiple' => 'checkbox']
  5081. );
  5082. $expected = [
  5083. 'input' => [
  5084. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '',
  5085. ],
  5086. ['div' => ['class' => 'checkbox']],
  5087. ['label' => ['for' => 'model-multi-field-a-b']],
  5088. ['input' => [
  5089. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5090. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b',
  5091. ]],
  5092. 'first',
  5093. '/label',
  5094. '/div',
  5095. ['div' => ['class' => 'checkbox']],
  5096. ['label' => ['for' => 'model-multi-field-a-b1']],
  5097. ['input' => [
  5098. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5099. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1',
  5100. ]],
  5101. 'second',
  5102. '/label',
  5103. '/div',
  5104. ['div' => ['class' => 'checkbox']],
  5105. ['label' => ['for' => 'model-multi-field-a-b2']],
  5106. ['input' => [
  5107. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5108. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2',
  5109. ]],
  5110. 'third',
  5111. '/label',
  5112. '/div',
  5113. ];
  5114. $this->assertHtml($expected, $result);
  5115. }
  5116. /**
  5117. * testSelectMultipleCheckboxRequestData method
  5118. *
  5119. * Ensure that multiCheckbox reads from the request data.
  5120. *
  5121. * @return void
  5122. */
  5123. public function testSelectMultipleCheckboxRequestData()
  5124. {
  5125. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5126. $result = $this->Form->select(
  5127. 'Model.tags',
  5128. ['1' => 'first', 'Array' => 'Array'],
  5129. ['multiple' => 'checkbox']
  5130. );
  5131. $expected = [
  5132. 'input' => [
  5133. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '',
  5134. ],
  5135. ['div' => ['class' => 'checkbox']],
  5136. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5137. ['input' => [
  5138. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5139. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked',
  5140. ]],
  5141. 'first',
  5142. '/label',
  5143. '/div',
  5144. ['div' => ['class' => 'checkbox']],
  5145. ['label' => ['for' => 'model-tags-array']],
  5146. ['input' => [
  5147. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5148. 'value' => 'Array', 'id' => 'model-tags-array',
  5149. ]],
  5150. 'Array',
  5151. '/label',
  5152. '/div',
  5153. ];
  5154. $this->assertHtml($expected, $result);
  5155. }
  5156. /**
  5157. * testSelectMultipleCheckboxSecurity method
  5158. *
  5159. * Checks the security hash array generated for multiple-input checkbox elements.
  5160. *
  5161. * @return void
  5162. */
  5163. public function testSelectMultipleCheckboxSecurity()
  5164. {
  5165. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5166. $this->Form->create();
  5167. $this->Form->select(
  5168. 'Model.multi_field',
  5169. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5170. ['multiple' => 'checkbox']
  5171. );
  5172. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5173. $this->assertEquals(['Model.multi_field'], $fields);
  5174. $result = $this->Form->secure();
  5175. $hash = hash_hmac('sha1', $this->url . serialize($fields) . session_id(), Security::getSalt());
  5176. $hash = urlencode($hash . ':');
  5177. $this->assertStringContainsString('"' . $hash . '"', $result);
  5178. }
  5179. /**
  5180. * testSelectMultipleSecureWithNoOptions method
  5181. *
  5182. * Multiple select elements should always be secured as they always participate
  5183. * in the POST data.
  5184. *
  5185. * @return void
  5186. */
  5187. public function testSelectMultipleSecureWithNoOptions()
  5188. {
  5189. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5190. $this->Form->create();
  5191. $this->Form->select(
  5192. 'Model.select',
  5193. [],
  5194. ['multiple' => true]
  5195. );
  5196. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5197. $this->assertEquals(['Model.select'], $result);
  5198. }
  5199. /**
  5200. * testSelectNoSecureWithNoOptions method
  5201. *
  5202. * When a select box has no options it should not be added to the fields list
  5203. * as it always fail post validation.
  5204. *
  5205. * @return void
  5206. */
  5207. public function testSelectNoSecureWithNoOptions()
  5208. {
  5209. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  5210. $this->Form->create();
  5211. $this->Form->select(
  5212. 'Model.select',
  5213. []
  5214. );
  5215. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5216. $this->assertEquals([], $result);
  5217. $this->Form->select(
  5218. 'Model.user_id',
  5219. [],
  5220. ['empty' => true]
  5221. );
  5222. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5223. $this->assertEquals(['Model.user_id'], $result);
  5224. }
  5225. /**
  5226. * testControlMultipleCheckboxes method
  5227. *
  5228. * Test control() resulting in multi select elements being generated.
  5229. *
  5230. * @return void
  5231. */
  5232. public function testControlMultipleCheckboxes()
  5233. {
  5234. $result = $this->Form->control('Model.multi_field', [
  5235. 'options' => ['first', 'second', 'third'],
  5236. 'multiple' => 'checkbox',
  5237. ]);
  5238. $expected = [
  5239. ['div' => ['class' => 'input select']],
  5240. ['label' => ['for' => 'model-multi-field']],
  5241. 'Multi Field',
  5242. '/label',
  5243. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5244. ['div' => ['class' => 'checkbox']],
  5245. ['label' => ['for' => 'model-multi-field-0']],
  5246. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5247. 'first',
  5248. '/label',
  5249. '/div',
  5250. ['div' => ['class' => 'checkbox']],
  5251. ['label' => ['for' => 'model-multi-field-1']],
  5252. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5253. 'second',
  5254. '/label',
  5255. '/div',
  5256. ['div' => ['class' => 'checkbox']],
  5257. ['label' => ['for' => 'model-multi-field-2']],
  5258. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5259. 'third',
  5260. '/label',
  5261. '/div',
  5262. '/div',
  5263. ];
  5264. $this->assertHtml($expected, $result);
  5265. $result = $this->Form->control('Model.multi_field', [
  5266. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5267. 'multiple' => 'checkbox',
  5268. ]);
  5269. $expected = [
  5270. ['div' => ['class' => 'input select']],
  5271. ['label' => ['for' => 'model-multi-field']],
  5272. 'Multi Field',
  5273. '/label',
  5274. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5275. ['div' => ['class' => 'checkbox']],
  5276. ['label' => ['for' => 'model-multi-field-a']],
  5277. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5278. 'first',
  5279. '/label',
  5280. '/div',
  5281. ['div' => ['class' => 'checkbox']],
  5282. ['label' => ['for' => 'model-multi-field-b']],
  5283. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5284. 'second',
  5285. '/label',
  5286. '/div',
  5287. ['div' => ['class' => 'checkbox']],
  5288. ['label' => ['for' => 'model-multi-field-c']],
  5289. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5290. 'third',
  5291. '/label',
  5292. '/div',
  5293. '/div',
  5294. ];
  5295. $this->assertHtml($expected, $result);
  5296. }
  5297. /**
  5298. * testSelectHiddenFieldOmission method
  5299. *
  5300. * Test that select() with 'hiddenField' => false omits the hidden field.
  5301. *
  5302. * @return void
  5303. */
  5304. public function testSelectHiddenFieldOmission()
  5305. {
  5306. $result = $this->Form->select(
  5307. 'Model.multi_field',
  5308. ['first', 'second'],
  5309. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5310. );
  5311. $this->assertStringNotContainsString('type="hidden"', $result);
  5312. }
  5313. /**
  5314. * testSelectCheckboxMultipleOverrideName method
  5315. *
  5316. * Test that select() with multiple = checkbox works with overriding name attribute.
  5317. *
  5318. * @return void
  5319. */
  5320. public function testSelectCheckboxMultipleOverrideName()
  5321. {
  5322. $result = $this->Form->select('category', ['1', '2'], [
  5323. 'multiple' => 'checkbox',
  5324. 'name' => 'fish',
  5325. ]);
  5326. $expected = [
  5327. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5328. ['div' => ['class' => 'checkbox']],
  5329. ['label' => ['for' => 'fish-0']],
  5330. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5331. '1',
  5332. '/label',
  5333. '/div',
  5334. ['div' => ['class' => 'checkbox']],
  5335. ['label' => ['for' => 'fish-1']],
  5336. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5337. '2',
  5338. '/label',
  5339. '/div',
  5340. ];
  5341. $this->assertHtml($expected, $result);
  5342. $result = $this->Form->multiCheckbox(
  5343. 'category',
  5344. new Collection(['1', '2']),
  5345. ['name' => 'fish']
  5346. );
  5347. $this->assertHtml($expected, $result);
  5348. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5349. 'name' => 'fish',
  5350. ]);
  5351. $this->assertHtml($expected, $result);
  5352. }
  5353. /**
  5354. * testControlMultiCheckbox method
  5355. *
  5356. * Test that control() works with multicheckbox.
  5357. *
  5358. * @return void
  5359. */
  5360. public function testControlMultiCheckbox()
  5361. {
  5362. $result = $this->Form->control('category', [
  5363. 'type' => 'multicheckbox',
  5364. 'options' => ['1', '2'],
  5365. ]);
  5366. $expected = [
  5367. ['div' => ['class' => 'input multicheckbox']],
  5368. '<label',
  5369. 'Category',
  5370. '/label',
  5371. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5372. ['div' => ['class' => 'checkbox']],
  5373. ['label' => ['for' => 'category-0']],
  5374. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5375. '1',
  5376. '/label',
  5377. '/div',
  5378. ['div' => ['class' => 'checkbox']],
  5379. ['label' => ['for' => 'category-1']],
  5380. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5381. '2',
  5382. '/label',
  5383. '/div',
  5384. '/div',
  5385. ];
  5386. $this->assertHtml($expected, $result);
  5387. }
  5388. /**
  5389. * testCheckbox method
  5390. *
  5391. * Test generation of checkboxes.
  5392. *
  5393. * @return void
  5394. */
  5395. public function testCheckbox()
  5396. {
  5397. $result = $this->Form->checkbox('Model.field');
  5398. $expected = [
  5399. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5400. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']],
  5401. ];
  5402. $this->assertHtml($expected, $result);
  5403. $result = $this->Form->checkbox('Model.field', [
  5404. 'id' => 'theID',
  5405. 'value' => 'myvalue',
  5406. 'form' => 'my-form',
  5407. ]);
  5408. $expected = [
  5409. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5410. ['input' => [
  5411. 'type' => 'checkbox', 'name' => 'Model[field]',
  5412. 'value' => 'myvalue', 'id' => 'theID',
  5413. 'form' => 'my-form',
  5414. ]],
  5415. ];
  5416. $this->assertHtml($expected, $result);
  5417. }
  5418. /**
  5419. * testCheckboxDefaultValue method
  5420. *
  5421. * Test default value setting on checkbox() method.
  5422. *
  5423. * @return void
  5424. */
  5425. public function testCheckboxDefaultValue()
  5426. {
  5427. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5428. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5429. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5430. $this->assertHtml($expected, $result);
  5431. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5432. $this->Form->create();
  5433. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5434. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5435. $this->assertHtml($expected, $result);
  5436. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5437. $this->Form->create();
  5438. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5439. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5440. $this->assertHtml($expected, $result);
  5441. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5442. $this->Form->create();
  5443. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5444. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5445. $this->assertHtml($expected, $result);
  5446. $Articles = $this->getTableLocator()->get('Articles');
  5447. $Articles->getSchema()->addColumn(
  5448. 'published',
  5449. ['type' => 'boolean', 'null' => false, 'default' => true]
  5450. );
  5451. $this->Form->create($Articles->newEmptyEntity());
  5452. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5453. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5454. $this->assertHtml($expected, $result);
  5455. }
  5456. /**
  5457. * testCheckboxCheckedAndError method
  5458. *
  5459. * Test checkbox being checked or having errors.
  5460. *
  5461. * @return void
  5462. */
  5463. public function testCheckboxCheckedAndError()
  5464. {
  5465. $this->article['errors'] = [
  5466. 'published' => true,
  5467. ];
  5468. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5469. $this->Form->create($this->article);
  5470. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5471. $expected = [
  5472. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5473. ['input' => [
  5474. 'type' => 'checkbox',
  5475. 'name' => 'published',
  5476. 'value' => 'myvalue',
  5477. 'id' => 'theID',
  5478. 'checked' => 'checked',
  5479. 'class' => 'form-error',
  5480. ]],
  5481. ];
  5482. $this->assertHtml($expected, $result);
  5483. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5484. $this->Form->create($this->article);
  5485. $result = $this->Form->checkbox('published');
  5486. $expected = [
  5487. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5488. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']],
  5489. ];
  5490. $this->assertHtml($expected, $result);
  5491. }
  5492. /**
  5493. * testCheckboxCustomNameAttribute method
  5494. *
  5495. * Test checkbox() with a custom name attribute.
  5496. *
  5497. * @return void
  5498. */
  5499. public function testCheckboxCustomNameAttribute()
  5500. {
  5501. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5502. $expected = [
  5503. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5504. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']],
  5505. ];
  5506. $this->assertHtml($expected, $result);
  5507. }
  5508. /**
  5509. * testCheckboxHiddenField method
  5510. *
  5511. * Test that the hidden input for checkboxes can be omitted or set to a
  5512. * specific value.
  5513. *
  5514. * @return void
  5515. */
  5516. public function testCheckboxHiddenField()
  5517. {
  5518. $result = $this->Form->checkbox('UserForm.something', [
  5519. 'hiddenField' => false,
  5520. ]);
  5521. $expected = [
  5522. 'input' => [
  5523. 'type' => 'checkbox',
  5524. 'name' => 'UserForm[something]',
  5525. 'value' => '1',
  5526. ],
  5527. ];
  5528. $this->assertHtml($expected, $result);
  5529. $result = $this->Form->checkbox('UserForm.something', [
  5530. 'value' => 'Y',
  5531. 'hiddenField' => 'N',
  5532. ]);
  5533. $expected = [
  5534. ['input' => [
  5535. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5536. 'value' => 'N',
  5537. ]],
  5538. ['input' => [
  5539. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5540. 'value' => 'Y',
  5541. ]],
  5542. ];
  5543. $this->assertHtml($expected, $result);
  5544. }
  5545. /**
  5546. * testTime method
  5547. *
  5548. * Test the time type.
  5549. *
  5550. * @return void
  5551. */
  5552. public function testTime()
  5553. {
  5554. $result = $this->Form->time('start_time', [
  5555. 'value' => '2014-03-08 16:30:00',
  5556. ]);
  5557. $expected = [
  5558. 'input' => [
  5559. 'type' => 'time',
  5560. 'name' => 'start_time',
  5561. 'value' => '16:30:00',
  5562. ],
  5563. ];
  5564. $this->assertHtml($expected, $result);
  5565. }
  5566. /**
  5567. * testDate method
  5568. *
  5569. * Test the date type.
  5570. *
  5571. * @return void
  5572. */
  5573. public function testDate()
  5574. {
  5575. $result = $this->Form->date('start_day', [
  5576. 'value' => '2014-03-08',
  5577. ]);
  5578. $expected = [
  5579. 'input' => [
  5580. 'type' => 'date',
  5581. 'name' => 'start_day',
  5582. 'value' => '2014-03-08',
  5583. ],
  5584. ];
  5585. $this->assertHtml($expected, $result);
  5586. $result = $this->Form->date('start_day', [
  5587. 'value' => new Date('2014-03-08'),
  5588. ]);
  5589. $this->assertHtml($expected, $result);
  5590. }
  5591. /**
  5592. * testDateTime method
  5593. *
  5594. * @return void
  5595. */
  5596. public function testDateTime()
  5597. {
  5598. $result = $this->Form->dateTime('date', ['default' => true]);
  5599. $expected = [
  5600. 'input' => [
  5601. 'type' => 'datetime-local',
  5602. 'name' => 'date',
  5603. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}(:\d{2})?/',
  5604. ],
  5605. ];
  5606. $this->assertHtml($expected, $result);
  5607. }
  5608. /**
  5609. * testDateTimeSecured method
  5610. *
  5611. * Test that datetime fields are added to protected fields list.
  5612. *
  5613. * @return void
  5614. */
  5615. public function testDateTimeSecured()
  5616. {
  5617. $this->View->setRequest(
  5618. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5619. );
  5620. $this->Form->create();
  5621. $this->Form->dateTime('date');
  5622. $expected = ['date'];
  5623. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5624. $this->assertEquals($expected, $result);
  5625. $this->Form->fields = [];
  5626. $this->Form->date('published');
  5627. $expected = ['date', 'published'];
  5628. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5629. $this->assertEquals($expected, $result);
  5630. }
  5631. /**
  5632. * testDateTimeSecuredDisabled method
  5633. *
  5634. * Test that datetime fields are added to protected fields list.
  5635. *
  5636. * @return void
  5637. */
  5638. public function testDateTimeSecuredDisabled()
  5639. {
  5640. $this->View->setRequest(
  5641. $this->View->getRequest()->withAttribute('formTokenData', ['unlockedFields' => []])
  5642. );
  5643. $this->Form->create();
  5644. $this->Form->dateTime('date', ['secure' => false]);
  5645. $expected = [];
  5646. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5647. $this->assertEquals($expected, $result);
  5648. $this->Form->fields = [];
  5649. $this->Form->date('published', ['secure' => false]);
  5650. $expected = [];
  5651. $result = $this->Form->getFormProtector()->__debugInfo()['fields'];
  5652. $this->assertEquals($expected, $result);
  5653. }
  5654. /**
  5655. * testDatetimeWithDefault method
  5656. *
  5657. * Test that datetime() and default values work.
  5658. *
  5659. * @return void
  5660. */
  5661. public function testDatetimeWithDefault()
  5662. {
  5663. $result = $this->Form->dateTime('updated', ['value' => '2009-06-01 11:15:30']);
  5664. $expected = [
  5665. 'input' => [
  5666. 'type' => 'datetime-local',
  5667. 'name' => 'updated',
  5668. 'value' => '2009-06-01T11:15:30',
  5669. ],
  5670. ];
  5671. $this->assertHtml($expected, $result);
  5672. $result = $this->Form->dateTime('updated', [
  5673. 'default' => '2009-06-01 11:15:30',
  5674. ]);
  5675. $expected = [
  5676. 'input' => [
  5677. 'type' => 'datetime-local',
  5678. 'name' => 'updated',
  5679. 'value' => '2009-06-01T11:15:30',
  5680. ],
  5681. ];
  5682. $this->assertHtml($expected, $result);
  5683. }
  5684. /**
  5685. * testMonth method
  5686. *
  5687. * Test generation of a month input.
  5688. *
  5689. * @return void
  5690. */
  5691. public function testMonth()
  5692. {
  5693. $result = $this->Form->month('field', ['value' => '']);
  5694. $expected = [
  5695. 'input' => [
  5696. 'type' => 'month',
  5697. 'name' => 'field',
  5698. 'value' => '',
  5699. ],
  5700. ];
  5701. $this->assertHtml($expected, $result);
  5702. $this->View->setRequest(
  5703. $this->View->getRequest()->withData('release', '2050-02-10')
  5704. );
  5705. $this->Form->create();
  5706. $result = $this->Form->month('release');
  5707. $expected = [
  5708. 'input' => [
  5709. 'type' => 'month',
  5710. 'name' => 'release',
  5711. 'value' => '2050-02',
  5712. ],
  5713. ];
  5714. $this->assertHtml($expected, $result);
  5715. $this->View->setRequest(
  5716. $this->View->getRequest()->withData('release', '2050-03')
  5717. );
  5718. $this->Form->create();
  5719. $result = $this->Form->month('release');
  5720. $expected = [
  5721. 'input' => [
  5722. 'type' => 'month',
  5723. 'name' => 'release',
  5724. 'value' => '2050-03',
  5725. ],
  5726. ];
  5727. $this->assertHtml($expected, $result);
  5728. }
  5729. /**
  5730. * testYear method
  5731. *
  5732. * Test generation of a year input.
  5733. *
  5734. * @return void
  5735. */
  5736. public function testYear()
  5737. {
  5738. $this->View->setRequest(
  5739. $this->View->getRequest()->withData('published', '2006')
  5740. );
  5741. $result = $this->Form->year('field', ['value' => '', 'min' => 2006, 'max' => 2007]);
  5742. $expected = [
  5743. ['select' => ['name' => 'field']],
  5744. ['option' => ['selected' => 'selected', 'value' => '']],
  5745. '/option',
  5746. ['option' => ['value' => '2007']],
  5747. '2007',
  5748. '/option',
  5749. ['option' => ['value' => '2006']],
  5750. '2006',
  5751. '/option',
  5752. '/select',
  5753. ];
  5754. $this->assertHtml($expected, $result);
  5755. $result = $this->Form->year('field', [
  5756. 'value' => '',
  5757. 'min' => 2006,
  5758. 'max' => 2007,
  5759. 'order' => 'asc',
  5760. ]);
  5761. $expected = [
  5762. ['select' => ['name' => 'field']],
  5763. ['option' => ['selected' => 'selected', 'value' => '']],
  5764. '/option',
  5765. ['option' => ['value' => '2006']],
  5766. '2006',
  5767. '/option',
  5768. ['option' => ['value' => '2007']],
  5769. '2007',
  5770. '/option',
  5771. '/select',
  5772. ];
  5773. $this->assertHtml($expected, $result);
  5774. $result = $this->Form->year('published', [
  5775. 'empty' => false,
  5776. 'min' => 2006,
  5777. 'max' => 2007,
  5778. ]);
  5779. $expected = [
  5780. ['select' => ['name' => 'published']],
  5781. ['option' => ['value' => '2007']],
  5782. '2007',
  5783. '/option',
  5784. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5785. '2006',
  5786. '/option',
  5787. '/select',
  5788. ];
  5789. $this->assertHtml($expected, $result);
  5790. $result = $this->Form->year('published', [
  5791. 'empty' => 'Published on',
  5792. ]);
  5793. $this->assertStringContainsString('Published on', $result);
  5794. }
  5795. /**
  5796. * testControlYearPreEpoch method
  5797. *
  5798. * Test minYear being prior to the unix epoch.
  5799. *
  5800. * @return void
  5801. */
  5802. public function testControlYearPreEpoch()
  5803. {
  5804. $start = date('Y') - 80;
  5805. $end = date('Y') - 18;
  5806. $result = $this->Form->control('birth_year', [
  5807. 'type' => 'year',
  5808. 'label' => 'Birth Year',
  5809. 'min' => $start,
  5810. 'max' => $end,
  5811. ]);
  5812. $this->assertStringContainsString('value="' . $start . '">' . $start, $result);
  5813. $this->assertStringContainsString('value="' . $end . '">' . $end, $result);
  5814. $this->assertStringNotContainsString('value="00">00', $result);
  5815. }
  5816. /**
  5817. * test control() datetime & required attributes
  5818. *
  5819. * @return void
  5820. */
  5821. public function testControlDatetimeRequired()
  5822. {
  5823. $result = $this->Form->control('birthday', [
  5824. 'type' => 'date',
  5825. 'required' => true,
  5826. ]);
  5827. $this->assertStringContainsString(
  5828. '<input type="date" name="birthday" required="required"',
  5829. $result
  5830. );
  5831. }
  5832. /**
  5833. * testYearAutoExpandRange method
  5834. *
  5835. * @return void
  5836. */
  5837. public function testYearAutoExpandRange()
  5838. {
  5839. $this->View->setRequest($this->View->getRequest()->withData('birthday', '1930'));
  5840. $result = $this->Form->year('birthday');
  5841. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5842. $result = $matches[1];
  5843. $expected = range(date('Y') + 5, 1930);
  5844. $this->assertEquals($expected, $result);
  5845. $this->View->setRequest($this->View->getRequest()->withData('release', '2050'));
  5846. $this->Form->create();
  5847. $result = $this->Form->year('release');
  5848. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5849. $result = $matches[1];
  5850. $expected = range(2050, date('Y') - 5);
  5851. $this->assertEquals($expected, $result);
  5852. $this->View->setRequest($this->View->getRequest()->withData('release', '1881'));
  5853. $this->Form->create();
  5854. $result = $this->Form->year('release', [
  5855. 'min' => 1890,
  5856. 'max' => 1900,
  5857. ]);
  5858. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5859. $result = $matches[1];
  5860. $expected = range(1900, 1881);
  5861. $this->assertEquals($expected, $result);
  5862. }
  5863. /**
  5864. * testControlLabelFalse method
  5865. *
  5866. * Test the label option being set to false.
  5867. *
  5868. * @return void
  5869. */
  5870. public function testControlLabelFalse()
  5871. {
  5872. $this->Form->create($this->article);
  5873. $result = $this->Form->control('title', ['label' => false]);
  5874. $expected = [
  5875. 'div' => ['class' => 'input text required'],
  5876. 'input' => [
  5877. 'type' => 'text',
  5878. 'required' => 'required',
  5879. 'id' => 'title',
  5880. 'name' => 'title',
  5881. 'data-validity-message' => 'This field cannot be left empty',
  5882. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  5883. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  5884. ],
  5885. '/div',
  5886. ];
  5887. $this->assertHtml($expected, $result);
  5888. }
  5889. /**
  5890. * testTextArea method
  5891. *
  5892. * Test generation of a textarea input.
  5893. *
  5894. * @return void
  5895. */
  5896. public function testTextArea()
  5897. {
  5898. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  5899. $result = $this->Form->textarea('field');
  5900. $expected = [
  5901. 'textarea' => ['name' => 'field', 'rows' => 5],
  5902. 'some test data',
  5903. '/textarea',
  5904. ];
  5905. $this->assertHtml($expected, $result);
  5906. $result = $this->Form->textarea('user.bio');
  5907. $expected = [
  5908. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5909. '/textarea',
  5910. ];
  5911. $this->assertHtml($expected, $result);
  5912. $this->View->setRequest($this->View->getRequest()
  5913. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5914. $this->Form->create();
  5915. $result = $this->Form->textarea('field');
  5916. $expected = [
  5917. 'textarea' => ['name' => 'field', 'rows' => 5],
  5918. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5919. '/textarea',
  5920. ];
  5921. $this->assertHtml($expected, $result);
  5922. $this->View->setRequest($this->View->getRequest()->withData(
  5923. 'Model.field',
  5924. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  5925. ));
  5926. $this->Form->create();
  5927. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5928. $expected = [
  5929. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5930. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5931. '/textarea',
  5932. ];
  5933. $this->assertHtml($expected, $result);
  5934. $result = $this->Form->textarea('0.OtherModel.field');
  5935. $expected = [
  5936. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5937. '/textarea',
  5938. ];
  5939. $this->assertHtml($expected, $result);
  5940. }
  5941. /**
  5942. * testTextAreaWithStupidCharacters method
  5943. *
  5944. * Test text area with non-ascii characters.
  5945. *
  5946. * @return void
  5947. */
  5948. public function testTextAreaWithStupidCharacters()
  5949. {
  5950. $result = $this->Form->textarea('Post.content', [
  5951. 'value' => 'GREAT®',
  5952. 'rows' => '15',
  5953. 'cols' => '75',
  5954. ]);
  5955. $expected = [
  5956. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5957. 'GREAT®',
  5958. '/textarea',
  5959. ];
  5960. $this->assertHtml($expected, $result);
  5961. }
  5962. /**
  5963. * testTextAreaMaxLength method
  5964. *
  5965. * Test textareas maxlength read from schema.
  5966. *
  5967. * @return void
  5968. */
  5969. public function testTextAreaMaxLength()
  5970. {
  5971. $this->Form->create([
  5972. 'schema' => [
  5973. 'stuff' => ['type' => 'string', 'length' => 10],
  5974. ],
  5975. ]);
  5976. $result = $this->Form->control('other', ['type' => 'textarea']);
  5977. $expected = [
  5978. 'div' => ['class' => 'input textarea'],
  5979. 'label' => ['for' => 'other'],
  5980. 'Other',
  5981. '/label',
  5982. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  5983. '/textarea',
  5984. '/div',
  5985. ];
  5986. $this->assertHtml($expected, $result);
  5987. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  5988. $expected = [
  5989. 'div' => ['class' => 'input textarea'],
  5990. 'label' => ['for' => 'stuff'],
  5991. 'Stuff',
  5992. '/label',
  5993. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  5994. '/textarea',
  5995. '/div',
  5996. ];
  5997. $this->assertHtml($expected, $result);
  5998. }
  5999. /**
  6000. * testHiddenField method
  6001. *
  6002. * Test generation of a hidden input.
  6003. *
  6004. * @return void
  6005. */
  6006. public function testHiddenField()
  6007. {
  6008. $this->article['errors'] = [
  6009. 'field' => true,
  6010. ];
  6011. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6012. $this->Form->create($this->article);
  6013. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6014. $expected = [
  6015. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6016. $this->assertHtml($expected, $result);
  6017. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6018. $expected = [
  6019. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value'],
  6020. ];
  6021. $this->assertHtml($expected, $result);
  6022. }
  6023. /**
  6024. * testFileUploadField method
  6025. *
  6026. * Test generation of a file upload input.
  6027. *
  6028. * @return void
  6029. */
  6030. public function testFileUploadField()
  6031. {
  6032. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6033. $result = $this->Form->file('Model.upload');
  6034. $this->assertHtml($expected, $result);
  6035. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6036. 'name' => '', 'type' => '', 'tmp_name' => '',
  6037. 'error' => 4, 'size' => 0,
  6038. ]));
  6039. $result = $this->Form->file('Model.upload');
  6040. $this->assertHtml($expected, $result);
  6041. $this->View->setRequest(
  6042. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6043. );
  6044. $result = $this->Form->file('Model.upload');
  6045. $this->assertHtml($expected, $result);
  6046. }
  6047. /**
  6048. * testFileUploadOnOtherModel method
  6049. *
  6050. * Test File upload input on a model not used in create().
  6051. *
  6052. * @return void
  6053. */
  6054. public function testFileUploadOnOtherModel()
  6055. {
  6056. $this->Form->create($this->article, ['type' => 'file']);
  6057. $result = $this->Form->file('ValidateProfile.city');
  6058. $expected = [
  6059. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]'],
  6060. ];
  6061. $this->assertHtml($expected, $result);
  6062. }
  6063. /**
  6064. * testButton method
  6065. *
  6066. * Test generation of a form button.
  6067. *
  6068. * @return void
  6069. */
  6070. public function testButton()
  6071. {
  6072. $result = $this->Form->button('Hi');
  6073. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6074. $this->assertHtml($expected, $result);
  6075. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'escapeTitle' => false]);
  6076. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6077. $this->assertHtml($expected, $result);
  6078. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm', 'escapeTitle' => false]);
  6079. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6080. $this->assertHtml($expected, $result);
  6081. $result = $this->Form->button('<Clear Form>', ['type' => 'reset']);
  6082. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6083. $this->assertHtml($expected, $result);
  6084. $result = $this->Form->button('No type', ['type' => false]);
  6085. $expected = ['button' => [], 'No type', '/button'];
  6086. $this->assertHtml($expected, $result);
  6087. $result = $this->Form->button('Upload Text', [
  6088. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6089. 'escape' => false,
  6090. ]);
  6091. $this->assertNotRegExp('/\&039/', $result);
  6092. }
  6093. /**
  6094. * testButtonUnlockedByDefault method
  6095. *
  6096. * Test that button() makes unlocked fields by default.
  6097. *
  6098. * @return void
  6099. */
  6100. public function testButtonUnlockedByDefault()
  6101. {
  6102. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6103. $this->Form->create();
  6104. $this->Form->button('Save', ['name' => 'save']);
  6105. $this->Form->button('Clear');
  6106. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6107. $this->assertEquals(['save'], $result);
  6108. }
  6109. /**
  6110. * Test generation of a form button with confirm message.
  6111. *
  6112. * @return void
  6113. */
  6114. public function testButtonWithConfirm()
  6115. {
  6116. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6117. $expected = ['button' => [
  6118. 'type' => 'submit',
  6119. 'data-confirm-message' => 'Confirm me!',
  6120. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6121. ], 'Hi', '/button'];
  6122. $this->assertHtml($expected, $result);
  6123. $result = $this->Form->button('Hi', ['escape' => false, 'confirm' => 'Confirm "me"!']);
  6124. $expected = ['button' => [
  6125. 'type' => 'submit',
  6126. 'data-confirm-message' => 'Confirm "me"!',
  6127. 'onclick' => 'if (confirm(this.dataset.confirmMessage)) { return true; } return false;',
  6128. ], 'Hi', '/button'];
  6129. $this->assertHtml($expected, $result);
  6130. }
  6131. /**
  6132. * testPostButton method
  6133. *
  6134. * @return void
  6135. */
  6136. public function testPostButton()
  6137. {
  6138. $result = $this->Form->postButton('Hi', '/controller/action');
  6139. $expected = [
  6140. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6141. 'div' => ['style' => 'display:none;'],
  6142. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6143. '/div',
  6144. 'button' => ['type' => 'submit'],
  6145. 'Hi',
  6146. '/button',
  6147. '/form',
  6148. ];
  6149. $this->assertHtml($expected, $result);
  6150. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6151. $this->assertStringContainsString('<input type="hidden" name="extra" value="value"', $result);
  6152. }
  6153. /**
  6154. * testPostButtonMethodType method
  6155. *
  6156. * @return void
  6157. */
  6158. public function testPostButtonMethodType()
  6159. {
  6160. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6161. $expected = [
  6162. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6163. 'div' => ['style' => 'display:none;'],
  6164. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6165. '/div',
  6166. 'button' => ['type' => 'submit'],
  6167. 'Hi',
  6168. '/button',
  6169. '/form',
  6170. ];
  6171. $this->assertHtml($expected, $result);
  6172. }
  6173. /**
  6174. * testPostButtonFormOptions method
  6175. *
  6176. * @return void
  6177. */
  6178. public function testPostButtonFormOptions()
  6179. {
  6180. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6181. $expected = [
  6182. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6183. 'div' => ['style' => 'display:none;'],
  6184. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6185. '/div',
  6186. 'button' => ['type' => 'submit'],
  6187. 'Hi',
  6188. '/button',
  6189. '/form',
  6190. ];
  6191. $this->assertHtml($expected, $result);
  6192. }
  6193. /**
  6194. * testPostButtonNestedData method
  6195. *
  6196. * Test using postButton with N dimensional data.
  6197. *
  6198. * @return void
  6199. */
  6200. public function testPostButtonNestedData()
  6201. {
  6202. $data = [
  6203. 'one' => [
  6204. 'two' => [
  6205. 3, 4, 5,
  6206. ],
  6207. ],
  6208. ];
  6209. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6210. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6211. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6212. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6213. }
  6214. /**
  6215. * testSecurePostButton method
  6216. *
  6217. * Test that postButton adds _Token fields.
  6218. *
  6219. * @return void
  6220. */
  6221. public function testSecurePostButton()
  6222. {
  6223. $this->View->setRequest($this->View->getRequest()
  6224. ->withAttribute('csrfToken', 'testkey')
  6225. ->withAttribute('formTokenData', ['unlockedFields' => []]));
  6226. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6227. $tokenDebug = urlencode(json_encode([
  6228. '/posts/delete/1',
  6229. [],
  6230. [],
  6231. ]));
  6232. $expected = [
  6233. 'form' => [
  6234. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6235. ],
  6236. ['div' => ['style' => 'display:none;']],
  6237. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6238. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6239. '/div',
  6240. 'button' => ['type' => 'submit'],
  6241. 'Delete',
  6242. '/button',
  6243. ['div' => ['style' => 'display:none;']],
  6244. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6245. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6246. ['input' => [
  6247. 'type' => 'hidden',
  6248. 'name' => '_Token[debug]',
  6249. 'value' => $tokenDebug,
  6250. 'autocomplete' => 'off',
  6251. ]],
  6252. '/div',
  6253. '/form',
  6254. ];
  6255. $this->assertHtml($expected, $result);
  6256. }
  6257. /**
  6258. * testPostLink method
  6259. *
  6260. * @return void
  6261. */
  6262. public function testPostLink()
  6263. {
  6264. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6265. $expected = [
  6266. 'form' => [
  6267. 'method' => 'post', 'action' => '/posts/delete/1',
  6268. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6269. ],
  6270. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6271. '/form',
  6272. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6273. 'Delete',
  6274. '/a',
  6275. ];
  6276. $this->assertHtml($expected, $result);
  6277. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6278. $expected = [
  6279. 'form' => [
  6280. 'method' => 'post', 'action' => '/posts/delete/1',
  6281. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6282. ],
  6283. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6284. '/form',
  6285. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6286. 'Delete',
  6287. '/a',
  6288. ];
  6289. $this->assertHtml($expected, $result);
  6290. $result = $this->Form->postLink(
  6291. 'Delete',
  6292. '/posts/delete/1',
  6293. ['target' => '_blank', 'class' => 'btn btn-danger']
  6294. );
  6295. $expected = [
  6296. 'form' => [
  6297. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6298. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6299. ],
  6300. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6301. '/form',
  6302. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6303. 'Delete',
  6304. '/a',
  6305. ];
  6306. $this->assertHtml($expected, $result);
  6307. }
  6308. /**
  6309. * testPostLinkWithConfirm method
  6310. *
  6311. * Test the confirm option for postLink().
  6312. *
  6313. * @return void
  6314. */
  6315. public function testPostLinkWithConfirm()
  6316. {
  6317. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6318. $expected = [
  6319. 'form' => [
  6320. 'method' => 'post', 'action' => '/posts/delete/1',
  6321. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6322. ],
  6323. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6324. '/form',
  6325. 'a' => [
  6326. 'href' => '#',
  6327. 'data-confirm-message' => 'Confirm?',
  6328. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/',
  6329. ],
  6330. 'Delete',
  6331. '/a',
  6332. ];
  6333. $this->assertHtml($expected, $result);
  6334. $result = $this->Form->postLink(
  6335. 'Delete',
  6336. '/posts/delete/1',
  6337. ['confirm' => "'Confirm'\nthis \"deletion\"?"]
  6338. );
  6339. $expected = [
  6340. 'form' => [
  6341. 'method' => 'post', 'action' => '/posts/delete/1',
  6342. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6343. ],
  6344. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6345. '/form',
  6346. 'a' => [
  6347. 'href' => '#',
  6348. 'data-confirm-message' => "&#039;Confirm&#039;\nthis &quot;deletion&quot;?",
  6349. 'onclick' => "preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/",
  6350. ],
  6351. 'Delete',
  6352. '/a',
  6353. ];
  6354. $this->assertHtml($expected, $result);
  6355. $this->Form->setTemplates(['confirmJs' => 'if (confirm(this.dataset.confirmMessage)) { $(\'form[name="{{formName}}"]\').submit();};']);
  6356. $result = $this->Form->postLink(
  6357. 'Delete',
  6358. '/posts/delete/1',
  6359. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  6360. );
  6361. $expected = [
  6362. 'form' => [
  6363. 'method' => 'post', 'action' => '/posts/delete/1',
  6364. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6365. ],
  6366. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6367. '/form',
  6368. 'a' => [
  6369. 'href' => '#',
  6370. 'data-confirm-message' => 'Confirm this deletion?',
  6371. 'onclick' => 'preg:/if \(confirm\(this.dataset.confirmMessage\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/',
  6372. ],
  6373. 'Delete',
  6374. '/a',
  6375. ];
  6376. $this->assertHtml($expected, $result);
  6377. }
  6378. /**
  6379. * testPostLinkWithQuery method
  6380. *
  6381. * Test postLink() with query string args.
  6382. *
  6383. * @return void
  6384. */
  6385. public function testPostLinkWithQuery()
  6386. {
  6387. $result = $this->Form->postLink(
  6388. 'Delete',
  6389. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6390. );
  6391. $expected = [
  6392. 'form' => [
  6393. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6394. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6395. ],
  6396. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6397. '/form',
  6398. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6399. 'Delete',
  6400. '/a',
  6401. ];
  6402. $this->assertHtml($expected, $result);
  6403. }
  6404. /**
  6405. * testPostLinkWithData method
  6406. *
  6407. * Test postLink with additional data.
  6408. *
  6409. * @return void
  6410. */
  6411. public function testPostLinkWithData()
  6412. {
  6413. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6414. $this->assertStringContainsString('<input type="hidden" name="id" value="1"', $result);
  6415. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6416. $this->Form->create($entity);
  6417. $this->Form->end();
  6418. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6419. $this->assertStringContainsString(
  6420. '<input type="hidden" name="name" value="show"',
  6421. $result,
  6422. 'should not contain entity data.'
  6423. );
  6424. }
  6425. /**
  6426. * testPostLinkSecurityHash method
  6427. *
  6428. * Test that security hashes for postLink include the url.
  6429. *
  6430. * @return void
  6431. */
  6432. public function testPostLinkSecurityHash()
  6433. {
  6434. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6435. $hash .= '%3Aid';
  6436. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6437. $result = $this->Form->postLink(
  6438. 'Delete',
  6439. '/posts/delete/1',
  6440. ['data' => ['id' => 1]]
  6441. );
  6442. $tokenDebug = urlencode(json_encode([
  6443. '/posts/delete/1',
  6444. [
  6445. 'id' => 1,
  6446. ],
  6447. [],
  6448. ]));
  6449. $expected = [
  6450. 'form' => [
  6451. 'method' => 'post', 'action' => '/posts/delete/1',
  6452. 'name', 'style' => 'display:none;',
  6453. ],
  6454. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6455. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6456. 'div' => ['style' => 'display:none;'],
  6457. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6458. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6459. ['input' => [
  6460. 'type' => 'hidden',
  6461. 'name' => '_Token[debug]',
  6462. 'value' => $tokenDebug,
  6463. 'autocomplete' => 'off',
  6464. ]],
  6465. '/div',
  6466. '/form',
  6467. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6468. 'Delete',
  6469. '/a',
  6470. ];
  6471. $this->assertHtml($expected, $result);
  6472. }
  6473. /**
  6474. * testPostLinkSecurityHashBlockMode method
  6475. *
  6476. * Test that postLink doesn't modify the fields in the containing form.
  6477. *
  6478. * postLink() calls inside open forms should not modify the field list
  6479. * for the form.
  6480. *
  6481. * @return void
  6482. */
  6483. public function testPostLinkSecurityHashBlockMode()
  6484. {
  6485. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  6486. $hash .= '%3A';
  6487. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', ['key' => 'test']));
  6488. $this->Form->create(null, ['url' => ['action' => 'add']]);
  6489. $this->Form->control('title');
  6490. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6491. $result = $this->View->fetch('postLink');
  6492. $fields = $this->Form->getFormProtector()->__debugInfo()['fields'];
  6493. $this->assertEquals(['title'], $fields);
  6494. $this->assertStringContainsString($hash, $result, 'Should contain the correct hash.');
  6495. $reflect = new ReflectionProperty($this->Form, '_lastAction');
  6496. $reflect->setAccessible(true);
  6497. $this->assertEquals('/articles/add', $reflect->getValue($this->Form), 'lastAction was should be restored.');
  6498. }
  6499. /**
  6500. * testPostLinkSecurityHashNoDebugMode method
  6501. *
  6502. * Test that security does not include debug token if debug is false.
  6503. *
  6504. * @return void
  6505. */
  6506. public function testPostLinkSecurityHashNoDebugMode()
  6507. {
  6508. Configure::write('debug', false);
  6509. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  6510. $hash .= '%3Aid';
  6511. $this->View->setRequest($this->View->getRequest()
  6512. ->withAttribute('formTokenData', ['key' => 'test']));
  6513. $result = $this->Form->postLink(
  6514. 'Delete',
  6515. '/posts/delete/1',
  6516. ['data' => ['id' => 1]]
  6517. );
  6518. $expected = [
  6519. 'form' => [
  6520. 'method' => 'post', 'action' => '/posts/delete/1',
  6521. 'name', 'style' => 'display:none;',
  6522. ],
  6523. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6524. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6525. 'div' => ['style' => 'display:none;'],
  6526. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6527. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6528. '/div',
  6529. '/form',
  6530. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6531. 'Delete',
  6532. '/a',
  6533. ];
  6534. $this->assertHtml($expected, $result);
  6535. }
  6536. /**
  6537. * testPostLinkNestedData method
  6538. *
  6539. * Test using postLink with N dimensional data.
  6540. *
  6541. * @return void
  6542. */
  6543. public function testPostLinkNestedData()
  6544. {
  6545. $data = [
  6546. 'one' => [
  6547. 'two' => [
  6548. 3, 4, 5,
  6549. ],
  6550. ],
  6551. ];
  6552. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6553. $this->assertStringContainsString('<input type="hidden" name="one[two][0]" value="3"', $result);
  6554. $this->assertStringContainsString('<input type="hidden" name="one[two][1]" value="4"', $result);
  6555. $this->assertStringContainsString('<input type="hidden" name="one[two][2]" value="5"', $result);
  6556. }
  6557. /**
  6558. * testPostLinkAfterGetForm method
  6559. *
  6560. * Test creating postLinks after a GET form.
  6561. *
  6562. * @return void
  6563. */
  6564. public function testPostLinkAfterGetForm()
  6565. {
  6566. $this->View->setRequest($this->View->getRequest()
  6567. ->withAttribute('csrfToken', 'testkey')
  6568. ->withAttribute('formTokenData', []));
  6569. $this->Form->create($this->article, ['type' => 'get']);
  6570. $this->Form->end();
  6571. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6572. $tokenDebug = urlencode(json_encode([
  6573. '/posts/delete/1',
  6574. [],
  6575. [],
  6576. ]));
  6577. $expected = [
  6578. 'form' => [
  6579. 'method' => 'post', 'action' => '/posts/delete/1',
  6580. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6581. ],
  6582. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6583. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6584. 'div' => ['style' => 'display:none;'],
  6585. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6586. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6587. ['input' => [
  6588. 'type' => 'hidden', 'name' => '_Token[debug]',
  6589. 'value' => $tokenDebug,
  6590. 'autocomplete' => 'off',
  6591. ]],
  6592. '/div',
  6593. '/form',
  6594. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6595. 'Delete',
  6596. '/a',
  6597. ];
  6598. $this->assertHtml($expected, $result);
  6599. }
  6600. /**
  6601. * testPostLinkFormBuffer method
  6602. *
  6603. * Test that postLink adds form tags to view block.
  6604. *
  6605. * @return void
  6606. */
  6607. public function testPostLinkFormBuffer()
  6608. {
  6609. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6610. $expected = [
  6611. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6612. 'Delete',
  6613. '/a',
  6614. ];
  6615. $this->assertHtml($expected, $result);
  6616. $result = $this->View->fetch('postLink');
  6617. $expected = [
  6618. 'form' => [
  6619. 'method' => 'post', 'action' => '/posts/delete/1',
  6620. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6621. ],
  6622. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6623. '/form',
  6624. ];
  6625. $this->assertHtml($expected, $result);
  6626. $result = $this->Form->postLink(
  6627. 'Delete',
  6628. '/posts/delete/2',
  6629. ['block' => true, 'method' => 'DELETE']
  6630. );
  6631. $expected = [
  6632. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6633. 'Delete',
  6634. '/a',
  6635. ];
  6636. $this->assertHtml($expected, $result);
  6637. $result = $this->View->fetch('postLink');
  6638. $expected = [
  6639. 'form' => [
  6640. 'method' => 'post', 'action' => '/posts/delete/1',
  6641. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6642. ],
  6643. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6644. '/form',
  6645. [
  6646. 'form' => [
  6647. 'method' => 'post', 'action' => '/posts/delete/2',
  6648. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6649. ],
  6650. ],
  6651. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6652. '/form',
  6653. ];
  6654. $this->assertHtml($expected, $result);
  6655. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6656. $expected = [
  6657. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6658. 'Delete',
  6659. '/a',
  6660. ];
  6661. $this->assertHtml($expected, $result);
  6662. $result = $this->View->fetch('foobar');
  6663. $expected = [
  6664. 'form' => [
  6665. 'method' => 'post', 'action' => '/posts/delete/1',
  6666. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;',
  6667. ],
  6668. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6669. '/form',
  6670. ];
  6671. $this->assertHtml($expected, $result);
  6672. }
  6673. /**
  6674. * testSubmitButton method
  6675. *
  6676. * @return void
  6677. */
  6678. public function testSubmitButton()
  6679. {
  6680. $result = $this->Form->submit('');
  6681. $expected = [
  6682. 'div' => ['class' => 'submit'],
  6683. 'input' => ['type' => 'submit', 'value' => ''],
  6684. '/div',
  6685. ];
  6686. $this->assertHtml($expected, $result);
  6687. $result = $this->Form->submit('Test Submit');
  6688. $expected = [
  6689. 'div' => ['class' => 'submit'],
  6690. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6691. '/div',
  6692. ];
  6693. $this->assertHtml($expected, $result);
  6694. $result = $this->Form->submit('Next >');
  6695. $expected = [
  6696. 'div' => ['class' => 'submit'],
  6697. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6698. '/div',
  6699. ];
  6700. $this->assertHtml($expected, $result);
  6701. $result = $this->Form->submit('Next >', ['escape' => false]);
  6702. $expected = [
  6703. 'div' => ['class' => 'submit'],
  6704. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6705. '/div',
  6706. ];
  6707. $this->assertHtml($expected, $result);
  6708. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6709. $expected = [
  6710. 'div' => ['class' => 'submit'],
  6711. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6712. '/div',
  6713. ];
  6714. $this->assertHtml($expected, $result);
  6715. }
  6716. /**
  6717. * testSubmitImage method
  6718. *
  6719. * Test image submit types.
  6720. *
  6721. * @return void
  6722. */
  6723. public function testSubmitImage()
  6724. {
  6725. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6726. $expected = [
  6727. 'div' => ['class' => 'submit'],
  6728. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6729. '/div',
  6730. ];
  6731. $this->assertHtml($expected, $result);
  6732. $result = $this->Form->submit('/relative/cake.power.gif');
  6733. $expected = [
  6734. 'div' => ['class' => 'submit'],
  6735. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6736. '/div',
  6737. ];
  6738. $this->assertHtml($expected, $result);
  6739. $result = $this->Form->submit('cake.power.gif');
  6740. $expected = [
  6741. 'div' => ['class' => 'submit'],
  6742. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6743. '/div',
  6744. ];
  6745. $this->assertHtml($expected, $result);
  6746. $result = $this->Form->submit('Not.an.image');
  6747. $expected = [
  6748. 'div' => ['class' => 'submit'],
  6749. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6750. '/div',
  6751. ];
  6752. $this->assertHtml($expected, $result);
  6753. }
  6754. /**
  6755. * testSubmitUnlockedByDefault method
  6756. *
  6757. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6758. * be submitted at a time.
  6759. *
  6760. * @return void
  6761. */
  6762. public function testSubmitUnlockedByDefault()
  6763. {
  6764. $this->View->setRequest($this->View->getRequest()->withAttribute('formTokenData', []));
  6765. $this->Form->create();
  6766. $this->Form->submit('Go go');
  6767. $this->Form->submit('Save', ['name' => 'save']);
  6768. $result = $this->Form->getFormProtector()->__debugInfo()['unlockedFields'];
  6769. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6770. }
  6771. /**
  6772. * testSubmitImageTimestamp method
  6773. *
  6774. * Test submit image with timestamps.
  6775. *
  6776. * @return void
  6777. */
  6778. public function testSubmitImageTimestamp()
  6779. {
  6780. Configure::write('Asset.timestamp', 'force');
  6781. $result = $this->Form->submit('cake.power.gif');
  6782. $expected = [
  6783. 'div' => ['class' => 'submit'],
  6784. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6785. '/div',
  6786. ];
  6787. $this->assertHtml($expected, $result);
  6788. }
  6789. /**
  6790. * testDateTimeWithGetForms method
  6791. *
  6792. * Test that datetime() works with GET style forms.
  6793. *
  6794. * @return void
  6795. */
  6796. public function testDateTimeWithGetForms()
  6797. {
  6798. $this->Form->create($this->article, ['type' => 'get']);
  6799. $result = $this->Form->datetime('created');
  6800. $expected = [
  6801. 'input' => [
  6802. 'type' => 'datetime-local',
  6803. 'name' => 'created',
  6804. 'value' => '',
  6805. ],
  6806. ];
  6807. $this->assertHtml($expected, $result);
  6808. $result = $this->Form->datetime('created', ['default' => true]);
  6809. $expected = [
  6810. 'input' => [
  6811. 'type' => 'datetime-local',
  6812. 'name' => 'created',
  6813. 'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}(:\d{2})?/',
  6814. ],
  6815. ];
  6816. $this->assertHtml($expected, $result);
  6817. }
  6818. /**
  6819. * testForMagicControlNonExistingNorValidated method
  6820. *
  6821. * @return void
  6822. */
  6823. public function testForMagicControlNonExistingNorValidated()
  6824. {
  6825. $this->Form->create($this->article);
  6826. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6827. $result = $this->Form->control('non_existing_nor_validated');
  6828. $expected = [
  6829. 'label' => ['for' => 'non-existing-nor-validated'],
  6830. 'Non Existing Nor Validated',
  6831. '/label',
  6832. 'input' => [
  6833. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6834. 'id' => 'non-existing-nor-validated',
  6835. ],
  6836. ];
  6837. $this->assertHtml($expected, $result);
  6838. $result = $this->Form->control('non_existing_nor_validated', [
  6839. 'val' => 'my value',
  6840. ]);
  6841. $expected = [
  6842. 'label' => ['for' => 'non-existing-nor-validated'],
  6843. 'Non Existing Nor Validated',
  6844. '/label',
  6845. 'input' => [
  6846. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6847. 'value' => 'my value', 'id' => 'non-existing-nor-validated',
  6848. ],
  6849. ];
  6850. $this->assertHtml($expected, $result);
  6851. $this->View->setRequest(
  6852. $this->View->getRequest()->withData('non_existing_nor_validated', 'CakePHP magic')
  6853. );
  6854. $this->Form->create($this->article);
  6855. $result = $this->Form->control('non_existing_nor_validated');
  6856. $expected = [
  6857. 'label' => ['for' => 'non-existing-nor-validated'],
  6858. 'Non Existing Nor Validated',
  6859. '/label',
  6860. 'input' => [
  6861. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6862. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated',
  6863. ],
  6864. ];
  6865. $this->assertHtml($expected, $result);
  6866. }
  6867. /**
  6868. * testFormMagicControlLabel method
  6869. *
  6870. * @return void
  6871. */
  6872. public function testFormMagicControlLabel()
  6873. {
  6874. $this->getTableLocator()->get('Contacts', [
  6875. 'className' => ContactsTable::class,
  6876. ]);
  6877. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6878. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  6879. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  6880. $expected = [
  6881. 'label' => ['for' => 'contacts-name'],
  6882. 'My label',
  6883. '/label',
  6884. 'input' => [
  6885. 'type' => 'text', 'name' => 'Contacts[name]',
  6886. 'id' => 'contacts-name', 'maxlength' => '255',
  6887. ],
  6888. ];
  6889. $this->assertHtml($expected, $result);
  6890. $result = $this->Form->control('name', [
  6891. 'label' => ['class' => 'mandatory'],
  6892. ]);
  6893. $expected = [
  6894. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6895. 'Name',
  6896. '/label',
  6897. 'input' => [
  6898. 'type' => 'text', 'name' => 'name',
  6899. 'id' => 'name', 'maxlength' => '255',
  6900. ],
  6901. ];
  6902. $this->assertHtml($expected, $result);
  6903. $result = $this->Form->control('name', [
  6904. 'div' => false,
  6905. 'label' => ['class' => 'mandatory', 'text' => 'My label'],
  6906. ]);
  6907. $expected = [
  6908. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6909. 'My label',
  6910. '/label',
  6911. 'input' => [
  6912. 'type' => 'text', 'name' => 'name',
  6913. 'id' => 'name', 'maxlength' => '255',
  6914. ],
  6915. ];
  6916. $this->assertHtml($expected, $result);
  6917. $result = $this->Form->control('Contact.name', [
  6918. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id'],
  6919. ]);
  6920. $expected = [
  6921. 'label' => ['for' => 'my_id'],
  6922. 'Name',
  6923. '/label',
  6924. 'input' => [
  6925. 'type' => 'text', 'name' => 'Contact[name]',
  6926. 'id' => 'my_id', 'maxlength' => '255',
  6927. ],
  6928. ];
  6929. $this->assertHtml($expected, $result);
  6930. $result = $this->Form->control('1.id');
  6931. $expected = ['input' => [
  6932. 'type' => 'hidden', 'name' => '1[id]',
  6933. 'id' => '1-id',
  6934. ]];
  6935. $this->assertHtml($expected, $result);
  6936. $result = $this->Form->control('1.name');
  6937. $expected = [
  6938. 'label' => ['for' => '1-name'],
  6939. 'Name',
  6940. '/label',
  6941. 'input' => [
  6942. 'type' => 'text', 'name' => '1[name]',
  6943. 'id' => '1-name', 'maxlength' => '255',
  6944. ],
  6945. ];
  6946. $this->assertHtml($expected, $result);
  6947. }
  6948. /**
  6949. * testFormEnd method
  6950. *
  6951. * @return void
  6952. */
  6953. public function testFormEnd()
  6954. {
  6955. $this->assertSame('</form>', $this->Form->end());
  6956. }
  6957. /**
  6958. * testMultiRecordForm method
  6959. *
  6960. * Test the generation of fields for a multi record form.
  6961. *
  6962. * @return void
  6963. */
  6964. public function testMultiRecordForm()
  6965. {
  6966. $this->loadFixtures('Articles', 'Comments');
  6967. $articles = $this->getTableLocator()->get('Articles');
  6968. $articles->hasMany('Comments');
  6969. $comment = new Entity(['comment' => 'Value']);
  6970. $article = new Article(['comments' => [$comment]]);
  6971. $this->Form->create([$article]);
  6972. $result = $this->Form->control('0.comments.1.comment');
  6973. // phpcs:disable
  6974. $expected = [
  6975. 'div' => ['class' => 'input textarea'],
  6976. 'label' => ['for' => '0-comments-1-comment'],
  6977. 'Comment',
  6978. '/label',
  6979. 'textarea' => [
  6980. 'name',
  6981. 'id' => '0-comments-1-comment',
  6982. 'rows' => 5
  6983. ],
  6984. '/textarea',
  6985. '/div'
  6986. ];
  6987. // phpcs:enable
  6988. $this->assertHtml($expected, $result);
  6989. $result = $this->Form->control('0.comments.0.comment');
  6990. // phpcs:disable
  6991. $expected = [
  6992. 'div' => ['class' => 'input textarea'],
  6993. 'label' => ['for' => '0-comments-0-comment'],
  6994. 'Comment',
  6995. '/label',
  6996. 'textarea' => [
  6997. 'name',
  6998. 'id' => '0-comments-0-comment',
  6999. 'rows' => 5
  7000. ],
  7001. 'Value',
  7002. '/textarea',
  7003. '/div'
  7004. ];
  7005. // phpcs:enable
  7006. $this->assertHtml($expected, $result);
  7007. $comment->setError('comment', ['Not valid']);
  7008. $result = $this->Form->control('0.comments.0.comment');
  7009. // phpcs:disable
  7010. $expected = [
  7011. 'div' => ['class' => 'input textarea error'],
  7012. 'label' => ['for' => '0-comments-0-comment'],
  7013. 'Comment',
  7014. '/label',
  7015. 'textarea' => [
  7016. 'name',
  7017. 'class' => 'form-error',
  7018. 'id' => '0-comments-0-comment',
  7019. 'rows' => 5
  7020. ],
  7021. 'Value',
  7022. '/textarea',
  7023. ['div' => ['class' => 'error-message']],
  7024. 'Not valid',
  7025. '/div',
  7026. '/div'
  7027. ];
  7028. // phpcs:enable
  7029. $this->assertHtml($expected, $result);
  7030. $this->getTableLocator()->get('Comments')
  7031. ->getValidator('default')
  7032. ->allowEmpty('comment', false);
  7033. $result = $this->Form->control('0.comments.1.comment');
  7034. // phpcs:disable
  7035. $expected = [
  7036. 'div' => ['class' => 'input textarea required'],
  7037. 'label' => ['for' => '0-comments-1-comment'],
  7038. 'Comment',
  7039. '/label',
  7040. 'textarea' => [
  7041. 'name',
  7042. 'required' => 'required',
  7043. 'id' => '0-comments-1-comment',
  7044. 'rows' => 5,
  7045. 'data-validity-message' => 'This field cannot be left empty',
  7046. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7047. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7048. ],
  7049. '/textarea',
  7050. '/div'
  7051. ];
  7052. // phpcs:enable
  7053. $this->assertHtml($expected, $result);
  7054. }
  7055. /**
  7056. * testHtml5Controls method
  7057. *
  7058. * Test that some html5 inputs + FormHelper::__call() work.
  7059. *
  7060. * @return void
  7061. */
  7062. public function testHtml5Controls()
  7063. {
  7064. $result = $this->Form->email('User.email');
  7065. $expected = [
  7066. 'input' => ['type' => 'email', 'name' => 'User[email]'],
  7067. ];
  7068. $this->assertHtml($expected, $result);
  7069. $result = $this->Form->search('User.query');
  7070. $expected = [
  7071. 'input' => ['type' => 'search', 'name' => 'User[query]'],
  7072. ];
  7073. $this->assertHtml($expected, $result);
  7074. $result = $this->Form->search('User.query', ['value' => 'test']);
  7075. $expected = [
  7076. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test'],
  7077. ];
  7078. $this->assertHtml($expected, $result);
  7079. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7080. $expected = [
  7081. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test'],
  7082. ];
  7083. $this->assertHtml($expected, $result);
  7084. }
  7085. /**
  7086. * testHtml5ControlWithControl method
  7087. *
  7088. * Test accessing html5 inputs through control().
  7089. *
  7090. * @return void
  7091. */
  7092. public function testHtml5ControlWithControl()
  7093. {
  7094. $this->Form->create();
  7095. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7096. $result = $this->Form->control('website', [
  7097. 'type' => 'url',
  7098. 'val' => 'http://domain.tld',
  7099. 'label' => false,
  7100. ]);
  7101. $expected = [
  7102. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld'],
  7103. ];
  7104. $this->assertHtml($expected, $result);
  7105. }
  7106. /**
  7107. * testHtml5ControlException method
  7108. *
  7109. * Test errors when field name is missing.
  7110. *
  7111. * @return void
  7112. */
  7113. public function testHtml5ControlException()
  7114. {
  7115. $this->expectException(\Cake\Core\Exception\Exception::class);
  7116. $this->Form->email();
  7117. }
  7118. /**
  7119. * tests fields that are required use custom validation messages
  7120. *
  7121. * @return void
  7122. */
  7123. public function testHtml5ErrorMessage()
  7124. {
  7125. $this->Form->setConfig('autoSetCustomValidity', true);
  7126. $validator = (new \Cake\Validation\Validator())
  7127. ->notEmpty('email', 'Custom error message')
  7128. ->requirePresence('password')
  7129. ->alphaNumeric('password')
  7130. ->notBlank('phone');
  7131. $table = $this->getTableLocator()->get('Contacts', [
  7132. 'className' => ContactsTable::class,
  7133. ]);
  7134. $table->setValidator('default', $validator);
  7135. $contact = new Entity();
  7136. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7137. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7138. $result = $this->Form->control('password');
  7139. $expected = [
  7140. 'label' => ['for' => 'password'],
  7141. 'Password',
  7142. '/label',
  7143. 'input' => [
  7144. 'id' => 'password',
  7145. 'name' => 'password',
  7146. 'type' => 'password',
  7147. 'value' => '',
  7148. 'required' => 'required',
  7149. 'data-validity-message' => 'This field cannot be left empty',
  7150. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7151. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7152. ],
  7153. ];
  7154. $this->assertHtml($expected, $result);
  7155. $result = $this->Form->control('phone');
  7156. $expected = [
  7157. 'label' => ['for' => 'phone'],
  7158. 'Phone',
  7159. '/label',
  7160. 'input' => [
  7161. 'id' => 'phone',
  7162. 'name' => 'phone',
  7163. 'type' => 'tel',
  7164. 'value' => '',
  7165. 'maxlength' => 255,
  7166. 'required' => 'required',
  7167. 'data-validity-message' => 'This field cannot be left empty',
  7168. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7169. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7170. ],
  7171. ];
  7172. $this->assertHtml($expected, $result);
  7173. $result = $this->Form->control('email');
  7174. $expected = [
  7175. 'label' => ['for' => 'email'],
  7176. 'Email',
  7177. '/label',
  7178. 'input' => [
  7179. 'id' => 'email',
  7180. 'name' => 'email',
  7181. 'type' => 'email',
  7182. 'value' => '',
  7183. 'maxlength' => 255,
  7184. 'required' => 'required',
  7185. 'data-validity-message' => 'Custom error message',
  7186. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7187. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7188. ],
  7189. ];
  7190. $this->assertHtml($expected, $result);
  7191. }
  7192. /**
  7193. * tests that custom validation messages are in templateVars
  7194. *
  7195. * @return void
  7196. */
  7197. public function testHtml5ErrorMessageInTemplateVars()
  7198. {
  7199. $validator = (new \Cake\Validation\Validator())
  7200. ->notEmpty('email', 'Custom error "message" & entities')
  7201. ->requirePresence('password')
  7202. ->alphaNumeric('password')
  7203. ->notBlank('phone');
  7204. $table = $this->getTableLocator()->get('Contacts', [
  7205. 'className' => ContactsTable::class,
  7206. ]);
  7207. $table->setValidator('default', $validator);
  7208. $contact = new Entity();
  7209. $this->Form->setConfig('autoSetCustomValidity', false);
  7210. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7211. $this->Form->setTemplates([
  7212. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7213. 'inputContainer' => '{{content}}',
  7214. ]);
  7215. $result = $this->Form->control('password');
  7216. $expected = [
  7217. 'label' => ['for' => 'password'],
  7218. 'Password',
  7219. '/label',
  7220. 'input' => [
  7221. 'id' => 'password',
  7222. 'name' => 'password',
  7223. 'type' => 'password',
  7224. 'value' => '',
  7225. 'required' => 'required',
  7226. 'data-message' => 'This field cannot be left empty',
  7227. ],
  7228. ];
  7229. $this->assertHtml($expected, $result);
  7230. $result = $this->Form->control('phone');
  7231. $expected = [
  7232. 'label' => ['for' => 'phone'],
  7233. 'Phone',
  7234. '/label',
  7235. 'input' => [
  7236. 'id' => 'phone',
  7237. 'name' => 'phone',
  7238. 'type' => 'tel',
  7239. 'value' => '',
  7240. 'maxlength' => 255,
  7241. 'required' => 'required',
  7242. 'data-message' => 'This field cannot be left empty',
  7243. ],
  7244. ];
  7245. $this->assertHtml($expected, $result);
  7246. $result = $this->Form->control('email', [
  7247. 'templateVars' => [
  7248. 'custom' => 'data-custom="1"',
  7249. ],
  7250. ]);
  7251. $expected = [
  7252. 'label' => ['for' => 'email'],
  7253. 'Email',
  7254. '/label',
  7255. 'input' => [
  7256. 'id' => 'email',
  7257. 'name' => 'email',
  7258. 'type' => 'email',
  7259. 'value' => '',
  7260. 'maxlength' => 255,
  7261. 'required' => 'required',
  7262. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  7263. 'data-custom' => '1',
  7264. ],
  7265. ];
  7266. $this->assertHtml($expected, $result);
  7267. $this->Form->setConfig('autoSetCustomValidity', true);
  7268. }
  7269. /**
  7270. * testRequiredAttribute method
  7271. *
  7272. * Tests that formhelper sets required attributes.
  7273. *
  7274. * @return void
  7275. */
  7276. public function testRequiredAttribute()
  7277. {
  7278. $this->article['required'] = [
  7279. 'title' => true,
  7280. 'body' => false,
  7281. ];
  7282. $this->Form->create($this->article);
  7283. $result = $this->Form->control('title');
  7284. $expected = [
  7285. 'div' => ['class' => 'input text required'],
  7286. 'label' => ['for' => 'title'],
  7287. 'Title',
  7288. '/label',
  7289. 'input' => [
  7290. 'type' => 'text',
  7291. 'name' => 'title',
  7292. 'id' => 'title',
  7293. 'required' => 'required',
  7294. 'data-validity-message' => 'This field cannot be left empty',
  7295. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  7296. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7297. ],
  7298. '/div',
  7299. ];
  7300. $this->assertHtml($expected, $result);
  7301. $result = $this->Form->control('title', ['required' => false]);
  7302. $this->assertStringNotContainsString('required', $result);
  7303. $result = $this->Form->control('body');
  7304. $expected = [
  7305. 'div' => ['class' => 'input text'],
  7306. 'label' => ['for' => 'body'],
  7307. 'Body',
  7308. '/label',
  7309. 'input' => [
  7310. 'type' => 'text',
  7311. 'name' => 'body',
  7312. 'id' => 'body',
  7313. ],
  7314. '/div',
  7315. ];
  7316. $this->assertHtml($expected, $result);
  7317. $result = $this->Form->control('body', ['required' => true]);
  7318. $this->assertStringContainsString('required', $result);
  7319. }
  7320. /**
  7321. * testControlsNotNested method
  7322. *
  7323. * Tests that it is possible to put inputs outside of the label.
  7324. *
  7325. * @return void
  7326. */
  7327. public function testControlsNotNested()
  7328. {
  7329. $this->Form->setTemplates([
  7330. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7331. 'formGroup' => '{{input}}{{label}}',
  7332. ]);
  7333. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7334. $expected = [
  7335. 'div' => ['class' => 'input checkbox'],
  7336. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7337. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7338. 'label' => ['for' => 'foo'],
  7339. 'Foo',
  7340. '/label',
  7341. '/div',
  7342. ];
  7343. $this->assertHtml($expected, $result);
  7344. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7345. $expected = [
  7346. 'div' => ['class' => 'input checkbox'],
  7347. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7348. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7349. '/div',
  7350. ];
  7351. $this->assertHtml($expected, $result);
  7352. $result = $this->Form->control('confirm', [
  7353. 'type' => 'radio',
  7354. 'options' => ['Y' => 'Yes', 'N' => 'No'],
  7355. ]);
  7356. $expected = [
  7357. 'div' => ['class' => 'input radio'],
  7358. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7359. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7360. ['label' => ['for' => 'confirm-y']],
  7361. 'Yes',
  7362. '/label',
  7363. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7364. ['label' => ['for' => 'confirm-n']],
  7365. 'No',
  7366. '/label',
  7367. '<label',
  7368. 'Confirm',
  7369. '/label',
  7370. '/div',
  7371. ];
  7372. $this->assertHtml($expected, $result);
  7373. $result = $this->Form->select('category', ['1', '2'], [
  7374. 'multiple' => 'checkbox',
  7375. 'name' => 'fish',
  7376. ]);
  7377. $expected = [
  7378. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7379. ['div' => ['class' => 'checkbox']],
  7380. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7381. ['label' => ['for' => 'fish-0']],
  7382. '1',
  7383. '/label',
  7384. '/div',
  7385. ['div' => ['class' => 'checkbox']],
  7386. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7387. ['label' => ['for' => 'fish-1']],
  7388. '2',
  7389. '/label',
  7390. '/div',
  7391. ];
  7392. $this->assertHtml($expected, $result);
  7393. }
  7394. /**
  7395. * testControlContainerTemplates method
  7396. *
  7397. * Test that *Container templates are used by input.
  7398. *
  7399. * @return void
  7400. */
  7401. public function testControlContainerTemplates()
  7402. {
  7403. $this->Form->setTemplates([
  7404. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7405. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7406. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7407. ]);
  7408. $this->article['errors'] = [
  7409. 'Article' => ['published' => 'error message'],
  7410. ];
  7411. $this->Form->create($this->article);
  7412. $result = $this->Form->control('accept', [
  7413. 'type' => 'checkbox',
  7414. ]);
  7415. $expected = [
  7416. 'div' => ['class' => 'check'],
  7417. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7418. 'label' => ['for' => 'accept'],
  7419. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7420. 'Accept',
  7421. '/label',
  7422. '/div',
  7423. ];
  7424. $this->assertHtml($expected, $result);
  7425. $result = $this->Form->control('accept', [
  7426. 'type' => 'radio',
  7427. 'options' => ['Y', 'N'],
  7428. ]);
  7429. $this->assertStringContainsString('<div class="rad">', $result);
  7430. $result = $this->Form->control('Article.published', [
  7431. 'type' => 'radio',
  7432. 'options' => ['Y', 'N'],
  7433. ]);
  7434. $this->assertStringContainsString('<div class="rad err">', $result);
  7435. }
  7436. /**
  7437. * testFormGroupTemplates method
  7438. *
  7439. * Test that *Container templates are used by input.
  7440. *
  7441. * @return void
  7442. */
  7443. public function testFormGroupTemplates()
  7444. {
  7445. $this->Form->setTemplates([
  7446. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7447. ]);
  7448. $this->Form->create($this->article);
  7449. $result = $this->Form->control('accept', [
  7450. 'type' => 'radio',
  7451. 'options' => ['Y', 'N'],
  7452. ]);
  7453. $this->assertStringContainsString('<div class="radio">', $result);
  7454. }
  7455. /**
  7456. * testResetTemplates method
  7457. *
  7458. * Test resetting templates.
  7459. *
  7460. * @return void
  7461. */
  7462. public function testResetTemplates()
  7463. {
  7464. $this->Form->setTemplates(['input' => '<input/>']);
  7465. $this->assertSame('<input/>', $this->Form->templater()->get('input'));
  7466. $this->Form->resetTemplates();
  7467. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7468. }
  7469. /**
  7470. * testContext method
  7471. *
  7472. * Test the context method.
  7473. *
  7474. * @return void
  7475. */
  7476. public function testContext()
  7477. {
  7478. $result = $this->Form->context();
  7479. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7480. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7481. $this->assertSame($mock, $this->Form->context($mock));
  7482. $this->assertSame($mock, $this->Form->context());
  7483. }
  7484. /**
  7485. * testAutoDomId method
  7486. *
  7487. * @return void
  7488. */
  7489. public function testAutoDomId()
  7490. {
  7491. $result = $this->Form->text('field', ['id' => true]);
  7492. $expected = [
  7493. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7494. ];
  7495. $this->assertHtml($expected, $result);
  7496. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7497. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7498. $expected = [
  7499. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7500. ['label' => ['for' => 'field-0']],
  7501. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7502. 'option A',
  7503. '/label',
  7504. ['label' => ['for' => 'field-1']],
  7505. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7506. 'option B',
  7507. '/label',
  7508. ];
  7509. $this->assertHtml($expected, $result);
  7510. $result = $this->Form->select(
  7511. 'multi_field',
  7512. ['first', 'second'],
  7513. ['multiple' => 'checkbox', 'id' => true]
  7514. );
  7515. $expected = [
  7516. 'input' => [
  7517. 'type' => 'hidden', 'name' => 'multi_field', 'value' => '',
  7518. ],
  7519. ['div' => ['class' => 'checkbox']],
  7520. ['label' => ['for' => 'multi-field-0']],
  7521. ['input' => [
  7522. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7523. 'value' => '0', 'id' => 'multi-field-0',
  7524. ]],
  7525. 'first',
  7526. '/label',
  7527. '/div',
  7528. ['div' => ['class' => 'checkbox']],
  7529. ['label' => ['for' => 'multi-field-1']],
  7530. ['input' => [
  7531. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7532. 'value' => '1', 'id' => 'multi-field-1',
  7533. ]],
  7534. 'second',
  7535. '/label',
  7536. '/div',
  7537. ];
  7538. $this->assertHtml($expected, $result);
  7539. }
  7540. /**
  7541. * Test the basic setters and getters for value sources
  7542. *
  7543. * @return void
  7544. */
  7545. public function testFormValueSourcesSettersGetters()
  7546. {
  7547. $this->View->setRequest($this->View->getRequest()
  7548. ->withData('id', '1')
  7549. ->withQueryParams(['id' => '2']));
  7550. $expected = ['context'];
  7551. $result = $this->Form->getValueSources();
  7552. $this->assertEquals($expected, $result);
  7553. $expected = ['query', 'data', 'context'];
  7554. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7555. $result = $this->Form->getValueSources();
  7556. $this->assertEquals($expected, $result);
  7557. $this->Form->setValueSources(['context']);
  7558. $expected = '1';
  7559. $result = $this->Form->getSourceValue('id');
  7560. $this->assertEquals($expected, $result);
  7561. $this->Form->setValueSources('query');
  7562. $expected = ['query'];
  7563. $result = $this->Form->getValueSources();
  7564. $this->assertEquals($expected, $result);
  7565. $expected = '2';
  7566. $result = $this->Form->getSourceValue('id');
  7567. $this->assertEquals($expected, $result);
  7568. $this->Form->setValueSources(['data']);
  7569. $expected = '1';
  7570. $result = $this->Form->getSourceValue('id');
  7571. $this->assertEquals($expected, $result);
  7572. $this->Form->setValueSources(['query', 'data']);
  7573. $expected = '2';
  7574. $result = $this->Form->getSourceValue('id');
  7575. $this->assertEquals($expected, $result);
  7576. }
  7577. /**
  7578. * Tests the different input rendering values based on sources values switching
  7579. *
  7580. * @return void
  7581. */
  7582. public function testFormValueSourcesSingleSwitchRendering()
  7583. {
  7584. $this->loadFixtures('Articles');
  7585. $articles = $this->getTableLocator()->get('Articles');
  7586. $article = new Article();
  7587. $articles->patchEntity($article, ['id' => '3']);
  7588. $this->Form->create($article);
  7589. $this->Form->setValueSources(['context']);
  7590. $result = $this->Form->control('id');
  7591. $expected = [
  7592. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7593. ];
  7594. $this->assertHtml($expected, $result);
  7595. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  7596. $this->Form->setValueSources(['query']);
  7597. $result = $this->Form->control('id');
  7598. $expected = [
  7599. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7600. ];
  7601. $this->assertHtml($expected, $result);
  7602. $this->View->setRequest($this->View->getRequest()
  7603. ->withQueryParams(['id' => '5a'])
  7604. ->withData('id', '5b'));
  7605. $this->Form->setValueSources(['context']);
  7606. $this->Form->create($article);
  7607. $result = $this->Form->control('id');
  7608. $expected = [
  7609. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7610. ];
  7611. $this->assertHtml($expected, $result);
  7612. $this->Form->setValueSources(['data']);
  7613. $this->Form->create($article);
  7614. $result = $this->Form->control('id');
  7615. $expected = [
  7616. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7617. ];
  7618. $this->assertHtml($expected, $result);
  7619. $this->Form->setValueSources(['query']);
  7620. $result = $this->Form->control('id');
  7621. $expected = [
  7622. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7623. ];
  7624. $this->assertHtml($expected, $result);
  7625. }
  7626. /**
  7627. * Tests the different input rendering values based on sources values switching while supplying
  7628. * an entity (base context) and multiple sources (such as data, query)
  7629. *
  7630. * @return void
  7631. */
  7632. public function testFormValueSourcesListSwitchRendering()
  7633. {
  7634. $this->loadFixtures('Articles');
  7635. $articles = $this->getTableLocator()->get('Articles');
  7636. $article = new Article();
  7637. $articles->patchEntity($article, ['id' => '3']);
  7638. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  7639. $this->Form->create($article);
  7640. $this->Form->setValueSources(['context', 'query']);
  7641. $result = $this->Form->control('id');
  7642. $expected = [
  7643. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7644. ];
  7645. $this->assertHtml($expected, $result);
  7646. $this->Form->setValueSources(['query', 'context']);
  7647. $result = $this->Form->control('id');
  7648. $expected = [
  7649. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7650. ];
  7651. $this->assertHtml($expected, $result);
  7652. $this->Form->setValueSources(['data', 'query', 'context']);
  7653. $result = $this->Form->control('id');
  7654. $expected = [
  7655. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7656. ];
  7657. $this->assertHtml($expected, $result);
  7658. $this->View->setRequest($this->View->getRequest()
  7659. ->withData('id', '8')
  7660. ->withQueryParams(['id' => '9']));
  7661. $this->Form->setValueSources(['data', 'query', 'context']);
  7662. $result = $this->Form->control('id');
  7663. $expected = [
  7664. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7665. ];
  7666. $this->assertHtml($expected, $result);
  7667. }
  7668. /**
  7669. * Test the different form input renderings based on values sources switchings through form options
  7670. *
  7671. * @return void
  7672. */
  7673. public function testFormValueSourcesSwitchViaOptionsRendering()
  7674. {
  7675. $this->loadFixtures('Articles');
  7676. $articles = $this->getTableLocator()->get('Articles');
  7677. $article = new Article();
  7678. $articles->patchEntity($article, ['id' => '3']);
  7679. $this->View->setRequest(
  7680. $this->View->getRequest()->withData('id', '4')->withQueryParams(['id' => '5'])
  7681. );
  7682. $this->Form->create($article, ['valueSources' => 'query']);
  7683. $result = $this->Form->control('id');
  7684. $expected = [
  7685. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7686. ];
  7687. $this->assertHtml($expected, $result);
  7688. $result = $this->Form->getSourceValue('id');
  7689. $this->assertSame('5', $result);
  7690. $this->Form->setValueSources(['context']);
  7691. $this->Form->create($article, ['valueSources' => 'query']);
  7692. $result = $this->Form->control('id');
  7693. $expected = [
  7694. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7695. ];
  7696. $this->assertHtml($expected, $result);
  7697. $result = $this->Form->getSourceValue('id');
  7698. $this->assertSame('5', $result);
  7699. $this->Form->setValueSources(['query']);
  7700. $this->Form->create($article, ['valueSources' => 'data']);
  7701. $result = $this->Form->control('id');
  7702. $expected = [
  7703. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7704. ];
  7705. $this->assertHtml($expected, $result);
  7706. $result = $this->Form->getSourceValue('id');
  7707. $this->assertSame('4', $result);
  7708. $this->Form->setValueSources(['query']);
  7709. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7710. $result = $this->Form->control('id');
  7711. $expected = [
  7712. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7713. ];
  7714. $this->assertHtml($expected, $result);
  7715. $result = $this->Form->getSourceValue('id');
  7716. $this->assertSame('4', $result);
  7717. }
  7718. /**
  7719. * Test the different form input renderings based on values sources switchings through form options
  7720. *
  7721. * @return void
  7722. */
  7723. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  7724. {
  7725. $this->loadFixtures('Articles');
  7726. $articles = $this->getTableLocator()->get('Articles');
  7727. $article = new Article();
  7728. $articles->patchEntity($article, ['id' => '3']);
  7729. $this->View->setRequest(
  7730. $this->View->getRequest()->withData('id', '10')->withQueryParams(['id' => '11'])
  7731. );
  7732. $this->Form->setValueSources(['context'])
  7733. ->create($article, ['valueSources' => ['query', 'data']]);
  7734. $result = $this->Form->control('id');
  7735. $expected = [
  7736. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7737. ];
  7738. $this->assertHtml($expected, $result);
  7739. $result = $this->Form->getSourceValue('id');
  7740. $this->assertSame('11', $result);
  7741. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  7742. $this->Form->setValueSources(['context'])
  7743. ->create($article, ['valueSources' => ['query', 'data']]);
  7744. $result = $this->Form->control('id');
  7745. $expected = [
  7746. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7747. ];
  7748. $this->assertHtml($expected, $result);
  7749. $result = $this->Form->getSourceValue('id');
  7750. $this->assertSame('10', $result);
  7751. }
  7752. /**
  7753. * Test the different form values sources resetting through From::end();
  7754. *
  7755. * @return void
  7756. */
  7757. public function testFormValueSourcesResetViaEnd()
  7758. {
  7759. $expected = ['context'];
  7760. $result = $this->Form->getValueSources();
  7761. $this->assertEquals($expected, $result);
  7762. $expected = ['query', 'context', 'data'];
  7763. $this->Form->setValueSources(['query', 'context', 'data']);
  7764. $result = $this->Form->getValueSources();
  7765. $this->assertEquals($expected, $result);
  7766. $this->Form->create();
  7767. $result = $this->Form->getValueSources();
  7768. $this->assertEquals($expected, $result);
  7769. $this->Form->end();
  7770. $result = $this->Form->getValueSources();
  7771. $this->assertEquals(['context'], $result);
  7772. }
  7773. /**
  7774. * Test sources values defaults handling
  7775. *
  7776. * @return void
  7777. */
  7778. public function testFormValueSourcesDefaults()
  7779. {
  7780. $this->View->setRequest(
  7781. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  7782. );
  7783. $this->Form->create();
  7784. $result = $this->Form->password('password');
  7785. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7786. $this->assertHtml($expected, $result);
  7787. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7788. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7789. $this->assertHtml($expected, $result);
  7790. $this->Form->setValueSources('query');
  7791. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7792. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7793. $this->assertHtml($expected, $result);
  7794. $this->Form->setValueSources('data');
  7795. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7796. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7797. $this->assertHtml($expected, $result);
  7798. }
  7799. /**
  7800. * Test sources values schema defaults handling
  7801. *
  7802. * @return void
  7803. */
  7804. public function testSourcesValueDoesntExistPassThrough()
  7805. {
  7806. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  7807. $articles = $this->getTableLocator()->get('Articles');
  7808. $entity = $articles->newEmptyEntity();
  7809. $this->Form->create($entity);
  7810. $this->Form->setValueSources(['query', 'context']);
  7811. $result = $this->Form->getSourceValue('category');
  7812. $this->assertSame('sesame-cookies', $result);
  7813. $this->Form->setValueSources(['context', 'query']);
  7814. $result = $this->Form->getSourceValue('category');
  7815. $this->assertSame('sesame-cookies', $result);
  7816. }
  7817. /**
  7818. * testNestedLabelInput method
  7819. *
  7820. * Test the `nestedInput` parameter
  7821. *
  7822. * @return void
  7823. */
  7824. public function testNestedLabelInput()
  7825. {
  7826. $result = $this->Form->control('foo', ['nestedInput' => true]);
  7827. $expected = [
  7828. 'div' => ['class' => 'input text'],
  7829. 'label' => ['for' => 'foo'],
  7830. ['input' => [
  7831. 'type' => 'text',
  7832. 'name' => 'foo',
  7833. 'id' => 'foo',
  7834. ]],
  7835. 'Foo',
  7836. '/label',
  7837. '/div',
  7838. ];
  7839. $this->assertHtml($expected, $result);
  7840. }
  7841. /**
  7842. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  7843. *
  7844. * This test makes sure `false` excludes the label from the render
  7845. *
  7846. * @return void
  7847. */
  7848. public function testControlLabelManipulationDisableLabels()
  7849. {
  7850. $result = $this->Form->control('test', [
  7851. 'type' => 'radio',
  7852. 'options' => ['A', 'B'],
  7853. 'labelOptions' => false,
  7854. ]);
  7855. $expected = [
  7856. ['div' => ['class' => 'input radio']],
  7857. '<label',
  7858. 'Test',
  7859. '/label',
  7860. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7861. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7862. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7863. '/div',
  7864. ];
  7865. $this->assertHtml($expected, $result);
  7866. $result = $this->Form->control('checkbox1', [
  7867. 'label' => 'My checkboxes',
  7868. 'multiple' => 'checkbox',
  7869. 'type' => 'select',
  7870. 'options' => [
  7871. ['text' => 'First Checkbox', 'value' => 1],
  7872. ['text' => 'Second Checkbox', 'value' => 2],
  7873. ],
  7874. 'labelOptions' => false,
  7875. ]);
  7876. $expected = [
  7877. ['div' => ['class' => 'input select']],
  7878. ['label' => ['for' => 'checkbox1']],
  7879. 'My checkboxes',
  7880. '/label',
  7881. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  7882. ['div' => ['class' => 'checkbox']],
  7883. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  7884. '/div',
  7885. ['div' => ['class' => 'checkbox']],
  7886. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  7887. '/div',
  7888. '/div',
  7889. ];
  7890. $this->assertHtml($expected, $result);
  7891. }
  7892. /**
  7893. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  7894. *
  7895. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  7896. * added to the class if checked.
  7897. *
  7898. * Also checks to make sure any custom attributes are rendered correctly
  7899. *
  7900. * @return void
  7901. */
  7902. public function testControlLabelManipulationRadios()
  7903. {
  7904. $result = $this->Form->control('test', [
  7905. 'type' => 'radio',
  7906. 'options' => ['A', 'B'],
  7907. 'labelOptions' => ['class' => 'custom-class'],
  7908. ]);
  7909. $expected = [
  7910. ['div' => ['class' => 'input radio']],
  7911. '<label',
  7912. 'Test',
  7913. '/label',
  7914. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7915. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7916. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7917. 'A',
  7918. '/label',
  7919. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  7920. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  7921. 'B',
  7922. '/label',
  7923. '/div',
  7924. ];
  7925. $this->assertHtml($expected, $result);
  7926. $result = $this->Form->control('test', [
  7927. 'type' => 'radio',
  7928. 'options' => ['A', 'B'],
  7929. 'value' => 1,
  7930. 'labelOptions' => ['class' => 'custom-class'],
  7931. ]);
  7932. $expected = [
  7933. ['div' => ['class' => 'input radio']],
  7934. '<label',
  7935. 'Test',
  7936. '/label',
  7937. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7938. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  7939. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7940. 'A',
  7941. '/label',
  7942. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  7943. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7944. 'B',
  7945. '/label',
  7946. '/div',
  7947. ];
  7948. $this->assertHtml($expected, $result);
  7949. $result = $this->Form->control('test', [
  7950. 'type' => 'radio',
  7951. 'options' => ['A', 'B'],
  7952. 'value' => 1,
  7953. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']],
  7954. ]);
  7955. $expected = [
  7956. ['div' => ['class' => 'input radio']],
  7957. '<label',
  7958. 'Test',
  7959. '/label',
  7960. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  7961. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  7962. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7963. 'A',
  7964. '/label',
  7965. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  7966. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  7967. 'B',
  7968. '/label',
  7969. '/div',
  7970. ];
  7971. $this->assertHtml($expected, $result);
  7972. $result = $this->Form->radio('test', ['A', 'B'], [
  7973. 'label' => [
  7974. 'class' => ['custom-class', 'another-class'],
  7975. 'data-name' => 'bob',
  7976. ],
  7977. 'value' => 1,
  7978. ]);
  7979. $expected = [
  7980. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  7981. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  7982. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  7983. 'A',
  7984. '/label',
  7985. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  7986. ['input' => [
  7987. 'type' => 'radio',
  7988. 'name' => 'test',
  7989. 'value' => '1',
  7990. 'id' => 'test-1',
  7991. 'checked' => 'checked',
  7992. ]],
  7993. 'B',
  7994. '/label',
  7995. ];
  7996. $this->assertHtml($expected, $result);
  7997. }
  7998. /**
  7999. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8000. *
  8001. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8002. * added to the class if checked.
  8003. *
  8004. * Also checks to make sure any custom attributes are rendered correctly
  8005. *
  8006. * @return void
  8007. */
  8008. public function testControlLabelManipulationCheckboxes()
  8009. {
  8010. $result = $this->Form->control('checkbox1', [
  8011. 'label' => 'My checkboxes',
  8012. 'multiple' => 'checkbox',
  8013. 'type' => 'select',
  8014. 'options' => [
  8015. ['text' => 'First Checkbox', 'value' => 1],
  8016. ['text' => 'Second Checkbox', 'value' => 2],
  8017. ],
  8018. 'labelOptions' => ['class' => 'custom-class'],
  8019. 'value' => ['1'],
  8020. ]);
  8021. $expected = [
  8022. ['div' => ['class' => 'input select']],
  8023. ['label' => ['for' => 'checkbox1']],
  8024. 'My checkboxes',
  8025. '/label',
  8026. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8027. ['div' => ['class' => 'checkbox']],
  8028. ['label' => [
  8029. 'class' => 'custom-class selected',
  8030. 'for' => 'checkbox1-1',
  8031. ]],
  8032. ['input' => [
  8033. 'type' => 'checkbox',
  8034. 'name' => 'checkbox1[]',
  8035. 'value' => '1',
  8036. 'id' => 'checkbox1-1',
  8037. 'checked' => 'checked',
  8038. ]],
  8039. 'First Checkbox',
  8040. '/label',
  8041. '/div',
  8042. ['div' => ['class' => 'checkbox']],
  8043. ['label' => [
  8044. 'class' => 'custom-class',
  8045. 'for' => 'checkbox1-2',
  8046. ]],
  8047. ['input' => [
  8048. 'type' => 'checkbox',
  8049. 'name' => 'checkbox1[]',
  8050. 'value' => '2',
  8051. 'id' => 'checkbox1-2',
  8052. ]],
  8053. 'Second Checkbox',
  8054. '/label',
  8055. '/div',
  8056. '/div',
  8057. ];
  8058. $this->assertHtml($expected, $result);
  8059. $result = $this->Form->control('checkbox1', [
  8060. 'label' => 'My checkboxes',
  8061. 'multiple' => 'checkbox',
  8062. 'type' => 'select',
  8063. 'options' => [
  8064. ['text' => 'First Checkbox', 'value' => 1],
  8065. ['text' => 'Second Checkbox', 'value' => 2],
  8066. ],
  8067. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8068. 'value' => ['1'],
  8069. ]);
  8070. $expected = [
  8071. ['div' => ['class' => 'input select']],
  8072. ['label' => ['for' => 'checkbox1']],
  8073. 'My checkboxes',
  8074. '/label',
  8075. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8076. ['div' => ['class' => 'checkbox']],
  8077. ['label' => [
  8078. 'class' => 'custom-class another-class selected',
  8079. 'data-name' => 'bob',
  8080. 'for' => 'checkbox1-1',
  8081. ]],
  8082. ['input' => [
  8083. 'type' => 'checkbox',
  8084. 'name' => 'checkbox1[]',
  8085. 'value' => '1',
  8086. 'id' => 'checkbox1-1',
  8087. 'checked' => 'checked',
  8088. ]],
  8089. 'First Checkbox',
  8090. '/label',
  8091. '/div',
  8092. ['div' => ['class' => 'checkbox']],
  8093. ['label' => [
  8094. 'class' => 'custom-class another-class',
  8095. 'data-name' => 'bob',
  8096. 'for' => 'checkbox1-2',
  8097. ]],
  8098. ['input' => [
  8099. 'type' => 'checkbox',
  8100. 'name' => 'checkbox1[]',
  8101. 'value' => '2',
  8102. 'id' => 'checkbox1-2',
  8103. ]],
  8104. 'Second Checkbox',
  8105. '/label',
  8106. '/div',
  8107. '/div',
  8108. ];
  8109. $this->assertHtml($expected, $result);
  8110. }
  8111. /**
  8112. * testControlMaxLengthArrayContext method
  8113. *
  8114. * Test control() with maxlength attribute in Array Context.
  8115. *
  8116. * @return void
  8117. */
  8118. public function testControlMaxLengthArrayContext()
  8119. {
  8120. $this->article['schema'] = [
  8121. 'title' => ['length' => 10],
  8122. ];
  8123. $this->Form->create($this->article);
  8124. $result = $this->Form->control('title');
  8125. $expected = [
  8126. 'div' => ['class'],
  8127. 'label' => ['for'],
  8128. 'Title',
  8129. '/label',
  8130. 'input' => [
  8131. 'id',
  8132. 'name' => 'title',
  8133. 'type' => 'text',
  8134. 'required' => 'required',
  8135. 'maxlength' => 10,
  8136. 'data-validity-message' => 'This field cannot be left empty',
  8137. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8138. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8139. ],
  8140. '/div',
  8141. ];
  8142. $this->assertHtml($expected, $result);
  8143. }
  8144. /**
  8145. * testControlMaxLengthEntityContext method
  8146. *
  8147. * Test control() with maxlength attribute in Entity Context.
  8148. *
  8149. * @return void
  8150. */
  8151. public function testControlMaxLengthEntityContext()
  8152. {
  8153. $this->article['schema']['title']['length'] = 45;
  8154. $validator = new Validator();
  8155. $validator->maxLength('title', 10);
  8156. $article = new EntityContext(
  8157. new ServerRequest(),
  8158. [
  8159. 'entity' => new Entity($this->article),
  8160. 'table' => new Table([
  8161. 'alias' => 'Articles',
  8162. 'schema' => $this->article['schema'],
  8163. 'validator' => $validator,
  8164. ]),
  8165. ]
  8166. );
  8167. $this->Form->create($article);
  8168. $result = $this->Form->control('title');
  8169. $expected = [
  8170. 'div' => ['class'],
  8171. 'label' => ['for'],
  8172. 'Title',
  8173. '/label',
  8174. 'input' => [
  8175. 'id',
  8176. 'name' => 'title',
  8177. 'type' => 'text',
  8178. 'required' => 'required',
  8179. 'maxlength' => 10,
  8180. 'data-validity-message' => 'This field cannot be left empty',
  8181. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8182. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8183. ],
  8184. '/div',
  8185. ];
  8186. $this->assertHtml($expected, $result);
  8187. $this->article['schema']['title']['length'] = 45;
  8188. $validator = new Validator();
  8189. $validator->maxLength('title', 55);
  8190. $article = new EntityContext(
  8191. new ServerRequest(),
  8192. [
  8193. 'entity' => new Entity($this->article),
  8194. 'table' => new Table([
  8195. 'schema' => $this->article['schema'],
  8196. 'validator' => $validator,
  8197. 'alias' => 'Articles',
  8198. ]),
  8199. ]
  8200. );
  8201. $this->Form->create($article);
  8202. $result = $this->Form->control('title');
  8203. $expected = [
  8204. 'div' => ['class'],
  8205. 'label' => ['for'],
  8206. 'Title',
  8207. '/label',
  8208. 'input' => [
  8209. 'id',
  8210. 'name' => 'title',
  8211. 'type' => 'text',
  8212. 'required' => 'required',
  8213. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8214. 'data-validity-message' => 'This field cannot be left empty',
  8215. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8216. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8217. ],
  8218. '/div',
  8219. ];
  8220. $this->assertHtml($expected, $result);
  8221. $this->article['schema']['title']['length'] = 45;
  8222. $validator = new Validator();
  8223. $validator->maxLength('title', 55);
  8224. $article = new EntityContext(
  8225. new ServerRequest(),
  8226. [
  8227. 'entity' => new Entity($this->article),
  8228. 'table' => new Table([
  8229. 'schema' => $this->article['schema'],
  8230. 'validator' => $validator,
  8231. 'alias' => 'Articles',
  8232. ]),
  8233. ]
  8234. );
  8235. $this->Form->create($article);
  8236. $result = $this->Form->control('title', ['maxlength' => 10]);
  8237. $expected = [
  8238. 'div' => ['class'],
  8239. 'label' => ['for'],
  8240. 'Title',
  8241. '/label',
  8242. 'input' => [
  8243. 'id',
  8244. 'name' => 'title',
  8245. 'type' => 'text',
  8246. 'required' => 'required',
  8247. 'maxlength' => 10, // Length set in options should take highest precedence.
  8248. 'data-validity-message' => 'This field cannot be left empty',
  8249. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8250. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8251. ],
  8252. '/div',
  8253. ];
  8254. $this->assertHtml($expected, $result);
  8255. }
  8256. /**
  8257. * testControlMinMaxLengthEntityContext method
  8258. *
  8259. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8260. *
  8261. * @return void
  8262. */
  8263. public function testControlMinMaxLengthEntityContext()
  8264. {
  8265. $validator = new Validator();
  8266. $validator->maxLength('title', 10);
  8267. $article = new EntityContext(
  8268. new ServerRequest(),
  8269. [
  8270. 'entity' => new Entity($this->article),
  8271. 'table' => new Table([
  8272. 'alias' => 'Articles',
  8273. 'schema' => $this->article['schema'],
  8274. 'validator' => $validator,
  8275. ]),
  8276. ]
  8277. );
  8278. $this->Form->create($article);
  8279. $result = $this->Form->control('title');
  8280. $expected = [
  8281. 'div' => ['class'],
  8282. 'label' => ['for'],
  8283. 'Title',
  8284. '/label',
  8285. 'input' => [
  8286. 'id',
  8287. 'name' => 'title',
  8288. 'type' => 'text',
  8289. 'required' => 'required',
  8290. 'maxlength' => 10,
  8291. 'data-validity-message' => 'This field cannot be left empty',
  8292. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8293. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8294. ],
  8295. '/div',
  8296. ];
  8297. $this->assertHtml($expected, $result);
  8298. }
  8299. /**
  8300. * testControlMaxLengthFormContext method
  8301. *
  8302. * Test control() with maxlength attribute in Form Context.
  8303. *
  8304. * @return void
  8305. */
  8306. public function testControlMaxLengthFormContext()
  8307. {
  8308. $validator = new Validator();
  8309. $validator->maxLength('title', 10);
  8310. $form = new Form();
  8311. $form->setValidator('default', $validator);
  8312. $this->Form->create($form);
  8313. $result = $this->Form->control('title');
  8314. $expected = [
  8315. 'div' => ['class'],
  8316. 'label' => ['for'],
  8317. 'Title',
  8318. '/label',
  8319. 'input' => [
  8320. 'id',
  8321. 'name' => 'title',
  8322. 'type' => 'text',
  8323. 'required' => 'required',
  8324. 'maxlength' => 10,
  8325. 'data-validity-message' => 'This field cannot be left empty',
  8326. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.value) this.setCustomValidity(this.dataset.validityMessage)',
  8327. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  8328. ],
  8329. '/div',
  8330. ];
  8331. $this->assertHtml($expected, $result);
  8332. }
  8333. }