FormHelperTest.php 303 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219
  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. * Test setting a hiddenField value on radio buttons.
  4331. *
  4332. * @return void
  4333. */
  4334. public function testRadioHiddenFieldValue()
  4335. {
  4336. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4337. $expected = [
  4338. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4339. 'label' => ['for' => 'title-0'],
  4340. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4341. 'option A',
  4342. '/label',
  4343. ];
  4344. $this->assertHtml($expected, $result);
  4345. }
  4346. /**
  4347. * testControlRadio method
  4348. *
  4349. * Test that input works with radio types.
  4350. *
  4351. * @return void
  4352. */
  4353. public function testControlRadio()
  4354. {
  4355. $result = $this->Form->control('test', [
  4356. 'type' => 'radio',
  4357. 'options' => ['A', 'B'],
  4358. ]);
  4359. $expected = [
  4360. ['div' => ['class' => 'input radio']],
  4361. '<label',
  4362. 'Test',
  4363. '/label',
  4364. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4365. ['label' => ['for' => 'test-0']],
  4366. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4367. 'A',
  4368. '/label',
  4369. ['label' => ['for' => 'test-1']],
  4370. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4371. 'B',
  4372. '/label',
  4373. '/div',
  4374. ];
  4375. $this->assertHtml($expected, $result);
  4376. $result = $this->Form->control('test', [
  4377. 'type' => 'radio',
  4378. 'options' => ['A', 'B'],
  4379. 'value' => '0'
  4380. ]);
  4381. $expected = [
  4382. ['div' => ['class' => 'input radio']],
  4383. '<label',
  4384. 'Test',
  4385. '/label',
  4386. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4387. ['label' => ['for' => 'test-0']],
  4388. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4389. 'A',
  4390. '/label',
  4391. ['label' => ['for' => 'test-1']],
  4392. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4393. 'B',
  4394. '/label',
  4395. '/div',
  4396. ];
  4397. $this->assertHtml($expected, $result);
  4398. $result = $this->Form->control('test', [
  4399. 'type' => 'radio',
  4400. 'options' => ['A', 'B'],
  4401. 'label' => false
  4402. ]);
  4403. $expected = [
  4404. ['div' => ['class' => 'input radio']],
  4405. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4406. ['label' => ['for' => 'test-0']],
  4407. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4408. 'A',
  4409. '/label',
  4410. ['label' => ['for' => 'test-1']],
  4411. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4412. 'B',
  4413. '/label',
  4414. '/div',
  4415. ];
  4416. $this->assertHtml($expected, $result);
  4417. }
  4418. /**
  4419. * testRadioNoLabel method
  4420. *
  4421. * Test that radio() works with label = false.
  4422. *
  4423. * @return void
  4424. */
  4425. public function testRadioNoLabel()
  4426. {
  4427. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4428. $expected = [
  4429. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4430. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4431. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4432. ];
  4433. $this->assertHtml($expected, $result);
  4434. }
  4435. /**
  4436. * testRadioControlInsideLabel method
  4437. *
  4438. * Test generating radio input inside label ala twitter bootstrap.
  4439. *
  4440. * @return void
  4441. */
  4442. public function testRadioControlInsideLabel()
  4443. {
  4444. $this->Form->templates([
  4445. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4446. 'radioWrapper' => '{{label}}'
  4447. ]);
  4448. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4449. //@codingStandardsIgnoreStart
  4450. $expected = [
  4451. ['input' => [
  4452. 'type' => 'hidden',
  4453. 'name' => 'Model[field]',
  4454. 'value' => ''
  4455. ]],
  4456. ['label' => ['for' => 'model-field-0']],
  4457. ['input' => [
  4458. 'type' => 'radio',
  4459. 'name' => 'Model[field]',
  4460. 'value' => '0',
  4461. 'id' => 'model-field-0'
  4462. ]],
  4463. 'option A',
  4464. '/label',
  4465. ['label' => ['for' => 'model-field-1']],
  4466. ['input' => [
  4467. 'type' => 'radio',
  4468. 'name' => 'Model[field]',
  4469. 'value' => '1',
  4470. 'id' => 'model-field-1'
  4471. ]],
  4472. 'option B',
  4473. '/label',
  4474. ];
  4475. //@codingStandardsIgnoreEnd
  4476. $this->assertHtml($expected, $result);
  4477. }
  4478. /**
  4479. * testRadioHiddenControlDisabling method
  4480. *
  4481. * Test disabling the hidden input for radio buttons.
  4482. *
  4483. * @return void
  4484. */
  4485. public function testRadioHiddenControlDisabling()
  4486. {
  4487. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4488. $expected = [
  4489. 'label' => ['for' => 'model-1-field-0'],
  4490. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4491. 'option A',
  4492. '/label'
  4493. ];
  4494. $this->assertHtml($expected, $result);
  4495. }
  4496. /**
  4497. * testRadioOutOfRange method
  4498. *
  4499. * Test radio element set generation.
  4500. *
  4501. * @return void
  4502. */
  4503. public function testRadioOutOfRange()
  4504. {
  4505. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4506. $expected = [
  4507. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4508. 'label' => ['for' => 'model-field-v'],
  4509. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4510. 'value',
  4511. '/label'
  4512. ];
  4513. $this->assertHtml($expected, $result);
  4514. }
  4515. /**
  4516. * testSelect method
  4517. *
  4518. * Test select element generation.
  4519. *
  4520. * @return void
  4521. */
  4522. public function testSelect()
  4523. {
  4524. $result = $this->Form->select('Model.field', []);
  4525. $expected = [
  4526. 'select' => ['name' => 'Model[field]'],
  4527. '/select'
  4528. ];
  4529. $this->assertHtml($expected, $result);
  4530. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4531. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4532. $expected = [
  4533. 'select' => ['name' => 'Model[field]'],
  4534. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4535. 'good',
  4536. '/option',
  4537. ['option' => ['value' => 'other']],
  4538. 'bad',
  4539. '/option',
  4540. '/select'
  4541. ];
  4542. $this->assertHtml($expected, $result);
  4543. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4544. $this->assertHtml($expected, $result);
  4545. $this->Form->request->data = [];
  4546. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4547. $expected = [
  4548. 'select' => ['name' => 'Model[field]'],
  4549. ['option' => ['value' => 'value']],
  4550. 'good',
  4551. '/option',
  4552. ['option' => ['value' => 'other']],
  4553. 'bad',
  4554. '/option',
  4555. '/select'
  4556. ];
  4557. $this->assertHtml($expected, $result);
  4558. $options = [
  4559. ['value' => 'first', 'text' => 'First'],
  4560. ['value' => 'first', 'text' => 'Another First'],
  4561. ];
  4562. $result = $this->Form->select(
  4563. 'Model.field',
  4564. $options,
  4565. ['escape' => false, 'empty' => false]
  4566. );
  4567. $expected = [
  4568. 'select' => ['name' => 'Model[field]'],
  4569. ['option' => ['value' => 'first']],
  4570. 'First',
  4571. '/option',
  4572. ['option' => ['value' => 'first']],
  4573. 'Another First',
  4574. '/option',
  4575. '/select'
  4576. ];
  4577. $this->assertHtml($expected, $result);
  4578. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4579. $result = $this->Form->select(
  4580. 'Model.contact_id',
  4581. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4582. ['escape' => false, 'empty' => 'pick something']
  4583. );
  4584. $expected = [
  4585. 'select' => ['name' => 'Model[contact_id]'],
  4586. ['option' => ['value' => '']], 'pick something', '/option',
  4587. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4588. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4589. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4590. '/select'
  4591. ];
  4592. $this->assertHtml($expected, $result);
  4593. $this->Form->request->data['Model']['field'] = 0;
  4594. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4595. $expected = [
  4596. 'select' => ['name' => 'Model[field]'],
  4597. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4598. ['option' => ['value' => '1']], 'Yes', '/option',
  4599. '/select'
  4600. ];
  4601. $this->assertHtml($expected, $result);
  4602. }
  4603. /**
  4604. * testSelectEscapeHtml method
  4605. *
  4606. * Test that select() escapes HTML.
  4607. *
  4608. * @return void
  4609. */
  4610. public function testSelectEscapeHtml()
  4611. {
  4612. $result = $this->Form->select(
  4613. 'Model.field',
  4614. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4615. ['empty' => false]
  4616. );
  4617. $expected = [
  4618. 'select' => ['name' => 'Model[field]'],
  4619. ['option' => ['value' => 'first']],
  4620. 'first &quot;html&quot; &lt;chars&gt;',
  4621. '/option',
  4622. ['option' => ['value' => 'second']],
  4623. 'value',
  4624. '/option',
  4625. '/select'
  4626. ];
  4627. $this->assertHtml($expected, $result);
  4628. $result = $this->Form->select(
  4629. 'Model.field',
  4630. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4631. ['escape' => false, 'empty' => false]
  4632. );
  4633. $expected = [
  4634. 'select' => ['name' => 'Model[field]'],
  4635. ['option' => ['value' => 'first']],
  4636. 'first "html" <chars>',
  4637. '/option',
  4638. ['option' => ['value' => 'second']],
  4639. 'value',
  4640. '/option',
  4641. '/select'
  4642. ];
  4643. $this->assertHtml($expected, $result);
  4644. }
  4645. /**
  4646. * testSelectRequired method
  4647. *
  4648. * Test select() with required and disabled attributes.
  4649. *
  4650. * @return void
  4651. */
  4652. public function testSelectRequired()
  4653. {
  4654. $this->article['required'] = [
  4655. 'user_id' => true
  4656. ];
  4657. $this->Form->create($this->article);
  4658. $result = $this->Form->select('user_id', ['option A']);
  4659. $expected = [
  4660. 'select' => [
  4661. 'name' => 'user_id',
  4662. 'required' => 'required'
  4663. ],
  4664. ['option' => ['value' => '0']], 'option A', '/option',
  4665. '/select'
  4666. ];
  4667. $this->assertHtml($expected, $result);
  4668. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4669. $expected = [
  4670. 'select' => [
  4671. 'name' => 'user_id',
  4672. 'disabled' => 'disabled'
  4673. ],
  4674. ['option' => ['value' => '0']], 'option A', '/option',
  4675. '/select'
  4676. ];
  4677. $this->assertHtml($expected, $result);
  4678. }
  4679. /**
  4680. * testNestedSelect method
  4681. *
  4682. * Test select element generation with optgroups.
  4683. *
  4684. * @return void
  4685. */
  4686. public function testNestedSelect()
  4687. {
  4688. $result = $this->Form->select(
  4689. 'Model.field',
  4690. [1 => 'One', 2 => 'Two', 'Three' => [
  4691. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4692. ]],
  4693. ['empty' => false]
  4694. );
  4695. $expected = [
  4696. 'select' => ['name' => 'Model[field]'],
  4697. ['option' => ['value' => 1]],
  4698. 'One',
  4699. '/option',
  4700. ['option' => ['value' => 2]],
  4701. 'Two',
  4702. '/option',
  4703. ['optgroup' => ['label' => 'Three']],
  4704. ['option' => ['value' => 3]],
  4705. 'Three',
  4706. '/option',
  4707. ['option' => ['value' => 4]],
  4708. 'Four',
  4709. '/option',
  4710. ['option' => ['value' => 5]],
  4711. 'Five',
  4712. '/option',
  4713. '/optgroup',
  4714. '/select'
  4715. ];
  4716. $this->assertHtml($expected, $result);
  4717. }
  4718. /**
  4719. * testSelectMultiple method
  4720. *
  4721. * Test generation of multiple select elements.
  4722. *
  4723. * @return void
  4724. */
  4725. public function testSelectMultiple()
  4726. {
  4727. $options = ['first', 'second', 'third'];
  4728. $result = $this->Form->select(
  4729. 'Model.multi_field',
  4730. $options,
  4731. ['form' => 'my-form', 'multiple' => true]
  4732. );
  4733. $expected = [
  4734. 'input' => [
  4735. 'type' => 'hidden',
  4736. 'name' => 'Model[multi_field]',
  4737. 'value' => '',
  4738. 'form' => 'my-form',
  4739. ],
  4740. 'select' => [
  4741. 'name' => 'Model[multi_field][]',
  4742. 'multiple' => 'multiple',
  4743. 'form' => 'my-form',
  4744. ],
  4745. ['option' => ['value' => '0']],
  4746. 'first',
  4747. '/option',
  4748. ['option' => ['value' => '1']],
  4749. 'second',
  4750. '/option',
  4751. ['option' => ['value' => '2']],
  4752. 'third',
  4753. '/option',
  4754. '/select'
  4755. ];
  4756. $this->assertHtml($expected, $result);
  4757. $result = $this->Form->select(
  4758. 'Model.multi_field',
  4759. $options,
  4760. ['multiple' => 'multiple', 'form' => 'my-form']
  4761. );
  4762. $this->assertHtml($expected, $result);
  4763. }
  4764. /**
  4765. * testCheckboxZeroValue method
  4766. *
  4767. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4768. *
  4769. * @return void
  4770. */
  4771. public function testCheckboxZeroValue()
  4772. {
  4773. $result = $this->Form->control('User.get_spam', [
  4774. 'type' => 'checkbox',
  4775. 'value' => '0',
  4776. 'hiddenField' => '1',
  4777. ]);
  4778. $expected = [
  4779. 'div' => ['class' => 'input checkbox'],
  4780. 'label' => ['for' => 'user-get-spam'],
  4781. ['input' => [
  4782. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4783. 'value' => '1'
  4784. ]],
  4785. ['input' => [
  4786. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4787. 'value' => '0', 'id' => 'user-get-spam'
  4788. ]],
  4789. 'Get Spam',
  4790. '/label',
  4791. '/div'
  4792. ];
  4793. $this->assertHtml($expected, $result);
  4794. }
  4795. /**
  4796. * testHabtmSelectBox method
  4797. *
  4798. * Test generation of habtm select boxes.
  4799. *
  4800. * @return void
  4801. */
  4802. public function testHabtmSelectBox()
  4803. {
  4804. $this->loadFixtures('Articles');
  4805. $options = [
  4806. 1 => 'blue',
  4807. 2 => 'red',
  4808. 3 => 'green'
  4809. ];
  4810. $tags = [
  4811. new Entity(['id' => 1, 'name' => 'blue']),
  4812. new Entity(['id' => 3, 'name' => 'green'])
  4813. ];
  4814. $article = new Article(['tags' => $tags]);
  4815. $this->Form->create($article);
  4816. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4817. $expected = [
  4818. 'div' => ['class' => 'input select'],
  4819. 'label' => ['for' => 'tags-ids'],
  4820. 'Tags',
  4821. '/label',
  4822. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4823. 'select' => [
  4824. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4825. 'multiple' => 'multiple'
  4826. ],
  4827. ['option' => ['value' => '1', 'selected' => 'selected']],
  4828. 'blue',
  4829. '/option',
  4830. ['option' => ['value' => '2']],
  4831. 'red',
  4832. '/option',
  4833. ['option' => ['value' => '3', 'selected' => 'selected']],
  4834. 'green',
  4835. '/option',
  4836. '/select',
  4837. '/div'
  4838. ];
  4839. $this->assertHtml($expected, $result);
  4840. // make sure only 50 is selected, and not 50f5c0cf
  4841. $options = [
  4842. '1' => 'blue',
  4843. '50f5c0cf' => 'red',
  4844. '50' => 'green'
  4845. ];
  4846. $tags = [
  4847. new Entity(['id' => 1, 'name' => 'blue']),
  4848. new Entity(['id' => 50, 'name' => 'green'])
  4849. ];
  4850. $article = new Article(['tags' => $tags]);
  4851. $this->Form->create($article);
  4852. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4853. $expected = [
  4854. 'div' => ['class' => 'input select'],
  4855. 'label' => ['for' => 'tags-ids'],
  4856. 'Tags',
  4857. '/label',
  4858. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4859. 'select' => [
  4860. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4861. 'multiple' => 'multiple'
  4862. ],
  4863. ['option' => ['value' => '1', 'selected' => 'selected']],
  4864. 'blue',
  4865. '/option',
  4866. ['option' => ['value' => '50f5c0cf']],
  4867. 'red',
  4868. '/option',
  4869. ['option' => ['value' => '50', 'selected' => 'selected']],
  4870. 'green',
  4871. '/option',
  4872. '/select',
  4873. '/div'
  4874. ];
  4875. $this->assertHtml($expected, $result);
  4876. $spacecraft = [
  4877. 1 => 'Orion',
  4878. 2 => 'Helios'
  4879. ];
  4880. $this->View->viewVars['spacecraft'] = $spacecraft;
  4881. $this->Form->create();
  4882. $result = $this->Form->control('spacecraft._ids');
  4883. $expected = [
  4884. 'div' => ['class' => 'input select'],
  4885. 'label' => ['for' => 'spacecraft-ids'],
  4886. 'Spacecraft',
  4887. '/label',
  4888. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4889. 'select' => [
  4890. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4891. 'multiple' => 'multiple'
  4892. ],
  4893. ['option' => ['value' => '1']],
  4894. 'Orion',
  4895. '/option',
  4896. ['option' => ['value' => '2']],
  4897. 'Helios',
  4898. '/option',
  4899. '/select',
  4900. '/div'
  4901. ];
  4902. $this->assertHtml($expected, $result);
  4903. }
  4904. /**
  4905. * testErrorsForBelongsToManySelect method
  4906. *
  4907. * Tests that errors for belongsToMany select fields are being
  4908. * picked up properly.
  4909. *
  4910. * @return void
  4911. */
  4912. public function testErrorsForBelongsToManySelect()
  4913. {
  4914. $spacecraft = [
  4915. 1 => 'Orion',
  4916. 2 => 'Helios'
  4917. ];
  4918. $this->View->viewVars['spacecraft'] = $spacecraft;
  4919. $article = new Article();
  4920. $article->errors('spacecraft', ['Invalid']);
  4921. $this->Form->create($article);
  4922. $result = $this->Form->control('spacecraft._ids');
  4923. $expected = [
  4924. ['div' => ['class' => 'input select error']],
  4925. 'label' => ['for' => 'spacecraft-ids'],
  4926. 'Spacecraft',
  4927. '/label',
  4928. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4929. 'select' => [
  4930. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4931. 'multiple' => 'multiple'
  4932. ],
  4933. ['option' => ['value' => '1']],
  4934. 'Orion',
  4935. '/option',
  4936. ['option' => ['value' => '2']],
  4937. 'Helios',
  4938. '/option',
  4939. '/select',
  4940. ['div' => ['class' => 'error-message']],
  4941. 'Invalid',
  4942. '/div',
  4943. '/div'
  4944. ];
  4945. $this->assertHtml($expected, $result);
  4946. }
  4947. /**
  4948. * testSelectMultipleCheckboxes method
  4949. *
  4950. * Test generation of multi select elements in checkbox format.
  4951. *
  4952. * @return void
  4953. */
  4954. public function testSelectMultipleCheckboxes()
  4955. {
  4956. $result = $this->Form->select(
  4957. 'Model.multi_field',
  4958. ['first', 'second', 'third'],
  4959. ['multiple' => 'checkbox']
  4960. );
  4961. $expected = [
  4962. 'input' => [
  4963. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4964. ],
  4965. ['div' => ['class' => 'checkbox']],
  4966. ['label' => ['for' => 'model-multi-field-0']],
  4967. ['input' => [
  4968. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4969. 'value' => '0', 'id' => 'model-multi-field-0'
  4970. ]],
  4971. 'first',
  4972. '/label',
  4973. '/div',
  4974. ['div' => ['class' => 'checkbox']],
  4975. ['label' => ['for' => 'model-multi-field-1']],
  4976. ['input' => [
  4977. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4978. 'value' => '1', 'id' => 'model-multi-field-1'
  4979. ]],
  4980. 'second',
  4981. '/label',
  4982. '/div',
  4983. ['div' => ['class' => 'checkbox']],
  4984. ['label' => ['for' => 'model-multi-field-2']],
  4985. ['input' => [
  4986. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4987. 'value' => '2', 'id' => 'model-multi-field-2'
  4988. ]],
  4989. 'third',
  4990. '/label',
  4991. '/div'
  4992. ];
  4993. $this->assertHtml($expected, $result);
  4994. $result = $this->Form->select(
  4995. 'Model.multi_field',
  4996. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4997. ['multiple' => 'checkbox']
  4998. );
  4999. $expected = [
  5000. 'input' => [
  5001. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5002. ],
  5003. ['div' => ['class' => 'checkbox']],
  5004. ['label' => ['for' => 'model-multi-field-a+']],
  5005. ['input' => [
  5006. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5007. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5008. ]],
  5009. 'first',
  5010. '/label',
  5011. '/div',
  5012. ['div' => ['class' => 'checkbox']],
  5013. ['label' => ['for' => 'model-multi-field-a++']],
  5014. ['input' => [
  5015. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5016. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5017. ]],
  5018. 'second',
  5019. '/label',
  5020. '/div',
  5021. ['div' => ['class' => 'checkbox']],
  5022. ['label' => ['for' => 'model-multi-field-a+++']],
  5023. ['input' => [
  5024. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5025. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5026. ]],
  5027. 'third',
  5028. '/label',
  5029. '/div'
  5030. ];
  5031. $this->assertHtml($expected, $result);
  5032. $result = $this->Form->select(
  5033. 'Model.multi_field',
  5034. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5035. ['multiple' => 'checkbox']
  5036. );
  5037. $expected = [
  5038. 'input' => [
  5039. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5040. ],
  5041. ['div' => ['class' => 'checkbox']],
  5042. ['label' => ['for' => 'model-multi-field-a-b']],
  5043. ['input' => [
  5044. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5045. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5046. ]],
  5047. 'first',
  5048. '/label',
  5049. '/div',
  5050. ['div' => ['class' => 'checkbox']],
  5051. ['label' => ['for' => 'model-multi-field-a-b1']],
  5052. ['input' => [
  5053. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5054. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5055. ]],
  5056. 'second',
  5057. '/label',
  5058. '/div',
  5059. ['div' => ['class' => 'checkbox']],
  5060. ['label' => ['for' => 'model-multi-field-a-b2']],
  5061. ['input' => [
  5062. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5063. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5064. ]],
  5065. 'third',
  5066. '/label',
  5067. '/div'
  5068. ];
  5069. $this->assertHtml($expected, $result);
  5070. }
  5071. /**
  5072. * testSelectMultipleCheckboxRequestData method
  5073. *
  5074. * Ensure that multiCheckbox reads from the request data.
  5075. *
  5076. * @return void
  5077. */
  5078. public function testSelectMultipleCheckboxRequestData()
  5079. {
  5080. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5081. $result = $this->Form->select(
  5082. 'Model.tags',
  5083. ['1' => 'first', 'Array' => 'Array'],
  5084. ['multiple' => 'checkbox']
  5085. );
  5086. $expected = [
  5087. 'input' => [
  5088. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5089. ],
  5090. ['div' => ['class' => 'checkbox']],
  5091. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5092. ['input' => [
  5093. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5094. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5095. ]],
  5096. 'first',
  5097. '/label',
  5098. '/div',
  5099. ['div' => ['class' => 'checkbox']],
  5100. ['label' => ['for' => 'model-tags-array']],
  5101. ['input' => [
  5102. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5103. 'value' => 'Array', 'id' => 'model-tags-array'
  5104. ]],
  5105. 'Array',
  5106. '/label',
  5107. '/div'
  5108. ];
  5109. $this->assertHtml($expected, $result);
  5110. }
  5111. /**
  5112. * testSelectMultipleCheckboxSecurity method
  5113. *
  5114. * Checks the security hash array generated for multiple-input checkbox elements.
  5115. *
  5116. * @return void
  5117. */
  5118. public function testSelectMultipleCheckboxSecurity()
  5119. {
  5120. $this->Form->request->params['_Token'] = 'testKey';
  5121. $this->assertEquals([], $this->Form->fields);
  5122. $this->Form->select(
  5123. 'Model.multi_field',
  5124. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5125. ['multiple' => 'checkbox']
  5126. );
  5127. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5128. $result = $this->Form->secure($this->Form->fields);
  5129. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5130. $this->assertRegExp('/"' . $key . '"/', $result);
  5131. }
  5132. /**
  5133. * testSelectMultipleSecureWithNoOptions method
  5134. *
  5135. * Multiple select elements should always be secured as they always participate
  5136. * in the POST data.
  5137. *
  5138. * @return void
  5139. */
  5140. public function testSelectMultipleSecureWithNoOptions()
  5141. {
  5142. $this->assertEquals([], $this->Form->fields);
  5143. $this->Form->select(
  5144. 'Model.select',
  5145. [],
  5146. ['multiple' => true]
  5147. );
  5148. $this->assertEquals(['Model.select'], $this->Form->fields);
  5149. }
  5150. /**
  5151. * testSelectNoSecureWithNoOptions method
  5152. *
  5153. * When a select box has no options it should not be added to the fields list
  5154. * as it always fail post validation.
  5155. *
  5156. * @return void
  5157. */
  5158. public function testSelectNoSecureWithNoOptions()
  5159. {
  5160. $this->Form->request->params['_Token'] = 'testkey';
  5161. $this->assertEquals([], $this->Form->fields);
  5162. $this->Form->select(
  5163. 'Model.select',
  5164. []
  5165. );
  5166. $this->assertEquals([], $this->Form->fields);
  5167. $this->Form->select(
  5168. 'Model.user_id',
  5169. [],
  5170. ['empty' => true]
  5171. );
  5172. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5173. }
  5174. /**
  5175. * testControlMultipleCheckboxes method
  5176. *
  5177. * Test control() resulting in multi select elements being generated.
  5178. *
  5179. * @return void
  5180. */
  5181. public function testControlMultipleCheckboxes()
  5182. {
  5183. $result = $this->Form->control('Model.multi_field', [
  5184. 'options' => ['first', 'second', 'third'],
  5185. 'multiple' => 'checkbox'
  5186. ]);
  5187. $expected = [
  5188. ['div' => ['class' => 'input select']],
  5189. ['label' => ['for' => 'model-multi-field']],
  5190. 'Multi Field',
  5191. '/label',
  5192. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5193. ['div' => ['class' => 'checkbox']],
  5194. ['label' => ['for' => 'model-multi-field-0']],
  5195. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5196. 'first',
  5197. '/label',
  5198. '/div',
  5199. ['div' => ['class' => 'checkbox']],
  5200. ['label' => ['for' => 'model-multi-field-1']],
  5201. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5202. 'second',
  5203. '/label',
  5204. '/div',
  5205. ['div' => ['class' => 'checkbox']],
  5206. ['label' => ['for' => 'model-multi-field-2']],
  5207. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5208. 'third',
  5209. '/label',
  5210. '/div',
  5211. '/div'
  5212. ];
  5213. $this->assertHtml($expected, $result);
  5214. $result = $this->Form->control('Model.multi_field', [
  5215. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5216. 'multiple' => 'checkbox'
  5217. ]);
  5218. $expected = [
  5219. ['div' => ['class' => 'input select']],
  5220. ['label' => ['for' => 'model-multi-field']],
  5221. 'Multi Field',
  5222. '/label',
  5223. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5224. ['div' => ['class' => 'checkbox']],
  5225. ['label' => ['for' => 'model-multi-field-a']],
  5226. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5227. 'first',
  5228. '/label',
  5229. '/div',
  5230. ['div' => ['class' => 'checkbox']],
  5231. ['label' => ['for' => 'model-multi-field-b']],
  5232. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5233. 'second',
  5234. '/label',
  5235. '/div',
  5236. ['div' => ['class' => 'checkbox']],
  5237. ['label' => ['for' => 'model-multi-field-c']],
  5238. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5239. 'third',
  5240. '/label',
  5241. '/div',
  5242. '/div'
  5243. ];
  5244. $this->assertHtml($expected, $result);
  5245. }
  5246. /**
  5247. * testSelectHiddenFieldOmission method
  5248. *
  5249. * Test that select() with 'hiddenField' => false omits the hidden field.
  5250. *
  5251. * @return void
  5252. */
  5253. public function testSelectHiddenFieldOmission()
  5254. {
  5255. $result = $this->Form->select(
  5256. 'Model.multi_field',
  5257. ['first', 'second'],
  5258. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5259. );
  5260. $this->assertNotContains('type="hidden"', $result);
  5261. }
  5262. /**
  5263. * testSelectCheckboxMultipleOverrideName method
  5264. *
  5265. * Test that select() with multiple = checkbox works with overriding name attribute.
  5266. *
  5267. * @return void
  5268. */
  5269. public function testSelectCheckboxMultipleOverrideName()
  5270. {
  5271. $result = $this->Form->select('category', ['1', '2'], [
  5272. 'multiple' => 'checkbox',
  5273. 'name' => 'fish',
  5274. ]);
  5275. $expected = [
  5276. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5277. ['div' => ['class' => 'checkbox']],
  5278. ['label' => ['for' => 'fish-0']],
  5279. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5280. '1',
  5281. '/label',
  5282. '/div',
  5283. ['div' => ['class' => 'checkbox']],
  5284. ['label' => ['for' => 'fish-1']],
  5285. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5286. '2',
  5287. '/label',
  5288. '/div'
  5289. ];
  5290. $this->assertHtml($expected, $result);
  5291. $result = $this->Form->multiCheckbox(
  5292. 'category',
  5293. new Collection(['1', '2']),
  5294. ['name' => 'fish']
  5295. );
  5296. $this->assertHtml($expected, $result);
  5297. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5298. 'name' => 'fish',
  5299. ]);
  5300. $this->assertHtml($expected, $result);
  5301. }
  5302. /**
  5303. * testControlMultiCheckbox method
  5304. *
  5305. * Test that control() works with multicheckbox.
  5306. *
  5307. * @return void
  5308. */
  5309. public function testControlMultiCheckbox()
  5310. {
  5311. $result = $this->Form->control('category', [
  5312. 'type' => 'multicheckbox',
  5313. 'options' => ['1', '2'],
  5314. ]);
  5315. $expected = [
  5316. ['div' => ['class' => 'input multicheckbox']],
  5317. '<label',
  5318. 'Category',
  5319. '/label',
  5320. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5321. ['div' => ['class' => 'checkbox']],
  5322. ['label' => ['for' => 'category-0']],
  5323. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5324. '1',
  5325. '/label',
  5326. '/div',
  5327. ['div' => ['class' => 'checkbox']],
  5328. ['label' => ['for' => 'category-1']],
  5329. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5330. '2',
  5331. '/label',
  5332. '/div',
  5333. '/div',
  5334. ];
  5335. $this->assertHtml($expected, $result);
  5336. }
  5337. /**
  5338. * testCheckbox method
  5339. *
  5340. * Test generation of checkboxes.
  5341. *
  5342. * @return void
  5343. */
  5344. public function testCheckbox()
  5345. {
  5346. $result = $this->Form->checkbox('Model.field');
  5347. $expected = [
  5348. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5349. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5350. ];
  5351. $this->assertHtml($expected, $result);
  5352. $result = $this->Form->checkbox('Model.field', [
  5353. 'id' => 'theID',
  5354. 'value' => 'myvalue',
  5355. 'form' => 'my-form',
  5356. ]);
  5357. $expected = [
  5358. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5359. ['input' => [
  5360. 'type' => 'checkbox', 'name' => 'Model[field]',
  5361. 'value' => 'myvalue', 'id' => 'theID',
  5362. 'form' => 'my-form',
  5363. ]]
  5364. ];
  5365. $this->assertHtml($expected, $result);
  5366. }
  5367. /**
  5368. * testCheckboxDefaultValue method
  5369. *
  5370. * Test default value setting on checkbox() method.
  5371. *
  5372. * @return void
  5373. */
  5374. public function testCheckboxDefaultValue()
  5375. {
  5376. $this->Form->request->data['Model']['field'] = false;
  5377. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5378. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5379. $this->assertHtml($expected, $result);
  5380. unset($this->Form->request->data['Model']['field']);
  5381. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5382. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5383. $this->assertHtml($expected, $result);
  5384. $this->Form->request->data['Model']['field'] = true;
  5385. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5386. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5387. $this->assertHtml($expected, $result);
  5388. unset($this->Form->request->data['Model']['field']);
  5389. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5390. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5391. $this->assertHtml($expected, $result);
  5392. $Articles = TableRegistry::get('Articles');
  5393. $Articles->schema()->addColumn(
  5394. 'published',
  5395. ['type' => 'boolean', 'null' => false, 'default' => true]
  5396. );
  5397. $this->Form->create($Articles->newEntity());
  5398. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5399. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5400. $this->assertHtml($expected, $result);
  5401. }
  5402. /**
  5403. * testCheckboxCheckedAndError method
  5404. *
  5405. * Test checkbox being checked or having errors.
  5406. *
  5407. * @return void
  5408. */
  5409. public function testCheckboxCheckedAndError()
  5410. {
  5411. $this->article['errors'] = [
  5412. 'published' => true
  5413. ];
  5414. $this->Form->request->data['published'] = 'myvalue';
  5415. $this->Form->create($this->article);
  5416. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5417. $expected = [
  5418. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5419. ['input' => [
  5420. 'type' => 'checkbox',
  5421. 'name' => 'published',
  5422. 'value' => 'myvalue',
  5423. 'id' => 'theID',
  5424. 'checked' => 'checked',
  5425. 'class' => 'form-error'
  5426. ]]
  5427. ];
  5428. $this->assertHtml($expected, $result);
  5429. $this->Form->request->data['published'] = '';
  5430. $result = $this->Form->checkbox('published');
  5431. $expected = [
  5432. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5433. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5434. ];
  5435. $this->assertHtml($expected, $result);
  5436. }
  5437. /**
  5438. * testCheckboxCustomNameAttribute method
  5439. *
  5440. * Test checkbox() with a custom name attribute.
  5441. *
  5442. * @return void
  5443. */
  5444. public function testCheckboxCustomNameAttribute()
  5445. {
  5446. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5447. $expected = [
  5448. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5449. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5450. ];
  5451. $this->assertHtml($expected, $result);
  5452. }
  5453. /**
  5454. * testCheckboxHiddenField method
  5455. *
  5456. * Test that the hidden input for checkboxes can be omitted or set to a
  5457. * specific value.
  5458. *
  5459. * @return void
  5460. */
  5461. public function testCheckboxHiddenField()
  5462. {
  5463. $result = $this->Form->checkbox('UserForm.something', [
  5464. 'hiddenField' => false
  5465. ]);
  5466. $expected = [
  5467. 'input' => [
  5468. 'type' => 'checkbox',
  5469. 'name' => 'UserForm[something]',
  5470. 'value' => '1'
  5471. ],
  5472. ];
  5473. $this->assertHtml($expected, $result);
  5474. $result = $this->Form->checkbox('UserForm.something', [
  5475. 'value' => 'Y',
  5476. 'hiddenField' => 'N',
  5477. ]);
  5478. $expected = [
  5479. ['input' => [
  5480. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5481. 'value' => 'N'
  5482. ]],
  5483. ['input' => [
  5484. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5485. 'value' => 'Y'
  5486. ]],
  5487. ];
  5488. $this->assertHtml($expected, $result);
  5489. }
  5490. /**
  5491. * testTime method
  5492. *
  5493. * Test the time type.
  5494. *
  5495. * @return void
  5496. */
  5497. public function testTime()
  5498. {
  5499. $result = $this->Form->time('start_time', [
  5500. 'timeFormat' => 12,
  5501. 'interval' => 5,
  5502. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5503. ]);
  5504. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5505. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5506. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5507. $this->assertNotContains('year', $result);
  5508. $this->assertNotContains('month', $result);
  5509. $this->assertNotContains('day', $result);
  5510. $result = $this->Form->time('start_time', [
  5511. 'timeFormat' => 12,
  5512. 'interval' => 5,
  5513. 'value' => '2014-03-08 16:30:00'
  5514. ]);
  5515. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5516. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5517. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5518. $this->assertNotContains('year', $result);
  5519. $this->assertNotContains('month', $result);
  5520. $this->assertNotContains('day', $result);
  5521. }
  5522. /**
  5523. * testTimeFormat24NoMeridian method
  5524. *
  5525. * Ensure that timeFormat=24 has no merdian.
  5526. *
  5527. * @return void.
  5528. */
  5529. public function testTimeFormat24NoMeridian()
  5530. {
  5531. $result = $this->Form->time('start_time', [
  5532. 'timeFormat' => 24,
  5533. 'interval' => 5,
  5534. 'value' => '2014-03-08 16:30:00'
  5535. ]);
  5536. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5537. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5538. $this->assertNotContains('meridian', $result);
  5539. $this->assertNotContains('pm', $result);
  5540. $this->assertNotContains('year', $result);
  5541. $this->assertNotContains('month', $result);
  5542. $this->assertNotContains('day', $result);
  5543. }
  5544. /**
  5545. * testDate method
  5546. *
  5547. * Test the date type.
  5548. *
  5549. * @return void
  5550. */
  5551. public function testDate()
  5552. {
  5553. $result = $this->Form->date('start_day', [
  5554. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5555. ]);
  5556. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5557. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5558. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5559. $this->assertNotContains('hour', $result);
  5560. $this->assertNotContains('minute', $result);
  5561. $this->assertNotContains('second', $result);
  5562. $this->assertNotContains('meridian', $result);
  5563. }
  5564. /**
  5565. * testDateTime method
  5566. *
  5567. * Test generation of date/time select elements.
  5568. *
  5569. * @return void
  5570. */
  5571. public function testDateTime()
  5572. {
  5573. extract($this->dateRegex);
  5574. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5575. $now = strtotime('now');
  5576. $expected = [
  5577. ['select' => ['name' => 'Contact[date][year]']],
  5578. ['option' => ['value' => '']],
  5579. '/option',
  5580. $yearsRegex,
  5581. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5582. date('Y', $now),
  5583. '/option',
  5584. '*/select',
  5585. ['select' => ['name' => 'Contact[date][month]']],
  5586. ['option' => ['value' => '']],
  5587. '/option',
  5588. $monthsRegex,
  5589. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5590. date('F', $now),
  5591. '/option',
  5592. '*/select',
  5593. ['select' => ['name' => 'Contact[date][day]']],
  5594. ['option' => ['value' => '']],
  5595. '/option',
  5596. $daysRegex,
  5597. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5598. date('j', $now),
  5599. '/option',
  5600. '*/select',
  5601. ['select' => ['name' => 'Contact[date][hour]']],
  5602. ['option' => ['value' => '']],
  5603. '/option',
  5604. $hoursRegex,
  5605. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5606. date('G', $now),
  5607. '/option',
  5608. '*/select',
  5609. ['select' => ['name' => 'Contact[date][minute]']],
  5610. ['option' => ['value' => '']],
  5611. '/option',
  5612. $minutesRegex,
  5613. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5614. date('i', $now),
  5615. '/option',
  5616. '*/select',
  5617. ];
  5618. $this->assertHtml($expected, $result);
  5619. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5620. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5621. $now = strtotime('now');
  5622. $expected = [
  5623. ['select' => ['name' => 'Contact[date][year]']],
  5624. $yearsRegex,
  5625. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5626. date('Y', $now),
  5627. '/option',
  5628. '*/select',
  5629. ['select' => ['name' => 'Contact[date][month]']],
  5630. $monthsRegex,
  5631. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5632. date('F', $now),
  5633. '/option',
  5634. '*/select',
  5635. ['select' => ['name' => 'Contact[date][day]']],
  5636. $daysRegex,
  5637. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5638. date('j', $now),
  5639. '/option',
  5640. '*/select',
  5641. ['select' => ['name' => 'Contact[date][hour]']],
  5642. $hoursRegex,
  5643. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5644. date('G', $now),
  5645. '/option',
  5646. '*/select',
  5647. ['select' => ['name' => 'Contact[date][minute]']],
  5648. $minutesRegex,
  5649. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5650. date('i', $now),
  5651. '/option',
  5652. '*/select',
  5653. ];
  5654. $this->assertHtml($expected, $result);
  5655. }
  5656. /**
  5657. * testDateTimeSecured method
  5658. *
  5659. * Test that datetime fields are added to protected fields list.
  5660. *
  5661. * @return void
  5662. */
  5663. public function testDateTimeSecured()
  5664. {
  5665. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5666. $this->Form->dateTime('Contact.date');
  5667. $expected = [
  5668. 'Contact.date.year',
  5669. 'Contact.date.month',
  5670. 'Contact.date.day',
  5671. 'Contact.date.hour',
  5672. 'Contact.date.minute',
  5673. ];
  5674. $this->assertEquals($expected, $this->Form->fields);
  5675. $this->Form->fields = [];
  5676. $this->Form->date('Contact.published');
  5677. $expected = [
  5678. 'Contact.published.year',
  5679. 'Contact.published.month',
  5680. 'Contact.published.day',
  5681. ];
  5682. $this->assertEquals($expected, $this->Form->fields);
  5683. }
  5684. /**
  5685. * testDateTimeSecuredDisabled method
  5686. *
  5687. * Test that datetime fields are added to protected fields list.
  5688. *
  5689. * @return void
  5690. */
  5691. public function testDateTimeSecuredDisabled()
  5692. {
  5693. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5694. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5695. $expected = [];
  5696. $this->assertEquals($expected, $this->Form->fields);
  5697. $this->Form->fields = [];
  5698. $this->Form->date('Contact.published', ['secure' => false]);
  5699. $expected = [];
  5700. $this->assertEquals($expected, $this->Form->fields);
  5701. }
  5702. /**
  5703. * testDatetimeEmpty method
  5704. *
  5705. * Test empty defaulting to true for datetime.
  5706. *
  5707. * @return void
  5708. */
  5709. public function testDatetimeEmpty()
  5710. {
  5711. extract($this->dateRegex);
  5712. $result = $this->Form->dateTime('Contact.date', [
  5713. 'timeFormat' => 12,
  5714. 'empty' => true,
  5715. 'default' => true
  5716. ]);
  5717. $expected = [
  5718. ['select' => ['name' => 'Contact[date][year]']],
  5719. $yearsRegex,
  5720. ['option' => ['value' => '']],
  5721. '/option',
  5722. '*/select',
  5723. ['select' => ['name' => 'Contact[date][month]']],
  5724. $monthsRegex,
  5725. ['option' => ['value' => '']],
  5726. '/option',
  5727. '*/select',
  5728. ['select' => ['name' => 'Contact[date][day]']],
  5729. $daysRegex,
  5730. ['option' => ['value' => '']],
  5731. '/option',
  5732. '*/select',
  5733. ['select' => ['name' => 'Contact[date][hour]']],
  5734. $hoursRegex,
  5735. ['option' => ['value' => '']],
  5736. '/option',
  5737. '*/select',
  5738. ['select' => ['name' => 'Contact[date][minute]']],
  5739. $minutesRegex,
  5740. ['option' => ['value' => '']],
  5741. '/option',
  5742. '*/select',
  5743. ['select' => ['name' => 'Contact[date][meridian]']],
  5744. $meridianRegex,
  5745. ['option' => ['value' => '']],
  5746. '/option',
  5747. '*/select'
  5748. ];
  5749. $this->assertHtml($expected, $result);
  5750. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5751. }
  5752. /**
  5753. * testDatetimeMinuteInterval method
  5754. *
  5755. * Test datetime with interval option.
  5756. *
  5757. * @return void
  5758. */
  5759. public function testDatetimeMinuteInterval()
  5760. {
  5761. extract($this->dateRegex);
  5762. $result = $this->Form->dateTime('Contact.date', [
  5763. 'interval' => 5,
  5764. 'value' => ''
  5765. ]);
  5766. $expected = [
  5767. ['select' => ['name' => 'Contact[date][year]']],
  5768. $yearsRegex,
  5769. ['option' => ['selected' => 'selected', 'value' => '']],
  5770. '/option',
  5771. '*/select',
  5772. ['select' => ['name' => 'Contact[date][month]']],
  5773. $monthsRegex,
  5774. ['option' => ['selected' => 'selected', 'value' => '']],
  5775. '/option',
  5776. '*/select',
  5777. ['select' => ['name' => 'Contact[date][day]']],
  5778. $daysRegex,
  5779. ['option' => ['selected' => 'selected', 'value' => '']],
  5780. '/option',
  5781. '*/select',
  5782. ['select' => ['name' => 'Contact[date][hour]']],
  5783. $hoursRegex,
  5784. ['option' => ['selected' => 'selected', 'value' => '']],
  5785. '/option',
  5786. '*/select',
  5787. ['select' => ['name' => 'Contact[date][minute]']],
  5788. $minutesRegex,
  5789. ['option' => ['selected' => 'selected', 'value' => '']],
  5790. '/option',
  5791. ['option' => ['value' => '00']],
  5792. '00',
  5793. '/option',
  5794. ['option' => ['value' => '05']],
  5795. '05',
  5796. '/option',
  5797. ['option' => ['value' => '10']],
  5798. '10',
  5799. '/option',
  5800. '*/select',
  5801. ];
  5802. $this->assertHtml($expected, $result);
  5803. }
  5804. /**
  5805. * testDateTimeRounding method
  5806. *
  5807. * Test dateTime with rounding.
  5808. *
  5809. * @return void
  5810. */
  5811. public function testDateTimeRounding()
  5812. {
  5813. $this->Form->request->data['Contact'] = [
  5814. 'date' => [
  5815. 'day' => '13',
  5816. 'month' => '12',
  5817. 'year' => '2010',
  5818. 'hour' => '04',
  5819. 'minute' => '19',
  5820. 'meridian' => 'AM'
  5821. ]
  5822. ];
  5823. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5824. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5825. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5826. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5827. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5828. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5829. }
  5830. /**
  5831. * testDatetimeWithDefault method
  5832. *
  5833. * Test that datetime() and default values work.
  5834. *
  5835. * @return void
  5836. */
  5837. public function testDatetimeWithDefault()
  5838. {
  5839. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5840. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5841. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5842. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5843. $result = $this->Form->dateTime('Contact.updated', [
  5844. 'default' => '2009-06-01 11:15:30'
  5845. ]);
  5846. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5847. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5848. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5849. }
  5850. /**
  5851. * testDateTimeAllZeros method
  5852. *
  5853. * Test datetime() with all zeros.
  5854. *
  5855. * @return void
  5856. */
  5857. public function testDateTimeAllZeros()
  5858. {
  5859. $result = $this->Form->dateTime('Contact.date', [
  5860. 'timeFormat' => false,
  5861. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5862. 'value' => '0000-00-00'
  5863. ]);
  5864. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5865. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5866. }
  5867. /**
  5868. * testDateTimeEmptyAsArray method
  5869. *
  5870. * @return void
  5871. */
  5872. public function testDateTimeEmptyAsArray()
  5873. {
  5874. $result = $this->Form->dateTime('Contact.date', [
  5875. 'empty' => [
  5876. 'day' => 'DAY',
  5877. 'month' => 'MONTH',
  5878. 'year' => 'YEAR',
  5879. 'hour' => 'HOUR',
  5880. 'minute' => 'MINUTE',
  5881. 'meridian' => false
  5882. ],
  5883. 'default' => true
  5884. ]);
  5885. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5886. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5887. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5888. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5889. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5890. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5891. $result = $this->Form->dateTime('Contact.date', [
  5892. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5893. 'default' => true
  5894. ]);
  5895. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5896. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5897. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5898. }
  5899. /**
  5900. * testFormDateTimeMulti method
  5901. *
  5902. * Test multiple datetime element generation.
  5903. *
  5904. * @return void
  5905. */
  5906. public function testFormDateTimeMulti()
  5907. {
  5908. extract($this->dateRegex);
  5909. $result = $this->Form->dateTime('Contact.1.updated');
  5910. $this->assertContains('Contact[1][updated][month]', $result);
  5911. $this->assertContains('Contact[1][updated][day]', $result);
  5912. $this->assertContains('Contact[1][updated][year]', $result);
  5913. $this->assertContains('Contact[1][updated][hour]', $result);
  5914. $this->assertContains('Contact[1][updated][minute]', $result);
  5915. }
  5916. /**
  5917. * testDateTimeLabelIdMatchesFirstControl method
  5918. *
  5919. * When changing the date format, the label should always focus the first select box when
  5920. * clicked.
  5921. *
  5922. * @return void
  5923. */
  5924. public function testDateTimeLabelIdMatchesFirstControl()
  5925. {
  5926. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5927. $this->assertContains('<label>Date</label>', $result);
  5928. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5929. $this->assertContains('<label>Date</label>', $result);
  5930. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5931. $this->assertContains('<label>Date</label>', $result);
  5932. }
  5933. /**
  5934. * testDateTimeSecondOptions method
  5935. *
  5936. * Test datetime second=true.
  5937. *
  5938. * @return void
  5939. */
  5940. public function testDateTimeSecondOptions()
  5941. {
  5942. $result = $this->Form->dateTime('updated', ['second' => true]);
  5943. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5944. $result = $this->Form->dateTime('updated', ['second' => []]);
  5945. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5946. $result = $this->Form->dateTime('updated', ['second' => null]);
  5947. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5948. $result = $this->Form->dateTime('updated', ['second' => false]);
  5949. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5950. }
  5951. /**
  5952. * testMonth method
  5953. *
  5954. * Test generation of a month input.
  5955. *
  5956. * @return void
  5957. */
  5958. public function testMonth()
  5959. {
  5960. $result = $this->Form->month('Model.field', ['value' => '']);
  5961. $expected = [
  5962. ['select' => ['name' => 'Model[field][month]']],
  5963. ['option' => ['value' => '', 'selected' => 'selected']],
  5964. '/option',
  5965. ['option' => ['value' => '01']],
  5966. date('F', strtotime('2008-01-01 00:00:00')),
  5967. '/option',
  5968. ['option' => ['value' => '02']],
  5969. date('F', strtotime('2008-02-01 00:00:00')),
  5970. '/option',
  5971. '*/select',
  5972. ];
  5973. $this->assertHtml($expected, $result);
  5974. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5975. $expected = [
  5976. ['select' => ['name' => 'Model[field][month]']],
  5977. ['option' => ['selected' => 'selected', 'value' => '']],
  5978. '/option',
  5979. ['option' => ['value' => '01']],
  5980. date('F', strtotime('2008-01-01 00:00:00')),
  5981. '/option',
  5982. ['option' => ['value' => '02']],
  5983. date('F', strtotime('2008-02-01 00:00:00')),
  5984. '/option',
  5985. '*/select',
  5986. ];
  5987. $this->assertHtml($expected, $result);
  5988. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5989. $expected = [
  5990. ['select' => ['name' => 'Model[field][month]']],
  5991. ['option' => ['selected' => 'selected', 'value' => '']],
  5992. '/option',
  5993. ['option' => ['value' => '01']],
  5994. '1',
  5995. '/option',
  5996. ['option' => ['value' => '02']],
  5997. '2',
  5998. '/option',
  5999. '*/select',
  6000. ];
  6001. $this->assertHtml($expected, $result);
  6002. $monthNames = [
  6003. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6004. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6005. ];
  6006. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6007. $expected = [
  6008. ['select' => ['name' => 'Model[field][month]']],
  6009. ['option' => ['value' => '']],
  6010. '/option',
  6011. ['option' => ['value' => '01', 'selected' => 'selected']],
  6012. 'Jan',
  6013. '/option',
  6014. ['option' => ['value' => '02']],
  6015. 'Feb',
  6016. '/option',
  6017. '*/select',
  6018. ];
  6019. $this->assertHtml($expected, $result);
  6020. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6021. $result = $this->Form->month('Project.release');
  6022. $expected = [
  6023. ['select' => ['name' => 'Project[release][month]']],
  6024. ['option' => ['value' => '']],
  6025. '/option',
  6026. ['option' => ['value' => '01']],
  6027. 'January',
  6028. '/option',
  6029. ['option' => ['value' => '02', 'selected' => 'selected']],
  6030. 'February',
  6031. '/option',
  6032. '*/select',
  6033. ];
  6034. $this->assertHtml($expected, $result);
  6035. $result = $this->Form->month('Contact.published', [
  6036. 'empty' => 'Published on',
  6037. ]);
  6038. $this->assertContains('Published on', $result);
  6039. }
  6040. /**
  6041. * testDay method
  6042. *
  6043. * Test generation of a day input.
  6044. *
  6045. * @return void
  6046. */
  6047. public function testDay()
  6048. {
  6049. extract($this->dateRegex);
  6050. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6051. $expected = [
  6052. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6053. ['option' => ['selected' => 'selected', 'value' => '']],
  6054. '/option',
  6055. ['option' => ['value' => '01']],
  6056. '1',
  6057. '/option',
  6058. ['option' => ['value' => '02']],
  6059. '2',
  6060. '/option',
  6061. $daysRegex,
  6062. '/select',
  6063. ];
  6064. $this->assertHtml($expected, $result);
  6065. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6066. $result = $this->Form->day('Model.field');
  6067. $expected = [
  6068. ['select' => ['name' => 'Model[field][day]']],
  6069. ['option' => ['value' => '']],
  6070. '/option',
  6071. ['option' => ['value' => '01']],
  6072. '1',
  6073. '/option',
  6074. ['option' => ['value' => '02']],
  6075. '2',
  6076. '/option',
  6077. $daysRegex,
  6078. ['option' => ['value' => '10', 'selected' => 'selected']],
  6079. '10',
  6080. '/option',
  6081. $daysRegex,
  6082. '/select',
  6083. ];
  6084. $this->assertHtml($expected, $result);
  6085. $this->Form->request->data['Model']['field'] = '';
  6086. $result = $this->Form->day('Model.field', ['value' => '10']);
  6087. $expected = [
  6088. ['select' => ['name' => 'Model[field][day]']],
  6089. ['option' => ['value' => '']],
  6090. '/option',
  6091. ['option' => ['value' => '01']],
  6092. '1',
  6093. '/option',
  6094. ['option' => ['value' => '02']],
  6095. '2',
  6096. '/option',
  6097. $daysRegex,
  6098. ['option' => ['value' => '10', 'selected' => 'selected']],
  6099. '10',
  6100. '/option',
  6101. $daysRegex,
  6102. '/select',
  6103. ];
  6104. $this->assertHtml($expected, $result);
  6105. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6106. $result = $this->Form->day('Project.release');
  6107. $expected = [
  6108. ['select' => ['name' => 'Project[release][day]']],
  6109. ['option' => ['value' => '']],
  6110. '/option',
  6111. ['option' => ['value' => '01']],
  6112. '1',
  6113. '/option',
  6114. ['option' => ['value' => '02']],
  6115. '2',
  6116. '/option',
  6117. $daysRegex,
  6118. ['option' => ['value' => '10', 'selected' => 'selected']],
  6119. '10',
  6120. '/option',
  6121. $daysRegex,
  6122. '/select',
  6123. ];
  6124. $this->assertHtml($expected, $result);
  6125. $result = $this->Form->day('Contact.published', [
  6126. 'empty' => 'Published on',
  6127. ]);
  6128. $this->assertContains('Published on', $result);
  6129. }
  6130. /**
  6131. * testMinute method
  6132. *
  6133. * Test generation of a minute input.
  6134. *
  6135. * @return void
  6136. */
  6137. public function testMinute()
  6138. {
  6139. extract($this->dateRegex);
  6140. $result = $this->Form->minute('Model.field', ['value' => '']);
  6141. $expected = [
  6142. ['select' => ['name' => 'Model[field][minute]']],
  6143. ['option' => ['selected' => 'selected', 'value' => '']],
  6144. '/option',
  6145. ['option' => ['value' => '00']],
  6146. '00',
  6147. '/option',
  6148. ['option' => ['value' => '01']],
  6149. '01',
  6150. '/option',
  6151. ['option' => ['value' => '02']],
  6152. '02',
  6153. '/option',
  6154. $minutesRegex,
  6155. '/select',
  6156. ];
  6157. $this->assertHtml($expected, $result);
  6158. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6159. $result = $this->Form->minute('Model.field');
  6160. $expected = [
  6161. ['select' => ['name' => 'Model[field][minute]']],
  6162. ['option' => ['value' => '']],
  6163. '/option',
  6164. ['option' => ['value' => '00']],
  6165. '00',
  6166. '/option',
  6167. ['option' => ['value' => '01']],
  6168. '01',
  6169. '/option',
  6170. ['option' => ['value' => '02']],
  6171. '02',
  6172. '/option',
  6173. $minutesRegex,
  6174. ['option' => ['value' => '12', 'selected' => 'selected']],
  6175. '12',
  6176. '/option',
  6177. $minutesRegex,
  6178. '/select',
  6179. ];
  6180. $this->assertHtml($expected, $result);
  6181. $this->Form->request->data['Model']['field'] = '';
  6182. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6183. $expected = [
  6184. ['select' => ['name' => 'Model[field][minute]']],
  6185. ['option' => ['selected' => 'selected', 'value' => '']],
  6186. '/option',
  6187. ['option' => ['value' => '00']],
  6188. '00',
  6189. '/option',
  6190. ['option' => ['value' => '05']],
  6191. '05',
  6192. '/option',
  6193. ['option' => ['value' => '10']],
  6194. '10',
  6195. '/option',
  6196. $minutesRegex,
  6197. '/select',
  6198. ];
  6199. $this->assertHtml($expected, $result);
  6200. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6201. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6202. $expected = [
  6203. ['select' => ['name' => 'Model[field][minute]']],
  6204. ['option' => ['value' => '']],
  6205. '/option',
  6206. ['option' => ['value' => '00']],
  6207. '00',
  6208. '/option',
  6209. ['option' => ['value' => '05']],
  6210. '05',
  6211. '/option',
  6212. ['option' => ['value' => '10', 'selected' => 'selected']],
  6213. '10',
  6214. '/option',
  6215. $minutesRegex,
  6216. '/select',
  6217. ];
  6218. $this->assertHtml($expected, $result);
  6219. }
  6220. /**
  6221. * testMeridian method
  6222. *
  6223. * Test generating an input for the meridian.
  6224. *
  6225. * @return void
  6226. */
  6227. public function testMeridian()
  6228. {
  6229. extract($this->dateRegex);
  6230. $now = new \DateTime();
  6231. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6232. $expected = [
  6233. ['select' => ['name' => 'Model[field][meridian]']],
  6234. ['option' => ['value' => '']],
  6235. '/option',
  6236. $meridianRegex,
  6237. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6238. $now->format('a'),
  6239. '/option',
  6240. '*/select'
  6241. ];
  6242. $this->assertHtml($expected, $result);
  6243. }
  6244. /**
  6245. * testHour method
  6246. *
  6247. * Test generation of an hour input.
  6248. *
  6249. * @return void
  6250. */
  6251. public function testHour()
  6252. {
  6253. extract($this->dateRegex);
  6254. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6255. $expected = [
  6256. ['select' => ['name' => 'Model[field][hour]']],
  6257. ['option' => ['selected' => 'selected', 'value' => '']],
  6258. '/option',
  6259. ['option' => ['value' => '01']],
  6260. '1',
  6261. '/option',
  6262. ['option' => ['value' => '02']],
  6263. '2',
  6264. '/option',
  6265. $hoursRegex,
  6266. '/select',
  6267. ];
  6268. $this->assertHtml($expected, $result);
  6269. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6270. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6271. $expected = [
  6272. ['select' => ['name' => 'Model[field][hour]']],
  6273. ['option' => ['value' => '']],
  6274. '/option',
  6275. ['option' => ['value' => '01']],
  6276. '1',
  6277. '/option',
  6278. ['option' => ['value' => '02']],
  6279. '2',
  6280. '/option',
  6281. $hoursRegex,
  6282. ['option' => ['value' => '12', 'selected' => 'selected']],
  6283. '12',
  6284. '/option',
  6285. '/select',
  6286. ];
  6287. $this->assertHtml($expected, $result);
  6288. $this->Form->request->data['Model']['field'] = '';
  6289. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6290. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6291. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6292. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6293. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6294. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6295. $expected = [
  6296. ['select' => ['name' => 'Model[field][hour]']],
  6297. ['option' => ['value' => '']],
  6298. '/option',
  6299. ['option' => ['value' => '00', 'selected' => 'selected']],
  6300. '0',
  6301. '/option',
  6302. ['option' => ['value' => '01']],
  6303. '1',
  6304. '/option',
  6305. ['option' => ['value' => '02']],
  6306. '2',
  6307. '/option',
  6308. $hoursRegex,
  6309. '/select',
  6310. ];
  6311. $this->assertHtml($expected, $result);
  6312. unset($this->Form->request->data['Model']['field']);
  6313. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6314. $thisHour = date('H');
  6315. $optValue = date('G');
  6316. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6317. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6318. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6319. $expected = [
  6320. ['select' => ['name' => 'Model[field][hour]']],
  6321. ['option' => ['value' => '']],
  6322. '/option',
  6323. ['option' => ['value' => '00']],
  6324. '0',
  6325. '/option',
  6326. ['option' => ['value' => '01', 'selected' => 'selected']],
  6327. '1',
  6328. '/option',
  6329. ['option' => ['value' => '02']],
  6330. '2',
  6331. '/option',
  6332. $hoursRegex,
  6333. '/select',
  6334. ];
  6335. $this->assertHtml($expected, $result);
  6336. }
  6337. /**
  6338. * testYear method
  6339. *
  6340. * Test generation of a year input.
  6341. *
  6342. * @return void
  6343. */
  6344. public function testYear()
  6345. {
  6346. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6347. $expected = [
  6348. ['select' => ['name' => 'Model[field][year]']],
  6349. ['option' => ['selected' => 'selected', 'value' => '']],
  6350. '/option',
  6351. ['option' => ['value' => '2007']],
  6352. '2007',
  6353. '/option',
  6354. ['option' => ['value' => '2006']],
  6355. '2006',
  6356. '/option',
  6357. '/select',
  6358. ];
  6359. $this->assertHtml($expected, $result);
  6360. $result = $this->Form->year('Model.field', [
  6361. 'value' => '',
  6362. 'minYear' => 2006,
  6363. 'maxYear' => 2007,
  6364. 'orderYear' => 'asc'
  6365. ]);
  6366. $expected = [
  6367. ['select' => ['name' => 'Model[field][year]']],
  6368. ['option' => ['selected' => 'selected', 'value' => '']],
  6369. '/option',
  6370. ['option' => ['value' => '2006']],
  6371. '2006',
  6372. '/option',
  6373. ['option' => ['value' => '2007']],
  6374. '2007',
  6375. '/option',
  6376. '/select',
  6377. ];
  6378. $this->assertHtml($expected, $result);
  6379. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6380. $result = $this->Form->year('Contact.published', [
  6381. 'empty' => false,
  6382. 'minYear' => 2006,
  6383. 'maxYear' => 2007,
  6384. ]);
  6385. $expected = [
  6386. ['select' => ['name' => 'Contact[published][year]']],
  6387. ['option' => ['value' => '2007']],
  6388. '2007',
  6389. '/option',
  6390. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6391. '2006',
  6392. '/option',
  6393. '/select',
  6394. ];
  6395. $this->assertHtml($expected, $result);
  6396. $result = $this->Form->year('Contact.published', [
  6397. 'empty' => 'Published on',
  6398. ]);
  6399. $this->assertContains('Published on', $result);
  6400. }
  6401. /**
  6402. * testControlDatetimePreEpoch method
  6403. *
  6404. * Test minYear being prior to the unix epoch.
  6405. *
  6406. * @return void
  6407. */
  6408. public function testControlDatetimePreEpoch()
  6409. {
  6410. $start = date('Y') - 80;
  6411. $end = date('Y') - 18;
  6412. $result = $this->Form->control('birth_year', [
  6413. 'type' => 'date',
  6414. 'label' => 'Birth Year',
  6415. 'minYear' => $start,
  6416. 'maxYear' => $end,
  6417. 'month' => false,
  6418. 'day' => false,
  6419. ]);
  6420. $this->assertContains('value="' . $start . '">' . $start, $result);
  6421. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6422. $this->assertNotContains('value="00">00', $result);
  6423. }
  6424. /**
  6425. * testYearAutoExpandRange method
  6426. *
  6427. * @return void
  6428. */
  6429. public function testYearAutoExpandRange()
  6430. {
  6431. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6432. $result = $this->Form->year('User.birthday');
  6433. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6434. $result = $matches[1];
  6435. $expected = range(date('Y') + 5, 1930);
  6436. $this->assertEquals($expected, $result);
  6437. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6438. $result = $this->Form->year('Project.release');
  6439. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6440. $result = $matches[1];
  6441. $expected = range(2050, date('Y') - 5);
  6442. $this->assertEquals($expected, $result);
  6443. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6444. $result = $this->Form->year('Project.release', [
  6445. 'minYear' => 1890,
  6446. 'maxYear' => 1900
  6447. ]);
  6448. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6449. $result = $matches[1];
  6450. $expected = range(1900, 1881);
  6451. $this->assertEquals($expected, $result);
  6452. }
  6453. /**
  6454. * testControlDate method
  6455. *
  6456. * Test that control() accepts the type of date and passes options in.
  6457. *
  6458. * @return void
  6459. */
  6460. public function testControlDate()
  6461. {
  6462. $this->Form->request->data = [
  6463. 'month_year' => ['month' => date('m')],
  6464. ];
  6465. $this->Form->create($this->article);
  6466. $result = $this->Form->control('month_year', [
  6467. 'label' => false,
  6468. 'type' => 'date',
  6469. 'minYear' => 2006,
  6470. 'maxYear' => 2008
  6471. ]);
  6472. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6473. $this->assertNotContains('value="2008" selected="selected"', $result);
  6474. }
  6475. /**
  6476. * testControlLabelFalse method
  6477. *
  6478. * Test the label option being set to false.
  6479. *
  6480. * @return void
  6481. */
  6482. public function testControlLabelFalse()
  6483. {
  6484. $this->Form->create($this->article);
  6485. $result = $this->Form->control('title', ['label' => false]);
  6486. $expected = [
  6487. 'div' => ['class' => 'input text required'],
  6488. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6489. '/div'
  6490. ];
  6491. $this->assertHtml($expected, $result);
  6492. }
  6493. /**
  6494. * testControlDateMaxYear method
  6495. *
  6496. * Let's say we want to only allow users born from 2006 to 2008 to register
  6497. * This being the first signup page, we still don't have any data.
  6498. *
  6499. * @return void
  6500. */
  6501. public function testControlDateMaxYear()
  6502. {
  6503. $this->Form->request->data = [];
  6504. $this->Form->create($this->article);
  6505. $result = $this->Form->control('birthday', [
  6506. 'label' => false,
  6507. 'type' => 'date',
  6508. 'minYear' => 2006,
  6509. 'maxYear' => 2008,
  6510. 'default' => true
  6511. ]);
  6512. $this->assertContains('value="2008" selected="selected"', $result);
  6513. $this->assertContains('value="2006"', $result);
  6514. $this->assertNotContains('value="2005"', $result);
  6515. $this->assertNotContains('value="2009"', $result);
  6516. }
  6517. /**
  6518. * testTextArea method
  6519. *
  6520. * Test generation of a textarea input.
  6521. *
  6522. * @return void
  6523. */
  6524. public function testTextArea()
  6525. {
  6526. $this->Form->request->data = ['field' => 'some test data'];
  6527. $result = $this->Form->textarea('field');
  6528. $expected = [
  6529. 'textarea' => ['name' => 'field', 'rows' => 5],
  6530. 'some test data',
  6531. '/textarea',
  6532. ];
  6533. $this->assertHtml($expected, $result);
  6534. $result = $this->Form->textarea('user.bio');
  6535. $expected = [
  6536. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6537. '/textarea',
  6538. ];
  6539. $this->assertHtml($expected, $result);
  6540. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6541. $result = $this->Form->textarea('field');
  6542. $expected = [
  6543. 'textarea' => ['name' => 'field', 'rows' => 5],
  6544. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6545. '/textarea',
  6546. ];
  6547. $this->assertHtml($expected, $result);
  6548. $this->Form->request->data = [
  6549. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6550. ];
  6551. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6552. $expected = [
  6553. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6554. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6555. '/textarea',
  6556. ];
  6557. $this->assertHtml($expected, $result);
  6558. $result = $this->Form->textarea('0.OtherModel.field');
  6559. $expected = [
  6560. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6561. '/textarea'
  6562. ];
  6563. $this->assertHtml($expected, $result);
  6564. }
  6565. /**
  6566. * testTextAreaWithStupidCharacters method
  6567. *
  6568. * Test text area with non-ascii characters.
  6569. *
  6570. * @return void
  6571. */
  6572. public function testTextAreaWithStupidCharacters()
  6573. {
  6574. $result = $this->Form->textarea('Post.content', [
  6575. 'value' => 'GREAT®',
  6576. 'rows' => '15',
  6577. 'cols' => '75'
  6578. ]);
  6579. $expected = [
  6580. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6581. 'GREAT®',
  6582. '/textarea',
  6583. ];
  6584. $this->assertHtml($expected, $result);
  6585. }
  6586. /**
  6587. * testTextAreaMaxLength method
  6588. *
  6589. * Test textareas maxlength read from schema.
  6590. *
  6591. * @return void
  6592. */
  6593. public function testTextAreaMaxLength()
  6594. {
  6595. $this->Form->create([
  6596. 'schema' => [
  6597. 'stuff' => ['type' => 'string', 'length' => 10],
  6598. ]
  6599. ]);
  6600. $result = $this->Form->control('other', ['type' => 'textarea']);
  6601. $expected = [
  6602. 'div' => ['class' => 'input textarea'],
  6603. 'label' => ['for' => 'other'],
  6604. 'Other',
  6605. '/label',
  6606. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6607. '/textarea',
  6608. '/div'
  6609. ];
  6610. $this->assertHtml($expected, $result);
  6611. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6612. $expected = [
  6613. 'div' => ['class' => 'input textarea'],
  6614. 'label' => ['for' => 'stuff'],
  6615. 'Stuff',
  6616. '/label',
  6617. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6618. '/textarea',
  6619. '/div'
  6620. ];
  6621. $this->assertHtml($expected, $result);
  6622. }
  6623. /**
  6624. * testHiddenField method
  6625. *
  6626. * Test generation of a hidden input.
  6627. *
  6628. * @return void
  6629. */
  6630. public function testHiddenField()
  6631. {
  6632. $this->article['errors'] = [
  6633. 'field' => true
  6634. ];
  6635. $this->Form->request->data['field'] = 'test';
  6636. $this->Form->create($this->article);
  6637. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6638. $expected = [
  6639. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6640. $this->assertHtml($expected, $result);
  6641. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6642. $expected = [
  6643. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6644. ];
  6645. $this->assertHtml($expected, $result);
  6646. }
  6647. /**
  6648. * testFileUploadField method
  6649. *
  6650. * Test generation of a file upload input.
  6651. *
  6652. * @return void
  6653. */
  6654. public function testFileUploadField()
  6655. {
  6656. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6657. $result = $this->Form->file('Model.upload');
  6658. $this->assertHtml($expected, $result);
  6659. $this->Form->request->data['Model']['upload'] = [
  6660. 'name' => '', 'type' => '', 'tmp_name' => '',
  6661. 'error' => 4, 'size' => 0
  6662. ];
  6663. $result = $this->Form->file('Model.upload');
  6664. $this->assertHtml($expected, $result);
  6665. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6666. $result = $this->Form->file('Model.upload');
  6667. $this->assertHtml($expected, $result);
  6668. }
  6669. /**
  6670. * testFileUploadOnOtherModel method
  6671. *
  6672. * Test File upload input on a model not used in create().
  6673. *
  6674. * @return void
  6675. */
  6676. public function testFileUploadOnOtherModel()
  6677. {
  6678. $this->Form->create($this->article, ['type' => 'file']);
  6679. $result = $this->Form->file('ValidateProfile.city');
  6680. $expected = [
  6681. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6682. ];
  6683. $this->assertHtml($expected, $result);
  6684. }
  6685. /**
  6686. * testButton method
  6687. *
  6688. * Test generation of a form button.
  6689. *
  6690. * @return void
  6691. */
  6692. public function testButton()
  6693. {
  6694. $result = $this->Form->button('Hi');
  6695. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6696. $this->assertHtml($expected, $result);
  6697. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6698. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6699. $this->assertHtml($expected, $result);
  6700. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6701. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6702. $this->assertHtml($expected, $result);
  6703. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6704. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6705. $this->assertHtml($expected, $result);
  6706. $result = $this->Form->button('No type', ['type' => false]);
  6707. $expected = ['button' => [], 'No type', '/button'];
  6708. $this->assertHtml($expected, $result);
  6709. $result = $this->Form->button('Upload Text', [
  6710. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6711. 'escape' => false
  6712. ]);
  6713. $this->assertNotRegExp('/\&039/', $result);
  6714. }
  6715. /**
  6716. * testButtonUnlockedByDefault method
  6717. *
  6718. * Test that button() makes unlocked fields by default.
  6719. *
  6720. * @return void
  6721. */
  6722. public function testButtonUnlockedByDefault()
  6723. {
  6724. $this->Form->request->params['_csrfToken'] = 'secured';
  6725. $this->Form->button('Save', ['name' => 'save']);
  6726. $this->Form->button('Clear');
  6727. $result = $this->Form->unlockField();
  6728. $this->assertEquals(['save'], $result);
  6729. }
  6730. /**
  6731. * testPostButton method
  6732. *
  6733. * @return void
  6734. */
  6735. public function testPostButton()
  6736. {
  6737. $result = $this->Form->postButton('Hi', '/controller/action');
  6738. $expected = [
  6739. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6740. 'div' => ['style' => 'display:none;'],
  6741. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6742. '/div',
  6743. 'button' => ['type' => 'submit'],
  6744. 'Hi',
  6745. '/button',
  6746. '/form'
  6747. ];
  6748. $this->assertHtml($expected, $result);
  6749. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6750. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6751. }
  6752. /**
  6753. * testPostButtonMethodType method
  6754. *
  6755. * @return void
  6756. */
  6757. public function testPostButtonMethodType()
  6758. {
  6759. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6760. $expected = [
  6761. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6762. 'div' => ['style' => 'display:none;'],
  6763. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6764. '/div',
  6765. 'button' => ['type' => 'submit'],
  6766. 'Hi',
  6767. '/button',
  6768. '/form'
  6769. ];
  6770. $this->assertHtml($expected, $result);
  6771. }
  6772. /**
  6773. * testPostButtonFormOptions method
  6774. *
  6775. * @return void
  6776. */
  6777. public function testPostButtonFormOptions()
  6778. {
  6779. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6780. $expected = [
  6781. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6782. 'div' => ['style' => 'display:none;'],
  6783. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6784. '/div',
  6785. 'button' => ['type' => 'submit'],
  6786. 'Hi',
  6787. '/button',
  6788. '/form'
  6789. ];
  6790. $this->assertHtml($expected, $result);
  6791. }
  6792. /**
  6793. * testPostButtonNestedData method
  6794. *
  6795. * Test using postButton with N dimensional data.
  6796. *
  6797. * @return void
  6798. */
  6799. public function testPostButtonNestedData()
  6800. {
  6801. $data = [
  6802. 'one' => [
  6803. 'two' => [
  6804. 3, 4, 5
  6805. ]
  6806. ]
  6807. ];
  6808. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6809. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6810. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6811. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6812. }
  6813. /**
  6814. * testSecurePostButton method
  6815. *
  6816. * Test that postButton adds _Token fields.
  6817. *
  6818. * @return void
  6819. */
  6820. public function testSecurePostButton()
  6821. {
  6822. $this->Form->request->params['_csrfToken'] = 'testkey';
  6823. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6824. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6825. $tokenDebug = urlencode(json_encode([
  6826. '/posts/delete/1',
  6827. [],
  6828. []
  6829. ]));
  6830. $expected = [
  6831. 'form' => [
  6832. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6833. ],
  6834. ['div' => ['style' => 'display:none;']],
  6835. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6836. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6837. '/div',
  6838. 'button' => ['type' => 'submit'],
  6839. 'Delete',
  6840. '/button',
  6841. ['div' => ['style' => 'display:none;']],
  6842. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6843. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6844. ['input' => [
  6845. 'type' => 'hidden',
  6846. 'name' => '_Token[debug]',
  6847. 'value' => $tokenDebug,
  6848. 'autocomplete' => 'off',
  6849. ]],
  6850. '/div',
  6851. '/form',
  6852. ];
  6853. $this->assertHtml($expected, $result);
  6854. }
  6855. /**
  6856. * testPostLink method
  6857. *
  6858. * @return void
  6859. */
  6860. public function testPostLink()
  6861. {
  6862. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6863. $expected = [
  6864. 'form' => [
  6865. 'method' => 'post', 'action' => '/posts/delete/1',
  6866. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6867. ],
  6868. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6869. '/form',
  6870. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6871. 'Delete',
  6872. '/a'
  6873. ];
  6874. $this->assertHtml($expected, $result);
  6875. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6876. $expected = [
  6877. 'form' => [
  6878. 'method' => 'post', 'action' => '/posts/delete/1',
  6879. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6880. ],
  6881. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6882. '/form',
  6883. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6884. 'Delete',
  6885. '/a'
  6886. ];
  6887. $this->assertHtml($expected, $result);
  6888. $result = $this->Form->postLink(
  6889. 'Delete',
  6890. '/posts/delete/1',
  6891. ['target' => '_blank', 'class' => 'btn btn-danger']
  6892. );
  6893. $expected = [
  6894. 'form' => [
  6895. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6896. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6897. ],
  6898. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6899. '/form',
  6900. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6901. 'Delete',
  6902. '/a'
  6903. ];
  6904. $this->assertHtml($expected, $result);
  6905. }
  6906. /**
  6907. * testPostLinkWithConfirm method
  6908. *
  6909. * Test the confirm option for postLink().
  6910. *
  6911. * @return void
  6912. */
  6913. public function testPostLinkWithConfirm()
  6914. {
  6915. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  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;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6924. 'Delete',
  6925. '/a'
  6926. ];
  6927. $this->assertHtml($expected, $result);
  6928. $result = $this->Form->postLink(
  6929. 'Delete',
  6930. '/posts/delete/1',
  6931. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6932. );
  6933. $expected = [
  6934. 'form' => [
  6935. 'method' => 'post', 'action' => '/posts/delete/1',
  6936. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6937. ],
  6938. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6939. '/form',
  6940. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6941. 'Delete',
  6942. '/a'
  6943. ];
  6944. $this->assertHtml($expected, $result);
  6945. }
  6946. /**
  6947. * testPostLinkWithQuery method
  6948. *
  6949. * Test postLink() with query string args.
  6950. *
  6951. * @return void
  6952. */
  6953. public function testPostLinkWithQuery()
  6954. {
  6955. $result = $this->Form->postLink(
  6956. 'Delete',
  6957. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6958. );
  6959. $expected = [
  6960. 'form' => [
  6961. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6962. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6963. ],
  6964. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6965. '/form',
  6966. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6967. 'Delete',
  6968. '/a'
  6969. ];
  6970. $this->assertHtml($expected, $result);
  6971. }
  6972. /**
  6973. * testPostLinkWithData method
  6974. *
  6975. * Test postLink with additional data.
  6976. *
  6977. * @return void
  6978. */
  6979. public function testPostLinkWithData()
  6980. {
  6981. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6982. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6983. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6984. $this->Form->create($entity);
  6985. $this->Form->end();
  6986. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6987. $this->assertContains(
  6988. '<input type="hidden" name="name" value="show"',
  6989. $result,
  6990. 'should not contain entity data.'
  6991. );
  6992. }
  6993. /**
  6994. * testPostLinkSecurityHash method
  6995. *
  6996. * Test that security hashes for postLink include the url.
  6997. *
  6998. * @return void
  6999. */
  7000. public function testPostLinkSecurityHash()
  7001. {
  7002. $hash = Security::hash(
  7003. '/posts/delete/1' .
  7004. serialize(['id' => '1']) .
  7005. '' .
  7006. Security::salt()
  7007. );
  7008. $hash .= '%3Aid';
  7009. $this->Form->request->params['_Token']['key'] = 'test';
  7010. $result = $this->Form->postLink(
  7011. 'Delete',
  7012. '/posts/delete/1',
  7013. ['data' => ['id' => 1]]
  7014. );
  7015. $tokenDebug = urlencode(json_encode([
  7016. '/posts/delete/1',
  7017. [
  7018. 'id' => 1
  7019. ],
  7020. []
  7021. ]));
  7022. $expected = [
  7023. 'form' => [
  7024. 'method' => 'post', 'action' => '/posts/delete/1',
  7025. 'name', 'style' => 'display:none;'
  7026. ],
  7027. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7028. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7029. 'div' => ['style' => 'display:none;'],
  7030. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7031. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7032. ['input' => [
  7033. 'type' => 'hidden',
  7034. 'name' => '_Token[debug]',
  7035. 'value' => $tokenDebug,
  7036. 'autocomplete' => 'off',
  7037. ]],
  7038. '/div',
  7039. '/form',
  7040. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7041. 'Delete',
  7042. '/a'
  7043. ];
  7044. $this->assertHtml($expected, $result);
  7045. }
  7046. /**
  7047. * testPostLinkSecurityHashBlockMode method
  7048. *
  7049. * Test that postLink doesn't modify the fields in the containing form.
  7050. *
  7051. * postLink() calls inside open forms should not modify the field list
  7052. * for the form.
  7053. *
  7054. * @return void
  7055. */
  7056. public function testPostLinkSecurityHashBlockMode()
  7057. {
  7058. $hash = Security::hash(
  7059. '/posts/delete/1' .
  7060. serialize([]) .
  7061. '' .
  7062. Security::salt()
  7063. );
  7064. $hash .= '%3A';
  7065. $this->Form->request->params['_Token']['key'] = 'test';
  7066. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7067. $this->Form->control('title');
  7068. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7069. $result = $this->View->fetch('postLink');
  7070. $this->assertEquals(['title'], $this->Form->fields);
  7071. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7072. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7073. }
  7074. /**
  7075. * testPostLinkSecurityHashNoDebugMode method
  7076. *
  7077. * Test that security does not include debug token if debug is false.
  7078. *
  7079. * @return void
  7080. */
  7081. public function testPostLinkSecurityHashNoDebugMode()
  7082. {
  7083. Configure::write('debug', false);
  7084. $hash = Security::hash(
  7085. '/posts/delete/1' .
  7086. serialize(['id' => '1']) .
  7087. '' .
  7088. Security::salt()
  7089. );
  7090. $hash .= '%3Aid';
  7091. $this->Form->request->params['_Token']['key'] = 'test';
  7092. $result = $this->Form->postLink(
  7093. 'Delete',
  7094. '/posts/delete/1',
  7095. ['data' => ['id' => 1]]
  7096. );
  7097. $expected = [
  7098. 'form' => [
  7099. 'method' => 'post', 'action' => '/posts/delete/1',
  7100. 'name', 'style' => 'display:none;'
  7101. ],
  7102. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7103. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7104. 'div' => ['style' => 'display:none;'],
  7105. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7106. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7107. '/div',
  7108. '/form',
  7109. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7110. 'Delete',
  7111. '/a'
  7112. ];
  7113. $this->assertHtml($expected, $result);
  7114. }
  7115. /**
  7116. * testPostLinkNestedData method
  7117. *
  7118. * Test using postLink with N dimensional data.
  7119. *
  7120. * @return void
  7121. */
  7122. public function testPostLinkNestedData()
  7123. {
  7124. $data = [
  7125. 'one' => [
  7126. 'two' => [
  7127. 3, 4, 5
  7128. ]
  7129. ]
  7130. ];
  7131. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7132. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7133. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7134. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7135. }
  7136. /**
  7137. * testPostLinkAfterGetForm method
  7138. *
  7139. * Test creating postLinks after a GET form.
  7140. *
  7141. * @return void
  7142. */
  7143. public function testPostLinkAfterGetForm()
  7144. {
  7145. $this->Form->request->params['_csrfToken'] = 'testkey';
  7146. $this->Form->request->params['_Token'] = 'val';
  7147. $this->Form->create($this->article, ['type' => 'get']);
  7148. $this->Form->end();
  7149. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7150. $tokenDebug = urlencode(json_encode([
  7151. '/posts/delete/1',
  7152. [],
  7153. []
  7154. ]));
  7155. $expected = [
  7156. 'form' => [
  7157. 'method' => 'post', 'action' => '/posts/delete/1',
  7158. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7159. ],
  7160. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7161. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7162. 'div' => ['style' => 'display:none;'],
  7163. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7164. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7165. ['input' => [
  7166. 'type' => 'hidden', 'name' => '_Token[debug]',
  7167. 'value' => $tokenDebug,
  7168. 'autocomplete' => 'off'
  7169. ]],
  7170. '/div',
  7171. '/form',
  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. }
  7178. /**
  7179. * testPostLinkFormBuffer method
  7180. *
  7181. * Test that postLink adds form tags to view block.
  7182. *
  7183. * @return void
  7184. */
  7185. public function testPostLinkFormBuffer()
  7186. {
  7187. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7188. $expected = [
  7189. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7190. 'Delete',
  7191. '/a'
  7192. ];
  7193. $this->assertHtml($expected, $result);
  7194. $result = $this->View->fetch('postLink');
  7195. $expected = [
  7196. 'form' => [
  7197. 'method' => 'post', 'action' => '/posts/delete/1',
  7198. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7199. ],
  7200. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7201. '/form'
  7202. ];
  7203. $this->assertHtml($expected, $result);
  7204. $result = $this->Form->postLink(
  7205. 'Delete',
  7206. '/posts/delete/2',
  7207. ['block' => true, 'method' => 'DELETE']
  7208. );
  7209. $expected = [
  7210. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7211. 'Delete',
  7212. '/a'
  7213. ];
  7214. $this->assertHtml($expected, $result);
  7215. $result = $this->View->fetch('postLink');
  7216. $expected = [
  7217. 'form' => [
  7218. 'method' => 'post', 'action' => '/posts/delete/1',
  7219. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7220. ],
  7221. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7222. '/form',
  7223. [
  7224. 'form' => [
  7225. 'method' => 'post', 'action' => '/posts/delete/2',
  7226. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7227. ],
  7228. ],
  7229. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7230. '/form'
  7231. ];
  7232. $this->assertHtml($expected, $result);
  7233. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7234. $expected = [
  7235. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7236. 'Delete',
  7237. '/a'
  7238. ];
  7239. $this->assertHtml($expected, $result);
  7240. $result = $this->View->fetch('foobar');
  7241. $expected = [
  7242. 'form' => [
  7243. 'method' => 'post', 'action' => '/posts/delete/1',
  7244. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7245. ],
  7246. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7247. '/form'
  7248. ];
  7249. $this->assertHtml($expected, $result);
  7250. }
  7251. /**
  7252. * testSubmitButton method
  7253. *
  7254. * @return void
  7255. */
  7256. public function testSubmitButton()
  7257. {
  7258. $result = $this->Form->submit('');
  7259. $expected = [
  7260. 'div' => ['class' => 'submit'],
  7261. 'input' => ['type' => 'submit', 'value' => ''],
  7262. '/div'
  7263. ];
  7264. $this->assertHtml($expected, $result);
  7265. $result = $this->Form->submit('Test Submit');
  7266. $expected = [
  7267. 'div' => ['class' => 'submit'],
  7268. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7269. '/div'
  7270. ];
  7271. $this->assertHtml($expected, $result);
  7272. $result = $this->Form->submit('Next >');
  7273. $expected = [
  7274. 'div' => ['class' => 'submit'],
  7275. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7276. '/div'
  7277. ];
  7278. $this->assertHtml($expected, $result);
  7279. $result = $this->Form->submit('Next >', ['escape' => false]);
  7280. $expected = [
  7281. 'div' => ['class' => 'submit'],
  7282. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7283. '/div'
  7284. ];
  7285. $this->assertHtml($expected, $result);
  7286. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7287. $expected = [
  7288. 'div' => ['class' => 'submit'],
  7289. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7290. '/div'
  7291. ];
  7292. $this->assertHtml($expected, $result);
  7293. }
  7294. /**
  7295. * testSubmitImage method
  7296. *
  7297. * Test image submit types.
  7298. *
  7299. * @return void
  7300. */
  7301. public function testSubmitImage()
  7302. {
  7303. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7304. $expected = [
  7305. 'div' => ['class' => 'submit'],
  7306. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7307. '/div'
  7308. ];
  7309. $this->assertHtml($expected, $result);
  7310. $result = $this->Form->submit('/relative/cake.power.gif');
  7311. $expected = [
  7312. 'div' => ['class' => 'submit'],
  7313. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7314. '/div'
  7315. ];
  7316. $this->assertHtml($expected, $result);
  7317. $result = $this->Form->submit('cake.power.gif');
  7318. $expected = [
  7319. 'div' => ['class' => 'submit'],
  7320. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7321. '/div'
  7322. ];
  7323. $this->assertHtml($expected, $result);
  7324. $result = $this->Form->submit('Not.an.image');
  7325. $expected = [
  7326. 'div' => ['class' => 'submit'],
  7327. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7328. '/div'
  7329. ];
  7330. $this->assertHtml($expected, $result);
  7331. }
  7332. /**
  7333. * testSubmitUnlockedByDefault method
  7334. *
  7335. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7336. * be submitted at a time.
  7337. *
  7338. * @return void
  7339. */
  7340. public function testSubmitUnlockedByDefault()
  7341. {
  7342. $this->Form->request->params['_Token'] = 'secured';
  7343. $this->Form->submit('Go go');
  7344. $this->Form->submit('Save', ['name' => 'save']);
  7345. $result = $this->Form->unlockField();
  7346. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7347. }
  7348. /**
  7349. * testSubmitImageTimestamp method
  7350. *
  7351. * Test submit image with timestamps.
  7352. *
  7353. * @return void
  7354. */
  7355. public function testSubmitImageTimestamp()
  7356. {
  7357. Configure::write('Asset.timestamp', 'force');
  7358. $result = $this->Form->submit('cake.power.gif');
  7359. $expected = [
  7360. 'div' => ['class' => 'submit'],
  7361. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7362. '/div'
  7363. ];
  7364. $this->assertHtml($expected, $result);
  7365. }
  7366. /**
  7367. * testDateTimeWithGetForms method
  7368. *
  7369. * Test that datetime() works with GET style forms.
  7370. *
  7371. * @return void
  7372. */
  7373. public function testDateTimeWithGetForms()
  7374. {
  7375. extract($this->dateRegex);
  7376. $this->Form->create($this->article, ['type' => 'get']);
  7377. $result = $this->Form->datetime('created');
  7378. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7379. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7380. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7381. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7382. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7383. }
  7384. /**
  7385. * testForMagicControlNonExistingNorValidated method
  7386. *
  7387. * @return void
  7388. */
  7389. public function testForMagicControlNonExistingNorValidated()
  7390. {
  7391. $this->Form->create($this->article);
  7392. $this->Form->templates(['inputContainer' => '{{content}}']);
  7393. $result = $this->Form->control('non_existing_nor_validated');
  7394. $expected = [
  7395. 'label' => ['for' => 'non-existing-nor-validated'],
  7396. 'Non Existing Nor Validated',
  7397. '/label',
  7398. 'input' => [
  7399. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7400. 'id' => 'non-existing-nor-validated'
  7401. ]
  7402. ];
  7403. $this->assertHtml($expected, $result);
  7404. $result = $this->Form->control('non_existing_nor_validated', [
  7405. 'val' => 'my value'
  7406. ]);
  7407. $expected = [
  7408. 'label' => ['for' => 'non-existing-nor-validated'],
  7409. 'Non Existing Nor Validated',
  7410. '/label',
  7411. 'input' => [
  7412. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7413. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7414. ]
  7415. ];
  7416. $this->assertHtml($expected, $result);
  7417. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7418. $result = $this->Form->control('non_existing_nor_validated');
  7419. $expected = [
  7420. 'label' => ['for' => 'non-existing-nor-validated'],
  7421. 'Non Existing Nor Validated',
  7422. '/label',
  7423. 'input' => [
  7424. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7425. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7426. ]
  7427. ];
  7428. $this->assertHtml($expected, $result);
  7429. }
  7430. /**
  7431. * testFormMagicControlLabel method
  7432. *
  7433. * @return void
  7434. */
  7435. public function testFormMagicControlLabel()
  7436. {
  7437. TableRegistry::get('Contacts', [
  7438. 'className' => __NAMESPACE__ . '\ContactsTable'
  7439. ]);
  7440. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7441. $this->Form->templates(['inputContainer' => '{{content}}']);
  7442. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7443. $expected = [
  7444. 'label' => ['for' => 'contacts-name'],
  7445. 'My label',
  7446. '/label',
  7447. 'input' => [
  7448. 'type' => 'text', 'name' => 'Contacts[name]',
  7449. 'id' => 'contacts-name', 'maxlength' => '255'
  7450. ]
  7451. ];
  7452. $this->assertHtml($expected, $result);
  7453. $result = $this->Form->control('name', [
  7454. 'label' => ['class' => 'mandatory']
  7455. ]);
  7456. $expected = [
  7457. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7458. 'Name',
  7459. '/label',
  7460. 'input' => [
  7461. 'type' => 'text', 'name' => 'name',
  7462. 'id' => 'name', 'maxlength' => '255'
  7463. ]
  7464. ];
  7465. $this->assertHtml($expected, $result);
  7466. $result = $this->Form->control('name', [
  7467. 'div' => false,
  7468. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7469. ]);
  7470. $expected = [
  7471. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7472. 'My label',
  7473. '/label',
  7474. 'input' => [
  7475. 'type' => 'text', 'name' => 'name',
  7476. 'id' => 'name', 'maxlength' => '255'
  7477. ]
  7478. ];
  7479. $this->assertHtml($expected, $result);
  7480. $result = $this->Form->control('Contact.name', [
  7481. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7482. ]);
  7483. $expected = [
  7484. 'label' => ['for' => 'my_id'],
  7485. 'Name',
  7486. '/label',
  7487. 'input' => [
  7488. 'type' => 'text', 'name' => 'Contact[name]',
  7489. 'id' => 'my_id', 'maxlength' => '255'
  7490. ]
  7491. ];
  7492. $this->assertHtml($expected, $result);
  7493. $result = $this->Form->control('1.id');
  7494. $expected = ['input' => [
  7495. 'type' => 'hidden', 'name' => '1[id]',
  7496. 'id' => '1-id'
  7497. ]];
  7498. $this->assertHtml($expected, $result);
  7499. $result = $this->Form->control('1.name');
  7500. $expected = [
  7501. 'label' => ['for' => '1-name'],
  7502. 'Name',
  7503. '/label',
  7504. 'input' => [
  7505. 'type' => 'text', 'name' => '1[name]',
  7506. 'id' => '1-name', 'maxlength' => '255'
  7507. ]
  7508. ];
  7509. $this->assertHtml($expected, $result);
  7510. }
  7511. /**
  7512. * testFormEnd method
  7513. *
  7514. * @return void
  7515. */
  7516. public function testFormEnd()
  7517. {
  7518. $this->assertEquals('</form>', $this->Form->end());
  7519. }
  7520. /**
  7521. * testMultiRecordForm method
  7522. *
  7523. * Test the generation of fields for a multi record form.
  7524. *
  7525. * @return void
  7526. */
  7527. public function testMultiRecordForm()
  7528. {
  7529. $this->loadFixtures('Articles', 'Comments');
  7530. $articles = TableRegistry::get('Articles');
  7531. $articles->hasMany('Comments');
  7532. $comment = new Entity(['comment' => 'Value']);
  7533. $article = new Article(['comments' => [$comment]]);
  7534. $this->Form->create([$article]);
  7535. $result = $this->Form->control('0.comments.1.comment');
  7536. //@codingStandardsIgnoreStart
  7537. $expected = [
  7538. 'div' => ['class' => 'input textarea'],
  7539. 'label' => ['for' => '0-comments-1-comment'],
  7540. 'Comment',
  7541. '/label',
  7542. 'textarea' => [
  7543. 'name',
  7544. 'id' => '0-comments-1-comment',
  7545. 'rows' => 5
  7546. ],
  7547. '/textarea',
  7548. '/div'
  7549. ];
  7550. //@codingStandardsIgnoreEnd
  7551. $this->assertHtml($expected, $result);
  7552. $result = $this->Form->control('0.comments.0.comment');
  7553. //@codingStandardsIgnoreStart
  7554. $expected = [
  7555. 'div' => ['class' => 'input textarea'],
  7556. 'label' => ['for' => '0-comments-0-comment'],
  7557. 'Comment',
  7558. '/label',
  7559. 'textarea' => [
  7560. 'name',
  7561. 'id' => '0-comments-0-comment',
  7562. 'rows' => 5
  7563. ],
  7564. 'Value',
  7565. '/textarea',
  7566. '/div'
  7567. ];
  7568. //@codingStandardsIgnoreEnd
  7569. $this->assertHtml($expected, $result);
  7570. $comment->errors('comment', ['Not valid']);
  7571. $result = $this->Form->control('0.comments.0.comment');
  7572. //@codingStandardsIgnoreStart
  7573. $expected = [
  7574. 'div' => ['class' => 'input textarea error'],
  7575. 'label' => ['for' => '0-comments-0-comment'],
  7576. 'Comment',
  7577. '/label',
  7578. 'textarea' => [
  7579. 'name',
  7580. 'class' => 'form-error',
  7581. 'id' => '0-comments-0-comment',
  7582. 'rows' => 5
  7583. ],
  7584. 'Value',
  7585. '/textarea',
  7586. ['div' => ['class' => 'error-message']],
  7587. 'Not valid',
  7588. '/div',
  7589. '/div'
  7590. ];
  7591. //@codingStandardsIgnoreEnd
  7592. $this->assertHtml($expected, $result);
  7593. TableRegistry::get('Comments')
  7594. ->validator('default')
  7595. ->allowEmpty('comment', false);
  7596. $result = $this->Form->control('0.comments.1.comment');
  7597. //@codingStandardsIgnoreStart
  7598. $expected = [
  7599. 'div' => ['class' => 'input textarea required'],
  7600. 'label' => ['for' => '0-comments-1-comment'],
  7601. 'Comment',
  7602. '/label',
  7603. 'textarea' => [
  7604. 'name',
  7605. 'required' => 'required',
  7606. 'id' => '0-comments-1-comment',
  7607. 'rows' => 5
  7608. ],
  7609. '/textarea',
  7610. '/div'
  7611. ];
  7612. //@codingStandardsIgnoreEnd
  7613. $this->assertHtml($expected, $result);
  7614. }
  7615. /**
  7616. * testHtml5Controls method
  7617. *
  7618. * Test that some html5 inputs + FormHelper::__call() work.
  7619. *
  7620. * @return void
  7621. */
  7622. public function testHtml5Controls()
  7623. {
  7624. $result = $this->Form->email('User.email');
  7625. $expected = [
  7626. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7627. ];
  7628. $this->assertHtml($expected, $result);
  7629. $result = $this->Form->search('User.query');
  7630. $expected = [
  7631. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7632. ];
  7633. $this->assertHtml($expected, $result);
  7634. $result = $this->Form->search('User.query', ['value' => 'test']);
  7635. $expected = [
  7636. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7637. ];
  7638. $this->assertHtml($expected, $result);
  7639. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7640. $expected = [
  7641. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7642. ];
  7643. $this->assertHtml($expected, $result);
  7644. }
  7645. /**
  7646. * testHtml5ControlWithControl method
  7647. *
  7648. * Test accessing html5 inputs through control().
  7649. *
  7650. * @return void
  7651. */
  7652. public function testHtml5ControlWithControl()
  7653. {
  7654. $this->Form->create();
  7655. $this->Form->templates(['inputContainer' => '{{content}}']);
  7656. $result = $this->Form->control('website', [
  7657. 'type' => 'url',
  7658. 'val' => 'http://domain.tld',
  7659. 'label' => false
  7660. ]);
  7661. $expected = [
  7662. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7663. ];
  7664. $this->assertHtml($expected, $result);
  7665. }
  7666. /**
  7667. * testHtml5ControlException method
  7668. *
  7669. * Test errors when field name is missing.
  7670. *
  7671. * @expectedException \Cake\Core\Exception\Exception
  7672. * @return void
  7673. */
  7674. public function testHtml5ControlException()
  7675. {
  7676. $this->Form->email();
  7677. }
  7678. /**
  7679. * testRequiredAttribute method
  7680. *
  7681. * Tests that formhelper sets required attributes.
  7682. *
  7683. * @return void
  7684. */
  7685. public function testRequiredAttribute()
  7686. {
  7687. $this->article['required'] = [
  7688. 'title' => true,
  7689. 'body' => false,
  7690. ];
  7691. $this->Form->create($this->article);
  7692. $result = $this->Form->control('title');
  7693. $expected = [
  7694. 'div' => ['class' => 'input text required'],
  7695. 'label' => ['for' => 'title'],
  7696. 'Title',
  7697. '/label',
  7698. 'input' => [
  7699. 'type' => 'text',
  7700. 'name' => 'title',
  7701. 'id' => 'title',
  7702. 'required' => 'required',
  7703. ],
  7704. '/div'
  7705. ];
  7706. $this->assertHtml($expected, $result);
  7707. $result = $this->Form->control('title', ['required' => false]);
  7708. $this->assertNotContains('required', $result);
  7709. $result = $this->Form->control('body');
  7710. $expected = [
  7711. 'div' => ['class' => 'input text'],
  7712. 'label' => ['for' => 'body'],
  7713. 'Body',
  7714. '/label',
  7715. 'input' => [
  7716. 'type' => 'text',
  7717. 'name' => 'body',
  7718. 'id' => 'body',
  7719. ],
  7720. '/div'
  7721. ];
  7722. $this->assertHtml($expected, $result);
  7723. $result = $this->Form->control('body', ['required' => true]);
  7724. $this->assertContains('required', $result);
  7725. }
  7726. /**
  7727. * testControlsNotNested method
  7728. *
  7729. * Tests that it is possible to put inputs outside of the label.
  7730. *
  7731. * @return void
  7732. */
  7733. public function testControlsNotNested()
  7734. {
  7735. $this->Form->templates([
  7736. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7737. 'formGroup' => '{{input}}{{label}}',
  7738. ]);
  7739. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7740. $expected = [
  7741. 'div' => ['class' => 'input checkbox'],
  7742. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7743. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7744. 'label' => ['for' => 'foo'],
  7745. 'Foo',
  7746. '/label',
  7747. '/div'
  7748. ];
  7749. $this->assertHtml($expected, $result);
  7750. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7751. $expected = [
  7752. 'div' => ['class' => 'input checkbox'],
  7753. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7754. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7755. '/div'
  7756. ];
  7757. $this->assertHtml($expected, $result);
  7758. $result = $this->Form->control('confirm', [
  7759. 'type' => 'radio',
  7760. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7761. ]);
  7762. $expected = [
  7763. 'div' => ['class' => 'input radio'],
  7764. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7765. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7766. ['label' => ['for' => 'confirm-y']],
  7767. 'Yes',
  7768. '/label',
  7769. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7770. ['label' => ['for' => 'confirm-n']],
  7771. 'No',
  7772. '/label',
  7773. '<label',
  7774. 'Confirm',
  7775. '/label',
  7776. '/div',
  7777. ];
  7778. $this->assertHtml($expected, $result);
  7779. $result = $this->Form->select('category', ['1', '2'], [
  7780. 'multiple' => 'checkbox',
  7781. 'name' => 'fish',
  7782. ]);
  7783. $expected = [
  7784. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7785. ['div' => ['class' => 'checkbox']],
  7786. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7787. ['label' => ['for' => 'fish-0']],
  7788. '1',
  7789. '/label',
  7790. '/div',
  7791. ['div' => ['class' => 'checkbox']],
  7792. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7793. ['label' => ['for' => 'fish-1']],
  7794. '2',
  7795. '/label',
  7796. '/div'
  7797. ];
  7798. $this->assertHtml($expected, $result);
  7799. }
  7800. /**
  7801. * testControlContainerTemplates method
  7802. *
  7803. * Test that *Container templates are used by input.
  7804. *
  7805. * @return void
  7806. */
  7807. public function testControlContainerTemplates()
  7808. {
  7809. $this->Form->templates([
  7810. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7811. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7812. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7813. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7814. ]);
  7815. $this->article['errors'] = [
  7816. 'Article' => ['published' => 'error message']
  7817. ];
  7818. $this->Form->create($this->article);
  7819. $result = $this->Form->control('accept', [
  7820. 'type' => 'checkbox'
  7821. ]);
  7822. $expected = [
  7823. 'div' => ['class' => 'check'],
  7824. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7825. 'label' => ['for' => 'accept'],
  7826. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7827. 'Accept',
  7828. '/label',
  7829. '/div'
  7830. ];
  7831. $this->assertHtml($expected, $result);
  7832. $result = $this->Form->control('accept', [
  7833. 'type' => 'radio',
  7834. 'options' => ['Y', 'N']
  7835. ]);
  7836. $this->assertContains('<div class="rad">', $result);
  7837. $result = $this->Form->control('Article.published', [
  7838. 'type' => 'radio',
  7839. 'options' => ['Y', 'N']
  7840. ]);
  7841. $this->assertContains('<div class="rad err">', $result);
  7842. $result = $this->Form->control('Article.created', [
  7843. 'type' => 'datetime',
  7844. ]);
  7845. $this->assertContains('<div class="dt">', $result);
  7846. }
  7847. /**
  7848. * testFormGroupTemplates method
  7849. *
  7850. * Test that *Container templates are used by input.
  7851. *
  7852. * @return void
  7853. */
  7854. public function testFormGroupTemplates()
  7855. {
  7856. $this->Form->templates([
  7857. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7858. ]);
  7859. $this->Form->create($this->article);
  7860. $result = $this->Form->control('accept', [
  7861. 'type' => 'radio',
  7862. 'options' => ['Y', 'N']
  7863. ]);
  7864. $this->assertContains('<div class="radio">', $result);
  7865. }
  7866. /**
  7867. * testResetTemplates method
  7868. *
  7869. * Test resetting templates.
  7870. *
  7871. * @return void
  7872. */
  7873. public function testResetTemplates()
  7874. {
  7875. $this->Form->templates(['input' => '<input/>']);
  7876. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7877. $this->Form->resetTemplates();
  7878. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7879. }
  7880. /**
  7881. * testContext method
  7882. *
  7883. * Test the context method.
  7884. *
  7885. * @return void
  7886. */
  7887. public function testContext()
  7888. {
  7889. $result = $this->Form->context();
  7890. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7891. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7892. $this->assertSame($mock, $this->Form->context($mock));
  7893. $this->assertSame($mock, $this->Form->context());
  7894. }
  7895. /**
  7896. * testAutoDomId method
  7897. *
  7898. * @return void
  7899. */
  7900. public function testAutoDomId()
  7901. {
  7902. $result = $this->Form->text('field', ['id' => true]);
  7903. $expected = [
  7904. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7905. ];
  7906. $this->assertHtml($expected, $result);
  7907. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7908. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7909. $expected = [
  7910. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7911. ['label' => ['for' => 'field-0']],
  7912. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7913. 'option A',
  7914. '/label',
  7915. ['label' => ['for' => 'field-1']],
  7916. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7917. 'option B',
  7918. '/label',
  7919. ];
  7920. $this->assertHtml($expected, $result);
  7921. $result = $this->Form->select(
  7922. 'multi_field',
  7923. ['first', 'second'],
  7924. ['multiple' => 'checkbox', 'id' => true]
  7925. );
  7926. $expected = [
  7927. 'input' => [
  7928. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7929. ],
  7930. ['div' => ['class' => 'checkbox']],
  7931. ['label' => ['for' => 'multi-field-0']],
  7932. ['input' => [
  7933. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7934. 'value' => '0', 'id' => 'multi-field-0'
  7935. ]],
  7936. 'first',
  7937. '/label',
  7938. '/div',
  7939. ['div' => ['class' => 'checkbox']],
  7940. ['label' => ['for' => 'multi-field-1']],
  7941. ['input' => [
  7942. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7943. 'value' => '1', 'id' => 'multi-field-1'
  7944. ]],
  7945. 'second',
  7946. '/label',
  7947. '/div',
  7948. ];
  7949. $this->assertHtml($expected, $result);
  7950. }
  7951. /**
  7952. * Test the basic setters and getters for value sources
  7953. *
  7954. * @return void
  7955. */
  7956. public function testFormValueSourcesSettersGetters()
  7957. {
  7958. $expected = ['context'];
  7959. $result = $this->Form->getValueSources();
  7960. $this->assertEquals($expected, $result);
  7961. $expected = null;
  7962. $result = $this->Form->getSourceValue('id');
  7963. $this->assertEquals($expected, $result);
  7964. $expected = ['query', 'data', 'context'];
  7965. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7966. $result = $this->Form->getValueSources();
  7967. $this->assertEquals($expected, $result);
  7968. $this->Form->request->data['id'] = '1';
  7969. $this->Form->request->query['id'] = '2';
  7970. $this->Form->setValueSources(['context']);
  7971. $expected = '1';
  7972. $result = $this->Form->getSourceValue('id');
  7973. $this->assertEquals($expected, $result);
  7974. $this->Form->setValueSources('query');
  7975. $expected = ['query'];
  7976. $result = $this->Form->getValueSources();
  7977. $this->assertEquals($expected, $result);
  7978. $expected = '2';
  7979. $result = $this->Form->getSourceValue('id');
  7980. $this->assertEquals($expected, $result);
  7981. $this->Form->setValueSources(['data']);
  7982. $expected = '1';
  7983. $result = $this->Form->getSourceValue('id');
  7984. $this->assertEquals($expected, $result);
  7985. $this->Form->setValueSources(['query', 'data']);
  7986. $expected = '2';
  7987. $result = $this->Form->getSourceValue('id');
  7988. $this->assertEquals($expected, $result);
  7989. }
  7990. /**
  7991. * Tests the different input rendering values based on sources values switching
  7992. *
  7993. * @return void
  7994. */
  7995. public function testFormValueSourcesSingleSwitchRendering()
  7996. {
  7997. $this->loadFixtures('Articles');
  7998. $articles = TableRegistry::get('Articles');
  7999. $article = new Article();
  8000. $articles->patchEntity($article, ['id' => '3']);
  8001. $this->Form->create($article);
  8002. $this->Form->setValueSources(['context']);
  8003. $result = $this->Form->control('id');
  8004. $expected = [
  8005. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8006. ];
  8007. $this->assertHtml($expected, $result);
  8008. $this->Form->request->query['id'] = '5';
  8009. $this->Form->setValueSources(['query']);
  8010. $result = $this->Form->control('id');
  8011. $expected = [
  8012. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8013. ];
  8014. $this->assertHtml($expected, $result);
  8015. $this->Form->request->query['id'] = '5a';
  8016. $this->Form->request->data['id'] = '5b';
  8017. $this->Form->setValueSources(['context']);
  8018. $this->Form->create($article);
  8019. $result = $this->Form->control('id');
  8020. $expected = [
  8021. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8022. ];
  8023. $this->assertHtml($expected, $result);
  8024. $this->Form->setValueSources(['data']);
  8025. $this->Form->create($article);
  8026. $result = $this->Form->control('id');
  8027. $expected = [
  8028. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8029. ];
  8030. $this->assertHtml($expected, $result);
  8031. $this->Form->setValueSources(['query']);
  8032. $result = $this->Form->control('id');
  8033. $expected = [
  8034. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8035. ];
  8036. $this->assertHtml($expected, $result);
  8037. }
  8038. /**
  8039. * Tests the different input rendering values based on sources values switching while supplying
  8040. * an entity (base context) and multiple sources (such as data, query)
  8041. *
  8042. * @return void
  8043. */
  8044. public function testFormValueSourcesListSwitchRendering()
  8045. {
  8046. $this->loadFixtures('Articles');
  8047. $articles = TableRegistry::get('Articles');
  8048. $article = new Article();
  8049. $articles->patchEntity($article, ['id' => '3']);
  8050. $this->Form->request->query['id'] = '9';
  8051. $this->Form->create($article);
  8052. $this->Form->setValueSources(['context', 'query']);
  8053. $result = $this->Form->control('id');
  8054. $expected = [
  8055. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8056. ];
  8057. $this->assertHtml($expected, $result);
  8058. $this->Form->setValueSources(['query', 'context']);
  8059. $result = $this->Form->control('id');
  8060. $expected = [
  8061. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8062. ];
  8063. $this->assertHtml($expected, $result);
  8064. $this->Form->setValueSources(['data', 'query', 'context']);
  8065. $result = $this->Form->control('id');
  8066. $expected = [
  8067. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8068. ];
  8069. $this->assertHtml($expected, $result);
  8070. $this->Form->request->data['id'] = '8';
  8071. $this->Form->request->query['id'] = '9';
  8072. $this->Form->setValueSources(['data', 'query', 'context']);
  8073. $result = $this->Form->control('id');
  8074. $expected = [
  8075. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8076. ];
  8077. $this->assertHtml($expected, $result);
  8078. }
  8079. /**
  8080. * Test the different form input renderings based on values sources switchings through form options
  8081. *
  8082. * @return void
  8083. */
  8084. public function testFormValueSourcesSwitchViaOptionsRendering()
  8085. {
  8086. $this->loadFixtures('Articles');
  8087. $articles = TableRegistry::get('Articles');
  8088. $article = new Article();
  8089. $articles->patchEntity($article, ['id' => '3']);
  8090. $this->Form->request->data['id'] = '4';
  8091. $this->Form->request->query['id'] = '5';
  8092. $this->Form->create($article, ['valueSources' => 'query']);
  8093. $result = $this->Form->control('id');
  8094. $expected = [
  8095. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8096. ];
  8097. $this->assertHtml($expected, $result);
  8098. $result = $this->Form->getSourceValue('id');
  8099. $this->assertEquals('5', $result);
  8100. $this->Form->setValueSources(['context']);
  8101. $this->Form->create($article, ['valueSources' => 'query']);
  8102. $result = $this->Form->control('id');
  8103. $expected = [
  8104. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8105. ];
  8106. $this->assertHtml($expected, $result);
  8107. $result = $this->Form->getSourceValue('id');
  8108. $this->assertEquals('5', $result);
  8109. $this->Form->setValueSources(['query']);
  8110. $this->Form->create($article, ['valueSources' => 'data']);
  8111. $result = $this->Form->control('id');
  8112. $expected = [
  8113. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8114. ];
  8115. $this->assertHtml($expected, $result);
  8116. $result = $this->Form->getSourceValue('id');
  8117. $this->assertEquals('4', $result);
  8118. $this->Form->setValueSources(['query']);
  8119. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8120. $result = $this->Form->control('id');
  8121. $expected = [
  8122. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8123. ];
  8124. $this->assertHtml($expected, $result);
  8125. $result = $this->Form->getSourceValue('id');
  8126. $this->assertEquals('4', $result);
  8127. }
  8128. /**
  8129. * Test the different form input renderings based on values sources switchings through form options
  8130. *
  8131. * @return void
  8132. */
  8133. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8134. {
  8135. $this->loadFixtures('Articles');
  8136. $articles = TableRegistry::get('Articles');
  8137. $article = new Article();
  8138. $articles->patchEntity($article, ['id' => '3']);
  8139. $this->Form->request->data['id'] = '10';
  8140. $this->Form->request->query['id'] = '11';
  8141. $this->Form->setValueSources(['context'])
  8142. ->create($article, ['valueSources' => ['query', 'data']]);
  8143. $result = $this->Form->control('id');
  8144. $expected = [
  8145. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8146. ];
  8147. $this->assertHtml($expected, $result);
  8148. $result = $this->Form->getSourceValue('id');
  8149. $this->assertEquals('11', $result);
  8150. unset($this->Form->request->query['id']);
  8151. $this->Form->setValueSources(['context'])
  8152. ->create($article, ['valueSources' => ['query', 'data']]);
  8153. $result = $this->Form->control('id');
  8154. $expected = [
  8155. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8156. ];
  8157. $this->assertHtml($expected, $result);
  8158. $result = $this->Form->getSourceValue('id');
  8159. $this->assertEquals('10', $result);
  8160. }
  8161. /**
  8162. * Test the different form values sources resetting through From::end();
  8163. *
  8164. * @return void
  8165. */
  8166. public function testFormValueSourcesResetViaEnd()
  8167. {
  8168. $expected = ['context'];
  8169. $result = $this->Form->getValueSources();
  8170. $this->assertEquals($expected, $result);
  8171. $expected = ['query', 'context', 'data'];
  8172. $this->Form->setValueSources(['query', 'context', 'data']);
  8173. $result = $this->Form->getValueSources();
  8174. $this->assertEquals($expected, $result);
  8175. $this->Form->create();
  8176. $result = $this->Form->getValueSources();
  8177. $this->assertEquals($expected, $result);
  8178. $this->Form->end();
  8179. $result = $this->Form->getValueSources();
  8180. $this->assertEquals(['context'], $result);
  8181. }
  8182. /**
  8183. * Test sources values defaults handling
  8184. *
  8185. * @return void
  8186. */
  8187. public function testFormValueSourcesDefaults()
  8188. {
  8189. $this->Form->request->query['password'] = 'open Sesame';
  8190. $this->Form->create();
  8191. $result = $this->Form->password('password');
  8192. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8193. $this->assertHtml($expected, $result);
  8194. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8195. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8196. $this->assertHtml($expected, $result);
  8197. $this->Form->setValueSources('query');
  8198. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8199. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8200. $this->assertHtml($expected, $result);
  8201. $this->Form->setValueSources('data');
  8202. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8203. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8204. $this->assertHtml($expected, $result);
  8205. }
  8206. /**
  8207. * Test sources values schema defaults handling
  8208. *
  8209. * @return void
  8210. */
  8211. public function testSourcesValueDoesntExistPassThrough()
  8212. {
  8213. $this->Form->request->query['category'] = 'sesame-cookies';
  8214. $articles = TableRegistry::get('Articles');
  8215. $entity = $articles->newEntity();
  8216. $this->Form->create($entity);
  8217. $this->Form->setValueSources(['query', 'context']);
  8218. $result = $this->Form->getSourceValue('category');
  8219. $this->assertEquals('sesame-cookies', $result);
  8220. $this->Form->setValueSources(['context', 'query']);
  8221. $result = $this->Form->getSourceValue('category');
  8222. $this->assertEquals('sesame-cookies', $result);
  8223. }
  8224. /**
  8225. * testNestedLabelInput method
  8226. *
  8227. * Test the `nestedInput` parameter
  8228. *
  8229. * @return void
  8230. */
  8231. public function testNestedLabelInput()
  8232. {
  8233. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8234. $expected = [
  8235. 'div' => ['class' => 'input text'],
  8236. 'label' => ['for' => 'foo'],
  8237. ['input' => [
  8238. 'type' => 'text',
  8239. 'name' => 'foo',
  8240. 'id' => 'foo'
  8241. ]],
  8242. 'Foo',
  8243. '/label',
  8244. '/div'
  8245. ];
  8246. $this->assertHtml($expected, $result);
  8247. }
  8248. /**
  8249. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8250. *
  8251. * This test makes sure `false` excludes the label from the render
  8252. *
  8253. * @return void
  8254. */
  8255. public function testInputLabelManipulationDisableLabels()
  8256. {
  8257. $result = $this->Form->input('test', [
  8258. 'type' => 'radio',
  8259. 'options' => ['A', 'B'],
  8260. 'labelOptions' => false
  8261. ]);
  8262. $expected = [
  8263. ['div' => ['class' => 'input radio']],
  8264. '<label',
  8265. 'Test',
  8266. '/label',
  8267. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8268. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8269. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8270. '/div',
  8271. ];
  8272. $this->assertHtml($expected, $result);
  8273. $result = $this->Form->input('checkbox1', [
  8274. 'label' => 'My checkboxes',
  8275. 'multiple' => 'checkbox',
  8276. 'type' => 'select',
  8277. 'options' => [
  8278. ['text' => 'First Checkbox', 'value' => 1],
  8279. ['text' => 'Second Checkbox', 'value' => 2]
  8280. ],
  8281. 'labelOptions' => false
  8282. ]);
  8283. $expected = [
  8284. ['div' => ['class' => 'input select']],
  8285. ['label' => ['for' => 'checkbox1']],
  8286. 'My checkboxes',
  8287. '/label',
  8288. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8289. ['div' => ['class' => 'checkbox']],
  8290. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8291. '/div',
  8292. ['div' => ['class' => 'checkbox']],
  8293. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8294. '/div',
  8295. '/div'
  8296. ];
  8297. $this->assertHtml($expected, $result);
  8298. }
  8299. /**
  8300. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8301. *
  8302. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8303. * added to the class if checked.
  8304. *
  8305. * Also checks to make sure any custom attributes are rendered correctly
  8306. *
  8307. * @return void
  8308. */
  8309. public function testInputLabelManipulationRadios()
  8310. {
  8311. $result = $this->Form->input('test', [
  8312. 'type' => 'radio',
  8313. 'options' => ['A', 'B'],
  8314. 'labelOptions' => ['class' => 'custom-class']
  8315. ]);
  8316. $expected = [
  8317. ['div' => ['class' => 'input radio']],
  8318. '<label',
  8319. 'Test',
  8320. '/label',
  8321. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8322. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8323. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8324. 'A',
  8325. '/label',
  8326. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8327. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8328. 'B',
  8329. '/label',
  8330. '/div',
  8331. ];
  8332. $this->assertHtml($expected, $result);
  8333. $result = $this->Form->input('test', [
  8334. 'type' => 'radio',
  8335. 'options' => ['A', 'B'],
  8336. 'value' => 1,
  8337. 'labelOptions' => ['class' => 'custom-class']
  8338. ]);
  8339. $expected = [
  8340. ['div' => ['class' => 'input radio']],
  8341. '<label',
  8342. 'Test',
  8343. '/label',
  8344. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8345. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8346. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8347. 'A',
  8348. '/label',
  8349. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8350. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8351. 'B',
  8352. '/label',
  8353. '/div',
  8354. ];
  8355. $this->assertHtml($expected, $result);
  8356. $result = $this->Form->input('test', [
  8357. 'type' => 'radio',
  8358. 'options' => ['A', 'B'],
  8359. 'value' => 1,
  8360. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8361. ]);
  8362. $expected = [
  8363. ['div' => ['class' => 'input radio']],
  8364. '<label',
  8365. 'Test',
  8366. '/label',
  8367. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8368. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8369. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8370. 'A',
  8371. '/label',
  8372. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8373. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8374. 'B',
  8375. '/label',
  8376. '/div',
  8377. ];
  8378. $this->assertHtml($expected, $result);
  8379. $result = $this->Form->radio('test', ['A', 'B'], [
  8380. 'label' => [
  8381. 'class' => ['custom-class', 'another-class'],
  8382. 'data-name' => 'bob'
  8383. ],
  8384. 'value' => 1
  8385. ]);
  8386. $expected = [
  8387. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8388. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8389. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8390. 'A',
  8391. '/label',
  8392. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8393. ['input' => [
  8394. 'type' => 'radio',
  8395. 'name' => 'test',
  8396. 'value' => '1',
  8397. 'id' => 'test-1',
  8398. 'checked' => 'checked'
  8399. ]],
  8400. 'B',
  8401. '/label',
  8402. ];
  8403. $this->assertHtml($expected, $result);
  8404. }
  8405. /**
  8406. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8407. *
  8408. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8409. * added to the class if checked.
  8410. *
  8411. * Also checks to make sure any custom attributes are rendered correctly
  8412. *
  8413. * @return void
  8414. */
  8415. public function testInputLabelManipulationCheckboxes()
  8416. {
  8417. $result = $this->Form->input('checkbox1', [
  8418. 'label' => 'My checkboxes',
  8419. 'multiple' => 'checkbox',
  8420. 'type' => 'select',
  8421. 'options' => [
  8422. ['text' => 'First Checkbox', 'value' => 1],
  8423. ['text' => 'Second Checkbox', 'value' => 2]
  8424. ],
  8425. 'labelOptions' => ['class' => 'custom-class'],
  8426. 'value' => ['1']
  8427. ]);
  8428. $expected = [
  8429. ['div' => ['class' => 'input select']],
  8430. ['label' => ['for' => 'checkbox1']],
  8431. 'My checkboxes',
  8432. '/label',
  8433. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8434. ['div' => ['class' => 'checkbox']],
  8435. ['label' => [
  8436. 'class' => 'custom-class selected',
  8437. 'for' => 'checkbox1-1'
  8438. ]],
  8439. ['input' => [
  8440. 'type' => 'checkbox',
  8441. 'name' => 'checkbox1[]',
  8442. 'value' => '1',
  8443. 'id' => 'checkbox1-1',
  8444. 'checked' => 'checked'
  8445. ]],
  8446. 'First Checkbox',
  8447. '/label',
  8448. '/div',
  8449. ['div' => ['class' => 'checkbox']],
  8450. ['label' => [
  8451. 'class' => 'custom-class',
  8452. 'for' => 'checkbox1-2'
  8453. ]],
  8454. ['input' => [
  8455. 'type' => 'checkbox',
  8456. 'name' => 'checkbox1[]',
  8457. 'value' => '2',
  8458. 'id' => 'checkbox1-2'
  8459. ]],
  8460. 'Second Checkbox',
  8461. '/label',
  8462. '/div',
  8463. '/div'
  8464. ];
  8465. $this->assertHtml($expected, $result);
  8466. $result = $this->Form->input('checkbox1', [
  8467. 'label' => 'My checkboxes',
  8468. 'multiple' => 'checkbox',
  8469. 'type' => 'select',
  8470. 'options' => [
  8471. ['text' => 'First Checkbox', 'value' => 1],
  8472. ['text' => 'Second Checkbox', 'value' => 2]
  8473. ],
  8474. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8475. 'value' => ['1']
  8476. ]);
  8477. $expected = [
  8478. ['div' => ['class' => 'input select']],
  8479. ['label' => ['for' => 'checkbox1']],
  8480. 'My checkboxes',
  8481. '/label',
  8482. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8483. ['div' => ['class' => 'checkbox']],
  8484. ['label' => [
  8485. 'class' => 'custom-class another-class selected',
  8486. 'data-name' => 'bob',
  8487. 'for' => 'checkbox1-1'
  8488. ]],
  8489. ['input' => [
  8490. 'type' => 'checkbox',
  8491. 'name' => 'checkbox1[]',
  8492. 'value' => '1',
  8493. 'id' => 'checkbox1-1',
  8494. 'checked' => 'checked'
  8495. ]],
  8496. 'First Checkbox',
  8497. '/label',
  8498. '/div',
  8499. ['div' => ['class' => 'checkbox']],
  8500. ['label' => [
  8501. 'class' => 'custom-class another-class',
  8502. 'data-name' => 'bob',
  8503. 'for' => 'checkbox1-2'
  8504. ]],
  8505. ['input' => [
  8506. 'type' => 'checkbox',
  8507. 'name' => 'checkbox1[]',
  8508. 'value' => '2',
  8509. 'id' => 'checkbox1-2'
  8510. ]],
  8511. 'Second Checkbox',
  8512. '/label',
  8513. '/div',
  8514. '/div'
  8515. ];
  8516. $this->assertHtml($expected, $result);
  8517. }
  8518. }