FormHelperTest.php 301 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.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. */
  104. class FormHelperTest extends TestCase
  105. {
  106. /**
  107. * Fixtures to be used
  108. *
  109. * @var array
  110. */
  111. public $fixtures = ['core.articles', 'core.comments'];
  112. /**
  113. * Do not load the fixtures by default
  114. *
  115. * @var bool
  116. */
  117. public $autoFixtures = false;
  118. /**
  119. * setUp method
  120. *
  121. * @return void
  122. */
  123. public function setUp()
  124. {
  125. parent::setUp();
  126. Configure::write('Config.language', 'eng');
  127. Configure::write('App.base', '');
  128. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  129. $this->View = new View();
  130. $this->Form = new FormHelper($this->View);
  131. $request = new ServerRequest([
  132. 'webroot' => '',
  133. 'base' => '',
  134. 'url' => '/articles/add',
  135. 'params' => [
  136. 'controller' => 'articles',
  137. 'action' => 'add',
  138. ]
  139. ]);
  140. $this->Form->Url->request = $this->Form->request = $request;
  141. $this->dateRegex = [
  142. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  143. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  144. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  145. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  146. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  147. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  148. ];
  149. $this->article = [
  150. 'schema' => [
  151. 'id' => ['type' => 'integer'],
  152. 'author_id' => ['type' => 'integer', 'null' => true],
  153. 'title' => ['type' => 'string', 'null' => true],
  154. 'body' => 'text',
  155. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  156. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  157. ],
  158. 'required' => [
  159. 'author_id' => true,
  160. 'title' => true,
  161. ]
  162. ];
  163. Security::salt('foo!');
  164. Router::connect('/:controller', ['action' => 'index']);
  165. Router::connect('/:controller/:action/*');
  166. }
  167. /**
  168. * tearDown method
  169. *
  170. * @return void
  171. */
  172. public function tearDown()
  173. {
  174. parent::tearDown();
  175. unset($this->Form, $this->Controller, $this->View);
  176. TableRegistry::clear();
  177. }
  178. /**
  179. * Test construct() with the templates option.
  180. *
  181. * @return void
  182. */
  183. public function testConstructTemplatesFile()
  184. {
  185. $helper = new FormHelper($this->View, [
  186. 'templates' => 'htmlhelper_tags'
  187. ]);
  188. $result = $helper->control('name');
  189. $this->assertContains('<input', $result);
  190. }
  191. /**
  192. * Test that when specifying custom widgets the config array for that widget
  193. * is overwritten instead of merged.
  194. *
  195. * @return void
  196. */
  197. public function testConstructWithWidgets()
  198. {
  199. $config = [
  200. 'widgets' => [
  201. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  202. ]
  203. ];
  204. $helper = new FormHelper($this->View, $config);
  205. $registry = $helper->widgetRegistry();
  206. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  207. }
  208. /**
  209. * Test that when specifying custom widgets config file and it should be
  210. * added to widgets array. WidgetRegistry will load widgets in constructor.
  211. *
  212. * @return void
  213. */
  214. public function testConstructWithWidgetsConfig()
  215. {
  216. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  217. $registry = $helper->widgetRegistry();
  218. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  219. }
  220. /**
  221. * Test registering a new widget class and rendering it.
  222. *
  223. * @return void
  224. */
  225. public function testAddWidgetAndRenderWidget()
  226. {
  227. $data = [
  228. 'val' => 1
  229. ];
  230. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  231. $this->Form->addWidget('test', $mock);
  232. $mock->expects($this->once())
  233. ->method('render')
  234. ->with($data)
  235. ->will($this->returnValue('HTML'));
  236. $result = $this->Form->widget('test', $data);
  237. $this->assertEquals('HTML', $result);
  238. }
  239. /**
  240. * Test that secureFields() of widget is called after calling render(),
  241. * not before.
  242. *
  243. * @return void
  244. */
  245. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  246. {
  247. $data = [
  248. 'val' => 1,
  249. 'name' => 'test'
  250. ];
  251. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  252. $this->Form->addWidget('test', $mock);
  253. $mock->expects($this->at(0))
  254. ->method('render')
  255. ->with($data)
  256. ->will($this->returnValue('HTML'));
  257. $mock->expects($this->at(1))
  258. ->method('secureFields')
  259. ->with($data)
  260. ->will($this->returnValue(['test']));
  261. $result = $this->Form->widget('test', $data + ['secure' => true]);
  262. $this->assertEquals('HTML', $result);
  263. }
  264. /**
  265. * Test that empty string is not added to secure fields list when
  266. * rendering input widget without name.
  267. *
  268. * @return void
  269. */
  270. public function testRenderingWidgetWithEmptyName()
  271. {
  272. $this->assertEquals([], $this->Form->fields);
  273. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  274. $this->assertEquals('<select name=""></select>', $result);
  275. $this->assertEquals([], $this->Form->fields);
  276. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  277. $this->assertEquals('<select name="0"></select>', $result);
  278. $this->assertEquals(['0'], $this->Form->fields);
  279. }
  280. /**
  281. * Test registering an invalid widget class.
  282. *
  283. * @expectedException \RuntimeException
  284. * @return void
  285. */
  286. public function testAddWidgetInvalid()
  287. {
  288. $mock = new \StdClass();
  289. $this->Form->addWidget('test', $mock);
  290. $this->Form->widget('test');
  291. }
  292. /**
  293. * Test adding a new context class.
  294. *
  295. * @return void
  296. */
  297. public function testAddContextProvider()
  298. {
  299. $context = 'My data';
  300. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  301. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  302. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  303. $this->assertEquals($context, $data['entity']);
  304. return $stub;
  305. });
  306. $this->Form->create($context);
  307. $result = $this->Form->context();
  308. $this->assertSame($stub, $result);
  309. }
  310. /**
  311. * Test replacing a context class.
  312. *
  313. * @return void
  314. */
  315. public function testAddContextProviderReplace()
  316. {
  317. $entity = new Article();
  318. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  319. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  320. return $stub;
  321. });
  322. $this->Form->create($entity);
  323. $result = $this->Form->context();
  324. $this->assertSame($stub, $result);
  325. }
  326. /**
  327. * Test overriding a context class.
  328. *
  329. * @return void
  330. */
  331. public function testAddContextProviderAdd()
  332. {
  333. $entity = new Article();
  334. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  335. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  336. if ($data['entity'] instanceof Entity) {
  337. return $stub;
  338. }
  339. });
  340. $this->Form->create($entity);
  341. $result = $this->Form->context();
  342. $this->assertSame($stub, $result);
  343. }
  344. /**
  345. * Test adding an invalid context class.
  346. *
  347. * @expectedException \RuntimeException
  348. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  349. * @return void
  350. */
  351. public function testAddContextProviderInvalid()
  352. {
  353. $context = 'My data';
  354. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  355. return new \StdClass();
  356. });
  357. $this->Form->create($context);
  358. }
  359. /**
  360. * Provides context options for create().
  361. *
  362. * @return array
  363. */
  364. public function contextSelectionProvider()
  365. {
  366. $entity = new Article();
  367. $collection = new Collection([$entity]);
  368. $emptyCollection = new Collection([]);
  369. $arrayObject = new \ArrayObject([]);
  370. $data = [
  371. 'schema' => [
  372. 'title' => ['type' => 'string']
  373. ]
  374. ];
  375. $form = new Form();
  376. return [
  377. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  378. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  379. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  380. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  381. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  382. 'form' => [$form, 'Cake\View\Form\FormContext'],
  383. 'none' => [null, 'Cake\View\Form\NullContext'],
  384. 'false' => [false, 'Cake\View\Form\NullContext'],
  385. ];
  386. }
  387. /**
  388. * Test default context selection in create()
  389. *
  390. * @dataProvider contextSelectionProvider
  391. * @return void
  392. */
  393. public function testCreateContextSelectionBuiltIn($data, $class)
  394. {
  395. $this->loadFixtures('Articles');
  396. $this->Form->create($data);
  397. $this->assertInstanceOf($class, $this->Form->context());
  398. }
  399. /**
  400. * Data provider for type option.
  401. *
  402. * @return array
  403. */
  404. public static function requestTypeProvider()
  405. {
  406. return [
  407. // type, method, override
  408. ['post', 'post', 'POST'],
  409. ['put', 'post', 'PUT'],
  410. ['patch', 'post', 'PATCH'],
  411. ['delete', 'post', 'DELETE'],
  412. ];
  413. }
  414. /**
  415. * Test creating file forms.
  416. *
  417. * @return void
  418. */
  419. public function testCreateFile()
  420. {
  421. $encoding = strtolower(Configure::read('App.encoding'));
  422. $result = $this->Form->create(false, ['type' => 'file']);
  423. $expected = [
  424. 'form' => [
  425. 'method' => 'post', 'action' => '/articles/add',
  426. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  427. ],
  428. 'div' => ['style' => 'display:none;'],
  429. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  430. '/div'
  431. ];
  432. $this->assertHtml($expected, $result);
  433. }
  434. /**
  435. * Test creating GET forms.
  436. *
  437. * @return void
  438. */
  439. public function testCreateGet()
  440. {
  441. $encoding = strtolower(Configure::read('App.encoding'));
  442. $result = $this->Form->create(false, ['type' => 'get']);
  443. $expected = ['form' => [
  444. 'method' => 'get', 'action' => '/articles/add',
  445. 'accept-charset' => $encoding
  446. ]];
  447. $this->assertHtml($expected, $result);
  448. }
  449. /**
  450. * Test explicit method/enctype options.
  451. *
  452. * Explicit method overwrites inferred method from 'type'
  453. *
  454. * @return void
  455. */
  456. public function testCreateExplicitMethodEnctype()
  457. {
  458. $encoding = strtolower(Configure::read('App.encoding'));
  459. $result = $this->Form->create(false, [
  460. 'type' => 'get',
  461. 'method' => 'put',
  462. 'enctype' => 'multipart/form-data'
  463. ]);
  464. $expected = ['form' => [
  465. 'method' => 'put',
  466. 'action' => '/articles/add',
  467. 'enctype' => 'multipart/form-data',
  468. 'accept-charset' => $encoding
  469. ]];
  470. $this->assertHtml($expected, $result);
  471. }
  472. /**
  473. * Test create() with the templates option.
  474. *
  475. * @return void
  476. */
  477. public function testCreateTemplatesArray()
  478. {
  479. $result = $this->Form->create($this->article, [
  480. 'templates' => [
  481. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  482. ]
  483. ]);
  484. $expected = [
  485. 'form' => [
  486. 'class' => 'form-horizontal',
  487. 'method' => 'post',
  488. 'action' => '/articles/add',
  489. 'accept-charset' => 'utf-8'
  490. ]
  491. ];
  492. $this->assertHtml($expected, $result);
  493. }
  494. /**
  495. * Test create() with the templates option.
  496. *
  497. * @return void
  498. */
  499. public function testCreateTemplatesFile()
  500. {
  501. $result = $this->Form->create($this->article, [
  502. 'templates' => 'htmlhelper_tags',
  503. ]);
  504. $expected = [
  505. 'start form',
  506. 'div' => ['class' => 'hidden'],
  507. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  508. '/div'
  509. ];
  510. $this->assertHtml($expected, $result);
  511. }
  512. /**
  513. * Test that create() and end() restore templates.
  514. *
  515. * @return void
  516. */
  517. public function testCreateEndRestoreTemplates()
  518. {
  519. $this->Form->create($this->article, [
  520. 'templates' => ['input' => 'custom input element']
  521. ]);
  522. $this->Form->end();
  523. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  524. }
  525. /**
  526. * Test using template vars in various templates used by control() method.
  527. *
  528. * @return void
  529. */
  530. public function testControlTemplateVars()
  531. {
  532. $result = $this->Form->control('text', [
  533. 'templates' => [
  534. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  535. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  536. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  537. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  538. ],
  539. 'templateVars' => [
  540. 'forinput' => 'in-input',
  541. 'forlabel' => 'in-label',
  542. 'forgroup' => 'in-group',
  543. 'forcontainer' => 'in-container'
  544. ]
  545. ]);
  546. $expected = [
  547. 'div' => ['class'],
  548. 'label' => ['for'],
  549. 'Text in-label',
  550. '/label',
  551. 'in-group',
  552. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  553. 'in-container',
  554. '/div',
  555. ];
  556. $this->assertHtml($expected, $result);
  557. }
  558. /**
  559. * Test ensuring template variables work in template files loaded
  560. * during control().
  561. *
  562. * @return void
  563. */
  564. public function testControlTemplatesFromFile()
  565. {
  566. $result = $this->Form->control('title', [
  567. 'templates' => 'test_templates',
  568. 'templateVars' => [
  569. 'forcontainer' => 'container-data'
  570. ]
  571. ]);
  572. $expected = [
  573. 'div' => ['class'],
  574. 'label' => ['for'],
  575. 'Title',
  576. '/label',
  577. 'input' => ['name', 'type' => 'text', 'id'],
  578. 'container-data',
  579. '/div',
  580. ];
  581. $this->assertHtml($expected, $result);
  582. }
  583. /**
  584. * Test using template vars in inputSubmit and submitContainer template.
  585. *
  586. * @return void
  587. */
  588. public function testSubmitTemplateVars()
  589. {
  590. $this->Form->templates([
  591. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  592. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  593. ]);
  594. $result = $this->Form->submit('Submit', [
  595. 'templateVars' => [
  596. 'forinput' => 'in-input',
  597. 'forcontainer' => 'in-container'
  598. ]
  599. ]);
  600. $expected = [
  601. 'div' => ['class'],
  602. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  603. 'in-container',
  604. '/div',
  605. ];
  606. $this->assertHtml($expected, $result);
  607. }
  608. /**
  609. * test the create() method
  610. *
  611. * @dataProvider requestTypeProvider
  612. * @return void
  613. */
  614. public function testCreateTypeOptions($type, $method, $override)
  615. {
  616. $encoding = strtolower(Configure::read('App.encoding'));
  617. $result = $this->Form->create(false, ['type' => $type]);
  618. $expected = [
  619. 'form' => [
  620. 'method' => $method, 'action' => '/articles/add',
  621. 'accept-charset' => $encoding
  622. ],
  623. 'div' => ['style' => 'display:none;'],
  624. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  625. '/div'
  626. ];
  627. $this->assertHtml($expected, $result);
  628. }
  629. /**
  630. * Test using template vars in Create (formStart template)
  631. *
  632. * @return void
  633. */
  634. public function testCreateTemplateVars()
  635. {
  636. $result = $this->Form->create($this->article, [
  637. 'templates' => [
  638. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  639. ],
  640. 'templateVars' => ['header' => 'headertext']
  641. ]);
  642. $expected = [
  643. 'h4' => ['class'],
  644. 'headertext',
  645. '/h4',
  646. 'form' => [
  647. 'method' => 'post',
  648. 'action' => '/articles/add',
  649. 'accept-charset' => 'utf-8'
  650. ]
  651. ];
  652. $this->assertHtml($expected, $result);
  653. }
  654. /**
  655. * Test opening a form for an update operation.
  656. *
  657. * @return void
  658. */
  659. public function testCreateUpdateForm()
  660. {
  661. $encoding = strtolower(Configure::read('App.encoding'));
  662. $this->Form->request->here = '/articles/edit/1';
  663. $this->Form->request->params['action'] = 'edit';
  664. $this->article['defaults']['id'] = 1;
  665. $result = $this->Form->create($this->article);
  666. $expected = [
  667. 'form' => [
  668. 'method' => 'post',
  669. 'action' => '/articles/edit/1',
  670. 'accept-charset' => $encoding
  671. ],
  672. 'div' => ['style' => 'display:none;'],
  673. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  674. '/div'
  675. ];
  676. $this->assertHtml($expected, $result);
  677. }
  678. /**
  679. * test create() with automatic url generation
  680. *
  681. * @return void
  682. */
  683. public function testCreateAutoUrl()
  684. {
  685. $encoding = strtolower(Configure::read('App.encoding'));
  686. $this->Form->request->params['action'] = 'delete';
  687. $this->Form->request->here = '/articles/delete/10';
  688. $this->Form->request->base = '';
  689. $result = $this->Form->create($this->article);
  690. $expected = [
  691. 'form' => [
  692. 'method' => 'post', 'action' => '/articles/delete/10',
  693. 'accept-charset' => $encoding
  694. ],
  695. 'div' => ['style' => 'display:none;'],
  696. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  697. '/div'
  698. ];
  699. $this->assertHtml($expected, $result);
  700. $this->article['defaults'] = ['id' => 1];
  701. $this->Form->request->here = '/articles/edit/1';
  702. $this->Form->request->params['action'] = 'delete';
  703. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  704. $expected = [
  705. 'form' => [
  706. 'method' => 'post',
  707. 'action' => '/articles/edit/1',
  708. 'accept-charset' => $encoding
  709. ],
  710. 'div' => ['style' => 'display:none;'],
  711. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  712. '/div'
  713. ];
  714. $this->assertHtml($expected, $result);
  715. $this->Form->request->params['action'] = 'add';
  716. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  717. $expected = [
  718. 'form' => [
  719. 'method' => 'post',
  720. 'action' => '/articles/publish/1',
  721. 'accept-charset' => $encoding
  722. ],
  723. 'div' => ['style' => 'display:none;'],
  724. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  725. '/div'
  726. ];
  727. $this->assertHtml($expected, $result);
  728. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  729. $expected = [
  730. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  731. 'div' => ['style' => 'display:none;'],
  732. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  733. '/div'
  734. ];
  735. $this->assertHtml($expected, $result);
  736. $this->Form->request->params['controller'] = 'Pages';
  737. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  738. $expected = [
  739. 'form' => [
  740. 'method' => 'post', 'action' => '/Pages/signup/1',
  741. 'accept-charset' => $encoding
  742. ],
  743. 'div' => ['style' => 'display:none;'],
  744. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  745. '/div'
  746. ];
  747. $this->assertHtml($expected, $result);
  748. }
  749. /**
  750. * Test create() with no URL (no "action" attribute for <form> tag)
  751. *
  752. * @return void
  753. */
  754. public function testCreateNoUrl()
  755. {
  756. $result = $this->Form->create(false, ['url' => false]);
  757. $expected = [
  758. 'form' => [
  759. 'method' => 'post',
  760. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  761. ],
  762. 'div' => ['style' => 'display:none;'],
  763. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  764. '/div'
  765. ];
  766. $this->assertHtml($expected, $result);
  767. }
  768. /**
  769. * test create() with a custom route
  770. *
  771. * @return void
  772. */
  773. public function testCreateCustomRoute()
  774. {
  775. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  776. $encoding = strtolower(Configure::read('App.encoding'));
  777. $this->Form->request->params['controller'] = 'users';
  778. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  779. $expected = [
  780. 'form' => [
  781. 'method' => 'post', 'action' => '/login',
  782. 'accept-charset' => $encoding
  783. ],
  784. 'div' => ['style' => 'display:none;'],
  785. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  786. '/div'
  787. ];
  788. $this->assertHtml($expected, $result);
  789. Router::connect(
  790. '/new-article',
  791. ['controller' => 'articles', 'action' => 'myaction'],
  792. ['_name' => 'my-route']
  793. );
  794. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  795. $expected = [
  796. 'form' => [
  797. 'method' => 'post', 'action' => '/new-article',
  798. 'accept-charset' => $encoding,
  799. ],
  800. 'div' => ['style' => 'display:none;'],
  801. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  802. '/div'
  803. ];
  804. $this->assertHtml($expected, $result);
  805. }
  806. /**
  807. * test automatic accept-charset overriding
  808. *
  809. * @return void
  810. */
  811. public function testCreateWithAcceptCharset()
  812. {
  813. $result = $this->Form->create(
  814. $this->article,
  815. [
  816. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  817. ]
  818. );
  819. $expected = [
  820. 'form' => [
  821. 'method' => 'post', 'action' => '/articles',
  822. 'accept-charset' => 'iso-8859-1'
  823. ],
  824. 'div' => ['style' => 'display:none;'],
  825. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  826. '/div'
  827. ];
  828. $this->assertHtml($expected, $result);
  829. }
  830. /**
  831. * Test base form URL when url param is passed with multiple parameters (&)
  832. *
  833. */
  834. public function testCreateQueryStringRequest()
  835. {
  836. $encoding = strtolower(Configure::read('App.encoding'));
  837. $result = $this->Form->create($this->article, [
  838. 'type' => 'post',
  839. 'escape' => false,
  840. 'url' => [
  841. 'controller' => 'controller',
  842. 'action' => 'action',
  843. '?' => ['param1' => 'value1', 'param2' => 'value2']
  844. ]
  845. ]);
  846. $expected = [
  847. 'form' => [
  848. 'method' => 'post',
  849. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  850. 'accept-charset' => $encoding
  851. ],
  852. 'div' => ['style' => 'display:none;'],
  853. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  854. '/div'
  855. ];
  856. $this->assertHtml($expected, $result);
  857. $result = $this->Form->create($this->article, [
  858. 'type' => 'post',
  859. 'url' => [
  860. 'controller' => 'controller',
  861. 'action' => 'action',
  862. '?' => ['param1' => 'value1', 'param2' => 'value2']
  863. ]
  864. ]);
  865. $this->assertHtml($expected, $result);
  866. }
  867. /**
  868. * test that create() doesn't cause errors by multiple id's being in the primary key
  869. * as could happen with multiple select or checkboxes.
  870. *
  871. * @return void
  872. */
  873. public function testCreateWithMultipleIdInData()
  874. {
  875. $encoding = strtolower(Configure::read('App.encoding'));
  876. $this->Form->request->data['Article']['id'] = [1, 2];
  877. $result = $this->Form->create($this->article);
  878. $expected = [
  879. 'form' => [
  880. 'method' => 'post',
  881. 'action' => '/articles/add',
  882. 'accept-charset' => $encoding
  883. ],
  884. 'div' => ['style' => 'display:none;'],
  885. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  886. '/div'
  887. ];
  888. $this->assertHtml($expected, $result);
  889. }
  890. /**
  891. * test that create() doesn't add in extra passed params.
  892. *
  893. * @return void
  894. */
  895. public function testCreatePassedArgs()
  896. {
  897. $encoding = strtolower(Configure::read('App.encoding'));
  898. $this->Form->request->data['Article']['id'] = 1;
  899. $result = $this->Form->create($this->article, [
  900. 'type' => 'post',
  901. 'escape' => false,
  902. 'url' => [
  903. 'action' => 'edit',
  904. 'myparam'
  905. ]
  906. ]);
  907. $expected = [
  908. 'form' => [
  909. 'method' => 'post',
  910. 'action' => '/articles/edit/myparam',
  911. 'accept-charset' => $encoding
  912. ],
  913. 'div' => ['style' => 'display:none;'],
  914. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  915. '/div'
  916. ];
  917. $this->assertHtml($expected, $result);
  918. }
  919. /**
  920. * test creating a get form, and get form inputs.
  921. *
  922. * @return void
  923. */
  924. public function testGetFormCreate()
  925. {
  926. $encoding = strtolower(Configure::read('App.encoding'));
  927. $result = $this->Form->create($this->article, ['type' => 'get']);
  928. $expected = ['form' => [
  929. 'method' => 'get', 'action' => '/articles/add',
  930. 'accept-charset' => $encoding
  931. ]];
  932. $this->assertHtml($expected, $result);
  933. $result = $this->Form->text('title');
  934. $expected = ['input' => [
  935. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  936. ]];
  937. $this->assertHtml($expected, $result);
  938. $result = $this->Form->password('password');
  939. $expected = ['input' => [
  940. 'name' => 'password', 'type' => 'password'
  941. ]];
  942. $this->assertHtml($expected, $result);
  943. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  944. $result = $this->Form->text('user_form');
  945. $expected = ['input' => [
  946. 'name' => 'user_form', 'type' => 'text'
  947. ]];
  948. $this->assertHtml($expected, $result);
  949. }
  950. /**
  951. * test get form, and inputs when the model param is false
  952. *
  953. * @return void
  954. */
  955. public function testGetFormWithFalseModel()
  956. {
  957. $encoding = strtolower(Configure::read('App.encoding'));
  958. $this->Form->request->params['controller'] = 'contact_test';
  959. $result = $this->Form->create(false, [
  960. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  961. ]);
  962. $expected = ['form' => [
  963. 'method' => 'get', 'action' => '/contact_test/add',
  964. 'accept-charset' => $encoding
  965. ]];
  966. $this->assertHtml($expected, $result);
  967. $result = $this->Form->text('reason');
  968. $expected = [
  969. 'input' => ['type' => 'text', 'name' => 'reason']
  970. ];
  971. $this->assertHtml($expected, $result);
  972. }
  973. /**
  974. * testFormCreateWithSecurity method
  975. *
  976. * Test form->create() with security key.
  977. *
  978. * @return void
  979. */
  980. public function testCreateWithSecurity()
  981. {
  982. $this->Form->request->params['_csrfToken'] = 'testKey';
  983. $encoding = strtolower(Configure::read('App.encoding'));
  984. $result = $this->Form->create($this->article, [
  985. 'url' => '/articles/publish',
  986. ]);
  987. $expected = [
  988. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  989. 'div' => ['style' => 'display:none;'],
  990. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  991. ['input' => [
  992. 'type' => 'hidden',
  993. 'name' => '_csrfToken',
  994. 'value' => 'testKey',
  995. 'autocomplete' => 'off',
  996. ]],
  997. '/div'
  998. ];
  999. $this->assertHtml($expected, $result);
  1000. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1001. $expected['form']['id'] = 'MyForm';
  1002. $this->assertHtml($expected, $result);
  1003. }
  1004. /**
  1005. * testFormCreateGetNoSecurity method
  1006. *
  1007. * Test form->create() with no security key as its a get form
  1008. *
  1009. * @return void
  1010. */
  1011. public function testCreateEndGetNoSecurity()
  1012. {
  1013. $this->Form->request->params['_csrfToken'] = 'testKey';
  1014. $article = new Article();
  1015. $result = $this->Form->create($article, [
  1016. 'type' => 'get',
  1017. 'url' => '/contacts/add'
  1018. ]);
  1019. $this->assertNotContains('testKey', $result);
  1020. $result = $this->Form->end();
  1021. $this->assertNotContains('testKey', $result);
  1022. }
  1023. /**
  1024. * test that create() clears the fields property so it starts fresh
  1025. *
  1026. * @return void
  1027. */
  1028. public function testCreateClearingFields()
  1029. {
  1030. $this->Form->fields = ['model_id'];
  1031. $this->Form->create($this->article);
  1032. $this->assertEquals([], $this->Form->fields);
  1033. }
  1034. /**
  1035. * Tests form hash generation with model-less data
  1036. *
  1037. * @return void
  1038. */
  1039. public function testValidateHashNoModel()
  1040. {
  1041. $this->Form->request->params['_Token'] = 'foo';
  1042. $result = $this->Form->secure(['anything']);
  1043. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1044. }
  1045. /**
  1046. * Tests that hidden fields generated for checkboxes don't get locked
  1047. *
  1048. * @return void
  1049. */
  1050. public function testNoCheckboxLocking()
  1051. {
  1052. $this->Form->request->params['_Token'] = 'foo';
  1053. $this->assertSame([], $this->Form->fields);
  1054. $this->Form->checkbox('check', ['value' => '1']);
  1055. $this->assertSame($this->Form->fields, ['check']);
  1056. }
  1057. /**
  1058. * testFormSecurityFields method
  1059. *
  1060. * Test generation of secure form hash generation.
  1061. *
  1062. * @return void
  1063. */
  1064. public function testFormSecurityFields()
  1065. {
  1066. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1067. $this->Form->request->params['_Token'] = 'testKey';
  1068. $result = $this->Form->secure($fields);
  1069. $hash = Security::hash(serialize($fields) . Security::salt());
  1070. $hash .= ':' . 'Model.valid';
  1071. $hash = urlencode($hash);
  1072. $tokenDebug = urlencode(json_encode([
  1073. '',
  1074. $fields,
  1075. []
  1076. ]));
  1077. $expected = [
  1078. 'div' => ['style' => 'display:none;'],
  1079. ['input' => [
  1080. 'type' => 'hidden',
  1081. 'name' => '_Token[fields]',
  1082. 'value' => $hash,
  1083. 'autocomplete' => 'off',
  1084. ]],
  1085. ['input' => [
  1086. 'type' => 'hidden',
  1087. 'name' => '_Token[unlocked]',
  1088. 'value' => '',
  1089. 'autocomplete' => 'off',
  1090. ]],
  1091. ['input' => [
  1092. 'type' => 'hidden',
  1093. 'name' => '_Token[debug]',
  1094. 'value' => $tokenDebug,
  1095. 'autocomplete' => 'off',
  1096. ]],
  1097. '/div'
  1098. ];
  1099. $this->assertHtml($expected, $result);
  1100. }
  1101. /**
  1102. * testFormSecurityFields method
  1103. *
  1104. * Test debug token is not generated if debug is false
  1105. *
  1106. * @return void
  1107. */
  1108. public function testFormSecurityFieldsNoDebugMode()
  1109. {
  1110. Configure::write('debug', false);
  1111. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1112. $this->Form->request->params['_Token'] = 'testKey';
  1113. $result = $this->Form->secure($fields);
  1114. $hash = Security::hash(serialize($fields) . Security::salt());
  1115. $hash .= ':' . 'Model.valid';
  1116. $hash = urlencode($hash);
  1117. $expected = [
  1118. 'div' => ['style' => 'display:none;'],
  1119. ['input' => [
  1120. 'type' => 'hidden',
  1121. 'name' => '_Token[fields]',
  1122. 'autocomplete' => 'off',
  1123. 'value' => $hash
  1124. ]],
  1125. ['input' => [
  1126. 'type' => 'hidden',
  1127. 'name' => '_Token[unlocked]',
  1128. 'autocomplete' => 'off',
  1129. 'value' => '',
  1130. ]],
  1131. '/div'
  1132. ];
  1133. $this->assertHtml($expected, $result);
  1134. }
  1135. /**
  1136. * Tests correct generation of number fields for double and float fields
  1137. *
  1138. * @return void
  1139. */
  1140. public function testTextFieldGenerationForFloats()
  1141. {
  1142. $this->article['schema'] = [
  1143. 'foo' => [
  1144. 'type' => 'float',
  1145. 'null' => false,
  1146. 'default' => null,
  1147. 'length' => 10
  1148. ]
  1149. ];
  1150. $this->Form->create($this->article);
  1151. $result = $this->Form->control('foo');
  1152. $expected = [
  1153. 'div' => ['class' => 'input number'],
  1154. 'label' => ['for' => 'foo'],
  1155. 'Foo',
  1156. '/label',
  1157. ['input' => [
  1158. 'type' => 'number',
  1159. 'name' => 'foo',
  1160. 'id' => 'foo',
  1161. 'step' => 'any'
  1162. ]],
  1163. '/div'
  1164. ];
  1165. $this->assertHtml($expected, $result);
  1166. $result = $this->Form->control('foo', ['step' => 0.5]);
  1167. $expected = [
  1168. 'div' => ['class' => 'input number'],
  1169. 'label' => ['for' => 'foo'],
  1170. 'Foo',
  1171. '/label',
  1172. ['input' => [
  1173. 'type' => 'number',
  1174. 'name' => 'foo',
  1175. 'id' => 'foo',
  1176. 'step' => '0.5'
  1177. ]],
  1178. '/div'
  1179. ];
  1180. $this->assertHtml($expected, $result);
  1181. }
  1182. /**
  1183. * Tests correct generation of number fields for integer fields
  1184. *
  1185. * @return void
  1186. */
  1187. public function testTextFieldTypeNumberGenerationForIntegers()
  1188. {
  1189. TableRegistry::get('Contacts', [
  1190. 'className' => __NAMESPACE__ . '\ContactsTable'
  1191. ]);
  1192. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1193. $result = $this->Form->control('age');
  1194. $expected = [
  1195. 'div' => ['class' => 'input number'],
  1196. 'label' => ['for' => 'age'],
  1197. 'Age',
  1198. '/label',
  1199. ['input' => [
  1200. 'type' => 'number', 'name' => 'age',
  1201. 'id' => 'age'
  1202. ]],
  1203. '/div'
  1204. ];
  1205. $this->assertHtml($expected, $result);
  1206. }
  1207. /**
  1208. * Tests correct generation of file upload fields for binary fields
  1209. *
  1210. * @return void
  1211. */
  1212. public function testFileUploadFieldTypeGenerationForBinaries()
  1213. {
  1214. $table = TableRegistry::get('Contacts', [
  1215. 'className' => __NAMESPACE__ . '\ContactsTable'
  1216. ]);
  1217. $table->schema(['foo' => [
  1218. 'type' => 'binary',
  1219. 'null' => false,
  1220. 'default' => null,
  1221. 'length' => 1024
  1222. ]]);
  1223. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1224. $result = $this->Form->control('foo');
  1225. $expected = [
  1226. 'div' => ['class' => 'input file'],
  1227. 'label' => ['for' => 'foo'],
  1228. 'Foo',
  1229. '/label',
  1230. ['input' => [
  1231. 'type' => 'file', 'name' => 'foo',
  1232. 'id' => 'foo'
  1233. ]],
  1234. '/div'
  1235. ];
  1236. $this->assertHtml($expected, $result);
  1237. }
  1238. /**
  1239. * testFormSecurityMultipleFields method
  1240. *
  1241. * Test secure() with multiple row form. Ensure hash is correct.
  1242. *
  1243. * @return void
  1244. */
  1245. public function testFormSecurityMultipleFields()
  1246. {
  1247. $this->Form->request->params['_Token'] = 'foo';
  1248. $fields = [
  1249. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1250. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1251. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1252. ];
  1253. $result = $this->Form->secure($fields);
  1254. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1255. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1256. $tokenDebug = urlencode(json_encode([
  1257. '',
  1258. $fields,
  1259. []
  1260. ]));
  1261. $expected = [
  1262. 'div' => ['style' => 'display:none;'],
  1263. ['input' => [
  1264. 'type' => 'hidden',
  1265. 'name' => '_Token[fields]',
  1266. 'value' => $hash,
  1267. 'autocomplete' => 'off',
  1268. ]],
  1269. ['input' => [
  1270. 'type' => 'hidden',
  1271. 'name' => '_Token[unlocked]',
  1272. 'autocomplete' => 'off',
  1273. 'value' => ''
  1274. ]],
  1275. ['input' => [
  1276. 'type' => 'hidden',
  1277. 'name' => '_Token[debug]',
  1278. 'value' => $tokenDebug,
  1279. 'autocomplete' => 'off',
  1280. ]],
  1281. '/div'
  1282. ];
  1283. $this->assertHtml($expected, $result);
  1284. }
  1285. /**
  1286. * testFormSecurityMultipleSubmitButtons
  1287. *
  1288. * test form submit generation and ensure that _Token is only created on end()
  1289. *
  1290. * @return void
  1291. */
  1292. public function testFormSecurityMultipleSubmitButtons()
  1293. {
  1294. $this->Form->request->params['_Token'] = 'testKey';
  1295. $this->Form->create($this->article);
  1296. $this->Form->text('Address.title');
  1297. $this->Form->text('Address.first_name');
  1298. $result = $this->Form->submit('Save', ['name' => 'save']);
  1299. $expected = [
  1300. 'div' => ['class' => 'submit'],
  1301. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1302. '/div',
  1303. ];
  1304. $this->assertHtml($expected, $result);
  1305. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1306. $expected = [
  1307. 'div' => ['class' => 'submit'],
  1308. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1309. '/div',
  1310. ];
  1311. $this->assertHtml($expected, $result);
  1312. $result = $this->Form->end();
  1313. $tokenDebug = urlencode(json_encode([
  1314. '/articles/add',
  1315. [
  1316. 'Address.title',
  1317. 'Address.first_name',
  1318. ],
  1319. ['save', 'cancel']
  1320. ]));
  1321. $expected = [
  1322. 'div' => ['style' => 'display:none;'],
  1323. ['input' => [
  1324. 'type' => 'hidden',
  1325. 'name' => '_Token[fields]',
  1326. 'autocomplete',
  1327. 'value'
  1328. ]],
  1329. ['input' => [
  1330. 'type' => 'hidden',
  1331. 'name' => '_Token[unlocked]',
  1332. 'value' => 'cancel%7Csave',
  1333. 'autocomplete' => 'off',
  1334. ]],
  1335. ['input' => [
  1336. 'type' => 'hidden',
  1337. 'name' => '_Token[debug]',
  1338. 'value' => $tokenDebug,
  1339. 'autocomplete' => 'off',
  1340. ]],
  1341. '/div'
  1342. ];
  1343. $this->assertHtml($expected, $result);
  1344. }
  1345. /**
  1346. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1347. *
  1348. * @return void
  1349. */
  1350. public function testSecurityButtonNestedNamed()
  1351. {
  1352. $key = 'testKey';
  1353. $this->Form->request->params['_csrfToken'] = $key;
  1354. $this->Form->create('Addresses');
  1355. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1356. $result = $this->Form->unlockField();
  1357. $this->assertEquals(['Address.button'], $result);
  1358. }
  1359. /**
  1360. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1361. *
  1362. * @return void
  1363. */
  1364. public function testSecuritySubmitNestedNamed()
  1365. {
  1366. $this->Form->request->params['_Token'] = 'testKey';
  1367. $this->Form->create($this->article);
  1368. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1369. $result = $this->Form->unlockField();
  1370. $this->assertEquals(['Address.button'], $result);
  1371. }
  1372. /**
  1373. * Test that the correct fields are unlocked for image submits with no names.
  1374. *
  1375. * @return void
  1376. */
  1377. public function testSecuritySubmitImageNoName()
  1378. {
  1379. $this->Form->request->params['_Token'] = 'testKey';
  1380. $this->Form->create(false);
  1381. $result = $this->Form->submit('save.png');
  1382. $expected = [
  1383. 'div' => ['class' => 'submit'],
  1384. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1385. '/div'
  1386. ];
  1387. $this->assertHtml($expected, $result);
  1388. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1389. }
  1390. /**
  1391. * Test that the correct fields are unlocked for image submits with names.
  1392. *
  1393. * @return void
  1394. */
  1395. public function testSecuritySubmitImageName()
  1396. {
  1397. $this->Form->request->params['_Token'] = 'testKey';
  1398. $this->Form->create(null);
  1399. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1400. $expected = [
  1401. 'div' => ['class' => 'submit'],
  1402. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1403. '/div'
  1404. ];
  1405. $this->assertHtml($expected, $result);
  1406. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1407. }
  1408. /**
  1409. * testFormSecurityMultipleControlFields method
  1410. *
  1411. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1412. *
  1413. * @return void
  1414. */
  1415. public function testFormSecurityMultipleControlFields()
  1416. {
  1417. $this->Form->request->params['_Token'] = 'testKey';
  1418. $this->Form->create();
  1419. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1420. $this->Form->control('Addresses.0.title');
  1421. $this->Form->control('Addresses.0.first_name');
  1422. $this->Form->control('Addresses.0.last_name');
  1423. $this->Form->control('Addresses.0.address');
  1424. $this->Form->control('Addresses.0.city');
  1425. $this->Form->control('Addresses.0.phone');
  1426. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1427. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1428. $this->Form->control('Addresses.1.title');
  1429. $this->Form->control('Addresses.1.first_name');
  1430. $this->Form->control('Addresses.1.last_name');
  1431. $this->Form->control('Addresses.1.address');
  1432. $this->Form->control('Addresses.1.city');
  1433. $this->Form->control('Addresses.1.phone');
  1434. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1435. $result = $this->Form->secure($this->Form->fields);
  1436. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1437. $tokenDebug = urlencode(json_encode([
  1438. '/articles/add',
  1439. [
  1440. 'Addresses.0.id' => '123456',
  1441. 'Addresses.0.title',
  1442. 'Addresses.0.first_name',
  1443. 'Addresses.0.last_name',
  1444. 'Addresses.0.address',
  1445. 'Addresses.0.city',
  1446. 'Addresses.0.phone',
  1447. 'Addresses.0.primary',
  1448. 'Addresses.1.id' => '654321',
  1449. 'Addresses.1.title',
  1450. 'Addresses.1.first_name',
  1451. 'Addresses.1.last_name',
  1452. 'Addresses.1.address',
  1453. 'Addresses.1.city',
  1454. 'Addresses.1.phone',
  1455. 'Addresses.1.primary',
  1456. ],
  1457. []
  1458. ]));
  1459. $expected = [
  1460. 'div' => ['style' => 'display:none;'],
  1461. ['input' => [
  1462. 'type' => 'hidden',
  1463. 'name' => '_Token[fields]',
  1464. 'value' => $hash,
  1465. 'autocomplete' => 'off',
  1466. ]],
  1467. ['input' => [
  1468. 'type' => 'hidden',
  1469. 'name' => '_Token[unlocked]',
  1470. 'autocomplete' => 'off',
  1471. 'value' => ''
  1472. ]],
  1473. ['input' => [
  1474. 'type' => 'hidden',
  1475. 'name' => '_Token[debug]',
  1476. 'value' => $tokenDebug,
  1477. 'autocomplete' => 'off',
  1478. ]],
  1479. '/div'
  1480. ];
  1481. $this->assertHtml($expected, $result);
  1482. }
  1483. /**
  1484. * testFormSecurityArrayFields method
  1485. *
  1486. * Test form security with Model.field.0 style inputs.
  1487. *
  1488. * @return void
  1489. */
  1490. public function testFormSecurityArrayFields()
  1491. {
  1492. $this->Form->request->params['_Token'] = 'testKey';
  1493. $this->Form->create();
  1494. $this->Form->text('Address.primary.1');
  1495. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1496. $this->Form->text('Address.secondary.1.0');
  1497. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1498. }
  1499. /**
  1500. * testFormSecurityMultipleControlDisabledFields method
  1501. *
  1502. * Test secure form generation with multiple records and disabled fields.
  1503. *
  1504. * @return void
  1505. */
  1506. public function testFormSecurityMultipleControlDisabledFields()
  1507. {
  1508. $this->Form->request->params['_Token'] = [
  1509. 'unlockedFields' => ['first_name', 'address']
  1510. ];
  1511. $this->Form->create();
  1512. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1513. $this->Form->text('Addresses.0.title');
  1514. $this->Form->text('Addresses.0.first_name');
  1515. $this->Form->text('Addresses.0.last_name');
  1516. $this->Form->text('Addresses.0.address');
  1517. $this->Form->text('Addresses.0.city');
  1518. $this->Form->text('Addresses.0.phone');
  1519. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1520. $this->Form->text('Addresses.1.title');
  1521. $this->Form->text('Addresses.1.first_name');
  1522. $this->Form->text('Addresses.1.last_name');
  1523. $this->Form->text('Addresses.1.address');
  1524. $this->Form->text('Addresses.1.city');
  1525. $this->Form->text('Addresses.1.phone');
  1526. $result = $this->Form->secure($this->Form->fields);
  1527. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1528. $tokenDebug = urlencode(json_encode([
  1529. '/articles/add',
  1530. [
  1531. 'Addresses.0.id' => '123456',
  1532. 'Addresses.0.title',
  1533. 'Addresses.0.last_name',
  1534. 'Addresses.0.city',
  1535. 'Addresses.0.phone',
  1536. 'Addresses.1.id' => '654321',
  1537. 'Addresses.1.title',
  1538. 'Addresses.1.last_name',
  1539. 'Addresses.1.city',
  1540. 'Addresses.1.phone'
  1541. ],
  1542. [
  1543. 'first_name',
  1544. 'address'
  1545. ]
  1546. ]));
  1547. $expected = [
  1548. 'div' => ['style' => 'display:none;'],
  1549. ['input' => [
  1550. 'type' => 'hidden',
  1551. 'name' => '_Token[fields]',
  1552. 'autocomplete' => 'off',
  1553. 'value' => $hash
  1554. ]],
  1555. ['input' => [
  1556. 'type' => 'hidden',
  1557. 'name' => '_Token[unlocked]',
  1558. 'autocomplete' => 'off',
  1559. 'value' => 'address%7Cfirst_name',
  1560. ]],
  1561. ['input' => [
  1562. 'type' => 'hidden',
  1563. 'name' => '_Token[debug]',
  1564. 'autocomplete' => 'off',
  1565. 'value' => $tokenDebug,
  1566. ]],
  1567. '/div'
  1568. ];
  1569. $this->assertHtml($expected, $result);
  1570. }
  1571. /**
  1572. * testFormSecurityControlDisabledFields method
  1573. *
  1574. * Test single record form with disabled fields.
  1575. *
  1576. * @return void
  1577. */
  1578. public function testFormSecurityControlUnlockedFields()
  1579. {
  1580. $this->Form->request->params['_Token'] = [
  1581. 'unlockedFields' => ['first_name', 'address']
  1582. ];
  1583. $this->Form->create();
  1584. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1585. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1586. $this->Form->text('Addresses.title');
  1587. $this->Form->text('Addresses.first_name');
  1588. $this->Form->text('Addresses.last_name');
  1589. $this->Form->text('Addresses.address');
  1590. $this->Form->text('Addresses.city');
  1591. $this->Form->text('Addresses.phone');
  1592. $result = $this->Form->fields;
  1593. $expected = [
  1594. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1595. 'Addresses.city', 'Addresses.phone'
  1596. ];
  1597. $this->assertEquals($expected, $result);
  1598. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1599. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1600. $tokenDebug = urlencode(json_encode([
  1601. '/articles/add',
  1602. [
  1603. 'Addresses.id' => '123456',
  1604. 'Addresses.title',
  1605. 'Addresses.last_name',
  1606. 'Addresses.city',
  1607. 'Addresses.phone'
  1608. ],
  1609. [
  1610. 'first_name',
  1611. 'address'
  1612. ]
  1613. ]));
  1614. $expected = [
  1615. 'div' => ['style' => 'display:none;'],
  1616. ['input' => [
  1617. 'type' => 'hidden',
  1618. 'name' => '_Token[fields]',
  1619. 'value' => $hash,
  1620. 'autocomplete' => 'off',
  1621. 'data-foo' => 'bar',
  1622. ]],
  1623. ['input' => [
  1624. 'type' => 'hidden',
  1625. 'name' => '_Token[unlocked]',
  1626. 'value' => 'address%7Cfirst_name',
  1627. 'autocomplete' => 'off',
  1628. 'data-foo' => 'bar',
  1629. ]],
  1630. ['input' => [
  1631. 'type' => 'hidden', 'name' => '_Token[debug]',
  1632. 'value' => $tokenDebug,
  1633. 'autocomplete' => 'off',
  1634. 'data-foo' => 'bar'
  1635. ]],
  1636. '/div'
  1637. ];
  1638. $this->assertHtml($expected, $result);
  1639. }
  1640. /**
  1641. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1642. *
  1643. * Test single record form with debugSecurity param.
  1644. *
  1645. * @return void
  1646. */
  1647. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1648. {
  1649. $this->Form->request->params['_Token'] = [
  1650. 'unlockedFields' => ['first_name', 'address']
  1651. ];
  1652. $this->Form->create();
  1653. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1654. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1655. $this->Form->text('Addresses.title');
  1656. $this->Form->text('Addresses.first_name');
  1657. $this->Form->text('Addresses.last_name');
  1658. $this->Form->text('Addresses.address');
  1659. $this->Form->text('Addresses.city');
  1660. $this->Form->text('Addresses.phone');
  1661. $result = $this->Form->fields;
  1662. $expected = [
  1663. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1664. 'Addresses.city', 'Addresses.phone'
  1665. ];
  1666. $this->assertEquals($expected, $result);
  1667. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1668. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1669. $tokenDebug = urlencode(json_encode([
  1670. '/articles/add',
  1671. [
  1672. 'Addresses.id' => '123456',
  1673. 'Addresses.title',
  1674. 'Addresses.last_name',
  1675. 'Addresses.city',
  1676. 'Addresses.phone'
  1677. ],
  1678. [
  1679. 'first_name',
  1680. 'address'
  1681. ]
  1682. ]));
  1683. $expected = [
  1684. 'div' => ['style' => 'display:none;'],
  1685. ['input' => [
  1686. 'type' => 'hidden',
  1687. 'name' => '_Token[fields]',
  1688. 'value' => $hash,
  1689. 'autocomplete' => 'off',
  1690. 'data-foo' => 'bar',
  1691. ]],
  1692. ['input' => [
  1693. 'type' => 'hidden',
  1694. 'name' => '_Token[unlocked]',
  1695. 'value' => 'address%7Cfirst_name',
  1696. 'autocomplete' => 'off',
  1697. 'data-foo' => 'bar',
  1698. ]],
  1699. ['input' => [
  1700. 'type' => 'hidden', 'name' => '_Token[debug]',
  1701. 'value' => $tokenDebug,
  1702. 'autocomplete' => 'off',
  1703. 'data-foo' => 'bar'
  1704. ]],
  1705. '/div'
  1706. ];
  1707. $this->assertHtml($expected, $result);
  1708. }
  1709. /**
  1710. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1711. *
  1712. * Debug is false, debugSecurity is true -> no debug
  1713. *
  1714. * @return void
  1715. */
  1716. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1717. {
  1718. $this->Form->request->params['_Token'] = [
  1719. 'unlockedFields' => ['first_name', 'address']
  1720. ];
  1721. $this->Form->create();
  1722. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1723. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1724. $this->Form->text('Addresses.title');
  1725. $this->Form->text('Addresses.first_name');
  1726. $this->Form->text('Addresses.last_name');
  1727. $this->Form->text('Addresses.address');
  1728. $this->Form->text('Addresses.city');
  1729. $this->Form->text('Addresses.phone');
  1730. $result = $this->Form->fields;
  1731. $expected = [
  1732. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1733. 'Addresses.city', 'Addresses.phone'
  1734. ];
  1735. $this->assertEquals($expected, $result);
  1736. Configure::write('debug', false);
  1737. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1738. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1739. $expected = [
  1740. 'div' => ['style' => 'display:none;'],
  1741. ['input' => [
  1742. 'type' => 'hidden',
  1743. 'name' => '_Token[fields]',
  1744. 'value' => $hash,
  1745. 'autocomplete' => 'off',
  1746. 'data-foo' => 'bar',
  1747. ]],
  1748. ['input' => [
  1749. 'type' => 'hidden',
  1750. 'name' => '_Token[unlocked]',
  1751. 'value' => 'address%7Cfirst_name',
  1752. 'autocomplete' => 'off',
  1753. 'data-foo' => 'bar',
  1754. ]],
  1755. '/div'
  1756. ];
  1757. $this->assertHtml($expected, $result);
  1758. }
  1759. /**
  1760. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1761. *
  1762. * Test single record form with debugSecurity param.
  1763. *
  1764. * @return void
  1765. */
  1766. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1767. {
  1768. $this->Form->request->params['_Token'] = [
  1769. 'unlockedFields' => ['first_name', 'address']
  1770. ];
  1771. $this->Form->create();
  1772. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1773. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1774. $this->Form->text('Addresses.title');
  1775. $this->Form->text('Addresses.first_name');
  1776. $this->Form->text('Addresses.last_name');
  1777. $this->Form->text('Addresses.address');
  1778. $this->Form->text('Addresses.city');
  1779. $this->Form->text('Addresses.phone');
  1780. $result = $this->Form->fields;
  1781. $expected = [
  1782. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1783. 'Addresses.city', 'Addresses.phone'
  1784. ];
  1785. $this->assertEquals($expected, $result);
  1786. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1787. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1788. $expected = [
  1789. 'div' => ['style' => 'display:none;'],
  1790. ['input' => [
  1791. 'type' => 'hidden',
  1792. 'name' => '_Token[fields]',
  1793. 'value' => $hash,
  1794. 'autocomplete' => 'off',
  1795. 'data-foo' => 'bar',
  1796. ]],
  1797. ['input' => [
  1798. 'type' => 'hidden',
  1799. 'name' => '_Token[unlocked]',
  1800. 'value' => 'address%7Cfirst_name',
  1801. 'autocomplete' => 'off',
  1802. 'data-foo' => 'bar',
  1803. ]],
  1804. '/div'
  1805. ];
  1806. $this->assertHtml($expected, $result);
  1807. }
  1808. /**
  1809. * testFormSecureWithCustomNameAttribute method
  1810. *
  1811. * Test securing inputs with custom name attributes.
  1812. *
  1813. * @return void
  1814. */
  1815. public function testFormSecureWithCustomNameAttribute()
  1816. {
  1817. $this->Form->request->params['_Token'] = 'testKey';
  1818. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1819. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1820. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1821. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1822. }
  1823. /**
  1824. * testFormSecuredControl method
  1825. *
  1826. * Test generation of entire secure form, assertions made on control() output.
  1827. *
  1828. * @return void
  1829. */
  1830. public function testFormSecuredControl()
  1831. {
  1832. $this->Form->request->params['_csrfToken'] = 'testKey';
  1833. $this->Form->request->params['_Token'] = 'stuff';
  1834. $this->article['schema'] = [
  1835. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1836. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1837. ];
  1838. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1839. $encoding = strtolower(Configure::read('App.encoding'));
  1840. $expected = [
  1841. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1842. 'div' => ['style' => 'display:none;'],
  1843. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1844. ['input' => [
  1845. 'type' => 'hidden',
  1846. 'name' => '_csrfToken',
  1847. 'value' => 'testKey',
  1848. 'autocomplete' => 'off'
  1849. ]],
  1850. '/div'
  1851. ];
  1852. $this->assertHtml($expected, $result);
  1853. $result = $this->Form->control('ratio');
  1854. $expected = [
  1855. 'div' => ['class'],
  1856. 'label' => ['for'],
  1857. 'Ratio',
  1858. '/label',
  1859. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1860. '/div',
  1861. ];
  1862. $this->assertHtml($expected, $result);
  1863. $result = $this->Form->control('population');
  1864. $expected = [
  1865. 'div' => ['class'],
  1866. 'label' => ['for'],
  1867. 'Population',
  1868. '/label',
  1869. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1870. '/div',
  1871. ];
  1872. $this->assertHtml($expected, $result);
  1873. $result = $this->Form->control('published', ['type' => 'text']);
  1874. $expected = [
  1875. 'div' => ['class' => 'input text'],
  1876. 'label' => ['for' => 'published'],
  1877. 'Published',
  1878. '/label',
  1879. ['input' => [
  1880. 'type' => 'text',
  1881. 'name' => 'published',
  1882. 'id' => 'published'
  1883. ]],
  1884. '/div'
  1885. ];
  1886. $this->assertHtml($expected, $result);
  1887. $result = $this->Form->control('other', ['type' => 'text']);
  1888. $expected = [
  1889. 'div' => ['class' => 'input text'],
  1890. 'label' => ['for' => 'other'],
  1891. 'Other',
  1892. '/label',
  1893. ['input' => [
  1894. 'type' => 'text',
  1895. 'name' => 'other',
  1896. 'id',
  1897. ]],
  1898. '/div'
  1899. ];
  1900. $this->assertHtml($expected, $result);
  1901. $result = $this->Form->hidden('stuff');
  1902. $expected = [
  1903. 'input' => [
  1904. 'type' => 'hidden',
  1905. 'name' => 'stuff'
  1906. ]
  1907. ];
  1908. $this->assertHtml($expected, $result);
  1909. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1910. $expected = ['input' => [
  1911. 'type' => 'hidden',
  1912. 'name' => 'hidden',
  1913. 'value' => '0'
  1914. ]];
  1915. $this->assertHtml($expected, $result);
  1916. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1917. $expected = [
  1918. 'div' => ['class' => 'input checkbox'],
  1919. ['input' => [
  1920. 'type' => 'hidden',
  1921. 'name' => 'something',
  1922. 'value' => '0'
  1923. ]],
  1924. 'label' => ['for' => 'something'],
  1925. ['input' => [
  1926. 'type' => 'checkbox',
  1927. 'name' => 'something',
  1928. 'value' => '1',
  1929. 'id' => 'something'
  1930. ]],
  1931. 'Something',
  1932. '/label',
  1933. '/div'
  1934. ];
  1935. $this->assertHtml($expected, $result);
  1936. $result = $this->Form->fields;
  1937. $expectedFields = [
  1938. 'ratio',
  1939. 'population',
  1940. 'published',
  1941. 'other',
  1942. 'stuff' => '',
  1943. 'hidden' => '0',
  1944. 'something'
  1945. ];
  1946. $this->assertEquals($expectedFields, $result);
  1947. $result = $this->Form->secure($this->Form->fields);
  1948. $tokenDebug = urlencode(json_encode([
  1949. '/articles/add',
  1950. $expectedFields,
  1951. []
  1952. ]));
  1953. $expected = [
  1954. 'div' => ['style' => 'display:none;'],
  1955. ['input' => [
  1956. 'type' => 'hidden',
  1957. 'name' => '_Token[fields]',
  1958. 'value',
  1959. 'autocomplete'
  1960. ]],
  1961. ['input' => [
  1962. 'type' => 'hidden',
  1963. 'name' => '_Token[unlocked]',
  1964. 'value' => '',
  1965. 'autocomplete' => 'off'
  1966. ]],
  1967. ['input' => [
  1968. 'type' => 'hidden', 'name' => '_Token[debug]',
  1969. 'value' => $tokenDebug,
  1970. 'autocomplete' => 'off'
  1971. ]],
  1972. '/div'
  1973. ];
  1974. $this->assertHtml($expected, $result);
  1975. }
  1976. /**
  1977. * testSecuredControlCustomName method
  1978. *
  1979. * Test secured inputs with custom names.
  1980. *
  1981. * @return void
  1982. */
  1983. public function testSecuredControlCustomName()
  1984. {
  1985. $this->Form->request->params['_Token'] = 'testKey';
  1986. $this->assertEquals([], $this->Form->fields);
  1987. $this->Form->text('text_input', [
  1988. 'name' => 'Option[General.default_role]',
  1989. ]);
  1990. $expected = ['Option.General.default_role'];
  1991. $this->assertEquals($expected, $this->Form->fields);
  1992. $this->Form->select('select_box', [1, 2], [
  1993. 'name' => 'Option[General.select_role]',
  1994. ]);
  1995. $expected[] = 'Option.General.select_role';
  1996. $this->assertEquals($expected, $this->Form->fields);
  1997. $this->Form->text('other.things[]');
  1998. $expected[] = 'other.things';
  1999. $this->assertEquals($expected, $this->Form->fields);
  2000. }
  2001. /**
  2002. * testSecuredControlDuplicate method
  2003. *
  2004. * Test that a hidden field followed by a visible field
  2005. * undoes the hidden field locking.
  2006. *
  2007. * @return void
  2008. */
  2009. public function testSecuredControlDuplicate()
  2010. {
  2011. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2012. $this->assertEquals([], $this->Form->fields);
  2013. $this->Form->control('text_val', [
  2014. 'type' => 'hidden',
  2015. 'value' => 'some text',
  2016. ]);
  2017. $expected = ['text_val' => 'some text'];
  2018. $this->assertEquals($expected, $this->Form->fields);
  2019. $this->Form->control('text_val', [
  2020. 'type' => 'text',
  2021. ]);
  2022. $expected = ['text_val'];
  2023. $this->assertEquals($expected, $this->Form->fields);
  2024. }
  2025. /**
  2026. * testFormSecuredFileControl method
  2027. *
  2028. * Tests that the correct keys are added to the field hash index.
  2029. *
  2030. * @return void
  2031. */
  2032. public function testFormSecuredFileControl()
  2033. {
  2034. $this->assertEquals([], $this->Form->fields);
  2035. $this->Form->file('Attachment.file');
  2036. $expected = [
  2037. 'Attachment.file.name', 'Attachment.file.type',
  2038. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2039. 'Attachment.file.size'
  2040. ];
  2041. $this->assertEquals($expected, $this->Form->fields);
  2042. }
  2043. /**
  2044. * testFormSecuredMultipleSelect method
  2045. *
  2046. * Test that multiple selects keys are added to field hash.
  2047. *
  2048. * @return void
  2049. */
  2050. public function testFormSecuredMultipleSelect()
  2051. {
  2052. $this->Form->request->params['_csrfToken'] = 'testKey';
  2053. $this->assertEquals([], $this->Form->fields);
  2054. $options = ['1' => 'one', '2' => 'two'];
  2055. $this->Form->select('Model.select', $options);
  2056. $expected = ['Model.select'];
  2057. $this->assertEquals($expected, $this->Form->fields);
  2058. $this->Form->fields = [];
  2059. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2060. $this->assertEquals($expected, $this->Form->fields);
  2061. }
  2062. /**
  2063. * testFormSecuredRadio method
  2064. *
  2065. * @return void
  2066. */
  2067. public function testFormSecuredRadio()
  2068. {
  2069. $this->Form->request->params['_Token'] = 'testKey';
  2070. $this->assertEquals([], $this->Form->fields);
  2071. $options = ['1' => 'option1', '2' => 'option2'];
  2072. $this->Form->radio('Test.test', $options);
  2073. $expected = ['Test.test'];
  2074. $this->assertEquals($expected, $this->Form->fields);
  2075. $this->Form->radio('Test.all', $options, [
  2076. 'disabled' => ['option1', 'option2']
  2077. ]);
  2078. $expected = ['Test.test', 'Test.all' => ''];
  2079. $this->assertEquals($expected, $this->Form->fields);
  2080. $this->Form->radio('Test.some', $options, [
  2081. 'disabled' => ['option1']
  2082. ]);
  2083. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2084. $this->assertEquals($expected, $this->Form->fields);
  2085. }
  2086. /**
  2087. * testFormSecuredAndDisabledNotAssoc method
  2088. *
  2089. * Test that when disabled is in a list based attribute array it works.
  2090. *
  2091. * @return void
  2092. */
  2093. public function testFormSecuredAndDisabledNotAssoc()
  2094. {
  2095. $this->Form->request->params['_Token'] = 'testKey';
  2096. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2097. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2098. $this->Form->text('Model.text', ['disabled']);
  2099. $this->Form->textarea('Model.textarea', ['disabled']);
  2100. $this->Form->password('Model.password', ['disabled']);
  2101. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2102. $expected = [
  2103. 'Model.radio' => ''
  2104. ];
  2105. $this->assertEquals($expected, $this->Form->fields);
  2106. }
  2107. /**
  2108. * testFormSecuredAndDisabled method
  2109. *
  2110. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2111. * hashing.
  2112. *
  2113. * @return void
  2114. */
  2115. public function testFormSecuredAndDisabled()
  2116. {
  2117. $this->Form->request->params['_Token'] = 'testKey';
  2118. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2119. $this->Form->text('Model.text', ['disabled' => true]);
  2120. $this->Form->password('Model.text', ['disabled' => true]);
  2121. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2122. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2123. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2124. $this->Form->year('Model.year', ['disabled' => true]);
  2125. $this->Form->month('Model.month', ['disabled' => true]);
  2126. $this->Form->day('Model.day', ['disabled' => true]);
  2127. $this->Form->hour('Model.hour', ['disabled' => true]);
  2128. $this->Form->minute('Model.minute', ['disabled' => true]);
  2129. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2130. $expected = [
  2131. 'Model.radio' => ''
  2132. ];
  2133. $this->assertEquals($expected, $this->Form->fields);
  2134. }
  2135. /**
  2136. * testDisableSecurityUsingForm method
  2137. *
  2138. * @return void
  2139. */
  2140. public function testDisableSecurityUsingForm()
  2141. {
  2142. $this->Form->request->params['_Token'] = [
  2143. 'disabledFields' => []
  2144. ];
  2145. $this->Form->create();
  2146. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2147. $this->Form->text('Addresses.title');
  2148. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2149. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2150. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2151. $result = $this->Form->fields;
  2152. $expected = [
  2153. 'Addresses.id' => '123456', 'Addresses.title',
  2154. ];
  2155. $this->assertEquals($expected, $result);
  2156. }
  2157. /**
  2158. * testUnlockFieldAddsToList method
  2159. *
  2160. * Test disableField.
  2161. *
  2162. * @return void
  2163. */
  2164. public function testUnlockFieldAddsToList()
  2165. {
  2166. $this->Form->request->params['_Token'] = [
  2167. 'unlockedFields' => []
  2168. ];
  2169. $this->Form->unlockField('Contact.name');
  2170. $this->Form->text('Contact.name');
  2171. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2172. $this->assertEquals([], $this->Form->fields);
  2173. }
  2174. /**
  2175. * testUnlockFieldRemovingFromFields method
  2176. *
  2177. * Test unlockField removing from fields array.
  2178. *
  2179. * @return void
  2180. */
  2181. public function testUnlockFieldRemovingFromFields()
  2182. {
  2183. $this->Form->request->params['_Token'] = [
  2184. 'unlockedFields' => []
  2185. ];
  2186. $this->Form->create($this->article);
  2187. $this->Form->hidden('Article.id', ['value' => 1]);
  2188. $this->Form->text('Article.title');
  2189. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2190. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2191. $this->Form->unlockField('Article.title');
  2192. $this->Form->unlockField('Article.id');
  2193. $this->assertEquals([], $this->Form->fields);
  2194. }
  2195. /**
  2196. * testResetUnlockFields method
  2197. *
  2198. * Test reset unlockFields, when create new form.
  2199. *
  2200. * @return void
  2201. */
  2202. public function testResetUnlockFields()
  2203. {
  2204. $this->Form->request->params['_Token'] = [
  2205. 'key' => 'testKey',
  2206. 'unlockedFields' => []
  2207. ];
  2208. $this->Form->unlockField('Contact.id');
  2209. $this->Form->create('Contact');
  2210. $this->Form->hidden('Contact.id', ['value' => 1]);
  2211. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2212. $this->Form->end();
  2213. $this->Form->create('Contact');
  2214. $this->Form->hidden('Contact.id', ['value' => 1]);
  2215. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2216. }
  2217. /**
  2218. * testSecuredFormUrlIgnoresHost method
  2219. *
  2220. * Test that only the path + query elements of a form's URL show up in their hash.
  2221. *
  2222. * @return void
  2223. */
  2224. public function testSecuredFormUrlIgnoresHost()
  2225. {
  2226. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2227. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2228. $this->Form->create($this->article, [
  2229. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2230. ]);
  2231. $result = $this->Form->secure();
  2232. $this->assertContains($expected, $result);
  2233. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2234. $result = $this->Form->secure();
  2235. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2236. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2237. $result = $this->Form->secure();
  2238. $this->assertContains($expected, $result, 'URL path + query should work.');
  2239. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2240. $result = $this->Form->secure();
  2241. $this->assertNotContains($expected, $result, 'URL is different');
  2242. }
  2243. /**
  2244. * testSecuredFormUrlHasHtmlAndIdentifier method
  2245. *
  2246. * Test that URL, HTML and identifier show up in their hashes.
  2247. *
  2248. * @return void
  2249. */
  2250. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2251. {
  2252. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2253. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2254. $this->Form->create($this->article, [
  2255. 'url' => [
  2256. 'controller' => 'articles',
  2257. 'action' => 'view',
  2258. '?' => [
  2259. 'page' => 1,
  2260. 'limit' => 10,
  2261. 'html' => '<>"',
  2262. ],
  2263. '#' => 'result',
  2264. ],
  2265. ]);
  2266. $result = $this->Form->secure();
  2267. $this->assertContains($expected, $result);
  2268. $this->Form->create($this->article, [
  2269. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2270. ]);
  2271. $result = $this->Form->secure();
  2272. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2273. $this->Form->create($this->article, [
  2274. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2275. ]);
  2276. $result = $this->Form->secure();
  2277. $this->assertContains($expected, $result, 'URL path + query should work.');
  2278. }
  2279. /**
  2280. * testErrorMessageDisplay method
  2281. *
  2282. * Test error message display.
  2283. *
  2284. * @return void
  2285. */
  2286. public function testErrorMessageDisplay()
  2287. {
  2288. $this->article['errors'] = [
  2289. 'Article' => [
  2290. 'title' => 'error message',
  2291. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2292. ]
  2293. ];
  2294. $this->Form->create($this->article);
  2295. $result = $this->Form->control('Article.title');
  2296. $expected = [
  2297. 'div' => ['class' => 'input text error'],
  2298. 'label' => ['for' => 'article-title'],
  2299. 'Title',
  2300. '/label',
  2301. 'input' => [
  2302. 'type' => 'text', 'name' => 'Article[title]',
  2303. 'id' => 'article-title', 'class' => 'form-error'
  2304. ],
  2305. ['div' => ['class' => 'error-message']],
  2306. 'error message',
  2307. '/div',
  2308. '/div'
  2309. ];
  2310. $this->assertHtml($expected, $result);
  2311. $result = $this->Form->control('Article.title', [
  2312. 'templates' => [
  2313. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2314. ]
  2315. ]);
  2316. $expected = [
  2317. 'div' => ['class' => 'input text error'],
  2318. 'label' => ['for' => 'article-title'],
  2319. 'Title',
  2320. '/label',
  2321. 'input' => [
  2322. 'type' => 'text', 'name' => 'Article[title]',
  2323. 'id' => 'article-title', 'class' => 'form-error'
  2324. ],
  2325. '/div'
  2326. ];
  2327. $this->assertHtml($expected, $result);
  2328. $result = $this->Form->control('Article.content');
  2329. $expected = [
  2330. 'div' => ['class' => 'input text error'],
  2331. 'label' => ['for' => 'article-content'],
  2332. 'Content',
  2333. '/label',
  2334. 'input' => [
  2335. 'type' => 'text', 'name' => 'Article[content]',
  2336. 'id' => 'article-content', 'class' => 'form-error'
  2337. ],
  2338. ['div' => ['class' => 'error-message']],
  2339. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2340. '/div',
  2341. '/div'
  2342. ];
  2343. $this->assertHtml($expected, $result);
  2344. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2345. $expected = [
  2346. 'div' => ['class' => 'input text error'],
  2347. 'label' => ['for' => 'article-content'],
  2348. 'Content',
  2349. '/label',
  2350. 'input' => [
  2351. 'type' => 'text', 'name' => 'Article[content]',
  2352. 'id' => 'article-content', 'class' => 'form-error'
  2353. ],
  2354. ['div' => ['class' => 'error-message']],
  2355. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2356. '/div',
  2357. '/div'
  2358. ];
  2359. $this->assertHtml($expected, $result);
  2360. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2361. $expected = [
  2362. 'div' => ['class' => 'input text error'],
  2363. 'label' => ['for' => 'article-content'],
  2364. 'Content',
  2365. '/label',
  2366. 'input' => [
  2367. 'type' => 'text', 'name' => 'Article[content]',
  2368. 'id' => 'article-content', 'class' => 'form-error'
  2369. ],
  2370. ['div' => ['class' => 'error-message']],
  2371. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2372. '/div',
  2373. '/div'
  2374. ];
  2375. $this->assertHtml($expected, $result);
  2376. }
  2377. /**
  2378. * testEmptyErrorValidation method
  2379. *
  2380. * Test validation errors, when validation message is an empty string.
  2381. *
  2382. * @return void
  2383. */
  2384. public function testEmptyErrorValidation()
  2385. {
  2386. $this->article['errors'] = [
  2387. 'Article' => ['title' => '']
  2388. ];
  2389. $this->Form->create($this->article);
  2390. $result = $this->Form->control('Article.title');
  2391. $expected = [
  2392. 'div' => ['class' => 'input text error'],
  2393. 'label' => ['for' => 'article-title'],
  2394. 'Title',
  2395. '/label',
  2396. 'input' => [
  2397. 'type' => 'text', 'name' => 'Article[title]',
  2398. 'id' => 'article-title', 'class' => 'form-error'
  2399. ],
  2400. ['div' => ['class' => 'error-message']],
  2401. [],
  2402. '/div',
  2403. '/div'
  2404. ];
  2405. $this->assertHtml($expected, $result);
  2406. }
  2407. /**
  2408. * testEmptyControlErrorValidation method
  2409. *
  2410. * Test validation errors, when calling control() overriding validation message by an empty string.
  2411. *
  2412. * @return void
  2413. */
  2414. public function testEmptyControlErrorValidation()
  2415. {
  2416. $this->article['errors'] = [
  2417. 'Article' => ['title' => 'error message']
  2418. ];
  2419. $this->Form->create($this->article);
  2420. $result = $this->Form->control('Article.title', ['error' => '']);
  2421. $expected = [
  2422. 'div' => ['class' => 'input text error'],
  2423. 'label' => ['for' => 'article-title'],
  2424. 'Title',
  2425. '/label',
  2426. 'input' => [
  2427. 'type' => 'text', 'name' => 'Article[title]',
  2428. 'id' => 'article-title', 'class' => 'form-error'
  2429. ],
  2430. ['div' => ['class' => 'error-message']],
  2431. [],
  2432. '/div',
  2433. '/div'
  2434. ];
  2435. $this->assertHtml($expected, $result);
  2436. }
  2437. /**
  2438. * testControlErrorMessage method
  2439. *
  2440. * Test validation errors, when calling control() overriding validation messages.
  2441. *
  2442. * @return void
  2443. */
  2444. public function testControlErrorMessage()
  2445. {
  2446. $this->article['errors'] = [
  2447. 'title' => ['error message']
  2448. ];
  2449. $this->Form->create($this->article);
  2450. $result = $this->Form->control('title', [
  2451. 'error' => 'Custom error!'
  2452. ]);
  2453. $expected = [
  2454. 'div' => ['class' => 'input text required error'],
  2455. 'label' => ['for' => 'title'],
  2456. 'Title',
  2457. '/label',
  2458. 'input' => [
  2459. 'type' => 'text', 'name' => 'title',
  2460. 'id' => 'title', 'class' => 'form-error',
  2461. 'required' => 'required',
  2462. ],
  2463. ['div' => ['class' => 'error-message']],
  2464. 'Custom error!',
  2465. '/div',
  2466. '/div'
  2467. ];
  2468. $this->assertHtml($expected, $result);
  2469. $result = $this->Form->control('title', [
  2470. 'error' => ['error message' => 'Custom error!']
  2471. ]);
  2472. $expected = [
  2473. 'div' => ['class' => 'input text required error'],
  2474. 'label' => ['for' => 'title'],
  2475. 'Title',
  2476. '/label',
  2477. 'input' => [
  2478. 'type' => 'text',
  2479. 'name' => 'title',
  2480. 'id' => 'title',
  2481. 'class' => 'form-error',
  2482. 'required' => 'required'
  2483. ],
  2484. ['div' => ['class' => 'error-message']],
  2485. 'Custom error!',
  2486. '/div',
  2487. '/div'
  2488. ];
  2489. $this->assertHtml($expected, $result);
  2490. }
  2491. /**
  2492. * testFormValidationAssociated method
  2493. *
  2494. * Tests displaying errors for nested entities.
  2495. *
  2496. * @return void
  2497. */
  2498. public function testFormValidationAssociated()
  2499. {
  2500. $nested = new Entity(['foo' => 'bar']);
  2501. $nested->errors('foo', ['not a valid bar']);
  2502. $entity = new Entity(['nested' => $nested]);
  2503. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2504. $result = $this->Form->error('nested.foo');
  2505. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2506. }
  2507. /**
  2508. * testFormValidationAssociatedSecondLevel method
  2509. *
  2510. * Test form error display with associated model.
  2511. *
  2512. * @return void
  2513. */
  2514. public function testFormValidationAssociatedSecondLevel()
  2515. {
  2516. $inner = new Entity(['bar' => 'baz']);
  2517. $nested = new Entity(['foo' => $inner]);
  2518. $entity = new Entity(['nested' => $nested]);
  2519. $inner->errors('bar', ['not a valid one']);
  2520. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2521. $result = $this->Form->error('nested.foo.bar');
  2522. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2523. }
  2524. /**
  2525. * testFormValidationMultiRecord method
  2526. *
  2527. * Test form error display with multiple records.
  2528. *
  2529. * @return void
  2530. */
  2531. public function testFormValidationMultiRecord()
  2532. {
  2533. $one = new Entity();
  2534. $two = new Entity();
  2535. TableRegistry::get('Contacts', [
  2536. 'className' => __NAMESPACE__ . '\ContactsTable'
  2537. ]);
  2538. $one->set('email', '');
  2539. $one->errors('email', ['invalid email']);
  2540. $two->set('name', '');
  2541. $two->errors('name', ['This is wrong']);
  2542. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2543. $result = $this->Form->control('0.email');
  2544. $expected = [
  2545. 'div' => ['class' => 'input email error'],
  2546. 'label' => ['for' => '0-email'],
  2547. 'Email',
  2548. '/label',
  2549. 'input' => [
  2550. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2551. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2552. ],
  2553. ['div' => ['class' => 'error-message']],
  2554. 'invalid email',
  2555. '/div',
  2556. '/div'
  2557. ];
  2558. $this->assertHtml($expected, $result);
  2559. $result = $this->Form->control('1.name');
  2560. $expected = [
  2561. 'div' => ['class' => 'input text error'],
  2562. 'label' => ['for' => '1-name'],
  2563. 'Name',
  2564. '/label',
  2565. 'input' => [
  2566. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2567. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2568. ],
  2569. ['div' => ['class' => 'error-message']],
  2570. 'This is wrong',
  2571. '/div',
  2572. '/div'
  2573. ];
  2574. $this->assertHtml($expected, $result);
  2575. }
  2576. /**
  2577. * testControl method
  2578. *
  2579. * Test various incarnations of control().
  2580. *
  2581. * @return void
  2582. */
  2583. public function testControl()
  2584. {
  2585. TableRegistry::get('ValidateUsers', [
  2586. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2587. ]);
  2588. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2589. $result = $this->Form->control('ValidateUsers.balance');
  2590. $expected = [
  2591. 'div' => ['class'],
  2592. 'label' => ['for'],
  2593. 'Balance',
  2594. '/label',
  2595. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2596. '/div',
  2597. ];
  2598. $this->assertHtml($expected, $result);
  2599. $result = $this->Form->control('ValidateUser.cost_decimal');
  2600. $expected = [
  2601. 'div' => ['class'],
  2602. 'label' => ['for'],
  2603. 'Cost Decimal',
  2604. '/label',
  2605. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2606. '/div',
  2607. ];
  2608. $this->assertHtml($expected, $result);
  2609. $result = $this->Form->control('ValidateUser.null_decimal');
  2610. $expected = [
  2611. 'div' => ['class'],
  2612. 'label' => ['for'],
  2613. 'Null Decimal',
  2614. '/label',
  2615. 'input' => ['name', 'type' => 'number', 'id'],
  2616. '/div',
  2617. ];
  2618. $this->assertHtml($expected, $result);
  2619. }
  2620. /**
  2621. * testControlCustomization method
  2622. *
  2623. * Tests the input method and passing custom options.
  2624. *
  2625. * @return void
  2626. */
  2627. public function testControlCustomization()
  2628. {
  2629. TableRegistry::get('Contacts', [
  2630. 'className' => __NAMESPACE__ . '\ContactsTable'
  2631. ]);
  2632. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2633. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2634. $expected = [
  2635. 'div' => ['class' => 'input email'],
  2636. 'label' => ['for' => 'custom'],
  2637. 'Email',
  2638. '/label',
  2639. ['input' => [
  2640. 'type' => 'email', 'name' => 'Contact[email]',
  2641. 'id' => 'custom', 'maxlength' => 255
  2642. ]],
  2643. '/div'
  2644. ];
  2645. $this->assertHtml($expected, $result);
  2646. $result = $this->Form->control('Contact.email', [
  2647. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2648. ]);
  2649. $expected = [
  2650. '<div',
  2651. 'label' => ['for' => 'contact-email'],
  2652. 'Email',
  2653. '/label',
  2654. ['input' => [
  2655. 'type' => 'email', 'name' => 'Contact[email]',
  2656. 'id' => 'contact-email', 'maxlength' => 255
  2657. ]],
  2658. '/div'
  2659. ];
  2660. $this->assertHtml($expected, $result);
  2661. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2662. $expected = [
  2663. 'div' => ['class' => 'input text'],
  2664. 'label' => ['for' => 'contact-email'],
  2665. 'Email',
  2666. '/label',
  2667. ['input' => [
  2668. 'type' => 'text', 'name' => 'Contact[email]',
  2669. 'id' => 'contact-email', 'maxlength' => '255'
  2670. ]],
  2671. '/div'
  2672. ];
  2673. $this->assertHtml($expected, $result);
  2674. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2675. $expected = [
  2676. 'div' => ['class' => 'input text'],
  2677. 'label' => ['for' => 'contact-5-email'],
  2678. 'Email',
  2679. '/label',
  2680. ['input' => [
  2681. 'type' => 'text', 'name' => 'Contact[5][email]',
  2682. 'id' => 'contact-5-email', 'maxlength' => '255'
  2683. ]],
  2684. '/div'
  2685. ];
  2686. $this->assertHtml($expected, $result);
  2687. $result = $this->Form->control('Contact.password');
  2688. $expected = [
  2689. 'div' => ['class' => 'input password'],
  2690. 'label' => ['for' => 'contact-password'],
  2691. 'Password',
  2692. '/label',
  2693. ['input' => [
  2694. 'type' => 'password', 'name' => 'Contact[password]',
  2695. 'id' => 'contact-password'
  2696. ]],
  2697. '/div'
  2698. ];
  2699. $this->assertHtml($expected, $result);
  2700. $result = $this->Form->control('Contact.email', [
  2701. 'type' => 'file', 'class' => 'textbox'
  2702. ]);
  2703. $expected = [
  2704. 'div' => ['class' => 'input file'],
  2705. 'label' => ['for' => 'contact-email'],
  2706. 'Email',
  2707. '/label',
  2708. ['input' => [
  2709. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2710. 'id' => 'contact-email'
  2711. ]],
  2712. '/div'
  2713. ];
  2714. $this->assertHtml($expected, $result);
  2715. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2716. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2717. $result = $this->Form->control('phone');
  2718. $expected = [
  2719. 'div' => ['class' => 'input tel'],
  2720. 'label' => ['for' => 'phone'],
  2721. 'Phone',
  2722. '/label',
  2723. ['input' => [
  2724. 'type' => 'tel', 'name' => 'phone',
  2725. 'value' => 'Hello &amp; World &gt; weird chars',
  2726. 'id' => 'phone', 'maxlength' => 255
  2727. ]],
  2728. '/div'
  2729. ];
  2730. $this->assertHtml($expected, $result);
  2731. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2732. $this->Form->create();
  2733. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2734. $expected = [
  2735. 'div' => ['class' => 'input text'],
  2736. 'label' => ['for' => 'myId'],
  2737. 'Field',
  2738. '/label',
  2739. 'input' => [
  2740. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2741. 'value' => 'My value', 'id' => 'myId'
  2742. ],
  2743. '/div'
  2744. ];
  2745. $this->assertHtml($expected, $result);
  2746. $this->Form->request->data = [];
  2747. $entity->errors('field', 'Badness!');
  2748. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2749. $result = $this->Form->control('field');
  2750. $expected = [
  2751. 'div' => ['class' => 'input text error'],
  2752. 'label' => ['for' => 'field'],
  2753. 'Field',
  2754. '/label',
  2755. 'input' => [
  2756. 'type' => 'text', 'name' => 'field',
  2757. 'id' => 'field', 'class' => 'form-error'
  2758. ],
  2759. ['div' => ['class' => 'error-message']],
  2760. 'Badness!',
  2761. '/div',
  2762. '/div'
  2763. ];
  2764. $this->assertHtml($expected, $result);
  2765. $result = $this->Form->control('field', [
  2766. 'templates' => [
  2767. 'inputContainerError' => '{{content}}{{error}}',
  2768. 'error' => '<span class="error-message">{{content}}</span>'
  2769. ]
  2770. ]);
  2771. $expected = [
  2772. 'label' => ['for' => 'field'],
  2773. 'Field',
  2774. '/label',
  2775. 'input' => [
  2776. 'type' => 'text', 'name' => 'field',
  2777. 'id' => 'field', 'class' => 'form-error'
  2778. ],
  2779. ['span' => ['class' => 'error-message']],
  2780. 'Badness!',
  2781. '/span'
  2782. ];
  2783. $this->assertHtml($expected, $result);
  2784. $entity->errors('field', ['minLength'], true);
  2785. $result = $this->Form->control('field', [
  2786. 'error' => [
  2787. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2788. 'maxLength' => 'login too large'
  2789. ]
  2790. ]);
  2791. $expected = [
  2792. 'div' => ['class' => 'input text error'],
  2793. 'label' => ['for' => 'field'],
  2794. 'Field',
  2795. '/label',
  2796. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2797. ['div' => ['class' => 'error-message']],
  2798. 'Le login doit contenir au moins 2 caractères',
  2799. '/div',
  2800. '/div'
  2801. ];
  2802. $this->assertHtml($expected, $result);
  2803. $entity->errors('field', ['maxLength'], true);
  2804. $result = $this->Form->control('field', [
  2805. 'error' => [
  2806. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2807. 'maxLength' => 'login too large',
  2808. ]
  2809. ]);
  2810. $expected = [
  2811. 'div' => ['class' => 'input text error'],
  2812. 'label' => ['for' => 'field'],
  2813. 'Field',
  2814. '/label',
  2815. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2816. ['div' => ['class' => 'error-message']],
  2817. 'login too large',
  2818. '/div',
  2819. '/div'
  2820. ];
  2821. $this->assertHtml($expected, $result);
  2822. }
  2823. /**
  2824. * testControlWithTemplateFile method
  2825. *
  2826. * Test that control() accepts a template file.
  2827. *
  2828. * @return void
  2829. */
  2830. public function testControlWithTemplateFile()
  2831. {
  2832. $result = $this->Form->control('field', [
  2833. 'templates' => 'htmlhelper_tags'
  2834. ]);
  2835. $expected = [
  2836. 'label' => ['for' => 'field'],
  2837. 'Field',
  2838. '/label',
  2839. 'input' => [
  2840. 'type' => 'text', 'name' => 'field',
  2841. 'id' => 'field'
  2842. ],
  2843. ];
  2844. $this->assertHtml($expected, $result);
  2845. }
  2846. /**
  2847. * testNestedControlsEndWithBrackets method
  2848. *
  2849. * Test that nested inputs end with brackets.
  2850. *
  2851. * @return void
  2852. */
  2853. public function testNestedControlsEndWithBrackets()
  2854. {
  2855. $result = $this->Form->text('nested.text[]');
  2856. $expected = [
  2857. 'input' => [
  2858. 'type' => 'text', 'name' => 'nested[text][]'
  2859. ],
  2860. ];
  2861. $this->assertHtml($expected, $result);
  2862. $result = $this->Form->file('nested.file[]');
  2863. $expected = [
  2864. 'input' => [
  2865. 'type' => 'file', 'name' => 'nested[file][]'
  2866. ],
  2867. ];
  2868. $this->assertHtml($expected, $result);
  2869. }
  2870. /**
  2871. * testCreateIdPrefix method
  2872. *
  2873. * Test id prefix.
  2874. *
  2875. * @return void
  2876. */
  2877. public function testCreateIdPrefix()
  2878. {
  2879. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2880. $result = $this->Form->control('field');
  2881. $expected = [
  2882. 'div' => ['class' => 'input text'],
  2883. 'label' => ['for' => 'prefix-field'],
  2884. 'Field',
  2885. '/label',
  2886. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2887. '/div'
  2888. ];
  2889. $this->assertHtml($expected, $result);
  2890. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2891. $expected = [
  2892. 'div' => ['class' => 'input text'],
  2893. 'label' => ['for' => 'custom-id'],
  2894. 'Field',
  2895. '/label',
  2896. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2897. '/div'
  2898. ];
  2899. $this->assertHtml($expected, $result);
  2900. $result = $this->Form->radio('Model.field', ['option A']);
  2901. $expected = [
  2902. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2903. 'label' => ['for' => 'prefix-model-field-0'],
  2904. ['input' => [
  2905. 'type' => 'radio',
  2906. 'name' => 'Model[field]',
  2907. 'value' => '0',
  2908. 'id' => 'prefix-model-field-0'
  2909. ]],
  2910. 'option A',
  2911. '/label'
  2912. ];
  2913. $this->assertHtml($expected, $result);
  2914. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2915. $expected = [
  2916. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2917. 'label' => ['for' => 'prefix-model-field-0'],
  2918. ['input' => [
  2919. 'type' => 'radio',
  2920. 'name' => 'Model[field]',
  2921. 'value' => '0',
  2922. 'id' => 'prefix-model-field-0'
  2923. ]],
  2924. 'option A',
  2925. '/label'
  2926. ];
  2927. $this->assertHtml($expected, $result);
  2928. $result = $this->Form->select(
  2929. 'Model.multi_field',
  2930. ['first'],
  2931. ['multiple' => 'checkbox']
  2932. );
  2933. $expected = [
  2934. 'input' => [
  2935. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2936. ],
  2937. ['div' => ['class' => 'checkbox']],
  2938. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2939. ['input' => [
  2940. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2941. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2942. ]],
  2943. 'first',
  2944. '/label',
  2945. '/div',
  2946. ];
  2947. $this->assertHtml($expected, $result);
  2948. $this->Form->end();
  2949. $result = $this->Form->control('field');
  2950. $expected = [
  2951. 'div' => ['class' => 'input text'],
  2952. 'label' => ['for' => 'field'],
  2953. 'Field',
  2954. '/label',
  2955. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2956. '/div'
  2957. ];
  2958. $this->assertHtml($expected, $result);
  2959. }
  2960. /**
  2961. * testControlZero method
  2962. *
  2963. * Test that inputs with 0 can be created.
  2964. *
  2965. * @return void
  2966. */
  2967. public function testControlZero()
  2968. {
  2969. TableRegistry::get('Contacts', [
  2970. 'className' => __NAMESPACE__ . '\ContactsTable'
  2971. ]);
  2972. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2973. $result = $this->Form->control('0');
  2974. $expected = [
  2975. 'div' => ['class' => 'input text'],
  2976. 'label' => ['for' => '0'], '/label',
  2977. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2978. '/div'
  2979. ];
  2980. $this->assertHtml($expected, $result);
  2981. }
  2982. /**
  2983. * testControlCheckbox method
  2984. *
  2985. * Test control() with checkbox creation.
  2986. *
  2987. * @return void
  2988. */
  2989. public function testControlCheckbox()
  2990. {
  2991. $articles = TableRegistry::get('Articles');
  2992. $articles->schema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2993. $article = $articles->newEntity();
  2994. $this->Form->create($article);
  2995. $result = $this->Form->control('Articles.active');
  2996. $expected = [
  2997. 'div' => ['class' => 'input checkbox'],
  2998. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  2999. 'label' => ['for' => 'articles-active'],
  3000. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3001. 'Active',
  3002. '/label',
  3003. '/div'
  3004. ];
  3005. $this->assertHtml($expected, $result);
  3006. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3007. $expected = [
  3008. 'div' => ['class' => 'input checkbox'],
  3009. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3010. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3011. '/div'
  3012. ];
  3013. $this->assertHtml($expected, $result);
  3014. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3015. $expected = [
  3016. 'div' => ['class' => 'input checkbox'],
  3017. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3018. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3019. '/div'
  3020. ];
  3021. $this->assertHtml($expected, $result);
  3022. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3023. $expected = [
  3024. 'div' => ['class' => 'input checkbox'],
  3025. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3026. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3027. '/div'
  3028. ];
  3029. $this->assertHtml($expected, $result);
  3030. $result = $this->Form->control('Articles.disabled', [
  3031. 'label' => 'Disabled',
  3032. 'type' => 'checkbox',
  3033. 'data-foo' => 'disabled'
  3034. ]);
  3035. $expected = [
  3036. 'div' => ['class' => 'input checkbox'],
  3037. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3038. 'label' => ['for' => 'articles-disabled'],
  3039. ['input' => [
  3040. 'type' => 'checkbox',
  3041. 'name' => 'Articles[disabled]',
  3042. 'value' => '1',
  3043. 'id' => 'articles-disabled',
  3044. 'data-foo' => 'disabled'
  3045. ]],
  3046. 'Disabled',
  3047. '/label',
  3048. '/div'
  3049. ];
  3050. $this->assertHtml($expected, $result);
  3051. $result = $this->Form->control('Articles.confirm', [
  3052. 'label' => 'Confirm <b>me</b>!',
  3053. 'type' => 'checkbox',
  3054. 'escape' => false
  3055. ]);
  3056. $expected = [
  3057. 'div' => ['class' => 'input checkbox'],
  3058. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3059. 'label' => ['for' => 'articles-confirm'],
  3060. ['input' => [
  3061. 'type' => 'checkbox',
  3062. 'name' => 'Articles[confirm]',
  3063. 'value' => '1',
  3064. 'id' => 'articles-confirm',
  3065. ]],
  3066. 'Confirm <b>me</b>!',
  3067. '/label',
  3068. '/div'
  3069. ];
  3070. $this->assertHtml($expected, $result);
  3071. }
  3072. /**
  3073. * testControlHidden method
  3074. *
  3075. * Test that control() does not create wrapping div and label tag for hidden fields.
  3076. *
  3077. * @return void
  3078. */
  3079. public function testControlHidden()
  3080. {
  3081. TableRegistry::get('ValidateUsers', [
  3082. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3083. ]);
  3084. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3085. $result = $this->Form->control('ValidateUser.id');
  3086. $expected = [
  3087. 'input' => ['name', 'type' => 'hidden', 'id']
  3088. ];
  3089. $this->assertHtml($expected, $result);
  3090. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3091. $expected = [
  3092. 'input' => ['name', 'type' => 'hidden', 'id']
  3093. ];
  3094. $this->assertHtml($expected, $result);
  3095. }
  3096. /**
  3097. * testControlDatetime method
  3098. *
  3099. * Test form->control() with datetime.
  3100. *
  3101. * @return void
  3102. */
  3103. public function testControlDatetime()
  3104. {
  3105. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3106. ->setMethods(['datetime'])
  3107. ->setConstructorArgs([new View()])
  3108. ->getMock();
  3109. $this->Form->expects($this->once())->method('datetime')
  3110. ->with('prueba', [
  3111. 'type' => 'datetime',
  3112. 'timeFormat' => 24,
  3113. 'minYear' => 2008,
  3114. 'maxYear' => 2011,
  3115. 'interval' => 15,
  3116. 'options' => null,
  3117. 'empty' => false,
  3118. 'id' => 'prueba',
  3119. 'required' => false,
  3120. 'templateVars' => []
  3121. ])
  3122. ->will($this->returnValue('This is it!'));
  3123. $result = $this->Form->control('prueba', [
  3124. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3125. 'maxYear' => 2011, 'interval' => 15
  3126. ]);
  3127. $expected = [
  3128. 'div' => ['class' => 'input datetime'],
  3129. '<label',
  3130. 'Prueba',
  3131. '/label',
  3132. 'This is it!',
  3133. '/div'
  3134. ];
  3135. $this->assertHtml($expected, $result);
  3136. }
  3137. /**
  3138. * testControlDatetimeIdPrefix method
  3139. *
  3140. * Test form->control() with datetime with id prefix.
  3141. *
  3142. * @return void
  3143. */
  3144. public function testControlDatetimeIdPrefix()
  3145. {
  3146. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3147. ->setMethods(['datetime'])
  3148. ->setConstructorArgs([new View()])
  3149. ->getMock();
  3150. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3151. $this->Form->expects($this->once())->method('datetime')
  3152. ->with('prueba', [
  3153. 'type' => 'datetime',
  3154. 'timeFormat' => 24,
  3155. 'minYear' => 2008,
  3156. 'maxYear' => 2011,
  3157. 'interval' => 15,
  3158. 'options' => null,
  3159. 'empty' => false,
  3160. 'id' => 'prefix-prueba',
  3161. 'required' => false,
  3162. 'templateVars' => []
  3163. ])
  3164. ->will($this->returnValue('This is it!'));
  3165. $result = $this->Form->control('prueba', [
  3166. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3167. 'maxYear' => 2011, 'interval' => 15
  3168. ]);
  3169. $expected = [
  3170. 'div' => ['class' => 'input datetime'],
  3171. '<label',
  3172. 'Prueba',
  3173. '/label',
  3174. 'This is it!',
  3175. '/div'
  3176. ];
  3177. $this->assertHtml($expected, $result);
  3178. }
  3179. /**
  3180. * testControlCheckboxWithDisabledElements method
  3181. *
  3182. * Test generating checkboxes with disabled elements.
  3183. *
  3184. * @return void
  3185. */
  3186. public function testControlCheckboxWithDisabledElements()
  3187. {
  3188. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3189. $result = $this->Form->control('Contact.multiple', [
  3190. 'multiple' => 'checkbox',
  3191. 'disabled' => 'disabled',
  3192. 'options' => $options
  3193. ]);
  3194. $expected = [
  3195. ['div' => ['class' => 'input select']],
  3196. ['label' => ['for' => "contact-multiple"]],
  3197. 'Multiple',
  3198. '/label',
  3199. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  3200. ['div' => ['class' => 'checkbox']],
  3201. ['label' => ['for' => "contact-multiple-1"]],
  3202. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  3203. 'One',
  3204. '/label',
  3205. '/div',
  3206. ['div' => ['class' => 'checkbox']],
  3207. ['label' => ['for' => "contact-multiple-2"]],
  3208. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  3209. 'Two',
  3210. '/label',
  3211. '/div',
  3212. ['div' => ['class' => 'checkbox']],
  3213. ['label' => ['for' => "contact-multiple-3"]],
  3214. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  3215. 'Three',
  3216. '/label',
  3217. '/div',
  3218. '/div'
  3219. ];
  3220. $this->assertHtml($expected, $result);
  3221. // make sure 50 does only disable 50, and not 50f5c0cf
  3222. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3223. $disabled = [50];
  3224. $expected = [
  3225. ['div' => ['class' => 'input select']],
  3226. ['label' => ['for' => "contact-multiple"]],
  3227. 'Multiple',
  3228. '/label',
  3229. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  3230. ['div' => ['class' => 'checkbox']],
  3231. ['label' => ['for' => "contact-multiple-50"]],
  3232. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  3233. 'Fifty',
  3234. '/label',
  3235. '/div',
  3236. ['div' => ['class' => 'checkbox']],
  3237. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  3238. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  3239. 'Stringy',
  3240. '/label',
  3241. '/div',
  3242. '/div'
  3243. ];
  3244. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3245. $this->assertHtml($expected, $result);
  3246. }
  3247. /**
  3248. * testControlWithLeadingInteger method
  3249. *
  3250. * Test input name with leading integer, ensure attributes are generated correctly.
  3251. *
  3252. * @return void
  3253. */
  3254. public function testControlWithLeadingInteger()
  3255. {
  3256. $result = $this->Form->text('0.Node.title');
  3257. $expected = [
  3258. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3259. ];
  3260. $this->assertHtml($expected, $result);
  3261. }
  3262. /**
  3263. * testControlSelectType method
  3264. *
  3265. * Test form->control() with select type inputs.
  3266. *
  3267. * @return void
  3268. */
  3269. public function testControlSelectType()
  3270. {
  3271. $result = $this->Form->control(
  3272. 'email',
  3273. [
  3274. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3275. );
  3276. $expected = [
  3277. 'div' => ['class' => 'input select'],
  3278. 'label' => ['for' => 'email'],
  3279. 'Email',
  3280. '/label',
  3281. ['select' => ['name' => 'email', 'id' => 'email']],
  3282. ['option' => ['value' => '']],
  3283. '/option',
  3284. ['option' => ['value' => 'è']],
  3285. 'Firést',
  3286. '/option',
  3287. ['option' => ['value' => 'é']],
  3288. 'Secoènd',
  3289. '/option',
  3290. '/select',
  3291. '/div'
  3292. ];
  3293. $this->assertHtml($expected, $result);
  3294. $result = $this->Form->control(
  3295. 'email',
  3296. [
  3297. 'options' => ['First', 'Second'], 'empty' => true]
  3298. );
  3299. $expected = [
  3300. 'div' => ['class' => 'input select'],
  3301. 'label' => ['for' => 'email'],
  3302. 'Email',
  3303. '/label',
  3304. ['select' => ['name' => 'email', 'id' => 'email']],
  3305. ['option' => ['value' => '']],
  3306. '/option',
  3307. ['option' => ['value' => '0']],
  3308. 'First',
  3309. '/option',
  3310. ['option' => ['value' => '1']],
  3311. 'Second',
  3312. '/option',
  3313. '/select',
  3314. '/div'
  3315. ];
  3316. $this->assertHtml($expected, $result);
  3317. $result = $this->Form->control('email', [
  3318. 'type' => 'select',
  3319. 'options' => new \ArrayObject(['First', 'Second']),
  3320. 'empty' => true
  3321. ]);
  3322. $this->assertHtml($expected, $result);
  3323. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3324. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3325. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3326. $expected = [
  3327. 'div' => ['class' => 'input select'],
  3328. 'label' => ['for' => 'model-user-id'],
  3329. 'User',
  3330. '/label',
  3331. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3332. ['option' => ['value' => '']],
  3333. '/option',
  3334. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3335. 'good',
  3336. '/option',
  3337. ['option' => ['value' => 'other']],
  3338. 'bad',
  3339. '/option',
  3340. '/select',
  3341. '/div'
  3342. ];
  3343. $this->assertHtml($expected, $result);
  3344. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3345. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3346. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3347. $expected = [
  3348. 'div' => ['class' => 'input select'],
  3349. 'label' => ['for' => 'thing-user-id'],
  3350. 'User',
  3351. '/label',
  3352. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3353. ['option' => ['value' => '']],
  3354. 'Some Empty',
  3355. '/option',
  3356. ['option' => ['value' => 'value']],
  3357. 'good',
  3358. '/option',
  3359. ['option' => ['value' => 'other']],
  3360. 'bad',
  3361. '/option',
  3362. '/select',
  3363. '/div'
  3364. ];
  3365. $this->assertHtml($expected, $result);
  3366. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3367. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3368. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3369. $expected = [
  3370. 'div' => ['class' => 'input select'],
  3371. 'label' => ['for' => 'thing-user-id'],
  3372. 'User',
  3373. '/label',
  3374. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3375. ['option' => ['value' => '']],
  3376. 'Some Empty',
  3377. '/option',
  3378. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3379. 'good',
  3380. '/option',
  3381. ['option' => ['value' => 'other']],
  3382. 'bad',
  3383. '/option',
  3384. '/select',
  3385. '/div'
  3386. ];
  3387. $this->assertHtml($expected, $result);
  3388. $this->Form->data = [];
  3389. $result = $this->Form->control('Publisher.id', [
  3390. 'label' => 'Publisher',
  3391. 'type' => 'select',
  3392. 'multiple' => 'checkbox',
  3393. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3394. ]);
  3395. $expected = [
  3396. ['div' => ['class' => 'input select']],
  3397. ['label' => ['for' => 'publisher-id']],
  3398. 'Publisher',
  3399. '/label',
  3400. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3401. ['div' => ['class' => 'checkbox']],
  3402. ['label' => ['for' => 'publisher-id-value-1']],
  3403. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3404. 'Label 1',
  3405. '/label',
  3406. '/div',
  3407. ['div' => ['class' => 'checkbox']],
  3408. ['label' => ['for' => 'publisher-id-value-2']],
  3409. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3410. 'Label 2',
  3411. '/label',
  3412. '/div',
  3413. '/div'
  3414. ];
  3415. $this->assertHtml($expected, $result);
  3416. }
  3417. /**
  3418. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3419. *
  3420. * Test that control() and a non standard primary key makes a hidden input by default.
  3421. *
  3422. * @return void
  3423. */
  3424. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3425. {
  3426. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3427. $this->Form->create($this->article);
  3428. $result = $this->Form->control('title');
  3429. $expected = [
  3430. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3431. ];
  3432. $this->assertHtml($expected, $result);
  3433. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3434. $this->Form->create($this->article);
  3435. $result = $this->Form->control('title');
  3436. $expected = [
  3437. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3438. ];
  3439. $this->assertHtml($expected, $result);
  3440. $result = $this->Form->control('body');
  3441. $expected = [
  3442. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3443. ];
  3444. $this->assertHtml($expected, $result);
  3445. }
  3446. /**
  3447. * testControlOverridingMagicSelectType method
  3448. *
  3449. * Test that overriding the magic select type widget is possible.
  3450. *
  3451. * @return void
  3452. */
  3453. public function testControlOverridingMagicSelectType()
  3454. {
  3455. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3456. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3457. $expected = [
  3458. 'div' => ['class' => 'input text'],
  3459. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3460. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3461. '/div'
  3462. ];
  3463. $this->assertHtml($expected, $result);
  3464. //Check that magic types still work for plural/singular vars
  3465. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3466. $result = $this->Form->control('Model.type');
  3467. $expected = [
  3468. 'div' => ['class' => 'input select'],
  3469. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3470. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3471. ['option' => ['value' => 'value']], 'good', '/option',
  3472. ['option' => ['value' => 'other']], 'bad', '/option',
  3473. '/select',
  3474. '/div'
  3475. ];
  3476. $this->assertHtml($expected, $result);
  3477. }
  3478. /**
  3479. * testControlMagicTypeDoesNotOverride method
  3480. *
  3481. * Test that inferred types do not override developer input.
  3482. *
  3483. * @return void
  3484. */
  3485. public function testControlMagicTypeDoesNotOverride()
  3486. {
  3487. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3488. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3489. $expected = [
  3490. 'div' => ['class' => 'input checkbox'],
  3491. ['input' => [
  3492. 'type' => 'hidden',
  3493. 'name' => 'Model[user]',
  3494. 'value' => 0,
  3495. ]],
  3496. 'label' => ['for' => 'model-user'],
  3497. ['input' => [
  3498. 'name' => 'Model[user]',
  3499. 'type' => 'checkbox',
  3500. 'id' => 'model-user',
  3501. 'value' => 1
  3502. ]],
  3503. 'User',
  3504. '/label',
  3505. '/div'
  3506. ];
  3507. $this->assertHtml($expected, $result);
  3508. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3509. $options = [
  3510. 1 => 'blue',
  3511. 2 => 'red'
  3512. ];
  3513. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3514. $expected = [
  3515. 'div' => ['class' => 'input select'],
  3516. 'label' => ['for' => 'tags-ids'],
  3517. 'Tags',
  3518. '/label',
  3519. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3520. ['div' => ['class' => 'checkbox']],
  3521. ['label' => ['for' => 'tags-ids-1']],
  3522. ['input' => [
  3523. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3524. 'value' => '1', 'name' => 'tags[_ids][]'
  3525. ]],
  3526. 'blue',
  3527. '/label',
  3528. '/div',
  3529. ['div' => ['class' => 'checkbox']],
  3530. ['label' => ['for' => 'tags-ids-2']],
  3531. ['input' => [
  3532. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3533. 'value' => '2', 'name' => 'tags[_ids][]'
  3534. ]],
  3535. 'red',
  3536. '/label',
  3537. '/div',
  3538. '/div'
  3539. ];
  3540. $this->assertHtml($expected, $result);
  3541. }
  3542. /**
  3543. * testControlMagicSelectForTypeNumber method
  3544. *
  3545. * Test that magic control() selects are created for type=number.
  3546. *
  3547. * @return void
  3548. */
  3549. public function testControlMagicSelectForTypeNumber()
  3550. {
  3551. TableRegistry::get('ValidateUsers', [
  3552. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3553. ]);
  3554. $entity = new Entity(['balance' => 1]);
  3555. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3556. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3557. $result = $this->Form->control('balance');
  3558. $expected = [
  3559. 'div' => ['class' => 'input select'],
  3560. 'label' => ['for' => 'balance'],
  3561. 'Balance',
  3562. '/label',
  3563. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3564. ['option' => ['value' => '0']],
  3565. 'nothing',
  3566. '/option',
  3567. ['option' => ['value' => '1', 'selected' => 'selected']],
  3568. 'some',
  3569. '/option',
  3570. ['option' => ['value' => '100']],
  3571. 'a lot',
  3572. '/option',
  3573. '/select',
  3574. '/div'
  3575. ];
  3576. $this->assertHtml($expected, $result);
  3577. }
  3578. /**
  3579. * testInvalidControlTypeOption method
  3580. *
  3581. * Test invalid 'input' type option to control() function.
  3582. *
  3583. * @expectedException \RuntimeException
  3584. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3585. * @return void
  3586. */
  3587. public function testInvalidControlTypeOption()
  3588. {
  3589. $this->Form->control('text', ['type' => 'input']);
  3590. }
  3591. /**
  3592. * testControlMagicSelectChangeToRadio method
  3593. *
  3594. * Test that magic control() selects can easily be converted into radio types without error.
  3595. *
  3596. * @return void
  3597. */
  3598. public function testControlMagicSelectChangeToRadio()
  3599. {
  3600. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3601. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3602. $this->assertContains('input type="radio"', $result);
  3603. }
  3604. /**
  3605. * testFormControlSubmit method
  3606. *
  3607. * Test correct results for form::control() and type submit.
  3608. *
  3609. * @return void
  3610. */
  3611. public function testFormControlSubmit()
  3612. {
  3613. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3614. $expected = [
  3615. 'div' => ['class' => 'submit'],
  3616. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3617. '/div'
  3618. ];
  3619. $this->assertHtml($expected, $result);
  3620. }
  3621. /**
  3622. * testFormControls method
  3623. *
  3624. * Test correct results from Form::controls().
  3625. *
  3626. * @return void
  3627. */
  3628. public function testFormControlsLegendFieldset()
  3629. {
  3630. $this->Form->create($this->article);
  3631. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3632. $expected = [
  3633. '<fieldset',
  3634. '<legend',
  3635. 'The Legend',
  3636. '/legend',
  3637. '*/fieldset',
  3638. ];
  3639. $this->assertHtml($expected, $result);
  3640. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3641. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3642. $this->assertContains('<fieldset>', $result);
  3643. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3644. $this->assertNotContains('<legend>', $result);
  3645. $this->assertNotContains('<fieldset>', $result);
  3646. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3647. $this->assertNotContains('<legend>', $result);
  3648. $this->assertNotContains('<fieldset>', $result);
  3649. $this->Form->create($this->article);
  3650. $this->Form->request->params['prefix'] = 'admin';
  3651. $this->Form->request->params['action'] = 'admin_edit';
  3652. $this->Form->request->params['controller'] = 'articles';
  3653. $result = $this->Form->allControls();
  3654. $expected = [
  3655. '<fieldset',
  3656. '<legend',
  3657. 'New Article',
  3658. '/legend',
  3659. '*/fieldset',
  3660. ];
  3661. $this->assertHtml($expected, $result);
  3662. $this->Form->create($this->article);
  3663. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3664. $expected = [
  3665. '<fieldset',
  3666. '<legend',
  3667. 'The Legend',
  3668. '/legend',
  3669. '*/fieldset',
  3670. ];
  3671. $this->assertHtml($expected, $result);
  3672. $this->Form->create($this->article);
  3673. $result = $this->Form->allControls([], [
  3674. 'fieldset' => [
  3675. 'class' => 'some-class some-other-class',
  3676. 'disabled' => true,
  3677. 'data-param' => 'a-param'
  3678. ],
  3679. 'legend' => 'The Legend'
  3680. ]);
  3681. $expected = [
  3682. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3683. '<legend',
  3684. 'The Legend',
  3685. '/legend',
  3686. '*/fieldset',
  3687. ];
  3688. $this->assertHtml($expected, $result);
  3689. }
  3690. /**
  3691. * testFormControls method
  3692. *
  3693. * Test the controls() method.
  3694. *
  3695. * @return void
  3696. */
  3697. public function testFormControls()
  3698. {
  3699. $this->Form->create($this->article);
  3700. $result = $this->Form->allControls();
  3701. $expected = [
  3702. '<fieldset',
  3703. '<legend', 'New Article', '/legend',
  3704. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3705. ['div' => ['class' => 'input select required']],
  3706. '*/div',
  3707. ['div' => ['class' => 'input text required']],
  3708. '*/div',
  3709. ['div' => ['class' => 'input text']],
  3710. '*/div',
  3711. ['div' => ['class' => 'input text']],
  3712. '*/div',
  3713. '/fieldset',
  3714. ];
  3715. $this->assertHtml($expected, $result);
  3716. $result = $this->Form->allControls([
  3717. 'published' => ['type' => 'boolean']
  3718. ]);
  3719. $expected = [
  3720. '<fieldset',
  3721. '<legend', 'New Article', '/legend',
  3722. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3723. ['div' => ['class' => 'input select required']],
  3724. '*/div',
  3725. ['div' => ['class' => 'input text required']],
  3726. '*/div',
  3727. ['div' => ['class' => 'input text']],
  3728. '*/div',
  3729. ['div' => ['class' => 'input boolean']],
  3730. '*/div',
  3731. '/fieldset',
  3732. ];
  3733. $this->assertHtml($expected, $result);
  3734. $this->Form->create($this->article);
  3735. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3736. $expected = [
  3737. 'fieldset' => [],
  3738. 'legend' => [],
  3739. 'Hello',
  3740. '/legend',
  3741. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3742. ['div' => ['class' => 'input select required']],
  3743. '*/div',
  3744. ['div' => ['class' => 'input text required']],
  3745. '*/div',
  3746. ['div' => ['class' => 'input text']],
  3747. '*/div',
  3748. ['div' => ['class' => 'input text']],
  3749. '*/div',
  3750. '/fieldset'
  3751. ];
  3752. $this->assertHtml($expected, $result);
  3753. $this->Form->create(false);
  3754. $expected = [
  3755. 'fieldset' => [],
  3756. ['div' => ['class' => 'input text']],
  3757. 'label' => ['for' => 'foo'],
  3758. 'Foo',
  3759. '/label',
  3760. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3761. '*/div',
  3762. '/fieldset'
  3763. ];
  3764. $result = $this->Form->allControls(
  3765. ['foo' => ['type' => 'text']],
  3766. ['legend' => false]
  3767. );
  3768. $this->assertHtml($expected, $result);
  3769. }
  3770. /**
  3771. * testFormControlsBlacklist method
  3772. *
  3773. * @return void
  3774. */
  3775. public function testFormControlsBlacklist()
  3776. {
  3777. $this->Form->create($this->article);
  3778. $result = $this->Form->allControls([
  3779. 'id' => false
  3780. ]);
  3781. $expected = [
  3782. '<fieldset',
  3783. '<legend', 'New Article', '/legend',
  3784. ['div' => ['class' => 'input select required']],
  3785. '*/div',
  3786. ['div' => ['class' => 'input text required']],
  3787. '*/div',
  3788. ['div' => ['class' => 'input text']],
  3789. '*/div',
  3790. ['div' => ['class' => 'input text']],
  3791. '*/div',
  3792. '/fieldset',
  3793. ];
  3794. $this->assertHtml($expected, $result);
  3795. $this->Form->create($this->article);
  3796. $result = $this->Form->allControls([
  3797. 'id' => []
  3798. ]);
  3799. $expected = [
  3800. '<fieldset',
  3801. '<legend', 'New Article', '/legend',
  3802. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3803. ['div' => ['class' => 'input select required']],
  3804. '*/div',
  3805. ['div' => ['class' => 'input text required']],
  3806. '*/div',
  3807. ['div' => ['class' => 'input text']],
  3808. '*/div',
  3809. ['div' => ['class' => 'input text']],
  3810. '*/div',
  3811. '/fieldset',
  3812. ];
  3813. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3814. }
  3815. /**
  3816. * testSelectAsCheckbox method
  3817. *
  3818. * Test multi-select widget with checkbox formatting.
  3819. *
  3820. * @return void
  3821. */
  3822. public function testSelectAsCheckbox()
  3823. {
  3824. $result = $this->Form->select(
  3825. 'Model.multi_field',
  3826. ['first', 'second', 'third'],
  3827. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3828. );
  3829. $expected = [
  3830. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3831. ['div' => ['class' => 'checkbox']],
  3832. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3833. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3834. 'first',
  3835. '/label',
  3836. '/div',
  3837. ['div' => ['class' => 'checkbox']],
  3838. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3839. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3840. 'second',
  3841. '/label',
  3842. '/div',
  3843. ['div' => ['class' => 'checkbox']],
  3844. ['label' => ['for' => 'model-multi-field-2']],
  3845. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3846. 'third',
  3847. '/label',
  3848. '/div',
  3849. ];
  3850. $this->assertHtml($expected, $result);
  3851. $result = $this->Form->select(
  3852. 'Model.multi_field',
  3853. ['1/2' => 'half'],
  3854. ['multiple' => 'checkbox']
  3855. );
  3856. $expected = [
  3857. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3858. ['div' => ['class' => 'checkbox']],
  3859. ['label' => ['for' => 'model-multi-field-1-2']],
  3860. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3861. 'half',
  3862. '/label',
  3863. '/div',
  3864. ];
  3865. $this->assertHtml($expected, $result);
  3866. }
  3867. /**
  3868. * testLabel method
  3869. *
  3870. * Test label generation.
  3871. *
  3872. * @return void
  3873. */
  3874. public function testLabel()
  3875. {
  3876. $result = $this->Form->label('Person.name');
  3877. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3878. $this->assertHtml($expected, $result);
  3879. $result = $this->Form->label('Person.name');
  3880. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3881. $this->assertHtml($expected, $result);
  3882. $result = $this->Form->label('Person.first_name');
  3883. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3884. $this->assertHtml($expected, $result);
  3885. $result = $this->Form->label('Person.first_name', 'Your first name');
  3886. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3887. $this->assertHtml($expected, $result);
  3888. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3889. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3890. $this->assertHtml($expected, $result);
  3891. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3892. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3893. $this->assertHtml($expected, $result);
  3894. $result = $this->Form->label('Person.first_name', '');
  3895. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3896. $this->assertHtml($expected, $result);
  3897. $result = $this->Form->label('Person.2.name', '');
  3898. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3899. $this->assertHtml($expected, $result);
  3900. }
  3901. /**
  3902. * testLabelContainControl method
  3903. *
  3904. * Test that label() can accept an input with the correct template vars.
  3905. *
  3906. * @return void
  3907. */
  3908. public function testLabelContainControl()
  3909. {
  3910. $this->Form->templates([
  3911. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3912. ]);
  3913. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3914. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3915. ]);
  3916. $expected = [
  3917. 'label' => ['for' => 'person-accept-terms'],
  3918. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3919. 'Accept',
  3920. '/label',
  3921. ];
  3922. $this->assertHtml($expected, $result);
  3923. }
  3924. /**
  3925. * testTextbox method
  3926. *
  3927. * Test textbox element generation.
  3928. *
  3929. * @return void
  3930. */
  3931. public function testTextbox()
  3932. {
  3933. $result = $this->Form->text('Model.field');
  3934. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3935. $this->assertHtml($expected, $result);
  3936. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3937. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3938. $this->assertHtml($expected, $result);
  3939. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3940. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3941. $this->assertHtml($expected, $result);
  3942. }
  3943. /**
  3944. * testTextBoxDataAndError method
  3945. *
  3946. * Test that text() hooks up with request data and error fields.
  3947. *
  3948. * @return void
  3949. */
  3950. public function testTextBoxDataAndError()
  3951. {
  3952. $this->article['errors'] = [
  3953. 'Contact' => ['text' => 'wrong']
  3954. ];
  3955. $this->Form->create($this->article);
  3956. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3957. $result = $this->Form->text('Model.text');
  3958. $expected = [
  3959. 'input' => [
  3960. 'type' => 'text',
  3961. 'name' => 'Model[text]',
  3962. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3963. ]
  3964. ];
  3965. $this->assertHtml($expected, $result);
  3966. $this->Form->request->data['Contact']['text'] = 'test';
  3967. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3968. $expected = [
  3969. 'input' => [
  3970. 'type' => 'text',
  3971. 'name' => 'Contact[text]',
  3972. 'value' => 'test',
  3973. 'id' => 'theID',
  3974. 'class' => 'form-error'
  3975. ]
  3976. ];
  3977. $this->assertHtml($expected, $result);
  3978. }
  3979. /**
  3980. * testDefaultValue method
  3981. *
  3982. * Test default value setting.
  3983. *
  3984. * @return void
  3985. */
  3986. public function testTextDefaultValue()
  3987. {
  3988. $this->Form->request->data['Model']['field'] = 'test';
  3989. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3990. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3991. $this->assertHtml($expected, $result);
  3992. unset($this->Form->request->data['Model']['field']);
  3993. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3994. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3995. $this->assertHtml($expected, $result);
  3996. $Articles = TableRegistry::get('Articles');
  3997. $title = $Articles->schema()->column('title');
  3998. $Articles->schema()->addColumn(
  3999. 'title',
  4000. ['default' => 'default title'] + $title
  4001. );
  4002. $entity = $Articles->newEntity();
  4003. $this->Form->create($entity);
  4004. // Get default value from schema
  4005. $result = $this->Form->text('title');
  4006. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4007. $this->assertHtml($expected, $result);
  4008. // Don't get value from schema
  4009. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4010. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4011. $this->assertHtml($expected, $result);
  4012. // Custom default value overrides default value from schema
  4013. $result = $this->Form->text('title', ['default' => 'override default']);
  4014. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4015. $this->assertHtml($expected, $result);
  4016. // Default value from schema is used only for new entities.
  4017. $entity->isNew(false);
  4018. $result = $this->Form->text('title');
  4019. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4020. $this->assertHtml($expected, $result);
  4021. }
  4022. /**
  4023. * testError method
  4024. *
  4025. * Test field error generation.
  4026. *
  4027. * @return void
  4028. */
  4029. public function testError()
  4030. {
  4031. $this->article['errors'] = [
  4032. 'Article' => ['field' => 'email']
  4033. ];
  4034. $this->Form->create($this->article);
  4035. $result = $this->Form->error('Article.field');
  4036. $expected = [
  4037. ['div' => ['class' => 'error-message']],
  4038. 'email',
  4039. '/div',
  4040. ];
  4041. $this->assertHtml($expected, $result);
  4042. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  4043. $expected = [
  4044. ['div' => ['class' => 'error-message']],
  4045. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4046. '/div',
  4047. ];
  4048. $this->assertHtml($expected, $result);
  4049. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  4050. $expected = [
  4051. ['div' => ['class' => 'error-message']],
  4052. '<strong', 'Badness!', '/strong',
  4053. '/div',
  4054. ];
  4055. $this->assertHtml($expected, $result);
  4056. }
  4057. /**
  4058. * testErrorRuleName method
  4059. *
  4060. * Test error translation can use rule names for translating.
  4061. *
  4062. * @return void
  4063. */
  4064. public function testErrorRuleName()
  4065. {
  4066. $this->article['errors'] = [
  4067. 'Article' => [
  4068. 'field' => ['email' => 'Your email was not good']
  4069. ]
  4070. ];
  4071. $this->Form->create($this->article);
  4072. $result = $this->Form->error('Article.field');
  4073. $expected = [
  4074. ['div' => ['class' => 'error-message']],
  4075. 'Your email was not good',
  4076. '/div',
  4077. ];
  4078. $this->assertHtml($expected, $result);
  4079. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4080. $expected = [
  4081. ['div' => ['class' => 'error-message']],
  4082. 'Email in use',
  4083. '/div',
  4084. ];
  4085. $this->assertHtml($expected, $result);
  4086. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4087. $expected = [
  4088. ['div' => ['class' => 'error-message']],
  4089. 'Email in use',
  4090. '/div',
  4091. ];
  4092. $this->assertHtml($expected, $result);
  4093. $result = $this->Form->error('Article.field', [
  4094. 'email' => 'Key is preferred',
  4095. 'Your email was not good' => 'Email in use'
  4096. ]);
  4097. $expected = [
  4098. ['div' => ['class' => 'error-message']],
  4099. 'Key is preferred',
  4100. '/div',
  4101. ];
  4102. $this->assertHtml($expected, $result);
  4103. }
  4104. /**
  4105. * testErrorMessages method
  4106. *
  4107. * Test error with nested lists.
  4108. *
  4109. * @return void
  4110. */
  4111. public function testErrorMessages()
  4112. {
  4113. $this->article['errors'] = [
  4114. 'Article' => ['field' => 'email']
  4115. ];
  4116. $this->Form->create($this->article);
  4117. $result = $this->Form->error('Article.field', [
  4118. 'email' => 'No good!'
  4119. ]);
  4120. $expected = [
  4121. 'div' => ['class' => 'error-message'],
  4122. 'No good!',
  4123. '/div'
  4124. ];
  4125. $this->assertHtml($expected, $result);
  4126. }
  4127. /**
  4128. * testErrorMultipleMessages method
  4129. *
  4130. * Test error() with multiple messages.
  4131. *
  4132. * @return void
  4133. */
  4134. public function testErrorMultipleMessages()
  4135. {
  4136. $this->article['errors'] = [
  4137. 'field' => ['notBlank', 'email', 'Something else']
  4138. ];
  4139. $this->Form->create($this->article);
  4140. $result = $this->Form->error('field', [
  4141. 'notBlank' => 'Cannot be empty',
  4142. 'email' => 'No good!'
  4143. ]);
  4144. $expected = [
  4145. 'div' => ['class' => 'error-message'],
  4146. 'ul' => [],
  4147. '<li', 'Cannot be empty', '/li',
  4148. '<li', 'No good!', '/li',
  4149. '<li', 'Something else', '/li',
  4150. '/ul',
  4151. '/div'
  4152. ];
  4153. $this->assertHtml($expected, $result);
  4154. }
  4155. /**
  4156. * testPassword method
  4157. *
  4158. * Test password element generation.
  4159. *
  4160. * @return void
  4161. */
  4162. public function testPassword()
  4163. {
  4164. $this->article['errors'] = [
  4165. 'Contact' => [
  4166. 'passwd' => 1
  4167. ]
  4168. ];
  4169. $this->Form->create($this->article);
  4170. $result = $this->Form->password('Contact.field');
  4171. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4172. $this->assertHtml($expected, $result);
  4173. $this->Form->request->data['Contact']['passwd'] = 'test';
  4174. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4175. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4176. $this->assertHtml($expected, $result);
  4177. }
  4178. /**
  4179. * testRadio method
  4180. *
  4181. * Test radio element set generation.
  4182. *
  4183. * @return void
  4184. */
  4185. public function testRadio()
  4186. {
  4187. $result = $this->Form->radio('Model.field', ['option A']);
  4188. $expected = [
  4189. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4190. 'label' => ['for' => 'model-field-0'],
  4191. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4192. 'option A',
  4193. '/label'
  4194. ];
  4195. $this->assertHtml($expected, $result);
  4196. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4197. $this->assertHtml($expected, $result);
  4198. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4199. $expected = [
  4200. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4201. ['label' => ['for' => 'model-field-0']],
  4202. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4203. 'option A',
  4204. '/label',
  4205. ['label' => ['for' => 'model-field-1']],
  4206. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4207. 'option B',
  4208. '/label',
  4209. ];
  4210. $this->assertHtml($expected, $result);
  4211. $result = $this->Form->radio(
  4212. 'Employee.gender',
  4213. ['male' => 'Male', 'female' => 'Female'],
  4214. ['form' => 'my-form']
  4215. );
  4216. $expected = [
  4217. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4218. ['label' => ['for' => 'employee-gender-male']],
  4219. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4220. 'Male',
  4221. '/label',
  4222. ['label' => ['for' => 'employee-gender-female']],
  4223. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4224. 'Female',
  4225. '/label',
  4226. ];
  4227. $this->assertHtml($expected, $result);
  4228. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4229. $expected = [
  4230. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4231. ['label' => ['for' => 'model-custom-0']],
  4232. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4233. 'option A',
  4234. '/label',
  4235. ['label' => ['for' => 'model-custom-1']],
  4236. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4237. 'option B',
  4238. '/label',
  4239. ];
  4240. $this->assertHtml($expected, $result);
  4241. $result = $this->Form->radio(
  4242. 'Employee.gender',
  4243. [
  4244. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4245. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4246. ]
  4247. );
  4248. $expected = [
  4249. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4250. ['label' => ['for' => 'employee-gender-male']],
  4251. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4252. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4253. 'Male',
  4254. '/label',
  4255. ['label' => ['for' => 'employee-gender-female']],
  4256. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4257. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4258. 'Female',
  4259. '/label',
  4260. ];
  4261. $this->assertHtml($expected, $result);
  4262. }
  4263. /**
  4264. * testRadioDefaultValue method
  4265. *
  4266. * Test default value setting on radio() method.
  4267. *
  4268. * @return void
  4269. */
  4270. public function testRadioDefaultValue()
  4271. {
  4272. $Articles = TableRegistry::get('Articles');
  4273. $title = $Articles->schema()->column('title');
  4274. $Articles->schema()->addColumn(
  4275. 'title',
  4276. ['default' => '1'] + $title
  4277. );
  4278. $this->Form->create($Articles->newEntity());
  4279. $result = $this->Form->radio('title', ['option A', 'option B']);
  4280. $expected = [
  4281. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4282. ['label' => ['for' => 'title-0']],
  4283. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4284. 'option A',
  4285. '/label',
  4286. ['label' => ['for' => 'title-1']],
  4287. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4288. 'option B',
  4289. '/label',
  4290. ];
  4291. $this->assertHtml($expected, $result);
  4292. }
  4293. /**
  4294. * testControlRadio method
  4295. *
  4296. * Test that input works with radio types.
  4297. *
  4298. * @return void
  4299. */
  4300. public function testControlRadio()
  4301. {
  4302. $result = $this->Form->control('test', [
  4303. 'type' => 'radio',
  4304. 'options' => ['A', 'B'],
  4305. ]);
  4306. $expected = [
  4307. ['div' => ['class' => 'input radio']],
  4308. '<label',
  4309. 'Test',
  4310. '/label',
  4311. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4312. ['label' => ['for' => 'test-0']],
  4313. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4314. 'A',
  4315. '/label',
  4316. ['label' => ['for' => 'test-1']],
  4317. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4318. 'B',
  4319. '/label',
  4320. '/div',
  4321. ];
  4322. $this->assertHtml($expected, $result);
  4323. $result = $this->Form->control('test', [
  4324. 'type' => 'radio',
  4325. 'options' => ['A', 'B'],
  4326. 'value' => '0'
  4327. ]);
  4328. $expected = [
  4329. ['div' => ['class' => 'input radio']],
  4330. '<label',
  4331. 'Test',
  4332. '/label',
  4333. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4334. ['label' => ['for' => 'test-0']],
  4335. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4336. 'A',
  4337. '/label',
  4338. ['label' => ['for' => 'test-1']],
  4339. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4340. 'B',
  4341. '/label',
  4342. '/div',
  4343. ];
  4344. $this->assertHtml($expected, $result);
  4345. $result = $this->Form->control('test', [
  4346. 'type' => 'radio',
  4347. 'options' => ['A', 'B'],
  4348. 'label' => false
  4349. ]);
  4350. $expected = [
  4351. ['div' => ['class' => 'input radio']],
  4352. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4353. ['label' => ['for' => 'test-0']],
  4354. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4355. 'A',
  4356. '/label',
  4357. ['label' => ['for' => 'test-1']],
  4358. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4359. 'B',
  4360. '/label',
  4361. '/div',
  4362. ];
  4363. $this->assertHtml($expected, $result);
  4364. }
  4365. /**
  4366. * testRadioNoLabel method
  4367. *
  4368. * Test that radio() works with label = false.
  4369. *
  4370. * @return void
  4371. */
  4372. public function testRadioNoLabel()
  4373. {
  4374. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4375. $expected = [
  4376. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4377. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4378. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4379. ];
  4380. $this->assertHtml($expected, $result);
  4381. }
  4382. /**
  4383. * testRadioControlInsideLabel method
  4384. *
  4385. * Test generating radio input inside label ala twitter bootstrap.
  4386. *
  4387. * @return void
  4388. */
  4389. public function testRadioControlInsideLabel()
  4390. {
  4391. $this->Form->templates([
  4392. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4393. 'radioWrapper' => '{{label}}'
  4394. ]);
  4395. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4396. //@codingStandardsIgnoreStart
  4397. $expected = [
  4398. ['input' => [
  4399. 'type' => 'hidden',
  4400. 'name' => 'Model[field]',
  4401. 'value' => ''
  4402. ]],
  4403. ['label' => ['for' => 'model-field-0']],
  4404. ['input' => [
  4405. 'type' => 'radio',
  4406. 'name' => 'Model[field]',
  4407. 'value' => '0',
  4408. 'id' => 'model-field-0'
  4409. ]],
  4410. 'option A',
  4411. '/label',
  4412. ['label' => ['for' => 'model-field-1']],
  4413. ['input' => [
  4414. 'type' => 'radio',
  4415. 'name' => 'Model[field]',
  4416. 'value' => '1',
  4417. 'id' => 'model-field-1'
  4418. ]],
  4419. 'option B',
  4420. '/label',
  4421. ];
  4422. //@codingStandardsIgnoreEnd
  4423. $this->assertHtml($expected, $result);
  4424. }
  4425. /**
  4426. * testRadioHiddenControlDisabling method
  4427. *
  4428. * Test disabling the hidden input for radio buttons.
  4429. *
  4430. * @return void
  4431. */
  4432. public function testRadioHiddenControlDisabling()
  4433. {
  4434. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4435. $expected = [
  4436. 'label' => ['for' => 'model-1-field-0'],
  4437. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4438. 'option A',
  4439. '/label'
  4440. ];
  4441. $this->assertHtml($expected, $result);
  4442. }
  4443. /**
  4444. * testRadioOutOfRange method
  4445. *
  4446. * Test radio element set generation.
  4447. *
  4448. * @return void
  4449. */
  4450. public function testRadioOutOfRange()
  4451. {
  4452. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4453. $expected = [
  4454. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4455. 'label' => ['for' => 'model-field-v'],
  4456. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4457. 'value',
  4458. '/label'
  4459. ];
  4460. $this->assertHtml($expected, $result);
  4461. }
  4462. /**
  4463. * testSelect method
  4464. *
  4465. * Test select element generation.
  4466. *
  4467. * @return void
  4468. */
  4469. public function testSelect()
  4470. {
  4471. $result = $this->Form->select('Model.field', []);
  4472. $expected = [
  4473. 'select' => ['name' => 'Model[field]'],
  4474. '/select'
  4475. ];
  4476. $this->assertHtml($expected, $result);
  4477. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4478. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4479. $expected = [
  4480. 'select' => ['name' => 'Model[field]'],
  4481. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4482. 'good',
  4483. '/option',
  4484. ['option' => ['value' => 'other']],
  4485. 'bad',
  4486. '/option',
  4487. '/select'
  4488. ];
  4489. $this->assertHtml($expected, $result);
  4490. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4491. $this->assertHtml($expected, $result);
  4492. $this->Form->request->data = [];
  4493. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4494. $expected = [
  4495. 'select' => ['name' => 'Model[field]'],
  4496. ['option' => ['value' => 'value']],
  4497. 'good',
  4498. '/option',
  4499. ['option' => ['value' => 'other']],
  4500. 'bad',
  4501. '/option',
  4502. '/select'
  4503. ];
  4504. $this->assertHtml($expected, $result);
  4505. $options = [
  4506. ['value' => 'first', 'text' => 'First'],
  4507. ['value' => 'first', 'text' => 'Another First'],
  4508. ];
  4509. $result = $this->Form->select(
  4510. 'Model.field',
  4511. $options,
  4512. ['escape' => false, 'empty' => false]
  4513. );
  4514. $expected = [
  4515. 'select' => ['name' => 'Model[field]'],
  4516. ['option' => ['value' => 'first']],
  4517. 'First',
  4518. '/option',
  4519. ['option' => ['value' => 'first']],
  4520. 'Another First',
  4521. '/option',
  4522. '/select'
  4523. ];
  4524. $this->assertHtml($expected, $result);
  4525. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4526. $result = $this->Form->select(
  4527. 'Model.contact_id',
  4528. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4529. ['escape' => false, 'empty' => 'pick something']
  4530. );
  4531. $expected = [
  4532. 'select' => ['name' => 'Model[contact_id]'],
  4533. ['option' => ['value' => '']], 'pick something', '/option',
  4534. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4535. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4536. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4537. '/select'
  4538. ];
  4539. $this->assertHtml($expected, $result);
  4540. $this->Form->request->data['Model']['field'] = 0;
  4541. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4542. $expected = [
  4543. 'select' => ['name' => 'Model[field]'],
  4544. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4545. ['option' => ['value' => '1']], 'Yes', '/option',
  4546. '/select'
  4547. ];
  4548. $this->assertHtml($expected, $result);
  4549. }
  4550. /**
  4551. * testSelectEscapeHtml method
  4552. *
  4553. * Test that select() escapes HTML.
  4554. *
  4555. * @return void
  4556. */
  4557. public function testSelectEscapeHtml()
  4558. {
  4559. $result = $this->Form->select(
  4560. 'Model.field',
  4561. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4562. ['empty' => false]
  4563. );
  4564. $expected = [
  4565. 'select' => ['name' => 'Model[field]'],
  4566. ['option' => ['value' => 'first']],
  4567. 'first &quot;html&quot; &lt;chars&gt;',
  4568. '/option',
  4569. ['option' => ['value' => 'second']],
  4570. 'value',
  4571. '/option',
  4572. '/select'
  4573. ];
  4574. $this->assertHtml($expected, $result);
  4575. $result = $this->Form->select(
  4576. 'Model.field',
  4577. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4578. ['escape' => false, 'empty' => false]
  4579. );
  4580. $expected = [
  4581. 'select' => ['name' => 'Model[field]'],
  4582. ['option' => ['value' => 'first']],
  4583. 'first "html" <chars>',
  4584. '/option',
  4585. ['option' => ['value' => 'second']],
  4586. 'value',
  4587. '/option',
  4588. '/select'
  4589. ];
  4590. $this->assertHtml($expected, $result);
  4591. }
  4592. /**
  4593. * testSelectRequired method
  4594. *
  4595. * Test select() with required and disabled attributes.
  4596. *
  4597. * @return void
  4598. */
  4599. public function testSelectRequired()
  4600. {
  4601. $this->article['required'] = [
  4602. 'user_id' => true
  4603. ];
  4604. $this->Form->create($this->article);
  4605. $result = $this->Form->select('user_id', ['option A']);
  4606. $expected = [
  4607. 'select' => [
  4608. 'name' => 'user_id',
  4609. 'required' => 'required'
  4610. ],
  4611. ['option' => ['value' => '0']], 'option A', '/option',
  4612. '/select'
  4613. ];
  4614. $this->assertHtml($expected, $result);
  4615. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4616. $expected = [
  4617. 'select' => [
  4618. 'name' => 'user_id',
  4619. 'disabled' => 'disabled'
  4620. ],
  4621. ['option' => ['value' => '0']], 'option A', '/option',
  4622. '/select'
  4623. ];
  4624. $this->assertHtml($expected, $result);
  4625. }
  4626. /**
  4627. * testNestedSelect method
  4628. *
  4629. * Test select element generation with optgroups.
  4630. *
  4631. * @return void
  4632. */
  4633. public function testNestedSelect()
  4634. {
  4635. $result = $this->Form->select(
  4636. 'Model.field',
  4637. [1 => 'One', 2 => 'Two', 'Three' => [
  4638. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4639. ]],
  4640. ['empty' => false]
  4641. );
  4642. $expected = [
  4643. 'select' => ['name' => 'Model[field]'],
  4644. ['option' => ['value' => 1]],
  4645. 'One',
  4646. '/option',
  4647. ['option' => ['value' => 2]],
  4648. 'Two',
  4649. '/option',
  4650. ['optgroup' => ['label' => 'Three']],
  4651. ['option' => ['value' => 3]],
  4652. 'Three',
  4653. '/option',
  4654. ['option' => ['value' => 4]],
  4655. 'Four',
  4656. '/option',
  4657. ['option' => ['value' => 5]],
  4658. 'Five',
  4659. '/option',
  4660. '/optgroup',
  4661. '/select'
  4662. ];
  4663. $this->assertHtml($expected, $result);
  4664. }
  4665. /**
  4666. * testSelectMultiple method
  4667. *
  4668. * Test generation of multiple select elements.
  4669. *
  4670. * @return void
  4671. */
  4672. public function testSelectMultiple()
  4673. {
  4674. $options = ['first', 'second', 'third'];
  4675. $result = $this->Form->select(
  4676. 'Model.multi_field',
  4677. $options,
  4678. ['form' => 'my-form', 'multiple' => true]
  4679. );
  4680. $expected = [
  4681. 'input' => [
  4682. 'type' => 'hidden',
  4683. 'name' => 'Model[multi_field]',
  4684. 'value' => '',
  4685. 'form' => 'my-form',
  4686. ],
  4687. 'select' => [
  4688. 'name' => 'Model[multi_field][]',
  4689. 'multiple' => 'multiple',
  4690. 'form' => 'my-form',
  4691. ],
  4692. ['option' => ['value' => '0']],
  4693. 'first',
  4694. '/option',
  4695. ['option' => ['value' => '1']],
  4696. 'second',
  4697. '/option',
  4698. ['option' => ['value' => '2']],
  4699. 'third',
  4700. '/option',
  4701. '/select'
  4702. ];
  4703. $this->assertHtml($expected, $result);
  4704. $result = $this->Form->select(
  4705. 'Model.multi_field',
  4706. $options,
  4707. ['multiple' => 'multiple', 'form' => 'my-form']
  4708. );
  4709. $this->assertHtml($expected, $result);
  4710. }
  4711. /**
  4712. * testCheckboxZeroValue method
  4713. *
  4714. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4715. *
  4716. * @return void
  4717. */
  4718. public function testCheckboxZeroValue()
  4719. {
  4720. $result = $this->Form->control('User.get_spam', [
  4721. 'type' => 'checkbox',
  4722. 'value' => '0',
  4723. 'hiddenField' => '1',
  4724. ]);
  4725. $expected = [
  4726. 'div' => ['class' => 'input checkbox'],
  4727. 'label' => ['for' => 'user-get-spam'],
  4728. ['input' => [
  4729. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4730. 'value' => '1'
  4731. ]],
  4732. ['input' => [
  4733. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4734. 'value' => '0', 'id' => 'user-get-spam'
  4735. ]],
  4736. 'Get Spam',
  4737. '/label',
  4738. '/div'
  4739. ];
  4740. $this->assertHtml($expected, $result);
  4741. }
  4742. /**
  4743. * testHabtmSelectBox method
  4744. *
  4745. * Test generation of habtm select boxes.
  4746. *
  4747. * @return void
  4748. */
  4749. public function testHabtmSelectBox()
  4750. {
  4751. $this->loadFixtures('Articles');
  4752. $options = [
  4753. 1 => 'blue',
  4754. 2 => 'red',
  4755. 3 => 'green'
  4756. ];
  4757. $tags = [
  4758. new Entity(['id' => 1, 'name' => 'blue']),
  4759. new Entity(['id' => 3, 'name' => 'green'])
  4760. ];
  4761. $article = new Article(['tags' => $tags]);
  4762. $this->Form->create($article);
  4763. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4764. $expected = [
  4765. 'div' => ['class' => 'input select'],
  4766. 'label' => ['for' => 'tags-ids'],
  4767. 'Tags',
  4768. '/label',
  4769. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4770. 'select' => [
  4771. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4772. 'multiple' => 'multiple'
  4773. ],
  4774. ['option' => ['value' => '1', 'selected' => 'selected']],
  4775. 'blue',
  4776. '/option',
  4777. ['option' => ['value' => '2']],
  4778. 'red',
  4779. '/option',
  4780. ['option' => ['value' => '3', 'selected' => 'selected']],
  4781. 'green',
  4782. '/option',
  4783. '/select',
  4784. '/div'
  4785. ];
  4786. $this->assertHtml($expected, $result);
  4787. // make sure only 50 is selected, and not 50f5c0cf
  4788. $options = [
  4789. '1' => 'blue',
  4790. '50f5c0cf' => 'red',
  4791. '50' => 'green'
  4792. ];
  4793. $tags = [
  4794. new Entity(['id' => 1, 'name' => 'blue']),
  4795. new Entity(['id' => 50, 'name' => 'green'])
  4796. ];
  4797. $article = new Article(['tags' => $tags]);
  4798. $this->Form->create($article);
  4799. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4800. $expected = [
  4801. 'div' => ['class' => 'input select'],
  4802. 'label' => ['for' => 'tags-ids'],
  4803. 'Tags',
  4804. '/label',
  4805. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4806. 'select' => [
  4807. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4808. 'multiple' => 'multiple'
  4809. ],
  4810. ['option' => ['value' => '1', 'selected' => 'selected']],
  4811. 'blue',
  4812. '/option',
  4813. ['option' => ['value' => '50f5c0cf']],
  4814. 'red',
  4815. '/option',
  4816. ['option' => ['value' => '50', 'selected' => 'selected']],
  4817. 'green',
  4818. '/option',
  4819. '/select',
  4820. '/div'
  4821. ];
  4822. $this->assertHtml($expected, $result);
  4823. $spacecraft = [
  4824. 1 => 'Orion',
  4825. 2 => 'Helios'
  4826. ];
  4827. $this->View->viewVars['spacecraft'] = $spacecraft;
  4828. $this->Form->create();
  4829. $result = $this->Form->control('spacecraft._ids');
  4830. $expected = [
  4831. 'div' => ['class' => 'input select'],
  4832. 'label' => ['for' => 'spacecraft-ids'],
  4833. 'Spacecraft',
  4834. '/label',
  4835. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4836. 'select' => [
  4837. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4838. 'multiple' => 'multiple'
  4839. ],
  4840. ['option' => ['value' => '1']],
  4841. 'Orion',
  4842. '/option',
  4843. ['option' => ['value' => '2']],
  4844. 'Helios',
  4845. '/option',
  4846. '/select',
  4847. '/div'
  4848. ];
  4849. $this->assertHtml($expected, $result);
  4850. }
  4851. /**
  4852. * testErrorsForBelongsToManySelect method
  4853. *
  4854. * Tests that errors for belongsToMany select fields are being
  4855. * picked up properly.
  4856. *
  4857. * @return void
  4858. */
  4859. public function testErrorsForBelongsToManySelect()
  4860. {
  4861. $spacecraft = [
  4862. 1 => 'Orion',
  4863. 2 => 'Helios'
  4864. ];
  4865. $this->View->viewVars['spacecraft'] = $spacecraft;
  4866. $article = new Article();
  4867. $article->errors('spacecraft', ['Invalid']);
  4868. $this->Form->create($article);
  4869. $result = $this->Form->control('spacecraft._ids');
  4870. $expected = [
  4871. ['div' => ['class' => 'input select error']],
  4872. 'label' => ['for' => 'spacecraft-ids'],
  4873. 'Spacecraft',
  4874. '/label',
  4875. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4876. 'select' => [
  4877. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4878. 'multiple' => 'multiple'
  4879. ],
  4880. ['option' => ['value' => '1']],
  4881. 'Orion',
  4882. '/option',
  4883. ['option' => ['value' => '2']],
  4884. 'Helios',
  4885. '/option',
  4886. '/select',
  4887. ['div' => ['class' => 'error-message']],
  4888. 'Invalid',
  4889. '/div',
  4890. '/div'
  4891. ];
  4892. $this->assertHtml($expected, $result);
  4893. }
  4894. /**
  4895. * testSelectMultipleCheckboxes method
  4896. *
  4897. * Test generation of multi select elements in checkbox format.
  4898. *
  4899. * @return void
  4900. */
  4901. public function testSelectMultipleCheckboxes()
  4902. {
  4903. $result = $this->Form->select(
  4904. 'Model.multi_field',
  4905. ['first', 'second', 'third'],
  4906. ['multiple' => 'checkbox']
  4907. );
  4908. $expected = [
  4909. 'input' => [
  4910. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4911. ],
  4912. ['div' => ['class' => 'checkbox']],
  4913. ['label' => ['for' => 'model-multi-field-0']],
  4914. ['input' => [
  4915. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4916. 'value' => '0', 'id' => 'model-multi-field-0'
  4917. ]],
  4918. 'first',
  4919. '/label',
  4920. '/div',
  4921. ['div' => ['class' => 'checkbox']],
  4922. ['label' => ['for' => 'model-multi-field-1']],
  4923. ['input' => [
  4924. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4925. 'value' => '1', 'id' => 'model-multi-field-1'
  4926. ]],
  4927. 'second',
  4928. '/label',
  4929. '/div',
  4930. ['div' => ['class' => 'checkbox']],
  4931. ['label' => ['for' => 'model-multi-field-2']],
  4932. ['input' => [
  4933. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4934. 'value' => '2', 'id' => 'model-multi-field-2'
  4935. ]],
  4936. 'third',
  4937. '/label',
  4938. '/div'
  4939. ];
  4940. $this->assertHtml($expected, $result);
  4941. $result = $this->Form->select(
  4942. 'Model.multi_field',
  4943. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4944. ['multiple' => 'checkbox']
  4945. );
  4946. $expected = [
  4947. 'input' => [
  4948. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4949. ],
  4950. ['div' => ['class' => 'checkbox']],
  4951. ['label' => ['for' => 'model-multi-field-a+']],
  4952. ['input' => [
  4953. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4954. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4955. ]],
  4956. 'first',
  4957. '/label',
  4958. '/div',
  4959. ['div' => ['class' => 'checkbox']],
  4960. ['label' => ['for' => 'model-multi-field-a++']],
  4961. ['input' => [
  4962. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4963. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4964. ]],
  4965. 'second',
  4966. '/label',
  4967. '/div',
  4968. ['div' => ['class' => 'checkbox']],
  4969. ['label' => ['for' => 'model-multi-field-a+++']],
  4970. ['input' => [
  4971. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4972. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4973. ]],
  4974. 'third',
  4975. '/label',
  4976. '/div'
  4977. ];
  4978. $this->assertHtml($expected, $result);
  4979. $result = $this->Form->select(
  4980. 'Model.multi_field',
  4981. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4982. ['multiple' => 'checkbox']
  4983. );
  4984. $expected = [
  4985. 'input' => [
  4986. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4987. ],
  4988. ['div' => ['class' => 'checkbox']],
  4989. ['label' => ['for' => 'model-multi-field-a-b']],
  4990. ['input' => [
  4991. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4992. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4993. ]],
  4994. 'first',
  4995. '/label',
  4996. '/div',
  4997. ['div' => ['class' => 'checkbox']],
  4998. ['label' => ['for' => 'model-multi-field-a-b1']],
  4999. ['input' => [
  5000. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5001. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5002. ]],
  5003. 'second',
  5004. '/label',
  5005. '/div',
  5006. ['div' => ['class' => 'checkbox']],
  5007. ['label' => ['for' => 'model-multi-field-a-b2']],
  5008. ['input' => [
  5009. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5010. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5011. ]],
  5012. 'third',
  5013. '/label',
  5014. '/div'
  5015. ];
  5016. $this->assertHtml($expected, $result);
  5017. }
  5018. /**
  5019. * testSelectMultipleCheckboxRequestData method
  5020. *
  5021. * Ensure that multiCheckbox reads from the request data.
  5022. *
  5023. * @return void
  5024. */
  5025. public function testSelectMultipleCheckboxRequestData()
  5026. {
  5027. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5028. $result = $this->Form->select(
  5029. 'Model.tags',
  5030. ['1' => 'first', 'Array' => 'Array'],
  5031. ['multiple' => 'checkbox']
  5032. );
  5033. $expected = [
  5034. 'input' => [
  5035. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5036. ],
  5037. ['div' => ['class' => 'checkbox']],
  5038. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5039. ['input' => [
  5040. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5041. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5042. ]],
  5043. 'first',
  5044. '/label',
  5045. '/div',
  5046. ['div' => ['class' => 'checkbox']],
  5047. ['label' => ['for' => 'model-tags-array']],
  5048. ['input' => [
  5049. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5050. 'value' => 'Array', 'id' => 'model-tags-array'
  5051. ]],
  5052. 'Array',
  5053. '/label',
  5054. '/div'
  5055. ];
  5056. $this->assertHtml($expected, $result);
  5057. }
  5058. /**
  5059. * testSelectMultipleCheckboxSecurity method
  5060. *
  5061. * Checks the security hash array generated for multiple-input checkbox elements.
  5062. *
  5063. * @return void
  5064. */
  5065. public function testSelectMultipleCheckboxSecurity()
  5066. {
  5067. $this->Form->request->params['_Token'] = 'testKey';
  5068. $this->assertEquals([], $this->Form->fields);
  5069. $this->Form->select(
  5070. 'Model.multi_field',
  5071. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5072. ['multiple' => 'checkbox']
  5073. );
  5074. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5075. $result = $this->Form->secure($this->Form->fields);
  5076. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5077. $this->assertRegExp('/"' . $key . '"/', $result);
  5078. }
  5079. /**
  5080. * testSelectMultipleSecureWithNoOptions method
  5081. *
  5082. * Multiple select elements should always be secured as they always participate
  5083. * in the POST data.
  5084. *
  5085. * @return void
  5086. */
  5087. public function testSelectMultipleSecureWithNoOptions()
  5088. {
  5089. $this->assertEquals([], $this->Form->fields);
  5090. $this->Form->select(
  5091. 'Model.select',
  5092. [],
  5093. ['multiple' => true]
  5094. );
  5095. $this->assertEquals(['Model.select'], $this->Form->fields);
  5096. }
  5097. /**
  5098. * testSelectNoSecureWithNoOptions method
  5099. *
  5100. * When a select box has no options it should not be added to the fields list
  5101. * as it always fail post validation.
  5102. *
  5103. * @return void
  5104. */
  5105. public function testSelectNoSecureWithNoOptions()
  5106. {
  5107. $this->Form->request->params['_Token'] = 'testkey';
  5108. $this->assertEquals([], $this->Form->fields);
  5109. $this->Form->select(
  5110. 'Model.select',
  5111. []
  5112. );
  5113. $this->assertEquals([], $this->Form->fields);
  5114. $this->Form->select(
  5115. 'Model.user_id',
  5116. [],
  5117. ['empty' => true]
  5118. );
  5119. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5120. }
  5121. /**
  5122. * testControlMultipleCheckboxes method
  5123. *
  5124. * Test control() resulting in multi select elements being generated.
  5125. *
  5126. * @return void
  5127. */
  5128. public function testControlMultipleCheckboxes()
  5129. {
  5130. $result = $this->Form->control('Model.multi_field', [
  5131. 'options' => ['first', 'second', 'third'],
  5132. 'multiple' => 'checkbox'
  5133. ]);
  5134. $expected = [
  5135. ['div' => ['class' => 'input select']],
  5136. ['label' => ['for' => 'model-multi-field']],
  5137. 'Multi Field',
  5138. '/label',
  5139. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5140. ['div' => ['class' => 'checkbox']],
  5141. ['label' => ['for' => 'model-multi-field-0']],
  5142. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5143. 'first',
  5144. '/label',
  5145. '/div',
  5146. ['div' => ['class' => 'checkbox']],
  5147. ['label' => ['for' => 'model-multi-field-1']],
  5148. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5149. 'second',
  5150. '/label',
  5151. '/div',
  5152. ['div' => ['class' => 'checkbox']],
  5153. ['label' => ['for' => 'model-multi-field-2']],
  5154. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5155. 'third',
  5156. '/label',
  5157. '/div',
  5158. '/div'
  5159. ];
  5160. $this->assertHtml($expected, $result);
  5161. $result = $this->Form->control('Model.multi_field', [
  5162. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5163. 'multiple' => 'checkbox'
  5164. ]);
  5165. $expected = [
  5166. ['div' => ['class' => 'input select']],
  5167. ['label' => ['for' => 'model-multi-field']],
  5168. 'Multi Field',
  5169. '/label',
  5170. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5171. ['div' => ['class' => 'checkbox']],
  5172. ['label' => ['for' => 'model-multi-field-a']],
  5173. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5174. 'first',
  5175. '/label',
  5176. '/div',
  5177. ['div' => ['class' => 'checkbox']],
  5178. ['label' => ['for' => 'model-multi-field-b']],
  5179. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5180. 'second',
  5181. '/label',
  5182. '/div',
  5183. ['div' => ['class' => 'checkbox']],
  5184. ['label' => ['for' => 'model-multi-field-c']],
  5185. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5186. 'third',
  5187. '/label',
  5188. '/div',
  5189. '/div'
  5190. ];
  5191. $this->assertHtml($expected, $result);
  5192. }
  5193. /**
  5194. * testSelectHiddenFieldOmission method
  5195. *
  5196. * Test that select() with 'hiddenField' => false omits the hidden field.
  5197. *
  5198. * @return void
  5199. */
  5200. public function testSelectHiddenFieldOmission()
  5201. {
  5202. $result = $this->Form->select(
  5203. 'Model.multi_field',
  5204. ['first', 'second'],
  5205. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5206. );
  5207. $this->assertNotContains('type="hidden"', $result);
  5208. }
  5209. /**
  5210. * testSelectCheckboxMultipleOverrideName method
  5211. *
  5212. * Test that select() with multiple = checkbox works with overriding name attribute.
  5213. *
  5214. * @return void
  5215. */
  5216. public function testSelectCheckboxMultipleOverrideName()
  5217. {
  5218. $result = $this->Form->select('category', ['1', '2'], [
  5219. 'multiple' => 'checkbox',
  5220. 'name' => 'fish',
  5221. ]);
  5222. $expected = [
  5223. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5224. ['div' => ['class' => 'checkbox']],
  5225. ['label' => ['for' => 'fish-0']],
  5226. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5227. '1',
  5228. '/label',
  5229. '/div',
  5230. ['div' => ['class' => 'checkbox']],
  5231. ['label' => ['for' => 'fish-1']],
  5232. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5233. '2',
  5234. '/label',
  5235. '/div'
  5236. ];
  5237. $this->assertHtml($expected, $result);
  5238. $result = $this->Form->multiCheckbox(
  5239. 'category',
  5240. new Collection(['1', '2']),
  5241. ['name' => 'fish']
  5242. );
  5243. $this->assertHtml($expected, $result);
  5244. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5245. 'name' => 'fish',
  5246. ]);
  5247. $this->assertHtml($expected, $result);
  5248. }
  5249. /**
  5250. * testControlMultiCheckbox method
  5251. *
  5252. * Test that control() works with multicheckbox.
  5253. *
  5254. * @return void
  5255. */
  5256. public function testControlMultiCheckbox()
  5257. {
  5258. $result = $this->Form->control('category', [
  5259. 'type' => 'multicheckbox',
  5260. 'options' => ['1', '2'],
  5261. ]);
  5262. $expected = [
  5263. ['div' => ['class' => 'input multicheckbox']],
  5264. '<label',
  5265. 'Category',
  5266. '/label',
  5267. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5268. ['div' => ['class' => 'checkbox']],
  5269. ['label' => ['for' => 'category-0']],
  5270. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5271. '1',
  5272. '/label',
  5273. '/div',
  5274. ['div' => ['class' => 'checkbox']],
  5275. ['label' => ['for' => 'category-1']],
  5276. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5277. '2',
  5278. '/label',
  5279. '/div',
  5280. '/div',
  5281. ];
  5282. $this->assertHtml($expected, $result);
  5283. }
  5284. /**
  5285. * testCheckbox method
  5286. *
  5287. * Test generation of checkboxes.
  5288. *
  5289. * @return void
  5290. */
  5291. public function testCheckbox()
  5292. {
  5293. $result = $this->Form->checkbox('Model.field');
  5294. $expected = [
  5295. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5296. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5297. ];
  5298. $this->assertHtml($expected, $result);
  5299. $result = $this->Form->checkbox('Model.field', [
  5300. 'id' => 'theID',
  5301. 'value' => 'myvalue',
  5302. 'form' => 'my-form',
  5303. ]);
  5304. $expected = [
  5305. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5306. ['input' => [
  5307. 'type' => 'checkbox', 'name' => 'Model[field]',
  5308. 'value' => 'myvalue', 'id' => 'theID',
  5309. 'form' => 'my-form',
  5310. ]]
  5311. ];
  5312. $this->assertHtml($expected, $result);
  5313. }
  5314. /**
  5315. * testCheckboxDefaultValue method
  5316. *
  5317. * Test default value setting on checkbox() method.
  5318. *
  5319. * @return void
  5320. */
  5321. public function testCheckboxDefaultValue()
  5322. {
  5323. $this->Form->request->data['Model']['field'] = false;
  5324. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5325. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5326. $this->assertHtml($expected, $result);
  5327. unset($this->Form->request->data['Model']['field']);
  5328. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5329. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5330. $this->assertHtml($expected, $result);
  5331. $this->Form->request->data['Model']['field'] = true;
  5332. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5333. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5334. $this->assertHtml($expected, $result);
  5335. unset($this->Form->request->data['Model']['field']);
  5336. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5337. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5338. $this->assertHtml($expected, $result);
  5339. $Articles = TableRegistry::get('Articles');
  5340. $Articles->schema()->addColumn(
  5341. 'published',
  5342. ['type' => 'boolean', 'null' => false, 'default' => true]
  5343. );
  5344. $this->Form->create($Articles->newEntity());
  5345. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5346. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5347. $this->assertHtml($expected, $result);
  5348. }
  5349. /**
  5350. * testCheckboxCheckedAndError method
  5351. *
  5352. * Test checkbox being checked or having errors.
  5353. *
  5354. * @return void
  5355. */
  5356. public function testCheckboxCheckedAndError()
  5357. {
  5358. $this->article['errors'] = [
  5359. 'published' => true
  5360. ];
  5361. $this->Form->request->data['published'] = 'myvalue';
  5362. $this->Form->create($this->article);
  5363. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5364. $expected = [
  5365. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5366. ['input' => [
  5367. 'type' => 'checkbox',
  5368. 'name' => 'published',
  5369. 'value' => 'myvalue',
  5370. 'id' => 'theID',
  5371. 'checked' => 'checked',
  5372. 'class' => 'form-error'
  5373. ]]
  5374. ];
  5375. $this->assertHtml($expected, $result);
  5376. $this->Form->request->data['published'] = '';
  5377. $result = $this->Form->checkbox('published');
  5378. $expected = [
  5379. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5380. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5381. ];
  5382. $this->assertHtml($expected, $result);
  5383. }
  5384. /**
  5385. * testCheckboxCustomNameAttribute method
  5386. *
  5387. * Test checkbox() with a custom name attribute.
  5388. *
  5389. * @return void
  5390. */
  5391. public function testCheckboxCustomNameAttribute()
  5392. {
  5393. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5394. $expected = [
  5395. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5396. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5397. ];
  5398. $this->assertHtml($expected, $result);
  5399. }
  5400. /**
  5401. * testCheckboxHiddenField method
  5402. *
  5403. * Test that the hidden input for checkboxes can be omitted or set to a
  5404. * specific value.
  5405. *
  5406. * @return void
  5407. */
  5408. public function testCheckboxHiddenField()
  5409. {
  5410. $result = $this->Form->checkbox('UserForm.something', [
  5411. 'hiddenField' => false
  5412. ]);
  5413. $expected = [
  5414. 'input' => [
  5415. 'type' => 'checkbox',
  5416. 'name' => 'UserForm[something]',
  5417. 'value' => '1'
  5418. ],
  5419. ];
  5420. $this->assertHtml($expected, $result);
  5421. $result = $this->Form->checkbox('UserForm.something', [
  5422. 'value' => 'Y',
  5423. 'hiddenField' => 'N',
  5424. ]);
  5425. $expected = [
  5426. ['input' => [
  5427. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5428. 'value' => 'N'
  5429. ]],
  5430. ['input' => [
  5431. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5432. 'value' => 'Y'
  5433. ]],
  5434. ];
  5435. $this->assertHtml($expected, $result);
  5436. }
  5437. /**
  5438. * testTime method
  5439. *
  5440. * Test the time type.
  5441. *
  5442. * @return void
  5443. */
  5444. public function testTime()
  5445. {
  5446. $result = $this->Form->time('start_time', [
  5447. 'timeFormat' => 12,
  5448. 'interval' => 5,
  5449. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5450. ]);
  5451. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5452. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5453. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5454. $this->assertNotContains('year', $result);
  5455. $this->assertNotContains('month', $result);
  5456. $this->assertNotContains('day', $result);
  5457. $result = $this->Form->time('start_time', [
  5458. 'timeFormat' => 12,
  5459. 'interval' => 5,
  5460. 'value' => '2014-03-08 16:30:00'
  5461. ]);
  5462. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5463. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5464. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5465. $this->assertNotContains('year', $result);
  5466. $this->assertNotContains('month', $result);
  5467. $this->assertNotContains('day', $result);
  5468. }
  5469. /**
  5470. * testTimeFormat24NoMeridian method
  5471. *
  5472. * Ensure that timeFormat=24 has no merdian.
  5473. *
  5474. * @return void.
  5475. */
  5476. public function testTimeFormat24NoMeridian()
  5477. {
  5478. $result = $this->Form->time('start_time', [
  5479. 'timeFormat' => 24,
  5480. 'interval' => 5,
  5481. 'value' => '2014-03-08 16:30:00'
  5482. ]);
  5483. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5484. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5485. $this->assertNotContains('meridian', $result);
  5486. $this->assertNotContains('pm', $result);
  5487. $this->assertNotContains('year', $result);
  5488. $this->assertNotContains('month', $result);
  5489. $this->assertNotContains('day', $result);
  5490. }
  5491. /**
  5492. * testDate method
  5493. *
  5494. * Test the date type.
  5495. *
  5496. * @return void
  5497. */
  5498. public function testDate()
  5499. {
  5500. $result = $this->Form->date('start_day', [
  5501. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5502. ]);
  5503. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5504. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5505. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5506. $this->assertNotContains('hour', $result);
  5507. $this->assertNotContains('minute', $result);
  5508. $this->assertNotContains('second', $result);
  5509. $this->assertNotContains('meridian', $result);
  5510. }
  5511. /**
  5512. * testDateTime method
  5513. *
  5514. * Test generation of date/time select elements.
  5515. *
  5516. * @return void
  5517. */
  5518. public function testDateTime()
  5519. {
  5520. extract($this->dateRegex);
  5521. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5522. $now = strtotime('now');
  5523. $expected = [
  5524. ['select' => ['name' => 'Contact[date][year]']],
  5525. ['option' => ['value' => '']],
  5526. '/option',
  5527. $yearsRegex,
  5528. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5529. date('Y', $now),
  5530. '/option',
  5531. '*/select',
  5532. ['select' => ['name' => 'Contact[date][month]']],
  5533. ['option' => ['value' => '']],
  5534. '/option',
  5535. $monthsRegex,
  5536. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5537. date('F', $now),
  5538. '/option',
  5539. '*/select',
  5540. ['select' => ['name' => 'Contact[date][day]']],
  5541. ['option' => ['value' => '']],
  5542. '/option',
  5543. $daysRegex,
  5544. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5545. date('j', $now),
  5546. '/option',
  5547. '*/select',
  5548. ['select' => ['name' => 'Contact[date][hour]']],
  5549. ['option' => ['value' => '']],
  5550. '/option',
  5551. $hoursRegex,
  5552. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5553. date('G', $now),
  5554. '/option',
  5555. '*/select',
  5556. ['select' => ['name' => 'Contact[date][minute]']],
  5557. ['option' => ['value' => '']],
  5558. '/option',
  5559. $minutesRegex,
  5560. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5561. date('i', $now),
  5562. '/option',
  5563. '*/select',
  5564. ];
  5565. $this->assertHtml($expected, $result);
  5566. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5567. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5568. $now = strtotime('now');
  5569. $expected = [
  5570. ['select' => ['name' => 'Contact[date][year]']],
  5571. $yearsRegex,
  5572. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5573. date('Y', $now),
  5574. '/option',
  5575. '*/select',
  5576. ['select' => ['name' => 'Contact[date][month]']],
  5577. $monthsRegex,
  5578. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5579. date('F', $now),
  5580. '/option',
  5581. '*/select',
  5582. ['select' => ['name' => 'Contact[date][day]']],
  5583. $daysRegex,
  5584. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5585. date('j', $now),
  5586. '/option',
  5587. '*/select',
  5588. ['select' => ['name' => 'Contact[date][hour]']],
  5589. $hoursRegex,
  5590. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5591. date('G', $now),
  5592. '/option',
  5593. '*/select',
  5594. ['select' => ['name' => 'Contact[date][minute]']],
  5595. $minutesRegex,
  5596. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5597. date('i', $now),
  5598. '/option',
  5599. '*/select',
  5600. ];
  5601. $this->assertHtml($expected, $result);
  5602. }
  5603. /**
  5604. * testDateTimeSecured method
  5605. *
  5606. * Test that datetime fields are added to protected fields list.
  5607. *
  5608. * @return void
  5609. */
  5610. public function testDateTimeSecured()
  5611. {
  5612. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5613. $this->Form->dateTime('Contact.date');
  5614. $expected = [
  5615. 'Contact.date.year',
  5616. 'Contact.date.month',
  5617. 'Contact.date.day',
  5618. 'Contact.date.hour',
  5619. 'Contact.date.minute',
  5620. ];
  5621. $this->assertEquals($expected, $this->Form->fields);
  5622. $this->Form->fields = [];
  5623. $this->Form->date('Contact.published');
  5624. $expected = [
  5625. 'Contact.published.year',
  5626. 'Contact.published.month',
  5627. 'Contact.published.day',
  5628. ];
  5629. $this->assertEquals($expected, $this->Form->fields);
  5630. }
  5631. /**
  5632. * testDateTimeSecuredDisabled method
  5633. *
  5634. * Test that datetime fields are added to protected fields list.
  5635. *
  5636. * @return void
  5637. */
  5638. public function testDateTimeSecuredDisabled()
  5639. {
  5640. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5641. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5642. $expected = [];
  5643. $this->assertEquals($expected, $this->Form->fields);
  5644. $this->Form->fields = [];
  5645. $this->Form->date('Contact.published', ['secure' => false]);
  5646. $expected = [];
  5647. $this->assertEquals($expected, $this->Form->fields);
  5648. }
  5649. /**
  5650. * testDatetimeEmpty method
  5651. *
  5652. * Test empty defaulting to true for datetime.
  5653. *
  5654. * @return void
  5655. */
  5656. public function testDatetimeEmpty()
  5657. {
  5658. extract($this->dateRegex);
  5659. $result = $this->Form->dateTime('Contact.date', [
  5660. 'timeFormat' => 12,
  5661. 'empty' => true,
  5662. 'default' => true
  5663. ]);
  5664. $expected = [
  5665. ['select' => ['name' => 'Contact[date][year]']],
  5666. $yearsRegex,
  5667. ['option' => ['value' => '']],
  5668. '/option',
  5669. '*/select',
  5670. ['select' => ['name' => 'Contact[date][month]']],
  5671. $monthsRegex,
  5672. ['option' => ['value' => '']],
  5673. '/option',
  5674. '*/select',
  5675. ['select' => ['name' => 'Contact[date][day]']],
  5676. $daysRegex,
  5677. ['option' => ['value' => '']],
  5678. '/option',
  5679. '*/select',
  5680. ['select' => ['name' => 'Contact[date][hour]']],
  5681. $hoursRegex,
  5682. ['option' => ['value' => '']],
  5683. '/option',
  5684. '*/select',
  5685. ['select' => ['name' => 'Contact[date][minute]']],
  5686. $minutesRegex,
  5687. ['option' => ['value' => '']],
  5688. '/option',
  5689. '*/select',
  5690. ['select' => ['name' => 'Contact[date][meridian]']],
  5691. $meridianRegex,
  5692. ['option' => ['value' => '']],
  5693. '/option',
  5694. '*/select'
  5695. ];
  5696. $this->assertHtml($expected, $result);
  5697. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5698. }
  5699. /**
  5700. * testDatetimeMinuteInterval method
  5701. *
  5702. * Test datetime with interval option.
  5703. *
  5704. * @return void
  5705. */
  5706. public function testDatetimeMinuteInterval()
  5707. {
  5708. extract($this->dateRegex);
  5709. $result = $this->Form->dateTime('Contact.date', [
  5710. 'interval' => 5,
  5711. 'value' => ''
  5712. ]);
  5713. $expected = [
  5714. ['select' => ['name' => 'Contact[date][year]']],
  5715. $yearsRegex,
  5716. ['option' => ['selected' => 'selected', 'value' => '']],
  5717. '/option',
  5718. '*/select',
  5719. ['select' => ['name' => 'Contact[date][month]']],
  5720. $monthsRegex,
  5721. ['option' => ['selected' => 'selected', 'value' => '']],
  5722. '/option',
  5723. '*/select',
  5724. ['select' => ['name' => 'Contact[date][day]']],
  5725. $daysRegex,
  5726. ['option' => ['selected' => 'selected', 'value' => '']],
  5727. '/option',
  5728. '*/select',
  5729. ['select' => ['name' => 'Contact[date][hour]']],
  5730. $hoursRegex,
  5731. ['option' => ['selected' => 'selected', 'value' => '']],
  5732. '/option',
  5733. '*/select',
  5734. ['select' => ['name' => 'Contact[date][minute]']],
  5735. $minutesRegex,
  5736. ['option' => ['selected' => 'selected', 'value' => '']],
  5737. '/option',
  5738. ['option' => ['value' => '00']],
  5739. '00',
  5740. '/option',
  5741. ['option' => ['value' => '05']],
  5742. '05',
  5743. '/option',
  5744. ['option' => ['value' => '10']],
  5745. '10',
  5746. '/option',
  5747. '*/select',
  5748. ];
  5749. $this->assertHtml($expected, $result);
  5750. }
  5751. /**
  5752. * testDateTimeRounding method
  5753. *
  5754. * Test dateTime with rounding.
  5755. *
  5756. * @return void
  5757. */
  5758. public function testDateTimeRounding()
  5759. {
  5760. $this->Form->request->data['Contact'] = [
  5761. 'date' => [
  5762. 'day' => '13',
  5763. 'month' => '12',
  5764. 'year' => '2010',
  5765. 'hour' => '04',
  5766. 'minute' => '19',
  5767. 'meridian' => 'AM'
  5768. ]
  5769. ];
  5770. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5771. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5772. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5773. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5774. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5775. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5776. }
  5777. /**
  5778. * testDatetimeWithDefault method
  5779. *
  5780. * Test that datetime() and default values work.
  5781. *
  5782. * @return void
  5783. */
  5784. public function testDatetimeWithDefault()
  5785. {
  5786. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5787. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5788. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5789. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5790. $result = $this->Form->dateTime('Contact.updated', [
  5791. 'default' => '2009-06-01 11:15:30'
  5792. ]);
  5793. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5794. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5795. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5796. }
  5797. /**
  5798. * testDateTimeAllZeros method
  5799. *
  5800. * Test datetime() with all zeros.
  5801. *
  5802. * @return void
  5803. */
  5804. public function testDateTimeAllZeros()
  5805. {
  5806. $result = $this->Form->dateTime('Contact.date', [
  5807. 'timeFormat' => false,
  5808. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5809. 'value' => '0000-00-00'
  5810. ]);
  5811. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5812. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5813. }
  5814. /**
  5815. * testDateTimeEmptyAsArray method
  5816. *
  5817. * @return void
  5818. */
  5819. public function testDateTimeEmptyAsArray()
  5820. {
  5821. $result = $this->Form->dateTime('Contact.date', [
  5822. 'empty' => [
  5823. 'day' => 'DAY',
  5824. 'month' => 'MONTH',
  5825. 'year' => 'YEAR',
  5826. 'hour' => 'HOUR',
  5827. 'minute' => 'MINUTE',
  5828. 'meridian' => false
  5829. ],
  5830. 'default' => true
  5831. ]);
  5832. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5833. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5834. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5835. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5836. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5837. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5838. $result = $this->Form->dateTime('Contact.date', [
  5839. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5840. 'default' => true
  5841. ]);
  5842. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5843. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5844. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5845. }
  5846. /**
  5847. * testFormDateTimeMulti method
  5848. *
  5849. * Test multiple datetime element generation.
  5850. *
  5851. * @return void
  5852. */
  5853. public function testFormDateTimeMulti()
  5854. {
  5855. extract($this->dateRegex);
  5856. $result = $this->Form->dateTime('Contact.1.updated');
  5857. $this->assertContains('Contact[1][updated][month]', $result);
  5858. $this->assertContains('Contact[1][updated][day]', $result);
  5859. $this->assertContains('Contact[1][updated][year]', $result);
  5860. $this->assertContains('Contact[1][updated][hour]', $result);
  5861. $this->assertContains('Contact[1][updated][minute]', $result);
  5862. }
  5863. /**
  5864. * testDateTimeLabelIdMatchesFirstControl method
  5865. *
  5866. * When changing the date format, the label should always focus the first select box when
  5867. * clicked.
  5868. *
  5869. * @return void
  5870. */
  5871. public function testDateTimeLabelIdMatchesFirstControl()
  5872. {
  5873. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5874. $this->assertContains('<label>Date</label>', $result);
  5875. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5876. $this->assertContains('<label>Date</label>', $result);
  5877. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5878. $this->assertContains('<label>Date</label>', $result);
  5879. }
  5880. /**
  5881. * testDateTimeSecondOptions method
  5882. *
  5883. * Test datetime second=true.
  5884. *
  5885. * @return void
  5886. */
  5887. public function testDateTimeSecondOptions()
  5888. {
  5889. $result = $this->Form->dateTime('updated', ['second' => true]);
  5890. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5891. $result = $this->Form->dateTime('updated', ['second' => []]);
  5892. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5893. $result = $this->Form->dateTime('updated', ['second' => null]);
  5894. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5895. $result = $this->Form->dateTime('updated', ['second' => false]);
  5896. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5897. }
  5898. /**
  5899. * testMonth method
  5900. *
  5901. * Test generation of a month input.
  5902. *
  5903. * @return void
  5904. */
  5905. public function testMonth()
  5906. {
  5907. $result = $this->Form->month('Model.field', ['value' => '']);
  5908. $expected = [
  5909. ['select' => ['name' => 'Model[field][month]']],
  5910. ['option' => ['value' => '', 'selected' => 'selected']],
  5911. '/option',
  5912. ['option' => ['value' => '01']],
  5913. date('F', strtotime('2008-01-01 00:00:00')),
  5914. '/option',
  5915. ['option' => ['value' => '02']],
  5916. date('F', strtotime('2008-02-01 00:00:00')),
  5917. '/option',
  5918. '*/select',
  5919. ];
  5920. $this->assertHtml($expected, $result);
  5921. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5922. $expected = [
  5923. ['select' => ['name' => 'Model[field][month]']],
  5924. ['option' => ['selected' => 'selected', 'value' => '']],
  5925. '/option',
  5926. ['option' => ['value' => '01']],
  5927. date('F', strtotime('2008-01-01 00:00:00')),
  5928. '/option',
  5929. ['option' => ['value' => '02']],
  5930. date('F', strtotime('2008-02-01 00:00:00')),
  5931. '/option',
  5932. '*/select',
  5933. ];
  5934. $this->assertHtml($expected, $result);
  5935. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5936. $expected = [
  5937. ['select' => ['name' => 'Model[field][month]']],
  5938. ['option' => ['selected' => 'selected', 'value' => '']],
  5939. '/option',
  5940. ['option' => ['value' => '01']],
  5941. '1',
  5942. '/option',
  5943. ['option' => ['value' => '02']],
  5944. '2',
  5945. '/option',
  5946. '*/select',
  5947. ];
  5948. $this->assertHtml($expected, $result);
  5949. $monthNames = [
  5950. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5951. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5952. ];
  5953. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5954. $expected = [
  5955. ['select' => ['name' => 'Model[field][month]']],
  5956. ['option' => ['value' => '']],
  5957. '/option',
  5958. ['option' => ['value' => '01', 'selected' => 'selected']],
  5959. 'Jan',
  5960. '/option',
  5961. ['option' => ['value' => '02']],
  5962. 'Feb',
  5963. '/option',
  5964. '*/select',
  5965. ];
  5966. $this->assertHtml($expected, $result);
  5967. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5968. $result = $this->Form->month('Project.release');
  5969. $expected = [
  5970. ['select' => ['name' => 'Project[release][month]']],
  5971. ['option' => ['value' => '']],
  5972. '/option',
  5973. ['option' => ['value' => '01']],
  5974. 'January',
  5975. '/option',
  5976. ['option' => ['value' => '02', 'selected' => 'selected']],
  5977. 'February',
  5978. '/option',
  5979. '*/select',
  5980. ];
  5981. $this->assertHtml($expected, $result);
  5982. $result = $this->Form->month('Contact.published', [
  5983. 'empty' => 'Published on',
  5984. ]);
  5985. $this->assertContains('Published on', $result);
  5986. }
  5987. /**
  5988. * testDay method
  5989. *
  5990. * Test generation of a day input.
  5991. *
  5992. * @return void
  5993. */
  5994. public function testDay()
  5995. {
  5996. extract($this->dateRegex);
  5997. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  5998. $expected = [
  5999. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6000. ['option' => ['selected' => 'selected', 'value' => '']],
  6001. '/option',
  6002. ['option' => ['value' => '01']],
  6003. '1',
  6004. '/option',
  6005. ['option' => ['value' => '02']],
  6006. '2',
  6007. '/option',
  6008. $daysRegex,
  6009. '/select',
  6010. ];
  6011. $this->assertHtml($expected, $result);
  6012. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6013. $result = $this->Form->day('Model.field');
  6014. $expected = [
  6015. ['select' => ['name' => 'Model[field][day]']],
  6016. ['option' => ['value' => '']],
  6017. '/option',
  6018. ['option' => ['value' => '01']],
  6019. '1',
  6020. '/option',
  6021. ['option' => ['value' => '02']],
  6022. '2',
  6023. '/option',
  6024. $daysRegex,
  6025. ['option' => ['value' => '10', 'selected' => 'selected']],
  6026. '10',
  6027. '/option',
  6028. $daysRegex,
  6029. '/select',
  6030. ];
  6031. $this->assertHtml($expected, $result);
  6032. $this->Form->request->data['Model']['field'] = '';
  6033. $result = $this->Form->day('Model.field', ['value' => '10']);
  6034. $expected = [
  6035. ['select' => ['name' => 'Model[field][day]']],
  6036. ['option' => ['value' => '']],
  6037. '/option',
  6038. ['option' => ['value' => '01']],
  6039. '1',
  6040. '/option',
  6041. ['option' => ['value' => '02']],
  6042. '2',
  6043. '/option',
  6044. $daysRegex,
  6045. ['option' => ['value' => '10', 'selected' => 'selected']],
  6046. '10',
  6047. '/option',
  6048. $daysRegex,
  6049. '/select',
  6050. ];
  6051. $this->assertHtml($expected, $result);
  6052. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6053. $result = $this->Form->day('Project.release');
  6054. $expected = [
  6055. ['select' => ['name' => 'Project[release][day]']],
  6056. ['option' => ['value' => '']],
  6057. '/option',
  6058. ['option' => ['value' => '01']],
  6059. '1',
  6060. '/option',
  6061. ['option' => ['value' => '02']],
  6062. '2',
  6063. '/option',
  6064. $daysRegex,
  6065. ['option' => ['value' => '10', 'selected' => 'selected']],
  6066. '10',
  6067. '/option',
  6068. $daysRegex,
  6069. '/select',
  6070. ];
  6071. $this->assertHtml($expected, $result);
  6072. $result = $this->Form->day('Contact.published', [
  6073. 'empty' => 'Published on',
  6074. ]);
  6075. $this->assertContains('Published on', $result);
  6076. }
  6077. /**
  6078. * testMinute method
  6079. *
  6080. * Test generation of a minute input.
  6081. *
  6082. * @return void
  6083. */
  6084. public function testMinute()
  6085. {
  6086. extract($this->dateRegex);
  6087. $result = $this->Form->minute('Model.field', ['value' => '']);
  6088. $expected = [
  6089. ['select' => ['name' => 'Model[field][minute]']],
  6090. ['option' => ['selected' => 'selected', 'value' => '']],
  6091. '/option',
  6092. ['option' => ['value' => '00']],
  6093. '00',
  6094. '/option',
  6095. ['option' => ['value' => '01']],
  6096. '01',
  6097. '/option',
  6098. ['option' => ['value' => '02']],
  6099. '02',
  6100. '/option',
  6101. $minutesRegex,
  6102. '/select',
  6103. ];
  6104. $this->assertHtml($expected, $result);
  6105. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6106. $result = $this->Form->minute('Model.field');
  6107. $expected = [
  6108. ['select' => ['name' => 'Model[field][minute]']],
  6109. ['option' => ['value' => '']],
  6110. '/option',
  6111. ['option' => ['value' => '00']],
  6112. '00',
  6113. '/option',
  6114. ['option' => ['value' => '01']],
  6115. '01',
  6116. '/option',
  6117. ['option' => ['value' => '02']],
  6118. '02',
  6119. '/option',
  6120. $minutesRegex,
  6121. ['option' => ['value' => '12', 'selected' => 'selected']],
  6122. '12',
  6123. '/option',
  6124. $minutesRegex,
  6125. '/select',
  6126. ];
  6127. $this->assertHtml($expected, $result);
  6128. $this->Form->request->data['Model']['field'] = '';
  6129. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6130. $expected = [
  6131. ['select' => ['name' => 'Model[field][minute]']],
  6132. ['option' => ['selected' => 'selected', 'value' => '']],
  6133. '/option',
  6134. ['option' => ['value' => '00']],
  6135. '00',
  6136. '/option',
  6137. ['option' => ['value' => '05']],
  6138. '05',
  6139. '/option',
  6140. ['option' => ['value' => '10']],
  6141. '10',
  6142. '/option',
  6143. $minutesRegex,
  6144. '/select',
  6145. ];
  6146. $this->assertHtml($expected, $result);
  6147. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6148. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6149. $expected = [
  6150. ['select' => ['name' => 'Model[field][minute]']],
  6151. ['option' => ['value' => '']],
  6152. '/option',
  6153. ['option' => ['value' => '00']],
  6154. '00',
  6155. '/option',
  6156. ['option' => ['value' => '05']],
  6157. '05',
  6158. '/option',
  6159. ['option' => ['value' => '10', 'selected' => 'selected']],
  6160. '10',
  6161. '/option',
  6162. $minutesRegex,
  6163. '/select',
  6164. ];
  6165. $this->assertHtml($expected, $result);
  6166. }
  6167. /**
  6168. * testMeridian method
  6169. *
  6170. * Test generating an input for the meridian.
  6171. *
  6172. * @return void
  6173. */
  6174. public function testMeridian()
  6175. {
  6176. extract($this->dateRegex);
  6177. $now = new \DateTime();
  6178. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6179. $expected = [
  6180. ['select' => ['name' => 'Model[field][meridian]']],
  6181. ['option' => ['value' => '']],
  6182. '/option',
  6183. $meridianRegex,
  6184. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6185. $now->format('a'),
  6186. '/option',
  6187. '*/select'
  6188. ];
  6189. $this->assertHtml($expected, $result);
  6190. }
  6191. /**
  6192. * testHour method
  6193. *
  6194. * Test generation of an hour input.
  6195. *
  6196. * @return void
  6197. */
  6198. public function testHour()
  6199. {
  6200. extract($this->dateRegex);
  6201. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6202. $expected = [
  6203. ['select' => ['name' => 'Model[field][hour]']],
  6204. ['option' => ['selected' => 'selected', 'value' => '']],
  6205. '/option',
  6206. ['option' => ['value' => '01']],
  6207. '1',
  6208. '/option',
  6209. ['option' => ['value' => '02']],
  6210. '2',
  6211. '/option',
  6212. $hoursRegex,
  6213. '/select',
  6214. ];
  6215. $this->assertHtml($expected, $result);
  6216. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6217. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6218. $expected = [
  6219. ['select' => ['name' => 'Model[field][hour]']],
  6220. ['option' => ['value' => '']],
  6221. '/option',
  6222. ['option' => ['value' => '01']],
  6223. '1',
  6224. '/option',
  6225. ['option' => ['value' => '02']],
  6226. '2',
  6227. '/option',
  6228. $hoursRegex,
  6229. ['option' => ['value' => '12', 'selected' => 'selected']],
  6230. '12',
  6231. '/option',
  6232. '/select',
  6233. ];
  6234. $this->assertHtml($expected, $result);
  6235. $this->Form->request->data['Model']['field'] = '';
  6236. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6237. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6238. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6239. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6240. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6241. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6242. $expected = [
  6243. ['select' => ['name' => 'Model[field][hour]']],
  6244. ['option' => ['value' => '']],
  6245. '/option',
  6246. ['option' => ['value' => '00', 'selected' => 'selected']],
  6247. '0',
  6248. '/option',
  6249. ['option' => ['value' => '01']],
  6250. '1',
  6251. '/option',
  6252. ['option' => ['value' => '02']],
  6253. '2',
  6254. '/option',
  6255. $hoursRegex,
  6256. '/select',
  6257. ];
  6258. $this->assertHtml($expected, $result);
  6259. unset($this->Form->request->data['Model']['field']);
  6260. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6261. $thisHour = date('H');
  6262. $optValue = date('G');
  6263. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6264. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6265. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6266. $expected = [
  6267. ['select' => ['name' => 'Model[field][hour]']],
  6268. ['option' => ['value' => '']],
  6269. '/option',
  6270. ['option' => ['value' => '00']],
  6271. '0',
  6272. '/option',
  6273. ['option' => ['value' => '01', 'selected' => 'selected']],
  6274. '1',
  6275. '/option',
  6276. ['option' => ['value' => '02']],
  6277. '2',
  6278. '/option',
  6279. $hoursRegex,
  6280. '/select',
  6281. ];
  6282. $this->assertHtml($expected, $result);
  6283. }
  6284. /**
  6285. * testYear method
  6286. *
  6287. * Test generation of a year input.
  6288. *
  6289. * @return void
  6290. */
  6291. public function testYear()
  6292. {
  6293. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6294. $expected = [
  6295. ['select' => ['name' => 'Model[field][year]']],
  6296. ['option' => ['selected' => 'selected', 'value' => '']],
  6297. '/option',
  6298. ['option' => ['value' => '2007']],
  6299. '2007',
  6300. '/option',
  6301. ['option' => ['value' => '2006']],
  6302. '2006',
  6303. '/option',
  6304. '/select',
  6305. ];
  6306. $this->assertHtml($expected, $result);
  6307. $result = $this->Form->year('Model.field', [
  6308. 'value' => '',
  6309. 'minYear' => 2006,
  6310. 'maxYear' => 2007,
  6311. 'orderYear' => 'asc'
  6312. ]);
  6313. $expected = [
  6314. ['select' => ['name' => 'Model[field][year]']],
  6315. ['option' => ['selected' => 'selected', 'value' => '']],
  6316. '/option',
  6317. ['option' => ['value' => '2006']],
  6318. '2006',
  6319. '/option',
  6320. ['option' => ['value' => '2007']],
  6321. '2007',
  6322. '/option',
  6323. '/select',
  6324. ];
  6325. $this->assertHtml($expected, $result);
  6326. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6327. $result = $this->Form->year('Contact.published', [
  6328. 'empty' => false,
  6329. 'minYear' => 2006,
  6330. 'maxYear' => 2007,
  6331. ]);
  6332. $expected = [
  6333. ['select' => ['name' => 'Contact[published][year]']],
  6334. ['option' => ['value' => '2007']],
  6335. '2007',
  6336. '/option',
  6337. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6338. '2006',
  6339. '/option',
  6340. '/select',
  6341. ];
  6342. $this->assertHtml($expected, $result);
  6343. $result = $this->Form->year('Contact.published', [
  6344. 'empty' => 'Published on',
  6345. ]);
  6346. $this->assertContains('Published on', $result);
  6347. }
  6348. /**
  6349. * testControlDatetimePreEpoch method
  6350. *
  6351. * Test minYear being prior to the unix epoch.
  6352. *
  6353. * @return void
  6354. */
  6355. public function testControlDatetimePreEpoch()
  6356. {
  6357. $start = date('Y') - 80;
  6358. $end = date('Y') - 18;
  6359. $result = $this->Form->control('birth_year', [
  6360. 'type' => 'date',
  6361. 'label' => 'Birth Year',
  6362. 'minYear' => $start,
  6363. 'maxYear' => $end,
  6364. 'month' => false,
  6365. 'day' => false,
  6366. ]);
  6367. $this->assertContains('value="' . $start . '">' . $start, $result);
  6368. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6369. $this->assertNotContains('value="00">00', $result);
  6370. }
  6371. /**
  6372. * testYearAutoExpandRange method
  6373. *
  6374. * @return void
  6375. */
  6376. public function testYearAutoExpandRange()
  6377. {
  6378. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6379. $result = $this->Form->year('User.birthday');
  6380. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6381. $result = $matches[1];
  6382. $expected = range(date('Y') + 5, 1930);
  6383. $this->assertEquals($expected, $result);
  6384. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6385. $result = $this->Form->year('Project.release');
  6386. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6387. $result = $matches[1];
  6388. $expected = range(2050, date('Y') - 5);
  6389. $this->assertEquals($expected, $result);
  6390. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6391. $result = $this->Form->year('Project.release', [
  6392. 'minYear' => 1890,
  6393. 'maxYear' => 1900
  6394. ]);
  6395. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6396. $result = $matches[1];
  6397. $expected = range(1900, 1881);
  6398. $this->assertEquals($expected, $result);
  6399. }
  6400. /**
  6401. * testControlDate method
  6402. *
  6403. * Test that control() accepts the type of date and passes options in.
  6404. *
  6405. * @return void
  6406. */
  6407. public function testControlDate()
  6408. {
  6409. $this->Form->request->data = [
  6410. 'month_year' => ['month' => date('m')],
  6411. ];
  6412. $this->Form->create($this->article);
  6413. $result = $this->Form->control('month_year', [
  6414. 'label' => false,
  6415. 'type' => 'date',
  6416. 'minYear' => 2006,
  6417. 'maxYear' => 2008
  6418. ]);
  6419. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6420. $this->assertNotContains('value="2008" selected="selected"', $result);
  6421. }
  6422. /**
  6423. * testControlLabelFalse method
  6424. *
  6425. * Test the label option being set to false.
  6426. *
  6427. * @return void
  6428. */
  6429. public function testControlLabelFalse()
  6430. {
  6431. $this->Form->create($this->article);
  6432. $result = $this->Form->control('title', ['label' => false]);
  6433. $expected = [
  6434. 'div' => ['class' => 'input text required'],
  6435. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6436. '/div'
  6437. ];
  6438. $this->assertHtml($expected, $result);
  6439. }
  6440. /**
  6441. * testControlDateMaxYear method
  6442. *
  6443. * Let's say we want to only allow users born from 2006 to 2008 to register
  6444. * This being the first signup page, we still don't have any data.
  6445. *
  6446. * @return void
  6447. */
  6448. public function testControlDateMaxYear()
  6449. {
  6450. $this->Form->request->data = [];
  6451. $this->Form->create($this->article);
  6452. $result = $this->Form->control('birthday', [
  6453. 'label' => false,
  6454. 'type' => 'date',
  6455. 'minYear' => 2006,
  6456. 'maxYear' => 2008,
  6457. 'default' => true
  6458. ]);
  6459. $this->assertContains('value="2008" selected="selected"', $result);
  6460. $this->assertContains('value="2006"', $result);
  6461. $this->assertNotContains('value="2005"', $result);
  6462. $this->assertNotContains('value="2009"', $result);
  6463. }
  6464. /**
  6465. * testTextArea method
  6466. *
  6467. * Test generation of a textarea input.
  6468. *
  6469. * @return void
  6470. */
  6471. public function testTextArea()
  6472. {
  6473. $this->Form->request->data = ['field' => 'some test data'];
  6474. $result = $this->Form->textarea('field');
  6475. $expected = [
  6476. 'textarea' => ['name' => 'field', 'rows' => 5],
  6477. 'some test data',
  6478. '/textarea',
  6479. ];
  6480. $this->assertHtml($expected, $result);
  6481. $result = $this->Form->textarea('user.bio');
  6482. $expected = [
  6483. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6484. '/textarea',
  6485. ];
  6486. $this->assertHtml($expected, $result);
  6487. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6488. $result = $this->Form->textarea('field');
  6489. $expected = [
  6490. 'textarea' => ['name' => 'field', 'rows' => 5],
  6491. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6492. '/textarea',
  6493. ];
  6494. $this->assertHtml($expected, $result);
  6495. $this->Form->request->data = [
  6496. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6497. ];
  6498. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6499. $expected = [
  6500. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6501. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6502. '/textarea',
  6503. ];
  6504. $this->assertHtml($expected, $result);
  6505. $result = $this->Form->textarea('0.OtherModel.field');
  6506. $expected = [
  6507. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6508. '/textarea'
  6509. ];
  6510. $this->assertHtml($expected, $result);
  6511. }
  6512. /**
  6513. * testTextAreaWithStupidCharacters method
  6514. *
  6515. * Test text area with non-ascii characters.
  6516. *
  6517. * @return void
  6518. */
  6519. public function testTextAreaWithStupidCharacters()
  6520. {
  6521. $result = $this->Form->textarea('Post.content', [
  6522. 'value' => "GREAT®",
  6523. 'rows' => '15',
  6524. 'cols' => '75'
  6525. ]);
  6526. $expected = [
  6527. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6528. 'GREAT®',
  6529. '/textarea',
  6530. ];
  6531. $this->assertHtml($expected, $result);
  6532. }
  6533. /**
  6534. * testTextAreaMaxLength method
  6535. *
  6536. * Test textareas maxlength read from schema.
  6537. *
  6538. * @return void
  6539. */
  6540. public function testTextAreaMaxLength()
  6541. {
  6542. $this->Form->create([
  6543. 'schema' => [
  6544. 'stuff' => ['type' => 'string', 'length' => 10],
  6545. ]
  6546. ]);
  6547. $result = $this->Form->control('other', ['type' => 'textarea']);
  6548. $expected = [
  6549. 'div' => ['class' => 'input textarea'],
  6550. 'label' => ['for' => 'other'],
  6551. 'Other',
  6552. '/label',
  6553. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6554. '/textarea',
  6555. '/div'
  6556. ];
  6557. $this->assertHtml($expected, $result);
  6558. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6559. $expected = [
  6560. 'div' => ['class' => 'input textarea'],
  6561. 'label' => ['for' => 'stuff'],
  6562. 'Stuff',
  6563. '/label',
  6564. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6565. '/textarea',
  6566. '/div'
  6567. ];
  6568. $this->assertHtml($expected, $result);
  6569. }
  6570. /**
  6571. * testHiddenField method
  6572. *
  6573. * Test generation of a hidden input.
  6574. *
  6575. * @return void
  6576. */
  6577. public function testHiddenField()
  6578. {
  6579. $this->article['errors'] = [
  6580. 'field' => true
  6581. ];
  6582. $this->Form->request->data['field'] = 'test';
  6583. $this->Form->create($this->article);
  6584. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6585. $expected = [
  6586. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6587. $this->assertHtml($expected, $result);
  6588. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6589. $expected = [
  6590. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6591. ];
  6592. $this->assertHtml($expected, $result);
  6593. }
  6594. /**
  6595. * testFileUploadField method
  6596. *
  6597. * Test generation of a file upload input.
  6598. *
  6599. * @return void
  6600. */
  6601. public function testFileUploadField()
  6602. {
  6603. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6604. $result = $this->Form->file('Model.upload');
  6605. $this->assertHtml($expected, $result);
  6606. $this->Form->request->data['Model']['upload'] = [
  6607. 'name' => '', 'type' => '', 'tmp_name' => '',
  6608. 'error' => 4, 'size' => 0
  6609. ];
  6610. $result = $this->Form->file('Model.upload');
  6611. $this->assertHtml($expected, $result);
  6612. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6613. $result = $this->Form->file('Model.upload');
  6614. $this->assertHtml($expected, $result);
  6615. }
  6616. /**
  6617. * testFileUploadOnOtherModel method
  6618. *
  6619. * Test File upload input on a model not used in create().
  6620. *
  6621. * @return void
  6622. */
  6623. public function testFileUploadOnOtherModel()
  6624. {
  6625. $this->Form->create($this->article, ['type' => 'file']);
  6626. $result = $this->Form->file('ValidateProfile.city');
  6627. $expected = [
  6628. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6629. ];
  6630. $this->assertHtml($expected, $result);
  6631. }
  6632. /**
  6633. * testButton method
  6634. *
  6635. * Test generation of a form button.
  6636. *
  6637. * @return void
  6638. */
  6639. public function testButton()
  6640. {
  6641. $result = $this->Form->button('Hi');
  6642. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6643. $this->assertHtml($expected, $result);
  6644. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6645. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6646. $this->assertHtml($expected, $result);
  6647. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6648. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6649. $this->assertHtml($expected, $result);
  6650. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6651. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6652. $this->assertHtml($expected, $result);
  6653. $result = $this->Form->button('No type', ['type' => false]);
  6654. $expected = ['button' => [], 'No type', '/button'];
  6655. $this->assertHtml($expected, $result);
  6656. $result = $this->Form->button('Upload Text', [
  6657. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6658. 'escape' => false
  6659. ]);
  6660. $this->assertNotRegExp('/\&039/', $result);
  6661. }
  6662. /**
  6663. * testButtonUnlockedByDefault method
  6664. *
  6665. * Test that button() makes unlocked fields by default.
  6666. *
  6667. * @return void
  6668. */
  6669. public function testButtonUnlockedByDefault()
  6670. {
  6671. $this->Form->request->params['_csrfToken'] = 'secured';
  6672. $this->Form->button('Save', ['name' => 'save']);
  6673. $this->Form->button('Clear');
  6674. $result = $this->Form->unlockField();
  6675. $this->assertEquals(['save'], $result);
  6676. }
  6677. /**
  6678. * testPostButton method
  6679. *
  6680. * @return void
  6681. */
  6682. public function testPostButton()
  6683. {
  6684. $result = $this->Form->postButton('Hi', '/controller/action');
  6685. $expected = [
  6686. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6687. 'div' => ['style' => 'display:none;'],
  6688. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6689. '/div',
  6690. 'button' => ['type' => 'submit'],
  6691. 'Hi',
  6692. '/button',
  6693. '/form'
  6694. ];
  6695. $this->assertHtml($expected, $result);
  6696. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6697. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6698. }
  6699. /**
  6700. * testPostButtonMethodType method
  6701. *
  6702. * @return void
  6703. */
  6704. public function testPostButtonMethodType()
  6705. {
  6706. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6707. $expected = [
  6708. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6709. 'div' => ['style' => 'display:none;'],
  6710. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6711. '/div',
  6712. 'button' => ['type' => 'submit'],
  6713. 'Hi',
  6714. '/button',
  6715. '/form'
  6716. ];
  6717. $this->assertHtml($expected, $result);
  6718. }
  6719. /**
  6720. * testPostButtonFormOptions method
  6721. *
  6722. * @return void
  6723. */
  6724. public function testPostButtonFormOptions()
  6725. {
  6726. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6727. $expected = [
  6728. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6729. 'div' => ['style' => 'display:none;'],
  6730. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6731. '/div',
  6732. 'button' => ['type' => 'submit'],
  6733. 'Hi',
  6734. '/button',
  6735. '/form'
  6736. ];
  6737. $this->assertHtml($expected, $result);
  6738. }
  6739. /**
  6740. * testPostButtonNestedData method
  6741. *
  6742. * Test using postButton with N dimensional data.
  6743. *
  6744. * @return void
  6745. */
  6746. public function testPostButtonNestedData()
  6747. {
  6748. $data = [
  6749. 'one' => [
  6750. 'two' => [
  6751. 3, 4, 5
  6752. ]
  6753. ]
  6754. ];
  6755. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6756. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6757. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6758. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6759. }
  6760. /**
  6761. * testSecurePostButton method
  6762. *
  6763. * Test that postButton adds _Token fields.
  6764. *
  6765. * @return void
  6766. */
  6767. public function testSecurePostButton()
  6768. {
  6769. $this->Form->request->params['_csrfToken'] = 'testkey';
  6770. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6771. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6772. $tokenDebug = urlencode(json_encode([
  6773. '/posts/delete/1',
  6774. [],
  6775. []
  6776. ]));
  6777. $expected = [
  6778. 'form' => [
  6779. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6780. ],
  6781. ['div' => ['style' => 'display:none;']],
  6782. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6783. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6784. '/div',
  6785. 'button' => ['type' => 'submit'],
  6786. 'Delete',
  6787. '/button',
  6788. ['div' => ['style' => 'display:none;']],
  6789. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6790. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6791. ['input' => [
  6792. 'type' => 'hidden',
  6793. 'name' => '_Token[debug]',
  6794. 'value' => $tokenDebug,
  6795. 'autocomplete' => 'off',
  6796. ]],
  6797. '/div',
  6798. '/form',
  6799. ];
  6800. $this->assertHtml($expected, $result);
  6801. }
  6802. /**
  6803. * testPostLink method
  6804. *
  6805. * @return void
  6806. */
  6807. public function testPostLink()
  6808. {
  6809. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6810. $expected = [
  6811. 'form' => [
  6812. 'method' => 'post', 'action' => '/posts/delete/1',
  6813. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6814. ],
  6815. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6816. '/form',
  6817. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6818. 'Delete',
  6819. '/a'
  6820. ];
  6821. $this->assertHtml($expected, $result);
  6822. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6823. $expected = [
  6824. 'form' => [
  6825. 'method' => 'post', 'action' => '/posts/delete/1',
  6826. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6827. ],
  6828. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6829. '/form',
  6830. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6831. 'Delete',
  6832. '/a'
  6833. ];
  6834. $this->assertHtml($expected, $result);
  6835. $result = $this->Form->postLink(
  6836. 'Delete',
  6837. '/posts/delete/1',
  6838. ['target' => '_blank', 'class' => 'btn btn-danger']
  6839. );
  6840. $expected = [
  6841. 'form' => [
  6842. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6843. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6844. ],
  6845. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6846. '/form',
  6847. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6848. 'Delete',
  6849. '/a'
  6850. ];
  6851. $this->assertHtml($expected, $result);
  6852. }
  6853. /**
  6854. * testPostLinkWithConfirm method
  6855. *
  6856. * Test the confirm option for postLink().
  6857. *
  6858. * @return void
  6859. */
  6860. public function testPostLinkWithConfirm()
  6861. {
  6862. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6863. $expected = [
  6864. 'form' => [
  6865. 'method' => 'post', 'action' => '/posts/delete/1',
  6866. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6867. ],
  6868. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6869. '/form',
  6870. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6871. 'Delete',
  6872. '/a'
  6873. ];
  6874. $this->assertHtml($expected, $result);
  6875. $result = $this->Form->postLink(
  6876. 'Delete',
  6877. '/posts/delete/1',
  6878. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6879. );
  6880. $expected = [
  6881. 'form' => [
  6882. 'method' => 'post', 'action' => '/posts/delete/1',
  6883. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6884. ],
  6885. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6886. '/form',
  6887. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6888. 'Delete',
  6889. '/a'
  6890. ];
  6891. $this->assertHtml($expected, $result);
  6892. }
  6893. /**
  6894. * testPostLinkWithQuery method
  6895. *
  6896. * Test postLink() with query string args.
  6897. *
  6898. * @return void
  6899. */
  6900. public function testPostLinkWithQuery()
  6901. {
  6902. $result = $this->Form->postLink(
  6903. 'Delete',
  6904. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6905. );
  6906. $expected = [
  6907. 'form' => [
  6908. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6909. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6910. ],
  6911. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6912. '/form',
  6913. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6914. 'Delete',
  6915. '/a'
  6916. ];
  6917. $this->assertHtml($expected, $result);
  6918. }
  6919. /**
  6920. * testPostLinkWithData method
  6921. *
  6922. * Test postLink with additional data.
  6923. *
  6924. * @return void
  6925. */
  6926. public function testPostLinkWithData()
  6927. {
  6928. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6929. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6930. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6931. $this->Form->create($entity);
  6932. $this->Form->end();
  6933. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6934. $this->assertContains(
  6935. '<input type="hidden" name="name" value="show"',
  6936. $result,
  6937. 'should not contain entity data.'
  6938. );
  6939. }
  6940. /**
  6941. * testPostLinkSecurityHash method
  6942. *
  6943. * Test that security hashes for postLink include the url.
  6944. *
  6945. * @return void
  6946. */
  6947. public function testPostLinkSecurityHash()
  6948. {
  6949. $hash = Security::hash(
  6950. '/posts/delete/1' .
  6951. serialize(['id' => '1']) .
  6952. '' .
  6953. Security::salt()
  6954. );
  6955. $hash .= '%3Aid';
  6956. $this->Form->request->params['_Token']['key'] = 'test';
  6957. $result = $this->Form->postLink(
  6958. 'Delete',
  6959. '/posts/delete/1',
  6960. ['data' => ['id' => 1]]
  6961. );
  6962. $tokenDebug = urlencode(json_encode([
  6963. '/posts/delete/1',
  6964. [
  6965. 'id' => 1
  6966. ],
  6967. []
  6968. ]));
  6969. $expected = [
  6970. 'form' => [
  6971. 'method' => 'post', 'action' => '/posts/delete/1',
  6972. 'name', 'style' => 'display:none;'
  6973. ],
  6974. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6975. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6976. 'div' => ['style' => 'display:none;'],
  6977. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6978. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6979. ['input' => [
  6980. 'type' => 'hidden',
  6981. 'name' => '_Token[debug]',
  6982. 'value' => $tokenDebug,
  6983. 'autocomplete' => 'off',
  6984. ]],
  6985. '/div',
  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. * testPostLinkSecurityHashBlockMode method
  6995. *
  6996. * Test that postLink doesn't modify the fields in the containing form.
  6997. *
  6998. * postLink() calls inside open forms should not modify the field list
  6999. * for the form.
  7000. *
  7001. * @return void
  7002. */
  7003. public function testPostLinkSecurityHashBlockMode()
  7004. {
  7005. $hash = Security::hash(
  7006. '/posts/delete/1' .
  7007. serialize([]) .
  7008. '' .
  7009. Security::salt()
  7010. );
  7011. $hash .= '%3A';
  7012. $this->Form->request->params['_Token']['key'] = 'test';
  7013. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7014. $this->Form->control('title');
  7015. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7016. $result = $this->View->fetch('postLink');
  7017. $this->assertEquals(['title'], $this->Form->fields);
  7018. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7019. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7020. }
  7021. /**
  7022. * testPostLinkSecurityHashNoDebugMode method
  7023. *
  7024. * Test that security does not include debug token if debug is false.
  7025. *
  7026. * @return void
  7027. */
  7028. public function testPostLinkSecurityHashNoDebugMode()
  7029. {
  7030. Configure::write('debug', false);
  7031. $hash = Security::hash(
  7032. '/posts/delete/1' .
  7033. serialize(['id' => '1']) .
  7034. '' .
  7035. Security::salt()
  7036. );
  7037. $hash .= '%3Aid';
  7038. $this->Form->request->params['_Token']['key'] = 'test';
  7039. $result = $this->Form->postLink(
  7040. 'Delete',
  7041. '/posts/delete/1',
  7042. ['data' => ['id' => 1]]
  7043. );
  7044. $expected = [
  7045. 'form' => [
  7046. 'method' => 'post', 'action' => '/posts/delete/1',
  7047. 'name', 'style' => 'display:none;'
  7048. ],
  7049. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7050. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7051. 'div' => ['style' => 'display:none;'],
  7052. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7053. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7054. '/div',
  7055. '/form',
  7056. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7057. 'Delete',
  7058. '/a'
  7059. ];
  7060. $this->assertHtml($expected, $result);
  7061. }
  7062. /**
  7063. * testPostLinkNestedData method
  7064. *
  7065. * Test using postLink with N dimensional data.
  7066. *
  7067. * @return void
  7068. */
  7069. public function testPostLinkNestedData()
  7070. {
  7071. $data = [
  7072. 'one' => [
  7073. 'two' => [
  7074. 3, 4, 5
  7075. ]
  7076. ]
  7077. ];
  7078. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7079. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7080. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7081. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7082. }
  7083. /**
  7084. * testPostLinkAfterGetForm method
  7085. *
  7086. * Test creating postLinks after a GET form.
  7087. *
  7088. * @return void
  7089. */
  7090. public function testPostLinkAfterGetForm()
  7091. {
  7092. $this->Form->request->params['_csrfToken'] = 'testkey';
  7093. $this->Form->request->params['_Token'] = 'val';
  7094. $this->Form->create($this->article, ['type' => 'get']);
  7095. $this->Form->end();
  7096. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7097. $tokenDebug = urlencode(json_encode([
  7098. '/posts/delete/1',
  7099. [],
  7100. []
  7101. ]));
  7102. $expected = [
  7103. 'form' => [
  7104. 'method' => 'post', 'action' => '/posts/delete/1',
  7105. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7106. ],
  7107. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7108. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7109. 'div' => ['style' => 'display:none;'],
  7110. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7111. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7112. ['input' => [
  7113. 'type' => 'hidden', 'name' => '_Token[debug]',
  7114. 'value' => $tokenDebug,
  7115. 'autocomplete' => 'off'
  7116. ]],
  7117. '/div',
  7118. '/form',
  7119. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7120. 'Delete',
  7121. '/a'
  7122. ];
  7123. $this->assertHtml($expected, $result);
  7124. }
  7125. /**
  7126. * testPostLinkFormBuffer method
  7127. *
  7128. * Test that postLink adds form tags to view block.
  7129. *
  7130. * @return void
  7131. */
  7132. public function testPostLinkFormBuffer()
  7133. {
  7134. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7135. $expected = [
  7136. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7137. 'Delete',
  7138. '/a'
  7139. ];
  7140. $this->assertHtml($expected, $result);
  7141. $result = $this->View->fetch('postLink');
  7142. $expected = [
  7143. 'form' => [
  7144. 'method' => 'post', 'action' => '/posts/delete/1',
  7145. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7146. ],
  7147. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7148. '/form'
  7149. ];
  7150. $this->assertHtml($expected, $result);
  7151. $result = $this->Form->postLink(
  7152. 'Delete',
  7153. '/posts/delete/2',
  7154. ['block' => true, 'method' => 'DELETE']
  7155. );
  7156. $expected = [
  7157. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7158. 'Delete',
  7159. '/a'
  7160. ];
  7161. $this->assertHtml($expected, $result);
  7162. $result = $this->View->fetch('postLink');
  7163. $expected = [
  7164. 'form' => [
  7165. 'method' => 'post', 'action' => '/posts/delete/1',
  7166. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7167. ],
  7168. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7169. '/form',
  7170. [
  7171. 'form' => [
  7172. 'method' => 'post', 'action' => '/posts/delete/2',
  7173. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7174. ],
  7175. ],
  7176. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7177. '/form'
  7178. ];
  7179. $this->assertHtml($expected, $result);
  7180. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7181. $expected = [
  7182. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7183. 'Delete',
  7184. '/a'
  7185. ];
  7186. $this->assertHtml($expected, $result);
  7187. $result = $this->View->fetch('foobar');
  7188. $expected = [
  7189. 'form' => [
  7190. 'method' => 'post', 'action' => '/posts/delete/1',
  7191. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7192. ],
  7193. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7194. '/form'
  7195. ];
  7196. $this->assertHtml($expected, $result);
  7197. }
  7198. /**
  7199. * testSubmitButton method
  7200. *
  7201. * @return void
  7202. */
  7203. public function testSubmitButton()
  7204. {
  7205. $result = $this->Form->submit('');
  7206. $expected = [
  7207. 'div' => ['class' => 'submit'],
  7208. 'input' => ['type' => 'submit', 'value' => ''],
  7209. '/div'
  7210. ];
  7211. $this->assertHtml($expected, $result);
  7212. $result = $this->Form->submit('Test Submit');
  7213. $expected = [
  7214. 'div' => ['class' => 'submit'],
  7215. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7216. '/div'
  7217. ];
  7218. $this->assertHtml($expected, $result);
  7219. $result = $this->Form->submit('Next >');
  7220. $expected = [
  7221. 'div' => ['class' => 'submit'],
  7222. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7223. '/div'
  7224. ];
  7225. $this->assertHtml($expected, $result);
  7226. $result = $this->Form->submit('Next >', ['escape' => false]);
  7227. $expected = [
  7228. 'div' => ['class' => 'submit'],
  7229. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7230. '/div'
  7231. ];
  7232. $this->assertHtml($expected, $result);
  7233. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7234. $expected = [
  7235. 'div' => ['class' => 'submit'],
  7236. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7237. '/div'
  7238. ];
  7239. $this->assertHtml($expected, $result);
  7240. }
  7241. /**
  7242. * testSubmitImage method
  7243. *
  7244. * Test image submit types.
  7245. *
  7246. * @return void
  7247. */
  7248. public function testSubmitImage()
  7249. {
  7250. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7251. $expected = [
  7252. 'div' => ['class' => 'submit'],
  7253. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7254. '/div'
  7255. ];
  7256. $this->assertHtml($expected, $result);
  7257. $result = $this->Form->submit('/relative/cake.power.gif');
  7258. $expected = [
  7259. 'div' => ['class' => 'submit'],
  7260. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7261. '/div'
  7262. ];
  7263. $this->assertHtml($expected, $result);
  7264. $result = $this->Form->submit('cake.power.gif');
  7265. $expected = [
  7266. 'div' => ['class' => 'submit'],
  7267. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7268. '/div'
  7269. ];
  7270. $this->assertHtml($expected, $result);
  7271. $result = $this->Form->submit('Not.an.image');
  7272. $expected = [
  7273. 'div' => ['class' => 'submit'],
  7274. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7275. '/div'
  7276. ];
  7277. $this->assertHtml($expected, $result);
  7278. }
  7279. /**
  7280. * testSubmitUnlockedByDefault method
  7281. *
  7282. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7283. * be submitted at a time.
  7284. *
  7285. * @return void
  7286. */
  7287. public function testSubmitUnlockedByDefault()
  7288. {
  7289. $this->Form->request->params['_Token'] = 'secured';
  7290. $this->Form->submit('Go go');
  7291. $this->Form->submit('Save', ['name' => 'save']);
  7292. $result = $this->Form->unlockField();
  7293. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7294. }
  7295. /**
  7296. * testSubmitImageTimestamp method
  7297. *
  7298. * Test submit image with timestamps.
  7299. *
  7300. * @return void
  7301. */
  7302. public function testSubmitImageTimestamp()
  7303. {
  7304. Configure::write('Asset.timestamp', 'force');
  7305. $result = $this->Form->submit('cake.power.gif');
  7306. $expected = [
  7307. 'div' => ['class' => 'submit'],
  7308. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7309. '/div'
  7310. ];
  7311. $this->assertHtml($expected, $result);
  7312. }
  7313. /**
  7314. * testDateTimeWithGetForms method
  7315. *
  7316. * Test that datetime() works with GET style forms.
  7317. *
  7318. * @return void
  7319. */
  7320. public function testDateTimeWithGetForms()
  7321. {
  7322. extract($this->dateRegex);
  7323. $this->Form->create($this->article, ['type' => 'get']);
  7324. $result = $this->Form->datetime('created');
  7325. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7326. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7327. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7328. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7329. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7330. }
  7331. /**
  7332. * testForMagicControlNonExistingNorValidated method
  7333. *
  7334. * @return void
  7335. */
  7336. public function testForMagicControlNonExistingNorValidated()
  7337. {
  7338. $this->Form->create($this->article);
  7339. $this->Form->templates(['inputContainer' => '{{content}}']);
  7340. $result = $this->Form->control('non_existing_nor_validated');
  7341. $expected = [
  7342. 'label' => ['for' => 'non-existing-nor-validated'],
  7343. 'Non Existing Nor Validated',
  7344. '/label',
  7345. 'input' => [
  7346. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7347. 'id' => 'non-existing-nor-validated'
  7348. ]
  7349. ];
  7350. $this->assertHtml($expected, $result);
  7351. $result = $this->Form->control('non_existing_nor_validated', [
  7352. 'val' => 'my value'
  7353. ]);
  7354. $expected = [
  7355. 'label' => ['for' => 'non-existing-nor-validated'],
  7356. 'Non Existing Nor Validated',
  7357. '/label',
  7358. 'input' => [
  7359. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7360. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7361. ]
  7362. ];
  7363. $this->assertHtml($expected, $result);
  7364. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7365. $result = $this->Form->control('non_existing_nor_validated');
  7366. $expected = [
  7367. 'label' => ['for' => 'non-existing-nor-validated'],
  7368. 'Non Existing Nor Validated',
  7369. '/label',
  7370. 'input' => [
  7371. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7372. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7373. ]
  7374. ];
  7375. $this->assertHtml($expected, $result);
  7376. }
  7377. /**
  7378. * testFormMagicControlLabel method
  7379. *
  7380. * @return void
  7381. */
  7382. public function testFormMagicControlLabel()
  7383. {
  7384. TableRegistry::get('Contacts', [
  7385. 'className' => __NAMESPACE__ . '\ContactsTable'
  7386. ]);
  7387. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7388. $this->Form->templates(['inputContainer' => '{{content}}']);
  7389. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7390. $expected = [
  7391. 'label' => ['for' => 'contacts-name'],
  7392. 'My label',
  7393. '/label',
  7394. 'input' => [
  7395. 'type' => 'text', 'name' => 'Contacts[name]',
  7396. 'id' => 'contacts-name', 'maxlength' => '255'
  7397. ]
  7398. ];
  7399. $this->assertHtml($expected, $result);
  7400. $result = $this->Form->control('name', [
  7401. 'label' => ['class' => 'mandatory']
  7402. ]);
  7403. $expected = [
  7404. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7405. 'Name',
  7406. '/label',
  7407. 'input' => [
  7408. 'type' => 'text', 'name' => 'name',
  7409. 'id' => 'name', 'maxlength' => '255'
  7410. ]
  7411. ];
  7412. $this->assertHtml($expected, $result);
  7413. $result = $this->Form->control('name', [
  7414. 'div' => false,
  7415. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7416. ]);
  7417. $expected = [
  7418. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7419. 'My label',
  7420. '/label',
  7421. 'input' => [
  7422. 'type' => 'text', 'name' => 'name',
  7423. 'id' => 'name', 'maxlength' => '255'
  7424. ]
  7425. ];
  7426. $this->assertHtml($expected, $result);
  7427. $result = $this->Form->control('Contact.name', [
  7428. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7429. ]);
  7430. $expected = [
  7431. 'label' => ['for' => 'my_id'],
  7432. 'Name',
  7433. '/label',
  7434. 'input' => [
  7435. 'type' => 'text', 'name' => 'Contact[name]',
  7436. 'id' => 'my_id', 'maxlength' => '255'
  7437. ]
  7438. ];
  7439. $this->assertHtml($expected, $result);
  7440. $result = $this->Form->control('1.id');
  7441. $expected = ['input' => [
  7442. 'type' => 'hidden', 'name' => '1[id]',
  7443. 'id' => '1-id'
  7444. ]];
  7445. $this->assertHtml($expected, $result);
  7446. $result = $this->Form->control("1.name");
  7447. $expected = [
  7448. 'label' => ['for' => '1-name'],
  7449. 'Name',
  7450. '/label',
  7451. 'input' => [
  7452. 'type' => 'text', 'name' => '1[name]',
  7453. 'id' => '1-name', 'maxlength' => '255'
  7454. ]
  7455. ];
  7456. $this->assertHtml($expected, $result);
  7457. }
  7458. /**
  7459. * testFormEnd method
  7460. *
  7461. * @return void
  7462. */
  7463. public function testFormEnd()
  7464. {
  7465. $this->assertEquals('</form>', $this->Form->end());
  7466. }
  7467. /**
  7468. * testMultiRecordForm method
  7469. *
  7470. * Test the generation of fields for a multi record form.
  7471. *
  7472. * @return void
  7473. */
  7474. public function testMultiRecordForm()
  7475. {
  7476. $this->loadFixtures('Articles', 'Comments');
  7477. $articles = TableRegistry::get('Articles');
  7478. $articles->hasMany('Comments');
  7479. $comment = new Entity(['comment' => 'Value']);
  7480. $article = new Article(['comments' => [$comment]]);
  7481. $this->Form->create([$article]);
  7482. $result = $this->Form->control('0.comments.1.comment');
  7483. //@codingStandardsIgnoreStart
  7484. $expected = [
  7485. 'div' => ['class' => 'input textarea'],
  7486. 'label' => ['for' => '0-comments-1-comment'],
  7487. 'Comment',
  7488. '/label',
  7489. 'textarea' => [
  7490. 'name',
  7491. 'id' => '0-comments-1-comment',
  7492. 'rows' => 5
  7493. ],
  7494. '/textarea',
  7495. '/div'
  7496. ];
  7497. //@codingStandardsIgnoreEnd
  7498. $this->assertHtml($expected, $result);
  7499. $result = $this->Form->control('0.comments.0.comment');
  7500. //@codingStandardsIgnoreStart
  7501. $expected = [
  7502. 'div' => ['class' => 'input textarea'],
  7503. 'label' => ['for' => '0-comments-0-comment'],
  7504. 'Comment',
  7505. '/label',
  7506. 'textarea' => [
  7507. 'name',
  7508. 'id' => '0-comments-0-comment',
  7509. 'rows' => 5
  7510. ],
  7511. 'Value',
  7512. '/textarea',
  7513. '/div'
  7514. ];
  7515. //@codingStandardsIgnoreEnd
  7516. $this->assertHtml($expected, $result);
  7517. $comment->errors('comment', ['Not valid']);
  7518. $result = $this->Form->control('0.comments.0.comment');
  7519. //@codingStandardsIgnoreStart
  7520. $expected = [
  7521. 'div' => ['class' => 'input textarea error'],
  7522. 'label' => ['for' => '0-comments-0-comment'],
  7523. 'Comment',
  7524. '/label',
  7525. 'textarea' => [
  7526. 'name',
  7527. 'class' => 'form-error',
  7528. 'id' => '0-comments-0-comment',
  7529. 'rows' => 5
  7530. ],
  7531. 'Value',
  7532. '/textarea',
  7533. ['div' => ['class' => 'error-message']],
  7534. 'Not valid',
  7535. '/div',
  7536. '/div'
  7537. ];
  7538. //@codingStandardsIgnoreEnd
  7539. $this->assertHtml($expected, $result);
  7540. TableRegistry::get('Comments')
  7541. ->validator('default')
  7542. ->allowEmpty('comment', false);
  7543. $result = $this->Form->control('0.comments.1.comment');
  7544. //@codingStandardsIgnoreStart
  7545. $expected = [
  7546. 'div' => ['class' => 'input textarea required'],
  7547. 'label' => ['for' => '0-comments-1-comment'],
  7548. 'Comment',
  7549. '/label',
  7550. 'textarea' => [
  7551. 'name',
  7552. 'required' => 'required',
  7553. 'id' => '0-comments-1-comment',
  7554. 'rows' => 5
  7555. ],
  7556. '/textarea',
  7557. '/div'
  7558. ];
  7559. //@codingStandardsIgnoreEnd
  7560. $this->assertHtml($expected, $result);
  7561. }
  7562. /**
  7563. * testHtml5Controls method
  7564. *
  7565. * Test that some html5 inputs + FormHelper::__call() work.
  7566. *
  7567. * @return void
  7568. */
  7569. public function testHtml5Controls()
  7570. {
  7571. $result = $this->Form->email('User.email');
  7572. $expected = [
  7573. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7574. ];
  7575. $this->assertHtml($expected, $result);
  7576. $result = $this->Form->search('User.query');
  7577. $expected = [
  7578. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7579. ];
  7580. $this->assertHtml($expected, $result);
  7581. $result = $this->Form->search('User.query', ['value' => 'test']);
  7582. $expected = [
  7583. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7584. ];
  7585. $this->assertHtml($expected, $result);
  7586. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7587. $expected = [
  7588. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7589. ];
  7590. $this->assertHtml($expected, $result);
  7591. }
  7592. /**
  7593. * testHtml5ControlWithControl method
  7594. *
  7595. * Test accessing html5 inputs through control().
  7596. *
  7597. * @return void
  7598. */
  7599. public function testHtml5ControlWithControl()
  7600. {
  7601. $this->Form->create();
  7602. $this->Form->templates(['inputContainer' => '{{content}}']);
  7603. $result = $this->Form->control('website', [
  7604. 'type' => 'url',
  7605. 'val' => 'http://domain.tld',
  7606. 'label' => false
  7607. ]);
  7608. $expected = [
  7609. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7610. ];
  7611. $this->assertHtml($expected, $result);
  7612. }
  7613. /**
  7614. * testHtml5ControlException method
  7615. *
  7616. * Test errors when field name is missing.
  7617. *
  7618. * @expectedException \Cake\Core\Exception\Exception
  7619. * @return void
  7620. */
  7621. public function testHtml5ControlException()
  7622. {
  7623. $this->Form->email();
  7624. }
  7625. /**
  7626. * testRequiredAttribute method
  7627. *
  7628. * Tests that formhelper sets required attributes.
  7629. *
  7630. * @return void
  7631. */
  7632. public function testRequiredAttribute()
  7633. {
  7634. $this->article['required'] = [
  7635. 'title' => true,
  7636. 'body' => false,
  7637. ];
  7638. $this->Form->create($this->article);
  7639. $result = $this->Form->control('title');
  7640. $expected = [
  7641. 'div' => ['class' => 'input text required'],
  7642. 'label' => ['for' => 'title'],
  7643. 'Title',
  7644. '/label',
  7645. 'input' => [
  7646. 'type' => 'text',
  7647. 'name' => 'title',
  7648. 'id' => 'title',
  7649. 'required' => 'required',
  7650. ],
  7651. '/div'
  7652. ];
  7653. $this->assertHtml($expected, $result);
  7654. $result = $this->Form->control('title', ['required' => false]);
  7655. $this->assertNotContains('required', $result);
  7656. $result = $this->Form->control('body');
  7657. $expected = [
  7658. 'div' => ['class' => 'input text'],
  7659. 'label' => ['for' => 'body'],
  7660. 'Body',
  7661. '/label',
  7662. 'input' => [
  7663. 'type' => 'text',
  7664. 'name' => 'body',
  7665. 'id' => 'body',
  7666. ],
  7667. '/div'
  7668. ];
  7669. $this->assertHtml($expected, $result);
  7670. $result = $this->Form->control('body', ['required' => true]);
  7671. $this->assertContains('required', $result);
  7672. }
  7673. /**
  7674. * testControlsNotNested method
  7675. *
  7676. * Tests that it is possible to put inputs outside of the label.
  7677. *
  7678. * @return void
  7679. */
  7680. public function testControlsNotNested()
  7681. {
  7682. $this->Form->templates([
  7683. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7684. 'formGroup' => '{{input}}{{label}}',
  7685. ]);
  7686. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7687. $expected = [
  7688. 'div' => ['class' => 'input checkbox'],
  7689. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7690. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7691. 'label' => ['for' => 'foo'],
  7692. 'Foo',
  7693. '/label',
  7694. '/div'
  7695. ];
  7696. $this->assertHtml($expected, $result);
  7697. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7698. $expected = [
  7699. 'div' => ['class' => 'input checkbox'],
  7700. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7701. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7702. '/div'
  7703. ];
  7704. $this->assertHtml($expected, $result);
  7705. $result = $this->Form->control('confirm', [
  7706. 'type' => 'radio',
  7707. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7708. ]);
  7709. $expected = [
  7710. 'div' => ['class' => 'input radio'],
  7711. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7712. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7713. ['label' => ['for' => 'confirm-y']],
  7714. 'Yes',
  7715. '/label',
  7716. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7717. ['label' => ['for' => 'confirm-n']],
  7718. 'No',
  7719. '/label',
  7720. '<label',
  7721. 'Confirm',
  7722. '/label',
  7723. '/div',
  7724. ];
  7725. $this->assertHtml($expected, $result);
  7726. $result = $this->Form->select('category', ['1', '2'], [
  7727. 'multiple' => 'checkbox',
  7728. 'name' => 'fish',
  7729. ]);
  7730. $expected = [
  7731. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7732. ['div' => ['class' => 'checkbox']],
  7733. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7734. ['label' => ['for' => 'fish-0']],
  7735. '1',
  7736. '/label',
  7737. '/div',
  7738. ['div' => ['class' => 'checkbox']],
  7739. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7740. ['label' => ['for' => 'fish-1']],
  7741. '2',
  7742. '/label',
  7743. '/div'
  7744. ];
  7745. $this->assertHtml($expected, $result);
  7746. }
  7747. /**
  7748. * testControlContainerTemplates method
  7749. *
  7750. * Test that *Container templates are used by input.
  7751. *
  7752. * @return void
  7753. */
  7754. public function testControlContainerTemplates()
  7755. {
  7756. $this->Form->templates([
  7757. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7758. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7759. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7760. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7761. ]);
  7762. $this->article['errors'] = [
  7763. 'Article' => ['published' => 'error message']
  7764. ];
  7765. $this->Form->create($this->article);
  7766. $result = $this->Form->control('accept', [
  7767. 'type' => 'checkbox'
  7768. ]);
  7769. $expected = [
  7770. 'div' => ['class' => 'check'],
  7771. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7772. 'label' => ['for' => 'accept'],
  7773. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7774. 'Accept',
  7775. '/label',
  7776. '/div'
  7777. ];
  7778. $this->assertHtml($expected, $result);
  7779. $result = $this->Form->control('accept', [
  7780. 'type' => 'radio',
  7781. 'options' => ['Y', 'N']
  7782. ]);
  7783. $this->assertContains('<div class="rad">', $result);
  7784. $result = $this->Form->control('Article.published', [
  7785. 'type' => 'radio',
  7786. 'options' => ['Y', 'N']
  7787. ]);
  7788. $this->assertContains('<div class="rad err">', $result);
  7789. $result = $this->Form->control('Article.created', [
  7790. 'type' => 'datetime',
  7791. ]);
  7792. $this->assertContains('<div class="dt">', $result);
  7793. }
  7794. /**
  7795. * testFormGroupTemplates method
  7796. *
  7797. * Test that *Container templates are used by input.
  7798. *
  7799. * @return void
  7800. */
  7801. public function testFormGroupTemplates()
  7802. {
  7803. $this->Form->templates([
  7804. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7805. ]);
  7806. $this->Form->create($this->article);
  7807. $result = $this->Form->control('accept', [
  7808. 'type' => 'radio',
  7809. 'options' => ['Y', 'N']
  7810. ]);
  7811. $this->assertContains('<div class="radio">', $result);
  7812. }
  7813. /**
  7814. * testResetTemplates method
  7815. *
  7816. * Test resetting templates.
  7817. *
  7818. * @return void
  7819. */
  7820. public function testResetTemplates()
  7821. {
  7822. $this->Form->templates(['input' => '<input/>']);
  7823. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7824. $this->assertNull($this->Form->resetTemplates());
  7825. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7826. }
  7827. /**
  7828. * testContext method
  7829. *
  7830. * Test the context method.
  7831. *
  7832. * @return void
  7833. */
  7834. public function testContext()
  7835. {
  7836. $result = $this->Form->context();
  7837. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7838. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7839. $this->assertSame($mock, $this->Form->context($mock));
  7840. $this->assertSame($mock, $this->Form->context());
  7841. }
  7842. /**
  7843. * testAutoDomId method
  7844. *
  7845. * @return void
  7846. */
  7847. public function testAutoDomId()
  7848. {
  7849. $result = $this->Form->text('field', ['id' => true]);
  7850. $expected = [
  7851. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7852. ];
  7853. $this->assertHtml($expected, $result);
  7854. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7855. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7856. $expected = [
  7857. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7858. ['label' => ['for' => 'field-0']],
  7859. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7860. 'option A',
  7861. '/label',
  7862. ['label' => ['for' => 'field-1']],
  7863. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7864. 'option B',
  7865. '/label',
  7866. ];
  7867. $this->assertHtml($expected, $result);
  7868. $result = $this->Form->select(
  7869. 'multi_field',
  7870. ['first', 'second'],
  7871. ['multiple' => 'checkbox', 'id' => true]
  7872. );
  7873. $expected = [
  7874. 'input' => [
  7875. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7876. ],
  7877. ['div' => ['class' => 'checkbox']],
  7878. ['label' => ['for' => 'multi-field-0']],
  7879. ['input' => [
  7880. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7881. 'value' => '0', 'id' => 'multi-field-0'
  7882. ]],
  7883. 'first',
  7884. '/label',
  7885. '/div',
  7886. ['div' => ['class' => 'checkbox']],
  7887. ['label' => ['for' => 'multi-field-1']],
  7888. ['input' => [
  7889. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7890. 'value' => '1', 'id' => 'multi-field-1'
  7891. ]],
  7892. 'second',
  7893. '/label',
  7894. '/div',
  7895. ];
  7896. $this->assertHtml($expected, $result);
  7897. }
  7898. /**
  7899. * Test the basic setters and getters for value sources
  7900. *
  7901. * @return void
  7902. */
  7903. public function testFormValueSourcesSettersGetters()
  7904. {
  7905. $expected = ['context'];
  7906. $result = $this->Form->getValueSources();
  7907. $this->assertEquals($expected, $result);
  7908. $expected = null;
  7909. $result = $this->Form->getSourceValue('id');
  7910. $this->assertEquals($expected, $result);
  7911. $expected = ['query', 'data', 'context'];
  7912. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7913. $result = $this->Form->getValueSources();
  7914. $this->assertEquals($expected, $result);
  7915. $this->Form->request->data['id'] = '1';
  7916. $this->Form->request->query['id'] = '2';
  7917. $this->Form->setValueSources(['context']);
  7918. $expected = '1';
  7919. $result = $this->Form->getSourceValue('id');
  7920. $this->assertEquals($expected, $result);
  7921. $this->Form->setValueSources('query');
  7922. $expected = ['query'];
  7923. $result = $this->Form->getValueSources();
  7924. $this->assertEquals($expected, $result);
  7925. $expected = '2';
  7926. $result = $this->Form->getSourceValue('id');
  7927. $this->assertEquals($expected, $result);
  7928. $this->Form->setValueSources(['data']);
  7929. $expected = '1';
  7930. $result = $this->Form->getSourceValue('id');
  7931. $this->assertEquals($expected, $result);
  7932. $this->Form->setValueSources(['query', 'data']);
  7933. $expected = '2';
  7934. $result = $this->Form->getSourceValue('id');
  7935. $this->assertEquals($expected, $result);
  7936. }
  7937. /**
  7938. * Tests the different input rendering values based on sources values switching
  7939. *
  7940. * @return void
  7941. */
  7942. public function testFormValueSourcesSingleSwitchRendering()
  7943. {
  7944. $this->loadFixtures('Articles');
  7945. $articles = TableRegistry::get('Articles');
  7946. $article = new Article();
  7947. $articles->patchEntity($article, ['id' => '3']);
  7948. $this->Form->create($article);
  7949. $this->Form->setValueSources(['context']);
  7950. $result = $this->Form->control('id');
  7951. $expected = [
  7952. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7953. ];
  7954. $this->assertHtml($expected, $result);
  7955. $this->Form->request->query['id'] = '5';
  7956. $this->Form->setValueSources(['query']);
  7957. $result = $this->Form->control('id');
  7958. $expected = [
  7959. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7960. ];
  7961. $this->assertHtml($expected, $result);
  7962. $this->Form->request->query['id'] = '5a';
  7963. $this->Form->request->data['id'] = '5b';
  7964. $this->Form->setValueSources(['context']);
  7965. $this->Form->create($article);
  7966. $result = $this->Form->control('id');
  7967. $expected = [
  7968. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7969. ];
  7970. $this->assertHtml($expected, $result);
  7971. $this->Form->setValueSources(['data']);
  7972. $this->Form->create($article);
  7973. $result = $this->Form->control('id');
  7974. $expected = [
  7975. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7976. ];
  7977. $this->assertHtml($expected, $result);
  7978. $this->Form->setValueSources(['query']);
  7979. $result = $this->Form->control('id');
  7980. $expected = [
  7981. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7982. ];
  7983. $this->assertHtml($expected, $result);
  7984. }
  7985. /**
  7986. * Tests the different input rendering values based on sources values switching while supplying
  7987. * an entity (base context) and multiple sources (such as data, query)
  7988. *
  7989. * @return void
  7990. */
  7991. public function testFormValueSourcesListSwitchRendering()
  7992. {
  7993. $this->loadFixtures('Articles');
  7994. $articles = TableRegistry::get('Articles');
  7995. $article = new Article();
  7996. $articles->patchEntity($article, ['id' => '3']);
  7997. $this->Form->request->query['id'] = '9';
  7998. $this->Form->create($article);
  7999. $this->Form->setValueSources(['context', 'query']);
  8000. $result = $this->Form->control('id');
  8001. $expected = [
  8002. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8003. ];
  8004. $this->assertHtml($expected, $result);
  8005. $this->Form->setValueSources(['query', 'context']);
  8006. $result = $this->Form->control('id');
  8007. $expected = [
  8008. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8009. ];
  8010. $this->assertHtml($expected, $result);
  8011. $this->Form->setValueSources(['data', 'query', 'context']);
  8012. $result = $this->Form->control('id');
  8013. $expected = [
  8014. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8015. ];
  8016. $this->assertHtml($expected, $result);
  8017. $this->Form->request->data['id'] = '8';
  8018. $this->Form->request->query['id'] = '9';
  8019. $this->Form->setValueSources(['data', 'query', 'context']);
  8020. $result = $this->Form->control('id');
  8021. $expected = [
  8022. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8023. ];
  8024. $this->assertHtml($expected, $result);
  8025. }
  8026. /**
  8027. * Test the different form input renderings based on values sources switchings through form options
  8028. *
  8029. * @return void
  8030. */
  8031. public function testFormValueSourcesSwitchViaOptionsRendering()
  8032. {
  8033. $this->loadFixtures('Articles');
  8034. $articles = TableRegistry::get('Articles');
  8035. $article = new Article();
  8036. $articles->patchEntity($article, ['id' => '3']);
  8037. $this->Form->request->data['id'] = '4';
  8038. $this->Form->request->query['id'] = '5';
  8039. $this->Form->create($article, ['valueSources' => 'query']);
  8040. $result = $this->Form->control('id');
  8041. $expected = [
  8042. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8043. ];
  8044. $this->assertHtml($expected, $result);
  8045. $result = $this->Form->getSourceValue('id');
  8046. $this->assertEquals('5', $result);
  8047. $this->Form->setValueSources(['context']);
  8048. $this->Form->create($article, ['valueSources' => 'query']);
  8049. $result = $this->Form->control('id');
  8050. $expected = [
  8051. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8052. ];
  8053. $this->assertHtml($expected, $result);
  8054. $result = $this->Form->getSourceValue('id');
  8055. $this->assertEquals('5', $result);
  8056. $this->Form->setValueSources(['query']);
  8057. $this->Form->create($article, ['valueSources' => 'data']);
  8058. $result = $this->Form->control('id');
  8059. $expected = [
  8060. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8061. ];
  8062. $this->assertHtml($expected, $result);
  8063. $result = $this->Form->getSourceValue('id');
  8064. $this->assertEquals('4', $result);
  8065. $this->Form->setValueSources(['query']);
  8066. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8067. $result = $this->Form->control('id');
  8068. $expected = [
  8069. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8070. ];
  8071. $this->assertHtml($expected, $result);
  8072. $result = $this->Form->getSourceValue('id');
  8073. $this->assertEquals('4', $result);
  8074. }
  8075. /**
  8076. * Test the different form input renderings based on values sources switchings through form options
  8077. *
  8078. * @return void
  8079. */
  8080. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8081. {
  8082. $this->loadFixtures('Articles');
  8083. $articles = TableRegistry::get('Articles');
  8084. $article = new Article();
  8085. $articles->patchEntity($article, ['id' => '3']);
  8086. $this->Form->request->data['id'] = '10';
  8087. $this->Form->request->query['id'] = '11';
  8088. $this->Form->setValueSources(['context'])
  8089. ->create($article, ['valueSources' => ['query', 'data']]);
  8090. $result = $this->Form->control('id');
  8091. $expected = [
  8092. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8093. ];
  8094. $this->assertHtml($expected, $result);
  8095. $result = $this->Form->getSourceValue('id');
  8096. $this->assertEquals('11', $result);
  8097. unset($this->Form->request->query['id']);
  8098. $this->Form->setValueSources(['context'])
  8099. ->create($article, ['valueSources' => ['query', 'data']]);
  8100. $result = $this->Form->control('id');
  8101. $expected = [
  8102. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8103. ];
  8104. $this->assertHtml($expected, $result);
  8105. $result = $this->Form->getSourceValue('id');
  8106. $this->assertEquals('10', $result);
  8107. }
  8108. /**
  8109. * Test the different form values sources resetting through From::end();
  8110. *
  8111. * @return void
  8112. */
  8113. public function testFormValueSourcesResetViaEnd()
  8114. {
  8115. $expected = ['context'];
  8116. $result = $this->Form->getValueSources();
  8117. $this->assertEquals($expected, $result);
  8118. $expected = ['query', 'context', 'data'];
  8119. $this->Form->setValueSources(['query', 'context', 'data']);
  8120. $result = $this->Form->getValueSources();
  8121. $this->assertEquals($expected, $result);
  8122. $this->Form->create();
  8123. $result = $this->Form->getValueSources();
  8124. $this->assertEquals($expected, $result);
  8125. $this->Form->end();
  8126. $result = $this->Form->getValueSources();
  8127. $this->assertEquals(['context'], $result);
  8128. }
  8129. /**
  8130. * Test sources values defaults handling
  8131. *
  8132. * @return void
  8133. */
  8134. public function testFormValueSourcesDefaults()
  8135. {
  8136. $this->Form->request->query['password'] = 'open Sesame';
  8137. $this->Form->create();
  8138. $result = $this->Form->password('password');
  8139. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8140. $this->assertHtml($expected, $result);
  8141. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8142. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8143. $this->assertHtml($expected, $result);
  8144. $this->Form->setValueSources('query');
  8145. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8146. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8147. $this->assertHtml($expected, $result);
  8148. $this->Form->setValueSources('data');
  8149. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8150. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8151. $this->assertHtml($expected, $result);
  8152. }
  8153. /**
  8154. * Test sources values schema defaults handling
  8155. *
  8156. * @return void
  8157. */
  8158. public function testSourcesValueDoesntExistPassThrough()
  8159. {
  8160. $this->Form->request->query['category'] = 'sesame-cookies';
  8161. $articles = TableRegistry::get('Articles');
  8162. $entity = $articles->newEntity();
  8163. $this->Form->create($entity);
  8164. $this->Form->setValueSources(['query', 'context']);
  8165. $result = $this->Form->getSourceValue('category');
  8166. $this->assertEquals('sesame-cookies', $result);
  8167. $this->Form->setValueSources(['context', 'query']);
  8168. $result = $this->Form->getSourceValue('category');
  8169. $this->assertEquals('sesame-cookies', $result);
  8170. }
  8171. /**
  8172. * testNestedLabelInput method
  8173. *
  8174. * Test the `nestedInput` parameter
  8175. *
  8176. * @return void
  8177. */
  8178. public function testNestedLabelInput()
  8179. {
  8180. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8181. $expected = [
  8182. 'div' => ['class' => 'input text'],
  8183. 'label' => ['for' => 'foo'],
  8184. ['input' => [
  8185. 'type' => 'text',
  8186. 'name' => 'foo',
  8187. 'id' => 'foo'
  8188. ]],
  8189. 'Foo',
  8190. '/label',
  8191. '/div'
  8192. ];
  8193. $this->assertHtml($expected, $result);
  8194. }
  8195. /**
  8196. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8197. *
  8198. * This test makes sure `false` excludes the label from the render
  8199. *
  8200. * @return void
  8201. */
  8202. public function testInputLabelManipulationDisableLabels()
  8203. {
  8204. $result = $this->Form->input('test', [
  8205. 'type' => 'radio',
  8206. 'options' => ['A', 'B'],
  8207. 'labelOptions' => false
  8208. ]);
  8209. $expected = [
  8210. ['div' => ['class' => 'input radio']],
  8211. '<label',
  8212. 'Test',
  8213. '/label',
  8214. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8215. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8216. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8217. '/div',
  8218. ];
  8219. $this->assertHtml($expected, $result);
  8220. $result = $this->Form->input('checkbox1', [
  8221. 'label' => 'My checkboxes',
  8222. 'multiple' => 'checkbox',
  8223. 'type' => 'select',
  8224. 'options' => [
  8225. ['text' => 'First Checkbox', 'value' => 1],
  8226. ['text' => 'Second Checkbox', 'value' => 2]
  8227. ],
  8228. 'labelOptions' => false
  8229. ]);
  8230. $expected = [
  8231. ['div' => ['class' => 'input select']],
  8232. ['label' => ['for' => 'checkbox1']],
  8233. 'My checkboxes',
  8234. '/label',
  8235. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8236. ['div' => ['class' => 'checkbox']],
  8237. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8238. '/div',
  8239. ['div' => ['class' => 'checkbox']],
  8240. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8241. '/div',
  8242. '/div'
  8243. ];
  8244. $this->assertHtml($expected, $result);
  8245. }
  8246. /**
  8247. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8248. *
  8249. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8250. * added to the class if checked.
  8251. *
  8252. * Also checks to make sure any custom attributes are rendered correctly
  8253. *
  8254. * @return void
  8255. */
  8256. public function testInputLabelManipulationRadios()
  8257. {
  8258. $result = $this->Form->input('test', [
  8259. 'type' => 'radio',
  8260. 'options' => ['A', 'B'],
  8261. 'labelOptions' => ['class' => 'custom-class']
  8262. ]);
  8263. $expected = [
  8264. ['div' => ['class' => 'input radio']],
  8265. '<label',
  8266. 'Test',
  8267. '/label',
  8268. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8269. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8270. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8271. 'A',
  8272. '/label',
  8273. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8274. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8275. 'B',
  8276. '/label',
  8277. '/div',
  8278. ];
  8279. $this->assertHtml($expected, $result);
  8280. $result = $this->Form->input('test', [
  8281. 'type' => 'radio',
  8282. 'options' => ['A', 'B'],
  8283. 'value' => 1,
  8284. 'labelOptions' => ['class' => 'custom-class']
  8285. ]);
  8286. $expected = [
  8287. ['div' => ['class' => 'input radio']],
  8288. '<label',
  8289. 'Test',
  8290. '/label',
  8291. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8292. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8293. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8294. 'A',
  8295. '/label',
  8296. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8297. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8298. 'B',
  8299. '/label',
  8300. '/div',
  8301. ];
  8302. $this->assertHtml($expected, $result);
  8303. $result = $this->Form->input('test', [
  8304. 'type' => 'radio',
  8305. 'options' => ['A', 'B'],
  8306. 'value' => 1,
  8307. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8308. ]);
  8309. $expected = [
  8310. ['div' => ['class' => 'input radio']],
  8311. '<label',
  8312. 'Test',
  8313. '/label',
  8314. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8315. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8316. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8317. 'A',
  8318. '/label',
  8319. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8320. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8321. 'B',
  8322. '/label',
  8323. '/div',
  8324. ];
  8325. $this->assertHtml($expected, $result);
  8326. $result = $this->Form->radio('test', ['A', 'B'], [
  8327. 'label' => [
  8328. 'class' => ['custom-class', 'another-class'],
  8329. 'data-name' => 'bob'
  8330. ],
  8331. 'value' => 1
  8332. ]);
  8333. $expected = [
  8334. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8335. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8336. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8337. 'A',
  8338. '/label',
  8339. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8340. ['input' => [
  8341. 'type' => 'radio',
  8342. 'name' => 'test',
  8343. 'value' => '1',
  8344. 'id' => 'test-1',
  8345. 'checked' => 'checked'
  8346. ]],
  8347. 'B',
  8348. '/label',
  8349. ];
  8350. $this->assertHtml($expected, $result);
  8351. }
  8352. /**
  8353. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8354. *
  8355. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8356. * added to the class if checked.
  8357. *
  8358. * Also checks to make sure any custom attributes are rendered correctly
  8359. *
  8360. * @return void
  8361. */
  8362. public function testInputLabelManipulationCheckboxes()
  8363. {
  8364. $result = $this->Form->input('checkbox1', [
  8365. 'label' => 'My checkboxes',
  8366. 'multiple' => 'checkbox',
  8367. 'type' => 'select',
  8368. 'options' => [
  8369. ['text' => 'First Checkbox', 'value' => 1],
  8370. ['text' => 'Second Checkbox', 'value' => 2]
  8371. ],
  8372. 'labelOptions' => ['class' => 'custom-class'],
  8373. 'value' => ['1']
  8374. ]);
  8375. $expected = [
  8376. ['div' => ['class' => 'input select']],
  8377. ['label' => ['for' => 'checkbox1']],
  8378. 'My checkboxes',
  8379. '/label',
  8380. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8381. ['div' => ['class' => 'checkbox']],
  8382. ['label' => [
  8383. 'class' => 'custom-class selected',
  8384. 'for' => 'checkbox1-1'
  8385. ]],
  8386. ['input' => [
  8387. 'type' => 'checkbox',
  8388. 'name' => 'checkbox1[]',
  8389. 'value' => '1',
  8390. 'id' => 'checkbox1-1',
  8391. 'checked' => 'checked'
  8392. ]],
  8393. 'First Checkbox',
  8394. '/label',
  8395. '/div',
  8396. ['div' => ['class' => 'checkbox']],
  8397. ['label' => [
  8398. 'class' => 'custom-class',
  8399. 'for' => 'checkbox1-2'
  8400. ]],
  8401. ['input' => [
  8402. 'type' => 'checkbox',
  8403. 'name' => 'checkbox1[]',
  8404. 'value' => '2',
  8405. 'id' => 'checkbox1-2'
  8406. ]],
  8407. 'Second Checkbox',
  8408. '/label',
  8409. '/div',
  8410. '/div'
  8411. ];
  8412. $this->assertHtml($expected, $result);
  8413. $result = $this->Form->input('checkbox1', [
  8414. 'label' => 'My checkboxes',
  8415. 'multiple' => 'checkbox',
  8416. 'type' => 'select',
  8417. 'options' => [
  8418. ['text' => 'First Checkbox', 'value' => 1],
  8419. ['text' => 'Second Checkbox', 'value' => 2]
  8420. ],
  8421. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8422. 'value' => ['1']
  8423. ]);
  8424. $expected = [
  8425. ['div' => ['class' => 'input select']],
  8426. ['label' => ['for' => 'checkbox1']],
  8427. 'My checkboxes',
  8428. '/label',
  8429. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8430. ['div' => ['class' => 'checkbox']],
  8431. ['label' => [
  8432. 'class' => 'custom-class another-class selected',
  8433. 'data-name' => 'bob',
  8434. 'for' => 'checkbox1-1'
  8435. ]],
  8436. ['input' => [
  8437. 'type' => 'checkbox',
  8438. 'name' => 'checkbox1[]',
  8439. 'value' => '1',
  8440. 'id' => 'checkbox1-1',
  8441. 'checked' => 'checked'
  8442. ]],
  8443. 'First Checkbox',
  8444. '/label',
  8445. '/div',
  8446. ['div' => ['class' => 'checkbox']],
  8447. ['label' => [
  8448. 'class' => 'custom-class another-class',
  8449. 'data-name' => 'bob',
  8450. 'for' => 'checkbox1-2'
  8451. ]],
  8452. ['input' => [
  8453. 'type' => 'checkbox',
  8454. 'name' => 'checkbox1[]',
  8455. 'value' => '2',
  8456. 'id' => 'checkbox1-2'
  8457. ]],
  8458. 'Second Checkbox',
  8459. '/label',
  8460. '/div',
  8461. '/div'
  8462. ];
  8463. $this->assertHtml($expected, $result);
  8464. }
  8465. }