FormHelperTest.php 301 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168
  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. * @property \Cake\View\View $View
  104. */
  105. class FormHelperTest extends TestCase
  106. {
  107. /**
  108. * Fixtures to be used
  109. *
  110. * @var array
  111. */
  112. public $fixtures = ['core.articles', 'core.comments'];
  113. /**
  114. * Do not load the fixtures by default
  115. *
  116. * @var bool
  117. */
  118. public $autoFixtures = false;
  119. /**
  120. * @var array
  121. */
  122. protected $article = [];
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp()
  129. {
  130. parent::setUp();
  131. Configure::write('Config.language', 'eng');
  132. Configure::write('App.base', '');
  133. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  134. $this->View = new View();
  135. $this->Form = new FormHelper($this->View);
  136. $request = new ServerRequest([
  137. 'webroot' => '',
  138. 'base' => '',
  139. 'url' => '/articles/add',
  140. 'params' => [
  141. 'controller' => 'articles',
  142. 'action' => 'add',
  143. ]
  144. ]);
  145. $this->Form->Url->request = $this->Form->request = $request;
  146. $this->dateRegex = [
  147. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  148. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  149. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  152. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  153. ];
  154. $this->article = [
  155. 'schema' => [
  156. 'id' => ['type' => 'integer'],
  157. 'author_id' => ['type' => 'integer', 'null' => true],
  158. 'title' => ['type' => 'string', 'null' => true],
  159. 'body' => 'text',
  160. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  161. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  162. ],
  163. 'required' => [
  164. 'author_id' => true,
  165. 'title' => true,
  166. ]
  167. ];
  168. Security::salt('foo!');
  169. Router::connect('/:controller', ['action' => 'index']);
  170. Router::connect('/:controller/:action/*');
  171. }
  172. /**
  173. * tearDown method
  174. *
  175. * @return void
  176. */
  177. public function tearDown()
  178. {
  179. parent::tearDown();
  180. unset($this->Form, $this->Controller, $this->View);
  181. TableRegistry::clear();
  182. }
  183. /**
  184. * Test construct() with the templates option.
  185. *
  186. * @return void
  187. */
  188. public function testConstructTemplatesFile()
  189. {
  190. $helper = new FormHelper($this->View, [
  191. 'templates' => 'htmlhelper_tags'
  192. ]);
  193. $result = $helper->control('name');
  194. $this->assertContains('<input', $result);
  195. }
  196. /**
  197. * Test that when specifying custom widgets the config array for that widget
  198. * is overwritten instead of merged.
  199. *
  200. * @return void
  201. */
  202. public function testConstructWithWidgets()
  203. {
  204. $config = [
  205. 'widgets' => [
  206. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  207. ]
  208. ];
  209. $helper = new FormHelper($this->View, $config);
  210. $registry = $helper->widgetRegistry();
  211. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  212. }
  213. /**
  214. * Test that when specifying custom widgets config file and it should be
  215. * added to widgets array. WidgetRegistry will load widgets in constructor.
  216. *
  217. * @return void
  218. */
  219. public function testConstructWithWidgetsConfig()
  220. {
  221. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  222. $registry = $helper->widgetRegistry();
  223. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  224. }
  225. /**
  226. * Test registering a new widget class and rendering it.
  227. *
  228. * @return void
  229. */
  230. public function testAddWidgetAndRenderWidget()
  231. {
  232. $data = [
  233. 'val' => 1
  234. ];
  235. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  236. $this->Form->addWidget('test', $mock);
  237. $mock->expects($this->once())
  238. ->method('render')
  239. ->with($data)
  240. ->will($this->returnValue('HTML'));
  241. $result = $this->Form->widget('test', $data);
  242. $this->assertEquals('HTML', $result);
  243. }
  244. /**
  245. * Test that secureFields() of widget is called after calling render(),
  246. * not before.
  247. *
  248. * @return void
  249. */
  250. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  251. {
  252. $data = [
  253. 'val' => 1,
  254. 'name' => 'test'
  255. ];
  256. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  257. $this->Form->addWidget('test', $mock);
  258. $mock->expects($this->at(0))
  259. ->method('render')
  260. ->with($data)
  261. ->will($this->returnValue('HTML'));
  262. $mock->expects($this->at(1))
  263. ->method('secureFields')
  264. ->with($data)
  265. ->will($this->returnValue(['test']));
  266. $result = $this->Form->widget('test', $data + ['secure' => true]);
  267. $this->assertEquals('HTML', $result);
  268. }
  269. /**
  270. * Test that empty string is not added to secure fields list when
  271. * rendering input widget without name.
  272. *
  273. * @return void
  274. */
  275. public function testRenderingWidgetWithEmptyName()
  276. {
  277. $this->assertEquals([], $this->Form->fields);
  278. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  279. $this->assertEquals('<select name=""></select>', $result);
  280. $this->assertEquals([], $this->Form->fields);
  281. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  282. $this->assertEquals('<select name="0"></select>', $result);
  283. $this->assertEquals(['0'], $this->Form->fields);
  284. }
  285. /**
  286. * Test registering an invalid widget class.
  287. *
  288. * @expectedException \RuntimeException
  289. * @return void
  290. */
  291. public function testAddWidgetInvalid()
  292. {
  293. $mock = new \StdClass();
  294. $this->Form->addWidget('test', $mock);
  295. $this->Form->widget('test');
  296. }
  297. /**
  298. * Test adding a new context class.
  299. *
  300. * @return void
  301. */
  302. public function testAddContextProvider()
  303. {
  304. $context = 'My data';
  305. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  306. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  307. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  308. $this->assertEquals($context, $data['entity']);
  309. return $stub;
  310. });
  311. $this->Form->create($context);
  312. $result = $this->Form->context();
  313. $this->assertSame($stub, $result);
  314. }
  315. /**
  316. * Test replacing a context class.
  317. *
  318. * @return void
  319. */
  320. public function testAddContextProviderReplace()
  321. {
  322. $entity = new Article();
  323. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  324. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  325. return $stub;
  326. });
  327. $this->Form->create($entity);
  328. $result = $this->Form->context();
  329. $this->assertSame($stub, $result);
  330. }
  331. /**
  332. * Test overriding a context class.
  333. *
  334. * @return void
  335. */
  336. public function testAddContextProviderAdd()
  337. {
  338. $entity = new Article();
  339. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  340. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  341. if ($data['entity'] instanceof Entity) {
  342. return $stub;
  343. }
  344. });
  345. $this->Form->create($entity);
  346. $result = $this->Form->context();
  347. $this->assertSame($stub, $result);
  348. }
  349. /**
  350. * Test adding an invalid context class.
  351. *
  352. * @expectedException \RuntimeException
  353. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  354. * @return void
  355. */
  356. public function testAddContextProviderInvalid()
  357. {
  358. $context = 'My data';
  359. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  360. return new \StdClass();
  361. });
  362. $this->Form->create($context);
  363. }
  364. /**
  365. * Provides context options for create().
  366. *
  367. * @return array
  368. */
  369. public function contextSelectionProvider()
  370. {
  371. $entity = new Article();
  372. $collection = new Collection([$entity]);
  373. $emptyCollection = new Collection([]);
  374. $arrayObject = new \ArrayObject([]);
  375. $data = [
  376. 'schema' => [
  377. 'title' => ['type' => 'string']
  378. ]
  379. ];
  380. $form = new Form();
  381. return [
  382. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  383. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  384. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  385. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  386. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  387. 'form' => [$form, 'Cake\View\Form\FormContext'],
  388. 'none' => [null, 'Cake\View\Form\NullContext'],
  389. 'false' => [false, 'Cake\View\Form\NullContext'],
  390. ];
  391. }
  392. /**
  393. * Test default context selection in create()
  394. *
  395. * @dataProvider contextSelectionProvider
  396. * @return void
  397. */
  398. public function testCreateContextSelectionBuiltIn($data, $class)
  399. {
  400. $this->loadFixtures('Articles');
  401. $this->Form->create($data);
  402. $this->assertInstanceOf($class, $this->Form->context());
  403. }
  404. /**
  405. * Data provider for type option.
  406. *
  407. * @return array
  408. */
  409. public static function requestTypeProvider()
  410. {
  411. return [
  412. // type, method, override
  413. ['post', 'post', 'POST'],
  414. ['put', 'post', 'PUT'],
  415. ['patch', 'post', 'PATCH'],
  416. ['delete', 'post', 'DELETE'],
  417. ];
  418. }
  419. /**
  420. * Test creating file forms.
  421. *
  422. * @return void
  423. */
  424. public function testCreateFile()
  425. {
  426. $encoding = strtolower(Configure::read('App.encoding'));
  427. $result = $this->Form->create(false, ['type' => 'file']);
  428. $expected = [
  429. 'form' => [
  430. 'method' => 'post', 'action' => '/articles/add',
  431. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  432. ],
  433. 'div' => ['style' => 'display:none;'],
  434. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  435. '/div'
  436. ];
  437. $this->assertHtml($expected, $result);
  438. }
  439. /**
  440. * Test creating GET forms.
  441. *
  442. * @return void
  443. */
  444. public function testCreateGet()
  445. {
  446. $encoding = strtolower(Configure::read('App.encoding'));
  447. $result = $this->Form->create(false, ['type' => 'get']);
  448. $expected = ['form' => [
  449. 'method' => 'get', 'action' => '/articles/add',
  450. 'accept-charset' => $encoding
  451. ]];
  452. $this->assertHtml($expected, $result);
  453. }
  454. /**
  455. * Test explicit method/enctype options.
  456. *
  457. * Explicit method overwrites inferred method from 'type'
  458. *
  459. * @return void
  460. */
  461. public function testCreateExplicitMethodEnctype()
  462. {
  463. $encoding = strtolower(Configure::read('App.encoding'));
  464. $result = $this->Form->create(false, [
  465. 'type' => 'get',
  466. 'method' => 'put',
  467. 'enctype' => 'multipart/form-data'
  468. ]);
  469. $expected = ['form' => [
  470. 'method' => 'put',
  471. 'action' => '/articles/add',
  472. 'enctype' => 'multipart/form-data',
  473. 'accept-charset' => $encoding
  474. ]];
  475. $this->assertHtml($expected, $result);
  476. }
  477. /**
  478. * Test create() with the templates option.
  479. *
  480. * @return void
  481. */
  482. public function testCreateTemplatesArray()
  483. {
  484. $result = $this->Form->create($this->article, [
  485. 'templates' => [
  486. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  487. ]
  488. ]);
  489. $expected = [
  490. 'form' => [
  491. 'class' => 'form-horizontal',
  492. 'method' => 'post',
  493. 'action' => '/articles/add',
  494. 'accept-charset' => 'utf-8'
  495. ]
  496. ];
  497. $this->assertHtml($expected, $result);
  498. }
  499. /**
  500. * Test create() with the templates option.
  501. *
  502. * @return void
  503. */
  504. public function testCreateTemplatesFile()
  505. {
  506. $result = $this->Form->create($this->article, [
  507. 'templates' => 'htmlhelper_tags',
  508. ]);
  509. $expected = [
  510. 'start form',
  511. 'div' => ['class' => 'hidden'],
  512. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  513. '/div'
  514. ];
  515. $this->assertHtml($expected, $result);
  516. }
  517. /**
  518. * Test that create() and end() restore templates.
  519. *
  520. * @return void
  521. */
  522. public function testCreateEndRestoreTemplates()
  523. {
  524. $this->Form->create($this->article, [
  525. 'templates' => ['input' => 'custom input element']
  526. ]);
  527. $this->Form->end();
  528. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  529. }
  530. /**
  531. * Test using template vars in various templates used by control() method.
  532. *
  533. * @return void
  534. */
  535. public function testControlTemplateVars()
  536. {
  537. $result = $this->Form->control('text', [
  538. 'templates' => [
  539. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  540. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  541. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  542. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  543. ],
  544. 'templateVars' => [
  545. 'forinput' => 'in-input',
  546. 'forlabel' => 'in-label',
  547. 'forgroup' => 'in-group',
  548. 'forcontainer' => 'in-container'
  549. ]
  550. ]);
  551. $expected = [
  552. 'div' => ['class'],
  553. 'label' => ['for'],
  554. 'Text in-label',
  555. '/label',
  556. 'in-group',
  557. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  558. 'in-container',
  559. '/div',
  560. ];
  561. $this->assertHtml($expected, $result);
  562. }
  563. /**
  564. * Test ensuring template variables work in template files loaded
  565. * during control().
  566. *
  567. * @return void
  568. */
  569. public function testControlTemplatesFromFile()
  570. {
  571. $result = $this->Form->control('title', [
  572. 'templates' => 'test_templates',
  573. 'templateVars' => [
  574. 'forcontainer' => 'container-data'
  575. ]
  576. ]);
  577. $expected = [
  578. 'div' => ['class'],
  579. 'label' => ['for'],
  580. 'Title',
  581. '/label',
  582. 'input' => ['name', 'type' => 'text', 'id'],
  583. 'container-data',
  584. '/div',
  585. ];
  586. $this->assertHtml($expected, $result);
  587. }
  588. /**
  589. * Test using template vars in inputSubmit and submitContainer template.
  590. *
  591. * @return void
  592. */
  593. public function testSubmitTemplateVars()
  594. {
  595. $this->Form->templates([
  596. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  597. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  598. ]);
  599. $result = $this->Form->submit('Submit', [
  600. 'templateVars' => [
  601. 'forinput' => 'in-input',
  602. 'forcontainer' => 'in-container'
  603. ]
  604. ]);
  605. $expected = [
  606. 'div' => ['class'],
  607. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  608. 'in-container',
  609. '/div',
  610. ];
  611. $this->assertHtml($expected, $result);
  612. }
  613. /**
  614. * test the create() method
  615. *
  616. * @dataProvider requestTypeProvider
  617. * @return void
  618. */
  619. public function testCreateTypeOptions($type, $method, $override)
  620. {
  621. $encoding = strtolower(Configure::read('App.encoding'));
  622. $result = $this->Form->create(false, ['type' => $type]);
  623. $expected = [
  624. 'form' => [
  625. 'method' => $method, 'action' => '/articles/add',
  626. 'accept-charset' => $encoding
  627. ],
  628. 'div' => ['style' => 'display:none;'],
  629. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  630. '/div'
  631. ];
  632. $this->assertHtml($expected, $result);
  633. }
  634. /**
  635. * Test using template vars in Create (formStart template)
  636. *
  637. * @return void
  638. */
  639. public function testCreateTemplateVars()
  640. {
  641. $result = $this->Form->create($this->article, [
  642. 'templates' => [
  643. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  644. ],
  645. 'templateVars' => ['header' => 'headertext']
  646. ]);
  647. $expected = [
  648. 'h4' => ['class'],
  649. 'headertext',
  650. '/h4',
  651. 'form' => [
  652. 'method' => 'post',
  653. 'action' => '/articles/add',
  654. 'accept-charset' => 'utf-8'
  655. ]
  656. ];
  657. $this->assertHtml($expected, $result);
  658. }
  659. /**
  660. * Test opening a form for an update operation.
  661. *
  662. * @return void
  663. */
  664. public function testCreateUpdateForm()
  665. {
  666. $encoding = strtolower(Configure::read('App.encoding'));
  667. $this->Form->request->here = '/articles/edit/1';
  668. $this->Form->request->params['action'] = 'edit';
  669. $this->article['defaults']['id'] = 1;
  670. $result = $this->Form->create($this->article);
  671. $expected = [
  672. 'form' => [
  673. 'method' => 'post',
  674. 'action' => '/articles/edit/1',
  675. 'accept-charset' => $encoding
  676. ],
  677. 'div' => ['style' => 'display:none;'],
  678. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  679. '/div'
  680. ];
  681. $this->assertHtml($expected, $result);
  682. }
  683. /**
  684. * test create() with automatic url generation
  685. *
  686. * @return void
  687. */
  688. public function testCreateAutoUrl()
  689. {
  690. $encoding = strtolower(Configure::read('App.encoding'));
  691. $this->Form->request->params['action'] = 'delete';
  692. $this->Form->request->here = '/articles/delete/10';
  693. $this->Form->request->base = '';
  694. $result = $this->Form->create($this->article);
  695. $expected = [
  696. 'form' => [
  697. 'method' => 'post', 'action' => '/articles/delete/10',
  698. 'accept-charset' => $encoding
  699. ],
  700. 'div' => ['style' => 'display:none;'],
  701. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  702. '/div'
  703. ];
  704. $this->assertHtml($expected, $result);
  705. $this->article['defaults'] = ['id' => 1];
  706. $this->Form->request->here = '/articles/edit/1';
  707. $this->Form->request->params['action'] = 'delete';
  708. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  709. $expected = [
  710. 'form' => [
  711. 'method' => 'post',
  712. 'action' => '/articles/edit/1',
  713. 'accept-charset' => $encoding
  714. ],
  715. 'div' => ['style' => 'display:none;'],
  716. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  717. '/div'
  718. ];
  719. $this->assertHtml($expected, $result);
  720. $this->Form->request->params['action'] = 'add';
  721. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  722. $expected = [
  723. 'form' => [
  724. 'method' => 'post',
  725. 'action' => '/articles/publish/1',
  726. 'accept-charset' => $encoding
  727. ],
  728. 'div' => ['style' => 'display:none;'],
  729. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  730. '/div'
  731. ];
  732. $this->assertHtml($expected, $result);
  733. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  734. $expected = [
  735. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  736. 'div' => ['style' => 'display:none;'],
  737. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  738. '/div'
  739. ];
  740. $this->assertHtml($expected, $result);
  741. $this->Form->request->params['controller'] = 'Pages';
  742. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  743. $expected = [
  744. 'form' => [
  745. 'method' => 'post', 'action' => '/Pages/signup/1',
  746. 'accept-charset' => $encoding
  747. ],
  748. 'div' => ['style' => 'display:none;'],
  749. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  750. '/div'
  751. ];
  752. $this->assertHtml($expected, $result);
  753. }
  754. /**
  755. * Test create() with no URL (no "action" attribute for <form> tag)
  756. *
  757. * @return void
  758. */
  759. public function testCreateNoUrl()
  760. {
  761. $result = $this->Form->create(false, ['url' => false]);
  762. $expected = [
  763. 'form' => [
  764. 'method' => 'post',
  765. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  766. ],
  767. 'div' => ['style' => 'display:none;'],
  768. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  769. '/div'
  770. ];
  771. $this->assertHtml($expected, $result);
  772. }
  773. /**
  774. * test create() with a custom route
  775. *
  776. * @return void
  777. */
  778. public function testCreateCustomRoute()
  779. {
  780. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  781. $encoding = strtolower(Configure::read('App.encoding'));
  782. $this->Form->request->params['controller'] = 'users';
  783. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  784. $expected = [
  785. 'form' => [
  786. 'method' => 'post', 'action' => '/login',
  787. 'accept-charset' => $encoding
  788. ],
  789. 'div' => ['style' => 'display:none;'],
  790. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  791. '/div'
  792. ];
  793. $this->assertHtml($expected, $result);
  794. Router::connect(
  795. '/new-article',
  796. ['controller' => 'articles', 'action' => 'myaction'],
  797. ['_name' => 'my-route']
  798. );
  799. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  800. $expected = [
  801. 'form' => [
  802. 'method' => 'post', 'action' => '/new-article',
  803. 'accept-charset' => $encoding,
  804. ],
  805. 'div' => ['style' => 'display:none;'],
  806. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  807. '/div'
  808. ];
  809. $this->assertHtml($expected, $result);
  810. }
  811. /**
  812. * test automatic accept-charset overriding
  813. *
  814. * @return void
  815. */
  816. public function testCreateWithAcceptCharset()
  817. {
  818. $result = $this->Form->create(
  819. $this->article,
  820. [
  821. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  822. ]
  823. );
  824. $expected = [
  825. 'form' => [
  826. 'method' => 'post', 'action' => '/articles',
  827. 'accept-charset' => 'iso-8859-1'
  828. ],
  829. 'div' => ['style' => 'display:none;'],
  830. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  831. '/div'
  832. ];
  833. $this->assertHtml($expected, $result);
  834. }
  835. /**
  836. * Test base form URL when url param is passed with multiple parameters (&)
  837. *
  838. */
  839. public function testCreateQueryStringRequest()
  840. {
  841. $encoding = strtolower(Configure::read('App.encoding'));
  842. $result = $this->Form->create($this->article, [
  843. 'type' => 'post',
  844. 'escape' => false,
  845. 'url' => [
  846. 'controller' => 'controller',
  847. 'action' => 'action',
  848. '?' => ['param1' => 'value1', 'param2' => 'value2']
  849. ]
  850. ]);
  851. $expected = [
  852. 'form' => [
  853. 'method' => 'post',
  854. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  855. 'accept-charset' => $encoding
  856. ],
  857. 'div' => ['style' => 'display:none;'],
  858. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  859. '/div'
  860. ];
  861. $this->assertHtml($expected, $result);
  862. $result = $this->Form->create($this->article, [
  863. 'type' => 'post',
  864. 'url' => [
  865. 'controller' => 'controller',
  866. 'action' => 'action',
  867. '?' => ['param1' => 'value1', 'param2' => 'value2']
  868. ]
  869. ]);
  870. $this->assertHtml($expected, $result);
  871. }
  872. /**
  873. * test that create() doesn't cause errors by multiple id's being in the primary key
  874. * as could happen with multiple select or checkboxes.
  875. *
  876. * @return void
  877. */
  878. public function testCreateWithMultipleIdInData()
  879. {
  880. $encoding = strtolower(Configure::read('App.encoding'));
  881. $this->Form->request->data['Article']['id'] = [1, 2];
  882. $result = $this->Form->create($this->article);
  883. $expected = [
  884. 'form' => [
  885. 'method' => 'post',
  886. 'action' => '/articles/add',
  887. 'accept-charset' => $encoding
  888. ],
  889. 'div' => ['style' => 'display:none;'],
  890. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  891. '/div'
  892. ];
  893. $this->assertHtml($expected, $result);
  894. }
  895. /**
  896. * test that create() doesn't add in extra passed params.
  897. *
  898. * @return void
  899. */
  900. public function testCreatePassedArgs()
  901. {
  902. $encoding = strtolower(Configure::read('App.encoding'));
  903. $this->Form->request->data['Article']['id'] = 1;
  904. $result = $this->Form->create($this->article, [
  905. 'type' => 'post',
  906. 'escape' => false,
  907. 'url' => [
  908. 'action' => 'edit',
  909. 'myparam'
  910. ]
  911. ]);
  912. $expected = [
  913. 'form' => [
  914. 'method' => 'post',
  915. 'action' => '/articles/edit/myparam',
  916. 'accept-charset' => $encoding
  917. ],
  918. 'div' => ['style' => 'display:none;'],
  919. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  920. '/div'
  921. ];
  922. $this->assertHtml($expected, $result);
  923. }
  924. /**
  925. * test creating a get form, and get form inputs.
  926. *
  927. * @return void
  928. */
  929. public function testGetFormCreate()
  930. {
  931. $encoding = strtolower(Configure::read('App.encoding'));
  932. $result = $this->Form->create($this->article, ['type' => 'get']);
  933. $expected = ['form' => [
  934. 'method' => 'get', 'action' => '/articles/add',
  935. 'accept-charset' => $encoding
  936. ]];
  937. $this->assertHtml($expected, $result);
  938. $result = $this->Form->text('title');
  939. $expected = ['input' => [
  940. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  941. ]];
  942. $this->assertHtml($expected, $result);
  943. $result = $this->Form->password('password');
  944. $expected = ['input' => [
  945. 'name' => 'password', 'type' => 'password'
  946. ]];
  947. $this->assertHtml($expected, $result);
  948. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  949. $result = $this->Form->text('user_form');
  950. $expected = ['input' => [
  951. 'name' => 'user_form', 'type' => 'text'
  952. ]];
  953. $this->assertHtml($expected, $result);
  954. }
  955. /**
  956. * test get form, and inputs when the model param is false
  957. *
  958. * @return void
  959. */
  960. public function testGetFormWithFalseModel()
  961. {
  962. $encoding = strtolower(Configure::read('App.encoding'));
  963. $this->Form->request->params['controller'] = 'contact_test';
  964. $result = $this->Form->create(false, [
  965. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  966. ]);
  967. $expected = ['form' => [
  968. 'method' => 'get', 'action' => '/contact_test/add',
  969. 'accept-charset' => $encoding
  970. ]];
  971. $this->assertHtml($expected, $result);
  972. $result = $this->Form->text('reason');
  973. $expected = [
  974. 'input' => ['type' => 'text', 'name' => 'reason']
  975. ];
  976. $this->assertHtml($expected, $result);
  977. }
  978. /**
  979. * testFormCreateWithSecurity method
  980. *
  981. * Test form->create() with security key.
  982. *
  983. * @return void
  984. */
  985. public function testCreateWithSecurity()
  986. {
  987. $this->Form->request->params['_csrfToken'] = 'testKey';
  988. $encoding = strtolower(Configure::read('App.encoding'));
  989. $result = $this->Form->create($this->article, [
  990. 'url' => '/articles/publish',
  991. ]);
  992. $expected = [
  993. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  994. 'div' => ['style' => 'display:none;'],
  995. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  996. ['input' => [
  997. 'type' => 'hidden',
  998. 'name' => '_csrfToken',
  999. 'value' => 'testKey',
  1000. 'autocomplete' => 'off',
  1001. ]],
  1002. '/div'
  1003. ];
  1004. $this->assertHtml($expected, $result);
  1005. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1006. $expected['form']['id'] = 'MyForm';
  1007. $this->assertHtml($expected, $result);
  1008. }
  1009. /**
  1010. * testFormCreateGetNoSecurity method
  1011. *
  1012. * Test form->create() with no security key as its a get form
  1013. *
  1014. * @return void
  1015. */
  1016. public function testCreateEndGetNoSecurity()
  1017. {
  1018. $this->Form->request->params['_csrfToken'] = 'testKey';
  1019. $article = new Article();
  1020. $result = $this->Form->create($article, [
  1021. 'type' => 'get',
  1022. 'url' => '/contacts/add'
  1023. ]);
  1024. $this->assertNotContains('testKey', $result);
  1025. $result = $this->Form->end();
  1026. $this->assertNotContains('testKey', $result);
  1027. }
  1028. /**
  1029. * test that create() clears the fields property so it starts fresh
  1030. *
  1031. * @return void
  1032. */
  1033. public function testCreateClearingFields()
  1034. {
  1035. $this->Form->fields = ['model_id'];
  1036. $this->Form->create($this->article);
  1037. $this->assertEquals([], $this->Form->fields);
  1038. }
  1039. /**
  1040. * Tests form hash generation with model-less data
  1041. *
  1042. * @return void
  1043. */
  1044. public function testValidateHashNoModel()
  1045. {
  1046. $this->Form->request->params['_Token'] = 'foo';
  1047. $result = $this->Form->secure(['anything']);
  1048. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1049. }
  1050. /**
  1051. * Tests that hidden fields generated for checkboxes don't get locked
  1052. *
  1053. * @return void
  1054. */
  1055. public function testNoCheckboxLocking()
  1056. {
  1057. $this->Form->request->params['_Token'] = 'foo';
  1058. $this->assertSame([], $this->Form->fields);
  1059. $this->Form->checkbox('check', ['value' => '1']);
  1060. $this->assertSame($this->Form->fields, ['check']);
  1061. }
  1062. /**
  1063. * testFormSecurityFields method
  1064. *
  1065. * Test generation of secure form hash generation.
  1066. *
  1067. * @return void
  1068. */
  1069. public function testFormSecurityFields()
  1070. {
  1071. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1072. $this->Form->request->params['_Token'] = 'testKey';
  1073. $result = $this->Form->secure($fields);
  1074. $hash = Security::hash(serialize($fields) . Security::salt());
  1075. $hash .= ':' . 'Model.valid';
  1076. $hash = urlencode($hash);
  1077. $tokenDebug = urlencode(json_encode([
  1078. '',
  1079. $fields,
  1080. []
  1081. ]));
  1082. $expected = [
  1083. 'div' => ['style' => 'display:none;'],
  1084. ['input' => [
  1085. 'type' => 'hidden',
  1086. 'name' => '_Token[fields]',
  1087. 'value' => $hash,
  1088. 'autocomplete' => 'off',
  1089. ]],
  1090. ['input' => [
  1091. 'type' => 'hidden',
  1092. 'name' => '_Token[unlocked]',
  1093. 'value' => '',
  1094. 'autocomplete' => 'off',
  1095. ]],
  1096. ['input' => [
  1097. 'type' => 'hidden',
  1098. 'name' => '_Token[debug]',
  1099. 'value' => $tokenDebug,
  1100. 'autocomplete' => 'off',
  1101. ]],
  1102. '/div'
  1103. ];
  1104. $this->assertHtml($expected, $result);
  1105. }
  1106. /**
  1107. * testFormSecurityFields method
  1108. *
  1109. * Test debug token is not generated if debug is false
  1110. *
  1111. * @return void
  1112. */
  1113. public function testFormSecurityFieldsNoDebugMode()
  1114. {
  1115. Configure::write('debug', false);
  1116. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1117. $this->Form->request->params['_Token'] = 'testKey';
  1118. $result = $this->Form->secure($fields);
  1119. $hash = Security::hash(serialize($fields) . Security::salt());
  1120. $hash .= ':' . 'Model.valid';
  1121. $hash = urlencode($hash);
  1122. $expected = [
  1123. 'div' => ['style' => 'display:none;'],
  1124. ['input' => [
  1125. 'type' => 'hidden',
  1126. 'name' => '_Token[fields]',
  1127. 'autocomplete' => 'off',
  1128. 'value' => $hash
  1129. ]],
  1130. ['input' => [
  1131. 'type' => 'hidden',
  1132. 'name' => '_Token[unlocked]',
  1133. 'autocomplete' => 'off',
  1134. 'value' => '',
  1135. ]],
  1136. '/div'
  1137. ];
  1138. $this->assertHtml($expected, $result);
  1139. }
  1140. /**
  1141. * Tests correct generation of number fields for double and float fields
  1142. *
  1143. * @return void
  1144. */
  1145. public function testTextFieldGenerationForFloats()
  1146. {
  1147. $this->article['schema'] = [
  1148. 'foo' => [
  1149. 'type' => 'float',
  1150. 'null' => false,
  1151. 'default' => null,
  1152. 'length' => 10
  1153. ]
  1154. ];
  1155. $this->Form->create($this->article);
  1156. $result = $this->Form->control('foo');
  1157. $expected = [
  1158. 'div' => ['class' => 'input number'],
  1159. 'label' => ['for' => 'foo'],
  1160. 'Foo',
  1161. '/label',
  1162. ['input' => [
  1163. 'type' => 'number',
  1164. 'name' => 'foo',
  1165. 'id' => 'foo',
  1166. 'step' => 'any'
  1167. ]],
  1168. '/div'
  1169. ];
  1170. $this->assertHtml($expected, $result);
  1171. $result = $this->Form->control('foo', ['step' => 0.5]);
  1172. $expected = [
  1173. 'div' => ['class' => 'input number'],
  1174. 'label' => ['for' => 'foo'],
  1175. 'Foo',
  1176. '/label',
  1177. ['input' => [
  1178. 'type' => 'number',
  1179. 'name' => 'foo',
  1180. 'id' => 'foo',
  1181. 'step' => '0.5'
  1182. ]],
  1183. '/div'
  1184. ];
  1185. $this->assertHtml($expected, $result);
  1186. }
  1187. /**
  1188. * Tests correct generation of number fields for integer fields
  1189. *
  1190. * @return void
  1191. */
  1192. public function testTextFieldTypeNumberGenerationForIntegers()
  1193. {
  1194. TableRegistry::get('Contacts', [
  1195. 'className' => __NAMESPACE__ . '\ContactsTable'
  1196. ]);
  1197. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1198. $result = $this->Form->control('age');
  1199. $expected = [
  1200. 'div' => ['class' => 'input number'],
  1201. 'label' => ['for' => 'age'],
  1202. 'Age',
  1203. '/label',
  1204. ['input' => [
  1205. 'type' => 'number', 'name' => 'age',
  1206. 'id' => 'age'
  1207. ]],
  1208. '/div'
  1209. ];
  1210. $this->assertHtml($expected, $result);
  1211. }
  1212. /**
  1213. * Tests correct generation of file upload fields for binary fields
  1214. *
  1215. * @return void
  1216. */
  1217. public function testFileUploadFieldTypeGenerationForBinaries()
  1218. {
  1219. $table = TableRegistry::get('Contacts', [
  1220. 'className' => __NAMESPACE__ . '\ContactsTable'
  1221. ]);
  1222. $table->schema(['foo' => [
  1223. 'type' => 'binary',
  1224. 'null' => false,
  1225. 'default' => null,
  1226. 'length' => 1024
  1227. ]]);
  1228. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1229. $result = $this->Form->control('foo');
  1230. $expected = [
  1231. 'div' => ['class' => 'input file'],
  1232. 'label' => ['for' => 'foo'],
  1233. 'Foo',
  1234. '/label',
  1235. ['input' => [
  1236. 'type' => 'file', 'name' => 'foo',
  1237. 'id' => 'foo'
  1238. ]],
  1239. '/div'
  1240. ];
  1241. $this->assertHtml($expected, $result);
  1242. }
  1243. /**
  1244. * testFormSecurityMultipleFields method
  1245. *
  1246. * Test secure() with multiple row form. Ensure hash is correct.
  1247. *
  1248. * @return void
  1249. */
  1250. public function testFormSecurityMultipleFields()
  1251. {
  1252. $this->Form->request->params['_Token'] = 'foo';
  1253. $fields = [
  1254. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1255. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1256. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1257. ];
  1258. $result = $this->Form->secure($fields);
  1259. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1260. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1261. $tokenDebug = urlencode(json_encode([
  1262. '',
  1263. $fields,
  1264. []
  1265. ]));
  1266. $expected = [
  1267. 'div' => ['style' => 'display:none;'],
  1268. ['input' => [
  1269. 'type' => 'hidden',
  1270. 'name' => '_Token[fields]',
  1271. 'value' => $hash,
  1272. 'autocomplete' => 'off',
  1273. ]],
  1274. ['input' => [
  1275. 'type' => 'hidden',
  1276. 'name' => '_Token[unlocked]',
  1277. 'autocomplete' => 'off',
  1278. 'value' => ''
  1279. ]],
  1280. ['input' => [
  1281. 'type' => 'hidden',
  1282. 'name' => '_Token[debug]',
  1283. 'value' => $tokenDebug,
  1284. 'autocomplete' => 'off',
  1285. ]],
  1286. '/div'
  1287. ];
  1288. $this->assertHtml($expected, $result);
  1289. }
  1290. /**
  1291. * testFormSecurityMultipleSubmitButtons
  1292. *
  1293. * test form submit generation and ensure that _Token is only created on end()
  1294. *
  1295. * @return void
  1296. */
  1297. public function testFormSecurityMultipleSubmitButtons()
  1298. {
  1299. $this->Form->request->params['_Token'] = 'testKey';
  1300. $this->Form->create($this->article);
  1301. $this->Form->text('Address.title');
  1302. $this->Form->text('Address.first_name');
  1303. $result = $this->Form->submit('Save', ['name' => 'save']);
  1304. $expected = [
  1305. 'div' => ['class' => 'submit'],
  1306. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1307. '/div',
  1308. ];
  1309. $this->assertHtml($expected, $result);
  1310. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1311. $expected = [
  1312. 'div' => ['class' => 'submit'],
  1313. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1314. '/div',
  1315. ];
  1316. $this->assertHtml($expected, $result);
  1317. $result = $this->Form->end();
  1318. $tokenDebug = urlencode(json_encode([
  1319. '/articles/add',
  1320. [
  1321. 'Address.title',
  1322. 'Address.first_name',
  1323. ],
  1324. ['save', 'cancel']
  1325. ]));
  1326. $expected = [
  1327. 'div' => ['style' => 'display:none;'],
  1328. ['input' => [
  1329. 'type' => 'hidden',
  1330. 'name' => '_Token[fields]',
  1331. 'autocomplete',
  1332. 'value'
  1333. ]],
  1334. ['input' => [
  1335. 'type' => 'hidden',
  1336. 'name' => '_Token[unlocked]',
  1337. 'value' => 'cancel%7Csave',
  1338. 'autocomplete' => 'off',
  1339. ]],
  1340. ['input' => [
  1341. 'type' => 'hidden',
  1342. 'name' => '_Token[debug]',
  1343. 'value' => $tokenDebug,
  1344. 'autocomplete' => 'off',
  1345. ]],
  1346. '/div'
  1347. ];
  1348. $this->assertHtml($expected, $result);
  1349. }
  1350. /**
  1351. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1352. *
  1353. * @return void
  1354. */
  1355. public function testSecurityButtonNestedNamed()
  1356. {
  1357. $key = 'testKey';
  1358. $this->Form->request->params['_csrfToken'] = $key;
  1359. $this->Form->create('Addresses');
  1360. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1361. $result = $this->Form->unlockField();
  1362. $this->assertEquals(['Address.button'], $result);
  1363. }
  1364. /**
  1365. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1366. *
  1367. * @return void
  1368. */
  1369. public function testSecuritySubmitNestedNamed()
  1370. {
  1371. $this->Form->request->params['_Token'] = 'testKey';
  1372. $this->Form->create($this->article);
  1373. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1374. $result = $this->Form->unlockField();
  1375. $this->assertEquals(['Address.button'], $result);
  1376. }
  1377. /**
  1378. * Test that the correct fields are unlocked for image submits with no names.
  1379. *
  1380. * @return void
  1381. */
  1382. public function testSecuritySubmitImageNoName()
  1383. {
  1384. $this->Form->request->params['_Token'] = 'testKey';
  1385. $this->Form->create(false);
  1386. $result = $this->Form->submit('save.png');
  1387. $expected = [
  1388. 'div' => ['class' => 'submit'],
  1389. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1390. '/div'
  1391. ];
  1392. $this->assertHtml($expected, $result);
  1393. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1394. }
  1395. /**
  1396. * Test that the correct fields are unlocked for image submits with names.
  1397. *
  1398. * @return void
  1399. */
  1400. public function testSecuritySubmitImageName()
  1401. {
  1402. $this->Form->request->params['_Token'] = 'testKey';
  1403. $this->Form->create(null);
  1404. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1405. $expected = [
  1406. 'div' => ['class' => 'submit'],
  1407. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1408. '/div'
  1409. ];
  1410. $this->assertHtml($expected, $result);
  1411. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1412. }
  1413. /**
  1414. * testFormSecurityMultipleControlFields method
  1415. *
  1416. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1417. *
  1418. * @return void
  1419. */
  1420. public function testFormSecurityMultipleControlFields()
  1421. {
  1422. $this->Form->request->params['_Token'] = 'testKey';
  1423. $this->Form->create();
  1424. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1425. $this->Form->control('Addresses.0.title');
  1426. $this->Form->control('Addresses.0.first_name');
  1427. $this->Form->control('Addresses.0.last_name');
  1428. $this->Form->control('Addresses.0.address');
  1429. $this->Form->control('Addresses.0.city');
  1430. $this->Form->control('Addresses.0.phone');
  1431. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1432. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1433. $this->Form->control('Addresses.1.title');
  1434. $this->Form->control('Addresses.1.first_name');
  1435. $this->Form->control('Addresses.1.last_name');
  1436. $this->Form->control('Addresses.1.address');
  1437. $this->Form->control('Addresses.1.city');
  1438. $this->Form->control('Addresses.1.phone');
  1439. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1440. $result = $this->Form->secure($this->Form->fields);
  1441. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1442. $tokenDebug = urlencode(json_encode([
  1443. '/articles/add',
  1444. [
  1445. 'Addresses.0.id' => '123456',
  1446. 'Addresses.0.title',
  1447. 'Addresses.0.first_name',
  1448. 'Addresses.0.last_name',
  1449. 'Addresses.0.address',
  1450. 'Addresses.0.city',
  1451. 'Addresses.0.phone',
  1452. 'Addresses.0.primary',
  1453. 'Addresses.1.id' => '654321',
  1454. 'Addresses.1.title',
  1455. 'Addresses.1.first_name',
  1456. 'Addresses.1.last_name',
  1457. 'Addresses.1.address',
  1458. 'Addresses.1.city',
  1459. 'Addresses.1.phone',
  1460. 'Addresses.1.primary',
  1461. ],
  1462. []
  1463. ]));
  1464. $expected = [
  1465. 'div' => ['style' => 'display:none;'],
  1466. ['input' => [
  1467. 'type' => 'hidden',
  1468. 'name' => '_Token[fields]',
  1469. 'value' => $hash,
  1470. 'autocomplete' => 'off',
  1471. ]],
  1472. ['input' => [
  1473. 'type' => 'hidden',
  1474. 'name' => '_Token[unlocked]',
  1475. 'autocomplete' => 'off',
  1476. 'value' => ''
  1477. ]],
  1478. ['input' => [
  1479. 'type' => 'hidden',
  1480. 'name' => '_Token[debug]',
  1481. 'value' => $tokenDebug,
  1482. 'autocomplete' => 'off',
  1483. ]],
  1484. '/div'
  1485. ];
  1486. $this->assertHtml($expected, $result);
  1487. }
  1488. /**
  1489. * testFormSecurityArrayFields method
  1490. *
  1491. * Test form security with Model.field.0 style inputs.
  1492. *
  1493. * @return void
  1494. */
  1495. public function testFormSecurityArrayFields()
  1496. {
  1497. $this->Form->request->params['_Token'] = 'testKey';
  1498. $this->Form->create();
  1499. $this->Form->text('Address.primary.1');
  1500. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1501. $this->Form->text('Address.secondary.1.0');
  1502. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1503. }
  1504. /**
  1505. * testFormSecurityMultipleControlDisabledFields method
  1506. *
  1507. * Test secure form generation with multiple records and disabled fields.
  1508. *
  1509. * @return void
  1510. */
  1511. public function testFormSecurityMultipleControlDisabledFields()
  1512. {
  1513. $this->Form->request->params['_Token'] = [
  1514. 'unlockedFields' => ['first_name', 'address']
  1515. ];
  1516. $this->Form->create();
  1517. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1518. $this->Form->text('Addresses.0.title');
  1519. $this->Form->text('Addresses.0.first_name');
  1520. $this->Form->text('Addresses.0.last_name');
  1521. $this->Form->text('Addresses.0.address');
  1522. $this->Form->text('Addresses.0.city');
  1523. $this->Form->text('Addresses.0.phone');
  1524. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1525. $this->Form->text('Addresses.1.title');
  1526. $this->Form->text('Addresses.1.first_name');
  1527. $this->Form->text('Addresses.1.last_name');
  1528. $this->Form->text('Addresses.1.address');
  1529. $this->Form->text('Addresses.1.city');
  1530. $this->Form->text('Addresses.1.phone');
  1531. $result = $this->Form->secure($this->Form->fields);
  1532. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1533. $tokenDebug = urlencode(json_encode([
  1534. '/articles/add',
  1535. [
  1536. 'Addresses.0.id' => '123456',
  1537. 'Addresses.0.title',
  1538. 'Addresses.0.last_name',
  1539. 'Addresses.0.city',
  1540. 'Addresses.0.phone',
  1541. 'Addresses.1.id' => '654321',
  1542. 'Addresses.1.title',
  1543. 'Addresses.1.last_name',
  1544. 'Addresses.1.city',
  1545. 'Addresses.1.phone'
  1546. ],
  1547. [
  1548. 'first_name',
  1549. 'address'
  1550. ]
  1551. ]));
  1552. $expected = [
  1553. 'div' => ['style' => 'display:none;'],
  1554. ['input' => [
  1555. 'type' => 'hidden',
  1556. 'name' => '_Token[fields]',
  1557. 'autocomplete' => 'off',
  1558. 'value' => $hash
  1559. ]],
  1560. ['input' => [
  1561. 'type' => 'hidden',
  1562. 'name' => '_Token[unlocked]',
  1563. 'autocomplete' => 'off',
  1564. 'value' => 'address%7Cfirst_name',
  1565. ]],
  1566. ['input' => [
  1567. 'type' => 'hidden',
  1568. 'name' => '_Token[debug]',
  1569. 'autocomplete' => 'off',
  1570. 'value' => $tokenDebug,
  1571. ]],
  1572. '/div'
  1573. ];
  1574. $this->assertHtml($expected, $result);
  1575. }
  1576. /**
  1577. * testFormSecurityControlDisabledFields method
  1578. *
  1579. * Test single record form with disabled fields.
  1580. *
  1581. * @return void
  1582. */
  1583. public function testFormSecurityControlUnlockedFields()
  1584. {
  1585. $this->Form->request->params['_Token'] = [
  1586. 'unlockedFields' => ['first_name', 'address']
  1587. ];
  1588. $this->Form->create();
  1589. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1590. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1591. $this->Form->text('Addresses.title');
  1592. $this->Form->text('Addresses.first_name');
  1593. $this->Form->text('Addresses.last_name');
  1594. $this->Form->text('Addresses.address');
  1595. $this->Form->text('Addresses.city');
  1596. $this->Form->text('Addresses.phone');
  1597. $result = $this->Form->fields;
  1598. $expected = [
  1599. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1600. 'Addresses.city', 'Addresses.phone'
  1601. ];
  1602. $this->assertEquals($expected, $result);
  1603. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1604. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1605. $tokenDebug = urlencode(json_encode([
  1606. '/articles/add',
  1607. [
  1608. 'Addresses.id' => '123456',
  1609. 'Addresses.title',
  1610. 'Addresses.last_name',
  1611. 'Addresses.city',
  1612. 'Addresses.phone'
  1613. ],
  1614. [
  1615. 'first_name',
  1616. 'address'
  1617. ]
  1618. ]));
  1619. $expected = [
  1620. 'div' => ['style' => 'display:none;'],
  1621. ['input' => [
  1622. 'type' => 'hidden',
  1623. 'name' => '_Token[fields]',
  1624. 'value' => $hash,
  1625. 'autocomplete' => 'off',
  1626. 'data-foo' => 'bar',
  1627. ]],
  1628. ['input' => [
  1629. 'type' => 'hidden',
  1630. 'name' => '_Token[unlocked]',
  1631. 'value' => 'address%7Cfirst_name',
  1632. 'autocomplete' => 'off',
  1633. 'data-foo' => 'bar',
  1634. ]],
  1635. ['input' => [
  1636. 'type' => 'hidden', 'name' => '_Token[debug]',
  1637. 'value' => $tokenDebug,
  1638. 'autocomplete' => 'off',
  1639. 'data-foo' => 'bar'
  1640. ]],
  1641. '/div'
  1642. ];
  1643. $this->assertHtml($expected, $result);
  1644. }
  1645. /**
  1646. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1647. *
  1648. * Test single record form with debugSecurity param.
  1649. *
  1650. * @return void
  1651. */
  1652. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1653. {
  1654. $this->Form->request->params['_Token'] = [
  1655. 'unlockedFields' => ['first_name', 'address']
  1656. ];
  1657. $this->Form->create();
  1658. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1659. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1660. $this->Form->text('Addresses.title');
  1661. $this->Form->text('Addresses.first_name');
  1662. $this->Form->text('Addresses.last_name');
  1663. $this->Form->text('Addresses.address');
  1664. $this->Form->text('Addresses.city');
  1665. $this->Form->text('Addresses.phone');
  1666. $result = $this->Form->fields;
  1667. $expected = [
  1668. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1669. 'Addresses.city', 'Addresses.phone'
  1670. ];
  1671. $this->assertEquals($expected, $result);
  1672. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1673. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1674. $tokenDebug = urlencode(json_encode([
  1675. '/articles/add',
  1676. [
  1677. 'Addresses.id' => '123456',
  1678. 'Addresses.title',
  1679. 'Addresses.last_name',
  1680. 'Addresses.city',
  1681. 'Addresses.phone'
  1682. ],
  1683. [
  1684. 'first_name',
  1685. 'address'
  1686. ]
  1687. ]));
  1688. $expected = [
  1689. 'div' => ['style' => 'display:none;'],
  1690. ['input' => [
  1691. 'type' => 'hidden',
  1692. 'name' => '_Token[fields]',
  1693. 'value' => $hash,
  1694. 'autocomplete' => 'off',
  1695. 'data-foo' => 'bar',
  1696. ]],
  1697. ['input' => [
  1698. 'type' => 'hidden',
  1699. 'name' => '_Token[unlocked]',
  1700. 'value' => 'address%7Cfirst_name',
  1701. 'autocomplete' => 'off',
  1702. 'data-foo' => 'bar',
  1703. ]],
  1704. ['input' => [
  1705. 'type' => 'hidden', 'name' => '_Token[debug]',
  1706. 'value' => $tokenDebug,
  1707. 'autocomplete' => 'off',
  1708. 'data-foo' => 'bar'
  1709. ]],
  1710. '/div'
  1711. ];
  1712. $this->assertHtml($expected, $result);
  1713. }
  1714. /**
  1715. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1716. *
  1717. * Debug is false, debugSecurity is true -> no debug
  1718. *
  1719. * @return void
  1720. */
  1721. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1722. {
  1723. $this->Form->request->params['_Token'] = [
  1724. 'unlockedFields' => ['first_name', 'address']
  1725. ];
  1726. $this->Form->create();
  1727. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1728. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1729. $this->Form->text('Addresses.title');
  1730. $this->Form->text('Addresses.first_name');
  1731. $this->Form->text('Addresses.last_name');
  1732. $this->Form->text('Addresses.address');
  1733. $this->Form->text('Addresses.city');
  1734. $this->Form->text('Addresses.phone');
  1735. $result = $this->Form->fields;
  1736. $expected = [
  1737. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1738. 'Addresses.city', 'Addresses.phone'
  1739. ];
  1740. $this->assertEquals($expected, $result);
  1741. Configure::write('debug', false);
  1742. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1743. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1744. $expected = [
  1745. 'div' => ['style' => 'display:none;'],
  1746. ['input' => [
  1747. 'type' => 'hidden',
  1748. 'name' => '_Token[fields]',
  1749. 'value' => $hash,
  1750. 'autocomplete' => 'off',
  1751. 'data-foo' => 'bar',
  1752. ]],
  1753. ['input' => [
  1754. 'type' => 'hidden',
  1755. 'name' => '_Token[unlocked]',
  1756. 'value' => 'address%7Cfirst_name',
  1757. 'autocomplete' => 'off',
  1758. 'data-foo' => 'bar',
  1759. ]],
  1760. '/div'
  1761. ];
  1762. $this->assertHtml($expected, $result);
  1763. }
  1764. /**
  1765. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1766. *
  1767. * Test single record form with debugSecurity param.
  1768. *
  1769. * @return void
  1770. */
  1771. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1772. {
  1773. $this->Form->request->params['_Token'] = [
  1774. 'unlockedFields' => ['first_name', 'address']
  1775. ];
  1776. $this->Form->create();
  1777. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1778. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1779. $this->Form->text('Addresses.title');
  1780. $this->Form->text('Addresses.first_name');
  1781. $this->Form->text('Addresses.last_name');
  1782. $this->Form->text('Addresses.address');
  1783. $this->Form->text('Addresses.city');
  1784. $this->Form->text('Addresses.phone');
  1785. $result = $this->Form->fields;
  1786. $expected = [
  1787. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1788. 'Addresses.city', 'Addresses.phone'
  1789. ];
  1790. $this->assertEquals($expected, $result);
  1791. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1792. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1793. $expected = [
  1794. 'div' => ['style' => 'display:none;'],
  1795. ['input' => [
  1796. 'type' => 'hidden',
  1797. 'name' => '_Token[fields]',
  1798. 'value' => $hash,
  1799. 'autocomplete' => 'off',
  1800. 'data-foo' => 'bar',
  1801. ]],
  1802. ['input' => [
  1803. 'type' => 'hidden',
  1804. 'name' => '_Token[unlocked]',
  1805. 'value' => 'address%7Cfirst_name',
  1806. 'autocomplete' => 'off',
  1807. 'data-foo' => 'bar',
  1808. ]],
  1809. '/div'
  1810. ];
  1811. $this->assertHtml($expected, $result);
  1812. }
  1813. /**
  1814. * testFormSecureWithCustomNameAttribute method
  1815. *
  1816. * Test securing inputs with custom name attributes.
  1817. *
  1818. * @return void
  1819. */
  1820. public function testFormSecureWithCustomNameAttribute()
  1821. {
  1822. $this->Form->request->params['_Token'] = 'testKey';
  1823. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1824. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1825. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1826. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1827. }
  1828. /**
  1829. * testFormSecuredControl method
  1830. *
  1831. * Test generation of entire secure form, assertions made on control() output.
  1832. *
  1833. * @return void
  1834. */
  1835. public function testFormSecuredControl()
  1836. {
  1837. $this->Form->request->params['_csrfToken'] = 'testKey';
  1838. $this->Form->request->params['_Token'] = 'stuff';
  1839. $this->article['schema'] = [
  1840. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1841. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1842. ];
  1843. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1844. $encoding = strtolower(Configure::read('App.encoding'));
  1845. $expected = [
  1846. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1847. 'div' => ['style' => 'display:none;'],
  1848. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1849. ['input' => [
  1850. 'type' => 'hidden',
  1851. 'name' => '_csrfToken',
  1852. 'value' => 'testKey',
  1853. 'autocomplete' => 'off'
  1854. ]],
  1855. '/div'
  1856. ];
  1857. $this->assertHtml($expected, $result);
  1858. $result = $this->Form->control('ratio');
  1859. $expected = [
  1860. 'div' => ['class'],
  1861. 'label' => ['for'],
  1862. 'Ratio',
  1863. '/label',
  1864. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1865. '/div',
  1866. ];
  1867. $this->assertHtml($expected, $result);
  1868. $result = $this->Form->control('population');
  1869. $expected = [
  1870. 'div' => ['class'],
  1871. 'label' => ['for'],
  1872. 'Population',
  1873. '/label',
  1874. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1875. '/div',
  1876. ];
  1877. $this->assertHtml($expected, $result);
  1878. $result = $this->Form->control('published', ['type' => 'text']);
  1879. $expected = [
  1880. 'div' => ['class' => 'input text'],
  1881. 'label' => ['for' => 'published'],
  1882. 'Published',
  1883. '/label',
  1884. ['input' => [
  1885. 'type' => 'text',
  1886. 'name' => 'published',
  1887. 'id' => 'published'
  1888. ]],
  1889. '/div'
  1890. ];
  1891. $this->assertHtml($expected, $result);
  1892. $result = $this->Form->control('other', ['type' => 'text']);
  1893. $expected = [
  1894. 'div' => ['class' => 'input text'],
  1895. 'label' => ['for' => 'other'],
  1896. 'Other',
  1897. '/label',
  1898. ['input' => [
  1899. 'type' => 'text',
  1900. 'name' => 'other',
  1901. 'id',
  1902. ]],
  1903. '/div'
  1904. ];
  1905. $this->assertHtml($expected, $result);
  1906. $result = $this->Form->hidden('stuff');
  1907. $expected = [
  1908. 'input' => [
  1909. 'type' => 'hidden',
  1910. 'name' => 'stuff'
  1911. ]
  1912. ];
  1913. $this->assertHtml($expected, $result);
  1914. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1915. $expected = ['input' => [
  1916. 'type' => 'hidden',
  1917. 'name' => 'hidden',
  1918. 'value' => '0'
  1919. ]];
  1920. $this->assertHtml($expected, $result);
  1921. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1922. $expected = [
  1923. 'div' => ['class' => 'input checkbox'],
  1924. ['input' => [
  1925. 'type' => 'hidden',
  1926. 'name' => 'something',
  1927. 'value' => '0'
  1928. ]],
  1929. 'label' => ['for' => 'something'],
  1930. ['input' => [
  1931. 'type' => 'checkbox',
  1932. 'name' => 'something',
  1933. 'value' => '1',
  1934. 'id' => 'something'
  1935. ]],
  1936. 'Something',
  1937. '/label',
  1938. '/div'
  1939. ];
  1940. $this->assertHtml($expected, $result);
  1941. $result = $this->Form->fields;
  1942. $expectedFields = [
  1943. 'ratio',
  1944. 'population',
  1945. 'published',
  1946. 'other',
  1947. 'stuff' => '',
  1948. 'hidden' => '0',
  1949. 'something'
  1950. ];
  1951. $this->assertEquals($expectedFields, $result);
  1952. $result = $this->Form->secure($this->Form->fields);
  1953. $tokenDebug = urlencode(json_encode([
  1954. '/articles/add',
  1955. $expectedFields,
  1956. []
  1957. ]));
  1958. $expected = [
  1959. 'div' => ['style' => 'display:none;'],
  1960. ['input' => [
  1961. 'type' => 'hidden',
  1962. 'name' => '_Token[fields]',
  1963. 'value',
  1964. 'autocomplete'
  1965. ]],
  1966. ['input' => [
  1967. 'type' => 'hidden',
  1968. 'name' => '_Token[unlocked]',
  1969. 'value' => '',
  1970. 'autocomplete' => 'off'
  1971. ]],
  1972. ['input' => [
  1973. 'type' => 'hidden', 'name' => '_Token[debug]',
  1974. 'value' => $tokenDebug,
  1975. 'autocomplete' => 'off'
  1976. ]],
  1977. '/div'
  1978. ];
  1979. $this->assertHtml($expected, $result);
  1980. }
  1981. /**
  1982. * testSecuredControlCustomName method
  1983. *
  1984. * Test secured inputs with custom names.
  1985. *
  1986. * @return void
  1987. */
  1988. public function testSecuredControlCustomName()
  1989. {
  1990. $this->Form->request->params['_Token'] = 'testKey';
  1991. $this->assertEquals([], $this->Form->fields);
  1992. $this->Form->text('text_input', [
  1993. 'name' => 'Option[General.default_role]',
  1994. ]);
  1995. $expected = ['Option.General.default_role'];
  1996. $this->assertEquals($expected, $this->Form->fields);
  1997. $this->Form->select('select_box', [1, 2], [
  1998. 'name' => 'Option[General.select_role]',
  1999. ]);
  2000. $expected[] = 'Option.General.select_role';
  2001. $this->assertEquals($expected, $this->Form->fields);
  2002. $this->Form->text('other.things[]');
  2003. $expected[] = 'other.things';
  2004. $this->assertEquals($expected, $this->Form->fields);
  2005. }
  2006. /**
  2007. * testSecuredControlDuplicate method
  2008. *
  2009. * Test that a hidden field followed by a visible field
  2010. * undoes the hidden field locking.
  2011. *
  2012. * @return void
  2013. */
  2014. public function testSecuredControlDuplicate()
  2015. {
  2016. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2017. $this->assertEquals([], $this->Form->fields);
  2018. $this->Form->control('text_val', [
  2019. 'type' => 'hidden',
  2020. 'value' => 'some text',
  2021. ]);
  2022. $expected = ['text_val' => 'some text'];
  2023. $this->assertEquals($expected, $this->Form->fields);
  2024. $this->Form->control('text_val', [
  2025. 'type' => 'text',
  2026. ]);
  2027. $expected = ['text_val'];
  2028. $this->assertEquals($expected, $this->Form->fields);
  2029. }
  2030. /**
  2031. * testFormSecuredFileControl method
  2032. *
  2033. * Tests that the correct keys are added to the field hash index.
  2034. *
  2035. * @return void
  2036. */
  2037. public function testFormSecuredFileControl()
  2038. {
  2039. $this->assertEquals([], $this->Form->fields);
  2040. $this->Form->file('Attachment.file');
  2041. $expected = [
  2042. 'Attachment.file.name', 'Attachment.file.type',
  2043. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2044. 'Attachment.file.size'
  2045. ];
  2046. $this->assertEquals($expected, $this->Form->fields);
  2047. }
  2048. /**
  2049. * testFormSecuredMultipleSelect method
  2050. *
  2051. * Test that multiple selects keys are added to field hash.
  2052. *
  2053. * @return void
  2054. */
  2055. public function testFormSecuredMultipleSelect()
  2056. {
  2057. $this->Form->request->params['_csrfToken'] = 'testKey';
  2058. $this->assertEquals([], $this->Form->fields);
  2059. $options = ['1' => 'one', '2' => 'two'];
  2060. $this->Form->select('Model.select', $options);
  2061. $expected = ['Model.select'];
  2062. $this->assertEquals($expected, $this->Form->fields);
  2063. $this->Form->fields = [];
  2064. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2065. $this->assertEquals($expected, $this->Form->fields);
  2066. }
  2067. /**
  2068. * testFormSecuredRadio method
  2069. *
  2070. * @return void
  2071. */
  2072. public function testFormSecuredRadio()
  2073. {
  2074. $this->Form->request->params['_Token'] = 'testKey';
  2075. $this->assertEquals([], $this->Form->fields);
  2076. $options = ['1' => 'option1', '2' => 'option2'];
  2077. $this->Form->radio('Test.test', $options);
  2078. $expected = ['Test.test'];
  2079. $this->assertEquals($expected, $this->Form->fields);
  2080. $this->Form->radio('Test.all', $options, [
  2081. 'disabled' => ['option1', 'option2']
  2082. ]);
  2083. $expected = ['Test.test', 'Test.all' => ''];
  2084. $this->assertEquals($expected, $this->Form->fields);
  2085. $this->Form->radio('Test.some', $options, [
  2086. 'disabled' => ['option1']
  2087. ]);
  2088. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2089. $this->assertEquals($expected, $this->Form->fields);
  2090. }
  2091. /**
  2092. * testFormSecuredAndDisabledNotAssoc method
  2093. *
  2094. * Test that when disabled is in a list based attribute array it works.
  2095. *
  2096. * @return void
  2097. */
  2098. public function testFormSecuredAndDisabledNotAssoc()
  2099. {
  2100. $this->Form->request->params['_Token'] = 'testKey';
  2101. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2102. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2103. $this->Form->text('Model.text', ['disabled']);
  2104. $this->Form->textarea('Model.textarea', ['disabled']);
  2105. $this->Form->password('Model.password', ['disabled']);
  2106. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2107. $expected = [
  2108. 'Model.radio' => ''
  2109. ];
  2110. $this->assertEquals($expected, $this->Form->fields);
  2111. }
  2112. /**
  2113. * testFormSecuredAndDisabled method
  2114. *
  2115. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2116. * hashing.
  2117. *
  2118. * @return void
  2119. */
  2120. public function testFormSecuredAndDisabled()
  2121. {
  2122. $this->Form->request->params['_Token'] = 'testKey';
  2123. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2124. $this->Form->text('Model.text', ['disabled' => true]);
  2125. $this->Form->password('Model.text', ['disabled' => true]);
  2126. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2127. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2128. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2129. $this->Form->year('Model.year', ['disabled' => true]);
  2130. $this->Form->month('Model.month', ['disabled' => true]);
  2131. $this->Form->day('Model.day', ['disabled' => true]);
  2132. $this->Form->hour('Model.hour', ['disabled' => true]);
  2133. $this->Form->minute('Model.minute', ['disabled' => true]);
  2134. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2135. $expected = [
  2136. 'Model.radio' => ''
  2137. ];
  2138. $this->assertEquals($expected, $this->Form->fields);
  2139. }
  2140. /**
  2141. * testDisableSecurityUsingForm method
  2142. *
  2143. * @return void
  2144. */
  2145. public function testDisableSecurityUsingForm()
  2146. {
  2147. $this->Form->request->params['_Token'] = [
  2148. 'disabledFields' => []
  2149. ];
  2150. $this->Form->create();
  2151. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2152. $this->Form->text('Addresses.title');
  2153. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2154. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2155. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2156. $result = $this->Form->fields;
  2157. $expected = [
  2158. 'Addresses.id' => '123456', 'Addresses.title',
  2159. ];
  2160. $this->assertEquals($expected, $result);
  2161. }
  2162. /**
  2163. * testUnlockFieldAddsToList method
  2164. *
  2165. * Test disableField.
  2166. *
  2167. * @return void
  2168. */
  2169. public function testUnlockFieldAddsToList()
  2170. {
  2171. $this->Form->request->params['_Token'] = [
  2172. 'unlockedFields' => []
  2173. ];
  2174. $this->Form->unlockField('Contact.name');
  2175. $this->Form->text('Contact.name');
  2176. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2177. $this->assertEquals([], $this->Form->fields);
  2178. }
  2179. /**
  2180. * testUnlockFieldRemovingFromFields method
  2181. *
  2182. * Test unlockField removing from fields array.
  2183. *
  2184. * @return void
  2185. */
  2186. public function testUnlockFieldRemovingFromFields()
  2187. {
  2188. $this->Form->request->params['_Token'] = [
  2189. 'unlockedFields' => []
  2190. ];
  2191. $this->Form->create($this->article);
  2192. $this->Form->hidden('Article.id', ['value' => 1]);
  2193. $this->Form->text('Article.title');
  2194. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2195. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2196. $this->Form->unlockField('Article.title');
  2197. $this->Form->unlockField('Article.id');
  2198. $this->assertEquals([], $this->Form->fields);
  2199. }
  2200. /**
  2201. * testResetUnlockFields method
  2202. *
  2203. * Test reset unlockFields, when create new form.
  2204. *
  2205. * @return void
  2206. */
  2207. public function testResetUnlockFields()
  2208. {
  2209. $this->Form->request->params['_Token'] = [
  2210. 'key' => 'testKey',
  2211. 'unlockedFields' => []
  2212. ];
  2213. $this->Form->unlockField('Contact.id');
  2214. $this->Form->create('Contact');
  2215. $this->Form->hidden('Contact.id', ['value' => 1]);
  2216. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2217. $this->Form->end();
  2218. $this->Form->create('Contact');
  2219. $this->Form->hidden('Contact.id', ['value' => 1]);
  2220. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2221. }
  2222. /**
  2223. * testSecuredFormUrlIgnoresHost method
  2224. *
  2225. * Test that only the path + query elements of a form's URL show up in their hash.
  2226. *
  2227. * @return void
  2228. */
  2229. public function testSecuredFormUrlIgnoresHost()
  2230. {
  2231. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2232. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2233. $this->Form->create($this->article, [
  2234. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2235. ]);
  2236. $result = $this->Form->secure();
  2237. $this->assertContains($expected, $result);
  2238. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2239. $result = $this->Form->secure();
  2240. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2241. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2242. $result = $this->Form->secure();
  2243. $this->assertContains($expected, $result, 'URL path + query should work.');
  2244. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2245. $result = $this->Form->secure();
  2246. $this->assertNotContains($expected, $result, 'URL is different');
  2247. }
  2248. /**
  2249. * testSecuredFormUrlHasHtmlAndIdentifier method
  2250. *
  2251. * Test that URL, HTML and identifier show up in their hashes.
  2252. *
  2253. * @return void
  2254. */
  2255. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2256. {
  2257. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2258. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2259. $this->Form->create($this->article, [
  2260. 'url' => [
  2261. 'controller' => 'articles',
  2262. 'action' => 'view',
  2263. '?' => [
  2264. 'page' => 1,
  2265. 'limit' => 10,
  2266. 'html' => '<>"',
  2267. ],
  2268. '#' => 'result',
  2269. ],
  2270. ]);
  2271. $result = $this->Form->secure();
  2272. $this->assertContains($expected, $result);
  2273. $this->Form->create($this->article, [
  2274. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2275. ]);
  2276. $result = $this->Form->secure();
  2277. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2278. $this->Form->create($this->article, [
  2279. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2280. ]);
  2281. $result = $this->Form->secure();
  2282. $this->assertContains($expected, $result, 'URL path + query should work.');
  2283. }
  2284. /**
  2285. * testErrorMessageDisplay method
  2286. *
  2287. * Test error message display.
  2288. *
  2289. * @return void
  2290. */
  2291. public function testErrorMessageDisplay()
  2292. {
  2293. $this->article['errors'] = [
  2294. 'Article' => [
  2295. 'title' => 'error message',
  2296. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2297. ]
  2298. ];
  2299. $this->Form->create($this->article);
  2300. $result = $this->Form->control('Article.title');
  2301. $expected = [
  2302. 'div' => ['class' => 'input text error'],
  2303. 'label' => ['for' => 'article-title'],
  2304. 'Title',
  2305. '/label',
  2306. 'input' => [
  2307. 'type' => 'text', 'name' => 'Article[title]',
  2308. 'id' => 'article-title', 'class' => 'form-error'
  2309. ],
  2310. ['div' => ['class' => 'error-message']],
  2311. 'error message',
  2312. '/div',
  2313. '/div'
  2314. ];
  2315. $this->assertHtml($expected, $result);
  2316. $result = $this->Form->control('Article.title', [
  2317. 'templates' => [
  2318. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2319. ]
  2320. ]);
  2321. $expected = [
  2322. 'div' => ['class' => 'input text error'],
  2323. 'label' => ['for' => 'article-title'],
  2324. 'Title',
  2325. '/label',
  2326. 'input' => [
  2327. 'type' => 'text', 'name' => 'Article[title]',
  2328. 'id' => 'article-title', 'class' => 'form-error'
  2329. ],
  2330. '/div'
  2331. ];
  2332. $this->assertHtml($expected, $result);
  2333. $result = $this->Form->control('Article.content');
  2334. $expected = [
  2335. 'div' => ['class' => 'input text error'],
  2336. 'label' => ['for' => 'article-content'],
  2337. 'Content',
  2338. '/label',
  2339. 'input' => [
  2340. 'type' => 'text', 'name' => 'Article[content]',
  2341. 'id' => 'article-content', 'class' => 'form-error'
  2342. ],
  2343. ['div' => ['class' => 'error-message']],
  2344. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2345. '/div',
  2346. '/div'
  2347. ];
  2348. $this->assertHtml($expected, $result);
  2349. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2350. $expected = [
  2351. 'div' => ['class' => 'input text error'],
  2352. 'label' => ['for' => 'article-content'],
  2353. 'Content',
  2354. '/label',
  2355. 'input' => [
  2356. 'type' => 'text', 'name' => 'Article[content]',
  2357. 'id' => 'article-content', 'class' => 'form-error'
  2358. ],
  2359. ['div' => ['class' => 'error-message']],
  2360. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2361. '/div',
  2362. '/div'
  2363. ];
  2364. $this->assertHtml($expected, $result);
  2365. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2366. $expected = [
  2367. 'div' => ['class' => 'input text error'],
  2368. 'label' => ['for' => 'article-content'],
  2369. 'Content',
  2370. '/label',
  2371. 'input' => [
  2372. 'type' => 'text', 'name' => 'Article[content]',
  2373. 'id' => 'article-content', 'class' => 'form-error'
  2374. ],
  2375. ['div' => ['class' => 'error-message']],
  2376. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2377. '/div',
  2378. '/div'
  2379. ];
  2380. $this->assertHtml($expected, $result);
  2381. }
  2382. /**
  2383. * testEmptyErrorValidation method
  2384. *
  2385. * Test validation errors, when validation message is an empty string.
  2386. *
  2387. * @return void
  2388. */
  2389. public function testEmptyErrorValidation()
  2390. {
  2391. $this->article['errors'] = [
  2392. 'Article' => ['title' => '']
  2393. ];
  2394. $this->Form->create($this->article);
  2395. $result = $this->Form->control('Article.title');
  2396. $expected = [
  2397. 'div' => ['class' => 'input text error'],
  2398. 'label' => ['for' => 'article-title'],
  2399. 'Title',
  2400. '/label',
  2401. 'input' => [
  2402. 'type' => 'text', 'name' => 'Article[title]',
  2403. 'id' => 'article-title', 'class' => 'form-error'
  2404. ],
  2405. ['div' => ['class' => 'error-message']],
  2406. [],
  2407. '/div',
  2408. '/div'
  2409. ];
  2410. $this->assertHtml($expected, $result);
  2411. }
  2412. /**
  2413. * testEmptyControlErrorValidation method
  2414. *
  2415. * Test validation errors, when calling control() overriding validation message by an empty string.
  2416. *
  2417. * @return void
  2418. */
  2419. public function testEmptyControlErrorValidation()
  2420. {
  2421. $this->article['errors'] = [
  2422. 'Article' => ['title' => 'error message']
  2423. ];
  2424. $this->Form->create($this->article);
  2425. $result = $this->Form->control('Article.title', ['error' => '']);
  2426. $expected = [
  2427. 'div' => ['class' => 'input text error'],
  2428. 'label' => ['for' => 'article-title'],
  2429. 'Title',
  2430. '/label',
  2431. 'input' => [
  2432. 'type' => 'text', 'name' => 'Article[title]',
  2433. 'id' => 'article-title', 'class' => 'form-error'
  2434. ],
  2435. ['div' => ['class' => 'error-message']],
  2436. [],
  2437. '/div',
  2438. '/div'
  2439. ];
  2440. $this->assertHtml($expected, $result);
  2441. }
  2442. /**
  2443. * testControlErrorMessage method
  2444. *
  2445. * Test validation errors, when calling control() overriding validation messages.
  2446. *
  2447. * @return void
  2448. */
  2449. public function testControlErrorMessage()
  2450. {
  2451. $this->article['errors'] = [
  2452. 'title' => ['error message']
  2453. ];
  2454. $this->Form->create($this->article);
  2455. $result = $this->Form->control('title', [
  2456. 'error' => 'Custom error!'
  2457. ]);
  2458. $expected = [
  2459. 'div' => ['class' => 'input text required error'],
  2460. 'label' => ['for' => 'title'],
  2461. 'Title',
  2462. '/label',
  2463. 'input' => [
  2464. 'type' => 'text', 'name' => 'title',
  2465. 'id' => 'title', 'class' => 'form-error',
  2466. 'required' => 'required',
  2467. ],
  2468. ['div' => ['class' => 'error-message']],
  2469. 'Custom error!',
  2470. '/div',
  2471. '/div'
  2472. ];
  2473. $this->assertHtml($expected, $result);
  2474. $result = $this->Form->control('title', [
  2475. 'error' => ['error message' => 'Custom error!']
  2476. ]);
  2477. $expected = [
  2478. 'div' => ['class' => 'input text required error'],
  2479. 'label' => ['for' => 'title'],
  2480. 'Title',
  2481. '/label',
  2482. 'input' => [
  2483. 'type' => 'text',
  2484. 'name' => 'title',
  2485. 'id' => 'title',
  2486. 'class' => 'form-error',
  2487. 'required' => 'required'
  2488. ],
  2489. ['div' => ['class' => 'error-message']],
  2490. 'Custom error!',
  2491. '/div',
  2492. '/div'
  2493. ];
  2494. $this->assertHtml($expected, $result);
  2495. }
  2496. /**
  2497. * testFormValidationAssociated method
  2498. *
  2499. * Tests displaying errors for nested entities.
  2500. *
  2501. * @return void
  2502. */
  2503. public function testFormValidationAssociated()
  2504. {
  2505. $nested = new Entity(['foo' => 'bar']);
  2506. $nested->errors('foo', ['not a valid bar']);
  2507. $entity = new Entity(['nested' => $nested]);
  2508. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2509. $result = $this->Form->error('nested.foo');
  2510. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2511. }
  2512. /**
  2513. * testFormValidationAssociatedSecondLevel method
  2514. *
  2515. * Test form error display with associated model.
  2516. *
  2517. * @return void
  2518. */
  2519. public function testFormValidationAssociatedSecondLevel()
  2520. {
  2521. $inner = new Entity(['bar' => 'baz']);
  2522. $nested = new Entity(['foo' => $inner]);
  2523. $entity = new Entity(['nested' => $nested]);
  2524. $inner->errors('bar', ['not a valid one']);
  2525. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2526. $result = $this->Form->error('nested.foo.bar');
  2527. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2528. }
  2529. /**
  2530. * testFormValidationMultiRecord method
  2531. *
  2532. * Test form error display with multiple records.
  2533. *
  2534. * @return void
  2535. */
  2536. public function testFormValidationMultiRecord()
  2537. {
  2538. $one = new Entity();
  2539. $two = new Entity();
  2540. TableRegistry::get('Contacts', [
  2541. 'className' => __NAMESPACE__ . '\ContactsTable'
  2542. ]);
  2543. $one->set('email', '');
  2544. $one->errors('email', ['invalid email']);
  2545. $two->set('name', '');
  2546. $two->errors('name', ['This is wrong']);
  2547. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2548. $result = $this->Form->control('0.email');
  2549. $expected = [
  2550. 'div' => ['class' => 'input email error'],
  2551. 'label' => ['for' => '0-email'],
  2552. 'Email',
  2553. '/label',
  2554. 'input' => [
  2555. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2556. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2557. ],
  2558. ['div' => ['class' => 'error-message']],
  2559. 'invalid email',
  2560. '/div',
  2561. '/div'
  2562. ];
  2563. $this->assertHtml($expected, $result);
  2564. $result = $this->Form->control('1.name');
  2565. $expected = [
  2566. 'div' => ['class' => 'input text error'],
  2567. 'label' => ['for' => '1-name'],
  2568. 'Name',
  2569. '/label',
  2570. 'input' => [
  2571. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2572. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2573. ],
  2574. ['div' => ['class' => 'error-message']],
  2575. 'This is wrong',
  2576. '/div',
  2577. '/div'
  2578. ];
  2579. $this->assertHtml($expected, $result);
  2580. }
  2581. /**
  2582. * testControl method
  2583. *
  2584. * Test various incarnations of control().
  2585. *
  2586. * @return void
  2587. */
  2588. public function testControl()
  2589. {
  2590. TableRegistry::get('ValidateUsers', [
  2591. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2592. ]);
  2593. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2594. $result = $this->Form->control('ValidateUsers.balance');
  2595. $expected = [
  2596. 'div' => ['class'],
  2597. 'label' => ['for'],
  2598. 'Balance',
  2599. '/label',
  2600. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2601. '/div',
  2602. ];
  2603. $this->assertHtml($expected, $result);
  2604. $result = $this->Form->control('ValidateUser.cost_decimal');
  2605. $expected = [
  2606. 'div' => ['class'],
  2607. 'label' => ['for'],
  2608. 'Cost Decimal',
  2609. '/label',
  2610. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2611. '/div',
  2612. ];
  2613. $this->assertHtml($expected, $result);
  2614. $result = $this->Form->control('ValidateUser.null_decimal');
  2615. $expected = [
  2616. 'div' => ['class'],
  2617. 'label' => ['for'],
  2618. 'Null Decimal',
  2619. '/label',
  2620. 'input' => ['name', 'type' => 'number', 'id'],
  2621. '/div',
  2622. ];
  2623. $this->assertHtml($expected, $result);
  2624. }
  2625. /**
  2626. * testControlCustomization method
  2627. *
  2628. * Tests the input method and passing custom options.
  2629. *
  2630. * @return void
  2631. */
  2632. public function testControlCustomization()
  2633. {
  2634. TableRegistry::get('Contacts', [
  2635. 'className' => __NAMESPACE__ . '\ContactsTable'
  2636. ]);
  2637. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2638. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2639. $expected = [
  2640. 'div' => ['class' => 'input email'],
  2641. 'label' => ['for' => 'custom'],
  2642. 'Email',
  2643. '/label',
  2644. ['input' => [
  2645. 'type' => 'email', 'name' => 'Contact[email]',
  2646. 'id' => 'custom', 'maxlength' => 255
  2647. ]],
  2648. '/div'
  2649. ];
  2650. $this->assertHtml($expected, $result);
  2651. $result = $this->Form->control('Contact.email', [
  2652. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2653. ]);
  2654. $expected = [
  2655. '<div',
  2656. 'label' => ['for' => 'contact-email'],
  2657. 'Email',
  2658. '/label',
  2659. ['input' => [
  2660. 'type' => 'email', 'name' => 'Contact[email]',
  2661. 'id' => 'contact-email', 'maxlength' => 255
  2662. ]],
  2663. '/div'
  2664. ];
  2665. $this->assertHtml($expected, $result);
  2666. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2667. $expected = [
  2668. 'div' => ['class' => 'input text'],
  2669. 'label' => ['for' => 'contact-email'],
  2670. 'Email',
  2671. '/label',
  2672. ['input' => [
  2673. 'type' => 'text', 'name' => 'Contact[email]',
  2674. 'id' => 'contact-email', 'maxlength' => '255'
  2675. ]],
  2676. '/div'
  2677. ];
  2678. $this->assertHtml($expected, $result);
  2679. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2680. $expected = [
  2681. 'div' => ['class' => 'input text'],
  2682. 'label' => ['for' => 'contact-5-email'],
  2683. 'Email',
  2684. '/label',
  2685. ['input' => [
  2686. 'type' => 'text', 'name' => 'Contact[5][email]',
  2687. 'id' => 'contact-5-email', 'maxlength' => '255'
  2688. ]],
  2689. '/div'
  2690. ];
  2691. $this->assertHtml($expected, $result);
  2692. $result = $this->Form->control('Contact.password');
  2693. $expected = [
  2694. 'div' => ['class' => 'input password'],
  2695. 'label' => ['for' => 'contact-password'],
  2696. 'Password',
  2697. '/label',
  2698. ['input' => [
  2699. 'type' => 'password', 'name' => 'Contact[password]',
  2700. 'id' => 'contact-password'
  2701. ]],
  2702. '/div'
  2703. ];
  2704. $this->assertHtml($expected, $result);
  2705. $result = $this->Form->control('Contact.email', [
  2706. 'type' => 'file', 'class' => 'textbox'
  2707. ]);
  2708. $expected = [
  2709. 'div' => ['class' => 'input file'],
  2710. 'label' => ['for' => 'contact-email'],
  2711. 'Email',
  2712. '/label',
  2713. ['input' => [
  2714. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2715. 'id' => 'contact-email'
  2716. ]],
  2717. '/div'
  2718. ];
  2719. $this->assertHtml($expected, $result);
  2720. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2721. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2722. $result = $this->Form->control('phone');
  2723. $expected = [
  2724. 'div' => ['class' => 'input tel'],
  2725. 'label' => ['for' => 'phone'],
  2726. 'Phone',
  2727. '/label',
  2728. ['input' => [
  2729. 'type' => 'tel', 'name' => 'phone',
  2730. 'value' => 'Hello &amp; World &gt; weird chars',
  2731. 'id' => 'phone', 'maxlength' => 255
  2732. ]],
  2733. '/div'
  2734. ];
  2735. $this->assertHtml($expected, $result);
  2736. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2737. $this->Form->create();
  2738. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2739. $expected = [
  2740. 'div' => ['class' => 'input text'],
  2741. 'label' => ['for' => 'myId'],
  2742. 'Field',
  2743. '/label',
  2744. 'input' => [
  2745. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2746. 'value' => 'My value', 'id' => 'myId'
  2747. ],
  2748. '/div'
  2749. ];
  2750. $this->assertHtml($expected, $result);
  2751. $this->Form->request->data = [];
  2752. $entity->errors('field', 'Badness!');
  2753. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2754. $result = $this->Form->control('field');
  2755. $expected = [
  2756. 'div' => ['class' => 'input text error'],
  2757. 'label' => ['for' => 'field'],
  2758. 'Field',
  2759. '/label',
  2760. 'input' => [
  2761. 'type' => 'text', 'name' => 'field',
  2762. 'id' => 'field', 'class' => 'form-error'
  2763. ],
  2764. ['div' => ['class' => 'error-message']],
  2765. 'Badness!',
  2766. '/div',
  2767. '/div'
  2768. ];
  2769. $this->assertHtml($expected, $result);
  2770. $result = $this->Form->control('field', [
  2771. 'templates' => [
  2772. 'inputContainerError' => '{{content}}{{error}}',
  2773. 'error' => '<span class="error-message">{{content}}</span>'
  2774. ]
  2775. ]);
  2776. $expected = [
  2777. 'label' => ['for' => 'field'],
  2778. 'Field',
  2779. '/label',
  2780. 'input' => [
  2781. 'type' => 'text', 'name' => 'field',
  2782. 'id' => 'field', 'class' => 'form-error'
  2783. ],
  2784. ['span' => ['class' => 'error-message']],
  2785. 'Badness!',
  2786. '/span'
  2787. ];
  2788. $this->assertHtml($expected, $result);
  2789. $entity->errors('field', ['minLength'], true);
  2790. $result = $this->Form->control('field', [
  2791. 'error' => [
  2792. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2793. 'maxLength' => 'login too large'
  2794. ]
  2795. ]);
  2796. $expected = [
  2797. 'div' => ['class' => 'input text error'],
  2798. 'label' => ['for' => 'field'],
  2799. 'Field',
  2800. '/label',
  2801. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2802. ['div' => ['class' => 'error-message']],
  2803. 'Le login doit contenir au moins 2 caractères',
  2804. '/div',
  2805. '/div'
  2806. ];
  2807. $this->assertHtml($expected, $result);
  2808. $entity->errors('field', ['maxLength'], true);
  2809. $result = $this->Form->control('field', [
  2810. 'error' => [
  2811. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2812. 'maxLength' => 'login too large',
  2813. ]
  2814. ]);
  2815. $expected = [
  2816. 'div' => ['class' => 'input text error'],
  2817. 'label' => ['for' => 'field'],
  2818. 'Field',
  2819. '/label',
  2820. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2821. ['div' => ['class' => 'error-message']],
  2822. 'login too large',
  2823. '/div',
  2824. '/div'
  2825. ];
  2826. $this->assertHtml($expected, $result);
  2827. }
  2828. /**
  2829. * testControlWithTemplateFile method
  2830. *
  2831. * Test that control() accepts a template file.
  2832. *
  2833. * @return void
  2834. */
  2835. public function testControlWithTemplateFile()
  2836. {
  2837. $result = $this->Form->control('field', [
  2838. 'templates' => 'htmlhelper_tags'
  2839. ]);
  2840. $expected = [
  2841. 'label' => ['for' => 'field'],
  2842. 'Field',
  2843. '/label',
  2844. 'input' => [
  2845. 'type' => 'text', 'name' => 'field',
  2846. 'id' => 'field'
  2847. ],
  2848. ];
  2849. $this->assertHtml($expected, $result);
  2850. }
  2851. /**
  2852. * testNestedControlsEndWithBrackets method
  2853. *
  2854. * Test that nested inputs end with brackets.
  2855. *
  2856. * @return void
  2857. */
  2858. public function testNestedControlsEndWithBrackets()
  2859. {
  2860. $result = $this->Form->text('nested.text[]');
  2861. $expected = [
  2862. 'input' => [
  2863. 'type' => 'text', 'name' => 'nested[text][]'
  2864. ],
  2865. ];
  2866. $this->assertHtml($expected, $result);
  2867. $result = $this->Form->file('nested.file[]');
  2868. $expected = [
  2869. 'input' => [
  2870. 'type' => 'file', 'name' => 'nested[file][]'
  2871. ],
  2872. ];
  2873. $this->assertHtml($expected, $result);
  2874. }
  2875. /**
  2876. * testCreateIdPrefix method
  2877. *
  2878. * Test id prefix.
  2879. *
  2880. * @return void
  2881. */
  2882. public function testCreateIdPrefix()
  2883. {
  2884. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2885. $result = $this->Form->control('field');
  2886. $expected = [
  2887. 'div' => ['class' => 'input text'],
  2888. 'label' => ['for' => 'prefix-field'],
  2889. 'Field',
  2890. '/label',
  2891. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2892. '/div'
  2893. ];
  2894. $this->assertHtml($expected, $result);
  2895. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2896. $expected = [
  2897. 'div' => ['class' => 'input text'],
  2898. 'label' => ['for' => 'custom-id'],
  2899. 'Field',
  2900. '/label',
  2901. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2902. '/div'
  2903. ];
  2904. $this->assertHtml($expected, $result);
  2905. $result = $this->Form->radio('Model.field', ['option A']);
  2906. $expected = [
  2907. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2908. 'label' => ['for' => 'prefix-model-field-0'],
  2909. ['input' => [
  2910. 'type' => 'radio',
  2911. 'name' => 'Model[field]',
  2912. 'value' => '0',
  2913. 'id' => 'prefix-model-field-0'
  2914. ]],
  2915. 'option A',
  2916. '/label'
  2917. ];
  2918. $this->assertHtml($expected, $result);
  2919. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2920. $expected = [
  2921. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2922. 'label' => ['for' => 'prefix-model-field-0'],
  2923. ['input' => [
  2924. 'type' => 'radio',
  2925. 'name' => 'Model[field]',
  2926. 'value' => '0',
  2927. 'id' => 'prefix-model-field-0'
  2928. ]],
  2929. 'option A',
  2930. '/label'
  2931. ];
  2932. $this->assertHtml($expected, $result);
  2933. $result = $this->Form->select(
  2934. 'Model.multi_field',
  2935. ['first'],
  2936. ['multiple' => 'checkbox']
  2937. );
  2938. $expected = [
  2939. 'input' => [
  2940. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2941. ],
  2942. ['div' => ['class' => 'checkbox']],
  2943. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2944. ['input' => [
  2945. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2946. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2947. ]],
  2948. 'first',
  2949. '/label',
  2950. '/div',
  2951. ];
  2952. $this->assertHtml($expected, $result);
  2953. $this->Form->end();
  2954. $result = $this->Form->control('field');
  2955. $expected = [
  2956. 'div' => ['class' => 'input text'],
  2957. 'label' => ['for' => 'field'],
  2958. 'Field',
  2959. '/label',
  2960. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2961. '/div'
  2962. ];
  2963. $this->assertHtml($expected, $result);
  2964. }
  2965. /**
  2966. * testControlZero method
  2967. *
  2968. * Test that inputs with 0 can be created.
  2969. *
  2970. * @return void
  2971. */
  2972. public function testControlZero()
  2973. {
  2974. TableRegistry::get('Contacts', [
  2975. 'className' => __NAMESPACE__ . '\ContactsTable'
  2976. ]);
  2977. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2978. $result = $this->Form->control('0');
  2979. $expected = [
  2980. 'div' => ['class' => 'input text'],
  2981. 'label' => ['for' => '0'], '/label',
  2982. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2983. '/div'
  2984. ];
  2985. $this->assertHtml($expected, $result);
  2986. }
  2987. /**
  2988. * testControlCheckbox method
  2989. *
  2990. * Test control() with checkbox creation.
  2991. *
  2992. * @return void
  2993. */
  2994. public function testControlCheckbox()
  2995. {
  2996. $articles = TableRegistry::get('Articles');
  2997. $articles->schema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  2998. $article = $articles->newEntity();
  2999. $this->Form->create($article);
  3000. $result = $this->Form->control('Articles.active');
  3001. $expected = [
  3002. 'div' => ['class' => 'input checkbox'],
  3003. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3004. 'label' => ['for' => 'articles-active'],
  3005. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3006. 'Active',
  3007. '/label',
  3008. '/div'
  3009. ];
  3010. $this->assertHtml($expected, $result);
  3011. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3012. $expected = [
  3013. 'div' => ['class' => 'input checkbox'],
  3014. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3015. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3016. '/div'
  3017. ];
  3018. $this->assertHtml($expected, $result);
  3019. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3020. $expected = [
  3021. 'div' => ['class' => 'input checkbox'],
  3022. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3023. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3024. '/div'
  3025. ];
  3026. $this->assertHtml($expected, $result);
  3027. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3028. $expected = [
  3029. 'div' => ['class' => 'input checkbox'],
  3030. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3031. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3032. '/div'
  3033. ];
  3034. $this->assertHtml($expected, $result);
  3035. $result = $this->Form->control('Articles.disabled', [
  3036. 'label' => 'Disabled',
  3037. 'type' => 'checkbox',
  3038. 'data-foo' => 'disabled'
  3039. ]);
  3040. $expected = [
  3041. 'div' => ['class' => 'input checkbox'],
  3042. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3043. 'label' => ['for' => 'articles-disabled'],
  3044. ['input' => [
  3045. 'type' => 'checkbox',
  3046. 'name' => 'Articles[disabled]',
  3047. 'value' => '1',
  3048. 'id' => 'articles-disabled',
  3049. 'data-foo' => 'disabled'
  3050. ]],
  3051. 'Disabled',
  3052. '/label',
  3053. '/div'
  3054. ];
  3055. $this->assertHtml($expected, $result);
  3056. $result = $this->Form->control('Articles.confirm', [
  3057. 'label' => 'Confirm <b>me</b>!',
  3058. 'type' => 'checkbox',
  3059. 'escape' => false
  3060. ]);
  3061. $expected = [
  3062. 'div' => ['class' => 'input checkbox'],
  3063. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3064. 'label' => ['for' => 'articles-confirm'],
  3065. ['input' => [
  3066. 'type' => 'checkbox',
  3067. 'name' => 'Articles[confirm]',
  3068. 'value' => '1',
  3069. 'id' => 'articles-confirm',
  3070. ]],
  3071. 'Confirm <b>me</b>!',
  3072. '/label',
  3073. '/div'
  3074. ];
  3075. $this->assertHtml($expected, $result);
  3076. }
  3077. /**
  3078. * testControlHidden method
  3079. *
  3080. * Test that control() does not create wrapping div and label tag for hidden fields.
  3081. *
  3082. * @return void
  3083. */
  3084. public function testControlHidden()
  3085. {
  3086. TableRegistry::get('ValidateUsers', [
  3087. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3088. ]);
  3089. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3090. $result = $this->Form->control('ValidateUser.id');
  3091. $expected = [
  3092. 'input' => ['name', 'type' => 'hidden', 'id']
  3093. ];
  3094. $this->assertHtml($expected, $result);
  3095. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3096. $expected = [
  3097. 'input' => ['name', 'type' => 'hidden', 'id']
  3098. ];
  3099. $this->assertHtml($expected, $result);
  3100. }
  3101. /**
  3102. * testControlDatetime method
  3103. *
  3104. * Test form->control() with datetime.
  3105. *
  3106. * @return void
  3107. */
  3108. public function testControlDatetime()
  3109. {
  3110. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3111. ->setMethods(['datetime'])
  3112. ->setConstructorArgs([new View()])
  3113. ->getMock();
  3114. $this->Form->expects($this->once())->method('datetime')
  3115. ->with('prueba', [
  3116. 'type' => 'datetime',
  3117. 'timeFormat' => 24,
  3118. 'minYear' => 2008,
  3119. 'maxYear' => 2011,
  3120. 'interval' => 15,
  3121. 'options' => null,
  3122. 'empty' => false,
  3123. 'id' => 'prueba',
  3124. 'required' => false,
  3125. 'templateVars' => []
  3126. ])
  3127. ->will($this->returnValue('This is it!'));
  3128. $result = $this->Form->control('prueba', [
  3129. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3130. 'maxYear' => 2011, 'interval' => 15
  3131. ]);
  3132. $expected = [
  3133. 'div' => ['class' => 'input datetime'],
  3134. '<label',
  3135. 'Prueba',
  3136. '/label',
  3137. 'This is it!',
  3138. '/div'
  3139. ];
  3140. $this->assertHtml($expected, $result);
  3141. }
  3142. /**
  3143. * testControlDatetimeIdPrefix method
  3144. *
  3145. * Test form->control() with datetime with id prefix.
  3146. *
  3147. * @return void
  3148. */
  3149. public function testControlDatetimeIdPrefix()
  3150. {
  3151. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3152. ->setMethods(['datetime'])
  3153. ->setConstructorArgs([new View()])
  3154. ->getMock();
  3155. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3156. $this->Form->expects($this->once())->method('datetime')
  3157. ->with('prueba', [
  3158. 'type' => 'datetime',
  3159. 'timeFormat' => 24,
  3160. 'minYear' => 2008,
  3161. 'maxYear' => 2011,
  3162. 'interval' => 15,
  3163. 'options' => null,
  3164. 'empty' => false,
  3165. 'id' => 'prefix-prueba',
  3166. 'required' => false,
  3167. 'templateVars' => []
  3168. ])
  3169. ->will($this->returnValue('This is it!'));
  3170. $result = $this->Form->control('prueba', [
  3171. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3172. 'maxYear' => 2011, 'interval' => 15
  3173. ]);
  3174. $expected = [
  3175. 'div' => ['class' => 'input datetime'],
  3176. '<label',
  3177. 'Prueba',
  3178. '/label',
  3179. 'This is it!',
  3180. '/div'
  3181. ];
  3182. $this->assertHtml($expected, $result);
  3183. }
  3184. /**
  3185. * testControlCheckboxWithDisabledElements method
  3186. *
  3187. * Test generating checkboxes with disabled elements.
  3188. *
  3189. * @return void
  3190. */
  3191. public function testControlCheckboxWithDisabledElements()
  3192. {
  3193. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3194. $result = $this->Form->control('Contact.multiple', [
  3195. 'multiple' => 'checkbox',
  3196. 'disabled' => 'disabled',
  3197. 'options' => $options
  3198. ]);
  3199. $expected = [
  3200. ['div' => ['class' => 'input select']],
  3201. ['label' => ['for' => "contact-multiple"]],
  3202. 'Multiple',
  3203. '/label',
  3204. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  3205. ['div' => ['class' => 'checkbox']],
  3206. ['label' => ['for' => "contact-multiple-1"]],
  3207. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  3208. 'One',
  3209. '/label',
  3210. '/div',
  3211. ['div' => ['class' => 'checkbox']],
  3212. ['label' => ['for' => "contact-multiple-2"]],
  3213. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  3214. 'Two',
  3215. '/label',
  3216. '/div',
  3217. ['div' => ['class' => 'checkbox']],
  3218. ['label' => ['for' => "contact-multiple-3"]],
  3219. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  3220. 'Three',
  3221. '/label',
  3222. '/div',
  3223. '/div'
  3224. ];
  3225. $this->assertHtml($expected, $result);
  3226. // make sure 50 does only disable 50, and not 50f5c0cf
  3227. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3228. $disabled = [50];
  3229. $expected = [
  3230. ['div' => ['class' => 'input select']],
  3231. ['label' => ['for' => "contact-multiple"]],
  3232. 'Multiple',
  3233. '/label',
  3234. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  3235. ['div' => ['class' => 'checkbox']],
  3236. ['label' => ['for' => "contact-multiple-50"]],
  3237. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  3238. 'Fifty',
  3239. '/label',
  3240. '/div',
  3241. ['div' => ['class' => 'checkbox']],
  3242. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  3243. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  3244. 'Stringy',
  3245. '/label',
  3246. '/div',
  3247. '/div'
  3248. ];
  3249. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3250. $this->assertHtml($expected, $result);
  3251. }
  3252. /**
  3253. * testControlWithLeadingInteger method
  3254. *
  3255. * Test input name with leading integer, ensure attributes are generated correctly.
  3256. *
  3257. * @return void
  3258. */
  3259. public function testControlWithLeadingInteger()
  3260. {
  3261. $result = $this->Form->text('0.Node.title');
  3262. $expected = [
  3263. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3264. ];
  3265. $this->assertHtml($expected, $result);
  3266. }
  3267. /**
  3268. * testControlSelectType method
  3269. *
  3270. * Test form->control() with select type inputs.
  3271. *
  3272. * @return void
  3273. */
  3274. public function testControlSelectType()
  3275. {
  3276. $result = $this->Form->control(
  3277. 'email',
  3278. [
  3279. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3280. );
  3281. $expected = [
  3282. 'div' => ['class' => 'input select'],
  3283. 'label' => ['for' => 'email'],
  3284. 'Email',
  3285. '/label',
  3286. ['select' => ['name' => 'email', 'id' => 'email']],
  3287. ['option' => ['value' => '']],
  3288. '/option',
  3289. ['option' => ['value' => 'è']],
  3290. 'Firést',
  3291. '/option',
  3292. ['option' => ['value' => 'é']],
  3293. 'Secoènd',
  3294. '/option',
  3295. '/select',
  3296. '/div'
  3297. ];
  3298. $this->assertHtml($expected, $result);
  3299. $result = $this->Form->control(
  3300. 'email',
  3301. [
  3302. 'options' => ['First', 'Second'], 'empty' => true]
  3303. );
  3304. $expected = [
  3305. 'div' => ['class' => 'input select'],
  3306. 'label' => ['for' => 'email'],
  3307. 'Email',
  3308. '/label',
  3309. ['select' => ['name' => 'email', 'id' => 'email']],
  3310. ['option' => ['value' => '']],
  3311. '/option',
  3312. ['option' => ['value' => '0']],
  3313. 'First',
  3314. '/option',
  3315. ['option' => ['value' => '1']],
  3316. 'Second',
  3317. '/option',
  3318. '/select',
  3319. '/div'
  3320. ];
  3321. $this->assertHtml($expected, $result);
  3322. $result = $this->Form->control('email', [
  3323. 'type' => 'select',
  3324. 'options' => new \ArrayObject(['First', 'Second']),
  3325. 'empty' => true
  3326. ]);
  3327. $this->assertHtml($expected, $result);
  3328. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3329. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3330. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3331. $expected = [
  3332. 'div' => ['class' => 'input select'],
  3333. 'label' => ['for' => 'model-user-id'],
  3334. 'User',
  3335. '/label',
  3336. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3337. ['option' => ['value' => '']],
  3338. '/option',
  3339. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3340. 'good',
  3341. '/option',
  3342. ['option' => ['value' => 'other']],
  3343. 'bad',
  3344. '/option',
  3345. '/select',
  3346. '/div'
  3347. ];
  3348. $this->assertHtml($expected, $result);
  3349. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3350. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3351. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3352. $expected = [
  3353. 'div' => ['class' => 'input select'],
  3354. 'label' => ['for' => 'thing-user-id'],
  3355. 'User',
  3356. '/label',
  3357. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3358. ['option' => ['value' => '']],
  3359. 'Some Empty',
  3360. '/option',
  3361. ['option' => ['value' => 'value']],
  3362. 'good',
  3363. '/option',
  3364. ['option' => ['value' => 'other']],
  3365. 'bad',
  3366. '/option',
  3367. '/select',
  3368. '/div'
  3369. ];
  3370. $this->assertHtml($expected, $result);
  3371. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3372. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3373. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3374. $expected = [
  3375. 'div' => ['class' => 'input select'],
  3376. 'label' => ['for' => 'thing-user-id'],
  3377. 'User',
  3378. '/label',
  3379. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3380. ['option' => ['value' => '']],
  3381. 'Some Empty',
  3382. '/option',
  3383. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3384. 'good',
  3385. '/option',
  3386. ['option' => ['value' => 'other']],
  3387. 'bad',
  3388. '/option',
  3389. '/select',
  3390. '/div'
  3391. ];
  3392. $this->assertHtml($expected, $result);
  3393. $this->Form->data = [];
  3394. $result = $this->Form->control('Publisher.id', [
  3395. 'label' => 'Publisher',
  3396. 'type' => 'select',
  3397. 'multiple' => 'checkbox',
  3398. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3399. ]);
  3400. $expected = [
  3401. ['div' => ['class' => 'input select']],
  3402. ['label' => ['for' => 'publisher-id']],
  3403. 'Publisher',
  3404. '/label',
  3405. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3406. ['div' => ['class' => 'checkbox']],
  3407. ['label' => ['for' => 'publisher-id-value-1']],
  3408. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3409. 'Label 1',
  3410. '/label',
  3411. '/div',
  3412. ['div' => ['class' => 'checkbox']],
  3413. ['label' => ['for' => 'publisher-id-value-2']],
  3414. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3415. 'Label 2',
  3416. '/label',
  3417. '/div',
  3418. '/div'
  3419. ];
  3420. $this->assertHtml($expected, $result);
  3421. }
  3422. /**
  3423. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3424. *
  3425. * Test that control() and a non standard primary key makes a hidden input by default.
  3426. *
  3427. * @return void
  3428. */
  3429. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3430. {
  3431. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3432. $this->Form->create($this->article);
  3433. $result = $this->Form->control('title');
  3434. $expected = [
  3435. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3436. ];
  3437. $this->assertHtml($expected, $result);
  3438. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3439. $this->Form->create($this->article);
  3440. $result = $this->Form->control('title');
  3441. $expected = [
  3442. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3443. ];
  3444. $this->assertHtml($expected, $result);
  3445. $result = $this->Form->control('body');
  3446. $expected = [
  3447. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3448. ];
  3449. $this->assertHtml($expected, $result);
  3450. }
  3451. /**
  3452. * testControlOverridingMagicSelectType method
  3453. *
  3454. * Test that overriding the magic select type widget is possible.
  3455. *
  3456. * @return void
  3457. */
  3458. public function testControlOverridingMagicSelectType()
  3459. {
  3460. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3461. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3462. $expected = [
  3463. 'div' => ['class' => 'input text'],
  3464. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3465. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3466. '/div'
  3467. ];
  3468. $this->assertHtml($expected, $result);
  3469. //Check that magic types still work for plural/singular vars
  3470. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3471. $result = $this->Form->control('Model.type');
  3472. $expected = [
  3473. 'div' => ['class' => 'input select'],
  3474. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3475. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3476. ['option' => ['value' => 'value']], 'good', '/option',
  3477. ['option' => ['value' => 'other']], 'bad', '/option',
  3478. '/select',
  3479. '/div'
  3480. ];
  3481. $this->assertHtml($expected, $result);
  3482. }
  3483. /**
  3484. * testControlMagicTypeDoesNotOverride method
  3485. *
  3486. * Test that inferred types do not override developer input.
  3487. *
  3488. * @return void
  3489. */
  3490. public function testControlMagicTypeDoesNotOverride()
  3491. {
  3492. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3493. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3494. $expected = [
  3495. 'div' => ['class' => 'input checkbox'],
  3496. ['input' => [
  3497. 'type' => 'hidden',
  3498. 'name' => 'Model[user]',
  3499. 'value' => 0,
  3500. ]],
  3501. 'label' => ['for' => 'model-user'],
  3502. ['input' => [
  3503. 'name' => 'Model[user]',
  3504. 'type' => 'checkbox',
  3505. 'id' => 'model-user',
  3506. 'value' => 1
  3507. ]],
  3508. 'User',
  3509. '/label',
  3510. '/div'
  3511. ];
  3512. $this->assertHtml($expected, $result);
  3513. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3514. $options = [
  3515. 1 => 'blue',
  3516. 2 => 'red'
  3517. ];
  3518. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3519. $expected = [
  3520. 'div' => ['class' => 'input select'],
  3521. 'label' => ['for' => 'tags-ids'],
  3522. 'Tags',
  3523. '/label',
  3524. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3525. ['div' => ['class' => 'checkbox']],
  3526. ['label' => ['for' => 'tags-ids-1']],
  3527. ['input' => [
  3528. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3529. 'value' => '1', 'name' => 'tags[_ids][]'
  3530. ]],
  3531. 'blue',
  3532. '/label',
  3533. '/div',
  3534. ['div' => ['class' => 'checkbox']],
  3535. ['label' => ['for' => 'tags-ids-2']],
  3536. ['input' => [
  3537. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3538. 'value' => '2', 'name' => 'tags[_ids][]'
  3539. ]],
  3540. 'red',
  3541. '/label',
  3542. '/div',
  3543. '/div'
  3544. ];
  3545. $this->assertHtml($expected, $result);
  3546. }
  3547. /**
  3548. * testControlMagicSelectForTypeNumber method
  3549. *
  3550. * Test that magic control() selects are created for type=number.
  3551. *
  3552. * @return void
  3553. */
  3554. public function testControlMagicSelectForTypeNumber()
  3555. {
  3556. TableRegistry::get('ValidateUsers', [
  3557. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3558. ]);
  3559. $entity = new Entity(['balance' => 1]);
  3560. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3561. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3562. $result = $this->Form->control('balance');
  3563. $expected = [
  3564. 'div' => ['class' => 'input select'],
  3565. 'label' => ['for' => 'balance'],
  3566. 'Balance',
  3567. '/label',
  3568. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3569. ['option' => ['value' => '0']],
  3570. 'nothing',
  3571. '/option',
  3572. ['option' => ['value' => '1', 'selected' => 'selected']],
  3573. 'some',
  3574. '/option',
  3575. ['option' => ['value' => '100']],
  3576. 'a lot',
  3577. '/option',
  3578. '/select',
  3579. '/div'
  3580. ];
  3581. $this->assertHtml($expected, $result);
  3582. }
  3583. /**
  3584. * testInvalidControlTypeOption method
  3585. *
  3586. * Test invalid 'input' type option to control() function.
  3587. *
  3588. * @expectedException \RuntimeException
  3589. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3590. * @return void
  3591. */
  3592. public function testInvalidControlTypeOption()
  3593. {
  3594. $this->Form->control('text', ['type' => 'input']);
  3595. }
  3596. /**
  3597. * testControlMagicSelectChangeToRadio method
  3598. *
  3599. * Test that magic control() selects can easily be converted into radio types without error.
  3600. *
  3601. * @return void
  3602. */
  3603. public function testControlMagicSelectChangeToRadio()
  3604. {
  3605. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3606. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3607. $this->assertContains('input type="radio"', $result);
  3608. }
  3609. /**
  3610. * testFormControlSubmit method
  3611. *
  3612. * Test correct results for form::control() and type submit.
  3613. *
  3614. * @return void
  3615. */
  3616. public function testFormControlSubmit()
  3617. {
  3618. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3619. $expected = [
  3620. 'div' => ['class' => 'submit'],
  3621. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3622. '/div'
  3623. ];
  3624. $this->assertHtml($expected, $result);
  3625. }
  3626. /**
  3627. * testFormControls method
  3628. *
  3629. * Test correct results from Form::controls().
  3630. *
  3631. * @return void
  3632. */
  3633. public function testFormControlsLegendFieldset()
  3634. {
  3635. $this->Form->create($this->article);
  3636. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3637. $expected = [
  3638. '<fieldset',
  3639. '<legend',
  3640. 'The Legend',
  3641. '/legend',
  3642. '*/fieldset',
  3643. ];
  3644. $this->assertHtml($expected, $result);
  3645. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3646. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3647. $this->assertContains('<fieldset>', $result);
  3648. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3649. $this->assertNotContains('<legend>', $result);
  3650. $this->assertNotContains('<fieldset>', $result);
  3651. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3652. $this->assertNotContains('<legend>', $result);
  3653. $this->assertNotContains('<fieldset>', $result);
  3654. $this->Form->create($this->article);
  3655. $this->Form->request->params['prefix'] = 'admin';
  3656. $this->Form->request->params['action'] = 'admin_edit';
  3657. $this->Form->request->params['controller'] = 'articles';
  3658. $result = $this->Form->allControls();
  3659. $expected = [
  3660. '<fieldset',
  3661. '<legend',
  3662. 'New Article',
  3663. '/legend',
  3664. '*/fieldset',
  3665. ];
  3666. $this->assertHtml($expected, $result);
  3667. $this->Form->create($this->article);
  3668. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3669. $expected = [
  3670. '<fieldset',
  3671. '<legend',
  3672. 'The Legend',
  3673. '/legend',
  3674. '*/fieldset',
  3675. ];
  3676. $this->assertHtml($expected, $result);
  3677. $this->Form->create($this->article);
  3678. $result = $this->Form->allControls([], [
  3679. 'fieldset' => [
  3680. 'class' => 'some-class some-other-class',
  3681. 'disabled' => true,
  3682. 'data-param' => 'a-param'
  3683. ],
  3684. 'legend' => 'The Legend'
  3685. ]);
  3686. $expected = [
  3687. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3688. '<legend',
  3689. 'The Legend',
  3690. '/legend',
  3691. '*/fieldset',
  3692. ];
  3693. $this->assertHtml($expected, $result);
  3694. }
  3695. /**
  3696. * testFormControls method
  3697. *
  3698. * Test the controls() method.
  3699. *
  3700. * @return void
  3701. */
  3702. public function testFormControls()
  3703. {
  3704. $this->Form->create($this->article);
  3705. $result = $this->Form->allControls();
  3706. $expected = [
  3707. '<fieldset',
  3708. '<legend', 'New Article', '/legend',
  3709. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3710. ['div' => ['class' => 'input select required']],
  3711. '*/div',
  3712. ['div' => ['class' => 'input text required']],
  3713. '*/div',
  3714. ['div' => ['class' => 'input text']],
  3715. '*/div',
  3716. ['div' => ['class' => 'input text']],
  3717. '*/div',
  3718. '/fieldset',
  3719. ];
  3720. $this->assertHtml($expected, $result);
  3721. $result = $this->Form->allControls([
  3722. 'published' => ['type' => 'boolean']
  3723. ]);
  3724. $expected = [
  3725. '<fieldset',
  3726. '<legend', 'New Article', '/legend',
  3727. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3728. ['div' => ['class' => 'input select required']],
  3729. '*/div',
  3730. ['div' => ['class' => 'input text required']],
  3731. '*/div',
  3732. ['div' => ['class' => 'input text']],
  3733. '*/div',
  3734. ['div' => ['class' => 'input boolean']],
  3735. '*/div',
  3736. '/fieldset',
  3737. ];
  3738. $this->assertHtml($expected, $result);
  3739. $this->Form->create($this->article);
  3740. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3741. $expected = [
  3742. 'fieldset' => [],
  3743. 'legend' => [],
  3744. 'Hello',
  3745. '/legend',
  3746. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3747. ['div' => ['class' => 'input select required']],
  3748. '*/div',
  3749. ['div' => ['class' => 'input text required']],
  3750. '*/div',
  3751. ['div' => ['class' => 'input text']],
  3752. '*/div',
  3753. ['div' => ['class' => 'input text']],
  3754. '*/div',
  3755. '/fieldset'
  3756. ];
  3757. $this->assertHtml($expected, $result);
  3758. $this->Form->create(false);
  3759. $expected = [
  3760. 'fieldset' => [],
  3761. ['div' => ['class' => 'input text']],
  3762. 'label' => ['for' => 'foo'],
  3763. 'Foo',
  3764. '/label',
  3765. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3766. '*/div',
  3767. '/fieldset'
  3768. ];
  3769. $result = $this->Form->allControls(
  3770. ['foo' => ['type' => 'text']],
  3771. ['legend' => false]
  3772. );
  3773. $this->assertHtml($expected, $result);
  3774. }
  3775. /**
  3776. * testFormControlsBlacklist method
  3777. *
  3778. * @return void
  3779. */
  3780. public function testFormControlsBlacklist()
  3781. {
  3782. $this->Form->create($this->article);
  3783. $result = $this->Form->allControls([
  3784. 'id' => false
  3785. ]);
  3786. $expected = [
  3787. '<fieldset',
  3788. '<legend', 'New Article', '/legend',
  3789. ['div' => ['class' => 'input select required']],
  3790. '*/div',
  3791. ['div' => ['class' => 'input text required']],
  3792. '*/div',
  3793. ['div' => ['class' => 'input text']],
  3794. '*/div',
  3795. ['div' => ['class' => 'input text']],
  3796. '*/div',
  3797. '/fieldset',
  3798. ];
  3799. $this->assertHtml($expected, $result);
  3800. $this->Form->create($this->article);
  3801. $result = $this->Form->allControls([
  3802. 'id' => []
  3803. ]);
  3804. $expected = [
  3805. '<fieldset',
  3806. '<legend', 'New Article', '/legend',
  3807. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3808. ['div' => ['class' => 'input select required']],
  3809. '*/div',
  3810. ['div' => ['class' => 'input text required']],
  3811. '*/div',
  3812. ['div' => ['class' => 'input text']],
  3813. '*/div',
  3814. ['div' => ['class' => 'input text']],
  3815. '*/div',
  3816. '/fieldset',
  3817. ];
  3818. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3819. }
  3820. /**
  3821. * testSelectAsCheckbox method
  3822. *
  3823. * Test multi-select widget with checkbox formatting.
  3824. *
  3825. * @return void
  3826. */
  3827. public function testSelectAsCheckbox()
  3828. {
  3829. $result = $this->Form->select(
  3830. 'Model.multi_field',
  3831. ['first', 'second', 'third'],
  3832. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3833. );
  3834. $expected = [
  3835. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3836. ['div' => ['class' => 'checkbox']],
  3837. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3838. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3839. 'first',
  3840. '/label',
  3841. '/div',
  3842. ['div' => ['class' => 'checkbox']],
  3843. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3844. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3845. 'second',
  3846. '/label',
  3847. '/div',
  3848. ['div' => ['class' => 'checkbox']],
  3849. ['label' => ['for' => 'model-multi-field-2']],
  3850. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3851. 'third',
  3852. '/label',
  3853. '/div',
  3854. ];
  3855. $this->assertHtml($expected, $result);
  3856. $result = $this->Form->select(
  3857. 'Model.multi_field',
  3858. ['1/2' => 'half'],
  3859. ['multiple' => 'checkbox']
  3860. );
  3861. $expected = [
  3862. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3863. ['div' => ['class' => 'checkbox']],
  3864. ['label' => ['for' => 'model-multi-field-1-2']],
  3865. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3866. 'half',
  3867. '/label',
  3868. '/div',
  3869. ];
  3870. $this->assertHtml($expected, $result);
  3871. }
  3872. /**
  3873. * testLabel method
  3874. *
  3875. * Test label generation.
  3876. *
  3877. * @return void
  3878. */
  3879. public function testLabel()
  3880. {
  3881. $result = $this->Form->label('Person.name');
  3882. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3883. $this->assertHtml($expected, $result);
  3884. $result = $this->Form->label('Person.name');
  3885. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3886. $this->assertHtml($expected, $result);
  3887. $result = $this->Form->label('Person.first_name');
  3888. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3889. $this->assertHtml($expected, $result);
  3890. $result = $this->Form->label('Person.first_name', 'Your first name');
  3891. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3892. $this->assertHtml($expected, $result);
  3893. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3894. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3895. $this->assertHtml($expected, $result);
  3896. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3897. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3898. $this->assertHtml($expected, $result);
  3899. $result = $this->Form->label('Person.first_name', '');
  3900. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3901. $this->assertHtml($expected, $result);
  3902. $result = $this->Form->label('Person.2.name', '');
  3903. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3904. $this->assertHtml($expected, $result);
  3905. }
  3906. /**
  3907. * testLabelContainControl method
  3908. *
  3909. * Test that label() can accept an input with the correct template vars.
  3910. *
  3911. * @return void
  3912. */
  3913. public function testLabelContainControl()
  3914. {
  3915. $this->Form->templates([
  3916. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3917. ]);
  3918. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3919. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3920. ]);
  3921. $expected = [
  3922. 'label' => ['for' => 'person-accept-terms'],
  3923. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3924. 'Accept',
  3925. '/label',
  3926. ];
  3927. $this->assertHtml($expected, $result);
  3928. }
  3929. /**
  3930. * testTextbox method
  3931. *
  3932. * Test textbox element generation.
  3933. *
  3934. * @return void
  3935. */
  3936. public function testTextbox()
  3937. {
  3938. $result = $this->Form->text('Model.field');
  3939. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3940. $this->assertHtml($expected, $result);
  3941. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3942. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3943. $this->assertHtml($expected, $result);
  3944. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3945. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3946. $this->assertHtml($expected, $result);
  3947. }
  3948. /**
  3949. * testTextBoxDataAndError method
  3950. *
  3951. * Test that text() hooks up with request data and error fields.
  3952. *
  3953. * @return void
  3954. */
  3955. public function testTextBoxDataAndError()
  3956. {
  3957. $this->article['errors'] = [
  3958. 'Contact' => ['text' => 'wrong']
  3959. ];
  3960. $this->Form->create($this->article);
  3961. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3962. $result = $this->Form->text('Model.text');
  3963. $expected = [
  3964. 'input' => [
  3965. 'type' => 'text',
  3966. 'name' => 'Model[text]',
  3967. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3968. ]
  3969. ];
  3970. $this->assertHtml($expected, $result);
  3971. $this->Form->request->data['Contact']['text'] = 'test';
  3972. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3973. $expected = [
  3974. 'input' => [
  3975. 'type' => 'text',
  3976. 'name' => 'Contact[text]',
  3977. 'value' => 'test',
  3978. 'id' => 'theID',
  3979. 'class' => 'form-error'
  3980. ]
  3981. ];
  3982. $this->assertHtml($expected, $result);
  3983. }
  3984. /**
  3985. * testDefaultValue method
  3986. *
  3987. * Test default value setting.
  3988. *
  3989. * @return void
  3990. */
  3991. public function testTextDefaultValue()
  3992. {
  3993. $this->Form->request->data['Model']['field'] = 'test';
  3994. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3995. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3996. $this->assertHtml($expected, $result);
  3997. unset($this->Form->request->data['Model']['field']);
  3998. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3999. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4000. $this->assertHtml($expected, $result);
  4001. $Articles = TableRegistry::get('Articles');
  4002. $title = $Articles->schema()->column('title');
  4003. $Articles->schema()->addColumn(
  4004. 'title',
  4005. ['default' => 'default title'] + $title
  4006. );
  4007. $entity = $Articles->newEntity();
  4008. $this->Form->create($entity);
  4009. // Get default value from schema
  4010. $result = $this->Form->text('title');
  4011. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4012. $this->assertHtml($expected, $result);
  4013. // Don't get value from schema
  4014. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4015. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4016. $this->assertHtml($expected, $result);
  4017. // Custom default value overrides default value from schema
  4018. $result = $this->Form->text('title', ['default' => 'override default']);
  4019. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4020. $this->assertHtml($expected, $result);
  4021. // Default value from schema is used only for new entities.
  4022. $entity->isNew(false);
  4023. $result = $this->Form->text('title');
  4024. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4025. $this->assertHtml($expected, $result);
  4026. }
  4027. /**
  4028. * testError method
  4029. *
  4030. * Test field error generation.
  4031. *
  4032. * @return void
  4033. */
  4034. public function testError()
  4035. {
  4036. $this->article['errors'] = [
  4037. 'Article' => ['field' => 'email']
  4038. ];
  4039. $this->Form->create($this->article);
  4040. $result = $this->Form->error('Article.field');
  4041. $expected = [
  4042. ['div' => ['class' => 'error-message']],
  4043. 'email',
  4044. '/div',
  4045. ];
  4046. $this->assertHtml($expected, $result);
  4047. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  4048. $expected = [
  4049. ['div' => ['class' => 'error-message']],
  4050. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4051. '/div',
  4052. ];
  4053. $this->assertHtml($expected, $result);
  4054. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  4055. $expected = [
  4056. ['div' => ['class' => 'error-message']],
  4057. '<strong', 'Badness!', '/strong',
  4058. '/div',
  4059. ];
  4060. $this->assertHtml($expected, $result);
  4061. }
  4062. /**
  4063. * testErrorRuleName method
  4064. *
  4065. * Test error translation can use rule names for translating.
  4066. *
  4067. * @return void
  4068. */
  4069. public function testErrorRuleName()
  4070. {
  4071. $this->article['errors'] = [
  4072. 'Article' => [
  4073. 'field' => ['email' => 'Your email was not good']
  4074. ]
  4075. ];
  4076. $this->Form->create($this->article);
  4077. $result = $this->Form->error('Article.field');
  4078. $expected = [
  4079. ['div' => ['class' => 'error-message']],
  4080. 'Your email was not good',
  4081. '/div',
  4082. ];
  4083. $this->assertHtml($expected, $result);
  4084. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4085. $expected = [
  4086. ['div' => ['class' => 'error-message']],
  4087. 'Email in use',
  4088. '/div',
  4089. ];
  4090. $this->assertHtml($expected, $result);
  4091. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4092. $expected = [
  4093. ['div' => ['class' => 'error-message']],
  4094. 'Email in use',
  4095. '/div',
  4096. ];
  4097. $this->assertHtml($expected, $result);
  4098. $result = $this->Form->error('Article.field', [
  4099. 'email' => 'Key is preferred',
  4100. 'Your email was not good' => 'Email in use'
  4101. ]);
  4102. $expected = [
  4103. ['div' => ['class' => 'error-message']],
  4104. 'Key is preferred',
  4105. '/div',
  4106. ];
  4107. $this->assertHtml($expected, $result);
  4108. }
  4109. /**
  4110. * testErrorMessages method
  4111. *
  4112. * Test error with nested lists.
  4113. *
  4114. * @return void
  4115. */
  4116. public function testErrorMessages()
  4117. {
  4118. $this->article['errors'] = [
  4119. 'Article' => ['field' => 'email']
  4120. ];
  4121. $this->Form->create($this->article);
  4122. $result = $this->Form->error('Article.field', [
  4123. 'email' => 'No good!'
  4124. ]);
  4125. $expected = [
  4126. 'div' => ['class' => 'error-message'],
  4127. 'No good!',
  4128. '/div'
  4129. ];
  4130. $this->assertHtml($expected, $result);
  4131. }
  4132. /**
  4133. * testErrorMultipleMessages method
  4134. *
  4135. * Test error() with multiple messages.
  4136. *
  4137. * @return void
  4138. */
  4139. public function testErrorMultipleMessages()
  4140. {
  4141. $this->article['errors'] = [
  4142. 'field' => ['notBlank', 'email', 'Something else']
  4143. ];
  4144. $this->Form->create($this->article);
  4145. $result = $this->Form->error('field', [
  4146. 'notBlank' => 'Cannot be empty',
  4147. 'email' => 'No good!'
  4148. ]);
  4149. $expected = [
  4150. 'div' => ['class' => 'error-message'],
  4151. 'ul' => [],
  4152. '<li', 'Cannot be empty', '/li',
  4153. '<li', 'No good!', '/li',
  4154. '<li', 'Something else', '/li',
  4155. '/ul',
  4156. '/div'
  4157. ];
  4158. $this->assertHtml($expected, $result);
  4159. }
  4160. /**
  4161. * testPassword method
  4162. *
  4163. * Test password element generation.
  4164. *
  4165. * @return void
  4166. */
  4167. public function testPassword()
  4168. {
  4169. $this->article['errors'] = [
  4170. 'Contact' => [
  4171. 'passwd' => 1
  4172. ]
  4173. ];
  4174. $this->Form->create($this->article);
  4175. $result = $this->Form->password('Contact.field');
  4176. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4177. $this->assertHtml($expected, $result);
  4178. $this->Form->request->data['Contact']['passwd'] = 'test';
  4179. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4180. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4181. $this->assertHtml($expected, $result);
  4182. }
  4183. /**
  4184. * testRadio method
  4185. *
  4186. * Test radio element set generation.
  4187. *
  4188. * @return void
  4189. */
  4190. public function testRadio()
  4191. {
  4192. $result = $this->Form->radio('Model.field', ['option A']);
  4193. $expected = [
  4194. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4195. 'label' => ['for' => 'model-field-0'],
  4196. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4197. 'option A',
  4198. '/label'
  4199. ];
  4200. $this->assertHtml($expected, $result);
  4201. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4202. $this->assertHtml($expected, $result);
  4203. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4204. $expected = [
  4205. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4206. ['label' => ['for' => 'model-field-0']],
  4207. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4208. 'option A',
  4209. '/label',
  4210. ['label' => ['for' => 'model-field-1']],
  4211. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4212. 'option B',
  4213. '/label',
  4214. ];
  4215. $this->assertHtml($expected, $result);
  4216. $result = $this->Form->radio(
  4217. 'Employee.gender',
  4218. ['male' => 'Male', 'female' => 'Female'],
  4219. ['form' => 'my-form']
  4220. );
  4221. $expected = [
  4222. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4223. ['label' => ['for' => 'employee-gender-male']],
  4224. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4225. 'Male',
  4226. '/label',
  4227. ['label' => ['for' => 'employee-gender-female']],
  4228. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4229. 'Female',
  4230. '/label',
  4231. ];
  4232. $this->assertHtml($expected, $result);
  4233. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4234. $expected = [
  4235. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4236. ['label' => ['for' => 'model-custom-0']],
  4237. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4238. 'option A',
  4239. '/label',
  4240. ['label' => ['for' => 'model-custom-1']],
  4241. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4242. 'option B',
  4243. '/label',
  4244. ];
  4245. $this->assertHtml($expected, $result);
  4246. $result = $this->Form->radio(
  4247. 'Employee.gender',
  4248. [
  4249. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4250. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4251. ]
  4252. );
  4253. $expected = [
  4254. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4255. ['label' => ['for' => 'employee-gender-male']],
  4256. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4257. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4258. 'Male',
  4259. '/label',
  4260. ['label' => ['for' => 'employee-gender-female']],
  4261. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4262. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4263. 'Female',
  4264. '/label',
  4265. ];
  4266. $this->assertHtml($expected, $result);
  4267. }
  4268. /**
  4269. * testRadioDefaultValue method
  4270. *
  4271. * Test default value setting on radio() method.
  4272. *
  4273. * @return void
  4274. */
  4275. public function testRadioDefaultValue()
  4276. {
  4277. $Articles = TableRegistry::get('Articles');
  4278. $title = $Articles->schema()->column('title');
  4279. $Articles->schema()->addColumn(
  4280. 'title',
  4281. ['default' => '1'] + $title
  4282. );
  4283. $this->Form->create($Articles->newEntity());
  4284. $result = $this->Form->radio('title', ['option A', 'option B']);
  4285. $expected = [
  4286. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4287. ['label' => ['for' => 'title-0']],
  4288. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4289. 'option A',
  4290. '/label',
  4291. ['label' => ['for' => 'title-1']],
  4292. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4293. 'option B',
  4294. '/label',
  4295. ];
  4296. $this->assertHtml($expected, $result);
  4297. }
  4298. /**
  4299. * testControlRadio method
  4300. *
  4301. * Test that input works with radio types.
  4302. *
  4303. * @return void
  4304. */
  4305. public function testControlRadio()
  4306. {
  4307. $result = $this->Form->control('test', [
  4308. 'type' => 'radio',
  4309. 'options' => ['A', 'B'],
  4310. ]);
  4311. $expected = [
  4312. ['div' => ['class' => 'input radio']],
  4313. '<label',
  4314. 'Test',
  4315. '/label',
  4316. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4317. ['label' => ['for' => 'test-0']],
  4318. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4319. 'A',
  4320. '/label',
  4321. ['label' => ['for' => 'test-1']],
  4322. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4323. 'B',
  4324. '/label',
  4325. '/div',
  4326. ];
  4327. $this->assertHtml($expected, $result);
  4328. $result = $this->Form->control('test', [
  4329. 'type' => 'radio',
  4330. 'options' => ['A', 'B'],
  4331. 'value' => '0'
  4332. ]);
  4333. $expected = [
  4334. ['div' => ['class' => 'input radio']],
  4335. '<label',
  4336. 'Test',
  4337. '/label',
  4338. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4339. ['label' => ['for' => 'test-0']],
  4340. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4341. 'A',
  4342. '/label',
  4343. ['label' => ['for' => 'test-1']],
  4344. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4345. 'B',
  4346. '/label',
  4347. '/div',
  4348. ];
  4349. $this->assertHtml($expected, $result);
  4350. $result = $this->Form->control('test', [
  4351. 'type' => 'radio',
  4352. 'options' => ['A', 'B'],
  4353. 'label' => false
  4354. ]);
  4355. $expected = [
  4356. ['div' => ['class' => 'input radio']],
  4357. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4358. ['label' => ['for' => 'test-0']],
  4359. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4360. 'A',
  4361. '/label',
  4362. ['label' => ['for' => 'test-1']],
  4363. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4364. 'B',
  4365. '/label',
  4366. '/div',
  4367. ];
  4368. $this->assertHtml($expected, $result);
  4369. }
  4370. /**
  4371. * testRadioNoLabel method
  4372. *
  4373. * Test that radio() works with label = false.
  4374. *
  4375. * @return void
  4376. */
  4377. public function testRadioNoLabel()
  4378. {
  4379. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4380. $expected = [
  4381. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4382. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4383. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4384. ];
  4385. $this->assertHtml($expected, $result);
  4386. }
  4387. /**
  4388. * testRadioControlInsideLabel method
  4389. *
  4390. * Test generating radio input inside label ala twitter bootstrap.
  4391. *
  4392. * @return void
  4393. */
  4394. public function testRadioControlInsideLabel()
  4395. {
  4396. $this->Form->templates([
  4397. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4398. 'radioWrapper' => '{{label}}'
  4399. ]);
  4400. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4401. //@codingStandardsIgnoreStart
  4402. $expected = [
  4403. ['input' => [
  4404. 'type' => 'hidden',
  4405. 'name' => 'Model[field]',
  4406. 'value' => ''
  4407. ]],
  4408. ['label' => ['for' => 'model-field-0']],
  4409. ['input' => [
  4410. 'type' => 'radio',
  4411. 'name' => 'Model[field]',
  4412. 'value' => '0',
  4413. 'id' => 'model-field-0'
  4414. ]],
  4415. 'option A',
  4416. '/label',
  4417. ['label' => ['for' => 'model-field-1']],
  4418. ['input' => [
  4419. 'type' => 'radio',
  4420. 'name' => 'Model[field]',
  4421. 'value' => '1',
  4422. 'id' => 'model-field-1'
  4423. ]],
  4424. 'option B',
  4425. '/label',
  4426. ];
  4427. //@codingStandardsIgnoreEnd
  4428. $this->assertHtml($expected, $result);
  4429. }
  4430. /**
  4431. * testRadioHiddenControlDisabling method
  4432. *
  4433. * Test disabling the hidden input for radio buttons.
  4434. *
  4435. * @return void
  4436. */
  4437. public function testRadioHiddenControlDisabling()
  4438. {
  4439. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4440. $expected = [
  4441. 'label' => ['for' => 'model-1-field-0'],
  4442. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4443. 'option A',
  4444. '/label'
  4445. ];
  4446. $this->assertHtml($expected, $result);
  4447. }
  4448. /**
  4449. * testRadioOutOfRange method
  4450. *
  4451. * Test radio element set generation.
  4452. *
  4453. * @return void
  4454. */
  4455. public function testRadioOutOfRange()
  4456. {
  4457. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4458. $expected = [
  4459. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4460. 'label' => ['for' => 'model-field-v'],
  4461. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4462. 'value',
  4463. '/label'
  4464. ];
  4465. $this->assertHtml($expected, $result);
  4466. }
  4467. /**
  4468. * testSelect method
  4469. *
  4470. * Test select element generation.
  4471. *
  4472. * @return void
  4473. */
  4474. public function testSelect()
  4475. {
  4476. $result = $this->Form->select('Model.field', []);
  4477. $expected = [
  4478. 'select' => ['name' => 'Model[field]'],
  4479. '/select'
  4480. ];
  4481. $this->assertHtml($expected, $result);
  4482. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4483. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4484. $expected = [
  4485. 'select' => ['name' => 'Model[field]'],
  4486. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4487. 'good',
  4488. '/option',
  4489. ['option' => ['value' => 'other']],
  4490. 'bad',
  4491. '/option',
  4492. '/select'
  4493. ];
  4494. $this->assertHtml($expected, $result);
  4495. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4496. $this->assertHtml($expected, $result);
  4497. $this->Form->request->data = [];
  4498. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4499. $expected = [
  4500. 'select' => ['name' => 'Model[field]'],
  4501. ['option' => ['value' => 'value']],
  4502. 'good',
  4503. '/option',
  4504. ['option' => ['value' => 'other']],
  4505. 'bad',
  4506. '/option',
  4507. '/select'
  4508. ];
  4509. $this->assertHtml($expected, $result);
  4510. $options = [
  4511. ['value' => 'first', 'text' => 'First'],
  4512. ['value' => 'first', 'text' => 'Another First'],
  4513. ];
  4514. $result = $this->Form->select(
  4515. 'Model.field',
  4516. $options,
  4517. ['escape' => false, 'empty' => false]
  4518. );
  4519. $expected = [
  4520. 'select' => ['name' => 'Model[field]'],
  4521. ['option' => ['value' => 'first']],
  4522. 'First',
  4523. '/option',
  4524. ['option' => ['value' => 'first']],
  4525. 'Another First',
  4526. '/option',
  4527. '/select'
  4528. ];
  4529. $this->assertHtml($expected, $result);
  4530. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4531. $result = $this->Form->select(
  4532. 'Model.contact_id',
  4533. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4534. ['escape' => false, 'empty' => 'pick something']
  4535. );
  4536. $expected = [
  4537. 'select' => ['name' => 'Model[contact_id]'],
  4538. ['option' => ['value' => '']], 'pick something', '/option',
  4539. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4540. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4541. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4542. '/select'
  4543. ];
  4544. $this->assertHtml($expected, $result);
  4545. $this->Form->request->data['Model']['field'] = 0;
  4546. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4547. $expected = [
  4548. 'select' => ['name' => 'Model[field]'],
  4549. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4550. ['option' => ['value' => '1']], 'Yes', '/option',
  4551. '/select'
  4552. ];
  4553. $this->assertHtml($expected, $result);
  4554. }
  4555. /**
  4556. * testSelectEscapeHtml method
  4557. *
  4558. * Test that select() escapes HTML.
  4559. *
  4560. * @return void
  4561. */
  4562. public function testSelectEscapeHtml()
  4563. {
  4564. $result = $this->Form->select(
  4565. 'Model.field',
  4566. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4567. ['empty' => false]
  4568. );
  4569. $expected = [
  4570. 'select' => ['name' => 'Model[field]'],
  4571. ['option' => ['value' => 'first']],
  4572. 'first &quot;html&quot; &lt;chars&gt;',
  4573. '/option',
  4574. ['option' => ['value' => 'second']],
  4575. 'value',
  4576. '/option',
  4577. '/select'
  4578. ];
  4579. $this->assertHtml($expected, $result);
  4580. $result = $this->Form->select(
  4581. 'Model.field',
  4582. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4583. ['escape' => false, 'empty' => false]
  4584. );
  4585. $expected = [
  4586. 'select' => ['name' => 'Model[field]'],
  4587. ['option' => ['value' => 'first']],
  4588. 'first "html" <chars>',
  4589. '/option',
  4590. ['option' => ['value' => 'second']],
  4591. 'value',
  4592. '/option',
  4593. '/select'
  4594. ];
  4595. $this->assertHtml($expected, $result);
  4596. }
  4597. /**
  4598. * testSelectRequired method
  4599. *
  4600. * Test select() with required and disabled attributes.
  4601. *
  4602. * @return void
  4603. */
  4604. public function testSelectRequired()
  4605. {
  4606. $this->article['required'] = [
  4607. 'user_id' => true
  4608. ];
  4609. $this->Form->create($this->article);
  4610. $result = $this->Form->select('user_id', ['option A']);
  4611. $expected = [
  4612. 'select' => [
  4613. 'name' => 'user_id',
  4614. 'required' => 'required'
  4615. ],
  4616. ['option' => ['value' => '0']], 'option A', '/option',
  4617. '/select'
  4618. ];
  4619. $this->assertHtml($expected, $result);
  4620. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4621. $expected = [
  4622. 'select' => [
  4623. 'name' => 'user_id',
  4624. 'disabled' => 'disabled'
  4625. ],
  4626. ['option' => ['value' => '0']], 'option A', '/option',
  4627. '/select'
  4628. ];
  4629. $this->assertHtml($expected, $result);
  4630. }
  4631. /**
  4632. * testNestedSelect method
  4633. *
  4634. * Test select element generation with optgroups.
  4635. *
  4636. * @return void
  4637. */
  4638. public function testNestedSelect()
  4639. {
  4640. $result = $this->Form->select(
  4641. 'Model.field',
  4642. [1 => 'One', 2 => 'Two', 'Three' => [
  4643. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4644. ]],
  4645. ['empty' => false]
  4646. );
  4647. $expected = [
  4648. 'select' => ['name' => 'Model[field]'],
  4649. ['option' => ['value' => 1]],
  4650. 'One',
  4651. '/option',
  4652. ['option' => ['value' => 2]],
  4653. 'Two',
  4654. '/option',
  4655. ['optgroup' => ['label' => 'Three']],
  4656. ['option' => ['value' => 3]],
  4657. 'Three',
  4658. '/option',
  4659. ['option' => ['value' => 4]],
  4660. 'Four',
  4661. '/option',
  4662. ['option' => ['value' => 5]],
  4663. 'Five',
  4664. '/option',
  4665. '/optgroup',
  4666. '/select'
  4667. ];
  4668. $this->assertHtml($expected, $result);
  4669. }
  4670. /**
  4671. * testSelectMultiple method
  4672. *
  4673. * Test generation of multiple select elements.
  4674. *
  4675. * @return void
  4676. */
  4677. public function testSelectMultiple()
  4678. {
  4679. $options = ['first', 'second', 'third'];
  4680. $result = $this->Form->select(
  4681. 'Model.multi_field',
  4682. $options,
  4683. ['form' => 'my-form', 'multiple' => true]
  4684. );
  4685. $expected = [
  4686. 'input' => [
  4687. 'type' => 'hidden',
  4688. 'name' => 'Model[multi_field]',
  4689. 'value' => '',
  4690. 'form' => 'my-form',
  4691. ],
  4692. 'select' => [
  4693. 'name' => 'Model[multi_field][]',
  4694. 'multiple' => 'multiple',
  4695. 'form' => 'my-form',
  4696. ],
  4697. ['option' => ['value' => '0']],
  4698. 'first',
  4699. '/option',
  4700. ['option' => ['value' => '1']],
  4701. 'second',
  4702. '/option',
  4703. ['option' => ['value' => '2']],
  4704. 'third',
  4705. '/option',
  4706. '/select'
  4707. ];
  4708. $this->assertHtml($expected, $result);
  4709. $result = $this->Form->select(
  4710. 'Model.multi_field',
  4711. $options,
  4712. ['multiple' => 'multiple', 'form' => 'my-form']
  4713. );
  4714. $this->assertHtml($expected, $result);
  4715. }
  4716. /**
  4717. * testCheckboxZeroValue method
  4718. *
  4719. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4720. *
  4721. * @return void
  4722. */
  4723. public function testCheckboxZeroValue()
  4724. {
  4725. $result = $this->Form->control('User.get_spam', [
  4726. 'type' => 'checkbox',
  4727. 'value' => '0',
  4728. 'hiddenField' => '1',
  4729. ]);
  4730. $expected = [
  4731. 'div' => ['class' => 'input checkbox'],
  4732. 'label' => ['for' => 'user-get-spam'],
  4733. ['input' => [
  4734. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4735. 'value' => '1'
  4736. ]],
  4737. ['input' => [
  4738. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4739. 'value' => '0', 'id' => 'user-get-spam'
  4740. ]],
  4741. 'Get Spam',
  4742. '/label',
  4743. '/div'
  4744. ];
  4745. $this->assertHtml($expected, $result);
  4746. }
  4747. /**
  4748. * testHabtmSelectBox method
  4749. *
  4750. * Test generation of habtm select boxes.
  4751. *
  4752. * @return void
  4753. */
  4754. public function testHabtmSelectBox()
  4755. {
  4756. $this->loadFixtures('Articles');
  4757. $options = [
  4758. 1 => 'blue',
  4759. 2 => 'red',
  4760. 3 => 'green'
  4761. ];
  4762. $tags = [
  4763. new Entity(['id' => 1, 'name' => 'blue']),
  4764. new Entity(['id' => 3, 'name' => 'green'])
  4765. ];
  4766. $article = new Article(['tags' => $tags]);
  4767. $this->Form->create($article);
  4768. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4769. $expected = [
  4770. 'div' => ['class' => 'input select'],
  4771. 'label' => ['for' => 'tags-ids'],
  4772. 'Tags',
  4773. '/label',
  4774. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4775. 'select' => [
  4776. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4777. 'multiple' => 'multiple'
  4778. ],
  4779. ['option' => ['value' => '1', 'selected' => 'selected']],
  4780. 'blue',
  4781. '/option',
  4782. ['option' => ['value' => '2']],
  4783. 'red',
  4784. '/option',
  4785. ['option' => ['value' => '3', 'selected' => 'selected']],
  4786. 'green',
  4787. '/option',
  4788. '/select',
  4789. '/div'
  4790. ];
  4791. $this->assertHtml($expected, $result);
  4792. // make sure only 50 is selected, and not 50f5c0cf
  4793. $options = [
  4794. '1' => 'blue',
  4795. '50f5c0cf' => 'red',
  4796. '50' => 'green'
  4797. ];
  4798. $tags = [
  4799. new Entity(['id' => 1, 'name' => 'blue']),
  4800. new Entity(['id' => 50, 'name' => 'green'])
  4801. ];
  4802. $article = new Article(['tags' => $tags]);
  4803. $this->Form->create($article);
  4804. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4805. $expected = [
  4806. 'div' => ['class' => 'input select'],
  4807. 'label' => ['for' => 'tags-ids'],
  4808. 'Tags',
  4809. '/label',
  4810. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4811. 'select' => [
  4812. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4813. 'multiple' => 'multiple'
  4814. ],
  4815. ['option' => ['value' => '1', 'selected' => 'selected']],
  4816. 'blue',
  4817. '/option',
  4818. ['option' => ['value' => '50f5c0cf']],
  4819. 'red',
  4820. '/option',
  4821. ['option' => ['value' => '50', 'selected' => 'selected']],
  4822. 'green',
  4823. '/option',
  4824. '/select',
  4825. '/div'
  4826. ];
  4827. $this->assertHtml($expected, $result);
  4828. $spacecraft = [
  4829. 1 => 'Orion',
  4830. 2 => 'Helios'
  4831. ];
  4832. $this->View->viewVars['spacecraft'] = $spacecraft;
  4833. $this->Form->create();
  4834. $result = $this->Form->control('spacecraft._ids');
  4835. $expected = [
  4836. 'div' => ['class' => 'input select'],
  4837. 'label' => ['for' => 'spacecraft-ids'],
  4838. 'Spacecraft',
  4839. '/label',
  4840. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4841. 'select' => [
  4842. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4843. 'multiple' => 'multiple'
  4844. ],
  4845. ['option' => ['value' => '1']],
  4846. 'Orion',
  4847. '/option',
  4848. ['option' => ['value' => '2']],
  4849. 'Helios',
  4850. '/option',
  4851. '/select',
  4852. '/div'
  4853. ];
  4854. $this->assertHtml($expected, $result);
  4855. }
  4856. /**
  4857. * testErrorsForBelongsToManySelect method
  4858. *
  4859. * Tests that errors for belongsToMany select fields are being
  4860. * picked up properly.
  4861. *
  4862. * @return void
  4863. */
  4864. public function testErrorsForBelongsToManySelect()
  4865. {
  4866. $spacecraft = [
  4867. 1 => 'Orion',
  4868. 2 => 'Helios'
  4869. ];
  4870. $this->View->viewVars['spacecraft'] = $spacecraft;
  4871. $article = new Article();
  4872. $article->errors('spacecraft', ['Invalid']);
  4873. $this->Form->create($article);
  4874. $result = $this->Form->control('spacecraft._ids');
  4875. $expected = [
  4876. ['div' => ['class' => 'input select error']],
  4877. 'label' => ['for' => 'spacecraft-ids'],
  4878. 'Spacecraft',
  4879. '/label',
  4880. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4881. 'select' => [
  4882. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4883. 'multiple' => 'multiple'
  4884. ],
  4885. ['option' => ['value' => '1']],
  4886. 'Orion',
  4887. '/option',
  4888. ['option' => ['value' => '2']],
  4889. 'Helios',
  4890. '/option',
  4891. '/select',
  4892. ['div' => ['class' => 'error-message']],
  4893. 'Invalid',
  4894. '/div',
  4895. '/div'
  4896. ];
  4897. $this->assertHtml($expected, $result);
  4898. }
  4899. /**
  4900. * testSelectMultipleCheckboxes method
  4901. *
  4902. * Test generation of multi select elements in checkbox format.
  4903. *
  4904. * @return void
  4905. */
  4906. public function testSelectMultipleCheckboxes()
  4907. {
  4908. $result = $this->Form->select(
  4909. 'Model.multi_field',
  4910. ['first', 'second', 'third'],
  4911. ['multiple' => 'checkbox']
  4912. );
  4913. $expected = [
  4914. 'input' => [
  4915. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4916. ],
  4917. ['div' => ['class' => 'checkbox']],
  4918. ['label' => ['for' => 'model-multi-field-0']],
  4919. ['input' => [
  4920. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4921. 'value' => '0', 'id' => 'model-multi-field-0'
  4922. ]],
  4923. 'first',
  4924. '/label',
  4925. '/div',
  4926. ['div' => ['class' => 'checkbox']],
  4927. ['label' => ['for' => 'model-multi-field-1']],
  4928. ['input' => [
  4929. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4930. 'value' => '1', 'id' => 'model-multi-field-1'
  4931. ]],
  4932. 'second',
  4933. '/label',
  4934. '/div',
  4935. ['div' => ['class' => 'checkbox']],
  4936. ['label' => ['for' => 'model-multi-field-2']],
  4937. ['input' => [
  4938. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4939. 'value' => '2', 'id' => 'model-multi-field-2'
  4940. ]],
  4941. 'third',
  4942. '/label',
  4943. '/div'
  4944. ];
  4945. $this->assertHtml($expected, $result);
  4946. $result = $this->Form->select(
  4947. 'Model.multi_field',
  4948. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4949. ['multiple' => 'checkbox']
  4950. );
  4951. $expected = [
  4952. 'input' => [
  4953. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4954. ],
  4955. ['div' => ['class' => 'checkbox']],
  4956. ['label' => ['for' => 'model-multi-field-a+']],
  4957. ['input' => [
  4958. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4959. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4960. ]],
  4961. 'first',
  4962. '/label',
  4963. '/div',
  4964. ['div' => ['class' => 'checkbox']],
  4965. ['label' => ['for' => 'model-multi-field-a++']],
  4966. ['input' => [
  4967. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4968. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4969. ]],
  4970. 'second',
  4971. '/label',
  4972. '/div',
  4973. ['div' => ['class' => 'checkbox']],
  4974. ['label' => ['for' => 'model-multi-field-a+++']],
  4975. ['input' => [
  4976. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4977. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4978. ]],
  4979. 'third',
  4980. '/label',
  4981. '/div'
  4982. ];
  4983. $this->assertHtml($expected, $result);
  4984. $result = $this->Form->select(
  4985. 'Model.multi_field',
  4986. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4987. ['multiple' => 'checkbox']
  4988. );
  4989. $expected = [
  4990. 'input' => [
  4991. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4992. ],
  4993. ['div' => ['class' => 'checkbox']],
  4994. ['label' => ['for' => 'model-multi-field-a-b']],
  4995. ['input' => [
  4996. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4997. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4998. ]],
  4999. 'first',
  5000. '/label',
  5001. '/div',
  5002. ['div' => ['class' => 'checkbox']],
  5003. ['label' => ['for' => 'model-multi-field-a-b1']],
  5004. ['input' => [
  5005. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5006. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5007. ]],
  5008. 'second',
  5009. '/label',
  5010. '/div',
  5011. ['div' => ['class' => 'checkbox']],
  5012. ['label' => ['for' => 'model-multi-field-a-b2']],
  5013. ['input' => [
  5014. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5015. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5016. ]],
  5017. 'third',
  5018. '/label',
  5019. '/div'
  5020. ];
  5021. $this->assertHtml($expected, $result);
  5022. }
  5023. /**
  5024. * testSelectMultipleCheckboxRequestData method
  5025. *
  5026. * Ensure that multiCheckbox reads from the request data.
  5027. *
  5028. * @return void
  5029. */
  5030. public function testSelectMultipleCheckboxRequestData()
  5031. {
  5032. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5033. $result = $this->Form->select(
  5034. 'Model.tags',
  5035. ['1' => 'first', 'Array' => 'Array'],
  5036. ['multiple' => 'checkbox']
  5037. );
  5038. $expected = [
  5039. 'input' => [
  5040. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5041. ],
  5042. ['div' => ['class' => 'checkbox']],
  5043. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5044. ['input' => [
  5045. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5046. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5047. ]],
  5048. 'first',
  5049. '/label',
  5050. '/div',
  5051. ['div' => ['class' => 'checkbox']],
  5052. ['label' => ['for' => 'model-tags-array']],
  5053. ['input' => [
  5054. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5055. 'value' => 'Array', 'id' => 'model-tags-array'
  5056. ]],
  5057. 'Array',
  5058. '/label',
  5059. '/div'
  5060. ];
  5061. $this->assertHtml($expected, $result);
  5062. }
  5063. /**
  5064. * testSelectMultipleCheckboxSecurity method
  5065. *
  5066. * Checks the security hash array generated for multiple-input checkbox elements.
  5067. *
  5068. * @return void
  5069. */
  5070. public function testSelectMultipleCheckboxSecurity()
  5071. {
  5072. $this->Form->request->params['_Token'] = 'testKey';
  5073. $this->assertEquals([], $this->Form->fields);
  5074. $this->Form->select(
  5075. 'Model.multi_field',
  5076. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5077. ['multiple' => 'checkbox']
  5078. );
  5079. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5080. $result = $this->Form->secure($this->Form->fields);
  5081. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5082. $this->assertRegExp('/"' . $key . '"/', $result);
  5083. }
  5084. /**
  5085. * testSelectMultipleSecureWithNoOptions method
  5086. *
  5087. * Multiple select elements should always be secured as they always participate
  5088. * in the POST data.
  5089. *
  5090. * @return void
  5091. */
  5092. public function testSelectMultipleSecureWithNoOptions()
  5093. {
  5094. $this->assertEquals([], $this->Form->fields);
  5095. $this->Form->select(
  5096. 'Model.select',
  5097. [],
  5098. ['multiple' => true]
  5099. );
  5100. $this->assertEquals(['Model.select'], $this->Form->fields);
  5101. }
  5102. /**
  5103. * testSelectNoSecureWithNoOptions method
  5104. *
  5105. * When a select box has no options it should not be added to the fields list
  5106. * as it always fail post validation.
  5107. *
  5108. * @return void
  5109. */
  5110. public function testSelectNoSecureWithNoOptions()
  5111. {
  5112. $this->Form->request->params['_Token'] = 'testkey';
  5113. $this->assertEquals([], $this->Form->fields);
  5114. $this->Form->select(
  5115. 'Model.select',
  5116. []
  5117. );
  5118. $this->assertEquals([], $this->Form->fields);
  5119. $this->Form->select(
  5120. 'Model.user_id',
  5121. [],
  5122. ['empty' => true]
  5123. );
  5124. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5125. }
  5126. /**
  5127. * testControlMultipleCheckboxes method
  5128. *
  5129. * Test control() resulting in multi select elements being generated.
  5130. *
  5131. * @return void
  5132. */
  5133. public function testControlMultipleCheckboxes()
  5134. {
  5135. $result = $this->Form->control('Model.multi_field', [
  5136. 'options' => ['first', 'second', 'third'],
  5137. 'multiple' => 'checkbox'
  5138. ]);
  5139. $expected = [
  5140. ['div' => ['class' => 'input select']],
  5141. ['label' => ['for' => 'model-multi-field']],
  5142. 'Multi Field',
  5143. '/label',
  5144. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5145. ['div' => ['class' => 'checkbox']],
  5146. ['label' => ['for' => 'model-multi-field-0']],
  5147. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5148. 'first',
  5149. '/label',
  5150. '/div',
  5151. ['div' => ['class' => 'checkbox']],
  5152. ['label' => ['for' => 'model-multi-field-1']],
  5153. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5154. 'second',
  5155. '/label',
  5156. '/div',
  5157. ['div' => ['class' => 'checkbox']],
  5158. ['label' => ['for' => 'model-multi-field-2']],
  5159. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5160. 'third',
  5161. '/label',
  5162. '/div',
  5163. '/div'
  5164. ];
  5165. $this->assertHtml($expected, $result);
  5166. $result = $this->Form->control('Model.multi_field', [
  5167. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5168. 'multiple' => 'checkbox'
  5169. ]);
  5170. $expected = [
  5171. ['div' => ['class' => 'input select']],
  5172. ['label' => ['for' => 'model-multi-field']],
  5173. 'Multi Field',
  5174. '/label',
  5175. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5176. ['div' => ['class' => 'checkbox']],
  5177. ['label' => ['for' => 'model-multi-field-a']],
  5178. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5179. 'first',
  5180. '/label',
  5181. '/div',
  5182. ['div' => ['class' => 'checkbox']],
  5183. ['label' => ['for' => 'model-multi-field-b']],
  5184. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5185. 'second',
  5186. '/label',
  5187. '/div',
  5188. ['div' => ['class' => 'checkbox']],
  5189. ['label' => ['for' => 'model-multi-field-c']],
  5190. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5191. 'third',
  5192. '/label',
  5193. '/div',
  5194. '/div'
  5195. ];
  5196. $this->assertHtml($expected, $result);
  5197. }
  5198. /**
  5199. * testSelectHiddenFieldOmission method
  5200. *
  5201. * Test that select() with 'hiddenField' => false omits the hidden field.
  5202. *
  5203. * @return void
  5204. */
  5205. public function testSelectHiddenFieldOmission()
  5206. {
  5207. $result = $this->Form->select(
  5208. 'Model.multi_field',
  5209. ['first', 'second'],
  5210. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5211. );
  5212. $this->assertNotContains('type="hidden"', $result);
  5213. }
  5214. /**
  5215. * testSelectCheckboxMultipleOverrideName method
  5216. *
  5217. * Test that select() with multiple = checkbox works with overriding name attribute.
  5218. *
  5219. * @return void
  5220. */
  5221. public function testSelectCheckboxMultipleOverrideName()
  5222. {
  5223. $result = $this->Form->select('category', ['1', '2'], [
  5224. 'multiple' => 'checkbox',
  5225. 'name' => 'fish',
  5226. ]);
  5227. $expected = [
  5228. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5229. ['div' => ['class' => 'checkbox']],
  5230. ['label' => ['for' => 'fish-0']],
  5231. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5232. '1',
  5233. '/label',
  5234. '/div',
  5235. ['div' => ['class' => 'checkbox']],
  5236. ['label' => ['for' => 'fish-1']],
  5237. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5238. '2',
  5239. '/label',
  5240. '/div'
  5241. ];
  5242. $this->assertHtml($expected, $result);
  5243. $result = $this->Form->multiCheckbox(
  5244. 'category',
  5245. new Collection(['1', '2']),
  5246. ['name' => 'fish']
  5247. );
  5248. $this->assertHtml($expected, $result);
  5249. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5250. 'name' => 'fish',
  5251. ]);
  5252. $this->assertHtml($expected, $result);
  5253. }
  5254. /**
  5255. * testControlMultiCheckbox method
  5256. *
  5257. * Test that control() works with multicheckbox.
  5258. *
  5259. * @return void
  5260. */
  5261. public function testControlMultiCheckbox()
  5262. {
  5263. $result = $this->Form->control('category', [
  5264. 'type' => 'multicheckbox',
  5265. 'options' => ['1', '2'],
  5266. ]);
  5267. $expected = [
  5268. ['div' => ['class' => 'input multicheckbox']],
  5269. '<label',
  5270. 'Category',
  5271. '/label',
  5272. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5273. ['div' => ['class' => 'checkbox']],
  5274. ['label' => ['for' => 'category-0']],
  5275. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5276. '1',
  5277. '/label',
  5278. '/div',
  5279. ['div' => ['class' => 'checkbox']],
  5280. ['label' => ['for' => 'category-1']],
  5281. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5282. '2',
  5283. '/label',
  5284. '/div',
  5285. '/div',
  5286. ];
  5287. $this->assertHtml($expected, $result);
  5288. }
  5289. /**
  5290. * testCheckbox method
  5291. *
  5292. * Test generation of checkboxes.
  5293. *
  5294. * @return void
  5295. */
  5296. public function testCheckbox()
  5297. {
  5298. $result = $this->Form->checkbox('Model.field');
  5299. $expected = [
  5300. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5301. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5302. ];
  5303. $this->assertHtml($expected, $result);
  5304. $result = $this->Form->checkbox('Model.field', [
  5305. 'id' => 'theID',
  5306. 'value' => 'myvalue',
  5307. 'form' => 'my-form',
  5308. ]);
  5309. $expected = [
  5310. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5311. ['input' => [
  5312. 'type' => 'checkbox', 'name' => 'Model[field]',
  5313. 'value' => 'myvalue', 'id' => 'theID',
  5314. 'form' => 'my-form',
  5315. ]]
  5316. ];
  5317. $this->assertHtml($expected, $result);
  5318. }
  5319. /**
  5320. * testCheckboxDefaultValue method
  5321. *
  5322. * Test default value setting on checkbox() method.
  5323. *
  5324. * @return void
  5325. */
  5326. public function testCheckboxDefaultValue()
  5327. {
  5328. $this->Form->request->data['Model']['field'] = false;
  5329. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5330. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5331. $this->assertHtml($expected, $result);
  5332. unset($this->Form->request->data['Model']['field']);
  5333. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5334. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5335. $this->assertHtml($expected, $result);
  5336. $this->Form->request->data['Model']['field'] = true;
  5337. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5338. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5339. $this->assertHtml($expected, $result);
  5340. unset($this->Form->request->data['Model']['field']);
  5341. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5342. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5343. $this->assertHtml($expected, $result);
  5344. $Articles = TableRegistry::get('Articles');
  5345. $Articles->schema()->addColumn(
  5346. 'published',
  5347. ['type' => 'boolean', 'null' => false, 'default' => true]
  5348. );
  5349. $this->Form->create($Articles->newEntity());
  5350. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5351. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5352. $this->assertHtml($expected, $result);
  5353. }
  5354. /**
  5355. * testCheckboxCheckedAndError method
  5356. *
  5357. * Test checkbox being checked or having errors.
  5358. *
  5359. * @return void
  5360. */
  5361. public function testCheckboxCheckedAndError()
  5362. {
  5363. $this->article['errors'] = [
  5364. 'published' => true
  5365. ];
  5366. $this->Form->request->data['published'] = 'myvalue';
  5367. $this->Form->create($this->article);
  5368. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5369. $expected = [
  5370. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5371. ['input' => [
  5372. 'type' => 'checkbox',
  5373. 'name' => 'published',
  5374. 'value' => 'myvalue',
  5375. 'id' => 'theID',
  5376. 'checked' => 'checked',
  5377. 'class' => 'form-error'
  5378. ]]
  5379. ];
  5380. $this->assertHtml($expected, $result);
  5381. $this->Form->request->data['published'] = '';
  5382. $result = $this->Form->checkbox('published');
  5383. $expected = [
  5384. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5385. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5386. ];
  5387. $this->assertHtml($expected, $result);
  5388. }
  5389. /**
  5390. * testCheckboxCustomNameAttribute method
  5391. *
  5392. * Test checkbox() with a custom name attribute.
  5393. *
  5394. * @return void
  5395. */
  5396. public function testCheckboxCustomNameAttribute()
  5397. {
  5398. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5399. $expected = [
  5400. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5401. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5402. ];
  5403. $this->assertHtml($expected, $result);
  5404. }
  5405. /**
  5406. * testCheckboxHiddenField method
  5407. *
  5408. * Test that the hidden input for checkboxes can be omitted or set to a
  5409. * specific value.
  5410. *
  5411. * @return void
  5412. */
  5413. public function testCheckboxHiddenField()
  5414. {
  5415. $result = $this->Form->checkbox('UserForm.something', [
  5416. 'hiddenField' => false
  5417. ]);
  5418. $expected = [
  5419. 'input' => [
  5420. 'type' => 'checkbox',
  5421. 'name' => 'UserForm[something]',
  5422. 'value' => '1'
  5423. ],
  5424. ];
  5425. $this->assertHtml($expected, $result);
  5426. $result = $this->Form->checkbox('UserForm.something', [
  5427. 'value' => 'Y',
  5428. 'hiddenField' => 'N',
  5429. ]);
  5430. $expected = [
  5431. ['input' => [
  5432. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5433. 'value' => 'N'
  5434. ]],
  5435. ['input' => [
  5436. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5437. 'value' => 'Y'
  5438. ]],
  5439. ];
  5440. $this->assertHtml($expected, $result);
  5441. }
  5442. /**
  5443. * testTime method
  5444. *
  5445. * Test the time type.
  5446. *
  5447. * @return void
  5448. */
  5449. public function testTime()
  5450. {
  5451. $result = $this->Form->time('start_time', [
  5452. 'timeFormat' => 12,
  5453. 'interval' => 5,
  5454. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5455. ]);
  5456. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5457. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5458. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5459. $this->assertNotContains('year', $result);
  5460. $this->assertNotContains('month', $result);
  5461. $this->assertNotContains('day', $result);
  5462. $result = $this->Form->time('start_time', [
  5463. 'timeFormat' => 12,
  5464. 'interval' => 5,
  5465. 'value' => '2014-03-08 16:30:00'
  5466. ]);
  5467. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5468. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5469. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5470. $this->assertNotContains('year', $result);
  5471. $this->assertNotContains('month', $result);
  5472. $this->assertNotContains('day', $result);
  5473. }
  5474. /**
  5475. * testTimeFormat24NoMeridian method
  5476. *
  5477. * Ensure that timeFormat=24 has no merdian.
  5478. *
  5479. * @return void.
  5480. */
  5481. public function testTimeFormat24NoMeridian()
  5482. {
  5483. $result = $this->Form->time('start_time', [
  5484. 'timeFormat' => 24,
  5485. 'interval' => 5,
  5486. 'value' => '2014-03-08 16:30:00'
  5487. ]);
  5488. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5489. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5490. $this->assertNotContains('meridian', $result);
  5491. $this->assertNotContains('pm', $result);
  5492. $this->assertNotContains('year', $result);
  5493. $this->assertNotContains('month', $result);
  5494. $this->assertNotContains('day', $result);
  5495. }
  5496. /**
  5497. * testDate method
  5498. *
  5499. * Test the date type.
  5500. *
  5501. * @return void
  5502. */
  5503. public function testDate()
  5504. {
  5505. $result = $this->Form->date('start_day', [
  5506. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5507. ]);
  5508. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5509. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5510. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5511. $this->assertNotContains('hour', $result);
  5512. $this->assertNotContains('minute', $result);
  5513. $this->assertNotContains('second', $result);
  5514. $this->assertNotContains('meridian', $result);
  5515. }
  5516. /**
  5517. * testDateTime method
  5518. *
  5519. * Test generation of date/time select elements.
  5520. *
  5521. * @return void
  5522. */
  5523. public function testDateTime()
  5524. {
  5525. extract($this->dateRegex);
  5526. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5527. $now = strtotime('now');
  5528. $expected = [
  5529. ['select' => ['name' => 'Contact[date][year]']],
  5530. ['option' => ['value' => '']],
  5531. '/option',
  5532. $yearsRegex,
  5533. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5534. date('Y', $now),
  5535. '/option',
  5536. '*/select',
  5537. ['select' => ['name' => 'Contact[date][month]']],
  5538. ['option' => ['value' => '']],
  5539. '/option',
  5540. $monthsRegex,
  5541. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5542. date('F', $now),
  5543. '/option',
  5544. '*/select',
  5545. ['select' => ['name' => 'Contact[date][day]']],
  5546. ['option' => ['value' => '']],
  5547. '/option',
  5548. $daysRegex,
  5549. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5550. date('j', $now),
  5551. '/option',
  5552. '*/select',
  5553. ['select' => ['name' => 'Contact[date][hour]']],
  5554. ['option' => ['value' => '']],
  5555. '/option',
  5556. $hoursRegex,
  5557. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5558. date('G', $now),
  5559. '/option',
  5560. '*/select',
  5561. ['select' => ['name' => 'Contact[date][minute]']],
  5562. ['option' => ['value' => '']],
  5563. '/option',
  5564. $minutesRegex,
  5565. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5566. date('i', $now),
  5567. '/option',
  5568. '*/select',
  5569. ];
  5570. $this->assertHtml($expected, $result);
  5571. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5572. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5573. $now = strtotime('now');
  5574. $expected = [
  5575. ['select' => ['name' => 'Contact[date][year]']],
  5576. $yearsRegex,
  5577. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5578. date('Y', $now),
  5579. '/option',
  5580. '*/select',
  5581. ['select' => ['name' => 'Contact[date][month]']],
  5582. $monthsRegex,
  5583. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5584. date('F', $now),
  5585. '/option',
  5586. '*/select',
  5587. ['select' => ['name' => 'Contact[date][day]']],
  5588. $daysRegex,
  5589. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5590. date('j', $now),
  5591. '/option',
  5592. '*/select',
  5593. ['select' => ['name' => 'Contact[date][hour]']],
  5594. $hoursRegex,
  5595. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5596. date('G', $now),
  5597. '/option',
  5598. '*/select',
  5599. ['select' => ['name' => 'Contact[date][minute]']],
  5600. $minutesRegex,
  5601. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5602. date('i', $now),
  5603. '/option',
  5604. '*/select',
  5605. ];
  5606. $this->assertHtml($expected, $result);
  5607. }
  5608. /**
  5609. * testDateTimeSecured method
  5610. *
  5611. * Test that datetime fields are added to protected fields list.
  5612. *
  5613. * @return void
  5614. */
  5615. public function testDateTimeSecured()
  5616. {
  5617. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5618. $this->Form->dateTime('Contact.date');
  5619. $expected = [
  5620. 'Contact.date.year',
  5621. 'Contact.date.month',
  5622. 'Contact.date.day',
  5623. 'Contact.date.hour',
  5624. 'Contact.date.minute',
  5625. ];
  5626. $this->assertEquals($expected, $this->Form->fields);
  5627. $this->Form->fields = [];
  5628. $this->Form->date('Contact.published');
  5629. $expected = [
  5630. 'Contact.published.year',
  5631. 'Contact.published.month',
  5632. 'Contact.published.day',
  5633. ];
  5634. $this->assertEquals($expected, $this->Form->fields);
  5635. }
  5636. /**
  5637. * testDateTimeSecuredDisabled method
  5638. *
  5639. * Test that datetime fields are added to protected fields list.
  5640. *
  5641. * @return void
  5642. */
  5643. public function testDateTimeSecuredDisabled()
  5644. {
  5645. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5646. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5647. $expected = [];
  5648. $this->assertEquals($expected, $this->Form->fields);
  5649. $this->Form->fields = [];
  5650. $this->Form->date('Contact.published', ['secure' => false]);
  5651. $expected = [];
  5652. $this->assertEquals($expected, $this->Form->fields);
  5653. }
  5654. /**
  5655. * testDatetimeEmpty method
  5656. *
  5657. * Test empty defaulting to true for datetime.
  5658. *
  5659. * @return void
  5660. */
  5661. public function testDatetimeEmpty()
  5662. {
  5663. extract($this->dateRegex);
  5664. $result = $this->Form->dateTime('Contact.date', [
  5665. 'timeFormat' => 12,
  5666. 'empty' => true,
  5667. 'default' => true
  5668. ]);
  5669. $expected = [
  5670. ['select' => ['name' => 'Contact[date][year]']],
  5671. $yearsRegex,
  5672. ['option' => ['value' => '']],
  5673. '/option',
  5674. '*/select',
  5675. ['select' => ['name' => 'Contact[date][month]']],
  5676. $monthsRegex,
  5677. ['option' => ['value' => '']],
  5678. '/option',
  5679. '*/select',
  5680. ['select' => ['name' => 'Contact[date][day]']],
  5681. $daysRegex,
  5682. ['option' => ['value' => '']],
  5683. '/option',
  5684. '*/select',
  5685. ['select' => ['name' => 'Contact[date][hour]']],
  5686. $hoursRegex,
  5687. ['option' => ['value' => '']],
  5688. '/option',
  5689. '*/select',
  5690. ['select' => ['name' => 'Contact[date][minute]']],
  5691. $minutesRegex,
  5692. ['option' => ['value' => '']],
  5693. '/option',
  5694. '*/select',
  5695. ['select' => ['name' => 'Contact[date][meridian]']],
  5696. $meridianRegex,
  5697. ['option' => ['value' => '']],
  5698. '/option',
  5699. '*/select'
  5700. ];
  5701. $this->assertHtml($expected, $result);
  5702. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5703. }
  5704. /**
  5705. * testDatetimeMinuteInterval method
  5706. *
  5707. * Test datetime with interval option.
  5708. *
  5709. * @return void
  5710. */
  5711. public function testDatetimeMinuteInterval()
  5712. {
  5713. extract($this->dateRegex);
  5714. $result = $this->Form->dateTime('Contact.date', [
  5715. 'interval' => 5,
  5716. 'value' => ''
  5717. ]);
  5718. $expected = [
  5719. ['select' => ['name' => 'Contact[date][year]']],
  5720. $yearsRegex,
  5721. ['option' => ['selected' => 'selected', 'value' => '']],
  5722. '/option',
  5723. '*/select',
  5724. ['select' => ['name' => 'Contact[date][month]']],
  5725. $monthsRegex,
  5726. ['option' => ['selected' => 'selected', 'value' => '']],
  5727. '/option',
  5728. '*/select',
  5729. ['select' => ['name' => 'Contact[date][day]']],
  5730. $daysRegex,
  5731. ['option' => ['selected' => 'selected', 'value' => '']],
  5732. '/option',
  5733. '*/select',
  5734. ['select' => ['name' => 'Contact[date][hour]']],
  5735. $hoursRegex,
  5736. ['option' => ['selected' => 'selected', 'value' => '']],
  5737. '/option',
  5738. '*/select',
  5739. ['select' => ['name' => 'Contact[date][minute]']],
  5740. $minutesRegex,
  5741. ['option' => ['selected' => 'selected', 'value' => '']],
  5742. '/option',
  5743. ['option' => ['value' => '00']],
  5744. '00',
  5745. '/option',
  5746. ['option' => ['value' => '05']],
  5747. '05',
  5748. '/option',
  5749. ['option' => ['value' => '10']],
  5750. '10',
  5751. '/option',
  5752. '*/select',
  5753. ];
  5754. $this->assertHtml($expected, $result);
  5755. }
  5756. /**
  5757. * testDateTimeRounding method
  5758. *
  5759. * Test dateTime with rounding.
  5760. *
  5761. * @return void
  5762. */
  5763. public function testDateTimeRounding()
  5764. {
  5765. $this->Form->request->data['Contact'] = [
  5766. 'date' => [
  5767. 'day' => '13',
  5768. 'month' => '12',
  5769. 'year' => '2010',
  5770. 'hour' => '04',
  5771. 'minute' => '19',
  5772. 'meridian' => 'AM'
  5773. ]
  5774. ];
  5775. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5776. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5777. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5778. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5779. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5780. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5781. }
  5782. /**
  5783. * testDatetimeWithDefault method
  5784. *
  5785. * Test that datetime() and default values work.
  5786. *
  5787. * @return void
  5788. */
  5789. public function testDatetimeWithDefault()
  5790. {
  5791. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5792. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5793. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5794. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5795. $result = $this->Form->dateTime('Contact.updated', [
  5796. 'default' => '2009-06-01 11:15:30'
  5797. ]);
  5798. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5799. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5800. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5801. }
  5802. /**
  5803. * testDateTimeAllZeros method
  5804. *
  5805. * Test datetime() with all zeros.
  5806. *
  5807. * @return void
  5808. */
  5809. public function testDateTimeAllZeros()
  5810. {
  5811. $result = $this->Form->dateTime('Contact.date', [
  5812. 'timeFormat' => false,
  5813. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5814. 'value' => '0000-00-00'
  5815. ]);
  5816. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5817. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5818. }
  5819. /**
  5820. * testDateTimeEmptyAsArray method
  5821. *
  5822. * @return void
  5823. */
  5824. public function testDateTimeEmptyAsArray()
  5825. {
  5826. $result = $this->Form->dateTime('Contact.date', [
  5827. 'empty' => [
  5828. 'day' => 'DAY',
  5829. 'month' => 'MONTH',
  5830. 'year' => 'YEAR',
  5831. 'hour' => 'HOUR',
  5832. 'minute' => 'MINUTE',
  5833. 'meridian' => false
  5834. ],
  5835. 'default' => true
  5836. ]);
  5837. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5838. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5839. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5840. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5841. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5842. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5843. $result = $this->Form->dateTime('Contact.date', [
  5844. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5845. 'default' => true
  5846. ]);
  5847. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5848. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5849. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5850. }
  5851. /**
  5852. * testFormDateTimeMulti method
  5853. *
  5854. * Test multiple datetime element generation.
  5855. *
  5856. * @return void
  5857. */
  5858. public function testFormDateTimeMulti()
  5859. {
  5860. extract($this->dateRegex);
  5861. $result = $this->Form->dateTime('Contact.1.updated');
  5862. $this->assertContains('Contact[1][updated][month]', $result);
  5863. $this->assertContains('Contact[1][updated][day]', $result);
  5864. $this->assertContains('Contact[1][updated][year]', $result);
  5865. $this->assertContains('Contact[1][updated][hour]', $result);
  5866. $this->assertContains('Contact[1][updated][minute]', $result);
  5867. }
  5868. /**
  5869. * testDateTimeLabelIdMatchesFirstControl method
  5870. *
  5871. * When changing the date format, the label should always focus the first select box when
  5872. * clicked.
  5873. *
  5874. * @return void
  5875. */
  5876. public function testDateTimeLabelIdMatchesFirstControl()
  5877. {
  5878. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5879. $this->assertContains('<label>Date</label>', $result);
  5880. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5881. $this->assertContains('<label>Date</label>', $result);
  5882. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5883. $this->assertContains('<label>Date</label>', $result);
  5884. }
  5885. /**
  5886. * testDateTimeSecondOptions method
  5887. *
  5888. * Test datetime second=true.
  5889. *
  5890. * @return void
  5891. */
  5892. public function testDateTimeSecondOptions()
  5893. {
  5894. $result = $this->Form->dateTime('updated', ['second' => true]);
  5895. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5896. $result = $this->Form->dateTime('updated', ['second' => []]);
  5897. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5898. $result = $this->Form->dateTime('updated', ['second' => null]);
  5899. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5900. $result = $this->Form->dateTime('updated', ['second' => false]);
  5901. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5902. }
  5903. /**
  5904. * testMonth method
  5905. *
  5906. * Test generation of a month input.
  5907. *
  5908. * @return void
  5909. */
  5910. public function testMonth()
  5911. {
  5912. $result = $this->Form->month('Model.field', ['value' => '']);
  5913. $expected = [
  5914. ['select' => ['name' => 'Model[field][month]']],
  5915. ['option' => ['value' => '', 'selected' => 'selected']],
  5916. '/option',
  5917. ['option' => ['value' => '01']],
  5918. date('F', strtotime('2008-01-01 00:00:00')),
  5919. '/option',
  5920. ['option' => ['value' => '02']],
  5921. date('F', strtotime('2008-02-01 00:00:00')),
  5922. '/option',
  5923. '*/select',
  5924. ];
  5925. $this->assertHtml($expected, $result);
  5926. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5927. $expected = [
  5928. ['select' => ['name' => 'Model[field][month]']],
  5929. ['option' => ['selected' => 'selected', 'value' => '']],
  5930. '/option',
  5931. ['option' => ['value' => '01']],
  5932. date('F', strtotime('2008-01-01 00:00:00')),
  5933. '/option',
  5934. ['option' => ['value' => '02']],
  5935. date('F', strtotime('2008-02-01 00:00:00')),
  5936. '/option',
  5937. '*/select',
  5938. ];
  5939. $this->assertHtml($expected, $result);
  5940. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5941. $expected = [
  5942. ['select' => ['name' => 'Model[field][month]']],
  5943. ['option' => ['selected' => 'selected', 'value' => '']],
  5944. '/option',
  5945. ['option' => ['value' => '01']],
  5946. '1',
  5947. '/option',
  5948. ['option' => ['value' => '02']],
  5949. '2',
  5950. '/option',
  5951. '*/select',
  5952. ];
  5953. $this->assertHtml($expected, $result);
  5954. $monthNames = [
  5955. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5956. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5957. ];
  5958. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5959. $expected = [
  5960. ['select' => ['name' => 'Model[field][month]']],
  5961. ['option' => ['value' => '']],
  5962. '/option',
  5963. ['option' => ['value' => '01', 'selected' => 'selected']],
  5964. 'Jan',
  5965. '/option',
  5966. ['option' => ['value' => '02']],
  5967. 'Feb',
  5968. '/option',
  5969. '*/select',
  5970. ];
  5971. $this->assertHtml($expected, $result);
  5972. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5973. $result = $this->Form->month('Project.release');
  5974. $expected = [
  5975. ['select' => ['name' => 'Project[release][month]']],
  5976. ['option' => ['value' => '']],
  5977. '/option',
  5978. ['option' => ['value' => '01']],
  5979. 'January',
  5980. '/option',
  5981. ['option' => ['value' => '02', 'selected' => 'selected']],
  5982. 'February',
  5983. '/option',
  5984. '*/select',
  5985. ];
  5986. $this->assertHtml($expected, $result);
  5987. $result = $this->Form->month('Contact.published', [
  5988. 'empty' => 'Published on',
  5989. ]);
  5990. $this->assertContains('Published on', $result);
  5991. }
  5992. /**
  5993. * testDay method
  5994. *
  5995. * Test generation of a day input.
  5996. *
  5997. * @return void
  5998. */
  5999. public function testDay()
  6000. {
  6001. extract($this->dateRegex);
  6002. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6003. $expected = [
  6004. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6005. ['option' => ['selected' => 'selected', 'value' => '']],
  6006. '/option',
  6007. ['option' => ['value' => '01']],
  6008. '1',
  6009. '/option',
  6010. ['option' => ['value' => '02']],
  6011. '2',
  6012. '/option',
  6013. $daysRegex,
  6014. '/select',
  6015. ];
  6016. $this->assertHtml($expected, $result);
  6017. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6018. $result = $this->Form->day('Model.field');
  6019. $expected = [
  6020. ['select' => ['name' => 'Model[field][day]']],
  6021. ['option' => ['value' => '']],
  6022. '/option',
  6023. ['option' => ['value' => '01']],
  6024. '1',
  6025. '/option',
  6026. ['option' => ['value' => '02']],
  6027. '2',
  6028. '/option',
  6029. $daysRegex,
  6030. ['option' => ['value' => '10', 'selected' => 'selected']],
  6031. '10',
  6032. '/option',
  6033. $daysRegex,
  6034. '/select',
  6035. ];
  6036. $this->assertHtml($expected, $result);
  6037. $this->Form->request->data['Model']['field'] = '';
  6038. $result = $this->Form->day('Model.field', ['value' => '10']);
  6039. $expected = [
  6040. ['select' => ['name' => 'Model[field][day]']],
  6041. ['option' => ['value' => '']],
  6042. '/option',
  6043. ['option' => ['value' => '01']],
  6044. '1',
  6045. '/option',
  6046. ['option' => ['value' => '02']],
  6047. '2',
  6048. '/option',
  6049. $daysRegex,
  6050. ['option' => ['value' => '10', 'selected' => 'selected']],
  6051. '10',
  6052. '/option',
  6053. $daysRegex,
  6054. '/select',
  6055. ];
  6056. $this->assertHtml($expected, $result);
  6057. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6058. $result = $this->Form->day('Project.release');
  6059. $expected = [
  6060. ['select' => ['name' => 'Project[release][day]']],
  6061. ['option' => ['value' => '']],
  6062. '/option',
  6063. ['option' => ['value' => '01']],
  6064. '1',
  6065. '/option',
  6066. ['option' => ['value' => '02']],
  6067. '2',
  6068. '/option',
  6069. $daysRegex,
  6070. ['option' => ['value' => '10', 'selected' => 'selected']],
  6071. '10',
  6072. '/option',
  6073. $daysRegex,
  6074. '/select',
  6075. ];
  6076. $this->assertHtml($expected, $result);
  6077. $result = $this->Form->day('Contact.published', [
  6078. 'empty' => 'Published on',
  6079. ]);
  6080. $this->assertContains('Published on', $result);
  6081. }
  6082. /**
  6083. * testMinute method
  6084. *
  6085. * Test generation of a minute input.
  6086. *
  6087. * @return void
  6088. */
  6089. public function testMinute()
  6090. {
  6091. extract($this->dateRegex);
  6092. $result = $this->Form->minute('Model.field', ['value' => '']);
  6093. $expected = [
  6094. ['select' => ['name' => 'Model[field][minute]']],
  6095. ['option' => ['selected' => 'selected', 'value' => '']],
  6096. '/option',
  6097. ['option' => ['value' => '00']],
  6098. '00',
  6099. '/option',
  6100. ['option' => ['value' => '01']],
  6101. '01',
  6102. '/option',
  6103. ['option' => ['value' => '02']],
  6104. '02',
  6105. '/option',
  6106. $minutesRegex,
  6107. '/select',
  6108. ];
  6109. $this->assertHtml($expected, $result);
  6110. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6111. $result = $this->Form->minute('Model.field');
  6112. $expected = [
  6113. ['select' => ['name' => 'Model[field][minute]']],
  6114. ['option' => ['value' => '']],
  6115. '/option',
  6116. ['option' => ['value' => '00']],
  6117. '00',
  6118. '/option',
  6119. ['option' => ['value' => '01']],
  6120. '01',
  6121. '/option',
  6122. ['option' => ['value' => '02']],
  6123. '02',
  6124. '/option',
  6125. $minutesRegex,
  6126. ['option' => ['value' => '12', 'selected' => 'selected']],
  6127. '12',
  6128. '/option',
  6129. $minutesRegex,
  6130. '/select',
  6131. ];
  6132. $this->assertHtml($expected, $result);
  6133. $this->Form->request->data['Model']['field'] = '';
  6134. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6135. $expected = [
  6136. ['select' => ['name' => 'Model[field][minute]']],
  6137. ['option' => ['selected' => 'selected', 'value' => '']],
  6138. '/option',
  6139. ['option' => ['value' => '00']],
  6140. '00',
  6141. '/option',
  6142. ['option' => ['value' => '05']],
  6143. '05',
  6144. '/option',
  6145. ['option' => ['value' => '10']],
  6146. '10',
  6147. '/option',
  6148. $minutesRegex,
  6149. '/select',
  6150. ];
  6151. $this->assertHtml($expected, $result);
  6152. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6153. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6154. $expected = [
  6155. ['select' => ['name' => 'Model[field][minute]']],
  6156. ['option' => ['value' => '']],
  6157. '/option',
  6158. ['option' => ['value' => '00']],
  6159. '00',
  6160. '/option',
  6161. ['option' => ['value' => '05']],
  6162. '05',
  6163. '/option',
  6164. ['option' => ['value' => '10', 'selected' => 'selected']],
  6165. '10',
  6166. '/option',
  6167. $minutesRegex,
  6168. '/select',
  6169. ];
  6170. $this->assertHtml($expected, $result);
  6171. }
  6172. /**
  6173. * testMeridian method
  6174. *
  6175. * Test generating an input for the meridian.
  6176. *
  6177. * @return void
  6178. */
  6179. public function testMeridian()
  6180. {
  6181. extract($this->dateRegex);
  6182. $now = new \DateTime();
  6183. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6184. $expected = [
  6185. ['select' => ['name' => 'Model[field][meridian]']],
  6186. ['option' => ['value' => '']],
  6187. '/option',
  6188. $meridianRegex,
  6189. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6190. $now->format('a'),
  6191. '/option',
  6192. '*/select'
  6193. ];
  6194. $this->assertHtml($expected, $result);
  6195. }
  6196. /**
  6197. * testHour method
  6198. *
  6199. * Test generation of an hour input.
  6200. *
  6201. * @return void
  6202. */
  6203. public function testHour()
  6204. {
  6205. extract($this->dateRegex);
  6206. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6207. $expected = [
  6208. ['select' => ['name' => 'Model[field][hour]']],
  6209. ['option' => ['selected' => 'selected', 'value' => '']],
  6210. '/option',
  6211. ['option' => ['value' => '01']],
  6212. '1',
  6213. '/option',
  6214. ['option' => ['value' => '02']],
  6215. '2',
  6216. '/option',
  6217. $hoursRegex,
  6218. '/select',
  6219. ];
  6220. $this->assertHtml($expected, $result);
  6221. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6222. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6223. $expected = [
  6224. ['select' => ['name' => 'Model[field][hour]']],
  6225. ['option' => ['value' => '']],
  6226. '/option',
  6227. ['option' => ['value' => '01']],
  6228. '1',
  6229. '/option',
  6230. ['option' => ['value' => '02']],
  6231. '2',
  6232. '/option',
  6233. $hoursRegex,
  6234. ['option' => ['value' => '12', 'selected' => 'selected']],
  6235. '12',
  6236. '/option',
  6237. '/select',
  6238. ];
  6239. $this->assertHtml($expected, $result);
  6240. $this->Form->request->data['Model']['field'] = '';
  6241. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6242. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6243. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6244. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6245. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6246. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6247. $expected = [
  6248. ['select' => ['name' => 'Model[field][hour]']],
  6249. ['option' => ['value' => '']],
  6250. '/option',
  6251. ['option' => ['value' => '00', 'selected' => 'selected']],
  6252. '0',
  6253. '/option',
  6254. ['option' => ['value' => '01']],
  6255. '1',
  6256. '/option',
  6257. ['option' => ['value' => '02']],
  6258. '2',
  6259. '/option',
  6260. $hoursRegex,
  6261. '/select',
  6262. ];
  6263. $this->assertHtml($expected, $result);
  6264. unset($this->Form->request->data['Model']['field']);
  6265. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6266. $thisHour = date('H');
  6267. $optValue = date('G');
  6268. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6269. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6270. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6271. $expected = [
  6272. ['select' => ['name' => 'Model[field][hour]']],
  6273. ['option' => ['value' => '']],
  6274. '/option',
  6275. ['option' => ['value' => '00']],
  6276. '0',
  6277. '/option',
  6278. ['option' => ['value' => '01', 'selected' => 'selected']],
  6279. '1',
  6280. '/option',
  6281. ['option' => ['value' => '02']],
  6282. '2',
  6283. '/option',
  6284. $hoursRegex,
  6285. '/select',
  6286. ];
  6287. $this->assertHtml($expected, $result);
  6288. }
  6289. /**
  6290. * testYear method
  6291. *
  6292. * Test generation of a year input.
  6293. *
  6294. * @return void
  6295. */
  6296. public function testYear()
  6297. {
  6298. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6299. $expected = [
  6300. ['select' => ['name' => 'Model[field][year]']],
  6301. ['option' => ['selected' => 'selected', 'value' => '']],
  6302. '/option',
  6303. ['option' => ['value' => '2007']],
  6304. '2007',
  6305. '/option',
  6306. ['option' => ['value' => '2006']],
  6307. '2006',
  6308. '/option',
  6309. '/select',
  6310. ];
  6311. $this->assertHtml($expected, $result);
  6312. $result = $this->Form->year('Model.field', [
  6313. 'value' => '',
  6314. 'minYear' => 2006,
  6315. 'maxYear' => 2007,
  6316. 'orderYear' => 'asc'
  6317. ]);
  6318. $expected = [
  6319. ['select' => ['name' => 'Model[field][year]']],
  6320. ['option' => ['selected' => 'selected', 'value' => '']],
  6321. '/option',
  6322. ['option' => ['value' => '2006']],
  6323. '2006',
  6324. '/option',
  6325. ['option' => ['value' => '2007']],
  6326. '2007',
  6327. '/option',
  6328. '/select',
  6329. ];
  6330. $this->assertHtml($expected, $result);
  6331. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6332. $result = $this->Form->year('Contact.published', [
  6333. 'empty' => false,
  6334. 'minYear' => 2006,
  6335. 'maxYear' => 2007,
  6336. ]);
  6337. $expected = [
  6338. ['select' => ['name' => 'Contact[published][year]']],
  6339. ['option' => ['value' => '2007']],
  6340. '2007',
  6341. '/option',
  6342. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6343. '2006',
  6344. '/option',
  6345. '/select',
  6346. ];
  6347. $this->assertHtml($expected, $result);
  6348. $result = $this->Form->year('Contact.published', [
  6349. 'empty' => 'Published on',
  6350. ]);
  6351. $this->assertContains('Published on', $result);
  6352. }
  6353. /**
  6354. * testControlDatetimePreEpoch method
  6355. *
  6356. * Test minYear being prior to the unix epoch.
  6357. *
  6358. * @return void
  6359. */
  6360. public function testControlDatetimePreEpoch()
  6361. {
  6362. $start = date('Y') - 80;
  6363. $end = date('Y') - 18;
  6364. $result = $this->Form->control('birth_year', [
  6365. 'type' => 'date',
  6366. 'label' => 'Birth Year',
  6367. 'minYear' => $start,
  6368. 'maxYear' => $end,
  6369. 'month' => false,
  6370. 'day' => false,
  6371. ]);
  6372. $this->assertContains('value="' . $start . '">' . $start, $result);
  6373. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6374. $this->assertNotContains('value="00">00', $result);
  6375. }
  6376. /**
  6377. * testYearAutoExpandRange method
  6378. *
  6379. * @return void
  6380. */
  6381. public function testYearAutoExpandRange()
  6382. {
  6383. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6384. $result = $this->Form->year('User.birthday');
  6385. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6386. $result = $matches[1];
  6387. $expected = range(date('Y') + 5, 1930);
  6388. $this->assertEquals($expected, $result);
  6389. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6390. $result = $this->Form->year('Project.release');
  6391. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6392. $result = $matches[1];
  6393. $expected = range(2050, date('Y') - 5);
  6394. $this->assertEquals($expected, $result);
  6395. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6396. $result = $this->Form->year('Project.release', [
  6397. 'minYear' => 1890,
  6398. 'maxYear' => 1900
  6399. ]);
  6400. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6401. $result = $matches[1];
  6402. $expected = range(1900, 1881);
  6403. $this->assertEquals($expected, $result);
  6404. }
  6405. /**
  6406. * testControlDate method
  6407. *
  6408. * Test that control() accepts the type of date and passes options in.
  6409. *
  6410. * @return void
  6411. */
  6412. public function testControlDate()
  6413. {
  6414. $this->Form->request->data = [
  6415. 'month_year' => ['month' => date('m')],
  6416. ];
  6417. $this->Form->create($this->article);
  6418. $result = $this->Form->control('month_year', [
  6419. 'label' => false,
  6420. 'type' => 'date',
  6421. 'minYear' => 2006,
  6422. 'maxYear' => 2008
  6423. ]);
  6424. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6425. $this->assertNotContains('value="2008" selected="selected"', $result);
  6426. }
  6427. /**
  6428. * testControlLabelFalse method
  6429. *
  6430. * Test the label option being set to false.
  6431. *
  6432. * @return void
  6433. */
  6434. public function testControlLabelFalse()
  6435. {
  6436. $this->Form->create($this->article);
  6437. $result = $this->Form->control('title', ['label' => false]);
  6438. $expected = [
  6439. 'div' => ['class' => 'input text required'],
  6440. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6441. '/div'
  6442. ];
  6443. $this->assertHtml($expected, $result);
  6444. }
  6445. /**
  6446. * testControlDateMaxYear method
  6447. *
  6448. * Let's say we want to only allow users born from 2006 to 2008 to register
  6449. * This being the first signup page, we still don't have any data.
  6450. *
  6451. * @return void
  6452. */
  6453. public function testControlDateMaxYear()
  6454. {
  6455. $this->Form->request->data = [];
  6456. $this->Form->create($this->article);
  6457. $result = $this->Form->control('birthday', [
  6458. 'label' => false,
  6459. 'type' => 'date',
  6460. 'minYear' => 2006,
  6461. 'maxYear' => 2008,
  6462. 'default' => true
  6463. ]);
  6464. $this->assertContains('value="2008" selected="selected"', $result);
  6465. $this->assertContains('value="2006"', $result);
  6466. $this->assertNotContains('value="2005"', $result);
  6467. $this->assertNotContains('value="2009"', $result);
  6468. }
  6469. /**
  6470. * testTextArea method
  6471. *
  6472. * Test generation of a textarea input.
  6473. *
  6474. * @return void
  6475. */
  6476. public function testTextArea()
  6477. {
  6478. $this->Form->request->data = ['field' => 'some test data'];
  6479. $result = $this->Form->textarea('field');
  6480. $expected = [
  6481. 'textarea' => ['name' => 'field', 'rows' => 5],
  6482. 'some test data',
  6483. '/textarea',
  6484. ];
  6485. $this->assertHtml($expected, $result);
  6486. $result = $this->Form->textarea('user.bio');
  6487. $expected = [
  6488. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6489. '/textarea',
  6490. ];
  6491. $this->assertHtml($expected, $result);
  6492. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6493. $result = $this->Form->textarea('field');
  6494. $expected = [
  6495. 'textarea' => ['name' => 'field', 'rows' => 5],
  6496. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6497. '/textarea',
  6498. ];
  6499. $this->assertHtml($expected, $result);
  6500. $this->Form->request->data = [
  6501. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6502. ];
  6503. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6504. $expected = [
  6505. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6506. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6507. '/textarea',
  6508. ];
  6509. $this->assertHtml($expected, $result);
  6510. $result = $this->Form->textarea('0.OtherModel.field');
  6511. $expected = [
  6512. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6513. '/textarea'
  6514. ];
  6515. $this->assertHtml($expected, $result);
  6516. }
  6517. /**
  6518. * testTextAreaWithStupidCharacters method
  6519. *
  6520. * Test text area with non-ascii characters.
  6521. *
  6522. * @return void
  6523. */
  6524. public function testTextAreaWithStupidCharacters()
  6525. {
  6526. $result = $this->Form->textarea('Post.content', [
  6527. 'value' => "GREAT®",
  6528. 'rows' => '15',
  6529. 'cols' => '75'
  6530. ]);
  6531. $expected = [
  6532. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6533. 'GREAT®',
  6534. '/textarea',
  6535. ];
  6536. $this->assertHtml($expected, $result);
  6537. }
  6538. /**
  6539. * testTextAreaMaxLength method
  6540. *
  6541. * Test textareas maxlength read from schema.
  6542. *
  6543. * @return void
  6544. */
  6545. public function testTextAreaMaxLength()
  6546. {
  6547. $this->Form->create([
  6548. 'schema' => [
  6549. 'stuff' => ['type' => 'string', 'length' => 10],
  6550. ]
  6551. ]);
  6552. $result = $this->Form->control('other', ['type' => 'textarea']);
  6553. $expected = [
  6554. 'div' => ['class' => 'input textarea'],
  6555. 'label' => ['for' => 'other'],
  6556. 'Other',
  6557. '/label',
  6558. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6559. '/textarea',
  6560. '/div'
  6561. ];
  6562. $this->assertHtml($expected, $result);
  6563. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6564. $expected = [
  6565. 'div' => ['class' => 'input textarea'],
  6566. 'label' => ['for' => 'stuff'],
  6567. 'Stuff',
  6568. '/label',
  6569. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6570. '/textarea',
  6571. '/div'
  6572. ];
  6573. $this->assertHtml($expected, $result);
  6574. }
  6575. /**
  6576. * testHiddenField method
  6577. *
  6578. * Test generation of a hidden input.
  6579. *
  6580. * @return void
  6581. */
  6582. public function testHiddenField()
  6583. {
  6584. $this->article['errors'] = [
  6585. 'field' => true
  6586. ];
  6587. $this->Form->request->data['field'] = 'test';
  6588. $this->Form->create($this->article);
  6589. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6590. $expected = [
  6591. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6592. $this->assertHtml($expected, $result);
  6593. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6594. $expected = [
  6595. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6596. ];
  6597. $this->assertHtml($expected, $result);
  6598. }
  6599. /**
  6600. * testFileUploadField method
  6601. *
  6602. * Test generation of a file upload input.
  6603. *
  6604. * @return void
  6605. */
  6606. public function testFileUploadField()
  6607. {
  6608. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6609. $result = $this->Form->file('Model.upload');
  6610. $this->assertHtml($expected, $result);
  6611. $this->Form->request->data['Model']['upload'] = [
  6612. 'name' => '', 'type' => '', 'tmp_name' => '',
  6613. 'error' => 4, 'size' => 0
  6614. ];
  6615. $result = $this->Form->file('Model.upload');
  6616. $this->assertHtml($expected, $result);
  6617. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6618. $result = $this->Form->file('Model.upload');
  6619. $this->assertHtml($expected, $result);
  6620. }
  6621. /**
  6622. * testFileUploadOnOtherModel method
  6623. *
  6624. * Test File upload input on a model not used in create().
  6625. *
  6626. * @return void
  6627. */
  6628. public function testFileUploadOnOtherModel()
  6629. {
  6630. $this->Form->create($this->article, ['type' => 'file']);
  6631. $result = $this->Form->file('ValidateProfile.city');
  6632. $expected = [
  6633. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6634. ];
  6635. $this->assertHtml($expected, $result);
  6636. }
  6637. /**
  6638. * testButton method
  6639. *
  6640. * Test generation of a form button.
  6641. *
  6642. * @return void
  6643. */
  6644. public function testButton()
  6645. {
  6646. $result = $this->Form->button('Hi');
  6647. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6648. $this->assertHtml($expected, $result);
  6649. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6650. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6651. $this->assertHtml($expected, $result);
  6652. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6653. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6654. $this->assertHtml($expected, $result);
  6655. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6656. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6657. $this->assertHtml($expected, $result);
  6658. $result = $this->Form->button('No type', ['type' => false]);
  6659. $expected = ['button' => [], 'No type', '/button'];
  6660. $this->assertHtml($expected, $result);
  6661. $result = $this->Form->button('Upload Text', [
  6662. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6663. 'escape' => false
  6664. ]);
  6665. $this->assertNotRegExp('/\&039/', $result);
  6666. }
  6667. /**
  6668. * testButtonUnlockedByDefault method
  6669. *
  6670. * Test that button() makes unlocked fields by default.
  6671. *
  6672. * @return void
  6673. */
  6674. public function testButtonUnlockedByDefault()
  6675. {
  6676. $this->Form->request->params['_csrfToken'] = 'secured';
  6677. $this->Form->button('Save', ['name' => 'save']);
  6678. $this->Form->button('Clear');
  6679. $result = $this->Form->unlockField();
  6680. $this->assertEquals(['save'], $result);
  6681. }
  6682. /**
  6683. * testPostButton method
  6684. *
  6685. * @return void
  6686. */
  6687. public function testPostButton()
  6688. {
  6689. $result = $this->Form->postButton('Hi', '/controller/action');
  6690. $expected = [
  6691. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6692. 'div' => ['style' => 'display:none;'],
  6693. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6694. '/div',
  6695. 'button' => ['type' => 'submit'],
  6696. 'Hi',
  6697. '/button',
  6698. '/form'
  6699. ];
  6700. $this->assertHtml($expected, $result);
  6701. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6702. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6703. }
  6704. /**
  6705. * testPostButtonMethodType method
  6706. *
  6707. * @return void
  6708. */
  6709. public function testPostButtonMethodType()
  6710. {
  6711. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6712. $expected = [
  6713. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6714. 'div' => ['style' => 'display:none;'],
  6715. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6716. '/div',
  6717. 'button' => ['type' => 'submit'],
  6718. 'Hi',
  6719. '/button',
  6720. '/form'
  6721. ];
  6722. $this->assertHtml($expected, $result);
  6723. }
  6724. /**
  6725. * testPostButtonFormOptions method
  6726. *
  6727. * @return void
  6728. */
  6729. public function testPostButtonFormOptions()
  6730. {
  6731. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6732. $expected = [
  6733. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6734. 'div' => ['style' => 'display:none;'],
  6735. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6736. '/div',
  6737. 'button' => ['type' => 'submit'],
  6738. 'Hi',
  6739. '/button',
  6740. '/form'
  6741. ];
  6742. $this->assertHtml($expected, $result);
  6743. }
  6744. /**
  6745. * testPostButtonNestedData method
  6746. *
  6747. * Test using postButton with N dimensional data.
  6748. *
  6749. * @return void
  6750. */
  6751. public function testPostButtonNestedData()
  6752. {
  6753. $data = [
  6754. 'one' => [
  6755. 'two' => [
  6756. 3, 4, 5
  6757. ]
  6758. ]
  6759. ];
  6760. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6761. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6762. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6763. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6764. }
  6765. /**
  6766. * testSecurePostButton method
  6767. *
  6768. * Test that postButton adds _Token fields.
  6769. *
  6770. * @return void
  6771. */
  6772. public function testSecurePostButton()
  6773. {
  6774. $this->Form->request->params['_csrfToken'] = 'testkey';
  6775. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6776. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6777. $tokenDebug = urlencode(json_encode([
  6778. '/posts/delete/1',
  6779. [],
  6780. []
  6781. ]));
  6782. $expected = [
  6783. 'form' => [
  6784. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6785. ],
  6786. ['div' => ['style' => 'display:none;']],
  6787. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6788. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6789. '/div',
  6790. 'button' => ['type' => 'submit'],
  6791. 'Delete',
  6792. '/button',
  6793. ['div' => ['style' => 'display:none;']],
  6794. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6795. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6796. ['input' => [
  6797. 'type' => 'hidden',
  6798. 'name' => '_Token[debug]',
  6799. 'value' => $tokenDebug,
  6800. 'autocomplete' => 'off',
  6801. ]],
  6802. '/div',
  6803. '/form',
  6804. ];
  6805. $this->assertHtml($expected, $result);
  6806. }
  6807. /**
  6808. * testPostLink method
  6809. *
  6810. * @return void
  6811. */
  6812. public function testPostLink()
  6813. {
  6814. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6815. $expected = [
  6816. 'form' => [
  6817. 'method' => 'post', 'action' => '/posts/delete/1',
  6818. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6819. ],
  6820. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6821. '/form',
  6822. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6823. 'Delete',
  6824. '/a'
  6825. ];
  6826. $this->assertHtml($expected, $result);
  6827. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6828. $expected = [
  6829. 'form' => [
  6830. 'method' => 'post', 'action' => '/posts/delete/1',
  6831. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6832. ],
  6833. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6834. '/form',
  6835. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6836. 'Delete',
  6837. '/a'
  6838. ];
  6839. $this->assertHtml($expected, $result);
  6840. $result = $this->Form->postLink(
  6841. 'Delete',
  6842. '/posts/delete/1',
  6843. ['target' => '_blank', 'class' => 'btn btn-danger']
  6844. );
  6845. $expected = [
  6846. 'form' => [
  6847. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6848. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6849. ],
  6850. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6851. '/form',
  6852. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6853. 'Delete',
  6854. '/a'
  6855. ];
  6856. $this->assertHtml($expected, $result);
  6857. }
  6858. /**
  6859. * testPostLinkWithConfirm method
  6860. *
  6861. * Test the confirm option for postLink().
  6862. *
  6863. * @return void
  6864. */
  6865. public function testPostLinkWithConfirm()
  6866. {
  6867. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6868. $expected = [
  6869. 'form' => [
  6870. 'method' => 'post', 'action' => '/posts/delete/1',
  6871. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6872. ],
  6873. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6874. '/form',
  6875. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6876. 'Delete',
  6877. '/a'
  6878. ];
  6879. $this->assertHtml($expected, $result);
  6880. $result = $this->Form->postLink(
  6881. 'Delete',
  6882. '/posts/delete/1',
  6883. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6884. );
  6885. $expected = [
  6886. 'form' => [
  6887. 'method' => 'post', 'action' => '/posts/delete/1',
  6888. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6889. ],
  6890. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6891. '/form',
  6892. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6893. 'Delete',
  6894. '/a'
  6895. ];
  6896. $this->assertHtml($expected, $result);
  6897. }
  6898. /**
  6899. * testPostLinkWithQuery method
  6900. *
  6901. * Test postLink() with query string args.
  6902. *
  6903. * @return void
  6904. */
  6905. public function testPostLinkWithQuery()
  6906. {
  6907. $result = $this->Form->postLink(
  6908. 'Delete',
  6909. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6910. );
  6911. $expected = [
  6912. 'form' => [
  6913. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6914. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6915. ],
  6916. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6917. '/form',
  6918. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6919. 'Delete',
  6920. '/a'
  6921. ];
  6922. $this->assertHtml($expected, $result);
  6923. }
  6924. /**
  6925. * testPostLinkWithData method
  6926. *
  6927. * Test postLink with additional data.
  6928. *
  6929. * @return void
  6930. */
  6931. public function testPostLinkWithData()
  6932. {
  6933. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6934. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6935. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6936. $this->Form->create($entity);
  6937. $this->Form->end();
  6938. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6939. $this->assertContains(
  6940. '<input type="hidden" name="name" value="show"',
  6941. $result,
  6942. 'should not contain entity data.'
  6943. );
  6944. }
  6945. /**
  6946. * testPostLinkSecurityHash method
  6947. *
  6948. * Test that security hashes for postLink include the url.
  6949. *
  6950. * @return void
  6951. */
  6952. public function testPostLinkSecurityHash()
  6953. {
  6954. $hash = Security::hash(
  6955. '/posts/delete/1' .
  6956. serialize(['id' => '1']) .
  6957. '' .
  6958. Security::salt()
  6959. );
  6960. $hash .= '%3Aid';
  6961. $this->Form->request->params['_Token']['key'] = 'test';
  6962. $result = $this->Form->postLink(
  6963. 'Delete',
  6964. '/posts/delete/1',
  6965. ['data' => ['id' => 1]]
  6966. );
  6967. $tokenDebug = urlencode(json_encode([
  6968. '/posts/delete/1',
  6969. [
  6970. 'id' => 1
  6971. ],
  6972. []
  6973. ]));
  6974. $expected = [
  6975. 'form' => [
  6976. 'method' => 'post', 'action' => '/posts/delete/1',
  6977. 'name', 'style' => 'display:none;'
  6978. ],
  6979. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6980. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6981. 'div' => ['style' => 'display:none;'],
  6982. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  6983. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6984. ['input' => [
  6985. 'type' => 'hidden',
  6986. 'name' => '_Token[debug]',
  6987. 'value' => $tokenDebug,
  6988. 'autocomplete' => 'off',
  6989. ]],
  6990. '/div',
  6991. '/form',
  6992. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6993. 'Delete',
  6994. '/a'
  6995. ];
  6996. $this->assertHtml($expected, $result);
  6997. }
  6998. /**
  6999. * testPostLinkSecurityHashBlockMode method
  7000. *
  7001. * Test that postLink doesn't modify the fields in the containing form.
  7002. *
  7003. * postLink() calls inside open forms should not modify the field list
  7004. * for the form.
  7005. *
  7006. * @return void
  7007. */
  7008. public function testPostLinkSecurityHashBlockMode()
  7009. {
  7010. $hash = Security::hash(
  7011. '/posts/delete/1' .
  7012. serialize([]) .
  7013. '' .
  7014. Security::salt()
  7015. );
  7016. $hash .= '%3A';
  7017. $this->Form->request->params['_Token']['key'] = 'test';
  7018. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7019. $this->Form->control('title');
  7020. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7021. $result = $this->View->fetch('postLink');
  7022. $this->assertEquals(['title'], $this->Form->fields);
  7023. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7024. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7025. }
  7026. /**
  7027. * testPostLinkSecurityHashNoDebugMode method
  7028. *
  7029. * Test that security does not include debug token if debug is false.
  7030. *
  7031. * @return void
  7032. */
  7033. public function testPostLinkSecurityHashNoDebugMode()
  7034. {
  7035. Configure::write('debug', false);
  7036. $hash = Security::hash(
  7037. '/posts/delete/1' .
  7038. serialize(['id' => '1']) .
  7039. '' .
  7040. Security::salt()
  7041. );
  7042. $hash .= '%3Aid';
  7043. $this->Form->request->params['_Token']['key'] = 'test';
  7044. $result = $this->Form->postLink(
  7045. 'Delete',
  7046. '/posts/delete/1',
  7047. ['data' => ['id' => 1]]
  7048. );
  7049. $expected = [
  7050. 'form' => [
  7051. 'method' => 'post', 'action' => '/posts/delete/1',
  7052. 'name', 'style' => 'display:none;'
  7053. ],
  7054. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7055. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7056. 'div' => ['style' => 'display:none;'],
  7057. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7058. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7059. '/div',
  7060. '/form',
  7061. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7062. 'Delete',
  7063. '/a'
  7064. ];
  7065. $this->assertHtml($expected, $result);
  7066. }
  7067. /**
  7068. * testPostLinkNestedData method
  7069. *
  7070. * Test using postLink with N dimensional data.
  7071. *
  7072. * @return void
  7073. */
  7074. public function testPostLinkNestedData()
  7075. {
  7076. $data = [
  7077. 'one' => [
  7078. 'two' => [
  7079. 3, 4, 5
  7080. ]
  7081. ]
  7082. ];
  7083. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7084. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7085. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7086. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7087. }
  7088. /**
  7089. * testPostLinkAfterGetForm method
  7090. *
  7091. * Test creating postLinks after a GET form.
  7092. *
  7093. * @return void
  7094. */
  7095. public function testPostLinkAfterGetForm()
  7096. {
  7097. $this->Form->request->params['_csrfToken'] = 'testkey';
  7098. $this->Form->request->params['_Token'] = 'val';
  7099. $this->Form->create($this->article, ['type' => 'get']);
  7100. $this->Form->end();
  7101. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7102. $tokenDebug = urlencode(json_encode([
  7103. '/posts/delete/1',
  7104. [],
  7105. []
  7106. ]));
  7107. $expected = [
  7108. 'form' => [
  7109. 'method' => 'post', 'action' => '/posts/delete/1',
  7110. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7111. ],
  7112. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7113. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7114. 'div' => ['style' => 'display:none;'],
  7115. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7116. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7117. ['input' => [
  7118. 'type' => 'hidden', 'name' => '_Token[debug]',
  7119. 'value' => $tokenDebug,
  7120. 'autocomplete' => 'off'
  7121. ]],
  7122. '/div',
  7123. '/form',
  7124. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7125. 'Delete',
  7126. '/a'
  7127. ];
  7128. $this->assertHtml($expected, $result);
  7129. }
  7130. /**
  7131. * testPostLinkFormBuffer method
  7132. *
  7133. * Test that postLink adds form tags to view block.
  7134. *
  7135. * @return void
  7136. */
  7137. public function testPostLinkFormBuffer()
  7138. {
  7139. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7140. $expected = [
  7141. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7142. 'Delete',
  7143. '/a'
  7144. ];
  7145. $this->assertHtml($expected, $result);
  7146. $result = $this->View->fetch('postLink');
  7147. $expected = [
  7148. 'form' => [
  7149. 'method' => 'post', 'action' => '/posts/delete/1',
  7150. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7151. ],
  7152. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7153. '/form'
  7154. ];
  7155. $this->assertHtml($expected, $result);
  7156. $result = $this->Form->postLink(
  7157. 'Delete',
  7158. '/posts/delete/2',
  7159. ['block' => true, 'method' => 'DELETE']
  7160. );
  7161. $expected = [
  7162. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7163. 'Delete',
  7164. '/a'
  7165. ];
  7166. $this->assertHtml($expected, $result);
  7167. $result = $this->View->fetch('postLink');
  7168. $expected = [
  7169. 'form' => [
  7170. 'method' => 'post', 'action' => '/posts/delete/1',
  7171. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7172. ],
  7173. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7174. '/form',
  7175. [
  7176. 'form' => [
  7177. 'method' => 'post', 'action' => '/posts/delete/2',
  7178. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7179. ],
  7180. ],
  7181. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7182. '/form'
  7183. ];
  7184. $this->assertHtml($expected, $result);
  7185. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7186. $expected = [
  7187. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7188. 'Delete',
  7189. '/a'
  7190. ];
  7191. $this->assertHtml($expected, $result);
  7192. $result = $this->View->fetch('foobar');
  7193. $expected = [
  7194. 'form' => [
  7195. 'method' => 'post', 'action' => '/posts/delete/1',
  7196. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7197. ],
  7198. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7199. '/form'
  7200. ];
  7201. $this->assertHtml($expected, $result);
  7202. }
  7203. /**
  7204. * testSubmitButton method
  7205. *
  7206. * @return void
  7207. */
  7208. public function testSubmitButton()
  7209. {
  7210. $result = $this->Form->submit('');
  7211. $expected = [
  7212. 'div' => ['class' => 'submit'],
  7213. 'input' => ['type' => 'submit', 'value' => ''],
  7214. '/div'
  7215. ];
  7216. $this->assertHtml($expected, $result);
  7217. $result = $this->Form->submit('Test Submit');
  7218. $expected = [
  7219. 'div' => ['class' => 'submit'],
  7220. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7221. '/div'
  7222. ];
  7223. $this->assertHtml($expected, $result);
  7224. $result = $this->Form->submit('Next >');
  7225. $expected = [
  7226. 'div' => ['class' => 'submit'],
  7227. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7228. '/div'
  7229. ];
  7230. $this->assertHtml($expected, $result);
  7231. $result = $this->Form->submit('Next >', ['escape' => false]);
  7232. $expected = [
  7233. 'div' => ['class' => 'submit'],
  7234. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7235. '/div'
  7236. ];
  7237. $this->assertHtml($expected, $result);
  7238. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7239. $expected = [
  7240. 'div' => ['class' => 'submit'],
  7241. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7242. '/div'
  7243. ];
  7244. $this->assertHtml($expected, $result);
  7245. }
  7246. /**
  7247. * testSubmitImage method
  7248. *
  7249. * Test image submit types.
  7250. *
  7251. * @return void
  7252. */
  7253. public function testSubmitImage()
  7254. {
  7255. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7256. $expected = [
  7257. 'div' => ['class' => 'submit'],
  7258. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7259. '/div'
  7260. ];
  7261. $this->assertHtml($expected, $result);
  7262. $result = $this->Form->submit('/relative/cake.power.gif');
  7263. $expected = [
  7264. 'div' => ['class' => 'submit'],
  7265. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7266. '/div'
  7267. ];
  7268. $this->assertHtml($expected, $result);
  7269. $result = $this->Form->submit('cake.power.gif');
  7270. $expected = [
  7271. 'div' => ['class' => 'submit'],
  7272. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7273. '/div'
  7274. ];
  7275. $this->assertHtml($expected, $result);
  7276. $result = $this->Form->submit('Not.an.image');
  7277. $expected = [
  7278. 'div' => ['class' => 'submit'],
  7279. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7280. '/div'
  7281. ];
  7282. $this->assertHtml($expected, $result);
  7283. }
  7284. /**
  7285. * testSubmitUnlockedByDefault method
  7286. *
  7287. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7288. * be submitted at a time.
  7289. *
  7290. * @return void
  7291. */
  7292. public function testSubmitUnlockedByDefault()
  7293. {
  7294. $this->Form->request->params['_Token'] = 'secured';
  7295. $this->Form->submit('Go go');
  7296. $this->Form->submit('Save', ['name' => 'save']);
  7297. $result = $this->Form->unlockField();
  7298. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7299. }
  7300. /**
  7301. * testSubmitImageTimestamp method
  7302. *
  7303. * Test submit image with timestamps.
  7304. *
  7305. * @return void
  7306. */
  7307. public function testSubmitImageTimestamp()
  7308. {
  7309. Configure::write('Asset.timestamp', 'force');
  7310. $result = $this->Form->submit('cake.power.gif');
  7311. $expected = [
  7312. 'div' => ['class' => 'submit'],
  7313. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7314. '/div'
  7315. ];
  7316. $this->assertHtml($expected, $result);
  7317. }
  7318. /**
  7319. * testDateTimeWithGetForms method
  7320. *
  7321. * Test that datetime() works with GET style forms.
  7322. *
  7323. * @return void
  7324. */
  7325. public function testDateTimeWithGetForms()
  7326. {
  7327. extract($this->dateRegex);
  7328. $this->Form->create($this->article, ['type' => 'get']);
  7329. $result = $this->Form->datetime('created');
  7330. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7331. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7332. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7333. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7334. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7335. }
  7336. /**
  7337. * testForMagicControlNonExistingNorValidated method
  7338. *
  7339. * @return void
  7340. */
  7341. public function testForMagicControlNonExistingNorValidated()
  7342. {
  7343. $this->Form->create($this->article);
  7344. $this->Form->templates(['inputContainer' => '{{content}}']);
  7345. $result = $this->Form->control('non_existing_nor_validated');
  7346. $expected = [
  7347. 'label' => ['for' => 'non-existing-nor-validated'],
  7348. 'Non Existing Nor Validated',
  7349. '/label',
  7350. 'input' => [
  7351. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7352. 'id' => 'non-existing-nor-validated'
  7353. ]
  7354. ];
  7355. $this->assertHtml($expected, $result);
  7356. $result = $this->Form->control('non_existing_nor_validated', [
  7357. 'val' => 'my value'
  7358. ]);
  7359. $expected = [
  7360. 'label' => ['for' => 'non-existing-nor-validated'],
  7361. 'Non Existing Nor Validated',
  7362. '/label',
  7363. 'input' => [
  7364. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7365. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7366. ]
  7367. ];
  7368. $this->assertHtml($expected, $result);
  7369. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7370. $result = $this->Form->control('non_existing_nor_validated');
  7371. $expected = [
  7372. 'label' => ['for' => 'non-existing-nor-validated'],
  7373. 'Non Existing Nor Validated',
  7374. '/label',
  7375. 'input' => [
  7376. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7377. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7378. ]
  7379. ];
  7380. $this->assertHtml($expected, $result);
  7381. }
  7382. /**
  7383. * testFormMagicControlLabel method
  7384. *
  7385. * @return void
  7386. */
  7387. public function testFormMagicControlLabel()
  7388. {
  7389. TableRegistry::get('Contacts', [
  7390. 'className' => __NAMESPACE__ . '\ContactsTable'
  7391. ]);
  7392. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7393. $this->Form->templates(['inputContainer' => '{{content}}']);
  7394. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7395. $expected = [
  7396. 'label' => ['for' => 'contacts-name'],
  7397. 'My label',
  7398. '/label',
  7399. 'input' => [
  7400. 'type' => 'text', 'name' => 'Contacts[name]',
  7401. 'id' => 'contacts-name', 'maxlength' => '255'
  7402. ]
  7403. ];
  7404. $this->assertHtml($expected, $result);
  7405. $result = $this->Form->control('name', [
  7406. 'label' => ['class' => 'mandatory']
  7407. ]);
  7408. $expected = [
  7409. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7410. 'Name',
  7411. '/label',
  7412. 'input' => [
  7413. 'type' => 'text', 'name' => 'name',
  7414. 'id' => 'name', 'maxlength' => '255'
  7415. ]
  7416. ];
  7417. $this->assertHtml($expected, $result);
  7418. $result = $this->Form->control('name', [
  7419. 'div' => false,
  7420. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7421. ]);
  7422. $expected = [
  7423. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7424. 'My label',
  7425. '/label',
  7426. 'input' => [
  7427. 'type' => 'text', 'name' => 'name',
  7428. 'id' => 'name', 'maxlength' => '255'
  7429. ]
  7430. ];
  7431. $this->assertHtml($expected, $result);
  7432. $result = $this->Form->control('Contact.name', [
  7433. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7434. ]);
  7435. $expected = [
  7436. 'label' => ['for' => 'my_id'],
  7437. 'Name',
  7438. '/label',
  7439. 'input' => [
  7440. 'type' => 'text', 'name' => 'Contact[name]',
  7441. 'id' => 'my_id', 'maxlength' => '255'
  7442. ]
  7443. ];
  7444. $this->assertHtml($expected, $result);
  7445. $result = $this->Form->control('1.id');
  7446. $expected = ['input' => [
  7447. 'type' => 'hidden', 'name' => '1[id]',
  7448. 'id' => '1-id'
  7449. ]];
  7450. $this->assertHtml($expected, $result);
  7451. $result = $this->Form->control("1.name");
  7452. $expected = [
  7453. 'label' => ['for' => '1-name'],
  7454. 'Name',
  7455. '/label',
  7456. 'input' => [
  7457. 'type' => 'text', 'name' => '1[name]',
  7458. 'id' => '1-name', 'maxlength' => '255'
  7459. ]
  7460. ];
  7461. $this->assertHtml($expected, $result);
  7462. }
  7463. /**
  7464. * testFormEnd method
  7465. *
  7466. * @return void
  7467. */
  7468. public function testFormEnd()
  7469. {
  7470. $this->assertEquals('</form>', $this->Form->end());
  7471. }
  7472. /**
  7473. * testMultiRecordForm method
  7474. *
  7475. * Test the generation of fields for a multi record form.
  7476. *
  7477. * @return void
  7478. */
  7479. public function testMultiRecordForm()
  7480. {
  7481. $this->loadFixtures('Articles', 'Comments');
  7482. $articles = TableRegistry::get('Articles');
  7483. $articles->hasMany('Comments');
  7484. $comment = new Entity(['comment' => 'Value']);
  7485. $article = new Article(['comments' => [$comment]]);
  7486. $this->Form->create([$article]);
  7487. $result = $this->Form->control('0.comments.1.comment');
  7488. //@codingStandardsIgnoreStart
  7489. $expected = [
  7490. 'div' => ['class' => 'input textarea'],
  7491. 'label' => ['for' => '0-comments-1-comment'],
  7492. 'Comment',
  7493. '/label',
  7494. 'textarea' => [
  7495. 'name',
  7496. 'id' => '0-comments-1-comment',
  7497. 'rows' => 5
  7498. ],
  7499. '/textarea',
  7500. '/div'
  7501. ];
  7502. //@codingStandardsIgnoreEnd
  7503. $this->assertHtml($expected, $result);
  7504. $result = $this->Form->control('0.comments.0.comment');
  7505. //@codingStandardsIgnoreStart
  7506. $expected = [
  7507. 'div' => ['class' => 'input textarea'],
  7508. 'label' => ['for' => '0-comments-0-comment'],
  7509. 'Comment',
  7510. '/label',
  7511. 'textarea' => [
  7512. 'name',
  7513. 'id' => '0-comments-0-comment',
  7514. 'rows' => 5
  7515. ],
  7516. 'Value',
  7517. '/textarea',
  7518. '/div'
  7519. ];
  7520. //@codingStandardsIgnoreEnd
  7521. $this->assertHtml($expected, $result);
  7522. $comment->errors('comment', ['Not valid']);
  7523. $result = $this->Form->control('0.comments.0.comment');
  7524. //@codingStandardsIgnoreStart
  7525. $expected = [
  7526. 'div' => ['class' => 'input textarea error'],
  7527. 'label' => ['for' => '0-comments-0-comment'],
  7528. 'Comment',
  7529. '/label',
  7530. 'textarea' => [
  7531. 'name',
  7532. 'class' => 'form-error',
  7533. 'id' => '0-comments-0-comment',
  7534. 'rows' => 5
  7535. ],
  7536. 'Value',
  7537. '/textarea',
  7538. ['div' => ['class' => 'error-message']],
  7539. 'Not valid',
  7540. '/div',
  7541. '/div'
  7542. ];
  7543. //@codingStandardsIgnoreEnd
  7544. $this->assertHtml($expected, $result);
  7545. TableRegistry::get('Comments')
  7546. ->validator('default')
  7547. ->allowEmpty('comment', false);
  7548. $result = $this->Form->control('0.comments.1.comment');
  7549. //@codingStandardsIgnoreStart
  7550. $expected = [
  7551. 'div' => ['class' => 'input textarea required'],
  7552. 'label' => ['for' => '0-comments-1-comment'],
  7553. 'Comment',
  7554. '/label',
  7555. 'textarea' => [
  7556. 'name',
  7557. 'required' => 'required',
  7558. 'id' => '0-comments-1-comment',
  7559. 'rows' => 5
  7560. ],
  7561. '/textarea',
  7562. '/div'
  7563. ];
  7564. //@codingStandardsIgnoreEnd
  7565. $this->assertHtml($expected, $result);
  7566. }
  7567. /**
  7568. * testHtml5Controls method
  7569. *
  7570. * Test that some html5 inputs + FormHelper::__call() work.
  7571. *
  7572. * @return void
  7573. */
  7574. public function testHtml5Controls()
  7575. {
  7576. $result = $this->Form->email('User.email');
  7577. $expected = [
  7578. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7579. ];
  7580. $this->assertHtml($expected, $result);
  7581. $result = $this->Form->search('User.query');
  7582. $expected = [
  7583. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7584. ];
  7585. $this->assertHtml($expected, $result);
  7586. $result = $this->Form->search('User.query', ['value' => 'test']);
  7587. $expected = [
  7588. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7589. ];
  7590. $this->assertHtml($expected, $result);
  7591. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7592. $expected = [
  7593. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7594. ];
  7595. $this->assertHtml($expected, $result);
  7596. }
  7597. /**
  7598. * testHtml5ControlWithControl method
  7599. *
  7600. * Test accessing html5 inputs through control().
  7601. *
  7602. * @return void
  7603. */
  7604. public function testHtml5ControlWithControl()
  7605. {
  7606. $this->Form->create();
  7607. $this->Form->templates(['inputContainer' => '{{content}}']);
  7608. $result = $this->Form->control('website', [
  7609. 'type' => 'url',
  7610. 'val' => 'http://domain.tld',
  7611. 'label' => false
  7612. ]);
  7613. $expected = [
  7614. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7615. ];
  7616. $this->assertHtml($expected, $result);
  7617. }
  7618. /**
  7619. * testHtml5ControlException method
  7620. *
  7621. * Test errors when field name is missing.
  7622. *
  7623. * @expectedException \Cake\Core\Exception\Exception
  7624. * @return void
  7625. */
  7626. public function testHtml5ControlException()
  7627. {
  7628. $this->Form->email();
  7629. }
  7630. /**
  7631. * testRequiredAttribute method
  7632. *
  7633. * Tests that formhelper sets required attributes.
  7634. *
  7635. * @return void
  7636. */
  7637. public function testRequiredAttribute()
  7638. {
  7639. $this->article['required'] = [
  7640. 'title' => true,
  7641. 'body' => false,
  7642. ];
  7643. $this->Form->create($this->article);
  7644. $result = $this->Form->control('title');
  7645. $expected = [
  7646. 'div' => ['class' => 'input text required'],
  7647. 'label' => ['for' => 'title'],
  7648. 'Title',
  7649. '/label',
  7650. 'input' => [
  7651. 'type' => 'text',
  7652. 'name' => 'title',
  7653. 'id' => 'title',
  7654. 'required' => 'required',
  7655. ],
  7656. '/div'
  7657. ];
  7658. $this->assertHtml($expected, $result);
  7659. $result = $this->Form->control('title', ['required' => false]);
  7660. $this->assertNotContains('required', $result);
  7661. $result = $this->Form->control('body');
  7662. $expected = [
  7663. 'div' => ['class' => 'input text'],
  7664. 'label' => ['for' => 'body'],
  7665. 'Body',
  7666. '/label',
  7667. 'input' => [
  7668. 'type' => 'text',
  7669. 'name' => 'body',
  7670. 'id' => 'body',
  7671. ],
  7672. '/div'
  7673. ];
  7674. $this->assertHtml($expected, $result);
  7675. $result = $this->Form->control('body', ['required' => true]);
  7676. $this->assertContains('required', $result);
  7677. }
  7678. /**
  7679. * testControlsNotNested method
  7680. *
  7681. * Tests that it is possible to put inputs outside of the label.
  7682. *
  7683. * @return void
  7684. */
  7685. public function testControlsNotNested()
  7686. {
  7687. $this->Form->templates([
  7688. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7689. 'formGroup' => '{{input}}{{label}}',
  7690. ]);
  7691. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7692. $expected = [
  7693. 'div' => ['class' => 'input checkbox'],
  7694. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7695. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7696. 'label' => ['for' => 'foo'],
  7697. 'Foo',
  7698. '/label',
  7699. '/div'
  7700. ];
  7701. $this->assertHtml($expected, $result);
  7702. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7703. $expected = [
  7704. 'div' => ['class' => 'input checkbox'],
  7705. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7706. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7707. '/div'
  7708. ];
  7709. $this->assertHtml($expected, $result);
  7710. $result = $this->Form->control('confirm', [
  7711. 'type' => 'radio',
  7712. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7713. ]);
  7714. $expected = [
  7715. 'div' => ['class' => 'input radio'],
  7716. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7717. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7718. ['label' => ['for' => 'confirm-y']],
  7719. 'Yes',
  7720. '/label',
  7721. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7722. ['label' => ['for' => 'confirm-n']],
  7723. 'No',
  7724. '/label',
  7725. '<label',
  7726. 'Confirm',
  7727. '/label',
  7728. '/div',
  7729. ];
  7730. $this->assertHtml($expected, $result);
  7731. $result = $this->Form->select('category', ['1', '2'], [
  7732. 'multiple' => 'checkbox',
  7733. 'name' => 'fish',
  7734. ]);
  7735. $expected = [
  7736. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7737. ['div' => ['class' => 'checkbox']],
  7738. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7739. ['label' => ['for' => 'fish-0']],
  7740. '1',
  7741. '/label',
  7742. '/div',
  7743. ['div' => ['class' => 'checkbox']],
  7744. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7745. ['label' => ['for' => 'fish-1']],
  7746. '2',
  7747. '/label',
  7748. '/div'
  7749. ];
  7750. $this->assertHtml($expected, $result);
  7751. }
  7752. /**
  7753. * testControlContainerTemplates method
  7754. *
  7755. * Test that *Container templates are used by input.
  7756. *
  7757. * @return void
  7758. */
  7759. public function testControlContainerTemplates()
  7760. {
  7761. $this->Form->templates([
  7762. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7763. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7764. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7765. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7766. ]);
  7767. $this->article['errors'] = [
  7768. 'Article' => ['published' => 'error message']
  7769. ];
  7770. $this->Form->create($this->article);
  7771. $result = $this->Form->control('accept', [
  7772. 'type' => 'checkbox'
  7773. ]);
  7774. $expected = [
  7775. 'div' => ['class' => 'check'],
  7776. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7777. 'label' => ['for' => 'accept'],
  7778. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7779. 'Accept',
  7780. '/label',
  7781. '/div'
  7782. ];
  7783. $this->assertHtml($expected, $result);
  7784. $result = $this->Form->control('accept', [
  7785. 'type' => 'radio',
  7786. 'options' => ['Y', 'N']
  7787. ]);
  7788. $this->assertContains('<div class="rad">', $result);
  7789. $result = $this->Form->control('Article.published', [
  7790. 'type' => 'radio',
  7791. 'options' => ['Y', 'N']
  7792. ]);
  7793. $this->assertContains('<div class="rad err">', $result);
  7794. $result = $this->Form->control('Article.created', [
  7795. 'type' => 'datetime',
  7796. ]);
  7797. $this->assertContains('<div class="dt">', $result);
  7798. }
  7799. /**
  7800. * testFormGroupTemplates method
  7801. *
  7802. * Test that *Container templates are used by input.
  7803. *
  7804. * @return void
  7805. */
  7806. public function testFormGroupTemplates()
  7807. {
  7808. $this->Form->templates([
  7809. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7810. ]);
  7811. $this->Form->create($this->article);
  7812. $result = $this->Form->control('accept', [
  7813. 'type' => 'radio',
  7814. 'options' => ['Y', 'N']
  7815. ]);
  7816. $this->assertContains('<div class="radio">', $result);
  7817. }
  7818. /**
  7819. * testResetTemplates method
  7820. *
  7821. * Test resetting templates.
  7822. *
  7823. * @return void
  7824. */
  7825. public function testResetTemplates()
  7826. {
  7827. $this->Form->templates(['input' => '<input/>']);
  7828. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7829. $this->Form->resetTemplates();
  7830. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7831. }
  7832. /**
  7833. * testContext method
  7834. *
  7835. * Test the context method.
  7836. *
  7837. * @return void
  7838. */
  7839. public function testContext()
  7840. {
  7841. $result = $this->Form->context();
  7842. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7843. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7844. $this->assertSame($mock, $this->Form->context($mock));
  7845. $this->assertSame($mock, $this->Form->context());
  7846. }
  7847. /**
  7848. * testAutoDomId method
  7849. *
  7850. * @return void
  7851. */
  7852. public function testAutoDomId()
  7853. {
  7854. $result = $this->Form->text('field', ['id' => true]);
  7855. $expected = [
  7856. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7857. ];
  7858. $this->assertHtml($expected, $result);
  7859. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7860. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7861. $expected = [
  7862. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7863. ['label' => ['for' => 'field-0']],
  7864. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7865. 'option A',
  7866. '/label',
  7867. ['label' => ['for' => 'field-1']],
  7868. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7869. 'option B',
  7870. '/label',
  7871. ];
  7872. $this->assertHtml($expected, $result);
  7873. $result = $this->Form->select(
  7874. 'multi_field',
  7875. ['first', 'second'],
  7876. ['multiple' => 'checkbox', 'id' => true]
  7877. );
  7878. $expected = [
  7879. 'input' => [
  7880. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7881. ],
  7882. ['div' => ['class' => 'checkbox']],
  7883. ['label' => ['for' => 'multi-field-0']],
  7884. ['input' => [
  7885. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7886. 'value' => '0', 'id' => 'multi-field-0'
  7887. ]],
  7888. 'first',
  7889. '/label',
  7890. '/div',
  7891. ['div' => ['class' => 'checkbox']],
  7892. ['label' => ['for' => 'multi-field-1']],
  7893. ['input' => [
  7894. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7895. 'value' => '1', 'id' => 'multi-field-1'
  7896. ]],
  7897. 'second',
  7898. '/label',
  7899. '/div',
  7900. ];
  7901. $this->assertHtml($expected, $result);
  7902. }
  7903. /**
  7904. * Test the basic setters and getters for value sources
  7905. *
  7906. * @return void
  7907. */
  7908. public function testFormValueSourcesSettersGetters()
  7909. {
  7910. $expected = ['context'];
  7911. $result = $this->Form->getValueSources();
  7912. $this->assertEquals($expected, $result);
  7913. $expected = null;
  7914. $result = $this->Form->getSourceValue('id');
  7915. $this->assertEquals($expected, $result);
  7916. $expected = ['query', 'data', 'context'];
  7917. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7918. $result = $this->Form->getValueSources();
  7919. $this->assertEquals($expected, $result);
  7920. $this->Form->request->data['id'] = '1';
  7921. $this->Form->request->query['id'] = '2';
  7922. $this->Form->setValueSources(['context']);
  7923. $expected = '1';
  7924. $result = $this->Form->getSourceValue('id');
  7925. $this->assertEquals($expected, $result);
  7926. $this->Form->setValueSources('query');
  7927. $expected = ['query'];
  7928. $result = $this->Form->getValueSources();
  7929. $this->assertEquals($expected, $result);
  7930. $expected = '2';
  7931. $result = $this->Form->getSourceValue('id');
  7932. $this->assertEquals($expected, $result);
  7933. $this->Form->setValueSources(['data']);
  7934. $expected = '1';
  7935. $result = $this->Form->getSourceValue('id');
  7936. $this->assertEquals($expected, $result);
  7937. $this->Form->setValueSources(['query', 'data']);
  7938. $expected = '2';
  7939. $result = $this->Form->getSourceValue('id');
  7940. $this->assertEquals($expected, $result);
  7941. }
  7942. /**
  7943. * Tests the different input rendering values based on sources values switching
  7944. *
  7945. * @return void
  7946. */
  7947. public function testFormValueSourcesSingleSwitchRendering()
  7948. {
  7949. $this->loadFixtures('Articles');
  7950. $articles = TableRegistry::get('Articles');
  7951. $article = new Article();
  7952. $articles->patchEntity($article, ['id' => '3']);
  7953. $this->Form->create($article);
  7954. $this->Form->setValueSources(['context']);
  7955. $result = $this->Form->control('id');
  7956. $expected = [
  7957. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7958. ];
  7959. $this->assertHtml($expected, $result);
  7960. $this->Form->request->query['id'] = '5';
  7961. $this->Form->setValueSources(['query']);
  7962. $result = $this->Form->control('id');
  7963. $expected = [
  7964. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7965. ];
  7966. $this->assertHtml($expected, $result);
  7967. $this->Form->request->query['id'] = '5a';
  7968. $this->Form->request->data['id'] = '5b';
  7969. $this->Form->setValueSources(['context']);
  7970. $this->Form->create($article);
  7971. $result = $this->Form->control('id');
  7972. $expected = [
  7973. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7974. ];
  7975. $this->assertHtml($expected, $result);
  7976. $this->Form->setValueSources(['data']);
  7977. $this->Form->create($article);
  7978. $result = $this->Form->control('id');
  7979. $expected = [
  7980. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7981. ];
  7982. $this->assertHtml($expected, $result);
  7983. $this->Form->setValueSources(['query']);
  7984. $result = $this->Form->control('id');
  7985. $expected = [
  7986. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7987. ];
  7988. $this->assertHtml($expected, $result);
  7989. }
  7990. /**
  7991. * Tests the different input rendering values based on sources values switching while supplying
  7992. * an entity (base context) and multiple sources (such as data, query)
  7993. *
  7994. * @return void
  7995. */
  7996. public function testFormValueSourcesListSwitchRendering()
  7997. {
  7998. $this->loadFixtures('Articles');
  7999. $articles = TableRegistry::get('Articles');
  8000. $article = new Article();
  8001. $articles->patchEntity($article, ['id' => '3']);
  8002. $this->Form->request->query['id'] = '9';
  8003. $this->Form->create($article);
  8004. $this->Form->setValueSources(['context', 'query']);
  8005. $result = $this->Form->control('id');
  8006. $expected = [
  8007. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8008. ];
  8009. $this->assertHtml($expected, $result);
  8010. $this->Form->setValueSources(['query', 'context']);
  8011. $result = $this->Form->control('id');
  8012. $expected = [
  8013. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8014. ];
  8015. $this->assertHtml($expected, $result);
  8016. $this->Form->setValueSources(['data', 'query', 'context']);
  8017. $result = $this->Form->control('id');
  8018. $expected = [
  8019. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8020. ];
  8021. $this->assertHtml($expected, $result);
  8022. $this->Form->request->data['id'] = '8';
  8023. $this->Form->request->query['id'] = '9';
  8024. $this->Form->setValueSources(['data', 'query', 'context']);
  8025. $result = $this->Form->control('id');
  8026. $expected = [
  8027. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8028. ];
  8029. $this->assertHtml($expected, $result);
  8030. }
  8031. /**
  8032. * Test the different form input renderings based on values sources switchings through form options
  8033. *
  8034. * @return void
  8035. */
  8036. public function testFormValueSourcesSwitchViaOptionsRendering()
  8037. {
  8038. $this->loadFixtures('Articles');
  8039. $articles = TableRegistry::get('Articles');
  8040. $article = new Article();
  8041. $articles->patchEntity($article, ['id' => '3']);
  8042. $this->Form->request->data['id'] = '4';
  8043. $this->Form->request->query['id'] = '5';
  8044. $this->Form->create($article, ['valueSources' => 'query']);
  8045. $result = $this->Form->control('id');
  8046. $expected = [
  8047. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8048. ];
  8049. $this->assertHtml($expected, $result);
  8050. $result = $this->Form->getSourceValue('id');
  8051. $this->assertEquals('5', $result);
  8052. $this->Form->setValueSources(['context']);
  8053. $this->Form->create($article, ['valueSources' => 'query']);
  8054. $result = $this->Form->control('id');
  8055. $expected = [
  8056. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8057. ];
  8058. $this->assertHtml($expected, $result);
  8059. $result = $this->Form->getSourceValue('id');
  8060. $this->assertEquals('5', $result);
  8061. $this->Form->setValueSources(['query']);
  8062. $this->Form->create($article, ['valueSources' => 'data']);
  8063. $result = $this->Form->control('id');
  8064. $expected = [
  8065. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8066. ];
  8067. $this->assertHtml($expected, $result);
  8068. $result = $this->Form->getSourceValue('id');
  8069. $this->assertEquals('4', $result);
  8070. $this->Form->setValueSources(['query']);
  8071. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8072. $result = $this->Form->control('id');
  8073. $expected = [
  8074. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8075. ];
  8076. $this->assertHtml($expected, $result);
  8077. $result = $this->Form->getSourceValue('id');
  8078. $this->assertEquals('4', $result);
  8079. }
  8080. /**
  8081. * Test the different form input renderings based on values sources switchings through form options
  8082. *
  8083. * @return void
  8084. */
  8085. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8086. {
  8087. $this->loadFixtures('Articles');
  8088. $articles = TableRegistry::get('Articles');
  8089. $article = new Article();
  8090. $articles->patchEntity($article, ['id' => '3']);
  8091. $this->Form->request->data['id'] = '10';
  8092. $this->Form->request->query['id'] = '11';
  8093. $this->Form->setValueSources(['context'])
  8094. ->create($article, ['valueSources' => ['query', 'data']]);
  8095. $result = $this->Form->control('id');
  8096. $expected = [
  8097. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8098. ];
  8099. $this->assertHtml($expected, $result);
  8100. $result = $this->Form->getSourceValue('id');
  8101. $this->assertEquals('11', $result);
  8102. unset($this->Form->request->query['id']);
  8103. $this->Form->setValueSources(['context'])
  8104. ->create($article, ['valueSources' => ['query', 'data']]);
  8105. $result = $this->Form->control('id');
  8106. $expected = [
  8107. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8108. ];
  8109. $this->assertHtml($expected, $result);
  8110. $result = $this->Form->getSourceValue('id');
  8111. $this->assertEquals('10', $result);
  8112. }
  8113. /**
  8114. * Test the different form values sources resetting through From::end();
  8115. *
  8116. * @return void
  8117. */
  8118. public function testFormValueSourcesResetViaEnd()
  8119. {
  8120. $expected = ['context'];
  8121. $result = $this->Form->getValueSources();
  8122. $this->assertEquals($expected, $result);
  8123. $expected = ['query', 'context', 'data'];
  8124. $this->Form->setValueSources(['query', 'context', 'data']);
  8125. $result = $this->Form->getValueSources();
  8126. $this->assertEquals($expected, $result);
  8127. $this->Form->create();
  8128. $result = $this->Form->getValueSources();
  8129. $this->assertEquals($expected, $result);
  8130. $this->Form->end();
  8131. $result = $this->Form->getValueSources();
  8132. $this->assertEquals(['context'], $result);
  8133. }
  8134. /**
  8135. * Test sources values defaults handling
  8136. *
  8137. * @return void
  8138. */
  8139. public function testFormValueSourcesDefaults()
  8140. {
  8141. $this->Form->request->query['password'] = 'open Sesame';
  8142. $this->Form->create();
  8143. $result = $this->Form->password('password');
  8144. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8145. $this->assertHtml($expected, $result);
  8146. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8147. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8148. $this->assertHtml($expected, $result);
  8149. $this->Form->setValueSources('query');
  8150. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8151. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8152. $this->assertHtml($expected, $result);
  8153. $this->Form->setValueSources('data');
  8154. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8155. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8156. $this->assertHtml($expected, $result);
  8157. }
  8158. /**
  8159. * Test sources values schema defaults handling
  8160. *
  8161. * @return void
  8162. */
  8163. public function testSourcesValueDoesntExistPassThrough()
  8164. {
  8165. $this->Form->request->query['category'] = 'sesame-cookies';
  8166. $articles = TableRegistry::get('Articles');
  8167. $entity = $articles->newEntity();
  8168. $this->Form->create($entity);
  8169. $this->Form->setValueSources(['query', 'context']);
  8170. $result = $this->Form->getSourceValue('category');
  8171. $this->assertEquals('sesame-cookies', $result);
  8172. $this->Form->setValueSources(['context', 'query']);
  8173. $result = $this->Form->getSourceValue('category');
  8174. $this->assertEquals('sesame-cookies', $result);
  8175. }
  8176. /**
  8177. * testNestedLabelInput method
  8178. *
  8179. * Test the `nestedInput` parameter
  8180. *
  8181. * @return void
  8182. */
  8183. public function testNestedLabelInput()
  8184. {
  8185. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8186. $expected = [
  8187. 'div' => ['class' => 'input text'],
  8188. 'label' => ['for' => 'foo'],
  8189. ['input' => [
  8190. 'type' => 'text',
  8191. 'name' => 'foo',
  8192. 'id' => 'foo'
  8193. ]],
  8194. 'Foo',
  8195. '/label',
  8196. '/div'
  8197. ];
  8198. $this->assertHtml($expected, $result);
  8199. }
  8200. /**
  8201. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8202. *
  8203. * This test makes sure `false` excludes the label from the render
  8204. *
  8205. * @return void
  8206. */
  8207. public function testInputLabelManipulationDisableLabels()
  8208. {
  8209. $result = $this->Form->input('test', [
  8210. 'type' => 'radio',
  8211. 'options' => ['A', 'B'],
  8212. 'labelOptions' => false
  8213. ]);
  8214. $expected = [
  8215. ['div' => ['class' => 'input radio']],
  8216. '<label',
  8217. 'Test',
  8218. '/label',
  8219. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8220. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8221. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8222. '/div',
  8223. ];
  8224. $this->assertHtml($expected, $result);
  8225. $result = $this->Form->input('checkbox1', [
  8226. 'label' => 'My checkboxes',
  8227. 'multiple' => 'checkbox',
  8228. 'type' => 'select',
  8229. 'options' => [
  8230. ['text' => 'First Checkbox', 'value' => 1],
  8231. ['text' => 'Second Checkbox', 'value' => 2]
  8232. ],
  8233. 'labelOptions' => false
  8234. ]);
  8235. $expected = [
  8236. ['div' => ['class' => 'input select']],
  8237. ['label' => ['for' => 'checkbox1']],
  8238. 'My checkboxes',
  8239. '/label',
  8240. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8241. ['div' => ['class' => 'checkbox']],
  8242. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8243. '/div',
  8244. ['div' => ['class' => 'checkbox']],
  8245. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8246. '/div',
  8247. '/div'
  8248. ];
  8249. $this->assertHtml($expected, $result);
  8250. }
  8251. /**
  8252. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8253. *
  8254. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8255. * added to the class if checked.
  8256. *
  8257. * Also checks to make sure any custom attributes are rendered correctly
  8258. *
  8259. * @return void
  8260. */
  8261. public function testInputLabelManipulationRadios()
  8262. {
  8263. $result = $this->Form->input('test', [
  8264. 'type' => 'radio',
  8265. 'options' => ['A', 'B'],
  8266. 'labelOptions' => ['class' => 'custom-class']
  8267. ]);
  8268. $expected = [
  8269. ['div' => ['class' => 'input radio']],
  8270. '<label',
  8271. 'Test',
  8272. '/label',
  8273. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8274. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8275. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8276. 'A',
  8277. '/label',
  8278. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8279. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8280. 'B',
  8281. '/label',
  8282. '/div',
  8283. ];
  8284. $this->assertHtml($expected, $result);
  8285. $result = $this->Form->input('test', [
  8286. 'type' => 'radio',
  8287. 'options' => ['A', 'B'],
  8288. 'value' => 1,
  8289. 'labelOptions' => ['class' => 'custom-class']
  8290. ]);
  8291. $expected = [
  8292. ['div' => ['class' => 'input radio']],
  8293. '<label',
  8294. 'Test',
  8295. '/label',
  8296. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8297. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8298. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8299. 'A',
  8300. '/label',
  8301. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8302. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8303. 'B',
  8304. '/label',
  8305. '/div',
  8306. ];
  8307. $this->assertHtml($expected, $result);
  8308. $result = $this->Form->input('test', [
  8309. 'type' => 'radio',
  8310. 'options' => ['A', 'B'],
  8311. 'value' => 1,
  8312. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8313. ]);
  8314. $expected = [
  8315. ['div' => ['class' => 'input radio']],
  8316. '<label',
  8317. 'Test',
  8318. '/label',
  8319. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8320. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8321. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8322. 'A',
  8323. '/label',
  8324. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8325. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8326. 'B',
  8327. '/label',
  8328. '/div',
  8329. ];
  8330. $this->assertHtml($expected, $result);
  8331. $result = $this->Form->radio('test', ['A', 'B'], [
  8332. 'label' => [
  8333. 'class' => ['custom-class', 'another-class'],
  8334. 'data-name' => 'bob'
  8335. ],
  8336. 'value' => 1
  8337. ]);
  8338. $expected = [
  8339. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8340. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8341. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8342. 'A',
  8343. '/label',
  8344. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8345. ['input' => [
  8346. 'type' => 'radio',
  8347. 'name' => 'test',
  8348. 'value' => '1',
  8349. 'id' => 'test-1',
  8350. 'checked' => 'checked'
  8351. ]],
  8352. 'B',
  8353. '/label',
  8354. ];
  8355. $this->assertHtml($expected, $result);
  8356. }
  8357. /**
  8358. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8359. *
  8360. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8361. * added to the class if checked.
  8362. *
  8363. * Also checks to make sure any custom attributes are rendered correctly
  8364. *
  8365. * @return void
  8366. */
  8367. public function testInputLabelManipulationCheckboxes()
  8368. {
  8369. $result = $this->Form->input('checkbox1', [
  8370. 'label' => 'My checkboxes',
  8371. 'multiple' => 'checkbox',
  8372. 'type' => 'select',
  8373. 'options' => [
  8374. ['text' => 'First Checkbox', 'value' => 1],
  8375. ['text' => 'Second Checkbox', 'value' => 2]
  8376. ],
  8377. 'labelOptions' => ['class' => 'custom-class'],
  8378. 'value' => ['1']
  8379. ]);
  8380. $expected = [
  8381. ['div' => ['class' => 'input select']],
  8382. ['label' => ['for' => 'checkbox1']],
  8383. 'My checkboxes',
  8384. '/label',
  8385. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8386. ['div' => ['class' => 'checkbox']],
  8387. ['label' => [
  8388. 'class' => 'custom-class selected',
  8389. 'for' => 'checkbox1-1'
  8390. ]],
  8391. ['input' => [
  8392. 'type' => 'checkbox',
  8393. 'name' => 'checkbox1[]',
  8394. 'value' => '1',
  8395. 'id' => 'checkbox1-1',
  8396. 'checked' => 'checked'
  8397. ]],
  8398. 'First Checkbox',
  8399. '/label',
  8400. '/div',
  8401. ['div' => ['class' => 'checkbox']],
  8402. ['label' => [
  8403. 'class' => 'custom-class',
  8404. 'for' => 'checkbox1-2'
  8405. ]],
  8406. ['input' => [
  8407. 'type' => 'checkbox',
  8408. 'name' => 'checkbox1[]',
  8409. 'value' => '2',
  8410. 'id' => 'checkbox1-2'
  8411. ]],
  8412. 'Second Checkbox',
  8413. '/label',
  8414. '/div',
  8415. '/div'
  8416. ];
  8417. $this->assertHtml($expected, $result);
  8418. $result = $this->Form->input('checkbox1', [
  8419. 'label' => 'My checkboxes',
  8420. 'multiple' => 'checkbox',
  8421. 'type' => 'select',
  8422. 'options' => [
  8423. ['text' => 'First Checkbox', 'value' => 1],
  8424. ['text' => 'Second Checkbox', 'value' => 2]
  8425. ],
  8426. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8427. 'value' => ['1']
  8428. ]);
  8429. $expected = [
  8430. ['div' => ['class' => 'input select']],
  8431. ['label' => ['for' => 'checkbox1']],
  8432. 'My checkboxes',
  8433. '/label',
  8434. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8435. ['div' => ['class' => 'checkbox']],
  8436. ['label' => [
  8437. 'class' => 'custom-class another-class selected',
  8438. 'data-name' => 'bob',
  8439. 'for' => 'checkbox1-1'
  8440. ]],
  8441. ['input' => [
  8442. 'type' => 'checkbox',
  8443. 'name' => 'checkbox1[]',
  8444. 'value' => '1',
  8445. 'id' => 'checkbox1-1',
  8446. 'checked' => 'checked'
  8447. ]],
  8448. 'First Checkbox',
  8449. '/label',
  8450. '/div',
  8451. ['div' => ['class' => 'checkbox']],
  8452. ['label' => [
  8453. 'class' => 'custom-class another-class',
  8454. 'data-name' => 'bob',
  8455. 'for' => 'checkbox1-2'
  8456. ]],
  8457. ['input' => [
  8458. 'type' => 'checkbox',
  8459. 'name' => 'checkbox1[]',
  8460. 'value' => '2',
  8461. 'id' => 'checkbox1-2'
  8462. ]],
  8463. 'Second Checkbox',
  8464. '/label',
  8465. '/div',
  8466. '/div'
  8467. ];
  8468. $this->assertHtml($expected, $result);
  8469. }
  8470. }