FormHelperTest.php 312 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Core\Configure;
  18. use Cake\Form\Form;
  19. use Cake\Http\ServerRequest;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\Routing\Router;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Security;
  25. use Cake\View\Helper\FormHelper;
  26. use Cake\View\View;
  27. use Cake\View\Widget\WidgetLocator;
  28. /**
  29. * Test stub.
  30. */
  31. class Article extends Entity
  32. {
  33. }
  34. /**
  35. * Contact class
  36. */
  37. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->setSchema($this->_schema);
  64. }
  65. }
  66. /**
  67. * ValidateUser class
  68. */
  69. class ValidateUsersTable extends Table
  70. {
  71. /**
  72. * schema method
  73. *
  74. * @var array
  75. */
  76. protected $_schema = [
  77. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  78. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  79. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  81. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  82. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  83. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  84. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  85. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  86. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  87. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  88. ];
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config)
  95. {
  96. $this->setSchema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property \Cake\View\Helper\FormHelper $Form
  103. * @property \Cake\View\View $View
  104. */
  105. class FormHelperTest extends TestCase
  106. {
  107. /**
  108. * Fixtures to be used
  109. *
  110. * @var array
  111. */
  112. public $fixtures = ['core.articles', 'core.comments'];
  113. /**
  114. * Do not load the fixtures by default
  115. *
  116. * @var bool
  117. */
  118. public $autoFixtures = false;
  119. /**
  120. * @var array
  121. */
  122. protected $article = [];
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp()
  129. {
  130. parent::setUp();
  131. Configure::write('Config.language', 'eng');
  132. Configure::write('App.base', '');
  133. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  134. $this->View = new View();
  135. $this->Form = new FormHelper($this->View);
  136. $request = new ServerRequest([
  137. 'webroot' => '',
  138. 'base' => '',
  139. 'url' => '/articles/add',
  140. 'params' => [
  141. 'controller' => 'articles',
  142. 'action' => 'add',
  143. ]
  144. ]);
  145. $this->Form->Url->request = $this->Form->request = $request;
  146. $this->dateRegex = [
  147. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  148. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  149. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  152. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  153. ];
  154. $this->article = [
  155. 'schema' => [
  156. 'id' => ['type' => 'integer'],
  157. 'author_id' => ['type' => 'integer', 'null' => true],
  158. 'title' => ['type' => 'string', 'null' => true],
  159. 'body' => 'text',
  160. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  161. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  162. ],
  163. 'required' => [
  164. 'author_id' => true,
  165. 'title' => true,
  166. ]
  167. ];
  168. Security::setSalt('foo!');
  169. Router::connect('/:controller', ['action' => 'index']);
  170. Router::connect('/:controller/:action/*');
  171. }
  172. /**
  173. * tearDown method
  174. *
  175. * @return void
  176. */
  177. public function tearDown()
  178. {
  179. parent::tearDown();
  180. unset($this->Form, $this->Controller, $this->View);
  181. $this->getTableLocator()->clear();
  182. }
  183. /**
  184. * Test construct() with the templates option.
  185. *
  186. * @return void
  187. */
  188. public function testConstructTemplatesFile()
  189. {
  190. $helper = new FormHelper($this->View, [
  191. 'templates' => 'htmlhelper_tags'
  192. ]);
  193. $result = $helper->control('name');
  194. $this->assertContains('<input', $result);
  195. }
  196. /**
  197. * Test that when specifying custom widgets the config array for that widget
  198. * is overwritten instead of merged.
  199. *
  200. * @return void
  201. */
  202. public function testConstructWithWidgets()
  203. {
  204. $config = [
  205. 'widgets' => [
  206. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  207. ]
  208. ];
  209. $helper = new FormHelper($this->View, $config);
  210. $locator = $helper->getWidgetLocator();
  211. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  212. }
  213. /**
  214. * Test that when specifying custom widgets config file and it should be
  215. * added to widgets array. WidgetLocator will load widgets in constructor.
  216. *
  217. * @return void
  218. */
  219. public function testConstructWithWidgetsConfig()
  220. {
  221. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  222. $locator = $helper->getWidgetLocator();
  223. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  224. }
  225. /**
  226. * Test setting the widget locator
  227. *
  228. * @return void
  229. */
  230. public function testSetAndGetWidgetLocator()
  231. {
  232. $helper = new FormHelper($this->View);
  233. $locator = new WidgetLocator($helper->templater(), $this->View);
  234. $helper->setWidgetLocator($locator);
  235. $this->assertSame($locator, $helper->getWidgetLocator());
  236. }
  237. /**
  238. * Test registering a new widget class and rendering it.
  239. *
  240. * @return void
  241. */
  242. public function testAddWidgetAndRenderWidget()
  243. {
  244. $data = [
  245. 'val' => 1
  246. ];
  247. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  248. $this->Form->addWidget('test', $mock);
  249. $mock->expects($this->once())
  250. ->method('render')
  251. ->with($data)
  252. ->will($this->returnValue('HTML'));
  253. $result = $this->Form->widget('test', $data);
  254. $this->assertEquals('HTML', $result);
  255. }
  256. /**
  257. * Test that secureFields() of widget is called after calling render(),
  258. * not before.
  259. *
  260. * @return void
  261. */
  262. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  263. {
  264. $data = [
  265. 'val' => 1,
  266. 'name' => 'test'
  267. ];
  268. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  269. $this->Form->addWidget('test', $mock);
  270. $mock->expects($this->at(0))
  271. ->method('render')
  272. ->with($data)
  273. ->will($this->returnValue('HTML'));
  274. $mock->expects($this->at(1))
  275. ->method('secureFields')
  276. ->with($data)
  277. ->will($this->returnValue(['test']));
  278. $result = $this->Form->widget('test', $data + ['secure' => true]);
  279. $this->assertEquals('HTML', $result);
  280. }
  281. /**
  282. * Test that empty string is not added to secure fields list when
  283. * rendering input widget without name.
  284. *
  285. * @return void
  286. */
  287. public function testRenderingWidgetWithEmptyName()
  288. {
  289. $this->assertEquals([], $this->Form->fields);
  290. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  291. $this->assertEquals('<select name=""></select>', $result);
  292. $this->assertEquals([], $this->Form->fields);
  293. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  294. $this->assertEquals('<select name="0"></select>', $result);
  295. $this->assertEquals(['0'], $this->Form->fields);
  296. }
  297. /**
  298. * Test registering an invalid widget class.
  299. *
  300. * @return void
  301. */
  302. public function testAddWidgetInvalid()
  303. {
  304. $this->expectException(\RuntimeException::class);
  305. $mock = new \StdClass();
  306. $this->Form->addWidget('test', $mock);
  307. $this->Form->widget('test');
  308. }
  309. /**
  310. * Test adding a new context class.
  311. *
  312. * @return void
  313. */
  314. public function testAddContextProvider()
  315. {
  316. $context = 'My data';
  317. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  318. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  319. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  320. $this->assertEquals($context, $data['entity']);
  321. return $stub;
  322. });
  323. $this->Form->create($context);
  324. $result = $this->Form->context();
  325. $this->assertSame($stub, $result);
  326. }
  327. /**
  328. * Test replacing a context class.
  329. *
  330. * @return void
  331. */
  332. public function testAddContextProviderReplace()
  333. {
  334. $entity = new Article();
  335. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  336. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  337. return $stub;
  338. });
  339. $this->Form->create($entity);
  340. $result = $this->Form->context();
  341. $this->assertSame($stub, $result);
  342. }
  343. /**
  344. * Test overriding a context class.
  345. *
  346. * @return void
  347. */
  348. public function testAddContextProviderAdd()
  349. {
  350. $entity = new Article();
  351. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  352. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  353. if ($data['entity'] instanceof Entity) {
  354. return $stub;
  355. }
  356. });
  357. $this->Form->create($entity);
  358. $result = $this->Form->context();
  359. $this->assertSame($stub, $result);
  360. }
  361. /**
  362. * Test adding an invalid context class.
  363. *
  364. * @return void
  365. */
  366. public function testAddContextProviderInvalid()
  367. {
  368. $this->expectException(\RuntimeException::class);
  369. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  370. $context = 'My data';
  371. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  372. return new \StdClass();
  373. });
  374. $this->Form->create($context);
  375. }
  376. /**
  377. * Provides context options for create().
  378. *
  379. * @return array
  380. */
  381. public function contextSelectionProvider()
  382. {
  383. $entity = new Article();
  384. $collection = new Collection([$entity]);
  385. $emptyCollection = new Collection([]);
  386. $arrayObject = new \ArrayObject([]);
  387. $data = [
  388. 'schema' => [
  389. 'title' => ['type' => 'string']
  390. ]
  391. ];
  392. $form = new Form();
  393. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  394. return [
  395. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  396. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  397. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  398. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  399. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  400. 'form' => [$form, 'Cake\View\Form\FormContext'],
  401. 'none' => [null, 'Cake\View\Form\NullContext'],
  402. 'false' => [false, 'Cake\View\Form\NullContext'],
  403. 'custom' => [$custom, get_class($custom)]
  404. ];
  405. }
  406. /**
  407. * Test default context selection in create()
  408. *
  409. * @dataProvider contextSelectionProvider
  410. * @return void
  411. */
  412. public function testCreateContextSelectionBuiltIn($data, $class)
  413. {
  414. $this->loadFixtures('Articles');
  415. $this->Form->create($data);
  416. $this->assertInstanceOf($class, $this->Form->context());
  417. }
  418. /**
  419. * Data provider for type option.
  420. *
  421. * @return array
  422. */
  423. public static function requestTypeProvider()
  424. {
  425. return [
  426. // type, method, override
  427. ['post', 'post', 'POST'],
  428. ['put', 'post', 'PUT'],
  429. ['patch', 'post', 'PATCH'],
  430. ['delete', 'post', 'DELETE'],
  431. ];
  432. }
  433. /**
  434. * Test creating file forms.
  435. *
  436. * @return void
  437. */
  438. public function testCreateFile()
  439. {
  440. $encoding = strtolower(Configure::read('App.encoding'));
  441. $result = $this->Form->create(false, ['type' => 'file']);
  442. $expected = [
  443. 'form' => [
  444. 'method' => 'post', 'action' => '/articles/add',
  445. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  446. ],
  447. 'div' => ['style' => 'display:none;'],
  448. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  449. '/div'
  450. ];
  451. $this->assertHtml($expected, $result);
  452. }
  453. /**
  454. * Test creating GET forms.
  455. *
  456. * @return void
  457. */
  458. public function testCreateGet()
  459. {
  460. $encoding = strtolower(Configure::read('App.encoding'));
  461. $result = $this->Form->create(false, ['type' => 'get']);
  462. $expected = ['form' => [
  463. 'method' => 'get', 'action' => '/articles/add',
  464. 'accept-charset' => $encoding
  465. ]];
  466. $this->assertHtml($expected, $result);
  467. }
  468. /**
  469. * Test explicit method/enctype options.
  470. *
  471. * Explicit method overwrites inferred method from 'type'
  472. *
  473. * @return void
  474. */
  475. public function testCreateExplicitMethodEnctype()
  476. {
  477. $encoding = strtolower(Configure::read('App.encoding'));
  478. $result = $this->Form->create(false, [
  479. 'type' => 'get',
  480. 'method' => 'put',
  481. 'enctype' => 'multipart/form-data'
  482. ]);
  483. $expected = ['form' => [
  484. 'method' => 'put',
  485. 'action' => '/articles/add',
  486. 'enctype' => 'multipart/form-data',
  487. 'accept-charset' => $encoding
  488. ]];
  489. $this->assertHtml($expected, $result);
  490. }
  491. /**
  492. * Test create() with the templates option.
  493. *
  494. * @return void
  495. */
  496. public function testCreateTemplatesArray()
  497. {
  498. $result = $this->Form->create($this->article, [
  499. 'templates' => [
  500. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  501. ]
  502. ]);
  503. $expected = [
  504. 'form' => [
  505. 'class' => 'form-horizontal',
  506. 'method' => 'post',
  507. 'action' => '/articles/add',
  508. 'accept-charset' => 'utf-8'
  509. ]
  510. ];
  511. $this->assertHtml($expected, $result);
  512. }
  513. /**
  514. * Test create() with the templates option.
  515. *
  516. * @return void
  517. */
  518. public function testCreateTemplatesFile()
  519. {
  520. $result = $this->Form->create($this->article, [
  521. 'templates' => 'htmlhelper_tags',
  522. ]);
  523. $expected = [
  524. 'start form',
  525. 'div' => ['class' => 'hidden'],
  526. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  527. '/div'
  528. ];
  529. $this->assertHtml($expected, $result);
  530. }
  531. /**
  532. * Test that create() and end() restore templates.
  533. *
  534. * @return void
  535. */
  536. public function testCreateEndRestoreTemplates()
  537. {
  538. $this->Form->create($this->article, [
  539. 'templates' => ['input' => 'custom input element']
  540. ]);
  541. $this->Form->end();
  542. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  543. }
  544. /**
  545. * Test using template vars in various templates used by control() method.
  546. *
  547. * @return void
  548. */
  549. public function testControlTemplateVars()
  550. {
  551. $result = $this->Form->control('text', [
  552. 'templates' => [
  553. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  554. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  555. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  556. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  557. ],
  558. 'templateVars' => [
  559. 'forinput' => 'in-input',
  560. 'forlabel' => 'in-label',
  561. 'forgroup' => 'in-group',
  562. 'forcontainer' => 'in-container'
  563. ]
  564. ]);
  565. $expected = [
  566. 'div' => ['class'],
  567. 'label' => ['for'],
  568. 'Text in-label',
  569. '/label',
  570. 'in-group',
  571. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  572. 'in-container',
  573. '/div',
  574. ];
  575. $this->assertHtml($expected, $result);
  576. }
  577. /**
  578. * Test ensuring template variables work in template files loaded
  579. * during control().
  580. *
  581. * @return void
  582. */
  583. public function testControlTemplatesFromFile()
  584. {
  585. $result = $this->Form->control('title', [
  586. 'templates' => 'test_templates',
  587. 'templateVars' => [
  588. 'forcontainer' => 'container-data'
  589. ]
  590. ]);
  591. $expected = [
  592. 'div' => ['class'],
  593. 'label' => ['for'],
  594. 'Title',
  595. '/label',
  596. 'input' => ['name', 'type' => 'text', 'id'],
  597. 'container-data',
  598. '/div',
  599. ];
  600. $this->assertHtml($expected, $result);
  601. }
  602. /**
  603. * Test using template vars in inputSubmit and submitContainer template.
  604. *
  605. * @return void
  606. */
  607. public function testSubmitTemplateVars()
  608. {
  609. $this->Form->setTemplates([
  610. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  611. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  612. ]);
  613. $result = $this->Form->submit('Submit', [
  614. 'templateVars' => [
  615. 'forinput' => 'in-input',
  616. 'forcontainer' => 'in-container'
  617. ]
  618. ]);
  619. $expected = [
  620. 'div' => ['class'],
  621. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  622. 'in-container',
  623. '/div',
  624. ];
  625. $this->assertHtml($expected, $result);
  626. }
  627. /**
  628. * test the create() method
  629. *
  630. * @dataProvider requestTypeProvider
  631. * @return void
  632. */
  633. public function testCreateTypeOptions($type, $method, $override)
  634. {
  635. $encoding = strtolower(Configure::read('App.encoding'));
  636. $result = $this->Form->create(false, ['type' => $type]);
  637. $expected = [
  638. 'form' => [
  639. 'method' => $method, 'action' => '/articles/add',
  640. 'accept-charset' => $encoding
  641. ],
  642. 'div' => ['style' => 'display:none;'],
  643. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  644. '/div'
  645. ];
  646. $this->assertHtml($expected, $result);
  647. }
  648. /**
  649. * Test using template vars in Create (formStart template)
  650. *
  651. * @return void
  652. */
  653. public function testCreateTemplateVars()
  654. {
  655. $result = $this->Form->create($this->article, [
  656. 'templates' => [
  657. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  658. ],
  659. 'templateVars' => ['header' => 'headertext']
  660. ]);
  661. $expected = [
  662. 'h4' => ['class'],
  663. 'headertext',
  664. '/h4',
  665. 'form' => [
  666. 'method' => 'post',
  667. 'action' => '/articles/add',
  668. 'accept-charset' => 'utf-8'
  669. ]
  670. ];
  671. $this->assertHtml($expected, $result);
  672. }
  673. /**
  674. * Test opening a form for an update operation.
  675. *
  676. * @return void
  677. */
  678. public function testCreateUpdateForm()
  679. {
  680. $encoding = strtolower(Configure::read('App.encoding'));
  681. $this->Form->request = $this->Form->request
  682. ->withRequestTarget('/articles/edit/1')
  683. ->withParam('action', 'edit');
  684. $this->article['defaults']['id'] = 1;
  685. $result = $this->Form->create($this->article);
  686. $expected = [
  687. 'form' => [
  688. 'method' => 'post',
  689. 'action' => '/articles/edit/1',
  690. 'accept-charset' => $encoding
  691. ],
  692. 'div' => ['style' => 'display:none;'],
  693. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  694. '/div'
  695. ];
  696. $this->assertHtml($expected, $result);
  697. }
  698. /**
  699. * test create() with automatic url generation
  700. *
  701. * @return void
  702. */
  703. public function testCreateAutoUrl()
  704. {
  705. $encoding = strtolower(Configure::read('App.encoding'));
  706. $this->Form->request = $this->Form->request
  707. ->withRequestTarget('/articles/delete/10')
  708. ->withParam('action', 'delete');
  709. $result = $this->Form->create($this->article);
  710. $expected = [
  711. 'form' => [
  712. 'method' => 'post', 'action' => '/articles/delete/10',
  713. 'accept-charset' => $encoding
  714. ],
  715. 'div' => ['style' => 'display:none;'],
  716. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  717. '/div'
  718. ];
  719. $this->assertHtml($expected, $result);
  720. $this->article['defaults'] = ['id' => 1];
  721. $this->Form->request = $this->Form->request
  722. ->withRequestTarget('/articles/edit/1')
  723. ->withParam('action', 'delete');
  724. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  725. $expected = [
  726. 'form' => [
  727. 'method' => 'post',
  728. 'action' => '/articles/edit/1',
  729. 'accept-charset' => $encoding
  730. ],
  731. 'div' => ['style' => 'display:none;'],
  732. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  733. '/div'
  734. ];
  735. $this->assertHtml($expected, $result);
  736. $this->Form->request = $this->Form->request
  737. ->withParam('action', 'add');
  738. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  739. $expected = [
  740. 'form' => [
  741. 'method' => 'post',
  742. 'action' => '/articles/publish/1',
  743. 'accept-charset' => $encoding
  744. ],
  745. 'div' => ['style' => 'display:none;'],
  746. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  747. '/div'
  748. ];
  749. $this->assertHtml($expected, $result);
  750. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  751. $expected = [
  752. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  753. 'div' => ['style' => 'display:none;'],
  754. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  755. '/div'
  756. ];
  757. $this->assertHtml($expected, $result);
  758. $this->Form->request = $this->Form->request
  759. ->withParam('controller', 'Pages');
  760. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  761. $expected = [
  762. 'form' => [
  763. 'method' => 'post', 'action' => '/Pages/signup/1',
  764. 'accept-charset' => $encoding
  765. ],
  766. 'div' => ['style' => 'display:none;'],
  767. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  768. '/div'
  769. ];
  770. $this->assertHtml($expected, $result);
  771. }
  772. /**
  773. * Test create() with no URL (no "action" attribute for <form> tag)
  774. *
  775. * @return void
  776. */
  777. public function testCreateNoUrl()
  778. {
  779. $result = $this->Form->create(false, ['url' => false]);
  780. $expected = [
  781. 'form' => [
  782. 'method' => 'post',
  783. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  784. ],
  785. 'div' => ['style' => 'display:none;'],
  786. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  787. '/div'
  788. ];
  789. $this->assertHtml($expected, $result);
  790. }
  791. /**
  792. * test create() with a custom route
  793. *
  794. * @return void
  795. */
  796. public function testCreateCustomRoute()
  797. {
  798. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  799. $encoding = strtolower(Configure::read('App.encoding'));
  800. $this->Form->request = $this->Form->request
  801. ->withParam('controller', 'users');
  802. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  803. $expected = [
  804. 'form' => [
  805. 'method' => 'post', 'action' => '/login',
  806. 'accept-charset' => $encoding
  807. ],
  808. 'div' => ['style' => 'display:none;'],
  809. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  810. '/div'
  811. ];
  812. $this->assertHtml($expected, $result);
  813. Router::connect(
  814. '/new-article',
  815. ['controller' => 'articles', 'action' => 'myaction'],
  816. ['_name' => 'my-route']
  817. );
  818. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  819. $expected = [
  820. 'form' => [
  821. 'method' => 'post', 'action' => '/new-article',
  822. 'accept-charset' => $encoding,
  823. ],
  824. 'div' => ['style' => 'display:none;'],
  825. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  826. '/div'
  827. ];
  828. $this->assertHtml($expected, $result);
  829. }
  830. /**
  831. * test automatic accept-charset overriding
  832. *
  833. * @return void
  834. */
  835. public function testCreateWithAcceptCharset()
  836. {
  837. $result = $this->Form->create(
  838. $this->article,
  839. [
  840. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  841. ]
  842. );
  843. $expected = [
  844. 'form' => [
  845. 'method' => 'post', 'action' => '/articles',
  846. 'accept-charset' => 'iso-8859-1'
  847. ],
  848. 'div' => ['style' => 'display:none;'],
  849. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  850. '/div'
  851. ];
  852. $this->assertHtml($expected, $result);
  853. }
  854. /**
  855. * Test base form URL when url param is passed with multiple parameters (&)
  856. *
  857. */
  858. public function testCreateQueryStringRequest()
  859. {
  860. $encoding = strtolower(Configure::read('App.encoding'));
  861. $result = $this->Form->create($this->article, [
  862. 'type' => 'post',
  863. 'escape' => false,
  864. 'url' => [
  865. 'controller' => 'controller',
  866. 'action' => 'action',
  867. '?' => ['param1' => 'value1', 'param2' => 'value2']
  868. ]
  869. ]);
  870. $expected = [
  871. 'form' => [
  872. 'method' => 'post',
  873. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  874. 'accept-charset' => $encoding
  875. ],
  876. 'div' => ['style' => 'display:none;'],
  877. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  878. '/div'
  879. ];
  880. $this->assertHtml($expected, $result);
  881. $result = $this->Form->create($this->article, [
  882. 'type' => 'post',
  883. 'url' => [
  884. 'controller' => 'controller',
  885. 'action' => 'action',
  886. '?' => ['param1' => 'value1', 'param2' => 'value2']
  887. ]
  888. ]);
  889. $this->assertHtml($expected, $result);
  890. }
  891. /**
  892. * test that create() doesn't cause errors by multiple id's being in the primary key
  893. * as could happen with multiple select or checkboxes.
  894. *
  895. * @return void
  896. */
  897. public function testCreateWithMultipleIdInData()
  898. {
  899. $encoding = strtolower(Configure::read('App.encoding'));
  900. $this->Form->request = $this->Form->request->withData('Article.id', [1, 2]);
  901. $result = $this->Form->create($this->article);
  902. $expected = [
  903. 'form' => [
  904. 'method' => 'post',
  905. 'action' => '/articles/add',
  906. 'accept-charset' => $encoding
  907. ],
  908. 'div' => ['style' => 'display:none;'],
  909. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  910. '/div'
  911. ];
  912. $this->assertHtml($expected, $result);
  913. }
  914. /**
  915. * test that create() doesn't add in extra passed params.
  916. *
  917. * @return void
  918. */
  919. public function testCreatePassedArgs()
  920. {
  921. $encoding = strtolower(Configure::read('App.encoding'));
  922. $this->Form->request = $this->Form->request->withData('Article.id', 1);
  923. $result = $this->Form->create($this->article, [
  924. 'type' => 'post',
  925. 'escape' => false,
  926. 'url' => [
  927. 'action' => 'edit',
  928. 'myparam'
  929. ]
  930. ]);
  931. $expected = [
  932. 'form' => [
  933. 'method' => 'post',
  934. 'action' => '/articles/edit/myparam',
  935. 'accept-charset' => $encoding
  936. ],
  937. 'div' => ['style' => 'display:none;'],
  938. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  939. '/div'
  940. ];
  941. $this->assertHtml($expected, $result);
  942. }
  943. /**
  944. * test creating a get form, and get form inputs.
  945. *
  946. * @return void
  947. */
  948. public function testGetFormCreate()
  949. {
  950. $encoding = strtolower(Configure::read('App.encoding'));
  951. $result = $this->Form->create($this->article, ['type' => 'get']);
  952. $expected = ['form' => [
  953. 'method' => 'get', 'action' => '/articles/add',
  954. 'accept-charset' => $encoding
  955. ]];
  956. $this->assertHtml($expected, $result);
  957. $result = $this->Form->text('title');
  958. $expected = ['input' => [
  959. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  960. ]];
  961. $this->assertHtml($expected, $result);
  962. $result = $this->Form->password('password');
  963. $expected = ['input' => [
  964. 'name' => 'password', 'type' => 'password'
  965. ]];
  966. $this->assertHtml($expected, $result);
  967. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  968. $result = $this->Form->text('user_form');
  969. $expected = ['input' => [
  970. 'name' => 'user_form', 'type' => 'text'
  971. ]];
  972. $this->assertHtml($expected, $result);
  973. }
  974. /**
  975. * test get form, and inputs when the model param is false
  976. *
  977. * @return void
  978. */
  979. public function testGetFormWithFalseModel()
  980. {
  981. $encoding = strtolower(Configure::read('App.encoding'));
  982. $this->Form->request = $this->Form->request->withParam('controller', 'contact_test');
  983. $result = $this->Form->create(false, [
  984. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  985. ]);
  986. $expected = ['form' => [
  987. 'method' => 'get', 'action' => '/contact_test/add',
  988. 'accept-charset' => $encoding
  989. ]];
  990. $this->assertHtml($expected, $result);
  991. $result = $this->Form->text('reason');
  992. $expected = [
  993. 'input' => ['type' => 'text', 'name' => 'reason']
  994. ];
  995. $this->assertHtml($expected, $result);
  996. }
  997. /**
  998. * testFormCreateWithSecurity method
  999. *
  1000. * Test form->create() with security key.
  1001. *
  1002. * @return void
  1003. */
  1004. public function testCreateWithSecurity()
  1005. {
  1006. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  1007. $encoding = strtolower(Configure::read('App.encoding'));
  1008. $result = $this->Form->create($this->article, [
  1009. 'url' => '/articles/publish',
  1010. ]);
  1011. $expected = [
  1012. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  1013. 'div' => ['style' => 'display:none;'],
  1014. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1015. ['input' => [
  1016. 'type' => 'hidden',
  1017. 'name' => '_csrfToken',
  1018. 'value' => 'testKey',
  1019. 'autocomplete' => 'off',
  1020. ]],
  1021. '/div'
  1022. ];
  1023. $this->assertHtml($expected, $result);
  1024. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1025. $expected['form']['id'] = 'MyForm';
  1026. $this->assertHtml($expected, $result);
  1027. }
  1028. /**
  1029. * testFormCreateGetNoSecurity method
  1030. *
  1031. * Test form->create() with no security key as its a get form
  1032. *
  1033. * @return void
  1034. */
  1035. public function testCreateEndGetNoSecurity()
  1036. {
  1037. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  1038. $article = new Article();
  1039. $result = $this->Form->create($article, [
  1040. 'type' => 'get',
  1041. 'url' => '/contacts/add'
  1042. ]);
  1043. $this->assertNotContains('testKey', $result);
  1044. $result = $this->Form->end();
  1045. $this->assertNotContains('testKey', $result);
  1046. }
  1047. /**
  1048. * test that create() clears the fields property so it starts fresh
  1049. *
  1050. * @return void
  1051. */
  1052. public function testCreateClearingFields()
  1053. {
  1054. $this->Form->fields = ['model_id'];
  1055. $this->Form->create($this->article);
  1056. $this->assertEquals([], $this->Form->fields);
  1057. }
  1058. /**
  1059. * Tests form hash generation with model-less data
  1060. *
  1061. * @return void
  1062. */
  1063. public function testValidateHashNoModel()
  1064. {
  1065. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1066. $fields = ['anything'];
  1067. $result = $this->Form->secure($fields);
  1068. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1069. $this->assertContains($hash, $result);
  1070. }
  1071. /**
  1072. * Tests that hidden fields generated for checkboxes don't get locked
  1073. *
  1074. * @return void
  1075. */
  1076. public function testNoCheckboxLocking()
  1077. {
  1078. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1079. $this->assertSame([], $this->Form->fields);
  1080. $this->Form->checkbox('check', ['value' => '1']);
  1081. $this->assertSame($this->Form->fields, ['check']);
  1082. }
  1083. /**
  1084. * testFormSecurityFields method
  1085. *
  1086. * Test generation of secure form hash generation.
  1087. *
  1088. * @return void
  1089. */
  1090. public function testFormSecurityFields()
  1091. {
  1092. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1093. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1094. $result = $this->Form->secure($fields);
  1095. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1096. $hash .= ':' . 'Model.valid';
  1097. $hash = urlencode($hash);
  1098. $tokenDebug = urlencode(json_encode([
  1099. '',
  1100. $fields,
  1101. []
  1102. ]));
  1103. $expected = [
  1104. 'div' => ['style' => 'display:none;'],
  1105. ['input' => [
  1106. 'type' => 'hidden',
  1107. 'name' => '_Token[fields]',
  1108. 'value' => $hash,
  1109. 'autocomplete' => 'off',
  1110. ]],
  1111. ['input' => [
  1112. 'type' => 'hidden',
  1113. 'name' => '_Token[unlocked]',
  1114. 'value' => '',
  1115. 'autocomplete' => 'off',
  1116. ]],
  1117. ['input' => [
  1118. 'type' => 'hidden',
  1119. 'name' => '_Token[debug]',
  1120. 'value' => $tokenDebug,
  1121. 'autocomplete' => 'off',
  1122. ]],
  1123. '/div'
  1124. ];
  1125. $this->assertHtml($expected, $result);
  1126. }
  1127. /**
  1128. * testFormSecurityFields method
  1129. *
  1130. * Test debug token is not generated if debug is false
  1131. *
  1132. * @return void
  1133. */
  1134. public function testFormSecurityFieldsNoDebugMode()
  1135. {
  1136. Configure::write('debug', false);
  1137. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1138. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1139. $result = $this->Form->secure($fields);
  1140. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1141. $hash .= ':' . 'Model.valid';
  1142. $hash = urlencode($hash);
  1143. $expected = [
  1144. 'div' => ['style' => 'display:none;'],
  1145. ['input' => [
  1146. 'type' => 'hidden',
  1147. 'name' => '_Token[fields]',
  1148. 'autocomplete' => 'off',
  1149. 'value' => $hash
  1150. ]],
  1151. ['input' => [
  1152. 'type' => 'hidden',
  1153. 'name' => '_Token[unlocked]',
  1154. 'autocomplete' => 'off',
  1155. 'value' => '',
  1156. ]],
  1157. '/div'
  1158. ];
  1159. $this->assertHtml($expected, $result);
  1160. }
  1161. /**
  1162. * Tests correct generation of number fields for smallint
  1163. *
  1164. * @return void
  1165. */
  1166. public function testTextFieldGenerationForSmallint()
  1167. {
  1168. $this->article['schema'] = [
  1169. 'foo' => [
  1170. 'type' => 'smallinteger',
  1171. 'null' => false,
  1172. 'default' => null,
  1173. 'length' => 10
  1174. ]
  1175. ];
  1176. $this->Form->create($this->article);
  1177. $result = $this->Form->control('foo');
  1178. $this->assertContains('class="input number"', $result);
  1179. $this->assertContains('type="number"', $result);
  1180. }
  1181. /**
  1182. * Tests correct generation of number fields for tinyint
  1183. *
  1184. * @return void
  1185. */
  1186. public function testTextFieldGenerationForTinyint()
  1187. {
  1188. $this->article['schema'] = [
  1189. 'foo' => [
  1190. 'type' => 'tinyinteger',
  1191. 'null' => false,
  1192. 'default' => null,
  1193. 'length' => 10
  1194. ]
  1195. ];
  1196. $this->Form->create($this->article);
  1197. $result = $this->Form->control('foo');
  1198. $this->assertContains('class="input number"', $result);
  1199. $this->assertContains('type="number"', $result);
  1200. }
  1201. /**
  1202. * Tests correct generation of number fields for double and float fields
  1203. *
  1204. * @return void
  1205. */
  1206. public function testTextFieldGenerationForFloats()
  1207. {
  1208. $this->article['schema'] = [
  1209. 'foo' => [
  1210. 'type' => 'float',
  1211. 'null' => false,
  1212. 'default' => null,
  1213. 'length' => 10
  1214. ]
  1215. ];
  1216. $this->Form->create($this->article);
  1217. $result = $this->Form->control('foo');
  1218. $expected = [
  1219. 'div' => ['class' => 'input number'],
  1220. 'label' => ['for' => 'foo'],
  1221. 'Foo',
  1222. '/label',
  1223. ['input' => [
  1224. 'type' => 'number',
  1225. 'name' => 'foo',
  1226. 'id' => 'foo',
  1227. 'step' => 'any'
  1228. ]],
  1229. '/div'
  1230. ];
  1231. $this->assertHtml($expected, $result);
  1232. $result = $this->Form->control('foo', ['step' => 0.5]);
  1233. $expected = [
  1234. 'div' => ['class' => 'input number'],
  1235. 'label' => ['for' => 'foo'],
  1236. 'Foo',
  1237. '/label',
  1238. ['input' => [
  1239. 'type' => 'number',
  1240. 'name' => 'foo',
  1241. 'id' => 'foo',
  1242. 'step' => '0.5'
  1243. ]],
  1244. '/div'
  1245. ];
  1246. $this->assertHtml($expected, $result);
  1247. }
  1248. /**
  1249. * Tests correct generation of number fields for integer fields
  1250. *
  1251. * @return void
  1252. */
  1253. public function testTextFieldTypeNumberGenerationForIntegers()
  1254. {
  1255. $this->getTableLocator()->get('Contacts', [
  1256. 'className' => __NAMESPACE__ . '\ContactsTable'
  1257. ]);
  1258. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1259. $result = $this->Form->control('age');
  1260. $expected = [
  1261. 'div' => ['class' => 'input number'],
  1262. 'label' => ['for' => 'age'],
  1263. 'Age',
  1264. '/label',
  1265. ['input' => [
  1266. 'type' => 'number', 'name' => 'age',
  1267. 'id' => 'age'
  1268. ]],
  1269. '/div'
  1270. ];
  1271. $this->assertHtml($expected, $result);
  1272. }
  1273. /**
  1274. * Tests correct generation of file upload fields for binary fields
  1275. *
  1276. * @return void
  1277. */
  1278. public function testFileUploadFieldTypeGenerationForBinaries()
  1279. {
  1280. $table = $this->getTableLocator()->get('Contacts', [
  1281. 'className' => __NAMESPACE__ . '\ContactsTable'
  1282. ]);
  1283. $table->setSchema(['foo' => [
  1284. 'type' => 'binary',
  1285. 'null' => false,
  1286. 'default' => null,
  1287. 'length' => 1024
  1288. ]]);
  1289. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1290. $result = $this->Form->control('foo');
  1291. $expected = [
  1292. 'div' => ['class' => 'input file'],
  1293. 'label' => ['for' => 'foo'],
  1294. 'Foo',
  1295. '/label',
  1296. ['input' => [
  1297. 'type' => 'file', 'name' => 'foo',
  1298. 'id' => 'foo'
  1299. ]],
  1300. '/div'
  1301. ];
  1302. $this->assertHtml($expected, $result);
  1303. }
  1304. /**
  1305. * testFormSecurityMultipleFields method
  1306. *
  1307. * Test secure() with multiple row form. Ensure hash is correct.
  1308. *
  1309. * @return void
  1310. */
  1311. public function testFormSecurityMultipleFields()
  1312. {
  1313. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1314. $fields = [
  1315. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1316. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1317. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1318. ];
  1319. $result = $this->Form->secure($fields);
  1320. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1321. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1322. $tokenDebug = urlencode(json_encode([
  1323. '',
  1324. $fields,
  1325. []
  1326. ]));
  1327. $expected = [
  1328. 'div' => ['style' => 'display:none;'],
  1329. ['input' => [
  1330. 'type' => 'hidden',
  1331. 'name' => '_Token[fields]',
  1332. 'value' => $hash,
  1333. 'autocomplete' => 'off',
  1334. ]],
  1335. ['input' => [
  1336. 'type' => 'hidden',
  1337. 'name' => '_Token[unlocked]',
  1338. 'autocomplete' => 'off',
  1339. 'value' => ''
  1340. ]],
  1341. ['input' => [
  1342. 'type' => 'hidden',
  1343. 'name' => '_Token[debug]',
  1344. 'value' => $tokenDebug,
  1345. 'autocomplete' => 'off',
  1346. ]],
  1347. '/div'
  1348. ];
  1349. $this->assertHtml($expected, $result);
  1350. }
  1351. /**
  1352. * testFormSecurityMultipleSubmitButtons
  1353. *
  1354. * test form submit generation and ensure that _Token is only created on end()
  1355. *
  1356. * @return void
  1357. */
  1358. public function testFormSecurityMultipleSubmitButtons()
  1359. {
  1360. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1361. $this->Form->create($this->article);
  1362. $this->Form->text('Address.title');
  1363. $this->Form->text('Address.first_name');
  1364. $result = $this->Form->submit('Save', ['name' => 'save']);
  1365. $expected = [
  1366. 'div' => ['class' => 'submit'],
  1367. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1368. '/div',
  1369. ];
  1370. $this->assertHtml($expected, $result);
  1371. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1372. $expected = [
  1373. 'div' => ['class' => 'submit'],
  1374. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1375. '/div',
  1376. ];
  1377. $this->assertHtml($expected, $result);
  1378. $result = $this->Form->end();
  1379. $tokenDebug = urlencode(json_encode([
  1380. '/articles/add',
  1381. [
  1382. 'Address.title',
  1383. 'Address.first_name',
  1384. ],
  1385. ['save', 'cancel']
  1386. ]));
  1387. $expected = [
  1388. 'div' => ['style' => 'display:none;'],
  1389. ['input' => [
  1390. 'type' => 'hidden',
  1391. 'name' => '_Token[fields]',
  1392. 'autocomplete',
  1393. 'value'
  1394. ]],
  1395. ['input' => [
  1396. 'type' => 'hidden',
  1397. 'name' => '_Token[unlocked]',
  1398. 'value' => 'cancel%7Csave',
  1399. 'autocomplete' => 'off',
  1400. ]],
  1401. ['input' => [
  1402. 'type' => 'hidden',
  1403. 'name' => '_Token[debug]',
  1404. 'value' => $tokenDebug,
  1405. 'autocomplete' => 'off',
  1406. ]],
  1407. '/div'
  1408. ];
  1409. $this->assertHtml($expected, $result);
  1410. }
  1411. /**
  1412. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1413. *
  1414. * @return void
  1415. */
  1416. public function testSecurityButtonNestedNamed()
  1417. {
  1418. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  1419. $this->Form->create('Addresses');
  1420. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1421. $result = $this->Form->unlockField();
  1422. $this->assertEquals(['Address.button'], $result);
  1423. }
  1424. /**
  1425. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1426. *
  1427. * @return void
  1428. */
  1429. public function testSecuritySubmitNestedNamed()
  1430. {
  1431. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1432. $this->Form->create($this->article);
  1433. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1434. $result = $this->Form->unlockField();
  1435. $this->assertEquals(['Address.button'], $result);
  1436. }
  1437. /**
  1438. * Test that the correct fields are unlocked for image submits with no names.
  1439. *
  1440. * @return void
  1441. */
  1442. public function testSecuritySubmitImageNoName()
  1443. {
  1444. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1445. $this->Form->create(false);
  1446. $result = $this->Form->submit('save.png');
  1447. $expected = [
  1448. 'div' => ['class' => 'submit'],
  1449. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1450. '/div'
  1451. ];
  1452. $this->assertHtml($expected, $result);
  1453. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1454. }
  1455. /**
  1456. * Test that the correct fields are unlocked for image submits with names.
  1457. *
  1458. * @return void
  1459. */
  1460. public function testSecuritySubmitImageName()
  1461. {
  1462. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1463. $this->Form->create(null);
  1464. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1465. $expected = [
  1466. 'div' => ['class' => 'submit'],
  1467. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1468. '/div'
  1469. ];
  1470. $this->assertHtml($expected, $result);
  1471. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1472. }
  1473. /**
  1474. * testFormSecurityMultipleControlFields method
  1475. *
  1476. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1477. *
  1478. * @return void
  1479. */
  1480. public function testFormSecurityMultipleControlFields()
  1481. {
  1482. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1483. $this->Form->create();
  1484. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1485. $this->Form->control('Addresses.0.title');
  1486. $this->Form->control('Addresses.0.first_name');
  1487. $this->Form->control('Addresses.0.last_name');
  1488. $this->Form->control('Addresses.0.address');
  1489. $this->Form->control('Addresses.0.city');
  1490. $this->Form->control('Addresses.0.phone');
  1491. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1492. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1493. $this->Form->control('Addresses.1.title');
  1494. $this->Form->control('Addresses.1.first_name');
  1495. $this->Form->control('Addresses.1.last_name');
  1496. $this->Form->control('Addresses.1.address');
  1497. $this->Form->control('Addresses.1.city');
  1498. $this->Form->control('Addresses.1.phone');
  1499. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1500. $result = $this->Form->secure($this->Form->fields);
  1501. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1502. $tokenDebug = urlencode(json_encode([
  1503. '/articles/add',
  1504. [
  1505. 'Addresses.0.id' => '123456',
  1506. 'Addresses.0.title',
  1507. 'Addresses.0.first_name',
  1508. 'Addresses.0.last_name',
  1509. 'Addresses.0.address',
  1510. 'Addresses.0.city',
  1511. 'Addresses.0.phone',
  1512. 'Addresses.0.primary',
  1513. 'Addresses.1.id' => '654321',
  1514. 'Addresses.1.title',
  1515. 'Addresses.1.first_name',
  1516. 'Addresses.1.last_name',
  1517. 'Addresses.1.address',
  1518. 'Addresses.1.city',
  1519. 'Addresses.1.phone',
  1520. 'Addresses.1.primary',
  1521. ],
  1522. []
  1523. ]));
  1524. $expected = [
  1525. 'div' => ['style' => 'display:none;'],
  1526. ['input' => [
  1527. 'type' => 'hidden',
  1528. 'name' => '_Token[fields]',
  1529. 'value' => $hash,
  1530. 'autocomplete' => 'off',
  1531. ]],
  1532. ['input' => [
  1533. 'type' => 'hidden',
  1534. 'name' => '_Token[unlocked]',
  1535. 'autocomplete' => 'off',
  1536. 'value' => ''
  1537. ]],
  1538. ['input' => [
  1539. 'type' => 'hidden',
  1540. 'name' => '_Token[debug]',
  1541. 'value' => $tokenDebug,
  1542. 'autocomplete' => 'off',
  1543. ]],
  1544. '/div'
  1545. ];
  1546. $this->assertHtml($expected, $result);
  1547. }
  1548. /**
  1549. * testFormSecurityArrayFields method
  1550. *
  1551. * Test form security with Model.field.0 style inputs.
  1552. *
  1553. * @return void
  1554. */
  1555. public function testFormSecurityArrayFields()
  1556. {
  1557. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1558. $this->Form->create();
  1559. $this->Form->text('Address.primary.1');
  1560. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1561. $this->Form->text('Address.secondary.1.0');
  1562. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1563. }
  1564. /**
  1565. * testFormSecurityMultipleControlDisabledFields method
  1566. *
  1567. * Test secure form generation with multiple records and disabled fields.
  1568. *
  1569. * @return void
  1570. */
  1571. public function testFormSecurityMultipleControlDisabledFields()
  1572. {
  1573. $this->Form->request = $this->Form->request->withParam('_Token', [
  1574. 'unlockedFields' => ['first_name', 'address']
  1575. ]);
  1576. $this->Form->create();
  1577. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1578. $this->Form->text('Addresses.0.title');
  1579. $this->Form->text('Addresses.0.first_name');
  1580. $this->Form->text('Addresses.0.last_name');
  1581. $this->Form->text('Addresses.0.address');
  1582. $this->Form->text('Addresses.0.city');
  1583. $this->Form->text('Addresses.0.phone');
  1584. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1585. $this->Form->text('Addresses.1.title');
  1586. $this->Form->text('Addresses.1.first_name');
  1587. $this->Form->text('Addresses.1.last_name');
  1588. $this->Form->text('Addresses.1.address');
  1589. $this->Form->text('Addresses.1.city');
  1590. $this->Form->text('Addresses.1.phone');
  1591. $result = $this->Form->secure($this->Form->fields);
  1592. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1593. $tokenDebug = urlencode(json_encode([
  1594. '/articles/add',
  1595. [
  1596. 'Addresses.0.id' => '123456',
  1597. 'Addresses.0.title',
  1598. 'Addresses.0.last_name',
  1599. 'Addresses.0.city',
  1600. 'Addresses.0.phone',
  1601. 'Addresses.1.id' => '654321',
  1602. 'Addresses.1.title',
  1603. 'Addresses.1.last_name',
  1604. 'Addresses.1.city',
  1605. 'Addresses.1.phone'
  1606. ],
  1607. [
  1608. 'first_name',
  1609. 'address'
  1610. ]
  1611. ]));
  1612. $expected = [
  1613. 'div' => ['style' => 'display:none;'],
  1614. ['input' => [
  1615. 'type' => 'hidden',
  1616. 'name' => '_Token[fields]',
  1617. 'autocomplete' => 'off',
  1618. 'value' => $hash
  1619. ]],
  1620. ['input' => [
  1621. 'type' => 'hidden',
  1622. 'name' => '_Token[unlocked]',
  1623. 'autocomplete' => 'off',
  1624. 'value' => 'address%7Cfirst_name',
  1625. ]],
  1626. ['input' => [
  1627. 'type' => 'hidden',
  1628. 'name' => '_Token[debug]',
  1629. 'autocomplete' => 'off',
  1630. 'value' => $tokenDebug,
  1631. ]],
  1632. '/div'
  1633. ];
  1634. $this->assertHtml($expected, $result);
  1635. }
  1636. /**
  1637. * testFormSecurityControlDisabledFields method
  1638. *
  1639. * Test single record form with disabled fields.
  1640. *
  1641. * @return void
  1642. */
  1643. public function testFormSecurityControlUnlockedFields()
  1644. {
  1645. $this->Form->request = $this->Form->request->withParam('_Token', [
  1646. 'unlockedFields' => ['first_name', 'address']
  1647. ]);
  1648. $this->Form->create();
  1649. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1650. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1651. $this->Form->text('Addresses.title');
  1652. $this->Form->text('Addresses.first_name');
  1653. $this->Form->text('Addresses.last_name');
  1654. $this->Form->text('Addresses.address');
  1655. $this->Form->text('Addresses.city');
  1656. $this->Form->text('Addresses.phone');
  1657. $result = $this->Form->fields;
  1658. $expected = [
  1659. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1660. 'Addresses.city', 'Addresses.phone'
  1661. ];
  1662. $this->assertEquals($expected, $result);
  1663. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1664. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1665. $tokenDebug = urlencode(json_encode([
  1666. '/articles/add',
  1667. [
  1668. 'Addresses.id' => '123456',
  1669. 'Addresses.title',
  1670. 'Addresses.last_name',
  1671. 'Addresses.city',
  1672. 'Addresses.phone'
  1673. ],
  1674. [
  1675. 'first_name',
  1676. 'address'
  1677. ]
  1678. ]));
  1679. $expected = [
  1680. 'div' => ['style' => 'display:none;'],
  1681. ['input' => [
  1682. 'type' => 'hidden',
  1683. 'name' => '_Token[fields]',
  1684. 'value' => $hash,
  1685. 'autocomplete' => 'off',
  1686. 'data-foo' => 'bar',
  1687. ]],
  1688. ['input' => [
  1689. 'type' => 'hidden',
  1690. 'name' => '_Token[unlocked]',
  1691. 'value' => 'address%7Cfirst_name',
  1692. 'autocomplete' => 'off',
  1693. 'data-foo' => 'bar',
  1694. ]],
  1695. ['input' => [
  1696. 'type' => 'hidden', 'name' => '_Token[debug]',
  1697. 'value' => $tokenDebug,
  1698. 'autocomplete' => 'off',
  1699. 'data-foo' => 'bar'
  1700. ]],
  1701. '/div'
  1702. ];
  1703. $this->assertHtml($expected, $result);
  1704. }
  1705. /**
  1706. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1707. *
  1708. * Test single record form with debugSecurity param.
  1709. *
  1710. * @return void
  1711. */
  1712. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1713. {
  1714. $this->Form->request = $this->Form->request->withParam('_Token', [
  1715. 'unlockedFields' => ['first_name', 'address']
  1716. ]);
  1717. $this->Form->create();
  1718. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1719. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1720. $this->Form->text('Addresses.title');
  1721. $this->Form->text('Addresses.first_name');
  1722. $this->Form->text('Addresses.last_name');
  1723. $this->Form->text('Addresses.address');
  1724. $this->Form->text('Addresses.city');
  1725. $this->Form->text('Addresses.phone');
  1726. $result = $this->Form->fields;
  1727. $expected = [
  1728. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1729. 'Addresses.city', 'Addresses.phone'
  1730. ];
  1731. $this->assertEquals($expected, $result);
  1732. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1733. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1734. $tokenDebug = urlencode(json_encode([
  1735. '/articles/add',
  1736. [
  1737. 'Addresses.id' => '123456',
  1738. 'Addresses.title',
  1739. 'Addresses.last_name',
  1740. 'Addresses.city',
  1741. 'Addresses.phone'
  1742. ],
  1743. [
  1744. 'first_name',
  1745. 'address'
  1746. ]
  1747. ]));
  1748. $expected = [
  1749. 'div' => ['style' => 'display:none;'],
  1750. ['input' => [
  1751. 'type' => 'hidden',
  1752. 'name' => '_Token[fields]',
  1753. 'value' => $hash,
  1754. 'autocomplete' => 'off',
  1755. 'data-foo' => 'bar',
  1756. ]],
  1757. ['input' => [
  1758. 'type' => 'hidden',
  1759. 'name' => '_Token[unlocked]',
  1760. 'value' => 'address%7Cfirst_name',
  1761. 'autocomplete' => 'off',
  1762. 'data-foo' => 'bar',
  1763. ]],
  1764. ['input' => [
  1765. 'type' => 'hidden', 'name' => '_Token[debug]',
  1766. 'value' => $tokenDebug,
  1767. 'autocomplete' => 'off',
  1768. 'data-foo' => 'bar'
  1769. ]],
  1770. '/div'
  1771. ];
  1772. $this->assertHtml($expected, $result);
  1773. }
  1774. /**
  1775. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1776. *
  1777. * Debug is false, debugSecurity is true -> no debug
  1778. *
  1779. * @return void
  1780. */
  1781. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1782. {
  1783. $this->Form->request = $this->Form->request->withParam('_Token', [
  1784. 'unlockedFields' => ['first_name', 'address']
  1785. ]);
  1786. $this->Form->create();
  1787. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1788. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1789. $this->Form->text('Addresses.title');
  1790. $this->Form->text('Addresses.first_name');
  1791. $this->Form->text('Addresses.last_name');
  1792. $this->Form->text('Addresses.address');
  1793. $this->Form->text('Addresses.city');
  1794. $this->Form->text('Addresses.phone');
  1795. $result = $this->Form->fields;
  1796. $expected = [
  1797. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1798. 'Addresses.city', 'Addresses.phone'
  1799. ];
  1800. $this->assertEquals($expected, $result);
  1801. Configure::write('debug', false);
  1802. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1803. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1804. $expected = [
  1805. 'div' => ['style' => 'display:none;'],
  1806. ['input' => [
  1807. 'type' => 'hidden',
  1808. 'name' => '_Token[fields]',
  1809. 'value' => $hash,
  1810. 'autocomplete' => 'off',
  1811. 'data-foo' => 'bar',
  1812. ]],
  1813. ['input' => [
  1814. 'type' => 'hidden',
  1815. 'name' => '_Token[unlocked]',
  1816. 'value' => 'address%7Cfirst_name',
  1817. 'autocomplete' => 'off',
  1818. 'data-foo' => 'bar',
  1819. ]],
  1820. '/div'
  1821. ];
  1822. $this->assertHtml($expected, $result);
  1823. }
  1824. /**
  1825. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1826. *
  1827. * Test single record form with debugSecurity param.
  1828. *
  1829. * @return void
  1830. */
  1831. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1832. {
  1833. $this->Form->request = $this->Form->request->withParam('_Token', [
  1834. 'unlockedFields' => ['first_name', 'address']
  1835. ]);
  1836. $this->Form->create();
  1837. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1838. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1839. $this->Form->text('Addresses.title');
  1840. $this->Form->text('Addresses.first_name');
  1841. $this->Form->text('Addresses.last_name');
  1842. $this->Form->text('Addresses.address');
  1843. $this->Form->text('Addresses.city');
  1844. $this->Form->text('Addresses.phone');
  1845. $result = $this->Form->fields;
  1846. $expected = [
  1847. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1848. 'Addresses.city', 'Addresses.phone'
  1849. ];
  1850. $this->assertEquals($expected, $result);
  1851. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1852. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1853. $expected = [
  1854. 'div' => ['style' => 'display:none;'],
  1855. ['input' => [
  1856. 'type' => 'hidden',
  1857. 'name' => '_Token[fields]',
  1858. 'value' => $hash,
  1859. 'autocomplete' => 'off',
  1860. 'data-foo' => 'bar',
  1861. ]],
  1862. ['input' => [
  1863. 'type' => 'hidden',
  1864. 'name' => '_Token[unlocked]',
  1865. 'value' => 'address%7Cfirst_name',
  1866. 'autocomplete' => 'off',
  1867. 'data-foo' => 'bar',
  1868. ]],
  1869. '/div'
  1870. ];
  1871. $this->assertHtml($expected, $result);
  1872. }
  1873. /**
  1874. * testFormSecureWithCustomNameAttribute method
  1875. *
  1876. * Test securing inputs with custom name attributes.
  1877. *
  1878. * @return void
  1879. */
  1880. public function testFormSecureWithCustomNameAttribute()
  1881. {
  1882. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1883. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1884. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1885. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1886. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1887. }
  1888. /**
  1889. * testFormSecuredControl method
  1890. *
  1891. * Test generation of entire secure form, assertions made on control() output.
  1892. *
  1893. * @return void
  1894. */
  1895. public function testFormSecuredControl()
  1896. {
  1897. $this->Form->request = $this->Form->request
  1898. ->withParam('_Token', 'stuff')
  1899. ->withParam('_csrfToken', 'testKey');
  1900. $this->article['schema'] = [
  1901. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1902. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1903. ];
  1904. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1905. $encoding = strtolower(Configure::read('App.encoding'));
  1906. $expected = [
  1907. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1908. 'div' => ['style' => 'display:none;'],
  1909. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1910. ['input' => [
  1911. 'type' => 'hidden',
  1912. 'name' => '_csrfToken',
  1913. 'value' => 'testKey',
  1914. 'autocomplete' => 'off'
  1915. ]],
  1916. '/div'
  1917. ];
  1918. $this->assertHtml($expected, $result);
  1919. $result = $this->Form->control('ratio');
  1920. $expected = [
  1921. 'div' => ['class'],
  1922. 'label' => ['for'],
  1923. 'Ratio',
  1924. '/label',
  1925. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1926. '/div',
  1927. ];
  1928. $this->assertHtml($expected, $result);
  1929. $result = $this->Form->control('population');
  1930. $expected = [
  1931. 'div' => ['class'],
  1932. 'label' => ['for'],
  1933. 'Population',
  1934. '/label',
  1935. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1936. '/div',
  1937. ];
  1938. $this->assertHtml($expected, $result);
  1939. $result = $this->Form->control('published', ['type' => 'text']);
  1940. $expected = [
  1941. 'div' => ['class' => 'input text'],
  1942. 'label' => ['for' => 'published'],
  1943. 'Published',
  1944. '/label',
  1945. ['input' => [
  1946. 'type' => 'text',
  1947. 'name' => 'published',
  1948. 'id' => 'published'
  1949. ]],
  1950. '/div'
  1951. ];
  1952. $this->assertHtml($expected, $result);
  1953. $result = $this->Form->control('other', ['type' => 'text']);
  1954. $expected = [
  1955. 'div' => ['class' => 'input text'],
  1956. 'label' => ['for' => 'other'],
  1957. 'Other',
  1958. '/label',
  1959. ['input' => [
  1960. 'type' => 'text',
  1961. 'name' => 'other',
  1962. 'id',
  1963. ]],
  1964. '/div'
  1965. ];
  1966. $this->assertHtml($expected, $result);
  1967. $result = $this->Form->hidden('stuff');
  1968. $expected = [
  1969. 'input' => [
  1970. 'type' => 'hidden',
  1971. 'name' => 'stuff'
  1972. ]
  1973. ];
  1974. $this->assertHtml($expected, $result);
  1975. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1976. $expected = ['input' => [
  1977. 'type' => 'hidden',
  1978. 'name' => 'hidden',
  1979. 'value' => '0'
  1980. ]];
  1981. $this->assertHtml($expected, $result);
  1982. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1983. $expected = [
  1984. 'div' => ['class' => 'input checkbox'],
  1985. ['input' => [
  1986. 'type' => 'hidden',
  1987. 'name' => 'something',
  1988. 'value' => '0'
  1989. ]],
  1990. 'label' => ['for' => 'something'],
  1991. ['input' => [
  1992. 'type' => 'checkbox',
  1993. 'name' => 'something',
  1994. 'value' => '1',
  1995. 'id' => 'something'
  1996. ]],
  1997. 'Something',
  1998. '/label',
  1999. '/div'
  2000. ];
  2001. $this->assertHtml($expected, $result);
  2002. $result = $this->Form->fields;
  2003. $expectedFields = [
  2004. 'ratio',
  2005. 'population',
  2006. 'published',
  2007. 'other',
  2008. 'stuff' => '',
  2009. 'hidden' => '0',
  2010. 'something'
  2011. ];
  2012. $this->assertEquals($expectedFields, $result);
  2013. $result = $this->Form->secure($this->Form->fields);
  2014. $tokenDebug = urlencode(json_encode([
  2015. '/articles/add',
  2016. $expectedFields,
  2017. []
  2018. ]));
  2019. $expected = [
  2020. 'div' => ['style' => 'display:none;'],
  2021. ['input' => [
  2022. 'type' => 'hidden',
  2023. 'name' => '_Token[fields]',
  2024. 'value',
  2025. 'autocomplete'
  2026. ]],
  2027. ['input' => [
  2028. 'type' => 'hidden',
  2029. 'name' => '_Token[unlocked]',
  2030. 'value' => '',
  2031. 'autocomplete' => 'off'
  2032. ]],
  2033. ['input' => [
  2034. 'type' => 'hidden', 'name' => '_Token[debug]',
  2035. 'value' => $tokenDebug,
  2036. 'autocomplete' => 'off'
  2037. ]],
  2038. '/div'
  2039. ];
  2040. $this->assertHtml($expected, $result);
  2041. }
  2042. /**
  2043. * testSecuredControlCustomName method
  2044. *
  2045. * Test secured inputs with custom names.
  2046. *
  2047. * @return void
  2048. */
  2049. public function testSecuredControlCustomName()
  2050. {
  2051. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2052. $this->assertEquals([], $this->Form->fields);
  2053. $this->Form->text('text_input', [
  2054. 'name' => 'Option[General.default_role]',
  2055. ]);
  2056. $expected = ['Option.General.default_role'];
  2057. $this->assertEquals($expected, $this->Form->fields);
  2058. $this->Form->select('select_box', [1, 2], [
  2059. 'name' => 'Option[General.select_role]',
  2060. ]);
  2061. $expected[] = 'Option.General.select_role';
  2062. $this->assertEquals($expected, $this->Form->fields);
  2063. $this->Form->text('other.things[]');
  2064. $expected[] = 'other.things';
  2065. $this->assertEquals($expected, $this->Form->fields);
  2066. }
  2067. /**
  2068. * testSecuredControlDuplicate method
  2069. *
  2070. * Test that a hidden field followed by a visible field
  2071. * undoes the hidden field locking.
  2072. *
  2073. * @return void
  2074. */
  2075. public function testSecuredControlDuplicate()
  2076. {
  2077. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2078. $this->assertEquals([], $this->Form->fields);
  2079. $this->Form->control('text_val', [
  2080. 'type' => 'hidden',
  2081. 'value' => 'some text',
  2082. ]);
  2083. $expected = ['text_val' => 'some text'];
  2084. $this->assertEquals($expected, $this->Form->fields);
  2085. $this->Form->control('text_val', [
  2086. 'type' => 'text',
  2087. ]);
  2088. $expected = ['text_val'];
  2089. $this->assertEquals($expected, $this->Form->fields);
  2090. }
  2091. /**
  2092. * testFormSecuredFileControl method
  2093. *
  2094. * Tests that the correct keys are added to the field hash index.
  2095. *
  2096. * @return void
  2097. */
  2098. public function testFormSecuredFileControl()
  2099. {
  2100. $this->assertEquals([], $this->Form->fields);
  2101. $this->Form->file('Attachment.file');
  2102. $expected = [
  2103. 'Attachment.file.name', 'Attachment.file.type',
  2104. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2105. 'Attachment.file.size'
  2106. ];
  2107. $this->assertEquals($expected, $this->Form->fields);
  2108. }
  2109. /**
  2110. * testFormSecuredMultipleSelect method
  2111. *
  2112. * Test that multiple selects keys are added to field hash.
  2113. *
  2114. * @return void
  2115. */
  2116. public function testFormSecuredMultipleSelect()
  2117. {
  2118. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  2119. $this->assertEquals([], $this->Form->fields);
  2120. $options = ['1' => 'one', '2' => 'two'];
  2121. $this->Form->select('Model.select', $options);
  2122. $expected = ['Model.select'];
  2123. $this->assertEquals($expected, $this->Form->fields);
  2124. $this->Form->fields = [];
  2125. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2126. $this->assertEquals($expected, $this->Form->fields);
  2127. }
  2128. /**
  2129. * testFormSecuredRadio method
  2130. *
  2131. * @return void
  2132. */
  2133. public function testFormSecuredRadio()
  2134. {
  2135. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2136. $this->assertEquals([], $this->Form->fields);
  2137. $options = ['1' => 'option1', '2' => 'option2'];
  2138. $this->Form->radio('Test.test', $options);
  2139. $expected = ['Test.test'];
  2140. $this->assertEquals($expected, $this->Form->fields);
  2141. $this->Form->radio('Test.all', $options, [
  2142. 'disabled' => ['option1', 'option2']
  2143. ]);
  2144. $expected = ['Test.test', 'Test.all' => ''];
  2145. $this->assertEquals($expected, $this->Form->fields);
  2146. $this->Form->radio('Test.some', $options, [
  2147. 'disabled' => ['option1']
  2148. ]);
  2149. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2150. $this->assertEquals($expected, $this->Form->fields);
  2151. }
  2152. /**
  2153. * testFormSecuredAndDisabledNotAssoc method
  2154. *
  2155. * Test that when disabled is in a list based attribute array it works.
  2156. *
  2157. * @return void
  2158. */
  2159. public function testFormSecuredAndDisabledNotAssoc()
  2160. {
  2161. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2162. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2163. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2164. $this->Form->text('Model.text', ['disabled']);
  2165. $this->Form->textarea('Model.textarea', ['disabled']);
  2166. $this->Form->password('Model.password', ['disabled']);
  2167. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2168. $expected = [
  2169. 'Model.radio' => ''
  2170. ];
  2171. $this->assertEquals($expected, $this->Form->fields);
  2172. }
  2173. /**
  2174. * testFormSecuredAndDisabled method
  2175. *
  2176. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2177. * hashing.
  2178. *
  2179. * @return void
  2180. */
  2181. public function testFormSecuredAndDisabled()
  2182. {
  2183. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2184. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2185. $this->Form->text('Model.text', ['disabled' => true]);
  2186. $this->Form->password('Model.text', ['disabled' => true]);
  2187. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2188. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2189. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2190. $this->Form->year('Model.year', ['disabled' => true]);
  2191. $this->Form->month('Model.month', ['disabled' => true]);
  2192. $this->Form->day('Model.day', ['disabled' => true]);
  2193. $this->Form->hour('Model.hour', ['disabled' => true]);
  2194. $this->Form->minute('Model.minute', ['disabled' => true]);
  2195. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2196. $expected = [
  2197. 'Model.radio' => ''
  2198. ];
  2199. $this->assertEquals($expected, $this->Form->fields);
  2200. }
  2201. /**
  2202. * testDisableSecurityUsingForm method
  2203. *
  2204. * @return void
  2205. */
  2206. public function testDisableSecurityUsingForm()
  2207. {
  2208. $this->Form->request = $this->Form->request->withParam('_Token', [
  2209. 'disabledFields' => []
  2210. ]);
  2211. $this->Form->create();
  2212. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2213. $this->Form->text('Addresses.title');
  2214. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2215. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2216. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2217. $result = $this->Form->fields;
  2218. $expected = [
  2219. 'Addresses.id' => '123456', 'Addresses.title',
  2220. ];
  2221. $this->assertEquals($expected, $result);
  2222. }
  2223. /**
  2224. * testUnlockFieldAddsToList method
  2225. *
  2226. * Test disableField.
  2227. *
  2228. * @return void
  2229. */
  2230. public function testUnlockFieldAddsToList()
  2231. {
  2232. $this->Form->request = $this->Form->request->withParam('_Token', [
  2233. 'unlockedFields' => []
  2234. ]);
  2235. $this->Form->unlockField('Contact.name');
  2236. $this->Form->text('Contact.name');
  2237. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2238. $this->assertEquals([], $this->Form->fields);
  2239. }
  2240. /**
  2241. * testUnlockFieldRemovingFromFields method
  2242. *
  2243. * Test unlockField removing from fields array.
  2244. *
  2245. * @return void
  2246. */
  2247. public function testUnlockFieldRemovingFromFields()
  2248. {
  2249. $this->Form->request = $this->Form->request->withParam('_Token', [
  2250. 'unlockedFields' => []
  2251. ]);
  2252. $this->Form->create($this->article);
  2253. $this->Form->hidden('Article.id', ['value' => 1]);
  2254. $this->Form->text('Article.title');
  2255. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2256. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2257. $this->Form->unlockField('Article.title');
  2258. $this->Form->unlockField('Article.id');
  2259. $this->assertEquals([], $this->Form->fields);
  2260. }
  2261. /**
  2262. * testResetUnlockFields method
  2263. *
  2264. * Test reset unlockFields, when create new form.
  2265. *
  2266. * @return void
  2267. */
  2268. public function testResetUnlockFields()
  2269. {
  2270. $this->Form->request = $this->Form->request->withParam('_Token', [
  2271. 'key' => 'testKey',
  2272. 'unlockedFields' => []
  2273. ]);
  2274. $this->Form->unlockField('Contact.id');
  2275. $this->Form->create('Contact');
  2276. $this->Form->hidden('Contact.id', ['value' => 1]);
  2277. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2278. $this->Form->end();
  2279. $this->Form->create('Contact');
  2280. $this->Form->hidden('Contact.id', ['value' => 1]);
  2281. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2282. }
  2283. /**
  2284. * testSecuredFormUrlIgnoresHost method
  2285. *
  2286. * Test that only the path + query elements of a form's URL show up in their hash.
  2287. *
  2288. * @return void
  2289. */
  2290. public function testSecuredFormUrlIgnoresHost()
  2291. {
  2292. $this->Form->request = $this->Form->request->withParam('_Token', ['key' => 'testKey']);
  2293. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2294. $this->Form->create($this->article, [
  2295. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2296. ]);
  2297. $result = $this->Form->secure();
  2298. $this->assertContains($expected, $result);
  2299. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2300. $result = $this->Form->secure();
  2301. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2302. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2303. $result = $this->Form->secure();
  2304. $this->assertContains($expected, $result, 'URL path + query should work.');
  2305. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2306. $result = $this->Form->secure();
  2307. $this->assertNotContains($expected, $result, 'URL is different');
  2308. }
  2309. /**
  2310. * testSecuredFormUrlHasHtmlAndIdentifier method
  2311. *
  2312. * Test that URL, HTML and identifier show up in their hashes.
  2313. *
  2314. * @return void
  2315. */
  2316. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2317. {
  2318. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2319. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2320. $this->Form->create($this->article, [
  2321. 'url' => [
  2322. 'controller' => 'articles',
  2323. 'action' => 'view',
  2324. '?' => [
  2325. 'page' => 1,
  2326. 'limit' => 10,
  2327. 'html' => '<>"',
  2328. ],
  2329. '#' => 'result',
  2330. ],
  2331. ]);
  2332. $result = $this->Form->secure();
  2333. $this->assertContains($expected, $result);
  2334. $this->Form->create($this->article, [
  2335. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2336. ]);
  2337. $result = $this->Form->secure();
  2338. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2339. $this->Form->create($this->article, [
  2340. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2341. ]);
  2342. $result = $this->Form->secure();
  2343. $this->assertContains($expected, $result, 'URL path + query should work.');
  2344. }
  2345. /**
  2346. * testErrorMessageDisplay method
  2347. *
  2348. * Test error message display.
  2349. *
  2350. * @return void
  2351. */
  2352. public function testErrorMessageDisplay()
  2353. {
  2354. $this->article['errors'] = [
  2355. 'Article' => [
  2356. 'title' => 'error message',
  2357. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2358. ]
  2359. ];
  2360. $this->Form->create($this->article);
  2361. $result = $this->Form->control('Article.title');
  2362. $expected = [
  2363. 'div' => ['class' => 'input text error'],
  2364. 'label' => ['for' => 'article-title'],
  2365. 'Title',
  2366. '/label',
  2367. 'input' => [
  2368. 'type' => 'text', 'name' => 'Article[title]',
  2369. 'id' => 'article-title', 'class' => 'form-error'
  2370. ],
  2371. ['div' => ['class' => 'error-message']],
  2372. 'error message',
  2373. '/div',
  2374. '/div'
  2375. ];
  2376. $this->assertHtml($expected, $result);
  2377. $result = $this->Form->control('Article.title', [
  2378. 'templates' => [
  2379. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2380. ]
  2381. ]);
  2382. $expected = [
  2383. 'div' => ['class' => 'input text error'],
  2384. 'label' => ['for' => 'article-title'],
  2385. 'Title',
  2386. '/label',
  2387. 'input' => [
  2388. 'type' => 'text', 'name' => 'Article[title]',
  2389. 'id' => 'article-title', 'class' => 'form-error'
  2390. ],
  2391. '/div'
  2392. ];
  2393. $this->assertHtml($expected, $result);
  2394. $result = $this->Form->control('Article.content');
  2395. $expected = [
  2396. 'div' => ['class' => 'input text error'],
  2397. 'label' => ['for' => 'article-content'],
  2398. 'Content',
  2399. '/label',
  2400. 'input' => [
  2401. 'type' => 'text', 'name' => 'Article[content]',
  2402. 'id' => 'article-content', 'class' => 'form-error'
  2403. ],
  2404. ['div' => ['class' => 'error-message']],
  2405. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2406. '/div',
  2407. '/div'
  2408. ];
  2409. $this->assertHtml($expected, $result);
  2410. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2411. $expected = [
  2412. 'div' => ['class' => 'input text error'],
  2413. 'label' => ['for' => 'article-content'],
  2414. 'Content',
  2415. '/label',
  2416. 'input' => [
  2417. 'type' => 'text', 'name' => 'Article[content]',
  2418. 'id' => 'article-content', 'class' => 'form-error'
  2419. ],
  2420. ['div' => ['class' => 'error-message']],
  2421. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2422. '/div',
  2423. '/div'
  2424. ];
  2425. $this->assertHtml($expected, $result);
  2426. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2427. $expected = [
  2428. 'div' => ['class' => 'input text error'],
  2429. 'label' => ['for' => 'article-content'],
  2430. 'Content',
  2431. '/label',
  2432. 'input' => [
  2433. 'type' => 'text', 'name' => 'Article[content]',
  2434. 'id' => 'article-content', 'class' => 'form-error'
  2435. ],
  2436. ['div' => ['class' => 'error-message']],
  2437. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2438. '/div',
  2439. '/div'
  2440. ];
  2441. $this->assertHtml($expected, $result);
  2442. }
  2443. /**
  2444. * testEmptyErrorValidation method
  2445. *
  2446. * Test validation errors, when validation message is an empty string.
  2447. *
  2448. * @return void
  2449. */
  2450. public function testEmptyErrorValidation()
  2451. {
  2452. $this->article['errors'] = [
  2453. 'Article' => ['title' => '']
  2454. ];
  2455. $this->Form->create($this->article);
  2456. $result = $this->Form->control('Article.title');
  2457. $expected = [
  2458. 'div' => ['class' => 'input text error'],
  2459. 'label' => ['for' => 'article-title'],
  2460. 'Title',
  2461. '/label',
  2462. 'input' => [
  2463. 'type' => 'text', 'name' => 'Article[title]',
  2464. 'id' => 'article-title', 'class' => 'form-error'
  2465. ],
  2466. ['div' => ['class' => 'error-message']],
  2467. [],
  2468. '/div',
  2469. '/div'
  2470. ];
  2471. $this->assertHtml($expected, $result);
  2472. }
  2473. /**
  2474. * testEmptyControlErrorValidation method
  2475. *
  2476. * Test validation errors, when calling control() overriding validation message by an empty string.
  2477. *
  2478. * @return void
  2479. */
  2480. public function testEmptyControlErrorValidation()
  2481. {
  2482. $this->article['errors'] = [
  2483. 'Article' => ['title' => 'error message']
  2484. ];
  2485. $this->Form->create($this->article);
  2486. $result = $this->Form->control('Article.title', ['error' => '']);
  2487. $expected = [
  2488. 'div' => ['class' => 'input text error'],
  2489. 'label' => ['for' => 'article-title'],
  2490. 'Title',
  2491. '/label',
  2492. 'input' => [
  2493. 'type' => 'text', 'name' => 'Article[title]',
  2494. 'id' => 'article-title', 'class' => 'form-error'
  2495. ],
  2496. ['div' => ['class' => 'error-message']],
  2497. [],
  2498. '/div',
  2499. '/div'
  2500. ];
  2501. $this->assertHtml($expected, $result);
  2502. }
  2503. /**
  2504. * testControlErrorMessage method
  2505. *
  2506. * Test validation errors, when calling control() overriding validation messages.
  2507. *
  2508. * @return void
  2509. */
  2510. public function testControlErrorMessage()
  2511. {
  2512. $this->article['errors'] = [
  2513. 'title' => ['error message']
  2514. ];
  2515. $this->Form->create($this->article);
  2516. $result = $this->Form->control('title', [
  2517. 'error' => 'Custom error!'
  2518. ]);
  2519. $expected = [
  2520. 'div' => ['class' => 'input text required error'],
  2521. 'label' => ['for' => 'title'],
  2522. 'Title',
  2523. '/label',
  2524. 'input' => [
  2525. 'type' => 'text', 'name' => 'title',
  2526. 'id' => 'title', 'class' => 'form-error',
  2527. 'required' => 'required',
  2528. ],
  2529. ['div' => ['class' => 'error-message']],
  2530. 'Custom error!',
  2531. '/div',
  2532. '/div'
  2533. ];
  2534. $this->assertHtml($expected, $result);
  2535. $result = $this->Form->control('title', [
  2536. 'error' => ['error message' => 'Custom error!']
  2537. ]);
  2538. $expected = [
  2539. 'div' => ['class' => 'input text required error'],
  2540. 'label' => ['for' => 'title'],
  2541. 'Title',
  2542. '/label',
  2543. 'input' => [
  2544. 'type' => 'text',
  2545. 'name' => 'title',
  2546. 'id' => 'title',
  2547. 'class' => 'form-error',
  2548. 'required' => 'required'
  2549. ],
  2550. ['div' => ['class' => 'error-message']],
  2551. 'Custom error!',
  2552. '/div',
  2553. '/div'
  2554. ];
  2555. $this->assertHtml($expected, $result);
  2556. }
  2557. /**
  2558. * testFormValidationAssociated method
  2559. *
  2560. * Tests displaying errors for nested entities.
  2561. *
  2562. * @return void
  2563. */
  2564. public function testFormValidationAssociated()
  2565. {
  2566. $nested = new Entity(['foo' => 'bar']);
  2567. $nested->setError('foo', ['not a valid bar']);
  2568. $entity = new Entity(['nested' => $nested]);
  2569. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2570. $result = $this->Form->error('nested.foo');
  2571. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2572. }
  2573. /**
  2574. * testFormValidationAssociatedSecondLevel method
  2575. *
  2576. * Test form error display with associated model.
  2577. *
  2578. * @return void
  2579. */
  2580. public function testFormValidationAssociatedSecondLevel()
  2581. {
  2582. $inner = new Entity(['bar' => 'baz']);
  2583. $nested = new Entity(['foo' => $inner]);
  2584. $entity = new Entity(['nested' => $nested]);
  2585. $inner->setError('bar', ['not a valid one']);
  2586. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2587. $result = $this->Form->error('nested.foo.bar');
  2588. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2589. }
  2590. /**
  2591. * testFormValidationMultiRecord method
  2592. *
  2593. * Test form error display with multiple records.
  2594. *
  2595. * @return void
  2596. */
  2597. public function testFormValidationMultiRecord()
  2598. {
  2599. $one = new Entity();
  2600. $two = new Entity();
  2601. $this->getTableLocator()->get('Contacts', [
  2602. 'className' => __NAMESPACE__ . '\ContactsTable'
  2603. ]);
  2604. $one->set('email', '');
  2605. $one->setError('email', ['invalid email']);
  2606. $two->set('name', '');
  2607. $two->setError('name', ['This is wrong']);
  2608. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2609. $result = $this->Form->control('0.email');
  2610. $expected = [
  2611. 'div' => ['class' => 'input email error'],
  2612. 'label' => ['for' => '0-email'],
  2613. 'Email',
  2614. '/label',
  2615. 'input' => [
  2616. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2617. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2618. ],
  2619. ['div' => ['class' => 'error-message']],
  2620. 'invalid email',
  2621. '/div',
  2622. '/div'
  2623. ];
  2624. $this->assertHtml($expected, $result);
  2625. $result = $this->Form->control('1.name');
  2626. $expected = [
  2627. 'div' => ['class' => 'input text error'],
  2628. 'label' => ['for' => '1-name'],
  2629. 'Name',
  2630. '/label',
  2631. 'input' => [
  2632. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2633. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2634. ],
  2635. ['div' => ['class' => 'error-message']],
  2636. 'This is wrong',
  2637. '/div',
  2638. '/div'
  2639. ];
  2640. $this->assertHtml($expected, $result);
  2641. }
  2642. /**
  2643. * testControl method
  2644. *
  2645. * Test various incarnations of control().
  2646. *
  2647. * @return void
  2648. */
  2649. public function testControl()
  2650. {
  2651. $this->getTableLocator()->get('ValidateUsers', [
  2652. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2653. ]);
  2654. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2655. $result = $this->Form->control('ValidateUsers.balance');
  2656. $expected = [
  2657. 'div' => ['class'],
  2658. 'label' => ['for'],
  2659. 'Balance',
  2660. '/label',
  2661. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2662. '/div',
  2663. ];
  2664. $this->assertHtml($expected, $result);
  2665. $result = $this->Form->control('ValidateUser.cost_decimal');
  2666. $expected = [
  2667. 'div' => ['class'],
  2668. 'label' => ['for'],
  2669. 'Cost Decimal',
  2670. '/label',
  2671. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2672. '/div',
  2673. ];
  2674. $this->assertHtml($expected, $result);
  2675. $result = $this->Form->control('ValidateUser.null_decimal');
  2676. $expected = [
  2677. 'div' => ['class'],
  2678. 'label' => ['for'],
  2679. 'Null Decimal',
  2680. '/label',
  2681. 'input' => ['name', 'type' => 'number', 'id'],
  2682. '/div',
  2683. ];
  2684. $this->assertHtml($expected, $result);
  2685. }
  2686. /**
  2687. * testControlCustomization method
  2688. *
  2689. * Tests the input method and passing custom options.
  2690. *
  2691. * @return void
  2692. */
  2693. public function testControlCustomization()
  2694. {
  2695. $this->getTableLocator()->get('Contacts', [
  2696. 'className' => __NAMESPACE__ . '\ContactsTable'
  2697. ]);
  2698. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2699. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2700. $expected = [
  2701. 'div' => ['class' => 'input email'],
  2702. 'label' => ['for' => 'custom'],
  2703. 'Email',
  2704. '/label',
  2705. ['input' => [
  2706. 'type' => 'email', 'name' => 'Contact[email]',
  2707. 'id' => 'custom', 'maxlength' => 255
  2708. ]],
  2709. '/div'
  2710. ];
  2711. $this->assertHtml($expected, $result);
  2712. $result = $this->Form->control('Contact.email', [
  2713. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2714. ]);
  2715. $expected = [
  2716. '<div',
  2717. 'label' => ['for' => 'contact-email'],
  2718. 'Email',
  2719. '/label',
  2720. ['input' => [
  2721. 'type' => 'email', 'name' => 'Contact[email]',
  2722. 'id' => 'contact-email', 'maxlength' => 255
  2723. ]],
  2724. '/div'
  2725. ];
  2726. $this->assertHtml($expected, $result);
  2727. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2728. $expected = [
  2729. 'div' => ['class' => 'input text'],
  2730. 'label' => ['for' => 'contact-email'],
  2731. 'Email',
  2732. '/label',
  2733. ['input' => [
  2734. 'type' => 'text', 'name' => 'Contact[email]',
  2735. 'id' => 'contact-email', 'maxlength' => '255'
  2736. ]],
  2737. '/div'
  2738. ];
  2739. $this->assertHtml($expected, $result);
  2740. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2741. $expected = [
  2742. 'div' => ['class' => 'input text'],
  2743. 'label' => ['for' => 'contact-5-email'],
  2744. 'Email',
  2745. '/label',
  2746. ['input' => [
  2747. 'type' => 'text', 'name' => 'Contact[5][email]',
  2748. 'id' => 'contact-5-email', 'maxlength' => '255'
  2749. ]],
  2750. '/div'
  2751. ];
  2752. $this->assertHtml($expected, $result);
  2753. $result = $this->Form->control('Contact.password');
  2754. $expected = [
  2755. 'div' => ['class' => 'input password'],
  2756. 'label' => ['for' => 'contact-password'],
  2757. 'Password',
  2758. '/label',
  2759. ['input' => [
  2760. 'type' => 'password', 'name' => 'Contact[password]',
  2761. 'id' => 'contact-password'
  2762. ]],
  2763. '/div'
  2764. ];
  2765. $this->assertHtml($expected, $result);
  2766. $result = $this->Form->control('Contact.email', [
  2767. 'type' => 'file', 'class' => 'textbox'
  2768. ]);
  2769. $expected = [
  2770. 'div' => ['class' => 'input file'],
  2771. 'label' => ['for' => 'contact-email'],
  2772. 'Email',
  2773. '/label',
  2774. ['input' => [
  2775. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2776. 'id' => 'contact-email'
  2777. ]],
  2778. '/div'
  2779. ];
  2780. $this->assertHtml($expected, $result);
  2781. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2782. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2783. $result = $this->Form->control('phone');
  2784. $expected = [
  2785. 'div' => ['class' => 'input tel'],
  2786. 'label' => ['for' => 'phone'],
  2787. 'Phone',
  2788. '/label',
  2789. ['input' => [
  2790. 'type' => 'tel', 'name' => 'phone',
  2791. 'value' => 'Hello &amp; World &gt; weird chars',
  2792. 'id' => 'phone', 'maxlength' => 255
  2793. ]],
  2794. '/div'
  2795. ];
  2796. $this->assertHtml($expected, $result);
  2797. $this->Form->request = $this->Form->request->withData('Model.0.OtherModel.field', 'My value');
  2798. $this->Form->create();
  2799. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2800. $expected = [
  2801. 'div' => ['class' => 'input text'],
  2802. 'label' => ['for' => 'myId'],
  2803. 'Field',
  2804. '/label',
  2805. 'input' => [
  2806. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2807. 'value' => 'My value', 'id' => 'myId'
  2808. ],
  2809. '/div'
  2810. ];
  2811. $this->assertHtml($expected, $result);
  2812. $this->Form->request = $this->Form->request->withParsedBody([]);
  2813. $this->Form->create();
  2814. $entity->setError('field', 'Badness!');
  2815. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2816. $result = $this->Form->control('field');
  2817. $expected = [
  2818. 'div' => ['class' => 'input text error'],
  2819. 'label' => ['for' => 'field'],
  2820. 'Field',
  2821. '/label',
  2822. 'input' => [
  2823. 'type' => 'text', 'name' => 'field',
  2824. 'id' => 'field', 'class' => 'form-error'
  2825. ],
  2826. ['div' => ['class' => 'error-message']],
  2827. 'Badness!',
  2828. '/div',
  2829. '/div'
  2830. ];
  2831. $this->assertHtml($expected, $result);
  2832. $result = $this->Form->control('field', [
  2833. 'templates' => [
  2834. 'inputContainerError' => '{{content}}{{error}}',
  2835. 'error' => '<span class="error-message">{{content}}</span>'
  2836. ]
  2837. ]);
  2838. $expected = [
  2839. 'label' => ['for' => 'field'],
  2840. 'Field',
  2841. '/label',
  2842. 'input' => [
  2843. 'type' => 'text', 'name' => 'field',
  2844. 'id' => 'field', 'class' => 'form-error'
  2845. ],
  2846. ['span' => ['class' => 'error-message']],
  2847. 'Badness!',
  2848. '/span'
  2849. ];
  2850. $this->assertHtml($expected, $result);
  2851. $entity->setError('field', ['minLength'], true);
  2852. $result = $this->Form->control('field', [
  2853. 'error' => [
  2854. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2855. 'maxLength' => 'login too large'
  2856. ]
  2857. ]);
  2858. $expected = [
  2859. 'div' => ['class' => 'input text error'],
  2860. 'label' => ['for' => 'field'],
  2861. 'Field',
  2862. '/label',
  2863. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2864. ['div' => ['class' => 'error-message']],
  2865. 'Le login doit contenir au moins 2 caractères',
  2866. '/div',
  2867. '/div'
  2868. ];
  2869. $this->assertHtml($expected, $result);
  2870. $entity->setError('field', ['maxLength'], true);
  2871. $result = $this->Form->control('field', [
  2872. 'error' => [
  2873. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2874. 'maxLength' => 'login too large',
  2875. ]
  2876. ]);
  2877. $expected = [
  2878. 'div' => ['class' => 'input text error'],
  2879. 'label' => ['for' => 'field'],
  2880. 'Field',
  2881. '/label',
  2882. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2883. ['div' => ['class' => 'error-message']],
  2884. 'login too large',
  2885. '/div',
  2886. '/div'
  2887. ];
  2888. $this->assertHtml($expected, $result);
  2889. }
  2890. /**
  2891. * testControlWithTemplateFile method
  2892. *
  2893. * Test that control() accepts a template file.
  2894. *
  2895. * @return void
  2896. */
  2897. public function testControlWithTemplateFile()
  2898. {
  2899. $result = $this->Form->control('field', [
  2900. 'templates' => 'htmlhelper_tags'
  2901. ]);
  2902. $expected = [
  2903. 'label' => ['for' => 'field'],
  2904. 'Field',
  2905. '/label',
  2906. 'input' => [
  2907. 'type' => 'text', 'name' => 'field',
  2908. 'id' => 'field'
  2909. ],
  2910. ];
  2911. $this->assertHtml($expected, $result);
  2912. }
  2913. /**
  2914. * testNestedControlsEndWithBrackets method
  2915. *
  2916. * Test that nested inputs end with brackets.
  2917. *
  2918. * @return void
  2919. */
  2920. public function testNestedControlsEndWithBrackets()
  2921. {
  2922. $result = $this->Form->text('nested.text[]');
  2923. $expected = [
  2924. 'input' => [
  2925. 'type' => 'text', 'name' => 'nested[text][]'
  2926. ],
  2927. ];
  2928. $this->assertHtml($expected, $result);
  2929. $result = $this->Form->file('nested.file[]');
  2930. $expected = [
  2931. 'input' => [
  2932. 'type' => 'file', 'name' => 'nested[file][]'
  2933. ],
  2934. ];
  2935. $this->assertHtml($expected, $result);
  2936. }
  2937. /**
  2938. * testCreateIdPrefix method
  2939. *
  2940. * Test id prefix.
  2941. *
  2942. * @return void
  2943. */
  2944. public function testCreateIdPrefix()
  2945. {
  2946. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2947. $result = $this->Form->control('field');
  2948. $expected = [
  2949. 'div' => ['class' => 'input text'],
  2950. 'label' => ['for' => 'prefix-field'],
  2951. 'Field',
  2952. '/label',
  2953. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2954. '/div'
  2955. ];
  2956. $this->assertHtml($expected, $result);
  2957. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2958. $expected = [
  2959. 'div' => ['class' => 'input text'],
  2960. 'label' => ['for' => 'custom-id'],
  2961. 'Field',
  2962. '/label',
  2963. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2964. '/div'
  2965. ];
  2966. $this->assertHtml($expected, $result);
  2967. $result = $this->Form->radio('Model.field', ['option A']);
  2968. $expected = [
  2969. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2970. 'label' => ['for' => 'prefix-model-field-0'],
  2971. ['input' => [
  2972. 'type' => 'radio',
  2973. 'name' => 'Model[field]',
  2974. 'value' => '0',
  2975. 'id' => 'prefix-model-field-0'
  2976. ]],
  2977. 'option A',
  2978. '/label'
  2979. ];
  2980. $this->assertHtml($expected, $result);
  2981. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2982. $expected = [
  2983. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2984. 'label' => ['for' => 'prefix-model-field-0'],
  2985. ['input' => [
  2986. 'type' => 'radio',
  2987. 'name' => 'Model[field]',
  2988. 'value' => '0',
  2989. 'id' => 'prefix-model-field-0'
  2990. ]],
  2991. 'option A',
  2992. '/label'
  2993. ];
  2994. $this->assertHtml($expected, $result);
  2995. $result = $this->Form->select(
  2996. 'Model.multi_field',
  2997. ['first'],
  2998. ['multiple' => 'checkbox']
  2999. );
  3000. $expected = [
  3001. 'input' => [
  3002. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3003. ],
  3004. ['div' => ['class' => 'checkbox']],
  3005. ['label' => ['for' => 'prefix-model-multi-field-0']],
  3006. ['input' => [
  3007. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3008. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  3009. ]],
  3010. 'first',
  3011. '/label',
  3012. '/div',
  3013. ];
  3014. $this->assertHtml($expected, $result);
  3015. $this->Form->end();
  3016. $result = $this->Form->control('field');
  3017. $expected = [
  3018. 'div' => ['class' => 'input text'],
  3019. 'label' => ['for' => 'field'],
  3020. 'Field',
  3021. '/label',
  3022. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3023. '/div'
  3024. ];
  3025. $this->assertHtml($expected, $result);
  3026. }
  3027. /**
  3028. * testControlZero method
  3029. *
  3030. * Test that inputs with 0 can be created.
  3031. *
  3032. * @return void
  3033. */
  3034. public function testControlZero()
  3035. {
  3036. $this->getTableLocator()->get('Contacts', [
  3037. 'className' => __NAMESPACE__ . '\ContactsTable'
  3038. ]);
  3039. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3040. $result = $this->Form->control('0');
  3041. $expected = [
  3042. 'div' => ['class' => 'input text'],
  3043. 'label' => ['for' => '0'], '/label',
  3044. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3045. '/div'
  3046. ];
  3047. $this->assertHtml($expected, $result);
  3048. }
  3049. /**
  3050. * testControlCheckbox method
  3051. *
  3052. * Test control() with checkbox creation.
  3053. *
  3054. * @return void
  3055. */
  3056. public function testControlCheckbox()
  3057. {
  3058. $articles = $this->getTableLocator()->get('Articles');
  3059. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3060. $article = $articles->newEntity();
  3061. $this->Form->create($article);
  3062. $result = $this->Form->control('Articles.active');
  3063. $expected = [
  3064. 'div' => ['class' => 'input checkbox'],
  3065. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3066. 'label' => ['for' => 'articles-active'],
  3067. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3068. 'Active',
  3069. '/label',
  3070. '/div'
  3071. ];
  3072. $this->assertHtml($expected, $result);
  3073. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3074. $expected = [
  3075. 'div' => ['class' => 'input checkbox'],
  3076. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3077. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3078. '/div'
  3079. ];
  3080. $this->assertHtml($expected, $result);
  3081. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3082. $expected = [
  3083. 'div' => ['class' => 'input checkbox'],
  3084. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3085. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3086. '/div'
  3087. ];
  3088. $this->assertHtml($expected, $result);
  3089. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3090. $expected = [
  3091. 'div' => ['class' => 'input checkbox'],
  3092. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3093. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3094. '/div'
  3095. ];
  3096. $this->assertHtml($expected, $result);
  3097. $result = $this->Form->control('Articles.disabled', [
  3098. 'label' => 'Disabled',
  3099. 'type' => 'checkbox',
  3100. 'data-foo' => 'disabled'
  3101. ]);
  3102. $expected = [
  3103. 'div' => ['class' => 'input checkbox'],
  3104. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3105. 'label' => ['for' => 'articles-disabled'],
  3106. ['input' => [
  3107. 'type' => 'checkbox',
  3108. 'name' => 'Articles[disabled]',
  3109. 'value' => '1',
  3110. 'id' => 'articles-disabled',
  3111. 'data-foo' => 'disabled'
  3112. ]],
  3113. 'Disabled',
  3114. '/label',
  3115. '/div'
  3116. ];
  3117. $this->assertHtml($expected, $result);
  3118. $result = $this->Form->control('Articles.confirm', [
  3119. 'label' => 'Confirm <b>me</b>!',
  3120. 'type' => 'checkbox',
  3121. 'escape' => false
  3122. ]);
  3123. $expected = [
  3124. 'div' => ['class' => 'input checkbox'],
  3125. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3126. 'label' => ['for' => 'articles-confirm'],
  3127. ['input' => [
  3128. 'type' => 'checkbox',
  3129. 'name' => 'Articles[confirm]',
  3130. 'value' => '1',
  3131. 'id' => 'articles-confirm',
  3132. ]],
  3133. 'Confirm <b>me</b>!',
  3134. '/label',
  3135. '/div'
  3136. ];
  3137. $this->assertHtml($expected, $result);
  3138. }
  3139. /**
  3140. * testControlHidden method
  3141. *
  3142. * Test that control() does not create wrapping div and label tag for hidden fields.
  3143. *
  3144. * @return void
  3145. */
  3146. public function testControlHidden()
  3147. {
  3148. $this->getTableLocator()->get('ValidateUsers', [
  3149. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3150. ]);
  3151. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3152. $result = $this->Form->control('ValidateUser.id');
  3153. $expected = [
  3154. 'input' => ['name', 'type' => 'hidden', 'id']
  3155. ];
  3156. $this->assertHtml($expected, $result);
  3157. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3158. $expected = [
  3159. 'input' => ['name', 'type' => 'hidden', 'id']
  3160. ];
  3161. $this->assertHtml($expected, $result);
  3162. }
  3163. /**
  3164. * testControlDatetime method
  3165. *
  3166. * Test form->control() with datetime.
  3167. *
  3168. * @return void
  3169. */
  3170. public function testControlDatetime()
  3171. {
  3172. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3173. ->setMethods(['datetime'])
  3174. ->setConstructorArgs([new View()])
  3175. ->getMock();
  3176. $this->Form->expects($this->once())->method('datetime')
  3177. ->with('prueba', [
  3178. 'type' => 'datetime',
  3179. 'timeFormat' => 24,
  3180. 'minYear' => 2008,
  3181. 'maxYear' => 2011,
  3182. 'interval' => 15,
  3183. 'options' => null,
  3184. 'empty' => false,
  3185. 'id' => 'prueba',
  3186. 'required' => false,
  3187. 'templateVars' => []
  3188. ])
  3189. ->will($this->returnValue('This is it!'));
  3190. $result = $this->Form->control('prueba', [
  3191. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3192. 'maxYear' => 2011, 'interval' => 15
  3193. ]);
  3194. $expected = [
  3195. 'div' => ['class' => 'input datetime'],
  3196. '<label',
  3197. 'Prueba',
  3198. '/label',
  3199. 'This is it!',
  3200. '/div'
  3201. ];
  3202. $this->assertHtml($expected, $result);
  3203. }
  3204. /**
  3205. * testControlDatetimeIdPrefix method
  3206. *
  3207. * Test form->control() with datetime with id prefix.
  3208. *
  3209. * @return void
  3210. */
  3211. public function testControlDatetimeIdPrefix()
  3212. {
  3213. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3214. ->setMethods(['datetime'])
  3215. ->setConstructorArgs([new View()])
  3216. ->getMock();
  3217. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3218. $this->Form->expects($this->once())->method('datetime')
  3219. ->with('prueba', [
  3220. 'type' => 'datetime',
  3221. 'timeFormat' => 24,
  3222. 'minYear' => 2008,
  3223. 'maxYear' => 2011,
  3224. 'interval' => 15,
  3225. 'options' => null,
  3226. 'empty' => false,
  3227. 'id' => 'prefix-prueba',
  3228. 'required' => false,
  3229. 'templateVars' => []
  3230. ])
  3231. ->will($this->returnValue('This is it!'));
  3232. $result = $this->Form->control('prueba', [
  3233. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3234. 'maxYear' => 2011, 'interval' => 15
  3235. ]);
  3236. $expected = [
  3237. 'div' => ['class' => 'input datetime'],
  3238. '<label',
  3239. 'Prueba',
  3240. '/label',
  3241. 'This is it!',
  3242. '/div'
  3243. ];
  3244. $this->assertHtml($expected, $result);
  3245. }
  3246. /**
  3247. * testControlCheckboxWithDisabledElements method
  3248. *
  3249. * Test generating checkboxes with disabled elements.
  3250. *
  3251. * @return void
  3252. */
  3253. public function testControlCheckboxWithDisabledElements()
  3254. {
  3255. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3256. $result = $this->Form->control('Contact.multiple', [
  3257. 'multiple' => 'checkbox',
  3258. 'disabled' => 'disabled',
  3259. 'options' => $options
  3260. ]);
  3261. $expected = [
  3262. ['div' => ['class' => 'input select']],
  3263. ['label' => ['for' => 'contact-multiple']],
  3264. 'Multiple',
  3265. '/label',
  3266. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3267. ['div' => ['class' => 'checkbox']],
  3268. ['label' => ['for' => 'contact-multiple-1']],
  3269. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3270. 'One',
  3271. '/label',
  3272. '/div',
  3273. ['div' => ['class' => 'checkbox']],
  3274. ['label' => ['for' => 'contact-multiple-2']],
  3275. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3276. 'Two',
  3277. '/label',
  3278. '/div',
  3279. ['div' => ['class' => 'checkbox']],
  3280. ['label' => ['for' => 'contact-multiple-3']],
  3281. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3282. 'Three',
  3283. '/label',
  3284. '/div',
  3285. '/div'
  3286. ];
  3287. $this->assertHtml($expected, $result);
  3288. // make sure 50 does only disable 50, and not 50f5c0cf
  3289. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3290. $disabled = [50];
  3291. $expected = [
  3292. ['div' => ['class' => 'input select']],
  3293. ['label' => ['for' => 'contact-multiple']],
  3294. 'Multiple',
  3295. '/label',
  3296. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3297. ['div' => ['class' => 'checkbox']],
  3298. ['label' => ['for' => 'contact-multiple-50']],
  3299. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3300. 'Fifty',
  3301. '/label',
  3302. '/div',
  3303. ['div' => ['class' => 'checkbox']],
  3304. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3305. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3306. 'Stringy',
  3307. '/label',
  3308. '/div',
  3309. '/div'
  3310. ];
  3311. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3312. $this->assertHtml($expected, $result);
  3313. }
  3314. /**
  3315. * testControlWithLeadingInteger method
  3316. *
  3317. * Test input name with leading integer, ensure attributes are generated correctly.
  3318. *
  3319. * @return void
  3320. */
  3321. public function testControlWithLeadingInteger()
  3322. {
  3323. $result = $this->Form->text('0.Node.title');
  3324. $expected = [
  3325. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3326. ];
  3327. $this->assertHtml($expected, $result);
  3328. }
  3329. /**
  3330. * testControlSelectType method
  3331. *
  3332. * Test form->control() with select type inputs.
  3333. *
  3334. * @return void
  3335. */
  3336. public function testControlSelectType()
  3337. {
  3338. $result = $this->Form->control(
  3339. 'email',
  3340. [
  3341. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3342. );
  3343. $expected = [
  3344. 'div' => ['class' => 'input select'],
  3345. 'label' => ['for' => 'email'],
  3346. 'Email',
  3347. '/label',
  3348. ['select' => ['name' => 'email', 'id' => 'email']],
  3349. ['option' => ['value' => '']],
  3350. '/option',
  3351. ['option' => ['value' => 'è']],
  3352. 'Firést',
  3353. '/option',
  3354. ['option' => ['value' => 'é']],
  3355. 'Secoènd',
  3356. '/option',
  3357. '/select',
  3358. '/div'
  3359. ];
  3360. $this->assertHtml($expected, $result);
  3361. $result = $this->Form->control(
  3362. 'email',
  3363. [
  3364. 'options' => ['First', 'Second'], 'empty' => true]
  3365. );
  3366. $expected = [
  3367. 'div' => ['class' => 'input select'],
  3368. 'label' => ['for' => 'email'],
  3369. 'Email',
  3370. '/label',
  3371. ['select' => ['name' => 'email', 'id' => 'email']],
  3372. ['option' => ['value' => '']],
  3373. '/option',
  3374. ['option' => ['value' => '0']],
  3375. 'First',
  3376. '/option',
  3377. ['option' => ['value' => '1']],
  3378. 'Second',
  3379. '/option',
  3380. '/select',
  3381. '/div'
  3382. ];
  3383. $this->assertHtml($expected, $result);
  3384. $result = $this->Form->control('email', [
  3385. 'type' => 'select',
  3386. 'options' => new \ArrayObject(['First', 'Second']),
  3387. 'empty' => true
  3388. ]);
  3389. $this->assertHtml($expected, $result);
  3390. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3391. $this->Form->request = $this->Form->request->withData('Model', ['user_id' => 'value']);
  3392. $this->Form->create();
  3393. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3394. $expected = [
  3395. 'div' => ['class' => 'input select'],
  3396. 'label' => ['for' => 'model-user-id'],
  3397. 'User',
  3398. '/label',
  3399. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3400. ['option' => ['value' => '']],
  3401. '/option',
  3402. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3403. 'good',
  3404. '/option',
  3405. ['option' => ['value' => 'other']],
  3406. 'bad',
  3407. '/option',
  3408. '/select',
  3409. '/div'
  3410. ];
  3411. $this->assertHtml($expected, $result);
  3412. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3413. $this->Form->request = $this->Form->request->withData('Thing', ['user_id' => null]);
  3414. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3415. $expected = [
  3416. 'div' => ['class' => 'input select'],
  3417. 'label' => ['for' => 'thing-user-id'],
  3418. 'User',
  3419. '/label',
  3420. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3421. ['option' => ['value' => '']],
  3422. 'Some Empty',
  3423. '/option',
  3424. ['option' => ['value' => 'value']],
  3425. 'good',
  3426. '/option',
  3427. ['option' => ['value' => 'other']],
  3428. 'bad',
  3429. '/option',
  3430. '/select',
  3431. '/div'
  3432. ];
  3433. $this->assertHtml($expected, $result);
  3434. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3435. $this->Form->request = $this->Form->request->withData('Thing', ['user_id' => 'value']);
  3436. $this->Form->create();
  3437. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3438. $expected = [
  3439. 'div' => ['class' => 'input select'],
  3440. 'label' => ['for' => 'thing-user-id'],
  3441. 'User',
  3442. '/label',
  3443. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3444. ['option' => ['value' => '']],
  3445. 'Some Empty',
  3446. '/option',
  3447. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3448. 'good',
  3449. '/option',
  3450. ['option' => ['value' => 'other']],
  3451. 'bad',
  3452. '/option',
  3453. '/select',
  3454. '/div'
  3455. ];
  3456. $this->assertHtml($expected, $result);
  3457. $result = $this->Form->control('Publisher.id', [
  3458. 'label' => 'Publisher',
  3459. 'type' => 'select',
  3460. 'multiple' => 'checkbox',
  3461. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3462. ]);
  3463. $expected = [
  3464. ['div' => ['class' => 'input select']],
  3465. ['label' => ['for' => 'publisher-id']],
  3466. 'Publisher',
  3467. '/label',
  3468. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3469. ['div' => ['class' => 'checkbox']],
  3470. ['label' => ['for' => 'publisher-id-value-1']],
  3471. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3472. 'Label 1',
  3473. '/label',
  3474. '/div',
  3475. ['div' => ['class' => 'checkbox']],
  3476. ['label' => ['for' => 'publisher-id-value-2']],
  3477. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3478. 'Label 2',
  3479. '/label',
  3480. '/div',
  3481. '/div'
  3482. ];
  3483. $this->assertHtml($expected, $result);
  3484. }
  3485. /**
  3486. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3487. *
  3488. * Test that control() and a non standard primary key makes a hidden input by default.
  3489. *
  3490. * @return void
  3491. */
  3492. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3493. {
  3494. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3495. $this->Form->create($this->article);
  3496. $result = $this->Form->control('title');
  3497. $expected = [
  3498. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3499. ];
  3500. $this->assertHtml($expected, $result);
  3501. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3502. $this->Form->create($this->article);
  3503. $result = $this->Form->control('title');
  3504. $expected = [
  3505. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3506. ];
  3507. $this->assertHtml($expected, $result);
  3508. $result = $this->Form->control('body');
  3509. $expected = [
  3510. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3511. ];
  3512. $this->assertHtml($expected, $result);
  3513. }
  3514. /**
  3515. * testControlOverridingMagicSelectType method
  3516. *
  3517. * Test that overriding the magic select type widget is possible.
  3518. *
  3519. * @return void
  3520. */
  3521. public function testControlOverridingMagicSelectType()
  3522. {
  3523. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3524. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3525. $expected = [
  3526. 'div' => ['class' => 'input text'],
  3527. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3528. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3529. '/div'
  3530. ];
  3531. $this->assertHtml($expected, $result);
  3532. //Check that magic types still work for plural/singular vars
  3533. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3534. $result = $this->Form->control('Model.type');
  3535. $expected = [
  3536. 'div' => ['class' => 'input select'],
  3537. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3538. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3539. ['option' => ['value' => 'value']], 'good', '/option',
  3540. ['option' => ['value' => 'other']], 'bad', '/option',
  3541. '/select',
  3542. '/div'
  3543. ];
  3544. $this->assertHtml($expected, $result);
  3545. }
  3546. /**
  3547. * testControlMagicTypeDoesNotOverride method
  3548. *
  3549. * Test that inferred types do not override developer input.
  3550. *
  3551. * @return void
  3552. */
  3553. public function testControlMagicTypeDoesNotOverride()
  3554. {
  3555. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3556. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3557. $expected = [
  3558. 'div' => ['class' => 'input checkbox'],
  3559. ['input' => [
  3560. 'type' => 'hidden',
  3561. 'name' => 'Model[user]',
  3562. 'value' => 0,
  3563. ]],
  3564. 'label' => ['for' => 'model-user'],
  3565. ['input' => [
  3566. 'name' => 'Model[user]',
  3567. 'type' => 'checkbox',
  3568. 'id' => 'model-user',
  3569. 'value' => 1
  3570. ]],
  3571. 'User',
  3572. '/label',
  3573. '/div'
  3574. ];
  3575. $this->assertHtml($expected, $result);
  3576. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3577. $options = [
  3578. 1 => 'blue',
  3579. 2 => 'red'
  3580. ];
  3581. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3582. $expected = [
  3583. 'div' => ['class' => 'input select'],
  3584. 'label' => ['for' => 'tags-ids'],
  3585. 'Tags',
  3586. '/label',
  3587. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3588. ['div' => ['class' => 'checkbox']],
  3589. ['label' => ['for' => 'tags-ids-1']],
  3590. ['input' => [
  3591. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3592. 'value' => '1', 'name' => 'tags[_ids][]'
  3593. ]],
  3594. 'blue',
  3595. '/label',
  3596. '/div',
  3597. ['div' => ['class' => 'checkbox']],
  3598. ['label' => ['for' => 'tags-ids-2']],
  3599. ['input' => [
  3600. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3601. 'value' => '2', 'name' => 'tags[_ids][]'
  3602. ]],
  3603. 'red',
  3604. '/label',
  3605. '/div',
  3606. '/div'
  3607. ];
  3608. $this->assertHtml($expected, $result);
  3609. }
  3610. /**
  3611. * testControlMagicSelectForTypeNumber method
  3612. *
  3613. * Test that magic control() selects are created for type=number.
  3614. *
  3615. * @return void
  3616. */
  3617. public function testControlMagicSelectForTypeNumber()
  3618. {
  3619. $this->getTableLocator()->get('ValidateUsers', [
  3620. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3621. ]);
  3622. $entity = new Entity(['balance' => 1]);
  3623. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3624. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3625. $result = $this->Form->control('balance');
  3626. $expected = [
  3627. 'div' => ['class' => 'input select'],
  3628. 'label' => ['for' => 'balance'],
  3629. 'Balance',
  3630. '/label',
  3631. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3632. ['option' => ['value' => '0']],
  3633. 'nothing',
  3634. '/option',
  3635. ['option' => ['value' => '1', 'selected' => 'selected']],
  3636. 'some',
  3637. '/option',
  3638. ['option' => ['value' => '100']],
  3639. 'a lot',
  3640. '/option',
  3641. '/select',
  3642. '/div'
  3643. ];
  3644. $this->assertHtml($expected, $result);
  3645. }
  3646. /**
  3647. * testInvalidControlTypeOption method
  3648. *
  3649. * Test invalid 'input' type option to control() function.
  3650. *
  3651. * @return void
  3652. */
  3653. public function testInvalidControlTypeOption()
  3654. {
  3655. $this->expectException(\RuntimeException::class);
  3656. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3657. $this->Form->control('text', ['type' => 'input']);
  3658. }
  3659. /**
  3660. * testControlMagicSelectChangeToRadio method
  3661. *
  3662. * Test that magic control() selects can easily be converted into radio types without error.
  3663. *
  3664. * @return void
  3665. */
  3666. public function testControlMagicSelectChangeToRadio()
  3667. {
  3668. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3669. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3670. $this->assertContains('input type="radio"', $result);
  3671. }
  3672. /**
  3673. * testFormControlSubmit method
  3674. *
  3675. * Test correct results for form::control() and type submit.
  3676. *
  3677. * @return void
  3678. */
  3679. public function testFormControlSubmit()
  3680. {
  3681. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3682. $expected = [
  3683. 'div' => ['class' => 'submit'],
  3684. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3685. '/div'
  3686. ];
  3687. $this->assertHtml($expected, $result);
  3688. }
  3689. /**
  3690. * testFormControls method
  3691. *
  3692. * Test correct results from Form::controls().
  3693. *
  3694. * @return void
  3695. */
  3696. public function testFormControlsLegendFieldset()
  3697. {
  3698. $this->Form->create($this->article);
  3699. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3700. $expected = [
  3701. '<fieldset',
  3702. '<legend',
  3703. 'The Legend',
  3704. '/legend',
  3705. '*/fieldset',
  3706. ];
  3707. $this->assertHtml($expected, $result);
  3708. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3709. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3710. $this->assertContains('<fieldset>', $result);
  3711. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3712. $this->assertNotContains('<legend>', $result);
  3713. $this->assertNotContains('<fieldset>', $result);
  3714. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3715. $this->assertNotContains('<legend>', $result);
  3716. $this->assertNotContains('<fieldset>', $result);
  3717. $this->Form->create($this->article);
  3718. $this->Form->request = $this->Form->request
  3719. ->withParam('prefix', 'admin')
  3720. ->withParam('action', 'admin_edit')
  3721. ->withParam('controller', 'articles');
  3722. $result = $this->Form->allControls();
  3723. $expected = [
  3724. '<fieldset',
  3725. '<legend',
  3726. 'New Article',
  3727. '/legend',
  3728. '*/fieldset',
  3729. ];
  3730. $this->assertHtml($expected, $result);
  3731. $this->Form->create($this->article);
  3732. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3733. $expected = [
  3734. '<fieldset',
  3735. '<legend',
  3736. 'The Legend',
  3737. '/legend',
  3738. '*/fieldset',
  3739. ];
  3740. $this->assertHtml($expected, $result);
  3741. $this->Form->create($this->article);
  3742. $result = $this->Form->allControls([], [
  3743. 'fieldset' => [
  3744. 'class' => 'some-class some-other-class',
  3745. 'disabled' => true,
  3746. 'data-param' => 'a-param'
  3747. ],
  3748. 'legend' => 'The Legend'
  3749. ]);
  3750. $expected = [
  3751. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3752. '<legend',
  3753. 'The Legend',
  3754. '/legend',
  3755. '*/fieldset',
  3756. ];
  3757. $this->assertHtml($expected, $result);
  3758. }
  3759. /**
  3760. * testFormControls method
  3761. *
  3762. * Test the controls() method.
  3763. *
  3764. * @return void
  3765. */
  3766. public function testFormControls()
  3767. {
  3768. $this->Form->create($this->article);
  3769. $result = $this->Form->allControls();
  3770. $expected = [
  3771. '<fieldset',
  3772. '<legend', 'New Article', '/legend',
  3773. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3774. ['div' => ['class' => 'input select required']],
  3775. '*/div',
  3776. ['div' => ['class' => 'input text required']],
  3777. '*/div',
  3778. ['div' => ['class' => 'input text']],
  3779. '*/div',
  3780. ['div' => ['class' => 'input text']],
  3781. '*/div',
  3782. '/fieldset',
  3783. ];
  3784. $this->assertHtml($expected, $result);
  3785. $result = $this->Form->allControls([
  3786. 'published' => ['type' => 'boolean']
  3787. ]);
  3788. $expected = [
  3789. '<fieldset',
  3790. '<legend', 'New Article', '/legend',
  3791. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3792. ['div' => ['class' => 'input select required']],
  3793. '*/div',
  3794. ['div' => ['class' => 'input text required']],
  3795. '*/div',
  3796. ['div' => ['class' => 'input text']],
  3797. '*/div',
  3798. ['div' => ['class' => 'input boolean']],
  3799. '*/div',
  3800. '/fieldset',
  3801. ];
  3802. $this->assertHtml($expected, $result);
  3803. $this->Form->create($this->article);
  3804. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3805. $expected = [
  3806. 'fieldset' => [],
  3807. 'legend' => [],
  3808. 'Hello',
  3809. '/legend',
  3810. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3811. ['div' => ['class' => 'input select required']],
  3812. '*/div',
  3813. ['div' => ['class' => 'input text required']],
  3814. '*/div',
  3815. ['div' => ['class' => 'input text']],
  3816. '*/div',
  3817. ['div' => ['class' => 'input text']],
  3818. '*/div',
  3819. '/fieldset'
  3820. ];
  3821. $this->assertHtml($expected, $result);
  3822. $this->Form->create(false);
  3823. $expected = [
  3824. 'fieldset' => [],
  3825. ['div' => ['class' => 'input text']],
  3826. 'label' => ['for' => 'foo'],
  3827. 'Foo',
  3828. '/label',
  3829. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3830. '*/div',
  3831. '/fieldset'
  3832. ];
  3833. $result = $this->Form->allControls(
  3834. ['foo' => ['type' => 'text']],
  3835. ['legend' => false]
  3836. );
  3837. $this->assertHtml($expected, $result);
  3838. }
  3839. /**
  3840. * testFormControlsBlacklist method
  3841. *
  3842. * @return void
  3843. */
  3844. public function testFormControlsBlacklist()
  3845. {
  3846. $this->Form->create($this->article);
  3847. $result = $this->Form->allControls([
  3848. 'id' => false
  3849. ]);
  3850. $expected = [
  3851. '<fieldset',
  3852. '<legend', 'New Article', '/legend',
  3853. ['div' => ['class' => 'input select required']],
  3854. '*/div',
  3855. ['div' => ['class' => 'input text required']],
  3856. '*/div',
  3857. ['div' => ['class' => 'input text']],
  3858. '*/div',
  3859. ['div' => ['class' => 'input text']],
  3860. '*/div',
  3861. '/fieldset',
  3862. ];
  3863. $this->assertHtml($expected, $result);
  3864. $this->Form->create($this->article);
  3865. $result = $this->Form->allControls([
  3866. 'id' => []
  3867. ]);
  3868. $expected = [
  3869. '<fieldset',
  3870. '<legend', 'New Article', '/legend',
  3871. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3872. ['div' => ['class' => 'input select required']],
  3873. '*/div',
  3874. ['div' => ['class' => 'input text required']],
  3875. '*/div',
  3876. ['div' => ['class' => 'input text']],
  3877. '*/div',
  3878. ['div' => ['class' => 'input text']],
  3879. '*/div',
  3880. '/fieldset',
  3881. ];
  3882. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3883. }
  3884. /**
  3885. * testSelectAsCheckbox method
  3886. *
  3887. * Test multi-select widget with checkbox formatting.
  3888. *
  3889. * @return void
  3890. */
  3891. public function testSelectAsCheckbox()
  3892. {
  3893. $result = $this->Form->select(
  3894. 'Model.multi_field',
  3895. ['first', 'second', 'third'],
  3896. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3897. );
  3898. $expected = [
  3899. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3900. ['div' => ['class' => 'checkbox']],
  3901. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3902. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3903. 'first',
  3904. '/label',
  3905. '/div',
  3906. ['div' => ['class' => 'checkbox']],
  3907. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3908. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3909. 'second',
  3910. '/label',
  3911. '/div',
  3912. ['div' => ['class' => 'checkbox']],
  3913. ['label' => ['for' => 'model-multi-field-2']],
  3914. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3915. 'third',
  3916. '/label',
  3917. '/div',
  3918. ];
  3919. $this->assertHtml($expected, $result);
  3920. $result = $this->Form->select(
  3921. 'Model.multi_field',
  3922. ['1/2' => 'half'],
  3923. ['multiple' => 'checkbox']
  3924. );
  3925. $expected = [
  3926. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3927. ['div' => ['class' => 'checkbox']],
  3928. ['label' => ['for' => 'model-multi-field-1-2']],
  3929. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3930. 'half',
  3931. '/label',
  3932. '/div',
  3933. ];
  3934. $this->assertHtml($expected, $result);
  3935. }
  3936. /**
  3937. * testLabel method
  3938. *
  3939. * Test label generation.
  3940. *
  3941. * @return void
  3942. */
  3943. public function testLabel()
  3944. {
  3945. $result = $this->Form->label('Person.name');
  3946. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3947. $this->assertHtml($expected, $result);
  3948. $result = $this->Form->label('Person.name');
  3949. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3950. $this->assertHtml($expected, $result);
  3951. $result = $this->Form->label('Person.first_name');
  3952. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3953. $this->assertHtml($expected, $result);
  3954. $result = $this->Form->label('Person.first_name', 'Your first name');
  3955. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3956. $this->assertHtml($expected, $result);
  3957. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3958. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3959. $this->assertHtml($expected, $result);
  3960. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3961. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3962. $this->assertHtml($expected, $result);
  3963. $result = $this->Form->label('Person.first_name', '');
  3964. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3965. $this->assertHtml($expected, $result);
  3966. $result = $this->Form->label('Person.2.name', '');
  3967. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3968. $this->assertHtml($expected, $result);
  3969. }
  3970. /**
  3971. * testLabelContainControl method
  3972. *
  3973. * Test that label() can accept an input with the correct template vars.
  3974. *
  3975. * @return void
  3976. */
  3977. public function testLabelContainControl()
  3978. {
  3979. $this->Form->setTemplates([
  3980. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3981. ]);
  3982. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3983. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3984. ]);
  3985. $expected = [
  3986. 'label' => ['for' => 'person-accept-terms'],
  3987. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3988. 'Accept',
  3989. '/label',
  3990. ];
  3991. $this->assertHtml($expected, $result);
  3992. }
  3993. /**
  3994. * testTextbox method
  3995. *
  3996. * Test textbox element generation.
  3997. *
  3998. * @return void
  3999. */
  4000. public function testTextbox()
  4001. {
  4002. $result = $this->Form->text('Model.field');
  4003. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  4004. $this->assertHtml($expected, $result);
  4005. $result = $this->Form->text('Model.field', ['type' => 'password']);
  4006. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  4007. $this->assertHtml($expected, $result);
  4008. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  4009. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  4010. $this->assertHtml($expected, $result);
  4011. }
  4012. /**
  4013. * testTextBoxDataAndError method
  4014. *
  4015. * Test that text() hooks up with request data and error fields.
  4016. *
  4017. * @return void
  4018. */
  4019. public function testTextBoxDataAndError()
  4020. {
  4021. $this->article['errors'] = [
  4022. 'Contact' => ['text' => 'wrong']
  4023. ];
  4024. $this->Form->request = $this->Form->request
  4025. ->withData('Model.text', 'test <strong>HTML</strong> values')
  4026. ->withData('Contact.text', 'test');
  4027. $this->Form->create($this->article);
  4028. $result = $this->Form->text('Model.text');
  4029. $expected = [
  4030. 'input' => [
  4031. 'type' => 'text',
  4032. 'name' => 'Model[text]',
  4033. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4034. ]
  4035. ];
  4036. $this->assertHtml($expected, $result);
  4037. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4038. $expected = [
  4039. 'input' => [
  4040. 'type' => 'text',
  4041. 'name' => 'Contact[text]',
  4042. 'value' => 'test',
  4043. 'id' => 'theID',
  4044. 'class' => 'form-error'
  4045. ]
  4046. ];
  4047. $this->assertHtml($expected, $result);
  4048. }
  4049. /**
  4050. * testDefaultValue method
  4051. *
  4052. * Test default value setting.
  4053. *
  4054. * @return void
  4055. */
  4056. public function testTextDefaultValue()
  4057. {
  4058. $this->Form->request = $this->Form->request->withData('Model.field', 'test');
  4059. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4060. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4061. $this->assertHtml($expected, $result);
  4062. $this->Form->request = $this->Form->request->withParsedBody([]);
  4063. $this->Form->create();
  4064. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4065. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4066. $this->assertHtml($expected, $result);
  4067. $Articles = $this->getTableLocator()->get('Articles');
  4068. $title = $Articles->getSchema()->getColumn('title');
  4069. $Articles->getSchema()->addColumn(
  4070. 'title',
  4071. ['default' => 'default title'] + $title
  4072. );
  4073. $entity = $Articles->newEntity();
  4074. $this->Form->create($entity);
  4075. // Get default value from schema
  4076. $result = $this->Form->text('title');
  4077. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4078. $this->assertHtml($expected, $result);
  4079. // Don't get value from schema
  4080. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4081. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4082. $this->assertHtml($expected, $result);
  4083. // Custom default value overrides default value from schema
  4084. $result = $this->Form->text('title', ['default' => 'override default']);
  4085. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4086. $this->assertHtml($expected, $result);
  4087. // Default value from schema is used only for new entities.
  4088. $entity->isNew(false);
  4089. $result = $this->Form->text('title');
  4090. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4091. $this->assertHtml($expected, $result);
  4092. }
  4093. /**
  4094. * testError method
  4095. *
  4096. * Test field error generation.
  4097. *
  4098. * @return void
  4099. */
  4100. public function testError()
  4101. {
  4102. $this->article['errors'] = [
  4103. 'Article' => ['field' => 'email']
  4104. ];
  4105. $this->Form->create($this->article);
  4106. $result = $this->Form->error('Article.field');
  4107. $expected = [
  4108. ['div' => ['class' => 'error-message']],
  4109. 'email',
  4110. '/div',
  4111. ];
  4112. $this->assertHtml($expected, $result);
  4113. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4114. $expected = [
  4115. ['div' => ['class' => 'error-message']],
  4116. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4117. '/div',
  4118. ];
  4119. $this->assertHtml($expected, $result);
  4120. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4121. $expected = [
  4122. ['div' => ['class' => 'error-message']],
  4123. '<strong', 'Badness!', '/strong',
  4124. '/div',
  4125. ];
  4126. $this->assertHtml($expected, $result);
  4127. }
  4128. /**
  4129. * testErrorRuleName method
  4130. *
  4131. * Test error translation can use rule names for translating.
  4132. *
  4133. * @return void
  4134. */
  4135. public function testErrorRuleName()
  4136. {
  4137. $this->article['errors'] = [
  4138. 'Article' => [
  4139. 'field' => ['email' => 'Your email was not good']
  4140. ]
  4141. ];
  4142. $this->Form->create($this->article);
  4143. $result = $this->Form->error('Article.field');
  4144. $expected = [
  4145. ['div' => ['class' => 'error-message']],
  4146. 'Your email was not good',
  4147. '/div',
  4148. ];
  4149. $this->assertHtml($expected, $result);
  4150. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4151. $expected = [
  4152. ['div' => ['class' => 'error-message']],
  4153. 'Email in use',
  4154. '/div',
  4155. ];
  4156. $this->assertHtml($expected, $result);
  4157. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4158. $expected = [
  4159. ['div' => ['class' => 'error-message']],
  4160. 'Email in use',
  4161. '/div',
  4162. ];
  4163. $this->assertHtml($expected, $result);
  4164. $result = $this->Form->error('Article.field', [
  4165. 'email' => 'Key is preferred',
  4166. 'Your email was not good' => 'Email in use'
  4167. ]);
  4168. $expected = [
  4169. ['div' => ['class' => 'error-message']],
  4170. 'Key is preferred',
  4171. '/div',
  4172. ];
  4173. $this->assertHtml($expected, $result);
  4174. }
  4175. /**
  4176. * testErrorMessages method
  4177. *
  4178. * Test error with nested lists.
  4179. *
  4180. * @return void
  4181. */
  4182. public function testErrorMessages()
  4183. {
  4184. $this->article['errors'] = [
  4185. 'Article' => ['field' => 'email']
  4186. ];
  4187. $this->Form->create($this->article);
  4188. $result = $this->Form->error('Article.field', [
  4189. 'email' => 'No good!'
  4190. ]);
  4191. $expected = [
  4192. 'div' => ['class' => 'error-message'],
  4193. 'No good!',
  4194. '/div'
  4195. ];
  4196. $this->assertHtml($expected, $result);
  4197. }
  4198. /**
  4199. * testErrorMultipleMessages method
  4200. *
  4201. * Test error() with multiple messages.
  4202. *
  4203. * @return void
  4204. */
  4205. public function testErrorMultipleMessages()
  4206. {
  4207. $this->article['errors'] = [
  4208. 'field' => ['notBlank', 'email', 'Something else']
  4209. ];
  4210. $this->Form->create($this->article);
  4211. $result = $this->Form->error('field', [
  4212. 'notBlank' => 'Cannot be empty',
  4213. 'email' => 'No good!'
  4214. ]);
  4215. $expected = [
  4216. 'div' => ['class' => 'error-message'],
  4217. 'ul' => [],
  4218. '<li', 'Cannot be empty', '/li',
  4219. '<li', 'No good!', '/li',
  4220. '<li', 'Something else', '/li',
  4221. '/ul',
  4222. '/div'
  4223. ];
  4224. $this->assertHtml($expected, $result);
  4225. }
  4226. /**
  4227. * testPassword method
  4228. *
  4229. * Test password element generation.
  4230. *
  4231. * @return void
  4232. */
  4233. public function testPassword()
  4234. {
  4235. $this->article['errors'] = [
  4236. 'Contact' => [
  4237. 'passwd' => 1
  4238. ]
  4239. ];
  4240. $this->Form->create($this->article);
  4241. $result = $this->Form->password('Contact.field');
  4242. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4243. $this->assertHtml($expected, $result);
  4244. $this->Form->request = $this->Form->request->withData('Contact.passwd', 'test');
  4245. $this->Form->create($this->article);
  4246. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4247. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4248. $this->assertHtml($expected, $result);
  4249. }
  4250. /**
  4251. * testRadio method
  4252. *
  4253. * Test radio element set generation.
  4254. *
  4255. * @return void
  4256. */
  4257. public function testRadio()
  4258. {
  4259. $result = $this->Form->radio('Model.field', ['option A']);
  4260. $expected = [
  4261. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4262. 'label' => ['for' => 'model-field-0'],
  4263. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4264. 'option A',
  4265. '/label'
  4266. ];
  4267. $this->assertHtml($expected, $result);
  4268. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4269. $this->assertHtml($expected, $result);
  4270. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4271. $expected = [
  4272. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4273. ['label' => ['for' => 'model-field-0']],
  4274. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4275. 'option A',
  4276. '/label',
  4277. ['label' => ['for' => 'model-field-1']],
  4278. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4279. 'option B',
  4280. '/label',
  4281. ];
  4282. $this->assertHtml($expected, $result);
  4283. $result = $this->Form->radio(
  4284. 'Employee.gender',
  4285. ['male' => 'Male', 'female' => 'Female'],
  4286. ['form' => 'my-form']
  4287. );
  4288. $expected = [
  4289. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4290. ['label' => ['for' => 'employee-gender-male']],
  4291. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4292. 'Male',
  4293. '/label',
  4294. ['label' => ['for' => 'employee-gender-female']],
  4295. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4296. 'Female',
  4297. '/label',
  4298. ];
  4299. $this->assertHtml($expected, $result);
  4300. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4301. $expected = [
  4302. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4303. ['label' => ['for' => 'model-custom-0']],
  4304. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4305. 'option A',
  4306. '/label',
  4307. ['label' => ['for' => 'model-custom-1']],
  4308. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4309. 'option B',
  4310. '/label',
  4311. ];
  4312. $this->assertHtml($expected, $result);
  4313. $result = $this->Form->radio(
  4314. 'Employee.gender',
  4315. [
  4316. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4317. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4318. ]
  4319. );
  4320. $expected = [
  4321. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4322. ['label' => ['for' => 'employee-gender-male']],
  4323. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4324. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4325. 'Male',
  4326. '/label',
  4327. ['label' => ['for' => 'employee-gender-female']],
  4328. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4329. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4330. 'Female',
  4331. '/label',
  4332. ];
  4333. $this->assertHtml($expected, $result);
  4334. }
  4335. /**
  4336. * Test radio with complex options and empty disabled data.
  4337. *
  4338. * @return void
  4339. */
  4340. public function testRadioComplexDisabled()
  4341. {
  4342. $options = [
  4343. ['value' => 'r', 'text' => 'red'],
  4344. ['value' => 'b', 'text' => 'blue'],
  4345. ];
  4346. $attrs = ['disabled' => []];
  4347. $result = $this->Form->radio('Model.field', $options, $attrs);
  4348. $expected = [
  4349. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4350. ['label' => ['for' => 'model-field-r']],
  4351. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4352. 'red',
  4353. '/label',
  4354. ['label' => ['for' => 'model-field-b']],
  4355. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4356. 'blue',
  4357. '/label',
  4358. ];
  4359. $this->assertHtml($expected, $result);
  4360. $attrs = ['disabled' => ['r']];
  4361. $result = $this->Form->radio('Model.field', $options, $attrs);
  4362. $this->assertContains('disabled="disabled"', $result);
  4363. }
  4364. /**
  4365. * testRadioDefaultValue method
  4366. *
  4367. * Test default value setting on radio() method.
  4368. *
  4369. * @return void
  4370. */
  4371. public function testRadioDefaultValue()
  4372. {
  4373. $Articles = $this->getTableLocator()->get('Articles');
  4374. $title = $Articles->getSchema()->getColumn('title');
  4375. $Articles->getSchema()->addColumn(
  4376. 'title',
  4377. ['default' => '1'] + $title
  4378. );
  4379. $this->Form->create($Articles->newEntity());
  4380. $result = $this->Form->radio('title', ['option A', 'option B']);
  4381. $expected = [
  4382. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4383. ['label' => ['for' => 'title-0']],
  4384. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4385. 'option A',
  4386. '/label',
  4387. ['label' => ['for' => 'title-1']],
  4388. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4389. 'option B',
  4390. '/label',
  4391. ];
  4392. $this->assertHtml($expected, $result);
  4393. }
  4394. /**
  4395. * Test setting a hiddenField value on radio buttons.
  4396. *
  4397. * @return void
  4398. */
  4399. public function testRadioHiddenFieldValue()
  4400. {
  4401. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4402. $expected = [
  4403. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4404. 'label' => ['for' => 'title-0'],
  4405. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4406. 'option A',
  4407. '/label',
  4408. ];
  4409. $this->assertHtml($expected, $result);
  4410. }
  4411. /**
  4412. * testControlRadio method
  4413. *
  4414. * Test that input works with radio types.
  4415. *
  4416. * @return void
  4417. */
  4418. public function testControlRadio()
  4419. {
  4420. $result = $this->Form->control('test', [
  4421. 'type' => 'radio',
  4422. 'options' => ['A', 'B'],
  4423. ]);
  4424. $expected = [
  4425. ['div' => ['class' => 'input radio']],
  4426. '<label',
  4427. 'Test',
  4428. '/label',
  4429. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4430. ['label' => ['for' => 'test-0']],
  4431. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4432. 'A',
  4433. '/label',
  4434. ['label' => ['for' => 'test-1']],
  4435. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4436. 'B',
  4437. '/label',
  4438. '/div',
  4439. ];
  4440. $this->assertHtml($expected, $result);
  4441. $result = $this->Form->control('test', [
  4442. 'type' => 'radio',
  4443. 'options' => ['A', 'B'],
  4444. 'value' => '0'
  4445. ]);
  4446. $expected = [
  4447. ['div' => ['class' => 'input radio']],
  4448. '<label',
  4449. 'Test',
  4450. '/label',
  4451. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4452. ['label' => ['for' => 'test-0']],
  4453. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4454. 'A',
  4455. '/label',
  4456. ['label' => ['for' => 'test-1']],
  4457. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4458. 'B',
  4459. '/label',
  4460. '/div',
  4461. ];
  4462. $this->assertHtml($expected, $result);
  4463. $result = $this->Form->control('test', [
  4464. 'type' => 'radio',
  4465. 'options' => ['A', 'B'],
  4466. 'label' => false
  4467. ]);
  4468. $expected = [
  4469. ['div' => ['class' => 'input radio']],
  4470. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4471. ['label' => ['for' => 'test-0']],
  4472. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4473. 'A',
  4474. '/label',
  4475. ['label' => ['for' => 'test-1']],
  4476. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4477. 'B',
  4478. '/label',
  4479. '/div',
  4480. ];
  4481. $this->assertHtml($expected, $result);
  4482. }
  4483. /**
  4484. * testRadioNoLabel method
  4485. *
  4486. * Test that radio() works with label = false.
  4487. *
  4488. * @return void
  4489. */
  4490. public function testRadioNoLabel()
  4491. {
  4492. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4493. $expected = [
  4494. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4495. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4496. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4497. ];
  4498. $this->assertHtml($expected, $result);
  4499. }
  4500. /**
  4501. * testRadioControlInsideLabel method
  4502. *
  4503. * Test generating radio input inside label ala twitter bootstrap.
  4504. *
  4505. * @return void
  4506. */
  4507. public function testRadioControlInsideLabel()
  4508. {
  4509. $this->Form->setTemplates([
  4510. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4511. 'radioWrapper' => '{{label}}'
  4512. ]);
  4513. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4514. //@codingStandardsIgnoreStart
  4515. $expected = [
  4516. ['input' => [
  4517. 'type' => 'hidden',
  4518. 'name' => 'Model[field]',
  4519. 'value' => ''
  4520. ]],
  4521. ['label' => ['for' => 'model-field-0']],
  4522. ['input' => [
  4523. 'type' => 'radio',
  4524. 'name' => 'Model[field]',
  4525. 'value' => '0',
  4526. 'id' => 'model-field-0'
  4527. ]],
  4528. 'option A',
  4529. '/label',
  4530. ['label' => ['for' => 'model-field-1']],
  4531. ['input' => [
  4532. 'type' => 'radio',
  4533. 'name' => 'Model[field]',
  4534. 'value' => '1',
  4535. 'id' => 'model-field-1'
  4536. ]],
  4537. 'option B',
  4538. '/label',
  4539. ];
  4540. //@codingStandardsIgnoreEnd
  4541. $this->assertHtml($expected, $result);
  4542. }
  4543. /**
  4544. * testRadioHiddenControlDisabling method
  4545. *
  4546. * Test disabling the hidden input for radio buttons.
  4547. *
  4548. * @return void
  4549. */
  4550. public function testRadioHiddenControlDisabling()
  4551. {
  4552. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4553. $expected = [
  4554. 'label' => ['for' => 'model-1-field-0'],
  4555. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4556. 'option A',
  4557. '/label'
  4558. ];
  4559. $this->assertHtml($expected, $result);
  4560. }
  4561. /**
  4562. * testRadioOutOfRange method
  4563. *
  4564. * Test radio element set generation.
  4565. *
  4566. * @return void
  4567. */
  4568. public function testRadioOutOfRange()
  4569. {
  4570. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4571. $expected = [
  4572. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4573. 'label' => ['for' => 'model-field-v'],
  4574. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4575. 'value',
  4576. '/label'
  4577. ];
  4578. $this->assertHtml($expected, $result);
  4579. }
  4580. /**
  4581. * testSelect method
  4582. *
  4583. * Test select element generation.
  4584. *
  4585. * @return void
  4586. */
  4587. public function testSelect()
  4588. {
  4589. $result = $this->Form->select('Model.field', []);
  4590. $expected = [
  4591. 'select' => ['name' => 'Model[field]'],
  4592. '/select'
  4593. ];
  4594. $this->assertHtml($expected, $result);
  4595. $this->Form->request = $this->Form->request->withData('Model', ['field' => 'value']);
  4596. $this->Form->create();
  4597. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4598. $expected = [
  4599. 'select' => ['name' => 'Model[field]'],
  4600. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4601. 'good',
  4602. '/option',
  4603. ['option' => ['value' => 'other']],
  4604. 'bad',
  4605. '/option',
  4606. '/select'
  4607. ];
  4608. $this->assertHtml($expected, $result);
  4609. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4610. $this->assertHtml($expected, $result);
  4611. $this->Form->request = $this->Form->request->withParsedBody([]);
  4612. $this->Form->create();
  4613. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4614. $expected = [
  4615. 'select' => ['name' => 'Model[field]'],
  4616. ['option' => ['value' => 'value']],
  4617. 'good',
  4618. '/option',
  4619. ['option' => ['value' => 'other']],
  4620. 'bad',
  4621. '/option',
  4622. '/select'
  4623. ];
  4624. $this->assertHtml($expected, $result);
  4625. $options = [
  4626. ['value' => 'first', 'text' => 'First'],
  4627. ['value' => 'first', 'text' => 'Another First'],
  4628. ];
  4629. $result = $this->Form->select(
  4630. 'Model.field',
  4631. $options,
  4632. ['escape' => false, 'empty' => false]
  4633. );
  4634. $expected = [
  4635. 'select' => ['name' => 'Model[field]'],
  4636. ['option' => ['value' => 'first']],
  4637. 'First',
  4638. '/option',
  4639. ['option' => ['value' => 'first']],
  4640. 'Another First',
  4641. '/option',
  4642. '/select'
  4643. ];
  4644. $this->assertHtml($expected, $result);
  4645. $this->Form->request = $this->Form->request->withParsedBody(['Model' => ['contact_id' => 228]]);
  4646. $this->Form->create();
  4647. $result = $this->Form->select(
  4648. 'Model.contact_id',
  4649. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4650. ['escape' => false, 'empty' => 'pick something']
  4651. );
  4652. $expected = [
  4653. 'select' => ['name' => 'Model[contact_id]'],
  4654. ['option' => ['value' => '']], 'pick something', '/option',
  4655. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4656. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4657. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4658. '/select'
  4659. ];
  4660. $this->assertHtml($expected, $result);
  4661. $this->Form->request = $this->Form->request->withData('Model.field', 0);
  4662. $this->Form->create();
  4663. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4664. $expected = [
  4665. 'select' => ['name' => 'Model[field]'],
  4666. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4667. ['option' => ['value' => '1']], 'Yes', '/option',
  4668. '/select'
  4669. ];
  4670. $this->assertHtml($expected, $result);
  4671. }
  4672. /**
  4673. * testSelectEscapeHtml method
  4674. *
  4675. * Test that select() escapes HTML.
  4676. *
  4677. * @return void
  4678. */
  4679. public function testSelectEscapeHtml()
  4680. {
  4681. $result = $this->Form->select(
  4682. 'Model.field',
  4683. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4684. ['empty' => false]
  4685. );
  4686. $expected = [
  4687. 'select' => ['name' => 'Model[field]'],
  4688. ['option' => ['value' => 'first']],
  4689. 'first &quot;html&quot; &lt;chars&gt;',
  4690. '/option',
  4691. ['option' => ['value' => 'second']],
  4692. 'value',
  4693. '/option',
  4694. '/select'
  4695. ];
  4696. $this->assertHtml($expected, $result);
  4697. $result = $this->Form->select(
  4698. 'Model.field',
  4699. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4700. ['escape' => false, 'empty' => false]
  4701. );
  4702. $expected = [
  4703. 'select' => ['name' => 'Model[field]'],
  4704. ['option' => ['value' => 'first']],
  4705. 'first "html" <chars>',
  4706. '/option',
  4707. ['option' => ['value' => 'second']],
  4708. 'value',
  4709. '/option',
  4710. '/select'
  4711. ];
  4712. $this->assertHtml($expected, $result);
  4713. }
  4714. /**
  4715. * testSelectRequired method
  4716. *
  4717. * Test select() with required and disabled attributes.
  4718. *
  4719. * @return void
  4720. */
  4721. public function testSelectRequired()
  4722. {
  4723. $this->article['required'] = [
  4724. 'user_id' => true
  4725. ];
  4726. $this->Form->create($this->article);
  4727. $result = $this->Form->select('user_id', ['option A']);
  4728. $expected = [
  4729. 'select' => [
  4730. 'name' => 'user_id',
  4731. 'required' => 'required'
  4732. ],
  4733. ['option' => ['value' => '0']], 'option A', '/option',
  4734. '/select'
  4735. ];
  4736. $this->assertHtml($expected, $result);
  4737. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4738. $expected = [
  4739. 'select' => [
  4740. 'name' => 'user_id',
  4741. 'disabled' => 'disabled'
  4742. ],
  4743. ['option' => ['value' => '0']], 'option A', '/option',
  4744. '/select'
  4745. ];
  4746. $this->assertHtml($expected, $result);
  4747. }
  4748. /**
  4749. * testNestedSelect method
  4750. *
  4751. * Test select element generation with optgroups.
  4752. *
  4753. * @return void
  4754. */
  4755. public function testNestedSelect()
  4756. {
  4757. $result = $this->Form->select(
  4758. 'Model.field',
  4759. [1 => 'One', 2 => 'Two', 'Three' => [
  4760. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4761. ]],
  4762. ['empty' => false]
  4763. );
  4764. $expected = [
  4765. 'select' => ['name' => 'Model[field]'],
  4766. ['option' => ['value' => 1]],
  4767. 'One',
  4768. '/option',
  4769. ['option' => ['value' => 2]],
  4770. 'Two',
  4771. '/option',
  4772. ['optgroup' => ['label' => 'Three']],
  4773. ['option' => ['value' => 3]],
  4774. 'Three',
  4775. '/option',
  4776. ['option' => ['value' => 4]],
  4777. 'Four',
  4778. '/option',
  4779. ['option' => ['value' => 5]],
  4780. 'Five',
  4781. '/option',
  4782. '/optgroup',
  4783. '/select'
  4784. ];
  4785. $this->assertHtml($expected, $result);
  4786. }
  4787. /**
  4788. * testSelectMultiple method
  4789. *
  4790. * Test generation of multiple select elements.
  4791. *
  4792. * @return void
  4793. */
  4794. public function testSelectMultiple()
  4795. {
  4796. $options = ['first', 'second', 'third'];
  4797. $result = $this->Form->select(
  4798. 'Model.multi_field',
  4799. $options,
  4800. ['form' => 'my-form', 'multiple' => true]
  4801. );
  4802. $expected = [
  4803. 'input' => [
  4804. 'type' => 'hidden',
  4805. 'name' => 'Model[multi_field]',
  4806. 'value' => '',
  4807. 'form' => 'my-form',
  4808. ],
  4809. 'select' => [
  4810. 'name' => 'Model[multi_field][]',
  4811. 'multiple' => 'multiple',
  4812. 'form' => 'my-form',
  4813. ],
  4814. ['option' => ['value' => '0']],
  4815. 'first',
  4816. '/option',
  4817. ['option' => ['value' => '1']],
  4818. 'second',
  4819. '/option',
  4820. ['option' => ['value' => '2']],
  4821. 'third',
  4822. '/option',
  4823. '/select'
  4824. ];
  4825. $this->assertHtml($expected, $result);
  4826. $result = $this->Form->select(
  4827. 'Model.multi_field',
  4828. $options,
  4829. ['multiple' => 'multiple', 'form' => 'my-form']
  4830. );
  4831. $this->assertHtml($expected, $result);
  4832. $result = $this->Form->select(
  4833. 'Model.multi_field',
  4834. $options,
  4835. ['form' => 'my-form', 'multiple' => false]
  4836. );
  4837. $this->assertNotContains('multiple', $result);
  4838. }
  4839. /**
  4840. * testCheckboxZeroValue method
  4841. *
  4842. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4843. *
  4844. * @return void
  4845. */
  4846. public function testCheckboxZeroValue()
  4847. {
  4848. $result = $this->Form->control('User.get_spam', [
  4849. 'type' => 'checkbox',
  4850. 'value' => '0',
  4851. 'hiddenField' => '1',
  4852. ]);
  4853. $expected = [
  4854. 'div' => ['class' => 'input checkbox'],
  4855. 'label' => ['for' => 'user-get-spam'],
  4856. ['input' => [
  4857. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4858. 'value' => '1'
  4859. ]],
  4860. ['input' => [
  4861. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4862. 'value' => '0', 'id' => 'user-get-spam'
  4863. ]],
  4864. 'Get Spam',
  4865. '/label',
  4866. '/div'
  4867. ];
  4868. $this->assertHtml($expected, $result);
  4869. }
  4870. /**
  4871. * testHabtmSelectBox method
  4872. *
  4873. * Test generation of habtm select boxes.
  4874. *
  4875. * @return void
  4876. */
  4877. public function testHabtmSelectBox()
  4878. {
  4879. $this->loadFixtures('Articles');
  4880. $options = [
  4881. 1 => 'blue',
  4882. 2 => 'red',
  4883. 3 => 'green'
  4884. ];
  4885. $tags = [
  4886. new Entity(['id' => 1, 'name' => 'blue']),
  4887. new Entity(['id' => 3, 'name' => 'green'])
  4888. ];
  4889. $article = new Article(['tags' => $tags]);
  4890. $this->Form->create($article);
  4891. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4892. $expected = [
  4893. 'div' => ['class' => 'input select'],
  4894. 'label' => ['for' => 'tags-ids'],
  4895. 'Tags',
  4896. '/label',
  4897. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4898. 'select' => [
  4899. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4900. 'multiple' => 'multiple'
  4901. ],
  4902. ['option' => ['value' => '1', 'selected' => 'selected']],
  4903. 'blue',
  4904. '/option',
  4905. ['option' => ['value' => '2']],
  4906. 'red',
  4907. '/option',
  4908. ['option' => ['value' => '3', 'selected' => 'selected']],
  4909. 'green',
  4910. '/option',
  4911. '/select',
  4912. '/div'
  4913. ];
  4914. $this->assertHtml($expected, $result);
  4915. // make sure only 50 is selected, and not 50f5c0cf
  4916. $options = [
  4917. '1' => 'blue',
  4918. '50f5c0cf' => 'red',
  4919. '50' => 'green'
  4920. ];
  4921. $tags = [
  4922. new Entity(['id' => 1, 'name' => 'blue']),
  4923. new Entity(['id' => 50, 'name' => 'green'])
  4924. ];
  4925. $article = new Article(['tags' => $tags]);
  4926. $this->Form->create($article);
  4927. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4928. $expected = [
  4929. 'div' => ['class' => 'input select'],
  4930. 'label' => ['for' => 'tags-ids'],
  4931. 'Tags',
  4932. '/label',
  4933. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4934. 'select' => [
  4935. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4936. 'multiple' => 'multiple'
  4937. ],
  4938. ['option' => ['value' => '1', 'selected' => 'selected']],
  4939. 'blue',
  4940. '/option',
  4941. ['option' => ['value' => '50f5c0cf']],
  4942. 'red',
  4943. '/option',
  4944. ['option' => ['value' => '50', 'selected' => 'selected']],
  4945. 'green',
  4946. '/option',
  4947. '/select',
  4948. '/div'
  4949. ];
  4950. $this->assertHtml($expected, $result);
  4951. $spacecraft = [
  4952. 1 => 'Orion',
  4953. 2 => 'Helios'
  4954. ];
  4955. $this->View->viewVars['spacecraft'] = $spacecraft;
  4956. $this->Form->create();
  4957. $result = $this->Form->control('spacecraft._ids');
  4958. $expected = [
  4959. 'div' => ['class' => 'input select'],
  4960. 'label' => ['for' => 'spacecraft-ids'],
  4961. 'Spacecraft',
  4962. '/label',
  4963. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4964. 'select' => [
  4965. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4966. 'multiple' => 'multiple'
  4967. ],
  4968. ['option' => ['value' => '1']],
  4969. 'Orion',
  4970. '/option',
  4971. ['option' => ['value' => '2']],
  4972. 'Helios',
  4973. '/option',
  4974. '/select',
  4975. '/div'
  4976. ];
  4977. $this->assertHtml($expected, $result);
  4978. }
  4979. /**
  4980. * testErrorsForBelongsToManySelect method
  4981. *
  4982. * Tests that errors for belongsToMany select fields are being
  4983. * picked up properly.
  4984. *
  4985. * @return void
  4986. */
  4987. public function testErrorsForBelongsToManySelect()
  4988. {
  4989. $spacecraft = [
  4990. 1 => 'Orion',
  4991. 2 => 'Helios'
  4992. ];
  4993. $this->View->viewVars['spacecraft'] = $spacecraft;
  4994. $article = new Article();
  4995. $article->setError('spacecraft', ['Invalid']);
  4996. $this->Form->create($article);
  4997. $result = $this->Form->control('spacecraft._ids');
  4998. $expected = [
  4999. ['div' => ['class' => 'input select error']],
  5000. 'label' => ['for' => 'spacecraft-ids'],
  5001. 'Spacecraft',
  5002. '/label',
  5003. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  5004. 'select' => [
  5005. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  5006. 'multiple' => 'multiple'
  5007. ],
  5008. ['option' => ['value' => '1']],
  5009. 'Orion',
  5010. '/option',
  5011. ['option' => ['value' => '2']],
  5012. 'Helios',
  5013. '/option',
  5014. '/select',
  5015. ['div' => ['class' => 'error-message']],
  5016. 'Invalid',
  5017. '/div',
  5018. '/div'
  5019. ];
  5020. $this->assertHtml($expected, $result);
  5021. }
  5022. /**
  5023. * testSelectMultipleCheckboxes method
  5024. *
  5025. * Test generation of multi select elements in checkbox format.
  5026. *
  5027. * @return void
  5028. */
  5029. public function testSelectMultipleCheckboxes()
  5030. {
  5031. $result = $this->Form->select(
  5032. 'Model.multi_field',
  5033. ['first', 'second', 'third'],
  5034. ['multiple' => 'checkbox']
  5035. );
  5036. $expected = [
  5037. 'input' => [
  5038. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5039. ],
  5040. ['div' => ['class' => 'checkbox']],
  5041. ['label' => ['for' => 'model-multi-field-0']],
  5042. ['input' => [
  5043. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5044. 'value' => '0', 'id' => 'model-multi-field-0'
  5045. ]],
  5046. 'first',
  5047. '/label',
  5048. '/div',
  5049. ['div' => ['class' => 'checkbox']],
  5050. ['label' => ['for' => 'model-multi-field-1']],
  5051. ['input' => [
  5052. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5053. 'value' => '1', 'id' => 'model-multi-field-1'
  5054. ]],
  5055. 'second',
  5056. '/label',
  5057. '/div',
  5058. ['div' => ['class' => 'checkbox']],
  5059. ['label' => ['for' => 'model-multi-field-2']],
  5060. ['input' => [
  5061. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5062. 'value' => '2', 'id' => 'model-multi-field-2'
  5063. ]],
  5064. 'third',
  5065. '/label',
  5066. '/div'
  5067. ];
  5068. $this->assertHtml($expected, $result);
  5069. $result = $this->Form->select(
  5070. 'Model.multi_field',
  5071. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5072. ['multiple' => 'checkbox']
  5073. );
  5074. $expected = [
  5075. 'input' => [
  5076. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5077. ],
  5078. ['div' => ['class' => 'checkbox']],
  5079. ['label' => ['for' => 'model-multi-field-a+']],
  5080. ['input' => [
  5081. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5082. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5083. ]],
  5084. 'first',
  5085. '/label',
  5086. '/div',
  5087. ['div' => ['class' => 'checkbox']],
  5088. ['label' => ['for' => 'model-multi-field-a++']],
  5089. ['input' => [
  5090. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5091. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5092. ]],
  5093. 'second',
  5094. '/label',
  5095. '/div',
  5096. ['div' => ['class' => 'checkbox']],
  5097. ['label' => ['for' => 'model-multi-field-a+++']],
  5098. ['input' => [
  5099. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5100. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5101. ]],
  5102. 'third',
  5103. '/label',
  5104. '/div'
  5105. ];
  5106. $this->assertHtml($expected, $result);
  5107. $result = $this->Form->select(
  5108. 'Model.multi_field',
  5109. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5110. ['multiple' => 'checkbox']
  5111. );
  5112. $expected = [
  5113. 'input' => [
  5114. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5115. ],
  5116. ['div' => ['class' => 'checkbox']],
  5117. ['label' => ['for' => 'model-multi-field-a-b']],
  5118. ['input' => [
  5119. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5120. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5121. ]],
  5122. 'first',
  5123. '/label',
  5124. '/div',
  5125. ['div' => ['class' => 'checkbox']],
  5126. ['label' => ['for' => 'model-multi-field-a-b1']],
  5127. ['input' => [
  5128. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5129. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5130. ]],
  5131. 'second',
  5132. '/label',
  5133. '/div',
  5134. ['div' => ['class' => 'checkbox']],
  5135. ['label' => ['for' => 'model-multi-field-a-b2']],
  5136. ['input' => [
  5137. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5138. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5139. ]],
  5140. 'third',
  5141. '/label',
  5142. '/div'
  5143. ];
  5144. $this->assertHtml($expected, $result);
  5145. }
  5146. /**
  5147. * testSelectMultipleCheckboxRequestData method
  5148. *
  5149. * Ensure that multiCheckbox reads from the request data.
  5150. *
  5151. * @return void
  5152. */
  5153. public function testSelectMultipleCheckboxRequestData()
  5154. {
  5155. $this->Form->request = $this->Form->request->withData('Model', ['tags' => [1]]);
  5156. $result = $this->Form->select(
  5157. 'Model.tags',
  5158. ['1' => 'first', 'Array' => 'Array'],
  5159. ['multiple' => 'checkbox']
  5160. );
  5161. $expected = [
  5162. 'input' => [
  5163. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5164. ],
  5165. ['div' => ['class' => 'checkbox']],
  5166. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5167. ['input' => [
  5168. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5169. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5170. ]],
  5171. 'first',
  5172. '/label',
  5173. '/div',
  5174. ['div' => ['class' => 'checkbox']],
  5175. ['label' => ['for' => 'model-tags-array']],
  5176. ['input' => [
  5177. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5178. 'value' => 'Array', 'id' => 'model-tags-array'
  5179. ]],
  5180. 'Array',
  5181. '/label',
  5182. '/div'
  5183. ];
  5184. $this->assertHtml($expected, $result);
  5185. }
  5186. /**
  5187. * testSelectMultipleCheckboxSecurity method
  5188. *
  5189. * Checks the security hash array generated for multiple-input checkbox elements.
  5190. *
  5191. * @return void
  5192. */
  5193. public function testSelectMultipleCheckboxSecurity()
  5194. {
  5195. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  5196. $this->assertEquals([], $this->Form->fields);
  5197. $this->Form->select(
  5198. 'Model.multi_field',
  5199. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5200. ['multiple' => 'checkbox']
  5201. );
  5202. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5203. $result = $this->Form->secure($this->Form->fields);
  5204. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5205. $this->assertContains('"' . $key . '"', $result);
  5206. }
  5207. /**
  5208. * testSelectMultipleSecureWithNoOptions method
  5209. *
  5210. * Multiple select elements should always be secured as they always participate
  5211. * in the POST data.
  5212. *
  5213. * @return void
  5214. */
  5215. public function testSelectMultipleSecureWithNoOptions()
  5216. {
  5217. $this->assertEquals([], $this->Form->fields);
  5218. $this->Form->select(
  5219. 'Model.select',
  5220. [],
  5221. ['multiple' => true]
  5222. );
  5223. $this->assertEquals(['Model.select'], $this->Form->fields);
  5224. }
  5225. /**
  5226. * testSelectNoSecureWithNoOptions method
  5227. *
  5228. * When a select box has no options it should not be added to the fields list
  5229. * as it always fail post validation.
  5230. *
  5231. * @return void
  5232. */
  5233. public function testSelectNoSecureWithNoOptions()
  5234. {
  5235. $this->Form->request = $this->Form->request->withParam('_Token', 'testkey');
  5236. $this->assertEquals([], $this->Form->fields);
  5237. $this->Form->select(
  5238. 'Model.select',
  5239. []
  5240. );
  5241. $this->assertEquals([], $this->Form->fields);
  5242. $this->Form->select(
  5243. 'Model.user_id',
  5244. [],
  5245. ['empty' => true]
  5246. );
  5247. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5248. }
  5249. /**
  5250. * testControlMultipleCheckboxes method
  5251. *
  5252. * Test control() resulting in multi select elements being generated.
  5253. *
  5254. * @return void
  5255. */
  5256. public function testControlMultipleCheckboxes()
  5257. {
  5258. $result = $this->Form->control('Model.multi_field', [
  5259. 'options' => ['first', 'second', 'third'],
  5260. 'multiple' => 'checkbox'
  5261. ]);
  5262. $expected = [
  5263. ['div' => ['class' => 'input select']],
  5264. ['label' => ['for' => 'model-multi-field']],
  5265. 'Multi Field',
  5266. '/label',
  5267. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5268. ['div' => ['class' => 'checkbox']],
  5269. ['label' => ['for' => 'model-multi-field-0']],
  5270. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5271. 'first',
  5272. '/label',
  5273. '/div',
  5274. ['div' => ['class' => 'checkbox']],
  5275. ['label' => ['for' => 'model-multi-field-1']],
  5276. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5277. 'second',
  5278. '/label',
  5279. '/div',
  5280. ['div' => ['class' => 'checkbox']],
  5281. ['label' => ['for' => 'model-multi-field-2']],
  5282. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5283. 'third',
  5284. '/label',
  5285. '/div',
  5286. '/div'
  5287. ];
  5288. $this->assertHtml($expected, $result);
  5289. $result = $this->Form->control('Model.multi_field', [
  5290. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5291. 'multiple' => 'checkbox'
  5292. ]);
  5293. $expected = [
  5294. ['div' => ['class' => 'input select']],
  5295. ['label' => ['for' => 'model-multi-field']],
  5296. 'Multi Field',
  5297. '/label',
  5298. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5299. ['div' => ['class' => 'checkbox']],
  5300. ['label' => ['for' => 'model-multi-field-a']],
  5301. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5302. 'first',
  5303. '/label',
  5304. '/div',
  5305. ['div' => ['class' => 'checkbox']],
  5306. ['label' => ['for' => 'model-multi-field-b']],
  5307. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5308. 'second',
  5309. '/label',
  5310. '/div',
  5311. ['div' => ['class' => 'checkbox']],
  5312. ['label' => ['for' => 'model-multi-field-c']],
  5313. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5314. 'third',
  5315. '/label',
  5316. '/div',
  5317. '/div'
  5318. ];
  5319. $this->assertHtml($expected, $result);
  5320. }
  5321. /**
  5322. * testSelectHiddenFieldOmission method
  5323. *
  5324. * Test that select() with 'hiddenField' => false omits the hidden field.
  5325. *
  5326. * @return void
  5327. */
  5328. public function testSelectHiddenFieldOmission()
  5329. {
  5330. $result = $this->Form->select(
  5331. 'Model.multi_field',
  5332. ['first', 'second'],
  5333. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5334. );
  5335. $this->assertNotContains('type="hidden"', $result);
  5336. }
  5337. /**
  5338. * testSelectCheckboxMultipleOverrideName method
  5339. *
  5340. * Test that select() with multiple = checkbox works with overriding name attribute.
  5341. *
  5342. * @return void
  5343. */
  5344. public function testSelectCheckboxMultipleOverrideName()
  5345. {
  5346. $result = $this->Form->select('category', ['1', '2'], [
  5347. 'multiple' => 'checkbox',
  5348. 'name' => 'fish',
  5349. ]);
  5350. $expected = [
  5351. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5352. ['div' => ['class' => 'checkbox']],
  5353. ['label' => ['for' => 'fish-0']],
  5354. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5355. '1',
  5356. '/label',
  5357. '/div',
  5358. ['div' => ['class' => 'checkbox']],
  5359. ['label' => ['for' => 'fish-1']],
  5360. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5361. '2',
  5362. '/label',
  5363. '/div'
  5364. ];
  5365. $this->assertHtml($expected, $result);
  5366. $result = $this->Form->multiCheckbox(
  5367. 'category',
  5368. new Collection(['1', '2']),
  5369. ['name' => 'fish']
  5370. );
  5371. $this->assertHtml($expected, $result);
  5372. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5373. 'name' => 'fish',
  5374. ]);
  5375. $this->assertHtml($expected, $result);
  5376. }
  5377. /**
  5378. * testControlMultiCheckbox method
  5379. *
  5380. * Test that control() works with multicheckbox.
  5381. *
  5382. * @return void
  5383. */
  5384. public function testControlMultiCheckbox()
  5385. {
  5386. $result = $this->Form->control('category', [
  5387. 'type' => 'multicheckbox',
  5388. 'options' => ['1', '2'],
  5389. ]);
  5390. $expected = [
  5391. ['div' => ['class' => 'input multicheckbox']],
  5392. '<label',
  5393. 'Category',
  5394. '/label',
  5395. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5396. ['div' => ['class' => 'checkbox']],
  5397. ['label' => ['for' => 'category-0']],
  5398. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5399. '1',
  5400. '/label',
  5401. '/div',
  5402. ['div' => ['class' => 'checkbox']],
  5403. ['label' => ['for' => 'category-1']],
  5404. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5405. '2',
  5406. '/label',
  5407. '/div',
  5408. '/div',
  5409. ];
  5410. $this->assertHtml($expected, $result);
  5411. }
  5412. /**
  5413. * testCheckbox method
  5414. *
  5415. * Test generation of checkboxes.
  5416. *
  5417. * @return void
  5418. */
  5419. public function testCheckbox()
  5420. {
  5421. $result = $this->Form->checkbox('Model.field');
  5422. $expected = [
  5423. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5424. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5425. ];
  5426. $this->assertHtml($expected, $result);
  5427. $result = $this->Form->checkbox('Model.field', [
  5428. 'id' => 'theID',
  5429. 'value' => 'myvalue',
  5430. 'form' => 'my-form',
  5431. ]);
  5432. $expected = [
  5433. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5434. ['input' => [
  5435. 'type' => 'checkbox', 'name' => 'Model[field]',
  5436. 'value' => 'myvalue', 'id' => 'theID',
  5437. 'form' => 'my-form',
  5438. ]]
  5439. ];
  5440. $this->assertHtml($expected, $result);
  5441. }
  5442. /**
  5443. * testCheckboxDefaultValue method
  5444. *
  5445. * Test default value setting on checkbox() method.
  5446. *
  5447. * @return void
  5448. */
  5449. public function testCheckboxDefaultValue()
  5450. {
  5451. $this->Form->request = $this->Form->request->withData('Model.field', false);
  5452. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5453. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5454. $this->assertHtml($expected, $result);
  5455. $this->Form->request = $this->Form->request->withData('Model.field', null);
  5456. $this->Form->create();
  5457. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5458. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5459. $this->assertHtml($expected, $result);
  5460. $this->Form->request = $this->Form->request->withData('Model.field', true);
  5461. $this->Form->create();
  5462. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5463. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5464. $this->assertHtml($expected, $result);
  5465. $this->Form->request = $this->Form->request->withData('Model.field', null);
  5466. $this->Form->create();
  5467. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5468. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5469. $this->assertHtml($expected, $result);
  5470. $Articles = $this->getTableLocator()->get('Articles');
  5471. $Articles->getSchema()->addColumn(
  5472. 'published',
  5473. ['type' => 'boolean', 'null' => false, 'default' => true]
  5474. );
  5475. $this->Form->create($Articles->newEntity());
  5476. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5477. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5478. $this->assertHtml($expected, $result);
  5479. }
  5480. /**
  5481. * testCheckboxCheckedAndError method
  5482. *
  5483. * Test checkbox being checked or having errors.
  5484. *
  5485. * @return void
  5486. */
  5487. public function testCheckboxCheckedAndError()
  5488. {
  5489. $this->article['errors'] = [
  5490. 'published' => true
  5491. ];
  5492. $this->Form->request = $this->Form->request->withData('published', 'myvalue');
  5493. $this->Form->create($this->article);
  5494. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5495. $expected = [
  5496. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5497. ['input' => [
  5498. 'type' => 'checkbox',
  5499. 'name' => 'published',
  5500. 'value' => 'myvalue',
  5501. 'id' => 'theID',
  5502. 'checked' => 'checked',
  5503. 'class' => 'form-error'
  5504. ]]
  5505. ];
  5506. $this->assertHtml($expected, $result);
  5507. $this->Form->request = $this->Form->request->withData('published', '');
  5508. $this->Form->create($this->article);
  5509. $result = $this->Form->checkbox('published');
  5510. $expected = [
  5511. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5512. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5513. ];
  5514. $this->assertHtml($expected, $result);
  5515. }
  5516. /**
  5517. * testCheckboxCustomNameAttribute method
  5518. *
  5519. * Test checkbox() with a custom name attribute.
  5520. *
  5521. * @return void
  5522. */
  5523. public function testCheckboxCustomNameAttribute()
  5524. {
  5525. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5526. $expected = [
  5527. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5528. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5529. ];
  5530. $this->assertHtml($expected, $result);
  5531. }
  5532. /**
  5533. * testCheckboxHiddenField method
  5534. *
  5535. * Test that the hidden input for checkboxes can be omitted or set to a
  5536. * specific value.
  5537. *
  5538. * @return void
  5539. */
  5540. public function testCheckboxHiddenField()
  5541. {
  5542. $result = $this->Form->checkbox('UserForm.something', [
  5543. 'hiddenField' => false
  5544. ]);
  5545. $expected = [
  5546. 'input' => [
  5547. 'type' => 'checkbox',
  5548. 'name' => 'UserForm[something]',
  5549. 'value' => '1'
  5550. ],
  5551. ];
  5552. $this->assertHtml($expected, $result);
  5553. $result = $this->Form->checkbox('UserForm.something', [
  5554. 'value' => 'Y',
  5555. 'hiddenField' => 'N',
  5556. ]);
  5557. $expected = [
  5558. ['input' => [
  5559. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5560. 'value' => 'N'
  5561. ]],
  5562. ['input' => [
  5563. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5564. 'value' => 'Y'
  5565. ]],
  5566. ];
  5567. $this->assertHtml($expected, $result);
  5568. }
  5569. /**
  5570. * testTime method
  5571. *
  5572. * Test the time type.
  5573. *
  5574. * @return void
  5575. */
  5576. public function testTime()
  5577. {
  5578. $result = $this->Form->time('start_time', [
  5579. 'timeFormat' => 12,
  5580. 'interval' => 5,
  5581. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5582. ]);
  5583. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5584. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5585. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5586. $this->assertNotContains('year', $result);
  5587. $this->assertNotContains('month', $result);
  5588. $this->assertNotContains('day', $result);
  5589. $result = $this->Form->time('start_time', [
  5590. 'timeFormat' => 12,
  5591. 'interval' => 5,
  5592. 'value' => '2014-03-08 16:30:00'
  5593. ]);
  5594. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5595. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5596. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5597. $this->assertNotContains('year', $result);
  5598. $this->assertNotContains('month', $result);
  5599. $this->assertNotContains('day', $result);
  5600. }
  5601. /**
  5602. * testTimeFormat24NoMeridian method
  5603. *
  5604. * Ensure that timeFormat=24 has no merdian.
  5605. *
  5606. * @return void.
  5607. */
  5608. public function testTimeFormat24NoMeridian()
  5609. {
  5610. $result = $this->Form->time('start_time', [
  5611. 'timeFormat' => 24,
  5612. 'interval' => 5,
  5613. 'value' => '2014-03-08 16:30:00'
  5614. ]);
  5615. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5616. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5617. $this->assertNotContains('meridian', $result);
  5618. $this->assertNotContains('pm', $result);
  5619. $this->assertNotContains('year', $result);
  5620. $this->assertNotContains('month', $result);
  5621. $this->assertNotContains('day', $result);
  5622. }
  5623. /**
  5624. * testDate method
  5625. *
  5626. * Test the date type.
  5627. *
  5628. * @return void
  5629. */
  5630. public function testDate()
  5631. {
  5632. $result = $this->Form->date('start_day', [
  5633. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5634. ]);
  5635. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5636. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5637. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5638. $this->assertNotContains('hour', $result);
  5639. $this->assertNotContains('minute', $result);
  5640. $this->assertNotContains('second', $result);
  5641. $this->assertNotContains('meridian', $result);
  5642. }
  5643. /**
  5644. * testDateTime method
  5645. *
  5646. * Test generation of date/time select elements.
  5647. *
  5648. * @return void
  5649. */
  5650. public function testDateTime()
  5651. {
  5652. extract($this->dateRegex);
  5653. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5654. $now = strtotime('now');
  5655. $expected = [
  5656. ['select' => ['name' => 'Contact[date][year]']],
  5657. ['option' => ['value' => '']],
  5658. '/option',
  5659. $yearsRegex,
  5660. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5661. date('Y', $now),
  5662. '/option',
  5663. '*/select',
  5664. ['select' => ['name' => 'Contact[date][month]']],
  5665. ['option' => ['value' => '']],
  5666. '/option',
  5667. $monthsRegex,
  5668. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5669. date('F', $now),
  5670. '/option',
  5671. '*/select',
  5672. ['select' => ['name' => 'Contact[date][day]']],
  5673. ['option' => ['value' => '']],
  5674. '/option',
  5675. $daysRegex,
  5676. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5677. date('j', $now),
  5678. '/option',
  5679. '*/select',
  5680. ['select' => ['name' => 'Contact[date][hour]']],
  5681. ['option' => ['value' => '']],
  5682. '/option',
  5683. $hoursRegex,
  5684. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5685. date('G', $now),
  5686. '/option',
  5687. '*/select',
  5688. ['select' => ['name' => 'Contact[date][minute]']],
  5689. ['option' => ['value' => '']],
  5690. '/option',
  5691. $minutesRegex,
  5692. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5693. date('i', $now),
  5694. '/option',
  5695. '*/select',
  5696. ];
  5697. $this->assertHtml($expected, $result);
  5698. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5699. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5700. $now = strtotime('now');
  5701. $expected = [
  5702. ['select' => ['name' => 'Contact[date][year]']],
  5703. $yearsRegex,
  5704. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5705. date('Y', $now),
  5706. '/option',
  5707. '*/select',
  5708. ['select' => ['name' => 'Contact[date][month]']],
  5709. $monthsRegex,
  5710. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5711. date('F', $now),
  5712. '/option',
  5713. '*/select',
  5714. ['select' => ['name' => 'Contact[date][day]']],
  5715. $daysRegex,
  5716. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5717. date('j', $now),
  5718. '/option',
  5719. '*/select',
  5720. ['select' => ['name' => 'Contact[date][hour]']],
  5721. $hoursRegex,
  5722. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5723. date('G', $now),
  5724. '/option',
  5725. '*/select',
  5726. ['select' => ['name' => 'Contact[date][minute]']],
  5727. $minutesRegex,
  5728. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5729. date('i', $now),
  5730. '/option',
  5731. '*/select',
  5732. ];
  5733. $this->assertHtml($expected, $result);
  5734. }
  5735. /**
  5736. * testDateTimeSecured method
  5737. *
  5738. * Test that datetime fields are added to protected fields list.
  5739. *
  5740. * @return void
  5741. */
  5742. public function testDateTimeSecured()
  5743. {
  5744. $this->Form->request = $this->Form->request->withParam('_Token', ['unlockedFields' => []]);
  5745. $this->Form->dateTime('Contact.date');
  5746. $expected = [
  5747. 'Contact.date.year',
  5748. 'Contact.date.month',
  5749. 'Contact.date.day',
  5750. 'Contact.date.hour',
  5751. 'Contact.date.minute',
  5752. ];
  5753. $this->assertEquals($expected, $this->Form->fields);
  5754. $this->Form->fields = [];
  5755. $this->Form->date('Contact.published');
  5756. $expected = [
  5757. 'Contact.published.year',
  5758. 'Contact.published.month',
  5759. 'Contact.published.day',
  5760. ];
  5761. $this->assertEquals($expected, $this->Form->fields);
  5762. }
  5763. /**
  5764. * testDateTimeSecuredDisabled method
  5765. *
  5766. * Test that datetime fields are added to protected fields list.
  5767. *
  5768. * @return void
  5769. */
  5770. public function testDateTimeSecuredDisabled()
  5771. {
  5772. $this->Form->request = $this->Form->request->withParam('_Token', ['unlockedFields' => []]);
  5773. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5774. $expected = [];
  5775. $this->assertEquals($expected, $this->Form->fields);
  5776. $this->Form->fields = [];
  5777. $this->Form->date('Contact.published', ['secure' => false]);
  5778. $expected = [];
  5779. $this->assertEquals($expected, $this->Form->fields);
  5780. }
  5781. /**
  5782. * testDatetimeEmpty method
  5783. *
  5784. * Test empty defaulting to true for datetime.
  5785. *
  5786. * @return void
  5787. */
  5788. public function testDatetimeEmpty()
  5789. {
  5790. extract($this->dateRegex);
  5791. $result = $this->Form->dateTime('Contact.date', [
  5792. 'timeFormat' => 12,
  5793. 'empty' => true,
  5794. 'default' => true
  5795. ]);
  5796. $expected = [
  5797. ['select' => ['name' => 'Contact[date][year]']],
  5798. $yearsRegex,
  5799. ['option' => ['value' => '']],
  5800. '/option',
  5801. '*/select',
  5802. ['select' => ['name' => 'Contact[date][month]']],
  5803. $monthsRegex,
  5804. ['option' => ['value' => '']],
  5805. '/option',
  5806. '*/select',
  5807. ['select' => ['name' => 'Contact[date][day]']],
  5808. $daysRegex,
  5809. ['option' => ['value' => '']],
  5810. '/option',
  5811. '*/select',
  5812. ['select' => ['name' => 'Contact[date][hour]']],
  5813. $hoursRegex,
  5814. ['option' => ['value' => '']],
  5815. '/option',
  5816. '*/select',
  5817. ['select' => ['name' => 'Contact[date][minute]']],
  5818. $minutesRegex,
  5819. ['option' => ['value' => '']],
  5820. '/option',
  5821. '*/select',
  5822. ['select' => ['name' => 'Contact[date][meridian]']],
  5823. $meridianRegex,
  5824. ['option' => ['value' => '']],
  5825. '/option',
  5826. '*/select'
  5827. ];
  5828. $this->assertHtml($expected, $result);
  5829. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5830. }
  5831. /**
  5832. * testDatetimeMinuteInterval method
  5833. *
  5834. * Test datetime with interval option.
  5835. *
  5836. * @return void
  5837. */
  5838. public function testDatetimeMinuteInterval()
  5839. {
  5840. extract($this->dateRegex);
  5841. $result = $this->Form->dateTime('Contact.date', [
  5842. 'interval' => 5,
  5843. 'value' => ''
  5844. ]);
  5845. $expected = [
  5846. ['select' => ['name' => 'Contact[date][year]']],
  5847. $yearsRegex,
  5848. ['option' => ['selected' => 'selected', 'value' => '']],
  5849. '/option',
  5850. '*/select',
  5851. ['select' => ['name' => 'Contact[date][month]']],
  5852. $monthsRegex,
  5853. ['option' => ['selected' => 'selected', 'value' => '']],
  5854. '/option',
  5855. '*/select',
  5856. ['select' => ['name' => 'Contact[date][day]']],
  5857. $daysRegex,
  5858. ['option' => ['selected' => 'selected', 'value' => '']],
  5859. '/option',
  5860. '*/select',
  5861. ['select' => ['name' => 'Contact[date][hour]']],
  5862. $hoursRegex,
  5863. ['option' => ['selected' => 'selected', 'value' => '']],
  5864. '/option',
  5865. '*/select',
  5866. ['select' => ['name' => 'Contact[date][minute]']],
  5867. $minutesRegex,
  5868. ['option' => ['selected' => 'selected', 'value' => '']],
  5869. '/option',
  5870. ['option' => ['value' => '00']],
  5871. '00',
  5872. '/option',
  5873. ['option' => ['value' => '05']],
  5874. '05',
  5875. '/option',
  5876. ['option' => ['value' => '10']],
  5877. '10',
  5878. '/option',
  5879. '*/select',
  5880. ];
  5881. $this->assertHtml($expected, $result);
  5882. }
  5883. /**
  5884. * testDateTimeRounding method
  5885. *
  5886. * Test dateTime with rounding.
  5887. *
  5888. * @return void
  5889. */
  5890. public function testDateTimeRounding()
  5891. {
  5892. $this->Form->request = $this->Form->request->withData('Contact', [
  5893. 'date' => [
  5894. 'day' => '13',
  5895. 'month' => '12',
  5896. 'year' => '2010',
  5897. 'hour' => '04',
  5898. 'minute' => '19',
  5899. 'meridian' => 'AM'
  5900. ]
  5901. ]);
  5902. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5903. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5904. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5905. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5906. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5907. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5908. }
  5909. /**
  5910. * testDatetimeWithDefault method
  5911. *
  5912. * Test that datetime() and default values work.
  5913. *
  5914. * @return void
  5915. */
  5916. public function testDatetimeWithDefault()
  5917. {
  5918. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5919. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5920. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5921. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5922. $result = $this->Form->dateTime('Contact.updated', [
  5923. 'default' => '2009-06-01 11:15:30'
  5924. ]);
  5925. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5926. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5927. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5928. }
  5929. /**
  5930. * testDateTimeAllZeros method
  5931. *
  5932. * Test datetime() with all zeros.
  5933. *
  5934. * @return void
  5935. */
  5936. public function testDateTimeAllZeros()
  5937. {
  5938. $result = $this->Form->dateTime('Contact.date', [
  5939. 'timeFormat' => false,
  5940. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5941. 'value' => '0000-00-00'
  5942. ]);
  5943. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5944. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5945. }
  5946. /**
  5947. * testDateTimeEmptyAsArray method
  5948. *
  5949. * @return void
  5950. */
  5951. public function testDateTimeEmptyAsArray()
  5952. {
  5953. $result = $this->Form->dateTime('Contact.date', [
  5954. 'empty' => [
  5955. 'day' => 'DAY',
  5956. 'month' => 'MONTH',
  5957. 'year' => 'YEAR',
  5958. 'hour' => 'HOUR',
  5959. 'minute' => 'MINUTE',
  5960. 'meridian' => false
  5961. ],
  5962. 'default' => true
  5963. ]);
  5964. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5965. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5966. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5967. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5968. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5969. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5970. $result = $this->Form->dateTime('Contact.date', [
  5971. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5972. 'default' => true
  5973. ]);
  5974. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5975. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5976. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5977. }
  5978. /**
  5979. * testFormDateTimeMulti method
  5980. *
  5981. * Test multiple datetime element generation.
  5982. *
  5983. * @return void
  5984. */
  5985. public function testFormDateTimeMulti()
  5986. {
  5987. extract($this->dateRegex);
  5988. $result = $this->Form->dateTime('Contact.1.updated');
  5989. $this->assertContains('Contact[1][updated][month]', $result);
  5990. $this->assertContains('Contact[1][updated][day]', $result);
  5991. $this->assertContains('Contact[1][updated][year]', $result);
  5992. $this->assertContains('Contact[1][updated][hour]', $result);
  5993. $this->assertContains('Contact[1][updated][minute]', $result);
  5994. }
  5995. /**
  5996. * testDateTimeLabelIdMatchesFirstControl method
  5997. *
  5998. * When changing the date format, the label should always focus the first select box when
  5999. * clicked.
  6000. *
  6001. * @return void
  6002. */
  6003. public function testDateTimeLabelIdMatchesFirstControl()
  6004. {
  6005. $result = $this->Form->control('Model.date', ['type' => 'date']);
  6006. $this->assertContains('<label>Date</label>', $result);
  6007. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  6008. $this->assertContains('<label>Date</label>', $result);
  6009. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  6010. $this->assertContains('<label>Date</label>', $result);
  6011. }
  6012. /**
  6013. * testDateTimeSecondOptions method
  6014. *
  6015. * Test datetime second=true.
  6016. *
  6017. * @return void
  6018. */
  6019. public function testDateTimeSecondOptions()
  6020. {
  6021. $result = $this->Form->dateTime('updated', ['second' => true]);
  6022. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6023. $result = $this->Form->dateTime('updated', ['second' => []]);
  6024. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6025. $result = $this->Form->dateTime('updated', ['second' => null]);
  6026. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6027. $result = $this->Form->dateTime('updated', ['second' => false]);
  6028. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6029. }
  6030. /**
  6031. * testMonth method
  6032. *
  6033. * Test generation of a month input.
  6034. *
  6035. * @return void
  6036. */
  6037. public function testMonth()
  6038. {
  6039. $result = $this->Form->month('Model.field', ['value' => '']);
  6040. $expected = [
  6041. ['select' => ['name' => 'Model[field][month]']],
  6042. ['option' => ['value' => '', 'selected' => 'selected']],
  6043. '/option',
  6044. ['option' => ['value' => '01']],
  6045. date('F', strtotime('2008-01-01 00:00:00')),
  6046. '/option',
  6047. ['option' => ['value' => '02']],
  6048. date('F', strtotime('2008-02-01 00:00:00')),
  6049. '/option',
  6050. '*/select',
  6051. ];
  6052. $this->assertHtml($expected, $result);
  6053. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6054. $expected = [
  6055. ['select' => ['name' => 'Model[field][month]']],
  6056. ['option' => ['selected' => 'selected', 'value' => '']],
  6057. '/option',
  6058. ['option' => ['value' => '01']],
  6059. date('F', strtotime('2008-01-01 00:00:00')),
  6060. '/option',
  6061. ['option' => ['value' => '02']],
  6062. date('F', strtotime('2008-02-01 00:00:00')),
  6063. '/option',
  6064. '*/select',
  6065. ];
  6066. $this->assertHtml($expected, $result);
  6067. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6068. $expected = [
  6069. ['select' => ['name' => 'Model[field][month]']],
  6070. ['option' => ['selected' => 'selected', 'value' => '']],
  6071. '/option',
  6072. ['option' => ['value' => '01']],
  6073. '1',
  6074. '/option',
  6075. ['option' => ['value' => '02']],
  6076. '2',
  6077. '/option',
  6078. '*/select',
  6079. ];
  6080. $this->assertHtml($expected, $result);
  6081. $monthNames = [
  6082. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6083. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6084. ];
  6085. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6086. $expected = [
  6087. ['select' => ['name' => 'Model[field][month]']],
  6088. ['option' => ['value' => '']],
  6089. '/option',
  6090. ['option' => ['value' => '01', 'selected' => 'selected']],
  6091. 'Jan',
  6092. '/option',
  6093. ['option' => ['value' => '02']],
  6094. 'Feb',
  6095. '/option',
  6096. '*/select',
  6097. ];
  6098. $this->assertHtml($expected, $result);
  6099. $this->Form->request = $this->Form->request->withData('Project.release', '2050-02-10');
  6100. $this->Form->create();
  6101. $result = $this->Form->month('Project.release');
  6102. $expected = [
  6103. ['select' => ['name' => 'Project[release][month]']],
  6104. ['option' => ['value' => '']],
  6105. '/option',
  6106. ['option' => ['value' => '01']],
  6107. 'January',
  6108. '/option',
  6109. ['option' => ['value' => '02', 'selected' => 'selected']],
  6110. 'February',
  6111. '/option',
  6112. '*/select',
  6113. ];
  6114. $this->assertHtml($expected, $result);
  6115. $result = $this->Form->month('Contact.published', [
  6116. 'empty' => 'Published on',
  6117. ]);
  6118. $this->assertContains('Published on', $result);
  6119. }
  6120. /**
  6121. * testDay method
  6122. *
  6123. * Test generation of a day input.
  6124. *
  6125. * @return void
  6126. */
  6127. public function testDay()
  6128. {
  6129. extract($this->dateRegex);
  6130. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6131. $expected = [
  6132. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6133. ['option' => ['selected' => 'selected', 'value' => '']],
  6134. '/option',
  6135. ['option' => ['value' => '01']],
  6136. '1',
  6137. '/option',
  6138. ['option' => ['value' => '02']],
  6139. '2',
  6140. '/option',
  6141. $daysRegex,
  6142. '/select',
  6143. ];
  6144. $this->assertHtml($expected, $result);
  6145. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 23:12:32');
  6146. $this->Form->create();
  6147. $result = $this->Form->day('Model.field');
  6148. $expected = [
  6149. ['select' => ['name' => 'Model[field][day]']],
  6150. ['option' => ['value' => '']],
  6151. '/option',
  6152. ['option' => ['value' => '01']],
  6153. '1',
  6154. '/option',
  6155. ['option' => ['value' => '02']],
  6156. '2',
  6157. '/option',
  6158. $daysRegex,
  6159. ['option' => ['value' => '10', 'selected' => 'selected']],
  6160. '10',
  6161. '/option',
  6162. $daysRegex,
  6163. '/select',
  6164. ];
  6165. $this->assertHtml($expected, $result);
  6166. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6167. $this->Form->create();
  6168. $result = $this->Form->day('Model.field', ['value' => '10']);
  6169. $expected = [
  6170. ['select' => ['name' => 'Model[field][day]']],
  6171. ['option' => ['value' => '']],
  6172. '/option',
  6173. ['option' => ['value' => '01']],
  6174. '1',
  6175. '/option',
  6176. ['option' => ['value' => '02']],
  6177. '2',
  6178. '/option',
  6179. $daysRegex,
  6180. ['option' => ['value' => '10', 'selected' => 'selected']],
  6181. '10',
  6182. '/option',
  6183. $daysRegex,
  6184. '/select',
  6185. ];
  6186. $this->assertHtml($expected, $result);
  6187. $this->Form->request = $this->Form->request->withData('Project.release', '2050-10-10');
  6188. $this->Form->create();
  6189. $result = $this->Form->day('Project.release');
  6190. $expected = [
  6191. ['select' => ['name' => 'Project[release][day]']],
  6192. ['option' => ['value' => '']],
  6193. '/option',
  6194. ['option' => ['value' => '01']],
  6195. '1',
  6196. '/option',
  6197. ['option' => ['value' => '02']],
  6198. '2',
  6199. '/option',
  6200. $daysRegex,
  6201. ['option' => ['value' => '10', 'selected' => 'selected']],
  6202. '10',
  6203. '/option',
  6204. $daysRegex,
  6205. '/select',
  6206. ];
  6207. $this->assertHtml($expected, $result);
  6208. $result = $this->Form->day('Contact.published', [
  6209. 'empty' => 'Published on',
  6210. ]);
  6211. $this->assertContains('Published on', $result);
  6212. }
  6213. /**
  6214. * testMinute method
  6215. *
  6216. * Test generation of a minute input.
  6217. *
  6218. * @return void
  6219. */
  6220. public function testMinute()
  6221. {
  6222. extract($this->dateRegex);
  6223. $result = $this->Form->minute('Model.field', ['value' => '']);
  6224. $expected = [
  6225. ['select' => ['name' => 'Model[field][minute]']],
  6226. ['option' => ['selected' => 'selected', 'value' => '']],
  6227. '/option',
  6228. ['option' => ['value' => '00']],
  6229. '00',
  6230. '/option',
  6231. ['option' => ['value' => '01']],
  6232. '01',
  6233. '/option',
  6234. ['option' => ['value' => '02']],
  6235. '02',
  6236. '/option',
  6237. $minutesRegex,
  6238. '/select',
  6239. ];
  6240. $this->assertHtml($expected, $result);
  6241. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6242. $this->Form->create();
  6243. $result = $this->Form->minute('Model.field');
  6244. $expected = [
  6245. ['select' => ['name' => 'Model[field][minute]']],
  6246. ['option' => ['value' => '']],
  6247. '/option',
  6248. ['option' => ['value' => '00']],
  6249. '00',
  6250. '/option',
  6251. ['option' => ['value' => '01']],
  6252. '01',
  6253. '/option',
  6254. ['option' => ['value' => '02']],
  6255. '02',
  6256. '/option',
  6257. $minutesRegex,
  6258. ['option' => ['value' => '12', 'selected' => 'selected']],
  6259. '12',
  6260. '/option',
  6261. $minutesRegex,
  6262. '/select',
  6263. ];
  6264. $this->assertHtml($expected, $result);
  6265. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6266. $this->Form->create();
  6267. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6268. $expected = [
  6269. ['select' => ['name' => 'Model[field][minute]']],
  6270. ['option' => ['selected' => 'selected', 'value' => '']],
  6271. '/option',
  6272. ['option' => ['value' => '00']],
  6273. '00',
  6274. '/option',
  6275. ['option' => ['value' => '05']],
  6276. '05',
  6277. '/option',
  6278. ['option' => ['value' => '10']],
  6279. '10',
  6280. '/option',
  6281. $minutesRegex,
  6282. '/select',
  6283. ];
  6284. $this->assertHtml($expected, $result);
  6285. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:10:32');
  6286. $this->Form->create();
  6287. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6288. $expected = [
  6289. ['select' => ['name' => 'Model[field][minute]']],
  6290. ['option' => ['value' => '']],
  6291. '/option',
  6292. ['option' => ['value' => '00']],
  6293. '00',
  6294. '/option',
  6295. ['option' => ['value' => '05']],
  6296. '05',
  6297. '/option',
  6298. ['option' => ['value' => '10', 'selected' => 'selected']],
  6299. '10',
  6300. '/option',
  6301. $minutesRegex,
  6302. '/select',
  6303. ];
  6304. $this->assertHtml($expected, $result);
  6305. }
  6306. /**
  6307. * testMeridian method
  6308. *
  6309. * Test generating an input for the meridian.
  6310. *
  6311. * @return void
  6312. */
  6313. public function testMeridian()
  6314. {
  6315. extract($this->dateRegex);
  6316. $now = new \DateTime();
  6317. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6318. $expected = [
  6319. ['select' => ['name' => 'Model[field][meridian]']],
  6320. ['option' => ['value' => '']],
  6321. '/option',
  6322. $meridianRegex,
  6323. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6324. $now->format('a'),
  6325. '/option',
  6326. '*/select'
  6327. ];
  6328. $this->assertHtml($expected, $result);
  6329. }
  6330. /**
  6331. * testHour method
  6332. *
  6333. * Test generation of an hour input.
  6334. *
  6335. * @return void
  6336. */
  6337. public function testHour()
  6338. {
  6339. extract($this->dateRegex);
  6340. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6341. $expected = [
  6342. ['select' => ['name' => 'Model[field][hour]']],
  6343. ['option' => ['selected' => 'selected', 'value' => '']],
  6344. '/option',
  6345. ['option' => ['value' => '01']],
  6346. '1',
  6347. '/option',
  6348. ['option' => ['value' => '02']],
  6349. '2',
  6350. '/option',
  6351. $hoursRegex,
  6352. '/select',
  6353. ];
  6354. $this->assertHtml($expected, $result);
  6355. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6356. $this->Form->create();
  6357. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6358. $expected = [
  6359. ['select' => ['name' => 'Model[field][hour]']],
  6360. ['option' => ['value' => '']],
  6361. '/option',
  6362. ['option' => ['value' => '01']],
  6363. '1',
  6364. '/option',
  6365. ['option' => ['value' => '02']],
  6366. '2',
  6367. '/option',
  6368. $hoursRegex,
  6369. ['option' => ['value' => '12', 'selected' => 'selected']],
  6370. '12',
  6371. '/option',
  6372. '/select',
  6373. ];
  6374. $this->assertHtml($expected, $result);
  6375. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6376. $this->Form->create();
  6377. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6378. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6379. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6380. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6381. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6382. $this->Form->create();
  6383. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6384. $expected = [
  6385. ['select' => ['name' => 'Model[field][hour]']],
  6386. ['option' => ['value' => '']],
  6387. '/option',
  6388. ['option' => ['value' => '00', 'selected' => 'selected']],
  6389. '0',
  6390. '/option',
  6391. ['option' => ['value' => '01']],
  6392. '1',
  6393. '/option',
  6394. ['option' => ['value' => '02']],
  6395. '2',
  6396. '/option',
  6397. $hoursRegex,
  6398. '/select',
  6399. ];
  6400. $this->assertHtml($expected, $result);
  6401. $this->Form->request = $this->Form->request->withData('Model.field', null);
  6402. $this->Form->create();
  6403. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6404. $thisHour = date('H');
  6405. $optValue = date('G');
  6406. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6407. $this->Form->request = $this->Form->request->withData('Model.field', '2050-10-10 01:12:32');
  6408. $this->Form->create();
  6409. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6410. $expected = [
  6411. ['select' => ['name' => 'Model[field][hour]']],
  6412. ['option' => ['value' => '']],
  6413. '/option',
  6414. ['option' => ['value' => '00']],
  6415. '0',
  6416. '/option',
  6417. ['option' => ['value' => '01', 'selected' => 'selected']],
  6418. '1',
  6419. '/option',
  6420. ['option' => ['value' => '02']],
  6421. '2',
  6422. '/option',
  6423. $hoursRegex,
  6424. '/select',
  6425. ];
  6426. $this->assertHtml($expected, $result);
  6427. }
  6428. /**
  6429. * testYear method
  6430. *
  6431. * Test generation of a year input.
  6432. *
  6433. * @return void
  6434. */
  6435. public function testYear()
  6436. {
  6437. $this->Form->request = $this->Form->request->withData('Contact.published', '2006-10-10');
  6438. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6439. $expected = [
  6440. ['select' => ['name' => 'Model[field][year]']],
  6441. ['option' => ['selected' => 'selected', 'value' => '']],
  6442. '/option',
  6443. ['option' => ['value' => '2007']],
  6444. '2007',
  6445. '/option',
  6446. ['option' => ['value' => '2006']],
  6447. '2006',
  6448. '/option',
  6449. '/select',
  6450. ];
  6451. $this->assertHtml($expected, $result);
  6452. $result = $this->Form->year('Model.field', [
  6453. 'value' => '',
  6454. 'minYear' => 2006,
  6455. 'maxYear' => 2007,
  6456. 'orderYear' => 'asc'
  6457. ]);
  6458. $expected = [
  6459. ['select' => ['name' => 'Model[field][year]']],
  6460. ['option' => ['selected' => 'selected', 'value' => '']],
  6461. '/option',
  6462. ['option' => ['value' => '2006']],
  6463. '2006',
  6464. '/option',
  6465. ['option' => ['value' => '2007']],
  6466. '2007',
  6467. '/option',
  6468. '/select',
  6469. ];
  6470. $this->assertHtml($expected, $result);
  6471. $result = $this->Form->year('Contact.published', [
  6472. 'empty' => false,
  6473. 'minYear' => 2006,
  6474. 'maxYear' => 2007,
  6475. ]);
  6476. $expected = [
  6477. ['select' => ['name' => 'Contact[published][year]']],
  6478. ['option' => ['value' => '2007']],
  6479. '2007',
  6480. '/option',
  6481. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6482. '2006',
  6483. '/option',
  6484. '/select',
  6485. ];
  6486. $this->assertHtml($expected, $result);
  6487. $result = $this->Form->year('Contact.published', [
  6488. 'empty' => 'Published on',
  6489. ]);
  6490. $this->assertContains('Published on', $result);
  6491. }
  6492. /**
  6493. * testControlDatetimePreEpoch method
  6494. *
  6495. * Test minYear being prior to the unix epoch.
  6496. *
  6497. * @return void
  6498. */
  6499. public function testControlDatetimePreEpoch()
  6500. {
  6501. $start = date('Y') - 80;
  6502. $end = date('Y') - 18;
  6503. $result = $this->Form->control('birth_year', [
  6504. 'type' => 'date',
  6505. 'label' => 'Birth Year',
  6506. 'minYear' => $start,
  6507. 'maxYear' => $end,
  6508. 'month' => false,
  6509. 'day' => false,
  6510. ]);
  6511. $this->assertContains('value="' . $start . '">' . $start, $result);
  6512. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6513. $this->assertNotContains('value="00">00', $result);
  6514. }
  6515. /**
  6516. * test control() datetime & required attributes
  6517. *
  6518. * @return void
  6519. */
  6520. public function testControlDatetimeRequired()
  6521. {
  6522. $result = $this->Form->control('birthday', [
  6523. 'type' => 'date',
  6524. 'required' => true
  6525. ]);
  6526. $this->assertContains(
  6527. '<select name="birthday[year]" required="required"',
  6528. $result
  6529. );
  6530. }
  6531. /**
  6532. * testYearAutoExpandRange method
  6533. *
  6534. * @return void
  6535. */
  6536. public function testYearAutoExpandRange()
  6537. {
  6538. $this->Form->request = $this->Form->request->withData('User.birthday', '1930-10-10');
  6539. $result = $this->Form->year('User.birthday');
  6540. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6541. $result = $matches[1];
  6542. $expected = range(date('Y') + 5, 1930);
  6543. $this->assertEquals($expected, $result);
  6544. $this->Form->request = $this->Form->request->withData('Project.release', '2050-10-10');
  6545. $this->Form->create();
  6546. $result = $this->Form->year('Project.release');
  6547. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6548. $result = $matches[1];
  6549. $expected = range(2050, date('Y') - 5);
  6550. $this->assertEquals($expected, $result);
  6551. $this->Form->request = $this->Form->request->withData('Project.release', '1881-10-10');
  6552. $this->Form->create();
  6553. $result = $this->Form->year('Project.release', [
  6554. 'minYear' => 1890,
  6555. 'maxYear' => 1900
  6556. ]);
  6557. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6558. $result = $matches[1];
  6559. $expected = range(1900, 1881);
  6560. $this->assertEquals($expected, $result);
  6561. }
  6562. /**
  6563. * testControlDate method
  6564. *
  6565. * Test that control() accepts the type of date and passes options in.
  6566. *
  6567. * @return void
  6568. */
  6569. public function testControlDate()
  6570. {
  6571. $this->Form->request = $this->Form->request->withParsedBody([
  6572. 'month_year' => ['month' => date('m')],
  6573. ]);
  6574. $this->Form->create($this->article);
  6575. $result = $this->Form->control('month_year', [
  6576. 'label' => false,
  6577. 'type' => 'date',
  6578. 'minYear' => 2006,
  6579. 'maxYear' => 2008
  6580. ]);
  6581. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6582. $this->assertNotContains('value="2008" selected="selected"', $result);
  6583. }
  6584. /**
  6585. * testControlLabelFalse method
  6586. *
  6587. * Test the label option being set to false.
  6588. *
  6589. * @return void
  6590. */
  6591. public function testControlLabelFalse()
  6592. {
  6593. $this->Form->create($this->article);
  6594. $result = $this->Form->control('title', ['label' => false]);
  6595. $expected = [
  6596. 'div' => ['class' => 'input text required'],
  6597. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6598. '/div'
  6599. ];
  6600. $this->assertHtml($expected, $result);
  6601. }
  6602. /**
  6603. * testControlDateMaxYear method
  6604. *
  6605. * Let's say we want to only allow users born from 2006 to 2008 to register
  6606. * This being the first signup page, we still don't have any data.
  6607. *
  6608. * @return void
  6609. */
  6610. public function testControlDateMaxYear()
  6611. {
  6612. $this->Form->create($this->article);
  6613. $result = $this->Form->control('birthday', [
  6614. 'label' => false,
  6615. 'type' => 'date',
  6616. 'minYear' => 2006,
  6617. 'maxYear' => 2008,
  6618. 'default' => true
  6619. ]);
  6620. $this->assertContains('value="2008" selected="selected"', $result);
  6621. $this->assertContains('value="2006"', $result);
  6622. $this->assertNotContains('value="2005"', $result);
  6623. $this->assertNotContains('value="2009"', $result);
  6624. }
  6625. /**
  6626. * testTextArea method
  6627. *
  6628. * Test generation of a textarea input.
  6629. *
  6630. * @return void
  6631. */
  6632. public function testTextArea()
  6633. {
  6634. $this->Form->request = $this->Form->request->withData('field', 'some test data');
  6635. $result = $this->Form->textarea('field');
  6636. $expected = [
  6637. 'textarea' => ['name' => 'field', 'rows' => 5],
  6638. 'some test data',
  6639. '/textarea',
  6640. ];
  6641. $this->assertHtml($expected, $result);
  6642. $result = $this->Form->textarea('user.bio');
  6643. $expected = [
  6644. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6645. '/textarea',
  6646. ];
  6647. $this->assertHtml($expected, $result);
  6648. $this->Form->request = $this->Form->request
  6649. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  6650. $this->Form->create();
  6651. $result = $this->Form->textarea('field');
  6652. $expected = [
  6653. 'textarea' => ['name' => 'field', 'rows' => 5],
  6654. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6655. '/textarea',
  6656. ];
  6657. $this->assertHtml($expected, $result);
  6658. $this->Form->request = $this->Form->request->withData(
  6659. 'Model.field',
  6660. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6661. );
  6662. $this->Form->create();
  6663. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6664. $expected = [
  6665. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6666. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6667. '/textarea',
  6668. ];
  6669. $this->assertHtml($expected, $result);
  6670. $result = $this->Form->textarea('0.OtherModel.field');
  6671. $expected = [
  6672. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6673. '/textarea'
  6674. ];
  6675. $this->assertHtml($expected, $result);
  6676. }
  6677. /**
  6678. * testTextAreaWithStupidCharacters method
  6679. *
  6680. * Test text area with non-ascii characters.
  6681. *
  6682. * @return void
  6683. */
  6684. public function testTextAreaWithStupidCharacters()
  6685. {
  6686. $result = $this->Form->textarea('Post.content', [
  6687. 'value' => 'GREAT®',
  6688. 'rows' => '15',
  6689. 'cols' => '75'
  6690. ]);
  6691. $expected = [
  6692. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6693. 'GREAT®',
  6694. '/textarea',
  6695. ];
  6696. $this->assertHtml($expected, $result);
  6697. }
  6698. /**
  6699. * testTextAreaMaxLength method
  6700. *
  6701. * Test textareas maxlength read from schema.
  6702. *
  6703. * @return void
  6704. */
  6705. public function testTextAreaMaxLength()
  6706. {
  6707. $this->Form->create([
  6708. 'schema' => [
  6709. 'stuff' => ['type' => 'string', 'length' => 10],
  6710. ]
  6711. ]);
  6712. $result = $this->Form->control('other', ['type' => 'textarea']);
  6713. $expected = [
  6714. 'div' => ['class' => 'input textarea'],
  6715. 'label' => ['for' => 'other'],
  6716. 'Other',
  6717. '/label',
  6718. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6719. '/textarea',
  6720. '/div'
  6721. ];
  6722. $this->assertHtml($expected, $result);
  6723. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6724. $expected = [
  6725. 'div' => ['class' => 'input textarea'],
  6726. 'label' => ['for' => 'stuff'],
  6727. 'Stuff',
  6728. '/label',
  6729. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6730. '/textarea',
  6731. '/div'
  6732. ];
  6733. $this->assertHtml($expected, $result);
  6734. }
  6735. /**
  6736. * testHiddenField method
  6737. *
  6738. * Test generation of a hidden input.
  6739. *
  6740. * @return void
  6741. */
  6742. public function testHiddenField()
  6743. {
  6744. $this->article['errors'] = [
  6745. 'field' => true
  6746. ];
  6747. $this->Form->request = $this->Form->request->withData('field', 'test');
  6748. $this->Form->create($this->article);
  6749. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6750. $expected = [
  6751. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6752. $this->assertHtml($expected, $result);
  6753. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6754. $expected = [
  6755. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6756. ];
  6757. $this->assertHtml($expected, $result);
  6758. }
  6759. /**
  6760. * testFileUploadField method
  6761. *
  6762. * Test generation of a file upload input.
  6763. *
  6764. * @return void
  6765. */
  6766. public function testFileUploadField()
  6767. {
  6768. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6769. $result = $this->Form->file('Model.upload');
  6770. $this->assertHtml($expected, $result);
  6771. $this->Form->request = $this->Form->request->withData('Model.upload', [
  6772. 'name' => '', 'type' => '', 'tmp_name' => '',
  6773. 'error' => 4, 'size' => 0
  6774. ]);
  6775. $result = $this->Form->file('Model.upload');
  6776. $this->assertHtml($expected, $result);
  6777. $this->Form->request = $this->Form->request->withData('Model.upload', 'no data should be set in value');
  6778. $result = $this->Form->file('Model.upload');
  6779. $this->assertHtml($expected, $result);
  6780. }
  6781. /**
  6782. * testFileUploadOnOtherModel method
  6783. *
  6784. * Test File upload input on a model not used in create().
  6785. *
  6786. * @return void
  6787. */
  6788. public function testFileUploadOnOtherModel()
  6789. {
  6790. $this->Form->create($this->article, ['type' => 'file']);
  6791. $result = $this->Form->file('ValidateProfile.city');
  6792. $expected = [
  6793. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6794. ];
  6795. $this->assertHtml($expected, $result);
  6796. }
  6797. /**
  6798. * testButton method
  6799. *
  6800. * Test generation of a form button.
  6801. *
  6802. * @return void
  6803. */
  6804. public function testButton()
  6805. {
  6806. $result = $this->Form->button('Hi');
  6807. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6808. $this->assertHtml($expected, $result);
  6809. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6810. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6811. $this->assertHtml($expected, $result);
  6812. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6813. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6814. $this->assertHtml($expected, $result);
  6815. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6816. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6817. $this->assertHtml($expected, $result);
  6818. $result = $this->Form->button('No type', ['type' => false]);
  6819. $expected = ['button' => [], 'No type', '/button'];
  6820. $this->assertHtml($expected, $result);
  6821. $result = $this->Form->button('Upload Text', [
  6822. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6823. 'escape' => false
  6824. ]);
  6825. $this->assertNotRegExp('/\&039/', $result);
  6826. }
  6827. /**
  6828. * testButtonUnlockedByDefault method
  6829. *
  6830. * Test that button() makes unlocked fields by default.
  6831. *
  6832. * @return void
  6833. */
  6834. public function testButtonUnlockedByDefault()
  6835. {
  6836. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'secured');
  6837. $this->Form->button('Save', ['name' => 'save']);
  6838. $this->Form->button('Clear');
  6839. $result = $this->Form->unlockField();
  6840. $this->assertEquals(['save'], $result);
  6841. }
  6842. /**
  6843. * Test generation of a form button with confirm message.
  6844. *
  6845. * @return void
  6846. */
  6847. public function testButtonWithConfirm()
  6848. {
  6849. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6850. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6851. $this->assertHtml($expected, $result);
  6852. }
  6853. /**
  6854. * testPostButton method
  6855. *
  6856. * @return void
  6857. */
  6858. public function testPostButton()
  6859. {
  6860. $result = $this->Form->postButton('Hi', '/controller/action');
  6861. $expected = [
  6862. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6863. 'div' => ['style' => 'display:none;'],
  6864. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6865. '/div',
  6866. 'button' => ['type' => 'submit'],
  6867. 'Hi',
  6868. '/button',
  6869. '/form'
  6870. ];
  6871. $this->assertHtml($expected, $result);
  6872. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6873. $this->assertContains('<input type="hidden" name="extra" value="value"', $result);
  6874. }
  6875. /**
  6876. * testPostButtonMethodType method
  6877. *
  6878. * @return void
  6879. */
  6880. public function testPostButtonMethodType()
  6881. {
  6882. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6883. $expected = [
  6884. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6885. 'div' => ['style' => 'display:none;'],
  6886. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6887. '/div',
  6888. 'button' => ['type' => 'submit'],
  6889. 'Hi',
  6890. '/button',
  6891. '/form'
  6892. ];
  6893. $this->assertHtml($expected, $result);
  6894. }
  6895. /**
  6896. * testPostButtonFormOptions method
  6897. *
  6898. * @return void
  6899. */
  6900. public function testPostButtonFormOptions()
  6901. {
  6902. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6903. $expected = [
  6904. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6905. 'div' => ['style' => 'display:none;'],
  6906. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6907. '/div',
  6908. 'button' => ['type' => 'submit'],
  6909. 'Hi',
  6910. '/button',
  6911. '/form'
  6912. ];
  6913. $this->assertHtml($expected, $result);
  6914. }
  6915. /**
  6916. * testPostButtonNestedData method
  6917. *
  6918. * Test using postButton with N dimensional data.
  6919. *
  6920. * @return void
  6921. */
  6922. public function testPostButtonNestedData()
  6923. {
  6924. $data = [
  6925. 'one' => [
  6926. 'two' => [
  6927. 3, 4, 5
  6928. ]
  6929. ]
  6930. ];
  6931. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6932. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6933. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6934. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6935. }
  6936. /**
  6937. * testSecurePostButton method
  6938. *
  6939. * Test that postButton adds _Token fields.
  6940. *
  6941. * @return void
  6942. */
  6943. public function testSecurePostButton()
  6944. {
  6945. $this->Form->request = $this->Form->request
  6946. ->withParam('_csrfToken', 'testkey')
  6947. ->withParam('_Token.unlockedFields', []);
  6948. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6949. $tokenDebug = urlencode(json_encode([
  6950. '/posts/delete/1',
  6951. [],
  6952. []
  6953. ]));
  6954. $expected = [
  6955. 'form' => [
  6956. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6957. ],
  6958. ['div' => ['style' => 'display:none;']],
  6959. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6960. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6961. '/div',
  6962. 'button' => ['type' => 'submit'],
  6963. 'Delete',
  6964. '/button',
  6965. ['div' => ['style' => 'display:none;']],
  6966. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6967. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6968. ['input' => [
  6969. 'type' => 'hidden',
  6970. 'name' => '_Token[debug]',
  6971. 'value' => $tokenDebug,
  6972. 'autocomplete' => 'off',
  6973. ]],
  6974. '/div',
  6975. '/form',
  6976. ];
  6977. $this->assertHtml($expected, $result);
  6978. }
  6979. /**
  6980. * testPostLink method
  6981. *
  6982. * @return void
  6983. */
  6984. public function testPostLink()
  6985. {
  6986. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6987. $expected = [
  6988. 'form' => [
  6989. 'method' => 'post', 'action' => '/posts/delete/1',
  6990. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6991. ],
  6992. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6993. '/form',
  6994. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6995. 'Delete',
  6996. '/a'
  6997. ];
  6998. $this->assertHtml($expected, $result);
  6999. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  7000. $expected = [
  7001. 'form' => [
  7002. 'method' => 'post', 'action' => '/posts/delete/1',
  7003. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7004. ],
  7005. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  7006. '/form',
  7007. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7008. 'Delete',
  7009. '/a'
  7010. ];
  7011. $this->assertHtml($expected, $result);
  7012. $result = $this->Form->postLink(
  7013. 'Delete',
  7014. '/posts/delete/1',
  7015. ['target' => '_blank', 'class' => 'btn btn-danger']
  7016. );
  7017. $expected = [
  7018. 'form' => [
  7019. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  7020. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7021. ],
  7022. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7023. '/form',
  7024. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7025. 'Delete',
  7026. '/a'
  7027. ];
  7028. $this->assertHtml($expected, $result);
  7029. }
  7030. /**
  7031. * testPostLinkWithConfirm method
  7032. *
  7033. * Test the confirm option for postLink().
  7034. *
  7035. * @return void
  7036. */
  7037. public function testPostLinkWithConfirm()
  7038. {
  7039. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  7040. $expected = [
  7041. 'form' => [
  7042. 'method' => 'post', 'action' => '/posts/delete/1',
  7043. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7044. ],
  7045. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7046. '/form',
  7047. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  7048. 'Delete',
  7049. '/a'
  7050. ];
  7051. $this->assertHtml($expected, $result);
  7052. $result = $this->Form->postLink(
  7053. 'Delete',
  7054. '/posts/delete/1',
  7055. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  7056. );
  7057. $expected = [
  7058. 'form' => [
  7059. 'method' => 'post', 'action' => '/posts/delete/1',
  7060. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7061. ],
  7062. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7063. '/form',
  7064. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7065. 'Delete',
  7066. '/a'
  7067. ];
  7068. $this->assertHtml($expected, $result);
  7069. }
  7070. /**
  7071. * testPostLinkWithQuery method
  7072. *
  7073. * Test postLink() with query string args.
  7074. *
  7075. * @return void
  7076. */
  7077. public function testPostLinkWithQuery()
  7078. {
  7079. $result = $this->Form->postLink(
  7080. 'Delete',
  7081. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7082. );
  7083. $expected = [
  7084. 'form' => [
  7085. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7086. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7087. ],
  7088. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7089. '/form',
  7090. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7091. 'Delete',
  7092. '/a'
  7093. ];
  7094. $this->assertHtml($expected, $result);
  7095. }
  7096. /**
  7097. * testPostLinkWithData method
  7098. *
  7099. * Test postLink with additional data.
  7100. *
  7101. * @return void
  7102. */
  7103. public function testPostLinkWithData()
  7104. {
  7105. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7106. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7107. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7108. $this->Form->create($entity);
  7109. $this->Form->end();
  7110. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7111. $this->assertContains(
  7112. '<input type="hidden" name="name" value="show"',
  7113. $result,
  7114. 'should not contain entity data.'
  7115. );
  7116. }
  7117. /**
  7118. * testPostLinkSecurityHash method
  7119. *
  7120. * Test that security hashes for postLink include the url.
  7121. *
  7122. * @return void
  7123. */
  7124. public function testPostLinkSecurityHash()
  7125. {
  7126. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7127. $hash .= '%3Aid';
  7128. $this->Form->request = $this->Form->request->withParam('_Token.key', 'test');
  7129. $result = $this->Form->postLink(
  7130. 'Delete',
  7131. '/posts/delete/1',
  7132. ['data' => ['id' => 1]]
  7133. );
  7134. $tokenDebug = urlencode(json_encode([
  7135. '/posts/delete/1',
  7136. [
  7137. 'id' => 1
  7138. ],
  7139. []
  7140. ]));
  7141. $expected = [
  7142. 'form' => [
  7143. 'method' => 'post', 'action' => '/posts/delete/1',
  7144. 'name', 'style' => 'display:none;'
  7145. ],
  7146. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7147. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7148. 'div' => ['style' => 'display:none;'],
  7149. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7150. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7151. ['input' => [
  7152. 'type' => 'hidden',
  7153. 'name' => '_Token[debug]',
  7154. 'value' => $tokenDebug,
  7155. 'autocomplete' => 'off',
  7156. ]],
  7157. '/div',
  7158. '/form',
  7159. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7160. 'Delete',
  7161. '/a'
  7162. ];
  7163. $this->assertHtml($expected, $result);
  7164. }
  7165. /**
  7166. * testPostLinkSecurityHashBlockMode method
  7167. *
  7168. * Test that postLink doesn't modify the fields in the containing form.
  7169. *
  7170. * postLink() calls inside open forms should not modify the field list
  7171. * for the form.
  7172. *
  7173. * @return void
  7174. */
  7175. public function testPostLinkSecurityHashBlockMode()
  7176. {
  7177. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7178. $hash .= '%3A';
  7179. $this->Form->request = $this->Form->request->withParam('_Token.key', 'test');
  7180. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7181. $this->Form->control('title');
  7182. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7183. $result = $this->View->fetch('postLink');
  7184. $this->assertEquals(['title'], $this->Form->fields);
  7185. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7186. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7187. }
  7188. /**
  7189. * testPostLinkSecurityHashNoDebugMode method
  7190. *
  7191. * Test that security does not include debug token if debug is false.
  7192. *
  7193. * @return void
  7194. */
  7195. public function testPostLinkSecurityHashNoDebugMode()
  7196. {
  7197. Configure::write('debug', false);
  7198. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7199. $hash .= '%3Aid';
  7200. $this->Form->request = $this->Form->request
  7201. ->withParam('_Token.key', 'test');
  7202. $result = $this->Form->postLink(
  7203. 'Delete',
  7204. '/posts/delete/1',
  7205. ['data' => ['id' => 1]]
  7206. );
  7207. $expected = [
  7208. 'form' => [
  7209. 'method' => 'post', 'action' => '/posts/delete/1',
  7210. 'name', 'style' => 'display:none;'
  7211. ],
  7212. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7213. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7214. 'div' => ['style' => 'display:none;'],
  7215. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7216. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7217. '/div',
  7218. '/form',
  7219. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7220. 'Delete',
  7221. '/a'
  7222. ];
  7223. $this->assertHtml($expected, $result);
  7224. }
  7225. /**
  7226. * testPostLinkNestedData method
  7227. *
  7228. * Test using postLink with N dimensional data.
  7229. *
  7230. * @return void
  7231. */
  7232. public function testPostLinkNestedData()
  7233. {
  7234. $data = [
  7235. 'one' => [
  7236. 'two' => [
  7237. 3, 4, 5
  7238. ]
  7239. ]
  7240. ];
  7241. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7242. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7243. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7244. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7245. }
  7246. /**
  7247. * testPostLinkAfterGetForm method
  7248. *
  7249. * Test creating postLinks after a GET form.
  7250. *
  7251. * @return void
  7252. */
  7253. public function testPostLinkAfterGetForm()
  7254. {
  7255. $this->Form->request = $this->Form->request
  7256. ->withParam('_csrfToken', 'testkey')
  7257. ->withParam('_Token', 'val');
  7258. $this->Form->create($this->article, ['type' => 'get']);
  7259. $this->Form->end();
  7260. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7261. $tokenDebug = urlencode(json_encode([
  7262. '/posts/delete/1',
  7263. [],
  7264. []
  7265. ]));
  7266. $expected = [
  7267. 'form' => [
  7268. 'method' => 'post', 'action' => '/posts/delete/1',
  7269. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7270. ],
  7271. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7272. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7273. 'div' => ['style' => 'display:none;'],
  7274. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7275. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7276. ['input' => [
  7277. 'type' => 'hidden', 'name' => '_Token[debug]',
  7278. 'value' => $tokenDebug,
  7279. 'autocomplete' => 'off'
  7280. ]],
  7281. '/div',
  7282. '/form',
  7283. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7284. 'Delete',
  7285. '/a'
  7286. ];
  7287. $this->assertHtml($expected, $result);
  7288. }
  7289. /**
  7290. * testPostLinkFormBuffer method
  7291. *
  7292. * Test that postLink adds form tags to view block.
  7293. *
  7294. * @return void
  7295. */
  7296. public function testPostLinkFormBuffer()
  7297. {
  7298. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7299. $expected = [
  7300. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7301. 'Delete',
  7302. '/a'
  7303. ];
  7304. $this->assertHtml($expected, $result);
  7305. $result = $this->View->fetch('postLink');
  7306. $expected = [
  7307. 'form' => [
  7308. 'method' => 'post', 'action' => '/posts/delete/1',
  7309. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7310. ],
  7311. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7312. '/form'
  7313. ];
  7314. $this->assertHtml($expected, $result);
  7315. $result = $this->Form->postLink(
  7316. 'Delete',
  7317. '/posts/delete/2',
  7318. ['block' => true, 'method' => 'DELETE']
  7319. );
  7320. $expected = [
  7321. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7322. 'Delete',
  7323. '/a'
  7324. ];
  7325. $this->assertHtml($expected, $result);
  7326. $result = $this->View->fetch('postLink');
  7327. $expected = [
  7328. 'form' => [
  7329. 'method' => 'post', 'action' => '/posts/delete/1',
  7330. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7331. ],
  7332. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7333. '/form',
  7334. [
  7335. 'form' => [
  7336. 'method' => 'post', 'action' => '/posts/delete/2',
  7337. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7338. ],
  7339. ],
  7340. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7341. '/form'
  7342. ];
  7343. $this->assertHtml($expected, $result);
  7344. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7345. $expected = [
  7346. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7347. 'Delete',
  7348. '/a'
  7349. ];
  7350. $this->assertHtml($expected, $result);
  7351. $result = $this->View->fetch('foobar');
  7352. $expected = [
  7353. 'form' => [
  7354. 'method' => 'post', 'action' => '/posts/delete/1',
  7355. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7356. ],
  7357. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7358. '/form'
  7359. ];
  7360. $this->assertHtml($expected, $result);
  7361. }
  7362. /**
  7363. * testSubmitButton method
  7364. *
  7365. * @return void
  7366. */
  7367. public function testSubmitButton()
  7368. {
  7369. $result = $this->Form->submit('');
  7370. $expected = [
  7371. 'div' => ['class' => 'submit'],
  7372. 'input' => ['type' => 'submit', 'value' => ''],
  7373. '/div'
  7374. ];
  7375. $this->assertHtml($expected, $result);
  7376. $result = $this->Form->submit('Test Submit');
  7377. $expected = [
  7378. 'div' => ['class' => 'submit'],
  7379. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7380. '/div'
  7381. ];
  7382. $this->assertHtml($expected, $result);
  7383. $result = $this->Form->submit('Next >');
  7384. $expected = [
  7385. 'div' => ['class' => 'submit'],
  7386. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7387. '/div'
  7388. ];
  7389. $this->assertHtml($expected, $result);
  7390. $result = $this->Form->submit('Next >', ['escape' => false]);
  7391. $expected = [
  7392. 'div' => ['class' => 'submit'],
  7393. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7394. '/div'
  7395. ];
  7396. $this->assertHtml($expected, $result);
  7397. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7398. $expected = [
  7399. 'div' => ['class' => 'submit'],
  7400. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7401. '/div'
  7402. ];
  7403. $this->assertHtml($expected, $result);
  7404. }
  7405. /**
  7406. * testSubmitImage method
  7407. *
  7408. * Test image submit types.
  7409. *
  7410. * @return void
  7411. */
  7412. public function testSubmitImage()
  7413. {
  7414. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7415. $expected = [
  7416. 'div' => ['class' => 'submit'],
  7417. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7418. '/div'
  7419. ];
  7420. $this->assertHtml($expected, $result);
  7421. $result = $this->Form->submit('/relative/cake.power.gif');
  7422. $expected = [
  7423. 'div' => ['class' => 'submit'],
  7424. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7425. '/div'
  7426. ];
  7427. $this->assertHtml($expected, $result);
  7428. $result = $this->Form->submit('cake.power.gif');
  7429. $expected = [
  7430. 'div' => ['class' => 'submit'],
  7431. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7432. '/div'
  7433. ];
  7434. $this->assertHtml($expected, $result);
  7435. $result = $this->Form->submit('Not.an.image');
  7436. $expected = [
  7437. 'div' => ['class' => 'submit'],
  7438. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7439. '/div'
  7440. ];
  7441. $this->assertHtml($expected, $result);
  7442. }
  7443. /**
  7444. * testSubmitUnlockedByDefault method
  7445. *
  7446. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7447. * be submitted at a time.
  7448. *
  7449. * @return void
  7450. */
  7451. public function testSubmitUnlockedByDefault()
  7452. {
  7453. $this->Form->request = $this->Form->request->withParam('_Token', 'secured');
  7454. $this->Form->submit('Go go');
  7455. $this->Form->submit('Save', ['name' => 'save']);
  7456. $result = $this->Form->unlockField();
  7457. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7458. }
  7459. /**
  7460. * testSubmitImageTimestamp method
  7461. *
  7462. * Test submit image with timestamps.
  7463. *
  7464. * @return void
  7465. */
  7466. public function testSubmitImageTimestamp()
  7467. {
  7468. Configure::write('Asset.timestamp', 'force');
  7469. $result = $this->Form->submit('cake.power.gif');
  7470. $expected = [
  7471. 'div' => ['class' => 'submit'],
  7472. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7473. '/div'
  7474. ];
  7475. $this->assertHtml($expected, $result);
  7476. }
  7477. /**
  7478. * testDateTimeWithGetForms method
  7479. *
  7480. * Test that datetime() works with GET style forms.
  7481. *
  7482. * @return void
  7483. */
  7484. public function testDateTimeWithGetForms()
  7485. {
  7486. extract($this->dateRegex);
  7487. $this->Form->create($this->article, ['type' => 'get']);
  7488. $result = $this->Form->datetime('created');
  7489. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7490. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7491. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7492. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7493. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7494. }
  7495. /**
  7496. * testForMagicControlNonExistingNorValidated method
  7497. *
  7498. * @return void
  7499. */
  7500. public function testForMagicControlNonExistingNorValidated()
  7501. {
  7502. $this->Form->create($this->article);
  7503. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7504. $result = $this->Form->control('non_existing_nor_validated');
  7505. $expected = [
  7506. 'label' => ['for' => 'non-existing-nor-validated'],
  7507. 'Non Existing Nor Validated',
  7508. '/label',
  7509. 'input' => [
  7510. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7511. 'id' => 'non-existing-nor-validated'
  7512. ]
  7513. ];
  7514. $this->assertHtml($expected, $result);
  7515. $result = $this->Form->control('non_existing_nor_validated', [
  7516. 'val' => 'my value'
  7517. ]);
  7518. $expected = [
  7519. 'label' => ['for' => 'non-existing-nor-validated'],
  7520. 'Non Existing Nor Validated',
  7521. '/label',
  7522. 'input' => [
  7523. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7524. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7525. ]
  7526. ];
  7527. $this->assertHtml($expected, $result);
  7528. $this->Form->request = $this->Form->request->withData('non_existing_nor_validated', 'CakePHP magic');
  7529. $this->Form->create($this->article);
  7530. $result = $this->Form->control('non_existing_nor_validated');
  7531. $expected = [
  7532. 'label' => ['for' => 'non-existing-nor-validated'],
  7533. 'Non Existing Nor Validated',
  7534. '/label',
  7535. 'input' => [
  7536. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7537. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7538. ]
  7539. ];
  7540. $this->assertHtml($expected, $result);
  7541. }
  7542. /**
  7543. * testFormMagicControlLabel method
  7544. *
  7545. * @return void
  7546. */
  7547. public function testFormMagicControlLabel()
  7548. {
  7549. $this->getTableLocator()->get('Contacts', [
  7550. 'className' => __NAMESPACE__ . '\ContactsTable'
  7551. ]);
  7552. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7553. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7554. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7555. $expected = [
  7556. 'label' => ['for' => 'contacts-name'],
  7557. 'My label',
  7558. '/label',
  7559. 'input' => [
  7560. 'type' => 'text', 'name' => 'Contacts[name]',
  7561. 'id' => 'contacts-name', 'maxlength' => '255'
  7562. ]
  7563. ];
  7564. $this->assertHtml($expected, $result);
  7565. $result = $this->Form->control('name', [
  7566. 'label' => ['class' => 'mandatory']
  7567. ]);
  7568. $expected = [
  7569. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7570. 'Name',
  7571. '/label',
  7572. 'input' => [
  7573. 'type' => 'text', 'name' => 'name',
  7574. 'id' => 'name', 'maxlength' => '255'
  7575. ]
  7576. ];
  7577. $this->assertHtml($expected, $result);
  7578. $result = $this->Form->control('name', [
  7579. 'div' => false,
  7580. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7581. ]);
  7582. $expected = [
  7583. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7584. 'My label',
  7585. '/label',
  7586. 'input' => [
  7587. 'type' => 'text', 'name' => 'name',
  7588. 'id' => 'name', 'maxlength' => '255'
  7589. ]
  7590. ];
  7591. $this->assertHtml($expected, $result);
  7592. $result = $this->Form->control('Contact.name', [
  7593. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7594. ]);
  7595. $expected = [
  7596. 'label' => ['for' => 'my_id'],
  7597. 'Name',
  7598. '/label',
  7599. 'input' => [
  7600. 'type' => 'text', 'name' => 'Contact[name]',
  7601. 'id' => 'my_id', 'maxlength' => '255'
  7602. ]
  7603. ];
  7604. $this->assertHtml($expected, $result);
  7605. $result = $this->Form->control('1.id');
  7606. $expected = ['input' => [
  7607. 'type' => 'hidden', 'name' => '1[id]',
  7608. 'id' => '1-id'
  7609. ]];
  7610. $this->assertHtml($expected, $result);
  7611. $result = $this->Form->control('1.name');
  7612. $expected = [
  7613. 'label' => ['for' => '1-name'],
  7614. 'Name',
  7615. '/label',
  7616. 'input' => [
  7617. 'type' => 'text', 'name' => '1[name]',
  7618. 'id' => '1-name', 'maxlength' => '255'
  7619. ]
  7620. ];
  7621. $this->assertHtml($expected, $result);
  7622. }
  7623. /**
  7624. * testFormEnd method
  7625. *
  7626. * @return void
  7627. */
  7628. public function testFormEnd()
  7629. {
  7630. $this->assertEquals('</form>', $this->Form->end());
  7631. }
  7632. /**
  7633. * testMultiRecordForm method
  7634. *
  7635. * Test the generation of fields for a multi record form.
  7636. *
  7637. * @return void
  7638. */
  7639. public function testMultiRecordForm()
  7640. {
  7641. $this->loadFixtures('Articles', 'Comments');
  7642. $articles = $this->getTableLocator()->get('Articles');
  7643. $articles->hasMany('Comments');
  7644. $comment = new Entity(['comment' => 'Value']);
  7645. $article = new Article(['comments' => [$comment]]);
  7646. $this->Form->create([$article]);
  7647. $result = $this->Form->control('0.comments.1.comment');
  7648. //@codingStandardsIgnoreStart
  7649. $expected = [
  7650. 'div' => ['class' => 'input textarea'],
  7651. 'label' => ['for' => '0-comments-1-comment'],
  7652. 'Comment',
  7653. '/label',
  7654. 'textarea' => [
  7655. 'name',
  7656. 'id' => '0-comments-1-comment',
  7657. 'rows' => 5
  7658. ],
  7659. '/textarea',
  7660. '/div'
  7661. ];
  7662. //@codingStandardsIgnoreEnd
  7663. $this->assertHtml($expected, $result);
  7664. $result = $this->Form->control('0.comments.0.comment');
  7665. //@codingStandardsIgnoreStart
  7666. $expected = [
  7667. 'div' => ['class' => 'input textarea'],
  7668. 'label' => ['for' => '0-comments-0-comment'],
  7669. 'Comment',
  7670. '/label',
  7671. 'textarea' => [
  7672. 'name',
  7673. 'id' => '0-comments-0-comment',
  7674. 'rows' => 5
  7675. ],
  7676. 'Value',
  7677. '/textarea',
  7678. '/div'
  7679. ];
  7680. //@codingStandardsIgnoreEnd
  7681. $this->assertHtml($expected, $result);
  7682. $comment->setError('comment', ['Not valid']);
  7683. $result = $this->Form->control('0.comments.0.comment');
  7684. //@codingStandardsIgnoreStart
  7685. $expected = [
  7686. 'div' => ['class' => 'input textarea error'],
  7687. 'label' => ['for' => '0-comments-0-comment'],
  7688. 'Comment',
  7689. '/label',
  7690. 'textarea' => [
  7691. 'name',
  7692. 'class' => 'form-error',
  7693. 'id' => '0-comments-0-comment',
  7694. 'rows' => 5
  7695. ],
  7696. 'Value',
  7697. '/textarea',
  7698. ['div' => ['class' => 'error-message']],
  7699. 'Not valid',
  7700. '/div',
  7701. '/div'
  7702. ];
  7703. //@codingStandardsIgnoreEnd
  7704. $this->assertHtml($expected, $result);
  7705. $this->getTableLocator()->get('Comments')
  7706. ->getValidator('default')
  7707. ->allowEmpty('comment', false);
  7708. $result = $this->Form->control('0.comments.1.comment');
  7709. //@codingStandardsIgnoreStart
  7710. $expected = [
  7711. 'div' => ['class' => 'input textarea required'],
  7712. 'label' => ['for' => '0-comments-1-comment'],
  7713. 'Comment',
  7714. '/label',
  7715. 'textarea' => [
  7716. 'name',
  7717. 'required' => 'required',
  7718. 'id' => '0-comments-1-comment',
  7719. 'rows' => 5,
  7720. ],
  7721. '/textarea',
  7722. '/div'
  7723. ];
  7724. //@codingStandardsIgnoreEnd
  7725. $this->assertHtml($expected, $result);
  7726. }
  7727. /**
  7728. * testHtml5Controls method
  7729. *
  7730. * Test that some html5 inputs + FormHelper::__call() work.
  7731. *
  7732. * @return void
  7733. */
  7734. public function testHtml5Controls()
  7735. {
  7736. $result = $this->Form->email('User.email');
  7737. $expected = [
  7738. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7739. ];
  7740. $this->assertHtml($expected, $result);
  7741. $result = $this->Form->search('User.query');
  7742. $expected = [
  7743. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7744. ];
  7745. $this->assertHtml($expected, $result);
  7746. $result = $this->Form->search('User.query', ['value' => 'test']);
  7747. $expected = [
  7748. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7749. ];
  7750. $this->assertHtml($expected, $result);
  7751. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7752. $expected = [
  7753. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7754. ];
  7755. $this->assertHtml($expected, $result);
  7756. }
  7757. /**
  7758. * testHtml5ControlWithControl method
  7759. *
  7760. * Test accessing html5 inputs through control().
  7761. *
  7762. * @return void
  7763. */
  7764. public function testHtml5ControlWithControl()
  7765. {
  7766. $this->Form->create();
  7767. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7768. $result = $this->Form->control('website', [
  7769. 'type' => 'url',
  7770. 'val' => 'http://domain.tld',
  7771. 'label' => false
  7772. ]);
  7773. $expected = [
  7774. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7775. ];
  7776. $this->assertHtml($expected, $result);
  7777. }
  7778. /**
  7779. * testHtml5ControlException method
  7780. *
  7781. * Test errors when field name is missing.
  7782. *
  7783. * @return void
  7784. */
  7785. public function testHtml5ControlException()
  7786. {
  7787. $this->expectException(\Cake\Core\Exception\Exception::class);
  7788. $this->Form->email();
  7789. }
  7790. /**
  7791. * tests fields that are required use custom validation messages
  7792. *
  7793. * @return void
  7794. */
  7795. public function testHtml5ErrorMessage()
  7796. {
  7797. $this->Form->setConfig('useValidationMessages', true);
  7798. $validator = (new \Cake\Validation\Validator())
  7799. ->requirePresence('email', true, 'Custom error message')
  7800. ->requirePresence('password')
  7801. ->alphaNumeric('password')
  7802. ->notBlank('phone');
  7803. $table = $this->getTableLocator()->get('Contacts', [
  7804. 'className' => __NAMESPACE__ . '\ContactsTable'
  7805. ]);
  7806. $table->setValidator('default', $validator);
  7807. $contact = new Entity();
  7808. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7809. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7810. $result = $this->Form->control('password');
  7811. $expected = [
  7812. 'label' => ['for' => 'password'],
  7813. 'Password',
  7814. '/label',
  7815. 'input' => [
  7816. 'id' => 'password',
  7817. 'name' => 'password',
  7818. 'type' => 'password',
  7819. 'value' => '',
  7820. 'required' => 'required',
  7821. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7822. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  7823. ]
  7824. ];
  7825. $this->assertHtml($expected, $result);
  7826. $result = $this->Form->control('phone');
  7827. $expected = [
  7828. 'label' => ['for' => 'phone'],
  7829. 'Phone',
  7830. '/label',
  7831. 'input' => [
  7832. 'id' => 'phone',
  7833. 'name' => 'phone',
  7834. 'type' => 'tel',
  7835. 'value' => '',
  7836. 'maxlength' => 255,
  7837. 'required' => 'required',
  7838. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7839. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  7840. ]
  7841. ];
  7842. $this->assertHtml($expected, $result);
  7843. $result = $this->Form->control('email');
  7844. $expected = [
  7845. 'label' => ['for' => 'email'],
  7846. 'Email',
  7847. '/label',
  7848. 'input' => [
  7849. 'id' => 'email',
  7850. 'name' => 'email',
  7851. 'type' => 'email',
  7852. 'value' => '',
  7853. 'maxlength' => 255,
  7854. 'required' => 'required',
  7855. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7856. 'oninvalid' => 'this.setCustomValidity(&#039;Custom error message&#039;)',
  7857. ]
  7858. ];
  7859. $this->assertHtml($expected, $result);
  7860. }
  7861. /**
  7862. * testRequiredAttribute method
  7863. *
  7864. * Tests that formhelper sets required attributes.
  7865. *
  7866. * @return void
  7867. */
  7868. public function testRequiredAttribute()
  7869. {
  7870. $this->article['required'] = [
  7871. 'title' => true,
  7872. 'body' => false,
  7873. ];
  7874. $this->Form->create($this->article);
  7875. $result = $this->Form->control('title');
  7876. $expected = [
  7877. 'div' => ['class' => 'input text required'],
  7878. 'label' => ['for' => 'title'],
  7879. 'Title',
  7880. '/label',
  7881. 'input' => [
  7882. 'type' => 'text',
  7883. 'name' => 'title',
  7884. 'id' => 'title',
  7885. 'required' => 'required',
  7886. ],
  7887. '/div'
  7888. ];
  7889. $this->assertHtml($expected, $result);
  7890. $result = $this->Form->control('title', ['required' => false]);
  7891. $this->assertNotContains('required', $result);
  7892. $result = $this->Form->control('body');
  7893. $expected = [
  7894. 'div' => ['class' => 'input text'],
  7895. 'label' => ['for' => 'body'],
  7896. 'Body',
  7897. '/label',
  7898. 'input' => [
  7899. 'type' => 'text',
  7900. 'name' => 'body',
  7901. 'id' => 'body',
  7902. ],
  7903. '/div'
  7904. ];
  7905. $this->assertHtml($expected, $result);
  7906. $result = $this->Form->control('body', ['required' => true]);
  7907. $this->assertContains('required', $result);
  7908. }
  7909. /**
  7910. * testControlsNotNested method
  7911. *
  7912. * Tests that it is possible to put inputs outside of the label.
  7913. *
  7914. * @return void
  7915. */
  7916. public function testControlsNotNested()
  7917. {
  7918. $this->Form->setTemplates([
  7919. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7920. 'formGroup' => '{{input}}{{label}}',
  7921. ]);
  7922. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7923. $expected = [
  7924. 'div' => ['class' => 'input checkbox'],
  7925. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7926. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7927. 'label' => ['for' => 'foo'],
  7928. 'Foo',
  7929. '/label',
  7930. '/div'
  7931. ];
  7932. $this->assertHtml($expected, $result);
  7933. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7934. $expected = [
  7935. 'div' => ['class' => 'input checkbox'],
  7936. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7937. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7938. '/div'
  7939. ];
  7940. $this->assertHtml($expected, $result);
  7941. $result = $this->Form->control('confirm', [
  7942. 'type' => 'radio',
  7943. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7944. ]);
  7945. $expected = [
  7946. 'div' => ['class' => 'input radio'],
  7947. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7948. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7949. ['label' => ['for' => 'confirm-y']],
  7950. 'Yes',
  7951. '/label',
  7952. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7953. ['label' => ['for' => 'confirm-n']],
  7954. 'No',
  7955. '/label',
  7956. '<label',
  7957. 'Confirm',
  7958. '/label',
  7959. '/div',
  7960. ];
  7961. $this->assertHtml($expected, $result);
  7962. $result = $this->Form->select('category', ['1', '2'], [
  7963. 'multiple' => 'checkbox',
  7964. 'name' => 'fish',
  7965. ]);
  7966. $expected = [
  7967. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7968. ['div' => ['class' => 'checkbox']],
  7969. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7970. ['label' => ['for' => 'fish-0']],
  7971. '1',
  7972. '/label',
  7973. '/div',
  7974. ['div' => ['class' => 'checkbox']],
  7975. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7976. ['label' => ['for' => 'fish-1']],
  7977. '2',
  7978. '/label',
  7979. '/div'
  7980. ];
  7981. $this->assertHtml($expected, $result);
  7982. }
  7983. /**
  7984. * testControlContainerTemplates method
  7985. *
  7986. * Test that *Container templates are used by input.
  7987. *
  7988. * @return void
  7989. */
  7990. public function testControlContainerTemplates()
  7991. {
  7992. $this->Form->setTemplates([
  7993. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7994. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7995. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7996. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7997. ]);
  7998. $this->article['errors'] = [
  7999. 'Article' => ['published' => 'error message']
  8000. ];
  8001. $this->Form->create($this->article);
  8002. $result = $this->Form->control('accept', [
  8003. 'type' => 'checkbox'
  8004. ]);
  8005. $expected = [
  8006. 'div' => ['class' => 'check'],
  8007. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  8008. 'label' => ['for' => 'accept'],
  8009. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  8010. 'Accept',
  8011. '/label',
  8012. '/div'
  8013. ];
  8014. $this->assertHtml($expected, $result);
  8015. $result = $this->Form->control('accept', [
  8016. 'type' => 'radio',
  8017. 'options' => ['Y', 'N']
  8018. ]);
  8019. $this->assertContains('<div class="rad">', $result);
  8020. $result = $this->Form->control('Article.published', [
  8021. 'type' => 'radio',
  8022. 'options' => ['Y', 'N']
  8023. ]);
  8024. $this->assertContains('<div class="rad err">', $result);
  8025. $result = $this->Form->control('Article.created', [
  8026. 'type' => 'datetime',
  8027. ]);
  8028. $this->assertContains('<div class="dt">', $result);
  8029. }
  8030. /**
  8031. * testFormGroupTemplates method
  8032. *
  8033. * Test that *Container templates are used by input.
  8034. *
  8035. * @return void
  8036. */
  8037. public function testFormGroupTemplates()
  8038. {
  8039. $this->Form->setTemplates([
  8040. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  8041. ]);
  8042. $this->Form->create($this->article);
  8043. $result = $this->Form->control('accept', [
  8044. 'type' => 'radio',
  8045. 'options' => ['Y', 'N']
  8046. ]);
  8047. $this->assertContains('<div class="radio">', $result);
  8048. }
  8049. /**
  8050. * testResetTemplates method
  8051. *
  8052. * Test resetting templates.
  8053. *
  8054. * @return void
  8055. */
  8056. public function testResetTemplates()
  8057. {
  8058. $this->Form->setTemplates(['input' => '<input/>']);
  8059. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  8060. $this->Form->resetTemplates();
  8061. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  8062. }
  8063. /**
  8064. * testContext method
  8065. *
  8066. * Test the context method.
  8067. *
  8068. * @return void
  8069. */
  8070. public function testContext()
  8071. {
  8072. $result = $this->Form->context();
  8073. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  8074. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  8075. $this->assertSame($mock, $this->Form->context($mock));
  8076. $this->assertSame($mock, $this->Form->context());
  8077. }
  8078. /**
  8079. * testAutoDomId method
  8080. *
  8081. * @return void
  8082. */
  8083. public function testAutoDomId()
  8084. {
  8085. $result = $this->Form->text('field', ['id' => true]);
  8086. $expected = [
  8087. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  8088. ];
  8089. $this->assertHtml($expected, $result);
  8090. // Ensure id => doesn't cause problem when multiple inputs are generated.
  8091. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  8092. $expected = [
  8093. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  8094. ['label' => ['for' => 'field-0']],
  8095. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  8096. 'option A',
  8097. '/label',
  8098. ['label' => ['for' => 'field-1']],
  8099. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  8100. 'option B',
  8101. '/label',
  8102. ];
  8103. $this->assertHtml($expected, $result);
  8104. $result = $this->Form->select(
  8105. 'multi_field',
  8106. ['first', 'second'],
  8107. ['multiple' => 'checkbox', 'id' => true]
  8108. );
  8109. $expected = [
  8110. 'input' => [
  8111. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  8112. ],
  8113. ['div' => ['class' => 'checkbox']],
  8114. ['label' => ['for' => 'multi-field-0']],
  8115. ['input' => [
  8116. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8117. 'value' => '0', 'id' => 'multi-field-0'
  8118. ]],
  8119. 'first',
  8120. '/label',
  8121. '/div',
  8122. ['div' => ['class' => 'checkbox']],
  8123. ['label' => ['for' => 'multi-field-1']],
  8124. ['input' => [
  8125. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8126. 'value' => '1', 'id' => 'multi-field-1'
  8127. ]],
  8128. 'second',
  8129. '/label',
  8130. '/div',
  8131. ];
  8132. $this->assertHtml($expected, $result);
  8133. }
  8134. /**
  8135. * Test the basic setters and getters for value sources
  8136. *
  8137. * @return void
  8138. */
  8139. public function testFormValueSourcesSettersGetters()
  8140. {
  8141. $this->Form->request = $this->Form->request
  8142. ->withData('id', '1')
  8143. ->withQueryParams(['id' => '2']);
  8144. $expected = ['context'];
  8145. $result = $this->Form->getValueSources();
  8146. $this->assertEquals($expected, $result);
  8147. $expected = ['query', 'data', 'context'];
  8148. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8149. $result = $this->Form->getValueSources();
  8150. $this->assertEquals($expected, $result);
  8151. $this->Form->setValueSources(['context']);
  8152. $expected = '1';
  8153. $result = $this->Form->getSourceValue('id');
  8154. $this->assertEquals($expected, $result);
  8155. $this->Form->setValueSources('query');
  8156. $expected = ['query'];
  8157. $result = $this->Form->getValueSources();
  8158. $this->assertEquals($expected, $result);
  8159. $expected = '2';
  8160. $result = $this->Form->getSourceValue('id');
  8161. $this->assertEquals($expected, $result);
  8162. $this->Form->setValueSources(['data']);
  8163. $expected = '1';
  8164. $result = $this->Form->getSourceValue('id');
  8165. $this->assertEquals($expected, $result);
  8166. $this->Form->setValueSources(['query', 'data']);
  8167. $expected = '2';
  8168. $result = $this->Form->getSourceValue('id');
  8169. $this->assertEquals($expected, $result);
  8170. }
  8171. /**
  8172. * Tests the different input rendering values based on sources values switching
  8173. *
  8174. * @return void
  8175. */
  8176. public function testFormValueSourcesSingleSwitchRendering()
  8177. {
  8178. $this->loadFixtures('Articles');
  8179. $articles = $this->getTableLocator()->get('Articles');
  8180. $article = new Article();
  8181. $articles->patchEntity($article, ['id' => '3']);
  8182. $this->Form->create($article);
  8183. $this->Form->setValueSources(['context']);
  8184. $result = $this->Form->control('id');
  8185. $expected = [
  8186. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8187. ];
  8188. $this->assertHtml($expected, $result);
  8189. $this->Form->request = $this->Form->request->withQueryParams(['id' => 5]);
  8190. $this->Form->setValueSources(['query']);
  8191. $result = $this->Form->control('id');
  8192. $expected = [
  8193. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8194. ];
  8195. $this->assertHtml($expected, $result);
  8196. $this->Form->request = $this->Form->request
  8197. ->withQueryParams(['id' => '5a'])
  8198. ->withData('id', '5b');
  8199. $this->Form->setValueSources(['context']);
  8200. $this->Form->create($article);
  8201. $result = $this->Form->control('id');
  8202. $expected = [
  8203. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8204. ];
  8205. $this->assertHtml($expected, $result);
  8206. $this->Form->setValueSources(['data']);
  8207. $this->Form->create($article);
  8208. $result = $this->Form->control('id');
  8209. $expected = [
  8210. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8211. ];
  8212. $this->assertHtml($expected, $result);
  8213. $this->Form->setValueSources(['query']);
  8214. $result = $this->Form->control('id');
  8215. $expected = [
  8216. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8217. ];
  8218. $this->assertHtml($expected, $result);
  8219. }
  8220. /**
  8221. * Tests the different input rendering values based on sources values switching while supplying
  8222. * an entity (base context) and multiple sources (such as data, query)
  8223. *
  8224. * @return void
  8225. */
  8226. public function testFormValueSourcesListSwitchRendering()
  8227. {
  8228. $this->loadFixtures('Articles');
  8229. $articles = $this->getTableLocator()->get('Articles');
  8230. $article = new Article();
  8231. $articles->patchEntity($article, ['id' => '3']);
  8232. $this->Form->request = $this->Form->request->withQueryParams(['id' => '9']);
  8233. $this->Form->create($article);
  8234. $this->Form->setValueSources(['context', 'query']);
  8235. $result = $this->Form->control('id');
  8236. $expected = [
  8237. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8238. ];
  8239. $this->assertHtml($expected, $result);
  8240. $this->Form->setValueSources(['query', 'context']);
  8241. $result = $this->Form->control('id');
  8242. $expected = [
  8243. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8244. ];
  8245. $this->assertHtml($expected, $result);
  8246. $this->Form->setValueSources(['data', 'query', 'context']);
  8247. $result = $this->Form->control('id');
  8248. $expected = [
  8249. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8250. ];
  8251. $this->assertHtml($expected, $result);
  8252. $this->Form->request = $this->Form->request
  8253. ->withData('id', '8')
  8254. ->withQueryParams(['id' => '9']);
  8255. $this->Form->setValueSources(['data', 'query', 'context']);
  8256. $result = $this->Form->control('id');
  8257. $expected = [
  8258. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8259. ];
  8260. $this->assertHtml($expected, $result);
  8261. }
  8262. /**
  8263. * Test the different form input renderings based on values sources switchings through form options
  8264. *
  8265. * @return void
  8266. */
  8267. public function testFormValueSourcesSwitchViaOptionsRendering()
  8268. {
  8269. $this->loadFixtures('Articles');
  8270. $articles = $this->getTableLocator()->get('Articles');
  8271. $article = new Article();
  8272. $articles->patchEntity($article, ['id' => '3']);
  8273. $this->Form->request = $this->Form->request->withData('id', 4)->withQueryParams(['id' => 5]);
  8274. $this->Form->create($article, ['valueSources' => 'query']);
  8275. $result = $this->Form->control('id');
  8276. $expected = [
  8277. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8278. ];
  8279. $this->assertHtml($expected, $result);
  8280. $result = $this->Form->getSourceValue('id');
  8281. $this->assertEquals('5', $result);
  8282. $this->Form->setValueSources(['context']);
  8283. $this->Form->create($article, ['valueSources' => 'query']);
  8284. $result = $this->Form->control('id');
  8285. $expected = [
  8286. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8287. ];
  8288. $this->assertHtml($expected, $result);
  8289. $result = $this->Form->getSourceValue('id');
  8290. $this->assertEquals('5', $result);
  8291. $this->Form->setValueSources(['query']);
  8292. $this->Form->create($article, ['valueSources' => 'data']);
  8293. $result = $this->Form->control('id');
  8294. $expected = [
  8295. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8296. ];
  8297. $this->assertHtml($expected, $result);
  8298. $result = $this->Form->getSourceValue('id');
  8299. $this->assertEquals('4', $result);
  8300. $this->Form->setValueSources(['query']);
  8301. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8302. $result = $this->Form->control('id');
  8303. $expected = [
  8304. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8305. ];
  8306. $this->assertHtml($expected, $result);
  8307. $result = $this->Form->getSourceValue('id');
  8308. $this->assertEquals('4', $result);
  8309. }
  8310. /**
  8311. * Test the different form input renderings based on values sources switchings through form options
  8312. *
  8313. * @return void
  8314. */
  8315. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8316. {
  8317. $this->loadFixtures('Articles');
  8318. $articles = $this->getTableLocator()->get('Articles');
  8319. $article = new Article();
  8320. $articles->patchEntity($article, ['id' => '3']);
  8321. $this->Form->request = $this->Form->request->withData('id', 10)->withQueryParams(['id' => 11]);
  8322. $this->Form->setValueSources(['context'])
  8323. ->create($article, ['valueSources' => ['query', 'data']]);
  8324. $result = $this->Form->control('id');
  8325. $expected = [
  8326. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8327. ];
  8328. $this->assertHtml($expected, $result);
  8329. $result = $this->Form->getSourceValue('id');
  8330. $this->assertEquals('11', $result);
  8331. $this->Form->request = $this->Form->request->withQueryParams([]);
  8332. $this->Form->setValueSources(['context'])
  8333. ->create($article, ['valueSources' => ['query', 'data']]);
  8334. $result = $this->Form->control('id');
  8335. $expected = [
  8336. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8337. ];
  8338. $this->assertHtml($expected, $result);
  8339. $result = $this->Form->getSourceValue('id');
  8340. $this->assertEquals('10', $result);
  8341. }
  8342. /**
  8343. * Test the different form values sources resetting through From::end();
  8344. *
  8345. * @return void
  8346. */
  8347. public function testFormValueSourcesResetViaEnd()
  8348. {
  8349. $expected = ['context'];
  8350. $result = $this->Form->getValueSources();
  8351. $this->assertEquals($expected, $result);
  8352. $expected = ['query', 'context', 'data'];
  8353. $this->Form->setValueSources(['query', 'context', 'data']);
  8354. $result = $this->Form->getValueSources();
  8355. $this->assertEquals($expected, $result);
  8356. $this->Form->create();
  8357. $result = $this->Form->getValueSources();
  8358. $this->assertEquals($expected, $result);
  8359. $this->Form->end();
  8360. $result = $this->Form->getValueSources();
  8361. $this->assertEquals(['context'], $result);
  8362. }
  8363. /**
  8364. * Test sources values defaults handling
  8365. *
  8366. * @return void
  8367. */
  8368. public function testFormValueSourcesDefaults()
  8369. {
  8370. $this->Form->request = $this->Form->request->withQueryParams(['password' => 'open Sesame']);
  8371. $this->Form->create();
  8372. $result = $this->Form->password('password');
  8373. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8374. $this->assertHtml($expected, $result);
  8375. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8376. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8377. $this->assertHtml($expected, $result);
  8378. $this->Form->setValueSources('query');
  8379. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8380. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8381. $this->assertHtml($expected, $result);
  8382. $this->Form->setValueSources('data');
  8383. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8384. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8385. $this->assertHtml($expected, $result);
  8386. }
  8387. /**
  8388. * Test sources values schema defaults handling
  8389. *
  8390. * @return void
  8391. */
  8392. public function testSourcesValueDoesntExistPassThrough()
  8393. {
  8394. $this->Form->request = $this->Form->request->withQueryParams(['category' => 'sesame-cookies']);
  8395. $articles = $this->getTableLocator()->get('Articles');
  8396. $entity = $articles->newEntity();
  8397. $this->Form->create($entity);
  8398. $this->Form->setValueSources(['query', 'context']);
  8399. $result = $this->Form->getSourceValue('category');
  8400. $this->assertEquals('sesame-cookies', $result);
  8401. $this->Form->setValueSources(['context', 'query']);
  8402. $result = $this->Form->getSourceValue('category');
  8403. $this->assertEquals('sesame-cookies', $result);
  8404. }
  8405. /**
  8406. * testNestedLabelInput method
  8407. *
  8408. * Test the `nestedInput` parameter
  8409. *
  8410. * @group deprecated
  8411. * @return void
  8412. */
  8413. public function testNestedLabelInput()
  8414. {
  8415. $this->deprecated(function () {
  8416. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8417. $expected = [
  8418. 'div' => ['class' => 'input text'],
  8419. 'label' => ['for' => 'foo'],
  8420. ['input' => [
  8421. 'type' => 'text',
  8422. 'name' => 'foo',
  8423. 'id' => 'foo'
  8424. ]],
  8425. 'Foo',
  8426. '/label',
  8427. '/div'
  8428. ];
  8429. $this->assertHtml($expected, $result);
  8430. });
  8431. }
  8432. /**
  8433. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8434. *
  8435. * This test makes sure `false` excludes the label from the render
  8436. *
  8437. * @return void
  8438. */
  8439. public function testControlLabelManipulationDisableLabels()
  8440. {
  8441. $result = $this->Form->control('test', [
  8442. 'type' => 'radio',
  8443. 'options' => ['A', 'B'],
  8444. 'labelOptions' => false
  8445. ]);
  8446. $expected = [
  8447. ['div' => ['class' => 'input radio']],
  8448. '<label',
  8449. 'Test',
  8450. '/label',
  8451. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8452. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8453. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8454. '/div',
  8455. ];
  8456. $this->assertHtml($expected, $result);
  8457. $result = $this->Form->control('checkbox1', [
  8458. 'label' => 'My checkboxes',
  8459. 'multiple' => 'checkbox',
  8460. 'type' => 'select',
  8461. 'options' => [
  8462. ['text' => 'First Checkbox', 'value' => 1],
  8463. ['text' => 'Second Checkbox', 'value' => 2]
  8464. ],
  8465. 'labelOptions' => false
  8466. ]);
  8467. $expected = [
  8468. ['div' => ['class' => 'input select']],
  8469. ['label' => ['for' => 'checkbox1']],
  8470. 'My checkboxes',
  8471. '/label',
  8472. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8473. ['div' => ['class' => 'checkbox']],
  8474. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8475. '/div',
  8476. ['div' => ['class' => 'checkbox']],
  8477. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8478. '/div',
  8479. '/div'
  8480. ];
  8481. $this->assertHtml($expected, $result);
  8482. }
  8483. /**
  8484. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8485. *
  8486. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8487. * added to the class if checked.
  8488. *
  8489. * Also checks to make sure any custom attributes are rendered correctly
  8490. *
  8491. * @return void
  8492. */
  8493. public function testControlLabelManipulationRadios()
  8494. {
  8495. $result = $this->Form->control('test', [
  8496. 'type' => 'radio',
  8497. 'options' => ['A', 'B'],
  8498. 'labelOptions' => ['class' => 'custom-class']
  8499. ]);
  8500. $expected = [
  8501. ['div' => ['class' => 'input radio']],
  8502. '<label',
  8503. 'Test',
  8504. '/label',
  8505. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8506. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8507. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8508. 'A',
  8509. '/label',
  8510. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8511. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8512. 'B',
  8513. '/label',
  8514. '/div',
  8515. ];
  8516. $this->assertHtml($expected, $result);
  8517. $result = $this->Form->control('test', [
  8518. 'type' => 'radio',
  8519. 'options' => ['A', 'B'],
  8520. 'value' => 1,
  8521. 'labelOptions' => ['class' => 'custom-class']
  8522. ]);
  8523. $expected = [
  8524. ['div' => ['class' => 'input radio']],
  8525. '<label',
  8526. 'Test',
  8527. '/label',
  8528. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8529. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8530. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8531. 'A',
  8532. '/label',
  8533. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8534. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8535. 'B',
  8536. '/label',
  8537. '/div',
  8538. ];
  8539. $this->assertHtml($expected, $result);
  8540. $result = $this->Form->control('test', [
  8541. 'type' => 'radio',
  8542. 'options' => ['A', 'B'],
  8543. 'value' => 1,
  8544. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8545. ]);
  8546. $expected = [
  8547. ['div' => ['class' => 'input radio']],
  8548. '<label',
  8549. 'Test',
  8550. '/label',
  8551. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8552. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8553. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8554. 'A',
  8555. '/label',
  8556. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8557. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8558. 'B',
  8559. '/label',
  8560. '/div',
  8561. ];
  8562. $this->assertHtml($expected, $result);
  8563. $result = $this->Form->radio('test', ['A', 'B'], [
  8564. 'label' => [
  8565. 'class' => ['custom-class', 'another-class'],
  8566. 'data-name' => 'bob'
  8567. ],
  8568. 'value' => 1
  8569. ]);
  8570. $expected = [
  8571. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8572. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8573. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8574. 'A',
  8575. '/label',
  8576. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8577. ['input' => [
  8578. 'type' => 'radio',
  8579. 'name' => 'test',
  8580. 'value' => '1',
  8581. 'id' => 'test-1',
  8582. 'checked' => 'checked'
  8583. ]],
  8584. 'B',
  8585. '/label',
  8586. ];
  8587. $this->assertHtml($expected, $result);
  8588. }
  8589. /**
  8590. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8591. *
  8592. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8593. * added to the class if checked.
  8594. *
  8595. * Also checks to make sure any custom attributes are rendered correctly
  8596. *
  8597. * @return void
  8598. */
  8599. public function testControlLabelManipulationCheckboxes()
  8600. {
  8601. $result = $this->Form->control('checkbox1', [
  8602. 'label' => 'My checkboxes',
  8603. 'multiple' => 'checkbox',
  8604. 'type' => 'select',
  8605. 'options' => [
  8606. ['text' => 'First Checkbox', 'value' => 1],
  8607. ['text' => 'Second Checkbox', 'value' => 2]
  8608. ],
  8609. 'labelOptions' => ['class' => 'custom-class'],
  8610. 'value' => ['1']
  8611. ]);
  8612. $expected = [
  8613. ['div' => ['class' => 'input select']],
  8614. ['label' => ['for' => 'checkbox1']],
  8615. 'My checkboxes',
  8616. '/label',
  8617. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8618. ['div' => ['class' => 'checkbox']],
  8619. ['label' => [
  8620. 'class' => 'custom-class selected',
  8621. 'for' => 'checkbox1-1'
  8622. ]],
  8623. ['input' => [
  8624. 'type' => 'checkbox',
  8625. 'name' => 'checkbox1[]',
  8626. 'value' => '1',
  8627. 'id' => 'checkbox1-1',
  8628. 'checked' => 'checked'
  8629. ]],
  8630. 'First Checkbox',
  8631. '/label',
  8632. '/div',
  8633. ['div' => ['class' => 'checkbox']],
  8634. ['label' => [
  8635. 'class' => 'custom-class',
  8636. 'for' => 'checkbox1-2'
  8637. ]],
  8638. ['input' => [
  8639. 'type' => 'checkbox',
  8640. 'name' => 'checkbox1[]',
  8641. 'value' => '2',
  8642. 'id' => 'checkbox1-2'
  8643. ]],
  8644. 'Second Checkbox',
  8645. '/label',
  8646. '/div',
  8647. '/div'
  8648. ];
  8649. $this->assertHtml($expected, $result);
  8650. $result = $this->Form->control('checkbox1', [
  8651. 'label' => 'My checkboxes',
  8652. 'multiple' => 'checkbox',
  8653. 'type' => 'select',
  8654. 'options' => [
  8655. ['text' => 'First Checkbox', 'value' => 1],
  8656. ['text' => 'Second Checkbox', 'value' => 2]
  8657. ],
  8658. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8659. 'value' => ['1']
  8660. ]);
  8661. $expected = [
  8662. ['div' => ['class' => 'input select']],
  8663. ['label' => ['for' => 'checkbox1']],
  8664. 'My checkboxes',
  8665. '/label',
  8666. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8667. ['div' => ['class' => 'checkbox']],
  8668. ['label' => [
  8669. 'class' => 'custom-class another-class selected',
  8670. 'data-name' => 'bob',
  8671. 'for' => 'checkbox1-1'
  8672. ]],
  8673. ['input' => [
  8674. 'type' => 'checkbox',
  8675. 'name' => 'checkbox1[]',
  8676. 'value' => '1',
  8677. 'id' => 'checkbox1-1',
  8678. 'checked' => 'checked'
  8679. ]],
  8680. 'First Checkbox',
  8681. '/label',
  8682. '/div',
  8683. ['div' => ['class' => 'checkbox']],
  8684. ['label' => [
  8685. 'class' => 'custom-class another-class',
  8686. 'data-name' => 'bob',
  8687. 'for' => 'checkbox1-2'
  8688. ]],
  8689. ['input' => [
  8690. 'type' => 'checkbox',
  8691. 'name' => 'checkbox1[]',
  8692. 'value' => '2',
  8693. 'id' => 'checkbox1-2'
  8694. ]],
  8695. 'Second Checkbox',
  8696. '/label',
  8697. '/div',
  8698. '/div'
  8699. ];
  8700. $this->assertHtml($expected, $result);
  8701. }
  8702. }