FormHelperTest.php 303 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243
  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->schema($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->schema($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. Configure::write('App.namespace', '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::salt('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. * @expectedException \RuntimeException
  289. * @return void
  290. */
  291. public function testAddWidgetInvalid()
  292. {
  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. * @expectedException \RuntimeException
  353. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  354. * @return void
  355. */
  356. public function testAddContextProviderInvalid()
  357. {
  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. return [
  382. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  383. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  384. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  385. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  386. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  387. 'form' => [$form, 'Cake\View\Form\FormContext'],
  388. 'none' => [null, 'Cake\View\Form\NullContext'],
  389. 'false' => [false, 'Cake\View\Form\NullContext'],
  390. ];
  391. }
  392. /**
  393. * Test default context selection in create()
  394. *
  395. * @dataProvider contextSelectionProvider
  396. * @return void
  397. */
  398. public function testCreateContextSelectionBuiltIn($data, $class)
  399. {
  400. $this->loadFixtures('Articles');
  401. $this->Form->create($data);
  402. $this->assertInstanceOf($class, $this->Form->context());
  403. }
  404. /**
  405. * Data provider for type option.
  406. *
  407. * @return array
  408. */
  409. public static function requestTypeProvider()
  410. {
  411. return [
  412. // type, method, override
  413. ['post', 'post', 'POST'],
  414. ['put', 'post', 'PUT'],
  415. ['patch', 'post', 'PATCH'],
  416. ['delete', 'post', 'DELETE'],
  417. ];
  418. }
  419. /**
  420. * Test creating file forms.
  421. *
  422. * @return void
  423. */
  424. public function testCreateFile()
  425. {
  426. $encoding = strtolower(Configure::read('App.encoding'));
  427. $result = $this->Form->create(false, ['type' => 'file']);
  428. $expected = [
  429. 'form' => [
  430. 'method' => 'post', 'action' => '/articles/add',
  431. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  432. ],
  433. 'div' => ['style' => 'display:none;'],
  434. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  435. '/div'
  436. ];
  437. $this->assertHtml($expected, $result);
  438. }
  439. /**
  440. * Test creating GET forms.
  441. *
  442. * @return void
  443. */
  444. public function testCreateGet()
  445. {
  446. $encoding = strtolower(Configure::read('App.encoding'));
  447. $result = $this->Form->create(false, ['type' => 'get']);
  448. $expected = ['form' => [
  449. 'method' => 'get', 'action' => '/articles/add',
  450. 'accept-charset' => $encoding
  451. ]];
  452. $this->assertHtml($expected, $result);
  453. }
  454. /**
  455. * Test explicit method/enctype options.
  456. *
  457. * Explicit method overwrites inferred method from 'type'
  458. *
  459. * @return void
  460. */
  461. public function testCreateExplicitMethodEnctype()
  462. {
  463. $encoding = strtolower(Configure::read('App.encoding'));
  464. $result = $this->Form->create(false, [
  465. 'type' => 'get',
  466. 'method' => 'put',
  467. 'enctype' => 'multipart/form-data'
  468. ]);
  469. $expected = ['form' => [
  470. 'method' => 'put',
  471. 'action' => '/articles/add',
  472. 'enctype' => 'multipart/form-data',
  473. 'accept-charset' => $encoding
  474. ]];
  475. $this->assertHtml($expected, $result);
  476. }
  477. /**
  478. * Test create() with the templates option.
  479. *
  480. * @return void
  481. */
  482. public function testCreateTemplatesArray()
  483. {
  484. $result = $this->Form->create($this->article, [
  485. 'templates' => [
  486. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  487. ]
  488. ]);
  489. $expected = [
  490. 'form' => [
  491. 'class' => 'form-horizontal',
  492. 'method' => 'post',
  493. 'action' => '/articles/add',
  494. 'accept-charset' => 'utf-8'
  495. ]
  496. ];
  497. $this->assertHtml($expected, $result);
  498. }
  499. /**
  500. * Test create() with the templates option.
  501. *
  502. * @return void
  503. */
  504. public function testCreateTemplatesFile()
  505. {
  506. $result = $this->Form->create($this->article, [
  507. 'templates' => 'htmlhelper_tags',
  508. ]);
  509. $expected = [
  510. 'start form',
  511. 'div' => ['class' => 'hidden'],
  512. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  513. '/div'
  514. ];
  515. $this->assertHtml($expected, $result);
  516. }
  517. /**
  518. * Test that create() and end() restore templates.
  519. *
  520. * @return void
  521. */
  522. public function testCreateEndRestoreTemplates()
  523. {
  524. $this->Form->create($this->article, [
  525. 'templates' => ['input' => 'custom input element']
  526. ]);
  527. $this->Form->end();
  528. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  529. }
  530. /**
  531. * Test using template vars in various templates used by control() method.
  532. *
  533. * @return void
  534. */
  535. public function testControlTemplateVars()
  536. {
  537. $result = $this->Form->control('text', [
  538. 'templates' => [
  539. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  540. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  541. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  542. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  543. ],
  544. 'templateVars' => [
  545. 'forinput' => 'in-input',
  546. 'forlabel' => 'in-label',
  547. 'forgroup' => 'in-group',
  548. 'forcontainer' => 'in-container'
  549. ]
  550. ]);
  551. $expected = [
  552. 'div' => ['class'],
  553. 'label' => ['for'],
  554. 'Text in-label',
  555. '/label',
  556. 'in-group',
  557. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  558. 'in-container',
  559. '/div',
  560. ];
  561. $this->assertHtml($expected, $result);
  562. }
  563. /**
  564. * Test ensuring template variables work in template files loaded
  565. * during control().
  566. *
  567. * @return void
  568. */
  569. public function testControlTemplatesFromFile()
  570. {
  571. $result = $this->Form->control('title', [
  572. 'templates' => 'test_templates',
  573. 'templateVars' => [
  574. 'forcontainer' => 'container-data'
  575. ]
  576. ]);
  577. $expected = [
  578. 'div' => ['class'],
  579. 'label' => ['for'],
  580. 'Title',
  581. '/label',
  582. 'input' => ['name', 'type' => 'text', 'id'],
  583. 'container-data',
  584. '/div',
  585. ];
  586. $this->assertHtml($expected, $result);
  587. }
  588. /**
  589. * Test using template vars in inputSubmit and submitContainer template.
  590. *
  591. * @return void
  592. */
  593. public function testSubmitTemplateVars()
  594. {
  595. $this->Form->templates([
  596. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  597. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  598. ]);
  599. $result = $this->Form->submit('Submit', [
  600. 'templateVars' => [
  601. 'forinput' => 'in-input',
  602. 'forcontainer' => 'in-container'
  603. ]
  604. ]);
  605. $expected = [
  606. 'div' => ['class'],
  607. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  608. 'in-container',
  609. '/div',
  610. ];
  611. $this->assertHtml($expected, $result);
  612. }
  613. /**
  614. * test the create() method
  615. *
  616. * @dataProvider requestTypeProvider
  617. * @return void
  618. */
  619. public function testCreateTypeOptions($type, $method, $override)
  620. {
  621. $encoding = strtolower(Configure::read('App.encoding'));
  622. $result = $this->Form->create(false, ['type' => $type]);
  623. $expected = [
  624. 'form' => [
  625. 'method' => $method, 'action' => '/articles/add',
  626. 'accept-charset' => $encoding
  627. ],
  628. 'div' => ['style' => 'display:none;'],
  629. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  630. '/div'
  631. ];
  632. $this->assertHtml($expected, $result);
  633. }
  634. /**
  635. * Test using template vars in Create (formStart template)
  636. *
  637. * @return void
  638. */
  639. public function testCreateTemplateVars()
  640. {
  641. $result = $this->Form->create($this->article, [
  642. 'templates' => [
  643. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  644. ],
  645. 'templateVars' => ['header' => 'headertext']
  646. ]);
  647. $expected = [
  648. 'h4' => ['class'],
  649. 'headertext',
  650. '/h4',
  651. 'form' => [
  652. 'method' => 'post',
  653. 'action' => '/articles/add',
  654. 'accept-charset' => 'utf-8'
  655. ]
  656. ];
  657. $this->assertHtml($expected, $result);
  658. }
  659. /**
  660. * Test opening a form for an update operation.
  661. *
  662. * @return void
  663. */
  664. public function testCreateUpdateForm()
  665. {
  666. $encoding = strtolower(Configure::read('App.encoding'));
  667. $this->Form->request->here = '/articles/edit/1';
  668. $this->Form->request->params['action'] = 'edit';
  669. $this->article['defaults']['id'] = 1;
  670. $result = $this->Form->create($this->article);
  671. $expected = [
  672. 'form' => [
  673. 'method' => 'post',
  674. 'action' => '/articles/edit/1',
  675. 'accept-charset' => $encoding
  676. ],
  677. 'div' => ['style' => 'display:none;'],
  678. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  679. '/div'
  680. ];
  681. $this->assertHtml($expected, $result);
  682. }
  683. /**
  684. * test create() with automatic url generation
  685. *
  686. * @return void
  687. */
  688. public function testCreateAutoUrl()
  689. {
  690. $encoding = strtolower(Configure::read('App.encoding'));
  691. $this->Form->request->params['action'] = 'delete';
  692. $this->Form->request->here = '/articles/delete/10';
  693. $this->Form->request->base = '';
  694. $result = $this->Form->create($this->article);
  695. $expected = [
  696. 'form' => [
  697. 'method' => 'post', 'action' => '/articles/delete/10',
  698. 'accept-charset' => $encoding
  699. ],
  700. 'div' => ['style' => 'display:none;'],
  701. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  702. '/div'
  703. ];
  704. $this->assertHtml($expected, $result);
  705. $this->article['defaults'] = ['id' => 1];
  706. $this->Form->request->here = '/articles/edit/1';
  707. $this->Form->request->params['action'] = 'delete';
  708. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  709. $expected = [
  710. 'form' => [
  711. 'method' => 'post',
  712. 'action' => '/articles/edit/1',
  713. 'accept-charset' => $encoding
  714. ],
  715. 'div' => ['style' => 'display:none;'],
  716. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  717. '/div'
  718. ];
  719. $this->assertHtml($expected, $result);
  720. $this->Form->request->params['action'] = 'add';
  721. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  722. $expected = [
  723. 'form' => [
  724. 'method' => 'post',
  725. 'action' => '/articles/publish/1',
  726. 'accept-charset' => $encoding
  727. ],
  728. 'div' => ['style' => 'display:none;'],
  729. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  730. '/div'
  731. ];
  732. $this->assertHtml($expected, $result);
  733. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  734. $expected = [
  735. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  736. 'div' => ['style' => 'display:none;'],
  737. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  738. '/div'
  739. ];
  740. $this->assertHtml($expected, $result);
  741. $this->Form->request->params['controller'] = 'Pages';
  742. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  743. $expected = [
  744. 'form' => [
  745. 'method' => 'post', 'action' => '/Pages/signup/1',
  746. 'accept-charset' => $encoding
  747. ],
  748. 'div' => ['style' => 'display:none;'],
  749. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  750. '/div'
  751. ];
  752. $this->assertHtml($expected, $result);
  753. }
  754. /**
  755. * Test create() with no URL (no "action" attribute for <form> tag)
  756. *
  757. * @return void
  758. */
  759. public function testCreateNoUrl()
  760. {
  761. $result = $this->Form->create(false, ['url' => false]);
  762. $expected = [
  763. 'form' => [
  764. 'method' => 'post',
  765. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  766. ],
  767. 'div' => ['style' => 'display:none;'],
  768. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  769. '/div'
  770. ];
  771. $this->assertHtml($expected, $result);
  772. }
  773. /**
  774. * test create() with a custom route
  775. *
  776. * @return void
  777. */
  778. public function testCreateCustomRoute()
  779. {
  780. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  781. $encoding = strtolower(Configure::read('App.encoding'));
  782. $this->Form->request->params['controller'] = 'users';
  783. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  784. $expected = [
  785. 'form' => [
  786. 'method' => 'post', 'action' => '/login',
  787. 'accept-charset' => $encoding
  788. ],
  789. 'div' => ['style' => 'display:none;'],
  790. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  791. '/div'
  792. ];
  793. $this->assertHtml($expected, $result);
  794. Router::connect(
  795. '/new-article',
  796. ['controller' => 'articles', 'action' => 'myaction'],
  797. ['_name' => 'my-route']
  798. );
  799. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  800. $expected = [
  801. 'form' => [
  802. 'method' => 'post', 'action' => '/new-article',
  803. 'accept-charset' => $encoding,
  804. ],
  805. 'div' => ['style' => 'display:none;'],
  806. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  807. '/div'
  808. ];
  809. $this->assertHtml($expected, $result);
  810. }
  811. /**
  812. * test automatic accept-charset overriding
  813. *
  814. * @return void
  815. */
  816. public function testCreateWithAcceptCharset()
  817. {
  818. $result = $this->Form->create(
  819. $this->article,
  820. [
  821. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  822. ]
  823. );
  824. $expected = [
  825. 'form' => [
  826. 'method' => 'post', 'action' => '/articles',
  827. 'accept-charset' => 'iso-8859-1'
  828. ],
  829. 'div' => ['style' => 'display:none;'],
  830. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  831. '/div'
  832. ];
  833. $this->assertHtml($expected, $result);
  834. }
  835. /**
  836. * Test base form URL when url param is passed with multiple parameters (&)
  837. *
  838. */
  839. public function testCreateQueryStringRequest()
  840. {
  841. $encoding = strtolower(Configure::read('App.encoding'));
  842. $result = $this->Form->create($this->article, [
  843. 'type' => 'post',
  844. 'escape' => false,
  845. 'url' => [
  846. 'controller' => 'controller',
  847. 'action' => 'action',
  848. '?' => ['param1' => 'value1', 'param2' => 'value2']
  849. ]
  850. ]);
  851. $expected = [
  852. 'form' => [
  853. 'method' => 'post',
  854. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  855. 'accept-charset' => $encoding
  856. ],
  857. 'div' => ['style' => 'display:none;'],
  858. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  859. '/div'
  860. ];
  861. $this->assertHtml($expected, $result);
  862. $result = $this->Form->create($this->article, [
  863. 'type' => 'post',
  864. 'url' => [
  865. 'controller' => 'controller',
  866. 'action' => 'action',
  867. '?' => ['param1' => 'value1', 'param2' => 'value2']
  868. ]
  869. ]);
  870. $this->assertHtml($expected, $result);
  871. }
  872. /**
  873. * test that create() doesn't cause errors by multiple id's being in the primary key
  874. * as could happen with multiple select or checkboxes.
  875. *
  876. * @return void
  877. */
  878. public function testCreateWithMultipleIdInData()
  879. {
  880. $encoding = strtolower(Configure::read('App.encoding'));
  881. $this->Form->request->data['Article']['id'] = [1, 2];
  882. $result = $this->Form->create($this->article);
  883. $expected = [
  884. 'form' => [
  885. 'method' => 'post',
  886. 'action' => '/articles/add',
  887. 'accept-charset' => $encoding
  888. ],
  889. 'div' => ['style' => 'display:none;'],
  890. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  891. '/div'
  892. ];
  893. $this->assertHtml($expected, $result);
  894. }
  895. /**
  896. * test that create() doesn't add in extra passed params.
  897. *
  898. * @return void
  899. */
  900. public function testCreatePassedArgs()
  901. {
  902. $encoding = strtolower(Configure::read('App.encoding'));
  903. $this->Form->request->data['Article']['id'] = 1;
  904. $result = $this->Form->create($this->article, [
  905. 'type' => 'post',
  906. 'escape' => false,
  907. 'url' => [
  908. 'action' => 'edit',
  909. 'myparam'
  910. ]
  911. ]);
  912. $expected = [
  913. 'form' => [
  914. 'method' => 'post',
  915. 'action' => '/articles/edit/myparam',
  916. 'accept-charset' => $encoding
  917. ],
  918. 'div' => ['style' => 'display:none;'],
  919. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  920. '/div'
  921. ];
  922. $this->assertHtml($expected, $result);
  923. }
  924. /**
  925. * test creating a get form, and get form inputs.
  926. *
  927. * @return void
  928. */
  929. public function testGetFormCreate()
  930. {
  931. $encoding = strtolower(Configure::read('App.encoding'));
  932. $result = $this->Form->create($this->article, ['type' => 'get']);
  933. $expected = ['form' => [
  934. 'method' => 'get', 'action' => '/articles/add',
  935. 'accept-charset' => $encoding
  936. ]];
  937. $this->assertHtml($expected, $result);
  938. $result = $this->Form->text('title');
  939. $expected = ['input' => [
  940. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  941. ]];
  942. $this->assertHtml($expected, $result);
  943. $result = $this->Form->password('password');
  944. $expected = ['input' => [
  945. 'name' => 'password', 'type' => 'password'
  946. ]];
  947. $this->assertHtml($expected, $result);
  948. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  949. $result = $this->Form->text('user_form');
  950. $expected = ['input' => [
  951. 'name' => 'user_form', 'type' => 'text'
  952. ]];
  953. $this->assertHtml($expected, $result);
  954. }
  955. /**
  956. * test get form, and inputs when the model param is false
  957. *
  958. * @return void
  959. */
  960. public function testGetFormWithFalseModel()
  961. {
  962. $encoding = strtolower(Configure::read('App.encoding'));
  963. $this->Form->request->params['controller'] = 'contact_test';
  964. $result = $this->Form->create(false, [
  965. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  966. ]);
  967. $expected = ['form' => [
  968. 'method' => 'get', 'action' => '/contact_test/add',
  969. 'accept-charset' => $encoding
  970. ]];
  971. $this->assertHtml($expected, $result);
  972. $result = $this->Form->text('reason');
  973. $expected = [
  974. 'input' => ['type' => 'text', 'name' => 'reason']
  975. ];
  976. $this->assertHtml($expected, $result);
  977. }
  978. /**
  979. * testFormCreateWithSecurity method
  980. *
  981. * Test form->create() with security key.
  982. *
  983. * @return void
  984. */
  985. public function testCreateWithSecurity()
  986. {
  987. $this->Form->request->params['_csrfToken'] = 'testKey';
  988. $encoding = strtolower(Configure::read('App.encoding'));
  989. $result = $this->Form->create($this->article, [
  990. 'url' => '/articles/publish',
  991. ]);
  992. $expected = [
  993. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  994. 'div' => ['style' => 'display:none;'],
  995. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  996. ['input' => [
  997. 'type' => 'hidden',
  998. 'name' => '_csrfToken',
  999. 'value' => 'testKey',
  1000. 'autocomplete' => 'off',
  1001. ]],
  1002. '/div'
  1003. ];
  1004. $this->assertHtml($expected, $result);
  1005. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1006. $expected['form']['id'] = 'MyForm';
  1007. $this->assertHtml($expected, $result);
  1008. }
  1009. /**
  1010. * testFormCreateGetNoSecurity method
  1011. *
  1012. * Test form->create() with no security key as its a get form
  1013. *
  1014. * @return void
  1015. */
  1016. public function testCreateEndGetNoSecurity()
  1017. {
  1018. $this->Form->request->params['_csrfToken'] = 'testKey';
  1019. $article = new Article();
  1020. $result = $this->Form->create($article, [
  1021. 'type' => 'get',
  1022. 'url' => '/contacts/add'
  1023. ]);
  1024. $this->assertNotContains('testKey', $result);
  1025. $result = $this->Form->end();
  1026. $this->assertNotContains('testKey', $result);
  1027. }
  1028. /**
  1029. * test that create() clears the fields property so it starts fresh
  1030. *
  1031. * @return void
  1032. */
  1033. public function testCreateClearingFields()
  1034. {
  1035. $this->Form->fields = ['model_id'];
  1036. $this->Form->create($this->article);
  1037. $this->assertEquals([], $this->Form->fields);
  1038. }
  1039. /**
  1040. * Tests form hash generation with model-less data
  1041. *
  1042. * @return void
  1043. */
  1044. public function testValidateHashNoModel()
  1045. {
  1046. $this->Form->request->params['_Token'] = 'foo';
  1047. $result = $this->Form->secure(['anything']);
  1048. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1049. }
  1050. /**
  1051. * Tests that hidden fields generated for checkboxes don't get locked
  1052. *
  1053. * @return void
  1054. */
  1055. public function testNoCheckboxLocking()
  1056. {
  1057. $this->Form->request->params['_Token'] = 'foo';
  1058. $this->assertSame([], $this->Form->fields);
  1059. $this->Form->checkbox('check', ['value' => '1']);
  1060. $this->assertSame($this->Form->fields, ['check']);
  1061. }
  1062. /**
  1063. * testFormSecurityFields method
  1064. *
  1065. * Test generation of secure form hash generation.
  1066. *
  1067. * @return void
  1068. */
  1069. public function testFormSecurityFields()
  1070. {
  1071. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1072. $this->Form->request->params['_Token'] = 'testKey';
  1073. $result = $this->Form->secure($fields);
  1074. $hash = Security::hash(serialize($fields) . Security::salt());
  1075. $hash .= ':' . 'Model.valid';
  1076. $hash = urlencode($hash);
  1077. $tokenDebug = urlencode(json_encode([
  1078. '',
  1079. $fields,
  1080. []
  1081. ]));
  1082. $expected = [
  1083. 'div' => ['style' => 'display:none;'],
  1084. ['input' => [
  1085. 'type' => 'hidden',
  1086. 'name' => '_Token[fields]',
  1087. 'value' => $hash,
  1088. 'autocomplete' => 'off',
  1089. ]],
  1090. ['input' => [
  1091. 'type' => 'hidden',
  1092. 'name' => '_Token[unlocked]',
  1093. 'value' => '',
  1094. 'autocomplete' => 'off',
  1095. ]],
  1096. ['input' => [
  1097. 'type' => 'hidden',
  1098. 'name' => '_Token[debug]',
  1099. 'value' => $tokenDebug,
  1100. 'autocomplete' => 'off',
  1101. ]],
  1102. '/div'
  1103. ];
  1104. $this->assertHtml($expected, $result);
  1105. }
  1106. /**
  1107. * testFormSecurityFields method
  1108. *
  1109. * Test debug token is not generated if debug is false
  1110. *
  1111. * @return void
  1112. */
  1113. public function testFormSecurityFieldsNoDebugMode()
  1114. {
  1115. Configure::write('debug', false);
  1116. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1117. $this->Form->request->params['_Token'] = 'testKey';
  1118. $result = $this->Form->secure($fields);
  1119. $hash = Security::hash(serialize($fields) . Security::salt());
  1120. $hash .= ':' . 'Model.valid';
  1121. $hash = urlencode($hash);
  1122. $expected = [
  1123. 'div' => ['style' => 'display:none;'],
  1124. ['input' => [
  1125. 'type' => 'hidden',
  1126. 'name' => '_Token[fields]',
  1127. 'autocomplete' => 'off',
  1128. 'value' => $hash
  1129. ]],
  1130. ['input' => [
  1131. 'type' => 'hidden',
  1132. 'name' => '_Token[unlocked]',
  1133. 'autocomplete' => 'off',
  1134. 'value' => '',
  1135. ]],
  1136. '/div'
  1137. ];
  1138. $this->assertHtml($expected, $result);
  1139. }
  1140. /**
  1141. * Tests correct generation of number fields for smallint
  1142. *
  1143. * @return void
  1144. */
  1145. public function testTextFieldGenerationForSmallint()
  1146. {
  1147. $this->article['schema'] = [
  1148. 'foo' => [
  1149. 'type' => 'smallinteger',
  1150. 'null' => false,
  1151. 'default' => null,
  1152. 'length' => 10
  1153. ]
  1154. ];
  1155. $this->Form->create($this->article);
  1156. $result = $this->Form->control('foo');
  1157. $this->assertContains('class="input number"', $result);
  1158. $this->assertContains('type="number"', $result);
  1159. }
  1160. /**
  1161. * Tests correct generation of number fields for tinyint
  1162. *
  1163. * @return void
  1164. */
  1165. public function testTextFieldGenerationForTinyint()
  1166. {
  1167. $this->article['schema'] = [
  1168. 'foo' => [
  1169. 'type' => 'tinyinteger',
  1170. 'null' => false,
  1171. 'default' => null,
  1172. 'length' => 10
  1173. ]
  1174. ];
  1175. $this->Form->create($this->article);
  1176. $result = $this->Form->control('foo');
  1177. $this->assertContains('class="input number"', $result);
  1178. $this->assertContains('type="number"', $result);
  1179. }
  1180. /**
  1181. * Tests correct generation of number fields for double and float fields
  1182. *
  1183. * @return void
  1184. */
  1185. public function testTextFieldGenerationForFloats()
  1186. {
  1187. $this->article['schema'] = [
  1188. 'foo' => [
  1189. 'type' => 'float',
  1190. 'null' => false,
  1191. 'default' => null,
  1192. 'length' => 10
  1193. ]
  1194. ];
  1195. $this->Form->create($this->article);
  1196. $result = $this->Form->control('foo');
  1197. $expected = [
  1198. 'div' => ['class' => 'input number'],
  1199. 'label' => ['for' => 'foo'],
  1200. 'Foo',
  1201. '/label',
  1202. ['input' => [
  1203. 'type' => 'number',
  1204. 'name' => 'foo',
  1205. 'id' => 'foo',
  1206. 'step' => 'any'
  1207. ]],
  1208. '/div'
  1209. ];
  1210. $this->assertHtml($expected, $result);
  1211. $result = $this->Form->control('foo', ['step' => 0.5]);
  1212. $expected = [
  1213. 'div' => ['class' => 'input number'],
  1214. 'label' => ['for' => 'foo'],
  1215. 'Foo',
  1216. '/label',
  1217. ['input' => [
  1218. 'type' => 'number',
  1219. 'name' => 'foo',
  1220. 'id' => 'foo',
  1221. 'step' => '0.5'
  1222. ]],
  1223. '/div'
  1224. ];
  1225. $this->assertHtml($expected, $result);
  1226. }
  1227. /**
  1228. * Tests correct generation of number fields for integer fields
  1229. *
  1230. * @return void
  1231. */
  1232. public function testTextFieldTypeNumberGenerationForIntegers()
  1233. {
  1234. TableRegistry::get('Contacts', [
  1235. 'className' => __NAMESPACE__ . '\ContactsTable'
  1236. ]);
  1237. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1238. $result = $this->Form->control('age');
  1239. $expected = [
  1240. 'div' => ['class' => 'input number'],
  1241. 'label' => ['for' => 'age'],
  1242. 'Age',
  1243. '/label',
  1244. ['input' => [
  1245. 'type' => 'number', 'name' => 'age',
  1246. 'id' => 'age'
  1247. ]],
  1248. '/div'
  1249. ];
  1250. $this->assertHtml($expected, $result);
  1251. }
  1252. /**
  1253. * Tests correct generation of file upload fields for binary fields
  1254. *
  1255. * @return void
  1256. */
  1257. public function testFileUploadFieldTypeGenerationForBinaries()
  1258. {
  1259. $table = TableRegistry::get('Contacts', [
  1260. 'className' => __NAMESPACE__ . '\ContactsTable'
  1261. ]);
  1262. $table->schema(['foo' => [
  1263. 'type' => 'binary',
  1264. 'null' => false,
  1265. 'default' => null,
  1266. 'length' => 1024
  1267. ]]);
  1268. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1269. $result = $this->Form->control('foo');
  1270. $expected = [
  1271. 'div' => ['class' => 'input file'],
  1272. 'label' => ['for' => 'foo'],
  1273. 'Foo',
  1274. '/label',
  1275. ['input' => [
  1276. 'type' => 'file', 'name' => 'foo',
  1277. 'id' => 'foo'
  1278. ]],
  1279. '/div'
  1280. ];
  1281. $this->assertHtml($expected, $result);
  1282. }
  1283. /**
  1284. * testFormSecurityMultipleFields method
  1285. *
  1286. * Test secure() with multiple row form. Ensure hash is correct.
  1287. *
  1288. * @return void
  1289. */
  1290. public function testFormSecurityMultipleFields()
  1291. {
  1292. $this->Form->request->params['_Token'] = 'foo';
  1293. $fields = [
  1294. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1295. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1296. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1297. ];
  1298. $result = $this->Form->secure($fields);
  1299. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1300. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1301. $tokenDebug = urlencode(json_encode([
  1302. '',
  1303. $fields,
  1304. []
  1305. ]));
  1306. $expected = [
  1307. 'div' => ['style' => 'display:none;'],
  1308. ['input' => [
  1309. 'type' => 'hidden',
  1310. 'name' => '_Token[fields]',
  1311. 'value' => $hash,
  1312. 'autocomplete' => 'off',
  1313. ]],
  1314. ['input' => [
  1315. 'type' => 'hidden',
  1316. 'name' => '_Token[unlocked]',
  1317. 'autocomplete' => 'off',
  1318. 'value' => ''
  1319. ]],
  1320. ['input' => [
  1321. 'type' => 'hidden',
  1322. 'name' => '_Token[debug]',
  1323. 'value' => $tokenDebug,
  1324. 'autocomplete' => 'off',
  1325. ]],
  1326. '/div'
  1327. ];
  1328. $this->assertHtml($expected, $result);
  1329. }
  1330. /**
  1331. * testFormSecurityMultipleSubmitButtons
  1332. *
  1333. * test form submit generation and ensure that _Token is only created on end()
  1334. *
  1335. * @return void
  1336. */
  1337. public function testFormSecurityMultipleSubmitButtons()
  1338. {
  1339. $this->Form->request->params['_Token'] = 'testKey';
  1340. $this->Form->create($this->article);
  1341. $this->Form->text('Address.title');
  1342. $this->Form->text('Address.first_name');
  1343. $result = $this->Form->submit('Save', ['name' => 'save']);
  1344. $expected = [
  1345. 'div' => ['class' => 'submit'],
  1346. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1347. '/div',
  1348. ];
  1349. $this->assertHtml($expected, $result);
  1350. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1351. $expected = [
  1352. 'div' => ['class' => 'submit'],
  1353. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1354. '/div',
  1355. ];
  1356. $this->assertHtml($expected, $result);
  1357. $result = $this->Form->end();
  1358. $tokenDebug = urlencode(json_encode([
  1359. '/articles/add',
  1360. [
  1361. 'Address.title',
  1362. 'Address.first_name',
  1363. ],
  1364. ['save', 'cancel']
  1365. ]));
  1366. $expected = [
  1367. 'div' => ['style' => 'display:none;'],
  1368. ['input' => [
  1369. 'type' => 'hidden',
  1370. 'name' => '_Token[fields]',
  1371. 'autocomplete',
  1372. 'value'
  1373. ]],
  1374. ['input' => [
  1375. 'type' => 'hidden',
  1376. 'name' => '_Token[unlocked]',
  1377. 'value' => 'cancel%7Csave',
  1378. 'autocomplete' => 'off',
  1379. ]],
  1380. ['input' => [
  1381. 'type' => 'hidden',
  1382. 'name' => '_Token[debug]',
  1383. 'value' => $tokenDebug,
  1384. 'autocomplete' => 'off',
  1385. ]],
  1386. '/div'
  1387. ];
  1388. $this->assertHtml($expected, $result);
  1389. }
  1390. /**
  1391. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1392. *
  1393. * @return void
  1394. */
  1395. public function testSecurityButtonNestedNamed()
  1396. {
  1397. $key = 'testKey';
  1398. $this->Form->request->params['_csrfToken'] = $key;
  1399. $this->Form->create('Addresses');
  1400. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1401. $result = $this->Form->unlockField();
  1402. $this->assertEquals(['Address.button'], $result);
  1403. }
  1404. /**
  1405. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1406. *
  1407. * @return void
  1408. */
  1409. public function testSecuritySubmitNestedNamed()
  1410. {
  1411. $this->Form->request->params['_Token'] = 'testKey';
  1412. $this->Form->create($this->article);
  1413. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1414. $result = $this->Form->unlockField();
  1415. $this->assertEquals(['Address.button'], $result);
  1416. }
  1417. /**
  1418. * Test that the correct fields are unlocked for image submits with no names.
  1419. *
  1420. * @return void
  1421. */
  1422. public function testSecuritySubmitImageNoName()
  1423. {
  1424. $this->Form->request->params['_Token'] = 'testKey';
  1425. $this->Form->create(false);
  1426. $result = $this->Form->submit('save.png');
  1427. $expected = [
  1428. 'div' => ['class' => 'submit'],
  1429. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1430. '/div'
  1431. ];
  1432. $this->assertHtml($expected, $result);
  1433. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1434. }
  1435. /**
  1436. * Test that the correct fields are unlocked for image submits with names.
  1437. *
  1438. * @return void
  1439. */
  1440. public function testSecuritySubmitImageName()
  1441. {
  1442. $this->Form->request->params['_Token'] = 'testKey';
  1443. $this->Form->create(null);
  1444. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1445. $expected = [
  1446. 'div' => ['class' => 'submit'],
  1447. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1448. '/div'
  1449. ];
  1450. $this->assertHtml($expected, $result);
  1451. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1452. }
  1453. /**
  1454. * testFormSecurityMultipleControlFields method
  1455. *
  1456. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1457. *
  1458. * @return void
  1459. */
  1460. public function testFormSecurityMultipleControlFields()
  1461. {
  1462. $this->Form->request->params['_Token'] = 'testKey';
  1463. $this->Form->create();
  1464. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1465. $this->Form->control('Addresses.0.title');
  1466. $this->Form->control('Addresses.0.first_name');
  1467. $this->Form->control('Addresses.0.last_name');
  1468. $this->Form->control('Addresses.0.address');
  1469. $this->Form->control('Addresses.0.city');
  1470. $this->Form->control('Addresses.0.phone');
  1471. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1472. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1473. $this->Form->control('Addresses.1.title');
  1474. $this->Form->control('Addresses.1.first_name');
  1475. $this->Form->control('Addresses.1.last_name');
  1476. $this->Form->control('Addresses.1.address');
  1477. $this->Form->control('Addresses.1.city');
  1478. $this->Form->control('Addresses.1.phone');
  1479. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1480. $result = $this->Form->secure($this->Form->fields);
  1481. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1482. $tokenDebug = urlencode(json_encode([
  1483. '/articles/add',
  1484. [
  1485. 'Addresses.0.id' => '123456',
  1486. 'Addresses.0.title',
  1487. 'Addresses.0.first_name',
  1488. 'Addresses.0.last_name',
  1489. 'Addresses.0.address',
  1490. 'Addresses.0.city',
  1491. 'Addresses.0.phone',
  1492. 'Addresses.0.primary',
  1493. 'Addresses.1.id' => '654321',
  1494. 'Addresses.1.title',
  1495. 'Addresses.1.first_name',
  1496. 'Addresses.1.last_name',
  1497. 'Addresses.1.address',
  1498. 'Addresses.1.city',
  1499. 'Addresses.1.phone',
  1500. 'Addresses.1.primary',
  1501. ],
  1502. []
  1503. ]));
  1504. $expected = [
  1505. 'div' => ['style' => 'display:none;'],
  1506. ['input' => [
  1507. 'type' => 'hidden',
  1508. 'name' => '_Token[fields]',
  1509. 'value' => $hash,
  1510. 'autocomplete' => 'off',
  1511. ]],
  1512. ['input' => [
  1513. 'type' => 'hidden',
  1514. 'name' => '_Token[unlocked]',
  1515. 'autocomplete' => 'off',
  1516. 'value' => ''
  1517. ]],
  1518. ['input' => [
  1519. 'type' => 'hidden',
  1520. 'name' => '_Token[debug]',
  1521. 'value' => $tokenDebug,
  1522. 'autocomplete' => 'off',
  1523. ]],
  1524. '/div'
  1525. ];
  1526. $this->assertHtml($expected, $result);
  1527. }
  1528. /**
  1529. * testFormSecurityArrayFields method
  1530. *
  1531. * Test form security with Model.field.0 style inputs.
  1532. *
  1533. * @return void
  1534. */
  1535. public function testFormSecurityArrayFields()
  1536. {
  1537. $this->Form->request->params['_Token'] = 'testKey';
  1538. $this->Form->create();
  1539. $this->Form->text('Address.primary.1');
  1540. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1541. $this->Form->text('Address.secondary.1.0');
  1542. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1543. }
  1544. /**
  1545. * testFormSecurityMultipleControlDisabledFields method
  1546. *
  1547. * Test secure form generation with multiple records and disabled fields.
  1548. *
  1549. * @return void
  1550. */
  1551. public function testFormSecurityMultipleControlDisabledFields()
  1552. {
  1553. $this->Form->request->params['_Token'] = [
  1554. 'unlockedFields' => ['first_name', 'address']
  1555. ];
  1556. $this->Form->create();
  1557. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1558. $this->Form->text('Addresses.0.title');
  1559. $this->Form->text('Addresses.0.first_name');
  1560. $this->Form->text('Addresses.0.last_name');
  1561. $this->Form->text('Addresses.0.address');
  1562. $this->Form->text('Addresses.0.city');
  1563. $this->Form->text('Addresses.0.phone');
  1564. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1565. $this->Form->text('Addresses.1.title');
  1566. $this->Form->text('Addresses.1.first_name');
  1567. $this->Form->text('Addresses.1.last_name');
  1568. $this->Form->text('Addresses.1.address');
  1569. $this->Form->text('Addresses.1.city');
  1570. $this->Form->text('Addresses.1.phone');
  1571. $result = $this->Form->secure($this->Form->fields);
  1572. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1573. $tokenDebug = urlencode(json_encode([
  1574. '/articles/add',
  1575. [
  1576. 'Addresses.0.id' => '123456',
  1577. 'Addresses.0.title',
  1578. 'Addresses.0.last_name',
  1579. 'Addresses.0.city',
  1580. 'Addresses.0.phone',
  1581. 'Addresses.1.id' => '654321',
  1582. 'Addresses.1.title',
  1583. 'Addresses.1.last_name',
  1584. 'Addresses.1.city',
  1585. 'Addresses.1.phone'
  1586. ],
  1587. [
  1588. 'first_name',
  1589. 'address'
  1590. ]
  1591. ]));
  1592. $expected = [
  1593. 'div' => ['style' => 'display:none;'],
  1594. ['input' => [
  1595. 'type' => 'hidden',
  1596. 'name' => '_Token[fields]',
  1597. 'autocomplete' => 'off',
  1598. 'value' => $hash
  1599. ]],
  1600. ['input' => [
  1601. 'type' => 'hidden',
  1602. 'name' => '_Token[unlocked]',
  1603. 'autocomplete' => 'off',
  1604. 'value' => 'address%7Cfirst_name',
  1605. ]],
  1606. ['input' => [
  1607. 'type' => 'hidden',
  1608. 'name' => '_Token[debug]',
  1609. 'autocomplete' => 'off',
  1610. 'value' => $tokenDebug,
  1611. ]],
  1612. '/div'
  1613. ];
  1614. $this->assertHtml($expected, $result);
  1615. }
  1616. /**
  1617. * testFormSecurityControlDisabledFields method
  1618. *
  1619. * Test single record form with disabled fields.
  1620. *
  1621. * @return void
  1622. */
  1623. public function testFormSecurityControlUnlockedFields()
  1624. {
  1625. $this->Form->request->params['_Token'] = [
  1626. 'unlockedFields' => ['first_name', 'address']
  1627. ];
  1628. $this->Form->create();
  1629. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1630. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1631. $this->Form->text('Addresses.title');
  1632. $this->Form->text('Addresses.first_name');
  1633. $this->Form->text('Addresses.last_name');
  1634. $this->Form->text('Addresses.address');
  1635. $this->Form->text('Addresses.city');
  1636. $this->Form->text('Addresses.phone');
  1637. $result = $this->Form->fields;
  1638. $expected = [
  1639. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1640. 'Addresses.city', 'Addresses.phone'
  1641. ];
  1642. $this->assertEquals($expected, $result);
  1643. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1644. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1645. $tokenDebug = urlencode(json_encode([
  1646. '/articles/add',
  1647. [
  1648. 'Addresses.id' => '123456',
  1649. 'Addresses.title',
  1650. 'Addresses.last_name',
  1651. 'Addresses.city',
  1652. 'Addresses.phone'
  1653. ],
  1654. [
  1655. 'first_name',
  1656. 'address'
  1657. ]
  1658. ]));
  1659. $expected = [
  1660. 'div' => ['style' => 'display:none;'],
  1661. ['input' => [
  1662. 'type' => 'hidden',
  1663. 'name' => '_Token[fields]',
  1664. 'value' => $hash,
  1665. 'autocomplete' => 'off',
  1666. 'data-foo' => 'bar',
  1667. ]],
  1668. ['input' => [
  1669. 'type' => 'hidden',
  1670. 'name' => '_Token[unlocked]',
  1671. 'value' => 'address%7Cfirst_name',
  1672. 'autocomplete' => 'off',
  1673. 'data-foo' => 'bar',
  1674. ]],
  1675. ['input' => [
  1676. 'type' => 'hidden', 'name' => '_Token[debug]',
  1677. 'value' => $tokenDebug,
  1678. 'autocomplete' => 'off',
  1679. 'data-foo' => 'bar'
  1680. ]],
  1681. '/div'
  1682. ];
  1683. $this->assertHtml($expected, $result);
  1684. }
  1685. /**
  1686. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1687. *
  1688. * Test single record form with debugSecurity param.
  1689. *
  1690. * @return void
  1691. */
  1692. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1693. {
  1694. $this->Form->request->params['_Token'] = [
  1695. 'unlockedFields' => ['first_name', 'address']
  1696. ];
  1697. $this->Form->create();
  1698. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1699. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1700. $this->Form->text('Addresses.title');
  1701. $this->Form->text('Addresses.first_name');
  1702. $this->Form->text('Addresses.last_name');
  1703. $this->Form->text('Addresses.address');
  1704. $this->Form->text('Addresses.city');
  1705. $this->Form->text('Addresses.phone');
  1706. $result = $this->Form->fields;
  1707. $expected = [
  1708. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1709. 'Addresses.city', 'Addresses.phone'
  1710. ];
  1711. $this->assertEquals($expected, $result);
  1712. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1713. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1714. $tokenDebug = urlencode(json_encode([
  1715. '/articles/add',
  1716. [
  1717. 'Addresses.id' => '123456',
  1718. 'Addresses.title',
  1719. 'Addresses.last_name',
  1720. 'Addresses.city',
  1721. 'Addresses.phone'
  1722. ],
  1723. [
  1724. 'first_name',
  1725. 'address'
  1726. ]
  1727. ]));
  1728. $expected = [
  1729. 'div' => ['style' => 'display:none;'],
  1730. ['input' => [
  1731. 'type' => 'hidden',
  1732. 'name' => '_Token[fields]',
  1733. 'value' => $hash,
  1734. 'autocomplete' => 'off',
  1735. 'data-foo' => 'bar',
  1736. ]],
  1737. ['input' => [
  1738. 'type' => 'hidden',
  1739. 'name' => '_Token[unlocked]',
  1740. 'value' => 'address%7Cfirst_name',
  1741. 'autocomplete' => 'off',
  1742. 'data-foo' => 'bar',
  1743. ]],
  1744. ['input' => [
  1745. 'type' => 'hidden', 'name' => '_Token[debug]',
  1746. 'value' => $tokenDebug,
  1747. 'autocomplete' => 'off',
  1748. 'data-foo' => 'bar'
  1749. ]],
  1750. '/div'
  1751. ];
  1752. $this->assertHtml($expected, $result);
  1753. }
  1754. /**
  1755. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1756. *
  1757. * Debug is false, debugSecurity is true -> no debug
  1758. *
  1759. * @return void
  1760. */
  1761. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1762. {
  1763. $this->Form->request->params['_Token'] = [
  1764. 'unlockedFields' => ['first_name', 'address']
  1765. ];
  1766. $this->Form->create();
  1767. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1768. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1769. $this->Form->text('Addresses.title');
  1770. $this->Form->text('Addresses.first_name');
  1771. $this->Form->text('Addresses.last_name');
  1772. $this->Form->text('Addresses.address');
  1773. $this->Form->text('Addresses.city');
  1774. $this->Form->text('Addresses.phone');
  1775. $result = $this->Form->fields;
  1776. $expected = [
  1777. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1778. 'Addresses.city', 'Addresses.phone'
  1779. ];
  1780. $this->assertEquals($expected, $result);
  1781. Configure::write('debug', false);
  1782. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1783. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1784. $expected = [
  1785. 'div' => ['style' => 'display:none;'],
  1786. ['input' => [
  1787. 'type' => 'hidden',
  1788. 'name' => '_Token[fields]',
  1789. 'value' => $hash,
  1790. 'autocomplete' => 'off',
  1791. 'data-foo' => 'bar',
  1792. ]],
  1793. ['input' => [
  1794. 'type' => 'hidden',
  1795. 'name' => '_Token[unlocked]',
  1796. 'value' => 'address%7Cfirst_name',
  1797. 'autocomplete' => 'off',
  1798. 'data-foo' => 'bar',
  1799. ]],
  1800. '/div'
  1801. ];
  1802. $this->assertHtml($expected, $result);
  1803. }
  1804. /**
  1805. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1806. *
  1807. * Test single record form with debugSecurity param.
  1808. *
  1809. * @return void
  1810. */
  1811. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1812. {
  1813. $this->Form->request->params['_Token'] = [
  1814. 'unlockedFields' => ['first_name', 'address']
  1815. ];
  1816. $this->Form->create();
  1817. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1818. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1819. $this->Form->text('Addresses.title');
  1820. $this->Form->text('Addresses.first_name');
  1821. $this->Form->text('Addresses.last_name');
  1822. $this->Form->text('Addresses.address');
  1823. $this->Form->text('Addresses.city');
  1824. $this->Form->text('Addresses.phone');
  1825. $result = $this->Form->fields;
  1826. $expected = [
  1827. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1828. 'Addresses.city', 'Addresses.phone'
  1829. ];
  1830. $this->assertEquals($expected, $result);
  1831. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1832. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1833. $expected = [
  1834. 'div' => ['style' => 'display:none;'],
  1835. ['input' => [
  1836. 'type' => 'hidden',
  1837. 'name' => '_Token[fields]',
  1838. 'value' => $hash,
  1839. 'autocomplete' => 'off',
  1840. 'data-foo' => 'bar',
  1841. ]],
  1842. ['input' => [
  1843. 'type' => 'hidden',
  1844. 'name' => '_Token[unlocked]',
  1845. 'value' => 'address%7Cfirst_name',
  1846. 'autocomplete' => 'off',
  1847. 'data-foo' => 'bar',
  1848. ]],
  1849. '/div'
  1850. ];
  1851. $this->assertHtml($expected, $result);
  1852. }
  1853. /**
  1854. * testFormSecureWithCustomNameAttribute method
  1855. *
  1856. * Test securing inputs with custom name attributes.
  1857. *
  1858. * @return void
  1859. */
  1860. public function testFormSecureWithCustomNameAttribute()
  1861. {
  1862. $this->Form->request->params['_Token'] = 'testKey';
  1863. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1864. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1865. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1866. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1867. }
  1868. /**
  1869. * testFormSecuredControl method
  1870. *
  1871. * Test generation of entire secure form, assertions made on control() output.
  1872. *
  1873. * @return void
  1874. */
  1875. public function testFormSecuredControl()
  1876. {
  1877. $this->Form->request->params['_csrfToken'] = 'testKey';
  1878. $this->Form->request->params['_Token'] = 'stuff';
  1879. $this->article['schema'] = [
  1880. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1881. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1882. ];
  1883. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1884. $encoding = strtolower(Configure::read('App.encoding'));
  1885. $expected = [
  1886. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1887. 'div' => ['style' => 'display:none;'],
  1888. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1889. ['input' => [
  1890. 'type' => 'hidden',
  1891. 'name' => '_csrfToken',
  1892. 'value' => 'testKey',
  1893. 'autocomplete' => 'off'
  1894. ]],
  1895. '/div'
  1896. ];
  1897. $this->assertHtml($expected, $result);
  1898. $result = $this->Form->control('ratio');
  1899. $expected = [
  1900. 'div' => ['class'],
  1901. 'label' => ['for'],
  1902. 'Ratio',
  1903. '/label',
  1904. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1905. '/div',
  1906. ];
  1907. $this->assertHtml($expected, $result);
  1908. $result = $this->Form->control('population');
  1909. $expected = [
  1910. 'div' => ['class'],
  1911. 'label' => ['for'],
  1912. 'Population',
  1913. '/label',
  1914. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1915. '/div',
  1916. ];
  1917. $this->assertHtml($expected, $result);
  1918. $result = $this->Form->control('published', ['type' => 'text']);
  1919. $expected = [
  1920. 'div' => ['class' => 'input text'],
  1921. 'label' => ['for' => 'published'],
  1922. 'Published',
  1923. '/label',
  1924. ['input' => [
  1925. 'type' => 'text',
  1926. 'name' => 'published',
  1927. 'id' => 'published'
  1928. ]],
  1929. '/div'
  1930. ];
  1931. $this->assertHtml($expected, $result);
  1932. $result = $this->Form->control('other', ['type' => 'text']);
  1933. $expected = [
  1934. 'div' => ['class' => 'input text'],
  1935. 'label' => ['for' => 'other'],
  1936. 'Other',
  1937. '/label',
  1938. ['input' => [
  1939. 'type' => 'text',
  1940. 'name' => 'other',
  1941. 'id',
  1942. ]],
  1943. '/div'
  1944. ];
  1945. $this->assertHtml($expected, $result);
  1946. $result = $this->Form->hidden('stuff');
  1947. $expected = [
  1948. 'input' => [
  1949. 'type' => 'hidden',
  1950. 'name' => 'stuff'
  1951. ]
  1952. ];
  1953. $this->assertHtml($expected, $result);
  1954. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1955. $expected = ['input' => [
  1956. 'type' => 'hidden',
  1957. 'name' => 'hidden',
  1958. 'value' => '0'
  1959. ]];
  1960. $this->assertHtml($expected, $result);
  1961. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1962. $expected = [
  1963. 'div' => ['class' => 'input checkbox'],
  1964. ['input' => [
  1965. 'type' => 'hidden',
  1966. 'name' => 'something',
  1967. 'value' => '0'
  1968. ]],
  1969. 'label' => ['for' => 'something'],
  1970. ['input' => [
  1971. 'type' => 'checkbox',
  1972. 'name' => 'something',
  1973. 'value' => '1',
  1974. 'id' => 'something'
  1975. ]],
  1976. 'Something',
  1977. '/label',
  1978. '/div'
  1979. ];
  1980. $this->assertHtml($expected, $result);
  1981. $result = $this->Form->fields;
  1982. $expectedFields = [
  1983. 'ratio',
  1984. 'population',
  1985. 'published',
  1986. 'other',
  1987. 'stuff' => '',
  1988. 'hidden' => '0',
  1989. 'something'
  1990. ];
  1991. $this->assertEquals($expectedFields, $result);
  1992. $result = $this->Form->secure($this->Form->fields);
  1993. $tokenDebug = urlencode(json_encode([
  1994. '/articles/add',
  1995. $expectedFields,
  1996. []
  1997. ]));
  1998. $expected = [
  1999. 'div' => ['style' => 'display:none;'],
  2000. ['input' => [
  2001. 'type' => 'hidden',
  2002. 'name' => '_Token[fields]',
  2003. 'value',
  2004. 'autocomplete'
  2005. ]],
  2006. ['input' => [
  2007. 'type' => 'hidden',
  2008. 'name' => '_Token[unlocked]',
  2009. 'value' => '',
  2010. 'autocomplete' => 'off'
  2011. ]],
  2012. ['input' => [
  2013. 'type' => 'hidden', 'name' => '_Token[debug]',
  2014. 'value' => $tokenDebug,
  2015. 'autocomplete' => 'off'
  2016. ]],
  2017. '/div'
  2018. ];
  2019. $this->assertHtml($expected, $result);
  2020. }
  2021. /**
  2022. * testSecuredControlCustomName method
  2023. *
  2024. * Test secured inputs with custom names.
  2025. *
  2026. * @return void
  2027. */
  2028. public function testSecuredControlCustomName()
  2029. {
  2030. $this->Form->request->params['_Token'] = 'testKey';
  2031. $this->assertEquals([], $this->Form->fields);
  2032. $this->Form->text('text_input', [
  2033. 'name' => 'Option[General.default_role]',
  2034. ]);
  2035. $expected = ['Option.General.default_role'];
  2036. $this->assertEquals($expected, $this->Form->fields);
  2037. $this->Form->select('select_box', [1, 2], [
  2038. 'name' => 'Option[General.select_role]',
  2039. ]);
  2040. $expected[] = 'Option.General.select_role';
  2041. $this->assertEquals($expected, $this->Form->fields);
  2042. $this->Form->text('other.things[]');
  2043. $expected[] = 'other.things';
  2044. $this->assertEquals($expected, $this->Form->fields);
  2045. }
  2046. /**
  2047. * testSecuredControlDuplicate method
  2048. *
  2049. * Test that a hidden field followed by a visible field
  2050. * undoes the hidden field locking.
  2051. *
  2052. * @return void
  2053. */
  2054. public function testSecuredControlDuplicate()
  2055. {
  2056. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2057. $this->assertEquals([], $this->Form->fields);
  2058. $this->Form->control('text_val', [
  2059. 'type' => 'hidden',
  2060. 'value' => 'some text',
  2061. ]);
  2062. $expected = ['text_val' => 'some text'];
  2063. $this->assertEquals($expected, $this->Form->fields);
  2064. $this->Form->control('text_val', [
  2065. 'type' => 'text',
  2066. ]);
  2067. $expected = ['text_val'];
  2068. $this->assertEquals($expected, $this->Form->fields);
  2069. }
  2070. /**
  2071. * testFormSecuredFileControl method
  2072. *
  2073. * Tests that the correct keys are added to the field hash index.
  2074. *
  2075. * @return void
  2076. */
  2077. public function testFormSecuredFileControl()
  2078. {
  2079. $this->assertEquals([], $this->Form->fields);
  2080. $this->Form->file('Attachment.file');
  2081. $expected = [
  2082. 'Attachment.file.name', 'Attachment.file.type',
  2083. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2084. 'Attachment.file.size'
  2085. ];
  2086. $this->assertEquals($expected, $this->Form->fields);
  2087. }
  2088. /**
  2089. * testFormSecuredMultipleSelect method
  2090. *
  2091. * Test that multiple selects keys are added to field hash.
  2092. *
  2093. * @return void
  2094. */
  2095. public function testFormSecuredMultipleSelect()
  2096. {
  2097. $this->Form->request->params['_csrfToken'] = 'testKey';
  2098. $this->assertEquals([], $this->Form->fields);
  2099. $options = ['1' => 'one', '2' => 'two'];
  2100. $this->Form->select('Model.select', $options);
  2101. $expected = ['Model.select'];
  2102. $this->assertEquals($expected, $this->Form->fields);
  2103. $this->Form->fields = [];
  2104. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2105. $this->assertEquals($expected, $this->Form->fields);
  2106. }
  2107. /**
  2108. * testFormSecuredRadio method
  2109. *
  2110. * @return void
  2111. */
  2112. public function testFormSecuredRadio()
  2113. {
  2114. $this->Form->request->params['_Token'] = 'testKey';
  2115. $this->assertEquals([], $this->Form->fields);
  2116. $options = ['1' => 'option1', '2' => 'option2'];
  2117. $this->Form->radio('Test.test', $options);
  2118. $expected = ['Test.test'];
  2119. $this->assertEquals($expected, $this->Form->fields);
  2120. $this->Form->radio('Test.all', $options, [
  2121. 'disabled' => ['option1', 'option2']
  2122. ]);
  2123. $expected = ['Test.test', 'Test.all' => ''];
  2124. $this->assertEquals($expected, $this->Form->fields);
  2125. $this->Form->radio('Test.some', $options, [
  2126. 'disabled' => ['option1']
  2127. ]);
  2128. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2129. $this->assertEquals($expected, $this->Form->fields);
  2130. }
  2131. /**
  2132. * testFormSecuredAndDisabledNotAssoc method
  2133. *
  2134. * Test that when disabled is in a list based attribute array it works.
  2135. *
  2136. * @return void
  2137. */
  2138. public function testFormSecuredAndDisabledNotAssoc()
  2139. {
  2140. $this->Form->request->params['_Token'] = 'testKey';
  2141. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2142. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2143. $this->Form->text('Model.text', ['disabled']);
  2144. $this->Form->textarea('Model.textarea', ['disabled']);
  2145. $this->Form->password('Model.password', ['disabled']);
  2146. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2147. $expected = [
  2148. 'Model.radio' => ''
  2149. ];
  2150. $this->assertEquals($expected, $this->Form->fields);
  2151. }
  2152. /**
  2153. * testFormSecuredAndDisabled method
  2154. *
  2155. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2156. * hashing.
  2157. *
  2158. * @return void
  2159. */
  2160. public function testFormSecuredAndDisabled()
  2161. {
  2162. $this->Form->request->params['_Token'] = 'testKey';
  2163. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2164. $this->Form->text('Model.text', ['disabled' => true]);
  2165. $this->Form->password('Model.text', ['disabled' => true]);
  2166. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2167. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2168. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2169. $this->Form->year('Model.year', ['disabled' => true]);
  2170. $this->Form->month('Model.month', ['disabled' => true]);
  2171. $this->Form->day('Model.day', ['disabled' => true]);
  2172. $this->Form->hour('Model.hour', ['disabled' => true]);
  2173. $this->Form->minute('Model.minute', ['disabled' => true]);
  2174. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2175. $expected = [
  2176. 'Model.radio' => ''
  2177. ];
  2178. $this->assertEquals($expected, $this->Form->fields);
  2179. }
  2180. /**
  2181. * testDisableSecurityUsingForm method
  2182. *
  2183. * @return void
  2184. */
  2185. public function testDisableSecurityUsingForm()
  2186. {
  2187. $this->Form->request->params['_Token'] = [
  2188. 'disabledFields' => []
  2189. ];
  2190. $this->Form->create();
  2191. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2192. $this->Form->text('Addresses.title');
  2193. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2194. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2195. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2196. $result = $this->Form->fields;
  2197. $expected = [
  2198. 'Addresses.id' => '123456', 'Addresses.title',
  2199. ];
  2200. $this->assertEquals($expected, $result);
  2201. }
  2202. /**
  2203. * testUnlockFieldAddsToList method
  2204. *
  2205. * Test disableField.
  2206. *
  2207. * @return void
  2208. */
  2209. public function testUnlockFieldAddsToList()
  2210. {
  2211. $this->Form->request->params['_Token'] = [
  2212. 'unlockedFields' => []
  2213. ];
  2214. $this->Form->unlockField('Contact.name');
  2215. $this->Form->text('Contact.name');
  2216. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2217. $this->assertEquals([], $this->Form->fields);
  2218. }
  2219. /**
  2220. * testUnlockFieldRemovingFromFields method
  2221. *
  2222. * Test unlockField removing from fields array.
  2223. *
  2224. * @return void
  2225. */
  2226. public function testUnlockFieldRemovingFromFields()
  2227. {
  2228. $this->Form->request->params['_Token'] = [
  2229. 'unlockedFields' => []
  2230. ];
  2231. $this->Form->create($this->article);
  2232. $this->Form->hidden('Article.id', ['value' => 1]);
  2233. $this->Form->text('Article.title');
  2234. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2235. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2236. $this->Form->unlockField('Article.title');
  2237. $this->Form->unlockField('Article.id');
  2238. $this->assertEquals([], $this->Form->fields);
  2239. }
  2240. /**
  2241. * testResetUnlockFields method
  2242. *
  2243. * Test reset unlockFields, when create new form.
  2244. *
  2245. * @return void
  2246. */
  2247. public function testResetUnlockFields()
  2248. {
  2249. $this->Form->request->params['_Token'] = [
  2250. 'key' => 'testKey',
  2251. 'unlockedFields' => []
  2252. ];
  2253. $this->Form->unlockField('Contact.id');
  2254. $this->Form->create('Contact');
  2255. $this->Form->hidden('Contact.id', ['value' => 1]);
  2256. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2257. $this->Form->end();
  2258. $this->Form->create('Contact');
  2259. $this->Form->hidden('Contact.id', ['value' => 1]);
  2260. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2261. }
  2262. /**
  2263. * testSecuredFormUrlIgnoresHost method
  2264. *
  2265. * Test that only the path + query elements of a form's URL show up in their hash.
  2266. *
  2267. * @return void
  2268. */
  2269. public function testSecuredFormUrlIgnoresHost()
  2270. {
  2271. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2272. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2273. $this->Form->create($this->article, [
  2274. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2275. ]);
  2276. $result = $this->Form->secure();
  2277. $this->assertContains($expected, $result);
  2278. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2279. $result = $this->Form->secure();
  2280. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2281. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2282. $result = $this->Form->secure();
  2283. $this->assertContains($expected, $result, 'URL path + query should work.');
  2284. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2285. $result = $this->Form->secure();
  2286. $this->assertNotContains($expected, $result, 'URL is different');
  2287. }
  2288. /**
  2289. * testSecuredFormUrlHasHtmlAndIdentifier method
  2290. *
  2291. * Test that URL, HTML and identifier show up in their hashes.
  2292. *
  2293. * @return void
  2294. */
  2295. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2296. {
  2297. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2298. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2299. $this->Form->create($this->article, [
  2300. 'url' => [
  2301. 'controller' => 'articles',
  2302. 'action' => 'view',
  2303. '?' => [
  2304. 'page' => 1,
  2305. 'limit' => 10,
  2306. 'html' => '<>"',
  2307. ],
  2308. '#' => 'result',
  2309. ],
  2310. ]);
  2311. $result = $this->Form->secure();
  2312. $this->assertContains($expected, $result);
  2313. $this->Form->create($this->article, [
  2314. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2315. ]);
  2316. $result = $this->Form->secure();
  2317. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2318. $this->Form->create($this->article, [
  2319. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2320. ]);
  2321. $result = $this->Form->secure();
  2322. $this->assertContains($expected, $result, 'URL path + query should work.');
  2323. }
  2324. /**
  2325. * testErrorMessageDisplay method
  2326. *
  2327. * Test error message display.
  2328. *
  2329. * @return void
  2330. */
  2331. public function testErrorMessageDisplay()
  2332. {
  2333. $this->article['errors'] = [
  2334. 'Article' => [
  2335. 'title' => 'error message',
  2336. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2337. ]
  2338. ];
  2339. $this->Form->create($this->article);
  2340. $result = $this->Form->control('Article.title');
  2341. $expected = [
  2342. 'div' => ['class' => 'input text error'],
  2343. 'label' => ['for' => 'article-title'],
  2344. 'Title',
  2345. '/label',
  2346. 'input' => [
  2347. 'type' => 'text', 'name' => 'Article[title]',
  2348. 'id' => 'article-title', 'class' => 'form-error'
  2349. ],
  2350. ['div' => ['class' => 'error-message']],
  2351. 'error message',
  2352. '/div',
  2353. '/div'
  2354. ];
  2355. $this->assertHtml($expected, $result);
  2356. $result = $this->Form->control('Article.title', [
  2357. 'templates' => [
  2358. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2359. ]
  2360. ]);
  2361. $expected = [
  2362. 'div' => ['class' => 'input text error'],
  2363. 'label' => ['for' => 'article-title'],
  2364. 'Title',
  2365. '/label',
  2366. 'input' => [
  2367. 'type' => 'text', 'name' => 'Article[title]',
  2368. 'id' => 'article-title', 'class' => 'form-error'
  2369. ],
  2370. '/div'
  2371. ];
  2372. $this->assertHtml($expected, $result);
  2373. $result = $this->Form->control('Article.content');
  2374. $expected = [
  2375. 'div' => ['class' => 'input text error'],
  2376. 'label' => ['for' => 'article-content'],
  2377. 'Content',
  2378. '/label',
  2379. 'input' => [
  2380. 'type' => 'text', 'name' => 'Article[content]',
  2381. 'id' => 'article-content', 'class' => 'form-error'
  2382. ],
  2383. ['div' => ['class' => 'error-message']],
  2384. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2385. '/div',
  2386. '/div'
  2387. ];
  2388. $this->assertHtml($expected, $result);
  2389. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2390. $expected = [
  2391. 'div' => ['class' => 'input text error'],
  2392. 'label' => ['for' => 'article-content'],
  2393. 'Content',
  2394. '/label',
  2395. 'input' => [
  2396. 'type' => 'text', 'name' => 'Article[content]',
  2397. 'id' => 'article-content', 'class' => 'form-error'
  2398. ],
  2399. ['div' => ['class' => 'error-message']],
  2400. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2401. '/div',
  2402. '/div'
  2403. ];
  2404. $this->assertHtml($expected, $result);
  2405. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2406. $expected = [
  2407. 'div' => ['class' => 'input text error'],
  2408. 'label' => ['for' => 'article-content'],
  2409. 'Content',
  2410. '/label',
  2411. 'input' => [
  2412. 'type' => 'text', 'name' => 'Article[content]',
  2413. 'id' => 'article-content', 'class' => 'form-error'
  2414. ],
  2415. ['div' => ['class' => 'error-message']],
  2416. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2417. '/div',
  2418. '/div'
  2419. ];
  2420. $this->assertHtml($expected, $result);
  2421. }
  2422. /**
  2423. * testEmptyErrorValidation method
  2424. *
  2425. * Test validation errors, when validation message is an empty string.
  2426. *
  2427. * @return void
  2428. */
  2429. public function testEmptyErrorValidation()
  2430. {
  2431. $this->article['errors'] = [
  2432. 'Article' => ['title' => '']
  2433. ];
  2434. $this->Form->create($this->article);
  2435. $result = $this->Form->control('Article.title');
  2436. $expected = [
  2437. 'div' => ['class' => 'input text error'],
  2438. 'label' => ['for' => 'article-title'],
  2439. 'Title',
  2440. '/label',
  2441. 'input' => [
  2442. 'type' => 'text', 'name' => 'Article[title]',
  2443. 'id' => 'article-title', 'class' => 'form-error'
  2444. ],
  2445. ['div' => ['class' => 'error-message']],
  2446. [],
  2447. '/div',
  2448. '/div'
  2449. ];
  2450. $this->assertHtml($expected, $result);
  2451. }
  2452. /**
  2453. * testEmptyControlErrorValidation method
  2454. *
  2455. * Test validation errors, when calling control() overriding validation message by an empty string.
  2456. *
  2457. * @return void
  2458. */
  2459. public function testEmptyControlErrorValidation()
  2460. {
  2461. $this->article['errors'] = [
  2462. 'Article' => ['title' => 'error message']
  2463. ];
  2464. $this->Form->create($this->article);
  2465. $result = $this->Form->control('Article.title', ['error' => '']);
  2466. $expected = [
  2467. 'div' => ['class' => 'input text error'],
  2468. 'label' => ['for' => 'article-title'],
  2469. 'Title',
  2470. '/label',
  2471. 'input' => [
  2472. 'type' => 'text', 'name' => 'Article[title]',
  2473. 'id' => 'article-title', 'class' => 'form-error'
  2474. ],
  2475. ['div' => ['class' => 'error-message']],
  2476. [],
  2477. '/div',
  2478. '/div'
  2479. ];
  2480. $this->assertHtml($expected, $result);
  2481. }
  2482. /**
  2483. * testControlErrorMessage method
  2484. *
  2485. * Test validation errors, when calling control() overriding validation messages.
  2486. *
  2487. * @return void
  2488. */
  2489. public function testControlErrorMessage()
  2490. {
  2491. $this->article['errors'] = [
  2492. 'title' => ['error message']
  2493. ];
  2494. $this->Form->create($this->article);
  2495. $result = $this->Form->control('title', [
  2496. 'error' => 'Custom error!'
  2497. ]);
  2498. $expected = [
  2499. 'div' => ['class' => 'input text required error'],
  2500. 'label' => ['for' => 'title'],
  2501. 'Title',
  2502. '/label',
  2503. 'input' => [
  2504. 'type' => 'text', 'name' => 'title',
  2505. 'id' => 'title', 'class' => 'form-error',
  2506. 'required' => 'required',
  2507. ],
  2508. ['div' => ['class' => 'error-message']],
  2509. 'Custom error!',
  2510. '/div',
  2511. '/div'
  2512. ];
  2513. $this->assertHtml($expected, $result);
  2514. $result = $this->Form->control('title', [
  2515. 'error' => ['error message' => 'Custom error!']
  2516. ]);
  2517. $expected = [
  2518. 'div' => ['class' => 'input text required error'],
  2519. 'label' => ['for' => 'title'],
  2520. 'Title',
  2521. '/label',
  2522. 'input' => [
  2523. 'type' => 'text',
  2524. 'name' => 'title',
  2525. 'id' => 'title',
  2526. 'class' => 'form-error',
  2527. 'required' => 'required'
  2528. ],
  2529. ['div' => ['class' => 'error-message']],
  2530. 'Custom error!',
  2531. '/div',
  2532. '/div'
  2533. ];
  2534. $this->assertHtml($expected, $result);
  2535. }
  2536. /**
  2537. * testFormValidationAssociated method
  2538. *
  2539. * Tests displaying errors for nested entities.
  2540. *
  2541. * @return void
  2542. */
  2543. public function testFormValidationAssociated()
  2544. {
  2545. $nested = new Entity(['foo' => 'bar']);
  2546. $nested->errors('foo', ['not a valid bar']);
  2547. $entity = new Entity(['nested' => $nested]);
  2548. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2549. $result = $this->Form->error('nested.foo');
  2550. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2551. }
  2552. /**
  2553. * testFormValidationAssociatedSecondLevel method
  2554. *
  2555. * Test form error display with associated model.
  2556. *
  2557. * @return void
  2558. */
  2559. public function testFormValidationAssociatedSecondLevel()
  2560. {
  2561. $inner = new Entity(['bar' => 'baz']);
  2562. $nested = new Entity(['foo' => $inner]);
  2563. $entity = new Entity(['nested' => $nested]);
  2564. $inner->errors('bar', ['not a valid one']);
  2565. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2566. $result = $this->Form->error('nested.foo.bar');
  2567. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2568. }
  2569. /**
  2570. * testFormValidationMultiRecord method
  2571. *
  2572. * Test form error display with multiple records.
  2573. *
  2574. * @return void
  2575. */
  2576. public function testFormValidationMultiRecord()
  2577. {
  2578. $one = new Entity();
  2579. $two = new Entity();
  2580. TableRegistry::get('Contacts', [
  2581. 'className' => __NAMESPACE__ . '\ContactsTable'
  2582. ]);
  2583. $one->set('email', '');
  2584. $one->errors('email', ['invalid email']);
  2585. $two->set('name', '');
  2586. $two->errors('name', ['This is wrong']);
  2587. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2588. $result = $this->Form->control('0.email');
  2589. $expected = [
  2590. 'div' => ['class' => 'input email error'],
  2591. 'label' => ['for' => '0-email'],
  2592. 'Email',
  2593. '/label',
  2594. 'input' => [
  2595. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2596. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2597. ],
  2598. ['div' => ['class' => 'error-message']],
  2599. 'invalid email',
  2600. '/div',
  2601. '/div'
  2602. ];
  2603. $this->assertHtml($expected, $result);
  2604. $result = $this->Form->control('1.name');
  2605. $expected = [
  2606. 'div' => ['class' => 'input text error'],
  2607. 'label' => ['for' => '1-name'],
  2608. 'Name',
  2609. '/label',
  2610. 'input' => [
  2611. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2612. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2613. ],
  2614. ['div' => ['class' => 'error-message']],
  2615. 'This is wrong',
  2616. '/div',
  2617. '/div'
  2618. ];
  2619. $this->assertHtml($expected, $result);
  2620. }
  2621. /**
  2622. * testControl method
  2623. *
  2624. * Test various incarnations of control().
  2625. *
  2626. * @return void
  2627. */
  2628. public function testControl()
  2629. {
  2630. TableRegistry::get('ValidateUsers', [
  2631. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2632. ]);
  2633. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2634. $result = $this->Form->control('ValidateUsers.balance');
  2635. $expected = [
  2636. 'div' => ['class'],
  2637. 'label' => ['for'],
  2638. 'Balance',
  2639. '/label',
  2640. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2641. '/div',
  2642. ];
  2643. $this->assertHtml($expected, $result);
  2644. $result = $this->Form->control('ValidateUser.cost_decimal');
  2645. $expected = [
  2646. 'div' => ['class'],
  2647. 'label' => ['for'],
  2648. 'Cost Decimal',
  2649. '/label',
  2650. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2651. '/div',
  2652. ];
  2653. $this->assertHtml($expected, $result);
  2654. $result = $this->Form->control('ValidateUser.null_decimal');
  2655. $expected = [
  2656. 'div' => ['class'],
  2657. 'label' => ['for'],
  2658. 'Null Decimal',
  2659. '/label',
  2660. 'input' => ['name', 'type' => 'number', 'id'],
  2661. '/div',
  2662. ];
  2663. $this->assertHtml($expected, $result);
  2664. }
  2665. /**
  2666. * testControlCustomization method
  2667. *
  2668. * Tests the input method and passing custom options.
  2669. *
  2670. * @return void
  2671. */
  2672. public function testControlCustomization()
  2673. {
  2674. TableRegistry::get('Contacts', [
  2675. 'className' => __NAMESPACE__ . '\ContactsTable'
  2676. ]);
  2677. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2678. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2679. $expected = [
  2680. 'div' => ['class' => 'input email'],
  2681. 'label' => ['for' => 'custom'],
  2682. 'Email',
  2683. '/label',
  2684. ['input' => [
  2685. 'type' => 'email', 'name' => 'Contact[email]',
  2686. 'id' => 'custom', 'maxlength' => 255
  2687. ]],
  2688. '/div'
  2689. ];
  2690. $this->assertHtml($expected, $result);
  2691. $result = $this->Form->control('Contact.email', [
  2692. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2693. ]);
  2694. $expected = [
  2695. '<div',
  2696. 'label' => ['for' => 'contact-email'],
  2697. 'Email',
  2698. '/label',
  2699. ['input' => [
  2700. 'type' => 'email', 'name' => 'Contact[email]',
  2701. 'id' => 'contact-email', 'maxlength' => 255
  2702. ]],
  2703. '/div'
  2704. ];
  2705. $this->assertHtml($expected, $result);
  2706. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2707. $expected = [
  2708. 'div' => ['class' => 'input text'],
  2709. 'label' => ['for' => 'contact-email'],
  2710. 'Email',
  2711. '/label',
  2712. ['input' => [
  2713. 'type' => 'text', 'name' => 'Contact[email]',
  2714. 'id' => 'contact-email', 'maxlength' => '255'
  2715. ]],
  2716. '/div'
  2717. ];
  2718. $this->assertHtml($expected, $result);
  2719. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2720. $expected = [
  2721. 'div' => ['class' => 'input text'],
  2722. 'label' => ['for' => 'contact-5-email'],
  2723. 'Email',
  2724. '/label',
  2725. ['input' => [
  2726. 'type' => 'text', 'name' => 'Contact[5][email]',
  2727. 'id' => 'contact-5-email', 'maxlength' => '255'
  2728. ]],
  2729. '/div'
  2730. ];
  2731. $this->assertHtml($expected, $result);
  2732. $result = $this->Form->control('Contact.password');
  2733. $expected = [
  2734. 'div' => ['class' => 'input password'],
  2735. 'label' => ['for' => 'contact-password'],
  2736. 'Password',
  2737. '/label',
  2738. ['input' => [
  2739. 'type' => 'password', 'name' => 'Contact[password]',
  2740. 'id' => 'contact-password'
  2741. ]],
  2742. '/div'
  2743. ];
  2744. $this->assertHtml($expected, $result);
  2745. $result = $this->Form->control('Contact.email', [
  2746. 'type' => 'file', 'class' => 'textbox'
  2747. ]);
  2748. $expected = [
  2749. 'div' => ['class' => 'input file'],
  2750. 'label' => ['for' => 'contact-email'],
  2751. 'Email',
  2752. '/label',
  2753. ['input' => [
  2754. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2755. 'id' => 'contact-email'
  2756. ]],
  2757. '/div'
  2758. ];
  2759. $this->assertHtml($expected, $result);
  2760. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2761. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2762. $result = $this->Form->control('phone');
  2763. $expected = [
  2764. 'div' => ['class' => 'input tel'],
  2765. 'label' => ['for' => 'phone'],
  2766. 'Phone',
  2767. '/label',
  2768. ['input' => [
  2769. 'type' => 'tel', 'name' => 'phone',
  2770. 'value' => 'Hello &amp; World &gt; weird chars',
  2771. 'id' => 'phone', 'maxlength' => 255
  2772. ]],
  2773. '/div'
  2774. ];
  2775. $this->assertHtml($expected, $result);
  2776. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2777. $this->Form->create();
  2778. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2779. $expected = [
  2780. 'div' => ['class' => 'input text'],
  2781. 'label' => ['for' => 'myId'],
  2782. 'Field',
  2783. '/label',
  2784. 'input' => [
  2785. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2786. 'value' => 'My value', 'id' => 'myId'
  2787. ],
  2788. '/div'
  2789. ];
  2790. $this->assertHtml($expected, $result);
  2791. $this->Form->request->data = [];
  2792. $entity->errors('field', 'Badness!');
  2793. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2794. $result = $this->Form->control('field');
  2795. $expected = [
  2796. 'div' => ['class' => 'input text error'],
  2797. 'label' => ['for' => 'field'],
  2798. 'Field',
  2799. '/label',
  2800. 'input' => [
  2801. 'type' => 'text', 'name' => 'field',
  2802. 'id' => 'field', 'class' => 'form-error'
  2803. ],
  2804. ['div' => ['class' => 'error-message']],
  2805. 'Badness!',
  2806. '/div',
  2807. '/div'
  2808. ];
  2809. $this->assertHtml($expected, $result);
  2810. $result = $this->Form->control('field', [
  2811. 'templates' => [
  2812. 'inputContainerError' => '{{content}}{{error}}',
  2813. 'error' => '<span class="error-message">{{content}}</span>'
  2814. ]
  2815. ]);
  2816. $expected = [
  2817. 'label' => ['for' => 'field'],
  2818. 'Field',
  2819. '/label',
  2820. 'input' => [
  2821. 'type' => 'text', 'name' => 'field',
  2822. 'id' => 'field', 'class' => 'form-error'
  2823. ],
  2824. ['span' => ['class' => 'error-message']],
  2825. 'Badness!',
  2826. '/span'
  2827. ];
  2828. $this->assertHtml($expected, $result);
  2829. $entity->errors('field', ['minLength'], true);
  2830. $result = $this->Form->control('field', [
  2831. 'error' => [
  2832. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2833. 'maxLength' => 'login too large'
  2834. ]
  2835. ]);
  2836. $expected = [
  2837. 'div' => ['class' => 'input text error'],
  2838. 'label' => ['for' => 'field'],
  2839. 'Field',
  2840. '/label',
  2841. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2842. ['div' => ['class' => 'error-message']],
  2843. 'Le login doit contenir au moins 2 caractères',
  2844. '/div',
  2845. '/div'
  2846. ];
  2847. $this->assertHtml($expected, $result);
  2848. $entity->errors('field', ['maxLength'], true);
  2849. $result = $this->Form->control('field', [
  2850. 'error' => [
  2851. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2852. 'maxLength' => 'login too large',
  2853. ]
  2854. ]);
  2855. $expected = [
  2856. 'div' => ['class' => 'input text error'],
  2857. 'label' => ['for' => 'field'],
  2858. 'Field',
  2859. '/label',
  2860. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2861. ['div' => ['class' => 'error-message']],
  2862. 'login too large',
  2863. '/div',
  2864. '/div'
  2865. ];
  2866. $this->assertHtml($expected, $result);
  2867. }
  2868. /**
  2869. * testControlWithTemplateFile method
  2870. *
  2871. * Test that control() accepts a template file.
  2872. *
  2873. * @return void
  2874. */
  2875. public function testControlWithTemplateFile()
  2876. {
  2877. $result = $this->Form->control('field', [
  2878. 'templates' => 'htmlhelper_tags'
  2879. ]);
  2880. $expected = [
  2881. 'label' => ['for' => 'field'],
  2882. 'Field',
  2883. '/label',
  2884. 'input' => [
  2885. 'type' => 'text', 'name' => 'field',
  2886. 'id' => 'field'
  2887. ],
  2888. ];
  2889. $this->assertHtml($expected, $result);
  2890. }
  2891. /**
  2892. * testNestedControlsEndWithBrackets method
  2893. *
  2894. * Test that nested inputs end with brackets.
  2895. *
  2896. * @return void
  2897. */
  2898. public function testNestedControlsEndWithBrackets()
  2899. {
  2900. $result = $this->Form->text('nested.text[]');
  2901. $expected = [
  2902. 'input' => [
  2903. 'type' => 'text', 'name' => 'nested[text][]'
  2904. ],
  2905. ];
  2906. $this->assertHtml($expected, $result);
  2907. $result = $this->Form->file('nested.file[]');
  2908. $expected = [
  2909. 'input' => [
  2910. 'type' => 'file', 'name' => 'nested[file][]'
  2911. ],
  2912. ];
  2913. $this->assertHtml($expected, $result);
  2914. }
  2915. /**
  2916. * testCreateIdPrefix method
  2917. *
  2918. * Test id prefix.
  2919. *
  2920. * @return void
  2921. */
  2922. public function testCreateIdPrefix()
  2923. {
  2924. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2925. $result = $this->Form->control('field');
  2926. $expected = [
  2927. 'div' => ['class' => 'input text'],
  2928. 'label' => ['for' => 'prefix-field'],
  2929. 'Field',
  2930. '/label',
  2931. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2932. '/div'
  2933. ];
  2934. $this->assertHtml($expected, $result);
  2935. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2936. $expected = [
  2937. 'div' => ['class' => 'input text'],
  2938. 'label' => ['for' => 'custom-id'],
  2939. 'Field',
  2940. '/label',
  2941. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2942. '/div'
  2943. ];
  2944. $this->assertHtml($expected, $result);
  2945. $result = $this->Form->radio('Model.field', ['option A']);
  2946. $expected = [
  2947. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2948. 'label' => ['for' => 'prefix-model-field-0'],
  2949. ['input' => [
  2950. 'type' => 'radio',
  2951. 'name' => 'Model[field]',
  2952. 'value' => '0',
  2953. 'id' => 'prefix-model-field-0'
  2954. ]],
  2955. 'option A',
  2956. '/label'
  2957. ];
  2958. $this->assertHtml($expected, $result);
  2959. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2960. $expected = [
  2961. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2962. 'label' => ['for' => 'prefix-model-field-0'],
  2963. ['input' => [
  2964. 'type' => 'radio',
  2965. 'name' => 'Model[field]',
  2966. 'value' => '0',
  2967. 'id' => 'prefix-model-field-0'
  2968. ]],
  2969. 'option A',
  2970. '/label'
  2971. ];
  2972. $this->assertHtml($expected, $result);
  2973. $result = $this->Form->select(
  2974. 'Model.multi_field',
  2975. ['first'],
  2976. ['multiple' => 'checkbox']
  2977. );
  2978. $expected = [
  2979. 'input' => [
  2980. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2981. ],
  2982. ['div' => ['class' => 'checkbox']],
  2983. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2984. ['input' => [
  2985. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2986. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2987. ]],
  2988. 'first',
  2989. '/label',
  2990. '/div',
  2991. ];
  2992. $this->assertHtml($expected, $result);
  2993. $this->Form->end();
  2994. $result = $this->Form->control('field');
  2995. $expected = [
  2996. 'div' => ['class' => 'input text'],
  2997. 'label' => ['for' => 'field'],
  2998. 'Field',
  2999. '/label',
  3000. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3001. '/div'
  3002. ];
  3003. $this->assertHtml($expected, $result);
  3004. }
  3005. /**
  3006. * testControlZero method
  3007. *
  3008. * Test that inputs with 0 can be created.
  3009. *
  3010. * @return void
  3011. */
  3012. public function testControlZero()
  3013. {
  3014. TableRegistry::get('Contacts', [
  3015. 'className' => __NAMESPACE__ . '\ContactsTable'
  3016. ]);
  3017. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3018. $result = $this->Form->control('0');
  3019. $expected = [
  3020. 'div' => ['class' => 'input text'],
  3021. 'label' => ['for' => '0'], '/label',
  3022. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3023. '/div'
  3024. ];
  3025. $this->assertHtml($expected, $result);
  3026. }
  3027. /**
  3028. * testControlCheckbox method
  3029. *
  3030. * Test control() with checkbox creation.
  3031. *
  3032. * @return void
  3033. */
  3034. public function testControlCheckbox()
  3035. {
  3036. $articles = TableRegistry::get('Articles');
  3037. $articles->schema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3038. $article = $articles->newEntity();
  3039. $this->Form->create($article);
  3040. $result = $this->Form->control('Articles.active');
  3041. $expected = [
  3042. 'div' => ['class' => 'input checkbox'],
  3043. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3044. 'label' => ['for' => 'articles-active'],
  3045. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3046. 'Active',
  3047. '/label',
  3048. '/div'
  3049. ];
  3050. $this->assertHtml($expected, $result);
  3051. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3052. $expected = [
  3053. 'div' => ['class' => 'input checkbox'],
  3054. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3055. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3056. '/div'
  3057. ];
  3058. $this->assertHtml($expected, $result);
  3059. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3060. $expected = [
  3061. 'div' => ['class' => 'input checkbox'],
  3062. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3063. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3064. '/div'
  3065. ];
  3066. $this->assertHtml($expected, $result);
  3067. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3068. $expected = [
  3069. 'div' => ['class' => 'input checkbox'],
  3070. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3071. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3072. '/div'
  3073. ];
  3074. $this->assertHtml($expected, $result);
  3075. $result = $this->Form->control('Articles.disabled', [
  3076. 'label' => 'Disabled',
  3077. 'type' => 'checkbox',
  3078. 'data-foo' => 'disabled'
  3079. ]);
  3080. $expected = [
  3081. 'div' => ['class' => 'input checkbox'],
  3082. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3083. 'label' => ['for' => 'articles-disabled'],
  3084. ['input' => [
  3085. 'type' => 'checkbox',
  3086. 'name' => 'Articles[disabled]',
  3087. 'value' => '1',
  3088. 'id' => 'articles-disabled',
  3089. 'data-foo' => 'disabled'
  3090. ]],
  3091. 'Disabled',
  3092. '/label',
  3093. '/div'
  3094. ];
  3095. $this->assertHtml($expected, $result);
  3096. $result = $this->Form->control('Articles.confirm', [
  3097. 'label' => 'Confirm <b>me</b>!',
  3098. 'type' => 'checkbox',
  3099. 'escape' => false
  3100. ]);
  3101. $expected = [
  3102. 'div' => ['class' => 'input checkbox'],
  3103. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3104. 'label' => ['for' => 'articles-confirm'],
  3105. ['input' => [
  3106. 'type' => 'checkbox',
  3107. 'name' => 'Articles[confirm]',
  3108. 'value' => '1',
  3109. 'id' => 'articles-confirm',
  3110. ]],
  3111. 'Confirm <b>me</b>!',
  3112. '/label',
  3113. '/div'
  3114. ];
  3115. $this->assertHtml($expected, $result);
  3116. }
  3117. /**
  3118. * testControlHidden method
  3119. *
  3120. * Test that control() does not create wrapping div and label tag for hidden fields.
  3121. *
  3122. * @return void
  3123. */
  3124. public function testControlHidden()
  3125. {
  3126. TableRegistry::get('ValidateUsers', [
  3127. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3128. ]);
  3129. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3130. $result = $this->Form->control('ValidateUser.id');
  3131. $expected = [
  3132. 'input' => ['name', 'type' => 'hidden', 'id']
  3133. ];
  3134. $this->assertHtml($expected, $result);
  3135. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3136. $expected = [
  3137. 'input' => ['name', 'type' => 'hidden', 'id']
  3138. ];
  3139. $this->assertHtml($expected, $result);
  3140. }
  3141. /**
  3142. * testControlDatetime method
  3143. *
  3144. * Test form->control() with datetime.
  3145. *
  3146. * @return void
  3147. */
  3148. public function testControlDatetime()
  3149. {
  3150. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3151. ->setMethods(['datetime'])
  3152. ->setConstructorArgs([new View()])
  3153. ->getMock();
  3154. $this->Form->expects($this->once())->method('datetime')
  3155. ->with('prueba', [
  3156. 'type' => 'datetime',
  3157. 'timeFormat' => 24,
  3158. 'minYear' => 2008,
  3159. 'maxYear' => 2011,
  3160. 'interval' => 15,
  3161. 'options' => null,
  3162. 'empty' => false,
  3163. 'id' => 'prueba',
  3164. 'required' => false,
  3165. 'templateVars' => []
  3166. ])
  3167. ->will($this->returnValue('This is it!'));
  3168. $result = $this->Form->control('prueba', [
  3169. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3170. 'maxYear' => 2011, 'interval' => 15
  3171. ]);
  3172. $expected = [
  3173. 'div' => ['class' => 'input datetime'],
  3174. '<label',
  3175. 'Prueba',
  3176. '/label',
  3177. 'This is it!',
  3178. '/div'
  3179. ];
  3180. $this->assertHtml($expected, $result);
  3181. }
  3182. /**
  3183. * testControlDatetimeIdPrefix method
  3184. *
  3185. * Test form->control() with datetime with id prefix.
  3186. *
  3187. * @return void
  3188. */
  3189. public function testControlDatetimeIdPrefix()
  3190. {
  3191. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3192. ->setMethods(['datetime'])
  3193. ->setConstructorArgs([new View()])
  3194. ->getMock();
  3195. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3196. $this->Form->expects($this->once())->method('datetime')
  3197. ->with('prueba', [
  3198. 'type' => 'datetime',
  3199. 'timeFormat' => 24,
  3200. 'minYear' => 2008,
  3201. 'maxYear' => 2011,
  3202. 'interval' => 15,
  3203. 'options' => null,
  3204. 'empty' => false,
  3205. 'id' => 'prefix-prueba',
  3206. 'required' => false,
  3207. 'templateVars' => []
  3208. ])
  3209. ->will($this->returnValue('This is it!'));
  3210. $result = $this->Form->control('prueba', [
  3211. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3212. 'maxYear' => 2011, 'interval' => 15
  3213. ]);
  3214. $expected = [
  3215. 'div' => ['class' => 'input datetime'],
  3216. '<label',
  3217. 'Prueba',
  3218. '/label',
  3219. 'This is it!',
  3220. '/div'
  3221. ];
  3222. $this->assertHtml($expected, $result);
  3223. }
  3224. /**
  3225. * testControlCheckboxWithDisabledElements method
  3226. *
  3227. * Test generating checkboxes with disabled elements.
  3228. *
  3229. * @return void
  3230. */
  3231. public function testControlCheckboxWithDisabledElements()
  3232. {
  3233. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3234. $result = $this->Form->control('Contact.multiple', [
  3235. 'multiple' => 'checkbox',
  3236. 'disabled' => 'disabled',
  3237. 'options' => $options
  3238. ]);
  3239. $expected = [
  3240. ['div' => ['class' => 'input select']],
  3241. ['label' => ['for' => "contact-multiple"]],
  3242. 'Multiple',
  3243. '/label',
  3244. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  3245. ['div' => ['class' => 'checkbox']],
  3246. ['label' => ['for' => "contact-multiple-1"]],
  3247. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  3248. 'One',
  3249. '/label',
  3250. '/div',
  3251. ['div' => ['class' => 'checkbox']],
  3252. ['label' => ['for' => "contact-multiple-2"]],
  3253. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  3254. 'Two',
  3255. '/label',
  3256. '/div',
  3257. ['div' => ['class' => 'checkbox']],
  3258. ['label' => ['for' => "contact-multiple-3"]],
  3259. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  3260. 'Three',
  3261. '/label',
  3262. '/div',
  3263. '/div'
  3264. ];
  3265. $this->assertHtml($expected, $result);
  3266. // make sure 50 does only disable 50, and not 50f5c0cf
  3267. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3268. $disabled = [50];
  3269. $expected = [
  3270. ['div' => ['class' => 'input select']],
  3271. ['label' => ['for' => "contact-multiple"]],
  3272. 'Multiple',
  3273. '/label',
  3274. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  3275. ['div' => ['class' => 'checkbox']],
  3276. ['label' => ['for' => "contact-multiple-50"]],
  3277. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  3278. 'Fifty',
  3279. '/label',
  3280. '/div',
  3281. ['div' => ['class' => 'checkbox']],
  3282. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  3283. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  3284. 'Stringy',
  3285. '/label',
  3286. '/div',
  3287. '/div'
  3288. ];
  3289. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3290. $this->assertHtml($expected, $result);
  3291. }
  3292. /**
  3293. * testControlWithLeadingInteger method
  3294. *
  3295. * Test input name with leading integer, ensure attributes are generated correctly.
  3296. *
  3297. * @return void
  3298. */
  3299. public function testControlWithLeadingInteger()
  3300. {
  3301. $result = $this->Form->text('0.Node.title');
  3302. $expected = [
  3303. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3304. ];
  3305. $this->assertHtml($expected, $result);
  3306. }
  3307. /**
  3308. * testControlSelectType method
  3309. *
  3310. * Test form->control() with select type inputs.
  3311. *
  3312. * @return void
  3313. */
  3314. public function testControlSelectType()
  3315. {
  3316. $result = $this->Form->control(
  3317. 'email',
  3318. [
  3319. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3320. );
  3321. $expected = [
  3322. 'div' => ['class' => 'input select'],
  3323. 'label' => ['for' => 'email'],
  3324. 'Email',
  3325. '/label',
  3326. ['select' => ['name' => 'email', 'id' => 'email']],
  3327. ['option' => ['value' => '']],
  3328. '/option',
  3329. ['option' => ['value' => 'è']],
  3330. 'Firést',
  3331. '/option',
  3332. ['option' => ['value' => 'é']],
  3333. 'Secoènd',
  3334. '/option',
  3335. '/select',
  3336. '/div'
  3337. ];
  3338. $this->assertHtml($expected, $result);
  3339. $result = $this->Form->control(
  3340. 'email',
  3341. [
  3342. 'options' => ['First', 'Second'], 'empty' => true]
  3343. );
  3344. $expected = [
  3345. 'div' => ['class' => 'input select'],
  3346. 'label' => ['for' => 'email'],
  3347. 'Email',
  3348. '/label',
  3349. ['select' => ['name' => 'email', 'id' => 'email']],
  3350. ['option' => ['value' => '']],
  3351. '/option',
  3352. ['option' => ['value' => '0']],
  3353. 'First',
  3354. '/option',
  3355. ['option' => ['value' => '1']],
  3356. 'Second',
  3357. '/option',
  3358. '/select',
  3359. '/div'
  3360. ];
  3361. $this->assertHtml($expected, $result);
  3362. $result = $this->Form->control('email', [
  3363. 'type' => 'select',
  3364. 'options' => new \ArrayObject(['First', 'Second']),
  3365. 'empty' => true
  3366. ]);
  3367. $this->assertHtml($expected, $result);
  3368. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3369. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3370. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3371. $expected = [
  3372. 'div' => ['class' => 'input select'],
  3373. 'label' => ['for' => 'model-user-id'],
  3374. 'User',
  3375. '/label',
  3376. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3377. ['option' => ['value' => '']],
  3378. '/option',
  3379. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3380. 'good',
  3381. '/option',
  3382. ['option' => ['value' => 'other']],
  3383. 'bad',
  3384. '/option',
  3385. '/select',
  3386. '/div'
  3387. ];
  3388. $this->assertHtml($expected, $result);
  3389. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3390. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3391. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3392. $expected = [
  3393. 'div' => ['class' => 'input select'],
  3394. 'label' => ['for' => 'thing-user-id'],
  3395. 'User',
  3396. '/label',
  3397. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3398. ['option' => ['value' => '']],
  3399. 'Some Empty',
  3400. '/option',
  3401. ['option' => ['value' => 'value']],
  3402. 'good',
  3403. '/option',
  3404. ['option' => ['value' => 'other']],
  3405. 'bad',
  3406. '/option',
  3407. '/select',
  3408. '/div'
  3409. ];
  3410. $this->assertHtml($expected, $result);
  3411. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3412. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3413. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3414. $expected = [
  3415. 'div' => ['class' => 'input select'],
  3416. 'label' => ['for' => 'thing-user-id'],
  3417. 'User',
  3418. '/label',
  3419. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3420. ['option' => ['value' => '']],
  3421. 'Some Empty',
  3422. '/option',
  3423. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3424. 'good',
  3425. '/option',
  3426. ['option' => ['value' => 'other']],
  3427. 'bad',
  3428. '/option',
  3429. '/select',
  3430. '/div'
  3431. ];
  3432. $this->assertHtml($expected, $result);
  3433. $this->Form->data = [];
  3434. $result = $this->Form->control('Publisher.id', [
  3435. 'label' => 'Publisher',
  3436. 'type' => 'select',
  3437. 'multiple' => 'checkbox',
  3438. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3439. ]);
  3440. $expected = [
  3441. ['div' => ['class' => 'input select']],
  3442. ['label' => ['for' => 'publisher-id']],
  3443. 'Publisher',
  3444. '/label',
  3445. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3446. ['div' => ['class' => 'checkbox']],
  3447. ['label' => ['for' => 'publisher-id-value-1']],
  3448. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3449. 'Label 1',
  3450. '/label',
  3451. '/div',
  3452. ['div' => ['class' => 'checkbox']],
  3453. ['label' => ['for' => 'publisher-id-value-2']],
  3454. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3455. 'Label 2',
  3456. '/label',
  3457. '/div',
  3458. '/div'
  3459. ];
  3460. $this->assertHtml($expected, $result);
  3461. }
  3462. /**
  3463. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3464. *
  3465. * Test that control() and a non standard primary key makes a hidden input by default.
  3466. *
  3467. * @return void
  3468. */
  3469. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3470. {
  3471. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3472. $this->Form->create($this->article);
  3473. $result = $this->Form->control('title');
  3474. $expected = [
  3475. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3476. ];
  3477. $this->assertHtml($expected, $result);
  3478. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3479. $this->Form->create($this->article);
  3480. $result = $this->Form->control('title');
  3481. $expected = [
  3482. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3483. ];
  3484. $this->assertHtml($expected, $result);
  3485. $result = $this->Form->control('body');
  3486. $expected = [
  3487. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3488. ];
  3489. $this->assertHtml($expected, $result);
  3490. }
  3491. /**
  3492. * testControlOverridingMagicSelectType method
  3493. *
  3494. * Test that overriding the magic select type widget is possible.
  3495. *
  3496. * @return void
  3497. */
  3498. public function testControlOverridingMagicSelectType()
  3499. {
  3500. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3501. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3502. $expected = [
  3503. 'div' => ['class' => 'input text'],
  3504. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3505. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3506. '/div'
  3507. ];
  3508. $this->assertHtml($expected, $result);
  3509. //Check that magic types still work for plural/singular vars
  3510. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3511. $result = $this->Form->control('Model.type');
  3512. $expected = [
  3513. 'div' => ['class' => 'input select'],
  3514. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3515. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3516. ['option' => ['value' => 'value']], 'good', '/option',
  3517. ['option' => ['value' => 'other']], 'bad', '/option',
  3518. '/select',
  3519. '/div'
  3520. ];
  3521. $this->assertHtml($expected, $result);
  3522. }
  3523. /**
  3524. * testControlMagicTypeDoesNotOverride method
  3525. *
  3526. * Test that inferred types do not override developer input.
  3527. *
  3528. * @return void
  3529. */
  3530. public function testControlMagicTypeDoesNotOverride()
  3531. {
  3532. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3533. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3534. $expected = [
  3535. 'div' => ['class' => 'input checkbox'],
  3536. ['input' => [
  3537. 'type' => 'hidden',
  3538. 'name' => 'Model[user]',
  3539. 'value' => 0,
  3540. ]],
  3541. 'label' => ['for' => 'model-user'],
  3542. ['input' => [
  3543. 'name' => 'Model[user]',
  3544. 'type' => 'checkbox',
  3545. 'id' => 'model-user',
  3546. 'value' => 1
  3547. ]],
  3548. 'User',
  3549. '/label',
  3550. '/div'
  3551. ];
  3552. $this->assertHtml($expected, $result);
  3553. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3554. $options = [
  3555. 1 => 'blue',
  3556. 2 => 'red'
  3557. ];
  3558. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3559. $expected = [
  3560. 'div' => ['class' => 'input select'],
  3561. 'label' => ['for' => 'tags-ids'],
  3562. 'Tags',
  3563. '/label',
  3564. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3565. ['div' => ['class' => 'checkbox']],
  3566. ['label' => ['for' => 'tags-ids-1']],
  3567. ['input' => [
  3568. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3569. 'value' => '1', 'name' => 'tags[_ids][]'
  3570. ]],
  3571. 'blue',
  3572. '/label',
  3573. '/div',
  3574. ['div' => ['class' => 'checkbox']],
  3575. ['label' => ['for' => 'tags-ids-2']],
  3576. ['input' => [
  3577. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3578. 'value' => '2', 'name' => 'tags[_ids][]'
  3579. ]],
  3580. 'red',
  3581. '/label',
  3582. '/div',
  3583. '/div'
  3584. ];
  3585. $this->assertHtml($expected, $result);
  3586. }
  3587. /**
  3588. * testControlMagicSelectForTypeNumber method
  3589. *
  3590. * Test that magic control() selects are created for type=number.
  3591. *
  3592. * @return void
  3593. */
  3594. public function testControlMagicSelectForTypeNumber()
  3595. {
  3596. TableRegistry::get('ValidateUsers', [
  3597. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3598. ]);
  3599. $entity = new Entity(['balance' => 1]);
  3600. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3601. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3602. $result = $this->Form->control('balance');
  3603. $expected = [
  3604. 'div' => ['class' => 'input select'],
  3605. 'label' => ['for' => 'balance'],
  3606. 'Balance',
  3607. '/label',
  3608. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3609. ['option' => ['value' => '0']],
  3610. 'nothing',
  3611. '/option',
  3612. ['option' => ['value' => '1', 'selected' => 'selected']],
  3613. 'some',
  3614. '/option',
  3615. ['option' => ['value' => '100']],
  3616. 'a lot',
  3617. '/option',
  3618. '/select',
  3619. '/div'
  3620. ];
  3621. $this->assertHtml($expected, $result);
  3622. }
  3623. /**
  3624. * testInvalidControlTypeOption method
  3625. *
  3626. * Test invalid 'input' type option to control() function.
  3627. *
  3628. * @expectedException \RuntimeException
  3629. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3630. * @return void
  3631. */
  3632. public function testInvalidControlTypeOption()
  3633. {
  3634. $this->Form->control('text', ['type' => 'input']);
  3635. }
  3636. /**
  3637. * testControlMagicSelectChangeToRadio method
  3638. *
  3639. * Test that magic control() selects can easily be converted into radio types without error.
  3640. *
  3641. * @return void
  3642. */
  3643. public function testControlMagicSelectChangeToRadio()
  3644. {
  3645. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3646. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3647. $this->assertContains('input type="radio"', $result);
  3648. }
  3649. /**
  3650. * testFormControlSubmit method
  3651. *
  3652. * Test correct results for form::control() and type submit.
  3653. *
  3654. * @return void
  3655. */
  3656. public function testFormControlSubmit()
  3657. {
  3658. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3659. $expected = [
  3660. 'div' => ['class' => 'submit'],
  3661. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3662. '/div'
  3663. ];
  3664. $this->assertHtml($expected, $result);
  3665. }
  3666. /**
  3667. * testFormControls method
  3668. *
  3669. * Test correct results from Form::controls().
  3670. *
  3671. * @return void
  3672. */
  3673. public function testFormControlsLegendFieldset()
  3674. {
  3675. $this->Form->create($this->article);
  3676. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3677. $expected = [
  3678. '<fieldset',
  3679. '<legend',
  3680. 'The Legend',
  3681. '/legend',
  3682. '*/fieldset',
  3683. ];
  3684. $this->assertHtml($expected, $result);
  3685. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3686. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3687. $this->assertContains('<fieldset>', $result);
  3688. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3689. $this->assertNotContains('<legend>', $result);
  3690. $this->assertNotContains('<fieldset>', $result);
  3691. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3692. $this->assertNotContains('<legend>', $result);
  3693. $this->assertNotContains('<fieldset>', $result);
  3694. $this->Form->create($this->article);
  3695. $this->Form->request->params['prefix'] = 'admin';
  3696. $this->Form->request->params['action'] = 'admin_edit';
  3697. $this->Form->request->params['controller'] = 'articles';
  3698. $result = $this->Form->allControls();
  3699. $expected = [
  3700. '<fieldset',
  3701. '<legend',
  3702. 'New Article',
  3703. '/legend',
  3704. '*/fieldset',
  3705. ];
  3706. $this->assertHtml($expected, $result);
  3707. $this->Form->create($this->article);
  3708. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3709. $expected = [
  3710. '<fieldset',
  3711. '<legend',
  3712. 'The Legend',
  3713. '/legend',
  3714. '*/fieldset',
  3715. ];
  3716. $this->assertHtml($expected, $result);
  3717. $this->Form->create($this->article);
  3718. $result = $this->Form->allControls([], [
  3719. 'fieldset' => [
  3720. 'class' => 'some-class some-other-class',
  3721. 'disabled' => true,
  3722. 'data-param' => 'a-param'
  3723. ],
  3724. 'legend' => 'The Legend'
  3725. ]);
  3726. $expected = [
  3727. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3728. '<legend',
  3729. 'The Legend',
  3730. '/legend',
  3731. '*/fieldset',
  3732. ];
  3733. $this->assertHtml($expected, $result);
  3734. }
  3735. /**
  3736. * testFormControls method
  3737. *
  3738. * Test the controls() method.
  3739. *
  3740. * @return void
  3741. */
  3742. public function testFormControls()
  3743. {
  3744. $this->Form->create($this->article);
  3745. $result = $this->Form->allControls();
  3746. $expected = [
  3747. '<fieldset',
  3748. '<legend', 'New Article', '/legend',
  3749. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3750. ['div' => ['class' => 'input select required']],
  3751. '*/div',
  3752. ['div' => ['class' => 'input text required']],
  3753. '*/div',
  3754. ['div' => ['class' => 'input text']],
  3755. '*/div',
  3756. ['div' => ['class' => 'input text']],
  3757. '*/div',
  3758. '/fieldset',
  3759. ];
  3760. $this->assertHtml($expected, $result);
  3761. $result = $this->Form->allControls([
  3762. 'published' => ['type' => 'boolean']
  3763. ]);
  3764. $expected = [
  3765. '<fieldset',
  3766. '<legend', 'New Article', '/legend',
  3767. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3768. ['div' => ['class' => 'input select required']],
  3769. '*/div',
  3770. ['div' => ['class' => 'input text required']],
  3771. '*/div',
  3772. ['div' => ['class' => 'input text']],
  3773. '*/div',
  3774. ['div' => ['class' => 'input boolean']],
  3775. '*/div',
  3776. '/fieldset',
  3777. ];
  3778. $this->assertHtml($expected, $result);
  3779. $this->Form->create($this->article);
  3780. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3781. $expected = [
  3782. 'fieldset' => [],
  3783. 'legend' => [],
  3784. 'Hello',
  3785. '/legend',
  3786. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3787. ['div' => ['class' => 'input select required']],
  3788. '*/div',
  3789. ['div' => ['class' => 'input text required']],
  3790. '*/div',
  3791. ['div' => ['class' => 'input text']],
  3792. '*/div',
  3793. ['div' => ['class' => 'input text']],
  3794. '*/div',
  3795. '/fieldset'
  3796. ];
  3797. $this->assertHtml($expected, $result);
  3798. $this->Form->create(false);
  3799. $expected = [
  3800. 'fieldset' => [],
  3801. ['div' => ['class' => 'input text']],
  3802. 'label' => ['for' => 'foo'],
  3803. 'Foo',
  3804. '/label',
  3805. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3806. '*/div',
  3807. '/fieldset'
  3808. ];
  3809. $result = $this->Form->allControls(
  3810. ['foo' => ['type' => 'text']],
  3811. ['legend' => false]
  3812. );
  3813. $this->assertHtml($expected, $result);
  3814. }
  3815. /**
  3816. * testFormControlsBlacklist method
  3817. *
  3818. * @return void
  3819. */
  3820. public function testFormControlsBlacklist()
  3821. {
  3822. $this->Form->create($this->article);
  3823. $result = $this->Form->allControls([
  3824. 'id' => false
  3825. ]);
  3826. $expected = [
  3827. '<fieldset',
  3828. '<legend', 'New Article', '/legend',
  3829. ['div' => ['class' => 'input select required']],
  3830. '*/div',
  3831. ['div' => ['class' => 'input text required']],
  3832. '*/div',
  3833. ['div' => ['class' => 'input text']],
  3834. '*/div',
  3835. ['div' => ['class' => 'input text']],
  3836. '*/div',
  3837. '/fieldset',
  3838. ];
  3839. $this->assertHtml($expected, $result);
  3840. $this->Form->create($this->article);
  3841. $result = $this->Form->allControls([
  3842. 'id' => []
  3843. ]);
  3844. $expected = [
  3845. '<fieldset',
  3846. '<legend', 'New Article', '/legend',
  3847. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3848. ['div' => ['class' => 'input select required']],
  3849. '*/div',
  3850. ['div' => ['class' => 'input text required']],
  3851. '*/div',
  3852. ['div' => ['class' => 'input text']],
  3853. '*/div',
  3854. ['div' => ['class' => 'input text']],
  3855. '*/div',
  3856. '/fieldset',
  3857. ];
  3858. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3859. }
  3860. /**
  3861. * testSelectAsCheckbox method
  3862. *
  3863. * Test multi-select widget with checkbox formatting.
  3864. *
  3865. * @return void
  3866. */
  3867. public function testSelectAsCheckbox()
  3868. {
  3869. $result = $this->Form->select(
  3870. 'Model.multi_field',
  3871. ['first', 'second', 'third'],
  3872. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3873. );
  3874. $expected = [
  3875. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3876. ['div' => ['class' => 'checkbox']],
  3877. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3878. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3879. 'first',
  3880. '/label',
  3881. '/div',
  3882. ['div' => ['class' => 'checkbox']],
  3883. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3884. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3885. 'second',
  3886. '/label',
  3887. '/div',
  3888. ['div' => ['class' => 'checkbox']],
  3889. ['label' => ['for' => 'model-multi-field-2']],
  3890. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3891. 'third',
  3892. '/label',
  3893. '/div',
  3894. ];
  3895. $this->assertHtml($expected, $result);
  3896. $result = $this->Form->select(
  3897. 'Model.multi_field',
  3898. ['1/2' => 'half'],
  3899. ['multiple' => 'checkbox']
  3900. );
  3901. $expected = [
  3902. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3903. ['div' => ['class' => 'checkbox']],
  3904. ['label' => ['for' => 'model-multi-field-1-2']],
  3905. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3906. 'half',
  3907. '/label',
  3908. '/div',
  3909. ];
  3910. $this->assertHtml($expected, $result);
  3911. }
  3912. /**
  3913. * testLabel method
  3914. *
  3915. * Test label generation.
  3916. *
  3917. * @return void
  3918. */
  3919. public function testLabel()
  3920. {
  3921. $result = $this->Form->label('Person.name');
  3922. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3923. $this->assertHtml($expected, $result);
  3924. $result = $this->Form->label('Person.name');
  3925. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3926. $this->assertHtml($expected, $result);
  3927. $result = $this->Form->label('Person.first_name');
  3928. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3929. $this->assertHtml($expected, $result);
  3930. $result = $this->Form->label('Person.first_name', 'Your first name');
  3931. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3932. $this->assertHtml($expected, $result);
  3933. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3934. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3935. $this->assertHtml($expected, $result);
  3936. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3937. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3938. $this->assertHtml($expected, $result);
  3939. $result = $this->Form->label('Person.first_name', '');
  3940. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3941. $this->assertHtml($expected, $result);
  3942. $result = $this->Form->label('Person.2.name', '');
  3943. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3944. $this->assertHtml($expected, $result);
  3945. }
  3946. /**
  3947. * testLabelContainControl method
  3948. *
  3949. * Test that label() can accept an input with the correct template vars.
  3950. *
  3951. * @return void
  3952. */
  3953. public function testLabelContainControl()
  3954. {
  3955. $this->Form->templates([
  3956. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3957. ]);
  3958. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3959. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3960. ]);
  3961. $expected = [
  3962. 'label' => ['for' => 'person-accept-terms'],
  3963. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3964. 'Accept',
  3965. '/label',
  3966. ];
  3967. $this->assertHtml($expected, $result);
  3968. }
  3969. /**
  3970. * testTextbox method
  3971. *
  3972. * Test textbox element generation.
  3973. *
  3974. * @return void
  3975. */
  3976. public function testTextbox()
  3977. {
  3978. $result = $this->Form->text('Model.field');
  3979. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3980. $this->assertHtml($expected, $result);
  3981. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3982. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3983. $this->assertHtml($expected, $result);
  3984. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3985. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3986. $this->assertHtml($expected, $result);
  3987. }
  3988. /**
  3989. * testTextBoxDataAndError method
  3990. *
  3991. * Test that text() hooks up with request data and error fields.
  3992. *
  3993. * @return void
  3994. */
  3995. public function testTextBoxDataAndError()
  3996. {
  3997. $this->article['errors'] = [
  3998. 'Contact' => ['text' => 'wrong']
  3999. ];
  4000. $this->Form->create($this->article);
  4001. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  4002. $result = $this->Form->text('Model.text');
  4003. $expected = [
  4004. 'input' => [
  4005. 'type' => 'text',
  4006. 'name' => 'Model[text]',
  4007. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4008. ]
  4009. ];
  4010. $this->assertHtml($expected, $result);
  4011. $this->Form->request->data['Contact']['text'] = 'test';
  4012. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4013. $expected = [
  4014. 'input' => [
  4015. 'type' => 'text',
  4016. 'name' => 'Contact[text]',
  4017. 'value' => 'test',
  4018. 'id' => 'theID',
  4019. 'class' => 'form-error'
  4020. ]
  4021. ];
  4022. $this->assertHtml($expected, $result);
  4023. }
  4024. /**
  4025. * testDefaultValue method
  4026. *
  4027. * Test default value setting.
  4028. *
  4029. * @return void
  4030. */
  4031. public function testTextDefaultValue()
  4032. {
  4033. $this->Form->request->data['Model']['field'] = 'test';
  4034. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4035. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4036. $this->assertHtml($expected, $result);
  4037. unset($this->Form->request->data['Model']['field']);
  4038. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4039. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4040. $this->assertHtml($expected, $result);
  4041. $Articles = TableRegistry::get('Articles');
  4042. $title = $Articles->schema()->column('title');
  4043. $Articles->schema()->addColumn(
  4044. 'title',
  4045. ['default' => 'default title'] + $title
  4046. );
  4047. $entity = $Articles->newEntity();
  4048. $this->Form->create($entity);
  4049. // Get default value from schema
  4050. $result = $this->Form->text('title');
  4051. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4052. $this->assertHtml($expected, $result);
  4053. // Don't get value from schema
  4054. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4055. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4056. $this->assertHtml($expected, $result);
  4057. // Custom default value overrides default value from schema
  4058. $result = $this->Form->text('title', ['default' => 'override default']);
  4059. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4060. $this->assertHtml($expected, $result);
  4061. // Default value from schema is used only for new entities.
  4062. $entity->isNew(false);
  4063. $result = $this->Form->text('title');
  4064. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4065. $this->assertHtml($expected, $result);
  4066. }
  4067. /**
  4068. * testError method
  4069. *
  4070. * Test field error generation.
  4071. *
  4072. * @return void
  4073. */
  4074. public function testError()
  4075. {
  4076. $this->article['errors'] = [
  4077. 'Article' => ['field' => 'email']
  4078. ];
  4079. $this->Form->create($this->article);
  4080. $result = $this->Form->error('Article.field');
  4081. $expected = [
  4082. ['div' => ['class' => 'error-message']],
  4083. 'email',
  4084. '/div',
  4085. ];
  4086. $this->assertHtml($expected, $result);
  4087. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  4088. $expected = [
  4089. ['div' => ['class' => 'error-message']],
  4090. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4091. '/div',
  4092. ];
  4093. $this->assertHtml($expected, $result);
  4094. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  4095. $expected = [
  4096. ['div' => ['class' => 'error-message']],
  4097. '<strong', 'Badness!', '/strong',
  4098. '/div',
  4099. ];
  4100. $this->assertHtml($expected, $result);
  4101. }
  4102. /**
  4103. * testErrorRuleName method
  4104. *
  4105. * Test error translation can use rule names for translating.
  4106. *
  4107. * @return void
  4108. */
  4109. public function testErrorRuleName()
  4110. {
  4111. $this->article['errors'] = [
  4112. 'Article' => [
  4113. 'field' => ['email' => 'Your email was not good']
  4114. ]
  4115. ];
  4116. $this->Form->create($this->article);
  4117. $result = $this->Form->error('Article.field');
  4118. $expected = [
  4119. ['div' => ['class' => 'error-message']],
  4120. 'Your email was not good',
  4121. '/div',
  4122. ];
  4123. $this->assertHtml($expected, $result);
  4124. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4125. $expected = [
  4126. ['div' => ['class' => 'error-message']],
  4127. 'Email in use',
  4128. '/div',
  4129. ];
  4130. $this->assertHtml($expected, $result);
  4131. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4132. $expected = [
  4133. ['div' => ['class' => 'error-message']],
  4134. 'Email in use',
  4135. '/div',
  4136. ];
  4137. $this->assertHtml($expected, $result);
  4138. $result = $this->Form->error('Article.field', [
  4139. 'email' => 'Key is preferred',
  4140. 'Your email was not good' => 'Email in use'
  4141. ]);
  4142. $expected = [
  4143. ['div' => ['class' => 'error-message']],
  4144. 'Key is preferred',
  4145. '/div',
  4146. ];
  4147. $this->assertHtml($expected, $result);
  4148. }
  4149. /**
  4150. * testErrorMessages method
  4151. *
  4152. * Test error with nested lists.
  4153. *
  4154. * @return void
  4155. */
  4156. public function testErrorMessages()
  4157. {
  4158. $this->article['errors'] = [
  4159. 'Article' => ['field' => 'email']
  4160. ];
  4161. $this->Form->create($this->article);
  4162. $result = $this->Form->error('Article.field', [
  4163. 'email' => 'No good!'
  4164. ]);
  4165. $expected = [
  4166. 'div' => ['class' => 'error-message'],
  4167. 'No good!',
  4168. '/div'
  4169. ];
  4170. $this->assertHtml($expected, $result);
  4171. }
  4172. /**
  4173. * testErrorMultipleMessages method
  4174. *
  4175. * Test error() with multiple messages.
  4176. *
  4177. * @return void
  4178. */
  4179. public function testErrorMultipleMessages()
  4180. {
  4181. $this->article['errors'] = [
  4182. 'field' => ['notBlank', 'email', 'Something else']
  4183. ];
  4184. $this->Form->create($this->article);
  4185. $result = $this->Form->error('field', [
  4186. 'notBlank' => 'Cannot be empty',
  4187. 'email' => 'No good!'
  4188. ]);
  4189. $expected = [
  4190. 'div' => ['class' => 'error-message'],
  4191. 'ul' => [],
  4192. '<li', 'Cannot be empty', '/li',
  4193. '<li', 'No good!', '/li',
  4194. '<li', 'Something else', '/li',
  4195. '/ul',
  4196. '/div'
  4197. ];
  4198. $this->assertHtml($expected, $result);
  4199. }
  4200. /**
  4201. * testPassword method
  4202. *
  4203. * Test password element generation.
  4204. *
  4205. * @return void
  4206. */
  4207. public function testPassword()
  4208. {
  4209. $this->article['errors'] = [
  4210. 'Contact' => [
  4211. 'passwd' => 1
  4212. ]
  4213. ];
  4214. $this->Form->create($this->article);
  4215. $result = $this->Form->password('Contact.field');
  4216. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4217. $this->assertHtml($expected, $result);
  4218. $this->Form->request->data['Contact']['passwd'] = 'test';
  4219. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4220. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4221. $this->assertHtml($expected, $result);
  4222. }
  4223. /**
  4224. * testRadio method
  4225. *
  4226. * Test radio element set generation.
  4227. *
  4228. * @return void
  4229. */
  4230. public function testRadio()
  4231. {
  4232. $result = $this->Form->radio('Model.field', ['option A']);
  4233. $expected = [
  4234. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4235. 'label' => ['for' => 'model-field-0'],
  4236. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4237. 'option A',
  4238. '/label'
  4239. ];
  4240. $this->assertHtml($expected, $result);
  4241. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4242. $this->assertHtml($expected, $result);
  4243. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4244. $expected = [
  4245. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4246. ['label' => ['for' => 'model-field-0']],
  4247. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4248. 'option A',
  4249. '/label',
  4250. ['label' => ['for' => 'model-field-1']],
  4251. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4252. 'option B',
  4253. '/label',
  4254. ];
  4255. $this->assertHtml($expected, $result);
  4256. $result = $this->Form->radio(
  4257. 'Employee.gender',
  4258. ['male' => 'Male', 'female' => 'Female'],
  4259. ['form' => 'my-form']
  4260. );
  4261. $expected = [
  4262. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4263. ['label' => ['for' => 'employee-gender-male']],
  4264. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4265. 'Male',
  4266. '/label',
  4267. ['label' => ['for' => 'employee-gender-female']],
  4268. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4269. 'Female',
  4270. '/label',
  4271. ];
  4272. $this->assertHtml($expected, $result);
  4273. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4274. $expected = [
  4275. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4276. ['label' => ['for' => 'model-custom-0']],
  4277. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4278. 'option A',
  4279. '/label',
  4280. ['label' => ['for' => 'model-custom-1']],
  4281. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4282. 'option B',
  4283. '/label',
  4284. ];
  4285. $this->assertHtml($expected, $result);
  4286. $result = $this->Form->radio(
  4287. 'Employee.gender',
  4288. [
  4289. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4290. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4291. ]
  4292. );
  4293. $expected = [
  4294. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4295. ['label' => ['for' => 'employee-gender-male']],
  4296. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4297. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4298. 'Male',
  4299. '/label',
  4300. ['label' => ['for' => 'employee-gender-female']],
  4301. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4302. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4303. 'Female',
  4304. '/label',
  4305. ];
  4306. $this->assertHtml($expected, $result);
  4307. }
  4308. /**
  4309. * Test radio with complex options and empty disabled data.
  4310. *
  4311. * @return void
  4312. */
  4313. public function testRadioComplexDisabled()
  4314. {
  4315. $options = [
  4316. ['value' => 'r', 'text' => 'red'],
  4317. ['value' => 'b', 'text' => 'blue'],
  4318. ];
  4319. $attrs = ['disabled' => []];
  4320. $result = $this->Form->radio('Model.field', $options, $attrs);
  4321. $expected = [
  4322. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4323. ['label' => ['for' => 'model-field-r']],
  4324. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4325. 'red',
  4326. '/label',
  4327. ['label' => ['for' => 'model-field-b']],
  4328. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4329. 'blue',
  4330. '/label',
  4331. ];
  4332. $this->assertHtml($expected, $result);
  4333. $attrs = ['disabled' => ['r']];
  4334. $result = $this->Form->radio('Model.field', $options, $attrs);
  4335. $this->assertContains('disabled="disabled"', $result);
  4336. }
  4337. /**
  4338. * testRadioDefaultValue method
  4339. *
  4340. * Test default value setting on radio() method.
  4341. *
  4342. * @return void
  4343. */
  4344. public function testRadioDefaultValue()
  4345. {
  4346. $Articles = TableRegistry::get('Articles');
  4347. $title = $Articles->schema()->column('title');
  4348. $Articles->schema()->addColumn(
  4349. 'title',
  4350. ['default' => '1'] + $title
  4351. );
  4352. $this->Form->create($Articles->newEntity());
  4353. $result = $this->Form->radio('title', ['option A', 'option B']);
  4354. $expected = [
  4355. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4356. ['label' => ['for' => 'title-0']],
  4357. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4358. 'option A',
  4359. '/label',
  4360. ['label' => ['for' => 'title-1']],
  4361. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4362. 'option B',
  4363. '/label',
  4364. ];
  4365. $this->assertHtml($expected, $result);
  4366. }
  4367. /**
  4368. * testControlRadio method
  4369. *
  4370. * Test that input works with radio types.
  4371. *
  4372. * @return void
  4373. */
  4374. public function testControlRadio()
  4375. {
  4376. $result = $this->Form->control('test', [
  4377. 'type' => 'radio',
  4378. 'options' => ['A', 'B'],
  4379. ]);
  4380. $expected = [
  4381. ['div' => ['class' => 'input radio']],
  4382. '<label',
  4383. 'Test',
  4384. '/label',
  4385. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4386. ['label' => ['for' => 'test-0']],
  4387. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4388. 'A',
  4389. '/label',
  4390. ['label' => ['for' => 'test-1']],
  4391. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4392. 'B',
  4393. '/label',
  4394. '/div',
  4395. ];
  4396. $this->assertHtml($expected, $result);
  4397. $result = $this->Form->control('test', [
  4398. 'type' => 'radio',
  4399. 'options' => ['A', 'B'],
  4400. 'value' => '0'
  4401. ]);
  4402. $expected = [
  4403. ['div' => ['class' => 'input radio']],
  4404. '<label',
  4405. 'Test',
  4406. '/label',
  4407. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4408. ['label' => ['for' => 'test-0']],
  4409. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4410. 'A',
  4411. '/label',
  4412. ['label' => ['for' => 'test-1']],
  4413. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4414. 'B',
  4415. '/label',
  4416. '/div',
  4417. ];
  4418. $this->assertHtml($expected, $result);
  4419. $result = $this->Form->control('test', [
  4420. 'type' => 'radio',
  4421. 'options' => ['A', 'B'],
  4422. 'label' => false
  4423. ]);
  4424. $expected = [
  4425. ['div' => ['class' => 'input radio']],
  4426. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4427. ['label' => ['for' => 'test-0']],
  4428. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4429. 'A',
  4430. '/label',
  4431. ['label' => ['for' => 'test-1']],
  4432. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4433. 'B',
  4434. '/label',
  4435. '/div',
  4436. ];
  4437. $this->assertHtml($expected, $result);
  4438. }
  4439. /**
  4440. * testRadioNoLabel method
  4441. *
  4442. * Test that radio() works with label = false.
  4443. *
  4444. * @return void
  4445. */
  4446. public function testRadioNoLabel()
  4447. {
  4448. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4449. $expected = [
  4450. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4451. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4452. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4453. ];
  4454. $this->assertHtml($expected, $result);
  4455. }
  4456. /**
  4457. * testRadioControlInsideLabel method
  4458. *
  4459. * Test generating radio input inside label ala twitter bootstrap.
  4460. *
  4461. * @return void
  4462. */
  4463. public function testRadioControlInsideLabel()
  4464. {
  4465. $this->Form->templates([
  4466. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4467. 'radioWrapper' => '{{label}}'
  4468. ]);
  4469. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4470. //@codingStandardsIgnoreStart
  4471. $expected = [
  4472. ['input' => [
  4473. 'type' => 'hidden',
  4474. 'name' => 'Model[field]',
  4475. 'value' => ''
  4476. ]],
  4477. ['label' => ['for' => 'model-field-0']],
  4478. ['input' => [
  4479. 'type' => 'radio',
  4480. 'name' => 'Model[field]',
  4481. 'value' => '0',
  4482. 'id' => 'model-field-0'
  4483. ]],
  4484. 'option A',
  4485. '/label',
  4486. ['label' => ['for' => 'model-field-1']],
  4487. ['input' => [
  4488. 'type' => 'radio',
  4489. 'name' => 'Model[field]',
  4490. 'value' => '1',
  4491. 'id' => 'model-field-1'
  4492. ]],
  4493. 'option B',
  4494. '/label',
  4495. ];
  4496. //@codingStandardsIgnoreEnd
  4497. $this->assertHtml($expected, $result);
  4498. }
  4499. /**
  4500. * testRadioHiddenControlDisabling method
  4501. *
  4502. * Test disabling the hidden input for radio buttons.
  4503. *
  4504. * @return void
  4505. */
  4506. public function testRadioHiddenControlDisabling()
  4507. {
  4508. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4509. $expected = [
  4510. 'label' => ['for' => 'model-1-field-0'],
  4511. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4512. 'option A',
  4513. '/label'
  4514. ];
  4515. $this->assertHtml($expected, $result);
  4516. }
  4517. /**
  4518. * testRadioOutOfRange method
  4519. *
  4520. * Test radio element set generation.
  4521. *
  4522. * @return void
  4523. */
  4524. public function testRadioOutOfRange()
  4525. {
  4526. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4527. $expected = [
  4528. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4529. 'label' => ['for' => 'model-field-v'],
  4530. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4531. 'value',
  4532. '/label'
  4533. ];
  4534. $this->assertHtml($expected, $result);
  4535. }
  4536. /**
  4537. * testSelect method
  4538. *
  4539. * Test select element generation.
  4540. *
  4541. * @return void
  4542. */
  4543. public function testSelect()
  4544. {
  4545. $result = $this->Form->select('Model.field', []);
  4546. $expected = [
  4547. 'select' => ['name' => 'Model[field]'],
  4548. '/select'
  4549. ];
  4550. $this->assertHtml($expected, $result);
  4551. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4552. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4553. $expected = [
  4554. 'select' => ['name' => 'Model[field]'],
  4555. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4556. 'good',
  4557. '/option',
  4558. ['option' => ['value' => 'other']],
  4559. 'bad',
  4560. '/option',
  4561. '/select'
  4562. ];
  4563. $this->assertHtml($expected, $result);
  4564. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4565. $this->assertHtml($expected, $result);
  4566. $this->Form->request->data = [];
  4567. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4568. $expected = [
  4569. 'select' => ['name' => 'Model[field]'],
  4570. ['option' => ['value' => 'value']],
  4571. 'good',
  4572. '/option',
  4573. ['option' => ['value' => 'other']],
  4574. 'bad',
  4575. '/option',
  4576. '/select'
  4577. ];
  4578. $this->assertHtml($expected, $result);
  4579. $options = [
  4580. ['value' => 'first', 'text' => 'First'],
  4581. ['value' => 'first', 'text' => 'Another First'],
  4582. ];
  4583. $result = $this->Form->select(
  4584. 'Model.field',
  4585. $options,
  4586. ['escape' => false, 'empty' => false]
  4587. );
  4588. $expected = [
  4589. 'select' => ['name' => 'Model[field]'],
  4590. ['option' => ['value' => 'first']],
  4591. 'First',
  4592. '/option',
  4593. ['option' => ['value' => 'first']],
  4594. 'Another First',
  4595. '/option',
  4596. '/select'
  4597. ];
  4598. $this->assertHtml($expected, $result);
  4599. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4600. $result = $this->Form->select(
  4601. 'Model.contact_id',
  4602. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4603. ['escape' => false, 'empty' => 'pick something']
  4604. );
  4605. $expected = [
  4606. 'select' => ['name' => 'Model[contact_id]'],
  4607. ['option' => ['value' => '']], 'pick something', '/option',
  4608. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4609. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4610. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4611. '/select'
  4612. ];
  4613. $this->assertHtml($expected, $result);
  4614. $this->Form->request->data['Model']['field'] = 0;
  4615. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4616. $expected = [
  4617. 'select' => ['name' => 'Model[field]'],
  4618. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4619. ['option' => ['value' => '1']], 'Yes', '/option',
  4620. '/select'
  4621. ];
  4622. $this->assertHtml($expected, $result);
  4623. }
  4624. /**
  4625. * testSelectEscapeHtml method
  4626. *
  4627. * Test that select() escapes HTML.
  4628. *
  4629. * @return void
  4630. */
  4631. public function testSelectEscapeHtml()
  4632. {
  4633. $result = $this->Form->select(
  4634. 'Model.field',
  4635. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4636. ['empty' => false]
  4637. );
  4638. $expected = [
  4639. 'select' => ['name' => 'Model[field]'],
  4640. ['option' => ['value' => 'first']],
  4641. 'first &quot;html&quot; &lt;chars&gt;',
  4642. '/option',
  4643. ['option' => ['value' => 'second']],
  4644. 'value',
  4645. '/option',
  4646. '/select'
  4647. ];
  4648. $this->assertHtml($expected, $result);
  4649. $result = $this->Form->select(
  4650. 'Model.field',
  4651. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4652. ['escape' => false, 'empty' => false]
  4653. );
  4654. $expected = [
  4655. 'select' => ['name' => 'Model[field]'],
  4656. ['option' => ['value' => 'first']],
  4657. 'first "html" <chars>',
  4658. '/option',
  4659. ['option' => ['value' => 'second']],
  4660. 'value',
  4661. '/option',
  4662. '/select'
  4663. ];
  4664. $this->assertHtml($expected, $result);
  4665. }
  4666. /**
  4667. * testSelectRequired method
  4668. *
  4669. * Test select() with required and disabled attributes.
  4670. *
  4671. * @return void
  4672. */
  4673. public function testSelectRequired()
  4674. {
  4675. $this->article['required'] = [
  4676. 'user_id' => true
  4677. ];
  4678. $this->Form->create($this->article);
  4679. $result = $this->Form->select('user_id', ['option A']);
  4680. $expected = [
  4681. 'select' => [
  4682. 'name' => 'user_id',
  4683. 'required' => 'required'
  4684. ],
  4685. ['option' => ['value' => '0']], 'option A', '/option',
  4686. '/select'
  4687. ];
  4688. $this->assertHtml($expected, $result);
  4689. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4690. $expected = [
  4691. 'select' => [
  4692. 'name' => 'user_id',
  4693. 'disabled' => 'disabled'
  4694. ],
  4695. ['option' => ['value' => '0']], 'option A', '/option',
  4696. '/select'
  4697. ];
  4698. $this->assertHtml($expected, $result);
  4699. }
  4700. /**
  4701. * testNestedSelect method
  4702. *
  4703. * Test select element generation with optgroups.
  4704. *
  4705. * @return void
  4706. */
  4707. public function testNestedSelect()
  4708. {
  4709. $result = $this->Form->select(
  4710. 'Model.field',
  4711. [1 => 'One', 2 => 'Two', 'Three' => [
  4712. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4713. ]],
  4714. ['empty' => false]
  4715. );
  4716. $expected = [
  4717. 'select' => ['name' => 'Model[field]'],
  4718. ['option' => ['value' => 1]],
  4719. 'One',
  4720. '/option',
  4721. ['option' => ['value' => 2]],
  4722. 'Two',
  4723. '/option',
  4724. ['optgroup' => ['label' => 'Three']],
  4725. ['option' => ['value' => 3]],
  4726. 'Three',
  4727. '/option',
  4728. ['option' => ['value' => 4]],
  4729. 'Four',
  4730. '/option',
  4731. ['option' => ['value' => 5]],
  4732. 'Five',
  4733. '/option',
  4734. '/optgroup',
  4735. '/select'
  4736. ];
  4737. $this->assertHtml($expected, $result);
  4738. }
  4739. /**
  4740. * testSelectMultiple method
  4741. *
  4742. * Test generation of multiple select elements.
  4743. *
  4744. * @return void
  4745. */
  4746. public function testSelectMultiple()
  4747. {
  4748. $options = ['first', 'second', 'third'];
  4749. $result = $this->Form->select(
  4750. 'Model.multi_field',
  4751. $options,
  4752. ['form' => 'my-form', 'multiple' => true]
  4753. );
  4754. $expected = [
  4755. 'input' => [
  4756. 'type' => 'hidden',
  4757. 'name' => 'Model[multi_field]',
  4758. 'value' => '',
  4759. 'form' => 'my-form',
  4760. ],
  4761. 'select' => [
  4762. 'name' => 'Model[multi_field][]',
  4763. 'multiple' => 'multiple',
  4764. 'form' => 'my-form',
  4765. ],
  4766. ['option' => ['value' => '0']],
  4767. 'first',
  4768. '/option',
  4769. ['option' => ['value' => '1']],
  4770. 'second',
  4771. '/option',
  4772. ['option' => ['value' => '2']],
  4773. 'third',
  4774. '/option',
  4775. '/select'
  4776. ];
  4777. $this->assertHtml($expected, $result);
  4778. $result = $this->Form->select(
  4779. 'Model.multi_field',
  4780. $options,
  4781. ['multiple' => 'multiple', 'form' => 'my-form']
  4782. );
  4783. $this->assertHtml($expected, $result);
  4784. }
  4785. /**
  4786. * testCheckboxZeroValue method
  4787. *
  4788. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4789. *
  4790. * @return void
  4791. */
  4792. public function testCheckboxZeroValue()
  4793. {
  4794. $result = $this->Form->control('User.get_spam', [
  4795. 'type' => 'checkbox',
  4796. 'value' => '0',
  4797. 'hiddenField' => '1',
  4798. ]);
  4799. $expected = [
  4800. 'div' => ['class' => 'input checkbox'],
  4801. 'label' => ['for' => 'user-get-spam'],
  4802. ['input' => [
  4803. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4804. 'value' => '1'
  4805. ]],
  4806. ['input' => [
  4807. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4808. 'value' => '0', 'id' => 'user-get-spam'
  4809. ]],
  4810. 'Get Spam',
  4811. '/label',
  4812. '/div'
  4813. ];
  4814. $this->assertHtml($expected, $result);
  4815. }
  4816. /**
  4817. * testHabtmSelectBox method
  4818. *
  4819. * Test generation of habtm select boxes.
  4820. *
  4821. * @return void
  4822. */
  4823. public function testHabtmSelectBox()
  4824. {
  4825. $this->loadFixtures('Articles');
  4826. $options = [
  4827. 1 => 'blue',
  4828. 2 => 'red',
  4829. 3 => 'green'
  4830. ];
  4831. $tags = [
  4832. new Entity(['id' => 1, 'name' => 'blue']),
  4833. new Entity(['id' => 3, 'name' => 'green'])
  4834. ];
  4835. $article = new Article(['tags' => $tags]);
  4836. $this->Form->create($article);
  4837. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4838. $expected = [
  4839. 'div' => ['class' => 'input select'],
  4840. 'label' => ['for' => 'tags-ids'],
  4841. 'Tags',
  4842. '/label',
  4843. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4844. 'select' => [
  4845. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4846. 'multiple' => 'multiple'
  4847. ],
  4848. ['option' => ['value' => '1', 'selected' => 'selected']],
  4849. 'blue',
  4850. '/option',
  4851. ['option' => ['value' => '2']],
  4852. 'red',
  4853. '/option',
  4854. ['option' => ['value' => '3', 'selected' => 'selected']],
  4855. 'green',
  4856. '/option',
  4857. '/select',
  4858. '/div'
  4859. ];
  4860. $this->assertHtml($expected, $result);
  4861. // make sure only 50 is selected, and not 50f5c0cf
  4862. $options = [
  4863. '1' => 'blue',
  4864. '50f5c0cf' => 'red',
  4865. '50' => 'green'
  4866. ];
  4867. $tags = [
  4868. new Entity(['id' => 1, 'name' => 'blue']),
  4869. new Entity(['id' => 50, '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' => '50f5c0cf']],
  4888. 'red',
  4889. '/option',
  4890. ['option' => ['value' => '50', 'selected' => 'selected']],
  4891. 'green',
  4892. '/option',
  4893. '/select',
  4894. '/div'
  4895. ];
  4896. $this->assertHtml($expected, $result);
  4897. $spacecraft = [
  4898. 1 => 'Orion',
  4899. 2 => 'Helios'
  4900. ];
  4901. $this->View->viewVars['spacecraft'] = $spacecraft;
  4902. $this->Form->create();
  4903. $result = $this->Form->control('spacecraft._ids');
  4904. $expected = [
  4905. 'div' => ['class' => 'input select'],
  4906. 'label' => ['for' => 'spacecraft-ids'],
  4907. 'Spacecraft',
  4908. '/label',
  4909. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4910. 'select' => [
  4911. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4912. 'multiple' => 'multiple'
  4913. ],
  4914. ['option' => ['value' => '1']],
  4915. 'Orion',
  4916. '/option',
  4917. ['option' => ['value' => '2']],
  4918. 'Helios',
  4919. '/option',
  4920. '/select',
  4921. '/div'
  4922. ];
  4923. $this->assertHtml($expected, $result);
  4924. }
  4925. /**
  4926. * testErrorsForBelongsToManySelect method
  4927. *
  4928. * Tests that errors for belongsToMany select fields are being
  4929. * picked up properly.
  4930. *
  4931. * @return void
  4932. */
  4933. public function testErrorsForBelongsToManySelect()
  4934. {
  4935. $spacecraft = [
  4936. 1 => 'Orion',
  4937. 2 => 'Helios'
  4938. ];
  4939. $this->View->viewVars['spacecraft'] = $spacecraft;
  4940. $article = new Article();
  4941. $article->errors('spacecraft', ['Invalid']);
  4942. $this->Form->create($article);
  4943. $result = $this->Form->control('spacecraft._ids');
  4944. $expected = [
  4945. ['div' => ['class' => 'input select error']],
  4946. 'label' => ['for' => 'spacecraft-ids'],
  4947. 'Spacecraft',
  4948. '/label',
  4949. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4950. 'select' => [
  4951. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4952. 'multiple' => 'multiple'
  4953. ],
  4954. ['option' => ['value' => '1']],
  4955. 'Orion',
  4956. '/option',
  4957. ['option' => ['value' => '2']],
  4958. 'Helios',
  4959. '/option',
  4960. '/select',
  4961. ['div' => ['class' => 'error-message']],
  4962. 'Invalid',
  4963. '/div',
  4964. '/div'
  4965. ];
  4966. $this->assertHtml($expected, $result);
  4967. }
  4968. /**
  4969. * testSelectMultipleCheckboxes method
  4970. *
  4971. * Test generation of multi select elements in checkbox format.
  4972. *
  4973. * @return void
  4974. */
  4975. public function testSelectMultipleCheckboxes()
  4976. {
  4977. $result = $this->Form->select(
  4978. 'Model.multi_field',
  4979. ['first', 'second', 'third'],
  4980. ['multiple' => 'checkbox']
  4981. );
  4982. $expected = [
  4983. 'input' => [
  4984. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4985. ],
  4986. ['div' => ['class' => 'checkbox']],
  4987. ['label' => ['for' => 'model-multi-field-0']],
  4988. ['input' => [
  4989. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4990. 'value' => '0', 'id' => 'model-multi-field-0'
  4991. ]],
  4992. 'first',
  4993. '/label',
  4994. '/div',
  4995. ['div' => ['class' => 'checkbox']],
  4996. ['label' => ['for' => 'model-multi-field-1']],
  4997. ['input' => [
  4998. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4999. 'value' => '1', 'id' => 'model-multi-field-1'
  5000. ]],
  5001. 'second',
  5002. '/label',
  5003. '/div',
  5004. ['div' => ['class' => 'checkbox']],
  5005. ['label' => ['for' => 'model-multi-field-2']],
  5006. ['input' => [
  5007. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5008. 'value' => '2', 'id' => 'model-multi-field-2'
  5009. ]],
  5010. 'third',
  5011. '/label',
  5012. '/div'
  5013. ];
  5014. $this->assertHtml($expected, $result);
  5015. $result = $this->Form->select(
  5016. 'Model.multi_field',
  5017. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5018. ['multiple' => 'checkbox']
  5019. );
  5020. $expected = [
  5021. 'input' => [
  5022. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5023. ],
  5024. ['div' => ['class' => 'checkbox']],
  5025. ['label' => ['for' => 'model-multi-field-a+']],
  5026. ['input' => [
  5027. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5028. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5029. ]],
  5030. 'first',
  5031. '/label',
  5032. '/div',
  5033. ['div' => ['class' => 'checkbox']],
  5034. ['label' => ['for' => 'model-multi-field-a++']],
  5035. ['input' => [
  5036. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5037. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5038. ]],
  5039. 'second',
  5040. '/label',
  5041. '/div',
  5042. ['div' => ['class' => 'checkbox']],
  5043. ['label' => ['for' => 'model-multi-field-a+++']],
  5044. ['input' => [
  5045. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5046. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5047. ]],
  5048. 'third',
  5049. '/label',
  5050. '/div'
  5051. ];
  5052. $this->assertHtml($expected, $result);
  5053. $result = $this->Form->select(
  5054. 'Model.multi_field',
  5055. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5056. ['multiple' => 'checkbox']
  5057. );
  5058. $expected = [
  5059. 'input' => [
  5060. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5061. ],
  5062. ['div' => ['class' => 'checkbox']],
  5063. ['label' => ['for' => 'model-multi-field-a-b']],
  5064. ['input' => [
  5065. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5066. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5067. ]],
  5068. 'first',
  5069. '/label',
  5070. '/div',
  5071. ['div' => ['class' => 'checkbox']],
  5072. ['label' => ['for' => 'model-multi-field-a-b1']],
  5073. ['input' => [
  5074. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5075. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5076. ]],
  5077. 'second',
  5078. '/label',
  5079. '/div',
  5080. ['div' => ['class' => 'checkbox']],
  5081. ['label' => ['for' => 'model-multi-field-a-b2']],
  5082. ['input' => [
  5083. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5084. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5085. ]],
  5086. 'third',
  5087. '/label',
  5088. '/div'
  5089. ];
  5090. $this->assertHtml($expected, $result);
  5091. }
  5092. /**
  5093. * testSelectMultipleCheckboxRequestData method
  5094. *
  5095. * Ensure that multiCheckbox reads from the request data.
  5096. *
  5097. * @return void
  5098. */
  5099. public function testSelectMultipleCheckboxRequestData()
  5100. {
  5101. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5102. $result = $this->Form->select(
  5103. 'Model.tags',
  5104. ['1' => 'first', 'Array' => 'Array'],
  5105. ['multiple' => 'checkbox']
  5106. );
  5107. $expected = [
  5108. 'input' => [
  5109. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5110. ],
  5111. ['div' => ['class' => 'checkbox']],
  5112. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5113. ['input' => [
  5114. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5115. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5116. ]],
  5117. 'first',
  5118. '/label',
  5119. '/div',
  5120. ['div' => ['class' => 'checkbox']],
  5121. ['label' => ['for' => 'model-tags-array']],
  5122. ['input' => [
  5123. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5124. 'value' => 'Array', 'id' => 'model-tags-array'
  5125. ]],
  5126. 'Array',
  5127. '/label',
  5128. '/div'
  5129. ];
  5130. $this->assertHtml($expected, $result);
  5131. }
  5132. /**
  5133. * testSelectMultipleCheckboxSecurity method
  5134. *
  5135. * Checks the security hash array generated for multiple-input checkbox elements.
  5136. *
  5137. * @return void
  5138. */
  5139. public function testSelectMultipleCheckboxSecurity()
  5140. {
  5141. $this->Form->request->params['_Token'] = 'testKey';
  5142. $this->assertEquals([], $this->Form->fields);
  5143. $this->Form->select(
  5144. 'Model.multi_field',
  5145. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5146. ['multiple' => 'checkbox']
  5147. );
  5148. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5149. $result = $this->Form->secure($this->Form->fields);
  5150. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5151. $this->assertRegExp('/"' . $key . '"/', $result);
  5152. }
  5153. /**
  5154. * testSelectMultipleSecureWithNoOptions method
  5155. *
  5156. * Multiple select elements should always be secured as they always participate
  5157. * in the POST data.
  5158. *
  5159. * @return void
  5160. */
  5161. public function testSelectMultipleSecureWithNoOptions()
  5162. {
  5163. $this->assertEquals([], $this->Form->fields);
  5164. $this->Form->select(
  5165. 'Model.select',
  5166. [],
  5167. ['multiple' => true]
  5168. );
  5169. $this->assertEquals(['Model.select'], $this->Form->fields);
  5170. }
  5171. /**
  5172. * testSelectNoSecureWithNoOptions method
  5173. *
  5174. * When a select box has no options it should not be added to the fields list
  5175. * as it always fail post validation.
  5176. *
  5177. * @return void
  5178. */
  5179. public function testSelectNoSecureWithNoOptions()
  5180. {
  5181. $this->Form->request->params['_Token'] = 'testkey';
  5182. $this->assertEquals([], $this->Form->fields);
  5183. $this->Form->select(
  5184. 'Model.select',
  5185. []
  5186. );
  5187. $this->assertEquals([], $this->Form->fields);
  5188. $this->Form->select(
  5189. 'Model.user_id',
  5190. [],
  5191. ['empty' => true]
  5192. );
  5193. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5194. }
  5195. /**
  5196. * testControlMultipleCheckboxes method
  5197. *
  5198. * Test control() resulting in multi select elements being generated.
  5199. *
  5200. * @return void
  5201. */
  5202. public function testControlMultipleCheckboxes()
  5203. {
  5204. $result = $this->Form->control('Model.multi_field', [
  5205. 'options' => ['first', 'second', 'third'],
  5206. 'multiple' => 'checkbox'
  5207. ]);
  5208. $expected = [
  5209. ['div' => ['class' => 'input select']],
  5210. ['label' => ['for' => 'model-multi-field']],
  5211. 'Multi Field',
  5212. '/label',
  5213. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5214. ['div' => ['class' => 'checkbox']],
  5215. ['label' => ['for' => 'model-multi-field-0']],
  5216. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5217. 'first',
  5218. '/label',
  5219. '/div',
  5220. ['div' => ['class' => 'checkbox']],
  5221. ['label' => ['for' => 'model-multi-field-1']],
  5222. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5223. 'second',
  5224. '/label',
  5225. '/div',
  5226. ['div' => ['class' => 'checkbox']],
  5227. ['label' => ['for' => 'model-multi-field-2']],
  5228. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5229. 'third',
  5230. '/label',
  5231. '/div',
  5232. '/div'
  5233. ];
  5234. $this->assertHtml($expected, $result);
  5235. $result = $this->Form->control('Model.multi_field', [
  5236. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5237. 'multiple' => 'checkbox'
  5238. ]);
  5239. $expected = [
  5240. ['div' => ['class' => 'input select']],
  5241. ['label' => ['for' => 'model-multi-field']],
  5242. 'Multi Field',
  5243. '/label',
  5244. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5245. ['div' => ['class' => 'checkbox']],
  5246. ['label' => ['for' => 'model-multi-field-a']],
  5247. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5248. 'first',
  5249. '/label',
  5250. '/div',
  5251. ['div' => ['class' => 'checkbox']],
  5252. ['label' => ['for' => 'model-multi-field-b']],
  5253. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5254. 'second',
  5255. '/label',
  5256. '/div',
  5257. ['div' => ['class' => 'checkbox']],
  5258. ['label' => ['for' => 'model-multi-field-c']],
  5259. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5260. 'third',
  5261. '/label',
  5262. '/div',
  5263. '/div'
  5264. ];
  5265. $this->assertHtml($expected, $result);
  5266. }
  5267. /**
  5268. * testSelectHiddenFieldOmission method
  5269. *
  5270. * Test that select() with 'hiddenField' => false omits the hidden field.
  5271. *
  5272. * @return void
  5273. */
  5274. public function testSelectHiddenFieldOmission()
  5275. {
  5276. $result = $this->Form->select(
  5277. 'Model.multi_field',
  5278. ['first', 'second'],
  5279. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5280. );
  5281. $this->assertNotContains('type="hidden"', $result);
  5282. }
  5283. /**
  5284. * testSelectCheckboxMultipleOverrideName method
  5285. *
  5286. * Test that select() with multiple = checkbox works with overriding name attribute.
  5287. *
  5288. * @return void
  5289. */
  5290. public function testSelectCheckboxMultipleOverrideName()
  5291. {
  5292. $result = $this->Form->select('category', ['1', '2'], [
  5293. 'multiple' => 'checkbox',
  5294. 'name' => 'fish',
  5295. ]);
  5296. $expected = [
  5297. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5298. ['div' => ['class' => 'checkbox']],
  5299. ['label' => ['for' => 'fish-0']],
  5300. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5301. '1',
  5302. '/label',
  5303. '/div',
  5304. ['div' => ['class' => 'checkbox']],
  5305. ['label' => ['for' => 'fish-1']],
  5306. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5307. '2',
  5308. '/label',
  5309. '/div'
  5310. ];
  5311. $this->assertHtml($expected, $result);
  5312. $result = $this->Form->multiCheckbox(
  5313. 'category',
  5314. new Collection(['1', '2']),
  5315. ['name' => 'fish']
  5316. );
  5317. $this->assertHtml($expected, $result);
  5318. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5319. 'name' => 'fish',
  5320. ]);
  5321. $this->assertHtml($expected, $result);
  5322. }
  5323. /**
  5324. * testControlMultiCheckbox method
  5325. *
  5326. * Test that control() works with multicheckbox.
  5327. *
  5328. * @return void
  5329. */
  5330. public function testControlMultiCheckbox()
  5331. {
  5332. $result = $this->Form->control('category', [
  5333. 'type' => 'multicheckbox',
  5334. 'options' => ['1', '2'],
  5335. ]);
  5336. $expected = [
  5337. ['div' => ['class' => 'input multicheckbox']],
  5338. '<label',
  5339. 'Category',
  5340. '/label',
  5341. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5342. ['div' => ['class' => 'checkbox']],
  5343. ['label' => ['for' => 'category-0']],
  5344. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5345. '1',
  5346. '/label',
  5347. '/div',
  5348. ['div' => ['class' => 'checkbox']],
  5349. ['label' => ['for' => 'category-1']],
  5350. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5351. '2',
  5352. '/label',
  5353. '/div',
  5354. '/div',
  5355. ];
  5356. $this->assertHtml($expected, $result);
  5357. }
  5358. /**
  5359. * testCheckbox method
  5360. *
  5361. * Test generation of checkboxes.
  5362. *
  5363. * @return void
  5364. */
  5365. public function testCheckbox()
  5366. {
  5367. $result = $this->Form->checkbox('Model.field');
  5368. $expected = [
  5369. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5370. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5371. ];
  5372. $this->assertHtml($expected, $result);
  5373. $result = $this->Form->checkbox('Model.field', [
  5374. 'id' => 'theID',
  5375. 'value' => 'myvalue',
  5376. 'form' => 'my-form',
  5377. ]);
  5378. $expected = [
  5379. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5380. ['input' => [
  5381. 'type' => 'checkbox', 'name' => 'Model[field]',
  5382. 'value' => 'myvalue', 'id' => 'theID',
  5383. 'form' => 'my-form',
  5384. ]]
  5385. ];
  5386. $this->assertHtml($expected, $result);
  5387. }
  5388. /**
  5389. * testCheckboxDefaultValue method
  5390. *
  5391. * Test default value setting on checkbox() method.
  5392. *
  5393. * @return void
  5394. */
  5395. public function testCheckboxDefaultValue()
  5396. {
  5397. $this->Form->request->data['Model']['field'] = false;
  5398. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5399. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5400. $this->assertHtml($expected, $result);
  5401. unset($this->Form->request->data['Model']['field']);
  5402. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5403. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5404. $this->assertHtml($expected, $result);
  5405. $this->Form->request->data['Model']['field'] = true;
  5406. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5407. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5408. $this->assertHtml($expected, $result);
  5409. unset($this->Form->request->data['Model']['field']);
  5410. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5411. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5412. $this->assertHtml($expected, $result);
  5413. $Articles = TableRegistry::get('Articles');
  5414. $Articles->schema()->addColumn(
  5415. 'published',
  5416. ['type' => 'boolean', 'null' => false, 'default' => true]
  5417. );
  5418. $this->Form->create($Articles->newEntity());
  5419. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5420. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5421. $this->assertHtml($expected, $result);
  5422. }
  5423. /**
  5424. * testCheckboxCheckedAndError method
  5425. *
  5426. * Test checkbox being checked or having errors.
  5427. *
  5428. * @return void
  5429. */
  5430. public function testCheckboxCheckedAndError()
  5431. {
  5432. $this->article['errors'] = [
  5433. 'published' => true
  5434. ];
  5435. $this->Form->request->data['published'] = 'myvalue';
  5436. $this->Form->create($this->article);
  5437. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5438. $expected = [
  5439. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5440. ['input' => [
  5441. 'type' => 'checkbox',
  5442. 'name' => 'published',
  5443. 'value' => 'myvalue',
  5444. 'id' => 'theID',
  5445. 'checked' => 'checked',
  5446. 'class' => 'form-error'
  5447. ]]
  5448. ];
  5449. $this->assertHtml($expected, $result);
  5450. $this->Form->request->data['published'] = '';
  5451. $result = $this->Form->checkbox('published');
  5452. $expected = [
  5453. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5454. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5455. ];
  5456. $this->assertHtml($expected, $result);
  5457. }
  5458. /**
  5459. * testCheckboxCustomNameAttribute method
  5460. *
  5461. * Test checkbox() with a custom name attribute.
  5462. *
  5463. * @return void
  5464. */
  5465. public function testCheckboxCustomNameAttribute()
  5466. {
  5467. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5468. $expected = [
  5469. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5470. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5471. ];
  5472. $this->assertHtml($expected, $result);
  5473. }
  5474. /**
  5475. * testCheckboxHiddenField method
  5476. *
  5477. * Test that the hidden input for checkboxes can be omitted or set to a
  5478. * specific value.
  5479. *
  5480. * @return void
  5481. */
  5482. public function testCheckboxHiddenField()
  5483. {
  5484. $result = $this->Form->checkbox('UserForm.something', [
  5485. 'hiddenField' => false
  5486. ]);
  5487. $expected = [
  5488. 'input' => [
  5489. 'type' => 'checkbox',
  5490. 'name' => 'UserForm[something]',
  5491. 'value' => '1'
  5492. ],
  5493. ];
  5494. $this->assertHtml($expected, $result);
  5495. $result = $this->Form->checkbox('UserForm.something', [
  5496. 'value' => 'Y',
  5497. 'hiddenField' => 'N',
  5498. ]);
  5499. $expected = [
  5500. ['input' => [
  5501. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5502. 'value' => 'N'
  5503. ]],
  5504. ['input' => [
  5505. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5506. 'value' => 'Y'
  5507. ]],
  5508. ];
  5509. $this->assertHtml($expected, $result);
  5510. }
  5511. /**
  5512. * testTime method
  5513. *
  5514. * Test the time type.
  5515. *
  5516. * @return void
  5517. */
  5518. public function testTime()
  5519. {
  5520. $result = $this->Form->time('start_time', [
  5521. 'timeFormat' => 12,
  5522. 'interval' => 5,
  5523. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5524. ]);
  5525. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5526. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5527. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5528. $this->assertNotContains('year', $result);
  5529. $this->assertNotContains('month', $result);
  5530. $this->assertNotContains('day', $result);
  5531. $result = $this->Form->time('start_time', [
  5532. 'timeFormat' => 12,
  5533. 'interval' => 5,
  5534. 'value' => '2014-03-08 16:30:00'
  5535. ]);
  5536. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5537. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5538. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5539. $this->assertNotContains('year', $result);
  5540. $this->assertNotContains('month', $result);
  5541. $this->assertNotContains('day', $result);
  5542. }
  5543. /**
  5544. * testTimeFormat24NoMeridian method
  5545. *
  5546. * Ensure that timeFormat=24 has no merdian.
  5547. *
  5548. * @return void.
  5549. */
  5550. public function testTimeFormat24NoMeridian()
  5551. {
  5552. $result = $this->Form->time('start_time', [
  5553. 'timeFormat' => 24,
  5554. 'interval' => 5,
  5555. 'value' => '2014-03-08 16:30:00'
  5556. ]);
  5557. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5558. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5559. $this->assertNotContains('meridian', $result);
  5560. $this->assertNotContains('pm', $result);
  5561. $this->assertNotContains('year', $result);
  5562. $this->assertNotContains('month', $result);
  5563. $this->assertNotContains('day', $result);
  5564. }
  5565. /**
  5566. * testDate method
  5567. *
  5568. * Test the date type.
  5569. *
  5570. * @return void
  5571. */
  5572. public function testDate()
  5573. {
  5574. $result = $this->Form->date('start_day', [
  5575. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5576. ]);
  5577. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5578. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5579. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5580. $this->assertNotContains('hour', $result);
  5581. $this->assertNotContains('minute', $result);
  5582. $this->assertNotContains('second', $result);
  5583. $this->assertNotContains('meridian', $result);
  5584. }
  5585. /**
  5586. * testDateTime method
  5587. *
  5588. * Test generation of date/time select elements.
  5589. *
  5590. * @return void
  5591. */
  5592. public function testDateTime()
  5593. {
  5594. extract($this->dateRegex);
  5595. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5596. $now = strtotime('now');
  5597. $expected = [
  5598. ['select' => ['name' => 'Contact[date][year]']],
  5599. ['option' => ['value' => '']],
  5600. '/option',
  5601. $yearsRegex,
  5602. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5603. date('Y', $now),
  5604. '/option',
  5605. '*/select',
  5606. ['select' => ['name' => 'Contact[date][month]']],
  5607. ['option' => ['value' => '']],
  5608. '/option',
  5609. $monthsRegex,
  5610. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5611. date('F', $now),
  5612. '/option',
  5613. '*/select',
  5614. ['select' => ['name' => 'Contact[date][day]']],
  5615. ['option' => ['value' => '']],
  5616. '/option',
  5617. $daysRegex,
  5618. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5619. date('j', $now),
  5620. '/option',
  5621. '*/select',
  5622. ['select' => ['name' => 'Contact[date][hour]']],
  5623. ['option' => ['value' => '']],
  5624. '/option',
  5625. $hoursRegex,
  5626. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5627. date('G', $now),
  5628. '/option',
  5629. '*/select',
  5630. ['select' => ['name' => 'Contact[date][minute]']],
  5631. ['option' => ['value' => '']],
  5632. '/option',
  5633. $minutesRegex,
  5634. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5635. date('i', $now),
  5636. '/option',
  5637. '*/select',
  5638. ];
  5639. $this->assertHtml($expected, $result);
  5640. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5641. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5642. $now = strtotime('now');
  5643. $expected = [
  5644. ['select' => ['name' => 'Contact[date][year]']],
  5645. $yearsRegex,
  5646. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5647. date('Y', $now),
  5648. '/option',
  5649. '*/select',
  5650. ['select' => ['name' => 'Contact[date][month]']],
  5651. $monthsRegex,
  5652. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5653. date('F', $now),
  5654. '/option',
  5655. '*/select',
  5656. ['select' => ['name' => 'Contact[date][day]']],
  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. $hoursRegex,
  5664. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5665. date('G', $now),
  5666. '/option',
  5667. '*/select',
  5668. ['select' => ['name' => 'Contact[date][minute]']],
  5669. $minutesRegex,
  5670. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5671. date('i', $now),
  5672. '/option',
  5673. '*/select',
  5674. ];
  5675. $this->assertHtml($expected, $result);
  5676. }
  5677. /**
  5678. * testDateTimeSecured method
  5679. *
  5680. * Test that datetime fields are added to protected fields list.
  5681. *
  5682. * @return void
  5683. */
  5684. public function testDateTimeSecured()
  5685. {
  5686. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5687. $this->Form->dateTime('Contact.date');
  5688. $expected = [
  5689. 'Contact.date.year',
  5690. 'Contact.date.month',
  5691. 'Contact.date.day',
  5692. 'Contact.date.hour',
  5693. 'Contact.date.minute',
  5694. ];
  5695. $this->assertEquals($expected, $this->Form->fields);
  5696. $this->Form->fields = [];
  5697. $this->Form->date('Contact.published');
  5698. $expected = [
  5699. 'Contact.published.year',
  5700. 'Contact.published.month',
  5701. 'Contact.published.day',
  5702. ];
  5703. $this->assertEquals($expected, $this->Form->fields);
  5704. }
  5705. /**
  5706. * testDateTimeSecuredDisabled method
  5707. *
  5708. * Test that datetime fields are added to protected fields list.
  5709. *
  5710. * @return void
  5711. */
  5712. public function testDateTimeSecuredDisabled()
  5713. {
  5714. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5715. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5716. $expected = [];
  5717. $this->assertEquals($expected, $this->Form->fields);
  5718. $this->Form->fields = [];
  5719. $this->Form->date('Contact.published', ['secure' => false]);
  5720. $expected = [];
  5721. $this->assertEquals($expected, $this->Form->fields);
  5722. }
  5723. /**
  5724. * testDatetimeEmpty method
  5725. *
  5726. * Test empty defaulting to true for datetime.
  5727. *
  5728. * @return void
  5729. */
  5730. public function testDatetimeEmpty()
  5731. {
  5732. extract($this->dateRegex);
  5733. $result = $this->Form->dateTime('Contact.date', [
  5734. 'timeFormat' => 12,
  5735. 'empty' => true,
  5736. 'default' => true
  5737. ]);
  5738. $expected = [
  5739. ['select' => ['name' => 'Contact[date][year]']],
  5740. $yearsRegex,
  5741. ['option' => ['value' => '']],
  5742. '/option',
  5743. '*/select',
  5744. ['select' => ['name' => 'Contact[date][month]']],
  5745. $monthsRegex,
  5746. ['option' => ['value' => '']],
  5747. '/option',
  5748. '*/select',
  5749. ['select' => ['name' => 'Contact[date][day]']],
  5750. $daysRegex,
  5751. ['option' => ['value' => '']],
  5752. '/option',
  5753. '*/select',
  5754. ['select' => ['name' => 'Contact[date][hour]']],
  5755. $hoursRegex,
  5756. ['option' => ['value' => '']],
  5757. '/option',
  5758. '*/select',
  5759. ['select' => ['name' => 'Contact[date][minute]']],
  5760. $minutesRegex,
  5761. ['option' => ['value' => '']],
  5762. '/option',
  5763. '*/select',
  5764. ['select' => ['name' => 'Contact[date][meridian]']],
  5765. $meridianRegex,
  5766. ['option' => ['value' => '']],
  5767. '/option',
  5768. '*/select'
  5769. ];
  5770. $this->assertHtml($expected, $result);
  5771. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5772. }
  5773. /**
  5774. * testDatetimeMinuteInterval method
  5775. *
  5776. * Test datetime with interval option.
  5777. *
  5778. * @return void
  5779. */
  5780. public function testDatetimeMinuteInterval()
  5781. {
  5782. extract($this->dateRegex);
  5783. $result = $this->Form->dateTime('Contact.date', [
  5784. 'interval' => 5,
  5785. 'value' => ''
  5786. ]);
  5787. $expected = [
  5788. ['select' => ['name' => 'Contact[date][year]']],
  5789. $yearsRegex,
  5790. ['option' => ['selected' => 'selected', 'value' => '']],
  5791. '/option',
  5792. '*/select',
  5793. ['select' => ['name' => 'Contact[date][month]']],
  5794. $monthsRegex,
  5795. ['option' => ['selected' => 'selected', 'value' => '']],
  5796. '/option',
  5797. '*/select',
  5798. ['select' => ['name' => 'Contact[date][day]']],
  5799. $daysRegex,
  5800. ['option' => ['selected' => 'selected', 'value' => '']],
  5801. '/option',
  5802. '*/select',
  5803. ['select' => ['name' => 'Contact[date][hour]']],
  5804. $hoursRegex,
  5805. ['option' => ['selected' => 'selected', 'value' => '']],
  5806. '/option',
  5807. '*/select',
  5808. ['select' => ['name' => 'Contact[date][minute]']],
  5809. $minutesRegex,
  5810. ['option' => ['selected' => 'selected', 'value' => '']],
  5811. '/option',
  5812. ['option' => ['value' => '00']],
  5813. '00',
  5814. '/option',
  5815. ['option' => ['value' => '05']],
  5816. '05',
  5817. '/option',
  5818. ['option' => ['value' => '10']],
  5819. '10',
  5820. '/option',
  5821. '*/select',
  5822. ];
  5823. $this->assertHtml($expected, $result);
  5824. }
  5825. /**
  5826. * testDateTimeRounding method
  5827. *
  5828. * Test dateTime with rounding.
  5829. *
  5830. * @return void
  5831. */
  5832. public function testDateTimeRounding()
  5833. {
  5834. $this->Form->request->data['Contact'] = [
  5835. 'date' => [
  5836. 'day' => '13',
  5837. 'month' => '12',
  5838. 'year' => '2010',
  5839. 'hour' => '04',
  5840. 'minute' => '19',
  5841. 'meridian' => 'AM'
  5842. ]
  5843. ];
  5844. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5845. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5846. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5847. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5848. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5849. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5850. }
  5851. /**
  5852. * testDatetimeWithDefault method
  5853. *
  5854. * Test that datetime() and default values work.
  5855. *
  5856. * @return void
  5857. */
  5858. public function testDatetimeWithDefault()
  5859. {
  5860. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5861. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5862. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5863. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5864. $result = $this->Form->dateTime('Contact.updated', [
  5865. 'default' => '2009-06-01 11:15:30'
  5866. ]);
  5867. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5868. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5869. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5870. }
  5871. /**
  5872. * testDateTimeAllZeros method
  5873. *
  5874. * Test datetime() with all zeros.
  5875. *
  5876. * @return void
  5877. */
  5878. public function testDateTimeAllZeros()
  5879. {
  5880. $result = $this->Form->dateTime('Contact.date', [
  5881. 'timeFormat' => false,
  5882. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5883. 'value' => '0000-00-00'
  5884. ]);
  5885. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5886. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5887. }
  5888. /**
  5889. * testDateTimeEmptyAsArray method
  5890. *
  5891. * @return void
  5892. */
  5893. public function testDateTimeEmptyAsArray()
  5894. {
  5895. $result = $this->Form->dateTime('Contact.date', [
  5896. 'empty' => [
  5897. 'day' => 'DAY',
  5898. 'month' => 'MONTH',
  5899. 'year' => 'YEAR',
  5900. 'hour' => 'HOUR',
  5901. 'minute' => 'MINUTE',
  5902. 'meridian' => false
  5903. ],
  5904. 'default' => true
  5905. ]);
  5906. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5907. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5908. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5909. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5910. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5911. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5912. $result = $this->Form->dateTime('Contact.date', [
  5913. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5914. 'default' => true
  5915. ]);
  5916. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5917. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5918. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5919. }
  5920. /**
  5921. * testFormDateTimeMulti method
  5922. *
  5923. * Test multiple datetime element generation.
  5924. *
  5925. * @return void
  5926. */
  5927. public function testFormDateTimeMulti()
  5928. {
  5929. extract($this->dateRegex);
  5930. $result = $this->Form->dateTime('Contact.1.updated');
  5931. $this->assertContains('Contact[1][updated][month]', $result);
  5932. $this->assertContains('Contact[1][updated][day]', $result);
  5933. $this->assertContains('Contact[1][updated][year]', $result);
  5934. $this->assertContains('Contact[1][updated][hour]', $result);
  5935. $this->assertContains('Contact[1][updated][minute]', $result);
  5936. }
  5937. /**
  5938. * testDateTimeLabelIdMatchesFirstControl method
  5939. *
  5940. * When changing the date format, the label should always focus the first select box when
  5941. * clicked.
  5942. *
  5943. * @return void
  5944. */
  5945. public function testDateTimeLabelIdMatchesFirstControl()
  5946. {
  5947. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5948. $this->assertContains('<label>Date</label>', $result);
  5949. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5950. $this->assertContains('<label>Date</label>', $result);
  5951. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5952. $this->assertContains('<label>Date</label>', $result);
  5953. }
  5954. /**
  5955. * testDateTimeSecondOptions method
  5956. *
  5957. * Test datetime second=true.
  5958. *
  5959. * @return void
  5960. */
  5961. public function testDateTimeSecondOptions()
  5962. {
  5963. $result = $this->Form->dateTime('updated', ['second' => true]);
  5964. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5965. $result = $this->Form->dateTime('updated', ['second' => []]);
  5966. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5967. $result = $this->Form->dateTime('updated', ['second' => null]);
  5968. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5969. $result = $this->Form->dateTime('updated', ['second' => false]);
  5970. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5971. }
  5972. /**
  5973. * testMonth method
  5974. *
  5975. * Test generation of a month input.
  5976. *
  5977. * @return void
  5978. */
  5979. public function testMonth()
  5980. {
  5981. $result = $this->Form->month('Model.field', ['value' => '']);
  5982. $expected = [
  5983. ['select' => ['name' => 'Model[field][month]']],
  5984. ['option' => ['value' => '', 'selected' => 'selected']],
  5985. '/option',
  5986. ['option' => ['value' => '01']],
  5987. date('F', strtotime('2008-01-01 00:00:00')),
  5988. '/option',
  5989. ['option' => ['value' => '02']],
  5990. date('F', strtotime('2008-02-01 00:00:00')),
  5991. '/option',
  5992. '*/select',
  5993. ];
  5994. $this->assertHtml($expected, $result);
  5995. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5996. $expected = [
  5997. ['select' => ['name' => 'Model[field][month]']],
  5998. ['option' => ['selected' => 'selected', 'value' => '']],
  5999. '/option',
  6000. ['option' => ['value' => '01']],
  6001. date('F', strtotime('2008-01-01 00:00:00')),
  6002. '/option',
  6003. ['option' => ['value' => '02']],
  6004. date('F', strtotime('2008-02-01 00:00:00')),
  6005. '/option',
  6006. '*/select',
  6007. ];
  6008. $this->assertHtml($expected, $result);
  6009. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6010. $expected = [
  6011. ['select' => ['name' => 'Model[field][month]']],
  6012. ['option' => ['selected' => 'selected', 'value' => '']],
  6013. '/option',
  6014. ['option' => ['value' => '01']],
  6015. '1',
  6016. '/option',
  6017. ['option' => ['value' => '02']],
  6018. '2',
  6019. '/option',
  6020. '*/select',
  6021. ];
  6022. $this->assertHtml($expected, $result);
  6023. $monthNames = [
  6024. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6025. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6026. ];
  6027. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6028. $expected = [
  6029. ['select' => ['name' => 'Model[field][month]']],
  6030. ['option' => ['value' => '']],
  6031. '/option',
  6032. ['option' => ['value' => '01', 'selected' => 'selected']],
  6033. 'Jan',
  6034. '/option',
  6035. ['option' => ['value' => '02']],
  6036. 'Feb',
  6037. '/option',
  6038. '*/select',
  6039. ];
  6040. $this->assertHtml($expected, $result);
  6041. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6042. $result = $this->Form->month('Project.release');
  6043. $expected = [
  6044. ['select' => ['name' => 'Project[release][month]']],
  6045. ['option' => ['value' => '']],
  6046. '/option',
  6047. ['option' => ['value' => '01']],
  6048. 'January',
  6049. '/option',
  6050. ['option' => ['value' => '02', 'selected' => 'selected']],
  6051. 'February',
  6052. '/option',
  6053. '*/select',
  6054. ];
  6055. $this->assertHtml($expected, $result);
  6056. $result = $this->Form->month('Contact.published', [
  6057. 'empty' => 'Published on',
  6058. ]);
  6059. $this->assertContains('Published on', $result);
  6060. }
  6061. /**
  6062. * testDay method
  6063. *
  6064. * Test generation of a day input.
  6065. *
  6066. * @return void
  6067. */
  6068. public function testDay()
  6069. {
  6070. extract($this->dateRegex);
  6071. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6072. $expected = [
  6073. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6074. ['option' => ['selected' => 'selected', 'value' => '']],
  6075. '/option',
  6076. ['option' => ['value' => '01']],
  6077. '1',
  6078. '/option',
  6079. ['option' => ['value' => '02']],
  6080. '2',
  6081. '/option',
  6082. $daysRegex,
  6083. '/select',
  6084. ];
  6085. $this->assertHtml($expected, $result);
  6086. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6087. $result = $this->Form->day('Model.field');
  6088. $expected = [
  6089. ['select' => ['name' => 'Model[field][day]']],
  6090. ['option' => ['value' => '']],
  6091. '/option',
  6092. ['option' => ['value' => '01']],
  6093. '1',
  6094. '/option',
  6095. ['option' => ['value' => '02']],
  6096. '2',
  6097. '/option',
  6098. $daysRegex,
  6099. ['option' => ['value' => '10', 'selected' => 'selected']],
  6100. '10',
  6101. '/option',
  6102. $daysRegex,
  6103. '/select',
  6104. ];
  6105. $this->assertHtml($expected, $result);
  6106. $this->Form->request->data['Model']['field'] = '';
  6107. $result = $this->Form->day('Model.field', ['value' => '10']);
  6108. $expected = [
  6109. ['select' => ['name' => 'Model[field][day]']],
  6110. ['option' => ['value' => '']],
  6111. '/option',
  6112. ['option' => ['value' => '01']],
  6113. '1',
  6114. '/option',
  6115. ['option' => ['value' => '02']],
  6116. '2',
  6117. '/option',
  6118. $daysRegex,
  6119. ['option' => ['value' => '10', 'selected' => 'selected']],
  6120. '10',
  6121. '/option',
  6122. $daysRegex,
  6123. '/select',
  6124. ];
  6125. $this->assertHtml($expected, $result);
  6126. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6127. $result = $this->Form->day('Project.release');
  6128. $expected = [
  6129. ['select' => ['name' => 'Project[release][day]']],
  6130. ['option' => ['value' => '']],
  6131. '/option',
  6132. ['option' => ['value' => '01']],
  6133. '1',
  6134. '/option',
  6135. ['option' => ['value' => '02']],
  6136. '2',
  6137. '/option',
  6138. $daysRegex,
  6139. ['option' => ['value' => '10', 'selected' => 'selected']],
  6140. '10',
  6141. '/option',
  6142. $daysRegex,
  6143. '/select',
  6144. ];
  6145. $this->assertHtml($expected, $result);
  6146. $result = $this->Form->day('Contact.published', [
  6147. 'empty' => 'Published on',
  6148. ]);
  6149. $this->assertContains('Published on', $result);
  6150. }
  6151. /**
  6152. * testMinute method
  6153. *
  6154. * Test generation of a minute input.
  6155. *
  6156. * @return void
  6157. */
  6158. public function testMinute()
  6159. {
  6160. extract($this->dateRegex);
  6161. $result = $this->Form->minute('Model.field', ['value' => '']);
  6162. $expected = [
  6163. ['select' => ['name' => 'Model[field][minute]']],
  6164. ['option' => ['selected' => 'selected', 'value' => '']],
  6165. '/option',
  6166. ['option' => ['value' => '00']],
  6167. '00',
  6168. '/option',
  6169. ['option' => ['value' => '01']],
  6170. '01',
  6171. '/option',
  6172. ['option' => ['value' => '02']],
  6173. '02',
  6174. '/option',
  6175. $minutesRegex,
  6176. '/select',
  6177. ];
  6178. $this->assertHtml($expected, $result);
  6179. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6180. $result = $this->Form->minute('Model.field');
  6181. $expected = [
  6182. ['select' => ['name' => 'Model[field][minute]']],
  6183. ['option' => ['value' => '']],
  6184. '/option',
  6185. ['option' => ['value' => '00']],
  6186. '00',
  6187. '/option',
  6188. ['option' => ['value' => '01']],
  6189. '01',
  6190. '/option',
  6191. ['option' => ['value' => '02']],
  6192. '02',
  6193. '/option',
  6194. $minutesRegex,
  6195. ['option' => ['value' => '12', 'selected' => 'selected']],
  6196. '12',
  6197. '/option',
  6198. $minutesRegex,
  6199. '/select',
  6200. ];
  6201. $this->assertHtml($expected, $result);
  6202. $this->Form->request->data['Model']['field'] = '';
  6203. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6204. $expected = [
  6205. ['select' => ['name' => 'Model[field][minute]']],
  6206. ['option' => ['selected' => 'selected', 'value' => '']],
  6207. '/option',
  6208. ['option' => ['value' => '00']],
  6209. '00',
  6210. '/option',
  6211. ['option' => ['value' => '05']],
  6212. '05',
  6213. '/option',
  6214. ['option' => ['value' => '10']],
  6215. '10',
  6216. '/option',
  6217. $minutesRegex,
  6218. '/select',
  6219. ];
  6220. $this->assertHtml($expected, $result);
  6221. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6222. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6223. $expected = [
  6224. ['select' => ['name' => 'Model[field][minute]']],
  6225. ['option' => ['value' => '']],
  6226. '/option',
  6227. ['option' => ['value' => '00']],
  6228. '00',
  6229. '/option',
  6230. ['option' => ['value' => '05']],
  6231. '05',
  6232. '/option',
  6233. ['option' => ['value' => '10', 'selected' => 'selected']],
  6234. '10',
  6235. '/option',
  6236. $minutesRegex,
  6237. '/select',
  6238. ];
  6239. $this->assertHtml($expected, $result);
  6240. }
  6241. /**
  6242. * testMeridian method
  6243. *
  6244. * Test generating an input for the meridian.
  6245. *
  6246. * @return void
  6247. */
  6248. public function testMeridian()
  6249. {
  6250. extract($this->dateRegex);
  6251. $now = new \DateTime();
  6252. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6253. $expected = [
  6254. ['select' => ['name' => 'Model[field][meridian]']],
  6255. ['option' => ['value' => '']],
  6256. '/option',
  6257. $meridianRegex,
  6258. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6259. $now->format('a'),
  6260. '/option',
  6261. '*/select'
  6262. ];
  6263. $this->assertHtml($expected, $result);
  6264. }
  6265. /**
  6266. * testHour method
  6267. *
  6268. * Test generation of an hour input.
  6269. *
  6270. * @return void
  6271. */
  6272. public function testHour()
  6273. {
  6274. extract($this->dateRegex);
  6275. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6276. $expected = [
  6277. ['select' => ['name' => 'Model[field][hour]']],
  6278. ['option' => ['selected' => 'selected', 'value' => '']],
  6279. '/option',
  6280. ['option' => ['value' => '01']],
  6281. '1',
  6282. '/option',
  6283. ['option' => ['value' => '02']],
  6284. '2',
  6285. '/option',
  6286. $hoursRegex,
  6287. '/select',
  6288. ];
  6289. $this->assertHtml($expected, $result);
  6290. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6291. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6292. $expected = [
  6293. ['select' => ['name' => 'Model[field][hour]']],
  6294. ['option' => ['value' => '']],
  6295. '/option',
  6296. ['option' => ['value' => '01']],
  6297. '1',
  6298. '/option',
  6299. ['option' => ['value' => '02']],
  6300. '2',
  6301. '/option',
  6302. $hoursRegex,
  6303. ['option' => ['value' => '12', 'selected' => 'selected']],
  6304. '12',
  6305. '/option',
  6306. '/select',
  6307. ];
  6308. $this->assertHtml($expected, $result);
  6309. $this->Form->request->data['Model']['field'] = '';
  6310. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6311. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6312. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6313. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6314. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6315. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6316. $expected = [
  6317. ['select' => ['name' => 'Model[field][hour]']],
  6318. ['option' => ['value' => '']],
  6319. '/option',
  6320. ['option' => ['value' => '00', 'selected' => 'selected']],
  6321. '0',
  6322. '/option',
  6323. ['option' => ['value' => '01']],
  6324. '1',
  6325. '/option',
  6326. ['option' => ['value' => '02']],
  6327. '2',
  6328. '/option',
  6329. $hoursRegex,
  6330. '/select',
  6331. ];
  6332. $this->assertHtml($expected, $result);
  6333. unset($this->Form->request->data['Model']['field']);
  6334. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6335. $thisHour = date('H');
  6336. $optValue = date('G');
  6337. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6338. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6339. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6340. $expected = [
  6341. ['select' => ['name' => 'Model[field][hour]']],
  6342. ['option' => ['value' => '']],
  6343. '/option',
  6344. ['option' => ['value' => '00']],
  6345. '0',
  6346. '/option',
  6347. ['option' => ['value' => '01', 'selected' => 'selected']],
  6348. '1',
  6349. '/option',
  6350. ['option' => ['value' => '02']],
  6351. '2',
  6352. '/option',
  6353. $hoursRegex,
  6354. '/select',
  6355. ];
  6356. $this->assertHtml($expected, $result);
  6357. }
  6358. /**
  6359. * testYear method
  6360. *
  6361. * Test generation of a year input.
  6362. *
  6363. * @return void
  6364. */
  6365. public function testYear()
  6366. {
  6367. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6368. $expected = [
  6369. ['select' => ['name' => 'Model[field][year]']],
  6370. ['option' => ['selected' => 'selected', 'value' => '']],
  6371. '/option',
  6372. ['option' => ['value' => '2007']],
  6373. '2007',
  6374. '/option',
  6375. ['option' => ['value' => '2006']],
  6376. '2006',
  6377. '/option',
  6378. '/select',
  6379. ];
  6380. $this->assertHtml($expected, $result);
  6381. $result = $this->Form->year('Model.field', [
  6382. 'value' => '',
  6383. 'minYear' => 2006,
  6384. 'maxYear' => 2007,
  6385. 'orderYear' => 'asc'
  6386. ]);
  6387. $expected = [
  6388. ['select' => ['name' => 'Model[field][year]']],
  6389. ['option' => ['selected' => 'selected', 'value' => '']],
  6390. '/option',
  6391. ['option' => ['value' => '2006']],
  6392. '2006',
  6393. '/option',
  6394. ['option' => ['value' => '2007']],
  6395. '2007',
  6396. '/option',
  6397. '/select',
  6398. ];
  6399. $this->assertHtml($expected, $result);
  6400. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6401. $result = $this->Form->year('Contact.published', [
  6402. 'empty' => false,
  6403. 'minYear' => 2006,
  6404. 'maxYear' => 2007,
  6405. ]);
  6406. $expected = [
  6407. ['select' => ['name' => 'Contact[published][year]']],
  6408. ['option' => ['value' => '2007']],
  6409. '2007',
  6410. '/option',
  6411. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6412. '2006',
  6413. '/option',
  6414. '/select',
  6415. ];
  6416. $this->assertHtml($expected, $result);
  6417. $result = $this->Form->year('Contact.published', [
  6418. 'empty' => 'Published on',
  6419. ]);
  6420. $this->assertContains('Published on', $result);
  6421. }
  6422. /**
  6423. * testControlDatetimePreEpoch method
  6424. *
  6425. * Test minYear being prior to the unix epoch.
  6426. *
  6427. * @return void
  6428. */
  6429. public function testControlDatetimePreEpoch()
  6430. {
  6431. $start = date('Y') - 80;
  6432. $end = date('Y') - 18;
  6433. $result = $this->Form->control('birth_year', [
  6434. 'type' => 'date',
  6435. 'label' => 'Birth Year',
  6436. 'minYear' => $start,
  6437. 'maxYear' => $end,
  6438. 'month' => false,
  6439. 'day' => false,
  6440. ]);
  6441. $this->assertContains('value="' . $start . '">' . $start, $result);
  6442. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6443. $this->assertNotContains('value="00">00', $result);
  6444. }
  6445. /**
  6446. * testYearAutoExpandRange method
  6447. *
  6448. * @return void
  6449. */
  6450. public function testYearAutoExpandRange()
  6451. {
  6452. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6453. $result = $this->Form->year('User.birthday');
  6454. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6455. $result = $matches[1];
  6456. $expected = range(date('Y') + 5, 1930);
  6457. $this->assertEquals($expected, $result);
  6458. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6459. $result = $this->Form->year('Project.release');
  6460. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6461. $result = $matches[1];
  6462. $expected = range(2050, date('Y') - 5);
  6463. $this->assertEquals($expected, $result);
  6464. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6465. $result = $this->Form->year('Project.release', [
  6466. 'minYear' => 1890,
  6467. 'maxYear' => 1900
  6468. ]);
  6469. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6470. $result = $matches[1];
  6471. $expected = range(1900, 1881);
  6472. $this->assertEquals($expected, $result);
  6473. }
  6474. /**
  6475. * testControlDate method
  6476. *
  6477. * Test that control() accepts the type of date and passes options in.
  6478. *
  6479. * @return void
  6480. */
  6481. public function testControlDate()
  6482. {
  6483. $this->Form->request->data = [
  6484. 'month_year' => ['month' => date('m')],
  6485. ];
  6486. $this->Form->create($this->article);
  6487. $result = $this->Form->control('month_year', [
  6488. 'label' => false,
  6489. 'type' => 'date',
  6490. 'minYear' => 2006,
  6491. 'maxYear' => 2008
  6492. ]);
  6493. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6494. $this->assertNotContains('value="2008" selected="selected"', $result);
  6495. }
  6496. /**
  6497. * testControlLabelFalse method
  6498. *
  6499. * Test the label option being set to false.
  6500. *
  6501. * @return void
  6502. */
  6503. public function testControlLabelFalse()
  6504. {
  6505. $this->Form->create($this->article);
  6506. $result = $this->Form->control('title', ['label' => false]);
  6507. $expected = [
  6508. 'div' => ['class' => 'input text required'],
  6509. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6510. '/div'
  6511. ];
  6512. $this->assertHtml($expected, $result);
  6513. }
  6514. /**
  6515. * testControlDateMaxYear method
  6516. *
  6517. * Let's say we want to only allow users born from 2006 to 2008 to register
  6518. * This being the first signup page, we still don't have any data.
  6519. *
  6520. * @return void
  6521. */
  6522. public function testControlDateMaxYear()
  6523. {
  6524. $this->Form->request->data = [];
  6525. $this->Form->create($this->article);
  6526. $result = $this->Form->control('birthday', [
  6527. 'label' => false,
  6528. 'type' => 'date',
  6529. 'minYear' => 2006,
  6530. 'maxYear' => 2008,
  6531. 'default' => true
  6532. ]);
  6533. $this->assertContains('value="2008" selected="selected"', $result);
  6534. $this->assertContains('value="2006"', $result);
  6535. $this->assertNotContains('value="2005"', $result);
  6536. $this->assertNotContains('value="2009"', $result);
  6537. }
  6538. /**
  6539. * testTextArea method
  6540. *
  6541. * Test generation of a textarea input.
  6542. *
  6543. * @return void
  6544. */
  6545. public function testTextArea()
  6546. {
  6547. $this->Form->request->data = ['field' => 'some test data'];
  6548. $result = $this->Form->textarea('field');
  6549. $expected = [
  6550. 'textarea' => ['name' => 'field', 'rows' => 5],
  6551. 'some test data',
  6552. '/textarea',
  6553. ];
  6554. $this->assertHtml($expected, $result);
  6555. $result = $this->Form->textarea('user.bio');
  6556. $expected = [
  6557. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6558. '/textarea',
  6559. ];
  6560. $this->assertHtml($expected, $result);
  6561. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6562. $result = $this->Form->textarea('field');
  6563. $expected = [
  6564. 'textarea' => ['name' => 'field', 'rows' => 5],
  6565. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6566. '/textarea',
  6567. ];
  6568. $this->assertHtml($expected, $result);
  6569. $this->Form->request->data = [
  6570. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6571. ];
  6572. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6573. $expected = [
  6574. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6575. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6576. '/textarea',
  6577. ];
  6578. $this->assertHtml($expected, $result);
  6579. $result = $this->Form->textarea('0.OtherModel.field');
  6580. $expected = [
  6581. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6582. '/textarea'
  6583. ];
  6584. $this->assertHtml($expected, $result);
  6585. }
  6586. /**
  6587. * testTextAreaWithStupidCharacters method
  6588. *
  6589. * Test text area with non-ascii characters.
  6590. *
  6591. * @return void
  6592. */
  6593. public function testTextAreaWithStupidCharacters()
  6594. {
  6595. $result = $this->Form->textarea('Post.content', [
  6596. 'value' => "GREAT®",
  6597. 'rows' => '15',
  6598. 'cols' => '75'
  6599. ]);
  6600. $expected = [
  6601. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6602. 'GREAT®',
  6603. '/textarea',
  6604. ];
  6605. $this->assertHtml($expected, $result);
  6606. }
  6607. /**
  6608. * testTextAreaMaxLength method
  6609. *
  6610. * Test textareas maxlength read from schema.
  6611. *
  6612. * @return void
  6613. */
  6614. public function testTextAreaMaxLength()
  6615. {
  6616. $this->Form->create([
  6617. 'schema' => [
  6618. 'stuff' => ['type' => 'string', 'length' => 10],
  6619. ]
  6620. ]);
  6621. $result = $this->Form->control('other', ['type' => 'textarea']);
  6622. $expected = [
  6623. 'div' => ['class' => 'input textarea'],
  6624. 'label' => ['for' => 'other'],
  6625. 'Other',
  6626. '/label',
  6627. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6628. '/textarea',
  6629. '/div'
  6630. ];
  6631. $this->assertHtml($expected, $result);
  6632. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6633. $expected = [
  6634. 'div' => ['class' => 'input textarea'],
  6635. 'label' => ['for' => 'stuff'],
  6636. 'Stuff',
  6637. '/label',
  6638. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6639. '/textarea',
  6640. '/div'
  6641. ];
  6642. $this->assertHtml($expected, $result);
  6643. }
  6644. /**
  6645. * testHiddenField method
  6646. *
  6647. * Test generation of a hidden input.
  6648. *
  6649. * @return void
  6650. */
  6651. public function testHiddenField()
  6652. {
  6653. $this->article['errors'] = [
  6654. 'field' => true
  6655. ];
  6656. $this->Form->request->data['field'] = 'test';
  6657. $this->Form->create($this->article);
  6658. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6659. $expected = [
  6660. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6661. $this->assertHtml($expected, $result);
  6662. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6663. $expected = [
  6664. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6665. ];
  6666. $this->assertHtml($expected, $result);
  6667. }
  6668. /**
  6669. * testFileUploadField method
  6670. *
  6671. * Test generation of a file upload input.
  6672. *
  6673. * @return void
  6674. */
  6675. public function testFileUploadField()
  6676. {
  6677. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6678. $result = $this->Form->file('Model.upload');
  6679. $this->assertHtml($expected, $result);
  6680. $this->Form->request->data['Model']['upload'] = [
  6681. 'name' => '', 'type' => '', 'tmp_name' => '',
  6682. 'error' => 4, 'size' => 0
  6683. ];
  6684. $result = $this->Form->file('Model.upload');
  6685. $this->assertHtml($expected, $result);
  6686. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6687. $result = $this->Form->file('Model.upload');
  6688. $this->assertHtml($expected, $result);
  6689. }
  6690. /**
  6691. * testFileUploadOnOtherModel method
  6692. *
  6693. * Test File upload input on a model not used in create().
  6694. *
  6695. * @return void
  6696. */
  6697. public function testFileUploadOnOtherModel()
  6698. {
  6699. $this->Form->create($this->article, ['type' => 'file']);
  6700. $result = $this->Form->file('ValidateProfile.city');
  6701. $expected = [
  6702. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6703. ];
  6704. $this->assertHtml($expected, $result);
  6705. }
  6706. /**
  6707. * testButton method
  6708. *
  6709. * Test generation of a form button.
  6710. *
  6711. * @return void
  6712. */
  6713. public function testButton()
  6714. {
  6715. $result = $this->Form->button('Hi');
  6716. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6717. $this->assertHtml($expected, $result);
  6718. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6719. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6720. $this->assertHtml($expected, $result);
  6721. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6722. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6723. $this->assertHtml($expected, $result);
  6724. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6725. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6726. $this->assertHtml($expected, $result);
  6727. $result = $this->Form->button('No type', ['type' => false]);
  6728. $expected = ['button' => [], 'No type', '/button'];
  6729. $this->assertHtml($expected, $result);
  6730. $result = $this->Form->button('Upload Text', [
  6731. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6732. 'escape' => false
  6733. ]);
  6734. $this->assertNotRegExp('/\&039/', $result);
  6735. }
  6736. /**
  6737. * testButtonUnlockedByDefault method
  6738. *
  6739. * Test that button() makes unlocked fields by default.
  6740. *
  6741. * @return void
  6742. */
  6743. public function testButtonUnlockedByDefault()
  6744. {
  6745. $this->Form->request->params['_csrfToken'] = 'secured';
  6746. $this->Form->button('Save', ['name' => 'save']);
  6747. $this->Form->button('Clear');
  6748. $result = $this->Form->unlockField();
  6749. $this->assertEquals(['save'], $result);
  6750. }
  6751. /**
  6752. * testPostButton method
  6753. *
  6754. * @return void
  6755. */
  6756. public function testPostButton()
  6757. {
  6758. $result = $this->Form->postButton('Hi', '/controller/action');
  6759. $expected = [
  6760. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6761. 'div' => ['style' => 'display:none;'],
  6762. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6763. '/div',
  6764. 'button' => ['type' => 'submit'],
  6765. 'Hi',
  6766. '/button',
  6767. '/form'
  6768. ];
  6769. $this->assertHtml($expected, $result);
  6770. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6771. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6772. }
  6773. /**
  6774. * testPostButtonMethodType method
  6775. *
  6776. * @return void
  6777. */
  6778. public function testPostButtonMethodType()
  6779. {
  6780. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6781. $expected = [
  6782. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6783. 'div' => ['style' => 'display:none;'],
  6784. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6785. '/div',
  6786. 'button' => ['type' => 'submit'],
  6787. 'Hi',
  6788. '/button',
  6789. '/form'
  6790. ];
  6791. $this->assertHtml($expected, $result);
  6792. }
  6793. /**
  6794. * testPostButtonFormOptions method
  6795. *
  6796. * @return void
  6797. */
  6798. public function testPostButtonFormOptions()
  6799. {
  6800. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6801. $expected = [
  6802. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6803. 'div' => ['style' => 'display:none;'],
  6804. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6805. '/div',
  6806. 'button' => ['type' => 'submit'],
  6807. 'Hi',
  6808. '/button',
  6809. '/form'
  6810. ];
  6811. $this->assertHtml($expected, $result);
  6812. }
  6813. /**
  6814. * testPostButtonNestedData method
  6815. *
  6816. * Test using postButton with N dimensional data.
  6817. *
  6818. * @return void
  6819. */
  6820. public function testPostButtonNestedData()
  6821. {
  6822. $data = [
  6823. 'one' => [
  6824. 'two' => [
  6825. 3, 4, 5
  6826. ]
  6827. ]
  6828. ];
  6829. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6830. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6831. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6832. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6833. }
  6834. /**
  6835. * testSecurePostButton method
  6836. *
  6837. * Test that postButton adds _Token fields.
  6838. *
  6839. * @return void
  6840. */
  6841. public function testSecurePostButton()
  6842. {
  6843. $this->Form->request->params['_csrfToken'] = 'testkey';
  6844. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6845. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6846. $tokenDebug = urlencode(json_encode([
  6847. '/posts/delete/1',
  6848. [],
  6849. []
  6850. ]));
  6851. $expected = [
  6852. 'form' => [
  6853. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6854. ],
  6855. ['div' => ['style' => 'display:none;']],
  6856. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6857. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6858. '/div',
  6859. 'button' => ['type' => 'submit'],
  6860. 'Delete',
  6861. '/button',
  6862. ['div' => ['style' => 'display:none;']],
  6863. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6864. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6865. ['input' => [
  6866. 'type' => 'hidden',
  6867. 'name' => '_Token[debug]',
  6868. 'value' => $tokenDebug,
  6869. 'autocomplete' => 'off',
  6870. ]],
  6871. '/div',
  6872. '/form',
  6873. ];
  6874. $this->assertHtml($expected, $result);
  6875. }
  6876. /**
  6877. * testPostLink method
  6878. *
  6879. * @return void
  6880. */
  6881. public function testPostLink()
  6882. {
  6883. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6884. $expected = [
  6885. 'form' => [
  6886. 'method' => 'post', 'action' => '/posts/delete/1',
  6887. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6888. ],
  6889. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6890. '/form',
  6891. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6892. 'Delete',
  6893. '/a'
  6894. ];
  6895. $this->assertHtml($expected, $result);
  6896. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6897. $expected = [
  6898. 'form' => [
  6899. 'method' => 'post', 'action' => '/posts/delete/1',
  6900. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6901. ],
  6902. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6903. '/form',
  6904. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6905. 'Delete',
  6906. '/a'
  6907. ];
  6908. $this->assertHtml($expected, $result);
  6909. $result = $this->Form->postLink(
  6910. 'Delete',
  6911. '/posts/delete/1',
  6912. ['target' => '_blank', 'class' => 'btn btn-danger']
  6913. );
  6914. $expected = [
  6915. 'form' => [
  6916. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6917. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6918. ],
  6919. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6920. '/form',
  6921. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6922. 'Delete',
  6923. '/a'
  6924. ];
  6925. $this->assertHtml($expected, $result);
  6926. }
  6927. /**
  6928. * testPostLinkWithConfirm method
  6929. *
  6930. * Test the confirm option for postLink().
  6931. *
  6932. * @return void
  6933. */
  6934. public function testPostLinkWithConfirm()
  6935. {
  6936. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6937. $expected = [
  6938. 'form' => [
  6939. 'method' => 'post', 'action' => '/posts/delete/1',
  6940. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6941. ],
  6942. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6943. '/form',
  6944. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6945. 'Delete',
  6946. '/a'
  6947. ];
  6948. $this->assertHtml($expected, $result);
  6949. $result = $this->Form->postLink(
  6950. 'Delete',
  6951. '/posts/delete/1',
  6952. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6953. );
  6954. $expected = [
  6955. 'form' => [
  6956. 'method' => 'post', 'action' => '/posts/delete/1',
  6957. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6958. ],
  6959. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6960. '/form',
  6961. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6962. 'Delete',
  6963. '/a'
  6964. ];
  6965. $this->assertHtml($expected, $result);
  6966. }
  6967. /**
  6968. * testPostLinkWithQuery method
  6969. *
  6970. * Test postLink() with query string args.
  6971. *
  6972. * @return void
  6973. */
  6974. public function testPostLinkWithQuery()
  6975. {
  6976. $result = $this->Form->postLink(
  6977. 'Delete',
  6978. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6979. );
  6980. $expected = [
  6981. 'form' => [
  6982. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6983. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6984. ],
  6985. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6986. '/form',
  6987. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6988. 'Delete',
  6989. '/a'
  6990. ];
  6991. $this->assertHtml($expected, $result);
  6992. }
  6993. /**
  6994. * testPostLinkWithData method
  6995. *
  6996. * Test postLink with additional data.
  6997. *
  6998. * @return void
  6999. */
  7000. public function testPostLinkWithData()
  7001. {
  7002. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7003. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7004. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7005. $this->Form->create($entity);
  7006. $this->Form->end();
  7007. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7008. $this->assertContains(
  7009. '<input type="hidden" name="name" value="show"',
  7010. $result,
  7011. 'should not contain entity data.'
  7012. );
  7013. }
  7014. /**
  7015. * testPostLinkSecurityHash method
  7016. *
  7017. * Test that security hashes for postLink include the url.
  7018. *
  7019. * @return void
  7020. */
  7021. public function testPostLinkSecurityHash()
  7022. {
  7023. $hash = Security::hash(
  7024. '/posts/delete/1' .
  7025. serialize(['id' => '1']) .
  7026. '' .
  7027. Security::salt()
  7028. );
  7029. $hash .= '%3Aid';
  7030. $this->Form->request->params['_Token']['key'] = 'test';
  7031. $result = $this->Form->postLink(
  7032. 'Delete',
  7033. '/posts/delete/1',
  7034. ['data' => ['id' => 1]]
  7035. );
  7036. $tokenDebug = urlencode(json_encode([
  7037. '/posts/delete/1',
  7038. [
  7039. 'id' => 1
  7040. ],
  7041. []
  7042. ]));
  7043. $expected = [
  7044. 'form' => [
  7045. 'method' => 'post', 'action' => '/posts/delete/1',
  7046. 'name', 'style' => 'display:none;'
  7047. ],
  7048. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7049. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7050. 'div' => ['style' => 'display:none;'],
  7051. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7052. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7053. ['input' => [
  7054. 'type' => 'hidden',
  7055. 'name' => '_Token[debug]',
  7056. 'value' => $tokenDebug,
  7057. 'autocomplete' => 'off',
  7058. ]],
  7059. '/div',
  7060. '/form',
  7061. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7062. 'Delete',
  7063. '/a'
  7064. ];
  7065. $this->assertHtml($expected, $result);
  7066. }
  7067. /**
  7068. * testPostLinkSecurityHashBlockMode method
  7069. *
  7070. * Test that postLink doesn't modify the fields in the containing form.
  7071. *
  7072. * postLink() calls inside open forms should not modify the field list
  7073. * for the form.
  7074. *
  7075. * @return void
  7076. */
  7077. public function testPostLinkSecurityHashBlockMode()
  7078. {
  7079. $hash = Security::hash(
  7080. '/posts/delete/1' .
  7081. serialize([]) .
  7082. '' .
  7083. Security::salt()
  7084. );
  7085. $hash .= '%3A';
  7086. $this->Form->request->params['_Token']['key'] = 'test';
  7087. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7088. $this->Form->control('title');
  7089. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7090. $result = $this->View->fetch('postLink');
  7091. $this->assertEquals(['title'], $this->Form->fields);
  7092. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7093. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7094. }
  7095. /**
  7096. * testPostLinkSecurityHashNoDebugMode method
  7097. *
  7098. * Test that security does not include debug token if debug is false.
  7099. *
  7100. * @return void
  7101. */
  7102. public function testPostLinkSecurityHashNoDebugMode()
  7103. {
  7104. Configure::write('debug', false);
  7105. $hash = Security::hash(
  7106. '/posts/delete/1' .
  7107. serialize(['id' => '1']) .
  7108. '' .
  7109. Security::salt()
  7110. );
  7111. $hash .= '%3Aid';
  7112. $this->Form->request->params['_Token']['key'] = 'test';
  7113. $result = $this->Form->postLink(
  7114. 'Delete',
  7115. '/posts/delete/1',
  7116. ['data' => ['id' => 1]]
  7117. );
  7118. $expected = [
  7119. 'form' => [
  7120. 'method' => 'post', 'action' => '/posts/delete/1',
  7121. 'name', 'style' => 'display:none;'
  7122. ],
  7123. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7124. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7125. 'div' => ['style' => 'display:none;'],
  7126. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7127. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7128. '/div',
  7129. '/form',
  7130. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7131. 'Delete',
  7132. '/a'
  7133. ];
  7134. $this->assertHtml($expected, $result);
  7135. }
  7136. /**
  7137. * testPostLinkNestedData method
  7138. *
  7139. * Test using postLink with N dimensional data.
  7140. *
  7141. * @return void
  7142. */
  7143. public function testPostLinkNestedData()
  7144. {
  7145. $data = [
  7146. 'one' => [
  7147. 'two' => [
  7148. 3, 4, 5
  7149. ]
  7150. ]
  7151. ];
  7152. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7153. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7154. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7155. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7156. }
  7157. /**
  7158. * testPostLinkAfterGetForm method
  7159. *
  7160. * Test creating postLinks after a GET form.
  7161. *
  7162. * @return void
  7163. */
  7164. public function testPostLinkAfterGetForm()
  7165. {
  7166. $this->Form->request->params['_csrfToken'] = 'testkey';
  7167. $this->Form->request->params['_Token'] = 'val';
  7168. $this->Form->create($this->article, ['type' => 'get']);
  7169. $this->Form->end();
  7170. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7171. $tokenDebug = urlencode(json_encode([
  7172. '/posts/delete/1',
  7173. [],
  7174. []
  7175. ]));
  7176. $expected = [
  7177. 'form' => [
  7178. 'method' => 'post', 'action' => '/posts/delete/1',
  7179. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7180. ],
  7181. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7182. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7183. 'div' => ['style' => 'display:none;'],
  7184. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7185. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7186. ['input' => [
  7187. 'type' => 'hidden', 'name' => '_Token[debug]',
  7188. 'value' => $tokenDebug,
  7189. 'autocomplete' => 'off'
  7190. ]],
  7191. '/div',
  7192. '/form',
  7193. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7194. 'Delete',
  7195. '/a'
  7196. ];
  7197. $this->assertHtml($expected, $result);
  7198. }
  7199. /**
  7200. * testPostLinkFormBuffer method
  7201. *
  7202. * Test that postLink adds form tags to view block.
  7203. *
  7204. * @return void
  7205. */
  7206. public function testPostLinkFormBuffer()
  7207. {
  7208. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7209. $expected = [
  7210. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7211. 'Delete',
  7212. '/a'
  7213. ];
  7214. $this->assertHtml($expected, $result);
  7215. $result = $this->View->fetch('postLink');
  7216. $expected = [
  7217. 'form' => [
  7218. 'method' => 'post', 'action' => '/posts/delete/1',
  7219. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7220. ],
  7221. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7222. '/form'
  7223. ];
  7224. $this->assertHtml($expected, $result);
  7225. $result = $this->Form->postLink(
  7226. 'Delete',
  7227. '/posts/delete/2',
  7228. ['block' => true, 'method' => 'DELETE']
  7229. );
  7230. $expected = [
  7231. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7232. 'Delete',
  7233. '/a'
  7234. ];
  7235. $this->assertHtml($expected, $result);
  7236. $result = $this->View->fetch('postLink');
  7237. $expected = [
  7238. 'form' => [
  7239. 'method' => 'post', 'action' => '/posts/delete/1',
  7240. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7241. ],
  7242. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7243. '/form',
  7244. [
  7245. 'form' => [
  7246. 'method' => 'post', 'action' => '/posts/delete/2',
  7247. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7248. ],
  7249. ],
  7250. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7251. '/form'
  7252. ];
  7253. $this->assertHtml($expected, $result);
  7254. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7255. $expected = [
  7256. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7257. 'Delete',
  7258. '/a'
  7259. ];
  7260. $this->assertHtml($expected, $result);
  7261. $result = $this->View->fetch('foobar');
  7262. $expected = [
  7263. 'form' => [
  7264. 'method' => 'post', 'action' => '/posts/delete/1',
  7265. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7266. ],
  7267. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7268. '/form'
  7269. ];
  7270. $this->assertHtml($expected, $result);
  7271. }
  7272. /**
  7273. * testSubmitButton method
  7274. *
  7275. * @return void
  7276. */
  7277. public function testSubmitButton()
  7278. {
  7279. $result = $this->Form->submit('');
  7280. $expected = [
  7281. 'div' => ['class' => 'submit'],
  7282. 'input' => ['type' => 'submit', 'value' => ''],
  7283. '/div'
  7284. ];
  7285. $this->assertHtml($expected, $result);
  7286. $result = $this->Form->submit('Test Submit');
  7287. $expected = [
  7288. 'div' => ['class' => 'submit'],
  7289. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7290. '/div'
  7291. ];
  7292. $this->assertHtml($expected, $result);
  7293. $result = $this->Form->submit('Next >');
  7294. $expected = [
  7295. 'div' => ['class' => 'submit'],
  7296. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7297. '/div'
  7298. ];
  7299. $this->assertHtml($expected, $result);
  7300. $result = $this->Form->submit('Next >', ['escape' => false]);
  7301. $expected = [
  7302. 'div' => ['class' => 'submit'],
  7303. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7304. '/div'
  7305. ];
  7306. $this->assertHtml($expected, $result);
  7307. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7308. $expected = [
  7309. 'div' => ['class' => 'submit'],
  7310. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7311. '/div'
  7312. ];
  7313. $this->assertHtml($expected, $result);
  7314. }
  7315. /**
  7316. * testSubmitImage method
  7317. *
  7318. * Test image submit types.
  7319. *
  7320. * @return void
  7321. */
  7322. public function testSubmitImage()
  7323. {
  7324. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7325. $expected = [
  7326. 'div' => ['class' => 'submit'],
  7327. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7328. '/div'
  7329. ];
  7330. $this->assertHtml($expected, $result);
  7331. $result = $this->Form->submit('/relative/cake.power.gif');
  7332. $expected = [
  7333. 'div' => ['class' => 'submit'],
  7334. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7335. '/div'
  7336. ];
  7337. $this->assertHtml($expected, $result);
  7338. $result = $this->Form->submit('cake.power.gif');
  7339. $expected = [
  7340. 'div' => ['class' => 'submit'],
  7341. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7342. '/div'
  7343. ];
  7344. $this->assertHtml($expected, $result);
  7345. $result = $this->Form->submit('Not.an.image');
  7346. $expected = [
  7347. 'div' => ['class' => 'submit'],
  7348. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7349. '/div'
  7350. ];
  7351. $this->assertHtml($expected, $result);
  7352. }
  7353. /**
  7354. * testSubmitUnlockedByDefault method
  7355. *
  7356. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7357. * be submitted at a time.
  7358. *
  7359. * @return void
  7360. */
  7361. public function testSubmitUnlockedByDefault()
  7362. {
  7363. $this->Form->request->params['_Token'] = 'secured';
  7364. $this->Form->submit('Go go');
  7365. $this->Form->submit('Save', ['name' => 'save']);
  7366. $result = $this->Form->unlockField();
  7367. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7368. }
  7369. /**
  7370. * testSubmitImageTimestamp method
  7371. *
  7372. * Test submit image with timestamps.
  7373. *
  7374. * @return void
  7375. */
  7376. public function testSubmitImageTimestamp()
  7377. {
  7378. Configure::write('Asset.timestamp', 'force');
  7379. $result = $this->Form->submit('cake.power.gif');
  7380. $expected = [
  7381. 'div' => ['class' => 'submit'],
  7382. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7383. '/div'
  7384. ];
  7385. $this->assertHtml($expected, $result);
  7386. }
  7387. /**
  7388. * testDateTimeWithGetForms method
  7389. *
  7390. * Test that datetime() works with GET style forms.
  7391. *
  7392. * @return void
  7393. */
  7394. public function testDateTimeWithGetForms()
  7395. {
  7396. extract($this->dateRegex);
  7397. $this->Form->create($this->article, ['type' => 'get']);
  7398. $result = $this->Form->datetime('created');
  7399. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7400. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7401. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7402. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7403. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7404. }
  7405. /**
  7406. * testForMagicControlNonExistingNorValidated method
  7407. *
  7408. * @return void
  7409. */
  7410. public function testForMagicControlNonExistingNorValidated()
  7411. {
  7412. $this->Form->create($this->article);
  7413. $this->Form->templates(['inputContainer' => '{{content}}']);
  7414. $result = $this->Form->control('non_existing_nor_validated');
  7415. $expected = [
  7416. 'label' => ['for' => 'non-existing-nor-validated'],
  7417. 'Non Existing Nor Validated',
  7418. '/label',
  7419. 'input' => [
  7420. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7421. 'id' => 'non-existing-nor-validated'
  7422. ]
  7423. ];
  7424. $this->assertHtml($expected, $result);
  7425. $result = $this->Form->control('non_existing_nor_validated', [
  7426. 'val' => 'my value'
  7427. ]);
  7428. $expected = [
  7429. 'label' => ['for' => 'non-existing-nor-validated'],
  7430. 'Non Existing Nor Validated',
  7431. '/label',
  7432. 'input' => [
  7433. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7434. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7435. ]
  7436. ];
  7437. $this->assertHtml($expected, $result);
  7438. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7439. $result = $this->Form->control('non_existing_nor_validated');
  7440. $expected = [
  7441. 'label' => ['for' => 'non-existing-nor-validated'],
  7442. 'Non Existing Nor Validated',
  7443. '/label',
  7444. 'input' => [
  7445. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7446. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7447. ]
  7448. ];
  7449. $this->assertHtml($expected, $result);
  7450. }
  7451. /**
  7452. * testFormMagicControlLabel method
  7453. *
  7454. * @return void
  7455. */
  7456. public function testFormMagicControlLabel()
  7457. {
  7458. TableRegistry::get('Contacts', [
  7459. 'className' => __NAMESPACE__ . '\ContactsTable'
  7460. ]);
  7461. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7462. $this->Form->templates(['inputContainer' => '{{content}}']);
  7463. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7464. $expected = [
  7465. 'label' => ['for' => 'contacts-name'],
  7466. 'My label',
  7467. '/label',
  7468. 'input' => [
  7469. 'type' => 'text', 'name' => 'Contacts[name]',
  7470. 'id' => 'contacts-name', 'maxlength' => '255'
  7471. ]
  7472. ];
  7473. $this->assertHtml($expected, $result);
  7474. $result = $this->Form->control('name', [
  7475. 'label' => ['class' => 'mandatory']
  7476. ]);
  7477. $expected = [
  7478. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7479. 'Name',
  7480. '/label',
  7481. 'input' => [
  7482. 'type' => 'text', 'name' => 'name',
  7483. 'id' => 'name', 'maxlength' => '255'
  7484. ]
  7485. ];
  7486. $this->assertHtml($expected, $result);
  7487. $result = $this->Form->control('name', [
  7488. 'div' => false,
  7489. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7490. ]);
  7491. $expected = [
  7492. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7493. 'My label',
  7494. '/label',
  7495. 'input' => [
  7496. 'type' => 'text', 'name' => 'name',
  7497. 'id' => 'name', 'maxlength' => '255'
  7498. ]
  7499. ];
  7500. $this->assertHtml($expected, $result);
  7501. $result = $this->Form->control('Contact.name', [
  7502. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7503. ]);
  7504. $expected = [
  7505. 'label' => ['for' => 'my_id'],
  7506. 'Name',
  7507. '/label',
  7508. 'input' => [
  7509. 'type' => 'text', 'name' => 'Contact[name]',
  7510. 'id' => 'my_id', 'maxlength' => '255'
  7511. ]
  7512. ];
  7513. $this->assertHtml($expected, $result);
  7514. $result = $this->Form->control('1.id');
  7515. $expected = ['input' => [
  7516. 'type' => 'hidden', 'name' => '1[id]',
  7517. 'id' => '1-id'
  7518. ]];
  7519. $this->assertHtml($expected, $result);
  7520. $result = $this->Form->control("1.name");
  7521. $expected = [
  7522. 'label' => ['for' => '1-name'],
  7523. 'Name',
  7524. '/label',
  7525. 'input' => [
  7526. 'type' => 'text', 'name' => '1[name]',
  7527. 'id' => '1-name', 'maxlength' => '255'
  7528. ]
  7529. ];
  7530. $this->assertHtml($expected, $result);
  7531. }
  7532. /**
  7533. * testFormEnd method
  7534. *
  7535. * @return void
  7536. */
  7537. public function testFormEnd()
  7538. {
  7539. $this->assertEquals('</form>', $this->Form->end());
  7540. }
  7541. /**
  7542. * testMultiRecordForm method
  7543. *
  7544. * Test the generation of fields for a multi record form.
  7545. *
  7546. * @return void
  7547. */
  7548. public function testMultiRecordForm()
  7549. {
  7550. $this->loadFixtures('Articles', 'Comments');
  7551. $articles = TableRegistry::get('Articles');
  7552. $articles->hasMany('Comments');
  7553. $comment = new Entity(['comment' => 'Value']);
  7554. $article = new Article(['comments' => [$comment]]);
  7555. $this->Form->create([$article]);
  7556. $result = $this->Form->control('0.comments.1.comment');
  7557. //@codingStandardsIgnoreStart
  7558. $expected = [
  7559. 'div' => ['class' => 'input textarea'],
  7560. 'label' => ['for' => '0-comments-1-comment'],
  7561. 'Comment',
  7562. '/label',
  7563. 'textarea' => [
  7564. 'name',
  7565. 'id' => '0-comments-1-comment',
  7566. 'rows' => 5
  7567. ],
  7568. '/textarea',
  7569. '/div'
  7570. ];
  7571. //@codingStandardsIgnoreEnd
  7572. $this->assertHtml($expected, $result);
  7573. $result = $this->Form->control('0.comments.0.comment');
  7574. //@codingStandardsIgnoreStart
  7575. $expected = [
  7576. 'div' => ['class' => 'input textarea'],
  7577. 'label' => ['for' => '0-comments-0-comment'],
  7578. 'Comment',
  7579. '/label',
  7580. 'textarea' => [
  7581. 'name',
  7582. 'id' => '0-comments-0-comment',
  7583. 'rows' => 5
  7584. ],
  7585. 'Value',
  7586. '/textarea',
  7587. '/div'
  7588. ];
  7589. //@codingStandardsIgnoreEnd
  7590. $this->assertHtml($expected, $result);
  7591. $comment->errors('comment', ['Not valid']);
  7592. $result = $this->Form->control('0.comments.0.comment');
  7593. //@codingStandardsIgnoreStart
  7594. $expected = [
  7595. 'div' => ['class' => 'input textarea error'],
  7596. 'label' => ['for' => '0-comments-0-comment'],
  7597. 'Comment',
  7598. '/label',
  7599. 'textarea' => [
  7600. 'name',
  7601. 'class' => 'form-error',
  7602. 'id' => '0-comments-0-comment',
  7603. 'rows' => 5
  7604. ],
  7605. 'Value',
  7606. '/textarea',
  7607. ['div' => ['class' => 'error-message']],
  7608. 'Not valid',
  7609. '/div',
  7610. '/div'
  7611. ];
  7612. //@codingStandardsIgnoreEnd
  7613. $this->assertHtml($expected, $result);
  7614. TableRegistry::get('Comments')
  7615. ->validator('default')
  7616. ->allowEmpty('comment', false);
  7617. $result = $this->Form->control('0.comments.1.comment');
  7618. //@codingStandardsIgnoreStart
  7619. $expected = [
  7620. 'div' => ['class' => 'input textarea required'],
  7621. 'label' => ['for' => '0-comments-1-comment'],
  7622. 'Comment',
  7623. '/label',
  7624. 'textarea' => [
  7625. 'name',
  7626. 'required' => 'required',
  7627. 'id' => '0-comments-1-comment',
  7628. 'rows' => 5
  7629. ],
  7630. '/textarea',
  7631. '/div'
  7632. ];
  7633. //@codingStandardsIgnoreEnd
  7634. $this->assertHtml($expected, $result);
  7635. }
  7636. /**
  7637. * testHtml5Controls method
  7638. *
  7639. * Test that some html5 inputs + FormHelper::__call() work.
  7640. *
  7641. * @return void
  7642. */
  7643. public function testHtml5Controls()
  7644. {
  7645. $result = $this->Form->email('User.email');
  7646. $expected = [
  7647. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7648. ];
  7649. $this->assertHtml($expected, $result);
  7650. $result = $this->Form->search('User.query');
  7651. $expected = [
  7652. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7653. ];
  7654. $this->assertHtml($expected, $result);
  7655. $result = $this->Form->search('User.query', ['value' => 'test']);
  7656. $expected = [
  7657. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7658. ];
  7659. $this->assertHtml($expected, $result);
  7660. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7661. $expected = [
  7662. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7663. ];
  7664. $this->assertHtml($expected, $result);
  7665. }
  7666. /**
  7667. * testHtml5ControlWithControl method
  7668. *
  7669. * Test accessing html5 inputs through control().
  7670. *
  7671. * @return void
  7672. */
  7673. public function testHtml5ControlWithControl()
  7674. {
  7675. $this->Form->create();
  7676. $this->Form->templates(['inputContainer' => '{{content}}']);
  7677. $result = $this->Form->control('website', [
  7678. 'type' => 'url',
  7679. 'val' => 'http://domain.tld',
  7680. 'label' => false
  7681. ]);
  7682. $expected = [
  7683. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7684. ];
  7685. $this->assertHtml($expected, $result);
  7686. }
  7687. /**
  7688. * testHtml5ControlException method
  7689. *
  7690. * Test errors when field name is missing.
  7691. *
  7692. * @expectedException \Cake\Core\Exception\Exception
  7693. * @return void
  7694. */
  7695. public function testHtml5ControlException()
  7696. {
  7697. $this->Form->email();
  7698. }
  7699. /**
  7700. * testRequiredAttribute method
  7701. *
  7702. * Tests that formhelper sets required attributes.
  7703. *
  7704. * @return void
  7705. */
  7706. public function testRequiredAttribute()
  7707. {
  7708. $this->article['required'] = [
  7709. 'title' => true,
  7710. 'body' => false,
  7711. ];
  7712. $this->Form->create($this->article);
  7713. $result = $this->Form->control('title');
  7714. $expected = [
  7715. 'div' => ['class' => 'input text required'],
  7716. 'label' => ['for' => 'title'],
  7717. 'Title',
  7718. '/label',
  7719. 'input' => [
  7720. 'type' => 'text',
  7721. 'name' => 'title',
  7722. 'id' => 'title',
  7723. 'required' => 'required',
  7724. ],
  7725. '/div'
  7726. ];
  7727. $this->assertHtml($expected, $result);
  7728. $result = $this->Form->control('title', ['required' => false]);
  7729. $this->assertNotContains('required', $result);
  7730. $result = $this->Form->control('body');
  7731. $expected = [
  7732. 'div' => ['class' => 'input text'],
  7733. 'label' => ['for' => 'body'],
  7734. 'Body',
  7735. '/label',
  7736. 'input' => [
  7737. 'type' => 'text',
  7738. 'name' => 'body',
  7739. 'id' => 'body',
  7740. ],
  7741. '/div'
  7742. ];
  7743. $this->assertHtml($expected, $result);
  7744. $result = $this->Form->control('body', ['required' => true]);
  7745. $this->assertContains('required', $result);
  7746. }
  7747. /**
  7748. * testControlsNotNested method
  7749. *
  7750. * Tests that it is possible to put inputs outside of the label.
  7751. *
  7752. * @return void
  7753. */
  7754. public function testControlsNotNested()
  7755. {
  7756. $this->Form->templates([
  7757. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7758. 'formGroup' => '{{input}}{{label}}',
  7759. ]);
  7760. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7761. $expected = [
  7762. 'div' => ['class' => 'input checkbox'],
  7763. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7764. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7765. 'label' => ['for' => 'foo'],
  7766. 'Foo',
  7767. '/label',
  7768. '/div'
  7769. ];
  7770. $this->assertHtml($expected, $result);
  7771. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7772. $expected = [
  7773. 'div' => ['class' => 'input checkbox'],
  7774. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7775. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7776. '/div'
  7777. ];
  7778. $this->assertHtml($expected, $result);
  7779. $result = $this->Form->control('confirm', [
  7780. 'type' => 'radio',
  7781. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7782. ]);
  7783. $expected = [
  7784. 'div' => ['class' => 'input radio'],
  7785. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7786. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7787. ['label' => ['for' => 'confirm-y']],
  7788. 'Yes',
  7789. '/label',
  7790. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7791. ['label' => ['for' => 'confirm-n']],
  7792. 'No',
  7793. '/label',
  7794. '<label',
  7795. 'Confirm',
  7796. '/label',
  7797. '/div',
  7798. ];
  7799. $this->assertHtml($expected, $result);
  7800. $result = $this->Form->select('category', ['1', '2'], [
  7801. 'multiple' => 'checkbox',
  7802. 'name' => 'fish',
  7803. ]);
  7804. $expected = [
  7805. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7806. ['div' => ['class' => 'checkbox']],
  7807. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7808. ['label' => ['for' => 'fish-0']],
  7809. '1',
  7810. '/label',
  7811. '/div',
  7812. ['div' => ['class' => 'checkbox']],
  7813. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7814. ['label' => ['for' => 'fish-1']],
  7815. '2',
  7816. '/label',
  7817. '/div'
  7818. ];
  7819. $this->assertHtml($expected, $result);
  7820. }
  7821. /**
  7822. * testControlContainerTemplates method
  7823. *
  7824. * Test that *Container templates are used by input.
  7825. *
  7826. * @return void
  7827. */
  7828. public function testControlContainerTemplates()
  7829. {
  7830. $this->Form->templates([
  7831. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7832. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7833. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7834. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7835. ]);
  7836. $this->article['errors'] = [
  7837. 'Article' => ['published' => 'error message']
  7838. ];
  7839. $this->Form->create($this->article);
  7840. $result = $this->Form->control('accept', [
  7841. 'type' => 'checkbox'
  7842. ]);
  7843. $expected = [
  7844. 'div' => ['class' => 'check'],
  7845. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7846. 'label' => ['for' => 'accept'],
  7847. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7848. 'Accept',
  7849. '/label',
  7850. '/div'
  7851. ];
  7852. $this->assertHtml($expected, $result);
  7853. $result = $this->Form->control('accept', [
  7854. 'type' => 'radio',
  7855. 'options' => ['Y', 'N']
  7856. ]);
  7857. $this->assertContains('<div class="rad">', $result);
  7858. $result = $this->Form->control('Article.published', [
  7859. 'type' => 'radio',
  7860. 'options' => ['Y', 'N']
  7861. ]);
  7862. $this->assertContains('<div class="rad err">', $result);
  7863. $result = $this->Form->control('Article.created', [
  7864. 'type' => 'datetime',
  7865. ]);
  7866. $this->assertContains('<div class="dt">', $result);
  7867. }
  7868. /**
  7869. * testFormGroupTemplates method
  7870. *
  7871. * Test that *Container templates are used by input.
  7872. *
  7873. * @return void
  7874. */
  7875. public function testFormGroupTemplates()
  7876. {
  7877. $this->Form->templates([
  7878. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7879. ]);
  7880. $this->Form->create($this->article);
  7881. $result = $this->Form->control('accept', [
  7882. 'type' => 'radio',
  7883. 'options' => ['Y', 'N']
  7884. ]);
  7885. $this->assertContains('<div class="radio">', $result);
  7886. }
  7887. /**
  7888. * testResetTemplates method
  7889. *
  7890. * Test resetting templates.
  7891. *
  7892. * @return void
  7893. */
  7894. public function testResetTemplates()
  7895. {
  7896. $this->Form->templates(['input' => '<input/>']);
  7897. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7898. $this->Form->resetTemplates();
  7899. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7900. }
  7901. /**
  7902. * testContext method
  7903. *
  7904. * Test the context method.
  7905. *
  7906. * @return void
  7907. */
  7908. public function testContext()
  7909. {
  7910. $result = $this->Form->context();
  7911. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7912. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7913. $this->assertSame($mock, $this->Form->context($mock));
  7914. $this->assertSame($mock, $this->Form->context());
  7915. }
  7916. /**
  7917. * testAutoDomId method
  7918. *
  7919. * @return void
  7920. */
  7921. public function testAutoDomId()
  7922. {
  7923. $result = $this->Form->text('field', ['id' => true]);
  7924. $expected = [
  7925. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7926. ];
  7927. $this->assertHtml($expected, $result);
  7928. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7929. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7930. $expected = [
  7931. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7932. ['label' => ['for' => 'field-0']],
  7933. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7934. 'option A',
  7935. '/label',
  7936. ['label' => ['for' => 'field-1']],
  7937. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7938. 'option B',
  7939. '/label',
  7940. ];
  7941. $this->assertHtml($expected, $result);
  7942. $result = $this->Form->select(
  7943. 'multi_field',
  7944. ['first', 'second'],
  7945. ['multiple' => 'checkbox', 'id' => true]
  7946. );
  7947. $expected = [
  7948. 'input' => [
  7949. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7950. ],
  7951. ['div' => ['class' => 'checkbox']],
  7952. ['label' => ['for' => 'multi-field-0']],
  7953. ['input' => [
  7954. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7955. 'value' => '0', 'id' => 'multi-field-0'
  7956. ]],
  7957. 'first',
  7958. '/label',
  7959. '/div',
  7960. ['div' => ['class' => 'checkbox']],
  7961. ['label' => ['for' => 'multi-field-1']],
  7962. ['input' => [
  7963. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7964. 'value' => '1', 'id' => 'multi-field-1'
  7965. ]],
  7966. 'second',
  7967. '/label',
  7968. '/div',
  7969. ];
  7970. $this->assertHtml($expected, $result);
  7971. }
  7972. /**
  7973. * Test the basic setters and getters for value sources
  7974. *
  7975. * @return void
  7976. */
  7977. public function testFormValueSourcesSettersGetters()
  7978. {
  7979. $expected = ['context'];
  7980. $result = $this->Form->getValueSources();
  7981. $this->assertEquals($expected, $result);
  7982. $expected = null;
  7983. $result = $this->Form->getSourceValue('id');
  7984. $this->assertEquals($expected, $result);
  7985. $expected = ['query', 'data', 'context'];
  7986. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7987. $result = $this->Form->getValueSources();
  7988. $this->assertEquals($expected, $result);
  7989. $this->Form->request->data['id'] = '1';
  7990. $this->Form->request->query['id'] = '2';
  7991. $this->Form->setValueSources(['context']);
  7992. $expected = '1';
  7993. $result = $this->Form->getSourceValue('id');
  7994. $this->assertEquals($expected, $result);
  7995. $this->Form->setValueSources('query');
  7996. $expected = ['query'];
  7997. $result = $this->Form->getValueSources();
  7998. $this->assertEquals($expected, $result);
  7999. $expected = '2';
  8000. $result = $this->Form->getSourceValue('id');
  8001. $this->assertEquals($expected, $result);
  8002. $this->Form->setValueSources(['data']);
  8003. $expected = '1';
  8004. $result = $this->Form->getSourceValue('id');
  8005. $this->assertEquals($expected, $result);
  8006. $this->Form->setValueSources(['query', 'data']);
  8007. $expected = '2';
  8008. $result = $this->Form->getSourceValue('id');
  8009. $this->assertEquals($expected, $result);
  8010. }
  8011. /**
  8012. * Tests the different input rendering values based on sources values switching
  8013. *
  8014. * @return void
  8015. */
  8016. public function testFormValueSourcesSingleSwitchRendering()
  8017. {
  8018. $this->loadFixtures('Articles');
  8019. $articles = TableRegistry::get('Articles');
  8020. $article = new Article();
  8021. $articles->patchEntity($article, ['id' => '3']);
  8022. $this->Form->create($article);
  8023. $this->Form->setValueSources(['context']);
  8024. $result = $this->Form->control('id');
  8025. $expected = [
  8026. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8027. ];
  8028. $this->assertHtml($expected, $result);
  8029. $this->Form->request->query['id'] = '5';
  8030. $this->Form->setValueSources(['query']);
  8031. $result = $this->Form->control('id');
  8032. $expected = [
  8033. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8034. ];
  8035. $this->assertHtml($expected, $result);
  8036. $this->Form->request->query['id'] = '5a';
  8037. $this->Form->request->data['id'] = '5b';
  8038. $this->Form->setValueSources(['context']);
  8039. $this->Form->create($article);
  8040. $result = $this->Form->control('id');
  8041. $expected = [
  8042. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8043. ];
  8044. $this->assertHtml($expected, $result);
  8045. $this->Form->setValueSources(['data']);
  8046. $this->Form->create($article);
  8047. $result = $this->Form->control('id');
  8048. $expected = [
  8049. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8050. ];
  8051. $this->assertHtml($expected, $result);
  8052. $this->Form->setValueSources(['query']);
  8053. $result = $this->Form->control('id');
  8054. $expected = [
  8055. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8056. ];
  8057. $this->assertHtml($expected, $result);
  8058. }
  8059. /**
  8060. * Tests the different input rendering values based on sources values switching while supplying
  8061. * an entity (base context) and multiple sources (such as data, query)
  8062. *
  8063. * @return void
  8064. */
  8065. public function testFormValueSourcesListSwitchRendering()
  8066. {
  8067. $this->loadFixtures('Articles');
  8068. $articles = TableRegistry::get('Articles');
  8069. $article = new Article();
  8070. $articles->patchEntity($article, ['id' => '3']);
  8071. $this->Form->request->query['id'] = '9';
  8072. $this->Form->create($article);
  8073. $this->Form->setValueSources(['context', 'query']);
  8074. $result = $this->Form->control('id');
  8075. $expected = [
  8076. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8077. ];
  8078. $this->assertHtml($expected, $result);
  8079. $this->Form->setValueSources(['query', 'context']);
  8080. $result = $this->Form->control('id');
  8081. $expected = [
  8082. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8083. ];
  8084. $this->assertHtml($expected, $result);
  8085. $this->Form->setValueSources(['data', 'query', 'context']);
  8086. $result = $this->Form->control('id');
  8087. $expected = [
  8088. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8089. ];
  8090. $this->assertHtml($expected, $result);
  8091. $this->Form->request->data['id'] = '8';
  8092. $this->Form->request->query['id'] = '9';
  8093. $this->Form->setValueSources(['data', 'query', 'context']);
  8094. $result = $this->Form->control('id');
  8095. $expected = [
  8096. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8097. ];
  8098. $this->assertHtml($expected, $result);
  8099. }
  8100. /**
  8101. * Test the different form input renderings based on values sources switchings through form options
  8102. *
  8103. * @return void
  8104. */
  8105. public function testFormValueSourcesSwitchViaOptionsRendering()
  8106. {
  8107. $this->loadFixtures('Articles');
  8108. $articles = TableRegistry::get('Articles');
  8109. $article = new Article();
  8110. $articles->patchEntity($article, ['id' => '3']);
  8111. $this->Form->request->data['id'] = '4';
  8112. $this->Form->request->query['id'] = '5';
  8113. $this->Form->create($article, ['valueSources' => 'query']);
  8114. $result = $this->Form->control('id');
  8115. $expected = [
  8116. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8117. ];
  8118. $this->assertHtml($expected, $result);
  8119. $result = $this->Form->getSourceValue('id');
  8120. $this->assertEquals('5', $result);
  8121. $this->Form->setValueSources(['context']);
  8122. $this->Form->create($article, ['valueSources' => 'query']);
  8123. $result = $this->Form->control('id');
  8124. $expected = [
  8125. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8126. ];
  8127. $this->assertHtml($expected, $result);
  8128. $result = $this->Form->getSourceValue('id');
  8129. $this->assertEquals('5', $result);
  8130. $this->Form->setValueSources(['query']);
  8131. $this->Form->create($article, ['valueSources' => 'data']);
  8132. $result = $this->Form->control('id');
  8133. $expected = [
  8134. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8135. ];
  8136. $this->assertHtml($expected, $result);
  8137. $result = $this->Form->getSourceValue('id');
  8138. $this->assertEquals('4', $result);
  8139. $this->Form->setValueSources(['query']);
  8140. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8141. $result = $this->Form->control('id');
  8142. $expected = [
  8143. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8144. ];
  8145. $this->assertHtml($expected, $result);
  8146. $result = $this->Form->getSourceValue('id');
  8147. $this->assertEquals('4', $result);
  8148. }
  8149. /**
  8150. * Test the different form input renderings based on values sources switchings through form options
  8151. *
  8152. * @return void
  8153. */
  8154. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8155. {
  8156. $this->loadFixtures('Articles');
  8157. $articles = TableRegistry::get('Articles');
  8158. $article = new Article();
  8159. $articles->patchEntity($article, ['id' => '3']);
  8160. $this->Form->request->data['id'] = '10';
  8161. $this->Form->request->query['id'] = '11';
  8162. $this->Form->setValueSources(['context'])
  8163. ->create($article, ['valueSources' => ['query', 'data']]);
  8164. $result = $this->Form->control('id');
  8165. $expected = [
  8166. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8167. ];
  8168. $this->assertHtml($expected, $result);
  8169. $result = $this->Form->getSourceValue('id');
  8170. $this->assertEquals('11', $result);
  8171. unset($this->Form->request->query['id']);
  8172. $this->Form->setValueSources(['context'])
  8173. ->create($article, ['valueSources' => ['query', 'data']]);
  8174. $result = $this->Form->control('id');
  8175. $expected = [
  8176. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8177. ];
  8178. $this->assertHtml($expected, $result);
  8179. $result = $this->Form->getSourceValue('id');
  8180. $this->assertEquals('10', $result);
  8181. }
  8182. /**
  8183. * Test the different form values sources resetting through From::end();
  8184. *
  8185. * @return void
  8186. */
  8187. public function testFormValueSourcesResetViaEnd()
  8188. {
  8189. $expected = ['context'];
  8190. $result = $this->Form->getValueSources();
  8191. $this->assertEquals($expected, $result);
  8192. $expected = ['query', 'context', 'data'];
  8193. $this->Form->setValueSources(['query', 'context', 'data']);
  8194. $result = $this->Form->getValueSources();
  8195. $this->assertEquals($expected, $result);
  8196. $this->Form->create();
  8197. $result = $this->Form->getValueSources();
  8198. $this->assertEquals($expected, $result);
  8199. $this->Form->end();
  8200. $result = $this->Form->getValueSources();
  8201. $this->assertEquals(['context'], $result);
  8202. }
  8203. /**
  8204. * Test sources values defaults handling
  8205. *
  8206. * @return void
  8207. */
  8208. public function testFormValueSourcesDefaults()
  8209. {
  8210. $this->Form->request->query['password'] = 'open Sesame';
  8211. $this->Form->create();
  8212. $result = $this->Form->password('password');
  8213. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8214. $this->assertHtml($expected, $result);
  8215. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8216. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8217. $this->assertHtml($expected, $result);
  8218. $this->Form->setValueSources('query');
  8219. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8220. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8221. $this->assertHtml($expected, $result);
  8222. $this->Form->setValueSources('data');
  8223. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8224. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8225. $this->assertHtml($expected, $result);
  8226. }
  8227. /**
  8228. * Test sources values schema defaults handling
  8229. *
  8230. * @return void
  8231. */
  8232. public function testSourcesValueDoesntExistPassThrough()
  8233. {
  8234. $this->Form->request->query['category'] = 'sesame-cookies';
  8235. $articles = TableRegistry::get('Articles');
  8236. $entity = $articles->newEntity();
  8237. $this->Form->create($entity);
  8238. $this->Form->setValueSources(['query', 'context']);
  8239. $result = $this->Form->getSourceValue('category');
  8240. $this->assertEquals('sesame-cookies', $result);
  8241. $this->Form->setValueSources(['context', 'query']);
  8242. $result = $this->Form->getSourceValue('category');
  8243. $this->assertEquals('sesame-cookies', $result);
  8244. }
  8245. /**
  8246. * testNestedLabelInput method
  8247. *
  8248. * Test the `nestedInput` parameter
  8249. *
  8250. * @return void
  8251. */
  8252. public function testNestedLabelInput()
  8253. {
  8254. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8255. $expected = [
  8256. 'div' => ['class' => 'input text'],
  8257. 'label' => ['for' => 'foo'],
  8258. ['input' => [
  8259. 'type' => 'text',
  8260. 'name' => 'foo',
  8261. 'id' => 'foo'
  8262. ]],
  8263. 'Foo',
  8264. '/label',
  8265. '/div'
  8266. ];
  8267. $this->assertHtml($expected, $result);
  8268. }
  8269. /**
  8270. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8271. *
  8272. * This test makes sure `false` excludes the label from the render
  8273. *
  8274. * @return void
  8275. */
  8276. public function testInputLabelManipulationDisableLabels()
  8277. {
  8278. $result = $this->Form->input('test', [
  8279. 'type' => 'radio',
  8280. 'options' => ['A', 'B'],
  8281. 'labelOptions' => false
  8282. ]);
  8283. $expected = [
  8284. ['div' => ['class' => 'input radio']],
  8285. '<label',
  8286. 'Test',
  8287. '/label',
  8288. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8289. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8290. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8291. '/div',
  8292. ];
  8293. $this->assertHtml($expected, $result);
  8294. $result = $this->Form->input('checkbox1', [
  8295. 'label' => 'My checkboxes',
  8296. 'multiple' => 'checkbox',
  8297. 'type' => 'select',
  8298. 'options' => [
  8299. ['text' => 'First Checkbox', 'value' => 1],
  8300. ['text' => 'Second Checkbox', 'value' => 2]
  8301. ],
  8302. 'labelOptions' => false
  8303. ]);
  8304. $expected = [
  8305. ['div' => ['class' => 'input select']],
  8306. ['label' => ['for' => 'checkbox1']],
  8307. 'My checkboxes',
  8308. '/label',
  8309. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8310. ['div' => ['class' => 'checkbox']],
  8311. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8312. '/div',
  8313. ['div' => ['class' => 'checkbox']],
  8314. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8315. '/div',
  8316. '/div'
  8317. ];
  8318. $this->assertHtml($expected, $result);
  8319. }
  8320. /**
  8321. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8322. *
  8323. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8324. * added to the class if checked.
  8325. *
  8326. * Also checks to make sure any custom attributes are rendered correctly
  8327. *
  8328. * @return void
  8329. */
  8330. public function testInputLabelManipulationRadios()
  8331. {
  8332. $result = $this->Form->input('test', [
  8333. 'type' => 'radio',
  8334. 'options' => ['A', 'B'],
  8335. 'labelOptions' => ['class' => 'custom-class']
  8336. ]);
  8337. $expected = [
  8338. ['div' => ['class' => 'input radio']],
  8339. '<label',
  8340. 'Test',
  8341. '/label',
  8342. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8343. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8344. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8345. 'A',
  8346. '/label',
  8347. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8348. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8349. 'B',
  8350. '/label',
  8351. '/div',
  8352. ];
  8353. $this->assertHtml($expected, $result);
  8354. $result = $this->Form->input('test', [
  8355. 'type' => 'radio',
  8356. 'options' => ['A', 'B'],
  8357. 'value' => 1,
  8358. 'labelOptions' => ['class' => 'custom-class']
  8359. ]);
  8360. $expected = [
  8361. ['div' => ['class' => 'input radio']],
  8362. '<label',
  8363. 'Test',
  8364. '/label',
  8365. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8366. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8367. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8368. 'A',
  8369. '/label',
  8370. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8371. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8372. 'B',
  8373. '/label',
  8374. '/div',
  8375. ];
  8376. $this->assertHtml($expected, $result);
  8377. $result = $this->Form->input('test', [
  8378. 'type' => 'radio',
  8379. 'options' => ['A', 'B'],
  8380. 'value' => 1,
  8381. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8382. ]);
  8383. $expected = [
  8384. ['div' => ['class' => 'input radio']],
  8385. '<label',
  8386. 'Test',
  8387. '/label',
  8388. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8389. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8390. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8391. 'A',
  8392. '/label',
  8393. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8394. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8395. 'B',
  8396. '/label',
  8397. '/div',
  8398. ];
  8399. $this->assertHtml($expected, $result);
  8400. $result = $this->Form->radio('test', ['A', 'B'], [
  8401. 'label' => [
  8402. 'class' => ['custom-class', 'another-class'],
  8403. 'data-name' => 'bob'
  8404. ],
  8405. 'value' => 1
  8406. ]);
  8407. $expected = [
  8408. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8409. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8410. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8411. 'A',
  8412. '/label',
  8413. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8414. ['input' => [
  8415. 'type' => 'radio',
  8416. 'name' => 'test',
  8417. 'value' => '1',
  8418. 'id' => 'test-1',
  8419. 'checked' => 'checked'
  8420. ]],
  8421. 'B',
  8422. '/label',
  8423. ];
  8424. $this->assertHtml($expected, $result);
  8425. }
  8426. /**
  8427. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8428. *
  8429. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8430. * added to the class if checked.
  8431. *
  8432. * Also checks to make sure any custom attributes are rendered correctly
  8433. *
  8434. * @return void
  8435. */
  8436. public function testInputLabelManipulationCheckboxes()
  8437. {
  8438. $result = $this->Form->input('checkbox1', [
  8439. 'label' => 'My checkboxes',
  8440. 'multiple' => 'checkbox',
  8441. 'type' => 'select',
  8442. 'options' => [
  8443. ['text' => 'First Checkbox', 'value' => 1],
  8444. ['text' => 'Second Checkbox', 'value' => 2]
  8445. ],
  8446. 'labelOptions' => ['class' => 'custom-class'],
  8447. 'value' => ['1']
  8448. ]);
  8449. $expected = [
  8450. ['div' => ['class' => 'input select']],
  8451. ['label' => ['for' => 'checkbox1']],
  8452. 'My checkboxes',
  8453. '/label',
  8454. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8455. ['div' => ['class' => 'checkbox']],
  8456. ['label' => [
  8457. 'class' => 'custom-class selected',
  8458. 'for' => 'checkbox1-1'
  8459. ]],
  8460. ['input' => [
  8461. 'type' => 'checkbox',
  8462. 'name' => 'checkbox1[]',
  8463. 'value' => '1',
  8464. 'id' => 'checkbox1-1',
  8465. 'checked' => 'checked'
  8466. ]],
  8467. 'First Checkbox',
  8468. '/label',
  8469. '/div',
  8470. ['div' => ['class' => 'checkbox']],
  8471. ['label' => [
  8472. 'class' => 'custom-class',
  8473. 'for' => 'checkbox1-2'
  8474. ]],
  8475. ['input' => [
  8476. 'type' => 'checkbox',
  8477. 'name' => 'checkbox1[]',
  8478. 'value' => '2',
  8479. 'id' => 'checkbox1-2'
  8480. ]],
  8481. 'Second Checkbox',
  8482. '/label',
  8483. '/div',
  8484. '/div'
  8485. ];
  8486. $this->assertHtml($expected, $result);
  8487. $result = $this->Form->input('checkbox1', [
  8488. 'label' => 'My checkboxes',
  8489. 'multiple' => 'checkbox',
  8490. 'type' => 'select',
  8491. 'options' => [
  8492. ['text' => 'First Checkbox', 'value' => 1],
  8493. ['text' => 'Second Checkbox', 'value' => 2]
  8494. ],
  8495. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8496. 'value' => ['1']
  8497. ]);
  8498. $expected = [
  8499. ['div' => ['class' => 'input select']],
  8500. ['label' => ['for' => 'checkbox1']],
  8501. 'My checkboxes',
  8502. '/label',
  8503. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8504. ['div' => ['class' => 'checkbox']],
  8505. ['label' => [
  8506. 'class' => 'custom-class another-class selected',
  8507. 'data-name' => 'bob',
  8508. 'for' => 'checkbox1-1'
  8509. ]],
  8510. ['input' => [
  8511. 'type' => 'checkbox',
  8512. 'name' => 'checkbox1[]',
  8513. 'value' => '1',
  8514. 'id' => 'checkbox1-1',
  8515. 'checked' => 'checked'
  8516. ]],
  8517. 'First Checkbox',
  8518. '/label',
  8519. '/div',
  8520. ['div' => ['class' => 'checkbox']],
  8521. ['label' => [
  8522. 'class' => 'custom-class another-class',
  8523. 'data-name' => 'bob',
  8524. 'for' => 'checkbox1-2'
  8525. ]],
  8526. ['input' => [
  8527. 'type' => 'checkbox',
  8528. 'name' => 'checkbox1[]',
  8529. 'value' => '2',
  8530. 'id' => 'checkbox1-2'
  8531. ]],
  8532. 'Second Checkbox',
  8533. '/label',
  8534. '/div',
  8535. '/div'
  8536. ];
  8537. $this->assertHtml($expected, $result);
  8538. }
  8539. }