FormHelperTest.php 302 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Core\Configure;
  18. use Cake\Form\Form;
  19. use Cake\Http\ServerRequest;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Security;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\View;
  28. /**
  29. * Test stub.
  30. */
  31. class Article extends Entity
  32. {
  33. }
  34. /**
  35. * Contact class
  36. */
  37. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->schema($this->_schema);
  64. }
  65. }
  66. /**
  67. * ValidateUser class
  68. */
  69. class ValidateUsersTable extends Table
  70. {
  71. /**
  72. * schema method
  73. *
  74. * @var array
  75. */
  76. protected $_schema = [
  77. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  78. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  79. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  81. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  82. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  83. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  84. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  85. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  86. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  87. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  88. ];
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config)
  95. {
  96. $this->schema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property \Cake\View\Helper\FormHelper $Form
  103. * @property \Cake\View\View $View
  104. */
  105. class FormHelperTest extends TestCase
  106. {
  107. /**
  108. * Fixtures to be used
  109. *
  110. * @var array
  111. */
  112. public $fixtures = ['core.articles', 'core.comments'];
  113. /**
  114. * Do not load the fixtures by default
  115. *
  116. * @var bool
  117. */
  118. public $autoFixtures = false;
  119. /**
  120. * @var array
  121. */
  122. protected $article = [];
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp()
  129. {
  130. parent::setUp();
  131. Configure::write('Config.language', 'eng');
  132. Configure::write('App.base', '');
  133. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  134. $this->View = new View();
  135. $this->Form = new FormHelper($this->View);
  136. $request = new ServerRequest([
  137. 'webroot' => '',
  138. 'base' => '',
  139. 'url' => '/articles/add',
  140. 'params' => [
  141. 'controller' => 'articles',
  142. 'action' => 'add',
  143. ]
  144. ]);
  145. $this->Form->Url->request = $this->Form->request = $request;
  146. $this->dateRegex = [
  147. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  148. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  149. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  152. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  153. ];
  154. $this->article = [
  155. 'schema' => [
  156. 'id' => ['type' => 'integer'],
  157. 'author_id' => ['type' => 'integer', 'null' => true],
  158. 'title' => ['type' => 'string', 'null' => true],
  159. 'body' => 'text',
  160. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  161. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  162. ],
  163. 'required' => [
  164. 'author_id' => true,
  165. 'title' => true,
  166. ]
  167. ];
  168. Security::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. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  382. return [
  383. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  384. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  385. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  386. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  387. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  388. 'form' => [$form, 'Cake\View\Form\FormContext'],
  389. 'none' => [null, 'Cake\View\Form\NullContext'],
  390. 'false' => [false, 'Cake\View\Form\NullContext'],
  391. 'custom' => [$custom, get_class($custom)]
  392. ];
  393. }
  394. /**
  395. * Test default context selection in create()
  396. *
  397. * @dataProvider contextSelectionProvider
  398. * @return void
  399. */
  400. public function testCreateContextSelectionBuiltIn($data, $class)
  401. {
  402. $this->loadFixtures('Articles');
  403. $this->Form->create($data);
  404. $this->assertInstanceOf($class, $this->Form->context());
  405. }
  406. /**
  407. * Data provider for type option.
  408. *
  409. * @return array
  410. */
  411. public static function requestTypeProvider()
  412. {
  413. return [
  414. // type, method, override
  415. ['post', 'post', 'POST'],
  416. ['put', 'post', 'PUT'],
  417. ['patch', 'post', 'PATCH'],
  418. ['delete', 'post', 'DELETE'],
  419. ];
  420. }
  421. /**
  422. * Test creating file forms.
  423. *
  424. * @return void
  425. */
  426. public function testCreateFile()
  427. {
  428. $encoding = strtolower(Configure::read('App.encoding'));
  429. $result = $this->Form->create(false, ['type' => 'file']);
  430. $expected = [
  431. 'form' => [
  432. 'method' => 'post', 'action' => '/articles/add',
  433. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  434. ],
  435. 'div' => ['style' => 'display:none;'],
  436. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  437. '/div'
  438. ];
  439. $this->assertHtml($expected, $result);
  440. }
  441. /**
  442. * Test creating GET forms.
  443. *
  444. * @return void
  445. */
  446. public function testCreateGet()
  447. {
  448. $encoding = strtolower(Configure::read('App.encoding'));
  449. $result = $this->Form->create(false, ['type' => 'get']);
  450. $expected = ['form' => [
  451. 'method' => 'get', 'action' => '/articles/add',
  452. 'accept-charset' => $encoding
  453. ]];
  454. $this->assertHtml($expected, $result);
  455. }
  456. /**
  457. * Test explicit method/enctype options.
  458. *
  459. * Explicit method overwrites inferred method from 'type'
  460. *
  461. * @return void
  462. */
  463. public function testCreateExplicitMethodEnctype()
  464. {
  465. $encoding = strtolower(Configure::read('App.encoding'));
  466. $result = $this->Form->create(false, [
  467. 'type' => 'get',
  468. 'method' => 'put',
  469. 'enctype' => 'multipart/form-data'
  470. ]);
  471. $expected = ['form' => [
  472. 'method' => 'put',
  473. 'action' => '/articles/add',
  474. 'enctype' => 'multipart/form-data',
  475. 'accept-charset' => $encoding
  476. ]];
  477. $this->assertHtml($expected, $result);
  478. }
  479. /**
  480. * Test create() with the templates option.
  481. *
  482. * @return void
  483. */
  484. public function testCreateTemplatesArray()
  485. {
  486. $result = $this->Form->create($this->article, [
  487. 'templates' => [
  488. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  489. ]
  490. ]);
  491. $expected = [
  492. 'form' => [
  493. 'class' => 'form-horizontal',
  494. 'method' => 'post',
  495. 'action' => '/articles/add',
  496. 'accept-charset' => 'utf-8'
  497. ]
  498. ];
  499. $this->assertHtml($expected, $result);
  500. }
  501. /**
  502. * Test create() with the templates option.
  503. *
  504. * @return void
  505. */
  506. public function testCreateTemplatesFile()
  507. {
  508. $result = $this->Form->create($this->article, [
  509. 'templates' => 'htmlhelper_tags',
  510. ]);
  511. $expected = [
  512. 'start form',
  513. 'div' => ['class' => 'hidden'],
  514. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  515. '/div'
  516. ];
  517. $this->assertHtml($expected, $result);
  518. }
  519. /**
  520. * Test that create() and end() restore templates.
  521. *
  522. * @return void
  523. */
  524. public function testCreateEndRestoreTemplates()
  525. {
  526. $this->Form->create($this->article, [
  527. 'templates' => ['input' => 'custom input element']
  528. ]);
  529. $this->Form->end();
  530. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  531. }
  532. /**
  533. * Test using template vars in various templates used by control() method.
  534. *
  535. * @return void
  536. */
  537. public function testControlTemplateVars()
  538. {
  539. $result = $this->Form->control('text', [
  540. 'templates' => [
  541. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  542. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  543. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  544. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  545. ],
  546. 'templateVars' => [
  547. 'forinput' => 'in-input',
  548. 'forlabel' => 'in-label',
  549. 'forgroup' => 'in-group',
  550. 'forcontainer' => 'in-container'
  551. ]
  552. ]);
  553. $expected = [
  554. 'div' => ['class'],
  555. 'label' => ['for'],
  556. 'Text in-label',
  557. '/label',
  558. 'in-group',
  559. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  560. 'in-container',
  561. '/div',
  562. ];
  563. $this->assertHtml($expected, $result);
  564. }
  565. /**
  566. * Test ensuring template variables work in template files loaded
  567. * during control().
  568. *
  569. * @return void
  570. */
  571. public function testControlTemplatesFromFile()
  572. {
  573. $result = $this->Form->control('title', [
  574. 'templates' => 'test_templates',
  575. 'templateVars' => [
  576. 'forcontainer' => 'container-data'
  577. ]
  578. ]);
  579. $expected = [
  580. 'div' => ['class'],
  581. 'label' => ['for'],
  582. 'Title',
  583. '/label',
  584. 'input' => ['name', 'type' => 'text', 'id'],
  585. 'container-data',
  586. '/div',
  587. ];
  588. $this->assertHtml($expected, $result);
  589. }
  590. /**
  591. * Test using template vars in inputSubmit and submitContainer template.
  592. *
  593. * @return void
  594. */
  595. public function testSubmitTemplateVars()
  596. {
  597. $this->Form->templates([
  598. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  599. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  600. ]);
  601. $result = $this->Form->submit('Submit', [
  602. 'templateVars' => [
  603. 'forinput' => 'in-input',
  604. 'forcontainer' => 'in-container'
  605. ]
  606. ]);
  607. $expected = [
  608. 'div' => ['class'],
  609. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  610. 'in-container',
  611. '/div',
  612. ];
  613. $this->assertHtml($expected, $result);
  614. }
  615. /**
  616. * test the create() method
  617. *
  618. * @dataProvider requestTypeProvider
  619. * @return void
  620. */
  621. public function testCreateTypeOptions($type, $method, $override)
  622. {
  623. $encoding = strtolower(Configure::read('App.encoding'));
  624. $result = $this->Form->create(false, ['type' => $type]);
  625. $expected = [
  626. 'form' => [
  627. 'method' => $method, 'action' => '/articles/add',
  628. 'accept-charset' => $encoding
  629. ],
  630. 'div' => ['style' => 'display:none;'],
  631. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  632. '/div'
  633. ];
  634. $this->assertHtml($expected, $result);
  635. }
  636. /**
  637. * Test using template vars in Create (formStart template)
  638. *
  639. * @return void
  640. */
  641. public function testCreateTemplateVars()
  642. {
  643. $result = $this->Form->create($this->article, [
  644. 'templates' => [
  645. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  646. ],
  647. 'templateVars' => ['header' => 'headertext']
  648. ]);
  649. $expected = [
  650. 'h4' => ['class'],
  651. 'headertext',
  652. '/h4',
  653. 'form' => [
  654. 'method' => 'post',
  655. 'action' => '/articles/add',
  656. 'accept-charset' => 'utf-8'
  657. ]
  658. ];
  659. $this->assertHtml($expected, $result);
  660. }
  661. /**
  662. * Test opening a form for an update operation.
  663. *
  664. * @return void
  665. */
  666. public function testCreateUpdateForm()
  667. {
  668. $encoding = strtolower(Configure::read('App.encoding'));
  669. $this->Form->request->here = '/articles/edit/1';
  670. $this->Form->request->params['action'] = 'edit';
  671. $this->article['defaults']['id'] = 1;
  672. $result = $this->Form->create($this->article);
  673. $expected = [
  674. 'form' => [
  675. 'method' => 'post',
  676. 'action' => '/articles/edit/1',
  677. 'accept-charset' => $encoding
  678. ],
  679. 'div' => ['style' => 'display:none;'],
  680. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  681. '/div'
  682. ];
  683. $this->assertHtml($expected, $result);
  684. }
  685. /**
  686. * test create() with automatic url generation
  687. *
  688. * @return void
  689. */
  690. public function testCreateAutoUrl()
  691. {
  692. $encoding = strtolower(Configure::read('App.encoding'));
  693. $this->Form->request->params['action'] = 'delete';
  694. $this->Form->request->here = '/articles/delete/10';
  695. $this->Form->request->base = '';
  696. $result = $this->Form->create($this->article);
  697. $expected = [
  698. 'form' => [
  699. 'method' => 'post', 'action' => '/articles/delete/10',
  700. 'accept-charset' => $encoding
  701. ],
  702. 'div' => ['style' => 'display:none;'],
  703. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  704. '/div'
  705. ];
  706. $this->assertHtml($expected, $result);
  707. $this->article['defaults'] = ['id' => 1];
  708. $this->Form->request->here = '/articles/edit/1';
  709. $this->Form->request->params['action'] = 'delete';
  710. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  711. $expected = [
  712. 'form' => [
  713. 'method' => 'post',
  714. 'action' => '/articles/edit/1',
  715. 'accept-charset' => $encoding
  716. ],
  717. 'div' => ['style' => 'display:none;'],
  718. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  719. '/div'
  720. ];
  721. $this->assertHtml($expected, $result);
  722. $this->Form->request->params['action'] = 'add';
  723. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  724. $expected = [
  725. 'form' => [
  726. 'method' => 'post',
  727. 'action' => '/articles/publish/1',
  728. 'accept-charset' => $encoding
  729. ],
  730. 'div' => ['style' => 'display:none;'],
  731. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  732. '/div'
  733. ];
  734. $this->assertHtml($expected, $result);
  735. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  736. $expected = [
  737. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  738. 'div' => ['style' => 'display:none;'],
  739. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  740. '/div'
  741. ];
  742. $this->assertHtml($expected, $result);
  743. $this->Form->request->params['controller'] = 'Pages';
  744. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  745. $expected = [
  746. 'form' => [
  747. 'method' => 'post', 'action' => '/Pages/signup/1',
  748. 'accept-charset' => $encoding
  749. ],
  750. 'div' => ['style' => 'display:none;'],
  751. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  752. '/div'
  753. ];
  754. $this->assertHtml($expected, $result);
  755. }
  756. /**
  757. * Test create() with no URL (no "action" attribute for <form> tag)
  758. *
  759. * @return void
  760. */
  761. public function testCreateNoUrl()
  762. {
  763. $result = $this->Form->create(false, ['url' => false]);
  764. $expected = [
  765. 'form' => [
  766. 'method' => 'post',
  767. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  768. ],
  769. 'div' => ['style' => 'display:none;'],
  770. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  771. '/div'
  772. ];
  773. $this->assertHtml($expected, $result);
  774. }
  775. /**
  776. * test create() with a custom route
  777. *
  778. * @return void
  779. */
  780. public function testCreateCustomRoute()
  781. {
  782. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  783. $encoding = strtolower(Configure::read('App.encoding'));
  784. $this->Form->request->params['controller'] = 'users';
  785. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  786. $expected = [
  787. 'form' => [
  788. 'method' => 'post', 'action' => '/login',
  789. 'accept-charset' => $encoding
  790. ],
  791. 'div' => ['style' => 'display:none;'],
  792. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  793. '/div'
  794. ];
  795. $this->assertHtml($expected, $result);
  796. Router::connect(
  797. '/new-article',
  798. ['controller' => 'articles', 'action' => 'myaction'],
  799. ['_name' => 'my-route']
  800. );
  801. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  802. $expected = [
  803. 'form' => [
  804. 'method' => 'post', 'action' => '/new-article',
  805. 'accept-charset' => $encoding,
  806. ],
  807. 'div' => ['style' => 'display:none;'],
  808. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  809. '/div'
  810. ];
  811. $this->assertHtml($expected, $result);
  812. }
  813. /**
  814. * test automatic accept-charset overriding
  815. *
  816. * @return void
  817. */
  818. public function testCreateWithAcceptCharset()
  819. {
  820. $result = $this->Form->create(
  821. $this->article,
  822. [
  823. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  824. ]
  825. );
  826. $expected = [
  827. 'form' => [
  828. 'method' => 'post', 'action' => '/articles',
  829. 'accept-charset' => 'iso-8859-1'
  830. ],
  831. 'div' => ['style' => 'display:none;'],
  832. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  833. '/div'
  834. ];
  835. $this->assertHtml($expected, $result);
  836. }
  837. /**
  838. * Test base form URL when url param is passed with multiple parameters (&)
  839. *
  840. */
  841. public function testCreateQueryStringRequest()
  842. {
  843. $encoding = strtolower(Configure::read('App.encoding'));
  844. $result = $this->Form->create($this->article, [
  845. 'type' => 'post',
  846. 'escape' => false,
  847. 'url' => [
  848. 'controller' => 'controller',
  849. 'action' => 'action',
  850. '?' => ['param1' => 'value1', 'param2' => 'value2']
  851. ]
  852. ]);
  853. $expected = [
  854. 'form' => [
  855. 'method' => 'post',
  856. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  857. 'accept-charset' => $encoding
  858. ],
  859. 'div' => ['style' => 'display:none;'],
  860. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  861. '/div'
  862. ];
  863. $this->assertHtml($expected, $result);
  864. $result = $this->Form->create($this->article, [
  865. 'type' => 'post',
  866. 'url' => [
  867. 'controller' => 'controller',
  868. 'action' => 'action',
  869. '?' => ['param1' => 'value1', 'param2' => 'value2']
  870. ]
  871. ]);
  872. $this->assertHtml($expected, $result);
  873. }
  874. /**
  875. * test that create() doesn't cause errors by multiple id's being in the primary key
  876. * as could happen with multiple select or checkboxes.
  877. *
  878. * @return void
  879. */
  880. public function testCreateWithMultipleIdInData()
  881. {
  882. $encoding = strtolower(Configure::read('App.encoding'));
  883. $this->Form->request->data['Article']['id'] = [1, 2];
  884. $result = $this->Form->create($this->article);
  885. $expected = [
  886. 'form' => [
  887. 'method' => 'post',
  888. 'action' => '/articles/add',
  889. 'accept-charset' => $encoding
  890. ],
  891. 'div' => ['style' => 'display:none;'],
  892. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  893. '/div'
  894. ];
  895. $this->assertHtml($expected, $result);
  896. }
  897. /**
  898. * test that create() doesn't add in extra passed params.
  899. *
  900. * @return void
  901. */
  902. public function testCreatePassedArgs()
  903. {
  904. $encoding = strtolower(Configure::read('App.encoding'));
  905. $this->Form->request->data['Article']['id'] = 1;
  906. $result = $this->Form->create($this->article, [
  907. 'type' => 'post',
  908. 'escape' => false,
  909. 'url' => [
  910. 'action' => 'edit',
  911. 'myparam'
  912. ]
  913. ]);
  914. $expected = [
  915. 'form' => [
  916. 'method' => 'post',
  917. 'action' => '/articles/edit/myparam',
  918. 'accept-charset' => $encoding
  919. ],
  920. 'div' => ['style' => 'display:none;'],
  921. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  922. '/div'
  923. ];
  924. $this->assertHtml($expected, $result);
  925. }
  926. /**
  927. * test creating a get form, and get form inputs.
  928. *
  929. * @return void
  930. */
  931. public function testGetFormCreate()
  932. {
  933. $encoding = strtolower(Configure::read('App.encoding'));
  934. $result = $this->Form->create($this->article, ['type' => 'get']);
  935. $expected = ['form' => [
  936. 'method' => 'get', 'action' => '/articles/add',
  937. 'accept-charset' => $encoding
  938. ]];
  939. $this->assertHtml($expected, $result);
  940. $result = $this->Form->text('title');
  941. $expected = ['input' => [
  942. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  943. ]];
  944. $this->assertHtml($expected, $result);
  945. $result = $this->Form->password('password');
  946. $expected = ['input' => [
  947. 'name' => 'password', 'type' => 'password'
  948. ]];
  949. $this->assertHtml($expected, $result);
  950. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  951. $result = $this->Form->text('user_form');
  952. $expected = ['input' => [
  953. 'name' => 'user_form', 'type' => 'text'
  954. ]];
  955. $this->assertHtml($expected, $result);
  956. }
  957. /**
  958. * test get form, and inputs when the model param is false
  959. *
  960. * @return void
  961. */
  962. public function testGetFormWithFalseModel()
  963. {
  964. $encoding = strtolower(Configure::read('App.encoding'));
  965. $this->Form->request->params['controller'] = 'contact_test';
  966. $result = $this->Form->create(false, [
  967. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  968. ]);
  969. $expected = ['form' => [
  970. 'method' => 'get', 'action' => '/contact_test/add',
  971. 'accept-charset' => $encoding
  972. ]];
  973. $this->assertHtml($expected, $result);
  974. $result = $this->Form->text('reason');
  975. $expected = [
  976. 'input' => ['type' => 'text', 'name' => 'reason']
  977. ];
  978. $this->assertHtml($expected, $result);
  979. }
  980. /**
  981. * testFormCreateWithSecurity method
  982. *
  983. * Test form->create() with security key.
  984. *
  985. * @return void
  986. */
  987. public function testCreateWithSecurity()
  988. {
  989. $this->Form->request->params['_csrfToken'] = 'testKey';
  990. $encoding = strtolower(Configure::read('App.encoding'));
  991. $result = $this->Form->create($this->article, [
  992. 'url' => '/articles/publish',
  993. ]);
  994. $expected = [
  995. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  996. 'div' => ['style' => 'display:none;'],
  997. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  998. ['input' => [
  999. 'type' => 'hidden',
  1000. 'name' => '_csrfToken',
  1001. 'value' => 'testKey',
  1002. 'autocomplete' => 'off',
  1003. ]],
  1004. '/div'
  1005. ];
  1006. $this->assertHtml($expected, $result);
  1007. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1008. $expected['form']['id'] = 'MyForm';
  1009. $this->assertHtml($expected, $result);
  1010. }
  1011. /**
  1012. * testFormCreateGetNoSecurity method
  1013. *
  1014. * Test form->create() with no security key as its a get form
  1015. *
  1016. * @return void
  1017. */
  1018. public function testCreateEndGetNoSecurity()
  1019. {
  1020. $this->Form->request->params['_csrfToken'] = 'testKey';
  1021. $article = new Article();
  1022. $result = $this->Form->create($article, [
  1023. 'type' => 'get',
  1024. 'url' => '/contacts/add'
  1025. ]);
  1026. $this->assertNotContains('testKey', $result);
  1027. $result = $this->Form->end();
  1028. $this->assertNotContains('testKey', $result);
  1029. }
  1030. /**
  1031. * test that create() clears the fields property so it starts fresh
  1032. *
  1033. * @return void
  1034. */
  1035. public function testCreateClearingFields()
  1036. {
  1037. $this->Form->fields = ['model_id'];
  1038. $this->Form->create($this->article);
  1039. $this->assertEquals([], $this->Form->fields);
  1040. }
  1041. /**
  1042. * Tests form hash generation with model-less data
  1043. *
  1044. * @return void
  1045. */
  1046. public function testValidateHashNoModel()
  1047. {
  1048. $this->Form->request->params['_Token'] = 'foo';
  1049. $result = $this->Form->secure(['anything']);
  1050. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1051. }
  1052. /**
  1053. * Tests that hidden fields generated for checkboxes don't get locked
  1054. *
  1055. * @return void
  1056. */
  1057. public function testNoCheckboxLocking()
  1058. {
  1059. $this->Form->request->params['_Token'] = 'foo';
  1060. $this->assertSame([], $this->Form->fields);
  1061. $this->Form->checkbox('check', ['value' => '1']);
  1062. $this->assertSame($this->Form->fields, ['check']);
  1063. }
  1064. /**
  1065. * testFormSecurityFields method
  1066. *
  1067. * Test generation of secure form hash generation.
  1068. *
  1069. * @return void
  1070. */
  1071. public function testFormSecurityFields()
  1072. {
  1073. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1074. $this->Form->request->params['_Token'] = 'testKey';
  1075. $result = $this->Form->secure($fields);
  1076. $hash = Security::hash(serialize($fields) . Security::salt());
  1077. $hash .= ':' . 'Model.valid';
  1078. $hash = urlencode($hash);
  1079. $tokenDebug = urlencode(json_encode([
  1080. '',
  1081. $fields,
  1082. []
  1083. ]));
  1084. $expected = [
  1085. 'div' => ['style' => 'display:none;'],
  1086. ['input' => [
  1087. 'type' => 'hidden',
  1088. 'name' => '_Token[fields]',
  1089. 'value' => $hash,
  1090. 'autocomplete' => 'off',
  1091. ]],
  1092. ['input' => [
  1093. 'type' => 'hidden',
  1094. 'name' => '_Token[unlocked]',
  1095. 'value' => '',
  1096. 'autocomplete' => 'off',
  1097. ]],
  1098. ['input' => [
  1099. 'type' => 'hidden',
  1100. 'name' => '_Token[debug]',
  1101. 'value' => $tokenDebug,
  1102. 'autocomplete' => 'off',
  1103. ]],
  1104. '/div'
  1105. ];
  1106. $this->assertHtml($expected, $result);
  1107. }
  1108. /**
  1109. * testFormSecurityFields method
  1110. *
  1111. * Test debug token is not generated if debug is false
  1112. *
  1113. * @return void
  1114. */
  1115. public function testFormSecurityFieldsNoDebugMode()
  1116. {
  1117. Configure::write('debug', false);
  1118. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1119. $this->Form->request->params['_Token'] = 'testKey';
  1120. $result = $this->Form->secure($fields);
  1121. $hash = Security::hash(serialize($fields) . Security::salt());
  1122. $hash .= ':' . 'Model.valid';
  1123. $hash = urlencode($hash);
  1124. $expected = [
  1125. 'div' => ['style' => 'display:none;'],
  1126. ['input' => [
  1127. 'type' => 'hidden',
  1128. 'name' => '_Token[fields]',
  1129. 'autocomplete' => 'off',
  1130. 'value' => $hash
  1131. ]],
  1132. ['input' => [
  1133. 'type' => 'hidden',
  1134. 'name' => '_Token[unlocked]',
  1135. 'autocomplete' => 'off',
  1136. 'value' => '',
  1137. ]],
  1138. '/div'
  1139. ];
  1140. $this->assertHtml($expected, $result);
  1141. }
  1142. /**
  1143. * Tests correct generation of number fields for double and float fields
  1144. *
  1145. * @return void
  1146. */
  1147. public function testTextFieldGenerationForFloats()
  1148. {
  1149. $this->article['schema'] = [
  1150. 'foo' => [
  1151. 'type' => 'float',
  1152. 'null' => false,
  1153. 'default' => null,
  1154. 'length' => 10
  1155. ]
  1156. ];
  1157. $this->Form->create($this->article);
  1158. $result = $this->Form->control('foo');
  1159. $expected = [
  1160. 'div' => ['class' => 'input number'],
  1161. 'label' => ['for' => 'foo'],
  1162. 'Foo',
  1163. '/label',
  1164. ['input' => [
  1165. 'type' => 'number',
  1166. 'name' => 'foo',
  1167. 'id' => 'foo',
  1168. 'step' => 'any'
  1169. ]],
  1170. '/div'
  1171. ];
  1172. $this->assertHtml($expected, $result);
  1173. $result = $this->Form->control('foo', ['step' => 0.5]);
  1174. $expected = [
  1175. 'div' => ['class' => 'input number'],
  1176. 'label' => ['for' => 'foo'],
  1177. 'Foo',
  1178. '/label',
  1179. ['input' => [
  1180. 'type' => 'number',
  1181. 'name' => 'foo',
  1182. 'id' => 'foo',
  1183. 'step' => '0.5'
  1184. ]],
  1185. '/div'
  1186. ];
  1187. $this->assertHtml($expected, $result);
  1188. }
  1189. /**
  1190. * Tests correct generation of number fields for integer fields
  1191. *
  1192. * @return void
  1193. */
  1194. public function testTextFieldTypeNumberGenerationForIntegers()
  1195. {
  1196. TableRegistry::get('Contacts', [
  1197. 'className' => __NAMESPACE__ . '\ContactsTable'
  1198. ]);
  1199. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1200. $result = $this->Form->control('age');
  1201. $expected = [
  1202. 'div' => ['class' => 'input number'],
  1203. 'label' => ['for' => 'age'],
  1204. 'Age',
  1205. '/label',
  1206. ['input' => [
  1207. 'type' => 'number', 'name' => 'age',
  1208. 'id' => 'age'
  1209. ]],
  1210. '/div'
  1211. ];
  1212. $this->assertHtml($expected, $result);
  1213. }
  1214. /**
  1215. * Tests correct generation of file upload fields for binary fields
  1216. *
  1217. * @return void
  1218. */
  1219. public function testFileUploadFieldTypeGenerationForBinaries()
  1220. {
  1221. $table = TableRegistry::get('Contacts', [
  1222. 'className' => __NAMESPACE__ . '\ContactsTable'
  1223. ]);
  1224. $table->schema(['foo' => [
  1225. 'type' => 'binary',
  1226. 'null' => false,
  1227. 'default' => null,
  1228. 'length' => 1024
  1229. ]]);
  1230. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1231. $result = $this->Form->control('foo');
  1232. $expected = [
  1233. 'div' => ['class' => 'input file'],
  1234. 'label' => ['for' => 'foo'],
  1235. 'Foo',
  1236. '/label',
  1237. ['input' => [
  1238. 'type' => 'file', 'name' => 'foo',
  1239. 'id' => 'foo'
  1240. ]],
  1241. '/div'
  1242. ];
  1243. $this->assertHtml($expected, $result);
  1244. }
  1245. /**
  1246. * testFormSecurityMultipleFields method
  1247. *
  1248. * Test secure() with multiple row form. Ensure hash is correct.
  1249. *
  1250. * @return void
  1251. */
  1252. public function testFormSecurityMultipleFields()
  1253. {
  1254. $this->Form->request->params['_Token'] = 'foo';
  1255. $fields = [
  1256. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1257. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1258. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1259. ];
  1260. $result = $this->Form->secure($fields);
  1261. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1262. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1263. $tokenDebug = urlencode(json_encode([
  1264. '',
  1265. $fields,
  1266. []
  1267. ]));
  1268. $expected = [
  1269. 'div' => ['style' => 'display:none;'],
  1270. ['input' => [
  1271. 'type' => 'hidden',
  1272. 'name' => '_Token[fields]',
  1273. 'value' => $hash,
  1274. 'autocomplete' => 'off',
  1275. ]],
  1276. ['input' => [
  1277. 'type' => 'hidden',
  1278. 'name' => '_Token[unlocked]',
  1279. 'autocomplete' => 'off',
  1280. 'value' => ''
  1281. ]],
  1282. ['input' => [
  1283. 'type' => 'hidden',
  1284. 'name' => '_Token[debug]',
  1285. 'value' => $tokenDebug,
  1286. 'autocomplete' => 'off',
  1287. ]],
  1288. '/div'
  1289. ];
  1290. $this->assertHtml($expected, $result);
  1291. }
  1292. /**
  1293. * testFormSecurityMultipleSubmitButtons
  1294. *
  1295. * test form submit generation and ensure that _Token is only created on end()
  1296. *
  1297. * @return void
  1298. */
  1299. public function testFormSecurityMultipleSubmitButtons()
  1300. {
  1301. $this->Form->request->params['_Token'] = 'testKey';
  1302. $this->Form->create($this->article);
  1303. $this->Form->text('Address.title');
  1304. $this->Form->text('Address.first_name');
  1305. $result = $this->Form->submit('Save', ['name' => 'save']);
  1306. $expected = [
  1307. 'div' => ['class' => 'submit'],
  1308. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1309. '/div',
  1310. ];
  1311. $this->assertHtml($expected, $result);
  1312. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1313. $expected = [
  1314. 'div' => ['class' => 'submit'],
  1315. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1316. '/div',
  1317. ];
  1318. $this->assertHtml($expected, $result);
  1319. $result = $this->Form->end();
  1320. $tokenDebug = urlencode(json_encode([
  1321. '/articles/add',
  1322. [
  1323. 'Address.title',
  1324. 'Address.first_name',
  1325. ],
  1326. ['save', 'cancel']
  1327. ]));
  1328. $expected = [
  1329. 'div' => ['style' => 'display:none;'],
  1330. ['input' => [
  1331. 'type' => 'hidden',
  1332. 'name' => '_Token[fields]',
  1333. 'autocomplete',
  1334. 'value'
  1335. ]],
  1336. ['input' => [
  1337. 'type' => 'hidden',
  1338. 'name' => '_Token[unlocked]',
  1339. 'value' => 'cancel%7Csave',
  1340. 'autocomplete' => 'off',
  1341. ]],
  1342. ['input' => [
  1343. 'type' => 'hidden',
  1344. 'name' => '_Token[debug]',
  1345. 'value' => $tokenDebug,
  1346. 'autocomplete' => 'off',
  1347. ]],
  1348. '/div'
  1349. ];
  1350. $this->assertHtml($expected, $result);
  1351. }
  1352. /**
  1353. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1354. *
  1355. * @return void
  1356. */
  1357. public function testSecurityButtonNestedNamed()
  1358. {
  1359. $key = 'testKey';
  1360. $this->Form->request->params['_csrfToken'] = $key;
  1361. $this->Form->create('Addresses');
  1362. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1363. $result = $this->Form->unlockField();
  1364. $this->assertEquals(['Address.button'], $result);
  1365. }
  1366. /**
  1367. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1368. *
  1369. * @return void
  1370. */
  1371. public function testSecuritySubmitNestedNamed()
  1372. {
  1373. $this->Form->request->params['_Token'] = 'testKey';
  1374. $this->Form->create($this->article);
  1375. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1376. $result = $this->Form->unlockField();
  1377. $this->assertEquals(['Address.button'], $result);
  1378. }
  1379. /**
  1380. * Test that the correct fields are unlocked for image submits with no names.
  1381. *
  1382. * @return void
  1383. */
  1384. public function testSecuritySubmitImageNoName()
  1385. {
  1386. $this->Form->request->params['_Token'] = 'testKey';
  1387. $this->Form->create(false);
  1388. $result = $this->Form->submit('save.png');
  1389. $expected = [
  1390. 'div' => ['class' => 'submit'],
  1391. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1392. '/div'
  1393. ];
  1394. $this->assertHtml($expected, $result);
  1395. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1396. }
  1397. /**
  1398. * Test that the correct fields are unlocked for image submits with names.
  1399. *
  1400. * @return void
  1401. */
  1402. public function testSecuritySubmitImageName()
  1403. {
  1404. $this->Form->request->params['_Token'] = 'testKey';
  1405. $this->Form->create(null);
  1406. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1407. $expected = [
  1408. 'div' => ['class' => 'submit'],
  1409. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1410. '/div'
  1411. ];
  1412. $this->assertHtml($expected, $result);
  1413. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1414. }
  1415. /**
  1416. * testFormSecurityMultipleControlFields method
  1417. *
  1418. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1419. *
  1420. * @return void
  1421. */
  1422. public function testFormSecurityMultipleControlFields()
  1423. {
  1424. $this->Form->request->params['_Token'] = 'testKey';
  1425. $this->Form->create();
  1426. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1427. $this->Form->control('Addresses.0.title');
  1428. $this->Form->control('Addresses.0.first_name');
  1429. $this->Form->control('Addresses.0.last_name');
  1430. $this->Form->control('Addresses.0.address');
  1431. $this->Form->control('Addresses.0.city');
  1432. $this->Form->control('Addresses.0.phone');
  1433. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1434. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1435. $this->Form->control('Addresses.1.title');
  1436. $this->Form->control('Addresses.1.first_name');
  1437. $this->Form->control('Addresses.1.last_name');
  1438. $this->Form->control('Addresses.1.address');
  1439. $this->Form->control('Addresses.1.city');
  1440. $this->Form->control('Addresses.1.phone');
  1441. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1442. $result = $this->Form->secure($this->Form->fields);
  1443. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1444. $tokenDebug = urlencode(json_encode([
  1445. '/articles/add',
  1446. [
  1447. 'Addresses.0.id' => '123456',
  1448. 'Addresses.0.title',
  1449. 'Addresses.0.first_name',
  1450. 'Addresses.0.last_name',
  1451. 'Addresses.0.address',
  1452. 'Addresses.0.city',
  1453. 'Addresses.0.phone',
  1454. 'Addresses.0.primary',
  1455. 'Addresses.1.id' => '654321',
  1456. 'Addresses.1.title',
  1457. 'Addresses.1.first_name',
  1458. 'Addresses.1.last_name',
  1459. 'Addresses.1.address',
  1460. 'Addresses.1.city',
  1461. 'Addresses.1.phone',
  1462. 'Addresses.1.primary',
  1463. ],
  1464. []
  1465. ]));
  1466. $expected = [
  1467. 'div' => ['style' => 'display:none;'],
  1468. ['input' => [
  1469. 'type' => 'hidden',
  1470. 'name' => '_Token[fields]',
  1471. 'value' => $hash,
  1472. 'autocomplete' => 'off',
  1473. ]],
  1474. ['input' => [
  1475. 'type' => 'hidden',
  1476. 'name' => '_Token[unlocked]',
  1477. 'autocomplete' => 'off',
  1478. 'value' => ''
  1479. ]],
  1480. ['input' => [
  1481. 'type' => 'hidden',
  1482. 'name' => '_Token[debug]',
  1483. 'value' => $tokenDebug,
  1484. 'autocomplete' => 'off',
  1485. ]],
  1486. '/div'
  1487. ];
  1488. $this->assertHtml($expected, $result);
  1489. }
  1490. /**
  1491. * testFormSecurityArrayFields method
  1492. *
  1493. * Test form security with Model.field.0 style inputs.
  1494. *
  1495. * @return void
  1496. */
  1497. public function testFormSecurityArrayFields()
  1498. {
  1499. $this->Form->request->params['_Token'] = 'testKey';
  1500. $this->Form->create();
  1501. $this->Form->text('Address.primary.1');
  1502. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1503. $this->Form->text('Address.secondary.1.0');
  1504. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1505. }
  1506. /**
  1507. * testFormSecurityMultipleControlDisabledFields method
  1508. *
  1509. * Test secure form generation with multiple records and disabled fields.
  1510. *
  1511. * @return void
  1512. */
  1513. public function testFormSecurityMultipleControlDisabledFields()
  1514. {
  1515. $this->Form->request->params['_Token'] = [
  1516. 'unlockedFields' => ['first_name', 'address']
  1517. ];
  1518. $this->Form->create();
  1519. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1520. $this->Form->text('Addresses.0.title');
  1521. $this->Form->text('Addresses.0.first_name');
  1522. $this->Form->text('Addresses.0.last_name');
  1523. $this->Form->text('Addresses.0.address');
  1524. $this->Form->text('Addresses.0.city');
  1525. $this->Form->text('Addresses.0.phone');
  1526. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1527. $this->Form->text('Addresses.1.title');
  1528. $this->Form->text('Addresses.1.first_name');
  1529. $this->Form->text('Addresses.1.last_name');
  1530. $this->Form->text('Addresses.1.address');
  1531. $this->Form->text('Addresses.1.city');
  1532. $this->Form->text('Addresses.1.phone');
  1533. $result = $this->Form->secure($this->Form->fields);
  1534. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1535. $tokenDebug = urlencode(json_encode([
  1536. '/articles/add',
  1537. [
  1538. 'Addresses.0.id' => '123456',
  1539. 'Addresses.0.title',
  1540. 'Addresses.0.last_name',
  1541. 'Addresses.0.city',
  1542. 'Addresses.0.phone',
  1543. 'Addresses.1.id' => '654321',
  1544. 'Addresses.1.title',
  1545. 'Addresses.1.last_name',
  1546. 'Addresses.1.city',
  1547. 'Addresses.1.phone'
  1548. ],
  1549. [
  1550. 'first_name',
  1551. 'address'
  1552. ]
  1553. ]));
  1554. $expected = [
  1555. 'div' => ['style' => 'display:none;'],
  1556. ['input' => [
  1557. 'type' => 'hidden',
  1558. 'name' => '_Token[fields]',
  1559. 'autocomplete' => 'off',
  1560. 'value' => $hash
  1561. ]],
  1562. ['input' => [
  1563. 'type' => 'hidden',
  1564. 'name' => '_Token[unlocked]',
  1565. 'autocomplete' => 'off',
  1566. 'value' => 'address%7Cfirst_name',
  1567. ]],
  1568. ['input' => [
  1569. 'type' => 'hidden',
  1570. 'name' => '_Token[debug]',
  1571. 'autocomplete' => 'off',
  1572. 'value' => $tokenDebug,
  1573. ]],
  1574. '/div'
  1575. ];
  1576. $this->assertHtml($expected, $result);
  1577. }
  1578. /**
  1579. * testFormSecurityControlDisabledFields method
  1580. *
  1581. * Test single record form with disabled fields.
  1582. *
  1583. * @return void
  1584. */
  1585. public function testFormSecurityControlUnlockedFields()
  1586. {
  1587. $this->Form->request->params['_Token'] = [
  1588. 'unlockedFields' => ['first_name', 'address']
  1589. ];
  1590. $this->Form->create();
  1591. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1592. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1593. $this->Form->text('Addresses.title');
  1594. $this->Form->text('Addresses.first_name');
  1595. $this->Form->text('Addresses.last_name');
  1596. $this->Form->text('Addresses.address');
  1597. $this->Form->text('Addresses.city');
  1598. $this->Form->text('Addresses.phone');
  1599. $result = $this->Form->fields;
  1600. $expected = [
  1601. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1602. 'Addresses.city', 'Addresses.phone'
  1603. ];
  1604. $this->assertEquals($expected, $result);
  1605. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1606. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1607. $tokenDebug = urlencode(json_encode([
  1608. '/articles/add',
  1609. [
  1610. 'Addresses.id' => '123456',
  1611. 'Addresses.title',
  1612. 'Addresses.last_name',
  1613. 'Addresses.city',
  1614. 'Addresses.phone'
  1615. ],
  1616. [
  1617. 'first_name',
  1618. 'address'
  1619. ]
  1620. ]));
  1621. $expected = [
  1622. 'div' => ['style' => 'display:none;'],
  1623. ['input' => [
  1624. 'type' => 'hidden',
  1625. 'name' => '_Token[fields]',
  1626. 'value' => $hash,
  1627. 'autocomplete' => 'off',
  1628. 'data-foo' => 'bar',
  1629. ]],
  1630. ['input' => [
  1631. 'type' => 'hidden',
  1632. 'name' => '_Token[unlocked]',
  1633. 'value' => 'address%7Cfirst_name',
  1634. 'autocomplete' => 'off',
  1635. 'data-foo' => 'bar',
  1636. ]],
  1637. ['input' => [
  1638. 'type' => 'hidden', 'name' => '_Token[debug]',
  1639. 'value' => $tokenDebug,
  1640. 'autocomplete' => 'off',
  1641. 'data-foo' => 'bar'
  1642. ]],
  1643. '/div'
  1644. ];
  1645. $this->assertHtml($expected, $result);
  1646. }
  1647. /**
  1648. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1649. *
  1650. * Test single record form with debugSecurity param.
  1651. *
  1652. * @return void
  1653. */
  1654. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1655. {
  1656. $this->Form->request->params['_Token'] = [
  1657. 'unlockedFields' => ['first_name', 'address']
  1658. ];
  1659. $this->Form->create();
  1660. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1661. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1662. $this->Form->text('Addresses.title');
  1663. $this->Form->text('Addresses.first_name');
  1664. $this->Form->text('Addresses.last_name');
  1665. $this->Form->text('Addresses.address');
  1666. $this->Form->text('Addresses.city');
  1667. $this->Form->text('Addresses.phone');
  1668. $result = $this->Form->fields;
  1669. $expected = [
  1670. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1671. 'Addresses.city', 'Addresses.phone'
  1672. ];
  1673. $this->assertEquals($expected, $result);
  1674. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1675. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1676. $tokenDebug = urlencode(json_encode([
  1677. '/articles/add',
  1678. [
  1679. 'Addresses.id' => '123456',
  1680. 'Addresses.title',
  1681. 'Addresses.last_name',
  1682. 'Addresses.city',
  1683. 'Addresses.phone'
  1684. ],
  1685. [
  1686. 'first_name',
  1687. 'address'
  1688. ]
  1689. ]));
  1690. $expected = [
  1691. 'div' => ['style' => 'display:none;'],
  1692. ['input' => [
  1693. 'type' => 'hidden',
  1694. 'name' => '_Token[fields]',
  1695. 'value' => $hash,
  1696. 'autocomplete' => 'off',
  1697. 'data-foo' => 'bar',
  1698. ]],
  1699. ['input' => [
  1700. 'type' => 'hidden',
  1701. 'name' => '_Token[unlocked]',
  1702. 'value' => 'address%7Cfirst_name',
  1703. 'autocomplete' => 'off',
  1704. 'data-foo' => 'bar',
  1705. ]],
  1706. ['input' => [
  1707. 'type' => 'hidden', 'name' => '_Token[debug]',
  1708. 'value' => $tokenDebug,
  1709. 'autocomplete' => 'off',
  1710. 'data-foo' => 'bar'
  1711. ]],
  1712. '/div'
  1713. ];
  1714. $this->assertHtml($expected, $result);
  1715. }
  1716. /**
  1717. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1718. *
  1719. * Debug is false, debugSecurity is true -> no debug
  1720. *
  1721. * @return void
  1722. */
  1723. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1724. {
  1725. $this->Form->request->params['_Token'] = [
  1726. 'unlockedFields' => ['first_name', 'address']
  1727. ];
  1728. $this->Form->create();
  1729. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1730. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1731. $this->Form->text('Addresses.title');
  1732. $this->Form->text('Addresses.first_name');
  1733. $this->Form->text('Addresses.last_name');
  1734. $this->Form->text('Addresses.address');
  1735. $this->Form->text('Addresses.city');
  1736. $this->Form->text('Addresses.phone');
  1737. $result = $this->Form->fields;
  1738. $expected = [
  1739. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1740. 'Addresses.city', 'Addresses.phone'
  1741. ];
  1742. $this->assertEquals($expected, $result);
  1743. Configure::write('debug', false);
  1744. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1745. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1746. $expected = [
  1747. 'div' => ['style' => 'display:none;'],
  1748. ['input' => [
  1749. 'type' => 'hidden',
  1750. 'name' => '_Token[fields]',
  1751. 'value' => $hash,
  1752. 'autocomplete' => 'off',
  1753. 'data-foo' => 'bar',
  1754. ]],
  1755. ['input' => [
  1756. 'type' => 'hidden',
  1757. 'name' => '_Token[unlocked]',
  1758. 'value' => 'address%7Cfirst_name',
  1759. 'autocomplete' => 'off',
  1760. 'data-foo' => 'bar',
  1761. ]],
  1762. '/div'
  1763. ];
  1764. $this->assertHtml($expected, $result);
  1765. }
  1766. /**
  1767. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1768. *
  1769. * Test single record form with debugSecurity param.
  1770. *
  1771. * @return void
  1772. */
  1773. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1774. {
  1775. $this->Form->request->params['_Token'] = [
  1776. 'unlockedFields' => ['first_name', 'address']
  1777. ];
  1778. $this->Form->create();
  1779. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1780. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1781. $this->Form->text('Addresses.title');
  1782. $this->Form->text('Addresses.first_name');
  1783. $this->Form->text('Addresses.last_name');
  1784. $this->Form->text('Addresses.address');
  1785. $this->Form->text('Addresses.city');
  1786. $this->Form->text('Addresses.phone');
  1787. $result = $this->Form->fields;
  1788. $expected = [
  1789. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1790. 'Addresses.city', 'Addresses.phone'
  1791. ];
  1792. $this->assertEquals($expected, $result);
  1793. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1794. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1795. $expected = [
  1796. 'div' => ['style' => 'display:none;'],
  1797. ['input' => [
  1798. 'type' => 'hidden',
  1799. 'name' => '_Token[fields]',
  1800. 'value' => $hash,
  1801. 'autocomplete' => 'off',
  1802. 'data-foo' => 'bar',
  1803. ]],
  1804. ['input' => [
  1805. 'type' => 'hidden',
  1806. 'name' => '_Token[unlocked]',
  1807. 'value' => 'address%7Cfirst_name',
  1808. 'autocomplete' => 'off',
  1809. 'data-foo' => 'bar',
  1810. ]],
  1811. '/div'
  1812. ];
  1813. $this->assertHtml($expected, $result);
  1814. }
  1815. /**
  1816. * testFormSecureWithCustomNameAttribute method
  1817. *
  1818. * Test securing inputs with custom name attributes.
  1819. *
  1820. * @return void
  1821. */
  1822. public function testFormSecureWithCustomNameAttribute()
  1823. {
  1824. $this->Form->request->params['_Token'] = 'testKey';
  1825. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1826. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1827. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1828. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1829. }
  1830. /**
  1831. * testFormSecuredControl method
  1832. *
  1833. * Test generation of entire secure form, assertions made on control() output.
  1834. *
  1835. * @return void
  1836. */
  1837. public function testFormSecuredControl()
  1838. {
  1839. $this->Form->request->params['_csrfToken'] = 'testKey';
  1840. $this->Form->request->params['_Token'] = 'stuff';
  1841. $this->article['schema'] = [
  1842. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1843. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1844. ];
  1845. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1846. $encoding = strtolower(Configure::read('App.encoding'));
  1847. $expected = [
  1848. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1849. 'div' => ['style' => 'display:none;'],
  1850. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1851. ['input' => [
  1852. 'type' => 'hidden',
  1853. 'name' => '_csrfToken',
  1854. 'value' => 'testKey',
  1855. 'autocomplete' => 'off'
  1856. ]],
  1857. '/div'
  1858. ];
  1859. $this->assertHtml($expected, $result);
  1860. $result = $this->Form->control('ratio');
  1861. $expected = [
  1862. 'div' => ['class'],
  1863. 'label' => ['for'],
  1864. 'Ratio',
  1865. '/label',
  1866. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1867. '/div',
  1868. ];
  1869. $this->assertHtml($expected, $result);
  1870. $result = $this->Form->control('population');
  1871. $expected = [
  1872. 'div' => ['class'],
  1873. 'label' => ['for'],
  1874. 'Population',
  1875. '/label',
  1876. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1877. '/div',
  1878. ];
  1879. $this->assertHtml($expected, $result);
  1880. $result = $this->Form->control('published', ['type' => 'text']);
  1881. $expected = [
  1882. 'div' => ['class' => 'input text'],
  1883. 'label' => ['for' => 'published'],
  1884. 'Published',
  1885. '/label',
  1886. ['input' => [
  1887. 'type' => 'text',
  1888. 'name' => 'published',
  1889. 'id' => 'published'
  1890. ]],
  1891. '/div'
  1892. ];
  1893. $this->assertHtml($expected, $result);
  1894. $result = $this->Form->control('other', ['type' => 'text']);
  1895. $expected = [
  1896. 'div' => ['class' => 'input text'],
  1897. 'label' => ['for' => 'other'],
  1898. 'Other',
  1899. '/label',
  1900. ['input' => [
  1901. 'type' => 'text',
  1902. 'name' => 'other',
  1903. 'id',
  1904. ]],
  1905. '/div'
  1906. ];
  1907. $this->assertHtml($expected, $result);
  1908. $result = $this->Form->hidden('stuff');
  1909. $expected = [
  1910. 'input' => [
  1911. 'type' => 'hidden',
  1912. 'name' => 'stuff'
  1913. ]
  1914. ];
  1915. $this->assertHtml($expected, $result);
  1916. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1917. $expected = ['input' => [
  1918. 'type' => 'hidden',
  1919. 'name' => 'hidden',
  1920. 'value' => '0'
  1921. ]];
  1922. $this->assertHtml($expected, $result);
  1923. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1924. $expected = [
  1925. 'div' => ['class' => 'input checkbox'],
  1926. ['input' => [
  1927. 'type' => 'hidden',
  1928. 'name' => 'something',
  1929. 'value' => '0'
  1930. ]],
  1931. 'label' => ['for' => 'something'],
  1932. ['input' => [
  1933. 'type' => 'checkbox',
  1934. 'name' => 'something',
  1935. 'value' => '1',
  1936. 'id' => 'something'
  1937. ]],
  1938. 'Something',
  1939. '/label',
  1940. '/div'
  1941. ];
  1942. $this->assertHtml($expected, $result);
  1943. $result = $this->Form->fields;
  1944. $expectedFields = [
  1945. 'ratio',
  1946. 'population',
  1947. 'published',
  1948. 'other',
  1949. 'stuff' => '',
  1950. 'hidden' => '0',
  1951. 'something'
  1952. ];
  1953. $this->assertEquals($expectedFields, $result);
  1954. $result = $this->Form->secure($this->Form->fields);
  1955. $tokenDebug = urlencode(json_encode([
  1956. '/articles/add',
  1957. $expectedFields,
  1958. []
  1959. ]));
  1960. $expected = [
  1961. 'div' => ['style' => 'display:none;'],
  1962. ['input' => [
  1963. 'type' => 'hidden',
  1964. 'name' => '_Token[fields]',
  1965. 'value',
  1966. 'autocomplete'
  1967. ]],
  1968. ['input' => [
  1969. 'type' => 'hidden',
  1970. 'name' => '_Token[unlocked]',
  1971. 'value' => '',
  1972. 'autocomplete' => 'off'
  1973. ]],
  1974. ['input' => [
  1975. 'type' => 'hidden', 'name' => '_Token[debug]',
  1976. 'value' => $tokenDebug,
  1977. 'autocomplete' => 'off'
  1978. ]],
  1979. '/div'
  1980. ];
  1981. $this->assertHtml($expected, $result);
  1982. }
  1983. /**
  1984. * testSecuredControlCustomName method
  1985. *
  1986. * Test secured inputs with custom names.
  1987. *
  1988. * @return void
  1989. */
  1990. public function testSecuredControlCustomName()
  1991. {
  1992. $this->Form->request->params['_Token'] = 'testKey';
  1993. $this->assertEquals([], $this->Form->fields);
  1994. $this->Form->text('text_input', [
  1995. 'name' => 'Option[General.default_role]',
  1996. ]);
  1997. $expected = ['Option.General.default_role'];
  1998. $this->assertEquals($expected, $this->Form->fields);
  1999. $this->Form->select('select_box', [1, 2], [
  2000. 'name' => 'Option[General.select_role]',
  2001. ]);
  2002. $expected[] = 'Option.General.select_role';
  2003. $this->assertEquals($expected, $this->Form->fields);
  2004. $this->Form->text('other.things[]');
  2005. $expected[] = 'other.things';
  2006. $this->assertEquals($expected, $this->Form->fields);
  2007. }
  2008. /**
  2009. * testSecuredControlDuplicate method
  2010. *
  2011. * Test that a hidden field followed by a visible field
  2012. * undoes the hidden field locking.
  2013. *
  2014. * @return void
  2015. */
  2016. public function testSecuredControlDuplicate()
  2017. {
  2018. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2019. $this->assertEquals([], $this->Form->fields);
  2020. $this->Form->control('text_val', [
  2021. 'type' => 'hidden',
  2022. 'value' => 'some text',
  2023. ]);
  2024. $expected = ['text_val' => 'some text'];
  2025. $this->assertEquals($expected, $this->Form->fields);
  2026. $this->Form->control('text_val', [
  2027. 'type' => 'text',
  2028. ]);
  2029. $expected = ['text_val'];
  2030. $this->assertEquals($expected, $this->Form->fields);
  2031. }
  2032. /**
  2033. * testFormSecuredFileControl method
  2034. *
  2035. * Tests that the correct keys are added to the field hash index.
  2036. *
  2037. * @return void
  2038. */
  2039. public function testFormSecuredFileControl()
  2040. {
  2041. $this->assertEquals([], $this->Form->fields);
  2042. $this->Form->file('Attachment.file');
  2043. $expected = [
  2044. 'Attachment.file.name', 'Attachment.file.type',
  2045. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2046. 'Attachment.file.size'
  2047. ];
  2048. $this->assertEquals($expected, $this->Form->fields);
  2049. }
  2050. /**
  2051. * testFormSecuredMultipleSelect method
  2052. *
  2053. * Test that multiple selects keys are added to field hash.
  2054. *
  2055. * @return void
  2056. */
  2057. public function testFormSecuredMultipleSelect()
  2058. {
  2059. $this->Form->request->params['_csrfToken'] = 'testKey';
  2060. $this->assertEquals([], $this->Form->fields);
  2061. $options = ['1' => 'one', '2' => 'two'];
  2062. $this->Form->select('Model.select', $options);
  2063. $expected = ['Model.select'];
  2064. $this->assertEquals($expected, $this->Form->fields);
  2065. $this->Form->fields = [];
  2066. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2067. $this->assertEquals($expected, $this->Form->fields);
  2068. }
  2069. /**
  2070. * testFormSecuredRadio method
  2071. *
  2072. * @return void
  2073. */
  2074. public function testFormSecuredRadio()
  2075. {
  2076. $this->Form->request->params['_Token'] = 'testKey';
  2077. $this->assertEquals([], $this->Form->fields);
  2078. $options = ['1' => 'option1', '2' => 'option2'];
  2079. $this->Form->radio('Test.test', $options);
  2080. $expected = ['Test.test'];
  2081. $this->assertEquals($expected, $this->Form->fields);
  2082. $this->Form->radio('Test.all', $options, [
  2083. 'disabled' => ['option1', 'option2']
  2084. ]);
  2085. $expected = ['Test.test', 'Test.all' => ''];
  2086. $this->assertEquals($expected, $this->Form->fields);
  2087. $this->Form->radio('Test.some', $options, [
  2088. 'disabled' => ['option1']
  2089. ]);
  2090. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2091. $this->assertEquals($expected, $this->Form->fields);
  2092. }
  2093. /**
  2094. * testFormSecuredAndDisabledNotAssoc method
  2095. *
  2096. * Test that when disabled is in a list based attribute array it works.
  2097. *
  2098. * @return void
  2099. */
  2100. public function testFormSecuredAndDisabledNotAssoc()
  2101. {
  2102. $this->Form->request->params['_Token'] = 'testKey';
  2103. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2104. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2105. $this->Form->text('Model.text', ['disabled']);
  2106. $this->Form->textarea('Model.textarea', ['disabled']);
  2107. $this->Form->password('Model.password', ['disabled']);
  2108. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2109. $expected = [
  2110. 'Model.radio' => ''
  2111. ];
  2112. $this->assertEquals($expected, $this->Form->fields);
  2113. }
  2114. /**
  2115. * testFormSecuredAndDisabled method
  2116. *
  2117. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2118. * hashing.
  2119. *
  2120. * @return void
  2121. */
  2122. public function testFormSecuredAndDisabled()
  2123. {
  2124. $this->Form->request->params['_Token'] = 'testKey';
  2125. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2126. $this->Form->text('Model.text', ['disabled' => true]);
  2127. $this->Form->password('Model.text', ['disabled' => true]);
  2128. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2129. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2130. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2131. $this->Form->year('Model.year', ['disabled' => true]);
  2132. $this->Form->month('Model.month', ['disabled' => true]);
  2133. $this->Form->day('Model.day', ['disabled' => true]);
  2134. $this->Form->hour('Model.hour', ['disabled' => true]);
  2135. $this->Form->minute('Model.minute', ['disabled' => true]);
  2136. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2137. $expected = [
  2138. 'Model.radio' => ''
  2139. ];
  2140. $this->assertEquals($expected, $this->Form->fields);
  2141. }
  2142. /**
  2143. * testDisableSecurityUsingForm method
  2144. *
  2145. * @return void
  2146. */
  2147. public function testDisableSecurityUsingForm()
  2148. {
  2149. $this->Form->request->params['_Token'] = [
  2150. 'disabledFields' => []
  2151. ];
  2152. $this->Form->create();
  2153. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2154. $this->Form->text('Addresses.title');
  2155. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2156. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2157. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2158. $result = $this->Form->fields;
  2159. $expected = [
  2160. 'Addresses.id' => '123456', 'Addresses.title',
  2161. ];
  2162. $this->assertEquals($expected, $result);
  2163. }
  2164. /**
  2165. * testUnlockFieldAddsToList method
  2166. *
  2167. * Test disableField.
  2168. *
  2169. * @return void
  2170. */
  2171. public function testUnlockFieldAddsToList()
  2172. {
  2173. $this->Form->request->params['_Token'] = [
  2174. 'unlockedFields' => []
  2175. ];
  2176. $this->Form->unlockField('Contact.name');
  2177. $this->Form->text('Contact.name');
  2178. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2179. $this->assertEquals([], $this->Form->fields);
  2180. }
  2181. /**
  2182. * testUnlockFieldRemovingFromFields method
  2183. *
  2184. * Test unlockField removing from fields array.
  2185. *
  2186. * @return void
  2187. */
  2188. public function testUnlockFieldRemovingFromFields()
  2189. {
  2190. $this->Form->request->params['_Token'] = [
  2191. 'unlockedFields' => []
  2192. ];
  2193. $this->Form->create($this->article);
  2194. $this->Form->hidden('Article.id', ['value' => 1]);
  2195. $this->Form->text('Article.title');
  2196. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2197. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2198. $this->Form->unlockField('Article.title');
  2199. $this->Form->unlockField('Article.id');
  2200. $this->assertEquals([], $this->Form->fields);
  2201. }
  2202. /**
  2203. * testResetUnlockFields method
  2204. *
  2205. * Test reset unlockFields, when create new form.
  2206. *
  2207. * @return void
  2208. */
  2209. public function testResetUnlockFields()
  2210. {
  2211. $this->Form->request->params['_Token'] = [
  2212. 'key' => 'testKey',
  2213. 'unlockedFields' => []
  2214. ];
  2215. $this->Form->unlockField('Contact.id');
  2216. $this->Form->create('Contact');
  2217. $this->Form->hidden('Contact.id', ['value' => 1]);
  2218. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2219. $this->Form->end();
  2220. $this->Form->create('Contact');
  2221. $this->Form->hidden('Contact.id', ['value' => 1]);
  2222. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2223. }
  2224. /**
  2225. * testSecuredFormUrlIgnoresHost method
  2226. *
  2227. * Test that only the path + query elements of a form's URL show up in their hash.
  2228. *
  2229. * @return void
  2230. */
  2231. public function testSecuredFormUrlIgnoresHost()
  2232. {
  2233. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2234. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2235. $this->Form->create($this->article, [
  2236. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2237. ]);
  2238. $result = $this->Form->secure();
  2239. $this->assertContains($expected, $result);
  2240. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2241. $result = $this->Form->secure();
  2242. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2243. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2244. $result = $this->Form->secure();
  2245. $this->assertContains($expected, $result, 'URL path + query should work.');
  2246. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2247. $result = $this->Form->secure();
  2248. $this->assertNotContains($expected, $result, 'URL is different');
  2249. }
  2250. /**
  2251. * testSecuredFormUrlHasHtmlAndIdentifier method
  2252. *
  2253. * Test that URL, HTML and identifier show up in their hashes.
  2254. *
  2255. * @return void
  2256. */
  2257. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2258. {
  2259. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2260. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2261. $this->Form->create($this->article, [
  2262. 'url' => [
  2263. 'controller' => 'articles',
  2264. 'action' => 'view',
  2265. '?' => [
  2266. 'page' => 1,
  2267. 'limit' => 10,
  2268. 'html' => '<>"',
  2269. ],
  2270. '#' => 'result',
  2271. ],
  2272. ]);
  2273. $result = $this->Form->secure();
  2274. $this->assertContains($expected, $result);
  2275. $this->Form->create($this->article, [
  2276. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2277. ]);
  2278. $result = $this->Form->secure();
  2279. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2280. $this->Form->create($this->article, [
  2281. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2282. ]);
  2283. $result = $this->Form->secure();
  2284. $this->assertContains($expected, $result, 'URL path + query should work.');
  2285. }
  2286. /**
  2287. * testErrorMessageDisplay method
  2288. *
  2289. * Test error message display.
  2290. *
  2291. * @return void
  2292. */
  2293. public function testErrorMessageDisplay()
  2294. {
  2295. $this->article['errors'] = [
  2296. 'Article' => [
  2297. 'title' => 'error message',
  2298. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2299. ]
  2300. ];
  2301. $this->Form->create($this->article);
  2302. $result = $this->Form->control('Article.title');
  2303. $expected = [
  2304. 'div' => ['class' => 'input text error'],
  2305. 'label' => ['for' => 'article-title'],
  2306. 'Title',
  2307. '/label',
  2308. 'input' => [
  2309. 'type' => 'text', 'name' => 'Article[title]',
  2310. 'id' => 'article-title', 'class' => 'form-error'
  2311. ],
  2312. ['div' => ['class' => 'error-message']],
  2313. 'error message',
  2314. '/div',
  2315. '/div'
  2316. ];
  2317. $this->assertHtml($expected, $result);
  2318. $result = $this->Form->control('Article.title', [
  2319. 'templates' => [
  2320. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2321. ]
  2322. ]);
  2323. $expected = [
  2324. 'div' => ['class' => 'input text error'],
  2325. 'label' => ['for' => 'article-title'],
  2326. 'Title',
  2327. '/label',
  2328. 'input' => [
  2329. 'type' => 'text', 'name' => 'Article[title]',
  2330. 'id' => 'article-title', 'class' => 'form-error'
  2331. ],
  2332. '/div'
  2333. ];
  2334. $this->assertHtml($expected, $result);
  2335. $result = $this->Form->control('Article.content');
  2336. $expected = [
  2337. 'div' => ['class' => 'input text error'],
  2338. 'label' => ['for' => 'article-content'],
  2339. 'Content',
  2340. '/label',
  2341. 'input' => [
  2342. 'type' => 'text', 'name' => 'Article[content]',
  2343. 'id' => 'article-content', 'class' => 'form-error'
  2344. ],
  2345. ['div' => ['class' => 'error-message']],
  2346. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2347. '/div',
  2348. '/div'
  2349. ];
  2350. $this->assertHtml($expected, $result);
  2351. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2352. $expected = [
  2353. 'div' => ['class' => 'input text error'],
  2354. 'label' => ['for' => 'article-content'],
  2355. 'Content',
  2356. '/label',
  2357. 'input' => [
  2358. 'type' => 'text', 'name' => 'Article[content]',
  2359. 'id' => 'article-content', 'class' => 'form-error'
  2360. ],
  2361. ['div' => ['class' => 'error-message']],
  2362. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2363. '/div',
  2364. '/div'
  2365. ];
  2366. $this->assertHtml($expected, $result);
  2367. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2368. $expected = [
  2369. 'div' => ['class' => 'input text error'],
  2370. 'label' => ['for' => 'article-content'],
  2371. 'Content',
  2372. '/label',
  2373. 'input' => [
  2374. 'type' => 'text', 'name' => 'Article[content]',
  2375. 'id' => 'article-content', 'class' => 'form-error'
  2376. ],
  2377. ['div' => ['class' => 'error-message']],
  2378. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2379. '/div',
  2380. '/div'
  2381. ];
  2382. $this->assertHtml($expected, $result);
  2383. }
  2384. /**
  2385. * testEmptyErrorValidation method
  2386. *
  2387. * Test validation errors, when validation message is an empty string.
  2388. *
  2389. * @return void
  2390. */
  2391. public function testEmptyErrorValidation()
  2392. {
  2393. $this->article['errors'] = [
  2394. 'Article' => ['title' => '']
  2395. ];
  2396. $this->Form->create($this->article);
  2397. $result = $this->Form->control('Article.title');
  2398. $expected = [
  2399. 'div' => ['class' => 'input text error'],
  2400. 'label' => ['for' => 'article-title'],
  2401. 'Title',
  2402. '/label',
  2403. 'input' => [
  2404. 'type' => 'text', 'name' => 'Article[title]',
  2405. 'id' => 'article-title', 'class' => 'form-error'
  2406. ],
  2407. ['div' => ['class' => 'error-message']],
  2408. [],
  2409. '/div',
  2410. '/div'
  2411. ];
  2412. $this->assertHtml($expected, $result);
  2413. }
  2414. /**
  2415. * testEmptyControlErrorValidation method
  2416. *
  2417. * Test validation errors, when calling control() overriding validation message by an empty string.
  2418. *
  2419. * @return void
  2420. */
  2421. public function testEmptyControlErrorValidation()
  2422. {
  2423. $this->article['errors'] = [
  2424. 'Article' => ['title' => 'error message']
  2425. ];
  2426. $this->Form->create($this->article);
  2427. $result = $this->Form->control('Article.title', ['error' => '']);
  2428. $expected = [
  2429. 'div' => ['class' => 'input text error'],
  2430. 'label' => ['for' => 'article-title'],
  2431. 'Title',
  2432. '/label',
  2433. 'input' => [
  2434. 'type' => 'text', 'name' => 'Article[title]',
  2435. 'id' => 'article-title', 'class' => 'form-error'
  2436. ],
  2437. ['div' => ['class' => 'error-message']],
  2438. [],
  2439. '/div',
  2440. '/div'
  2441. ];
  2442. $this->assertHtml($expected, $result);
  2443. }
  2444. /**
  2445. * testControlErrorMessage method
  2446. *
  2447. * Test validation errors, when calling control() overriding validation messages.
  2448. *
  2449. * @return void
  2450. */
  2451. public function testControlErrorMessage()
  2452. {
  2453. $this->article['errors'] = [
  2454. 'title' => ['error message']
  2455. ];
  2456. $this->Form->create($this->article);
  2457. $result = $this->Form->control('title', [
  2458. 'error' => 'Custom error!'
  2459. ]);
  2460. $expected = [
  2461. 'div' => ['class' => 'input text required error'],
  2462. 'label' => ['for' => 'title'],
  2463. 'Title',
  2464. '/label',
  2465. 'input' => [
  2466. 'type' => 'text', 'name' => 'title',
  2467. 'id' => 'title', 'class' => 'form-error',
  2468. 'required' => 'required',
  2469. ],
  2470. ['div' => ['class' => 'error-message']],
  2471. 'Custom error!',
  2472. '/div',
  2473. '/div'
  2474. ];
  2475. $this->assertHtml($expected, $result);
  2476. $result = $this->Form->control('title', [
  2477. 'error' => ['error message' => 'Custom error!']
  2478. ]);
  2479. $expected = [
  2480. 'div' => ['class' => 'input text required error'],
  2481. 'label' => ['for' => 'title'],
  2482. 'Title',
  2483. '/label',
  2484. 'input' => [
  2485. 'type' => 'text',
  2486. 'name' => 'title',
  2487. 'id' => 'title',
  2488. 'class' => 'form-error',
  2489. 'required' => 'required'
  2490. ],
  2491. ['div' => ['class' => 'error-message']],
  2492. 'Custom error!',
  2493. '/div',
  2494. '/div'
  2495. ];
  2496. $this->assertHtml($expected, $result);
  2497. }
  2498. /**
  2499. * testFormValidationAssociated method
  2500. *
  2501. * Tests displaying errors for nested entities.
  2502. *
  2503. * @return void
  2504. */
  2505. public function testFormValidationAssociated()
  2506. {
  2507. $nested = new Entity(['foo' => 'bar']);
  2508. $nested->errors('foo', ['not a valid bar']);
  2509. $entity = new Entity(['nested' => $nested]);
  2510. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2511. $result = $this->Form->error('nested.foo');
  2512. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2513. }
  2514. /**
  2515. * testFormValidationAssociatedSecondLevel method
  2516. *
  2517. * Test form error display with associated model.
  2518. *
  2519. * @return void
  2520. */
  2521. public function testFormValidationAssociatedSecondLevel()
  2522. {
  2523. $inner = new Entity(['bar' => 'baz']);
  2524. $nested = new Entity(['foo' => $inner]);
  2525. $entity = new Entity(['nested' => $nested]);
  2526. $inner->errors('bar', ['not a valid one']);
  2527. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2528. $result = $this->Form->error('nested.foo.bar');
  2529. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2530. }
  2531. /**
  2532. * testFormValidationMultiRecord method
  2533. *
  2534. * Test form error display with multiple records.
  2535. *
  2536. * @return void
  2537. */
  2538. public function testFormValidationMultiRecord()
  2539. {
  2540. $one = new Entity();
  2541. $two = new Entity();
  2542. TableRegistry::get('Contacts', [
  2543. 'className' => __NAMESPACE__ . '\ContactsTable'
  2544. ]);
  2545. $one->set('email', '');
  2546. $one->errors('email', ['invalid email']);
  2547. $two->set('name', '');
  2548. $two->errors('name', ['This is wrong']);
  2549. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2550. $result = $this->Form->control('0.email');
  2551. $expected = [
  2552. 'div' => ['class' => 'input email error'],
  2553. 'label' => ['for' => '0-email'],
  2554. 'Email',
  2555. '/label',
  2556. 'input' => [
  2557. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2558. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2559. ],
  2560. ['div' => ['class' => 'error-message']],
  2561. 'invalid email',
  2562. '/div',
  2563. '/div'
  2564. ];
  2565. $this->assertHtml($expected, $result);
  2566. $result = $this->Form->control('1.name');
  2567. $expected = [
  2568. 'div' => ['class' => 'input text error'],
  2569. 'label' => ['for' => '1-name'],
  2570. 'Name',
  2571. '/label',
  2572. 'input' => [
  2573. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2574. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2575. ],
  2576. ['div' => ['class' => 'error-message']],
  2577. 'This is wrong',
  2578. '/div',
  2579. '/div'
  2580. ];
  2581. $this->assertHtml($expected, $result);
  2582. }
  2583. /**
  2584. * testControl method
  2585. *
  2586. * Test various incarnations of control().
  2587. *
  2588. * @return void
  2589. */
  2590. public function testControl()
  2591. {
  2592. TableRegistry::get('ValidateUsers', [
  2593. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2594. ]);
  2595. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2596. $result = $this->Form->control('ValidateUsers.balance');
  2597. $expected = [
  2598. 'div' => ['class'],
  2599. 'label' => ['for'],
  2600. 'Balance',
  2601. '/label',
  2602. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2603. '/div',
  2604. ];
  2605. $this->assertHtml($expected, $result);
  2606. $result = $this->Form->control('ValidateUser.cost_decimal');
  2607. $expected = [
  2608. 'div' => ['class'],
  2609. 'label' => ['for'],
  2610. 'Cost Decimal',
  2611. '/label',
  2612. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2613. '/div',
  2614. ];
  2615. $this->assertHtml($expected, $result);
  2616. $result = $this->Form->control('ValidateUser.null_decimal');
  2617. $expected = [
  2618. 'div' => ['class'],
  2619. 'label' => ['for'],
  2620. 'Null Decimal',
  2621. '/label',
  2622. 'input' => ['name', 'type' => 'number', 'id'],
  2623. '/div',
  2624. ];
  2625. $this->assertHtml($expected, $result);
  2626. }
  2627. /**
  2628. * testControlCustomization method
  2629. *
  2630. * Tests the input method and passing custom options.
  2631. *
  2632. * @return void
  2633. */
  2634. public function testControlCustomization()
  2635. {
  2636. TableRegistry::get('Contacts', [
  2637. 'className' => __NAMESPACE__ . '\ContactsTable'
  2638. ]);
  2639. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2640. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2641. $expected = [
  2642. 'div' => ['class' => 'input email'],
  2643. 'label' => ['for' => 'custom'],
  2644. 'Email',
  2645. '/label',
  2646. ['input' => [
  2647. 'type' => 'email', 'name' => 'Contact[email]',
  2648. 'id' => 'custom', 'maxlength' => 255
  2649. ]],
  2650. '/div'
  2651. ];
  2652. $this->assertHtml($expected, $result);
  2653. $result = $this->Form->control('Contact.email', [
  2654. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2655. ]);
  2656. $expected = [
  2657. '<div',
  2658. 'label' => ['for' => 'contact-email'],
  2659. 'Email',
  2660. '/label',
  2661. ['input' => [
  2662. 'type' => 'email', 'name' => 'Contact[email]',
  2663. 'id' => 'contact-email', 'maxlength' => 255
  2664. ]],
  2665. '/div'
  2666. ];
  2667. $this->assertHtml($expected, $result);
  2668. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2669. $expected = [
  2670. 'div' => ['class' => 'input text'],
  2671. 'label' => ['for' => 'contact-email'],
  2672. 'Email',
  2673. '/label',
  2674. ['input' => [
  2675. 'type' => 'text', 'name' => 'Contact[email]',
  2676. 'id' => 'contact-email', 'maxlength' => '255'
  2677. ]],
  2678. '/div'
  2679. ];
  2680. $this->assertHtml($expected, $result);
  2681. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2682. $expected = [
  2683. 'div' => ['class' => 'input text'],
  2684. 'label' => ['for' => 'contact-5-email'],
  2685. 'Email',
  2686. '/label',
  2687. ['input' => [
  2688. 'type' => 'text', 'name' => 'Contact[5][email]',
  2689. 'id' => 'contact-5-email', 'maxlength' => '255'
  2690. ]],
  2691. '/div'
  2692. ];
  2693. $this->assertHtml($expected, $result);
  2694. $result = $this->Form->control('Contact.password');
  2695. $expected = [
  2696. 'div' => ['class' => 'input password'],
  2697. 'label' => ['for' => 'contact-password'],
  2698. 'Password',
  2699. '/label',
  2700. ['input' => [
  2701. 'type' => 'password', 'name' => 'Contact[password]',
  2702. 'id' => 'contact-password'
  2703. ]],
  2704. '/div'
  2705. ];
  2706. $this->assertHtml($expected, $result);
  2707. $result = $this->Form->control('Contact.email', [
  2708. 'type' => 'file', 'class' => 'textbox'
  2709. ]);
  2710. $expected = [
  2711. 'div' => ['class' => 'input file'],
  2712. 'label' => ['for' => 'contact-email'],
  2713. 'Email',
  2714. '/label',
  2715. ['input' => [
  2716. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2717. 'id' => 'contact-email'
  2718. ]],
  2719. '/div'
  2720. ];
  2721. $this->assertHtml($expected, $result);
  2722. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2723. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2724. $result = $this->Form->control('phone');
  2725. $expected = [
  2726. 'div' => ['class' => 'input tel'],
  2727. 'label' => ['for' => 'phone'],
  2728. 'Phone',
  2729. '/label',
  2730. ['input' => [
  2731. 'type' => 'tel', 'name' => 'phone',
  2732. 'value' => 'Hello &amp; World &gt; weird chars',
  2733. 'id' => 'phone', 'maxlength' => 255
  2734. ]],
  2735. '/div'
  2736. ];
  2737. $this->assertHtml($expected, $result);
  2738. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2739. $this->Form->create();
  2740. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2741. $expected = [
  2742. 'div' => ['class' => 'input text'],
  2743. 'label' => ['for' => 'myId'],
  2744. 'Field',
  2745. '/label',
  2746. 'input' => [
  2747. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2748. 'value' => 'My value', 'id' => 'myId'
  2749. ],
  2750. '/div'
  2751. ];
  2752. $this->assertHtml($expected, $result);
  2753. $this->Form->request->data = [];
  2754. $entity->errors('field', 'Badness!');
  2755. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2756. $result = $this->Form->control('field');
  2757. $expected = [
  2758. 'div' => ['class' => 'input text error'],
  2759. 'label' => ['for' => 'field'],
  2760. 'Field',
  2761. '/label',
  2762. 'input' => [
  2763. 'type' => 'text', 'name' => 'field',
  2764. 'id' => 'field', 'class' => 'form-error'
  2765. ],
  2766. ['div' => ['class' => 'error-message']],
  2767. 'Badness!',
  2768. '/div',
  2769. '/div'
  2770. ];
  2771. $this->assertHtml($expected, $result);
  2772. $result = $this->Form->control('field', [
  2773. 'templates' => [
  2774. 'inputContainerError' => '{{content}}{{error}}',
  2775. 'error' => '<span class="error-message">{{content}}</span>'
  2776. ]
  2777. ]);
  2778. $expected = [
  2779. 'label' => ['for' => 'field'],
  2780. 'Field',
  2781. '/label',
  2782. 'input' => [
  2783. 'type' => 'text', 'name' => 'field',
  2784. 'id' => 'field', 'class' => 'form-error'
  2785. ],
  2786. ['span' => ['class' => 'error-message']],
  2787. 'Badness!',
  2788. '/span'
  2789. ];
  2790. $this->assertHtml($expected, $result);
  2791. $entity->errors('field', ['minLength'], true);
  2792. $result = $this->Form->control('field', [
  2793. 'error' => [
  2794. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2795. 'maxLength' => 'login too large'
  2796. ]
  2797. ]);
  2798. $expected = [
  2799. 'div' => ['class' => 'input text error'],
  2800. 'label' => ['for' => 'field'],
  2801. 'Field',
  2802. '/label',
  2803. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2804. ['div' => ['class' => 'error-message']],
  2805. 'Le login doit contenir au moins 2 caractères',
  2806. '/div',
  2807. '/div'
  2808. ];
  2809. $this->assertHtml($expected, $result);
  2810. $entity->errors('field', ['maxLength'], true);
  2811. $result = $this->Form->control('field', [
  2812. 'error' => [
  2813. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2814. 'maxLength' => 'login too large',
  2815. ]
  2816. ]);
  2817. $expected = [
  2818. 'div' => ['class' => 'input text error'],
  2819. 'label' => ['for' => 'field'],
  2820. 'Field',
  2821. '/label',
  2822. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2823. ['div' => ['class' => 'error-message']],
  2824. 'login too large',
  2825. '/div',
  2826. '/div'
  2827. ];
  2828. $this->assertHtml($expected, $result);
  2829. }
  2830. /**
  2831. * testControlWithTemplateFile method
  2832. *
  2833. * Test that control() accepts a template file.
  2834. *
  2835. * @return void
  2836. */
  2837. public function testControlWithTemplateFile()
  2838. {
  2839. $result = $this->Form->control('field', [
  2840. 'templates' => 'htmlhelper_tags'
  2841. ]);
  2842. $expected = [
  2843. 'label' => ['for' => 'field'],
  2844. 'Field',
  2845. '/label',
  2846. 'input' => [
  2847. 'type' => 'text', 'name' => 'field',
  2848. 'id' => 'field'
  2849. ],
  2850. ];
  2851. $this->assertHtml($expected, $result);
  2852. }
  2853. /**
  2854. * testNestedControlsEndWithBrackets method
  2855. *
  2856. * Test that nested inputs end with brackets.
  2857. *
  2858. * @return void
  2859. */
  2860. public function testNestedControlsEndWithBrackets()
  2861. {
  2862. $result = $this->Form->text('nested.text[]');
  2863. $expected = [
  2864. 'input' => [
  2865. 'type' => 'text', 'name' => 'nested[text][]'
  2866. ],
  2867. ];
  2868. $this->assertHtml($expected, $result);
  2869. $result = $this->Form->file('nested.file[]');
  2870. $expected = [
  2871. 'input' => [
  2872. 'type' => 'file', 'name' => 'nested[file][]'
  2873. ],
  2874. ];
  2875. $this->assertHtml($expected, $result);
  2876. }
  2877. /**
  2878. * testCreateIdPrefix method
  2879. *
  2880. * Test id prefix.
  2881. *
  2882. * @return void
  2883. */
  2884. public function testCreateIdPrefix()
  2885. {
  2886. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2887. $result = $this->Form->control('field');
  2888. $expected = [
  2889. 'div' => ['class' => 'input text'],
  2890. 'label' => ['for' => 'prefix-field'],
  2891. 'Field',
  2892. '/label',
  2893. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2894. '/div'
  2895. ];
  2896. $this->assertHtml($expected, $result);
  2897. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2898. $expected = [
  2899. 'div' => ['class' => 'input text'],
  2900. 'label' => ['for' => 'custom-id'],
  2901. 'Field',
  2902. '/label',
  2903. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2904. '/div'
  2905. ];
  2906. $this->assertHtml($expected, $result);
  2907. $result = $this->Form->radio('Model.field', ['option A']);
  2908. $expected = [
  2909. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2910. 'label' => ['for' => 'prefix-model-field-0'],
  2911. ['input' => [
  2912. 'type' => 'radio',
  2913. 'name' => 'Model[field]',
  2914. 'value' => '0',
  2915. 'id' => 'prefix-model-field-0'
  2916. ]],
  2917. 'option A',
  2918. '/label'
  2919. ];
  2920. $this->assertHtml($expected, $result);
  2921. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2922. $expected = [
  2923. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2924. 'label' => ['for' => 'prefix-model-field-0'],
  2925. ['input' => [
  2926. 'type' => 'radio',
  2927. 'name' => 'Model[field]',
  2928. 'value' => '0',
  2929. 'id' => 'prefix-model-field-0'
  2930. ]],
  2931. 'option A',
  2932. '/label'
  2933. ];
  2934. $this->assertHtml($expected, $result);
  2935. $result = $this->Form->select(
  2936. 'Model.multi_field',
  2937. ['first'],
  2938. ['multiple' => 'checkbox']
  2939. );
  2940. $expected = [
  2941. 'input' => [
  2942. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2943. ],
  2944. ['div' => ['class' => 'checkbox']],
  2945. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2946. ['input' => [
  2947. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2948. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2949. ]],
  2950. 'first',
  2951. '/label',
  2952. '/div',
  2953. ];
  2954. $this->assertHtml($expected, $result);
  2955. $this->Form->end();
  2956. $result = $this->Form->control('field');
  2957. $expected = [
  2958. 'div' => ['class' => 'input text'],
  2959. 'label' => ['for' => 'field'],
  2960. 'Field',
  2961. '/label',
  2962. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2963. '/div'
  2964. ];
  2965. $this->assertHtml($expected, $result);
  2966. }
  2967. /**
  2968. * testControlZero method
  2969. *
  2970. * Test that inputs with 0 can be created.
  2971. *
  2972. * @return void
  2973. */
  2974. public function testControlZero()
  2975. {
  2976. TableRegistry::get('Contacts', [
  2977. 'className' => __NAMESPACE__ . '\ContactsTable'
  2978. ]);
  2979. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2980. $result = $this->Form->control('0');
  2981. $expected = [
  2982. 'div' => ['class' => 'input text'],
  2983. 'label' => ['for' => '0'], '/label',
  2984. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2985. '/div'
  2986. ];
  2987. $this->assertHtml($expected, $result);
  2988. }
  2989. /**
  2990. * testControlCheckbox method
  2991. *
  2992. * Test control() with checkbox creation.
  2993. *
  2994. * @return void
  2995. */
  2996. public function testControlCheckbox()
  2997. {
  2998. $articles = TableRegistry::get('Articles');
  2999. $articles->schema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3000. $article = $articles->newEntity();
  3001. $this->Form->create($article);
  3002. $result = $this->Form->control('Articles.active');
  3003. $expected = [
  3004. 'div' => ['class' => 'input checkbox'],
  3005. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3006. 'label' => ['for' => 'articles-active'],
  3007. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3008. 'Active',
  3009. '/label',
  3010. '/div'
  3011. ];
  3012. $this->assertHtml($expected, $result);
  3013. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3014. $expected = [
  3015. 'div' => ['class' => 'input checkbox'],
  3016. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3017. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3018. '/div'
  3019. ];
  3020. $this->assertHtml($expected, $result);
  3021. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3022. $expected = [
  3023. 'div' => ['class' => 'input checkbox'],
  3024. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3025. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3026. '/div'
  3027. ];
  3028. $this->assertHtml($expected, $result);
  3029. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3030. $expected = [
  3031. 'div' => ['class' => 'input checkbox'],
  3032. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3033. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3034. '/div'
  3035. ];
  3036. $this->assertHtml($expected, $result);
  3037. $result = $this->Form->control('Articles.disabled', [
  3038. 'label' => 'Disabled',
  3039. 'type' => 'checkbox',
  3040. 'data-foo' => 'disabled'
  3041. ]);
  3042. $expected = [
  3043. 'div' => ['class' => 'input checkbox'],
  3044. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3045. 'label' => ['for' => 'articles-disabled'],
  3046. ['input' => [
  3047. 'type' => 'checkbox',
  3048. 'name' => 'Articles[disabled]',
  3049. 'value' => '1',
  3050. 'id' => 'articles-disabled',
  3051. 'data-foo' => 'disabled'
  3052. ]],
  3053. 'Disabled',
  3054. '/label',
  3055. '/div'
  3056. ];
  3057. $this->assertHtml($expected, $result);
  3058. $result = $this->Form->control('Articles.confirm', [
  3059. 'label' => 'Confirm <b>me</b>!',
  3060. 'type' => 'checkbox',
  3061. 'escape' => false
  3062. ]);
  3063. $expected = [
  3064. 'div' => ['class' => 'input checkbox'],
  3065. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3066. 'label' => ['for' => 'articles-confirm'],
  3067. ['input' => [
  3068. 'type' => 'checkbox',
  3069. 'name' => 'Articles[confirm]',
  3070. 'value' => '1',
  3071. 'id' => 'articles-confirm',
  3072. ]],
  3073. 'Confirm <b>me</b>!',
  3074. '/label',
  3075. '/div'
  3076. ];
  3077. $this->assertHtml($expected, $result);
  3078. }
  3079. /**
  3080. * testControlHidden method
  3081. *
  3082. * Test that control() does not create wrapping div and label tag for hidden fields.
  3083. *
  3084. * @return void
  3085. */
  3086. public function testControlHidden()
  3087. {
  3088. TableRegistry::get('ValidateUsers', [
  3089. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3090. ]);
  3091. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3092. $result = $this->Form->control('ValidateUser.id');
  3093. $expected = [
  3094. 'input' => ['name', 'type' => 'hidden', 'id']
  3095. ];
  3096. $this->assertHtml($expected, $result);
  3097. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3098. $expected = [
  3099. 'input' => ['name', 'type' => 'hidden', 'id']
  3100. ];
  3101. $this->assertHtml($expected, $result);
  3102. }
  3103. /**
  3104. * testControlDatetime method
  3105. *
  3106. * Test form->control() with datetime.
  3107. *
  3108. * @return void
  3109. */
  3110. public function testControlDatetime()
  3111. {
  3112. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3113. ->setMethods(['datetime'])
  3114. ->setConstructorArgs([new View()])
  3115. ->getMock();
  3116. $this->Form->expects($this->once())->method('datetime')
  3117. ->with('prueba', [
  3118. 'type' => 'datetime',
  3119. 'timeFormat' => 24,
  3120. 'minYear' => 2008,
  3121. 'maxYear' => 2011,
  3122. 'interval' => 15,
  3123. 'options' => null,
  3124. 'empty' => false,
  3125. 'id' => 'prueba',
  3126. 'required' => false,
  3127. 'templateVars' => []
  3128. ])
  3129. ->will($this->returnValue('This is it!'));
  3130. $result = $this->Form->control('prueba', [
  3131. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3132. 'maxYear' => 2011, 'interval' => 15
  3133. ]);
  3134. $expected = [
  3135. 'div' => ['class' => 'input datetime'],
  3136. '<label',
  3137. 'Prueba',
  3138. '/label',
  3139. 'This is it!',
  3140. '/div'
  3141. ];
  3142. $this->assertHtml($expected, $result);
  3143. }
  3144. /**
  3145. * testControlDatetimeIdPrefix method
  3146. *
  3147. * Test form->control() with datetime with id prefix.
  3148. *
  3149. * @return void
  3150. */
  3151. public function testControlDatetimeIdPrefix()
  3152. {
  3153. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3154. ->setMethods(['datetime'])
  3155. ->setConstructorArgs([new View()])
  3156. ->getMock();
  3157. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3158. $this->Form->expects($this->once())->method('datetime')
  3159. ->with('prueba', [
  3160. 'type' => 'datetime',
  3161. 'timeFormat' => 24,
  3162. 'minYear' => 2008,
  3163. 'maxYear' => 2011,
  3164. 'interval' => 15,
  3165. 'options' => null,
  3166. 'empty' => false,
  3167. 'id' => 'prefix-prueba',
  3168. 'required' => false,
  3169. 'templateVars' => []
  3170. ])
  3171. ->will($this->returnValue('This is it!'));
  3172. $result = $this->Form->control('prueba', [
  3173. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3174. 'maxYear' => 2011, 'interval' => 15
  3175. ]);
  3176. $expected = [
  3177. 'div' => ['class' => 'input datetime'],
  3178. '<label',
  3179. 'Prueba',
  3180. '/label',
  3181. 'This is it!',
  3182. '/div'
  3183. ];
  3184. $this->assertHtml($expected, $result);
  3185. }
  3186. /**
  3187. * testControlCheckboxWithDisabledElements method
  3188. *
  3189. * Test generating checkboxes with disabled elements.
  3190. *
  3191. * @return void
  3192. */
  3193. public function testControlCheckboxWithDisabledElements()
  3194. {
  3195. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3196. $result = $this->Form->control('Contact.multiple', [
  3197. 'multiple' => 'checkbox',
  3198. 'disabled' => 'disabled',
  3199. 'options' => $options
  3200. ]);
  3201. $expected = [
  3202. ['div' => ['class' => 'input select']],
  3203. ['label' => ['for' => 'contact-multiple']],
  3204. 'Multiple',
  3205. '/label',
  3206. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3207. ['div' => ['class' => 'checkbox']],
  3208. ['label' => ['for' => 'contact-multiple-1']],
  3209. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3210. 'One',
  3211. '/label',
  3212. '/div',
  3213. ['div' => ['class' => 'checkbox']],
  3214. ['label' => ['for' => 'contact-multiple-2']],
  3215. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3216. 'Two',
  3217. '/label',
  3218. '/div',
  3219. ['div' => ['class' => 'checkbox']],
  3220. ['label' => ['for' => 'contact-multiple-3']],
  3221. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3222. 'Three',
  3223. '/label',
  3224. '/div',
  3225. '/div'
  3226. ];
  3227. $this->assertHtml($expected, $result);
  3228. // make sure 50 does only disable 50, and not 50f5c0cf
  3229. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3230. $disabled = [50];
  3231. $expected = [
  3232. ['div' => ['class' => 'input select']],
  3233. ['label' => ['for' => 'contact-multiple']],
  3234. 'Multiple',
  3235. '/label',
  3236. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3237. ['div' => ['class' => 'checkbox']],
  3238. ['label' => ['for' => 'contact-multiple-50']],
  3239. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3240. 'Fifty',
  3241. '/label',
  3242. '/div',
  3243. ['div' => ['class' => 'checkbox']],
  3244. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3245. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3246. 'Stringy',
  3247. '/label',
  3248. '/div',
  3249. '/div'
  3250. ];
  3251. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3252. $this->assertHtml($expected, $result);
  3253. }
  3254. /**
  3255. * testControlWithLeadingInteger method
  3256. *
  3257. * Test input name with leading integer, ensure attributes are generated correctly.
  3258. *
  3259. * @return void
  3260. */
  3261. public function testControlWithLeadingInteger()
  3262. {
  3263. $result = $this->Form->text('0.Node.title');
  3264. $expected = [
  3265. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3266. ];
  3267. $this->assertHtml($expected, $result);
  3268. }
  3269. /**
  3270. * testControlSelectType method
  3271. *
  3272. * Test form->control() with select type inputs.
  3273. *
  3274. * @return void
  3275. */
  3276. public function testControlSelectType()
  3277. {
  3278. $result = $this->Form->control(
  3279. 'email',
  3280. [
  3281. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3282. );
  3283. $expected = [
  3284. 'div' => ['class' => 'input select'],
  3285. 'label' => ['for' => 'email'],
  3286. 'Email',
  3287. '/label',
  3288. ['select' => ['name' => 'email', 'id' => 'email']],
  3289. ['option' => ['value' => '']],
  3290. '/option',
  3291. ['option' => ['value' => 'è']],
  3292. 'Firést',
  3293. '/option',
  3294. ['option' => ['value' => 'é']],
  3295. 'Secoènd',
  3296. '/option',
  3297. '/select',
  3298. '/div'
  3299. ];
  3300. $this->assertHtml($expected, $result);
  3301. $result = $this->Form->control(
  3302. 'email',
  3303. [
  3304. 'options' => ['First', 'Second'], 'empty' => true]
  3305. );
  3306. $expected = [
  3307. 'div' => ['class' => 'input select'],
  3308. 'label' => ['for' => 'email'],
  3309. 'Email',
  3310. '/label',
  3311. ['select' => ['name' => 'email', 'id' => 'email']],
  3312. ['option' => ['value' => '']],
  3313. '/option',
  3314. ['option' => ['value' => '0']],
  3315. 'First',
  3316. '/option',
  3317. ['option' => ['value' => '1']],
  3318. 'Second',
  3319. '/option',
  3320. '/select',
  3321. '/div'
  3322. ];
  3323. $this->assertHtml($expected, $result);
  3324. $result = $this->Form->control('email', [
  3325. 'type' => 'select',
  3326. 'options' => new \ArrayObject(['First', 'Second']),
  3327. 'empty' => true
  3328. ]);
  3329. $this->assertHtml($expected, $result);
  3330. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3331. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3332. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3333. $expected = [
  3334. 'div' => ['class' => 'input select'],
  3335. 'label' => ['for' => 'model-user-id'],
  3336. 'User',
  3337. '/label',
  3338. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3339. ['option' => ['value' => '']],
  3340. '/option',
  3341. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3342. 'good',
  3343. '/option',
  3344. ['option' => ['value' => 'other']],
  3345. 'bad',
  3346. '/option',
  3347. '/select',
  3348. '/div'
  3349. ];
  3350. $this->assertHtml($expected, $result);
  3351. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3352. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3353. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3354. $expected = [
  3355. 'div' => ['class' => 'input select'],
  3356. 'label' => ['for' => 'thing-user-id'],
  3357. 'User',
  3358. '/label',
  3359. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3360. ['option' => ['value' => '']],
  3361. 'Some Empty',
  3362. '/option',
  3363. ['option' => ['value' => 'value']],
  3364. 'good',
  3365. '/option',
  3366. ['option' => ['value' => 'other']],
  3367. 'bad',
  3368. '/option',
  3369. '/select',
  3370. '/div'
  3371. ];
  3372. $this->assertHtml($expected, $result);
  3373. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3374. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3375. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3376. $expected = [
  3377. 'div' => ['class' => 'input select'],
  3378. 'label' => ['for' => 'thing-user-id'],
  3379. 'User',
  3380. '/label',
  3381. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3382. ['option' => ['value' => '']],
  3383. 'Some Empty',
  3384. '/option',
  3385. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3386. 'good',
  3387. '/option',
  3388. ['option' => ['value' => 'other']],
  3389. 'bad',
  3390. '/option',
  3391. '/select',
  3392. '/div'
  3393. ];
  3394. $this->assertHtml($expected, $result);
  3395. $this->Form->data = [];
  3396. $result = $this->Form->control('Publisher.id', [
  3397. 'label' => 'Publisher',
  3398. 'type' => 'select',
  3399. 'multiple' => 'checkbox',
  3400. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3401. ]);
  3402. $expected = [
  3403. ['div' => ['class' => 'input select']],
  3404. ['label' => ['for' => 'publisher-id']],
  3405. 'Publisher',
  3406. '/label',
  3407. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3408. ['div' => ['class' => 'checkbox']],
  3409. ['label' => ['for' => 'publisher-id-value-1']],
  3410. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3411. 'Label 1',
  3412. '/label',
  3413. '/div',
  3414. ['div' => ['class' => 'checkbox']],
  3415. ['label' => ['for' => 'publisher-id-value-2']],
  3416. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3417. 'Label 2',
  3418. '/label',
  3419. '/div',
  3420. '/div'
  3421. ];
  3422. $this->assertHtml($expected, $result);
  3423. }
  3424. /**
  3425. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3426. *
  3427. * Test that control() and a non standard primary key makes a hidden input by default.
  3428. *
  3429. * @return void
  3430. */
  3431. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3432. {
  3433. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3434. $this->Form->create($this->article);
  3435. $result = $this->Form->control('title');
  3436. $expected = [
  3437. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3438. ];
  3439. $this->assertHtml($expected, $result);
  3440. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3441. $this->Form->create($this->article);
  3442. $result = $this->Form->control('title');
  3443. $expected = [
  3444. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3445. ];
  3446. $this->assertHtml($expected, $result);
  3447. $result = $this->Form->control('body');
  3448. $expected = [
  3449. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3450. ];
  3451. $this->assertHtml($expected, $result);
  3452. }
  3453. /**
  3454. * testControlOverridingMagicSelectType method
  3455. *
  3456. * Test that overriding the magic select type widget is possible.
  3457. *
  3458. * @return void
  3459. */
  3460. public function testControlOverridingMagicSelectType()
  3461. {
  3462. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3463. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3464. $expected = [
  3465. 'div' => ['class' => 'input text'],
  3466. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3467. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3468. '/div'
  3469. ];
  3470. $this->assertHtml($expected, $result);
  3471. //Check that magic types still work for plural/singular vars
  3472. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3473. $result = $this->Form->control('Model.type');
  3474. $expected = [
  3475. 'div' => ['class' => 'input select'],
  3476. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3477. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3478. ['option' => ['value' => 'value']], 'good', '/option',
  3479. ['option' => ['value' => 'other']], 'bad', '/option',
  3480. '/select',
  3481. '/div'
  3482. ];
  3483. $this->assertHtml($expected, $result);
  3484. }
  3485. /**
  3486. * testControlMagicTypeDoesNotOverride method
  3487. *
  3488. * Test that inferred types do not override developer input.
  3489. *
  3490. * @return void
  3491. */
  3492. public function testControlMagicTypeDoesNotOverride()
  3493. {
  3494. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3495. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3496. $expected = [
  3497. 'div' => ['class' => 'input checkbox'],
  3498. ['input' => [
  3499. 'type' => 'hidden',
  3500. 'name' => 'Model[user]',
  3501. 'value' => 0,
  3502. ]],
  3503. 'label' => ['for' => 'model-user'],
  3504. ['input' => [
  3505. 'name' => 'Model[user]',
  3506. 'type' => 'checkbox',
  3507. 'id' => 'model-user',
  3508. 'value' => 1
  3509. ]],
  3510. 'User',
  3511. '/label',
  3512. '/div'
  3513. ];
  3514. $this->assertHtml($expected, $result);
  3515. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3516. $options = [
  3517. 1 => 'blue',
  3518. 2 => 'red'
  3519. ];
  3520. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3521. $expected = [
  3522. 'div' => ['class' => 'input select'],
  3523. 'label' => ['for' => 'tags-ids'],
  3524. 'Tags',
  3525. '/label',
  3526. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3527. ['div' => ['class' => 'checkbox']],
  3528. ['label' => ['for' => 'tags-ids-1']],
  3529. ['input' => [
  3530. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3531. 'value' => '1', 'name' => 'tags[_ids][]'
  3532. ]],
  3533. 'blue',
  3534. '/label',
  3535. '/div',
  3536. ['div' => ['class' => 'checkbox']],
  3537. ['label' => ['for' => 'tags-ids-2']],
  3538. ['input' => [
  3539. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3540. 'value' => '2', 'name' => 'tags[_ids][]'
  3541. ]],
  3542. 'red',
  3543. '/label',
  3544. '/div',
  3545. '/div'
  3546. ];
  3547. $this->assertHtml($expected, $result);
  3548. }
  3549. /**
  3550. * testControlMagicSelectForTypeNumber method
  3551. *
  3552. * Test that magic control() selects are created for type=number.
  3553. *
  3554. * @return void
  3555. */
  3556. public function testControlMagicSelectForTypeNumber()
  3557. {
  3558. TableRegistry::get('ValidateUsers', [
  3559. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3560. ]);
  3561. $entity = new Entity(['balance' => 1]);
  3562. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3563. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3564. $result = $this->Form->control('balance');
  3565. $expected = [
  3566. 'div' => ['class' => 'input select'],
  3567. 'label' => ['for' => 'balance'],
  3568. 'Balance',
  3569. '/label',
  3570. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3571. ['option' => ['value' => '0']],
  3572. 'nothing',
  3573. '/option',
  3574. ['option' => ['value' => '1', 'selected' => 'selected']],
  3575. 'some',
  3576. '/option',
  3577. ['option' => ['value' => '100']],
  3578. 'a lot',
  3579. '/option',
  3580. '/select',
  3581. '/div'
  3582. ];
  3583. $this->assertHtml($expected, $result);
  3584. }
  3585. /**
  3586. * testInvalidControlTypeOption method
  3587. *
  3588. * Test invalid 'input' type option to control() function.
  3589. *
  3590. * @expectedException \RuntimeException
  3591. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3592. * @return void
  3593. */
  3594. public function testInvalidControlTypeOption()
  3595. {
  3596. $this->Form->control('text', ['type' => 'input']);
  3597. }
  3598. /**
  3599. * testControlMagicSelectChangeToRadio method
  3600. *
  3601. * Test that magic control() selects can easily be converted into radio types without error.
  3602. *
  3603. * @return void
  3604. */
  3605. public function testControlMagicSelectChangeToRadio()
  3606. {
  3607. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3608. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3609. $this->assertContains('input type="radio"', $result);
  3610. }
  3611. /**
  3612. * testFormControlSubmit method
  3613. *
  3614. * Test correct results for form::control() and type submit.
  3615. *
  3616. * @return void
  3617. */
  3618. public function testFormControlSubmit()
  3619. {
  3620. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3621. $expected = [
  3622. 'div' => ['class' => 'submit'],
  3623. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3624. '/div'
  3625. ];
  3626. $this->assertHtml($expected, $result);
  3627. }
  3628. /**
  3629. * testFormControls method
  3630. *
  3631. * Test correct results from Form::controls().
  3632. *
  3633. * @return void
  3634. */
  3635. public function testFormControlsLegendFieldset()
  3636. {
  3637. $this->Form->create($this->article);
  3638. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3639. $expected = [
  3640. '<fieldset',
  3641. '<legend',
  3642. 'The Legend',
  3643. '/legend',
  3644. '*/fieldset',
  3645. ];
  3646. $this->assertHtml($expected, $result);
  3647. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3648. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3649. $this->assertContains('<fieldset>', $result);
  3650. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3651. $this->assertNotContains('<legend>', $result);
  3652. $this->assertNotContains('<fieldset>', $result);
  3653. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3654. $this->assertNotContains('<legend>', $result);
  3655. $this->assertNotContains('<fieldset>', $result);
  3656. $this->Form->create($this->article);
  3657. $this->Form->request->params['prefix'] = 'admin';
  3658. $this->Form->request->params['action'] = 'admin_edit';
  3659. $this->Form->request->params['controller'] = 'articles';
  3660. $result = $this->Form->allControls();
  3661. $expected = [
  3662. '<fieldset',
  3663. '<legend',
  3664. 'New Article',
  3665. '/legend',
  3666. '*/fieldset',
  3667. ];
  3668. $this->assertHtml($expected, $result);
  3669. $this->Form->create($this->article);
  3670. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3671. $expected = [
  3672. '<fieldset',
  3673. '<legend',
  3674. 'The Legend',
  3675. '/legend',
  3676. '*/fieldset',
  3677. ];
  3678. $this->assertHtml($expected, $result);
  3679. $this->Form->create($this->article);
  3680. $result = $this->Form->allControls([], [
  3681. 'fieldset' => [
  3682. 'class' => 'some-class some-other-class',
  3683. 'disabled' => true,
  3684. 'data-param' => 'a-param'
  3685. ],
  3686. 'legend' => 'The Legend'
  3687. ]);
  3688. $expected = [
  3689. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3690. '<legend',
  3691. 'The Legend',
  3692. '/legend',
  3693. '*/fieldset',
  3694. ];
  3695. $this->assertHtml($expected, $result);
  3696. }
  3697. /**
  3698. * testFormControls method
  3699. *
  3700. * Test the controls() method.
  3701. *
  3702. * @return void
  3703. */
  3704. public function testFormControls()
  3705. {
  3706. $this->Form->create($this->article);
  3707. $result = $this->Form->allControls();
  3708. $expected = [
  3709. '<fieldset',
  3710. '<legend', 'New Article', '/legend',
  3711. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3712. ['div' => ['class' => 'input select required']],
  3713. '*/div',
  3714. ['div' => ['class' => 'input text required']],
  3715. '*/div',
  3716. ['div' => ['class' => 'input text']],
  3717. '*/div',
  3718. ['div' => ['class' => 'input text']],
  3719. '*/div',
  3720. '/fieldset',
  3721. ];
  3722. $this->assertHtml($expected, $result);
  3723. $result = $this->Form->allControls([
  3724. 'published' => ['type' => 'boolean']
  3725. ]);
  3726. $expected = [
  3727. '<fieldset',
  3728. '<legend', 'New Article', '/legend',
  3729. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3730. ['div' => ['class' => 'input select required']],
  3731. '*/div',
  3732. ['div' => ['class' => 'input text required']],
  3733. '*/div',
  3734. ['div' => ['class' => 'input text']],
  3735. '*/div',
  3736. ['div' => ['class' => 'input boolean']],
  3737. '*/div',
  3738. '/fieldset',
  3739. ];
  3740. $this->assertHtml($expected, $result);
  3741. $this->Form->create($this->article);
  3742. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3743. $expected = [
  3744. 'fieldset' => [],
  3745. 'legend' => [],
  3746. 'Hello',
  3747. '/legend',
  3748. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3749. ['div' => ['class' => 'input select required']],
  3750. '*/div',
  3751. ['div' => ['class' => 'input text required']],
  3752. '*/div',
  3753. ['div' => ['class' => 'input text']],
  3754. '*/div',
  3755. ['div' => ['class' => 'input text']],
  3756. '*/div',
  3757. '/fieldset'
  3758. ];
  3759. $this->assertHtml($expected, $result);
  3760. $this->Form->create(false);
  3761. $expected = [
  3762. 'fieldset' => [],
  3763. ['div' => ['class' => 'input text']],
  3764. 'label' => ['for' => 'foo'],
  3765. 'Foo',
  3766. '/label',
  3767. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3768. '*/div',
  3769. '/fieldset'
  3770. ];
  3771. $result = $this->Form->allControls(
  3772. ['foo' => ['type' => 'text']],
  3773. ['legend' => false]
  3774. );
  3775. $this->assertHtml($expected, $result);
  3776. }
  3777. /**
  3778. * testFormControlsBlacklist method
  3779. *
  3780. * @return void
  3781. */
  3782. public function testFormControlsBlacklist()
  3783. {
  3784. $this->Form->create($this->article);
  3785. $result = $this->Form->allControls([
  3786. 'id' => false
  3787. ]);
  3788. $expected = [
  3789. '<fieldset',
  3790. '<legend', 'New Article', '/legend',
  3791. ['div' => ['class' => 'input select required']],
  3792. '*/div',
  3793. ['div' => ['class' => 'input text required']],
  3794. '*/div',
  3795. ['div' => ['class' => 'input text']],
  3796. '*/div',
  3797. ['div' => ['class' => 'input text']],
  3798. '*/div',
  3799. '/fieldset',
  3800. ];
  3801. $this->assertHtml($expected, $result);
  3802. $this->Form->create($this->article);
  3803. $result = $this->Form->allControls([
  3804. 'id' => []
  3805. ]);
  3806. $expected = [
  3807. '<fieldset',
  3808. '<legend', 'New Article', '/legend',
  3809. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3810. ['div' => ['class' => 'input select required']],
  3811. '*/div',
  3812. ['div' => ['class' => 'input text required']],
  3813. '*/div',
  3814. ['div' => ['class' => 'input text']],
  3815. '*/div',
  3816. ['div' => ['class' => 'input text']],
  3817. '*/div',
  3818. '/fieldset',
  3819. ];
  3820. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3821. }
  3822. /**
  3823. * testSelectAsCheckbox method
  3824. *
  3825. * Test multi-select widget with checkbox formatting.
  3826. *
  3827. * @return void
  3828. */
  3829. public function testSelectAsCheckbox()
  3830. {
  3831. $result = $this->Form->select(
  3832. 'Model.multi_field',
  3833. ['first', 'second', 'third'],
  3834. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3835. );
  3836. $expected = [
  3837. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3838. ['div' => ['class' => 'checkbox']],
  3839. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3840. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3841. 'first',
  3842. '/label',
  3843. '/div',
  3844. ['div' => ['class' => 'checkbox']],
  3845. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3846. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3847. 'second',
  3848. '/label',
  3849. '/div',
  3850. ['div' => ['class' => 'checkbox']],
  3851. ['label' => ['for' => 'model-multi-field-2']],
  3852. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3853. 'third',
  3854. '/label',
  3855. '/div',
  3856. ];
  3857. $this->assertHtml($expected, $result);
  3858. $result = $this->Form->select(
  3859. 'Model.multi_field',
  3860. ['1/2' => 'half'],
  3861. ['multiple' => 'checkbox']
  3862. );
  3863. $expected = [
  3864. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3865. ['div' => ['class' => 'checkbox']],
  3866. ['label' => ['for' => 'model-multi-field-1-2']],
  3867. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3868. 'half',
  3869. '/label',
  3870. '/div',
  3871. ];
  3872. $this->assertHtml($expected, $result);
  3873. }
  3874. /**
  3875. * testLabel method
  3876. *
  3877. * Test label generation.
  3878. *
  3879. * @return void
  3880. */
  3881. public function testLabel()
  3882. {
  3883. $result = $this->Form->label('Person.name');
  3884. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3885. $this->assertHtml($expected, $result);
  3886. $result = $this->Form->label('Person.name');
  3887. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3888. $this->assertHtml($expected, $result);
  3889. $result = $this->Form->label('Person.first_name');
  3890. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3891. $this->assertHtml($expected, $result);
  3892. $result = $this->Form->label('Person.first_name', 'Your first name');
  3893. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3894. $this->assertHtml($expected, $result);
  3895. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3896. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3897. $this->assertHtml($expected, $result);
  3898. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3899. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3900. $this->assertHtml($expected, $result);
  3901. $result = $this->Form->label('Person.first_name', '');
  3902. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3903. $this->assertHtml($expected, $result);
  3904. $result = $this->Form->label('Person.2.name', '');
  3905. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3906. $this->assertHtml($expected, $result);
  3907. }
  3908. /**
  3909. * testLabelContainControl method
  3910. *
  3911. * Test that label() can accept an input with the correct template vars.
  3912. *
  3913. * @return void
  3914. */
  3915. public function testLabelContainControl()
  3916. {
  3917. $this->Form->templates([
  3918. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3919. ]);
  3920. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3921. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3922. ]);
  3923. $expected = [
  3924. 'label' => ['for' => 'person-accept-terms'],
  3925. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3926. 'Accept',
  3927. '/label',
  3928. ];
  3929. $this->assertHtml($expected, $result);
  3930. }
  3931. /**
  3932. * testTextbox method
  3933. *
  3934. * Test textbox element generation.
  3935. *
  3936. * @return void
  3937. */
  3938. public function testTextbox()
  3939. {
  3940. $result = $this->Form->text('Model.field');
  3941. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3942. $this->assertHtml($expected, $result);
  3943. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3944. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3945. $this->assertHtml($expected, $result);
  3946. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3947. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3948. $this->assertHtml($expected, $result);
  3949. }
  3950. /**
  3951. * testTextBoxDataAndError method
  3952. *
  3953. * Test that text() hooks up with request data and error fields.
  3954. *
  3955. * @return void
  3956. */
  3957. public function testTextBoxDataAndError()
  3958. {
  3959. $this->article['errors'] = [
  3960. 'Contact' => ['text' => 'wrong']
  3961. ];
  3962. $this->Form->create($this->article);
  3963. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3964. $result = $this->Form->text('Model.text');
  3965. $expected = [
  3966. 'input' => [
  3967. 'type' => 'text',
  3968. 'name' => 'Model[text]',
  3969. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3970. ]
  3971. ];
  3972. $this->assertHtml($expected, $result);
  3973. $this->Form->request->data['Contact']['text'] = 'test';
  3974. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3975. $expected = [
  3976. 'input' => [
  3977. 'type' => 'text',
  3978. 'name' => 'Contact[text]',
  3979. 'value' => 'test',
  3980. 'id' => 'theID',
  3981. 'class' => 'form-error'
  3982. ]
  3983. ];
  3984. $this->assertHtml($expected, $result);
  3985. }
  3986. /**
  3987. * testDefaultValue method
  3988. *
  3989. * Test default value setting.
  3990. *
  3991. * @return void
  3992. */
  3993. public function testTextDefaultValue()
  3994. {
  3995. $this->Form->request->data['Model']['field'] = 'test';
  3996. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3997. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3998. $this->assertHtml($expected, $result);
  3999. unset($this->Form->request->data['Model']['field']);
  4000. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4001. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4002. $this->assertHtml($expected, $result);
  4003. $Articles = TableRegistry::get('Articles');
  4004. $title = $Articles->schema()->column('title');
  4005. $Articles->schema()->addColumn(
  4006. 'title',
  4007. ['default' => 'default title'] + $title
  4008. );
  4009. $entity = $Articles->newEntity();
  4010. $this->Form->create($entity);
  4011. // Get default value from schema
  4012. $result = $this->Form->text('title');
  4013. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4014. $this->assertHtml($expected, $result);
  4015. // Don't get value from schema
  4016. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4017. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4018. $this->assertHtml($expected, $result);
  4019. // Custom default value overrides default value from schema
  4020. $result = $this->Form->text('title', ['default' => 'override default']);
  4021. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4022. $this->assertHtml($expected, $result);
  4023. // Default value from schema is used only for new entities.
  4024. $entity->isNew(false);
  4025. $result = $this->Form->text('title');
  4026. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4027. $this->assertHtml($expected, $result);
  4028. }
  4029. /**
  4030. * testError method
  4031. *
  4032. * Test field error generation.
  4033. *
  4034. * @return void
  4035. */
  4036. public function testError()
  4037. {
  4038. $this->article['errors'] = [
  4039. 'Article' => ['field' => 'email']
  4040. ];
  4041. $this->Form->create($this->article);
  4042. $result = $this->Form->error('Article.field');
  4043. $expected = [
  4044. ['div' => ['class' => 'error-message']],
  4045. 'email',
  4046. '/div',
  4047. ];
  4048. $this->assertHtml($expected, $result);
  4049. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4050. $expected = [
  4051. ['div' => ['class' => 'error-message']],
  4052. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4053. '/div',
  4054. ];
  4055. $this->assertHtml($expected, $result);
  4056. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4057. $expected = [
  4058. ['div' => ['class' => 'error-message']],
  4059. '<strong', 'Badness!', '/strong',
  4060. '/div',
  4061. ];
  4062. $this->assertHtml($expected, $result);
  4063. }
  4064. /**
  4065. * testErrorRuleName method
  4066. *
  4067. * Test error translation can use rule names for translating.
  4068. *
  4069. * @return void
  4070. */
  4071. public function testErrorRuleName()
  4072. {
  4073. $this->article['errors'] = [
  4074. 'Article' => [
  4075. 'field' => ['email' => 'Your email was not good']
  4076. ]
  4077. ];
  4078. $this->Form->create($this->article);
  4079. $result = $this->Form->error('Article.field');
  4080. $expected = [
  4081. ['div' => ['class' => 'error-message']],
  4082. 'Your email was not good',
  4083. '/div',
  4084. ];
  4085. $this->assertHtml($expected, $result);
  4086. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4087. $expected = [
  4088. ['div' => ['class' => 'error-message']],
  4089. 'Email in use',
  4090. '/div',
  4091. ];
  4092. $this->assertHtml($expected, $result);
  4093. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4094. $expected = [
  4095. ['div' => ['class' => 'error-message']],
  4096. 'Email in use',
  4097. '/div',
  4098. ];
  4099. $this->assertHtml($expected, $result);
  4100. $result = $this->Form->error('Article.field', [
  4101. 'email' => 'Key is preferred',
  4102. 'Your email was not good' => 'Email in use'
  4103. ]);
  4104. $expected = [
  4105. ['div' => ['class' => 'error-message']],
  4106. 'Key is preferred',
  4107. '/div',
  4108. ];
  4109. $this->assertHtml($expected, $result);
  4110. }
  4111. /**
  4112. * testErrorMessages method
  4113. *
  4114. * Test error with nested lists.
  4115. *
  4116. * @return void
  4117. */
  4118. public function testErrorMessages()
  4119. {
  4120. $this->article['errors'] = [
  4121. 'Article' => ['field' => 'email']
  4122. ];
  4123. $this->Form->create($this->article);
  4124. $result = $this->Form->error('Article.field', [
  4125. 'email' => 'No good!'
  4126. ]);
  4127. $expected = [
  4128. 'div' => ['class' => 'error-message'],
  4129. 'No good!',
  4130. '/div'
  4131. ];
  4132. $this->assertHtml($expected, $result);
  4133. }
  4134. /**
  4135. * testErrorMultipleMessages method
  4136. *
  4137. * Test error() with multiple messages.
  4138. *
  4139. * @return void
  4140. */
  4141. public function testErrorMultipleMessages()
  4142. {
  4143. $this->article['errors'] = [
  4144. 'field' => ['notBlank', 'email', 'Something else']
  4145. ];
  4146. $this->Form->create($this->article);
  4147. $result = $this->Form->error('field', [
  4148. 'notBlank' => 'Cannot be empty',
  4149. 'email' => 'No good!'
  4150. ]);
  4151. $expected = [
  4152. 'div' => ['class' => 'error-message'],
  4153. 'ul' => [],
  4154. '<li', 'Cannot be empty', '/li',
  4155. '<li', 'No good!', '/li',
  4156. '<li', 'Something else', '/li',
  4157. '/ul',
  4158. '/div'
  4159. ];
  4160. $this->assertHtml($expected, $result);
  4161. }
  4162. /**
  4163. * testPassword method
  4164. *
  4165. * Test password element generation.
  4166. *
  4167. * @return void
  4168. */
  4169. public function testPassword()
  4170. {
  4171. $this->article['errors'] = [
  4172. 'Contact' => [
  4173. 'passwd' => 1
  4174. ]
  4175. ];
  4176. $this->Form->create($this->article);
  4177. $result = $this->Form->password('Contact.field');
  4178. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4179. $this->assertHtml($expected, $result);
  4180. $this->Form->request->data['Contact']['passwd'] = 'test';
  4181. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4182. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4183. $this->assertHtml($expected, $result);
  4184. }
  4185. /**
  4186. * testRadio method
  4187. *
  4188. * Test radio element set generation.
  4189. *
  4190. * @return void
  4191. */
  4192. public function testRadio()
  4193. {
  4194. $result = $this->Form->radio('Model.field', ['option A']);
  4195. $expected = [
  4196. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4197. 'label' => ['for' => 'model-field-0'],
  4198. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4199. 'option A',
  4200. '/label'
  4201. ];
  4202. $this->assertHtml($expected, $result);
  4203. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4204. $this->assertHtml($expected, $result);
  4205. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4206. $expected = [
  4207. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4208. ['label' => ['for' => 'model-field-0']],
  4209. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4210. 'option A',
  4211. '/label',
  4212. ['label' => ['for' => 'model-field-1']],
  4213. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4214. 'option B',
  4215. '/label',
  4216. ];
  4217. $this->assertHtml($expected, $result);
  4218. $result = $this->Form->radio(
  4219. 'Employee.gender',
  4220. ['male' => 'Male', 'female' => 'Female'],
  4221. ['form' => 'my-form']
  4222. );
  4223. $expected = [
  4224. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4225. ['label' => ['for' => 'employee-gender-male']],
  4226. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4227. 'Male',
  4228. '/label',
  4229. ['label' => ['for' => 'employee-gender-female']],
  4230. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4231. 'Female',
  4232. '/label',
  4233. ];
  4234. $this->assertHtml($expected, $result);
  4235. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4236. $expected = [
  4237. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4238. ['label' => ['for' => 'model-custom-0']],
  4239. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4240. 'option A',
  4241. '/label',
  4242. ['label' => ['for' => 'model-custom-1']],
  4243. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4244. 'option B',
  4245. '/label',
  4246. ];
  4247. $this->assertHtml($expected, $result);
  4248. $result = $this->Form->radio(
  4249. 'Employee.gender',
  4250. [
  4251. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4252. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4253. ]
  4254. );
  4255. $expected = [
  4256. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4257. ['label' => ['for' => 'employee-gender-male']],
  4258. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4259. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4260. 'Male',
  4261. '/label',
  4262. ['label' => ['for' => 'employee-gender-female']],
  4263. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4264. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4265. 'Female',
  4266. '/label',
  4267. ];
  4268. $this->assertHtml($expected, $result);
  4269. }
  4270. /**
  4271. * Test radio with complex options and empty disabled data.
  4272. *
  4273. * @return void
  4274. */
  4275. public function testRadioComplexDisabled()
  4276. {
  4277. $options = [
  4278. ['value' => 'r', 'text' => 'red'],
  4279. ['value' => 'b', 'text' => 'blue'],
  4280. ];
  4281. $attrs = ['disabled' => []];
  4282. $result = $this->Form->radio('Model.field', $options, $attrs);
  4283. $expected = [
  4284. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4285. ['label' => ['for' => 'model-field-r']],
  4286. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4287. 'red',
  4288. '/label',
  4289. ['label' => ['for' => 'model-field-b']],
  4290. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4291. 'blue',
  4292. '/label',
  4293. ];
  4294. $this->assertHtml($expected, $result);
  4295. $attrs = ['disabled' => ['r']];
  4296. $result = $this->Form->radio('Model.field', $options, $attrs);
  4297. $this->assertContains('disabled="disabled"', $result);
  4298. }
  4299. /**
  4300. * testRadioDefaultValue method
  4301. *
  4302. * Test default value setting on radio() method.
  4303. *
  4304. * @return void
  4305. */
  4306. public function testRadioDefaultValue()
  4307. {
  4308. $Articles = TableRegistry::get('Articles');
  4309. $title = $Articles->schema()->column('title');
  4310. $Articles->schema()->addColumn(
  4311. 'title',
  4312. ['default' => '1'] + $title
  4313. );
  4314. $this->Form->create($Articles->newEntity());
  4315. $result = $this->Form->radio('title', ['option A', 'option B']);
  4316. $expected = [
  4317. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4318. ['label' => ['for' => 'title-0']],
  4319. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4320. 'option A',
  4321. '/label',
  4322. ['label' => ['for' => 'title-1']],
  4323. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4324. 'option B',
  4325. '/label',
  4326. ];
  4327. $this->assertHtml($expected, $result);
  4328. }
  4329. /**
  4330. * testControlRadio method
  4331. *
  4332. * Test that input works with radio types.
  4333. *
  4334. * @return void
  4335. */
  4336. public function testControlRadio()
  4337. {
  4338. $result = $this->Form->control('test', [
  4339. 'type' => 'radio',
  4340. 'options' => ['A', 'B'],
  4341. ]);
  4342. $expected = [
  4343. ['div' => ['class' => 'input radio']],
  4344. '<label',
  4345. 'Test',
  4346. '/label',
  4347. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4348. ['label' => ['for' => 'test-0']],
  4349. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4350. 'A',
  4351. '/label',
  4352. ['label' => ['for' => 'test-1']],
  4353. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4354. 'B',
  4355. '/label',
  4356. '/div',
  4357. ];
  4358. $this->assertHtml($expected, $result);
  4359. $result = $this->Form->control('test', [
  4360. 'type' => 'radio',
  4361. 'options' => ['A', 'B'],
  4362. 'value' => '0'
  4363. ]);
  4364. $expected = [
  4365. ['div' => ['class' => 'input radio']],
  4366. '<label',
  4367. 'Test',
  4368. '/label',
  4369. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4370. ['label' => ['for' => 'test-0']],
  4371. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4372. 'A',
  4373. '/label',
  4374. ['label' => ['for' => 'test-1']],
  4375. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4376. 'B',
  4377. '/label',
  4378. '/div',
  4379. ];
  4380. $this->assertHtml($expected, $result);
  4381. $result = $this->Form->control('test', [
  4382. 'type' => 'radio',
  4383. 'options' => ['A', 'B'],
  4384. 'label' => false
  4385. ]);
  4386. $expected = [
  4387. ['div' => ['class' => 'input radio']],
  4388. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4389. ['label' => ['for' => 'test-0']],
  4390. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4391. 'A',
  4392. '/label',
  4393. ['label' => ['for' => 'test-1']],
  4394. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4395. 'B',
  4396. '/label',
  4397. '/div',
  4398. ];
  4399. $this->assertHtml($expected, $result);
  4400. }
  4401. /**
  4402. * testRadioNoLabel method
  4403. *
  4404. * Test that radio() works with label = false.
  4405. *
  4406. * @return void
  4407. */
  4408. public function testRadioNoLabel()
  4409. {
  4410. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4411. $expected = [
  4412. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4413. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4414. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4415. ];
  4416. $this->assertHtml($expected, $result);
  4417. }
  4418. /**
  4419. * testRadioControlInsideLabel method
  4420. *
  4421. * Test generating radio input inside label ala twitter bootstrap.
  4422. *
  4423. * @return void
  4424. */
  4425. public function testRadioControlInsideLabel()
  4426. {
  4427. $this->Form->templates([
  4428. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4429. 'radioWrapper' => '{{label}}'
  4430. ]);
  4431. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4432. //@codingStandardsIgnoreStart
  4433. $expected = [
  4434. ['input' => [
  4435. 'type' => 'hidden',
  4436. 'name' => 'Model[field]',
  4437. 'value' => ''
  4438. ]],
  4439. ['label' => ['for' => 'model-field-0']],
  4440. ['input' => [
  4441. 'type' => 'radio',
  4442. 'name' => 'Model[field]',
  4443. 'value' => '0',
  4444. 'id' => 'model-field-0'
  4445. ]],
  4446. 'option A',
  4447. '/label',
  4448. ['label' => ['for' => 'model-field-1']],
  4449. ['input' => [
  4450. 'type' => 'radio',
  4451. 'name' => 'Model[field]',
  4452. 'value' => '1',
  4453. 'id' => 'model-field-1'
  4454. ]],
  4455. 'option B',
  4456. '/label',
  4457. ];
  4458. //@codingStandardsIgnoreEnd
  4459. $this->assertHtml($expected, $result);
  4460. }
  4461. /**
  4462. * testRadioHiddenControlDisabling method
  4463. *
  4464. * Test disabling the hidden input for radio buttons.
  4465. *
  4466. * @return void
  4467. */
  4468. public function testRadioHiddenControlDisabling()
  4469. {
  4470. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4471. $expected = [
  4472. 'label' => ['for' => 'model-1-field-0'],
  4473. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4474. 'option A',
  4475. '/label'
  4476. ];
  4477. $this->assertHtml($expected, $result);
  4478. }
  4479. /**
  4480. * testRadioOutOfRange method
  4481. *
  4482. * Test radio element set generation.
  4483. *
  4484. * @return void
  4485. */
  4486. public function testRadioOutOfRange()
  4487. {
  4488. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4489. $expected = [
  4490. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4491. 'label' => ['for' => 'model-field-v'],
  4492. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4493. 'value',
  4494. '/label'
  4495. ];
  4496. $this->assertHtml($expected, $result);
  4497. }
  4498. /**
  4499. * testSelect method
  4500. *
  4501. * Test select element generation.
  4502. *
  4503. * @return void
  4504. */
  4505. public function testSelect()
  4506. {
  4507. $result = $this->Form->select('Model.field', []);
  4508. $expected = [
  4509. 'select' => ['name' => 'Model[field]'],
  4510. '/select'
  4511. ];
  4512. $this->assertHtml($expected, $result);
  4513. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4514. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4515. $expected = [
  4516. 'select' => ['name' => 'Model[field]'],
  4517. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4518. 'good',
  4519. '/option',
  4520. ['option' => ['value' => 'other']],
  4521. 'bad',
  4522. '/option',
  4523. '/select'
  4524. ];
  4525. $this->assertHtml($expected, $result);
  4526. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4527. $this->assertHtml($expected, $result);
  4528. $this->Form->request->data = [];
  4529. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4530. $expected = [
  4531. 'select' => ['name' => 'Model[field]'],
  4532. ['option' => ['value' => 'value']],
  4533. 'good',
  4534. '/option',
  4535. ['option' => ['value' => 'other']],
  4536. 'bad',
  4537. '/option',
  4538. '/select'
  4539. ];
  4540. $this->assertHtml($expected, $result);
  4541. $options = [
  4542. ['value' => 'first', 'text' => 'First'],
  4543. ['value' => 'first', 'text' => 'Another First'],
  4544. ];
  4545. $result = $this->Form->select(
  4546. 'Model.field',
  4547. $options,
  4548. ['escape' => false, 'empty' => false]
  4549. );
  4550. $expected = [
  4551. 'select' => ['name' => 'Model[field]'],
  4552. ['option' => ['value' => 'first']],
  4553. 'First',
  4554. '/option',
  4555. ['option' => ['value' => 'first']],
  4556. 'Another First',
  4557. '/option',
  4558. '/select'
  4559. ];
  4560. $this->assertHtml($expected, $result);
  4561. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4562. $result = $this->Form->select(
  4563. 'Model.contact_id',
  4564. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4565. ['escape' => false, 'empty' => 'pick something']
  4566. );
  4567. $expected = [
  4568. 'select' => ['name' => 'Model[contact_id]'],
  4569. ['option' => ['value' => '']], 'pick something', '/option',
  4570. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4571. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4572. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4573. '/select'
  4574. ];
  4575. $this->assertHtml($expected, $result);
  4576. $this->Form->request->data['Model']['field'] = 0;
  4577. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4578. $expected = [
  4579. 'select' => ['name' => 'Model[field]'],
  4580. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4581. ['option' => ['value' => '1']], 'Yes', '/option',
  4582. '/select'
  4583. ];
  4584. $this->assertHtml($expected, $result);
  4585. }
  4586. /**
  4587. * testSelectEscapeHtml method
  4588. *
  4589. * Test that select() escapes HTML.
  4590. *
  4591. * @return void
  4592. */
  4593. public function testSelectEscapeHtml()
  4594. {
  4595. $result = $this->Form->select(
  4596. 'Model.field',
  4597. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4598. ['empty' => false]
  4599. );
  4600. $expected = [
  4601. 'select' => ['name' => 'Model[field]'],
  4602. ['option' => ['value' => 'first']],
  4603. 'first &quot;html&quot; &lt;chars&gt;',
  4604. '/option',
  4605. ['option' => ['value' => 'second']],
  4606. 'value',
  4607. '/option',
  4608. '/select'
  4609. ];
  4610. $this->assertHtml($expected, $result);
  4611. $result = $this->Form->select(
  4612. 'Model.field',
  4613. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4614. ['escape' => false, 'empty' => false]
  4615. );
  4616. $expected = [
  4617. 'select' => ['name' => 'Model[field]'],
  4618. ['option' => ['value' => 'first']],
  4619. 'first "html" <chars>',
  4620. '/option',
  4621. ['option' => ['value' => 'second']],
  4622. 'value',
  4623. '/option',
  4624. '/select'
  4625. ];
  4626. $this->assertHtml($expected, $result);
  4627. }
  4628. /**
  4629. * testSelectRequired method
  4630. *
  4631. * Test select() with required and disabled attributes.
  4632. *
  4633. * @return void
  4634. */
  4635. public function testSelectRequired()
  4636. {
  4637. $this->article['required'] = [
  4638. 'user_id' => true
  4639. ];
  4640. $this->Form->create($this->article);
  4641. $result = $this->Form->select('user_id', ['option A']);
  4642. $expected = [
  4643. 'select' => [
  4644. 'name' => 'user_id',
  4645. 'required' => 'required'
  4646. ],
  4647. ['option' => ['value' => '0']], 'option A', '/option',
  4648. '/select'
  4649. ];
  4650. $this->assertHtml($expected, $result);
  4651. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4652. $expected = [
  4653. 'select' => [
  4654. 'name' => 'user_id',
  4655. 'disabled' => 'disabled'
  4656. ],
  4657. ['option' => ['value' => '0']], 'option A', '/option',
  4658. '/select'
  4659. ];
  4660. $this->assertHtml($expected, $result);
  4661. }
  4662. /**
  4663. * testNestedSelect method
  4664. *
  4665. * Test select element generation with optgroups.
  4666. *
  4667. * @return void
  4668. */
  4669. public function testNestedSelect()
  4670. {
  4671. $result = $this->Form->select(
  4672. 'Model.field',
  4673. [1 => 'One', 2 => 'Two', 'Three' => [
  4674. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4675. ]],
  4676. ['empty' => false]
  4677. );
  4678. $expected = [
  4679. 'select' => ['name' => 'Model[field]'],
  4680. ['option' => ['value' => 1]],
  4681. 'One',
  4682. '/option',
  4683. ['option' => ['value' => 2]],
  4684. 'Two',
  4685. '/option',
  4686. ['optgroup' => ['label' => 'Three']],
  4687. ['option' => ['value' => 3]],
  4688. 'Three',
  4689. '/option',
  4690. ['option' => ['value' => 4]],
  4691. 'Four',
  4692. '/option',
  4693. ['option' => ['value' => 5]],
  4694. 'Five',
  4695. '/option',
  4696. '/optgroup',
  4697. '/select'
  4698. ];
  4699. $this->assertHtml($expected, $result);
  4700. }
  4701. /**
  4702. * testSelectMultiple method
  4703. *
  4704. * Test generation of multiple select elements.
  4705. *
  4706. * @return void
  4707. */
  4708. public function testSelectMultiple()
  4709. {
  4710. $options = ['first', 'second', 'third'];
  4711. $result = $this->Form->select(
  4712. 'Model.multi_field',
  4713. $options,
  4714. ['form' => 'my-form', 'multiple' => true]
  4715. );
  4716. $expected = [
  4717. 'input' => [
  4718. 'type' => 'hidden',
  4719. 'name' => 'Model[multi_field]',
  4720. 'value' => '',
  4721. 'form' => 'my-form',
  4722. ],
  4723. 'select' => [
  4724. 'name' => 'Model[multi_field][]',
  4725. 'multiple' => 'multiple',
  4726. 'form' => 'my-form',
  4727. ],
  4728. ['option' => ['value' => '0']],
  4729. 'first',
  4730. '/option',
  4731. ['option' => ['value' => '1']],
  4732. 'second',
  4733. '/option',
  4734. ['option' => ['value' => '2']],
  4735. 'third',
  4736. '/option',
  4737. '/select'
  4738. ];
  4739. $this->assertHtml($expected, $result);
  4740. $result = $this->Form->select(
  4741. 'Model.multi_field',
  4742. $options,
  4743. ['multiple' => 'multiple', 'form' => 'my-form']
  4744. );
  4745. $this->assertHtml($expected, $result);
  4746. }
  4747. /**
  4748. * testCheckboxZeroValue method
  4749. *
  4750. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4751. *
  4752. * @return void
  4753. */
  4754. public function testCheckboxZeroValue()
  4755. {
  4756. $result = $this->Form->control('User.get_spam', [
  4757. 'type' => 'checkbox',
  4758. 'value' => '0',
  4759. 'hiddenField' => '1',
  4760. ]);
  4761. $expected = [
  4762. 'div' => ['class' => 'input checkbox'],
  4763. 'label' => ['for' => 'user-get-spam'],
  4764. ['input' => [
  4765. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4766. 'value' => '1'
  4767. ]],
  4768. ['input' => [
  4769. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4770. 'value' => '0', 'id' => 'user-get-spam'
  4771. ]],
  4772. 'Get Spam',
  4773. '/label',
  4774. '/div'
  4775. ];
  4776. $this->assertHtml($expected, $result);
  4777. }
  4778. /**
  4779. * testHabtmSelectBox method
  4780. *
  4781. * Test generation of habtm select boxes.
  4782. *
  4783. * @return void
  4784. */
  4785. public function testHabtmSelectBox()
  4786. {
  4787. $this->loadFixtures('Articles');
  4788. $options = [
  4789. 1 => 'blue',
  4790. 2 => 'red',
  4791. 3 => 'green'
  4792. ];
  4793. $tags = [
  4794. new Entity(['id' => 1, 'name' => 'blue']),
  4795. new Entity(['id' => 3, 'name' => 'green'])
  4796. ];
  4797. $article = new Article(['tags' => $tags]);
  4798. $this->Form->create($article);
  4799. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4800. $expected = [
  4801. 'div' => ['class' => 'input select'],
  4802. 'label' => ['for' => 'tags-ids'],
  4803. 'Tags',
  4804. '/label',
  4805. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4806. 'select' => [
  4807. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4808. 'multiple' => 'multiple'
  4809. ],
  4810. ['option' => ['value' => '1', 'selected' => 'selected']],
  4811. 'blue',
  4812. '/option',
  4813. ['option' => ['value' => '2']],
  4814. 'red',
  4815. '/option',
  4816. ['option' => ['value' => '3', 'selected' => 'selected']],
  4817. 'green',
  4818. '/option',
  4819. '/select',
  4820. '/div'
  4821. ];
  4822. $this->assertHtml($expected, $result);
  4823. // make sure only 50 is selected, and not 50f5c0cf
  4824. $options = [
  4825. '1' => 'blue',
  4826. '50f5c0cf' => 'red',
  4827. '50' => 'green'
  4828. ];
  4829. $tags = [
  4830. new Entity(['id' => 1, 'name' => 'blue']),
  4831. new Entity(['id' => 50, 'name' => 'green'])
  4832. ];
  4833. $article = new Article(['tags' => $tags]);
  4834. $this->Form->create($article);
  4835. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4836. $expected = [
  4837. 'div' => ['class' => 'input select'],
  4838. 'label' => ['for' => 'tags-ids'],
  4839. 'Tags',
  4840. '/label',
  4841. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4842. 'select' => [
  4843. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4844. 'multiple' => 'multiple'
  4845. ],
  4846. ['option' => ['value' => '1', 'selected' => 'selected']],
  4847. 'blue',
  4848. '/option',
  4849. ['option' => ['value' => '50f5c0cf']],
  4850. 'red',
  4851. '/option',
  4852. ['option' => ['value' => '50', 'selected' => 'selected']],
  4853. 'green',
  4854. '/option',
  4855. '/select',
  4856. '/div'
  4857. ];
  4858. $this->assertHtml($expected, $result);
  4859. $spacecraft = [
  4860. 1 => 'Orion',
  4861. 2 => 'Helios'
  4862. ];
  4863. $this->View->viewVars['spacecraft'] = $spacecraft;
  4864. $this->Form->create();
  4865. $result = $this->Form->control('spacecraft._ids');
  4866. $expected = [
  4867. 'div' => ['class' => 'input select'],
  4868. 'label' => ['for' => 'spacecraft-ids'],
  4869. 'Spacecraft',
  4870. '/label',
  4871. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4872. 'select' => [
  4873. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4874. 'multiple' => 'multiple'
  4875. ],
  4876. ['option' => ['value' => '1']],
  4877. 'Orion',
  4878. '/option',
  4879. ['option' => ['value' => '2']],
  4880. 'Helios',
  4881. '/option',
  4882. '/select',
  4883. '/div'
  4884. ];
  4885. $this->assertHtml($expected, $result);
  4886. }
  4887. /**
  4888. * testErrorsForBelongsToManySelect method
  4889. *
  4890. * Tests that errors for belongsToMany select fields are being
  4891. * picked up properly.
  4892. *
  4893. * @return void
  4894. */
  4895. public function testErrorsForBelongsToManySelect()
  4896. {
  4897. $spacecraft = [
  4898. 1 => 'Orion',
  4899. 2 => 'Helios'
  4900. ];
  4901. $this->View->viewVars['spacecraft'] = $spacecraft;
  4902. $article = new Article();
  4903. $article->errors('spacecraft', ['Invalid']);
  4904. $this->Form->create($article);
  4905. $result = $this->Form->control('spacecraft._ids');
  4906. $expected = [
  4907. ['div' => ['class' => 'input select error']],
  4908. 'label' => ['for' => 'spacecraft-ids'],
  4909. 'Spacecraft',
  4910. '/label',
  4911. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4912. 'select' => [
  4913. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4914. 'multiple' => 'multiple'
  4915. ],
  4916. ['option' => ['value' => '1']],
  4917. 'Orion',
  4918. '/option',
  4919. ['option' => ['value' => '2']],
  4920. 'Helios',
  4921. '/option',
  4922. '/select',
  4923. ['div' => ['class' => 'error-message']],
  4924. 'Invalid',
  4925. '/div',
  4926. '/div'
  4927. ];
  4928. $this->assertHtml($expected, $result);
  4929. }
  4930. /**
  4931. * testSelectMultipleCheckboxes method
  4932. *
  4933. * Test generation of multi select elements in checkbox format.
  4934. *
  4935. * @return void
  4936. */
  4937. public function testSelectMultipleCheckboxes()
  4938. {
  4939. $result = $this->Form->select(
  4940. 'Model.multi_field',
  4941. ['first', 'second', 'third'],
  4942. ['multiple' => 'checkbox']
  4943. );
  4944. $expected = [
  4945. 'input' => [
  4946. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4947. ],
  4948. ['div' => ['class' => 'checkbox']],
  4949. ['label' => ['for' => 'model-multi-field-0']],
  4950. ['input' => [
  4951. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4952. 'value' => '0', 'id' => 'model-multi-field-0'
  4953. ]],
  4954. 'first',
  4955. '/label',
  4956. '/div',
  4957. ['div' => ['class' => 'checkbox']],
  4958. ['label' => ['for' => 'model-multi-field-1']],
  4959. ['input' => [
  4960. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4961. 'value' => '1', 'id' => 'model-multi-field-1'
  4962. ]],
  4963. 'second',
  4964. '/label',
  4965. '/div',
  4966. ['div' => ['class' => 'checkbox']],
  4967. ['label' => ['for' => 'model-multi-field-2']],
  4968. ['input' => [
  4969. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4970. 'value' => '2', 'id' => 'model-multi-field-2'
  4971. ]],
  4972. 'third',
  4973. '/label',
  4974. '/div'
  4975. ];
  4976. $this->assertHtml($expected, $result);
  4977. $result = $this->Form->select(
  4978. 'Model.multi_field',
  4979. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4980. ['multiple' => 'checkbox']
  4981. );
  4982. $expected = [
  4983. 'input' => [
  4984. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4985. ],
  4986. ['div' => ['class' => 'checkbox']],
  4987. ['label' => ['for' => 'model-multi-field-a+']],
  4988. ['input' => [
  4989. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4990. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4991. ]],
  4992. 'first',
  4993. '/label',
  4994. '/div',
  4995. ['div' => ['class' => 'checkbox']],
  4996. ['label' => ['for' => 'model-multi-field-a++']],
  4997. ['input' => [
  4998. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4999. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5000. ]],
  5001. 'second',
  5002. '/label',
  5003. '/div',
  5004. ['div' => ['class' => 'checkbox']],
  5005. ['label' => ['for' => 'model-multi-field-a+++']],
  5006. ['input' => [
  5007. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5008. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5009. ]],
  5010. 'third',
  5011. '/label',
  5012. '/div'
  5013. ];
  5014. $this->assertHtml($expected, $result);
  5015. $result = $this->Form->select(
  5016. 'Model.multi_field',
  5017. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5018. ['multiple' => 'checkbox']
  5019. );
  5020. $expected = [
  5021. 'input' => [
  5022. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5023. ],
  5024. ['div' => ['class' => 'checkbox']],
  5025. ['label' => ['for' => 'model-multi-field-a-b']],
  5026. ['input' => [
  5027. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5028. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5029. ]],
  5030. 'first',
  5031. '/label',
  5032. '/div',
  5033. ['div' => ['class' => 'checkbox']],
  5034. ['label' => ['for' => 'model-multi-field-a-b1']],
  5035. ['input' => [
  5036. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5037. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5038. ]],
  5039. 'second',
  5040. '/label',
  5041. '/div',
  5042. ['div' => ['class' => 'checkbox']],
  5043. ['label' => ['for' => 'model-multi-field-a-b2']],
  5044. ['input' => [
  5045. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5046. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5047. ]],
  5048. 'third',
  5049. '/label',
  5050. '/div'
  5051. ];
  5052. $this->assertHtml($expected, $result);
  5053. }
  5054. /**
  5055. * testSelectMultipleCheckboxRequestData method
  5056. *
  5057. * Ensure that multiCheckbox reads from the request data.
  5058. *
  5059. * @return void
  5060. */
  5061. public function testSelectMultipleCheckboxRequestData()
  5062. {
  5063. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5064. $result = $this->Form->select(
  5065. 'Model.tags',
  5066. ['1' => 'first', 'Array' => 'Array'],
  5067. ['multiple' => 'checkbox']
  5068. );
  5069. $expected = [
  5070. 'input' => [
  5071. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5072. ],
  5073. ['div' => ['class' => 'checkbox']],
  5074. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5075. ['input' => [
  5076. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5077. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5078. ]],
  5079. 'first',
  5080. '/label',
  5081. '/div',
  5082. ['div' => ['class' => 'checkbox']],
  5083. ['label' => ['for' => 'model-tags-array']],
  5084. ['input' => [
  5085. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5086. 'value' => 'Array', 'id' => 'model-tags-array'
  5087. ]],
  5088. 'Array',
  5089. '/label',
  5090. '/div'
  5091. ];
  5092. $this->assertHtml($expected, $result);
  5093. }
  5094. /**
  5095. * testSelectMultipleCheckboxSecurity method
  5096. *
  5097. * Checks the security hash array generated for multiple-input checkbox elements.
  5098. *
  5099. * @return void
  5100. */
  5101. public function testSelectMultipleCheckboxSecurity()
  5102. {
  5103. $this->Form->request->params['_Token'] = 'testKey';
  5104. $this->assertEquals([], $this->Form->fields);
  5105. $this->Form->select(
  5106. 'Model.multi_field',
  5107. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5108. ['multiple' => 'checkbox']
  5109. );
  5110. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5111. $result = $this->Form->secure($this->Form->fields);
  5112. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5113. $this->assertRegExp('/"' . $key . '"/', $result);
  5114. }
  5115. /**
  5116. * testSelectMultipleSecureWithNoOptions method
  5117. *
  5118. * Multiple select elements should always be secured as they always participate
  5119. * in the POST data.
  5120. *
  5121. * @return void
  5122. */
  5123. public function testSelectMultipleSecureWithNoOptions()
  5124. {
  5125. $this->assertEquals([], $this->Form->fields);
  5126. $this->Form->select(
  5127. 'Model.select',
  5128. [],
  5129. ['multiple' => true]
  5130. );
  5131. $this->assertEquals(['Model.select'], $this->Form->fields);
  5132. }
  5133. /**
  5134. * testSelectNoSecureWithNoOptions method
  5135. *
  5136. * When a select box has no options it should not be added to the fields list
  5137. * as it always fail post validation.
  5138. *
  5139. * @return void
  5140. */
  5141. public function testSelectNoSecureWithNoOptions()
  5142. {
  5143. $this->Form->request->params['_Token'] = 'testkey';
  5144. $this->assertEquals([], $this->Form->fields);
  5145. $this->Form->select(
  5146. 'Model.select',
  5147. []
  5148. );
  5149. $this->assertEquals([], $this->Form->fields);
  5150. $this->Form->select(
  5151. 'Model.user_id',
  5152. [],
  5153. ['empty' => true]
  5154. );
  5155. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5156. }
  5157. /**
  5158. * testControlMultipleCheckboxes method
  5159. *
  5160. * Test control() resulting in multi select elements being generated.
  5161. *
  5162. * @return void
  5163. */
  5164. public function testControlMultipleCheckboxes()
  5165. {
  5166. $result = $this->Form->control('Model.multi_field', [
  5167. 'options' => ['first', 'second', '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-0']],
  5178. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5179. 'first',
  5180. '/label',
  5181. '/div',
  5182. ['div' => ['class' => 'checkbox']],
  5183. ['label' => ['for' => 'model-multi-field-1']],
  5184. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5185. 'second',
  5186. '/label',
  5187. '/div',
  5188. ['div' => ['class' => 'checkbox']],
  5189. ['label' => ['for' => 'model-multi-field-2']],
  5190. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5191. 'third',
  5192. '/label',
  5193. '/div',
  5194. '/div'
  5195. ];
  5196. $this->assertHtml($expected, $result);
  5197. $result = $this->Form->control('Model.multi_field', [
  5198. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5199. 'multiple' => 'checkbox'
  5200. ]);
  5201. $expected = [
  5202. ['div' => ['class' => 'input select']],
  5203. ['label' => ['for' => 'model-multi-field']],
  5204. 'Multi Field',
  5205. '/label',
  5206. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5207. ['div' => ['class' => 'checkbox']],
  5208. ['label' => ['for' => 'model-multi-field-a']],
  5209. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5210. 'first',
  5211. '/label',
  5212. '/div',
  5213. ['div' => ['class' => 'checkbox']],
  5214. ['label' => ['for' => 'model-multi-field-b']],
  5215. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5216. 'second',
  5217. '/label',
  5218. '/div',
  5219. ['div' => ['class' => 'checkbox']],
  5220. ['label' => ['for' => 'model-multi-field-c']],
  5221. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5222. 'third',
  5223. '/label',
  5224. '/div',
  5225. '/div'
  5226. ];
  5227. $this->assertHtml($expected, $result);
  5228. }
  5229. /**
  5230. * testSelectHiddenFieldOmission method
  5231. *
  5232. * Test that select() with 'hiddenField' => false omits the hidden field.
  5233. *
  5234. * @return void
  5235. */
  5236. public function testSelectHiddenFieldOmission()
  5237. {
  5238. $result = $this->Form->select(
  5239. 'Model.multi_field',
  5240. ['first', 'second'],
  5241. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5242. );
  5243. $this->assertNotContains('type="hidden"', $result);
  5244. }
  5245. /**
  5246. * testSelectCheckboxMultipleOverrideName method
  5247. *
  5248. * Test that select() with multiple = checkbox works with overriding name attribute.
  5249. *
  5250. * @return void
  5251. */
  5252. public function testSelectCheckboxMultipleOverrideName()
  5253. {
  5254. $result = $this->Form->select('category', ['1', '2'], [
  5255. 'multiple' => 'checkbox',
  5256. 'name' => 'fish',
  5257. ]);
  5258. $expected = [
  5259. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5260. ['div' => ['class' => 'checkbox']],
  5261. ['label' => ['for' => 'fish-0']],
  5262. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5263. '1',
  5264. '/label',
  5265. '/div',
  5266. ['div' => ['class' => 'checkbox']],
  5267. ['label' => ['for' => 'fish-1']],
  5268. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5269. '2',
  5270. '/label',
  5271. '/div'
  5272. ];
  5273. $this->assertHtml($expected, $result);
  5274. $result = $this->Form->multiCheckbox(
  5275. 'category',
  5276. new Collection(['1', '2']),
  5277. ['name' => 'fish']
  5278. );
  5279. $this->assertHtml($expected, $result);
  5280. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5281. 'name' => 'fish',
  5282. ]);
  5283. $this->assertHtml($expected, $result);
  5284. }
  5285. /**
  5286. * testControlMultiCheckbox method
  5287. *
  5288. * Test that control() works with multicheckbox.
  5289. *
  5290. * @return void
  5291. */
  5292. public function testControlMultiCheckbox()
  5293. {
  5294. $result = $this->Form->control('category', [
  5295. 'type' => 'multicheckbox',
  5296. 'options' => ['1', '2'],
  5297. ]);
  5298. $expected = [
  5299. ['div' => ['class' => 'input multicheckbox']],
  5300. '<label',
  5301. 'Category',
  5302. '/label',
  5303. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5304. ['div' => ['class' => 'checkbox']],
  5305. ['label' => ['for' => 'category-0']],
  5306. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5307. '1',
  5308. '/label',
  5309. '/div',
  5310. ['div' => ['class' => 'checkbox']],
  5311. ['label' => ['for' => 'category-1']],
  5312. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5313. '2',
  5314. '/label',
  5315. '/div',
  5316. '/div',
  5317. ];
  5318. $this->assertHtml($expected, $result);
  5319. }
  5320. /**
  5321. * testCheckbox method
  5322. *
  5323. * Test generation of checkboxes.
  5324. *
  5325. * @return void
  5326. */
  5327. public function testCheckbox()
  5328. {
  5329. $result = $this->Form->checkbox('Model.field');
  5330. $expected = [
  5331. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5332. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5333. ];
  5334. $this->assertHtml($expected, $result);
  5335. $result = $this->Form->checkbox('Model.field', [
  5336. 'id' => 'theID',
  5337. 'value' => 'myvalue',
  5338. 'form' => 'my-form',
  5339. ]);
  5340. $expected = [
  5341. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5342. ['input' => [
  5343. 'type' => 'checkbox', 'name' => 'Model[field]',
  5344. 'value' => 'myvalue', 'id' => 'theID',
  5345. 'form' => 'my-form',
  5346. ]]
  5347. ];
  5348. $this->assertHtml($expected, $result);
  5349. }
  5350. /**
  5351. * testCheckboxDefaultValue method
  5352. *
  5353. * Test default value setting on checkbox() method.
  5354. *
  5355. * @return void
  5356. */
  5357. public function testCheckboxDefaultValue()
  5358. {
  5359. $this->Form->request->data['Model']['field'] = false;
  5360. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5361. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5362. $this->assertHtml($expected, $result);
  5363. unset($this->Form->request->data['Model']['field']);
  5364. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5365. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5366. $this->assertHtml($expected, $result);
  5367. $this->Form->request->data['Model']['field'] = true;
  5368. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5369. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5370. $this->assertHtml($expected, $result);
  5371. unset($this->Form->request->data['Model']['field']);
  5372. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5373. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5374. $this->assertHtml($expected, $result);
  5375. $Articles = TableRegistry::get('Articles');
  5376. $Articles->schema()->addColumn(
  5377. 'published',
  5378. ['type' => 'boolean', 'null' => false, 'default' => true]
  5379. );
  5380. $this->Form->create($Articles->newEntity());
  5381. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5382. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5383. $this->assertHtml($expected, $result);
  5384. }
  5385. /**
  5386. * testCheckboxCheckedAndError method
  5387. *
  5388. * Test checkbox being checked or having errors.
  5389. *
  5390. * @return void
  5391. */
  5392. public function testCheckboxCheckedAndError()
  5393. {
  5394. $this->article['errors'] = [
  5395. 'published' => true
  5396. ];
  5397. $this->Form->request->data['published'] = 'myvalue';
  5398. $this->Form->create($this->article);
  5399. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5400. $expected = [
  5401. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5402. ['input' => [
  5403. 'type' => 'checkbox',
  5404. 'name' => 'published',
  5405. 'value' => 'myvalue',
  5406. 'id' => 'theID',
  5407. 'checked' => 'checked',
  5408. 'class' => 'form-error'
  5409. ]]
  5410. ];
  5411. $this->assertHtml($expected, $result);
  5412. $this->Form->request->data['published'] = '';
  5413. $result = $this->Form->checkbox('published');
  5414. $expected = [
  5415. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5416. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5417. ];
  5418. $this->assertHtml($expected, $result);
  5419. }
  5420. /**
  5421. * testCheckboxCustomNameAttribute method
  5422. *
  5423. * Test checkbox() with a custom name attribute.
  5424. *
  5425. * @return void
  5426. */
  5427. public function testCheckboxCustomNameAttribute()
  5428. {
  5429. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5430. $expected = [
  5431. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5432. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5433. ];
  5434. $this->assertHtml($expected, $result);
  5435. }
  5436. /**
  5437. * testCheckboxHiddenField method
  5438. *
  5439. * Test that the hidden input for checkboxes can be omitted or set to a
  5440. * specific value.
  5441. *
  5442. * @return void
  5443. */
  5444. public function testCheckboxHiddenField()
  5445. {
  5446. $result = $this->Form->checkbox('UserForm.something', [
  5447. 'hiddenField' => false
  5448. ]);
  5449. $expected = [
  5450. 'input' => [
  5451. 'type' => 'checkbox',
  5452. 'name' => 'UserForm[something]',
  5453. 'value' => '1'
  5454. ],
  5455. ];
  5456. $this->assertHtml($expected, $result);
  5457. $result = $this->Form->checkbox('UserForm.something', [
  5458. 'value' => 'Y',
  5459. 'hiddenField' => 'N',
  5460. ]);
  5461. $expected = [
  5462. ['input' => [
  5463. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5464. 'value' => 'N'
  5465. ]],
  5466. ['input' => [
  5467. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5468. 'value' => 'Y'
  5469. ]],
  5470. ];
  5471. $this->assertHtml($expected, $result);
  5472. }
  5473. /**
  5474. * testTime method
  5475. *
  5476. * Test the time type.
  5477. *
  5478. * @return void
  5479. */
  5480. public function testTime()
  5481. {
  5482. $result = $this->Form->time('start_time', [
  5483. 'timeFormat' => 12,
  5484. 'interval' => 5,
  5485. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5486. ]);
  5487. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5488. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5489. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5490. $this->assertNotContains('year', $result);
  5491. $this->assertNotContains('month', $result);
  5492. $this->assertNotContains('day', $result);
  5493. $result = $this->Form->time('start_time', [
  5494. 'timeFormat' => 12,
  5495. 'interval' => 5,
  5496. 'value' => '2014-03-08 16:30:00'
  5497. ]);
  5498. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5499. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5500. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5501. $this->assertNotContains('year', $result);
  5502. $this->assertNotContains('month', $result);
  5503. $this->assertNotContains('day', $result);
  5504. }
  5505. /**
  5506. * testTimeFormat24NoMeridian method
  5507. *
  5508. * Ensure that timeFormat=24 has no merdian.
  5509. *
  5510. * @return void.
  5511. */
  5512. public function testTimeFormat24NoMeridian()
  5513. {
  5514. $result = $this->Form->time('start_time', [
  5515. 'timeFormat' => 24,
  5516. 'interval' => 5,
  5517. 'value' => '2014-03-08 16:30:00'
  5518. ]);
  5519. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5520. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5521. $this->assertNotContains('meridian', $result);
  5522. $this->assertNotContains('pm', $result);
  5523. $this->assertNotContains('year', $result);
  5524. $this->assertNotContains('month', $result);
  5525. $this->assertNotContains('day', $result);
  5526. }
  5527. /**
  5528. * testDate method
  5529. *
  5530. * Test the date type.
  5531. *
  5532. * @return void
  5533. */
  5534. public function testDate()
  5535. {
  5536. $result = $this->Form->date('start_day', [
  5537. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5538. ]);
  5539. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5540. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5541. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5542. $this->assertNotContains('hour', $result);
  5543. $this->assertNotContains('minute', $result);
  5544. $this->assertNotContains('second', $result);
  5545. $this->assertNotContains('meridian', $result);
  5546. }
  5547. /**
  5548. * testDateTime method
  5549. *
  5550. * Test generation of date/time select elements.
  5551. *
  5552. * @return void
  5553. */
  5554. public function testDateTime()
  5555. {
  5556. extract($this->dateRegex);
  5557. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5558. $now = strtotime('now');
  5559. $expected = [
  5560. ['select' => ['name' => 'Contact[date][year]']],
  5561. ['option' => ['value' => '']],
  5562. '/option',
  5563. $yearsRegex,
  5564. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5565. date('Y', $now),
  5566. '/option',
  5567. '*/select',
  5568. ['select' => ['name' => 'Contact[date][month]']],
  5569. ['option' => ['value' => '']],
  5570. '/option',
  5571. $monthsRegex,
  5572. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5573. date('F', $now),
  5574. '/option',
  5575. '*/select',
  5576. ['select' => ['name' => 'Contact[date][day]']],
  5577. ['option' => ['value' => '']],
  5578. '/option',
  5579. $daysRegex,
  5580. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5581. date('j', $now),
  5582. '/option',
  5583. '*/select',
  5584. ['select' => ['name' => 'Contact[date][hour]']],
  5585. ['option' => ['value' => '']],
  5586. '/option',
  5587. $hoursRegex,
  5588. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5589. date('G', $now),
  5590. '/option',
  5591. '*/select',
  5592. ['select' => ['name' => 'Contact[date][minute]']],
  5593. ['option' => ['value' => '']],
  5594. '/option',
  5595. $minutesRegex,
  5596. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5597. date('i', $now),
  5598. '/option',
  5599. '*/select',
  5600. ];
  5601. $this->assertHtml($expected, $result);
  5602. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5603. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5604. $now = strtotime('now');
  5605. $expected = [
  5606. ['select' => ['name' => 'Contact[date][year]']],
  5607. $yearsRegex,
  5608. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5609. date('Y', $now),
  5610. '/option',
  5611. '*/select',
  5612. ['select' => ['name' => 'Contact[date][month]']],
  5613. $monthsRegex,
  5614. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5615. date('F', $now),
  5616. '/option',
  5617. '*/select',
  5618. ['select' => ['name' => 'Contact[date][day]']],
  5619. $daysRegex,
  5620. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5621. date('j', $now),
  5622. '/option',
  5623. '*/select',
  5624. ['select' => ['name' => 'Contact[date][hour]']],
  5625. $hoursRegex,
  5626. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5627. date('G', $now),
  5628. '/option',
  5629. '*/select',
  5630. ['select' => ['name' => 'Contact[date][minute]']],
  5631. $minutesRegex,
  5632. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5633. date('i', $now),
  5634. '/option',
  5635. '*/select',
  5636. ];
  5637. $this->assertHtml($expected, $result);
  5638. }
  5639. /**
  5640. * testDateTimeSecured method
  5641. *
  5642. * Test that datetime fields are added to protected fields list.
  5643. *
  5644. * @return void
  5645. */
  5646. public function testDateTimeSecured()
  5647. {
  5648. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5649. $this->Form->dateTime('Contact.date');
  5650. $expected = [
  5651. 'Contact.date.year',
  5652. 'Contact.date.month',
  5653. 'Contact.date.day',
  5654. 'Contact.date.hour',
  5655. 'Contact.date.minute',
  5656. ];
  5657. $this->assertEquals($expected, $this->Form->fields);
  5658. $this->Form->fields = [];
  5659. $this->Form->date('Contact.published');
  5660. $expected = [
  5661. 'Contact.published.year',
  5662. 'Contact.published.month',
  5663. 'Contact.published.day',
  5664. ];
  5665. $this->assertEquals($expected, $this->Form->fields);
  5666. }
  5667. /**
  5668. * testDateTimeSecuredDisabled method
  5669. *
  5670. * Test that datetime fields are added to protected fields list.
  5671. *
  5672. * @return void
  5673. */
  5674. public function testDateTimeSecuredDisabled()
  5675. {
  5676. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5677. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5678. $expected = [];
  5679. $this->assertEquals($expected, $this->Form->fields);
  5680. $this->Form->fields = [];
  5681. $this->Form->date('Contact.published', ['secure' => false]);
  5682. $expected = [];
  5683. $this->assertEquals($expected, $this->Form->fields);
  5684. }
  5685. /**
  5686. * testDatetimeEmpty method
  5687. *
  5688. * Test empty defaulting to true for datetime.
  5689. *
  5690. * @return void
  5691. */
  5692. public function testDatetimeEmpty()
  5693. {
  5694. extract($this->dateRegex);
  5695. $result = $this->Form->dateTime('Contact.date', [
  5696. 'timeFormat' => 12,
  5697. 'empty' => true,
  5698. 'default' => true
  5699. ]);
  5700. $expected = [
  5701. ['select' => ['name' => 'Contact[date][year]']],
  5702. $yearsRegex,
  5703. ['option' => ['value' => '']],
  5704. '/option',
  5705. '*/select',
  5706. ['select' => ['name' => 'Contact[date][month]']],
  5707. $monthsRegex,
  5708. ['option' => ['value' => '']],
  5709. '/option',
  5710. '*/select',
  5711. ['select' => ['name' => 'Contact[date][day]']],
  5712. $daysRegex,
  5713. ['option' => ['value' => '']],
  5714. '/option',
  5715. '*/select',
  5716. ['select' => ['name' => 'Contact[date][hour]']],
  5717. $hoursRegex,
  5718. ['option' => ['value' => '']],
  5719. '/option',
  5720. '*/select',
  5721. ['select' => ['name' => 'Contact[date][minute]']],
  5722. $minutesRegex,
  5723. ['option' => ['value' => '']],
  5724. '/option',
  5725. '*/select',
  5726. ['select' => ['name' => 'Contact[date][meridian]']],
  5727. $meridianRegex,
  5728. ['option' => ['value' => '']],
  5729. '/option',
  5730. '*/select'
  5731. ];
  5732. $this->assertHtml($expected, $result);
  5733. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5734. }
  5735. /**
  5736. * testDatetimeMinuteInterval method
  5737. *
  5738. * Test datetime with interval option.
  5739. *
  5740. * @return void
  5741. */
  5742. public function testDatetimeMinuteInterval()
  5743. {
  5744. extract($this->dateRegex);
  5745. $result = $this->Form->dateTime('Contact.date', [
  5746. 'interval' => 5,
  5747. 'value' => ''
  5748. ]);
  5749. $expected = [
  5750. ['select' => ['name' => 'Contact[date][year]']],
  5751. $yearsRegex,
  5752. ['option' => ['selected' => 'selected', 'value' => '']],
  5753. '/option',
  5754. '*/select',
  5755. ['select' => ['name' => 'Contact[date][month]']],
  5756. $monthsRegex,
  5757. ['option' => ['selected' => 'selected', 'value' => '']],
  5758. '/option',
  5759. '*/select',
  5760. ['select' => ['name' => 'Contact[date][day]']],
  5761. $daysRegex,
  5762. ['option' => ['selected' => 'selected', 'value' => '']],
  5763. '/option',
  5764. '*/select',
  5765. ['select' => ['name' => 'Contact[date][hour]']],
  5766. $hoursRegex,
  5767. ['option' => ['selected' => 'selected', 'value' => '']],
  5768. '/option',
  5769. '*/select',
  5770. ['select' => ['name' => 'Contact[date][minute]']],
  5771. $minutesRegex,
  5772. ['option' => ['selected' => 'selected', 'value' => '']],
  5773. '/option',
  5774. ['option' => ['value' => '00']],
  5775. '00',
  5776. '/option',
  5777. ['option' => ['value' => '05']],
  5778. '05',
  5779. '/option',
  5780. ['option' => ['value' => '10']],
  5781. '10',
  5782. '/option',
  5783. '*/select',
  5784. ];
  5785. $this->assertHtml($expected, $result);
  5786. }
  5787. /**
  5788. * testDateTimeRounding method
  5789. *
  5790. * Test dateTime with rounding.
  5791. *
  5792. * @return void
  5793. */
  5794. public function testDateTimeRounding()
  5795. {
  5796. $this->Form->request->data['Contact'] = [
  5797. 'date' => [
  5798. 'day' => '13',
  5799. 'month' => '12',
  5800. 'year' => '2010',
  5801. 'hour' => '04',
  5802. 'minute' => '19',
  5803. 'meridian' => 'AM'
  5804. ]
  5805. ];
  5806. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5807. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5808. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5809. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5810. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5811. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5812. }
  5813. /**
  5814. * testDatetimeWithDefault method
  5815. *
  5816. * Test that datetime() and default values work.
  5817. *
  5818. * @return void
  5819. */
  5820. public function testDatetimeWithDefault()
  5821. {
  5822. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5823. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5824. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5825. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5826. $result = $this->Form->dateTime('Contact.updated', [
  5827. 'default' => '2009-06-01 11:15:30'
  5828. ]);
  5829. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5830. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5831. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5832. }
  5833. /**
  5834. * testDateTimeAllZeros method
  5835. *
  5836. * Test datetime() with all zeros.
  5837. *
  5838. * @return void
  5839. */
  5840. public function testDateTimeAllZeros()
  5841. {
  5842. $result = $this->Form->dateTime('Contact.date', [
  5843. 'timeFormat' => false,
  5844. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5845. 'value' => '0000-00-00'
  5846. ]);
  5847. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5848. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5849. }
  5850. /**
  5851. * testDateTimeEmptyAsArray method
  5852. *
  5853. * @return void
  5854. */
  5855. public function testDateTimeEmptyAsArray()
  5856. {
  5857. $result = $this->Form->dateTime('Contact.date', [
  5858. 'empty' => [
  5859. 'day' => 'DAY',
  5860. 'month' => 'MONTH',
  5861. 'year' => 'YEAR',
  5862. 'hour' => 'HOUR',
  5863. 'minute' => 'MINUTE',
  5864. 'meridian' => false
  5865. ],
  5866. 'default' => true
  5867. ]);
  5868. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5869. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5870. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5871. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5872. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5873. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5874. $result = $this->Form->dateTime('Contact.date', [
  5875. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5876. 'default' => true
  5877. ]);
  5878. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5879. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5880. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5881. }
  5882. /**
  5883. * testFormDateTimeMulti method
  5884. *
  5885. * Test multiple datetime element generation.
  5886. *
  5887. * @return void
  5888. */
  5889. public function testFormDateTimeMulti()
  5890. {
  5891. extract($this->dateRegex);
  5892. $result = $this->Form->dateTime('Contact.1.updated');
  5893. $this->assertContains('Contact[1][updated][month]', $result);
  5894. $this->assertContains('Contact[1][updated][day]', $result);
  5895. $this->assertContains('Contact[1][updated][year]', $result);
  5896. $this->assertContains('Contact[1][updated][hour]', $result);
  5897. $this->assertContains('Contact[1][updated][minute]', $result);
  5898. }
  5899. /**
  5900. * testDateTimeLabelIdMatchesFirstControl method
  5901. *
  5902. * When changing the date format, the label should always focus the first select box when
  5903. * clicked.
  5904. *
  5905. * @return void
  5906. */
  5907. public function testDateTimeLabelIdMatchesFirstControl()
  5908. {
  5909. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5910. $this->assertContains('<label>Date</label>', $result);
  5911. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5912. $this->assertContains('<label>Date</label>', $result);
  5913. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5914. $this->assertContains('<label>Date</label>', $result);
  5915. }
  5916. /**
  5917. * testDateTimeSecondOptions method
  5918. *
  5919. * Test datetime second=true.
  5920. *
  5921. * @return void
  5922. */
  5923. public function testDateTimeSecondOptions()
  5924. {
  5925. $result = $this->Form->dateTime('updated', ['second' => true]);
  5926. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5927. $result = $this->Form->dateTime('updated', ['second' => []]);
  5928. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5929. $result = $this->Form->dateTime('updated', ['second' => null]);
  5930. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5931. $result = $this->Form->dateTime('updated', ['second' => false]);
  5932. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5933. }
  5934. /**
  5935. * testMonth method
  5936. *
  5937. * Test generation of a month input.
  5938. *
  5939. * @return void
  5940. */
  5941. public function testMonth()
  5942. {
  5943. $result = $this->Form->month('Model.field', ['value' => '']);
  5944. $expected = [
  5945. ['select' => ['name' => 'Model[field][month]']],
  5946. ['option' => ['value' => '', 'selected' => 'selected']],
  5947. '/option',
  5948. ['option' => ['value' => '01']],
  5949. date('F', strtotime('2008-01-01 00:00:00')),
  5950. '/option',
  5951. ['option' => ['value' => '02']],
  5952. date('F', strtotime('2008-02-01 00:00:00')),
  5953. '/option',
  5954. '*/select',
  5955. ];
  5956. $this->assertHtml($expected, $result);
  5957. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5958. $expected = [
  5959. ['select' => ['name' => 'Model[field][month]']],
  5960. ['option' => ['selected' => 'selected', 'value' => '']],
  5961. '/option',
  5962. ['option' => ['value' => '01']],
  5963. date('F', strtotime('2008-01-01 00:00:00')),
  5964. '/option',
  5965. ['option' => ['value' => '02']],
  5966. date('F', strtotime('2008-02-01 00:00:00')),
  5967. '/option',
  5968. '*/select',
  5969. ];
  5970. $this->assertHtml($expected, $result);
  5971. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5972. $expected = [
  5973. ['select' => ['name' => 'Model[field][month]']],
  5974. ['option' => ['selected' => 'selected', 'value' => '']],
  5975. '/option',
  5976. ['option' => ['value' => '01']],
  5977. '1',
  5978. '/option',
  5979. ['option' => ['value' => '02']],
  5980. '2',
  5981. '/option',
  5982. '*/select',
  5983. ];
  5984. $this->assertHtml($expected, $result);
  5985. $monthNames = [
  5986. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5987. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5988. ];
  5989. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5990. $expected = [
  5991. ['select' => ['name' => 'Model[field][month]']],
  5992. ['option' => ['value' => '']],
  5993. '/option',
  5994. ['option' => ['value' => '01', 'selected' => 'selected']],
  5995. 'Jan',
  5996. '/option',
  5997. ['option' => ['value' => '02']],
  5998. 'Feb',
  5999. '/option',
  6000. '*/select',
  6001. ];
  6002. $this->assertHtml($expected, $result);
  6003. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6004. $result = $this->Form->month('Project.release');
  6005. $expected = [
  6006. ['select' => ['name' => 'Project[release][month]']],
  6007. ['option' => ['value' => '']],
  6008. '/option',
  6009. ['option' => ['value' => '01']],
  6010. 'January',
  6011. '/option',
  6012. ['option' => ['value' => '02', 'selected' => 'selected']],
  6013. 'February',
  6014. '/option',
  6015. '*/select',
  6016. ];
  6017. $this->assertHtml($expected, $result);
  6018. $result = $this->Form->month('Contact.published', [
  6019. 'empty' => 'Published on',
  6020. ]);
  6021. $this->assertContains('Published on', $result);
  6022. }
  6023. /**
  6024. * testDay method
  6025. *
  6026. * Test generation of a day input.
  6027. *
  6028. * @return void
  6029. */
  6030. public function testDay()
  6031. {
  6032. extract($this->dateRegex);
  6033. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6034. $expected = [
  6035. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6036. ['option' => ['selected' => 'selected', 'value' => '']],
  6037. '/option',
  6038. ['option' => ['value' => '01']],
  6039. '1',
  6040. '/option',
  6041. ['option' => ['value' => '02']],
  6042. '2',
  6043. '/option',
  6044. $daysRegex,
  6045. '/select',
  6046. ];
  6047. $this->assertHtml($expected, $result);
  6048. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6049. $result = $this->Form->day('Model.field');
  6050. $expected = [
  6051. ['select' => ['name' => 'Model[field][day]']],
  6052. ['option' => ['value' => '']],
  6053. '/option',
  6054. ['option' => ['value' => '01']],
  6055. '1',
  6056. '/option',
  6057. ['option' => ['value' => '02']],
  6058. '2',
  6059. '/option',
  6060. $daysRegex,
  6061. ['option' => ['value' => '10', 'selected' => 'selected']],
  6062. '10',
  6063. '/option',
  6064. $daysRegex,
  6065. '/select',
  6066. ];
  6067. $this->assertHtml($expected, $result);
  6068. $this->Form->request->data['Model']['field'] = '';
  6069. $result = $this->Form->day('Model.field', ['value' => '10']);
  6070. $expected = [
  6071. ['select' => ['name' => 'Model[field][day]']],
  6072. ['option' => ['value' => '']],
  6073. '/option',
  6074. ['option' => ['value' => '01']],
  6075. '1',
  6076. '/option',
  6077. ['option' => ['value' => '02']],
  6078. '2',
  6079. '/option',
  6080. $daysRegex,
  6081. ['option' => ['value' => '10', 'selected' => 'selected']],
  6082. '10',
  6083. '/option',
  6084. $daysRegex,
  6085. '/select',
  6086. ];
  6087. $this->assertHtml($expected, $result);
  6088. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6089. $result = $this->Form->day('Project.release');
  6090. $expected = [
  6091. ['select' => ['name' => 'Project[release][day]']],
  6092. ['option' => ['value' => '']],
  6093. '/option',
  6094. ['option' => ['value' => '01']],
  6095. '1',
  6096. '/option',
  6097. ['option' => ['value' => '02']],
  6098. '2',
  6099. '/option',
  6100. $daysRegex,
  6101. ['option' => ['value' => '10', 'selected' => 'selected']],
  6102. '10',
  6103. '/option',
  6104. $daysRegex,
  6105. '/select',
  6106. ];
  6107. $this->assertHtml($expected, $result);
  6108. $result = $this->Form->day('Contact.published', [
  6109. 'empty' => 'Published on',
  6110. ]);
  6111. $this->assertContains('Published on', $result);
  6112. }
  6113. /**
  6114. * testMinute method
  6115. *
  6116. * Test generation of a minute input.
  6117. *
  6118. * @return void
  6119. */
  6120. public function testMinute()
  6121. {
  6122. extract($this->dateRegex);
  6123. $result = $this->Form->minute('Model.field', ['value' => '']);
  6124. $expected = [
  6125. ['select' => ['name' => 'Model[field][minute]']],
  6126. ['option' => ['selected' => 'selected', 'value' => '']],
  6127. '/option',
  6128. ['option' => ['value' => '00']],
  6129. '00',
  6130. '/option',
  6131. ['option' => ['value' => '01']],
  6132. '01',
  6133. '/option',
  6134. ['option' => ['value' => '02']],
  6135. '02',
  6136. '/option',
  6137. $minutesRegex,
  6138. '/select',
  6139. ];
  6140. $this->assertHtml($expected, $result);
  6141. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6142. $result = $this->Form->minute('Model.field');
  6143. $expected = [
  6144. ['select' => ['name' => 'Model[field][minute]']],
  6145. ['option' => ['value' => '']],
  6146. '/option',
  6147. ['option' => ['value' => '00']],
  6148. '00',
  6149. '/option',
  6150. ['option' => ['value' => '01']],
  6151. '01',
  6152. '/option',
  6153. ['option' => ['value' => '02']],
  6154. '02',
  6155. '/option',
  6156. $minutesRegex,
  6157. ['option' => ['value' => '12', 'selected' => 'selected']],
  6158. '12',
  6159. '/option',
  6160. $minutesRegex,
  6161. '/select',
  6162. ];
  6163. $this->assertHtml($expected, $result);
  6164. $this->Form->request->data['Model']['field'] = '';
  6165. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6166. $expected = [
  6167. ['select' => ['name' => 'Model[field][minute]']],
  6168. ['option' => ['selected' => 'selected', 'value' => '']],
  6169. '/option',
  6170. ['option' => ['value' => '00']],
  6171. '00',
  6172. '/option',
  6173. ['option' => ['value' => '05']],
  6174. '05',
  6175. '/option',
  6176. ['option' => ['value' => '10']],
  6177. '10',
  6178. '/option',
  6179. $minutesRegex,
  6180. '/select',
  6181. ];
  6182. $this->assertHtml($expected, $result);
  6183. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6184. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6185. $expected = [
  6186. ['select' => ['name' => 'Model[field][minute]']],
  6187. ['option' => ['value' => '']],
  6188. '/option',
  6189. ['option' => ['value' => '00']],
  6190. '00',
  6191. '/option',
  6192. ['option' => ['value' => '05']],
  6193. '05',
  6194. '/option',
  6195. ['option' => ['value' => '10', 'selected' => 'selected']],
  6196. '10',
  6197. '/option',
  6198. $minutesRegex,
  6199. '/select',
  6200. ];
  6201. $this->assertHtml($expected, $result);
  6202. }
  6203. /**
  6204. * testMeridian method
  6205. *
  6206. * Test generating an input for the meridian.
  6207. *
  6208. * @return void
  6209. */
  6210. public function testMeridian()
  6211. {
  6212. extract($this->dateRegex);
  6213. $now = new \DateTime();
  6214. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6215. $expected = [
  6216. ['select' => ['name' => 'Model[field][meridian]']],
  6217. ['option' => ['value' => '']],
  6218. '/option',
  6219. $meridianRegex,
  6220. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6221. $now->format('a'),
  6222. '/option',
  6223. '*/select'
  6224. ];
  6225. $this->assertHtml($expected, $result);
  6226. }
  6227. /**
  6228. * testHour method
  6229. *
  6230. * Test generation of an hour input.
  6231. *
  6232. * @return void
  6233. */
  6234. public function testHour()
  6235. {
  6236. extract($this->dateRegex);
  6237. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6238. $expected = [
  6239. ['select' => ['name' => 'Model[field][hour]']],
  6240. ['option' => ['selected' => 'selected', 'value' => '']],
  6241. '/option',
  6242. ['option' => ['value' => '01']],
  6243. '1',
  6244. '/option',
  6245. ['option' => ['value' => '02']],
  6246. '2',
  6247. '/option',
  6248. $hoursRegex,
  6249. '/select',
  6250. ];
  6251. $this->assertHtml($expected, $result);
  6252. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6253. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6254. $expected = [
  6255. ['select' => ['name' => 'Model[field][hour]']],
  6256. ['option' => ['value' => '']],
  6257. '/option',
  6258. ['option' => ['value' => '01']],
  6259. '1',
  6260. '/option',
  6261. ['option' => ['value' => '02']],
  6262. '2',
  6263. '/option',
  6264. $hoursRegex,
  6265. ['option' => ['value' => '12', 'selected' => 'selected']],
  6266. '12',
  6267. '/option',
  6268. '/select',
  6269. ];
  6270. $this->assertHtml($expected, $result);
  6271. $this->Form->request->data['Model']['field'] = '';
  6272. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6273. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6274. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6275. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6276. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6277. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6278. $expected = [
  6279. ['select' => ['name' => 'Model[field][hour]']],
  6280. ['option' => ['value' => '']],
  6281. '/option',
  6282. ['option' => ['value' => '00', 'selected' => 'selected']],
  6283. '0',
  6284. '/option',
  6285. ['option' => ['value' => '01']],
  6286. '1',
  6287. '/option',
  6288. ['option' => ['value' => '02']],
  6289. '2',
  6290. '/option',
  6291. $hoursRegex,
  6292. '/select',
  6293. ];
  6294. $this->assertHtml($expected, $result);
  6295. unset($this->Form->request->data['Model']['field']);
  6296. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6297. $thisHour = date('H');
  6298. $optValue = date('G');
  6299. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6300. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6301. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6302. $expected = [
  6303. ['select' => ['name' => 'Model[field][hour]']],
  6304. ['option' => ['value' => '']],
  6305. '/option',
  6306. ['option' => ['value' => '00']],
  6307. '0',
  6308. '/option',
  6309. ['option' => ['value' => '01', 'selected' => 'selected']],
  6310. '1',
  6311. '/option',
  6312. ['option' => ['value' => '02']],
  6313. '2',
  6314. '/option',
  6315. $hoursRegex,
  6316. '/select',
  6317. ];
  6318. $this->assertHtml($expected, $result);
  6319. }
  6320. /**
  6321. * testYear method
  6322. *
  6323. * Test generation of a year input.
  6324. *
  6325. * @return void
  6326. */
  6327. public function testYear()
  6328. {
  6329. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6330. $expected = [
  6331. ['select' => ['name' => 'Model[field][year]']],
  6332. ['option' => ['selected' => 'selected', 'value' => '']],
  6333. '/option',
  6334. ['option' => ['value' => '2007']],
  6335. '2007',
  6336. '/option',
  6337. ['option' => ['value' => '2006']],
  6338. '2006',
  6339. '/option',
  6340. '/select',
  6341. ];
  6342. $this->assertHtml($expected, $result);
  6343. $result = $this->Form->year('Model.field', [
  6344. 'value' => '',
  6345. 'minYear' => 2006,
  6346. 'maxYear' => 2007,
  6347. 'orderYear' => 'asc'
  6348. ]);
  6349. $expected = [
  6350. ['select' => ['name' => 'Model[field][year]']],
  6351. ['option' => ['selected' => 'selected', 'value' => '']],
  6352. '/option',
  6353. ['option' => ['value' => '2006']],
  6354. '2006',
  6355. '/option',
  6356. ['option' => ['value' => '2007']],
  6357. '2007',
  6358. '/option',
  6359. '/select',
  6360. ];
  6361. $this->assertHtml($expected, $result);
  6362. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6363. $result = $this->Form->year('Contact.published', [
  6364. 'empty' => false,
  6365. 'minYear' => 2006,
  6366. 'maxYear' => 2007,
  6367. ]);
  6368. $expected = [
  6369. ['select' => ['name' => 'Contact[published][year]']],
  6370. ['option' => ['value' => '2007']],
  6371. '2007',
  6372. '/option',
  6373. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6374. '2006',
  6375. '/option',
  6376. '/select',
  6377. ];
  6378. $this->assertHtml($expected, $result);
  6379. $result = $this->Form->year('Contact.published', [
  6380. 'empty' => 'Published on',
  6381. ]);
  6382. $this->assertContains('Published on', $result);
  6383. }
  6384. /**
  6385. * testControlDatetimePreEpoch method
  6386. *
  6387. * Test minYear being prior to the unix epoch.
  6388. *
  6389. * @return void
  6390. */
  6391. public function testControlDatetimePreEpoch()
  6392. {
  6393. $start = date('Y') - 80;
  6394. $end = date('Y') - 18;
  6395. $result = $this->Form->control('birth_year', [
  6396. 'type' => 'date',
  6397. 'label' => 'Birth Year',
  6398. 'minYear' => $start,
  6399. 'maxYear' => $end,
  6400. 'month' => false,
  6401. 'day' => false,
  6402. ]);
  6403. $this->assertContains('value="' . $start . '">' . $start, $result);
  6404. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6405. $this->assertNotContains('value="00">00', $result);
  6406. }
  6407. /**
  6408. * testYearAutoExpandRange method
  6409. *
  6410. * @return void
  6411. */
  6412. public function testYearAutoExpandRange()
  6413. {
  6414. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6415. $result = $this->Form->year('User.birthday');
  6416. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6417. $result = $matches[1];
  6418. $expected = range(date('Y') + 5, 1930);
  6419. $this->assertEquals($expected, $result);
  6420. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6421. $result = $this->Form->year('Project.release');
  6422. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6423. $result = $matches[1];
  6424. $expected = range(2050, date('Y') - 5);
  6425. $this->assertEquals($expected, $result);
  6426. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6427. $result = $this->Form->year('Project.release', [
  6428. 'minYear' => 1890,
  6429. 'maxYear' => 1900
  6430. ]);
  6431. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6432. $result = $matches[1];
  6433. $expected = range(1900, 1881);
  6434. $this->assertEquals($expected, $result);
  6435. }
  6436. /**
  6437. * testControlDate method
  6438. *
  6439. * Test that control() accepts the type of date and passes options in.
  6440. *
  6441. * @return void
  6442. */
  6443. public function testControlDate()
  6444. {
  6445. $this->Form->request->data = [
  6446. 'month_year' => ['month' => date('m')],
  6447. ];
  6448. $this->Form->create($this->article);
  6449. $result = $this->Form->control('month_year', [
  6450. 'label' => false,
  6451. 'type' => 'date',
  6452. 'minYear' => 2006,
  6453. 'maxYear' => 2008
  6454. ]);
  6455. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6456. $this->assertNotContains('value="2008" selected="selected"', $result);
  6457. }
  6458. /**
  6459. * testControlLabelFalse method
  6460. *
  6461. * Test the label option being set to false.
  6462. *
  6463. * @return void
  6464. */
  6465. public function testControlLabelFalse()
  6466. {
  6467. $this->Form->create($this->article);
  6468. $result = $this->Form->control('title', ['label' => false]);
  6469. $expected = [
  6470. 'div' => ['class' => 'input text required'],
  6471. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6472. '/div'
  6473. ];
  6474. $this->assertHtml($expected, $result);
  6475. }
  6476. /**
  6477. * testControlDateMaxYear method
  6478. *
  6479. * Let's say we want to only allow users born from 2006 to 2008 to register
  6480. * This being the first signup page, we still don't have any data.
  6481. *
  6482. * @return void
  6483. */
  6484. public function testControlDateMaxYear()
  6485. {
  6486. $this->Form->request->data = [];
  6487. $this->Form->create($this->article);
  6488. $result = $this->Form->control('birthday', [
  6489. 'label' => false,
  6490. 'type' => 'date',
  6491. 'minYear' => 2006,
  6492. 'maxYear' => 2008,
  6493. 'default' => true
  6494. ]);
  6495. $this->assertContains('value="2008" selected="selected"', $result);
  6496. $this->assertContains('value="2006"', $result);
  6497. $this->assertNotContains('value="2005"', $result);
  6498. $this->assertNotContains('value="2009"', $result);
  6499. }
  6500. /**
  6501. * testTextArea method
  6502. *
  6503. * Test generation of a textarea input.
  6504. *
  6505. * @return void
  6506. */
  6507. public function testTextArea()
  6508. {
  6509. $this->Form->request->data = ['field' => 'some test data'];
  6510. $result = $this->Form->textarea('field');
  6511. $expected = [
  6512. 'textarea' => ['name' => 'field', 'rows' => 5],
  6513. 'some test data',
  6514. '/textarea',
  6515. ];
  6516. $this->assertHtml($expected, $result);
  6517. $result = $this->Form->textarea('user.bio');
  6518. $expected = [
  6519. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6520. '/textarea',
  6521. ];
  6522. $this->assertHtml($expected, $result);
  6523. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6524. $result = $this->Form->textarea('field');
  6525. $expected = [
  6526. 'textarea' => ['name' => 'field', 'rows' => 5],
  6527. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6528. '/textarea',
  6529. ];
  6530. $this->assertHtml($expected, $result);
  6531. $this->Form->request->data = [
  6532. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6533. ];
  6534. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6535. $expected = [
  6536. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6537. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6538. '/textarea',
  6539. ];
  6540. $this->assertHtml($expected, $result);
  6541. $result = $this->Form->textarea('0.OtherModel.field');
  6542. $expected = [
  6543. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6544. '/textarea'
  6545. ];
  6546. $this->assertHtml($expected, $result);
  6547. }
  6548. /**
  6549. * testTextAreaWithStupidCharacters method
  6550. *
  6551. * Test text area with non-ascii characters.
  6552. *
  6553. * @return void
  6554. */
  6555. public function testTextAreaWithStupidCharacters()
  6556. {
  6557. $result = $this->Form->textarea('Post.content', [
  6558. 'value' => 'GREAT®',
  6559. 'rows' => '15',
  6560. 'cols' => '75'
  6561. ]);
  6562. $expected = [
  6563. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6564. 'GREAT®',
  6565. '/textarea',
  6566. ];
  6567. $this->assertHtml($expected, $result);
  6568. }
  6569. /**
  6570. * testTextAreaMaxLength method
  6571. *
  6572. * Test textareas maxlength read from schema.
  6573. *
  6574. * @return void
  6575. */
  6576. public function testTextAreaMaxLength()
  6577. {
  6578. $this->Form->create([
  6579. 'schema' => [
  6580. 'stuff' => ['type' => 'string', 'length' => 10],
  6581. ]
  6582. ]);
  6583. $result = $this->Form->control('other', ['type' => 'textarea']);
  6584. $expected = [
  6585. 'div' => ['class' => 'input textarea'],
  6586. 'label' => ['for' => 'other'],
  6587. 'Other',
  6588. '/label',
  6589. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6590. '/textarea',
  6591. '/div'
  6592. ];
  6593. $this->assertHtml($expected, $result);
  6594. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6595. $expected = [
  6596. 'div' => ['class' => 'input textarea'],
  6597. 'label' => ['for' => 'stuff'],
  6598. 'Stuff',
  6599. '/label',
  6600. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6601. '/textarea',
  6602. '/div'
  6603. ];
  6604. $this->assertHtml($expected, $result);
  6605. }
  6606. /**
  6607. * testHiddenField method
  6608. *
  6609. * Test generation of a hidden input.
  6610. *
  6611. * @return void
  6612. */
  6613. public function testHiddenField()
  6614. {
  6615. $this->article['errors'] = [
  6616. 'field' => true
  6617. ];
  6618. $this->Form->request->data['field'] = 'test';
  6619. $this->Form->create($this->article);
  6620. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6621. $expected = [
  6622. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6623. $this->assertHtml($expected, $result);
  6624. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6625. $expected = [
  6626. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6627. ];
  6628. $this->assertHtml($expected, $result);
  6629. }
  6630. /**
  6631. * testFileUploadField method
  6632. *
  6633. * Test generation of a file upload input.
  6634. *
  6635. * @return void
  6636. */
  6637. public function testFileUploadField()
  6638. {
  6639. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6640. $result = $this->Form->file('Model.upload');
  6641. $this->assertHtml($expected, $result);
  6642. $this->Form->request->data['Model']['upload'] = [
  6643. 'name' => '', 'type' => '', 'tmp_name' => '',
  6644. 'error' => 4, 'size' => 0
  6645. ];
  6646. $result = $this->Form->file('Model.upload');
  6647. $this->assertHtml($expected, $result);
  6648. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6649. $result = $this->Form->file('Model.upload');
  6650. $this->assertHtml($expected, $result);
  6651. }
  6652. /**
  6653. * testFileUploadOnOtherModel method
  6654. *
  6655. * Test File upload input on a model not used in create().
  6656. *
  6657. * @return void
  6658. */
  6659. public function testFileUploadOnOtherModel()
  6660. {
  6661. $this->Form->create($this->article, ['type' => 'file']);
  6662. $result = $this->Form->file('ValidateProfile.city');
  6663. $expected = [
  6664. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6665. ];
  6666. $this->assertHtml($expected, $result);
  6667. }
  6668. /**
  6669. * testButton method
  6670. *
  6671. * Test generation of a form button.
  6672. *
  6673. * @return void
  6674. */
  6675. public function testButton()
  6676. {
  6677. $result = $this->Form->button('Hi');
  6678. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6679. $this->assertHtml($expected, $result);
  6680. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6681. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6682. $this->assertHtml($expected, $result);
  6683. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6684. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6685. $this->assertHtml($expected, $result);
  6686. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6687. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6688. $this->assertHtml($expected, $result);
  6689. $result = $this->Form->button('No type', ['type' => false]);
  6690. $expected = ['button' => [], 'No type', '/button'];
  6691. $this->assertHtml($expected, $result);
  6692. $result = $this->Form->button('Upload Text', [
  6693. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6694. 'escape' => false
  6695. ]);
  6696. $this->assertNotRegExp('/\&039/', $result);
  6697. }
  6698. /**
  6699. * testButtonUnlockedByDefault method
  6700. *
  6701. * Test that button() makes unlocked fields by default.
  6702. *
  6703. * @return void
  6704. */
  6705. public function testButtonUnlockedByDefault()
  6706. {
  6707. $this->Form->request->params['_csrfToken'] = 'secured';
  6708. $this->Form->button('Save', ['name' => 'save']);
  6709. $this->Form->button('Clear');
  6710. $result = $this->Form->unlockField();
  6711. $this->assertEquals(['save'], $result);
  6712. }
  6713. /**
  6714. * testPostButton method
  6715. *
  6716. * @return void
  6717. */
  6718. public function testPostButton()
  6719. {
  6720. $result = $this->Form->postButton('Hi', '/controller/action');
  6721. $expected = [
  6722. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6723. 'div' => ['style' => 'display:none;'],
  6724. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6725. '/div',
  6726. 'button' => ['type' => 'submit'],
  6727. 'Hi',
  6728. '/button',
  6729. '/form'
  6730. ];
  6731. $this->assertHtml($expected, $result);
  6732. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6733. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6734. }
  6735. /**
  6736. * testPostButtonMethodType method
  6737. *
  6738. * @return void
  6739. */
  6740. public function testPostButtonMethodType()
  6741. {
  6742. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6743. $expected = [
  6744. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6745. 'div' => ['style' => 'display:none;'],
  6746. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6747. '/div',
  6748. 'button' => ['type' => 'submit'],
  6749. 'Hi',
  6750. '/button',
  6751. '/form'
  6752. ];
  6753. $this->assertHtml($expected, $result);
  6754. }
  6755. /**
  6756. * testPostButtonFormOptions method
  6757. *
  6758. * @return void
  6759. */
  6760. public function testPostButtonFormOptions()
  6761. {
  6762. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6763. $expected = [
  6764. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6765. 'div' => ['style' => 'display:none;'],
  6766. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6767. '/div',
  6768. 'button' => ['type' => 'submit'],
  6769. 'Hi',
  6770. '/button',
  6771. '/form'
  6772. ];
  6773. $this->assertHtml($expected, $result);
  6774. }
  6775. /**
  6776. * testPostButtonNestedData method
  6777. *
  6778. * Test using postButton with N dimensional data.
  6779. *
  6780. * @return void
  6781. */
  6782. public function testPostButtonNestedData()
  6783. {
  6784. $data = [
  6785. 'one' => [
  6786. 'two' => [
  6787. 3, 4, 5
  6788. ]
  6789. ]
  6790. ];
  6791. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6792. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6793. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6794. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6795. }
  6796. /**
  6797. * testSecurePostButton method
  6798. *
  6799. * Test that postButton adds _Token fields.
  6800. *
  6801. * @return void
  6802. */
  6803. public function testSecurePostButton()
  6804. {
  6805. $this->Form->request->params['_csrfToken'] = 'testkey';
  6806. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6807. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6808. $tokenDebug = urlencode(json_encode([
  6809. '/posts/delete/1',
  6810. [],
  6811. []
  6812. ]));
  6813. $expected = [
  6814. 'form' => [
  6815. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6816. ],
  6817. ['div' => ['style' => 'display:none;']],
  6818. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6819. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6820. '/div',
  6821. 'button' => ['type' => 'submit'],
  6822. 'Delete',
  6823. '/button',
  6824. ['div' => ['style' => 'display:none;']],
  6825. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6826. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6827. ['input' => [
  6828. 'type' => 'hidden',
  6829. 'name' => '_Token[debug]',
  6830. 'value' => $tokenDebug,
  6831. 'autocomplete' => 'off',
  6832. ]],
  6833. '/div',
  6834. '/form',
  6835. ];
  6836. $this->assertHtml($expected, $result);
  6837. }
  6838. /**
  6839. * testPostLink method
  6840. *
  6841. * @return void
  6842. */
  6843. public function testPostLink()
  6844. {
  6845. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6846. $expected = [
  6847. 'form' => [
  6848. 'method' => 'post', 'action' => '/posts/delete/1',
  6849. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6850. ],
  6851. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6852. '/form',
  6853. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6854. 'Delete',
  6855. '/a'
  6856. ];
  6857. $this->assertHtml($expected, $result);
  6858. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6859. $expected = [
  6860. 'form' => [
  6861. 'method' => 'post', 'action' => '/posts/delete/1',
  6862. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6863. ],
  6864. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6865. '/form',
  6866. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6867. 'Delete',
  6868. '/a'
  6869. ];
  6870. $this->assertHtml($expected, $result);
  6871. $result = $this->Form->postLink(
  6872. 'Delete',
  6873. '/posts/delete/1',
  6874. ['target' => '_blank', 'class' => 'btn btn-danger']
  6875. );
  6876. $expected = [
  6877. 'form' => [
  6878. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6879. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6880. ],
  6881. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6882. '/form',
  6883. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6884. 'Delete',
  6885. '/a'
  6886. ];
  6887. $this->assertHtml($expected, $result);
  6888. }
  6889. /**
  6890. * testPostLinkWithConfirm method
  6891. *
  6892. * Test the confirm option for postLink().
  6893. *
  6894. * @return void
  6895. */
  6896. public function testPostLinkWithConfirm()
  6897. {
  6898. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6899. $expected = [
  6900. 'form' => [
  6901. 'method' => 'post', 'action' => '/posts/delete/1',
  6902. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6903. ],
  6904. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6905. '/form',
  6906. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6907. 'Delete',
  6908. '/a'
  6909. ];
  6910. $this->assertHtml($expected, $result);
  6911. $result = $this->Form->postLink(
  6912. 'Delete',
  6913. '/posts/delete/1',
  6914. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6915. );
  6916. $expected = [
  6917. 'form' => [
  6918. 'method' => 'post', 'action' => '/posts/delete/1',
  6919. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6920. ],
  6921. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6922. '/form',
  6923. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6924. 'Delete',
  6925. '/a'
  6926. ];
  6927. $this->assertHtml($expected, $result);
  6928. }
  6929. /**
  6930. * testPostLinkWithQuery method
  6931. *
  6932. * Test postLink() with query string args.
  6933. *
  6934. * @return void
  6935. */
  6936. public function testPostLinkWithQuery()
  6937. {
  6938. $result = $this->Form->postLink(
  6939. 'Delete',
  6940. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6941. );
  6942. $expected = [
  6943. 'form' => [
  6944. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6945. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6946. ],
  6947. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6948. '/form',
  6949. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6950. 'Delete',
  6951. '/a'
  6952. ];
  6953. $this->assertHtml($expected, $result);
  6954. }
  6955. /**
  6956. * testPostLinkWithData method
  6957. *
  6958. * Test postLink with additional data.
  6959. *
  6960. * @return void
  6961. */
  6962. public function testPostLinkWithData()
  6963. {
  6964. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6965. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6966. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6967. $this->Form->create($entity);
  6968. $this->Form->end();
  6969. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6970. $this->assertContains(
  6971. '<input type="hidden" name="name" value="show"',
  6972. $result,
  6973. 'should not contain entity data.'
  6974. );
  6975. }
  6976. /**
  6977. * testPostLinkSecurityHash method
  6978. *
  6979. * Test that security hashes for postLink include the url.
  6980. *
  6981. * @return void
  6982. */
  6983. public function testPostLinkSecurityHash()
  6984. {
  6985. $hash = Security::hash(
  6986. '/posts/delete/1' .
  6987. serialize(['id' => '1']) .
  6988. '' .
  6989. Security::salt()
  6990. );
  6991. $hash .= '%3Aid';
  6992. $this->Form->request->params['_Token']['key'] = 'test';
  6993. $result = $this->Form->postLink(
  6994. 'Delete',
  6995. '/posts/delete/1',
  6996. ['data' => ['id' => 1]]
  6997. );
  6998. $tokenDebug = urlencode(json_encode([
  6999. '/posts/delete/1',
  7000. [
  7001. 'id' => 1
  7002. ],
  7003. []
  7004. ]));
  7005. $expected = [
  7006. 'form' => [
  7007. 'method' => 'post', 'action' => '/posts/delete/1',
  7008. 'name', 'style' => 'display:none;'
  7009. ],
  7010. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7011. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7012. 'div' => ['style' => 'display:none;'],
  7013. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7014. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7015. ['input' => [
  7016. 'type' => 'hidden',
  7017. 'name' => '_Token[debug]',
  7018. 'value' => $tokenDebug,
  7019. 'autocomplete' => 'off',
  7020. ]],
  7021. '/div',
  7022. '/form',
  7023. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7024. 'Delete',
  7025. '/a'
  7026. ];
  7027. $this->assertHtml($expected, $result);
  7028. }
  7029. /**
  7030. * testPostLinkSecurityHashBlockMode method
  7031. *
  7032. * Test that postLink doesn't modify the fields in the containing form.
  7033. *
  7034. * postLink() calls inside open forms should not modify the field list
  7035. * for the form.
  7036. *
  7037. * @return void
  7038. */
  7039. public function testPostLinkSecurityHashBlockMode()
  7040. {
  7041. $hash = Security::hash(
  7042. '/posts/delete/1' .
  7043. serialize([]) .
  7044. '' .
  7045. Security::salt()
  7046. );
  7047. $hash .= '%3A';
  7048. $this->Form->request->params['_Token']['key'] = 'test';
  7049. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7050. $this->Form->control('title');
  7051. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7052. $result = $this->View->fetch('postLink');
  7053. $this->assertEquals(['title'], $this->Form->fields);
  7054. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7055. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7056. }
  7057. /**
  7058. * testPostLinkSecurityHashNoDebugMode method
  7059. *
  7060. * Test that security does not include debug token if debug is false.
  7061. *
  7062. * @return void
  7063. */
  7064. public function testPostLinkSecurityHashNoDebugMode()
  7065. {
  7066. Configure::write('debug', false);
  7067. $hash = Security::hash(
  7068. '/posts/delete/1' .
  7069. serialize(['id' => '1']) .
  7070. '' .
  7071. Security::salt()
  7072. );
  7073. $hash .= '%3Aid';
  7074. $this->Form->request->params['_Token']['key'] = 'test';
  7075. $result = $this->Form->postLink(
  7076. 'Delete',
  7077. '/posts/delete/1',
  7078. ['data' => ['id' => 1]]
  7079. );
  7080. $expected = [
  7081. 'form' => [
  7082. 'method' => 'post', 'action' => '/posts/delete/1',
  7083. 'name', 'style' => 'display:none;'
  7084. ],
  7085. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7086. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7087. 'div' => ['style' => 'display:none;'],
  7088. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7089. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7090. '/div',
  7091. '/form',
  7092. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7093. 'Delete',
  7094. '/a'
  7095. ];
  7096. $this->assertHtml($expected, $result);
  7097. }
  7098. /**
  7099. * testPostLinkNestedData method
  7100. *
  7101. * Test using postLink with N dimensional data.
  7102. *
  7103. * @return void
  7104. */
  7105. public function testPostLinkNestedData()
  7106. {
  7107. $data = [
  7108. 'one' => [
  7109. 'two' => [
  7110. 3, 4, 5
  7111. ]
  7112. ]
  7113. ];
  7114. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7115. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7116. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7117. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7118. }
  7119. /**
  7120. * testPostLinkAfterGetForm method
  7121. *
  7122. * Test creating postLinks after a GET form.
  7123. *
  7124. * @return void
  7125. */
  7126. public function testPostLinkAfterGetForm()
  7127. {
  7128. $this->Form->request->params['_csrfToken'] = 'testkey';
  7129. $this->Form->request->params['_Token'] = 'val';
  7130. $this->Form->create($this->article, ['type' => 'get']);
  7131. $this->Form->end();
  7132. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7133. $tokenDebug = urlencode(json_encode([
  7134. '/posts/delete/1',
  7135. [],
  7136. []
  7137. ]));
  7138. $expected = [
  7139. 'form' => [
  7140. 'method' => 'post', 'action' => '/posts/delete/1',
  7141. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7142. ],
  7143. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7144. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7145. 'div' => ['style' => 'display:none;'],
  7146. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7147. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7148. ['input' => [
  7149. 'type' => 'hidden', 'name' => '_Token[debug]',
  7150. 'value' => $tokenDebug,
  7151. 'autocomplete' => 'off'
  7152. ]],
  7153. '/div',
  7154. '/form',
  7155. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7156. 'Delete',
  7157. '/a'
  7158. ];
  7159. $this->assertHtml($expected, $result);
  7160. }
  7161. /**
  7162. * testPostLinkFormBuffer method
  7163. *
  7164. * Test that postLink adds form tags to view block.
  7165. *
  7166. * @return void
  7167. */
  7168. public function testPostLinkFormBuffer()
  7169. {
  7170. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7171. $expected = [
  7172. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7173. 'Delete',
  7174. '/a'
  7175. ];
  7176. $this->assertHtml($expected, $result);
  7177. $result = $this->View->fetch('postLink');
  7178. $expected = [
  7179. 'form' => [
  7180. 'method' => 'post', 'action' => '/posts/delete/1',
  7181. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7182. ],
  7183. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7184. '/form'
  7185. ];
  7186. $this->assertHtml($expected, $result);
  7187. $result = $this->Form->postLink(
  7188. 'Delete',
  7189. '/posts/delete/2',
  7190. ['block' => true, 'method' => 'DELETE']
  7191. );
  7192. $expected = [
  7193. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7194. 'Delete',
  7195. '/a'
  7196. ];
  7197. $this->assertHtml($expected, $result);
  7198. $result = $this->View->fetch('postLink');
  7199. $expected = [
  7200. 'form' => [
  7201. 'method' => 'post', 'action' => '/posts/delete/1',
  7202. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7203. ],
  7204. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7205. '/form',
  7206. [
  7207. 'form' => [
  7208. 'method' => 'post', 'action' => '/posts/delete/2',
  7209. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7210. ],
  7211. ],
  7212. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7213. '/form'
  7214. ];
  7215. $this->assertHtml($expected, $result);
  7216. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7217. $expected = [
  7218. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7219. 'Delete',
  7220. '/a'
  7221. ];
  7222. $this->assertHtml($expected, $result);
  7223. $result = $this->View->fetch('foobar');
  7224. $expected = [
  7225. 'form' => [
  7226. 'method' => 'post', 'action' => '/posts/delete/1',
  7227. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7228. ],
  7229. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7230. '/form'
  7231. ];
  7232. $this->assertHtml($expected, $result);
  7233. }
  7234. /**
  7235. * testSubmitButton method
  7236. *
  7237. * @return void
  7238. */
  7239. public function testSubmitButton()
  7240. {
  7241. $result = $this->Form->submit('');
  7242. $expected = [
  7243. 'div' => ['class' => 'submit'],
  7244. 'input' => ['type' => 'submit', 'value' => ''],
  7245. '/div'
  7246. ];
  7247. $this->assertHtml($expected, $result);
  7248. $result = $this->Form->submit('Test Submit');
  7249. $expected = [
  7250. 'div' => ['class' => 'submit'],
  7251. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7252. '/div'
  7253. ];
  7254. $this->assertHtml($expected, $result);
  7255. $result = $this->Form->submit('Next >');
  7256. $expected = [
  7257. 'div' => ['class' => 'submit'],
  7258. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7259. '/div'
  7260. ];
  7261. $this->assertHtml($expected, $result);
  7262. $result = $this->Form->submit('Next >', ['escape' => false]);
  7263. $expected = [
  7264. 'div' => ['class' => 'submit'],
  7265. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7266. '/div'
  7267. ];
  7268. $this->assertHtml($expected, $result);
  7269. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7270. $expected = [
  7271. 'div' => ['class' => 'submit'],
  7272. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7273. '/div'
  7274. ];
  7275. $this->assertHtml($expected, $result);
  7276. }
  7277. /**
  7278. * testSubmitImage method
  7279. *
  7280. * Test image submit types.
  7281. *
  7282. * @return void
  7283. */
  7284. public function testSubmitImage()
  7285. {
  7286. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7287. $expected = [
  7288. 'div' => ['class' => 'submit'],
  7289. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7290. '/div'
  7291. ];
  7292. $this->assertHtml($expected, $result);
  7293. $result = $this->Form->submit('/relative/cake.power.gif');
  7294. $expected = [
  7295. 'div' => ['class' => 'submit'],
  7296. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7297. '/div'
  7298. ];
  7299. $this->assertHtml($expected, $result);
  7300. $result = $this->Form->submit('cake.power.gif');
  7301. $expected = [
  7302. 'div' => ['class' => 'submit'],
  7303. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7304. '/div'
  7305. ];
  7306. $this->assertHtml($expected, $result);
  7307. $result = $this->Form->submit('Not.an.image');
  7308. $expected = [
  7309. 'div' => ['class' => 'submit'],
  7310. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7311. '/div'
  7312. ];
  7313. $this->assertHtml($expected, $result);
  7314. }
  7315. /**
  7316. * testSubmitUnlockedByDefault method
  7317. *
  7318. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7319. * be submitted at a time.
  7320. *
  7321. * @return void
  7322. */
  7323. public function testSubmitUnlockedByDefault()
  7324. {
  7325. $this->Form->request->params['_Token'] = 'secured';
  7326. $this->Form->submit('Go go');
  7327. $this->Form->submit('Save', ['name' => 'save']);
  7328. $result = $this->Form->unlockField();
  7329. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7330. }
  7331. /**
  7332. * testSubmitImageTimestamp method
  7333. *
  7334. * Test submit image with timestamps.
  7335. *
  7336. * @return void
  7337. */
  7338. public function testSubmitImageTimestamp()
  7339. {
  7340. Configure::write('Asset.timestamp', 'force');
  7341. $result = $this->Form->submit('cake.power.gif');
  7342. $expected = [
  7343. 'div' => ['class' => 'submit'],
  7344. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7345. '/div'
  7346. ];
  7347. $this->assertHtml($expected, $result);
  7348. }
  7349. /**
  7350. * testDateTimeWithGetForms method
  7351. *
  7352. * Test that datetime() works with GET style forms.
  7353. *
  7354. * @return void
  7355. */
  7356. public function testDateTimeWithGetForms()
  7357. {
  7358. extract($this->dateRegex);
  7359. $this->Form->create($this->article, ['type' => 'get']);
  7360. $result = $this->Form->datetime('created');
  7361. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7362. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7363. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7364. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7365. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7366. }
  7367. /**
  7368. * testForMagicControlNonExistingNorValidated method
  7369. *
  7370. * @return void
  7371. */
  7372. public function testForMagicControlNonExistingNorValidated()
  7373. {
  7374. $this->Form->create($this->article);
  7375. $this->Form->templates(['inputContainer' => '{{content}}']);
  7376. $result = $this->Form->control('non_existing_nor_validated');
  7377. $expected = [
  7378. 'label' => ['for' => 'non-existing-nor-validated'],
  7379. 'Non Existing Nor Validated',
  7380. '/label',
  7381. 'input' => [
  7382. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7383. 'id' => 'non-existing-nor-validated'
  7384. ]
  7385. ];
  7386. $this->assertHtml($expected, $result);
  7387. $result = $this->Form->control('non_existing_nor_validated', [
  7388. 'val' => 'my value'
  7389. ]);
  7390. $expected = [
  7391. 'label' => ['for' => 'non-existing-nor-validated'],
  7392. 'Non Existing Nor Validated',
  7393. '/label',
  7394. 'input' => [
  7395. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7396. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7397. ]
  7398. ];
  7399. $this->assertHtml($expected, $result);
  7400. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7401. $result = $this->Form->control('non_existing_nor_validated');
  7402. $expected = [
  7403. 'label' => ['for' => 'non-existing-nor-validated'],
  7404. 'Non Existing Nor Validated',
  7405. '/label',
  7406. 'input' => [
  7407. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7408. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7409. ]
  7410. ];
  7411. $this->assertHtml($expected, $result);
  7412. }
  7413. /**
  7414. * testFormMagicControlLabel method
  7415. *
  7416. * @return void
  7417. */
  7418. public function testFormMagicControlLabel()
  7419. {
  7420. TableRegistry::get('Contacts', [
  7421. 'className' => __NAMESPACE__ . '\ContactsTable'
  7422. ]);
  7423. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7424. $this->Form->templates(['inputContainer' => '{{content}}']);
  7425. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7426. $expected = [
  7427. 'label' => ['for' => 'contacts-name'],
  7428. 'My label',
  7429. '/label',
  7430. 'input' => [
  7431. 'type' => 'text', 'name' => 'Contacts[name]',
  7432. 'id' => 'contacts-name', 'maxlength' => '255'
  7433. ]
  7434. ];
  7435. $this->assertHtml($expected, $result);
  7436. $result = $this->Form->control('name', [
  7437. 'label' => ['class' => 'mandatory']
  7438. ]);
  7439. $expected = [
  7440. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7441. 'Name',
  7442. '/label',
  7443. 'input' => [
  7444. 'type' => 'text', 'name' => 'name',
  7445. 'id' => 'name', 'maxlength' => '255'
  7446. ]
  7447. ];
  7448. $this->assertHtml($expected, $result);
  7449. $result = $this->Form->control('name', [
  7450. 'div' => false,
  7451. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7452. ]);
  7453. $expected = [
  7454. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7455. 'My label',
  7456. '/label',
  7457. 'input' => [
  7458. 'type' => 'text', 'name' => 'name',
  7459. 'id' => 'name', 'maxlength' => '255'
  7460. ]
  7461. ];
  7462. $this->assertHtml($expected, $result);
  7463. $result = $this->Form->control('Contact.name', [
  7464. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7465. ]);
  7466. $expected = [
  7467. 'label' => ['for' => 'my_id'],
  7468. 'Name',
  7469. '/label',
  7470. 'input' => [
  7471. 'type' => 'text', 'name' => 'Contact[name]',
  7472. 'id' => 'my_id', 'maxlength' => '255'
  7473. ]
  7474. ];
  7475. $this->assertHtml($expected, $result);
  7476. $result = $this->Form->control('1.id');
  7477. $expected = ['input' => [
  7478. 'type' => 'hidden', 'name' => '1[id]',
  7479. 'id' => '1-id'
  7480. ]];
  7481. $this->assertHtml($expected, $result);
  7482. $result = $this->Form->control('1.name');
  7483. $expected = [
  7484. 'label' => ['for' => '1-name'],
  7485. 'Name',
  7486. '/label',
  7487. 'input' => [
  7488. 'type' => 'text', 'name' => '1[name]',
  7489. 'id' => '1-name', 'maxlength' => '255'
  7490. ]
  7491. ];
  7492. $this->assertHtml($expected, $result);
  7493. }
  7494. /**
  7495. * testFormEnd method
  7496. *
  7497. * @return void
  7498. */
  7499. public function testFormEnd()
  7500. {
  7501. $this->assertEquals('</form>', $this->Form->end());
  7502. }
  7503. /**
  7504. * testMultiRecordForm method
  7505. *
  7506. * Test the generation of fields for a multi record form.
  7507. *
  7508. * @return void
  7509. */
  7510. public function testMultiRecordForm()
  7511. {
  7512. $this->loadFixtures('Articles', 'Comments');
  7513. $articles = TableRegistry::get('Articles');
  7514. $articles->hasMany('Comments');
  7515. $comment = new Entity(['comment' => 'Value']);
  7516. $article = new Article(['comments' => [$comment]]);
  7517. $this->Form->create([$article]);
  7518. $result = $this->Form->control('0.comments.1.comment');
  7519. //@codingStandardsIgnoreStart
  7520. $expected = [
  7521. 'div' => ['class' => 'input textarea'],
  7522. 'label' => ['for' => '0-comments-1-comment'],
  7523. 'Comment',
  7524. '/label',
  7525. 'textarea' => [
  7526. 'name',
  7527. 'id' => '0-comments-1-comment',
  7528. 'rows' => 5
  7529. ],
  7530. '/textarea',
  7531. '/div'
  7532. ];
  7533. //@codingStandardsIgnoreEnd
  7534. $this->assertHtml($expected, $result);
  7535. $result = $this->Form->control('0.comments.0.comment');
  7536. //@codingStandardsIgnoreStart
  7537. $expected = [
  7538. 'div' => ['class' => 'input textarea'],
  7539. 'label' => ['for' => '0-comments-0-comment'],
  7540. 'Comment',
  7541. '/label',
  7542. 'textarea' => [
  7543. 'name',
  7544. 'id' => '0-comments-0-comment',
  7545. 'rows' => 5
  7546. ],
  7547. 'Value',
  7548. '/textarea',
  7549. '/div'
  7550. ];
  7551. //@codingStandardsIgnoreEnd
  7552. $this->assertHtml($expected, $result);
  7553. $comment->errors('comment', ['Not valid']);
  7554. $result = $this->Form->control('0.comments.0.comment');
  7555. //@codingStandardsIgnoreStart
  7556. $expected = [
  7557. 'div' => ['class' => 'input textarea error'],
  7558. 'label' => ['for' => '0-comments-0-comment'],
  7559. 'Comment',
  7560. '/label',
  7561. 'textarea' => [
  7562. 'name',
  7563. 'class' => 'form-error',
  7564. 'id' => '0-comments-0-comment',
  7565. 'rows' => 5
  7566. ],
  7567. 'Value',
  7568. '/textarea',
  7569. ['div' => ['class' => 'error-message']],
  7570. 'Not valid',
  7571. '/div',
  7572. '/div'
  7573. ];
  7574. //@codingStandardsIgnoreEnd
  7575. $this->assertHtml($expected, $result);
  7576. TableRegistry::get('Comments')
  7577. ->validator('default')
  7578. ->allowEmpty('comment', false);
  7579. $result = $this->Form->control('0.comments.1.comment');
  7580. //@codingStandardsIgnoreStart
  7581. $expected = [
  7582. 'div' => ['class' => 'input textarea required'],
  7583. 'label' => ['for' => '0-comments-1-comment'],
  7584. 'Comment',
  7585. '/label',
  7586. 'textarea' => [
  7587. 'name',
  7588. 'required' => 'required',
  7589. 'id' => '0-comments-1-comment',
  7590. 'rows' => 5
  7591. ],
  7592. '/textarea',
  7593. '/div'
  7594. ];
  7595. //@codingStandardsIgnoreEnd
  7596. $this->assertHtml($expected, $result);
  7597. }
  7598. /**
  7599. * testHtml5Controls method
  7600. *
  7601. * Test that some html5 inputs + FormHelper::__call() work.
  7602. *
  7603. * @return void
  7604. */
  7605. public function testHtml5Controls()
  7606. {
  7607. $result = $this->Form->email('User.email');
  7608. $expected = [
  7609. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7610. ];
  7611. $this->assertHtml($expected, $result);
  7612. $result = $this->Form->search('User.query');
  7613. $expected = [
  7614. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7615. ];
  7616. $this->assertHtml($expected, $result);
  7617. $result = $this->Form->search('User.query', ['value' => 'test']);
  7618. $expected = [
  7619. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7620. ];
  7621. $this->assertHtml($expected, $result);
  7622. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7623. $expected = [
  7624. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7625. ];
  7626. $this->assertHtml($expected, $result);
  7627. }
  7628. /**
  7629. * testHtml5ControlWithControl method
  7630. *
  7631. * Test accessing html5 inputs through control().
  7632. *
  7633. * @return void
  7634. */
  7635. public function testHtml5ControlWithControl()
  7636. {
  7637. $this->Form->create();
  7638. $this->Form->templates(['inputContainer' => '{{content}}']);
  7639. $result = $this->Form->control('website', [
  7640. 'type' => 'url',
  7641. 'val' => 'http://domain.tld',
  7642. 'label' => false
  7643. ]);
  7644. $expected = [
  7645. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7646. ];
  7647. $this->assertHtml($expected, $result);
  7648. }
  7649. /**
  7650. * testHtml5ControlException method
  7651. *
  7652. * Test errors when field name is missing.
  7653. *
  7654. * @expectedException \Cake\Core\Exception\Exception
  7655. * @return void
  7656. */
  7657. public function testHtml5ControlException()
  7658. {
  7659. $this->Form->email();
  7660. }
  7661. /**
  7662. * testRequiredAttribute method
  7663. *
  7664. * Tests that formhelper sets required attributes.
  7665. *
  7666. * @return void
  7667. */
  7668. public function testRequiredAttribute()
  7669. {
  7670. $this->article['required'] = [
  7671. 'title' => true,
  7672. 'body' => false,
  7673. ];
  7674. $this->Form->create($this->article);
  7675. $result = $this->Form->control('title');
  7676. $expected = [
  7677. 'div' => ['class' => 'input text required'],
  7678. 'label' => ['for' => 'title'],
  7679. 'Title',
  7680. '/label',
  7681. 'input' => [
  7682. 'type' => 'text',
  7683. 'name' => 'title',
  7684. 'id' => 'title',
  7685. 'required' => 'required',
  7686. ],
  7687. '/div'
  7688. ];
  7689. $this->assertHtml($expected, $result);
  7690. $result = $this->Form->control('title', ['required' => false]);
  7691. $this->assertNotContains('required', $result);
  7692. $result = $this->Form->control('body');
  7693. $expected = [
  7694. 'div' => ['class' => 'input text'],
  7695. 'label' => ['for' => 'body'],
  7696. 'Body',
  7697. '/label',
  7698. 'input' => [
  7699. 'type' => 'text',
  7700. 'name' => 'body',
  7701. 'id' => 'body',
  7702. ],
  7703. '/div'
  7704. ];
  7705. $this->assertHtml($expected, $result);
  7706. $result = $this->Form->control('body', ['required' => true]);
  7707. $this->assertContains('required', $result);
  7708. }
  7709. /**
  7710. * testControlsNotNested method
  7711. *
  7712. * Tests that it is possible to put inputs outside of the label.
  7713. *
  7714. * @return void
  7715. */
  7716. public function testControlsNotNested()
  7717. {
  7718. $this->Form->templates([
  7719. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7720. 'formGroup' => '{{input}}{{label}}',
  7721. ]);
  7722. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7723. $expected = [
  7724. 'div' => ['class' => 'input checkbox'],
  7725. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7726. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7727. 'label' => ['for' => 'foo'],
  7728. 'Foo',
  7729. '/label',
  7730. '/div'
  7731. ];
  7732. $this->assertHtml($expected, $result);
  7733. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7734. $expected = [
  7735. 'div' => ['class' => 'input checkbox'],
  7736. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7737. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7738. '/div'
  7739. ];
  7740. $this->assertHtml($expected, $result);
  7741. $result = $this->Form->control('confirm', [
  7742. 'type' => 'radio',
  7743. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7744. ]);
  7745. $expected = [
  7746. 'div' => ['class' => 'input radio'],
  7747. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7748. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7749. ['label' => ['for' => 'confirm-y']],
  7750. 'Yes',
  7751. '/label',
  7752. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7753. ['label' => ['for' => 'confirm-n']],
  7754. 'No',
  7755. '/label',
  7756. '<label',
  7757. 'Confirm',
  7758. '/label',
  7759. '/div',
  7760. ];
  7761. $this->assertHtml($expected, $result);
  7762. $result = $this->Form->select('category', ['1', '2'], [
  7763. 'multiple' => 'checkbox',
  7764. 'name' => 'fish',
  7765. ]);
  7766. $expected = [
  7767. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7768. ['div' => ['class' => 'checkbox']],
  7769. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7770. ['label' => ['for' => 'fish-0']],
  7771. '1',
  7772. '/label',
  7773. '/div',
  7774. ['div' => ['class' => 'checkbox']],
  7775. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7776. ['label' => ['for' => 'fish-1']],
  7777. '2',
  7778. '/label',
  7779. '/div'
  7780. ];
  7781. $this->assertHtml($expected, $result);
  7782. }
  7783. /**
  7784. * testControlContainerTemplates method
  7785. *
  7786. * Test that *Container templates are used by input.
  7787. *
  7788. * @return void
  7789. */
  7790. public function testControlContainerTemplates()
  7791. {
  7792. $this->Form->templates([
  7793. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7794. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7795. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7796. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7797. ]);
  7798. $this->article['errors'] = [
  7799. 'Article' => ['published' => 'error message']
  7800. ];
  7801. $this->Form->create($this->article);
  7802. $result = $this->Form->control('accept', [
  7803. 'type' => 'checkbox'
  7804. ]);
  7805. $expected = [
  7806. 'div' => ['class' => 'check'],
  7807. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7808. 'label' => ['for' => 'accept'],
  7809. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7810. 'Accept',
  7811. '/label',
  7812. '/div'
  7813. ];
  7814. $this->assertHtml($expected, $result);
  7815. $result = $this->Form->control('accept', [
  7816. 'type' => 'radio',
  7817. 'options' => ['Y', 'N']
  7818. ]);
  7819. $this->assertContains('<div class="rad">', $result);
  7820. $result = $this->Form->control('Article.published', [
  7821. 'type' => 'radio',
  7822. 'options' => ['Y', 'N']
  7823. ]);
  7824. $this->assertContains('<div class="rad err">', $result);
  7825. $result = $this->Form->control('Article.created', [
  7826. 'type' => 'datetime',
  7827. ]);
  7828. $this->assertContains('<div class="dt">', $result);
  7829. }
  7830. /**
  7831. * testFormGroupTemplates method
  7832. *
  7833. * Test that *Container templates are used by input.
  7834. *
  7835. * @return void
  7836. */
  7837. public function testFormGroupTemplates()
  7838. {
  7839. $this->Form->templates([
  7840. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7841. ]);
  7842. $this->Form->create($this->article);
  7843. $result = $this->Form->control('accept', [
  7844. 'type' => 'radio',
  7845. 'options' => ['Y', 'N']
  7846. ]);
  7847. $this->assertContains('<div class="radio">', $result);
  7848. }
  7849. /**
  7850. * testResetTemplates method
  7851. *
  7852. * Test resetting templates.
  7853. *
  7854. * @return void
  7855. */
  7856. public function testResetTemplates()
  7857. {
  7858. $this->Form->templates(['input' => '<input/>']);
  7859. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7860. $this->Form->resetTemplates();
  7861. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7862. }
  7863. /**
  7864. * testContext method
  7865. *
  7866. * Test the context method.
  7867. *
  7868. * @return void
  7869. */
  7870. public function testContext()
  7871. {
  7872. $result = $this->Form->context();
  7873. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7874. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7875. $this->assertSame($mock, $this->Form->context($mock));
  7876. $this->assertSame($mock, $this->Form->context());
  7877. }
  7878. /**
  7879. * testAutoDomId method
  7880. *
  7881. * @return void
  7882. */
  7883. public function testAutoDomId()
  7884. {
  7885. $result = $this->Form->text('field', ['id' => true]);
  7886. $expected = [
  7887. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7888. ];
  7889. $this->assertHtml($expected, $result);
  7890. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7891. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7892. $expected = [
  7893. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7894. ['label' => ['for' => 'field-0']],
  7895. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7896. 'option A',
  7897. '/label',
  7898. ['label' => ['for' => 'field-1']],
  7899. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7900. 'option B',
  7901. '/label',
  7902. ];
  7903. $this->assertHtml($expected, $result);
  7904. $result = $this->Form->select(
  7905. 'multi_field',
  7906. ['first', 'second'],
  7907. ['multiple' => 'checkbox', 'id' => true]
  7908. );
  7909. $expected = [
  7910. 'input' => [
  7911. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7912. ],
  7913. ['div' => ['class' => 'checkbox']],
  7914. ['label' => ['for' => 'multi-field-0']],
  7915. ['input' => [
  7916. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7917. 'value' => '0', 'id' => 'multi-field-0'
  7918. ]],
  7919. 'first',
  7920. '/label',
  7921. '/div',
  7922. ['div' => ['class' => 'checkbox']],
  7923. ['label' => ['for' => 'multi-field-1']],
  7924. ['input' => [
  7925. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7926. 'value' => '1', 'id' => 'multi-field-1'
  7927. ]],
  7928. 'second',
  7929. '/label',
  7930. '/div',
  7931. ];
  7932. $this->assertHtml($expected, $result);
  7933. }
  7934. /**
  7935. * Test the basic setters and getters for value sources
  7936. *
  7937. * @return void
  7938. */
  7939. public function testFormValueSourcesSettersGetters()
  7940. {
  7941. $expected = ['context'];
  7942. $result = $this->Form->getValueSources();
  7943. $this->assertEquals($expected, $result);
  7944. $expected = null;
  7945. $result = $this->Form->getSourceValue('id');
  7946. $this->assertEquals($expected, $result);
  7947. $expected = ['query', 'data', 'context'];
  7948. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7949. $result = $this->Form->getValueSources();
  7950. $this->assertEquals($expected, $result);
  7951. $this->Form->request->data['id'] = '1';
  7952. $this->Form->request->query['id'] = '2';
  7953. $this->Form->setValueSources(['context']);
  7954. $expected = '1';
  7955. $result = $this->Form->getSourceValue('id');
  7956. $this->assertEquals($expected, $result);
  7957. $this->Form->setValueSources('query');
  7958. $expected = ['query'];
  7959. $result = $this->Form->getValueSources();
  7960. $this->assertEquals($expected, $result);
  7961. $expected = '2';
  7962. $result = $this->Form->getSourceValue('id');
  7963. $this->assertEquals($expected, $result);
  7964. $this->Form->setValueSources(['data']);
  7965. $expected = '1';
  7966. $result = $this->Form->getSourceValue('id');
  7967. $this->assertEquals($expected, $result);
  7968. $this->Form->setValueSources(['query', 'data']);
  7969. $expected = '2';
  7970. $result = $this->Form->getSourceValue('id');
  7971. $this->assertEquals($expected, $result);
  7972. }
  7973. /**
  7974. * Tests the different input rendering values based on sources values switching
  7975. *
  7976. * @return void
  7977. */
  7978. public function testFormValueSourcesSingleSwitchRendering()
  7979. {
  7980. $this->loadFixtures('Articles');
  7981. $articles = TableRegistry::get('Articles');
  7982. $article = new Article();
  7983. $articles->patchEntity($article, ['id' => '3']);
  7984. $this->Form->create($article);
  7985. $this->Form->setValueSources(['context']);
  7986. $result = $this->Form->control('id');
  7987. $expected = [
  7988. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7989. ];
  7990. $this->assertHtml($expected, $result);
  7991. $this->Form->request->query['id'] = '5';
  7992. $this->Form->setValueSources(['query']);
  7993. $result = $this->Form->control('id');
  7994. $expected = [
  7995. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7996. ];
  7997. $this->assertHtml($expected, $result);
  7998. $this->Form->request->query['id'] = '5a';
  7999. $this->Form->request->data['id'] = '5b';
  8000. $this->Form->setValueSources(['context']);
  8001. $this->Form->create($article);
  8002. $result = $this->Form->control('id');
  8003. $expected = [
  8004. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8005. ];
  8006. $this->assertHtml($expected, $result);
  8007. $this->Form->setValueSources(['data']);
  8008. $this->Form->create($article);
  8009. $result = $this->Form->control('id');
  8010. $expected = [
  8011. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8012. ];
  8013. $this->assertHtml($expected, $result);
  8014. $this->Form->setValueSources(['query']);
  8015. $result = $this->Form->control('id');
  8016. $expected = [
  8017. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8018. ];
  8019. $this->assertHtml($expected, $result);
  8020. }
  8021. /**
  8022. * Tests the different input rendering values based on sources values switching while supplying
  8023. * an entity (base context) and multiple sources (such as data, query)
  8024. *
  8025. * @return void
  8026. */
  8027. public function testFormValueSourcesListSwitchRendering()
  8028. {
  8029. $this->loadFixtures('Articles');
  8030. $articles = TableRegistry::get('Articles');
  8031. $article = new Article();
  8032. $articles->patchEntity($article, ['id' => '3']);
  8033. $this->Form->request->query['id'] = '9';
  8034. $this->Form->create($article);
  8035. $this->Form->setValueSources(['context', 'query']);
  8036. $result = $this->Form->control('id');
  8037. $expected = [
  8038. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8039. ];
  8040. $this->assertHtml($expected, $result);
  8041. $this->Form->setValueSources(['query', 'context']);
  8042. $result = $this->Form->control('id');
  8043. $expected = [
  8044. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8045. ];
  8046. $this->assertHtml($expected, $result);
  8047. $this->Form->setValueSources(['data', 'query', 'context']);
  8048. $result = $this->Form->control('id');
  8049. $expected = [
  8050. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8051. ];
  8052. $this->assertHtml($expected, $result);
  8053. $this->Form->request->data['id'] = '8';
  8054. $this->Form->request->query['id'] = '9';
  8055. $this->Form->setValueSources(['data', 'query', 'context']);
  8056. $result = $this->Form->control('id');
  8057. $expected = [
  8058. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8059. ];
  8060. $this->assertHtml($expected, $result);
  8061. }
  8062. /**
  8063. * Test the different form input renderings based on values sources switchings through form options
  8064. *
  8065. * @return void
  8066. */
  8067. public function testFormValueSourcesSwitchViaOptionsRendering()
  8068. {
  8069. $this->loadFixtures('Articles');
  8070. $articles = TableRegistry::get('Articles');
  8071. $article = new Article();
  8072. $articles->patchEntity($article, ['id' => '3']);
  8073. $this->Form->request->data['id'] = '4';
  8074. $this->Form->request->query['id'] = '5';
  8075. $this->Form->create($article, ['valueSources' => 'query']);
  8076. $result = $this->Form->control('id');
  8077. $expected = [
  8078. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8079. ];
  8080. $this->assertHtml($expected, $result);
  8081. $result = $this->Form->getSourceValue('id');
  8082. $this->assertEquals('5', $result);
  8083. $this->Form->setValueSources(['context']);
  8084. $this->Form->create($article, ['valueSources' => 'query']);
  8085. $result = $this->Form->control('id');
  8086. $expected = [
  8087. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8088. ];
  8089. $this->assertHtml($expected, $result);
  8090. $result = $this->Form->getSourceValue('id');
  8091. $this->assertEquals('5', $result);
  8092. $this->Form->setValueSources(['query']);
  8093. $this->Form->create($article, ['valueSources' => 'data']);
  8094. $result = $this->Form->control('id');
  8095. $expected = [
  8096. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8097. ];
  8098. $this->assertHtml($expected, $result);
  8099. $result = $this->Form->getSourceValue('id');
  8100. $this->assertEquals('4', $result);
  8101. $this->Form->setValueSources(['query']);
  8102. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8103. $result = $this->Form->control('id');
  8104. $expected = [
  8105. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8106. ];
  8107. $this->assertHtml($expected, $result);
  8108. $result = $this->Form->getSourceValue('id');
  8109. $this->assertEquals('4', $result);
  8110. }
  8111. /**
  8112. * Test the different form input renderings based on values sources switchings through form options
  8113. *
  8114. * @return void
  8115. */
  8116. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8117. {
  8118. $this->loadFixtures('Articles');
  8119. $articles = TableRegistry::get('Articles');
  8120. $article = new Article();
  8121. $articles->patchEntity($article, ['id' => '3']);
  8122. $this->Form->request->data['id'] = '10';
  8123. $this->Form->request->query['id'] = '11';
  8124. $this->Form->setValueSources(['context'])
  8125. ->create($article, ['valueSources' => ['query', 'data']]);
  8126. $result = $this->Form->control('id');
  8127. $expected = [
  8128. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8129. ];
  8130. $this->assertHtml($expected, $result);
  8131. $result = $this->Form->getSourceValue('id');
  8132. $this->assertEquals('11', $result);
  8133. unset($this->Form->request->query['id']);
  8134. $this->Form->setValueSources(['context'])
  8135. ->create($article, ['valueSources' => ['query', 'data']]);
  8136. $result = $this->Form->control('id');
  8137. $expected = [
  8138. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8139. ];
  8140. $this->assertHtml($expected, $result);
  8141. $result = $this->Form->getSourceValue('id');
  8142. $this->assertEquals('10', $result);
  8143. }
  8144. /**
  8145. * Test the different form values sources resetting through From::end();
  8146. *
  8147. * @return void
  8148. */
  8149. public function testFormValueSourcesResetViaEnd()
  8150. {
  8151. $expected = ['context'];
  8152. $result = $this->Form->getValueSources();
  8153. $this->assertEquals($expected, $result);
  8154. $expected = ['query', 'context', 'data'];
  8155. $this->Form->setValueSources(['query', 'context', 'data']);
  8156. $result = $this->Form->getValueSources();
  8157. $this->assertEquals($expected, $result);
  8158. $this->Form->create();
  8159. $result = $this->Form->getValueSources();
  8160. $this->assertEquals($expected, $result);
  8161. $this->Form->end();
  8162. $result = $this->Form->getValueSources();
  8163. $this->assertEquals(['context'], $result);
  8164. }
  8165. /**
  8166. * Test sources values defaults handling
  8167. *
  8168. * @return void
  8169. */
  8170. public function testFormValueSourcesDefaults()
  8171. {
  8172. $this->Form->request->query['password'] = 'open Sesame';
  8173. $this->Form->create();
  8174. $result = $this->Form->password('password');
  8175. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8176. $this->assertHtml($expected, $result);
  8177. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8178. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8179. $this->assertHtml($expected, $result);
  8180. $this->Form->setValueSources('query');
  8181. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8182. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8183. $this->assertHtml($expected, $result);
  8184. $this->Form->setValueSources('data');
  8185. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8186. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8187. $this->assertHtml($expected, $result);
  8188. }
  8189. /**
  8190. * Test sources values schema defaults handling
  8191. *
  8192. * @return void
  8193. */
  8194. public function testSourcesValueDoesntExistPassThrough()
  8195. {
  8196. $this->Form->request->query['category'] = 'sesame-cookies';
  8197. $articles = TableRegistry::get('Articles');
  8198. $entity = $articles->newEntity();
  8199. $this->Form->create($entity);
  8200. $this->Form->setValueSources(['query', 'context']);
  8201. $result = $this->Form->getSourceValue('category');
  8202. $this->assertEquals('sesame-cookies', $result);
  8203. $this->Form->setValueSources(['context', 'query']);
  8204. $result = $this->Form->getSourceValue('category');
  8205. $this->assertEquals('sesame-cookies', $result);
  8206. }
  8207. /**
  8208. * testNestedLabelInput method
  8209. *
  8210. * Test the `nestedInput` parameter
  8211. *
  8212. * @return void
  8213. */
  8214. public function testNestedLabelInput()
  8215. {
  8216. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8217. $expected = [
  8218. 'div' => ['class' => 'input text'],
  8219. 'label' => ['for' => 'foo'],
  8220. ['input' => [
  8221. 'type' => 'text',
  8222. 'name' => 'foo',
  8223. 'id' => 'foo'
  8224. ]],
  8225. 'Foo',
  8226. '/label',
  8227. '/div'
  8228. ];
  8229. $this->assertHtml($expected, $result);
  8230. }
  8231. /**
  8232. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8233. *
  8234. * This test makes sure `false` excludes the label from the render
  8235. *
  8236. * @return void
  8237. */
  8238. public function testInputLabelManipulationDisableLabels()
  8239. {
  8240. $result = $this->Form->input('test', [
  8241. 'type' => 'radio',
  8242. 'options' => ['A', 'B'],
  8243. 'labelOptions' => false
  8244. ]);
  8245. $expected = [
  8246. ['div' => ['class' => 'input radio']],
  8247. '<label',
  8248. 'Test',
  8249. '/label',
  8250. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8251. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8252. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8253. '/div',
  8254. ];
  8255. $this->assertHtml($expected, $result);
  8256. $result = $this->Form->input('checkbox1', [
  8257. 'label' => 'My checkboxes',
  8258. 'multiple' => 'checkbox',
  8259. 'type' => 'select',
  8260. 'options' => [
  8261. ['text' => 'First Checkbox', 'value' => 1],
  8262. ['text' => 'Second Checkbox', 'value' => 2]
  8263. ],
  8264. 'labelOptions' => false
  8265. ]);
  8266. $expected = [
  8267. ['div' => ['class' => 'input select']],
  8268. ['label' => ['for' => 'checkbox1']],
  8269. 'My checkboxes',
  8270. '/label',
  8271. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8272. ['div' => ['class' => 'checkbox']],
  8273. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8274. '/div',
  8275. ['div' => ['class' => 'checkbox']],
  8276. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8277. '/div',
  8278. '/div'
  8279. ];
  8280. $this->assertHtml($expected, $result);
  8281. }
  8282. /**
  8283. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8284. *
  8285. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8286. * added to the class if checked.
  8287. *
  8288. * Also checks to make sure any custom attributes are rendered correctly
  8289. *
  8290. * @return void
  8291. */
  8292. public function testInputLabelManipulationRadios()
  8293. {
  8294. $result = $this->Form->input('test', [
  8295. 'type' => 'radio',
  8296. 'options' => ['A', 'B'],
  8297. 'labelOptions' => ['class' => 'custom-class']
  8298. ]);
  8299. $expected = [
  8300. ['div' => ['class' => 'input radio']],
  8301. '<label',
  8302. 'Test',
  8303. '/label',
  8304. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8305. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8306. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8307. 'A',
  8308. '/label',
  8309. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8310. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8311. 'B',
  8312. '/label',
  8313. '/div',
  8314. ];
  8315. $this->assertHtml($expected, $result);
  8316. $result = $this->Form->input('test', [
  8317. 'type' => 'radio',
  8318. 'options' => ['A', 'B'],
  8319. 'value' => 1,
  8320. 'labelOptions' => ['class' => 'custom-class']
  8321. ]);
  8322. $expected = [
  8323. ['div' => ['class' => 'input radio']],
  8324. '<label',
  8325. 'Test',
  8326. '/label',
  8327. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8328. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8329. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8330. 'A',
  8331. '/label',
  8332. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8333. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8334. 'B',
  8335. '/label',
  8336. '/div',
  8337. ];
  8338. $this->assertHtml($expected, $result);
  8339. $result = $this->Form->input('test', [
  8340. 'type' => 'radio',
  8341. 'options' => ['A', 'B'],
  8342. 'value' => 1,
  8343. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8344. ]);
  8345. $expected = [
  8346. ['div' => ['class' => 'input radio']],
  8347. '<label',
  8348. 'Test',
  8349. '/label',
  8350. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8351. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8352. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8353. 'A',
  8354. '/label',
  8355. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8356. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8357. 'B',
  8358. '/label',
  8359. '/div',
  8360. ];
  8361. $this->assertHtml($expected, $result);
  8362. $result = $this->Form->radio('test', ['A', 'B'], [
  8363. 'label' => [
  8364. 'class' => ['custom-class', 'another-class'],
  8365. 'data-name' => 'bob'
  8366. ],
  8367. 'value' => 1
  8368. ]);
  8369. $expected = [
  8370. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8371. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8372. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8373. 'A',
  8374. '/label',
  8375. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8376. ['input' => [
  8377. 'type' => 'radio',
  8378. 'name' => 'test',
  8379. 'value' => '1',
  8380. 'id' => 'test-1',
  8381. 'checked' => 'checked'
  8382. ]],
  8383. 'B',
  8384. '/label',
  8385. ];
  8386. $this->assertHtml($expected, $result);
  8387. }
  8388. /**
  8389. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8390. *
  8391. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8392. * added to the class if checked.
  8393. *
  8394. * Also checks to make sure any custom attributes are rendered correctly
  8395. *
  8396. * @return void
  8397. */
  8398. public function testInputLabelManipulationCheckboxes()
  8399. {
  8400. $result = $this->Form->input('checkbox1', [
  8401. 'label' => 'My checkboxes',
  8402. 'multiple' => 'checkbox',
  8403. 'type' => 'select',
  8404. 'options' => [
  8405. ['text' => 'First Checkbox', 'value' => 1],
  8406. ['text' => 'Second Checkbox', 'value' => 2]
  8407. ],
  8408. 'labelOptions' => ['class' => 'custom-class'],
  8409. 'value' => ['1']
  8410. ]);
  8411. $expected = [
  8412. ['div' => ['class' => 'input select']],
  8413. ['label' => ['for' => 'checkbox1']],
  8414. 'My checkboxes',
  8415. '/label',
  8416. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8417. ['div' => ['class' => 'checkbox']],
  8418. ['label' => [
  8419. 'class' => 'custom-class selected',
  8420. 'for' => 'checkbox1-1'
  8421. ]],
  8422. ['input' => [
  8423. 'type' => 'checkbox',
  8424. 'name' => 'checkbox1[]',
  8425. 'value' => '1',
  8426. 'id' => 'checkbox1-1',
  8427. 'checked' => 'checked'
  8428. ]],
  8429. 'First Checkbox',
  8430. '/label',
  8431. '/div',
  8432. ['div' => ['class' => 'checkbox']],
  8433. ['label' => [
  8434. 'class' => 'custom-class',
  8435. 'for' => 'checkbox1-2'
  8436. ]],
  8437. ['input' => [
  8438. 'type' => 'checkbox',
  8439. 'name' => 'checkbox1[]',
  8440. 'value' => '2',
  8441. 'id' => 'checkbox1-2'
  8442. ]],
  8443. 'Second Checkbox',
  8444. '/label',
  8445. '/div',
  8446. '/div'
  8447. ];
  8448. $this->assertHtml($expected, $result);
  8449. $result = $this->Form->input('checkbox1', [
  8450. 'label' => 'My checkboxes',
  8451. 'multiple' => 'checkbox',
  8452. 'type' => 'select',
  8453. 'options' => [
  8454. ['text' => 'First Checkbox', 'value' => 1],
  8455. ['text' => 'Second Checkbox', 'value' => 2]
  8456. ],
  8457. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8458. 'value' => ['1']
  8459. ]);
  8460. $expected = [
  8461. ['div' => ['class' => 'input select']],
  8462. ['label' => ['for' => 'checkbox1']],
  8463. 'My checkboxes',
  8464. '/label',
  8465. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8466. ['div' => ['class' => 'checkbox']],
  8467. ['label' => [
  8468. 'class' => 'custom-class another-class selected',
  8469. 'data-name' => 'bob',
  8470. 'for' => 'checkbox1-1'
  8471. ]],
  8472. ['input' => [
  8473. 'type' => 'checkbox',
  8474. 'name' => 'checkbox1[]',
  8475. 'value' => '1',
  8476. 'id' => 'checkbox1-1',
  8477. 'checked' => 'checked'
  8478. ]],
  8479. 'First Checkbox',
  8480. '/label',
  8481. '/div',
  8482. ['div' => ['class' => 'checkbox']],
  8483. ['label' => [
  8484. 'class' => 'custom-class another-class',
  8485. 'data-name' => 'bob',
  8486. 'for' => 'checkbox1-2'
  8487. ]],
  8488. ['input' => [
  8489. 'type' => 'checkbox',
  8490. 'name' => 'checkbox1[]',
  8491. 'value' => '2',
  8492. 'id' => 'checkbox1-2'
  8493. ]],
  8494. 'Second Checkbox',
  8495. '/label',
  8496. '/div',
  8497. '/div'
  8498. ];
  8499. $this->assertHtml($expected, $result);
  8500. }
  8501. }