FormHelperTest.php 308 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318
  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\ORM\TableRegistry;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Security;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\View;
  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. TableRegistry::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. $registry = $helper->widgetRegistry();
  211. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  212. }
  213. /**
  214. * Test that when specifying custom widgets config file and it should be
  215. * added to widgets array. WidgetRegistry 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. $registry = $helper->widgetRegistry();
  223. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  224. }
  225. /**
  226. * Test registering a new widget class and rendering it.
  227. *
  228. * @return void
  229. */
  230. public function testAddWidgetAndRenderWidget()
  231. {
  232. $data = [
  233. 'val' => 1
  234. ];
  235. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  236. $this->Form->addWidget('test', $mock);
  237. $mock->expects($this->once())
  238. ->method('render')
  239. ->with($data)
  240. ->will($this->returnValue('HTML'));
  241. $result = $this->Form->widget('test', $data);
  242. $this->assertEquals('HTML', $result);
  243. }
  244. /**
  245. * Test that secureFields() of widget is called after calling render(),
  246. * not before.
  247. *
  248. * @return void
  249. */
  250. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  251. {
  252. $data = [
  253. 'val' => 1,
  254. 'name' => 'test'
  255. ];
  256. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  257. $this->Form->addWidget('test', $mock);
  258. $mock->expects($this->at(0))
  259. ->method('render')
  260. ->with($data)
  261. ->will($this->returnValue('HTML'));
  262. $mock->expects($this->at(1))
  263. ->method('secureFields')
  264. ->with($data)
  265. ->will($this->returnValue(['test']));
  266. $result = $this->Form->widget('test', $data + ['secure' => true]);
  267. $this->assertEquals('HTML', $result);
  268. }
  269. /**
  270. * Test that empty string is not added to secure fields list when
  271. * rendering input widget without name.
  272. *
  273. * @return void
  274. */
  275. public function testRenderingWidgetWithEmptyName()
  276. {
  277. $this->assertEquals([], $this->Form->fields);
  278. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  279. $this->assertEquals('<select name=""></select>', $result);
  280. $this->assertEquals([], $this->Form->fields);
  281. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  282. $this->assertEquals('<select name="0"></select>', $result);
  283. $this->assertEquals(['0'], $this->Form->fields);
  284. }
  285. /**
  286. * Test registering an invalid widget class.
  287. *
  288. * @return void
  289. */
  290. public function testAddWidgetInvalid()
  291. {
  292. $this->expectException(\RuntimeException::class);
  293. $mock = new \StdClass();
  294. $this->Form->addWidget('test', $mock);
  295. $this->Form->widget('test');
  296. }
  297. /**
  298. * Test adding a new context class.
  299. *
  300. * @return void
  301. */
  302. public function testAddContextProvider()
  303. {
  304. $context = 'My data';
  305. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  306. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  307. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  308. $this->assertEquals($context, $data['entity']);
  309. return $stub;
  310. });
  311. $this->Form->create($context);
  312. $result = $this->Form->context();
  313. $this->assertSame($stub, $result);
  314. }
  315. /**
  316. * Test replacing a context class.
  317. *
  318. * @return void
  319. */
  320. public function testAddContextProviderReplace()
  321. {
  322. $entity = new Article();
  323. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  324. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  325. return $stub;
  326. });
  327. $this->Form->create($entity);
  328. $result = $this->Form->context();
  329. $this->assertSame($stub, $result);
  330. }
  331. /**
  332. * Test overriding a context class.
  333. *
  334. * @return void
  335. */
  336. public function testAddContextProviderAdd()
  337. {
  338. $entity = new Article();
  339. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  340. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  341. if ($data['entity'] instanceof Entity) {
  342. return $stub;
  343. }
  344. });
  345. $this->Form->create($entity);
  346. $result = $this->Form->context();
  347. $this->assertSame($stub, $result);
  348. }
  349. /**
  350. * Test adding an invalid context class.
  351. *
  352. * @return void
  353. */
  354. public function testAddContextProviderInvalid()
  355. {
  356. $this->expectException(\RuntimeException::class);
  357. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  358. $context = 'My data';
  359. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  360. return new \StdClass();
  361. });
  362. $this->Form->create($context);
  363. }
  364. /**
  365. * Provides context options for create().
  366. *
  367. * @return array
  368. */
  369. public function contextSelectionProvider()
  370. {
  371. $entity = new Article();
  372. $collection = new Collection([$entity]);
  373. $emptyCollection = new Collection([]);
  374. $arrayObject = new \ArrayObject([]);
  375. $data = [
  376. 'schema' => [
  377. 'title' => ['type' => 'string']
  378. ]
  379. ];
  380. $form = new Form();
  381. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  382. return [
  383. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  384. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  385. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  386. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  387. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  388. 'form' => [$form, 'Cake\View\Form\FormContext'],
  389. 'none' => [null, 'Cake\View\Form\NullContext'],
  390. 'false' => [false, 'Cake\View\Form\NullContext'],
  391. 'custom' => [$custom, get_class($custom)]
  392. ];
  393. }
  394. /**
  395. * Test default context selection in create()
  396. *
  397. * @dataProvider contextSelectionProvider
  398. * @return void
  399. */
  400. public function testCreateContextSelectionBuiltIn($data, $class)
  401. {
  402. $this->loadFixtures('Articles');
  403. $this->Form->create($data);
  404. $this->assertInstanceOf($class, $this->Form->context());
  405. }
  406. /**
  407. * Data provider for type option.
  408. *
  409. * @return array
  410. */
  411. public static function requestTypeProvider()
  412. {
  413. return [
  414. // type, method, override
  415. ['post', 'post', 'POST'],
  416. ['put', 'post', 'PUT'],
  417. ['patch', 'post', 'PATCH'],
  418. ['delete', 'post', 'DELETE'],
  419. ];
  420. }
  421. /**
  422. * Test creating file forms.
  423. *
  424. * @return void
  425. */
  426. public function testCreateFile()
  427. {
  428. $encoding = strtolower(Configure::read('App.encoding'));
  429. $result = $this->Form->create(false, ['type' => 'file']);
  430. $expected = [
  431. 'form' => [
  432. 'method' => 'post', 'action' => '/articles/add',
  433. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  434. ],
  435. 'div' => ['style' => 'display:none;'],
  436. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  437. '/div'
  438. ];
  439. $this->assertHtml($expected, $result);
  440. }
  441. /**
  442. * Test creating GET forms.
  443. *
  444. * @return void
  445. */
  446. public function testCreateGet()
  447. {
  448. $encoding = strtolower(Configure::read('App.encoding'));
  449. $result = $this->Form->create(false, ['type' => 'get']);
  450. $expected = ['form' => [
  451. 'method' => 'get', 'action' => '/articles/add',
  452. 'accept-charset' => $encoding
  453. ]];
  454. $this->assertHtml($expected, $result);
  455. }
  456. /**
  457. * Test explicit method/enctype options.
  458. *
  459. * Explicit method overwrites inferred method from 'type'
  460. *
  461. * @return void
  462. */
  463. public function testCreateExplicitMethodEnctype()
  464. {
  465. $encoding = strtolower(Configure::read('App.encoding'));
  466. $result = $this->Form->create(false, [
  467. 'type' => 'get',
  468. 'method' => 'put',
  469. 'enctype' => 'multipart/form-data'
  470. ]);
  471. $expected = ['form' => [
  472. 'method' => 'put',
  473. 'action' => '/articles/add',
  474. 'enctype' => 'multipart/form-data',
  475. 'accept-charset' => $encoding
  476. ]];
  477. $this->assertHtml($expected, $result);
  478. }
  479. /**
  480. * Test create() with the templates option.
  481. *
  482. * @return void
  483. */
  484. public function testCreateTemplatesArray()
  485. {
  486. $result = $this->Form->create($this->article, [
  487. 'templates' => [
  488. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  489. ]
  490. ]);
  491. $expected = [
  492. 'form' => [
  493. 'class' => 'form-horizontal',
  494. 'method' => 'post',
  495. 'action' => '/articles/add',
  496. 'accept-charset' => 'utf-8'
  497. ]
  498. ];
  499. $this->assertHtml($expected, $result);
  500. }
  501. /**
  502. * Test create() with the templates option.
  503. *
  504. * @return void
  505. */
  506. public function testCreateTemplatesFile()
  507. {
  508. $result = $this->Form->create($this->article, [
  509. 'templates' => 'htmlhelper_tags',
  510. ]);
  511. $expected = [
  512. 'start form',
  513. 'div' => ['class' => 'hidden'],
  514. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  515. '/div'
  516. ];
  517. $this->assertHtml($expected, $result);
  518. }
  519. /**
  520. * Test that create() and end() restore templates.
  521. *
  522. * @return void
  523. */
  524. public function testCreateEndRestoreTemplates()
  525. {
  526. $this->Form->create($this->article, [
  527. 'templates' => ['input' => 'custom input element']
  528. ]);
  529. $this->Form->end();
  530. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  531. }
  532. /**
  533. * Test using template vars in various templates used by control() method.
  534. *
  535. * @return void
  536. */
  537. public function testControlTemplateVars()
  538. {
  539. $result = $this->Form->control('text', [
  540. 'templates' => [
  541. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  542. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  543. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  544. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  545. ],
  546. 'templateVars' => [
  547. 'forinput' => 'in-input',
  548. 'forlabel' => 'in-label',
  549. 'forgroup' => 'in-group',
  550. 'forcontainer' => 'in-container'
  551. ]
  552. ]);
  553. $expected = [
  554. 'div' => ['class'],
  555. 'label' => ['for'],
  556. 'Text in-label',
  557. '/label',
  558. 'in-group',
  559. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  560. 'in-container',
  561. '/div',
  562. ];
  563. $this->assertHtml($expected, $result);
  564. }
  565. /**
  566. * Test ensuring template variables work in template files loaded
  567. * during control().
  568. *
  569. * @return void
  570. */
  571. public function testControlTemplatesFromFile()
  572. {
  573. $result = $this->Form->control('title', [
  574. 'templates' => 'test_templates',
  575. 'templateVars' => [
  576. 'forcontainer' => 'container-data'
  577. ]
  578. ]);
  579. $expected = [
  580. 'div' => ['class'],
  581. 'label' => ['for'],
  582. 'Title',
  583. '/label',
  584. 'input' => ['name', 'type' => 'text', 'id'],
  585. 'container-data',
  586. '/div',
  587. ];
  588. $this->assertHtml($expected, $result);
  589. }
  590. /**
  591. * Test using template vars in inputSubmit and submitContainer template.
  592. *
  593. * @return void
  594. */
  595. public function testSubmitTemplateVars()
  596. {
  597. $this->Form->setTemplates([
  598. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  599. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  600. ]);
  601. $result = $this->Form->submit('Submit', [
  602. 'templateVars' => [
  603. 'forinput' => 'in-input',
  604. 'forcontainer' => 'in-container'
  605. ]
  606. ]);
  607. $expected = [
  608. 'div' => ['class'],
  609. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  610. 'in-container',
  611. '/div',
  612. ];
  613. $this->assertHtml($expected, $result);
  614. }
  615. /**
  616. * test the create() method
  617. *
  618. * @dataProvider requestTypeProvider
  619. * @return void
  620. */
  621. public function testCreateTypeOptions($type, $method, $override)
  622. {
  623. $encoding = strtolower(Configure::read('App.encoding'));
  624. $result = $this->Form->create(false, ['type' => $type]);
  625. $expected = [
  626. 'form' => [
  627. 'method' => $method, 'action' => '/articles/add',
  628. 'accept-charset' => $encoding
  629. ],
  630. 'div' => ['style' => 'display:none;'],
  631. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  632. '/div'
  633. ];
  634. $this->assertHtml($expected, $result);
  635. }
  636. /**
  637. * Test using template vars in Create (formStart template)
  638. *
  639. * @return void
  640. */
  641. public function testCreateTemplateVars()
  642. {
  643. $result = $this->Form->create($this->article, [
  644. 'templates' => [
  645. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  646. ],
  647. 'templateVars' => ['header' => 'headertext']
  648. ]);
  649. $expected = [
  650. 'h4' => ['class'],
  651. 'headertext',
  652. '/h4',
  653. 'form' => [
  654. 'method' => 'post',
  655. 'action' => '/articles/add',
  656. 'accept-charset' => 'utf-8'
  657. ]
  658. ];
  659. $this->assertHtml($expected, $result);
  660. }
  661. /**
  662. * Test opening a form for an update operation.
  663. *
  664. * @return void
  665. */
  666. public function testCreateUpdateForm()
  667. {
  668. $encoding = strtolower(Configure::read('App.encoding'));
  669. $this->Form->request = $this->Form->request
  670. ->withRequestTarget('/articles/edit/1')
  671. ->withParam('action', 'edit');
  672. $this->article['defaults']['id'] = 1;
  673. $result = $this->Form->create($this->article);
  674. $expected = [
  675. 'form' => [
  676. 'method' => 'post',
  677. 'action' => '/articles/edit/1',
  678. 'accept-charset' => $encoding
  679. ],
  680. 'div' => ['style' => 'display:none;'],
  681. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  682. '/div'
  683. ];
  684. $this->assertHtml($expected, $result);
  685. }
  686. /**
  687. * test create() with automatic url generation
  688. *
  689. * @return void
  690. */
  691. public function testCreateAutoUrl()
  692. {
  693. $encoding = strtolower(Configure::read('App.encoding'));
  694. $this->Form->request = $this->Form->request
  695. ->withRequestTarget('/articles/delete/10')
  696. ->withParam('action', 'delete');
  697. $result = $this->Form->create($this->article);
  698. $expected = [
  699. 'form' => [
  700. 'method' => 'post', 'action' => '/articles/delete/10',
  701. 'accept-charset' => $encoding
  702. ],
  703. 'div' => ['style' => 'display:none;'],
  704. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  705. '/div'
  706. ];
  707. $this->assertHtml($expected, $result);
  708. $this->article['defaults'] = ['id' => 1];
  709. $this->Form->request = $this->Form->request
  710. ->withRequestTarget('/articles/edit/1')
  711. ->withParam('action', 'delete');
  712. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  713. $expected = [
  714. 'form' => [
  715. 'method' => 'post',
  716. 'action' => '/articles/edit/1',
  717. 'accept-charset' => $encoding
  718. ],
  719. 'div' => ['style' => 'display:none;'],
  720. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  721. '/div'
  722. ];
  723. $this->assertHtml($expected, $result);
  724. $this->Form->request = $this->Form->request
  725. ->withParam('action', 'add');
  726. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  727. $expected = [
  728. 'form' => [
  729. 'method' => 'post',
  730. 'action' => '/articles/publish/1',
  731. 'accept-charset' => $encoding
  732. ],
  733. 'div' => ['style' => 'display:none;'],
  734. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  735. '/div'
  736. ];
  737. $this->assertHtml($expected, $result);
  738. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  739. $expected = [
  740. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  741. 'div' => ['style' => 'display:none;'],
  742. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  743. '/div'
  744. ];
  745. $this->assertHtml($expected, $result);
  746. $this->Form->request = $this->Form->request
  747. ->withParam('controller', 'Pages');
  748. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  749. $expected = [
  750. 'form' => [
  751. 'method' => 'post', 'action' => '/Pages/signup/1',
  752. 'accept-charset' => $encoding
  753. ],
  754. 'div' => ['style' => 'display:none;'],
  755. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  756. '/div'
  757. ];
  758. $this->assertHtml($expected, $result);
  759. }
  760. /**
  761. * Test create() with no URL (no "action" attribute for <form> tag)
  762. *
  763. * @return void
  764. */
  765. public function testCreateNoUrl()
  766. {
  767. $result = $this->Form->create(false, ['url' => false]);
  768. $expected = [
  769. 'form' => [
  770. 'method' => 'post',
  771. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  772. ],
  773. 'div' => ['style' => 'display:none;'],
  774. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  775. '/div'
  776. ];
  777. $this->assertHtml($expected, $result);
  778. }
  779. /**
  780. * test create() with a custom route
  781. *
  782. * @return void
  783. */
  784. public function testCreateCustomRoute()
  785. {
  786. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  787. $encoding = strtolower(Configure::read('App.encoding'));
  788. $this->Form->request = $this->Form->request
  789. ->withParam('controller', 'users');
  790. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  791. $expected = [
  792. 'form' => [
  793. 'method' => 'post', 'action' => '/login',
  794. 'accept-charset' => $encoding
  795. ],
  796. 'div' => ['style' => 'display:none;'],
  797. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  798. '/div'
  799. ];
  800. $this->assertHtml($expected, $result);
  801. Router::connect(
  802. '/new-article',
  803. ['controller' => 'articles', 'action' => 'myaction'],
  804. ['_name' => 'my-route']
  805. );
  806. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  807. $expected = [
  808. 'form' => [
  809. 'method' => 'post', 'action' => '/new-article',
  810. 'accept-charset' => $encoding,
  811. ],
  812. 'div' => ['style' => 'display:none;'],
  813. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  814. '/div'
  815. ];
  816. $this->assertHtml($expected, $result);
  817. }
  818. /**
  819. * test automatic accept-charset overriding
  820. *
  821. * @return void
  822. */
  823. public function testCreateWithAcceptCharset()
  824. {
  825. $result = $this->Form->create(
  826. $this->article,
  827. [
  828. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  829. ]
  830. );
  831. $expected = [
  832. 'form' => [
  833. 'method' => 'post', 'action' => '/articles',
  834. 'accept-charset' => 'iso-8859-1'
  835. ],
  836. 'div' => ['style' => 'display:none;'],
  837. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  838. '/div'
  839. ];
  840. $this->assertHtml($expected, $result);
  841. }
  842. /**
  843. * Test base form URL when url param is passed with multiple parameters (&)
  844. *
  845. */
  846. public function testCreateQueryStringRequest()
  847. {
  848. $encoding = strtolower(Configure::read('App.encoding'));
  849. $result = $this->Form->create($this->article, [
  850. 'type' => 'post',
  851. 'escape' => false,
  852. 'url' => [
  853. 'controller' => 'controller',
  854. 'action' => 'action',
  855. '?' => ['param1' => 'value1', 'param2' => 'value2']
  856. ]
  857. ]);
  858. $expected = [
  859. 'form' => [
  860. 'method' => 'post',
  861. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  862. 'accept-charset' => $encoding
  863. ],
  864. 'div' => ['style' => 'display:none;'],
  865. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  866. '/div'
  867. ];
  868. $this->assertHtml($expected, $result);
  869. $result = $this->Form->create($this->article, [
  870. 'type' => 'post',
  871. 'url' => [
  872. 'controller' => 'controller',
  873. 'action' => 'action',
  874. '?' => ['param1' => 'value1', 'param2' => 'value2']
  875. ]
  876. ]);
  877. $this->assertHtml($expected, $result);
  878. }
  879. /**
  880. * test that create() doesn't cause errors by multiple id's being in the primary key
  881. * as could happen with multiple select or checkboxes.
  882. *
  883. * @return void
  884. */
  885. public function testCreateWithMultipleIdInData()
  886. {
  887. $encoding = strtolower(Configure::read('App.encoding'));
  888. $this->Form->request = $this->Form->request->withData('Article.id', [1, 2]);
  889. $result = $this->Form->create($this->article);
  890. $expected = [
  891. 'form' => [
  892. 'method' => 'post',
  893. 'action' => '/articles/add',
  894. 'accept-charset' => $encoding
  895. ],
  896. 'div' => ['style' => 'display:none;'],
  897. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  898. '/div'
  899. ];
  900. $this->assertHtml($expected, $result);
  901. }
  902. /**
  903. * test that create() doesn't add in extra passed params.
  904. *
  905. * @return void
  906. */
  907. public function testCreatePassedArgs()
  908. {
  909. $encoding = strtolower(Configure::read('App.encoding'));
  910. $this->Form->request = $this->Form->request->withData('Article.id', 1);
  911. $result = $this->Form->create($this->article, [
  912. 'type' => 'post',
  913. 'escape' => false,
  914. 'url' => [
  915. 'action' => 'edit',
  916. 'myparam'
  917. ]
  918. ]);
  919. $expected = [
  920. 'form' => [
  921. 'method' => 'post',
  922. 'action' => '/articles/edit/myparam',
  923. 'accept-charset' => $encoding
  924. ],
  925. 'div' => ['style' => 'display:none;'],
  926. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  927. '/div'
  928. ];
  929. $this->assertHtml($expected, $result);
  930. }
  931. /**
  932. * test creating a get form, and get form inputs.
  933. *
  934. * @return void
  935. */
  936. public function testGetFormCreate()
  937. {
  938. $encoding = strtolower(Configure::read('App.encoding'));
  939. $result = $this->Form->create($this->article, ['type' => 'get']);
  940. $expected = ['form' => [
  941. 'method' => 'get', 'action' => '/articles/add',
  942. 'accept-charset' => $encoding
  943. ]];
  944. $this->assertHtml($expected, $result);
  945. $result = $this->Form->text('title');
  946. $expected = ['input' => [
  947. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  948. ]];
  949. $this->assertHtml($expected, $result);
  950. $result = $this->Form->password('password');
  951. $expected = ['input' => [
  952. 'name' => 'password', 'type' => 'password'
  953. ]];
  954. $this->assertHtml($expected, $result);
  955. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  956. $result = $this->Form->text('user_form');
  957. $expected = ['input' => [
  958. 'name' => 'user_form', 'type' => 'text'
  959. ]];
  960. $this->assertHtml($expected, $result);
  961. }
  962. /**
  963. * test get form, and inputs when the model param is false
  964. *
  965. * @return void
  966. */
  967. public function testGetFormWithFalseModel()
  968. {
  969. $encoding = strtolower(Configure::read('App.encoding'));
  970. $this->Form->request = $this->Form->request->withParam('controller', 'contact_test');
  971. $result = $this->Form->create(false, [
  972. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  973. ]);
  974. $expected = ['form' => [
  975. 'method' => 'get', 'action' => '/contact_test/add',
  976. 'accept-charset' => $encoding
  977. ]];
  978. $this->assertHtml($expected, $result);
  979. $result = $this->Form->text('reason');
  980. $expected = [
  981. 'input' => ['type' => 'text', 'name' => 'reason']
  982. ];
  983. $this->assertHtml($expected, $result);
  984. }
  985. /**
  986. * testFormCreateWithSecurity method
  987. *
  988. * Test form->create() with security key.
  989. *
  990. * @return void
  991. */
  992. public function testCreateWithSecurity()
  993. {
  994. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  995. $encoding = strtolower(Configure::read('App.encoding'));
  996. $result = $this->Form->create($this->article, [
  997. 'url' => '/articles/publish',
  998. ]);
  999. $expected = [
  1000. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  1001. 'div' => ['style' => 'display:none;'],
  1002. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1003. ['input' => [
  1004. 'type' => 'hidden',
  1005. 'name' => '_csrfToken',
  1006. 'value' => 'testKey',
  1007. 'autocomplete' => 'off',
  1008. ]],
  1009. '/div'
  1010. ];
  1011. $this->assertHtml($expected, $result);
  1012. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1013. $expected['form']['id'] = 'MyForm';
  1014. $this->assertHtml($expected, $result);
  1015. }
  1016. /**
  1017. * testFormCreateGetNoSecurity method
  1018. *
  1019. * Test form->create() with no security key as its a get form
  1020. *
  1021. * @return void
  1022. */
  1023. public function testCreateEndGetNoSecurity()
  1024. {
  1025. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  1026. $article = new Article();
  1027. $result = $this->Form->create($article, [
  1028. 'type' => 'get',
  1029. 'url' => '/contacts/add'
  1030. ]);
  1031. $this->assertNotContains('testKey', $result);
  1032. $result = $this->Form->end();
  1033. $this->assertNotContains('testKey', $result);
  1034. }
  1035. /**
  1036. * test that create() clears the fields property so it starts fresh
  1037. *
  1038. * @return void
  1039. */
  1040. public function testCreateClearingFields()
  1041. {
  1042. $this->Form->fields = ['model_id'];
  1043. $this->Form->create($this->article);
  1044. $this->assertEquals([], $this->Form->fields);
  1045. }
  1046. /**
  1047. * Tests form hash generation with model-less data
  1048. *
  1049. * @return void
  1050. */
  1051. public function testValidateHashNoModel()
  1052. {
  1053. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1054. $fields = ['anything'];
  1055. $result = $this->Form->secure($fields);
  1056. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1057. $this->assertContains($hash, $result);
  1058. }
  1059. /**
  1060. * Tests that hidden fields generated for checkboxes don't get locked
  1061. *
  1062. * @return void
  1063. */
  1064. public function testNoCheckboxLocking()
  1065. {
  1066. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1067. $this->assertSame([], $this->Form->fields);
  1068. $this->Form->checkbox('check', ['value' => '1']);
  1069. $this->assertSame($this->Form->fields, ['check']);
  1070. }
  1071. /**
  1072. * testFormSecurityFields method
  1073. *
  1074. * Test generation of secure form hash generation.
  1075. *
  1076. * @return void
  1077. */
  1078. public function testFormSecurityFields()
  1079. {
  1080. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1081. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1082. $result = $this->Form->secure($fields);
  1083. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1084. $hash .= ':' . 'Model.valid';
  1085. $hash = urlencode($hash);
  1086. $tokenDebug = urlencode(json_encode([
  1087. '',
  1088. $fields,
  1089. []
  1090. ]));
  1091. $expected = [
  1092. 'div' => ['style' => 'display:none;'],
  1093. ['input' => [
  1094. 'type' => 'hidden',
  1095. 'name' => '_Token[fields]',
  1096. 'value' => $hash,
  1097. 'autocomplete' => 'off',
  1098. ]],
  1099. ['input' => [
  1100. 'type' => 'hidden',
  1101. 'name' => '_Token[unlocked]',
  1102. 'value' => '',
  1103. 'autocomplete' => 'off',
  1104. ]],
  1105. ['input' => [
  1106. 'type' => 'hidden',
  1107. 'name' => '_Token[debug]',
  1108. 'value' => $tokenDebug,
  1109. 'autocomplete' => 'off',
  1110. ]],
  1111. '/div'
  1112. ];
  1113. $this->assertHtml($expected, $result);
  1114. }
  1115. /**
  1116. * testFormSecurityFields method
  1117. *
  1118. * Test debug token is not generated if debug is false
  1119. *
  1120. * @return void
  1121. */
  1122. public function testFormSecurityFieldsNoDebugMode()
  1123. {
  1124. Configure::write('debug', false);
  1125. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1126. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1127. $result = $this->Form->secure($fields);
  1128. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1129. $hash .= ':' . 'Model.valid';
  1130. $hash = urlencode($hash);
  1131. $expected = [
  1132. 'div' => ['style' => 'display:none;'],
  1133. ['input' => [
  1134. 'type' => 'hidden',
  1135. 'name' => '_Token[fields]',
  1136. 'autocomplete' => 'off',
  1137. 'value' => $hash
  1138. ]],
  1139. ['input' => [
  1140. 'type' => 'hidden',
  1141. 'name' => '_Token[unlocked]',
  1142. 'autocomplete' => 'off',
  1143. 'value' => '',
  1144. ]],
  1145. '/div'
  1146. ];
  1147. $this->assertHtml($expected, $result);
  1148. }
  1149. /**
  1150. * Tests correct generation of number fields for smallint
  1151. *
  1152. * @return void
  1153. */
  1154. public function testTextFieldGenerationForSmallint()
  1155. {
  1156. $this->article['schema'] = [
  1157. 'foo' => [
  1158. 'type' => 'smallinteger',
  1159. 'null' => false,
  1160. 'default' => null,
  1161. 'length' => 10
  1162. ]
  1163. ];
  1164. $this->Form->create($this->article);
  1165. $result = $this->Form->control('foo');
  1166. $this->assertContains('class="input number"', $result);
  1167. $this->assertContains('type="number"', $result);
  1168. }
  1169. /**
  1170. * Tests correct generation of number fields for tinyint
  1171. *
  1172. * @return void
  1173. */
  1174. public function testTextFieldGenerationForTinyint()
  1175. {
  1176. $this->article['schema'] = [
  1177. 'foo' => [
  1178. 'type' => 'tinyinteger',
  1179. 'null' => false,
  1180. 'default' => null,
  1181. 'length' => 10
  1182. ]
  1183. ];
  1184. $this->Form->create($this->article);
  1185. $result = $this->Form->control('foo');
  1186. $this->assertContains('class="input number"', $result);
  1187. $this->assertContains('type="number"', $result);
  1188. }
  1189. /**
  1190. * Tests correct generation of number fields for double and float fields
  1191. *
  1192. * @return void
  1193. */
  1194. public function testTextFieldGenerationForFloats()
  1195. {
  1196. $this->article['schema'] = [
  1197. 'foo' => [
  1198. 'type' => 'float',
  1199. 'null' => false,
  1200. 'default' => null,
  1201. 'length' => 10
  1202. ]
  1203. ];
  1204. $this->Form->create($this->article);
  1205. $result = $this->Form->control('foo');
  1206. $expected = [
  1207. 'div' => ['class' => 'input number'],
  1208. 'label' => ['for' => 'foo'],
  1209. 'Foo',
  1210. '/label',
  1211. ['input' => [
  1212. 'type' => 'number',
  1213. 'name' => 'foo',
  1214. 'id' => 'foo',
  1215. 'step' => 'any'
  1216. ]],
  1217. '/div'
  1218. ];
  1219. $this->assertHtml($expected, $result);
  1220. $result = $this->Form->control('foo', ['step' => 0.5]);
  1221. $expected = [
  1222. 'div' => ['class' => 'input number'],
  1223. 'label' => ['for' => 'foo'],
  1224. 'Foo',
  1225. '/label',
  1226. ['input' => [
  1227. 'type' => 'number',
  1228. 'name' => 'foo',
  1229. 'id' => 'foo',
  1230. 'step' => '0.5'
  1231. ]],
  1232. '/div'
  1233. ];
  1234. $this->assertHtml($expected, $result);
  1235. }
  1236. /**
  1237. * Tests correct generation of number fields for integer fields
  1238. *
  1239. * @return void
  1240. */
  1241. public function testTextFieldTypeNumberGenerationForIntegers()
  1242. {
  1243. TableRegistry::get('Contacts', [
  1244. 'className' => __NAMESPACE__ . '\ContactsTable'
  1245. ]);
  1246. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1247. $result = $this->Form->control('age');
  1248. $expected = [
  1249. 'div' => ['class' => 'input number'],
  1250. 'label' => ['for' => 'age'],
  1251. 'Age',
  1252. '/label',
  1253. ['input' => [
  1254. 'type' => 'number', 'name' => 'age',
  1255. 'id' => 'age'
  1256. ]],
  1257. '/div'
  1258. ];
  1259. $this->assertHtml($expected, $result);
  1260. }
  1261. /**
  1262. * Tests correct generation of file upload fields for binary fields
  1263. *
  1264. * @return void
  1265. */
  1266. public function testFileUploadFieldTypeGenerationForBinaries()
  1267. {
  1268. $table = TableRegistry::get('Contacts', [
  1269. 'className' => __NAMESPACE__ . '\ContactsTable'
  1270. ]);
  1271. $table->setSchema(['foo' => [
  1272. 'type' => 'binary',
  1273. 'null' => false,
  1274. 'default' => null,
  1275. 'length' => 1024
  1276. ]]);
  1277. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1278. $result = $this->Form->control('foo');
  1279. $expected = [
  1280. 'div' => ['class' => 'input file'],
  1281. 'label' => ['for' => 'foo'],
  1282. 'Foo',
  1283. '/label',
  1284. ['input' => [
  1285. 'type' => 'file', 'name' => 'foo',
  1286. 'id' => 'foo'
  1287. ]],
  1288. '/div'
  1289. ];
  1290. $this->assertHtml($expected, $result);
  1291. }
  1292. /**
  1293. * testFormSecurityMultipleFields method
  1294. *
  1295. * Test secure() with multiple row form. Ensure hash is correct.
  1296. *
  1297. * @return void
  1298. */
  1299. public function testFormSecurityMultipleFields()
  1300. {
  1301. $this->Form->request = $this->Form->request->withParam('_Token', 'foo');
  1302. $fields = [
  1303. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1304. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1305. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1306. ];
  1307. $result = $this->Form->secure($fields);
  1308. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1309. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1310. $tokenDebug = urlencode(json_encode([
  1311. '',
  1312. $fields,
  1313. []
  1314. ]));
  1315. $expected = [
  1316. 'div' => ['style' => 'display:none;'],
  1317. ['input' => [
  1318. 'type' => 'hidden',
  1319. 'name' => '_Token[fields]',
  1320. 'value' => $hash,
  1321. 'autocomplete' => 'off',
  1322. ]],
  1323. ['input' => [
  1324. 'type' => 'hidden',
  1325. 'name' => '_Token[unlocked]',
  1326. 'autocomplete' => 'off',
  1327. 'value' => ''
  1328. ]],
  1329. ['input' => [
  1330. 'type' => 'hidden',
  1331. 'name' => '_Token[debug]',
  1332. 'value' => $tokenDebug,
  1333. 'autocomplete' => 'off',
  1334. ]],
  1335. '/div'
  1336. ];
  1337. $this->assertHtml($expected, $result);
  1338. }
  1339. /**
  1340. * testFormSecurityMultipleSubmitButtons
  1341. *
  1342. * test form submit generation and ensure that _Token is only created on end()
  1343. *
  1344. * @return void
  1345. */
  1346. public function testFormSecurityMultipleSubmitButtons()
  1347. {
  1348. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1349. $this->Form->create($this->article);
  1350. $this->Form->text('Address.title');
  1351. $this->Form->text('Address.first_name');
  1352. $result = $this->Form->submit('Save', ['name' => 'save']);
  1353. $expected = [
  1354. 'div' => ['class' => 'submit'],
  1355. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1356. '/div',
  1357. ];
  1358. $this->assertHtml($expected, $result);
  1359. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1360. $expected = [
  1361. 'div' => ['class' => 'submit'],
  1362. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1363. '/div',
  1364. ];
  1365. $this->assertHtml($expected, $result);
  1366. $result = $this->Form->end();
  1367. $tokenDebug = urlencode(json_encode([
  1368. '/articles/add',
  1369. [
  1370. 'Address.title',
  1371. 'Address.first_name',
  1372. ],
  1373. ['save', 'cancel']
  1374. ]));
  1375. $expected = [
  1376. 'div' => ['style' => 'display:none;'],
  1377. ['input' => [
  1378. 'type' => 'hidden',
  1379. 'name' => '_Token[fields]',
  1380. 'autocomplete',
  1381. 'value'
  1382. ]],
  1383. ['input' => [
  1384. 'type' => 'hidden',
  1385. 'name' => '_Token[unlocked]',
  1386. 'value' => 'cancel%7Csave',
  1387. 'autocomplete' => 'off',
  1388. ]],
  1389. ['input' => [
  1390. 'type' => 'hidden',
  1391. 'name' => '_Token[debug]',
  1392. 'value' => $tokenDebug,
  1393. 'autocomplete' => 'off',
  1394. ]],
  1395. '/div'
  1396. ];
  1397. $this->assertHtml($expected, $result);
  1398. }
  1399. /**
  1400. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1401. *
  1402. * @return void
  1403. */
  1404. public function testSecurityButtonNestedNamed()
  1405. {
  1406. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  1407. $this->Form->create('Addresses');
  1408. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1409. $result = $this->Form->unlockField();
  1410. $this->assertEquals(['Address.button'], $result);
  1411. }
  1412. /**
  1413. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1414. *
  1415. * @return void
  1416. */
  1417. public function testSecuritySubmitNestedNamed()
  1418. {
  1419. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1420. $this->Form->create($this->article);
  1421. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1422. $result = $this->Form->unlockField();
  1423. $this->assertEquals(['Address.button'], $result);
  1424. }
  1425. /**
  1426. * Test that the correct fields are unlocked for image submits with no names.
  1427. *
  1428. * @return void
  1429. */
  1430. public function testSecuritySubmitImageNoName()
  1431. {
  1432. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1433. $this->Form->create(false);
  1434. $result = $this->Form->submit('save.png');
  1435. $expected = [
  1436. 'div' => ['class' => 'submit'],
  1437. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1438. '/div'
  1439. ];
  1440. $this->assertHtml($expected, $result);
  1441. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1442. }
  1443. /**
  1444. * Test that the correct fields are unlocked for image submits with names.
  1445. *
  1446. * @return void
  1447. */
  1448. public function testSecuritySubmitImageName()
  1449. {
  1450. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1451. $this->Form->create(null);
  1452. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1453. $expected = [
  1454. 'div' => ['class' => 'submit'],
  1455. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1456. '/div'
  1457. ];
  1458. $this->assertHtml($expected, $result);
  1459. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1460. }
  1461. /**
  1462. * testFormSecurityMultipleControlFields method
  1463. *
  1464. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1465. *
  1466. * @return void
  1467. */
  1468. public function testFormSecurityMultipleControlFields()
  1469. {
  1470. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1471. $this->Form->create();
  1472. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1473. $this->Form->control('Addresses.0.title');
  1474. $this->Form->control('Addresses.0.first_name');
  1475. $this->Form->control('Addresses.0.last_name');
  1476. $this->Form->control('Addresses.0.address');
  1477. $this->Form->control('Addresses.0.city');
  1478. $this->Form->control('Addresses.0.phone');
  1479. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1480. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1481. $this->Form->control('Addresses.1.title');
  1482. $this->Form->control('Addresses.1.first_name');
  1483. $this->Form->control('Addresses.1.last_name');
  1484. $this->Form->control('Addresses.1.address');
  1485. $this->Form->control('Addresses.1.city');
  1486. $this->Form->control('Addresses.1.phone');
  1487. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1488. $result = $this->Form->secure($this->Form->fields);
  1489. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1490. $tokenDebug = urlencode(json_encode([
  1491. '/articles/add',
  1492. [
  1493. 'Addresses.0.id' => '123456',
  1494. 'Addresses.0.title',
  1495. 'Addresses.0.first_name',
  1496. 'Addresses.0.last_name',
  1497. 'Addresses.0.address',
  1498. 'Addresses.0.city',
  1499. 'Addresses.0.phone',
  1500. 'Addresses.0.primary',
  1501. 'Addresses.1.id' => '654321',
  1502. 'Addresses.1.title',
  1503. 'Addresses.1.first_name',
  1504. 'Addresses.1.last_name',
  1505. 'Addresses.1.address',
  1506. 'Addresses.1.city',
  1507. 'Addresses.1.phone',
  1508. 'Addresses.1.primary',
  1509. ],
  1510. []
  1511. ]));
  1512. $expected = [
  1513. 'div' => ['style' => 'display:none;'],
  1514. ['input' => [
  1515. 'type' => 'hidden',
  1516. 'name' => '_Token[fields]',
  1517. 'value' => $hash,
  1518. 'autocomplete' => 'off',
  1519. ]],
  1520. ['input' => [
  1521. 'type' => 'hidden',
  1522. 'name' => '_Token[unlocked]',
  1523. 'autocomplete' => 'off',
  1524. 'value' => ''
  1525. ]],
  1526. ['input' => [
  1527. 'type' => 'hidden',
  1528. 'name' => '_Token[debug]',
  1529. 'value' => $tokenDebug,
  1530. 'autocomplete' => 'off',
  1531. ]],
  1532. '/div'
  1533. ];
  1534. $this->assertHtml($expected, $result);
  1535. }
  1536. /**
  1537. * testFormSecurityArrayFields method
  1538. *
  1539. * Test form security with Model.field.0 style inputs.
  1540. *
  1541. * @return void
  1542. */
  1543. public function testFormSecurityArrayFields()
  1544. {
  1545. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1546. $this->Form->create();
  1547. $this->Form->text('Address.primary.1');
  1548. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1549. $this->Form->text('Address.secondary.1.0');
  1550. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1551. }
  1552. /**
  1553. * testFormSecurityMultipleControlDisabledFields method
  1554. *
  1555. * Test secure form generation with multiple records and disabled fields.
  1556. *
  1557. * @return void
  1558. */
  1559. public function testFormSecurityMultipleControlDisabledFields()
  1560. {
  1561. $this->Form->request = $this->Form->request->withParam('_Token', [
  1562. 'unlockedFields' => ['first_name', 'address']
  1563. ]);
  1564. $this->Form->create();
  1565. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1566. $this->Form->text('Addresses.0.title');
  1567. $this->Form->text('Addresses.0.first_name');
  1568. $this->Form->text('Addresses.0.last_name');
  1569. $this->Form->text('Addresses.0.address');
  1570. $this->Form->text('Addresses.0.city');
  1571. $this->Form->text('Addresses.0.phone');
  1572. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1573. $this->Form->text('Addresses.1.title');
  1574. $this->Form->text('Addresses.1.first_name');
  1575. $this->Form->text('Addresses.1.last_name');
  1576. $this->Form->text('Addresses.1.address');
  1577. $this->Form->text('Addresses.1.city');
  1578. $this->Form->text('Addresses.1.phone');
  1579. $result = $this->Form->secure($this->Form->fields);
  1580. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1581. $tokenDebug = urlencode(json_encode([
  1582. '/articles/add',
  1583. [
  1584. 'Addresses.0.id' => '123456',
  1585. 'Addresses.0.title',
  1586. 'Addresses.0.last_name',
  1587. 'Addresses.0.city',
  1588. 'Addresses.0.phone',
  1589. 'Addresses.1.id' => '654321',
  1590. 'Addresses.1.title',
  1591. 'Addresses.1.last_name',
  1592. 'Addresses.1.city',
  1593. 'Addresses.1.phone'
  1594. ],
  1595. [
  1596. 'first_name',
  1597. 'address'
  1598. ]
  1599. ]));
  1600. $expected = [
  1601. 'div' => ['style' => 'display:none;'],
  1602. ['input' => [
  1603. 'type' => 'hidden',
  1604. 'name' => '_Token[fields]',
  1605. 'autocomplete' => 'off',
  1606. 'value' => $hash
  1607. ]],
  1608. ['input' => [
  1609. 'type' => 'hidden',
  1610. 'name' => '_Token[unlocked]',
  1611. 'autocomplete' => 'off',
  1612. 'value' => 'address%7Cfirst_name',
  1613. ]],
  1614. ['input' => [
  1615. 'type' => 'hidden',
  1616. 'name' => '_Token[debug]',
  1617. 'autocomplete' => 'off',
  1618. 'value' => $tokenDebug,
  1619. ]],
  1620. '/div'
  1621. ];
  1622. $this->assertHtml($expected, $result);
  1623. }
  1624. /**
  1625. * testFormSecurityControlDisabledFields method
  1626. *
  1627. * Test single record form with disabled fields.
  1628. *
  1629. * @return void
  1630. */
  1631. public function testFormSecurityControlUnlockedFields()
  1632. {
  1633. $this->Form->request = $this->Form->request->withParam('_Token', [
  1634. 'unlockedFields' => ['first_name', 'address']
  1635. ]);
  1636. $this->Form->create();
  1637. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1638. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1639. $this->Form->text('Addresses.title');
  1640. $this->Form->text('Addresses.first_name');
  1641. $this->Form->text('Addresses.last_name');
  1642. $this->Form->text('Addresses.address');
  1643. $this->Form->text('Addresses.city');
  1644. $this->Form->text('Addresses.phone');
  1645. $result = $this->Form->fields;
  1646. $expected = [
  1647. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1648. 'Addresses.city', 'Addresses.phone'
  1649. ];
  1650. $this->assertEquals($expected, $result);
  1651. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1652. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1653. $tokenDebug = urlencode(json_encode([
  1654. '/articles/add',
  1655. [
  1656. 'Addresses.id' => '123456',
  1657. 'Addresses.title',
  1658. 'Addresses.last_name',
  1659. 'Addresses.city',
  1660. 'Addresses.phone'
  1661. ],
  1662. [
  1663. 'first_name',
  1664. 'address'
  1665. ]
  1666. ]));
  1667. $expected = [
  1668. 'div' => ['style' => 'display:none;'],
  1669. ['input' => [
  1670. 'type' => 'hidden',
  1671. 'name' => '_Token[fields]',
  1672. 'value' => $hash,
  1673. 'autocomplete' => 'off',
  1674. 'data-foo' => 'bar',
  1675. ]],
  1676. ['input' => [
  1677. 'type' => 'hidden',
  1678. 'name' => '_Token[unlocked]',
  1679. 'value' => 'address%7Cfirst_name',
  1680. 'autocomplete' => 'off',
  1681. 'data-foo' => 'bar',
  1682. ]],
  1683. ['input' => [
  1684. 'type' => 'hidden', 'name' => '_Token[debug]',
  1685. 'value' => $tokenDebug,
  1686. 'autocomplete' => 'off',
  1687. 'data-foo' => 'bar'
  1688. ]],
  1689. '/div'
  1690. ];
  1691. $this->assertHtml($expected, $result);
  1692. }
  1693. /**
  1694. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1695. *
  1696. * Test single record form with debugSecurity param.
  1697. *
  1698. * @return void
  1699. */
  1700. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1701. {
  1702. $this->Form->request = $this->Form->request->withParam('_Token', [
  1703. 'unlockedFields' => ['first_name', 'address']
  1704. ]);
  1705. $this->Form->create();
  1706. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1707. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1708. $this->Form->text('Addresses.title');
  1709. $this->Form->text('Addresses.first_name');
  1710. $this->Form->text('Addresses.last_name');
  1711. $this->Form->text('Addresses.address');
  1712. $this->Form->text('Addresses.city');
  1713. $this->Form->text('Addresses.phone');
  1714. $result = $this->Form->fields;
  1715. $expected = [
  1716. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1717. 'Addresses.city', 'Addresses.phone'
  1718. ];
  1719. $this->assertEquals($expected, $result);
  1720. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1721. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1722. $tokenDebug = urlencode(json_encode([
  1723. '/articles/add',
  1724. [
  1725. 'Addresses.id' => '123456',
  1726. 'Addresses.title',
  1727. 'Addresses.last_name',
  1728. 'Addresses.city',
  1729. 'Addresses.phone'
  1730. ],
  1731. [
  1732. 'first_name',
  1733. 'address'
  1734. ]
  1735. ]));
  1736. $expected = [
  1737. 'div' => ['style' => 'display:none;'],
  1738. ['input' => [
  1739. 'type' => 'hidden',
  1740. 'name' => '_Token[fields]',
  1741. 'value' => $hash,
  1742. 'autocomplete' => 'off',
  1743. 'data-foo' => 'bar',
  1744. ]],
  1745. ['input' => [
  1746. 'type' => 'hidden',
  1747. 'name' => '_Token[unlocked]',
  1748. 'value' => 'address%7Cfirst_name',
  1749. 'autocomplete' => 'off',
  1750. 'data-foo' => 'bar',
  1751. ]],
  1752. ['input' => [
  1753. 'type' => 'hidden', 'name' => '_Token[debug]',
  1754. 'value' => $tokenDebug,
  1755. 'autocomplete' => 'off',
  1756. 'data-foo' => 'bar'
  1757. ]],
  1758. '/div'
  1759. ];
  1760. $this->assertHtml($expected, $result);
  1761. }
  1762. /**
  1763. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1764. *
  1765. * Debug is false, debugSecurity is true -> no debug
  1766. *
  1767. * @return void
  1768. */
  1769. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1770. {
  1771. $this->Form->request = $this->Form->request->withParam('_Token', [
  1772. 'unlockedFields' => ['first_name', 'address']
  1773. ]);
  1774. $this->Form->create();
  1775. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1776. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1777. $this->Form->text('Addresses.title');
  1778. $this->Form->text('Addresses.first_name');
  1779. $this->Form->text('Addresses.last_name');
  1780. $this->Form->text('Addresses.address');
  1781. $this->Form->text('Addresses.city');
  1782. $this->Form->text('Addresses.phone');
  1783. $result = $this->Form->fields;
  1784. $expected = [
  1785. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1786. 'Addresses.city', 'Addresses.phone'
  1787. ];
  1788. $this->assertEquals($expected, $result);
  1789. Configure::write('debug', false);
  1790. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1791. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1792. $expected = [
  1793. 'div' => ['style' => 'display:none;'],
  1794. ['input' => [
  1795. 'type' => 'hidden',
  1796. 'name' => '_Token[fields]',
  1797. 'value' => $hash,
  1798. 'autocomplete' => 'off',
  1799. 'data-foo' => 'bar',
  1800. ]],
  1801. ['input' => [
  1802. 'type' => 'hidden',
  1803. 'name' => '_Token[unlocked]',
  1804. 'value' => 'address%7Cfirst_name',
  1805. 'autocomplete' => 'off',
  1806. 'data-foo' => 'bar',
  1807. ]],
  1808. '/div'
  1809. ];
  1810. $this->assertHtml($expected, $result);
  1811. }
  1812. /**
  1813. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1814. *
  1815. * Test single record form with debugSecurity param.
  1816. *
  1817. * @return void
  1818. */
  1819. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1820. {
  1821. $this->Form->request = $this->Form->request->withParam('_Token', [
  1822. 'unlockedFields' => ['first_name', 'address']
  1823. ]);
  1824. $this->Form->create();
  1825. $this->assertEquals($this->Form->request->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1826. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1827. $this->Form->text('Addresses.title');
  1828. $this->Form->text('Addresses.first_name');
  1829. $this->Form->text('Addresses.last_name');
  1830. $this->Form->text('Addresses.address');
  1831. $this->Form->text('Addresses.city');
  1832. $this->Form->text('Addresses.phone');
  1833. $result = $this->Form->fields;
  1834. $expected = [
  1835. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1836. 'Addresses.city', 'Addresses.phone'
  1837. ];
  1838. $this->assertEquals($expected, $result);
  1839. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1840. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1841. $expected = [
  1842. 'div' => ['style' => 'display:none;'],
  1843. ['input' => [
  1844. 'type' => 'hidden',
  1845. 'name' => '_Token[fields]',
  1846. 'value' => $hash,
  1847. 'autocomplete' => 'off',
  1848. 'data-foo' => 'bar',
  1849. ]],
  1850. ['input' => [
  1851. 'type' => 'hidden',
  1852. 'name' => '_Token[unlocked]',
  1853. 'value' => 'address%7Cfirst_name',
  1854. 'autocomplete' => 'off',
  1855. 'data-foo' => 'bar',
  1856. ]],
  1857. '/div'
  1858. ];
  1859. $this->assertHtml($expected, $result);
  1860. }
  1861. /**
  1862. * testFormSecureWithCustomNameAttribute method
  1863. *
  1864. * Test securing inputs with custom name attributes.
  1865. *
  1866. * @return void
  1867. */
  1868. public function testFormSecureWithCustomNameAttribute()
  1869. {
  1870. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  1871. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1872. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1873. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1874. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1875. }
  1876. /**
  1877. * testFormSecuredControl method
  1878. *
  1879. * Test generation of entire secure form, assertions made on control() output.
  1880. *
  1881. * @return void
  1882. */
  1883. public function testFormSecuredControl()
  1884. {
  1885. $this->Form->request = $this->Form->request
  1886. ->withParam('_Token', 'stuff')
  1887. ->withParam('_csrfToken', 'testKey');
  1888. $this->article['schema'] = [
  1889. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1890. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1891. ];
  1892. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1893. $encoding = strtolower(Configure::read('App.encoding'));
  1894. $expected = [
  1895. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1896. 'div' => ['style' => 'display:none;'],
  1897. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1898. ['input' => [
  1899. 'type' => 'hidden',
  1900. 'name' => '_csrfToken',
  1901. 'value' => 'testKey',
  1902. 'autocomplete' => 'off'
  1903. ]],
  1904. '/div'
  1905. ];
  1906. $this->assertHtml($expected, $result);
  1907. $result = $this->Form->control('ratio');
  1908. $expected = [
  1909. 'div' => ['class'],
  1910. 'label' => ['for'],
  1911. 'Ratio',
  1912. '/label',
  1913. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1914. '/div',
  1915. ];
  1916. $this->assertHtml($expected, $result);
  1917. $result = $this->Form->control('population');
  1918. $expected = [
  1919. 'div' => ['class'],
  1920. 'label' => ['for'],
  1921. 'Population',
  1922. '/label',
  1923. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1924. '/div',
  1925. ];
  1926. $this->assertHtml($expected, $result);
  1927. $result = $this->Form->control('published', ['type' => 'text']);
  1928. $expected = [
  1929. 'div' => ['class' => 'input text'],
  1930. 'label' => ['for' => 'published'],
  1931. 'Published',
  1932. '/label',
  1933. ['input' => [
  1934. 'type' => 'text',
  1935. 'name' => 'published',
  1936. 'id' => 'published'
  1937. ]],
  1938. '/div'
  1939. ];
  1940. $this->assertHtml($expected, $result);
  1941. $result = $this->Form->control('other', ['type' => 'text']);
  1942. $expected = [
  1943. 'div' => ['class' => 'input text'],
  1944. 'label' => ['for' => 'other'],
  1945. 'Other',
  1946. '/label',
  1947. ['input' => [
  1948. 'type' => 'text',
  1949. 'name' => 'other',
  1950. 'id',
  1951. ]],
  1952. '/div'
  1953. ];
  1954. $this->assertHtml($expected, $result);
  1955. $result = $this->Form->hidden('stuff');
  1956. $expected = [
  1957. 'input' => [
  1958. 'type' => 'hidden',
  1959. 'name' => 'stuff'
  1960. ]
  1961. ];
  1962. $this->assertHtml($expected, $result);
  1963. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1964. $expected = ['input' => [
  1965. 'type' => 'hidden',
  1966. 'name' => 'hidden',
  1967. 'value' => '0'
  1968. ]];
  1969. $this->assertHtml($expected, $result);
  1970. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1971. $expected = [
  1972. 'div' => ['class' => 'input checkbox'],
  1973. ['input' => [
  1974. 'type' => 'hidden',
  1975. 'name' => 'something',
  1976. 'value' => '0'
  1977. ]],
  1978. 'label' => ['for' => 'something'],
  1979. ['input' => [
  1980. 'type' => 'checkbox',
  1981. 'name' => 'something',
  1982. 'value' => '1',
  1983. 'id' => 'something'
  1984. ]],
  1985. 'Something',
  1986. '/label',
  1987. '/div'
  1988. ];
  1989. $this->assertHtml($expected, $result);
  1990. $result = $this->Form->fields;
  1991. $expectedFields = [
  1992. 'ratio',
  1993. 'population',
  1994. 'published',
  1995. 'other',
  1996. 'stuff' => '',
  1997. 'hidden' => '0',
  1998. 'something'
  1999. ];
  2000. $this->assertEquals($expectedFields, $result);
  2001. $result = $this->Form->secure($this->Form->fields);
  2002. $tokenDebug = urlencode(json_encode([
  2003. '/articles/add',
  2004. $expectedFields,
  2005. []
  2006. ]));
  2007. $expected = [
  2008. 'div' => ['style' => 'display:none;'],
  2009. ['input' => [
  2010. 'type' => 'hidden',
  2011. 'name' => '_Token[fields]',
  2012. 'value',
  2013. 'autocomplete'
  2014. ]],
  2015. ['input' => [
  2016. 'type' => 'hidden',
  2017. 'name' => '_Token[unlocked]',
  2018. 'value' => '',
  2019. 'autocomplete' => 'off'
  2020. ]],
  2021. ['input' => [
  2022. 'type' => 'hidden', 'name' => '_Token[debug]',
  2023. 'value' => $tokenDebug,
  2024. 'autocomplete' => 'off'
  2025. ]],
  2026. '/div'
  2027. ];
  2028. $this->assertHtml($expected, $result);
  2029. }
  2030. /**
  2031. * testSecuredControlCustomName method
  2032. *
  2033. * Test secured inputs with custom names.
  2034. *
  2035. * @return void
  2036. */
  2037. public function testSecuredControlCustomName()
  2038. {
  2039. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2040. $this->assertEquals([], $this->Form->fields);
  2041. $this->Form->text('text_input', [
  2042. 'name' => 'Option[General.default_role]',
  2043. ]);
  2044. $expected = ['Option.General.default_role'];
  2045. $this->assertEquals($expected, $this->Form->fields);
  2046. $this->Form->select('select_box', [1, 2], [
  2047. 'name' => 'Option[General.select_role]',
  2048. ]);
  2049. $expected[] = 'Option.General.select_role';
  2050. $this->assertEquals($expected, $this->Form->fields);
  2051. $this->Form->text('other.things[]');
  2052. $expected[] = 'other.things';
  2053. $this->assertEquals($expected, $this->Form->fields);
  2054. }
  2055. /**
  2056. * testSecuredControlDuplicate method
  2057. *
  2058. * Test that a hidden field followed by a visible field
  2059. * undoes the hidden field locking.
  2060. *
  2061. * @return void
  2062. */
  2063. public function testSecuredControlDuplicate()
  2064. {
  2065. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2066. $this->assertEquals([], $this->Form->fields);
  2067. $this->Form->control('text_val', [
  2068. 'type' => 'hidden',
  2069. 'value' => 'some text',
  2070. ]);
  2071. $expected = ['text_val' => 'some text'];
  2072. $this->assertEquals($expected, $this->Form->fields);
  2073. $this->Form->control('text_val', [
  2074. 'type' => 'text',
  2075. ]);
  2076. $expected = ['text_val'];
  2077. $this->assertEquals($expected, $this->Form->fields);
  2078. }
  2079. /**
  2080. * testFormSecuredFileControl method
  2081. *
  2082. * Tests that the correct keys are added to the field hash index.
  2083. *
  2084. * @return void
  2085. */
  2086. public function testFormSecuredFileControl()
  2087. {
  2088. $this->assertEquals([], $this->Form->fields);
  2089. $this->Form->file('Attachment.file');
  2090. $expected = [
  2091. 'Attachment.file.name', 'Attachment.file.type',
  2092. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2093. 'Attachment.file.size'
  2094. ];
  2095. $this->assertEquals($expected, $this->Form->fields);
  2096. }
  2097. /**
  2098. * testFormSecuredMultipleSelect method
  2099. *
  2100. * Test that multiple selects keys are added to field hash.
  2101. *
  2102. * @return void
  2103. */
  2104. public function testFormSecuredMultipleSelect()
  2105. {
  2106. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'testKey');
  2107. $this->assertEquals([], $this->Form->fields);
  2108. $options = ['1' => 'one', '2' => 'two'];
  2109. $this->Form->select('Model.select', $options);
  2110. $expected = ['Model.select'];
  2111. $this->assertEquals($expected, $this->Form->fields);
  2112. $this->Form->fields = [];
  2113. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2114. $this->assertEquals($expected, $this->Form->fields);
  2115. }
  2116. /**
  2117. * testFormSecuredRadio method
  2118. *
  2119. * @return void
  2120. */
  2121. public function testFormSecuredRadio()
  2122. {
  2123. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2124. $this->assertEquals([], $this->Form->fields);
  2125. $options = ['1' => 'option1', '2' => 'option2'];
  2126. $this->Form->radio('Test.test', $options);
  2127. $expected = ['Test.test'];
  2128. $this->assertEquals($expected, $this->Form->fields);
  2129. $this->Form->radio('Test.all', $options, [
  2130. 'disabled' => ['option1', 'option2']
  2131. ]);
  2132. $expected = ['Test.test', 'Test.all' => ''];
  2133. $this->assertEquals($expected, $this->Form->fields);
  2134. $this->Form->radio('Test.some', $options, [
  2135. 'disabled' => ['option1']
  2136. ]);
  2137. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2138. $this->assertEquals($expected, $this->Form->fields);
  2139. }
  2140. /**
  2141. * testFormSecuredAndDisabledNotAssoc method
  2142. *
  2143. * Test that when disabled is in a list based attribute array it works.
  2144. *
  2145. * @return void
  2146. */
  2147. public function testFormSecuredAndDisabledNotAssoc()
  2148. {
  2149. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2150. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2151. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2152. $this->Form->text('Model.text', ['disabled']);
  2153. $this->Form->textarea('Model.textarea', ['disabled']);
  2154. $this->Form->password('Model.password', ['disabled']);
  2155. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2156. $expected = [
  2157. 'Model.radio' => ''
  2158. ];
  2159. $this->assertEquals($expected, $this->Form->fields);
  2160. }
  2161. /**
  2162. * testFormSecuredAndDisabled method
  2163. *
  2164. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2165. * hashing.
  2166. *
  2167. * @return void
  2168. */
  2169. public function testFormSecuredAndDisabled()
  2170. {
  2171. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2172. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2173. $this->Form->text('Model.text', ['disabled' => true]);
  2174. $this->Form->password('Model.text', ['disabled' => true]);
  2175. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2176. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2177. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2178. $this->Form->year('Model.year', ['disabled' => true]);
  2179. $this->Form->month('Model.month', ['disabled' => true]);
  2180. $this->Form->day('Model.day', ['disabled' => true]);
  2181. $this->Form->hour('Model.hour', ['disabled' => true]);
  2182. $this->Form->minute('Model.minute', ['disabled' => true]);
  2183. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2184. $expected = [
  2185. 'Model.radio' => ''
  2186. ];
  2187. $this->assertEquals($expected, $this->Form->fields);
  2188. }
  2189. /**
  2190. * testDisableSecurityUsingForm method
  2191. *
  2192. * @return void
  2193. */
  2194. public function testDisableSecurityUsingForm()
  2195. {
  2196. $this->Form->request = $this->Form->request->withParam('_Token', [
  2197. 'disabledFields' => []
  2198. ]);
  2199. $this->Form->create();
  2200. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2201. $this->Form->text('Addresses.title');
  2202. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2203. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2204. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2205. $result = $this->Form->fields;
  2206. $expected = [
  2207. 'Addresses.id' => '123456', 'Addresses.title',
  2208. ];
  2209. $this->assertEquals($expected, $result);
  2210. }
  2211. /**
  2212. * testUnlockFieldAddsToList method
  2213. *
  2214. * Test disableField.
  2215. *
  2216. * @return void
  2217. */
  2218. public function testUnlockFieldAddsToList()
  2219. {
  2220. $this->Form->request = $this->Form->request->withParam('_Token', [
  2221. 'unlockedFields' => []
  2222. ]);
  2223. $this->Form->unlockField('Contact.name');
  2224. $this->Form->text('Contact.name');
  2225. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2226. $this->assertEquals([], $this->Form->fields);
  2227. }
  2228. /**
  2229. * testUnlockFieldRemovingFromFields method
  2230. *
  2231. * Test unlockField removing from fields array.
  2232. *
  2233. * @return void
  2234. */
  2235. public function testUnlockFieldRemovingFromFields()
  2236. {
  2237. $this->Form->request = $this->Form->request->withParam('_Token', [
  2238. 'unlockedFields' => []
  2239. ]);
  2240. $this->Form->create($this->article);
  2241. $this->Form->hidden('Article.id', ['value' => 1]);
  2242. $this->Form->text('Article.title');
  2243. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2244. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2245. $this->Form->unlockField('Article.title');
  2246. $this->Form->unlockField('Article.id');
  2247. $this->assertEquals([], $this->Form->fields);
  2248. }
  2249. /**
  2250. * testResetUnlockFields method
  2251. *
  2252. * Test reset unlockFields, when create new form.
  2253. *
  2254. * @return void
  2255. */
  2256. public function testResetUnlockFields()
  2257. {
  2258. $this->Form->request = $this->Form->request->withParam('_Token', [
  2259. 'key' => 'testKey',
  2260. 'unlockedFields' => []
  2261. ]);
  2262. $this->Form->unlockField('Contact.id');
  2263. $this->Form->create('Contact');
  2264. $this->Form->hidden('Contact.id', ['value' => 1]);
  2265. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2266. $this->Form->end();
  2267. $this->Form->create('Contact');
  2268. $this->Form->hidden('Contact.id', ['value' => 1]);
  2269. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2270. }
  2271. /**
  2272. * testSecuredFormUrlIgnoresHost method
  2273. *
  2274. * Test that only the path + query elements of a form's URL show up in their hash.
  2275. *
  2276. * @return void
  2277. */
  2278. public function testSecuredFormUrlIgnoresHost()
  2279. {
  2280. $this->Form->request = $this->Form->request->withParam('_Token', ['key' => 'testKey']);
  2281. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2282. $this->Form->create($this->article, [
  2283. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2284. ]);
  2285. $result = $this->Form->secure();
  2286. $this->assertContains($expected, $result);
  2287. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2288. $result = $this->Form->secure();
  2289. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2290. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2291. $result = $this->Form->secure();
  2292. $this->assertContains($expected, $result, 'URL path + query should work.');
  2293. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2294. $result = $this->Form->secure();
  2295. $this->assertNotContains($expected, $result, 'URL is different');
  2296. }
  2297. /**
  2298. * testSecuredFormUrlHasHtmlAndIdentifier method
  2299. *
  2300. * Test that URL, HTML and identifier show up in their hashes.
  2301. *
  2302. * @return void
  2303. */
  2304. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2305. {
  2306. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  2307. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2308. $this->Form->create($this->article, [
  2309. 'url' => [
  2310. 'controller' => 'articles',
  2311. 'action' => 'view',
  2312. '?' => [
  2313. 'page' => 1,
  2314. 'limit' => 10,
  2315. 'html' => '<>"',
  2316. ],
  2317. '#' => 'result',
  2318. ],
  2319. ]);
  2320. $result = $this->Form->secure();
  2321. $this->assertContains($expected, $result);
  2322. $this->Form->create($this->article, [
  2323. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2324. ]);
  2325. $result = $this->Form->secure();
  2326. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2327. $this->Form->create($this->article, [
  2328. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2329. ]);
  2330. $result = $this->Form->secure();
  2331. $this->assertContains($expected, $result, 'URL path + query should work.');
  2332. }
  2333. /**
  2334. * testErrorMessageDisplay method
  2335. *
  2336. * Test error message display.
  2337. *
  2338. * @return void
  2339. */
  2340. public function testErrorMessageDisplay()
  2341. {
  2342. $this->article['errors'] = [
  2343. 'Article' => [
  2344. 'title' => 'error message',
  2345. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2346. ]
  2347. ];
  2348. $this->Form->create($this->article);
  2349. $result = $this->Form->control('Article.title');
  2350. $expected = [
  2351. 'div' => ['class' => 'input text error'],
  2352. 'label' => ['for' => 'article-title'],
  2353. 'Title',
  2354. '/label',
  2355. 'input' => [
  2356. 'type' => 'text', 'name' => 'Article[title]',
  2357. 'id' => 'article-title', 'class' => 'form-error'
  2358. ],
  2359. ['div' => ['class' => 'error-message']],
  2360. 'error message',
  2361. '/div',
  2362. '/div'
  2363. ];
  2364. $this->assertHtml($expected, $result);
  2365. $result = $this->Form->control('Article.title', [
  2366. 'templates' => [
  2367. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2368. ]
  2369. ]);
  2370. $expected = [
  2371. 'div' => ['class' => 'input text error'],
  2372. 'label' => ['for' => 'article-title'],
  2373. 'Title',
  2374. '/label',
  2375. 'input' => [
  2376. 'type' => 'text', 'name' => 'Article[title]',
  2377. 'id' => 'article-title', 'class' => 'form-error'
  2378. ],
  2379. '/div'
  2380. ];
  2381. $this->assertHtml($expected, $result);
  2382. $result = $this->Form->control('Article.content');
  2383. $expected = [
  2384. 'div' => ['class' => 'input text error'],
  2385. 'label' => ['for' => 'article-content'],
  2386. 'Content',
  2387. '/label',
  2388. 'input' => [
  2389. 'type' => 'text', 'name' => 'Article[content]',
  2390. 'id' => 'article-content', 'class' => 'form-error'
  2391. ],
  2392. ['div' => ['class' => 'error-message']],
  2393. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2394. '/div',
  2395. '/div'
  2396. ];
  2397. $this->assertHtml($expected, $result);
  2398. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2399. $expected = [
  2400. 'div' => ['class' => 'input text error'],
  2401. 'label' => ['for' => 'article-content'],
  2402. 'Content',
  2403. '/label',
  2404. 'input' => [
  2405. 'type' => 'text', 'name' => 'Article[content]',
  2406. 'id' => 'article-content', 'class' => 'form-error'
  2407. ],
  2408. ['div' => ['class' => 'error-message']],
  2409. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2410. '/div',
  2411. '/div'
  2412. ];
  2413. $this->assertHtml($expected, $result);
  2414. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2415. $expected = [
  2416. 'div' => ['class' => 'input text error'],
  2417. 'label' => ['for' => 'article-content'],
  2418. 'Content',
  2419. '/label',
  2420. 'input' => [
  2421. 'type' => 'text', 'name' => 'Article[content]',
  2422. 'id' => 'article-content', 'class' => 'form-error'
  2423. ],
  2424. ['div' => ['class' => 'error-message']],
  2425. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2426. '/div',
  2427. '/div'
  2428. ];
  2429. $this->assertHtml($expected, $result);
  2430. }
  2431. /**
  2432. * testEmptyErrorValidation method
  2433. *
  2434. * Test validation errors, when validation message is an empty string.
  2435. *
  2436. * @return void
  2437. */
  2438. public function testEmptyErrorValidation()
  2439. {
  2440. $this->article['errors'] = [
  2441. 'Article' => ['title' => '']
  2442. ];
  2443. $this->Form->create($this->article);
  2444. $result = $this->Form->control('Article.title');
  2445. $expected = [
  2446. 'div' => ['class' => 'input text error'],
  2447. 'label' => ['for' => 'article-title'],
  2448. 'Title',
  2449. '/label',
  2450. 'input' => [
  2451. 'type' => 'text', 'name' => 'Article[title]',
  2452. 'id' => 'article-title', 'class' => 'form-error'
  2453. ],
  2454. ['div' => ['class' => 'error-message']],
  2455. [],
  2456. '/div',
  2457. '/div'
  2458. ];
  2459. $this->assertHtml($expected, $result);
  2460. }
  2461. /**
  2462. * testEmptyControlErrorValidation method
  2463. *
  2464. * Test validation errors, when calling control() overriding validation message by an empty string.
  2465. *
  2466. * @return void
  2467. */
  2468. public function testEmptyControlErrorValidation()
  2469. {
  2470. $this->article['errors'] = [
  2471. 'Article' => ['title' => 'error message']
  2472. ];
  2473. $this->Form->create($this->article);
  2474. $result = $this->Form->control('Article.title', ['error' => '']);
  2475. $expected = [
  2476. 'div' => ['class' => 'input text error'],
  2477. 'label' => ['for' => 'article-title'],
  2478. 'Title',
  2479. '/label',
  2480. 'input' => [
  2481. 'type' => 'text', 'name' => 'Article[title]',
  2482. 'id' => 'article-title', 'class' => 'form-error'
  2483. ],
  2484. ['div' => ['class' => 'error-message']],
  2485. [],
  2486. '/div',
  2487. '/div'
  2488. ];
  2489. $this->assertHtml($expected, $result);
  2490. }
  2491. /**
  2492. * testControlErrorMessage method
  2493. *
  2494. * Test validation errors, when calling control() overriding validation messages.
  2495. *
  2496. * @return void
  2497. */
  2498. public function testControlErrorMessage()
  2499. {
  2500. $this->article['errors'] = [
  2501. 'title' => ['error message']
  2502. ];
  2503. $this->Form->create($this->article);
  2504. $result = $this->Form->control('title', [
  2505. 'error' => 'Custom error!'
  2506. ]);
  2507. $expected = [
  2508. 'div' => ['class' => 'input text required error'],
  2509. 'label' => ['for' => 'title'],
  2510. 'Title',
  2511. '/label',
  2512. 'input' => [
  2513. 'type' => 'text', 'name' => 'title',
  2514. 'id' => 'title', 'class' => 'form-error',
  2515. 'required' => 'required',
  2516. ],
  2517. ['div' => ['class' => 'error-message']],
  2518. 'Custom error!',
  2519. '/div',
  2520. '/div'
  2521. ];
  2522. $this->assertHtml($expected, $result);
  2523. $result = $this->Form->control('title', [
  2524. 'error' => ['error message' => 'Custom error!']
  2525. ]);
  2526. $expected = [
  2527. 'div' => ['class' => 'input text required error'],
  2528. 'label' => ['for' => 'title'],
  2529. 'Title',
  2530. '/label',
  2531. 'input' => [
  2532. 'type' => 'text',
  2533. 'name' => 'title',
  2534. 'id' => 'title',
  2535. 'class' => 'form-error',
  2536. 'required' => 'required'
  2537. ],
  2538. ['div' => ['class' => 'error-message']],
  2539. 'Custom error!',
  2540. '/div',
  2541. '/div'
  2542. ];
  2543. $this->assertHtml($expected, $result);
  2544. }
  2545. /**
  2546. * testFormValidationAssociated method
  2547. *
  2548. * Tests displaying errors for nested entities.
  2549. *
  2550. * @return void
  2551. */
  2552. public function testFormValidationAssociated()
  2553. {
  2554. $nested = new Entity(['foo' => 'bar']);
  2555. $nested->setError('foo', ['not a valid bar']);
  2556. $entity = new Entity(['nested' => $nested]);
  2557. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2558. $result = $this->Form->error('nested.foo');
  2559. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2560. }
  2561. /**
  2562. * testFormValidationAssociatedSecondLevel method
  2563. *
  2564. * Test form error display with associated model.
  2565. *
  2566. * @return void
  2567. */
  2568. public function testFormValidationAssociatedSecondLevel()
  2569. {
  2570. $inner = new Entity(['bar' => 'baz']);
  2571. $nested = new Entity(['foo' => $inner]);
  2572. $entity = new Entity(['nested' => $nested]);
  2573. $inner->setError('bar', ['not a valid one']);
  2574. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2575. $result = $this->Form->error('nested.foo.bar');
  2576. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2577. }
  2578. /**
  2579. * testFormValidationMultiRecord method
  2580. *
  2581. * Test form error display with multiple records.
  2582. *
  2583. * @return void
  2584. */
  2585. public function testFormValidationMultiRecord()
  2586. {
  2587. $one = new Entity();
  2588. $two = new Entity();
  2589. TableRegistry::get('Contacts', [
  2590. 'className' => __NAMESPACE__ . '\ContactsTable'
  2591. ]);
  2592. $one->set('email', '');
  2593. $one->setError('email', ['invalid email']);
  2594. $two->set('name', '');
  2595. $two->setError('name', ['This is wrong']);
  2596. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2597. $result = $this->Form->control('0.email');
  2598. $expected = [
  2599. 'div' => ['class' => 'input email error'],
  2600. 'label' => ['for' => '0-email'],
  2601. 'Email',
  2602. '/label',
  2603. 'input' => [
  2604. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2605. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2606. ],
  2607. ['div' => ['class' => 'error-message']],
  2608. 'invalid email',
  2609. '/div',
  2610. '/div'
  2611. ];
  2612. $this->assertHtml($expected, $result);
  2613. $result = $this->Form->control('1.name');
  2614. $expected = [
  2615. 'div' => ['class' => 'input text error'],
  2616. 'label' => ['for' => '1-name'],
  2617. 'Name',
  2618. '/label',
  2619. 'input' => [
  2620. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2621. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2622. ],
  2623. ['div' => ['class' => 'error-message']],
  2624. 'This is wrong',
  2625. '/div',
  2626. '/div'
  2627. ];
  2628. $this->assertHtml($expected, $result);
  2629. }
  2630. /**
  2631. * testControl method
  2632. *
  2633. * Test various incarnations of control().
  2634. *
  2635. * @return void
  2636. */
  2637. public function testControl()
  2638. {
  2639. TableRegistry::get('ValidateUsers', [
  2640. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2641. ]);
  2642. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2643. $result = $this->Form->control('ValidateUsers.balance');
  2644. $expected = [
  2645. 'div' => ['class'],
  2646. 'label' => ['for'],
  2647. 'Balance',
  2648. '/label',
  2649. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2650. '/div',
  2651. ];
  2652. $this->assertHtml($expected, $result);
  2653. $result = $this->Form->control('ValidateUser.cost_decimal');
  2654. $expected = [
  2655. 'div' => ['class'],
  2656. 'label' => ['for'],
  2657. 'Cost Decimal',
  2658. '/label',
  2659. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2660. '/div',
  2661. ];
  2662. $this->assertHtml($expected, $result);
  2663. $result = $this->Form->control('ValidateUser.null_decimal');
  2664. $expected = [
  2665. 'div' => ['class'],
  2666. 'label' => ['for'],
  2667. 'Null Decimal',
  2668. '/label',
  2669. 'input' => ['name', 'type' => 'number', 'id'],
  2670. '/div',
  2671. ];
  2672. $this->assertHtml($expected, $result);
  2673. }
  2674. /**
  2675. * testControlCustomization method
  2676. *
  2677. * Tests the input method and passing custom options.
  2678. *
  2679. * @return void
  2680. */
  2681. public function testControlCustomization()
  2682. {
  2683. TableRegistry::get('Contacts', [
  2684. 'className' => __NAMESPACE__ . '\ContactsTable'
  2685. ]);
  2686. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2687. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2688. $expected = [
  2689. 'div' => ['class' => 'input email'],
  2690. 'label' => ['for' => 'custom'],
  2691. 'Email',
  2692. '/label',
  2693. ['input' => [
  2694. 'type' => 'email', 'name' => 'Contact[email]',
  2695. 'id' => 'custom', 'maxlength' => 255
  2696. ]],
  2697. '/div'
  2698. ];
  2699. $this->assertHtml($expected, $result);
  2700. $result = $this->Form->control('Contact.email', [
  2701. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2702. ]);
  2703. $expected = [
  2704. '<div',
  2705. 'label' => ['for' => 'contact-email'],
  2706. 'Email',
  2707. '/label',
  2708. ['input' => [
  2709. 'type' => 'email', 'name' => 'Contact[email]',
  2710. 'id' => 'contact-email', 'maxlength' => 255
  2711. ]],
  2712. '/div'
  2713. ];
  2714. $this->assertHtml($expected, $result);
  2715. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2716. $expected = [
  2717. 'div' => ['class' => 'input text'],
  2718. 'label' => ['for' => 'contact-email'],
  2719. 'Email',
  2720. '/label',
  2721. ['input' => [
  2722. 'type' => 'text', 'name' => 'Contact[email]',
  2723. 'id' => 'contact-email', 'maxlength' => '255'
  2724. ]],
  2725. '/div'
  2726. ];
  2727. $this->assertHtml($expected, $result);
  2728. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2729. $expected = [
  2730. 'div' => ['class' => 'input text'],
  2731. 'label' => ['for' => 'contact-5-email'],
  2732. 'Email',
  2733. '/label',
  2734. ['input' => [
  2735. 'type' => 'text', 'name' => 'Contact[5][email]',
  2736. 'id' => 'contact-5-email', 'maxlength' => '255'
  2737. ]],
  2738. '/div'
  2739. ];
  2740. $this->assertHtml($expected, $result);
  2741. $result = $this->Form->control('Contact.password');
  2742. $expected = [
  2743. 'div' => ['class' => 'input password'],
  2744. 'label' => ['for' => 'contact-password'],
  2745. 'Password',
  2746. '/label',
  2747. ['input' => [
  2748. 'type' => 'password', 'name' => 'Contact[password]',
  2749. 'id' => 'contact-password'
  2750. ]],
  2751. '/div'
  2752. ];
  2753. $this->assertHtml($expected, $result);
  2754. $result = $this->Form->control('Contact.email', [
  2755. 'type' => 'file', 'class' => 'textbox'
  2756. ]);
  2757. $expected = [
  2758. 'div' => ['class' => 'input file'],
  2759. 'label' => ['for' => 'contact-email'],
  2760. 'Email',
  2761. '/label',
  2762. ['input' => [
  2763. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2764. 'id' => 'contact-email'
  2765. ]],
  2766. '/div'
  2767. ];
  2768. $this->assertHtml($expected, $result);
  2769. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2770. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2771. $result = $this->Form->control('phone');
  2772. $expected = [
  2773. 'div' => ['class' => 'input tel'],
  2774. 'label' => ['for' => 'phone'],
  2775. 'Phone',
  2776. '/label',
  2777. ['input' => [
  2778. 'type' => 'tel', 'name' => 'phone',
  2779. 'value' => 'Hello &amp; World &gt; weird chars',
  2780. 'id' => 'phone', 'maxlength' => 255
  2781. ]],
  2782. '/div'
  2783. ];
  2784. $this->assertHtml($expected, $result);
  2785. $this->Form->request = $this->Form->request->withData('Model.0.OtherModel.field', 'My value');
  2786. $this->Form->create();
  2787. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2788. $expected = [
  2789. 'div' => ['class' => 'input text'],
  2790. 'label' => ['for' => 'myId'],
  2791. 'Field',
  2792. '/label',
  2793. 'input' => [
  2794. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2795. 'value' => 'My value', 'id' => 'myId'
  2796. ],
  2797. '/div'
  2798. ];
  2799. $this->assertHtml($expected, $result);
  2800. $this->Form->request = $this->Form->request->withParsedBody([]);
  2801. $this->Form->create();
  2802. $entity->setError('field', 'Badness!');
  2803. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2804. $result = $this->Form->control('field');
  2805. $expected = [
  2806. 'div' => ['class' => 'input text error'],
  2807. 'label' => ['for' => 'field'],
  2808. 'Field',
  2809. '/label',
  2810. 'input' => [
  2811. 'type' => 'text', 'name' => 'field',
  2812. 'id' => 'field', 'class' => 'form-error'
  2813. ],
  2814. ['div' => ['class' => 'error-message']],
  2815. 'Badness!',
  2816. '/div',
  2817. '/div'
  2818. ];
  2819. $this->assertHtml($expected, $result);
  2820. $result = $this->Form->control('field', [
  2821. 'templates' => [
  2822. 'inputContainerError' => '{{content}}{{error}}',
  2823. 'error' => '<span class="error-message">{{content}}</span>'
  2824. ]
  2825. ]);
  2826. $expected = [
  2827. 'label' => ['for' => 'field'],
  2828. 'Field',
  2829. '/label',
  2830. 'input' => [
  2831. 'type' => 'text', 'name' => 'field',
  2832. 'id' => 'field', 'class' => 'form-error'
  2833. ],
  2834. ['span' => ['class' => 'error-message']],
  2835. 'Badness!',
  2836. '/span'
  2837. ];
  2838. $this->assertHtml($expected, $result);
  2839. $entity->setError('field', ['minLength'], true);
  2840. $result = $this->Form->control('field', [
  2841. 'error' => [
  2842. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2843. 'maxLength' => 'login too large'
  2844. ]
  2845. ]);
  2846. $expected = [
  2847. 'div' => ['class' => 'input text error'],
  2848. 'label' => ['for' => 'field'],
  2849. 'Field',
  2850. '/label',
  2851. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2852. ['div' => ['class' => 'error-message']],
  2853. 'Le login doit contenir au moins 2 caractères',
  2854. '/div',
  2855. '/div'
  2856. ];
  2857. $this->assertHtml($expected, $result);
  2858. $entity->setError('field', ['maxLength'], true);
  2859. $result = $this->Form->control('field', [
  2860. 'error' => [
  2861. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2862. 'maxLength' => 'login too large',
  2863. ]
  2864. ]);
  2865. $expected = [
  2866. 'div' => ['class' => 'input text error'],
  2867. 'label' => ['for' => 'field'],
  2868. 'Field',
  2869. '/label',
  2870. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2871. ['div' => ['class' => 'error-message']],
  2872. 'login too large',
  2873. '/div',
  2874. '/div'
  2875. ];
  2876. $this->assertHtml($expected, $result);
  2877. }
  2878. /**
  2879. * testControlWithTemplateFile method
  2880. *
  2881. * Test that control() accepts a template file.
  2882. *
  2883. * @return void
  2884. */
  2885. public function testControlWithTemplateFile()
  2886. {
  2887. $result = $this->Form->control('field', [
  2888. 'templates' => 'htmlhelper_tags'
  2889. ]);
  2890. $expected = [
  2891. 'label' => ['for' => 'field'],
  2892. 'Field',
  2893. '/label',
  2894. 'input' => [
  2895. 'type' => 'text', 'name' => 'field',
  2896. 'id' => 'field'
  2897. ],
  2898. ];
  2899. $this->assertHtml($expected, $result);
  2900. }
  2901. /**
  2902. * testNestedControlsEndWithBrackets method
  2903. *
  2904. * Test that nested inputs end with brackets.
  2905. *
  2906. * @return void
  2907. */
  2908. public function testNestedControlsEndWithBrackets()
  2909. {
  2910. $result = $this->Form->text('nested.text[]');
  2911. $expected = [
  2912. 'input' => [
  2913. 'type' => 'text', 'name' => 'nested[text][]'
  2914. ],
  2915. ];
  2916. $this->assertHtml($expected, $result);
  2917. $result = $this->Form->file('nested.file[]');
  2918. $expected = [
  2919. 'input' => [
  2920. 'type' => 'file', 'name' => 'nested[file][]'
  2921. ],
  2922. ];
  2923. $this->assertHtml($expected, $result);
  2924. }
  2925. /**
  2926. * testCreateIdPrefix method
  2927. *
  2928. * Test id prefix.
  2929. *
  2930. * @return void
  2931. */
  2932. public function testCreateIdPrefix()
  2933. {
  2934. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2935. $result = $this->Form->control('field');
  2936. $expected = [
  2937. 'div' => ['class' => 'input text'],
  2938. 'label' => ['for' => 'prefix-field'],
  2939. 'Field',
  2940. '/label',
  2941. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2942. '/div'
  2943. ];
  2944. $this->assertHtml($expected, $result);
  2945. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2946. $expected = [
  2947. 'div' => ['class' => 'input text'],
  2948. 'label' => ['for' => 'custom-id'],
  2949. 'Field',
  2950. '/label',
  2951. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2952. '/div'
  2953. ];
  2954. $this->assertHtml($expected, $result);
  2955. $result = $this->Form->radio('Model.field', ['option A']);
  2956. $expected = [
  2957. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2958. 'label' => ['for' => 'prefix-model-field-0'],
  2959. ['input' => [
  2960. 'type' => 'radio',
  2961. 'name' => 'Model[field]',
  2962. 'value' => '0',
  2963. 'id' => 'prefix-model-field-0'
  2964. ]],
  2965. 'option A',
  2966. '/label'
  2967. ];
  2968. $this->assertHtml($expected, $result);
  2969. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2970. $expected = [
  2971. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2972. 'label' => ['for' => 'prefix-model-field-0'],
  2973. ['input' => [
  2974. 'type' => 'radio',
  2975. 'name' => 'Model[field]',
  2976. 'value' => '0',
  2977. 'id' => 'prefix-model-field-0'
  2978. ]],
  2979. 'option A',
  2980. '/label'
  2981. ];
  2982. $this->assertHtml($expected, $result);
  2983. $result = $this->Form->select(
  2984. 'Model.multi_field',
  2985. ['first'],
  2986. ['multiple' => 'checkbox']
  2987. );
  2988. $expected = [
  2989. 'input' => [
  2990. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2991. ],
  2992. ['div' => ['class' => 'checkbox']],
  2993. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2994. ['input' => [
  2995. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2996. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2997. ]],
  2998. 'first',
  2999. '/label',
  3000. '/div',
  3001. ];
  3002. $this->assertHtml($expected, $result);
  3003. $this->Form->end();
  3004. $result = $this->Form->control('field');
  3005. $expected = [
  3006. 'div' => ['class' => 'input text'],
  3007. 'label' => ['for' => 'field'],
  3008. 'Field',
  3009. '/label',
  3010. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3011. '/div'
  3012. ];
  3013. $this->assertHtml($expected, $result);
  3014. }
  3015. /**
  3016. * testControlZero method
  3017. *
  3018. * Test that inputs with 0 can be created.
  3019. *
  3020. * @return void
  3021. */
  3022. public function testControlZero()
  3023. {
  3024. TableRegistry::get('Contacts', [
  3025. 'className' => __NAMESPACE__ . '\ContactsTable'
  3026. ]);
  3027. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3028. $result = $this->Form->control('0');
  3029. $expected = [
  3030. 'div' => ['class' => 'input text'],
  3031. 'label' => ['for' => '0'], '/label',
  3032. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3033. '/div'
  3034. ];
  3035. $this->assertHtml($expected, $result);
  3036. }
  3037. /**
  3038. * testControlCheckbox method
  3039. *
  3040. * Test control() with checkbox creation.
  3041. *
  3042. * @return void
  3043. */
  3044. public function testControlCheckbox()
  3045. {
  3046. $articles = TableRegistry::get('Articles');
  3047. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3048. $article = $articles->newEntity();
  3049. $this->Form->create($article);
  3050. $result = $this->Form->control('Articles.active');
  3051. $expected = [
  3052. 'div' => ['class' => 'input checkbox'],
  3053. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3054. 'label' => ['for' => 'articles-active'],
  3055. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3056. 'Active',
  3057. '/label',
  3058. '/div'
  3059. ];
  3060. $this->assertHtml($expected, $result);
  3061. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3062. $expected = [
  3063. 'div' => ['class' => 'input checkbox'],
  3064. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3065. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3066. '/div'
  3067. ];
  3068. $this->assertHtml($expected, $result);
  3069. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3070. $expected = [
  3071. 'div' => ['class' => 'input checkbox'],
  3072. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3073. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3074. '/div'
  3075. ];
  3076. $this->assertHtml($expected, $result);
  3077. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3078. $expected = [
  3079. 'div' => ['class' => 'input checkbox'],
  3080. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3081. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3082. '/div'
  3083. ];
  3084. $this->assertHtml($expected, $result);
  3085. $result = $this->Form->control('Articles.disabled', [
  3086. 'label' => 'Disabled',
  3087. 'type' => 'checkbox',
  3088. 'data-foo' => 'disabled'
  3089. ]);
  3090. $expected = [
  3091. 'div' => ['class' => 'input checkbox'],
  3092. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3093. 'label' => ['for' => 'articles-disabled'],
  3094. ['input' => [
  3095. 'type' => 'checkbox',
  3096. 'name' => 'Articles[disabled]',
  3097. 'value' => '1',
  3098. 'id' => 'articles-disabled',
  3099. 'data-foo' => 'disabled'
  3100. ]],
  3101. 'Disabled',
  3102. '/label',
  3103. '/div'
  3104. ];
  3105. $this->assertHtml($expected, $result);
  3106. $result = $this->Form->control('Articles.confirm', [
  3107. 'label' => 'Confirm <b>me</b>!',
  3108. 'type' => 'checkbox',
  3109. 'escape' => false
  3110. ]);
  3111. $expected = [
  3112. 'div' => ['class' => 'input checkbox'],
  3113. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3114. 'label' => ['for' => 'articles-confirm'],
  3115. ['input' => [
  3116. 'type' => 'checkbox',
  3117. 'name' => 'Articles[confirm]',
  3118. 'value' => '1',
  3119. 'id' => 'articles-confirm',
  3120. ]],
  3121. 'Confirm <b>me</b>!',
  3122. '/label',
  3123. '/div'
  3124. ];
  3125. $this->assertHtml($expected, $result);
  3126. }
  3127. /**
  3128. * testControlHidden method
  3129. *
  3130. * Test that control() does not create wrapping div and label tag for hidden fields.
  3131. *
  3132. * @return void
  3133. */
  3134. public function testControlHidden()
  3135. {
  3136. TableRegistry::get('ValidateUsers', [
  3137. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3138. ]);
  3139. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3140. $result = $this->Form->control('ValidateUser.id');
  3141. $expected = [
  3142. 'input' => ['name', 'type' => 'hidden', 'id']
  3143. ];
  3144. $this->assertHtml($expected, $result);
  3145. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3146. $expected = [
  3147. 'input' => ['name', 'type' => 'hidden', 'id']
  3148. ];
  3149. $this->assertHtml($expected, $result);
  3150. }
  3151. /**
  3152. * testControlDatetime method
  3153. *
  3154. * Test form->control() with datetime.
  3155. *
  3156. * @return void
  3157. */
  3158. public function testControlDatetime()
  3159. {
  3160. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3161. ->setMethods(['datetime'])
  3162. ->setConstructorArgs([new View()])
  3163. ->getMock();
  3164. $this->Form->expects($this->once())->method('datetime')
  3165. ->with('prueba', [
  3166. 'type' => 'datetime',
  3167. 'timeFormat' => 24,
  3168. 'minYear' => 2008,
  3169. 'maxYear' => 2011,
  3170. 'interval' => 15,
  3171. 'options' => null,
  3172. 'empty' => false,
  3173. 'id' => 'prueba',
  3174. 'required' => false,
  3175. 'templateVars' => []
  3176. ])
  3177. ->will($this->returnValue('This is it!'));
  3178. $result = $this->Form->control('prueba', [
  3179. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3180. 'maxYear' => 2011, 'interval' => 15
  3181. ]);
  3182. $expected = [
  3183. 'div' => ['class' => 'input datetime'],
  3184. '<label',
  3185. 'Prueba',
  3186. '/label',
  3187. 'This is it!',
  3188. '/div'
  3189. ];
  3190. $this->assertHtml($expected, $result);
  3191. }
  3192. /**
  3193. * testControlDatetimeIdPrefix method
  3194. *
  3195. * Test form->control() with datetime with id prefix.
  3196. *
  3197. * @return void
  3198. */
  3199. public function testControlDatetimeIdPrefix()
  3200. {
  3201. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3202. ->setMethods(['datetime'])
  3203. ->setConstructorArgs([new View()])
  3204. ->getMock();
  3205. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3206. $this->Form->expects($this->once())->method('datetime')
  3207. ->with('prueba', [
  3208. 'type' => 'datetime',
  3209. 'timeFormat' => 24,
  3210. 'minYear' => 2008,
  3211. 'maxYear' => 2011,
  3212. 'interval' => 15,
  3213. 'options' => null,
  3214. 'empty' => false,
  3215. 'id' => 'prefix-prueba',
  3216. 'required' => false,
  3217. 'templateVars' => []
  3218. ])
  3219. ->will($this->returnValue('This is it!'));
  3220. $result = $this->Form->control('prueba', [
  3221. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3222. 'maxYear' => 2011, 'interval' => 15
  3223. ]);
  3224. $expected = [
  3225. 'div' => ['class' => 'input datetime'],
  3226. '<label',
  3227. 'Prueba',
  3228. '/label',
  3229. 'This is it!',
  3230. '/div'
  3231. ];
  3232. $this->assertHtml($expected, $result);
  3233. }
  3234. /**
  3235. * testControlCheckboxWithDisabledElements method
  3236. *
  3237. * Test generating checkboxes with disabled elements.
  3238. *
  3239. * @return void
  3240. */
  3241. public function testControlCheckboxWithDisabledElements()
  3242. {
  3243. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3244. $result = $this->Form->control('Contact.multiple', [
  3245. 'multiple' => 'checkbox',
  3246. 'disabled' => 'disabled',
  3247. 'options' => $options
  3248. ]);
  3249. $expected = [
  3250. ['div' => ['class' => 'input select']],
  3251. ['label' => ['for' => 'contact-multiple']],
  3252. 'Multiple',
  3253. '/label',
  3254. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3255. ['div' => ['class' => 'checkbox']],
  3256. ['label' => ['for' => 'contact-multiple-1']],
  3257. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3258. 'One',
  3259. '/label',
  3260. '/div',
  3261. ['div' => ['class' => 'checkbox']],
  3262. ['label' => ['for' => 'contact-multiple-2']],
  3263. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3264. 'Two',
  3265. '/label',
  3266. '/div',
  3267. ['div' => ['class' => 'checkbox']],
  3268. ['label' => ['for' => 'contact-multiple-3']],
  3269. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3270. 'Three',
  3271. '/label',
  3272. '/div',
  3273. '/div'
  3274. ];
  3275. $this->assertHtml($expected, $result);
  3276. // make sure 50 does only disable 50, and not 50f5c0cf
  3277. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3278. $disabled = [50];
  3279. $expected = [
  3280. ['div' => ['class' => 'input select']],
  3281. ['label' => ['for' => 'contact-multiple']],
  3282. 'Multiple',
  3283. '/label',
  3284. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3285. ['div' => ['class' => 'checkbox']],
  3286. ['label' => ['for' => 'contact-multiple-50']],
  3287. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3288. 'Fifty',
  3289. '/label',
  3290. '/div',
  3291. ['div' => ['class' => 'checkbox']],
  3292. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3293. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3294. 'Stringy',
  3295. '/label',
  3296. '/div',
  3297. '/div'
  3298. ];
  3299. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3300. $this->assertHtml($expected, $result);
  3301. }
  3302. /**
  3303. * testControlWithLeadingInteger method
  3304. *
  3305. * Test input name with leading integer, ensure attributes are generated correctly.
  3306. *
  3307. * @return void
  3308. */
  3309. public function testControlWithLeadingInteger()
  3310. {
  3311. $result = $this->Form->text('0.Node.title');
  3312. $expected = [
  3313. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3314. ];
  3315. $this->assertHtml($expected, $result);
  3316. }
  3317. /**
  3318. * testControlSelectType method
  3319. *
  3320. * Test form->control() with select type inputs.
  3321. *
  3322. * @return void
  3323. */
  3324. public function testControlSelectType()
  3325. {
  3326. $result = $this->Form->control(
  3327. 'email',
  3328. [
  3329. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3330. );
  3331. $expected = [
  3332. 'div' => ['class' => 'input select'],
  3333. 'label' => ['for' => 'email'],
  3334. 'Email',
  3335. '/label',
  3336. ['select' => ['name' => 'email', 'id' => 'email']],
  3337. ['option' => ['value' => '']],
  3338. '/option',
  3339. ['option' => ['value' => 'è']],
  3340. 'Firést',
  3341. '/option',
  3342. ['option' => ['value' => 'é']],
  3343. 'Secoènd',
  3344. '/option',
  3345. '/select',
  3346. '/div'
  3347. ];
  3348. $this->assertHtml($expected, $result);
  3349. $result = $this->Form->control(
  3350. 'email',
  3351. [
  3352. 'options' => ['First', 'Second'], 'empty' => true]
  3353. );
  3354. $expected = [
  3355. 'div' => ['class' => 'input select'],
  3356. 'label' => ['for' => 'email'],
  3357. 'Email',
  3358. '/label',
  3359. ['select' => ['name' => 'email', 'id' => 'email']],
  3360. ['option' => ['value' => '']],
  3361. '/option',
  3362. ['option' => ['value' => '0']],
  3363. 'First',
  3364. '/option',
  3365. ['option' => ['value' => '1']],
  3366. 'Second',
  3367. '/option',
  3368. '/select',
  3369. '/div'
  3370. ];
  3371. $this->assertHtml($expected, $result);
  3372. $result = $this->Form->control('email', [
  3373. 'type' => 'select',
  3374. 'options' => new \ArrayObject(['First', 'Second']),
  3375. 'empty' => true
  3376. ]);
  3377. $this->assertHtml($expected, $result);
  3378. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3379. $this->Form->request = $this->Form->request->withData('Model', ['user_id' => 'value']);
  3380. $this->Form->create();
  3381. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3382. $expected = [
  3383. 'div' => ['class' => 'input select'],
  3384. 'label' => ['for' => 'model-user-id'],
  3385. 'User',
  3386. '/label',
  3387. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3388. ['option' => ['value' => '']],
  3389. '/option',
  3390. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3391. 'good',
  3392. '/option',
  3393. ['option' => ['value' => 'other']],
  3394. 'bad',
  3395. '/option',
  3396. '/select',
  3397. '/div'
  3398. ];
  3399. $this->assertHtml($expected, $result);
  3400. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3401. $this->Form->request = $this->Form->request->withData('Thing', ['user_id' => null]);
  3402. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3403. $expected = [
  3404. 'div' => ['class' => 'input select'],
  3405. 'label' => ['for' => 'thing-user-id'],
  3406. 'User',
  3407. '/label',
  3408. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3409. ['option' => ['value' => '']],
  3410. 'Some Empty',
  3411. '/option',
  3412. ['option' => ['value' => 'value']],
  3413. 'good',
  3414. '/option',
  3415. ['option' => ['value' => 'other']],
  3416. 'bad',
  3417. '/option',
  3418. '/select',
  3419. '/div'
  3420. ];
  3421. $this->assertHtml($expected, $result);
  3422. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3423. $this->Form->request = $this->Form->request->withData('Thing', ['user_id' => 'value']);
  3424. $this->Form->create();
  3425. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3426. $expected = [
  3427. 'div' => ['class' => 'input select'],
  3428. 'label' => ['for' => 'thing-user-id'],
  3429. 'User',
  3430. '/label',
  3431. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3432. ['option' => ['value' => '']],
  3433. 'Some Empty',
  3434. '/option',
  3435. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3436. 'good',
  3437. '/option',
  3438. ['option' => ['value' => 'other']],
  3439. 'bad',
  3440. '/option',
  3441. '/select',
  3442. '/div'
  3443. ];
  3444. $this->assertHtml($expected, $result);
  3445. $result = $this->Form->control('Publisher.id', [
  3446. 'label' => 'Publisher',
  3447. 'type' => 'select',
  3448. 'multiple' => 'checkbox',
  3449. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3450. ]);
  3451. $expected = [
  3452. ['div' => ['class' => 'input select']],
  3453. ['label' => ['for' => 'publisher-id']],
  3454. 'Publisher',
  3455. '/label',
  3456. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3457. ['div' => ['class' => 'checkbox']],
  3458. ['label' => ['for' => 'publisher-id-value-1']],
  3459. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3460. 'Label 1',
  3461. '/label',
  3462. '/div',
  3463. ['div' => ['class' => 'checkbox']],
  3464. ['label' => ['for' => 'publisher-id-value-2']],
  3465. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3466. 'Label 2',
  3467. '/label',
  3468. '/div',
  3469. '/div'
  3470. ];
  3471. $this->assertHtml($expected, $result);
  3472. }
  3473. /**
  3474. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3475. *
  3476. * Test that control() and a non standard primary key makes a hidden input by default.
  3477. *
  3478. * @return void
  3479. */
  3480. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3481. {
  3482. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3483. $this->Form->create($this->article);
  3484. $result = $this->Form->control('title');
  3485. $expected = [
  3486. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3487. ];
  3488. $this->assertHtml($expected, $result);
  3489. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3490. $this->Form->create($this->article);
  3491. $result = $this->Form->control('title');
  3492. $expected = [
  3493. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3494. ];
  3495. $this->assertHtml($expected, $result);
  3496. $result = $this->Form->control('body');
  3497. $expected = [
  3498. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3499. ];
  3500. $this->assertHtml($expected, $result);
  3501. }
  3502. /**
  3503. * testControlOverridingMagicSelectType method
  3504. *
  3505. * Test that overriding the magic select type widget is possible.
  3506. *
  3507. * @return void
  3508. */
  3509. public function testControlOverridingMagicSelectType()
  3510. {
  3511. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3512. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3513. $expected = [
  3514. 'div' => ['class' => 'input text'],
  3515. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3516. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3517. '/div'
  3518. ];
  3519. $this->assertHtml($expected, $result);
  3520. //Check that magic types still work for plural/singular vars
  3521. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3522. $result = $this->Form->control('Model.type');
  3523. $expected = [
  3524. 'div' => ['class' => 'input select'],
  3525. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3526. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3527. ['option' => ['value' => 'value']], 'good', '/option',
  3528. ['option' => ['value' => 'other']], 'bad', '/option',
  3529. '/select',
  3530. '/div'
  3531. ];
  3532. $this->assertHtml($expected, $result);
  3533. }
  3534. /**
  3535. * testControlMagicTypeDoesNotOverride method
  3536. *
  3537. * Test that inferred types do not override developer input.
  3538. *
  3539. * @return void
  3540. */
  3541. public function testControlMagicTypeDoesNotOverride()
  3542. {
  3543. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3544. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3545. $expected = [
  3546. 'div' => ['class' => 'input checkbox'],
  3547. ['input' => [
  3548. 'type' => 'hidden',
  3549. 'name' => 'Model[user]',
  3550. 'value' => 0,
  3551. ]],
  3552. 'label' => ['for' => 'model-user'],
  3553. ['input' => [
  3554. 'name' => 'Model[user]',
  3555. 'type' => 'checkbox',
  3556. 'id' => 'model-user',
  3557. 'value' => 1
  3558. ]],
  3559. 'User',
  3560. '/label',
  3561. '/div'
  3562. ];
  3563. $this->assertHtml($expected, $result);
  3564. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3565. $options = [
  3566. 1 => 'blue',
  3567. 2 => 'red'
  3568. ];
  3569. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3570. $expected = [
  3571. 'div' => ['class' => 'input select'],
  3572. 'label' => ['for' => 'tags-ids'],
  3573. 'Tags',
  3574. '/label',
  3575. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3576. ['div' => ['class' => 'checkbox']],
  3577. ['label' => ['for' => 'tags-ids-1']],
  3578. ['input' => [
  3579. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3580. 'value' => '1', 'name' => 'tags[_ids][]'
  3581. ]],
  3582. 'blue',
  3583. '/label',
  3584. '/div',
  3585. ['div' => ['class' => 'checkbox']],
  3586. ['label' => ['for' => 'tags-ids-2']],
  3587. ['input' => [
  3588. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3589. 'value' => '2', 'name' => 'tags[_ids][]'
  3590. ]],
  3591. 'red',
  3592. '/label',
  3593. '/div',
  3594. '/div'
  3595. ];
  3596. $this->assertHtml($expected, $result);
  3597. }
  3598. /**
  3599. * testControlMagicSelectForTypeNumber method
  3600. *
  3601. * Test that magic control() selects are created for type=number.
  3602. *
  3603. * @return void
  3604. */
  3605. public function testControlMagicSelectForTypeNumber()
  3606. {
  3607. TableRegistry::get('ValidateUsers', [
  3608. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3609. ]);
  3610. $entity = new Entity(['balance' => 1]);
  3611. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3612. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3613. $result = $this->Form->control('balance');
  3614. $expected = [
  3615. 'div' => ['class' => 'input select'],
  3616. 'label' => ['for' => 'balance'],
  3617. 'Balance',
  3618. '/label',
  3619. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3620. ['option' => ['value' => '0']],
  3621. 'nothing',
  3622. '/option',
  3623. ['option' => ['value' => '1', 'selected' => 'selected']],
  3624. 'some',
  3625. '/option',
  3626. ['option' => ['value' => '100']],
  3627. 'a lot',
  3628. '/option',
  3629. '/select',
  3630. '/div'
  3631. ];
  3632. $this->assertHtml($expected, $result);
  3633. }
  3634. /**
  3635. * testInvalidControlTypeOption method
  3636. *
  3637. * Test invalid 'input' type option to control() function.
  3638. *
  3639. * @return void
  3640. */
  3641. public function testInvalidControlTypeOption()
  3642. {
  3643. $this->expectException(\RuntimeException::class);
  3644. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3645. $this->Form->control('text', ['type' => 'input']);
  3646. }
  3647. /**
  3648. * testControlMagicSelectChangeToRadio method
  3649. *
  3650. * Test that magic control() selects can easily be converted into radio types without error.
  3651. *
  3652. * @return void
  3653. */
  3654. public function testControlMagicSelectChangeToRadio()
  3655. {
  3656. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3657. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3658. $this->assertContains('input type="radio"', $result);
  3659. }
  3660. /**
  3661. * testFormControlSubmit method
  3662. *
  3663. * Test correct results for form::control() and type submit.
  3664. *
  3665. * @return void
  3666. */
  3667. public function testFormControlSubmit()
  3668. {
  3669. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3670. $expected = [
  3671. 'div' => ['class' => 'submit'],
  3672. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3673. '/div'
  3674. ];
  3675. $this->assertHtml($expected, $result);
  3676. }
  3677. /**
  3678. * testFormControls method
  3679. *
  3680. * Test correct results from Form::controls().
  3681. *
  3682. * @return void
  3683. */
  3684. public function testFormControlsLegendFieldset()
  3685. {
  3686. $this->Form->create($this->article);
  3687. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3688. $expected = [
  3689. '<fieldset',
  3690. '<legend',
  3691. 'The Legend',
  3692. '/legend',
  3693. '*/fieldset',
  3694. ];
  3695. $this->assertHtml($expected, $result);
  3696. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3697. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3698. $this->assertContains('<fieldset>', $result);
  3699. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3700. $this->assertNotContains('<legend>', $result);
  3701. $this->assertNotContains('<fieldset>', $result);
  3702. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3703. $this->assertNotContains('<legend>', $result);
  3704. $this->assertNotContains('<fieldset>', $result);
  3705. $this->Form->create($this->article);
  3706. $this->Form->request = $this->Form->request
  3707. ->withParam('prefix', 'admin')
  3708. ->withParam('action', 'admin_edit')
  3709. ->withParam('controller', 'articles');
  3710. $result = $this->Form->allControls();
  3711. $expected = [
  3712. '<fieldset',
  3713. '<legend',
  3714. 'New Article',
  3715. '/legend',
  3716. '*/fieldset',
  3717. ];
  3718. $this->assertHtml($expected, $result);
  3719. $this->Form->create($this->article);
  3720. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3721. $expected = [
  3722. '<fieldset',
  3723. '<legend',
  3724. 'The Legend',
  3725. '/legend',
  3726. '*/fieldset',
  3727. ];
  3728. $this->assertHtml($expected, $result);
  3729. $this->Form->create($this->article);
  3730. $result = $this->Form->allControls([], [
  3731. 'fieldset' => [
  3732. 'class' => 'some-class some-other-class',
  3733. 'disabled' => true,
  3734. 'data-param' => 'a-param'
  3735. ],
  3736. 'legend' => 'The Legend'
  3737. ]);
  3738. $expected = [
  3739. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3740. '<legend',
  3741. 'The Legend',
  3742. '/legend',
  3743. '*/fieldset',
  3744. ];
  3745. $this->assertHtml($expected, $result);
  3746. }
  3747. /**
  3748. * testFormControls method
  3749. *
  3750. * Test the controls() method.
  3751. *
  3752. * @return void
  3753. */
  3754. public function testFormControls()
  3755. {
  3756. $this->Form->create($this->article);
  3757. $result = $this->Form->allControls();
  3758. $expected = [
  3759. '<fieldset',
  3760. '<legend', 'New Article', '/legend',
  3761. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3762. ['div' => ['class' => 'input select required']],
  3763. '*/div',
  3764. ['div' => ['class' => 'input text required']],
  3765. '*/div',
  3766. ['div' => ['class' => 'input text']],
  3767. '*/div',
  3768. ['div' => ['class' => 'input text']],
  3769. '*/div',
  3770. '/fieldset',
  3771. ];
  3772. $this->assertHtml($expected, $result);
  3773. $result = $this->Form->allControls([
  3774. 'published' => ['type' => 'boolean']
  3775. ]);
  3776. $expected = [
  3777. '<fieldset',
  3778. '<legend', 'New Article', '/legend',
  3779. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3780. ['div' => ['class' => 'input select required']],
  3781. '*/div',
  3782. ['div' => ['class' => 'input text required']],
  3783. '*/div',
  3784. ['div' => ['class' => 'input text']],
  3785. '*/div',
  3786. ['div' => ['class' => 'input boolean']],
  3787. '*/div',
  3788. '/fieldset',
  3789. ];
  3790. $this->assertHtml($expected, $result);
  3791. $this->Form->create($this->article);
  3792. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3793. $expected = [
  3794. 'fieldset' => [],
  3795. 'legend' => [],
  3796. 'Hello',
  3797. '/legend',
  3798. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3799. ['div' => ['class' => 'input select required']],
  3800. '*/div',
  3801. ['div' => ['class' => 'input text required']],
  3802. '*/div',
  3803. ['div' => ['class' => 'input text']],
  3804. '*/div',
  3805. ['div' => ['class' => 'input text']],
  3806. '*/div',
  3807. '/fieldset'
  3808. ];
  3809. $this->assertHtml($expected, $result);
  3810. $this->Form->create(false);
  3811. $expected = [
  3812. 'fieldset' => [],
  3813. ['div' => ['class' => 'input text']],
  3814. 'label' => ['for' => 'foo'],
  3815. 'Foo',
  3816. '/label',
  3817. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3818. '*/div',
  3819. '/fieldset'
  3820. ];
  3821. $result = $this->Form->allControls(
  3822. ['foo' => ['type' => 'text']],
  3823. ['legend' => false]
  3824. );
  3825. $this->assertHtml($expected, $result);
  3826. }
  3827. /**
  3828. * testFormControlsBlacklist method
  3829. *
  3830. * @return void
  3831. */
  3832. public function testFormControlsBlacklist()
  3833. {
  3834. $this->Form->create($this->article);
  3835. $result = $this->Form->allControls([
  3836. 'id' => false
  3837. ]);
  3838. $expected = [
  3839. '<fieldset',
  3840. '<legend', 'New Article', '/legend',
  3841. ['div' => ['class' => 'input select required']],
  3842. '*/div',
  3843. ['div' => ['class' => 'input text required']],
  3844. '*/div',
  3845. ['div' => ['class' => 'input text']],
  3846. '*/div',
  3847. ['div' => ['class' => 'input text']],
  3848. '*/div',
  3849. '/fieldset',
  3850. ];
  3851. $this->assertHtml($expected, $result);
  3852. $this->Form->create($this->article);
  3853. $result = $this->Form->allControls([
  3854. 'id' => []
  3855. ]);
  3856. $expected = [
  3857. '<fieldset',
  3858. '<legend', 'New Article', '/legend',
  3859. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3860. ['div' => ['class' => 'input select required']],
  3861. '*/div',
  3862. ['div' => ['class' => 'input text required']],
  3863. '*/div',
  3864. ['div' => ['class' => 'input text']],
  3865. '*/div',
  3866. ['div' => ['class' => 'input text']],
  3867. '*/div',
  3868. '/fieldset',
  3869. ];
  3870. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3871. }
  3872. /**
  3873. * testSelectAsCheckbox method
  3874. *
  3875. * Test multi-select widget with checkbox formatting.
  3876. *
  3877. * @return void
  3878. */
  3879. public function testSelectAsCheckbox()
  3880. {
  3881. $result = $this->Form->select(
  3882. 'Model.multi_field',
  3883. ['first', 'second', 'third'],
  3884. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3885. );
  3886. $expected = [
  3887. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3888. ['div' => ['class' => 'checkbox']],
  3889. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3890. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3891. 'first',
  3892. '/label',
  3893. '/div',
  3894. ['div' => ['class' => 'checkbox']],
  3895. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3896. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3897. 'second',
  3898. '/label',
  3899. '/div',
  3900. ['div' => ['class' => 'checkbox']],
  3901. ['label' => ['for' => 'model-multi-field-2']],
  3902. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3903. 'third',
  3904. '/label',
  3905. '/div',
  3906. ];
  3907. $this->assertHtml($expected, $result);
  3908. $result = $this->Form->select(
  3909. 'Model.multi_field',
  3910. ['1/2' => 'half'],
  3911. ['multiple' => 'checkbox']
  3912. );
  3913. $expected = [
  3914. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3915. ['div' => ['class' => 'checkbox']],
  3916. ['label' => ['for' => 'model-multi-field-1-2']],
  3917. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3918. 'half',
  3919. '/label',
  3920. '/div',
  3921. ];
  3922. $this->assertHtml($expected, $result);
  3923. }
  3924. /**
  3925. * testLabel method
  3926. *
  3927. * Test label generation.
  3928. *
  3929. * @return void
  3930. */
  3931. public function testLabel()
  3932. {
  3933. $result = $this->Form->label('Person.name');
  3934. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3935. $this->assertHtml($expected, $result);
  3936. $result = $this->Form->label('Person.name');
  3937. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3938. $this->assertHtml($expected, $result);
  3939. $result = $this->Form->label('Person.first_name');
  3940. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3941. $this->assertHtml($expected, $result);
  3942. $result = $this->Form->label('Person.first_name', 'Your first name');
  3943. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3944. $this->assertHtml($expected, $result);
  3945. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3946. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3947. $this->assertHtml($expected, $result);
  3948. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3949. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3950. $this->assertHtml($expected, $result);
  3951. $result = $this->Form->label('Person.first_name', '');
  3952. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3953. $this->assertHtml($expected, $result);
  3954. $result = $this->Form->label('Person.2.name', '');
  3955. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3956. $this->assertHtml($expected, $result);
  3957. }
  3958. /**
  3959. * testLabelContainControl method
  3960. *
  3961. * Test that label() can accept an input with the correct template vars.
  3962. *
  3963. * @return void
  3964. */
  3965. public function testLabelContainControl()
  3966. {
  3967. $this->Form->setTemplates([
  3968. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3969. ]);
  3970. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3971. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3972. ]);
  3973. $expected = [
  3974. 'label' => ['for' => 'person-accept-terms'],
  3975. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3976. 'Accept',
  3977. '/label',
  3978. ];
  3979. $this->assertHtml($expected, $result);
  3980. }
  3981. /**
  3982. * testTextbox method
  3983. *
  3984. * Test textbox element generation.
  3985. *
  3986. * @return void
  3987. */
  3988. public function testTextbox()
  3989. {
  3990. $result = $this->Form->text('Model.field');
  3991. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3992. $this->assertHtml($expected, $result);
  3993. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3994. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3995. $this->assertHtml($expected, $result);
  3996. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3997. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3998. $this->assertHtml($expected, $result);
  3999. }
  4000. /**
  4001. * testTextBoxDataAndError method
  4002. *
  4003. * Test that text() hooks up with request data and error fields.
  4004. *
  4005. * @return void
  4006. */
  4007. public function testTextBoxDataAndError()
  4008. {
  4009. $this->article['errors'] = [
  4010. 'Contact' => ['text' => 'wrong']
  4011. ];
  4012. $this->Form->request = $this->Form->request
  4013. ->withData('Model.text', 'test <strong>HTML</strong> values')
  4014. ->withData('Contact.text', 'test');
  4015. $this->Form->create($this->article);
  4016. $result = $this->Form->text('Model.text');
  4017. $expected = [
  4018. 'input' => [
  4019. 'type' => 'text',
  4020. 'name' => 'Model[text]',
  4021. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4022. ]
  4023. ];
  4024. $this->assertHtml($expected, $result);
  4025. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4026. $expected = [
  4027. 'input' => [
  4028. 'type' => 'text',
  4029. 'name' => 'Contact[text]',
  4030. 'value' => 'test',
  4031. 'id' => 'theID',
  4032. 'class' => 'form-error'
  4033. ]
  4034. ];
  4035. $this->assertHtml($expected, $result);
  4036. }
  4037. /**
  4038. * testDefaultValue method
  4039. *
  4040. * Test default value setting.
  4041. *
  4042. * @return void
  4043. */
  4044. public function testTextDefaultValue()
  4045. {
  4046. $this->Form->request = $this->Form->request->withData('Model.field', 'test');
  4047. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4048. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4049. $this->assertHtml($expected, $result);
  4050. $this->Form->request = $this->Form->request->withParsedBody([]);
  4051. $this->Form->create();
  4052. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4053. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4054. $this->assertHtml($expected, $result);
  4055. $Articles = TableRegistry::get('Articles');
  4056. $title = $Articles->getSchema()->getColumn('title');
  4057. $Articles->getSchema()->addColumn(
  4058. 'title',
  4059. ['default' => 'default title'] + $title
  4060. );
  4061. $entity = $Articles->newEntity();
  4062. $this->Form->create($entity);
  4063. // Get default value from schema
  4064. $result = $this->Form->text('title');
  4065. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4066. $this->assertHtml($expected, $result);
  4067. // Don't get value from schema
  4068. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4069. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4070. $this->assertHtml($expected, $result);
  4071. // Custom default value overrides default value from schema
  4072. $result = $this->Form->text('title', ['default' => 'override default']);
  4073. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4074. $this->assertHtml($expected, $result);
  4075. // Default value from schema is used only for new entities.
  4076. $entity->isNew(false);
  4077. $result = $this->Form->text('title');
  4078. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4079. $this->assertHtml($expected, $result);
  4080. }
  4081. /**
  4082. * testError method
  4083. *
  4084. * Test field error generation.
  4085. *
  4086. * @return void
  4087. */
  4088. public function testError()
  4089. {
  4090. $this->article['errors'] = [
  4091. 'Article' => ['field' => 'email']
  4092. ];
  4093. $this->Form->create($this->article);
  4094. $result = $this->Form->error('Article.field');
  4095. $expected = [
  4096. ['div' => ['class' => 'error-message']],
  4097. 'email',
  4098. '/div',
  4099. ];
  4100. $this->assertHtml($expected, $result);
  4101. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4102. $expected = [
  4103. ['div' => ['class' => 'error-message']],
  4104. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4105. '/div',
  4106. ];
  4107. $this->assertHtml($expected, $result);
  4108. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4109. $expected = [
  4110. ['div' => ['class' => 'error-message']],
  4111. '<strong', 'Badness!', '/strong',
  4112. '/div',
  4113. ];
  4114. $this->assertHtml($expected, $result);
  4115. }
  4116. /**
  4117. * testErrorRuleName method
  4118. *
  4119. * Test error translation can use rule names for translating.
  4120. *
  4121. * @return void
  4122. */
  4123. public function testErrorRuleName()
  4124. {
  4125. $this->article['errors'] = [
  4126. 'Article' => [
  4127. 'field' => ['email' => 'Your email was not good']
  4128. ]
  4129. ];
  4130. $this->Form->create($this->article);
  4131. $result = $this->Form->error('Article.field');
  4132. $expected = [
  4133. ['div' => ['class' => 'error-message']],
  4134. 'Your email was not good',
  4135. '/div',
  4136. ];
  4137. $this->assertHtml($expected, $result);
  4138. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4139. $expected = [
  4140. ['div' => ['class' => 'error-message']],
  4141. 'Email in use',
  4142. '/div',
  4143. ];
  4144. $this->assertHtml($expected, $result);
  4145. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4146. $expected = [
  4147. ['div' => ['class' => 'error-message']],
  4148. 'Email in use',
  4149. '/div',
  4150. ];
  4151. $this->assertHtml($expected, $result);
  4152. $result = $this->Form->error('Article.field', [
  4153. 'email' => 'Key is preferred',
  4154. 'Your email was not good' => 'Email in use'
  4155. ]);
  4156. $expected = [
  4157. ['div' => ['class' => 'error-message']],
  4158. 'Key is preferred',
  4159. '/div',
  4160. ];
  4161. $this->assertHtml($expected, $result);
  4162. }
  4163. /**
  4164. * testErrorMessages method
  4165. *
  4166. * Test error with nested lists.
  4167. *
  4168. * @return void
  4169. */
  4170. public function testErrorMessages()
  4171. {
  4172. $this->article['errors'] = [
  4173. 'Article' => ['field' => 'email']
  4174. ];
  4175. $this->Form->create($this->article);
  4176. $result = $this->Form->error('Article.field', [
  4177. 'email' => 'No good!'
  4178. ]);
  4179. $expected = [
  4180. 'div' => ['class' => 'error-message'],
  4181. 'No good!',
  4182. '/div'
  4183. ];
  4184. $this->assertHtml($expected, $result);
  4185. }
  4186. /**
  4187. * testErrorMultipleMessages method
  4188. *
  4189. * Test error() with multiple messages.
  4190. *
  4191. * @return void
  4192. */
  4193. public function testErrorMultipleMessages()
  4194. {
  4195. $this->article['errors'] = [
  4196. 'field' => ['notBlank', 'email', 'Something else']
  4197. ];
  4198. $this->Form->create($this->article);
  4199. $result = $this->Form->error('field', [
  4200. 'notBlank' => 'Cannot be empty',
  4201. 'email' => 'No good!'
  4202. ]);
  4203. $expected = [
  4204. 'div' => ['class' => 'error-message'],
  4205. 'ul' => [],
  4206. '<li', 'Cannot be empty', '/li',
  4207. '<li', 'No good!', '/li',
  4208. '<li', 'Something else', '/li',
  4209. '/ul',
  4210. '/div'
  4211. ];
  4212. $this->assertHtml($expected, $result);
  4213. }
  4214. /**
  4215. * testPassword method
  4216. *
  4217. * Test password element generation.
  4218. *
  4219. * @return void
  4220. */
  4221. public function testPassword()
  4222. {
  4223. $this->article['errors'] = [
  4224. 'Contact' => [
  4225. 'passwd' => 1
  4226. ]
  4227. ];
  4228. $this->Form->create($this->article);
  4229. $result = $this->Form->password('Contact.field');
  4230. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4231. $this->assertHtml($expected, $result);
  4232. $this->Form->request = $this->Form->request->withData('Contact.passwd', 'test');
  4233. $this->Form->create($this->article);
  4234. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4235. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4236. $this->assertHtml($expected, $result);
  4237. }
  4238. /**
  4239. * testRadio method
  4240. *
  4241. * Test radio element set generation.
  4242. *
  4243. * @return void
  4244. */
  4245. public function testRadio()
  4246. {
  4247. $result = $this->Form->radio('Model.field', ['option A']);
  4248. $expected = [
  4249. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4250. 'label' => ['for' => 'model-field-0'],
  4251. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4252. 'option A',
  4253. '/label'
  4254. ];
  4255. $this->assertHtml($expected, $result);
  4256. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4257. $this->assertHtml($expected, $result);
  4258. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4259. $expected = [
  4260. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4261. ['label' => ['for' => 'model-field-0']],
  4262. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4263. 'option A',
  4264. '/label',
  4265. ['label' => ['for' => 'model-field-1']],
  4266. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4267. 'option B',
  4268. '/label',
  4269. ];
  4270. $this->assertHtml($expected, $result);
  4271. $result = $this->Form->radio(
  4272. 'Employee.gender',
  4273. ['male' => 'Male', 'female' => 'Female'],
  4274. ['form' => 'my-form']
  4275. );
  4276. $expected = [
  4277. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4278. ['label' => ['for' => 'employee-gender-male']],
  4279. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4280. 'Male',
  4281. '/label',
  4282. ['label' => ['for' => 'employee-gender-female']],
  4283. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4284. 'Female',
  4285. '/label',
  4286. ];
  4287. $this->assertHtml($expected, $result);
  4288. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4289. $expected = [
  4290. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4291. ['label' => ['for' => 'model-custom-0']],
  4292. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4293. 'option A',
  4294. '/label',
  4295. ['label' => ['for' => 'model-custom-1']],
  4296. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4297. 'option B',
  4298. '/label',
  4299. ];
  4300. $this->assertHtml($expected, $result);
  4301. $result = $this->Form->radio(
  4302. 'Employee.gender',
  4303. [
  4304. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4305. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4306. ]
  4307. );
  4308. $expected = [
  4309. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4310. ['label' => ['for' => 'employee-gender-male']],
  4311. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4312. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4313. 'Male',
  4314. '/label',
  4315. ['label' => ['for' => 'employee-gender-female']],
  4316. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4317. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4318. 'Female',
  4319. '/label',
  4320. ];
  4321. $this->assertHtml($expected, $result);
  4322. }
  4323. /**
  4324. * Test radio with complex options and empty disabled data.
  4325. *
  4326. * @return void
  4327. */
  4328. public function testRadioComplexDisabled()
  4329. {
  4330. $options = [
  4331. ['value' => 'r', 'text' => 'red'],
  4332. ['value' => 'b', 'text' => 'blue'],
  4333. ];
  4334. $attrs = ['disabled' => []];
  4335. $result = $this->Form->radio('Model.field', $options, $attrs);
  4336. $expected = [
  4337. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4338. ['label' => ['for' => 'model-field-r']],
  4339. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4340. 'red',
  4341. '/label',
  4342. ['label' => ['for' => 'model-field-b']],
  4343. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4344. 'blue',
  4345. '/label',
  4346. ];
  4347. $this->assertHtml($expected, $result);
  4348. $attrs = ['disabled' => ['r']];
  4349. $result = $this->Form->radio('Model.field', $options, $attrs);
  4350. $this->assertContains('disabled="disabled"', $result);
  4351. }
  4352. /**
  4353. * testRadioDefaultValue method
  4354. *
  4355. * Test default value setting on radio() method.
  4356. *
  4357. * @return void
  4358. */
  4359. public function testRadioDefaultValue()
  4360. {
  4361. $Articles = TableRegistry::get('Articles');
  4362. $title = $Articles->getSchema()->getColumn('title');
  4363. $Articles->getSchema()->addColumn(
  4364. 'title',
  4365. ['default' => '1'] + $title
  4366. );
  4367. $this->Form->create($Articles->newEntity());
  4368. $result = $this->Form->radio('title', ['option A', 'option B']);
  4369. $expected = [
  4370. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4371. ['label' => ['for' => 'title-0']],
  4372. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4373. 'option A',
  4374. '/label',
  4375. ['label' => ['for' => 'title-1']],
  4376. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4377. 'option B',
  4378. '/label',
  4379. ];
  4380. $this->assertHtml($expected, $result);
  4381. }
  4382. /**
  4383. * Test setting a hiddenField value on radio buttons.
  4384. *
  4385. * @return void
  4386. */
  4387. public function testRadioHiddenFieldValue()
  4388. {
  4389. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4390. $expected = [
  4391. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4392. 'label' => ['for' => 'title-0'],
  4393. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4394. 'option A',
  4395. '/label',
  4396. ];
  4397. $this->assertHtml($expected, $result);
  4398. }
  4399. /**
  4400. * testControlRadio method
  4401. *
  4402. * Test that input works with radio types.
  4403. *
  4404. * @return void
  4405. */
  4406. public function testControlRadio()
  4407. {
  4408. $result = $this->Form->control('test', [
  4409. 'type' => 'radio',
  4410. 'options' => ['A', 'B'],
  4411. ]);
  4412. $expected = [
  4413. ['div' => ['class' => 'input radio']],
  4414. '<label',
  4415. 'Test',
  4416. '/label',
  4417. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4418. ['label' => ['for' => 'test-0']],
  4419. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4420. 'A',
  4421. '/label',
  4422. ['label' => ['for' => 'test-1']],
  4423. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4424. 'B',
  4425. '/label',
  4426. '/div',
  4427. ];
  4428. $this->assertHtml($expected, $result);
  4429. $result = $this->Form->control('test', [
  4430. 'type' => 'radio',
  4431. 'options' => ['A', 'B'],
  4432. 'value' => '0'
  4433. ]);
  4434. $expected = [
  4435. ['div' => ['class' => 'input radio']],
  4436. '<label',
  4437. 'Test',
  4438. '/label',
  4439. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4440. ['label' => ['for' => 'test-0']],
  4441. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4442. 'A',
  4443. '/label',
  4444. ['label' => ['for' => 'test-1']],
  4445. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4446. 'B',
  4447. '/label',
  4448. '/div',
  4449. ];
  4450. $this->assertHtml($expected, $result);
  4451. $result = $this->Form->control('test', [
  4452. 'type' => 'radio',
  4453. 'options' => ['A', 'B'],
  4454. 'label' => false
  4455. ]);
  4456. $expected = [
  4457. ['div' => ['class' => 'input radio']],
  4458. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4459. ['label' => ['for' => 'test-0']],
  4460. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4461. 'A',
  4462. '/label',
  4463. ['label' => ['for' => 'test-1']],
  4464. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4465. 'B',
  4466. '/label',
  4467. '/div',
  4468. ];
  4469. $this->assertHtml($expected, $result);
  4470. }
  4471. /**
  4472. * testRadioNoLabel method
  4473. *
  4474. * Test that radio() works with label = false.
  4475. *
  4476. * @return void
  4477. */
  4478. public function testRadioNoLabel()
  4479. {
  4480. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4481. $expected = [
  4482. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4483. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4484. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4485. ];
  4486. $this->assertHtml($expected, $result);
  4487. }
  4488. /**
  4489. * testRadioControlInsideLabel method
  4490. *
  4491. * Test generating radio input inside label ala twitter bootstrap.
  4492. *
  4493. * @return void
  4494. */
  4495. public function testRadioControlInsideLabel()
  4496. {
  4497. $this->Form->setTemplates([
  4498. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4499. 'radioWrapper' => '{{label}}'
  4500. ]);
  4501. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4502. //@codingStandardsIgnoreStart
  4503. $expected = [
  4504. ['input' => [
  4505. 'type' => 'hidden',
  4506. 'name' => 'Model[field]',
  4507. 'value' => ''
  4508. ]],
  4509. ['label' => ['for' => 'model-field-0']],
  4510. ['input' => [
  4511. 'type' => 'radio',
  4512. 'name' => 'Model[field]',
  4513. 'value' => '0',
  4514. 'id' => 'model-field-0'
  4515. ]],
  4516. 'option A',
  4517. '/label',
  4518. ['label' => ['for' => 'model-field-1']],
  4519. ['input' => [
  4520. 'type' => 'radio',
  4521. 'name' => 'Model[field]',
  4522. 'value' => '1',
  4523. 'id' => 'model-field-1'
  4524. ]],
  4525. 'option B',
  4526. '/label',
  4527. ];
  4528. //@codingStandardsIgnoreEnd
  4529. $this->assertHtml($expected, $result);
  4530. }
  4531. /**
  4532. * testRadioHiddenControlDisabling method
  4533. *
  4534. * Test disabling the hidden input for radio buttons.
  4535. *
  4536. * @return void
  4537. */
  4538. public function testRadioHiddenControlDisabling()
  4539. {
  4540. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4541. $expected = [
  4542. 'label' => ['for' => 'model-1-field-0'],
  4543. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4544. 'option A',
  4545. '/label'
  4546. ];
  4547. $this->assertHtml($expected, $result);
  4548. }
  4549. /**
  4550. * testRadioOutOfRange method
  4551. *
  4552. * Test radio element set generation.
  4553. *
  4554. * @return void
  4555. */
  4556. public function testRadioOutOfRange()
  4557. {
  4558. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4559. $expected = [
  4560. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4561. 'label' => ['for' => 'model-field-v'],
  4562. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4563. 'value',
  4564. '/label'
  4565. ];
  4566. $this->assertHtml($expected, $result);
  4567. }
  4568. /**
  4569. * testSelect method
  4570. *
  4571. * Test select element generation.
  4572. *
  4573. * @return void
  4574. */
  4575. public function testSelect()
  4576. {
  4577. $result = $this->Form->select('Model.field', []);
  4578. $expected = [
  4579. 'select' => ['name' => 'Model[field]'],
  4580. '/select'
  4581. ];
  4582. $this->assertHtml($expected, $result);
  4583. $this->Form->request = $this->Form->request->withData('Model', ['field' => 'value']);
  4584. $this->Form->create();
  4585. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4586. $expected = [
  4587. 'select' => ['name' => 'Model[field]'],
  4588. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4589. 'good',
  4590. '/option',
  4591. ['option' => ['value' => 'other']],
  4592. 'bad',
  4593. '/option',
  4594. '/select'
  4595. ];
  4596. $this->assertHtml($expected, $result);
  4597. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4598. $this->assertHtml($expected, $result);
  4599. $this->Form->request = $this->Form->request->withParsedBody([]);
  4600. $this->Form->create();
  4601. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4602. $expected = [
  4603. 'select' => ['name' => 'Model[field]'],
  4604. ['option' => ['value' => 'value']],
  4605. 'good',
  4606. '/option',
  4607. ['option' => ['value' => 'other']],
  4608. 'bad',
  4609. '/option',
  4610. '/select'
  4611. ];
  4612. $this->assertHtml($expected, $result);
  4613. $options = [
  4614. ['value' => 'first', 'text' => 'First'],
  4615. ['value' => 'first', 'text' => 'Another First'],
  4616. ];
  4617. $result = $this->Form->select(
  4618. 'Model.field',
  4619. $options,
  4620. ['escape' => false, 'empty' => false]
  4621. );
  4622. $expected = [
  4623. 'select' => ['name' => 'Model[field]'],
  4624. ['option' => ['value' => 'first']],
  4625. 'First',
  4626. '/option',
  4627. ['option' => ['value' => 'first']],
  4628. 'Another First',
  4629. '/option',
  4630. '/select'
  4631. ];
  4632. $this->assertHtml($expected, $result);
  4633. $this->Form->request = $this->Form->request->withParsedBody(['Model' => ['contact_id' => 228]]);
  4634. $this->Form->create();
  4635. $result = $this->Form->select(
  4636. 'Model.contact_id',
  4637. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4638. ['escape' => false, 'empty' => 'pick something']
  4639. );
  4640. $expected = [
  4641. 'select' => ['name' => 'Model[contact_id]'],
  4642. ['option' => ['value' => '']], 'pick something', '/option',
  4643. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4644. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4645. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4646. '/select'
  4647. ];
  4648. $this->assertHtml($expected, $result);
  4649. $this->Form->request = $this->Form->request->withData('Model.field', 0);
  4650. $this->Form->create();
  4651. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4652. $expected = [
  4653. 'select' => ['name' => 'Model[field]'],
  4654. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4655. ['option' => ['value' => '1']], 'Yes', '/option',
  4656. '/select'
  4657. ];
  4658. $this->assertHtml($expected, $result);
  4659. }
  4660. /**
  4661. * testSelectEscapeHtml method
  4662. *
  4663. * Test that select() escapes HTML.
  4664. *
  4665. * @return void
  4666. */
  4667. public function testSelectEscapeHtml()
  4668. {
  4669. $result = $this->Form->select(
  4670. 'Model.field',
  4671. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4672. ['empty' => false]
  4673. );
  4674. $expected = [
  4675. 'select' => ['name' => 'Model[field]'],
  4676. ['option' => ['value' => 'first']],
  4677. 'first &quot;html&quot; &lt;chars&gt;',
  4678. '/option',
  4679. ['option' => ['value' => 'second']],
  4680. 'value',
  4681. '/option',
  4682. '/select'
  4683. ];
  4684. $this->assertHtml($expected, $result);
  4685. $result = $this->Form->select(
  4686. 'Model.field',
  4687. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4688. ['escape' => false, 'empty' => false]
  4689. );
  4690. $expected = [
  4691. 'select' => ['name' => 'Model[field]'],
  4692. ['option' => ['value' => 'first']],
  4693. 'first "html" <chars>',
  4694. '/option',
  4695. ['option' => ['value' => 'second']],
  4696. 'value',
  4697. '/option',
  4698. '/select'
  4699. ];
  4700. $this->assertHtml($expected, $result);
  4701. }
  4702. /**
  4703. * testSelectRequired method
  4704. *
  4705. * Test select() with required and disabled attributes.
  4706. *
  4707. * @return void
  4708. */
  4709. public function testSelectRequired()
  4710. {
  4711. $this->article['required'] = [
  4712. 'user_id' => true
  4713. ];
  4714. $this->Form->create($this->article);
  4715. $result = $this->Form->select('user_id', ['option A']);
  4716. $expected = [
  4717. 'select' => [
  4718. 'name' => 'user_id',
  4719. 'required' => 'required'
  4720. ],
  4721. ['option' => ['value' => '0']], 'option A', '/option',
  4722. '/select'
  4723. ];
  4724. $this->assertHtml($expected, $result);
  4725. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4726. $expected = [
  4727. 'select' => [
  4728. 'name' => 'user_id',
  4729. 'disabled' => 'disabled'
  4730. ],
  4731. ['option' => ['value' => '0']], 'option A', '/option',
  4732. '/select'
  4733. ];
  4734. $this->assertHtml($expected, $result);
  4735. }
  4736. /**
  4737. * testNestedSelect method
  4738. *
  4739. * Test select element generation with optgroups.
  4740. *
  4741. * @return void
  4742. */
  4743. public function testNestedSelect()
  4744. {
  4745. $result = $this->Form->select(
  4746. 'Model.field',
  4747. [1 => 'One', 2 => 'Two', 'Three' => [
  4748. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4749. ]],
  4750. ['empty' => false]
  4751. );
  4752. $expected = [
  4753. 'select' => ['name' => 'Model[field]'],
  4754. ['option' => ['value' => 1]],
  4755. 'One',
  4756. '/option',
  4757. ['option' => ['value' => 2]],
  4758. 'Two',
  4759. '/option',
  4760. ['optgroup' => ['label' => 'Three']],
  4761. ['option' => ['value' => 3]],
  4762. 'Three',
  4763. '/option',
  4764. ['option' => ['value' => 4]],
  4765. 'Four',
  4766. '/option',
  4767. ['option' => ['value' => 5]],
  4768. 'Five',
  4769. '/option',
  4770. '/optgroup',
  4771. '/select'
  4772. ];
  4773. $this->assertHtml($expected, $result);
  4774. }
  4775. /**
  4776. * testSelectMultiple method
  4777. *
  4778. * Test generation of multiple select elements.
  4779. *
  4780. * @return void
  4781. */
  4782. public function testSelectMultiple()
  4783. {
  4784. $options = ['first', 'second', 'third'];
  4785. $result = $this->Form->select(
  4786. 'Model.multi_field',
  4787. $options,
  4788. ['form' => 'my-form', 'multiple' => true]
  4789. );
  4790. $expected = [
  4791. 'input' => [
  4792. 'type' => 'hidden',
  4793. 'name' => 'Model[multi_field]',
  4794. 'value' => '',
  4795. 'form' => 'my-form',
  4796. ],
  4797. 'select' => [
  4798. 'name' => 'Model[multi_field][]',
  4799. 'multiple' => 'multiple',
  4800. 'form' => 'my-form',
  4801. ],
  4802. ['option' => ['value' => '0']],
  4803. 'first',
  4804. '/option',
  4805. ['option' => ['value' => '1']],
  4806. 'second',
  4807. '/option',
  4808. ['option' => ['value' => '2']],
  4809. 'third',
  4810. '/option',
  4811. '/select'
  4812. ];
  4813. $this->assertHtml($expected, $result);
  4814. $result = $this->Form->select(
  4815. 'Model.multi_field',
  4816. $options,
  4817. ['multiple' => 'multiple', 'form' => 'my-form']
  4818. );
  4819. $this->assertHtml($expected, $result);
  4820. }
  4821. /**
  4822. * testCheckboxZeroValue method
  4823. *
  4824. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4825. *
  4826. * @return void
  4827. */
  4828. public function testCheckboxZeroValue()
  4829. {
  4830. $result = $this->Form->control('User.get_spam', [
  4831. 'type' => 'checkbox',
  4832. 'value' => '0',
  4833. 'hiddenField' => '1',
  4834. ]);
  4835. $expected = [
  4836. 'div' => ['class' => 'input checkbox'],
  4837. 'label' => ['for' => 'user-get-spam'],
  4838. ['input' => [
  4839. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4840. 'value' => '1'
  4841. ]],
  4842. ['input' => [
  4843. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4844. 'value' => '0', 'id' => 'user-get-spam'
  4845. ]],
  4846. 'Get Spam',
  4847. '/label',
  4848. '/div'
  4849. ];
  4850. $this->assertHtml($expected, $result);
  4851. }
  4852. /**
  4853. * testHabtmSelectBox method
  4854. *
  4855. * Test generation of habtm select boxes.
  4856. *
  4857. * @return void
  4858. */
  4859. public function testHabtmSelectBox()
  4860. {
  4861. $this->loadFixtures('Articles');
  4862. $options = [
  4863. 1 => 'blue',
  4864. 2 => 'red',
  4865. 3 => 'green'
  4866. ];
  4867. $tags = [
  4868. new Entity(['id' => 1, 'name' => 'blue']),
  4869. new Entity(['id' => 3, 'name' => 'green'])
  4870. ];
  4871. $article = new Article(['tags' => $tags]);
  4872. $this->Form->create($article);
  4873. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4874. $expected = [
  4875. 'div' => ['class' => 'input select'],
  4876. 'label' => ['for' => 'tags-ids'],
  4877. 'Tags',
  4878. '/label',
  4879. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4880. 'select' => [
  4881. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4882. 'multiple' => 'multiple'
  4883. ],
  4884. ['option' => ['value' => '1', 'selected' => 'selected']],
  4885. 'blue',
  4886. '/option',
  4887. ['option' => ['value' => '2']],
  4888. 'red',
  4889. '/option',
  4890. ['option' => ['value' => '3', 'selected' => 'selected']],
  4891. 'green',
  4892. '/option',
  4893. '/select',
  4894. '/div'
  4895. ];
  4896. $this->assertHtml($expected, $result);
  4897. // make sure only 50 is selected, and not 50f5c0cf
  4898. $options = [
  4899. '1' => 'blue',
  4900. '50f5c0cf' => 'red',
  4901. '50' => 'green'
  4902. ];
  4903. $tags = [
  4904. new Entity(['id' => 1, 'name' => 'blue']),
  4905. new Entity(['id' => 50, 'name' => 'green'])
  4906. ];
  4907. $article = new Article(['tags' => $tags]);
  4908. $this->Form->create($article);
  4909. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4910. $expected = [
  4911. 'div' => ['class' => 'input select'],
  4912. 'label' => ['for' => 'tags-ids'],
  4913. 'Tags',
  4914. '/label',
  4915. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4916. 'select' => [
  4917. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4918. 'multiple' => 'multiple'
  4919. ],
  4920. ['option' => ['value' => '1', 'selected' => 'selected']],
  4921. 'blue',
  4922. '/option',
  4923. ['option' => ['value' => '50f5c0cf']],
  4924. 'red',
  4925. '/option',
  4926. ['option' => ['value' => '50', 'selected' => 'selected']],
  4927. 'green',
  4928. '/option',
  4929. '/select',
  4930. '/div'
  4931. ];
  4932. $this->assertHtml($expected, $result);
  4933. $spacecraft = [
  4934. 1 => 'Orion',
  4935. 2 => 'Helios'
  4936. ];
  4937. $this->View->viewVars['spacecraft'] = $spacecraft;
  4938. $this->Form->create();
  4939. $result = $this->Form->control('spacecraft._ids');
  4940. $expected = [
  4941. 'div' => ['class' => 'input select'],
  4942. 'label' => ['for' => 'spacecraft-ids'],
  4943. 'Spacecraft',
  4944. '/label',
  4945. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4946. 'select' => [
  4947. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4948. 'multiple' => 'multiple'
  4949. ],
  4950. ['option' => ['value' => '1']],
  4951. 'Orion',
  4952. '/option',
  4953. ['option' => ['value' => '2']],
  4954. 'Helios',
  4955. '/option',
  4956. '/select',
  4957. '/div'
  4958. ];
  4959. $this->assertHtml($expected, $result);
  4960. }
  4961. /**
  4962. * testErrorsForBelongsToManySelect method
  4963. *
  4964. * Tests that errors for belongsToMany select fields are being
  4965. * picked up properly.
  4966. *
  4967. * @return void
  4968. */
  4969. public function testErrorsForBelongsToManySelect()
  4970. {
  4971. $spacecraft = [
  4972. 1 => 'Orion',
  4973. 2 => 'Helios'
  4974. ];
  4975. $this->View->viewVars['spacecraft'] = $spacecraft;
  4976. $article = new Article();
  4977. $article->setError('spacecraft', ['Invalid']);
  4978. $this->Form->create($article);
  4979. $result = $this->Form->control('spacecraft._ids');
  4980. $expected = [
  4981. ['div' => ['class' => 'input select error']],
  4982. 'label' => ['for' => 'spacecraft-ids'],
  4983. 'Spacecraft',
  4984. '/label',
  4985. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4986. 'select' => [
  4987. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4988. 'multiple' => 'multiple'
  4989. ],
  4990. ['option' => ['value' => '1']],
  4991. 'Orion',
  4992. '/option',
  4993. ['option' => ['value' => '2']],
  4994. 'Helios',
  4995. '/option',
  4996. '/select',
  4997. ['div' => ['class' => 'error-message']],
  4998. 'Invalid',
  4999. '/div',
  5000. '/div'
  5001. ];
  5002. $this->assertHtml($expected, $result);
  5003. }
  5004. /**
  5005. * testSelectMultipleCheckboxes method
  5006. *
  5007. * Test generation of multi select elements in checkbox format.
  5008. *
  5009. * @return void
  5010. */
  5011. public function testSelectMultipleCheckboxes()
  5012. {
  5013. $result = $this->Form->select(
  5014. 'Model.multi_field',
  5015. ['first', 'second', 'third'],
  5016. ['multiple' => 'checkbox']
  5017. );
  5018. $expected = [
  5019. 'input' => [
  5020. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5021. ],
  5022. ['div' => ['class' => 'checkbox']],
  5023. ['label' => ['for' => 'model-multi-field-0']],
  5024. ['input' => [
  5025. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5026. 'value' => '0', 'id' => 'model-multi-field-0'
  5027. ]],
  5028. 'first',
  5029. '/label',
  5030. '/div',
  5031. ['div' => ['class' => 'checkbox']],
  5032. ['label' => ['for' => 'model-multi-field-1']],
  5033. ['input' => [
  5034. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5035. 'value' => '1', 'id' => 'model-multi-field-1'
  5036. ]],
  5037. 'second',
  5038. '/label',
  5039. '/div',
  5040. ['div' => ['class' => 'checkbox']],
  5041. ['label' => ['for' => 'model-multi-field-2']],
  5042. ['input' => [
  5043. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5044. 'value' => '2', 'id' => 'model-multi-field-2'
  5045. ]],
  5046. 'third',
  5047. '/label',
  5048. '/div'
  5049. ];
  5050. $this->assertHtml($expected, $result);
  5051. $result = $this->Form->select(
  5052. 'Model.multi_field',
  5053. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5054. ['multiple' => 'checkbox']
  5055. );
  5056. $expected = [
  5057. 'input' => [
  5058. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5059. ],
  5060. ['div' => ['class' => 'checkbox']],
  5061. ['label' => ['for' => 'model-multi-field-a+']],
  5062. ['input' => [
  5063. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5064. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5065. ]],
  5066. 'first',
  5067. '/label',
  5068. '/div',
  5069. ['div' => ['class' => 'checkbox']],
  5070. ['label' => ['for' => 'model-multi-field-a++']],
  5071. ['input' => [
  5072. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5073. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5074. ]],
  5075. 'second',
  5076. '/label',
  5077. '/div',
  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. 'third',
  5085. '/label',
  5086. '/div'
  5087. ];
  5088. $this->assertHtml($expected, $result);
  5089. $result = $this->Form->select(
  5090. 'Model.multi_field',
  5091. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5092. ['multiple' => 'checkbox']
  5093. );
  5094. $expected = [
  5095. 'input' => [
  5096. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5097. ],
  5098. ['div' => ['class' => 'checkbox']],
  5099. ['label' => ['for' => 'model-multi-field-a-b']],
  5100. ['input' => [
  5101. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5102. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5103. ]],
  5104. 'first',
  5105. '/label',
  5106. '/div',
  5107. ['div' => ['class' => 'checkbox']],
  5108. ['label' => ['for' => 'model-multi-field-a-b1']],
  5109. ['input' => [
  5110. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5111. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5112. ]],
  5113. 'second',
  5114. '/label',
  5115. '/div',
  5116. ['div' => ['class' => 'checkbox']],
  5117. ['label' => ['for' => 'model-multi-field-a-b2']],
  5118. ['input' => [
  5119. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5120. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5121. ]],
  5122. 'third',
  5123. '/label',
  5124. '/div'
  5125. ];
  5126. $this->assertHtml($expected, $result);
  5127. }
  5128. /**
  5129. * testSelectMultipleCheckboxRequestData method
  5130. *
  5131. * Ensure that multiCheckbox reads from the request data.
  5132. *
  5133. * @return void
  5134. */
  5135. public function testSelectMultipleCheckboxRequestData()
  5136. {
  5137. $this->Form->request = $this->Form->request->withData('Model', ['tags' => [1]]);
  5138. $result = $this->Form->select(
  5139. 'Model.tags',
  5140. ['1' => 'first', 'Array' => 'Array'],
  5141. ['multiple' => 'checkbox']
  5142. );
  5143. $expected = [
  5144. 'input' => [
  5145. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5146. ],
  5147. ['div' => ['class' => 'checkbox']],
  5148. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5149. ['input' => [
  5150. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5151. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5152. ]],
  5153. 'first',
  5154. '/label',
  5155. '/div',
  5156. ['div' => ['class' => 'checkbox']],
  5157. ['label' => ['for' => 'model-tags-array']],
  5158. ['input' => [
  5159. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5160. 'value' => 'Array', 'id' => 'model-tags-array'
  5161. ]],
  5162. 'Array',
  5163. '/label',
  5164. '/div'
  5165. ];
  5166. $this->assertHtml($expected, $result);
  5167. }
  5168. /**
  5169. * testSelectMultipleCheckboxSecurity method
  5170. *
  5171. * Checks the security hash array generated for multiple-input checkbox elements.
  5172. *
  5173. * @return void
  5174. */
  5175. public function testSelectMultipleCheckboxSecurity()
  5176. {
  5177. $this->Form->request = $this->Form->request->withParam('_Token', 'testKey');
  5178. $this->assertEquals([], $this->Form->fields);
  5179. $this->Form->select(
  5180. 'Model.multi_field',
  5181. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5182. ['multiple' => 'checkbox']
  5183. );
  5184. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5185. $result = $this->Form->secure($this->Form->fields);
  5186. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5187. $this->assertContains('"' . $key . '"', $result);
  5188. }
  5189. /**
  5190. * testSelectMultipleSecureWithNoOptions method
  5191. *
  5192. * Multiple select elements should always be secured as they always participate
  5193. * in the POST data.
  5194. *
  5195. * @return void
  5196. */
  5197. public function testSelectMultipleSecureWithNoOptions()
  5198. {
  5199. $this->assertEquals([], $this->Form->fields);
  5200. $this->Form->select(
  5201. 'Model.select',
  5202. [],
  5203. ['multiple' => true]
  5204. );
  5205. $this->assertEquals(['Model.select'], $this->Form->fields);
  5206. }
  5207. /**
  5208. * testSelectNoSecureWithNoOptions method
  5209. *
  5210. * When a select box has no options it should not be added to the fields list
  5211. * as it always fail post validation.
  5212. *
  5213. * @return void
  5214. */
  5215. public function testSelectNoSecureWithNoOptions()
  5216. {
  5217. $this->Form->request = $this->Form->request->withParam('_Token', 'testkey');
  5218. $this->assertEquals([], $this->Form->fields);
  5219. $this->Form->select(
  5220. 'Model.select',
  5221. []
  5222. );
  5223. $this->assertEquals([], $this->Form->fields);
  5224. $this->Form->select(
  5225. 'Model.user_id',
  5226. [],
  5227. ['empty' => true]
  5228. );
  5229. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5230. }
  5231. /**
  5232. * testControlMultipleCheckboxes method
  5233. *
  5234. * Test control() resulting in multi select elements being generated.
  5235. *
  5236. * @return void
  5237. */
  5238. public function testControlMultipleCheckboxes()
  5239. {
  5240. $result = $this->Form->control('Model.multi_field', [
  5241. 'options' => ['first', 'second', 'third'],
  5242. 'multiple' => 'checkbox'
  5243. ]);
  5244. $expected = [
  5245. ['div' => ['class' => 'input select']],
  5246. ['label' => ['for' => 'model-multi-field']],
  5247. 'Multi Field',
  5248. '/label',
  5249. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5250. ['div' => ['class' => 'checkbox']],
  5251. ['label' => ['for' => 'model-multi-field-0']],
  5252. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5253. 'first',
  5254. '/label',
  5255. '/div',
  5256. ['div' => ['class' => 'checkbox']],
  5257. ['label' => ['for' => 'model-multi-field-1']],
  5258. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5259. 'second',
  5260. '/label',
  5261. '/div',
  5262. ['div' => ['class' => 'checkbox']],
  5263. ['label' => ['for' => 'model-multi-field-2']],
  5264. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5265. 'third',
  5266. '/label',
  5267. '/div',
  5268. '/div'
  5269. ];
  5270. $this->assertHtml($expected, $result);
  5271. $result = $this->Form->control('Model.multi_field', [
  5272. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5273. 'multiple' => 'checkbox'
  5274. ]);
  5275. $expected = [
  5276. ['div' => ['class' => 'input select']],
  5277. ['label' => ['for' => 'model-multi-field']],
  5278. 'Multi Field',
  5279. '/label',
  5280. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5281. ['div' => ['class' => 'checkbox']],
  5282. ['label' => ['for' => 'model-multi-field-a']],
  5283. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5284. 'first',
  5285. '/label',
  5286. '/div',
  5287. ['div' => ['class' => 'checkbox']],
  5288. ['label' => ['for' => 'model-multi-field-b']],
  5289. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5290. 'second',
  5291. '/label',
  5292. '/div',
  5293. ['div' => ['class' => 'checkbox']],
  5294. ['label' => ['for' => 'model-multi-field-c']],
  5295. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5296. 'third',
  5297. '/label',
  5298. '/div',
  5299. '/div'
  5300. ];
  5301. $this->assertHtml($expected, $result);
  5302. }
  5303. /**
  5304. * testSelectHiddenFieldOmission method
  5305. *
  5306. * Test that select() with 'hiddenField' => false omits the hidden field.
  5307. *
  5308. * @return void
  5309. */
  5310. public function testSelectHiddenFieldOmission()
  5311. {
  5312. $result = $this->Form->select(
  5313. 'Model.multi_field',
  5314. ['first', 'second'],
  5315. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5316. );
  5317. $this->assertNotContains('type="hidden"', $result);
  5318. }
  5319. /**
  5320. * testSelectCheckboxMultipleOverrideName method
  5321. *
  5322. * Test that select() with multiple = checkbox works with overriding name attribute.
  5323. *
  5324. * @return void
  5325. */
  5326. public function testSelectCheckboxMultipleOverrideName()
  5327. {
  5328. $result = $this->Form->select('category', ['1', '2'], [
  5329. 'multiple' => 'checkbox',
  5330. 'name' => 'fish',
  5331. ]);
  5332. $expected = [
  5333. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5334. ['div' => ['class' => 'checkbox']],
  5335. ['label' => ['for' => 'fish-0']],
  5336. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5337. '1',
  5338. '/label',
  5339. '/div',
  5340. ['div' => ['class' => 'checkbox']],
  5341. ['label' => ['for' => 'fish-1']],
  5342. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5343. '2',
  5344. '/label',
  5345. '/div'
  5346. ];
  5347. $this->assertHtml($expected, $result);
  5348. $result = $this->Form->multiCheckbox(
  5349. 'category',
  5350. new Collection(['1', '2']),
  5351. ['name' => 'fish']
  5352. );
  5353. $this->assertHtml($expected, $result);
  5354. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5355. 'name' => 'fish',
  5356. ]);
  5357. $this->assertHtml($expected, $result);
  5358. }
  5359. /**
  5360. * testControlMultiCheckbox method
  5361. *
  5362. * Test that control() works with multicheckbox.
  5363. *
  5364. * @return void
  5365. */
  5366. public function testControlMultiCheckbox()
  5367. {
  5368. $result = $this->Form->control('category', [
  5369. 'type' => 'multicheckbox',
  5370. 'options' => ['1', '2'],
  5371. ]);
  5372. $expected = [
  5373. ['div' => ['class' => 'input multicheckbox']],
  5374. '<label',
  5375. 'Category',
  5376. '/label',
  5377. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5378. ['div' => ['class' => 'checkbox']],
  5379. ['label' => ['for' => 'category-0']],
  5380. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5381. '1',
  5382. '/label',
  5383. '/div',
  5384. ['div' => ['class' => 'checkbox']],
  5385. ['label' => ['for' => 'category-1']],
  5386. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5387. '2',
  5388. '/label',
  5389. '/div',
  5390. '/div',
  5391. ];
  5392. $this->assertHtml($expected, $result);
  5393. }
  5394. /**
  5395. * testCheckbox method
  5396. *
  5397. * Test generation of checkboxes.
  5398. *
  5399. * @return void
  5400. */
  5401. public function testCheckbox()
  5402. {
  5403. $result = $this->Form->checkbox('Model.field');
  5404. $expected = [
  5405. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5406. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5407. ];
  5408. $this->assertHtml($expected, $result);
  5409. $result = $this->Form->checkbox('Model.field', [
  5410. 'id' => 'theID',
  5411. 'value' => 'myvalue',
  5412. 'form' => 'my-form',
  5413. ]);
  5414. $expected = [
  5415. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5416. ['input' => [
  5417. 'type' => 'checkbox', 'name' => 'Model[field]',
  5418. 'value' => 'myvalue', 'id' => 'theID',
  5419. 'form' => 'my-form',
  5420. ]]
  5421. ];
  5422. $this->assertHtml($expected, $result);
  5423. }
  5424. /**
  5425. * testCheckboxDefaultValue method
  5426. *
  5427. * Test default value setting on checkbox() method.
  5428. *
  5429. * @return void
  5430. */
  5431. public function testCheckboxDefaultValue()
  5432. {
  5433. $this->Form->request = $this->Form->request->withData('Model.field', false);
  5434. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5435. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5436. $this->assertHtml($expected, $result);
  5437. $this->Form->request = $this->Form->request->withData('Model.field', null);
  5438. $this->Form->create();
  5439. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5440. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5441. $this->assertHtml($expected, $result);
  5442. $this->Form->request = $this->Form->request->withData('Model.field', true);
  5443. $this->Form->create();
  5444. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5445. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5446. $this->assertHtml($expected, $result);
  5447. $this->Form->request = $this->Form->request->withData('Model.field', null);
  5448. $this->Form->create();
  5449. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5450. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5451. $this->assertHtml($expected, $result);
  5452. $Articles = TableRegistry::get('Articles');
  5453. $Articles->getSchema()->addColumn(
  5454. 'published',
  5455. ['type' => 'boolean', 'null' => false, 'default' => true]
  5456. );
  5457. $this->Form->create($Articles->newEntity());
  5458. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5459. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5460. $this->assertHtml($expected, $result);
  5461. }
  5462. /**
  5463. * testCheckboxCheckedAndError method
  5464. *
  5465. * Test checkbox being checked or having errors.
  5466. *
  5467. * @return void
  5468. */
  5469. public function testCheckboxCheckedAndError()
  5470. {
  5471. $this->article['errors'] = [
  5472. 'published' => true
  5473. ];
  5474. $this->Form->request = $this->Form->request->withData('published', 'myvalue');
  5475. $this->Form->create($this->article);
  5476. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5477. $expected = [
  5478. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5479. ['input' => [
  5480. 'type' => 'checkbox',
  5481. 'name' => 'published',
  5482. 'value' => 'myvalue',
  5483. 'id' => 'theID',
  5484. 'checked' => 'checked',
  5485. 'class' => 'form-error'
  5486. ]]
  5487. ];
  5488. $this->assertHtml($expected, $result);
  5489. $this->Form->request = $this->Form->request->withData('published', '');
  5490. $this->Form->create($this->article);
  5491. $result = $this->Form->checkbox('published');
  5492. $expected = [
  5493. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5494. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5495. ];
  5496. $this->assertHtml($expected, $result);
  5497. }
  5498. /**
  5499. * testCheckboxCustomNameAttribute method
  5500. *
  5501. * Test checkbox() with a custom name attribute.
  5502. *
  5503. * @return void
  5504. */
  5505. public function testCheckboxCustomNameAttribute()
  5506. {
  5507. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5508. $expected = [
  5509. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5510. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5511. ];
  5512. $this->assertHtml($expected, $result);
  5513. }
  5514. /**
  5515. * testCheckboxHiddenField method
  5516. *
  5517. * Test that the hidden input for checkboxes can be omitted or set to a
  5518. * specific value.
  5519. *
  5520. * @return void
  5521. */
  5522. public function testCheckboxHiddenField()
  5523. {
  5524. $result = $this->Form->checkbox('UserForm.something', [
  5525. 'hiddenField' => false
  5526. ]);
  5527. $expected = [
  5528. 'input' => [
  5529. 'type' => 'checkbox',
  5530. 'name' => 'UserForm[something]',
  5531. 'value' => '1'
  5532. ],
  5533. ];
  5534. $this->assertHtml($expected, $result);
  5535. $result = $this->Form->checkbox('UserForm.something', [
  5536. 'value' => 'Y',
  5537. 'hiddenField' => 'N',
  5538. ]);
  5539. $expected = [
  5540. ['input' => [
  5541. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5542. 'value' => 'N'
  5543. ]],
  5544. ['input' => [
  5545. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5546. 'value' => 'Y'
  5547. ]],
  5548. ];
  5549. $this->assertHtml($expected, $result);
  5550. }
  5551. /**
  5552. * testTime method
  5553. *
  5554. * Test the time type.
  5555. *
  5556. * @return void
  5557. */
  5558. public function testTime()
  5559. {
  5560. $result = $this->Form->time('start_time', [
  5561. 'timeFormat' => 12,
  5562. 'interval' => 5,
  5563. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5564. ]);
  5565. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5566. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5567. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5568. $this->assertNotContains('year', $result);
  5569. $this->assertNotContains('month', $result);
  5570. $this->assertNotContains('day', $result);
  5571. $result = $this->Form->time('start_time', [
  5572. 'timeFormat' => 12,
  5573. 'interval' => 5,
  5574. 'value' => '2014-03-08 16:30:00'
  5575. ]);
  5576. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5577. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5578. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5579. $this->assertNotContains('year', $result);
  5580. $this->assertNotContains('month', $result);
  5581. $this->assertNotContains('day', $result);
  5582. }
  5583. /**
  5584. * testTimeFormat24NoMeridian method
  5585. *
  5586. * Ensure that timeFormat=24 has no merdian.
  5587. *
  5588. * @return void.
  5589. */
  5590. public function testTimeFormat24NoMeridian()
  5591. {
  5592. $result = $this->Form->time('start_time', [
  5593. 'timeFormat' => 24,
  5594. 'interval' => 5,
  5595. 'value' => '2014-03-08 16:30:00'
  5596. ]);
  5597. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5598. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5599. $this->assertNotContains('meridian', $result);
  5600. $this->assertNotContains('pm', $result);
  5601. $this->assertNotContains('year', $result);
  5602. $this->assertNotContains('month', $result);
  5603. $this->assertNotContains('day', $result);
  5604. }
  5605. /**
  5606. * testDate method
  5607. *
  5608. * Test the date type.
  5609. *
  5610. * @return void
  5611. */
  5612. public function testDate()
  5613. {
  5614. $result = $this->Form->date('start_day', [
  5615. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5616. ]);
  5617. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5618. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5619. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5620. $this->assertNotContains('hour', $result);
  5621. $this->assertNotContains('minute', $result);
  5622. $this->assertNotContains('second', $result);
  5623. $this->assertNotContains('meridian', $result);
  5624. }
  5625. /**
  5626. * testDateTime method
  5627. *
  5628. * Test generation of date/time select elements.
  5629. *
  5630. * @return void
  5631. */
  5632. public function testDateTime()
  5633. {
  5634. extract($this->dateRegex);
  5635. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5636. $now = strtotime('now');
  5637. $expected = [
  5638. ['select' => ['name' => 'Contact[date][year]']],
  5639. ['option' => ['value' => '']],
  5640. '/option',
  5641. $yearsRegex,
  5642. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5643. date('Y', $now),
  5644. '/option',
  5645. '*/select',
  5646. ['select' => ['name' => 'Contact[date][month]']],
  5647. ['option' => ['value' => '']],
  5648. '/option',
  5649. $monthsRegex,
  5650. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5651. date('F', $now),
  5652. '/option',
  5653. '*/select',
  5654. ['select' => ['name' => 'Contact[date][day]']],
  5655. ['option' => ['value' => '']],
  5656. '/option',
  5657. $daysRegex,
  5658. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5659. date('j', $now),
  5660. '/option',
  5661. '*/select',
  5662. ['select' => ['name' => 'Contact[date][hour]']],
  5663. ['option' => ['value' => '']],
  5664. '/option',
  5665. $hoursRegex,
  5666. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5667. date('G', $now),
  5668. '/option',
  5669. '*/select',
  5670. ['select' => ['name' => 'Contact[date][minute]']],
  5671. ['option' => ['value' => '']],
  5672. '/option',
  5673. $minutesRegex,
  5674. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5675. date('i', $now),
  5676. '/option',
  5677. '*/select',
  5678. ];
  5679. $this->assertHtml($expected, $result);
  5680. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5681. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5682. $now = strtotime('now');
  5683. $expected = [
  5684. ['select' => ['name' => 'Contact[date][year]']],
  5685. $yearsRegex,
  5686. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5687. date('Y', $now),
  5688. '/option',
  5689. '*/select',
  5690. ['select' => ['name' => 'Contact[date][month]']],
  5691. $monthsRegex,
  5692. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5693. date('F', $now),
  5694. '/option',
  5695. '*/select',
  5696. ['select' => ['name' => 'Contact[date][day]']],
  5697. $daysRegex,
  5698. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5699. date('j', $now),
  5700. '/option',
  5701. '*/select',
  5702. ['select' => ['name' => 'Contact[date][hour]']],
  5703. $hoursRegex,
  5704. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5705. date('G', $now),
  5706. '/option',
  5707. '*/select',
  5708. ['select' => ['name' => 'Contact[date][minute]']],
  5709. $minutesRegex,
  5710. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5711. date('i', $now),
  5712. '/option',
  5713. '*/select',
  5714. ];
  5715. $this->assertHtml($expected, $result);
  5716. }
  5717. /**
  5718. * testDateTimeSecured method
  5719. *
  5720. * Test that datetime fields are added to protected fields list.
  5721. *
  5722. * @return void
  5723. */
  5724. public function testDateTimeSecured()
  5725. {
  5726. $this->Form->request = $this->Form->request->withParam('_Token', ['unlockedFields' => []]);
  5727. $this->Form->dateTime('Contact.date');
  5728. $expected = [
  5729. 'Contact.date.year',
  5730. 'Contact.date.month',
  5731. 'Contact.date.day',
  5732. 'Contact.date.hour',
  5733. 'Contact.date.minute',
  5734. ];
  5735. $this->assertEquals($expected, $this->Form->fields);
  5736. $this->Form->fields = [];
  5737. $this->Form->date('Contact.published');
  5738. $expected = [
  5739. 'Contact.published.year',
  5740. 'Contact.published.month',
  5741. 'Contact.published.day',
  5742. ];
  5743. $this->assertEquals($expected, $this->Form->fields);
  5744. }
  5745. /**
  5746. * testDateTimeSecuredDisabled method
  5747. *
  5748. * Test that datetime fields are added to protected fields list.
  5749. *
  5750. * @return void
  5751. */
  5752. public function testDateTimeSecuredDisabled()
  5753. {
  5754. $this->Form->request = $this->Form->request->withParam('_Token', ['unlockedFields' => []]);
  5755. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5756. $expected = [];
  5757. $this->assertEquals($expected, $this->Form->fields);
  5758. $this->Form->fields = [];
  5759. $this->Form->date('Contact.published', ['secure' => false]);
  5760. $expected = [];
  5761. $this->assertEquals($expected, $this->Form->fields);
  5762. }
  5763. /**
  5764. * testDatetimeEmpty method
  5765. *
  5766. * Test empty defaulting to true for datetime.
  5767. *
  5768. * @return void
  5769. */
  5770. public function testDatetimeEmpty()
  5771. {
  5772. extract($this->dateRegex);
  5773. $result = $this->Form->dateTime('Contact.date', [
  5774. 'timeFormat' => 12,
  5775. 'empty' => true,
  5776. 'default' => true
  5777. ]);
  5778. $expected = [
  5779. ['select' => ['name' => 'Contact[date][year]']],
  5780. $yearsRegex,
  5781. ['option' => ['value' => '']],
  5782. '/option',
  5783. '*/select',
  5784. ['select' => ['name' => 'Contact[date][month]']],
  5785. $monthsRegex,
  5786. ['option' => ['value' => '']],
  5787. '/option',
  5788. '*/select',
  5789. ['select' => ['name' => 'Contact[date][day]']],
  5790. $daysRegex,
  5791. ['option' => ['value' => '']],
  5792. '/option',
  5793. '*/select',
  5794. ['select' => ['name' => 'Contact[date][hour]']],
  5795. $hoursRegex,
  5796. ['option' => ['value' => '']],
  5797. '/option',
  5798. '*/select',
  5799. ['select' => ['name' => 'Contact[date][minute]']],
  5800. $minutesRegex,
  5801. ['option' => ['value' => '']],
  5802. '/option',
  5803. '*/select',
  5804. ['select' => ['name' => 'Contact[date][meridian]']],
  5805. $meridianRegex,
  5806. ['option' => ['value' => '']],
  5807. '/option',
  5808. '*/select'
  5809. ];
  5810. $this->assertHtml($expected, $result);
  5811. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5812. }
  5813. /**
  5814. * testDatetimeMinuteInterval method
  5815. *
  5816. * Test datetime with interval option.
  5817. *
  5818. * @return void
  5819. */
  5820. public function testDatetimeMinuteInterval()
  5821. {
  5822. extract($this->dateRegex);
  5823. $result = $this->Form->dateTime('Contact.date', [
  5824. 'interval' => 5,
  5825. 'value' => ''
  5826. ]);
  5827. $expected = [
  5828. ['select' => ['name' => 'Contact[date][year]']],
  5829. $yearsRegex,
  5830. ['option' => ['selected' => 'selected', 'value' => '']],
  5831. '/option',
  5832. '*/select',
  5833. ['select' => ['name' => 'Contact[date][month]']],
  5834. $monthsRegex,
  5835. ['option' => ['selected' => 'selected', 'value' => '']],
  5836. '/option',
  5837. '*/select',
  5838. ['select' => ['name' => 'Contact[date][day]']],
  5839. $daysRegex,
  5840. ['option' => ['selected' => 'selected', 'value' => '']],
  5841. '/option',
  5842. '*/select',
  5843. ['select' => ['name' => 'Contact[date][hour]']],
  5844. $hoursRegex,
  5845. ['option' => ['selected' => 'selected', 'value' => '']],
  5846. '/option',
  5847. '*/select',
  5848. ['select' => ['name' => 'Contact[date][minute]']],
  5849. $minutesRegex,
  5850. ['option' => ['selected' => 'selected', 'value' => '']],
  5851. '/option',
  5852. ['option' => ['value' => '00']],
  5853. '00',
  5854. '/option',
  5855. ['option' => ['value' => '05']],
  5856. '05',
  5857. '/option',
  5858. ['option' => ['value' => '10']],
  5859. '10',
  5860. '/option',
  5861. '*/select',
  5862. ];
  5863. $this->assertHtml($expected, $result);
  5864. }
  5865. /**
  5866. * testDateTimeRounding method
  5867. *
  5868. * Test dateTime with rounding.
  5869. *
  5870. * @return void
  5871. */
  5872. public function testDateTimeRounding()
  5873. {
  5874. $this->Form->request = $this->Form->request->withData('Contact', [
  5875. 'date' => [
  5876. 'day' => '13',
  5877. 'month' => '12',
  5878. 'year' => '2010',
  5879. 'hour' => '04',
  5880. 'minute' => '19',
  5881. 'meridian' => 'AM'
  5882. ]
  5883. ]);
  5884. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5885. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5886. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5887. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5888. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5889. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5890. }
  5891. /**
  5892. * testDatetimeWithDefault method
  5893. *
  5894. * Test that datetime() and default values work.
  5895. *
  5896. * @return void
  5897. */
  5898. public function testDatetimeWithDefault()
  5899. {
  5900. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5901. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5902. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5903. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5904. $result = $this->Form->dateTime('Contact.updated', [
  5905. 'default' => '2009-06-01 11:15:30'
  5906. ]);
  5907. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5908. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5909. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5910. }
  5911. /**
  5912. * testDateTimeAllZeros method
  5913. *
  5914. * Test datetime() with all zeros.
  5915. *
  5916. * @return void
  5917. */
  5918. public function testDateTimeAllZeros()
  5919. {
  5920. $result = $this->Form->dateTime('Contact.date', [
  5921. 'timeFormat' => false,
  5922. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5923. 'value' => '0000-00-00'
  5924. ]);
  5925. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5926. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5927. }
  5928. /**
  5929. * testDateTimeEmptyAsArray method
  5930. *
  5931. * @return void
  5932. */
  5933. public function testDateTimeEmptyAsArray()
  5934. {
  5935. $result = $this->Form->dateTime('Contact.date', [
  5936. 'empty' => [
  5937. 'day' => 'DAY',
  5938. 'month' => 'MONTH',
  5939. 'year' => 'YEAR',
  5940. 'hour' => 'HOUR',
  5941. 'minute' => 'MINUTE',
  5942. 'meridian' => false
  5943. ],
  5944. 'default' => true
  5945. ]);
  5946. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5947. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5948. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5949. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5950. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5951. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5952. $result = $this->Form->dateTime('Contact.date', [
  5953. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5954. 'default' => true
  5955. ]);
  5956. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5957. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5958. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5959. }
  5960. /**
  5961. * testFormDateTimeMulti method
  5962. *
  5963. * Test multiple datetime element generation.
  5964. *
  5965. * @return void
  5966. */
  5967. public function testFormDateTimeMulti()
  5968. {
  5969. extract($this->dateRegex);
  5970. $result = $this->Form->dateTime('Contact.1.updated');
  5971. $this->assertContains('Contact[1][updated][month]', $result);
  5972. $this->assertContains('Contact[1][updated][day]', $result);
  5973. $this->assertContains('Contact[1][updated][year]', $result);
  5974. $this->assertContains('Contact[1][updated][hour]', $result);
  5975. $this->assertContains('Contact[1][updated][minute]', $result);
  5976. }
  5977. /**
  5978. * testDateTimeLabelIdMatchesFirstControl method
  5979. *
  5980. * When changing the date format, the label should always focus the first select box when
  5981. * clicked.
  5982. *
  5983. * @return void
  5984. */
  5985. public function testDateTimeLabelIdMatchesFirstControl()
  5986. {
  5987. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5988. $this->assertContains('<label>Date</label>', $result);
  5989. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5990. $this->assertContains('<label>Date</label>', $result);
  5991. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5992. $this->assertContains('<label>Date</label>', $result);
  5993. }
  5994. /**
  5995. * testDateTimeSecondOptions method
  5996. *
  5997. * Test datetime second=true.
  5998. *
  5999. * @return void
  6000. */
  6001. public function testDateTimeSecondOptions()
  6002. {
  6003. $result = $this->Form->dateTime('updated', ['second' => true]);
  6004. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6005. $result = $this->Form->dateTime('updated', ['second' => []]);
  6006. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6007. $result = $this->Form->dateTime('updated', ['second' => null]);
  6008. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6009. $result = $this->Form->dateTime('updated', ['second' => false]);
  6010. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6011. }
  6012. /**
  6013. * testMonth method
  6014. *
  6015. * Test generation of a month input.
  6016. *
  6017. * @return void
  6018. */
  6019. public function testMonth()
  6020. {
  6021. $result = $this->Form->month('Model.field', ['value' => '']);
  6022. $expected = [
  6023. ['select' => ['name' => 'Model[field][month]']],
  6024. ['option' => ['value' => '', 'selected' => 'selected']],
  6025. '/option',
  6026. ['option' => ['value' => '01']],
  6027. date('F', strtotime('2008-01-01 00:00:00')),
  6028. '/option',
  6029. ['option' => ['value' => '02']],
  6030. date('F', strtotime('2008-02-01 00:00:00')),
  6031. '/option',
  6032. '*/select',
  6033. ];
  6034. $this->assertHtml($expected, $result);
  6035. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6036. $expected = [
  6037. ['select' => ['name' => 'Model[field][month]']],
  6038. ['option' => ['selected' => 'selected', 'value' => '']],
  6039. '/option',
  6040. ['option' => ['value' => '01']],
  6041. date('F', strtotime('2008-01-01 00:00:00')),
  6042. '/option',
  6043. ['option' => ['value' => '02']],
  6044. date('F', strtotime('2008-02-01 00:00:00')),
  6045. '/option',
  6046. '*/select',
  6047. ];
  6048. $this->assertHtml($expected, $result);
  6049. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6050. $expected = [
  6051. ['select' => ['name' => 'Model[field][month]']],
  6052. ['option' => ['selected' => 'selected', 'value' => '']],
  6053. '/option',
  6054. ['option' => ['value' => '01']],
  6055. '1',
  6056. '/option',
  6057. ['option' => ['value' => '02']],
  6058. '2',
  6059. '/option',
  6060. '*/select',
  6061. ];
  6062. $this->assertHtml($expected, $result);
  6063. $monthNames = [
  6064. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6065. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6066. ];
  6067. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6068. $expected = [
  6069. ['select' => ['name' => 'Model[field][month]']],
  6070. ['option' => ['value' => '']],
  6071. '/option',
  6072. ['option' => ['value' => '01', 'selected' => 'selected']],
  6073. 'Jan',
  6074. '/option',
  6075. ['option' => ['value' => '02']],
  6076. 'Feb',
  6077. '/option',
  6078. '*/select',
  6079. ];
  6080. $this->assertHtml($expected, $result);
  6081. $this->Form->request = $this->Form->request->withData('Project.release', '2050-02-10');
  6082. $this->Form->create();
  6083. $result = $this->Form->month('Project.release');
  6084. $expected = [
  6085. ['select' => ['name' => 'Project[release][month]']],
  6086. ['option' => ['value' => '']],
  6087. '/option',
  6088. ['option' => ['value' => '01']],
  6089. 'January',
  6090. '/option',
  6091. ['option' => ['value' => '02', 'selected' => 'selected']],
  6092. 'February',
  6093. '/option',
  6094. '*/select',
  6095. ];
  6096. $this->assertHtml($expected, $result);
  6097. $result = $this->Form->month('Contact.published', [
  6098. 'empty' => 'Published on',
  6099. ]);
  6100. $this->assertContains('Published on', $result);
  6101. }
  6102. /**
  6103. * testDay method
  6104. *
  6105. * Test generation of a day input.
  6106. *
  6107. * @return void
  6108. */
  6109. public function testDay()
  6110. {
  6111. extract($this->dateRegex);
  6112. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6113. $expected = [
  6114. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6115. ['option' => ['selected' => 'selected', 'value' => '']],
  6116. '/option',
  6117. ['option' => ['value' => '01']],
  6118. '1',
  6119. '/option',
  6120. ['option' => ['value' => '02']],
  6121. '2',
  6122. '/option',
  6123. $daysRegex,
  6124. '/select',
  6125. ];
  6126. $this->assertHtml($expected, $result);
  6127. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 23:12:32');
  6128. $this->Form->create();
  6129. $result = $this->Form->day('Model.field');
  6130. $expected = [
  6131. ['select' => ['name' => 'Model[field][day]']],
  6132. ['option' => ['value' => '']],
  6133. '/option',
  6134. ['option' => ['value' => '01']],
  6135. '1',
  6136. '/option',
  6137. ['option' => ['value' => '02']],
  6138. '2',
  6139. '/option',
  6140. $daysRegex,
  6141. ['option' => ['value' => '10', 'selected' => 'selected']],
  6142. '10',
  6143. '/option',
  6144. $daysRegex,
  6145. '/select',
  6146. ];
  6147. $this->assertHtml($expected, $result);
  6148. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6149. $this->Form->create();
  6150. $result = $this->Form->day('Model.field', ['value' => '10']);
  6151. $expected = [
  6152. ['select' => ['name' => 'Model[field][day]']],
  6153. ['option' => ['value' => '']],
  6154. '/option',
  6155. ['option' => ['value' => '01']],
  6156. '1',
  6157. '/option',
  6158. ['option' => ['value' => '02']],
  6159. '2',
  6160. '/option',
  6161. $daysRegex,
  6162. ['option' => ['value' => '10', 'selected' => 'selected']],
  6163. '10',
  6164. '/option',
  6165. $daysRegex,
  6166. '/select',
  6167. ];
  6168. $this->assertHtml($expected, $result);
  6169. $this->Form->request = $this->Form->request->withData('Project.release', '2050-10-10');
  6170. $this->Form->create();
  6171. $result = $this->Form->day('Project.release');
  6172. $expected = [
  6173. ['select' => ['name' => 'Project[release][day]']],
  6174. ['option' => ['value' => '']],
  6175. '/option',
  6176. ['option' => ['value' => '01']],
  6177. '1',
  6178. '/option',
  6179. ['option' => ['value' => '02']],
  6180. '2',
  6181. '/option',
  6182. $daysRegex,
  6183. ['option' => ['value' => '10', 'selected' => 'selected']],
  6184. '10',
  6185. '/option',
  6186. $daysRegex,
  6187. '/select',
  6188. ];
  6189. $this->assertHtml($expected, $result);
  6190. $result = $this->Form->day('Contact.published', [
  6191. 'empty' => 'Published on',
  6192. ]);
  6193. $this->assertContains('Published on', $result);
  6194. }
  6195. /**
  6196. * testMinute method
  6197. *
  6198. * Test generation of a minute input.
  6199. *
  6200. * @return void
  6201. */
  6202. public function testMinute()
  6203. {
  6204. extract($this->dateRegex);
  6205. $result = $this->Form->minute('Model.field', ['value' => '']);
  6206. $expected = [
  6207. ['select' => ['name' => 'Model[field][minute]']],
  6208. ['option' => ['selected' => 'selected', 'value' => '']],
  6209. '/option',
  6210. ['option' => ['value' => '00']],
  6211. '00',
  6212. '/option',
  6213. ['option' => ['value' => '01']],
  6214. '01',
  6215. '/option',
  6216. ['option' => ['value' => '02']],
  6217. '02',
  6218. '/option',
  6219. $minutesRegex,
  6220. '/select',
  6221. ];
  6222. $this->assertHtml($expected, $result);
  6223. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6224. $this->Form->create();
  6225. $result = $this->Form->minute('Model.field');
  6226. $expected = [
  6227. ['select' => ['name' => 'Model[field][minute]']],
  6228. ['option' => ['value' => '']],
  6229. '/option',
  6230. ['option' => ['value' => '00']],
  6231. '00',
  6232. '/option',
  6233. ['option' => ['value' => '01']],
  6234. '01',
  6235. '/option',
  6236. ['option' => ['value' => '02']],
  6237. '02',
  6238. '/option',
  6239. $minutesRegex,
  6240. ['option' => ['value' => '12', 'selected' => 'selected']],
  6241. '12',
  6242. '/option',
  6243. $minutesRegex,
  6244. '/select',
  6245. ];
  6246. $this->assertHtml($expected, $result);
  6247. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6248. $this->Form->create();
  6249. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6250. $expected = [
  6251. ['select' => ['name' => 'Model[field][minute]']],
  6252. ['option' => ['selected' => 'selected', 'value' => '']],
  6253. '/option',
  6254. ['option' => ['value' => '00']],
  6255. '00',
  6256. '/option',
  6257. ['option' => ['value' => '05']],
  6258. '05',
  6259. '/option',
  6260. ['option' => ['value' => '10']],
  6261. '10',
  6262. '/option',
  6263. $minutesRegex,
  6264. '/select',
  6265. ];
  6266. $this->assertHtml($expected, $result);
  6267. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:10:32');
  6268. $this->Form->create();
  6269. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6270. $expected = [
  6271. ['select' => ['name' => 'Model[field][minute]']],
  6272. ['option' => ['value' => '']],
  6273. '/option',
  6274. ['option' => ['value' => '00']],
  6275. '00',
  6276. '/option',
  6277. ['option' => ['value' => '05']],
  6278. '05',
  6279. '/option',
  6280. ['option' => ['value' => '10', 'selected' => 'selected']],
  6281. '10',
  6282. '/option',
  6283. $minutesRegex,
  6284. '/select',
  6285. ];
  6286. $this->assertHtml($expected, $result);
  6287. }
  6288. /**
  6289. * testMeridian method
  6290. *
  6291. * Test generating an input for the meridian.
  6292. *
  6293. * @return void
  6294. */
  6295. public function testMeridian()
  6296. {
  6297. extract($this->dateRegex);
  6298. $now = new \DateTime();
  6299. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6300. $expected = [
  6301. ['select' => ['name' => 'Model[field][meridian]']],
  6302. ['option' => ['value' => '']],
  6303. '/option',
  6304. $meridianRegex,
  6305. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6306. $now->format('a'),
  6307. '/option',
  6308. '*/select'
  6309. ];
  6310. $this->assertHtml($expected, $result);
  6311. }
  6312. /**
  6313. * testHour method
  6314. *
  6315. * Test generation of an hour input.
  6316. *
  6317. * @return void
  6318. */
  6319. public function testHour()
  6320. {
  6321. extract($this->dateRegex);
  6322. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6323. $expected = [
  6324. ['select' => ['name' => 'Model[field][hour]']],
  6325. ['option' => ['selected' => 'selected', 'value' => '']],
  6326. '/option',
  6327. ['option' => ['value' => '01']],
  6328. '1',
  6329. '/option',
  6330. ['option' => ['value' => '02']],
  6331. '2',
  6332. '/option',
  6333. $hoursRegex,
  6334. '/select',
  6335. ];
  6336. $this->assertHtml($expected, $result);
  6337. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6338. $this->Form->create();
  6339. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6340. $expected = [
  6341. ['select' => ['name' => 'Model[field][hour]']],
  6342. ['option' => ['value' => '']],
  6343. '/option',
  6344. ['option' => ['value' => '01']],
  6345. '1',
  6346. '/option',
  6347. ['option' => ['value' => '02']],
  6348. '2',
  6349. '/option',
  6350. $hoursRegex,
  6351. ['option' => ['value' => '12', 'selected' => 'selected']],
  6352. '12',
  6353. '/option',
  6354. '/select',
  6355. ];
  6356. $this->assertHtml($expected, $result);
  6357. $this->Form->request = $this->Form->request->withData('Model.field', '');
  6358. $this->Form->create();
  6359. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6360. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6361. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6362. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6363. $this->Form->request = $this->Form->request->withData('Model.field', '2006-10-10 00:12:32');
  6364. $this->Form->create();
  6365. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6366. $expected = [
  6367. ['select' => ['name' => 'Model[field][hour]']],
  6368. ['option' => ['value' => '']],
  6369. '/option',
  6370. ['option' => ['value' => '00', 'selected' => 'selected']],
  6371. '0',
  6372. '/option',
  6373. ['option' => ['value' => '01']],
  6374. '1',
  6375. '/option',
  6376. ['option' => ['value' => '02']],
  6377. '2',
  6378. '/option',
  6379. $hoursRegex,
  6380. '/select',
  6381. ];
  6382. $this->assertHtml($expected, $result);
  6383. $this->Form->request = $this->Form->request->withData('Model.field', null);
  6384. $this->Form->create();
  6385. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6386. $thisHour = date('H');
  6387. $optValue = date('G');
  6388. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6389. $this->Form->request = $this->Form->request->withData('Model.field', '2050-10-10 01:12:32');
  6390. $this->Form->create();
  6391. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6392. $expected = [
  6393. ['select' => ['name' => 'Model[field][hour]']],
  6394. ['option' => ['value' => '']],
  6395. '/option',
  6396. ['option' => ['value' => '00']],
  6397. '0',
  6398. '/option',
  6399. ['option' => ['value' => '01', 'selected' => 'selected']],
  6400. '1',
  6401. '/option',
  6402. ['option' => ['value' => '02']],
  6403. '2',
  6404. '/option',
  6405. $hoursRegex,
  6406. '/select',
  6407. ];
  6408. $this->assertHtml($expected, $result);
  6409. }
  6410. /**
  6411. * testYear method
  6412. *
  6413. * Test generation of a year input.
  6414. *
  6415. * @return void
  6416. */
  6417. public function testYear()
  6418. {
  6419. $this->Form->request = $this->Form->request->withData('Contact.published', '2006-10-10');
  6420. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6421. $expected = [
  6422. ['select' => ['name' => 'Model[field][year]']],
  6423. ['option' => ['selected' => 'selected', 'value' => '']],
  6424. '/option',
  6425. ['option' => ['value' => '2007']],
  6426. '2007',
  6427. '/option',
  6428. ['option' => ['value' => '2006']],
  6429. '2006',
  6430. '/option',
  6431. '/select',
  6432. ];
  6433. $this->assertHtml($expected, $result);
  6434. $result = $this->Form->year('Model.field', [
  6435. 'value' => '',
  6436. 'minYear' => 2006,
  6437. 'maxYear' => 2007,
  6438. 'orderYear' => 'asc'
  6439. ]);
  6440. $expected = [
  6441. ['select' => ['name' => 'Model[field][year]']],
  6442. ['option' => ['selected' => 'selected', 'value' => '']],
  6443. '/option',
  6444. ['option' => ['value' => '2006']],
  6445. '2006',
  6446. '/option',
  6447. ['option' => ['value' => '2007']],
  6448. '2007',
  6449. '/option',
  6450. '/select',
  6451. ];
  6452. $this->assertHtml($expected, $result);
  6453. $result = $this->Form->year('Contact.published', [
  6454. 'empty' => false,
  6455. 'minYear' => 2006,
  6456. 'maxYear' => 2007,
  6457. ]);
  6458. $expected = [
  6459. ['select' => ['name' => 'Contact[published][year]']],
  6460. ['option' => ['value' => '2007']],
  6461. '2007',
  6462. '/option',
  6463. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6464. '2006',
  6465. '/option',
  6466. '/select',
  6467. ];
  6468. $this->assertHtml($expected, $result);
  6469. $result = $this->Form->year('Contact.published', [
  6470. 'empty' => 'Published on',
  6471. ]);
  6472. $this->assertContains('Published on', $result);
  6473. }
  6474. /**
  6475. * testControlDatetimePreEpoch method
  6476. *
  6477. * Test minYear being prior to the unix epoch.
  6478. *
  6479. * @return void
  6480. */
  6481. public function testControlDatetimePreEpoch()
  6482. {
  6483. $start = date('Y') - 80;
  6484. $end = date('Y') - 18;
  6485. $result = $this->Form->control('birth_year', [
  6486. 'type' => 'date',
  6487. 'label' => 'Birth Year',
  6488. 'minYear' => $start,
  6489. 'maxYear' => $end,
  6490. 'month' => false,
  6491. 'day' => false,
  6492. ]);
  6493. $this->assertContains('value="' . $start . '">' . $start, $result);
  6494. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6495. $this->assertNotContains('value="00">00', $result);
  6496. }
  6497. /**
  6498. * test control() datetime & required attributes
  6499. *
  6500. * @return void
  6501. */
  6502. public function testControlDatetimeRequired()
  6503. {
  6504. $result = $this->Form->control('birthday', [
  6505. 'type' => 'date',
  6506. 'required' => true
  6507. ]);
  6508. $this->assertContains(
  6509. '<select name="birthday[year]" required="required"',
  6510. $result
  6511. );
  6512. }
  6513. /**
  6514. * testYearAutoExpandRange method
  6515. *
  6516. * @return void
  6517. */
  6518. public function testYearAutoExpandRange()
  6519. {
  6520. $this->Form->request = $this->Form->request->withData('User.birthday', '1930-10-10');
  6521. $result = $this->Form->year('User.birthday');
  6522. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6523. $result = $matches[1];
  6524. $expected = range(date('Y') + 5, 1930);
  6525. $this->assertEquals($expected, $result);
  6526. $this->Form->request = $this->Form->request->withData('Project.release', '2050-10-10');
  6527. $this->Form->create();
  6528. $result = $this->Form->year('Project.release');
  6529. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6530. $result = $matches[1];
  6531. $expected = range(2050, date('Y') - 5);
  6532. $this->assertEquals($expected, $result);
  6533. $this->Form->request = $this->Form->request->withData('Project.release', '1881-10-10');
  6534. $this->Form->create();
  6535. $result = $this->Form->year('Project.release', [
  6536. 'minYear' => 1890,
  6537. 'maxYear' => 1900
  6538. ]);
  6539. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6540. $result = $matches[1];
  6541. $expected = range(1900, 1881);
  6542. $this->assertEquals($expected, $result);
  6543. }
  6544. /**
  6545. * testControlDate method
  6546. *
  6547. * Test that control() accepts the type of date and passes options in.
  6548. *
  6549. * @return void
  6550. */
  6551. public function testControlDate()
  6552. {
  6553. $this->Form->request = $this->Form->request->withParsedBody([
  6554. 'month_year' => ['month' => date('m')],
  6555. ]);
  6556. $this->Form->create($this->article);
  6557. $result = $this->Form->control('month_year', [
  6558. 'label' => false,
  6559. 'type' => 'date',
  6560. 'minYear' => 2006,
  6561. 'maxYear' => 2008
  6562. ]);
  6563. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6564. $this->assertNotContains('value="2008" selected="selected"', $result);
  6565. }
  6566. /**
  6567. * testControlLabelFalse method
  6568. *
  6569. * Test the label option being set to false.
  6570. *
  6571. * @return void
  6572. */
  6573. public function testControlLabelFalse()
  6574. {
  6575. $this->Form->create($this->article);
  6576. $result = $this->Form->control('title', ['label' => false]);
  6577. $expected = [
  6578. 'div' => ['class' => 'input text required'],
  6579. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6580. '/div'
  6581. ];
  6582. $this->assertHtml($expected, $result);
  6583. }
  6584. /**
  6585. * testControlDateMaxYear method
  6586. *
  6587. * Let's say we want to only allow users born from 2006 to 2008 to register
  6588. * This being the first signup page, we still don't have any data.
  6589. *
  6590. * @return void
  6591. */
  6592. public function testControlDateMaxYear()
  6593. {
  6594. $this->Form->create($this->article);
  6595. $result = $this->Form->control('birthday', [
  6596. 'label' => false,
  6597. 'type' => 'date',
  6598. 'minYear' => 2006,
  6599. 'maxYear' => 2008,
  6600. 'default' => true
  6601. ]);
  6602. $this->assertContains('value="2008" selected="selected"', $result);
  6603. $this->assertContains('value="2006"', $result);
  6604. $this->assertNotContains('value="2005"', $result);
  6605. $this->assertNotContains('value="2009"', $result);
  6606. }
  6607. /**
  6608. * testTextArea method
  6609. *
  6610. * Test generation of a textarea input.
  6611. *
  6612. * @return void
  6613. */
  6614. public function testTextArea()
  6615. {
  6616. $this->Form->request = $this->Form->request->withData('field', 'some test data');
  6617. $result = $this->Form->textarea('field');
  6618. $expected = [
  6619. 'textarea' => ['name' => 'field', 'rows' => 5],
  6620. 'some test data',
  6621. '/textarea',
  6622. ];
  6623. $this->assertHtml($expected, $result);
  6624. $result = $this->Form->textarea('user.bio');
  6625. $expected = [
  6626. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6627. '/textarea',
  6628. ];
  6629. $this->assertHtml($expected, $result);
  6630. $this->Form->request = $this->Form->request
  6631. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  6632. $this->Form->create();
  6633. $result = $this->Form->textarea('field');
  6634. $expected = [
  6635. 'textarea' => ['name' => 'field', 'rows' => 5],
  6636. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6637. '/textarea',
  6638. ];
  6639. $this->assertHtml($expected, $result);
  6640. $this->Form->request = $this->Form->request->withData(
  6641. 'Model.field',
  6642. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6643. );
  6644. $this->Form->create();
  6645. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6646. $expected = [
  6647. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6648. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6649. '/textarea',
  6650. ];
  6651. $this->assertHtml($expected, $result);
  6652. $result = $this->Form->textarea('0.OtherModel.field');
  6653. $expected = [
  6654. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6655. '/textarea'
  6656. ];
  6657. $this->assertHtml($expected, $result);
  6658. }
  6659. /**
  6660. * testTextAreaWithStupidCharacters method
  6661. *
  6662. * Test text area with non-ascii characters.
  6663. *
  6664. * @return void
  6665. */
  6666. public function testTextAreaWithStupidCharacters()
  6667. {
  6668. $result = $this->Form->textarea('Post.content', [
  6669. 'value' => 'GREAT®',
  6670. 'rows' => '15',
  6671. 'cols' => '75'
  6672. ]);
  6673. $expected = [
  6674. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6675. 'GREAT®',
  6676. '/textarea',
  6677. ];
  6678. $this->assertHtml($expected, $result);
  6679. }
  6680. /**
  6681. * testTextAreaMaxLength method
  6682. *
  6683. * Test textareas maxlength read from schema.
  6684. *
  6685. * @return void
  6686. */
  6687. public function testTextAreaMaxLength()
  6688. {
  6689. $this->Form->create([
  6690. 'schema' => [
  6691. 'stuff' => ['type' => 'string', 'length' => 10],
  6692. ]
  6693. ]);
  6694. $result = $this->Form->control('other', ['type' => 'textarea']);
  6695. $expected = [
  6696. 'div' => ['class' => 'input textarea'],
  6697. 'label' => ['for' => 'other'],
  6698. 'Other',
  6699. '/label',
  6700. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6701. '/textarea',
  6702. '/div'
  6703. ];
  6704. $this->assertHtml($expected, $result);
  6705. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6706. $expected = [
  6707. 'div' => ['class' => 'input textarea'],
  6708. 'label' => ['for' => 'stuff'],
  6709. 'Stuff',
  6710. '/label',
  6711. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6712. '/textarea',
  6713. '/div'
  6714. ];
  6715. $this->assertHtml($expected, $result);
  6716. }
  6717. /**
  6718. * testHiddenField method
  6719. *
  6720. * Test generation of a hidden input.
  6721. *
  6722. * @return void
  6723. */
  6724. public function testHiddenField()
  6725. {
  6726. $this->article['errors'] = [
  6727. 'field' => true
  6728. ];
  6729. $this->Form->request = $this->Form->request->withData('field', 'test');
  6730. $this->Form->create($this->article);
  6731. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6732. $expected = [
  6733. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6734. $this->assertHtml($expected, $result);
  6735. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6736. $expected = [
  6737. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6738. ];
  6739. $this->assertHtml($expected, $result);
  6740. }
  6741. /**
  6742. * testFileUploadField method
  6743. *
  6744. * Test generation of a file upload input.
  6745. *
  6746. * @return void
  6747. */
  6748. public function testFileUploadField()
  6749. {
  6750. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6751. $result = $this->Form->file('Model.upload');
  6752. $this->assertHtml($expected, $result);
  6753. $this->Form->request = $this->Form->request->withData('Model.upload', [
  6754. 'name' => '', 'type' => '', 'tmp_name' => '',
  6755. 'error' => 4, 'size' => 0
  6756. ]);
  6757. $result = $this->Form->file('Model.upload');
  6758. $this->assertHtml($expected, $result);
  6759. $this->Form->request = $this->Form->request->withData('Model.upload', 'no data should be set in value');
  6760. $result = $this->Form->file('Model.upload');
  6761. $this->assertHtml($expected, $result);
  6762. }
  6763. /**
  6764. * testFileUploadOnOtherModel method
  6765. *
  6766. * Test File upload input on a model not used in create().
  6767. *
  6768. * @return void
  6769. */
  6770. public function testFileUploadOnOtherModel()
  6771. {
  6772. $this->Form->create($this->article, ['type' => 'file']);
  6773. $result = $this->Form->file('ValidateProfile.city');
  6774. $expected = [
  6775. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6776. ];
  6777. $this->assertHtml($expected, $result);
  6778. }
  6779. /**
  6780. * testButton method
  6781. *
  6782. * Test generation of a form button.
  6783. *
  6784. * @return void
  6785. */
  6786. public function testButton()
  6787. {
  6788. $result = $this->Form->button('Hi');
  6789. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6790. $this->assertHtml($expected, $result);
  6791. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6792. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6793. $this->assertHtml($expected, $result);
  6794. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6795. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6796. $this->assertHtml($expected, $result);
  6797. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6798. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6799. $this->assertHtml($expected, $result);
  6800. $result = $this->Form->button('No type', ['type' => false]);
  6801. $expected = ['button' => [], 'No type', '/button'];
  6802. $this->assertHtml($expected, $result);
  6803. $result = $this->Form->button('Upload Text', [
  6804. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6805. 'escape' => false
  6806. ]);
  6807. $this->assertNotRegExp('/\&039/', $result);
  6808. }
  6809. /**
  6810. * testButtonUnlockedByDefault method
  6811. *
  6812. * Test that button() makes unlocked fields by default.
  6813. *
  6814. * @return void
  6815. */
  6816. public function testButtonUnlockedByDefault()
  6817. {
  6818. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'secured');
  6819. $this->Form->button('Save', ['name' => 'save']);
  6820. $this->Form->button('Clear');
  6821. $result = $this->Form->unlockField();
  6822. $this->assertEquals(['save'], $result);
  6823. }
  6824. /**
  6825. * Test generation of a form button with confirm message.
  6826. *
  6827. * @return void
  6828. */
  6829. public function testButtonWithConfirm()
  6830. {
  6831. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6832. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6833. $this->assertHtml($expected, $result);
  6834. }
  6835. /**
  6836. * testPostButton method
  6837. *
  6838. * @return void
  6839. */
  6840. public function testPostButton()
  6841. {
  6842. $result = $this->Form->postButton('Hi', '/controller/action');
  6843. $expected = [
  6844. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6845. 'div' => ['style' => 'display:none;'],
  6846. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6847. '/div',
  6848. 'button' => ['type' => 'submit'],
  6849. 'Hi',
  6850. '/button',
  6851. '/form'
  6852. ];
  6853. $this->assertHtml($expected, $result);
  6854. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6855. $this->assertContains('<input type="hidden" name="extra" value="value"', $result);
  6856. }
  6857. /**
  6858. * testPostButtonMethodType method
  6859. *
  6860. * @return void
  6861. */
  6862. public function testPostButtonMethodType()
  6863. {
  6864. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6865. $expected = [
  6866. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6867. 'div' => ['style' => 'display:none;'],
  6868. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6869. '/div',
  6870. 'button' => ['type' => 'submit'],
  6871. 'Hi',
  6872. '/button',
  6873. '/form'
  6874. ];
  6875. $this->assertHtml($expected, $result);
  6876. }
  6877. /**
  6878. * testPostButtonFormOptions method
  6879. *
  6880. * @return void
  6881. */
  6882. public function testPostButtonFormOptions()
  6883. {
  6884. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6885. $expected = [
  6886. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6887. 'div' => ['style' => 'display:none;'],
  6888. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6889. '/div',
  6890. 'button' => ['type' => 'submit'],
  6891. 'Hi',
  6892. '/button',
  6893. '/form'
  6894. ];
  6895. $this->assertHtml($expected, $result);
  6896. }
  6897. /**
  6898. * testPostButtonNestedData method
  6899. *
  6900. * Test using postButton with N dimensional data.
  6901. *
  6902. * @return void
  6903. */
  6904. public function testPostButtonNestedData()
  6905. {
  6906. $data = [
  6907. 'one' => [
  6908. 'two' => [
  6909. 3, 4, 5
  6910. ]
  6911. ]
  6912. ];
  6913. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6914. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6915. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6916. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6917. }
  6918. /**
  6919. * testSecurePostButton method
  6920. *
  6921. * Test that postButton adds _Token fields.
  6922. *
  6923. * @return void
  6924. */
  6925. public function testSecurePostButton()
  6926. {
  6927. $this->Form->request = $this->Form->request
  6928. ->withParam('_csrfToken', 'testkey')
  6929. ->withParam('_Token.unlockedFields', []);
  6930. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6931. $tokenDebug = urlencode(json_encode([
  6932. '/posts/delete/1',
  6933. [],
  6934. []
  6935. ]));
  6936. $expected = [
  6937. 'form' => [
  6938. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6939. ],
  6940. ['div' => ['style' => 'display:none;']],
  6941. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6942. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6943. '/div',
  6944. 'button' => ['type' => 'submit'],
  6945. 'Delete',
  6946. '/button',
  6947. ['div' => ['style' => 'display:none;']],
  6948. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6949. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6950. ['input' => [
  6951. 'type' => 'hidden',
  6952. 'name' => '_Token[debug]',
  6953. 'value' => $tokenDebug,
  6954. 'autocomplete' => 'off',
  6955. ]],
  6956. '/div',
  6957. '/form',
  6958. ];
  6959. $this->assertHtml($expected, $result);
  6960. }
  6961. /**
  6962. * testPostLink method
  6963. *
  6964. * @return void
  6965. */
  6966. public function testPostLink()
  6967. {
  6968. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6969. $expected = [
  6970. 'form' => [
  6971. 'method' => 'post', 'action' => '/posts/delete/1',
  6972. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6973. ],
  6974. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6975. '/form',
  6976. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6977. 'Delete',
  6978. '/a'
  6979. ];
  6980. $this->assertHtml($expected, $result);
  6981. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6982. $expected = [
  6983. 'form' => [
  6984. 'method' => 'post', 'action' => '/posts/delete/1',
  6985. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6986. ],
  6987. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6988. '/form',
  6989. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6990. 'Delete',
  6991. '/a'
  6992. ];
  6993. $this->assertHtml($expected, $result);
  6994. $result = $this->Form->postLink(
  6995. 'Delete',
  6996. '/posts/delete/1',
  6997. ['target' => '_blank', 'class' => 'btn btn-danger']
  6998. );
  6999. $expected = [
  7000. 'form' => [
  7001. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  7002. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7003. ],
  7004. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7005. '/form',
  7006. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7007. 'Delete',
  7008. '/a'
  7009. ];
  7010. $this->assertHtml($expected, $result);
  7011. }
  7012. /**
  7013. * testPostLinkWithConfirm method
  7014. *
  7015. * Test the confirm option for postLink().
  7016. *
  7017. * @return void
  7018. */
  7019. public function testPostLinkWithConfirm()
  7020. {
  7021. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  7022. $expected = [
  7023. 'form' => [
  7024. 'method' => 'post', 'action' => '/posts/delete/1',
  7025. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7026. ],
  7027. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7028. '/form',
  7029. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  7030. 'Delete',
  7031. '/a'
  7032. ];
  7033. $this->assertHtml($expected, $result);
  7034. $result = $this->Form->postLink(
  7035. 'Delete',
  7036. '/posts/delete/1',
  7037. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  7038. );
  7039. $expected = [
  7040. 'form' => [
  7041. 'method' => 'post', 'action' => '/posts/delete/1',
  7042. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7043. ],
  7044. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7045. '/form',
  7046. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7047. 'Delete',
  7048. '/a'
  7049. ];
  7050. $this->assertHtml($expected, $result);
  7051. }
  7052. /**
  7053. * testPostLinkWithQuery method
  7054. *
  7055. * Test postLink() with query string args.
  7056. *
  7057. * @return void
  7058. */
  7059. public function testPostLinkWithQuery()
  7060. {
  7061. $result = $this->Form->postLink(
  7062. 'Delete',
  7063. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7064. );
  7065. $expected = [
  7066. 'form' => [
  7067. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7068. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7069. ],
  7070. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7071. '/form',
  7072. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7073. 'Delete',
  7074. '/a'
  7075. ];
  7076. $this->assertHtml($expected, $result);
  7077. }
  7078. /**
  7079. * testPostLinkWithData method
  7080. *
  7081. * Test postLink with additional data.
  7082. *
  7083. * @return void
  7084. */
  7085. public function testPostLinkWithData()
  7086. {
  7087. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7088. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7089. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7090. $this->Form->create($entity);
  7091. $this->Form->end();
  7092. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7093. $this->assertContains(
  7094. '<input type="hidden" name="name" value="show"',
  7095. $result,
  7096. 'should not contain entity data.'
  7097. );
  7098. }
  7099. /**
  7100. * testPostLinkSecurityHash method
  7101. *
  7102. * Test that security hashes for postLink include the url.
  7103. *
  7104. * @return void
  7105. */
  7106. public function testPostLinkSecurityHash()
  7107. {
  7108. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7109. $hash .= '%3Aid';
  7110. $this->Form->request = $this->Form->request->withParam('_Token.key', 'test');
  7111. $result = $this->Form->postLink(
  7112. 'Delete',
  7113. '/posts/delete/1',
  7114. ['data' => ['id' => 1]]
  7115. );
  7116. $tokenDebug = urlencode(json_encode([
  7117. '/posts/delete/1',
  7118. [
  7119. 'id' => 1
  7120. ],
  7121. []
  7122. ]));
  7123. $expected = [
  7124. 'form' => [
  7125. 'method' => 'post', 'action' => '/posts/delete/1',
  7126. 'name', 'style' => 'display:none;'
  7127. ],
  7128. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7129. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7130. 'div' => ['style' => 'display:none;'],
  7131. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7132. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7133. ['input' => [
  7134. 'type' => 'hidden',
  7135. 'name' => '_Token[debug]',
  7136. 'value' => $tokenDebug,
  7137. 'autocomplete' => 'off',
  7138. ]],
  7139. '/div',
  7140. '/form',
  7141. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7142. 'Delete',
  7143. '/a'
  7144. ];
  7145. $this->assertHtml($expected, $result);
  7146. }
  7147. /**
  7148. * testPostLinkSecurityHashBlockMode method
  7149. *
  7150. * Test that postLink doesn't modify the fields in the containing form.
  7151. *
  7152. * postLink() calls inside open forms should not modify the field list
  7153. * for the form.
  7154. *
  7155. * @return void
  7156. */
  7157. public function testPostLinkSecurityHashBlockMode()
  7158. {
  7159. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7160. $hash .= '%3A';
  7161. $this->Form->request = $this->Form->request->withParam('_Token.key', 'test');
  7162. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7163. $this->Form->control('title');
  7164. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7165. $result = $this->View->fetch('postLink');
  7166. $this->assertEquals(['title'], $this->Form->fields);
  7167. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7168. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7169. }
  7170. /**
  7171. * testPostLinkSecurityHashNoDebugMode method
  7172. *
  7173. * Test that security does not include debug token if debug is false.
  7174. *
  7175. * @return void
  7176. */
  7177. public function testPostLinkSecurityHashNoDebugMode()
  7178. {
  7179. Configure::write('debug', false);
  7180. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7181. $hash .= '%3Aid';
  7182. $this->Form->request = $this->Form->request
  7183. ->withParam('_Token.key', 'test');
  7184. $result = $this->Form->postLink(
  7185. 'Delete',
  7186. '/posts/delete/1',
  7187. ['data' => ['id' => 1]]
  7188. );
  7189. $expected = [
  7190. 'form' => [
  7191. 'method' => 'post', 'action' => '/posts/delete/1',
  7192. 'name', 'style' => 'display:none;'
  7193. ],
  7194. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7195. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7196. 'div' => ['style' => 'display:none;'],
  7197. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7198. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7199. '/div',
  7200. '/form',
  7201. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7202. 'Delete',
  7203. '/a'
  7204. ];
  7205. $this->assertHtml($expected, $result);
  7206. }
  7207. /**
  7208. * testPostLinkNestedData method
  7209. *
  7210. * Test using postLink with N dimensional data.
  7211. *
  7212. * @return void
  7213. */
  7214. public function testPostLinkNestedData()
  7215. {
  7216. $data = [
  7217. 'one' => [
  7218. 'two' => [
  7219. 3, 4, 5
  7220. ]
  7221. ]
  7222. ];
  7223. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7224. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7225. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7226. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7227. }
  7228. /**
  7229. * testPostLinkAfterGetForm method
  7230. *
  7231. * Test creating postLinks after a GET form.
  7232. *
  7233. * @return void
  7234. */
  7235. public function testPostLinkAfterGetForm()
  7236. {
  7237. $this->Form->request = $this->Form->request
  7238. ->withParam('_csrfToken', 'testkey')
  7239. ->withParam('_Token', 'val');
  7240. $this->Form->create($this->article, ['type' => 'get']);
  7241. $this->Form->end();
  7242. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7243. $tokenDebug = urlencode(json_encode([
  7244. '/posts/delete/1',
  7245. [],
  7246. []
  7247. ]));
  7248. $expected = [
  7249. 'form' => [
  7250. 'method' => 'post', 'action' => '/posts/delete/1',
  7251. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7252. ],
  7253. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7254. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7255. 'div' => ['style' => 'display:none;'],
  7256. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7257. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7258. ['input' => [
  7259. 'type' => 'hidden', 'name' => '_Token[debug]',
  7260. 'value' => $tokenDebug,
  7261. 'autocomplete' => 'off'
  7262. ]],
  7263. '/div',
  7264. '/form',
  7265. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7266. 'Delete',
  7267. '/a'
  7268. ];
  7269. $this->assertHtml($expected, $result);
  7270. }
  7271. /**
  7272. * testPostLinkFormBuffer method
  7273. *
  7274. * Test that postLink adds form tags to view block.
  7275. *
  7276. * @return void
  7277. */
  7278. public function testPostLinkFormBuffer()
  7279. {
  7280. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7281. $expected = [
  7282. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7283. 'Delete',
  7284. '/a'
  7285. ];
  7286. $this->assertHtml($expected, $result);
  7287. $result = $this->View->fetch('postLink');
  7288. $expected = [
  7289. 'form' => [
  7290. 'method' => 'post', 'action' => '/posts/delete/1',
  7291. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7292. ],
  7293. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7294. '/form'
  7295. ];
  7296. $this->assertHtml($expected, $result);
  7297. $result = $this->Form->postLink(
  7298. 'Delete',
  7299. '/posts/delete/2',
  7300. ['block' => true, 'method' => 'DELETE']
  7301. );
  7302. $expected = [
  7303. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7304. 'Delete',
  7305. '/a'
  7306. ];
  7307. $this->assertHtml($expected, $result);
  7308. $result = $this->View->fetch('postLink');
  7309. $expected = [
  7310. 'form' => [
  7311. 'method' => 'post', 'action' => '/posts/delete/1',
  7312. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7313. ],
  7314. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7315. '/form',
  7316. [
  7317. 'form' => [
  7318. 'method' => 'post', 'action' => '/posts/delete/2',
  7319. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7320. ],
  7321. ],
  7322. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7323. '/form'
  7324. ];
  7325. $this->assertHtml($expected, $result);
  7326. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7327. $expected = [
  7328. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7329. 'Delete',
  7330. '/a'
  7331. ];
  7332. $this->assertHtml($expected, $result);
  7333. $result = $this->View->fetch('foobar');
  7334. $expected = [
  7335. 'form' => [
  7336. 'method' => 'post', 'action' => '/posts/delete/1',
  7337. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7338. ],
  7339. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7340. '/form'
  7341. ];
  7342. $this->assertHtml($expected, $result);
  7343. }
  7344. /**
  7345. * testSubmitButton method
  7346. *
  7347. * @return void
  7348. */
  7349. public function testSubmitButton()
  7350. {
  7351. $result = $this->Form->submit('');
  7352. $expected = [
  7353. 'div' => ['class' => 'submit'],
  7354. 'input' => ['type' => 'submit', 'value' => ''],
  7355. '/div'
  7356. ];
  7357. $this->assertHtml($expected, $result);
  7358. $result = $this->Form->submit('Test Submit');
  7359. $expected = [
  7360. 'div' => ['class' => 'submit'],
  7361. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7362. '/div'
  7363. ];
  7364. $this->assertHtml($expected, $result);
  7365. $result = $this->Form->submit('Next >');
  7366. $expected = [
  7367. 'div' => ['class' => 'submit'],
  7368. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7369. '/div'
  7370. ];
  7371. $this->assertHtml($expected, $result);
  7372. $result = $this->Form->submit('Next >', ['escape' => false]);
  7373. $expected = [
  7374. 'div' => ['class' => 'submit'],
  7375. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7376. '/div'
  7377. ];
  7378. $this->assertHtml($expected, $result);
  7379. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7380. $expected = [
  7381. 'div' => ['class' => 'submit'],
  7382. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7383. '/div'
  7384. ];
  7385. $this->assertHtml($expected, $result);
  7386. }
  7387. /**
  7388. * testSubmitImage method
  7389. *
  7390. * Test image submit types.
  7391. *
  7392. * @return void
  7393. */
  7394. public function testSubmitImage()
  7395. {
  7396. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7397. $expected = [
  7398. 'div' => ['class' => 'submit'],
  7399. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7400. '/div'
  7401. ];
  7402. $this->assertHtml($expected, $result);
  7403. $result = $this->Form->submit('/relative/cake.power.gif');
  7404. $expected = [
  7405. 'div' => ['class' => 'submit'],
  7406. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7407. '/div'
  7408. ];
  7409. $this->assertHtml($expected, $result);
  7410. $result = $this->Form->submit('cake.power.gif');
  7411. $expected = [
  7412. 'div' => ['class' => 'submit'],
  7413. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7414. '/div'
  7415. ];
  7416. $this->assertHtml($expected, $result);
  7417. $result = $this->Form->submit('Not.an.image');
  7418. $expected = [
  7419. 'div' => ['class' => 'submit'],
  7420. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7421. '/div'
  7422. ];
  7423. $this->assertHtml($expected, $result);
  7424. }
  7425. /**
  7426. * testSubmitUnlockedByDefault method
  7427. *
  7428. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7429. * be submitted at a time.
  7430. *
  7431. * @return void
  7432. */
  7433. public function testSubmitUnlockedByDefault()
  7434. {
  7435. $this->Form->request = $this->Form->request->withParam('_Token', 'secured');
  7436. $this->Form->submit('Go go');
  7437. $this->Form->submit('Save', ['name' => 'save']);
  7438. $result = $this->Form->unlockField();
  7439. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7440. }
  7441. /**
  7442. * testSubmitImageTimestamp method
  7443. *
  7444. * Test submit image with timestamps.
  7445. *
  7446. * @return void
  7447. */
  7448. public function testSubmitImageTimestamp()
  7449. {
  7450. Configure::write('Asset.timestamp', 'force');
  7451. $result = $this->Form->submit('cake.power.gif');
  7452. $expected = [
  7453. 'div' => ['class' => 'submit'],
  7454. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7455. '/div'
  7456. ];
  7457. $this->assertHtml($expected, $result);
  7458. }
  7459. /**
  7460. * testDateTimeWithGetForms method
  7461. *
  7462. * Test that datetime() works with GET style forms.
  7463. *
  7464. * @return void
  7465. */
  7466. public function testDateTimeWithGetForms()
  7467. {
  7468. extract($this->dateRegex);
  7469. $this->Form->create($this->article, ['type' => 'get']);
  7470. $result = $this->Form->datetime('created');
  7471. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7472. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7473. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7474. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7475. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7476. }
  7477. /**
  7478. * testForMagicControlNonExistingNorValidated method
  7479. *
  7480. * @return void
  7481. */
  7482. public function testForMagicControlNonExistingNorValidated()
  7483. {
  7484. $this->Form->create($this->article);
  7485. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7486. $result = $this->Form->control('non_existing_nor_validated');
  7487. $expected = [
  7488. 'label' => ['for' => 'non-existing-nor-validated'],
  7489. 'Non Existing Nor Validated',
  7490. '/label',
  7491. 'input' => [
  7492. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7493. 'id' => 'non-existing-nor-validated'
  7494. ]
  7495. ];
  7496. $this->assertHtml($expected, $result);
  7497. $result = $this->Form->control('non_existing_nor_validated', [
  7498. 'val' => 'my value'
  7499. ]);
  7500. $expected = [
  7501. 'label' => ['for' => 'non-existing-nor-validated'],
  7502. 'Non Existing Nor Validated',
  7503. '/label',
  7504. 'input' => [
  7505. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7506. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7507. ]
  7508. ];
  7509. $this->assertHtml($expected, $result);
  7510. $this->Form->request = $this->Form->request->withData('non_existing_nor_validated', 'CakePHP magic');
  7511. $this->Form->create($this->article);
  7512. $result = $this->Form->control('non_existing_nor_validated');
  7513. $expected = [
  7514. 'label' => ['for' => 'non-existing-nor-validated'],
  7515. 'Non Existing Nor Validated',
  7516. '/label',
  7517. 'input' => [
  7518. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7519. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7520. ]
  7521. ];
  7522. $this->assertHtml($expected, $result);
  7523. }
  7524. /**
  7525. * testFormMagicControlLabel method
  7526. *
  7527. * @return void
  7528. */
  7529. public function testFormMagicControlLabel()
  7530. {
  7531. TableRegistry::get('Contacts', [
  7532. 'className' => __NAMESPACE__ . '\ContactsTable'
  7533. ]);
  7534. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7535. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7536. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7537. $expected = [
  7538. 'label' => ['for' => 'contacts-name'],
  7539. 'My label',
  7540. '/label',
  7541. 'input' => [
  7542. 'type' => 'text', 'name' => 'Contacts[name]',
  7543. 'id' => 'contacts-name', 'maxlength' => '255'
  7544. ]
  7545. ];
  7546. $this->assertHtml($expected, $result);
  7547. $result = $this->Form->control('name', [
  7548. 'label' => ['class' => 'mandatory']
  7549. ]);
  7550. $expected = [
  7551. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7552. 'Name',
  7553. '/label',
  7554. 'input' => [
  7555. 'type' => 'text', 'name' => 'name',
  7556. 'id' => 'name', 'maxlength' => '255'
  7557. ]
  7558. ];
  7559. $this->assertHtml($expected, $result);
  7560. $result = $this->Form->control('name', [
  7561. 'div' => false,
  7562. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7563. ]);
  7564. $expected = [
  7565. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7566. 'My label',
  7567. '/label',
  7568. 'input' => [
  7569. 'type' => 'text', 'name' => 'name',
  7570. 'id' => 'name', 'maxlength' => '255'
  7571. ]
  7572. ];
  7573. $this->assertHtml($expected, $result);
  7574. $result = $this->Form->control('Contact.name', [
  7575. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7576. ]);
  7577. $expected = [
  7578. 'label' => ['for' => 'my_id'],
  7579. 'Name',
  7580. '/label',
  7581. 'input' => [
  7582. 'type' => 'text', 'name' => 'Contact[name]',
  7583. 'id' => 'my_id', 'maxlength' => '255'
  7584. ]
  7585. ];
  7586. $this->assertHtml($expected, $result);
  7587. $result = $this->Form->control('1.id');
  7588. $expected = ['input' => [
  7589. 'type' => 'hidden', 'name' => '1[id]',
  7590. 'id' => '1-id'
  7591. ]];
  7592. $this->assertHtml($expected, $result);
  7593. $result = $this->Form->control('1.name');
  7594. $expected = [
  7595. 'label' => ['for' => '1-name'],
  7596. 'Name',
  7597. '/label',
  7598. 'input' => [
  7599. 'type' => 'text', 'name' => '1[name]',
  7600. 'id' => '1-name', 'maxlength' => '255'
  7601. ]
  7602. ];
  7603. $this->assertHtml($expected, $result);
  7604. }
  7605. /**
  7606. * testFormEnd method
  7607. *
  7608. * @return void
  7609. */
  7610. public function testFormEnd()
  7611. {
  7612. $this->assertEquals('</form>', $this->Form->end());
  7613. }
  7614. /**
  7615. * testMultiRecordForm method
  7616. *
  7617. * Test the generation of fields for a multi record form.
  7618. *
  7619. * @return void
  7620. */
  7621. public function testMultiRecordForm()
  7622. {
  7623. $this->loadFixtures('Articles', 'Comments');
  7624. $articles = TableRegistry::get('Articles');
  7625. $articles->hasMany('Comments');
  7626. $comment = new Entity(['comment' => 'Value']);
  7627. $article = new Article(['comments' => [$comment]]);
  7628. $this->Form->create([$article]);
  7629. $result = $this->Form->control('0.comments.1.comment');
  7630. //@codingStandardsIgnoreStart
  7631. $expected = [
  7632. 'div' => ['class' => 'input textarea'],
  7633. 'label' => ['for' => '0-comments-1-comment'],
  7634. 'Comment',
  7635. '/label',
  7636. 'textarea' => [
  7637. 'name',
  7638. 'id' => '0-comments-1-comment',
  7639. 'rows' => 5
  7640. ],
  7641. '/textarea',
  7642. '/div'
  7643. ];
  7644. //@codingStandardsIgnoreEnd
  7645. $this->assertHtml($expected, $result);
  7646. $result = $this->Form->control('0.comments.0.comment');
  7647. //@codingStandardsIgnoreStart
  7648. $expected = [
  7649. 'div' => ['class' => 'input textarea'],
  7650. 'label' => ['for' => '0-comments-0-comment'],
  7651. 'Comment',
  7652. '/label',
  7653. 'textarea' => [
  7654. 'name',
  7655. 'id' => '0-comments-0-comment',
  7656. 'rows' => 5
  7657. ],
  7658. 'Value',
  7659. '/textarea',
  7660. '/div'
  7661. ];
  7662. //@codingStandardsIgnoreEnd
  7663. $this->assertHtml($expected, $result);
  7664. $comment->setError('comment', ['Not valid']);
  7665. $result = $this->Form->control('0.comments.0.comment');
  7666. //@codingStandardsIgnoreStart
  7667. $expected = [
  7668. 'div' => ['class' => 'input textarea error'],
  7669. 'label' => ['for' => '0-comments-0-comment'],
  7670. 'Comment',
  7671. '/label',
  7672. 'textarea' => [
  7673. 'name',
  7674. 'class' => 'form-error',
  7675. 'id' => '0-comments-0-comment',
  7676. 'rows' => 5
  7677. ],
  7678. 'Value',
  7679. '/textarea',
  7680. ['div' => ['class' => 'error-message']],
  7681. 'Not valid',
  7682. '/div',
  7683. '/div'
  7684. ];
  7685. //@codingStandardsIgnoreEnd
  7686. $this->assertHtml($expected, $result);
  7687. TableRegistry::get('Comments')
  7688. ->getValidator('default')
  7689. ->allowEmpty('comment', false);
  7690. $result = $this->Form->control('0.comments.1.comment');
  7691. //@codingStandardsIgnoreStart
  7692. $expected = [
  7693. 'div' => ['class' => 'input textarea required'],
  7694. 'label' => ['for' => '0-comments-1-comment'],
  7695. 'Comment',
  7696. '/label',
  7697. 'textarea' => [
  7698. 'name',
  7699. 'required' => 'required',
  7700. 'id' => '0-comments-1-comment',
  7701. 'rows' => 5
  7702. ],
  7703. '/textarea',
  7704. '/div'
  7705. ];
  7706. //@codingStandardsIgnoreEnd
  7707. $this->assertHtml($expected, $result);
  7708. }
  7709. /**
  7710. * testHtml5Controls method
  7711. *
  7712. * Test that some html5 inputs + FormHelper::__call() work.
  7713. *
  7714. * @return void
  7715. */
  7716. public function testHtml5Controls()
  7717. {
  7718. $result = $this->Form->email('User.email');
  7719. $expected = [
  7720. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7721. ];
  7722. $this->assertHtml($expected, $result);
  7723. $result = $this->Form->search('User.query');
  7724. $expected = [
  7725. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7726. ];
  7727. $this->assertHtml($expected, $result);
  7728. $result = $this->Form->search('User.query', ['value' => 'test']);
  7729. $expected = [
  7730. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7731. ];
  7732. $this->assertHtml($expected, $result);
  7733. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7734. $expected = [
  7735. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7736. ];
  7737. $this->assertHtml($expected, $result);
  7738. }
  7739. /**
  7740. * testHtml5ControlWithControl method
  7741. *
  7742. * Test accessing html5 inputs through control().
  7743. *
  7744. * @return void
  7745. */
  7746. public function testHtml5ControlWithControl()
  7747. {
  7748. $this->Form->create();
  7749. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7750. $result = $this->Form->control('website', [
  7751. 'type' => 'url',
  7752. 'val' => 'http://domain.tld',
  7753. 'label' => false
  7754. ]);
  7755. $expected = [
  7756. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7757. ];
  7758. $this->assertHtml($expected, $result);
  7759. }
  7760. /**
  7761. * testHtml5ControlException method
  7762. *
  7763. * Test errors when field name is missing.
  7764. *
  7765. * @return void
  7766. */
  7767. public function testHtml5ControlException()
  7768. {
  7769. $this->expectException(\Cake\Core\Exception\Exception::class);
  7770. $this->Form->email();
  7771. }
  7772. /**
  7773. * testRequiredAttribute method
  7774. *
  7775. * Tests that formhelper sets required attributes.
  7776. *
  7777. * @return void
  7778. */
  7779. public function testRequiredAttribute()
  7780. {
  7781. $this->article['required'] = [
  7782. 'title' => true,
  7783. 'body' => false,
  7784. ];
  7785. $this->Form->create($this->article);
  7786. $result = $this->Form->control('title');
  7787. $expected = [
  7788. 'div' => ['class' => 'input text required'],
  7789. 'label' => ['for' => 'title'],
  7790. 'Title',
  7791. '/label',
  7792. 'input' => [
  7793. 'type' => 'text',
  7794. 'name' => 'title',
  7795. 'id' => 'title',
  7796. 'required' => 'required',
  7797. ],
  7798. '/div'
  7799. ];
  7800. $this->assertHtml($expected, $result);
  7801. $result = $this->Form->control('title', ['required' => false]);
  7802. $this->assertNotContains('required', $result);
  7803. $result = $this->Form->control('body');
  7804. $expected = [
  7805. 'div' => ['class' => 'input text'],
  7806. 'label' => ['for' => 'body'],
  7807. 'Body',
  7808. '/label',
  7809. 'input' => [
  7810. 'type' => 'text',
  7811. 'name' => 'body',
  7812. 'id' => 'body',
  7813. ],
  7814. '/div'
  7815. ];
  7816. $this->assertHtml($expected, $result);
  7817. $result = $this->Form->control('body', ['required' => true]);
  7818. $this->assertContains('required', $result);
  7819. }
  7820. /**
  7821. * testControlsNotNested method
  7822. *
  7823. * Tests that it is possible to put inputs outside of the label.
  7824. *
  7825. * @return void
  7826. */
  7827. public function testControlsNotNested()
  7828. {
  7829. $this->Form->setTemplates([
  7830. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7831. 'formGroup' => '{{input}}{{label}}',
  7832. ]);
  7833. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7834. $expected = [
  7835. 'div' => ['class' => 'input checkbox'],
  7836. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7837. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7838. 'label' => ['for' => 'foo'],
  7839. 'Foo',
  7840. '/label',
  7841. '/div'
  7842. ];
  7843. $this->assertHtml($expected, $result);
  7844. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7845. $expected = [
  7846. 'div' => ['class' => 'input checkbox'],
  7847. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7848. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7849. '/div'
  7850. ];
  7851. $this->assertHtml($expected, $result);
  7852. $result = $this->Form->control('confirm', [
  7853. 'type' => 'radio',
  7854. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7855. ]);
  7856. $expected = [
  7857. 'div' => ['class' => 'input radio'],
  7858. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7859. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7860. ['label' => ['for' => 'confirm-y']],
  7861. 'Yes',
  7862. '/label',
  7863. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7864. ['label' => ['for' => 'confirm-n']],
  7865. 'No',
  7866. '/label',
  7867. '<label',
  7868. 'Confirm',
  7869. '/label',
  7870. '/div',
  7871. ];
  7872. $this->assertHtml($expected, $result);
  7873. $result = $this->Form->select('category', ['1', '2'], [
  7874. 'multiple' => 'checkbox',
  7875. 'name' => 'fish',
  7876. ]);
  7877. $expected = [
  7878. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7879. ['div' => ['class' => 'checkbox']],
  7880. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7881. ['label' => ['for' => 'fish-0']],
  7882. '1',
  7883. '/label',
  7884. '/div',
  7885. ['div' => ['class' => 'checkbox']],
  7886. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7887. ['label' => ['for' => 'fish-1']],
  7888. '2',
  7889. '/label',
  7890. '/div'
  7891. ];
  7892. $this->assertHtml($expected, $result);
  7893. }
  7894. /**
  7895. * testControlContainerTemplates method
  7896. *
  7897. * Test that *Container templates are used by input.
  7898. *
  7899. * @return void
  7900. */
  7901. public function testControlContainerTemplates()
  7902. {
  7903. $this->Form->setTemplates([
  7904. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7905. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7906. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7907. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7908. ]);
  7909. $this->article['errors'] = [
  7910. 'Article' => ['published' => 'error message']
  7911. ];
  7912. $this->Form->create($this->article);
  7913. $result = $this->Form->control('accept', [
  7914. 'type' => 'checkbox'
  7915. ]);
  7916. $expected = [
  7917. 'div' => ['class' => 'check'],
  7918. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7919. 'label' => ['for' => 'accept'],
  7920. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7921. 'Accept',
  7922. '/label',
  7923. '/div'
  7924. ];
  7925. $this->assertHtml($expected, $result);
  7926. $result = $this->Form->control('accept', [
  7927. 'type' => 'radio',
  7928. 'options' => ['Y', 'N']
  7929. ]);
  7930. $this->assertContains('<div class="rad">', $result);
  7931. $result = $this->Form->control('Article.published', [
  7932. 'type' => 'radio',
  7933. 'options' => ['Y', 'N']
  7934. ]);
  7935. $this->assertContains('<div class="rad err">', $result);
  7936. $result = $this->Form->control('Article.created', [
  7937. 'type' => 'datetime',
  7938. ]);
  7939. $this->assertContains('<div class="dt">', $result);
  7940. }
  7941. /**
  7942. * testFormGroupTemplates method
  7943. *
  7944. * Test that *Container templates are used by input.
  7945. *
  7946. * @return void
  7947. */
  7948. public function testFormGroupTemplates()
  7949. {
  7950. $this->Form->setTemplates([
  7951. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7952. ]);
  7953. $this->Form->create($this->article);
  7954. $result = $this->Form->control('accept', [
  7955. 'type' => 'radio',
  7956. 'options' => ['Y', 'N']
  7957. ]);
  7958. $this->assertContains('<div class="radio">', $result);
  7959. }
  7960. /**
  7961. * testResetTemplates method
  7962. *
  7963. * Test resetting templates.
  7964. *
  7965. * @return void
  7966. */
  7967. public function testResetTemplates()
  7968. {
  7969. $this->Form->setTemplates(['input' => '<input/>']);
  7970. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7971. $this->Form->resetTemplates();
  7972. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7973. }
  7974. /**
  7975. * testContext method
  7976. *
  7977. * Test the context method.
  7978. *
  7979. * @return void
  7980. */
  7981. public function testContext()
  7982. {
  7983. $result = $this->Form->context();
  7984. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7985. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7986. $this->assertSame($mock, $this->Form->context($mock));
  7987. $this->assertSame($mock, $this->Form->context());
  7988. }
  7989. /**
  7990. * testAutoDomId method
  7991. *
  7992. * @return void
  7993. */
  7994. public function testAutoDomId()
  7995. {
  7996. $result = $this->Form->text('field', ['id' => true]);
  7997. $expected = [
  7998. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7999. ];
  8000. $this->assertHtml($expected, $result);
  8001. // Ensure id => doesn't cause problem when multiple inputs are generated.
  8002. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  8003. $expected = [
  8004. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  8005. ['label' => ['for' => 'field-0']],
  8006. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  8007. 'option A',
  8008. '/label',
  8009. ['label' => ['for' => 'field-1']],
  8010. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  8011. 'option B',
  8012. '/label',
  8013. ];
  8014. $this->assertHtml($expected, $result);
  8015. $result = $this->Form->select(
  8016. 'multi_field',
  8017. ['first', 'second'],
  8018. ['multiple' => 'checkbox', 'id' => true]
  8019. );
  8020. $expected = [
  8021. 'input' => [
  8022. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  8023. ],
  8024. ['div' => ['class' => 'checkbox']],
  8025. ['label' => ['for' => 'multi-field-0']],
  8026. ['input' => [
  8027. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8028. 'value' => '0', 'id' => 'multi-field-0'
  8029. ]],
  8030. 'first',
  8031. '/label',
  8032. '/div',
  8033. ['div' => ['class' => 'checkbox']],
  8034. ['label' => ['for' => 'multi-field-1']],
  8035. ['input' => [
  8036. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8037. 'value' => '1', 'id' => 'multi-field-1'
  8038. ]],
  8039. 'second',
  8040. '/label',
  8041. '/div',
  8042. ];
  8043. $this->assertHtml($expected, $result);
  8044. }
  8045. /**
  8046. * Test the basic setters and getters for value sources
  8047. *
  8048. * @return void
  8049. */
  8050. public function testFormValueSourcesSettersGetters()
  8051. {
  8052. $this->Form->request = $this->Form->request
  8053. ->withData('id', '1')
  8054. ->withQueryParams(['id' => '2']);
  8055. $expected = ['context'];
  8056. $result = $this->Form->getValueSources();
  8057. $this->assertEquals($expected, $result);
  8058. $expected = ['query', 'data', 'context'];
  8059. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8060. $result = $this->Form->getValueSources();
  8061. $this->assertEquals($expected, $result);
  8062. $this->Form->setValueSources(['context']);
  8063. $expected = '1';
  8064. $result = $this->Form->getSourceValue('id');
  8065. $this->assertEquals($expected, $result);
  8066. $this->Form->setValueSources('query');
  8067. $expected = ['query'];
  8068. $result = $this->Form->getValueSources();
  8069. $this->assertEquals($expected, $result);
  8070. $expected = '2';
  8071. $result = $this->Form->getSourceValue('id');
  8072. $this->assertEquals($expected, $result);
  8073. $this->Form->setValueSources(['data']);
  8074. $expected = '1';
  8075. $result = $this->Form->getSourceValue('id');
  8076. $this->assertEquals($expected, $result);
  8077. $this->Form->setValueSources(['query', 'data']);
  8078. $expected = '2';
  8079. $result = $this->Form->getSourceValue('id');
  8080. $this->assertEquals($expected, $result);
  8081. }
  8082. /**
  8083. * Tests the different input rendering values based on sources values switching
  8084. *
  8085. * @return void
  8086. */
  8087. public function testFormValueSourcesSingleSwitchRendering()
  8088. {
  8089. $this->loadFixtures('Articles');
  8090. $articles = TableRegistry::get('Articles');
  8091. $article = new Article();
  8092. $articles->patchEntity($article, ['id' => '3']);
  8093. $this->Form->create($article);
  8094. $this->Form->setValueSources(['context']);
  8095. $result = $this->Form->control('id');
  8096. $expected = [
  8097. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8098. ];
  8099. $this->assertHtml($expected, $result);
  8100. $this->Form->request = $this->Form->request->withQueryParams(['id' => 5]);
  8101. $this->Form->setValueSources(['query']);
  8102. $result = $this->Form->control('id');
  8103. $expected = [
  8104. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8105. ];
  8106. $this->assertHtml($expected, $result);
  8107. $this->Form->request = $this->Form->request
  8108. ->withQueryParams(['id' => '5a'])
  8109. ->withData('id', '5b');
  8110. $this->Form->setValueSources(['context']);
  8111. $this->Form->create($article);
  8112. $result = $this->Form->control('id');
  8113. $expected = [
  8114. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8115. ];
  8116. $this->assertHtml($expected, $result);
  8117. $this->Form->setValueSources(['data']);
  8118. $this->Form->create($article);
  8119. $result = $this->Form->control('id');
  8120. $expected = [
  8121. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8122. ];
  8123. $this->assertHtml($expected, $result);
  8124. $this->Form->setValueSources(['query']);
  8125. $result = $this->Form->control('id');
  8126. $expected = [
  8127. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8128. ];
  8129. $this->assertHtml($expected, $result);
  8130. }
  8131. /**
  8132. * Tests the different input rendering values based on sources values switching while supplying
  8133. * an entity (base context) and multiple sources (such as data, query)
  8134. *
  8135. * @return void
  8136. */
  8137. public function testFormValueSourcesListSwitchRendering()
  8138. {
  8139. $this->loadFixtures('Articles');
  8140. $articles = TableRegistry::get('Articles');
  8141. $article = new Article();
  8142. $articles->patchEntity($article, ['id' => '3']);
  8143. $this->Form->request = $this->Form->request->withQueryParams(['id' => '9']);
  8144. $this->Form->create($article);
  8145. $this->Form->setValueSources(['context', 'query']);
  8146. $result = $this->Form->control('id');
  8147. $expected = [
  8148. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8149. ];
  8150. $this->assertHtml($expected, $result);
  8151. $this->Form->setValueSources(['query', 'context']);
  8152. $result = $this->Form->control('id');
  8153. $expected = [
  8154. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8155. ];
  8156. $this->assertHtml($expected, $result);
  8157. $this->Form->setValueSources(['data', 'query', 'context']);
  8158. $result = $this->Form->control('id');
  8159. $expected = [
  8160. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8161. ];
  8162. $this->assertHtml($expected, $result);
  8163. $this->Form->request = $this->Form->request
  8164. ->withData('id', '8')
  8165. ->withQueryParams(['id' => '9']);
  8166. $this->Form->setValueSources(['data', 'query', 'context']);
  8167. $result = $this->Form->control('id');
  8168. $expected = [
  8169. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8170. ];
  8171. $this->assertHtml($expected, $result);
  8172. }
  8173. /**
  8174. * Test the different form input renderings based on values sources switchings through form options
  8175. *
  8176. * @return void
  8177. */
  8178. public function testFormValueSourcesSwitchViaOptionsRendering()
  8179. {
  8180. $this->loadFixtures('Articles');
  8181. $articles = TableRegistry::get('Articles');
  8182. $article = new Article();
  8183. $articles->patchEntity($article, ['id' => '3']);
  8184. $this->Form->request = $this->Form->request->withData('id', 4)->withQueryParams(['id' => 5]);
  8185. $this->Form->create($article, ['valueSources' => 'query']);
  8186. $result = $this->Form->control('id');
  8187. $expected = [
  8188. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8189. ];
  8190. $this->assertHtml($expected, $result);
  8191. $result = $this->Form->getSourceValue('id');
  8192. $this->assertEquals('5', $result);
  8193. $this->Form->setValueSources(['context']);
  8194. $this->Form->create($article, ['valueSources' => 'query']);
  8195. $result = $this->Form->control('id');
  8196. $expected = [
  8197. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8198. ];
  8199. $this->assertHtml($expected, $result);
  8200. $result = $this->Form->getSourceValue('id');
  8201. $this->assertEquals('5', $result);
  8202. $this->Form->setValueSources(['query']);
  8203. $this->Form->create($article, ['valueSources' => 'data']);
  8204. $result = $this->Form->control('id');
  8205. $expected = [
  8206. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8207. ];
  8208. $this->assertHtml($expected, $result);
  8209. $result = $this->Form->getSourceValue('id');
  8210. $this->assertEquals('4', $result);
  8211. $this->Form->setValueSources(['query']);
  8212. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8213. $result = $this->Form->control('id');
  8214. $expected = [
  8215. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8216. ];
  8217. $this->assertHtml($expected, $result);
  8218. $result = $this->Form->getSourceValue('id');
  8219. $this->assertEquals('4', $result);
  8220. }
  8221. /**
  8222. * Test the different form input renderings based on values sources switchings through form options
  8223. *
  8224. * @return void
  8225. */
  8226. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8227. {
  8228. $this->loadFixtures('Articles');
  8229. $articles = TableRegistry::get('Articles');
  8230. $article = new Article();
  8231. $articles->patchEntity($article, ['id' => '3']);
  8232. $this->Form->request = $this->Form->request->withData('id', 10)->withQueryParams(['id' => 11]);
  8233. $this->Form->setValueSources(['context'])
  8234. ->create($article, ['valueSources' => ['query', 'data']]);
  8235. $result = $this->Form->control('id');
  8236. $expected = [
  8237. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8238. ];
  8239. $this->assertHtml($expected, $result);
  8240. $result = $this->Form->getSourceValue('id');
  8241. $this->assertEquals('11', $result);
  8242. $this->Form->request = $this->Form->request->withQueryParams([]);
  8243. $this->Form->setValueSources(['context'])
  8244. ->create($article, ['valueSources' => ['query', 'data']]);
  8245. $result = $this->Form->control('id');
  8246. $expected = [
  8247. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8248. ];
  8249. $this->assertHtml($expected, $result);
  8250. $result = $this->Form->getSourceValue('id');
  8251. $this->assertEquals('10', $result);
  8252. }
  8253. /**
  8254. * Test the different form values sources resetting through From::end();
  8255. *
  8256. * @return void
  8257. */
  8258. public function testFormValueSourcesResetViaEnd()
  8259. {
  8260. $expected = ['context'];
  8261. $result = $this->Form->getValueSources();
  8262. $this->assertEquals($expected, $result);
  8263. $expected = ['query', 'context', 'data'];
  8264. $this->Form->setValueSources(['query', 'context', 'data']);
  8265. $result = $this->Form->getValueSources();
  8266. $this->assertEquals($expected, $result);
  8267. $this->Form->create();
  8268. $result = $this->Form->getValueSources();
  8269. $this->assertEquals($expected, $result);
  8270. $this->Form->end();
  8271. $result = $this->Form->getValueSources();
  8272. $this->assertEquals(['context'], $result);
  8273. }
  8274. /**
  8275. * Test sources values defaults handling
  8276. *
  8277. * @return void
  8278. */
  8279. public function testFormValueSourcesDefaults()
  8280. {
  8281. $this->Form->request = $this->Form->request->withQueryParams(['password' => 'open Sesame']);
  8282. $this->Form->create();
  8283. $result = $this->Form->password('password');
  8284. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8285. $this->assertHtml($expected, $result);
  8286. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8287. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8288. $this->assertHtml($expected, $result);
  8289. $this->Form->setValueSources('query');
  8290. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8291. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8292. $this->assertHtml($expected, $result);
  8293. $this->Form->setValueSources('data');
  8294. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8295. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8296. $this->assertHtml($expected, $result);
  8297. }
  8298. /**
  8299. * Test sources values schema defaults handling
  8300. *
  8301. * @return void
  8302. */
  8303. public function testSourcesValueDoesntExistPassThrough()
  8304. {
  8305. $this->Form->request = $this->Form->request->withQueryParams(['category' => 'sesame-cookies']);
  8306. $articles = TableRegistry::get('Articles');
  8307. $entity = $articles->newEntity();
  8308. $this->Form->create($entity);
  8309. $this->Form->setValueSources(['query', 'context']);
  8310. $result = $this->Form->getSourceValue('category');
  8311. $this->assertEquals('sesame-cookies', $result);
  8312. $this->Form->setValueSources(['context', 'query']);
  8313. $result = $this->Form->getSourceValue('category');
  8314. $this->assertEquals('sesame-cookies', $result);
  8315. }
  8316. /**
  8317. * testNestedLabelInput method
  8318. *
  8319. * Test the `nestedInput` parameter
  8320. *
  8321. * @group deprecated
  8322. * @return void
  8323. */
  8324. public function testNestedLabelInput()
  8325. {
  8326. $this->deprecated(function () {
  8327. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8328. $expected = [
  8329. 'div' => ['class' => 'input text'],
  8330. 'label' => ['for' => 'foo'],
  8331. ['input' => [
  8332. 'type' => 'text',
  8333. 'name' => 'foo',
  8334. 'id' => 'foo'
  8335. ]],
  8336. 'Foo',
  8337. '/label',
  8338. '/div'
  8339. ];
  8340. $this->assertHtml($expected, $result);
  8341. });
  8342. }
  8343. /**
  8344. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8345. *
  8346. * This test makes sure `false` excludes the label from the render
  8347. *
  8348. * @return void
  8349. */
  8350. public function testControlLabelManipulationDisableLabels()
  8351. {
  8352. $result = $this->Form->control('test', [
  8353. 'type' => 'radio',
  8354. 'options' => ['A', 'B'],
  8355. 'labelOptions' => false
  8356. ]);
  8357. $expected = [
  8358. ['div' => ['class' => 'input radio']],
  8359. '<label',
  8360. 'Test',
  8361. '/label',
  8362. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8363. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8364. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8365. '/div',
  8366. ];
  8367. $this->assertHtml($expected, $result);
  8368. $result = $this->Form->control('checkbox1', [
  8369. 'label' => 'My checkboxes',
  8370. 'multiple' => 'checkbox',
  8371. 'type' => 'select',
  8372. 'options' => [
  8373. ['text' => 'First Checkbox', 'value' => 1],
  8374. ['text' => 'Second Checkbox', 'value' => 2]
  8375. ],
  8376. 'labelOptions' => false
  8377. ]);
  8378. $expected = [
  8379. ['div' => ['class' => 'input select']],
  8380. ['label' => ['for' => 'checkbox1']],
  8381. 'My checkboxes',
  8382. '/label',
  8383. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8384. ['div' => ['class' => 'checkbox']],
  8385. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8386. '/div',
  8387. ['div' => ['class' => 'checkbox']],
  8388. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8389. '/div',
  8390. '/div'
  8391. ];
  8392. $this->assertHtml($expected, $result);
  8393. }
  8394. /**
  8395. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8396. *
  8397. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8398. * added to the class if checked.
  8399. *
  8400. * Also checks to make sure any custom attributes are rendered correctly
  8401. *
  8402. * @return void
  8403. */
  8404. public function testControlLabelManipulationRadios()
  8405. {
  8406. $result = $this->Form->control('test', [
  8407. 'type' => 'radio',
  8408. 'options' => ['A', 'B'],
  8409. 'labelOptions' => ['class' => 'custom-class']
  8410. ]);
  8411. $expected = [
  8412. ['div' => ['class' => 'input radio']],
  8413. '<label',
  8414. 'Test',
  8415. '/label',
  8416. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8417. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8418. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8419. 'A',
  8420. '/label',
  8421. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8422. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8423. 'B',
  8424. '/label',
  8425. '/div',
  8426. ];
  8427. $this->assertHtml($expected, $result);
  8428. $result = $this->Form->control('test', [
  8429. 'type' => 'radio',
  8430. 'options' => ['A', 'B'],
  8431. 'value' => 1,
  8432. 'labelOptions' => ['class' => 'custom-class']
  8433. ]);
  8434. $expected = [
  8435. ['div' => ['class' => 'input radio']],
  8436. '<label',
  8437. 'Test',
  8438. '/label',
  8439. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8440. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8441. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8442. 'A',
  8443. '/label',
  8444. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8445. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8446. 'B',
  8447. '/label',
  8448. '/div',
  8449. ];
  8450. $this->assertHtml($expected, $result);
  8451. $result = $this->Form->control('test', [
  8452. 'type' => 'radio',
  8453. 'options' => ['A', 'B'],
  8454. 'value' => 1,
  8455. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8456. ]);
  8457. $expected = [
  8458. ['div' => ['class' => 'input radio']],
  8459. '<label',
  8460. 'Test',
  8461. '/label',
  8462. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8463. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8464. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8465. 'A',
  8466. '/label',
  8467. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8468. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8469. 'B',
  8470. '/label',
  8471. '/div',
  8472. ];
  8473. $this->assertHtml($expected, $result);
  8474. $result = $this->Form->radio('test', ['A', 'B'], [
  8475. 'label' => [
  8476. 'class' => ['custom-class', 'another-class'],
  8477. 'data-name' => 'bob'
  8478. ],
  8479. 'value' => 1
  8480. ]);
  8481. $expected = [
  8482. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8483. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8484. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8485. 'A',
  8486. '/label',
  8487. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8488. ['input' => [
  8489. 'type' => 'radio',
  8490. 'name' => 'test',
  8491. 'value' => '1',
  8492. 'id' => 'test-1',
  8493. 'checked' => 'checked'
  8494. ]],
  8495. 'B',
  8496. '/label',
  8497. ];
  8498. $this->assertHtml($expected, $result);
  8499. }
  8500. /**
  8501. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8502. *
  8503. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8504. * added to the class if checked.
  8505. *
  8506. * Also checks to make sure any custom attributes are rendered correctly
  8507. *
  8508. * @return void
  8509. */
  8510. public function testControlLabelManipulationCheckboxes()
  8511. {
  8512. $result = $this->Form->control('checkbox1', [
  8513. 'label' => 'My checkboxes',
  8514. 'multiple' => 'checkbox',
  8515. 'type' => 'select',
  8516. 'options' => [
  8517. ['text' => 'First Checkbox', 'value' => 1],
  8518. ['text' => 'Second Checkbox', 'value' => 2]
  8519. ],
  8520. 'labelOptions' => ['class' => 'custom-class'],
  8521. 'value' => ['1']
  8522. ]);
  8523. $expected = [
  8524. ['div' => ['class' => 'input select']],
  8525. ['label' => ['for' => 'checkbox1']],
  8526. 'My checkboxes',
  8527. '/label',
  8528. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8529. ['div' => ['class' => 'checkbox']],
  8530. ['label' => [
  8531. 'class' => 'custom-class selected',
  8532. 'for' => 'checkbox1-1'
  8533. ]],
  8534. ['input' => [
  8535. 'type' => 'checkbox',
  8536. 'name' => 'checkbox1[]',
  8537. 'value' => '1',
  8538. 'id' => 'checkbox1-1',
  8539. 'checked' => 'checked'
  8540. ]],
  8541. 'First Checkbox',
  8542. '/label',
  8543. '/div',
  8544. ['div' => ['class' => 'checkbox']],
  8545. ['label' => [
  8546. 'class' => 'custom-class',
  8547. 'for' => 'checkbox1-2'
  8548. ]],
  8549. ['input' => [
  8550. 'type' => 'checkbox',
  8551. 'name' => 'checkbox1[]',
  8552. 'value' => '2',
  8553. 'id' => 'checkbox1-2'
  8554. ]],
  8555. 'Second Checkbox',
  8556. '/label',
  8557. '/div',
  8558. '/div'
  8559. ];
  8560. $this->assertHtml($expected, $result);
  8561. $result = $this->Form->control('checkbox1', [
  8562. 'label' => 'My checkboxes',
  8563. 'multiple' => 'checkbox',
  8564. 'type' => 'select',
  8565. 'options' => [
  8566. ['text' => 'First Checkbox', 'value' => 1],
  8567. ['text' => 'Second Checkbox', 'value' => 2]
  8568. ],
  8569. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8570. 'value' => ['1']
  8571. ]);
  8572. $expected = [
  8573. ['div' => ['class' => 'input select']],
  8574. ['label' => ['for' => 'checkbox1']],
  8575. 'My checkboxes',
  8576. '/label',
  8577. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8578. ['div' => ['class' => 'checkbox']],
  8579. ['label' => [
  8580. 'class' => 'custom-class another-class selected',
  8581. 'data-name' => 'bob',
  8582. 'for' => 'checkbox1-1'
  8583. ]],
  8584. ['input' => [
  8585. 'type' => 'checkbox',
  8586. 'name' => 'checkbox1[]',
  8587. 'value' => '1',
  8588. 'id' => 'checkbox1-1',
  8589. 'checked' => 'checked'
  8590. ]],
  8591. 'First Checkbox',
  8592. '/label',
  8593. '/div',
  8594. ['div' => ['class' => 'checkbox']],
  8595. ['label' => [
  8596. 'class' => 'custom-class another-class',
  8597. 'data-name' => 'bob',
  8598. 'for' => 'checkbox1-2'
  8599. ]],
  8600. ['input' => [
  8601. 'type' => 'checkbox',
  8602. 'name' => 'checkbox1[]',
  8603. 'value' => '2',
  8604. 'id' => 'checkbox1-2'
  8605. ]],
  8606. 'Second Checkbox',
  8607. '/label',
  8608. '/div',
  8609. '/div'
  8610. ];
  8611. $this->assertHtml($expected, $result);
  8612. }
  8613. }