FormHelperTest.php 299 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Core\Configure;
  18. use Cake\Form\Form;
  19. use Cake\Network\Request;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Security;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\View;
  28. /**
  29. * Test stub.
  30. */
  31. class Article extends Entity
  32. {
  33. }
  34. /**
  35. * Contact class
  36. */
  37. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->schema($this->_schema);
  64. }
  65. }
  66. /**
  67. * ValidateUser class
  68. */
  69. class ValidateUsersTable extends Table
  70. {
  71. /**
  72. * schema method
  73. *
  74. * @var array
  75. */
  76. protected $_schema = [
  77. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  78. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  79. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  81. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  82. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  83. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  84. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  85. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  86. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  87. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  88. ];
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config)
  95. {
  96. $this->schema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property \Cake\View\Helper\FormHelper $Form
  103. */
  104. class FormHelperTest extends TestCase
  105. {
  106. /**
  107. * Fixtures to be used
  108. *
  109. * @var array
  110. */
  111. public $fixtures = ['core.articles', 'core.comments'];
  112. /**
  113. * Do not load the fixtures by default
  114. *
  115. * @var bool
  116. */
  117. public $autoFixtures = false;
  118. /**
  119. * setUp method
  120. *
  121. * @return void
  122. */
  123. public function setUp()
  124. {
  125. parent::setUp();
  126. Configure::write('Config.language', 'eng');
  127. Configure::write('App.base', '');
  128. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  129. $this->View = new View();
  130. $this->Form = new FormHelper($this->View);
  131. $request = new Request([
  132. 'webroot' => '',
  133. 'base' => '',
  134. 'url' => '/articles/add',
  135. 'params' => [
  136. 'controller' => 'articles',
  137. 'action' => 'add',
  138. ]
  139. ]);
  140. $this->Form->Url->request = $this->Form->request = $request;
  141. $this->dateRegex = [
  142. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  143. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  144. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  145. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  146. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  147. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  148. ];
  149. $this->article = [
  150. 'schema' => [
  151. 'id' => ['type' => 'integer'],
  152. 'author_id' => ['type' => 'integer', 'null' => true],
  153. 'title' => ['type' => 'string', 'null' => true],
  154. 'body' => 'text',
  155. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  156. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  157. ],
  158. 'required' => [
  159. 'author_id' => true,
  160. 'title' => true,
  161. ]
  162. ];
  163. Security::salt('foo!');
  164. Router::connect('/:controller', ['action' => 'index']);
  165. Router::connect('/:controller/:action/*');
  166. }
  167. /**
  168. * tearDown method
  169. *
  170. * @return void
  171. */
  172. public function tearDown()
  173. {
  174. parent::tearDown();
  175. unset($this->Form, $this->Controller, $this->View);
  176. TableRegistry::clear();
  177. }
  178. /**
  179. * Test construct() with the templates option.
  180. *
  181. * @return void
  182. */
  183. public function testConstructTemplatesFile()
  184. {
  185. $helper = new FormHelper($this->View, [
  186. 'templates' => 'htmlhelper_tags'
  187. ]);
  188. $result = $helper->control('name');
  189. $this->assertContains('<input', $result);
  190. }
  191. /**
  192. * Test that when specifying custom widgets the config array for that widget
  193. * is overwritten instead of merged.
  194. *
  195. * @return void
  196. */
  197. public function testConstructWithWidgets()
  198. {
  199. $config = [
  200. 'widgets' => [
  201. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  202. ]
  203. ];
  204. $helper = new FormHelper($this->View, $config);
  205. $registry = $helper->widgetRegistry();
  206. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  207. }
  208. /**
  209. * Test that when specifying custom widgets config file and it should be
  210. * added to widgets array. WidgetRegistry will load widgets in constructor.
  211. *
  212. * @return void
  213. */
  214. public function testConstructWithWidgetsConfig()
  215. {
  216. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  217. $registry = $helper->widgetRegistry();
  218. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  219. }
  220. /**
  221. * Test registering a new widget class and rendering it.
  222. *
  223. * @return void
  224. */
  225. public function testAddWidgetAndRenderWidget()
  226. {
  227. $data = [
  228. 'val' => 1
  229. ];
  230. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  231. $this->Form->addWidget('test', $mock);
  232. $mock->expects($this->once())
  233. ->method('render')
  234. ->with($data)
  235. ->will($this->returnValue('HTML'));
  236. $result = $this->Form->widget('test', $data);
  237. $this->assertEquals('HTML', $result);
  238. }
  239. /**
  240. * Test that secureFields() of widget is called after calling render(),
  241. * not before.
  242. *
  243. * @return void
  244. */
  245. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  246. {
  247. $data = [
  248. 'val' => 1,
  249. 'name' => 'test'
  250. ];
  251. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  252. $this->Form->addWidget('test', $mock);
  253. $mock->expects($this->at(0))
  254. ->method('render')
  255. ->with($data)
  256. ->will($this->returnValue('HTML'));
  257. $mock->expects($this->at(1))
  258. ->method('secureFields')
  259. ->with($data)
  260. ->will($this->returnValue(['test']));
  261. $result = $this->Form->widget('test', $data + ['secure' => true]);
  262. $this->assertEquals('HTML', $result);
  263. }
  264. /**
  265. * Test that empty string is not added to secure fields list when
  266. * rendering input widget without name.
  267. *
  268. * @return void
  269. */
  270. public function testRenderingWidgetWithEmptyName()
  271. {
  272. $this->assertEquals([], $this->Form->fields);
  273. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  274. $this->assertEquals('<select name=""></select>', $result);
  275. $this->assertEquals([], $this->Form->fields);
  276. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  277. $this->assertEquals('<select name="0"></select>', $result);
  278. $this->assertEquals(['0'], $this->Form->fields);
  279. }
  280. /**
  281. * Test registering an invalid widget class.
  282. *
  283. * @expectedException \RuntimeException
  284. * @return void
  285. */
  286. public function testAddWidgetInvalid()
  287. {
  288. $mock = new \StdClass();
  289. $this->Form->addWidget('test', $mock);
  290. $this->Form->widget('test');
  291. }
  292. /**
  293. * Test adding a new context class.
  294. *
  295. * @return void
  296. */
  297. public function testAddContextProvider()
  298. {
  299. $context = 'My data';
  300. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  301. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  302. $this->assertInstanceOf('Cake\Network\Request', $request);
  303. $this->assertEquals($context, $data['entity']);
  304. return $stub;
  305. });
  306. $this->Form->create($context);
  307. $result = $this->Form->context();
  308. $this->assertSame($stub, $result);
  309. }
  310. /**
  311. * Test replacing a context class.
  312. *
  313. * @return void
  314. */
  315. public function testAddContextProviderReplace()
  316. {
  317. $entity = new Article();
  318. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  319. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  320. return $stub;
  321. });
  322. $this->Form->create($entity);
  323. $result = $this->Form->context();
  324. $this->assertSame($stub, $result);
  325. }
  326. /**
  327. * Test overriding a context class.
  328. *
  329. * @return void
  330. */
  331. public function testAddContextProviderAdd()
  332. {
  333. $entity = new Article();
  334. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  335. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  336. if ($data['entity'] instanceof Entity) {
  337. return $stub;
  338. }
  339. });
  340. $this->Form->create($entity);
  341. $result = $this->Form->context();
  342. $this->assertSame($stub, $result);
  343. }
  344. /**
  345. * Test adding an invalid context class.
  346. *
  347. * @expectedException \RuntimeException
  348. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  349. * @return void
  350. */
  351. public function testAddContextProviderInvalid()
  352. {
  353. $context = 'My data';
  354. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  355. return new \StdClass();
  356. });
  357. $this->Form->create($context);
  358. }
  359. /**
  360. * Provides context options for create().
  361. *
  362. * @return array
  363. */
  364. public function contextSelectionProvider()
  365. {
  366. $entity = new Article();
  367. $collection = new Collection([$entity]);
  368. $emptyCollection = new Collection([]);
  369. $arrayObject = new \ArrayObject([]);
  370. $data = [
  371. 'schema' => [
  372. 'title' => ['type' => 'string']
  373. ]
  374. ];
  375. $form = new Form();
  376. return [
  377. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  378. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  379. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  380. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  381. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  382. 'form' => [$form, 'Cake\View\Form\FormContext'],
  383. 'none' => [null, 'Cake\View\Form\NullContext'],
  384. 'false' => [false, 'Cake\View\Form\NullContext'],
  385. ];
  386. }
  387. /**
  388. * Test default context selection in create()
  389. *
  390. * @dataProvider contextSelectionProvider
  391. * @return void
  392. */
  393. public function testCreateContextSelectionBuiltIn($data, $class)
  394. {
  395. $this->loadFixtures('Articles');
  396. $this->Form->create($data);
  397. $this->assertInstanceOf($class, $this->Form->context());
  398. }
  399. /**
  400. * Data provider for type option.
  401. *
  402. * @return array
  403. */
  404. public static function requestTypeProvider()
  405. {
  406. return [
  407. // type, method, override
  408. ['post', 'post', 'POST'],
  409. ['put', 'post', 'PUT'],
  410. ['patch', 'post', 'PATCH'],
  411. ['delete', 'post', 'DELETE'],
  412. ];
  413. }
  414. /**
  415. * Test creating file forms.
  416. *
  417. * @return void
  418. */
  419. public function testCreateFile()
  420. {
  421. $encoding = strtolower(Configure::read('App.encoding'));
  422. $result = $this->Form->create(false, ['type' => 'file']);
  423. $expected = [
  424. 'form' => [
  425. 'method' => 'post', 'action' => '/articles/add',
  426. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  427. ],
  428. 'div' => ['style' => 'display:none;'],
  429. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  430. '/div'
  431. ];
  432. $this->assertHtml($expected, $result);
  433. }
  434. /**
  435. * Test creating GET forms.
  436. *
  437. * @return void
  438. */
  439. public function testCreateGet()
  440. {
  441. $encoding = strtolower(Configure::read('App.encoding'));
  442. $result = $this->Form->create(false, ['type' => 'get']);
  443. $expected = ['form' => [
  444. 'method' => 'get', 'action' => '/articles/add',
  445. 'accept-charset' => $encoding
  446. ]];
  447. $this->assertHtml($expected, $result);
  448. }
  449. /**
  450. * Test explicit method/enctype options.
  451. *
  452. * Explicit method overwrites inferred method from 'type'
  453. *
  454. * @return void
  455. */
  456. public function testCreateExplicitMethodEnctype()
  457. {
  458. $encoding = strtolower(Configure::read('App.encoding'));
  459. $result = $this->Form->create(false, [
  460. 'type' => 'get',
  461. 'method' => 'put',
  462. 'enctype' => 'multipart/form-data'
  463. ]);
  464. $expected = ['form' => [
  465. 'method' => 'put',
  466. 'action' => '/articles/add',
  467. 'enctype' => 'multipart/form-data',
  468. 'accept-charset' => $encoding
  469. ]];
  470. $this->assertHtml($expected, $result);
  471. }
  472. /**
  473. * Test create() with the templates option.
  474. *
  475. * @return void
  476. */
  477. public function testCreateTemplatesArray()
  478. {
  479. $result = $this->Form->create($this->article, [
  480. 'templates' => [
  481. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  482. ]
  483. ]);
  484. $expected = [
  485. 'form' => [
  486. 'class' => 'form-horizontal',
  487. 'method' => 'post',
  488. 'action' => '/articles/add',
  489. 'accept-charset' => 'utf-8'
  490. ]
  491. ];
  492. $this->assertHtml($expected, $result);
  493. }
  494. /**
  495. * Test create() with the templates option.
  496. *
  497. * @return void
  498. */
  499. public function testCreateTemplatesFile()
  500. {
  501. $result = $this->Form->create($this->article, [
  502. 'templates' => 'htmlhelper_tags',
  503. ]);
  504. $expected = [
  505. 'start form',
  506. 'div' => ['class' => 'hidden'],
  507. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  508. '/div'
  509. ];
  510. $this->assertHtml($expected, $result);
  511. }
  512. /**
  513. * Test that create() and end() restore templates.
  514. *
  515. * @return void
  516. */
  517. public function testCreateEndRestoreTemplates()
  518. {
  519. $this->Form->create($this->article, [
  520. 'templates' => ['input' => 'custom input element']
  521. ]);
  522. $this->Form->end();
  523. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  524. }
  525. /**
  526. * Test using template vars in various templates used by control() method.
  527. *
  528. * @return void
  529. */
  530. public function testControlTemplateVars()
  531. {
  532. $result = $this->Form->control('text', [
  533. 'templates' => [
  534. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  535. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  536. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  537. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  538. ],
  539. 'templateVars' => [
  540. 'forinput' => 'in-input',
  541. 'forlabel' => 'in-label',
  542. 'forgroup' => 'in-group',
  543. 'forcontainer' => 'in-container'
  544. ]
  545. ]);
  546. $expected = [
  547. 'div' => ['class'],
  548. 'label' => ['for'],
  549. 'Text in-label',
  550. '/label',
  551. 'in-group',
  552. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  553. 'in-container',
  554. '/div',
  555. ];
  556. $this->assertHtml($expected, $result);
  557. }
  558. /**
  559. * Test ensuring template variables work in template files loaded
  560. * during control().
  561. *
  562. * @return void
  563. */
  564. public function testControlTemplatesFromFile()
  565. {
  566. $result = $this->Form->control('title', [
  567. 'templates' => 'test_templates',
  568. 'templateVars' => [
  569. 'forcontainer' => 'container-data'
  570. ]
  571. ]);
  572. $expected = [
  573. 'div' => ['class'],
  574. 'label' => ['for'],
  575. 'Title',
  576. '/label',
  577. 'input' => ['name', 'type' => 'text', 'id'],
  578. 'container-data',
  579. '/div',
  580. ];
  581. $this->assertHtml($expected, $result);
  582. }
  583. /**
  584. * Test using template vars in inputSubmit and submitContainer template.
  585. *
  586. * @return void
  587. */
  588. public function testSubmitTemplateVars()
  589. {
  590. $this->Form->templates([
  591. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  592. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  593. ]);
  594. $result = $this->Form->submit('Submit', [
  595. 'templateVars' => [
  596. 'forinput' => 'in-input',
  597. 'forcontainer' => 'in-container'
  598. ]
  599. ]);
  600. $expected = [
  601. 'div' => ['class'],
  602. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  603. 'in-container',
  604. '/div',
  605. ];
  606. $this->assertHtml($expected, $result);
  607. }
  608. /**
  609. * test the create() method
  610. *
  611. * @dataProvider requestTypeProvider
  612. * @return void
  613. */
  614. public function testCreateTypeOptions($type, $method, $override)
  615. {
  616. $encoding = strtolower(Configure::read('App.encoding'));
  617. $result = $this->Form->create(false, ['type' => $type]);
  618. $expected = [
  619. 'form' => [
  620. 'method' => $method, 'action' => '/articles/add',
  621. 'accept-charset' => $encoding
  622. ],
  623. 'div' => ['style' => 'display:none;'],
  624. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  625. '/div'
  626. ];
  627. $this->assertHtml($expected, $result);
  628. }
  629. /**
  630. * Test using template vars in Create (formStart template)
  631. *
  632. * @return void
  633. */
  634. public function testCreateTemplateVars()
  635. {
  636. $result = $this->Form->create($this->article, [
  637. 'templates' => [
  638. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  639. ],
  640. 'templateVars' => ['header' => 'headertext']
  641. ]);
  642. $expected = [
  643. 'h4' => ['class'],
  644. 'headertext',
  645. '/h4',
  646. 'form' => [
  647. 'method' => 'post',
  648. 'action' => '/articles/add',
  649. 'accept-charset' => 'utf-8'
  650. ]
  651. ];
  652. $this->assertHtml($expected, $result);
  653. }
  654. /**
  655. * Test opening a form for an update operation.
  656. *
  657. * @return void
  658. */
  659. public function testCreateUpdateForm()
  660. {
  661. $encoding = strtolower(Configure::read('App.encoding'));
  662. $this->Form->request->here = '/articles/edit/1';
  663. $this->Form->request->params['action'] = 'edit';
  664. $this->article['defaults']['id'] = 1;
  665. $result = $this->Form->create($this->article);
  666. $expected = [
  667. 'form' => [
  668. 'method' => 'post',
  669. 'action' => '/articles/edit/1',
  670. 'accept-charset' => $encoding
  671. ],
  672. 'div' => ['style' => 'display:none;'],
  673. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  674. '/div'
  675. ];
  676. $this->assertHtml($expected, $result);
  677. }
  678. /**
  679. * test create() with automatic url generation
  680. *
  681. * @return void
  682. */
  683. public function testCreateAutoUrl()
  684. {
  685. $encoding = strtolower(Configure::read('App.encoding'));
  686. $this->Form->request->params['action'] = 'delete';
  687. $this->Form->request->here = '/articles/delete/10';
  688. $this->Form->request->base = '';
  689. $result = $this->Form->create($this->article);
  690. $expected = [
  691. 'form' => [
  692. 'method' => 'post', 'action' => '/articles/delete/10',
  693. 'accept-charset' => $encoding
  694. ],
  695. 'div' => ['style' => 'display:none;'],
  696. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  697. '/div'
  698. ];
  699. $this->assertHtml($expected, $result);
  700. $this->article['defaults'] = ['id' => 1];
  701. $this->Form->request->here = '/articles/edit/1';
  702. $this->Form->request->params['action'] = 'delete';
  703. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  704. $expected = [
  705. 'form' => [
  706. 'method' => 'post',
  707. 'action' => '/articles/edit/1',
  708. 'accept-charset' => $encoding
  709. ],
  710. 'div' => ['style' => 'display:none;'],
  711. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  712. '/div'
  713. ];
  714. $this->assertHtml($expected, $result);
  715. $this->Form->request->params['action'] = 'add';
  716. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  717. $expected = [
  718. 'form' => [
  719. 'method' => 'post',
  720. 'action' => '/articles/publish/1',
  721. 'accept-charset' => $encoding
  722. ],
  723. 'div' => ['style' => 'display:none;'],
  724. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  725. '/div'
  726. ];
  727. $this->assertHtml($expected, $result);
  728. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  729. $expected = [
  730. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  731. 'div' => ['style' => 'display:none;'],
  732. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  733. '/div'
  734. ];
  735. $this->assertHtml($expected, $result);
  736. $this->Form->request->params['controller'] = 'Pages';
  737. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  738. $expected = [
  739. 'form' => [
  740. 'method' => 'post', 'action' => '/Pages/signup/1',
  741. 'accept-charset' => $encoding
  742. ],
  743. 'div' => ['style' => 'display:none;'],
  744. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  745. '/div'
  746. ];
  747. $this->assertHtml($expected, $result);
  748. }
  749. /**
  750. * Test create() with no URL (no "action" attribute for <form> tag)
  751. *
  752. * @return void
  753. */
  754. public function testCreateNoUrl()
  755. {
  756. $result = $this->Form->create(false, ['url' => false]);
  757. $expected = [
  758. 'form' => [
  759. 'method' => 'post',
  760. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  761. ],
  762. 'div' => ['style' => 'display:none;'],
  763. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  764. '/div'
  765. ];
  766. $this->assertHtml($expected, $result);
  767. }
  768. /**
  769. * test create() with a custom route
  770. *
  771. * @return void
  772. */
  773. public function testCreateCustomRoute()
  774. {
  775. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  776. $encoding = strtolower(Configure::read('App.encoding'));
  777. $this->Form->request->params['controller'] = 'users';
  778. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  779. $expected = [
  780. 'form' => [
  781. 'method' => 'post', 'action' => '/login',
  782. 'accept-charset' => $encoding
  783. ],
  784. 'div' => ['style' => 'display:none;'],
  785. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  786. '/div'
  787. ];
  788. $this->assertHtml($expected, $result);
  789. Router::connect(
  790. '/new-article',
  791. ['controller' => 'articles', 'action' => 'myaction'],
  792. ['_name' => 'my-route']
  793. );
  794. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  795. $expected = [
  796. 'form' => [
  797. 'method' => 'post', 'action' => '/new-article',
  798. 'accept-charset' => $encoding,
  799. ],
  800. 'div' => ['style' => 'display:none;'],
  801. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  802. '/div'
  803. ];
  804. $this->assertHtml($expected, $result);
  805. }
  806. /**
  807. * test automatic accept-charset overriding
  808. *
  809. * @return void
  810. */
  811. public function testCreateWithAcceptCharset()
  812. {
  813. $result = $this->Form->create(
  814. $this->article,
  815. [
  816. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  817. ]
  818. );
  819. $expected = [
  820. 'form' => [
  821. 'method' => 'post', 'action' => '/articles',
  822. 'accept-charset' => 'iso-8859-1'
  823. ],
  824. 'div' => ['style' => 'display:none;'],
  825. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  826. '/div'
  827. ];
  828. $this->assertHtml($expected, $result);
  829. }
  830. /**
  831. * Test base form URL when url param is passed with multiple parameters (&)
  832. *
  833. */
  834. public function testCreateQueryStringRequest()
  835. {
  836. $encoding = strtolower(Configure::read('App.encoding'));
  837. $result = $this->Form->create($this->article, [
  838. 'type' => 'post',
  839. 'escape' => false,
  840. 'url' => [
  841. 'controller' => 'controller',
  842. 'action' => 'action',
  843. '?' => ['param1' => 'value1', 'param2' => 'value2']
  844. ]
  845. ]);
  846. $expected = [
  847. 'form' => [
  848. 'method' => 'post',
  849. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  850. 'accept-charset' => $encoding
  851. ],
  852. 'div' => ['style' => 'display:none;'],
  853. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  854. '/div'
  855. ];
  856. $this->assertHtml($expected, $result);
  857. $result = $this->Form->create($this->article, [
  858. 'type' => 'post',
  859. 'url' => [
  860. 'controller' => 'controller',
  861. 'action' => 'action',
  862. '?' => ['param1' => 'value1', 'param2' => 'value2']
  863. ]
  864. ]);
  865. $this->assertHtml($expected, $result);
  866. }
  867. /**
  868. * test that create() doesn't cause errors by multiple id's being in the primary key
  869. * as could happen with multiple select or checkboxes.
  870. *
  871. * @return void
  872. */
  873. public function testCreateWithMultipleIdInData()
  874. {
  875. $encoding = strtolower(Configure::read('App.encoding'));
  876. $this->Form->request->data['Article']['id'] = [1, 2];
  877. $result = $this->Form->create($this->article);
  878. $expected = [
  879. 'form' => [
  880. 'method' => 'post',
  881. 'action' => '/articles/add',
  882. 'accept-charset' => $encoding
  883. ],
  884. 'div' => ['style' => 'display:none;'],
  885. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  886. '/div'
  887. ];
  888. $this->assertHtml($expected, $result);
  889. }
  890. /**
  891. * test that create() doesn't add in extra passed params.
  892. *
  893. * @return void
  894. */
  895. public function testCreatePassedArgs()
  896. {
  897. $encoding = strtolower(Configure::read('App.encoding'));
  898. $this->Form->request->data['Article']['id'] = 1;
  899. $result = $this->Form->create($this->article, [
  900. 'type' => 'post',
  901. 'escape' => false,
  902. 'url' => [
  903. 'action' => 'edit',
  904. 'myparam'
  905. ]
  906. ]);
  907. $expected = [
  908. 'form' => [
  909. 'method' => 'post',
  910. 'action' => '/articles/edit/myparam',
  911. 'accept-charset' => $encoding
  912. ],
  913. 'div' => ['style' => 'display:none;'],
  914. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  915. '/div'
  916. ];
  917. $this->assertHtml($expected, $result);
  918. }
  919. /**
  920. * test creating a get form, and get form inputs.
  921. *
  922. * @return void
  923. */
  924. public function testGetFormCreate()
  925. {
  926. $encoding = strtolower(Configure::read('App.encoding'));
  927. $result = $this->Form->create($this->article, ['type' => 'get']);
  928. $expected = ['form' => [
  929. 'method' => 'get', 'action' => '/articles/add',
  930. 'accept-charset' => $encoding
  931. ]];
  932. $this->assertHtml($expected, $result);
  933. $result = $this->Form->text('title');
  934. $expected = ['input' => [
  935. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  936. ]];
  937. $this->assertHtml($expected, $result);
  938. $result = $this->Form->password('password');
  939. $expected = ['input' => [
  940. 'name' => 'password', 'type' => 'password'
  941. ]];
  942. $this->assertHtml($expected, $result);
  943. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  944. $result = $this->Form->text('user_form');
  945. $expected = ['input' => [
  946. 'name' => 'user_form', 'type' => 'text'
  947. ]];
  948. $this->assertHtml($expected, $result);
  949. }
  950. /**
  951. * test get form, and inputs when the model param is false
  952. *
  953. * @return void
  954. */
  955. public function testGetFormWithFalseModel()
  956. {
  957. $encoding = strtolower(Configure::read('App.encoding'));
  958. $this->Form->request->params['controller'] = 'contact_test';
  959. $result = $this->Form->create(false, [
  960. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  961. ]);
  962. $expected = ['form' => [
  963. 'method' => 'get', 'action' => '/contact_test/add',
  964. 'accept-charset' => $encoding
  965. ]];
  966. $this->assertHtml($expected, $result);
  967. $result = $this->Form->text('reason');
  968. $expected = [
  969. 'input' => ['type' => 'text', 'name' => 'reason']
  970. ];
  971. $this->assertHtml($expected, $result);
  972. }
  973. /**
  974. * testFormCreateWithSecurity method
  975. *
  976. * Test form->create() with security key.
  977. *
  978. * @return void
  979. */
  980. public function testCreateWithSecurity()
  981. {
  982. $this->Form->request->params['_csrfToken'] = 'testKey';
  983. $encoding = strtolower(Configure::read('App.encoding'));
  984. $result = $this->Form->create($this->article, [
  985. 'url' => '/articles/publish',
  986. ]);
  987. $expected = [
  988. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  989. 'div' => ['style' => 'display:none;'],
  990. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  991. ['input' => [
  992. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  993. ]],
  994. '/div'
  995. ];
  996. $this->assertHtml($expected, $result);
  997. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  998. $expected['form']['id'] = 'MyForm';
  999. $this->assertHtml($expected, $result);
  1000. }
  1001. /**
  1002. * testFormCreateGetNoSecurity method
  1003. *
  1004. * Test form->create() with no security key as its a get form
  1005. *
  1006. * @return void
  1007. */
  1008. public function testCreateEndGetNoSecurity()
  1009. {
  1010. $this->Form->request->params['_csrfToken'] = 'testKey';
  1011. $article = new Article();
  1012. $result = $this->Form->create($article, [
  1013. 'type' => 'get',
  1014. 'url' => '/contacts/add'
  1015. ]);
  1016. $this->assertNotContains('testKey', $result);
  1017. $result = $this->Form->end();
  1018. $this->assertNotContains('testKey', $result);
  1019. }
  1020. /**
  1021. * test that create() clears the fields property so it starts fresh
  1022. *
  1023. * @return void
  1024. */
  1025. public function testCreateClearingFields()
  1026. {
  1027. $this->Form->fields = ['model_id'];
  1028. $this->Form->create($this->article);
  1029. $this->assertEquals([], $this->Form->fields);
  1030. }
  1031. /**
  1032. * Tests form hash generation with model-less data
  1033. *
  1034. * @return void
  1035. */
  1036. public function testValidateHashNoModel()
  1037. {
  1038. $this->Form->request->params['_Token'] = 'foo';
  1039. $result = $this->Form->secure(['anything']);
  1040. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1041. }
  1042. /**
  1043. * Tests that hidden fields generated for checkboxes don't get locked
  1044. *
  1045. * @return void
  1046. */
  1047. public function testNoCheckboxLocking()
  1048. {
  1049. $this->Form->request->params['_Token'] = 'foo';
  1050. $this->assertSame([], $this->Form->fields);
  1051. $this->Form->checkbox('check', ['value' => '1']);
  1052. $this->assertSame($this->Form->fields, ['check']);
  1053. }
  1054. /**
  1055. * testFormSecurityFields method
  1056. *
  1057. * Test generation of secure form hash generation.
  1058. *
  1059. * @return void
  1060. */
  1061. public function testFormSecurityFields()
  1062. {
  1063. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1064. $this->Form->request->params['_Token'] = 'testKey';
  1065. $result = $this->Form->secure($fields);
  1066. $hash = Security::hash(serialize($fields) . Security::salt());
  1067. $hash .= ':' . 'Model.valid';
  1068. $hash = urlencode($hash);
  1069. $tokenDebug = urlencode(json_encode([
  1070. '',
  1071. $fields,
  1072. []
  1073. ]));
  1074. $expected = [
  1075. 'div' => ['style' => 'display:none;'],
  1076. ['input' => [
  1077. 'type' => 'hidden',
  1078. 'name' => '_Token[fields]',
  1079. 'value' => $hash
  1080. ]],
  1081. ['input' => [
  1082. 'type' => 'hidden',
  1083. 'name' => '_Token[unlocked]',
  1084. 'value' => '',
  1085. ]],
  1086. ['input' => [
  1087. 'type' => 'hidden',
  1088. 'name' => '_Token[debug]',
  1089. 'value' => $tokenDebug
  1090. ]],
  1091. '/div'
  1092. ];
  1093. $this->assertHtml($expected, $result);
  1094. }
  1095. /**
  1096. * testFormSecurityFields method
  1097. *
  1098. * Test debug token is not generated if debug is false
  1099. *
  1100. * @return void
  1101. */
  1102. public function testFormSecurityFieldsNoDebugMode()
  1103. {
  1104. Configure::write('debug', false);
  1105. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1106. $this->Form->request->params['_Token'] = 'testKey';
  1107. $result = $this->Form->secure($fields);
  1108. $hash = Security::hash(serialize($fields) . Security::salt());
  1109. $hash .= ':' . 'Model.valid';
  1110. $hash = urlencode($hash);
  1111. $expected = [
  1112. 'div' => ['style' => 'display:none;'],
  1113. ['input' => [
  1114. 'type' => 'hidden',
  1115. 'name' => '_Token[fields]',
  1116. 'value' => $hash
  1117. ]],
  1118. ['input' => [
  1119. 'type' => 'hidden',
  1120. 'name' => '_Token[unlocked]',
  1121. 'value' => '',
  1122. ]],
  1123. '/div'
  1124. ];
  1125. $this->assertHtml($expected, $result);
  1126. }
  1127. /**
  1128. * Tests correct generation of number fields for smallint
  1129. *
  1130. * @return void
  1131. */
  1132. public function testTextFieldGenerationForSmallint()
  1133. {
  1134. $this->article['schema'] = [
  1135. 'foo' => [
  1136. 'type' => 'smallinteger',
  1137. 'null' => false,
  1138. 'default' => null,
  1139. 'length' => 10
  1140. ]
  1141. ];
  1142. $this->Form->create($this->article);
  1143. $result = $this->Form->control('foo');
  1144. $this->assertContains('class="input number"', $result);
  1145. $this->assertContains('type="number"', $result);
  1146. }
  1147. /**
  1148. * Tests correct generation of number fields for tinyint
  1149. *
  1150. * @return void
  1151. */
  1152. public function testTextFieldGenerationForTinyint()
  1153. {
  1154. $this->article['schema'] = [
  1155. 'foo' => [
  1156. 'type' => 'tinyinteger',
  1157. 'null' => false,
  1158. 'default' => null,
  1159. 'length' => 10
  1160. ]
  1161. ];
  1162. $this->Form->create($this->article);
  1163. $result = $this->Form->control('foo');
  1164. $this->assertContains('class="input number"', $result);
  1165. $this->assertContains('type="number"', $result);
  1166. }
  1167. /**
  1168. * Tests correct generation of number fields for double and float fields
  1169. *
  1170. * @return void
  1171. */
  1172. public function testTextFieldGenerationForFloats()
  1173. {
  1174. $this->article['schema'] = [
  1175. 'foo' => [
  1176. 'type' => 'float',
  1177. 'null' => false,
  1178. 'default' => null,
  1179. 'length' => 10
  1180. ]
  1181. ];
  1182. $this->Form->create($this->article);
  1183. $result = $this->Form->control('foo');
  1184. $expected = [
  1185. 'div' => ['class' => 'input number'],
  1186. 'label' => ['for' => 'foo'],
  1187. 'Foo',
  1188. '/label',
  1189. ['input' => [
  1190. 'type' => 'number',
  1191. 'name' => 'foo',
  1192. 'id' => 'foo',
  1193. 'step' => 'any'
  1194. ]],
  1195. '/div'
  1196. ];
  1197. $this->assertHtml($expected, $result);
  1198. $result = $this->Form->control('foo', ['step' => 0.5]);
  1199. $expected = [
  1200. 'div' => ['class' => 'input number'],
  1201. 'label' => ['for' => 'foo'],
  1202. 'Foo',
  1203. '/label',
  1204. ['input' => [
  1205. 'type' => 'number',
  1206. 'name' => 'foo',
  1207. 'id' => 'foo',
  1208. 'step' => '0.5'
  1209. ]],
  1210. '/div'
  1211. ];
  1212. $this->assertHtml($expected, $result);
  1213. }
  1214. /**
  1215. * Tests correct generation of number fields for integer fields
  1216. *
  1217. * @return void
  1218. */
  1219. public function testTextFieldTypeNumberGenerationForIntegers()
  1220. {
  1221. TableRegistry::get('Contacts', [
  1222. 'className' => __NAMESPACE__ . '\ContactsTable'
  1223. ]);
  1224. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1225. $result = $this->Form->control('age');
  1226. $expected = [
  1227. 'div' => ['class' => 'input number'],
  1228. 'label' => ['for' => 'age'],
  1229. 'Age',
  1230. '/label',
  1231. ['input' => [
  1232. 'type' => 'number', 'name' => 'age',
  1233. 'id' => 'age'
  1234. ]],
  1235. '/div'
  1236. ];
  1237. $this->assertHtml($expected, $result);
  1238. }
  1239. /**
  1240. * Tests correct generation of file upload fields for binary fields
  1241. *
  1242. * @return void
  1243. */
  1244. public function testFileUploadFieldTypeGenerationForBinaries()
  1245. {
  1246. $table = TableRegistry::get('Contacts', [
  1247. 'className' => __NAMESPACE__ . '\ContactsTable'
  1248. ]);
  1249. $table->schema(['foo' => [
  1250. 'type' => 'binary',
  1251. 'null' => false,
  1252. 'default' => null,
  1253. 'length' => 1024
  1254. ]]);
  1255. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1256. $result = $this->Form->control('foo');
  1257. $expected = [
  1258. 'div' => ['class' => 'input file'],
  1259. 'label' => ['for' => 'foo'],
  1260. 'Foo',
  1261. '/label',
  1262. ['input' => [
  1263. 'type' => 'file', 'name' => 'foo',
  1264. 'id' => 'foo'
  1265. ]],
  1266. '/div'
  1267. ];
  1268. $this->assertHtml($expected, $result);
  1269. }
  1270. /**
  1271. * testFormSecurityMultipleFields method
  1272. *
  1273. * Test secure() with multiple row form. Ensure hash is correct.
  1274. *
  1275. * @return void
  1276. */
  1277. public function testFormSecurityMultipleFields()
  1278. {
  1279. $this->Form->request->params['_Token'] = 'foo';
  1280. $fields = [
  1281. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1282. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1283. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1284. ];
  1285. $result = $this->Form->secure($fields);
  1286. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1287. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1288. $tokenDebug = urlencode(json_encode([
  1289. '',
  1290. $fields,
  1291. []
  1292. ]));
  1293. $expected = [
  1294. 'div' => ['style' => 'display:none;'],
  1295. ['input' => [
  1296. 'type' => 'hidden', 'name' => '_Token[fields]',
  1297. 'value' => $hash
  1298. ]],
  1299. ['input' => [
  1300. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1301. 'value' => ''
  1302. ]],
  1303. ['input' => [
  1304. 'type' => 'hidden', 'name' => '_Token[debug]',
  1305. 'value' => $tokenDebug,
  1306. ]],
  1307. '/div'
  1308. ];
  1309. $this->assertHtml($expected, $result);
  1310. }
  1311. /**
  1312. * testFormSecurityMultipleSubmitButtons
  1313. *
  1314. * test form submit generation and ensure that _Token is only created on end()
  1315. *
  1316. * @return void
  1317. */
  1318. public function testFormSecurityMultipleSubmitButtons()
  1319. {
  1320. $this->Form->request->params['_Token'] = 'testKey';
  1321. $this->Form->create($this->article);
  1322. $this->Form->text('Address.title');
  1323. $this->Form->text('Address.first_name');
  1324. $result = $this->Form->submit('Save', ['name' => 'save']);
  1325. $expected = [
  1326. 'div' => ['class' => 'submit'],
  1327. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1328. '/div',
  1329. ];
  1330. $this->assertHtml($expected, $result);
  1331. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1332. $expected = [
  1333. 'div' => ['class' => 'submit'],
  1334. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1335. '/div',
  1336. ];
  1337. $this->assertHtml($expected, $result);
  1338. $result = $this->Form->end();
  1339. $tokenDebug = urlencode(json_encode([
  1340. '/articles/add',
  1341. [
  1342. 'Address.title',
  1343. 'Address.first_name',
  1344. ],
  1345. ['save', 'cancel']
  1346. ]));
  1347. $expected = [
  1348. 'div' => ['style' => 'display:none;'],
  1349. ['input' => [
  1350. 'type' => 'hidden',
  1351. 'name' => '_Token[fields]',
  1352. 'value'
  1353. ]],
  1354. ['input' => [
  1355. 'type' => 'hidden',
  1356. 'name' => '_Token[unlocked]',
  1357. 'value' => 'cancel%7Csave'
  1358. ]],
  1359. ['input' => [
  1360. 'type' => 'hidden', 'name' => '_Token[debug]',
  1361. 'value' => $tokenDebug,
  1362. ]],
  1363. '/div'
  1364. ];
  1365. $this->assertHtml($expected, $result);
  1366. }
  1367. /**
  1368. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1369. *
  1370. * @return void
  1371. */
  1372. public function testSecurityButtonNestedNamed()
  1373. {
  1374. $key = 'testKey';
  1375. $this->Form->request->params['_csrfToken'] = $key;
  1376. $this->Form->create('Addresses');
  1377. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1378. $result = $this->Form->unlockField();
  1379. $this->assertEquals(['Address.button'], $result);
  1380. }
  1381. /**
  1382. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1383. *
  1384. * @return void
  1385. */
  1386. public function testSecuritySubmitNestedNamed()
  1387. {
  1388. $this->Form->request->params['_Token'] = 'testKey';
  1389. $this->Form->create($this->article);
  1390. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1391. $result = $this->Form->unlockField();
  1392. $this->assertEquals(['Address.button'], $result);
  1393. }
  1394. /**
  1395. * Test that the correct fields are unlocked for image submits with no names.
  1396. *
  1397. * @return void
  1398. */
  1399. public function testSecuritySubmitImageNoName()
  1400. {
  1401. $this->Form->request->params['_Token'] = 'testKey';
  1402. $this->Form->create(false);
  1403. $result = $this->Form->submit('save.png');
  1404. $expected = [
  1405. 'div' => ['class' => 'submit'],
  1406. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1407. '/div'
  1408. ];
  1409. $this->assertHtml($expected, $result);
  1410. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1411. }
  1412. /**
  1413. * Test that the correct fields are unlocked for image submits with names.
  1414. *
  1415. * @return void
  1416. */
  1417. public function testSecuritySubmitImageName()
  1418. {
  1419. $this->Form->request->params['_Token'] = 'testKey';
  1420. $this->Form->create(null);
  1421. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1422. $expected = [
  1423. 'div' => ['class' => 'submit'],
  1424. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1425. '/div'
  1426. ];
  1427. $this->assertHtml($expected, $result);
  1428. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1429. }
  1430. /**
  1431. * testFormSecurityMultipleControlFields method
  1432. *
  1433. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1434. *
  1435. * @return void
  1436. */
  1437. public function testFormSecurityMultipleControlFields()
  1438. {
  1439. $this->Form->request->params['_Token'] = 'testKey';
  1440. $this->Form->create();
  1441. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1442. $this->Form->control('Addresses.0.title');
  1443. $this->Form->control('Addresses.0.first_name');
  1444. $this->Form->control('Addresses.0.last_name');
  1445. $this->Form->control('Addresses.0.address');
  1446. $this->Form->control('Addresses.0.city');
  1447. $this->Form->control('Addresses.0.phone');
  1448. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1449. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1450. $this->Form->control('Addresses.1.title');
  1451. $this->Form->control('Addresses.1.first_name');
  1452. $this->Form->control('Addresses.1.last_name');
  1453. $this->Form->control('Addresses.1.address');
  1454. $this->Form->control('Addresses.1.city');
  1455. $this->Form->control('Addresses.1.phone');
  1456. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1457. $result = $this->Form->secure($this->Form->fields);
  1458. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1459. $tokenDebug = urlencode(json_encode([
  1460. '/articles/add',
  1461. [
  1462. 'Addresses.0.id' => '123456',
  1463. 'Addresses.0.title',
  1464. 'Addresses.0.first_name',
  1465. 'Addresses.0.last_name',
  1466. 'Addresses.0.address',
  1467. 'Addresses.0.city',
  1468. 'Addresses.0.phone',
  1469. 'Addresses.0.primary',
  1470. 'Addresses.1.id' => '654321',
  1471. 'Addresses.1.title',
  1472. 'Addresses.1.first_name',
  1473. 'Addresses.1.last_name',
  1474. 'Addresses.1.address',
  1475. 'Addresses.1.city',
  1476. 'Addresses.1.phone',
  1477. 'Addresses.1.primary',
  1478. ],
  1479. []
  1480. ]));
  1481. $expected = [
  1482. 'div' => ['style' => 'display:none;'],
  1483. ['input' => [
  1484. 'type' => 'hidden', 'name' => '_Token[fields]',
  1485. 'value' => $hash
  1486. ]],
  1487. ['input' => [
  1488. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1489. 'value' => ''
  1490. ]],
  1491. ['input' => [
  1492. 'type' => 'hidden', 'name' => '_Token[debug]',
  1493. 'value' => $tokenDebug,
  1494. ]],
  1495. '/div'
  1496. ];
  1497. $this->assertHtml($expected, $result);
  1498. }
  1499. /**
  1500. * testFormSecurityArrayFields method
  1501. *
  1502. * Test form security with Model.field.0 style inputs.
  1503. *
  1504. * @return void
  1505. */
  1506. public function testFormSecurityArrayFields()
  1507. {
  1508. $this->Form->request->params['_Token'] = 'testKey';
  1509. $this->Form->create();
  1510. $this->Form->text('Address.primary.1');
  1511. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1512. $this->Form->text('Address.secondary.1.0');
  1513. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1514. }
  1515. /**
  1516. * testFormSecurityMultipleControlDisabledFields method
  1517. *
  1518. * Test secure form generation with multiple records and disabled fields.
  1519. *
  1520. * @return void
  1521. */
  1522. public function testFormSecurityMultipleControlDisabledFields()
  1523. {
  1524. $this->Form->request->params['_Token'] = [
  1525. 'unlockedFields' => ['first_name', 'address']
  1526. ];
  1527. $this->Form->create();
  1528. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1529. $this->Form->text('Addresses.0.title');
  1530. $this->Form->text('Addresses.0.first_name');
  1531. $this->Form->text('Addresses.0.last_name');
  1532. $this->Form->text('Addresses.0.address');
  1533. $this->Form->text('Addresses.0.city');
  1534. $this->Form->text('Addresses.0.phone');
  1535. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1536. $this->Form->text('Addresses.1.title');
  1537. $this->Form->text('Addresses.1.first_name');
  1538. $this->Form->text('Addresses.1.last_name');
  1539. $this->Form->text('Addresses.1.address');
  1540. $this->Form->text('Addresses.1.city');
  1541. $this->Form->text('Addresses.1.phone');
  1542. $result = $this->Form->secure($this->Form->fields);
  1543. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1544. $tokenDebug = urlencode(json_encode([
  1545. '/articles/add',
  1546. [
  1547. 'Addresses.0.id' => '123456',
  1548. 'Addresses.0.title',
  1549. 'Addresses.0.last_name',
  1550. 'Addresses.0.city',
  1551. 'Addresses.0.phone',
  1552. 'Addresses.1.id' => '654321',
  1553. 'Addresses.1.title',
  1554. 'Addresses.1.last_name',
  1555. 'Addresses.1.city',
  1556. 'Addresses.1.phone'
  1557. ],
  1558. [
  1559. 'first_name',
  1560. 'address'
  1561. ]
  1562. ]));
  1563. $expected = [
  1564. 'div' => ['style' => 'display:none;'],
  1565. ['input' => [
  1566. 'type' => 'hidden',
  1567. 'name' => '_Token[fields]',
  1568. 'value' => $hash
  1569. ]],
  1570. ['input' => [
  1571. 'type' => 'hidden',
  1572. 'name' => '_Token[unlocked]',
  1573. 'value' => 'address%7Cfirst_name',
  1574. ]],
  1575. ['input' => [
  1576. 'type' => 'hidden', 'name' => '_Token[debug]',
  1577. 'value' => $tokenDebug,
  1578. ]],
  1579. '/div'
  1580. ];
  1581. $this->assertHtml($expected, $result);
  1582. }
  1583. /**
  1584. * testFormSecurityControlDisabledFields method
  1585. *
  1586. * Test single record form with disabled fields.
  1587. *
  1588. * @return void
  1589. */
  1590. public function testFormSecurityControlUnlockedFields()
  1591. {
  1592. $this->Form->request->params['_Token'] = [
  1593. 'unlockedFields' => ['first_name', 'address']
  1594. ];
  1595. $this->Form->create();
  1596. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1597. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1598. $this->Form->text('Addresses.title');
  1599. $this->Form->text('Addresses.first_name');
  1600. $this->Form->text('Addresses.last_name');
  1601. $this->Form->text('Addresses.address');
  1602. $this->Form->text('Addresses.city');
  1603. $this->Form->text('Addresses.phone');
  1604. $result = $this->Form->fields;
  1605. $expected = [
  1606. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1607. 'Addresses.city', 'Addresses.phone'
  1608. ];
  1609. $this->assertEquals($expected, $result);
  1610. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1611. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1612. $tokenDebug = urlencode(json_encode([
  1613. '/articles/add',
  1614. [
  1615. 'Addresses.id' => '123456',
  1616. 'Addresses.title',
  1617. 'Addresses.last_name',
  1618. 'Addresses.city',
  1619. 'Addresses.phone'
  1620. ],
  1621. [
  1622. 'first_name',
  1623. 'address'
  1624. ]
  1625. ]));
  1626. $expected = [
  1627. 'div' => ['style' => 'display:none;'],
  1628. ['input' => [
  1629. 'type' => 'hidden',
  1630. 'name' => '_Token[fields]',
  1631. 'value' => $hash,
  1632. 'data-foo' => 'bar',
  1633. ]],
  1634. ['input' => [
  1635. 'type' => 'hidden',
  1636. 'name' => '_Token[unlocked]',
  1637. 'value' => 'address%7Cfirst_name',
  1638. 'data-foo' => 'bar',
  1639. ]],
  1640. ['input' => [
  1641. 'type' => 'hidden', 'name' => '_Token[debug]',
  1642. 'value' => $tokenDebug,
  1643. 'data-foo' => 'bar'
  1644. ]],
  1645. '/div'
  1646. ];
  1647. $this->assertHtml($expected, $result);
  1648. }
  1649. /**
  1650. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1651. *
  1652. * Test single record form with debugSecurity param.
  1653. *
  1654. * @return void
  1655. */
  1656. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1657. {
  1658. $this->Form->request->params['_Token'] = [
  1659. 'unlockedFields' => ['first_name', 'address']
  1660. ];
  1661. $this->Form->create();
  1662. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1663. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1664. $this->Form->text('Addresses.title');
  1665. $this->Form->text('Addresses.first_name');
  1666. $this->Form->text('Addresses.last_name');
  1667. $this->Form->text('Addresses.address');
  1668. $this->Form->text('Addresses.city');
  1669. $this->Form->text('Addresses.phone');
  1670. $result = $this->Form->fields;
  1671. $expected = [
  1672. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1673. 'Addresses.city', 'Addresses.phone'
  1674. ];
  1675. $this->assertEquals($expected, $result);
  1676. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1677. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1678. $tokenDebug = urlencode(json_encode([
  1679. '/articles/add',
  1680. [
  1681. 'Addresses.id' => '123456',
  1682. 'Addresses.title',
  1683. 'Addresses.last_name',
  1684. 'Addresses.city',
  1685. 'Addresses.phone'
  1686. ],
  1687. [
  1688. 'first_name',
  1689. 'address'
  1690. ]
  1691. ]));
  1692. $expected = [
  1693. 'div' => ['style' => 'display:none;'],
  1694. ['input' => [
  1695. 'type' => 'hidden',
  1696. 'name' => '_Token[fields]',
  1697. 'value' => $hash,
  1698. 'data-foo' => 'bar',
  1699. ]],
  1700. ['input' => [
  1701. 'type' => 'hidden',
  1702. 'name' => '_Token[unlocked]',
  1703. 'value' => 'address%7Cfirst_name',
  1704. 'data-foo' => 'bar',
  1705. ]],
  1706. ['input' => [
  1707. 'type' => 'hidden', 'name' => '_Token[debug]',
  1708. 'value' => $tokenDebug,
  1709. 'data-foo' => 'bar'
  1710. ]],
  1711. '/div'
  1712. ];
  1713. $this->assertHtml($expected, $result);
  1714. }
  1715. /**
  1716. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1717. *
  1718. * Debug is false, debugSecurity is true -> no debug
  1719. *
  1720. * @return void
  1721. */
  1722. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1723. {
  1724. $this->Form->request->params['_Token'] = [
  1725. 'unlockedFields' => ['first_name', 'address']
  1726. ];
  1727. $this->Form->create();
  1728. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1729. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1730. $this->Form->text('Addresses.title');
  1731. $this->Form->text('Addresses.first_name');
  1732. $this->Form->text('Addresses.last_name');
  1733. $this->Form->text('Addresses.address');
  1734. $this->Form->text('Addresses.city');
  1735. $this->Form->text('Addresses.phone');
  1736. $result = $this->Form->fields;
  1737. $expected = [
  1738. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1739. 'Addresses.city', 'Addresses.phone'
  1740. ];
  1741. $this->assertEquals($expected, $result);
  1742. Configure::write('debug', false);
  1743. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1744. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1745. $expected = [
  1746. 'div' => ['style' => 'display:none;'],
  1747. ['input' => [
  1748. 'type' => 'hidden',
  1749. 'name' => '_Token[fields]',
  1750. 'value' => $hash,
  1751. 'data-foo' => 'bar',
  1752. ]],
  1753. ['input' => [
  1754. 'type' => 'hidden',
  1755. 'name' => '_Token[unlocked]',
  1756. 'value' => 'address%7Cfirst_name',
  1757. 'data-foo' => 'bar',
  1758. ]],
  1759. '/div'
  1760. ];
  1761. $this->assertHtml($expected, $result);
  1762. }
  1763. /**
  1764. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1765. *
  1766. * Test single record form with debugSecurity param.
  1767. *
  1768. * @return void
  1769. */
  1770. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1771. {
  1772. $this->Form->request->params['_Token'] = [
  1773. 'unlockedFields' => ['first_name', 'address']
  1774. ];
  1775. $this->Form->create();
  1776. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1777. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1778. $this->Form->text('Addresses.title');
  1779. $this->Form->text('Addresses.first_name');
  1780. $this->Form->text('Addresses.last_name');
  1781. $this->Form->text('Addresses.address');
  1782. $this->Form->text('Addresses.city');
  1783. $this->Form->text('Addresses.phone');
  1784. $result = $this->Form->fields;
  1785. $expected = [
  1786. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1787. 'Addresses.city', 'Addresses.phone'
  1788. ];
  1789. $this->assertEquals($expected, $result);
  1790. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1791. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1792. $expected = [
  1793. 'div' => ['style' => 'display:none;'],
  1794. ['input' => [
  1795. 'type' => 'hidden',
  1796. 'name' => '_Token[fields]',
  1797. 'value' => $hash,
  1798. 'data-foo' => 'bar',
  1799. ]],
  1800. ['input' => [
  1801. 'type' => 'hidden',
  1802. 'name' => '_Token[unlocked]',
  1803. 'value' => 'address%7Cfirst_name',
  1804. 'data-foo' => 'bar',
  1805. ]],
  1806. '/div'
  1807. ];
  1808. $this->assertHtml($expected, $result);
  1809. }
  1810. /**
  1811. * testFormSecureWithCustomNameAttribute method
  1812. *
  1813. * Test securing inputs with custom name attributes.
  1814. *
  1815. * @return void
  1816. */
  1817. public function testFormSecureWithCustomNameAttribute()
  1818. {
  1819. $this->Form->request->params['_Token'] = 'testKey';
  1820. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1821. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1822. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1823. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1824. }
  1825. /**
  1826. * testFormSecuredControl method
  1827. *
  1828. * Test generation of entire secure form, assertions made on control() output.
  1829. *
  1830. * @return void
  1831. */
  1832. public function testFormSecuredControl()
  1833. {
  1834. $this->Form->request->params['_csrfToken'] = 'testKey';
  1835. $this->Form->request->params['_Token'] = 'stuff';
  1836. $this->article['schema'] = [
  1837. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1838. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1839. ];
  1840. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1841. $encoding = strtolower(Configure::read('App.encoding'));
  1842. $expected = [
  1843. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1844. 'div' => ['style' => 'display:none;'],
  1845. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1846. ['input' => [
  1847. 'type' => 'hidden',
  1848. 'name' => '_csrfToken',
  1849. 'value' => 'testKey'
  1850. ]],
  1851. '/div'
  1852. ];
  1853. $this->assertHtml($expected, $result);
  1854. $result = $this->Form->control('ratio');
  1855. $expected = [
  1856. 'div' => ['class'],
  1857. 'label' => ['for'],
  1858. 'Ratio',
  1859. '/label',
  1860. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1861. '/div',
  1862. ];
  1863. $this->assertHtml($expected, $result);
  1864. $result = $this->Form->control('population');
  1865. $expected = [
  1866. 'div' => ['class'],
  1867. 'label' => ['for'],
  1868. 'Population',
  1869. '/label',
  1870. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1871. '/div',
  1872. ];
  1873. $this->assertHtml($expected, $result);
  1874. $result = $this->Form->control('published', ['type' => 'text']);
  1875. $expected = [
  1876. 'div' => ['class' => 'input text'],
  1877. 'label' => ['for' => 'published'],
  1878. 'Published',
  1879. '/label',
  1880. ['input' => [
  1881. 'type' => 'text',
  1882. 'name' => 'published',
  1883. 'id' => 'published'
  1884. ]],
  1885. '/div'
  1886. ];
  1887. $this->assertHtml($expected, $result);
  1888. $result = $this->Form->control('other', ['type' => 'text']);
  1889. $expected = [
  1890. 'div' => ['class' => 'input text'],
  1891. 'label' => ['for' => 'other'],
  1892. 'Other',
  1893. '/label',
  1894. ['input' => [
  1895. 'type' => 'text',
  1896. 'name' => 'other',
  1897. 'id',
  1898. ]],
  1899. '/div'
  1900. ];
  1901. $this->assertHtml($expected, $result);
  1902. $result = $this->Form->hidden('stuff');
  1903. $expected = [
  1904. 'input' => [
  1905. 'type' => 'hidden',
  1906. 'name' => 'stuff'
  1907. ]
  1908. ];
  1909. $this->assertHtml($expected, $result);
  1910. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1911. $expected = ['input' => [
  1912. 'type' => 'hidden',
  1913. 'name' => 'hidden',
  1914. 'value' => '0'
  1915. ]];
  1916. $this->assertHtml($expected, $result);
  1917. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1918. $expected = [
  1919. 'div' => ['class' => 'input checkbox'],
  1920. ['input' => [
  1921. 'type' => 'hidden',
  1922. 'name' => 'something',
  1923. 'value' => '0'
  1924. ]],
  1925. 'label' => ['for' => 'something'],
  1926. ['input' => [
  1927. 'type' => 'checkbox',
  1928. 'name' => 'something',
  1929. 'value' => '1',
  1930. 'id' => 'something'
  1931. ]],
  1932. 'Something',
  1933. '/label',
  1934. '/div'
  1935. ];
  1936. $this->assertHtml($expected, $result);
  1937. $result = $this->Form->fields;
  1938. $expectedFields = [
  1939. 'ratio',
  1940. 'population',
  1941. 'published',
  1942. 'other',
  1943. 'stuff' => '',
  1944. 'hidden' => '0',
  1945. 'something'
  1946. ];
  1947. $this->assertEquals($expectedFields, $result);
  1948. $result = $this->Form->secure($this->Form->fields);
  1949. $tokenDebug = urlencode(json_encode([
  1950. '/articles/add',
  1951. $expectedFields,
  1952. []
  1953. ]));
  1954. $expected = [
  1955. 'div' => ['style' => 'display:none;'],
  1956. ['input' => [
  1957. 'type' => 'hidden',
  1958. 'name' => '_Token[fields]',
  1959. 'value'
  1960. ]],
  1961. ['input' => [
  1962. 'type' => 'hidden',
  1963. 'name' => '_Token[unlocked]',
  1964. 'value' => ''
  1965. ]],
  1966. ['input' => [
  1967. 'type' => 'hidden', 'name' => '_Token[debug]',
  1968. 'value' => $tokenDebug,
  1969. ]],
  1970. '/div'
  1971. ];
  1972. $this->assertHtml($expected, $result);
  1973. }
  1974. /**
  1975. * testSecuredControlCustomName method
  1976. *
  1977. * Test secured inputs with custom names.
  1978. *
  1979. * @return void
  1980. */
  1981. public function testSecuredControlCustomName()
  1982. {
  1983. $this->Form->request->params['_Token'] = 'testKey';
  1984. $this->assertEquals([], $this->Form->fields);
  1985. $this->Form->text('text_input', [
  1986. 'name' => 'Option[General.default_role]',
  1987. ]);
  1988. $expected = ['Option.General.default_role'];
  1989. $this->assertEquals($expected, $this->Form->fields);
  1990. $this->Form->select('select_box', [1, 2], [
  1991. 'name' => 'Option[General.select_role]',
  1992. ]);
  1993. $expected[] = 'Option.General.select_role';
  1994. $this->assertEquals($expected, $this->Form->fields);
  1995. $this->Form->text('other.things[]');
  1996. $expected[] = 'other.things';
  1997. $this->assertEquals($expected, $this->Form->fields);
  1998. }
  1999. /**
  2000. * testSecuredControlDuplicate method
  2001. *
  2002. * Test that a hidden field followed by a visible field
  2003. * undoes the hidden field locking.
  2004. *
  2005. * @return void
  2006. */
  2007. public function testSecuredControlDuplicate()
  2008. {
  2009. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2010. $this->assertEquals([], $this->Form->fields);
  2011. $this->Form->control('text_val', [
  2012. 'type' => 'hidden',
  2013. 'value' => 'some text',
  2014. ]);
  2015. $expected = ['text_val' => 'some text'];
  2016. $this->assertEquals($expected, $this->Form->fields);
  2017. $this->Form->control('text_val', [
  2018. 'type' => 'text',
  2019. ]);
  2020. $expected = ['text_val'];
  2021. $this->assertEquals($expected, $this->Form->fields);
  2022. }
  2023. /**
  2024. * testFormSecuredFileControl method
  2025. *
  2026. * Tests that the correct keys are added to the field hash index.
  2027. *
  2028. * @return void
  2029. */
  2030. public function testFormSecuredFileControl()
  2031. {
  2032. $this->assertEquals([], $this->Form->fields);
  2033. $this->Form->file('Attachment.file');
  2034. $expected = [
  2035. 'Attachment.file.name', 'Attachment.file.type',
  2036. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2037. 'Attachment.file.size'
  2038. ];
  2039. $this->assertEquals($expected, $this->Form->fields);
  2040. }
  2041. /**
  2042. * testFormSecuredMultipleSelect method
  2043. *
  2044. * Test that multiple selects keys are added to field hash.
  2045. *
  2046. * @return void
  2047. */
  2048. public function testFormSecuredMultipleSelect()
  2049. {
  2050. $this->Form->request->params['_csrfToken'] = 'testKey';
  2051. $this->assertEquals([], $this->Form->fields);
  2052. $options = ['1' => 'one', '2' => 'two'];
  2053. $this->Form->select('Model.select', $options);
  2054. $expected = ['Model.select'];
  2055. $this->assertEquals($expected, $this->Form->fields);
  2056. $this->Form->fields = [];
  2057. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2058. $this->assertEquals($expected, $this->Form->fields);
  2059. }
  2060. /**
  2061. * testFormSecuredRadio method
  2062. *
  2063. * @return void
  2064. */
  2065. public function testFormSecuredRadio()
  2066. {
  2067. $this->Form->request->params['_Token'] = 'testKey';
  2068. $this->assertEquals([], $this->Form->fields);
  2069. $options = ['1' => 'option1', '2' => 'option2'];
  2070. $this->Form->radio('Test.test', $options);
  2071. $expected = ['Test.test'];
  2072. $this->assertEquals($expected, $this->Form->fields);
  2073. $this->Form->radio('Test.all', $options, [
  2074. 'disabled' => ['option1', 'option2']
  2075. ]);
  2076. $expected = ['Test.test', 'Test.all' => ''];
  2077. $this->assertEquals($expected, $this->Form->fields);
  2078. $this->Form->radio('Test.some', $options, [
  2079. 'disabled' => ['option1']
  2080. ]);
  2081. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2082. $this->assertEquals($expected, $this->Form->fields);
  2083. }
  2084. /**
  2085. * testFormSecuredAndDisabledNotAssoc method
  2086. *
  2087. * Test that when disabled is in a list based attribute array it works.
  2088. *
  2089. * @return void
  2090. */
  2091. public function testFormSecuredAndDisabledNotAssoc()
  2092. {
  2093. $this->Form->request->params['_Token'] = 'testKey';
  2094. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2095. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2096. $this->Form->text('Model.text', ['disabled']);
  2097. $this->Form->textarea('Model.textarea', ['disabled']);
  2098. $this->Form->password('Model.password', ['disabled']);
  2099. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2100. $expected = [
  2101. 'Model.radio' => ''
  2102. ];
  2103. $this->assertEquals($expected, $this->Form->fields);
  2104. }
  2105. /**
  2106. * testFormSecuredAndDisabled method
  2107. *
  2108. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2109. * hashing.
  2110. *
  2111. * @return void
  2112. */
  2113. public function testFormSecuredAndDisabled()
  2114. {
  2115. $this->Form->request->params['_Token'] = 'testKey';
  2116. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2117. $this->Form->text('Model.text', ['disabled' => true]);
  2118. $this->Form->password('Model.text', ['disabled' => true]);
  2119. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2120. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2121. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2122. $this->Form->year('Model.year', ['disabled' => true]);
  2123. $this->Form->month('Model.month', ['disabled' => true]);
  2124. $this->Form->day('Model.day', ['disabled' => true]);
  2125. $this->Form->hour('Model.hour', ['disabled' => true]);
  2126. $this->Form->minute('Model.minute', ['disabled' => true]);
  2127. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2128. $expected = [
  2129. 'Model.radio' => ''
  2130. ];
  2131. $this->assertEquals($expected, $this->Form->fields);
  2132. }
  2133. /**
  2134. * testDisableSecurityUsingForm method
  2135. *
  2136. * @return void
  2137. */
  2138. public function testDisableSecurityUsingForm()
  2139. {
  2140. $this->Form->request->params['_Token'] = [
  2141. 'disabledFields' => []
  2142. ];
  2143. $this->Form->create();
  2144. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2145. $this->Form->text('Addresses.title');
  2146. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2147. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2148. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2149. $result = $this->Form->fields;
  2150. $expected = [
  2151. 'Addresses.id' => '123456', 'Addresses.title',
  2152. ];
  2153. $this->assertEquals($expected, $result);
  2154. }
  2155. /**
  2156. * testUnlockFieldAddsToList method
  2157. *
  2158. * Test disableField.
  2159. *
  2160. * @return void
  2161. */
  2162. public function testUnlockFieldAddsToList()
  2163. {
  2164. $this->Form->request->params['_Token'] = [
  2165. 'unlockedFields' => []
  2166. ];
  2167. $this->Form->unlockField('Contact.name');
  2168. $this->Form->text('Contact.name');
  2169. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2170. $this->assertEquals([], $this->Form->fields);
  2171. }
  2172. /**
  2173. * testUnlockFieldRemovingFromFields method
  2174. *
  2175. * Test unlockField removing from fields array.
  2176. *
  2177. * @return void
  2178. */
  2179. public function testUnlockFieldRemovingFromFields()
  2180. {
  2181. $this->Form->request->params['_Token'] = [
  2182. 'unlockedFields' => []
  2183. ];
  2184. $this->Form->create($this->article);
  2185. $this->Form->hidden('Article.id', ['value' => 1]);
  2186. $this->Form->text('Article.title');
  2187. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2188. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2189. $this->Form->unlockField('Article.title');
  2190. $this->Form->unlockField('Article.id');
  2191. $this->assertEquals([], $this->Form->fields);
  2192. }
  2193. /**
  2194. * testResetUnlockFields method
  2195. *
  2196. * Test reset unlockFields, when create new form.
  2197. *
  2198. * @return void
  2199. */
  2200. public function testResetUnlockFields()
  2201. {
  2202. $this->Form->request->params['_Token'] = [
  2203. 'key' => 'testKey',
  2204. 'unlockedFields' => []
  2205. ];
  2206. $this->Form->unlockField('Contact.id');
  2207. $this->Form->create('Contact');
  2208. $this->Form->hidden('Contact.id', ['value' => 1]);
  2209. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2210. $this->Form->end();
  2211. $this->Form->create('Contact');
  2212. $this->Form->hidden('Contact.id', ['value' => 1]);
  2213. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2214. }
  2215. /**
  2216. * testSecuredFormUrlIgnoresHost method
  2217. *
  2218. * Test that only the path + query elements of a form's URL show up in their hash.
  2219. *
  2220. * @return void
  2221. */
  2222. public function testSecuredFormUrlIgnoresHost()
  2223. {
  2224. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2225. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2226. $this->Form->create($this->article, [
  2227. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2228. ]);
  2229. $result = $this->Form->secure();
  2230. $this->assertContains($expected, $result);
  2231. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2232. $result = $this->Form->secure();
  2233. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2234. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2235. $result = $this->Form->secure();
  2236. $this->assertContains($expected, $result, 'URL path + query should work.');
  2237. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2238. $result = $this->Form->secure();
  2239. $this->assertNotContains($expected, $result, 'URL is different');
  2240. }
  2241. /**
  2242. * testSecuredFormUrlHasHtmlAndIdentifier method
  2243. *
  2244. * Test that URL, HTML and identifier show up in their hashes.
  2245. *
  2246. * @return void
  2247. */
  2248. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2249. {
  2250. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2251. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2252. $this->Form->create($this->article, [
  2253. 'url' => [
  2254. 'controller' => 'articles',
  2255. 'action' => 'view',
  2256. '?' => [
  2257. 'page' => 1,
  2258. 'limit' => 10,
  2259. 'html' => '<>"',
  2260. ],
  2261. '#' => 'result',
  2262. ],
  2263. ]);
  2264. $result = $this->Form->secure();
  2265. $this->assertContains($expected, $result);
  2266. $this->Form->create($this->article, [
  2267. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2268. ]);
  2269. $result = $this->Form->secure();
  2270. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2271. $this->Form->create($this->article, [
  2272. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2273. ]);
  2274. $result = $this->Form->secure();
  2275. $this->assertContains($expected, $result, 'URL path + query should work.');
  2276. }
  2277. /**
  2278. * testErrorMessageDisplay method
  2279. *
  2280. * Test error message display.
  2281. *
  2282. * @return void
  2283. */
  2284. public function testErrorMessageDisplay()
  2285. {
  2286. $this->article['errors'] = [
  2287. 'Article' => [
  2288. 'title' => 'error message',
  2289. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2290. ]
  2291. ];
  2292. $this->Form->create($this->article);
  2293. $result = $this->Form->control('Article.title');
  2294. $expected = [
  2295. 'div' => ['class' => 'input text error'],
  2296. 'label' => ['for' => 'article-title'],
  2297. 'Title',
  2298. '/label',
  2299. 'input' => [
  2300. 'type' => 'text', 'name' => 'Article[title]',
  2301. 'id' => 'article-title', 'class' => 'form-error'
  2302. ],
  2303. ['div' => ['class' => 'error-message']],
  2304. 'error message',
  2305. '/div',
  2306. '/div'
  2307. ];
  2308. $this->assertHtml($expected, $result);
  2309. $result = $this->Form->control('Article.title', [
  2310. 'templates' => [
  2311. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2312. ]
  2313. ]);
  2314. $expected = [
  2315. 'div' => ['class' => 'input text error'],
  2316. 'label' => ['for' => 'article-title'],
  2317. 'Title',
  2318. '/label',
  2319. 'input' => [
  2320. 'type' => 'text', 'name' => 'Article[title]',
  2321. 'id' => 'article-title', 'class' => 'form-error'
  2322. ],
  2323. '/div'
  2324. ];
  2325. $this->assertHtml($expected, $result);
  2326. $result = $this->Form->control('Article.content');
  2327. $expected = [
  2328. 'div' => ['class' => 'input text error'],
  2329. 'label' => ['for' => 'article-content'],
  2330. 'Content',
  2331. '/label',
  2332. 'input' => [
  2333. 'type' => 'text', 'name' => 'Article[content]',
  2334. 'id' => 'article-content', 'class' => 'form-error'
  2335. ],
  2336. ['div' => ['class' => 'error-message']],
  2337. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2338. '/div',
  2339. '/div'
  2340. ];
  2341. $this->assertHtml($expected, $result);
  2342. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2343. $expected = [
  2344. 'div' => ['class' => 'input text error'],
  2345. 'label' => ['for' => 'article-content'],
  2346. 'Content',
  2347. '/label',
  2348. 'input' => [
  2349. 'type' => 'text', 'name' => 'Article[content]',
  2350. 'id' => 'article-content', 'class' => 'form-error'
  2351. ],
  2352. ['div' => ['class' => 'error-message']],
  2353. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2354. '/div',
  2355. '/div'
  2356. ];
  2357. $this->assertHtml($expected, $result);
  2358. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2359. $expected = [
  2360. 'div' => ['class' => 'input text error'],
  2361. 'label' => ['for' => 'article-content'],
  2362. 'Content',
  2363. '/label',
  2364. 'input' => [
  2365. 'type' => 'text', 'name' => 'Article[content]',
  2366. 'id' => 'article-content', 'class' => 'form-error'
  2367. ],
  2368. ['div' => ['class' => 'error-message']],
  2369. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2370. '/div',
  2371. '/div'
  2372. ];
  2373. $this->assertHtml($expected, $result);
  2374. }
  2375. /**
  2376. * testEmptyErrorValidation method
  2377. *
  2378. * Test validation errors, when validation message is an empty string.
  2379. *
  2380. * @return void
  2381. */
  2382. public function testEmptyErrorValidation()
  2383. {
  2384. $this->article['errors'] = [
  2385. 'Article' => ['title' => '']
  2386. ];
  2387. $this->Form->create($this->article);
  2388. $result = $this->Form->control('Article.title');
  2389. $expected = [
  2390. 'div' => ['class' => 'input text error'],
  2391. 'label' => ['for' => 'article-title'],
  2392. 'Title',
  2393. '/label',
  2394. 'input' => [
  2395. 'type' => 'text', 'name' => 'Article[title]',
  2396. 'id' => 'article-title', 'class' => 'form-error'
  2397. ],
  2398. ['div' => ['class' => 'error-message']],
  2399. [],
  2400. '/div',
  2401. '/div'
  2402. ];
  2403. $this->assertHtml($expected, $result);
  2404. }
  2405. /**
  2406. * testEmptyControlErrorValidation method
  2407. *
  2408. * Test validation errors, when calling control() overriding validation message by an empty string.
  2409. *
  2410. * @return void
  2411. */
  2412. public function testEmptyControlErrorValidation()
  2413. {
  2414. $this->article['errors'] = [
  2415. 'Article' => ['title' => 'error message']
  2416. ];
  2417. $this->Form->create($this->article);
  2418. $result = $this->Form->control('Article.title', ['error' => '']);
  2419. $expected = [
  2420. 'div' => ['class' => 'input text error'],
  2421. 'label' => ['for' => 'article-title'],
  2422. 'Title',
  2423. '/label',
  2424. 'input' => [
  2425. 'type' => 'text', 'name' => 'Article[title]',
  2426. 'id' => 'article-title', 'class' => 'form-error'
  2427. ],
  2428. ['div' => ['class' => 'error-message']],
  2429. [],
  2430. '/div',
  2431. '/div'
  2432. ];
  2433. $this->assertHtml($expected, $result);
  2434. }
  2435. /**
  2436. * testControlErrorMessage method
  2437. *
  2438. * Test validation errors, when calling control() overriding validation messages.
  2439. *
  2440. * @return void
  2441. */
  2442. public function testControlErrorMessage()
  2443. {
  2444. $this->article['errors'] = [
  2445. 'title' => ['error message']
  2446. ];
  2447. $this->Form->create($this->article);
  2448. $result = $this->Form->control('title', [
  2449. 'error' => 'Custom error!'
  2450. ]);
  2451. $expected = [
  2452. 'div' => ['class' => 'input text required error'],
  2453. 'label' => ['for' => 'title'],
  2454. 'Title',
  2455. '/label',
  2456. 'input' => [
  2457. 'type' => 'text', 'name' => 'title',
  2458. 'id' => 'title', 'class' => 'form-error',
  2459. 'required' => 'required',
  2460. ],
  2461. ['div' => ['class' => 'error-message']],
  2462. 'Custom error!',
  2463. '/div',
  2464. '/div'
  2465. ];
  2466. $this->assertHtml($expected, $result);
  2467. $result = $this->Form->control('title', [
  2468. 'error' => ['error message' => 'Custom error!']
  2469. ]);
  2470. $expected = [
  2471. 'div' => ['class' => 'input text required error'],
  2472. 'label' => ['for' => 'title'],
  2473. 'Title',
  2474. '/label',
  2475. 'input' => [
  2476. 'type' => 'text',
  2477. 'name' => 'title',
  2478. 'id' => 'title',
  2479. 'class' => 'form-error',
  2480. 'required' => 'required'
  2481. ],
  2482. ['div' => ['class' => 'error-message']],
  2483. 'Custom error!',
  2484. '/div',
  2485. '/div'
  2486. ];
  2487. $this->assertHtml($expected, $result);
  2488. }
  2489. /**
  2490. * testFormValidationAssociated method
  2491. *
  2492. * Tests displaying errors for nested entities.
  2493. *
  2494. * @return void
  2495. */
  2496. public function testFormValidationAssociated()
  2497. {
  2498. $nested = new Entity(['foo' => 'bar']);
  2499. $nested->errors('foo', ['not a valid bar']);
  2500. $entity = new Entity(['nested' => $nested]);
  2501. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2502. $result = $this->Form->error('nested.foo');
  2503. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2504. }
  2505. /**
  2506. * testFormValidationAssociatedSecondLevel method
  2507. *
  2508. * Test form error display with associated model.
  2509. *
  2510. * @return void
  2511. */
  2512. public function testFormValidationAssociatedSecondLevel()
  2513. {
  2514. $inner = new Entity(['bar' => 'baz']);
  2515. $nested = new Entity(['foo' => $inner]);
  2516. $entity = new Entity(['nested' => $nested]);
  2517. $inner->errors('bar', ['not a valid one']);
  2518. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2519. $result = $this->Form->error('nested.foo.bar');
  2520. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2521. }
  2522. /**
  2523. * testFormValidationMultiRecord method
  2524. *
  2525. * Test form error display with multiple records.
  2526. *
  2527. * @return void
  2528. */
  2529. public function testFormValidationMultiRecord()
  2530. {
  2531. $one = new Entity();
  2532. $two = new Entity();
  2533. TableRegistry::get('Contacts', [
  2534. 'className' => __NAMESPACE__ . '\ContactsTable'
  2535. ]);
  2536. $one->set('email', '');
  2537. $one->errors('email', ['invalid email']);
  2538. $two->set('name', '');
  2539. $two->errors('name', ['This is wrong']);
  2540. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2541. $result = $this->Form->control('0.email');
  2542. $expected = [
  2543. 'div' => ['class' => 'input email error'],
  2544. 'label' => ['for' => '0-email'],
  2545. 'Email',
  2546. '/label',
  2547. 'input' => [
  2548. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2549. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2550. ],
  2551. ['div' => ['class' => 'error-message']],
  2552. 'invalid email',
  2553. '/div',
  2554. '/div'
  2555. ];
  2556. $this->assertHtml($expected, $result);
  2557. $result = $this->Form->control('1.name');
  2558. $expected = [
  2559. 'div' => ['class' => 'input text error'],
  2560. 'label' => ['for' => '1-name'],
  2561. 'Name',
  2562. '/label',
  2563. 'input' => [
  2564. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2565. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2566. ],
  2567. ['div' => ['class' => 'error-message']],
  2568. 'This is wrong',
  2569. '/div',
  2570. '/div'
  2571. ];
  2572. $this->assertHtml($expected, $result);
  2573. }
  2574. /**
  2575. * testControl method
  2576. *
  2577. * Test various incarnations of control().
  2578. *
  2579. * @return void
  2580. */
  2581. public function testControl()
  2582. {
  2583. TableRegistry::get('ValidateUsers', [
  2584. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2585. ]);
  2586. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2587. $result = $this->Form->control('ValidateUsers.balance');
  2588. $expected = [
  2589. 'div' => ['class'],
  2590. 'label' => ['for'],
  2591. 'Balance',
  2592. '/label',
  2593. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2594. '/div',
  2595. ];
  2596. $this->assertHtml($expected, $result);
  2597. $result = $this->Form->control('ValidateUser.cost_decimal');
  2598. $expected = [
  2599. 'div' => ['class'],
  2600. 'label' => ['for'],
  2601. 'Cost Decimal',
  2602. '/label',
  2603. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2604. '/div',
  2605. ];
  2606. $this->assertHtml($expected, $result);
  2607. $result = $this->Form->control('ValidateUser.null_decimal');
  2608. $expected = [
  2609. 'div' => ['class'],
  2610. 'label' => ['for'],
  2611. 'Null Decimal',
  2612. '/label',
  2613. 'input' => ['name', 'type' => 'number', 'id'],
  2614. '/div',
  2615. ];
  2616. $this->assertHtml($expected, $result);
  2617. }
  2618. /**
  2619. * testControlCustomization method
  2620. *
  2621. * Tests the input method and passing custom options.
  2622. *
  2623. * @return void
  2624. */
  2625. public function testControlCustomization()
  2626. {
  2627. TableRegistry::get('Contacts', [
  2628. 'className' => __NAMESPACE__ . '\ContactsTable'
  2629. ]);
  2630. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2631. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2632. $expected = [
  2633. 'div' => ['class' => 'input email'],
  2634. 'label' => ['for' => 'custom'],
  2635. 'Email',
  2636. '/label',
  2637. ['input' => [
  2638. 'type' => 'email', 'name' => 'Contact[email]',
  2639. 'id' => 'custom', 'maxlength' => 255
  2640. ]],
  2641. '/div'
  2642. ];
  2643. $this->assertHtml($expected, $result);
  2644. $result = $this->Form->control('Contact.email', [
  2645. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2646. ]);
  2647. $expected = [
  2648. '<div',
  2649. 'label' => ['for' => 'contact-email'],
  2650. 'Email',
  2651. '/label',
  2652. ['input' => [
  2653. 'type' => 'email', 'name' => 'Contact[email]',
  2654. 'id' => 'contact-email', 'maxlength' => 255
  2655. ]],
  2656. '/div'
  2657. ];
  2658. $this->assertHtml($expected, $result);
  2659. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2660. $expected = [
  2661. 'div' => ['class' => 'input text'],
  2662. 'label' => ['for' => 'contact-email'],
  2663. 'Email',
  2664. '/label',
  2665. ['input' => [
  2666. 'type' => 'text', 'name' => 'Contact[email]',
  2667. 'id' => 'contact-email', 'maxlength' => '255'
  2668. ]],
  2669. '/div'
  2670. ];
  2671. $this->assertHtml($expected, $result);
  2672. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2673. $expected = [
  2674. 'div' => ['class' => 'input text'],
  2675. 'label' => ['for' => 'contact-5-email'],
  2676. 'Email',
  2677. '/label',
  2678. ['input' => [
  2679. 'type' => 'text', 'name' => 'Contact[5][email]',
  2680. 'id' => 'contact-5-email', 'maxlength' => '255'
  2681. ]],
  2682. '/div'
  2683. ];
  2684. $this->assertHtml($expected, $result);
  2685. $result = $this->Form->control('Contact.password');
  2686. $expected = [
  2687. 'div' => ['class' => 'input password'],
  2688. 'label' => ['for' => 'contact-password'],
  2689. 'Password',
  2690. '/label',
  2691. ['input' => [
  2692. 'type' => 'password', 'name' => 'Contact[password]',
  2693. 'id' => 'contact-password'
  2694. ]],
  2695. '/div'
  2696. ];
  2697. $this->assertHtml($expected, $result);
  2698. $result = $this->Form->control('Contact.email', [
  2699. 'type' => 'file', 'class' => 'textbox'
  2700. ]);
  2701. $expected = [
  2702. 'div' => ['class' => 'input file'],
  2703. 'label' => ['for' => 'contact-email'],
  2704. 'Email',
  2705. '/label',
  2706. ['input' => [
  2707. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2708. 'id' => 'contact-email'
  2709. ]],
  2710. '/div'
  2711. ];
  2712. $this->assertHtml($expected, $result);
  2713. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2714. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2715. $result = $this->Form->control('phone');
  2716. $expected = [
  2717. 'div' => ['class' => 'input tel'],
  2718. 'label' => ['for' => 'phone'],
  2719. 'Phone',
  2720. '/label',
  2721. ['input' => [
  2722. 'type' => 'tel', 'name' => 'phone',
  2723. 'value' => 'Hello &amp; World &gt; weird chars',
  2724. 'id' => 'phone', 'maxlength' => 255
  2725. ]],
  2726. '/div'
  2727. ];
  2728. $this->assertHtml($expected, $result);
  2729. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2730. $this->Form->create();
  2731. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2732. $expected = [
  2733. 'div' => ['class' => 'input text'],
  2734. 'label' => ['for' => 'myId'],
  2735. 'Field',
  2736. '/label',
  2737. 'input' => [
  2738. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2739. 'value' => 'My value', 'id' => 'myId'
  2740. ],
  2741. '/div'
  2742. ];
  2743. $this->assertHtml($expected, $result);
  2744. $this->Form->request->data = [];
  2745. $entity->errors('field', 'Badness!');
  2746. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2747. $result = $this->Form->control('field');
  2748. $expected = [
  2749. 'div' => ['class' => 'input text error'],
  2750. 'label' => ['for' => 'field'],
  2751. 'Field',
  2752. '/label',
  2753. 'input' => [
  2754. 'type' => 'text', 'name' => 'field',
  2755. 'id' => 'field', 'class' => 'form-error'
  2756. ],
  2757. ['div' => ['class' => 'error-message']],
  2758. 'Badness!',
  2759. '/div',
  2760. '/div'
  2761. ];
  2762. $this->assertHtml($expected, $result);
  2763. $result = $this->Form->control('field', [
  2764. 'templates' => [
  2765. 'inputContainerError' => '{{content}}{{error}}',
  2766. 'error' => '<span class="error-message">{{content}}</span>'
  2767. ]
  2768. ]);
  2769. $expected = [
  2770. 'label' => ['for' => 'field'],
  2771. 'Field',
  2772. '/label',
  2773. 'input' => [
  2774. 'type' => 'text', 'name' => 'field',
  2775. 'id' => 'field', 'class' => 'form-error'
  2776. ],
  2777. ['span' => ['class' => 'error-message']],
  2778. 'Badness!',
  2779. '/span'
  2780. ];
  2781. $this->assertHtml($expected, $result);
  2782. $entity->errors('field', ['minLength'], true);
  2783. $result = $this->Form->control('field', [
  2784. 'error' => [
  2785. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2786. 'maxLength' => 'login too large'
  2787. ]
  2788. ]);
  2789. $expected = [
  2790. 'div' => ['class' => 'input text error'],
  2791. 'label' => ['for' => 'field'],
  2792. 'Field',
  2793. '/label',
  2794. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2795. ['div' => ['class' => 'error-message']],
  2796. 'Le login doit contenir au moins 2 caractères',
  2797. '/div',
  2798. '/div'
  2799. ];
  2800. $this->assertHtml($expected, $result);
  2801. $entity->errors('field', ['maxLength'], true);
  2802. $result = $this->Form->control('field', [
  2803. 'error' => [
  2804. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2805. 'maxLength' => 'login too large',
  2806. ]
  2807. ]);
  2808. $expected = [
  2809. 'div' => ['class' => 'input text error'],
  2810. 'label' => ['for' => 'field'],
  2811. 'Field',
  2812. '/label',
  2813. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2814. ['div' => ['class' => 'error-message']],
  2815. 'login too large',
  2816. '/div',
  2817. '/div'
  2818. ];
  2819. $this->assertHtml($expected, $result);
  2820. }
  2821. /**
  2822. * testControlWithTemplateFile method
  2823. *
  2824. * Test that control() accepts a template file.
  2825. *
  2826. * @return void
  2827. */
  2828. public function testControlWithTemplateFile()
  2829. {
  2830. $result = $this->Form->control('field', [
  2831. 'templates' => 'htmlhelper_tags'
  2832. ]);
  2833. $expected = [
  2834. 'label' => ['for' => 'field'],
  2835. 'Field',
  2836. '/label',
  2837. 'input' => [
  2838. 'type' => 'text', 'name' => 'field',
  2839. 'id' => 'field'
  2840. ],
  2841. ];
  2842. $this->assertHtml($expected, $result);
  2843. }
  2844. /**
  2845. * testNestedControlsEndWithBrackets method
  2846. *
  2847. * Test that nested inputs end with brackets.
  2848. *
  2849. * @return void
  2850. */
  2851. public function testNestedControlsEndWithBrackets()
  2852. {
  2853. $result = $this->Form->text('nested.text[]');
  2854. $expected = [
  2855. 'input' => [
  2856. 'type' => 'text', 'name' => 'nested[text][]'
  2857. ],
  2858. ];
  2859. $this->assertHtml($expected, $result);
  2860. $result = $this->Form->file('nested.file[]');
  2861. $expected = [
  2862. 'input' => [
  2863. 'type' => 'file', 'name' => 'nested[file][]'
  2864. ],
  2865. ];
  2866. $this->assertHtml($expected, $result);
  2867. }
  2868. /**
  2869. * testCreateIdPrefix method
  2870. *
  2871. * Test id prefix.
  2872. *
  2873. * @return void
  2874. */
  2875. public function testCreateIdPrefix()
  2876. {
  2877. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2878. $result = $this->Form->control('field');
  2879. $expected = [
  2880. 'div' => ['class' => 'input text'],
  2881. 'label' => ['for' => 'prefix-field'],
  2882. 'Field',
  2883. '/label',
  2884. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2885. '/div'
  2886. ];
  2887. $this->assertHtml($expected, $result);
  2888. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2889. $expected = [
  2890. 'div' => ['class' => 'input text'],
  2891. 'label' => ['for' => 'custom-id'],
  2892. 'Field',
  2893. '/label',
  2894. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2895. '/div'
  2896. ];
  2897. $this->assertHtml($expected, $result);
  2898. $result = $this->Form->radio('Model.field', ['option A']);
  2899. $expected = [
  2900. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2901. 'label' => ['for' => 'prefix-model-field-0'],
  2902. ['input' => [
  2903. 'type' => 'radio',
  2904. 'name' => 'Model[field]',
  2905. 'value' => '0',
  2906. 'id' => 'prefix-model-field-0'
  2907. ]],
  2908. 'option A',
  2909. '/label'
  2910. ];
  2911. $this->assertHtml($expected, $result);
  2912. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2913. $expected = [
  2914. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2915. 'label' => ['for' => 'prefix-model-field-0'],
  2916. ['input' => [
  2917. 'type' => 'radio',
  2918. 'name' => 'Model[field]',
  2919. 'value' => '0',
  2920. 'id' => 'prefix-model-field-0'
  2921. ]],
  2922. 'option A',
  2923. '/label'
  2924. ];
  2925. $this->assertHtml($expected, $result);
  2926. $result = $this->Form->select(
  2927. 'Model.multi_field',
  2928. ['first'],
  2929. ['multiple' => 'checkbox']
  2930. );
  2931. $expected = [
  2932. 'input' => [
  2933. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2934. ],
  2935. ['div' => ['class' => 'checkbox']],
  2936. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2937. ['input' => [
  2938. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2939. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2940. ]],
  2941. 'first',
  2942. '/label',
  2943. '/div',
  2944. ];
  2945. $this->assertHtml($expected, $result);
  2946. $this->Form->end();
  2947. $result = $this->Form->control('field');
  2948. $expected = [
  2949. 'div' => ['class' => 'input text'],
  2950. 'label' => ['for' => 'field'],
  2951. 'Field',
  2952. '/label',
  2953. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2954. '/div'
  2955. ];
  2956. $this->assertHtml($expected, $result);
  2957. }
  2958. /**
  2959. * testControlZero method
  2960. *
  2961. * Test that inputs with 0 can be created.
  2962. *
  2963. * @return void
  2964. */
  2965. public function testControlZero()
  2966. {
  2967. TableRegistry::get('Contacts', [
  2968. 'className' => __NAMESPACE__ . '\ContactsTable'
  2969. ]);
  2970. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2971. $result = $this->Form->control('0');
  2972. $expected = [
  2973. 'div' => ['class' => 'input text'],
  2974. 'label' => ['for' => '0'], '/label',
  2975. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2976. '/div'
  2977. ];
  2978. $this->assertHtml($expected, $result);
  2979. }
  2980. /**
  2981. * testControlCheckbox method
  2982. *
  2983. * Test control() with checkbox creation.
  2984. *
  2985. * @return void
  2986. */
  2987. public function testControlCheckbox()
  2988. {
  2989. $result = $this->Form->control('User.active', ['label' => false, 'checked' => true]);
  2990. $expected = [
  2991. 'div' => ['class' => 'input checkbox'],
  2992. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2993. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2994. '/div'
  2995. ];
  2996. $this->assertHtml($expected, $result);
  2997. $result = $this->Form->control('User.active', ['label' => false, 'checked' => 1]);
  2998. $expected = [
  2999. 'div' => ['class' => 'input checkbox'],
  3000. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  3001. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  3002. '/div'
  3003. ];
  3004. $this->assertHtml($expected, $result);
  3005. $result = $this->Form->control('User.active', ['label' => false, 'checked' => '1']);
  3006. $expected = [
  3007. 'div' => ['class' => 'input checkbox'],
  3008. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  3009. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  3010. '/div'
  3011. ];
  3012. $this->assertHtml($expected, $result);
  3013. $result = $this->Form->control('User.disabled', [
  3014. 'label' => 'Disabled',
  3015. 'type' => 'checkbox',
  3016. 'data-foo' => 'disabled'
  3017. ]);
  3018. $expected = [
  3019. 'div' => ['class' => 'input checkbox'],
  3020. 'input' => ['type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'],
  3021. 'label' => ['for' => 'user-disabled'],
  3022. ['input' => [
  3023. 'type' => 'checkbox',
  3024. 'name' => 'User[disabled]',
  3025. 'value' => '1',
  3026. 'id' => 'user-disabled',
  3027. 'data-foo' => 'disabled'
  3028. ]],
  3029. 'Disabled',
  3030. '/label',
  3031. '/div'
  3032. ];
  3033. $this->assertHtml($expected, $result);
  3034. $result = $this->Form->control('User.confirm', [
  3035. 'label' => 'Confirm <b>me</b>!',
  3036. 'type' => 'checkbox',
  3037. 'escape' => false
  3038. ]);
  3039. $expected = [
  3040. 'div' => ['class' => 'input checkbox'],
  3041. 'input' => ['type' => 'hidden', 'name' => 'User[confirm]', 'value' => '0'],
  3042. 'label' => ['for' => 'user-confirm'],
  3043. ['input' => [
  3044. 'type' => 'checkbox',
  3045. 'name' => 'User[confirm]',
  3046. 'value' => '1',
  3047. 'id' => 'user-confirm',
  3048. ]],
  3049. 'Confirm <b>me</b>!',
  3050. '/label',
  3051. '/div'
  3052. ];
  3053. $this->assertHtml($expected, $result);
  3054. }
  3055. /**
  3056. * testControlHidden method
  3057. *
  3058. * Test that control() does not create wrapping div and label tag for hidden fields.
  3059. *
  3060. * @return void
  3061. */
  3062. public function testControlHidden()
  3063. {
  3064. TableRegistry::get('ValidateUsers', [
  3065. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3066. ]);
  3067. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3068. $result = $this->Form->control('ValidateUser.id');
  3069. $expected = [
  3070. 'input' => ['name', 'type' => 'hidden', 'id']
  3071. ];
  3072. $this->assertHtml($expected, $result);
  3073. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3074. $expected = [
  3075. 'input' => ['name', 'type' => 'hidden', 'id']
  3076. ];
  3077. $this->assertHtml($expected, $result);
  3078. }
  3079. /**
  3080. * testControlDatetime method
  3081. *
  3082. * Test form->control() with datetime.
  3083. *
  3084. * @return void
  3085. */
  3086. public function testControlDatetime()
  3087. {
  3088. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3089. ->setMethods(['datetime'])
  3090. ->setConstructorArgs([new View()])
  3091. ->getMock();
  3092. $this->Form->expects($this->once())->method('datetime')
  3093. ->with('prueba', [
  3094. 'type' => 'datetime',
  3095. 'timeFormat' => 24,
  3096. 'minYear' => 2008,
  3097. 'maxYear' => 2011,
  3098. 'interval' => 15,
  3099. 'options' => null,
  3100. 'empty' => false,
  3101. 'id' => 'prueba',
  3102. 'required' => false,
  3103. 'templateVars' => []
  3104. ])
  3105. ->will($this->returnValue('This is it!'));
  3106. $result = $this->Form->control('prueba', [
  3107. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3108. 'maxYear' => 2011, 'interval' => 15
  3109. ]);
  3110. $expected = [
  3111. 'div' => ['class' => 'input datetime'],
  3112. '<label',
  3113. 'Prueba',
  3114. '/label',
  3115. 'This is it!',
  3116. '/div'
  3117. ];
  3118. $this->assertHtml($expected, $result);
  3119. }
  3120. /**
  3121. * testControlDatetimeIdPrefix method
  3122. *
  3123. * Test form->control() with datetime with id prefix.
  3124. *
  3125. * @return void
  3126. */
  3127. public function testControlDatetimeIdPrefix()
  3128. {
  3129. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3130. ->setMethods(['datetime'])
  3131. ->setConstructorArgs([new View()])
  3132. ->getMock();
  3133. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3134. $this->Form->expects($this->once())->method('datetime')
  3135. ->with('prueba', [
  3136. 'type' => 'datetime',
  3137. 'timeFormat' => 24,
  3138. 'minYear' => 2008,
  3139. 'maxYear' => 2011,
  3140. 'interval' => 15,
  3141. 'options' => null,
  3142. 'empty' => false,
  3143. 'id' => 'prefix-prueba',
  3144. 'required' => false,
  3145. 'templateVars' => []
  3146. ])
  3147. ->will($this->returnValue('This is it!'));
  3148. $result = $this->Form->control('prueba', [
  3149. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3150. 'maxYear' => 2011, 'interval' => 15
  3151. ]);
  3152. $expected = [
  3153. 'div' => ['class' => 'input datetime'],
  3154. '<label',
  3155. 'Prueba',
  3156. '/label',
  3157. 'This is it!',
  3158. '/div'
  3159. ];
  3160. $this->assertHtml($expected, $result);
  3161. }
  3162. /**
  3163. * testControlCheckboxWithDisabledElements method
  3164. *
  3165. * Test generating checkboxes with disabled elements.
  3166. *
  3167. * @return void
  3168. */
  3169. public function testControlCheckboxWithDisabledElements()
  3170. {
  3171. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3172. $result = $this->Form->control('Contact.multiple', [
  3173. 'multiple' => 'checkbox',
  3174. 'disabled' => 'disabled',
  3175. 'options' => $options
  3176. ]);
  3177. $expected = [
  3178. ['div' => ['class' => 'input select']],
  3179. ['label' => ['for' => "contact-multiple"]],
  3180. 'Multiple',
  3181. '/label',
  3182. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  3183. ['div' => ['class' => 'checkbox']],
  3184. ['label' => ['for' => "contact-multiple-1"]],
  3185. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  3186. 'One',
  3187. '/label',
  3188. '/div',
  3189. ['div' => ['class' => 'checkbox']],
  3190. ['label' => ['for' => "contact-multiple-2"]],
  3191. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  3192. 'Two',
  3193. '/label',
  3194. '/div',
  3195. ['div' => ['class' => 'checkbox']],
  3196. ['label' => ['for' => "contact-multiple-3"]],
  3197. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  3198. 'Three',
  3199. '/label',
  3200. '/div',
  3201. '/div'
  3202. ];
  3203. $this->assertHtml($expected, $result);
  3204. // make sure 50 does only disable 50, and not 50f5c0cf
  3205. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3206. $disabled = [50];
  3207. $expected = [
  3208. ['div' => ['class' => 'input select']],
  3209. ['label' => ['for' => "contact-multiple"]],
  3210. 'Multiple',
  3211. '/label',
  3212. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  3213. ['div' => ['class' => 'checkbox']],
  3214. ['label' => ['for' => "contact-multiple-50"]],
  3215. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  3216. 'Fifty',
  3217. '/label',
  3218. '/div',
  3219. ['div' => ['class' => 'checkbox']],
  3220. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  3221. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  3222. 'Stringy',
  3223. '/label',
  3224. '/div',
  3225. '/div'
  3226. ];
  3227. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3228. $this->assertHtml($expected, $result);
  3229. }
  3230. /**
  3231. * testControlWithLeadingInteger method
  3232. *
  3233. * Test input name with leading integer, ensure attributes are generated correctly.
  3234. *
  3235. * @return void
  3236. */
  3237. public function testControlWithLeadingInteger()
  3238. {
  3239. $result = $this->Form->text('0.Node.title');
  3240. $expected = [
  3241. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3242. ];
  3243. $this->assertHtml($expected, $result);
  3244. }
  3245. /**
  3246. * testControlSelectType method
  3247. *
  3248. * Test form->control() with select type inputs.
  3249. *
  3250. * @return void
  3251. */
  3252. public function testControlSelectType()
  3253. {
  3254. $result = $this->Form->control(
  3255. 'email',
  3256. [
  3257. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3258. );
  3259. $expected = [
  3260. 'div' => ['class' => 'input select'],
  3261. 'label' => ['for' => 'email'],
  3262. 'Email',
  3263. '/label',
  3264. ['select' => ['name' => 'email', 'id' => 'email']],
  3265. ['option' => ['value' => '']],
  3266. '/option',
  3267. ['option' => ['value' => 'è']],
  3268. 'Firést',
  3269. '/option',
  3270. ['option' => ['value' => 'é']],
  3271. 'Secoènd',
  3272. '/option',
  3273. '/select',
  3274. '/div'
  3275. ];
  3276. $this->assertHtml($expected, $result);
  3277. $result = $this->Form->control(
  3278. 'email',
  3279. [
  3280. 'options' => ['First', 'Second'], 'empty' => true]
  3281. );
  3282. $expected = [
  3283. 'div' => ['class' => 'input select'],
  3284. 'label' => ['for' => 'email'],
  3285. 'Email',
  3286. '/label',
  3287. ['select' => ['name' => 'email', 'id' => 'email']],
  3288. ['option' => ['value' => '']],
  3289. '/option',
  3290. ['option' => ['value' => '0']],
  3291. 'First',
  3292. '/option',
  3293. ['option' => ['value' => '1']],
  3294. 'Second',
  3295. '/option',
  3296. '/select',
  3297. '/div'
  3298. ];
  3299. $this->assertHtml($expected, $result);
  3300. $result = $this->Form->control('email', [
  3301. 'type' => 'select',
  3302. 'options' => new \ArrayObject(['First', 'Second']),
  3303. 'empty' => true
  3304. ]);
  3305. $this->assertHtml($expected, $result);
  3306. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3307. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3308. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3309. $expected = [
  3310. 'div' => ['class' => 'input select'],
  3311. 'label' => ['for' => 'model-user-id'],
  3312. 'User',
  3313. '/label',
  3314. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3315. ['option' => ['value' => '']],
  3316. '/option',
  3317. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3318. 'good',
  3319. '/option',
  3320. ['option' => ['value' => 'other']],
  3321. 'bad',
  3322. '/option',
  3323. '/select',
  3324. '/div'
  3325. ];
  3326. $this->assertHtml($expected, $result);
  3327. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3328. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3329. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3330. $expected = [
  3331. 'div' => ['class' => 'input select'],
  3332. 'label' => ['for' => 'thing-user-id'],
  3333. 'User',
  3334. '/label',
  3335. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3336. ['option' => ['value' => '']],
  3337. 'Some Empty',
  3338. '/option',
  3339. ['option' => ['value' => 'value']],
  3340. 'good',
  3341. '/option',
  3342. ['option' => ['value' => 'other']],
  3343. 'bad',
  3344. '/option',
  3345. '/select',
  3346. '/div'
  3347. ];
  3348. $this->assertHtml($expected, $result);
  3349. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3350. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3351. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3352. $expected = [
  3353. 'div' => ['class' => 'input select'],
  3354. 'label' => ['for' => 'thing-user-id'],
  3355. 'User',
  3356. '/label',
  3357. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3358. ['option' => ['value' => '']],
  3359. 'Some Empty',
  3360. '/option',
  3361. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3362. 'good',
  3363. '/option',
  3364. ['option' => ['value' => 'other']],
  3365. 'bad',
  3366. '/option',
  3367. '/select',
  3368. '/div'
  3369. ];
  3370. $this->assertHtml($expected, $result);
  3371. $this->Form->data = [];
  3372. $result = $this->Form->control('Publisher.id', [
  3373. 'label' => 'Publisher',
  3374. 'type' => 'select',
  3375. 'multiple' => 'checkbox',
  3376. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3377. ]);
  3378. $expected = [
  3379. ['div' => ['class' => 'input select']],
  3380. ['label' => ['for' => 'publisher-id']],
  3381. 'Publisher',
  3382. '/label',
  3383. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3384. ['div' => ['class' => 'checkbox']],
  3385. ['label' => ['for' => 'publisher-id-value-1']],
  3386. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3387. 'Label 1',
  3388. '/label',
  3389. '/div',
  3390. ['div' => ['class' => 'checkbox']],
  3391. ['label' => ['for' => 'publisher-id-value-2']],
  3392. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3393. 'Label 2',
  3394. '/label',
  3395. '/div',
  3396. '/div'
  3397. ];
  3398. $this->assertHtml($expected, $result);
  3399. }
  3400. /**
  3401. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3402. *
  3403. * Test that control() and a non standard primary key makes a hidden input by default.
  3404. *
  3405. * @return void
  3406. */
  3407. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3408. {
  3409. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3410. $this->Form->create($this->article);
  3411. $result = $this->Form->control('title');
  3412. $expected = [
  3413. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3414. ];
  3415. $this->assertHtml($expected, $result);
  3416. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3417. $this->Form->create($this->article);
  3418. $result = $this->Form->control('title');
  3419. $expected = [
  3420. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3421. ];
  3422. $this->assertHtml($expected, $result);
  3423. $result = $this->Form->control('body');
  3424. $expected = [
  3425. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3426. ];
  3427. $this->assertHtml($expected, $result);
  3428. }
  3429. /**
  3430. * testControlOverridingMagicSelectType method
  3431. *
  3432. * Test that overriding the magic select type widget is possible.
  3433. *
  3434. * @return void
  3435. */
  3436. public function testControlOverridingMagicSelectType()
  3437. {
  3438. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3439. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3440. $expected = [
  3441. 'div' => ['class' => 'input text'],
  3442. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3443. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3444. '/div'
  3445. ];
  3446. $this->assertHtml($expected, $result);
  3447. //Check that magic types still work for plural/singular vars
  3448. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3449. $result = $this->Form->control('Model.type');
  3450. $expected = [
  3451. 'div' => ['class' => 'input select'],
  3452. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3453. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3454. ['option' => ['value' => 'value']], 'good', '/option',
  3455. ['option' => ['value' => 'other']], 'bad', '/option',
  3456. '/select',
  3457. '/div'
  3458. ];
  3459. $this->assertHtml($expected, $result);
  3460. }
  3461. /**
  3462. * testControlMagicTypeDoesNotOverride method
  3463. *
  3464. * Test that inferred types do not override developer input.
  3465. *
  3466. * @return void
  3467. */
  3468. public function testControlMagicTypeDoesNotOverride()
  3469. {
  3470. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3471. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3472. $expected = [
  3473. 'div' => ['class' => 'input checkbox'],
  3474. ['input' => [
  3475. 'type' => 'hidden',
  3476. 'name' => 'Model[user]',
  3477. 'value' => 0,
  3478. ]],
  3479. 'label' => ['for' => 'model-user'],
  3480. ['input' => [
  3481. 'name' => 'Model[user]',
  3482. 'type' => 'checkbox',
  3483. 'id' => 'model-user',
  3484. 'value' => 1
  3485. ]],
  3486. 'User',
  3487. '/label',
  3488. '/div'
  3489. ];
  3490. $this->assertHtml($expected, $result);
  3491. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3492. $options = [
  3493. 1 => 'blue',
  3494. 2 => 'red'
  3495. ];
  3496. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3497. $expected = [
  3498. 'div' => ['class' => 'input select'],
  3499. 'label' => ['for' => 'tags-ids'],
  3500. 'Tags',
  3501. '/label',
  3502. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3503. ['div' => ['class' => 'checkbox']],
  3504. ['label' => ['for' => 'tags-ids-1']],
  3505. ['input' => [
  3506. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3507. 'value' => '1', 'name' => 'tags[_ids][]'
  3508. ]],
  3509. 'blue',
  3510. '/label',
  3511. '/div',
  3512. ['div' => ['class' => 'checkbox']],
  3513. ['label' => ['for' => 'tags-ids-2']],
  3514. ['input' => [
  3515. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3516. 'value' => '2', 'name' => 'tags[_ids][]'
  3517. ]],
  3518. 'red',
  3519. '/label',
  3520. '/div',
  3521. '/div'
  3522. ];
  3523. $this->assertHtml($expected, $result);
  3524. }
  3525. /**
  3526. * testControlMagicSelectForTypeNumber method
  3527. *
  3528. * Test that magic control() selects are created for type=number.
  3529. *
  3530. * @return void
  3531. */
  3532. public function testControlMagicSelectForTypeNumber()
  3533. {
  3534. TableRegistry::get('ValidateUsers', [
  3535. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3536. ]);
  3537. $entity = new Entity(['balance' => 1]);
  3538. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3539. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3540. $result = $this->Form->control('balance');
  3541. $expected = [
  3542. 'div' => ['class' => 'input select'],
  3543. 'label' => ['for' => 'balance'],
  3544. 'Balance',
  3545. '/label',
  3546. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3547. ['option' => ['value' => '0']],
  3548. 'nothing',
  3549. '/option',
  3550. ['option' => ['value' => '1', 'selected' => 'selected']],
  3551. 'some',
  3552. '/option',
  3553. ['option' => ['value' => '100']],
  3554. 'a lot',
  3555. '/option',
  3556. '/select',
  3557. '/div'
  3558. ];
  3559. $this->assertHtml($expected, $result);
  3560. }
  3561. /**
  3562. * testInvalidControlTypeOption method
  3563. *
  3564. * Test invalid 'input' type option to control() function.
  3565. *
  3566. * @expectedException \RuntimeException
  3567. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3568. * @return void
  3569. */
  3570. public function testInvalidControlTypeOption()
  3571. {
  3572. $this->Form->control('text', ['type' => 'input']);
  3573. }
  3574. /**
  3575. * testControlMagicSelectChangeToRadio method
  3576. *
  3577. * Test that magic control() selects can easily be converted into radio types without error.
  3578. *
  3579. * @return void
  3580. */
  3581. public function testControlMagicSelectChangeToRadio()
  3582. {
  3583. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3584. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3585. $this->assertContains('input type="radio"', $result);
  3586. }
  3587. /**
  3588. * testFormControlSubmit method
  3589. *
  3590. * Test correct results for form::control() and type submit.
  3591. *
  3592. * @return void
  3593. */
  3594. public function testFormControlSubmit()
  3595. {
  3596. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3597. $expected = [
  3598. 'div' => ['class' => 'submit'],
  3599. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3600. '/div'
  3601. ];
  3602. $this->assertHtml($expected, $result);
  3603. }
  3604. /**
  3605. * testFormControls method
  3606. *
  3607. * Test correct results from Form::controls().
  3608. *
  3609. * @return void
  3610. */
  3611. public function testFormControlsLegendFieldset()
  3612. {
  3613. $this->Form->create($this->article);
  3614. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3615. $expected = [
  3616. '<fieldset',
  3617. '<legend',
  3618. 'The Legend',
  3619. '/legend',
  3620. '*/fieldset',
  3621. ];
  3622. $this->assertHtml($expected, $result);
  3623. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3624. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3625. $this->assertContains('<fieldset>', $result);
  3626. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3627. $this->assertNotContains('<legend>', $result);
  3628. $this->assertNotContains('<fieldset>', $result);
  3629. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3630. $this->assertNotContains('<legend>', $result);
  3631. $this->assertNotContains('<fieldset>', $result);
  3632. $this->Form->create($this->article);
  3633. $this->Form->request->params['prefix'] = 'admin';
  3634. $this->Form->request->params['action'] = 'admin_edit';
  3635. $this->Form->request->params['controller'] = 'articles';
  3636. $result = $this->Form->allControls();
  3637. $expected = [
  3638. '<fieldset',
  3639. '<legend',
  3640. 'New Article',
  3641. '/legend',
  3642. '*/fieldset',
  3643. ];
  3644. $this->assertHtml($expected, $result);
  3645. $this->Form->create($this->article);
  3646. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3647. $expected = [
  3648. '<fieldset',
  3649. '<legend',
  3650. 'The Legend',
  3651. '/legend',
  3652. '*/fieldset',
  3653. ];
  3654. $this->assertHtml($expected, $result);
  3655. $this->Form->create($this->article);
  3656. $result = $this->Form->allControls([], [
  3657. 'fieldset' => [
  3658. 'class' => 'some-class some-other-class',
  3659. 'disabled' => true,
  3660. 'data-param' => 'a-param'
  3661. ],
  3662. 'legend' => 'The Legend'
  3663. ]);
  3664. $expected = [
  3665. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3666. '<legend',
  3667. 'The Legend',
  3668. '/legend',
  3669. '*/fieldset',
  3670. ];
  3671. $this->assertHtml($expected, $result);
  3672. }
  3673. /**
  3674. * testFormControls method
  3675. *
  3676. * Test the controls() method.
  3677. *
  3678. * @return void
  3679. */
  3680. public function testFormControls()
  3681. {
  3682. $this->Form->create($this->article);
  3683. $result = $this->Form->allControls();
  3684. $expected = [
  3685. '<fieldset',
  3686. '<legend', 'New Article', '/legend',
  3687. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3688. ['div' => ['class' => 'input select required']],
  3689. '*/div',
  3690. ['div' => ['class' => 'input text required']],
  3691. '*/div',
  3692. ['div' => ['class' => 'input text']],
  3693. '*/div',
  3694. ['div' => ['class' => 'input text']],
  3695. '*/div',
  3696. '/fieldset',
  3697. ];
  3698. $this->assertHtml($expected, $result);
  3699. $result = $this->Form->allControls([
  3700. 'published' => ['type' => 'boolean']
  3701. ]);
  3702. $expected = [
  3703. '<fieldset',
  3704. '<legend', 'New Article', '/legend',
  3705. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3706. ['div' => ['class' => 'input select required']],
  3707. '*/div',
  3708. ['div' => ['class' => 'input text required']],
  3709. '*/div',
  3710. ['div' => ['class' => 'input text']],
  3711. '*/div',
  3712. ['div' => ['class' => 'input boolean']],
  3713. '*/div',
  3714. '/fieldset',
  3715. ];
  3716. $this->assertHtml($expected, $result);
  3717. $this->Form->create($this->article);
  3718. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3719. $expected = [
  3720. 'fieldset' => [],
  3721. 'legend' => [],
  3722. 'Hello',
  3723. '/legend',
  3724. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3725. ['div' => ['class' => 'input select required']],
  3726. '*/div',
  3727. ['div' => ['class' => 'input text required']],
  3728. '*/div',
  3729. ['div' => ['class' => 'input text']],
  3730. '*/div',
  3731. ['div' => ['class' => 'input text']],
  3732. '*/div',
  3733. '/fieldset'
  3734. ];
  3735. $this->assertHtml($expected, $result);
  3736. $this->Form->create(false);
  3737. $expected = [
  3738. 'fieldset' => [],
  3739. ['div' => ['class' => 'input text']],
  3740. 'label' => ['for' => 'foo'],
  3741. 'Foo',
  3742. '/label',
  3743. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3744. '*/div',
  3745. '/fieldset'
  3746. ];
  3747. $result = $this->Form->allControls(
  3748. ['foo' => ['type' => 'text']],
  3749. ['legend' => false]
  3750. );
  3751. $this->assertHtml($expected, $result);
  3752. }
  3753. /**
  3754. * testFormControlsBlacklist method
  3755. *
  3756. * @return void
  3757. */
  3758. public function testFormControlsBlacklist()
  3759. {
  3760. $this->Form->create($this->article);
  3761. $result = $this->Form->allControls([
  3762. 'id' => false
  3763. ]);
  3764. $expected = [
  3765. '<fieldset',
  3766. '<legend', 'New Article', '/legend',
  3767. ['div' => ['class' => 'input select required']],
  3768. '*/div',
  3769. ['div' => ['class' => 'input text required']],
  3770. '*/div',
  3771. ['div' => ['class' => 'input text']],
  3772. '*/div',
  3773. ['div' => ['class' => 'input text']],
  3774. '*/div',
  3775. '/fieldset',
  3776. ];
  3777. $this->assertHtml($expected, $result);
  3778. $this->Form->create($this->article);
  3779. $result = $this->Form->allControls([
  3780. 'id' => []
  3781. ]);
  3782. $expected = [
  3783. '<fieldset',
  3784. '<legend', 'New Article', '/legend',
  3785. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3786. ['div' => ['class' => 'input select required']],
  3787. '*/div',
  3788. ['div' => ['class' => 'input text required']],
  3789. '*/div',
  3790. ['div' => ['class' => 'input text']],
  3791. '*/div',
  3792. ['div' => ['class' => 'input text']],
  3793. '*/div',
  3794. '/fieldset',
  3795. ];
  3796. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3797. }
  3798. /**
  3799. * testSelectAsCheckbox method
  3800. *
  3801. * Test multi-select widget with checkbox formatting.
  3802. *
  3803. * @return void
  3804. */
  3805. public function testSelectAsCheckbox()
  3806. {
  3807. $result = $this->Form->select(
  3808. 'Model.multi_field',
  3809. ['first', 'second', 'third'],
  3810. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3811. );
  3812. $expected = [
  3813. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3814. ['div' => ['class' => 'checkbox']],
  3815. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3816. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3817. 'first',
  3818. '/label',
  3819. '/div',
  3820. ['div' => ['class' => 'checkbox']],
  3821. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3822. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3823. 'second',
  3824. '/label',
  3825. '/div',
  3826. ['div' => ['class' => 'checkbox']],
  3827. ['label' => ['for' => 'model-multi-field-2']],
  3828. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3829. 'third',
  3830. '/label',
  3831. '/div',
  3832. ];
  3833. $this->assertHtml($expected, $result);
  3834. $result = $this->Form->select(
  3835. 'Model.multi_field',
  3836. ['1/2' => 'half'],
  3837. ['multiple' => 'checkbox']
  3838. );
  3839. $expected = [
  3840. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3841. ['div' => ['class' => 'checkbox']],
  3842. ['label' => ['for' => 'model-multi-field-1-2']],
  3843. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3844. 'half',
  3845. '/label',
  3846. '/div',
  3847. ];
  3848. $this->assertHtml($expected, $result);
  3849. }
  3850. /**
  3851. * testLabel method
  3852. *
  3853. * Test label generation.
  3854. *
  3855. * @return void
  3856. */
  3857. public function testLabel()
  3858. {
  3859. $result = $this->Form->label('Person.name');
  3860. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3861. $this->assertHtml($expected, $result);
  3862. $result = $this->Form->label('Person.name');
  3863. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3864. $this->assertHtml($expected, $result);
  3865. $result = $this->Form->label('Person.first_name');
  3866. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3867. $this->assertHtml($expected, $result);
  3868. $result = $this->Form->label('Person.first_name', 'Your first name');
  3869. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3870. $this->assertHtml($expected, $result);
  3871. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3872. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3873. $this->assertHtml($expected, $result);
  3874. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3875. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3876. $this->assertHtml($expected, $result);
  3877. $result = $this->Form->label('Person.first_name', '');
  3878. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3879. $this->assertHtml($expected, $result);
  3880. $result = $this->Form->label('Person.2.name', '');
  3881. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3882. $this->assertHtml($expected, $result);
  3883. }
  3884. /**
  3885. * testLabelContainControl method
  3886. *
  3887. * Test that label() can accept an input with the correct template vars.
  3888. *
  3889. * @return void
  3890. */
  3891. public function testLabelContainControl()
  3892. {
  3893. $this->Form->templates([
  3894. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3895. ]);
  3896. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3897. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3898. ]);
  3899. $expected = [
  3900. 'label' => ['for' => 'person-accept-terms'],
  3901. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3902. 'Accept',
  3903. '/label',
  3904. ];
  3905. $this->assertHtml($expected, $result);
  3906. }
  3907. /**
  3908. * testTextbox method
  3909. *
  3910. * Test textbox element generation.
  3911. *
  3912. * @return void
  3913. */
  3914. public function testTextbox()
  3915. {
  3916. $result = $this->Form->text('Model.field');
  3917. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3918. $this->assertHtml($expected, $result);
  3919. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3920. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3921. $this->assertHtml($expected, $result);
  3922. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3923. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3924. $this->assertHtml($expected, $result);
  3925. }
  3926. /**
  3927. * testTextBoxDataAndError method
  3928. *
  3929. * Test that text() hooks up with request data and error fields.
  3930. *
  3931. * @return void
  3932. */
  3933. public function testTextBoxDataAndError()
  3934. {
  3935. $this->article['errors'] = [
  3936. 'Contact' => ['text' => 'wrong']
  3937. ];
  3938. $this->Form->create($this->article);
  3939. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3940. $result = $this->Form->text('Model.text');
  3941. $expected = [
  3942. 'input' => [
  3943. 'type' => 'text',
  3944. 'name' => 'Model[text]',
  3945. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3946. ]
  3947. ];
  3948. $this->assertHtml($expected, $result);
  3949. $this->Form->request->data['Contact']['text'] = 'test';
  3950. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3951. $expected = [
  3952. 'input' => [
  3953. 'type' => 'text',
  3954. 'name' => 'Contact[text]',
  3955. 'value' => 'test',
  3956. 'id' => 'theID',
  3957. 'class' => 'form-error'
  3958. ]
  3959. ];
  3960. $this->assertHtml($expected, $result);
  3961. }
  3962. /**
  3963. * testDefaultValue method
  3964. *
  3965. * Test default value setting.
  3966. *
  3967. * @return void
  3968. */
  3969. public function testTextDefaultValue()
  3970. {
  3971. $this->Form->request->data['Model']['field'] = 'test';
  3972. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3973. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3974. $this->assertHtml($expected, $result);
  3975. unset($this->Form->request->data['Model']['field']);
  3976. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3977. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3978. $this->assertHtml($expected, $result);
  3979. $Articles = TableRegistry::get('Articles');
  3980. $title = $Articles->schema()->column('title');
  3981. $Articles->schema()->addColumn(
  3982. 'title',
  3983. ['default' => 'default title'] + $title
  3984. );
  3985. $entity = $Articles->newEntity();
  3986. $this->Form->create($entity);
  3987. // Get default value from schema
  3988. $result = $this->Form->text('title');
  3989. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  3990. $this->assertHtml($expected, $result);
  3991. // Don't get value from schema
  3992. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3993. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3994. $this->assertHtml($expected, $result);
  3995. // Custom default value overrides default value from schema
  3996. $result = $this->Form->text('title', ['default' => 'override default']);
  3997. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  3998. $this->assertHtml($expected, $result);
  3999. // Default value from schema is used only for new entities.
  4000. $entity->isNew(false);
  4001. $result = $this->Form->text('title');
  4002. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4003. $this->assertHtml($expected, $result);
  4004. }
  4005. /**
  4006. * testError method
  4007. *
  4008. * Test field error generation.
  4009. *
  4010. * @return void
  4011. */
  4012. public function testError()
  4013. {
  4014. $this->article['errors'] = [
  4015. 'Article' => ['field' => 'email']
  4016. ];
  4017. $this->Form->create($this->article);
  4018. $result = $this->Form->error('Article.field');
  4019. $expected = [
  4020. ['div' => ['class' => 'error-message']],
  4021. 'email',
  4022. '/div',
  4023. ];
  4024. $this->assertHtml($expected, $result);
  4025. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  4026. $expected = [
  4027. ['div' => ['class' => 'error-message']],
  4028. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4029. '/div',
  4030. ];
  4031. $this->assertHtml($expected, $result);
  4032. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  4033. $expected = [
  4034. ['div' => ['class' => 'error-message']],
  4035. '<strong', 'Badness!', '/strong',
  4036. '/div',
  4037. ];
  4038. $this->assertHtml($expected, $result);
  4039. }
  4040. /**
  4041. * testErrorRuleName method
  4042. *
  4043. * Test error translation can use rule names for translating.
  4044. *
  4045. * @return void
  4046. */
  4047. public function testErrorRuleName()
  4048. {
  4049. $this->article['errors'] = [
  4050. 'Article' => [
  4051. 'field' => ['email' => 'Your email was not good']
  4052. ]
  4053. ];
  4054. $this->Form->create($this->article);
  4055. $result = $this->Form->error('Article.field');
  4056. $expected = [
  4057. ['div' => ['class' => 'error-message']],
  4058. 'Your email was not good',
  4059. '/div',
  4060. ];
  4061. $this->assertHtml($expected, $result);
  4062. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4063. $expected = [
  4064. ['div' => ['class' => 'error-message']],
  4065. 'Email in use',
  4066. '/div',
  4067. ];
  4068. $this->assertHtml($expected, $result);
  4069. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4070. $expected = [
  4071. ['div' => ['class' => 'error-message']],
  4072. 'Email in use',
  4073. '/div',
  4074. ];
  4075. $this->assertHtml($expected, $result);
  4076. $result = $this->Form->error('Article.field', [
  4077. 'email' => 'Key is preferred',
  4078. 'Your email was not good' => 'Email in use'
  4079. ]);
  4080. $expected = [
  4081. ['div' => ['class' => 'error-message']],
  4082. 'Key is preferred',
  4083. '/div',
  4084. ];
  4085. $this->assertHtml($expected, $result);
  4086. }
  4087. /**
  4088. * testErrorMessages method
  4089. *
  4090. * Test error with nested lists.
  4091. *
  4092. * @return void
  4093. */
  4094. public function testErrorMessages()
  4095. {
  4096. $this->article['errors'] = [
  4097. 'Article' => ['field' => 'email']
  4098. ];
  4099. $this->Form->create($this->article);
  4100. $result = $this->Form->error('Article.field', [
  4101. 'email' => 'No good!'
  4102. ]);
  4103. $expected = [
  4104. 'div' => ['class' => 'error-message'],
  4105. 'No good!',
  4106. '/div'
  4107. ];
  4108. $this->assertHtml($expected, $result);
  4109. }
  4110. /**
  4111. * testErrorMultipleMessages method
  4112. *
  4113. * Test error() with multiple messages.
  4114. *
  4115. * @return void
  4116. */
  4117. public function testErrorMultipleMessages()
  4118. {
  4119. $this->article['errors'] = [
  4120. 'field' => ['notBlank', 'email', 'Something else']
  4121. ];
  4122. $this->Form->create($this->article);
  4123. $result = $this->Form->error('field', [
  4124. 'notBlank' => 'Cannot be empty',
  4125. 'email' => 'No good!'
  4126. ]);
  4127. $expected = [
  4128. 'div' => ['class' => 'error-message'],
  4129. 'ul' => [],
  4130. '<li', 'Cannot be empty', '/li',
  4131. '<li', 'No good!', '/li',
  4132. '<li', 'Something else', '/li',
  4133. '/ul',
  4134. '/div'
  4135. ];
  4136. $this->assertHtml($expected, $result);
  4137. }
  4138. /**
  4139. * testPassword method
  4140. *
  4141. * Test password element generation.
  4142. *
  4143. * @return void
  4144. */
  4145. public function testPassword()
  4146. {
  4147. $this->article['errors'] = [
  4148. 'Contact' => [
  4149. 'passwd' => 1
  4150. ]
  4151. ];
  4152. $this->Form->create($this->article);
  4153. $result = $this->Form->password('Contact.field');
  4154. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4155. $this->assertHtml($expected, $result);
  4156. $this->Form->request->data['Contact']['passwd'] = 'test';
  4157. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4158. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4159. $this->assertHtml($expected, $result);
  4160. }
  4161. /**
  4162. * testRadio method
  4163. *
  4164. * Test radio element set generation.
  4165. *
  4166. * @return void
  4167. */
  4168. public function testRadio()
  4169. {
  4170. $result = $this->Form->radio('Model.field', ['option A']);
  4171. $expected = [
  4172. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4173. 'label' => ['for' => 'model-field-0'],
  4174. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4175. 'option A',
  4176. '/label'
  4177. ];
  4178. $this->assertHtml($expected, $result);
  4179. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4180. $this->assertHtml($expected, $result);
  4181. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4182. $expected = [
  4183. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4184. ['label' => ['for' => 'model-field-0']],
  4185. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4186. 'option A',
  4187. '/label',
  4188. ['label' => ['for' => 'model-field-1']],
  4189. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4190. 'option B',
  4191. '/label',
  4192. ];
  4193. $this->assertHtml($expected, $result);
  4194. $result = $this->Form->radio(
  4195. 'Employee.gender',
  4196. ['male' => 'Male', 'female' => 'Female'],
  4197. ['form' => 'my-form']
  4198. );
  4199. $expected = [
  4200. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4201. ['label' => ['for' => 'employee-gender-male']],
  4202. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4203. 'Male',
  4204. '/label',
  4205. ['label' => ['for' => 'employee-gender-female']],
  4206. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4207. 'Female',
  4208. '/label',
  4209. ];
  4210. $this->assertHtml($expected, $result);
  4211. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4212. $expected = [
  4213. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4214. ['label' => ['for' => 'model-custom-0']],
  4215. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4216. 'option A',
  4217. '/label',
  4218. ['label' => ['for' => 'model-custom-1']],
  4219. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4220. 'option B',
  4221. '/label',
  4222. ];
  4223. $this->assertHtml($expected, $result);
  4224. $result = $this->Form->radio(
  4225. 'Employee.gender',
  4226. [
  4227. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4228. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4229. ]
  4230. );
  4231. $expected = [
  4232. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4233. ['label' => ['for' => 'employee-gender-male']],
  4234. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4235. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4236. 'Male',
  4237. '/label',
  4238. ['label' => ['for' => 'employee-gender-female']],
  4239. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4240. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4241. 'Female',
  4242. '/label',
  4243. ];
  4244. $this->assertHtml($expected, $result);
  4245. }
  4246. /**
  4247. * testRadioDefaultValue method
  4248. *
  4249. * Test default value setting on radio() method.
  4250. *
  4251. * @return void
  4252. */
  4253. public function testRadioDefaultValue()
  4254. {
  4255. $Articles = TableRegistry::get('Articles');
  4256. $title = $Articles->schema()->column('title');
  4257. $Articles->schema()->addColumn(
  4258. 'title',
  4259. ['default' => '1'] + $title
  4260. );
  4261. $this->Form->create($Articles->newEntity());
  4262. $result = $this->Form->radio('title', ['option A', 'option B']);
  4263. $expected = [
  4264. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4265. ['label' => ['for' => 'title-0']],
  4266. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4267. 'option A',
  4268. '/label',
  4269. ['label' => ['for' => 'title-1']],
  4270. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4271. 'option B',
  4272. '/label',
  4273. ];
  4274. $this->assertHtml($expected, $result);
  4275. }
  4276. /**
  4277. * testControlRadio method
  4278. *
  4279. * Test that input works with radio types.
  4280. *
  4281. * @return void
  4282. */
  4283. public function testControlRadio()
  4284. {
  4285. $result = $this->Form->control('test', [
  4286. 'type' => 'radio',
  4287. 'options' => ['A', 'B'],
  4288. ]);
  4289. $expected = [
  4290. ['div' => ['class' => 'input radio']],
  4291. '<label',
  4292. 'Test',
  4293. '/label',
  4294. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4295. ['label' => ['for' => 'test-0']],
  4296. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4297. 'A',
  4298. '/label',
  4299. ['label' => ['for' => 'test-1']],
  4300. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4301. 'B',
  4302. '/label',
  4303. '/div',
  4304. ];
  4305. $this->assertHtml($expected, $result);
  4306. $result = $this->Form->control('test', [
  4307. 'type' => 'radio',
  4308. 'options' => ['A', 'B'],
  4309. 'value' => '0'
  4310. ]);
  4311. $expected = [
  4312. ['div' => ['class' => 'input radio']],
  4313. '<label',
  4314. 'Test',
  4315. '/label',
  4316. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4317. ['label' => ['for' => 'test-0']],
  4318. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4319. 'A',
  4320. '/label',
  4321. ['label' => ['for' => 'test-1']],
  4322. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4323. 'B',
  4324. '/label',
  4325. '/div',
  4326. ];
  4327. $this->assertHtml($expected, $result);
  4328. $result = $this->Form->control('test', [
  4329. 'type' => 'radio',
  4330. 'options' => ['A', 'B'],
  4331. 'label' => false
  4332. ]);
  4333. $expected = [
  4334. ['div' => ['class' => 'input radio']],
  4335. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4336. ['label' => ['for' => 'test-0']],
  4337. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4338. 'A',
  4339. '/label',
  4340. ['label' => ['for' => 'test-1']],
  4341. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4342. 'B',
  4343. '/label',
  4344. '/div',
  4345. ];
  4346. $this->assertHtml($expected, $result);
  4347. }
  4348. /**
  4349. * testRadioNoLabel method
  4350. *
  4351. * Test that radio() works with label = false.
  4352. *
  4353. * @return void
  4354. */
  4355. public function testRadioNoLabel()
  4356. {
  4357. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4358. $expected = [
  4359. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4360. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4361. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4362. ];
  4363. $this->assertHtml($expected, $result);
  4364. }
  4365. /**
  4366. * testRadioControlInsideLabel method
  4367. *
  4368. * Test generating radio input inside label ala twitter bootstrap.
  4369. *
  4370. * @return void
  4371. */
  4372. public function testRadioControlInsideLabel()
  4373. {
  4374. $this->Form->templates([
  4375. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4376. 'radioWrapper' => '{{label}}'
  4377. ]);
  4378. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4379. //@codingStandardsIgnoreStart
  4380. $expected = [
  4381. ['input' => [
  4382. 'type' => 'hidden',
  4383. 'name' => 'Model[field]',
  4384. 'value' => ''
  4385. ]],
  4386. ['label' => ['for' => 'model-field-0']],
  4387. ['input' => [
  4388. 'type' => 'radio',
  4389. 'name' => 'Model[field]',
  4390. 'value' => '0',
  4391. 'id' => 'model-field-0'
  4392. ]],
  4393. 'option A',
  4394. '/label',
  4395. ['label' => ['for' => 'model-field-1']],
  4396. ['input' => [
  4397. 'type' => 'radio',
  4398. 'name' => 'Model[field]',
  4399. 'value' => '1',
  4400. 'id' => 'model-field-1'
  4401. ]],
  4402. 'option B',
  4403. '/label',
  4404. ];
  4405. //@codingStandardsIgnoreEnd
  4406. $this->assertHtml($expected, $result);
  4407. }
  4408. /**
  4409. * testRadioHiddenControlDisabling method
  4410. *
  4411. * Test disabling the hidden input for radio buttons.
  4412. *
  4413. * @return void
  4414. */
  4415. public function testRadioHiddenControlDisabling()
  4416. {
  4417. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4418. $expected = [
  4419. 'label' => ['for' => 'model-1-field-0'],
  4420. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4421. 'option A',
  4422. '/label'
  4423. ];
  4424. $this->assertHtml($expected, $result);
  4425. }
  4426. /**
  4427. * testRadioOutOfRange method
  4428. *
  4429. * Test radio element set generation.
  4430. *
  4431. * @return void
  4432. */
  4433. public function testRadioOutOfRange()
  4434. {
  4435. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4436. $expected = [
  4437. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4438. 'label' => ['for' => 'model-field-v'],
  4439. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4440. 'value',
  4441. '/label'
  4442. ];
  4443. $this->assertHtml($expected, $result);
  4444. }
  4445. /**
  4446. * testSelect method
  4447. *
  4448. * Test select element generation.
  4449. *
  4450. * @return void
  4451. */
  4452. public function testSelect()
  4453. {
  4454. $result = $this->Form->select('Model.field', []);
  4455. $expected = [
  4456. 'select' => ['name' => 'Model[field]'],
  4457. '/select'
  4458. ];
  4459. $this->assertHtml($expected, $result);
  4460. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4461. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4462. $expected = [
  4463. 'select' => ['name' => 'Model[field]'],
  4464. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4465. 'good',
  4466. '/option',
  4467. ['option' => ['value' => 'other']],
  4468. 'bad',
  4469. '/option',
  4470. '/select'
  4471. ];
  4472. $this->assertHtml($expected, $result);
  4473. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4474. $this->assertHtml($expected, $result);
  4475. $this->Form->request->data = [];
  4476. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4477. $expected = [
  4478. 'select' => ['name' => 'Model[field]'],
  4479. ['option' => ['value' => 'value']],
  4480. 'good',
  4481. '/option',
  4482. ['option' => ['value' => 'other']],
  4483. 'bad',
  4484. '/option',
  4485. '/select'
  4486. ];
  4487. $this->assertHtml($expected, $result);
  4488. $options = [
  4489. ['value' => 'first', 'text' => 'First'],
  4490. ['value' => 'first', 'text' => 'Another First'],
  4491. ];
  4492. $result = $this->Form->select(
  4493. 'Model.field',
  4494. $options,
  4495. ['escape' => false, 'empty' => false]
  4496. );
  4497. $expected = [
  4498. 'select' => ['name' => 'Model[field]'],
  4499. ['option' => ['value' => 'first']],
  4500. 'First',
  4501. '/option',
  4502. ['option' => ['value' => 'first']],
  4503. 'Another First',
  4504. '/option',
  4505. '/select'
  4506. ];
  4507. $this->assertHtml($expected, $result);
  4508. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4509. $result = $this->Form->select(
  4510. 'Model.contact_id',
  4511. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4512. ['escape' => false, 'empty' => 'pick something']
  4513. );
  4514. $expected = [
  4515. 'select' => ['name' => 'Model[contact_id]'],
  4516. ['option' => ['value' => '']], 'pick something', '/option',
  4517. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4518. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4519. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4520. '/select'
  4521. ];
  4522. $this->assertHtml($expected, $result);
  4523. $this->Form->request->data['Model']['field'] = 0;
  4524. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4525. $expected = [
  4526. 'select' => ['name' => 'Model[field]'],
  4527. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4528. ['option' => ['value' => '1']], 'Yes', '/option',
  4529. '/select'
  4530. ];
  4531. $this->assertHtml($expected, $result);
  4532. }
  4533. /**
  4534. * testSelectEscapeHtml method
  4535. *
  4536. * Test that select() escapes HTML.
  4537. *
  4538. * @return void
  4539. */
  4540. public function testSelectEscapeHtml()
  4541. {
  4542. $result = $this->Form->select(
  4543. 'Model.field',
  4544. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4545. ['empty' => false]
  4546. );
  4547. $expected = [
  4548. 'select' => ['name' => 'Model[field]'],
  4549. ['option' => ['value' => 'first']],
  4550. 'first &quot;html&quot; &lt;chars&gt;',
  4551. '/option',
  4552. ['option' => ['value' => 'second']],
  4553. 'value',
  4554. '/option',
  4555. '/select'
  4556. ];
  4557. $this->assertHtml($expected, $result);
  4558. $result = $this->Form->select(
  4559. 'Model.field',
  4560. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4561. ['escape' => false, 'empty' => false]
  4562. );
  4563. $expected = [
  4564. 'select' => ['name' => 'Model[field]'],
  4565. ['option' => ['value' => 'first']],
  4566. 'first "html" <chars>',
  4567. '/option',
  4568. ['option' => ['value' => 'second']],
  4569. 'value',
  4570. '/option',
  4571. '/select'
  4572. ];
  4573. $this->assertHtml($expected, $result);
  4574. }
  4575. /**
  4576. * testSelectRequired method
  4577. *
  4578. * Test select() with required and disabled attributes.
  4579. *
  4580. * @return void
  4581. */
  4582. public function testSelectRequired()
  4583. {
  4584. $this->article['required'] = [
  4585. 'user_id' => true
  4586. ];
  4587. $this->Form->create($this->article);
  4588. $result = $this->Form->select('user_id', ['option A']);
  4589. $expected = [
  4590. 'select' => [
  4591. 'name' => 'user_id',
  4592. 'required' => 'required'
  4593. ],
  4594. ['option' => ['value' => '0']], 'option A', '/option',
  4595. '/select'
  4596. ];
  4597. $this->assertHtml($expected, $result);
  4598. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4599. $expected = [
  4600. 'select' => [
  4601. 'name' => 'user_id',
  4602. 'disabled' => 'disabled'
  4603. ],
  4604. ['option' => ['value' => '0']], 'option A', '/option',
  4605. '/select'
  4606. ];
  4607. $this->assertHtml($expected, $result);
  4608. }
  4609. /**
  4610. * testNestedSelect method
  4611. *
  4612. * Test select element generation with optgroups.
  4613. *
  4614. * @return void
  4615. */
  4616. public function testNestedSelect()
  4617. {
  4618. $result = $this->Form->select(
  4619. 'Model.field',
  4620. [1 => 'One', 2 => 'Two', 'Three' => [
  4621. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4622. ]],
  4623. ['empty' => false]
  4624. );
  4625. $expected = [
  4626. 'select' => ['name' => 'Model[field]'],
  4627. ['option' => ['value' => 1]],
  4628. 'One',
  4629. '/option',
  4630. ['option' => ['value' => 2]],
  4631. 'Two',
  4632. '/option',
  4633. ['optgroup' => ['label' => 'Three']],
  4634. ['option' => ['value' => 3]],
  4635. 'Three',
  4636. '/option',
  4637. ['option' => ['value' => 4]],
  4638. 'Four',
  4639. '/option',
  4640. ['option' => ['value' => 5]],
  4641. 'Five',
  4642. '/option',
  4643. '/optgroup',
  4644. '/select'
  4645. ];
  4646. $this->assertHtml($expected, $result);
  4647. }
  4648. /**
  4649. * testSelectMultiple method
  4650. *
  4651. * Test generation of multiple select elements.
  4652. *
  4653. * @return void
  4654. */
  4655. public function testSelectMultiple()
  4656. {
  4657. $options = ['first', 'second', 'third'];
  4658. $result = $this->Form->select(
  4659. 'Model.multi_field',
  4660. $options,
  4661. ['form' => 'my-form', 'multiple' => true]
  4662. );
  4663. $expected = [
  4664. 'input' => [
  4665. 'type' => 'hidden',
  4666. 'name' => 'Model[multi_field]',
  4667. 'value' => '',
  4668. 'form' => 'my-form',
  4669. ],
  4670. 'select' => [
  4671. 'name' => 'Model[multi_field][]',
  4672. 'multiple' => 'multiple',
  4673. 'form' => 'my-form',
  4674. ],
  4675. ['option' => ['value' => '0']],
  4676. 'first',
  4677. '/option',
  4678. ['option' => ['value' => '1']],
  4679. 'second',
  4680. '/option',
  4681. ['option' => ['value' => '2']],
  4682. 'third',
  4683. '/option',
  4684. '/select'
  4685. ];
  4686. $this->assertHtml($expected, $result);
  4687. $result = $this->Form->select(
  4688. 'Model.multi_field',
  4689. $options,
  4690. ['multiple' => 'multiple', 'form' => 'my-form']
  4691. );
  4692. $this->assertHtml($expected, $result);
  4693. }
  4694. /**
  4695. * testCheckboxZeroValue method
  4696. *
  4697. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4698. *
  4699. * @return void
  4700. */
  4701. public function testCheckboxZeroValue()
  4702. {
  4703. $result = $this->Form->control('User.get_spam', [
  4704. 'type' => 'checkbox',
  4705. 'value' => '0',
  4706. 'hiddenField' => '1',
  4707. ]);
  4708. $expected = [
  4709. 'div' => ['class' => 'input checkbox'],
  4710. 'label' => ['for' => 'user-get-spam'],
  4711. ['input' => [
  4712. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4713. 'value' => '1'
  4714. ]],
  4715. ['input' => [
  4716. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4717. 'value' => '0', 'id' => 'user-get-spam'
  4718. ]],
  4719. 'Get Spam',
  4720. '/label',
  4721. '/div'
  4722. ];
  4723. $this->assertHtml($expected, $result);
  4724. }
  4725. /**
  4726. * testHabtmSelectBox method
  4727. *
  4728. * Test generation of habtm select boxes.
  4729. *
  4730. * @return void
  4731. */
  4732. public function testHabtmSelectBox()
  4733. {
  4734. $this->loadFixtures('Articles');
  4735. $options = [
  4736. 1 => 'blue',
  4737. 2 => 'red',
  4738. 3 => 'green'
  4739. ];
  4740. $tags = [
  4741. new Entity(['id' => 1, 'name' => 'blue']),
  4742. new Entity(['id' => 3, 'name' => 'green'])
  4743. ];
  4744. $article = new Article(['tags' => $tags]);
  4745. $this->Form->create($article);
  4746. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4747. $expected = [
  4748. 'div' => ['class' => 'input select'],
  4749. 'label' => ['for' => 'tags-ids'],
  4750. 'Tags',
  4751. '/label',
  4752. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4753. 'select' => [
  4754. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4755. 'multiple' => 'multiple'
  4756. ],
  4757. ['option' => ['value' => '1', 'selected' => 'selected']],
  4758. 'blue',
  4759. '/option',
  4760. ['option' => ['value' => '2']],
  4761. 'red',
  4762. '/option',
  4763. ['option' => ['value' => '3', 'selected' => 'selected']],
  4764. 'green',
  4765. '/option',
  4766. '/select',
  4767. '/div'
  4768. ];
  4769. $this->assertHtml($expected, $result);
  4770. // make sure only 50 is selected, and not 50f5c0cf
  4771. $options = [
  4772. '1' => 'blue',
  4773. '50f5c0cf' => 'red',
  4774. '50' => 'green'
  4775. ];
  4776. $tags = [
  4777. new Entity(['id' => 1, 'name' => 'blue']),
  4778. new Entity(['id' => 50, 'name' => 'green'])
  4779. ];
  4780. $article = new Article(['tags' => $tags]);
  4781. $this->Form->create($article);
  4782. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4783. $expected = [
  4784. 'div' => ['class' => 'input select'],
  4785. 'label' => ['for' => 'tags-ids'],
  4786. 'Tags',
  4787. '/label',
  4788. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4789. 'select' => [
  4790. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4791. 'multiple' => 'multiple'
  4792. ],
  4793. ['option' => ['value' => '1', 'selected' => 'selected']],
  4794. 'blue',
  4795. '/option',
  4796. ['option' => ['value' => '50f5c0cf']],
  4797. 'red',
  4798. '/option',
  4799. ['option' => ['value' => '50', 'selected' => 'selected']],
  4800. 'green',
  4801. '/option',
  4802. '/select',
  4803. '/div'
  4804. ];
  4805. $this->assertHtml($expected, $result);
  4806. $spacecraft = [
  4807. 1 => 'Orion',
  4808. 2 => 'Helios'
  4809. ];
  4810. $this->View->viewVars['spacecraft'] = $spacecraft;
  4811. $this->Form->create();
  4812. $result = $this->Form->control('spacecraft._ids');
  4813. $expected = [
  4814. 'div' => ['class' => 'input select'],
  4815. 'label' => ['for' => 'spacecraft-ids'],
  4816. 'Spacecraft',
  4817. '/label',
  4818. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4819. 'select' => [
  4820. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4821. 'multiple' => 'multiple'
  4822. ],
  4823. ['option' => ['value' => '1']],
  4824. 'Orion',
  4825. '/option',
  4826. ['option' => ['value' => '2']],
  4827. 'Helios',
  4828. '/option',
  4829. '/select',
  4830. '/div'
  4831. ];
  4832. $this->assertHtml($expected, $result);
  4833. }
  4834. /**
  4835. * testErrorsForBelongsToManySelect method
  4836. *
  4837. * Tests that errors for belongsToMany select fields are being
  4838. * picked up properly.
  4839. *
  4840. * @return void
  4841. */
  4842. public function testErrorsForBelongsToManySelect()
  4843. {
  4844. $spacecraft = [
  4845. 1 => 'Orion',
  4846. 2 => 'Helios'
  4847. ];
  4848. $this->View->viewVars['spacecraft'] = $spacecraft;
  4849. $article = new Article();
  4850. $article->errors('spacecraft', ['Invalid']);
  4851. $this->Form->create($article);
  4852. $result = $this->Form->control('spacecraft._ids');
  4853. $expected = [
  4854. ['div' => ['class' => 'input select error']],
  4855. 'label' => ['for' => 'spacecraft-ids'],
  4856. 'Spacecraft',
  4857. '/label',
  4858. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4859. 'select' => [
  4860. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4861. 'multiple' => 'multiple'
  4862. ],
  4863. ['option' => ['value' => '1']],
  4864. 'Orion',
  4865. '/option',
  4866. ['option' => ['value' => '2']],
  4867. 'Helios',
  4868. '/option',
  4869. '/select',
  4870. ['div' => ['class' => 'error-message']],
  4871. 'Invalid',
  4872. '/div',
  4873. '/div'
  4874. ];
  4875. $this->assertHtml($expected, $result);
  4876. }
  4877. /**
  4878. * testSelectMultipleCheckboxes method
  4879. *
  4880. * Test generation of multi select elements in checkbox format.
  4881. *
  4882. * @return void
  4883. */
  4884. public function testSelectMultipleCheckboxes()
  4885. {
  4886. $result = $this->Form->select(
  4887. 'Model.multi_field',
  4888. ['first', 'second', 'third'],
  4889. ['multiple' => 'checkbox']
  4890. );
  4891. $expected = [
  4892. 'input' => [
  4893. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4894. ],
  4895. ['div' => ['class' => 'checkbox']],
  4896. ['label' => ['for' => 'model-multi-field-0']],
  4897. ['input' => [
  4898. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4899. 'value' => '0', 'id' => 'model-multi-field-0'
  4900. ]],
  4901. 'first',
  4902. '/label',
  4903. '/div',
  4904. ['div' => ['class' => 'checkbox']],
  4905. ['label' => ['for' => 'model-multi-field-1']],
  4906. ['input' => [
  4907. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4908. 'value' => '1', 'id' => 'model-multi-field-1'
  4909. ]],
  4910. 'second',
  4911. '/label',
  4912. '/div',
  4913. ['div' => ['class' => 'checkbox']],
  4914. ['label' => ['for' => 'model-multi-field-2']],
  4915. ['input' => [
  4916. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4917. 'value' => '2', 'id' => 'model-multi-field-2'
  4918. ]],
  4919. 'third',
  4920. '/label',
  4921. '/div'
  4922. ];
  4923. $this->assertHtml($expected, $result);
  4924. $result = $this->Form->select(
  4925. 'Model.multi_field',
  4926. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4927. ['multiple' => 'checkbox']
  4928. );
  4929. $expected = [
  4930. 'input' => [
  4931. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4932. ],
  4933. ['div' => ['class' => 'checkbox']],
  4934. ['label' => ['for' => 'model-multi-field-a+']],
  4935. ['input' => [
  4936. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4937. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4938. ]],
  4939. 'first',
  4940. '/label',
  4941. '/div',
  4942. ['div' => ['class' => 'checkbox']],
  4943. ['label' => ['for' => 'model-multi-field-a++']],
  4944. ['input' => [
  4945. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4946. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4947. ]],
  4948. 'second',
  4949. '/label',
  4950. '/div',
  4951. ['div' => ['class' => 'checkbox']],
  4952. ['label' => ['for' => 'model-multi-field-a+++']],
  4953. ['input' => [
  4954. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4955. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4956. ]],
  4957. 'third',
  4958. '/label',
  4959. '/div'
  4960. ];
  4961. $this->assertHtml($expected, $result);
  4962. $result = $this->Form->select(
  4963. 'Model.multi_field',
  4964. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4965. ['multiple' => 'checkbox']
  4966. );
  4967. $expected = [
  4968. 'input' => [
  4969. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4970. ],
  4971. ['div' => ['class' => 'checkbox']],
  4972. ['label' => ['for' => 'model-multi-field-a-b']],
  4973. ['input' => [
  4974. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4975. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4976. ]],
  4977. 'first',
  4978. '/label',
  4979. '/div',
  4980. ['div' => ['class' => 'checkbox']],
  4981. ['label' => ['for' => 'model-multi-field-a-b1']],
  4982. ['input' => [
  4983. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4984. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4985. ]],
  4986. 'second',
  4987. '/label',
  4988. '/div',
  4989. ['div' => ['class' => 'checkbox']],
  4990. ['label' => ['for' => 'model-multi-field-a-b2']],
  4991. ['input' => [
  4992. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4993. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4994. ]],
  4995. 'third',
  4996. '/label',
  4997. '/div'
  4998. ];
  4999. $this->assertHtml($expected, $result);
  5000. }
  5001. /**
  5002. * testSelectMultipleCheckboxRequestData method
  5003. *
  5004. * Ensure that multiCheckbox reads from the request data.
  5005. *
  5006. * @return void
  5007. */
  5008. public function testSelectMultipleCheckboxRequestData()
  5009. {
  5010. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5011. $result = $this->Form->select(
  5012. 'Model.tags',
  5013. ['1' => 'first', 'Array' => 'Array'],
  5014. ['multiple' => 'checkbox']
  5015. );
  5016. $expected = [
  5017. 'input' => [
  5018. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5019. ],
  5020. ['div' => ['class' => 'checkbox']],
  5021. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5022. ['input' => [
  5023. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5024. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5025. ]],
  5026. 'first',
  5027. '/label',
  5028. '/div',
  5029. ['div' => ['class' => 'checkbox']],
  5030. ['label' => ['for' => 'model-tags-array']],
  5031. ['input' => [
  5032. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5033. 'value' => 'Array', 'id' => 'model-tags-array'
  5034. ]],
  5035. 'Array',
  5036. '/label',
  5037. '/div'
  5038. ];
  5039. $this->assertHtml($expected, $result);
  5040. }
  5041. /**
  5042. * testSelectMultipleCheckboxSecurity method
  5043. *
  5044. * Checks the security hash array generated for multiple-input checkbox elements.
  5045. *
  5046. * @return void
  5047. */
  5048. public function testSelectMultipleCheckboxSecurity()
  5049. {
  5050. $this->Form->request->params['_Token'] = 'testKey';
  5051. $this->assertEquals([], $this->Form->fields);
  5052. $this->Form->select(
  5053. 'Model.multi_field',
  5054. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5055. ['multiple' => 'checkbox']
  5056. );
  5057. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5058. $result = $this->Form->secure($this->Form->fields);
  5059. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5060. $this->assertRegExp('/"' . $key . '"/', $result);
  5061. }
  5062. /**
  5063. * testSelectMultipleSecureWithNoOptions method
  5064. *
  5065. * Multiple select elements should always be secured as they always participate
  5066. * in the POST data.
  5067. *
  5068. * @return void
  5069. */
  5070. public function testSelectMultipleSecureWithNoOptions()
  5071. {
  5072. $this->assertEquals([], $this->Form->fields);
  5073. $this->Form->select(
  5074. 'Model.select',
  5075. [],
  5076. ['multiple' => true]
  5077. );
  5078. $this->assertEquals(['Model.select'], $this->Form->fields);
  5079. }
  5080. /**
  5081. * testSelectNoSecureWithNoOptions method
  5082. *
  5083. * When a select box has no options it should not be added to the fields list
  5084. * as it always fail post validation.
  5085. *
  5086. * @return void
  5087. */
  5088. public function testSelectNoSecureWithNoOptions()
  5089. {
  5090. $this->Form->request->params['_Token'] = 'testkey';
  5091. $this->assertEquals([], $this->Form->fields);
  5092. $this->Form->select(
  5093. 'Model.select',
  5094. []
  5095. );
  5096. $this->assertEquals([], $this->Form->fields);
  5097. $this->Form->select(
  5098. 'Model.user_id',
  5099. [],
  5100. ['empty' => true]
  5101. );
  5102. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5103. }
  5104. /**
  5105. * testControlMultipleCheckboxes method
  5106. *
  5107. * Test control() resulting in multi select elements being generated.
  5108. *
  5109. * @return void
  5110. */
  5111. public function testControlMultipleCheckboxes()
  5112. {
  5113. $result = $this->Form->control('Model.multi_field', [
  5114. 'options' => ['first', 'second', 'third'],
  5115. 'multiple' => 'checkbox'
  5116. ]);
  5117. $expected = [
  5118. ['div' => ['class' => 'input select']],
  5119. ['label' => ['for' => 'model-multi-field']],
  5120. 'Multi Field',
  5121. '/label',
  5122. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5123. ['div' => ['class' => 'checkbox']],
  5124. ['label' => ['for' => 'model-multi-field-0']],
  5125. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5126. 'first',
  5127. '/label',
  5128. '/div',
  5129. ['div' => ['class' => 'checkbox']],
  5130. ['label' => ['for' => 'model-multi-field-1']],
  5131. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5132. 'second',
  5133. '/label',
  5134. '/div',
  5135. ['div' => ['class' => 'checkbox']],
  5136. ['label' => ['for' => 'model-multi-field-2']],
  5137. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5138. 'third',
  5139. '/label',
  5140. '/div',
  5141. '/div'
  5142. ];
  5143. $this->assertHtml($expected, $result);
  5144. $result = $this->Form->control('Model.multi_field', [
  5145. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5146. 'multiple' => 'checkbox'
  5147. ]);
  5148. $expected = [
  5149. ['div' => ['class' => 'input select']],
  5150. ['label' => ['for' => 'model-multi-field']],
  5151. 'Multi Field',
  5152. '/label',
  5153. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5154. ['div' => ['class' => 'checkbox']],
  5155. ['label' => ['for' => 'model-multi-field-a']],
  5156. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5157. 'first',
  5158. '/label',
  5159. '/div',
  5160. ['div' => ['class' => 'checkbox']],
  5161. ['label' => ['for' => 'model-multi-field-b']],
  5162. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5163. 'second',
  5164. '/label',
  5165. '/div',
  5166. ['div' => ['class' => 'checkbox']],
  5167. ['label' => ['for' => 'model-multi-field-c']],
  5168. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5169. 'third',
  5170. '/label',
  5171. '/div',
  5172. '/div'
  5173. ];
  5174. $this->assertHtml($expected, $result);
  5175. }
  5176. /**
  5177. * testSelectHiddenFieldOmission method
  5178. *
  5179. * Test that select() with 'hiddenField' => false omits the hidden field.
  5180. *
  5181. * @return void
  5182. */
  5183. public function testSelectHiddenFieldOmission()
  5184. {
  5185. $result = $this->Form->select(
  5186. 'Model.multi_field',
  5187. ['first', 'second'],
  5188. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5189. );
  5190. $this->assertNotContains('type="hidden"', $result);
  5191. }
  5192. /**
  5193. * testSelectCheckboxMultipleOverrideName method
  5194. *
  5195. * Test that select() with multiple = checkbox works with overriding name attribute.
  5196. *
  5197. * @return void
  5198. */
  5199. public function testSelectCheckboxMultipleOverrideName()
  5200. {
  5201. $result = $this->Form->select('category', ['1', '2'], [
  5202. 'multiple' => 'checkbox',
  5203. 'name' => 'fish',
  5204. ]);
  5205. $expected = [
  5206. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5207. ['div' => ['class' => 'checkbox']],
  5208. ['label' => ['for' => 'fish-0']],
  5209. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5210. '1',
  5211. '/label',
  5212. '/div',
  5213. ['div' => ['class' => 'checkbox']],
  5214. ['label' => ['for' => 'fish-1']],
  5215. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5216. '2',
  5217. '/label',
  5218. '/div'
  5219. ];
  5220. $this->assertHtml($expected, $result);
  5221. $result = $this->Form->multiCheckbox(
  5222. 'category',
  5223. new Collection(['1', '2']),
  5224. ['name' => 'fish']
  5225. );
  5226. $this->assertHtml($expected, $result);
  5227. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5228. 'name' => 'fish',
  5229. ]);
  5230. $this->assertHtml($expected, $result);
  5231. }
  5232. /**
  5233. * testControlMultiCheckbox method
  5234. *
  5235. * Test that control() works with multicheckbox.
  5236. *
  5237. * @return void
  5238. */
  5239. public function testControlMultiCheckbox()
  5240. {
  5241. $result = $this->Form->control('category', [
  5242. 'type' => 'multicheckbox',
  5243. 'options' => ['1', '2'],
  5244. ]);
  5245. $expected = [
  5246. ['div' => ['class' => 'input multicheckbox']],
  5247. '<label',
  5248. 'Category',
  5249. '/label',
  5250. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5251. ['div' => ['class' => 'checkbox']],
  5252. ['label' => ['for' => 'category-0']],
  5253. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5254. '1',
  5255. '/label',
  5256. '/div',
  5257. ['div' => ['class' => 'checkbox']],
  5258. ['label' => ['for' => 'category-1']],
  5259. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5260. '2',
  5261. '/label',
  5262. '/div',
  5263. '/div',
  5264. ];
  5265. $this->assertHtml($expected, $result);
  5266. }
  5267. /**
  5268. * testCheckbox method
  5269. *
  5270. * Test generation of checkboxes.
  5271. *
  5272. * @return void
  5273. */
  5274. public function testCheckbox()
  5275. {
  5276. $result = $this->Form->checkbox('Model.field');
  5277. $expected = [
  5278. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5279. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5280. ];
  5281. $this->assertHtml($expected, $result);
  5282. $result = $this->Form->checkbox('Model.field', [
  5283. 'id' => 'theID',
  5284. 'value' => 'myvalue',
  5285. 'form' => 'my-form',
  5286. ]);
  5287. $expected = [
  5288. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5289. ['input' => [
  5290. 'type' => 'checkbox', 'name' => 'Model[field]',
  5291. 'value' => 'myvalue', 'id' => 'theID',
  5292. 'form' => 'my-form',
  5293. ]]
  5294. ];
  5295. $this->assertHtml($expected, $result);
  5296. }
  5297. /**
  5298. * testCheckboxDefaultValue method
  5299. *
  5300. * Test default value setting on checkbox() method.
  5301. *
  5302. * @return void
  5303. */
  5304. public function testCheckboxDefaultValue()
  5305. {
  5306. $this->Form->request->data['Model']['field'] = false;
  5307. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5308. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5309. $this->assertHtml($expected, $result);
  5310. unset($this->Form->request->data['Model']['field']);
  5311. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5312. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5313. $this->assertHtml($expected, $result);
  5314. $this->Form->request->data['Model']['field'] = true;
  5315. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5316. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5317. $this->assertHtml($expected, $result);
  5318. unset($this->Form->request->data['Model']['field']);
  5319. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5320. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5321. $this->assertHtml($expected, $result);
  5322. $Articles = TableRegistry::get('Articles');
  5323. $Articles->schema()->addColumn(
  5324. 'published',
  5325. ['type' => 'boolean', 'null' => false, 'default' => true]
  5326. );
  5327. $this->Form->create($Articles->newEntity());
  5328. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5329. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5330. $this->assertHtml($expected, $result);
  5331. }
  5332. /**
  5333. * testCheckboxCheckedAndError method
  5334. *
  5335. * Test checkbox being checked or having errors.
  5336. *
  5337. * @return void
  5338. */
  5339. public function testCheckboxCheckedAndError()
  5340. {
  5341. $this->article['errors'] = [
  5342. 'published' => true
  5343. ];
  5344. $this->Form->request->data['published'] = 'myvalue';
  5345. $this->Form->create($this->article);
  5346. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5347. $expected = [
  5348. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5349. ['input' => [
  5350. 'type' => 'checkbox',
  5351. 'name' => 'published',
  5352. 'value' => 'myvalue',
  5353. 'id' => 'theID',
  5354. 'checked' => 'checked',
  5355. 'class' => 'form-error'
  5356. ]]
  5357. ];
  5358. $this->assertHtml($expected, $result);
  5359. $this->Form->request->data['published'] = '';
  5360. $result = $this->Form->checkbox('published');
  5361. $expected = [
  5362. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5363. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5364. ];
  5365. $this->assertHtml($expected, $result);
  5366. }
  5367. /**
  5368. * testCheckboxCustomNameAttribute method
  5369. *
  5370. * Test checkbox() with a custom name attribute.
  5371. *
  5372. * @return void
  5373. */
  5374. public function testCheckboxCustomNameAttribute()
  5375. {
  5376. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5377. $expected = [
  5378. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5379. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5380. ];
  5381. $this->assertHtml($expected, $result);
  5382. }
  5383. /**
  5384. * testCheckboxHiddenField method
  5385. *
  5386. * Test that the hidden input for checkboxes can be omitted or set to a
  5387. * specific value.
  5388. *
  5389. * @return void
  5390. */
  5391. public function testCheckboxHiddenField()
  5392. {
  5393. $result = $this->Form->checkbox('UserForm.something', [
  5394. 'hiddenField' => false
  5395. ]);
  5396. $expected = [
  5397. 'input' => [
  5398. 'type' => 'checkbox',
  5399. 'name' => 'UserForm[something]',
  5400. 'value' => '1'
  5401. ],
  5402. ];
  5403. $this->assertHtml($expected, $result);
  5404. $result = $this->Form->checkbox('UserForm.something', [
  5405. 'value' => 'Y',
  5406. 'hiddenField' => 'N',
  5407. ]);
  5408. $expected = [
  5409. ['input' => [
  5410. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5411. 'value' => 'N'
  5412. ]],
  5413. ['input' => [
  5414. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5415. 'value' => 'Y'
  5416. ]],
  5417. ];
  5418. $this->assertHtml($expected, $result);
  5419. }
  5420. /**
  5421. * testTime method
  5422. *
  5423. * Test the time type.
  5424. *
  5425. * @return void
  5426. */
  5427. public function testTime()
  5428. {
  5429. $result = $this->Form->time('start_time', [
  5430. 'timeFormat' => 12,
  5431. 'interval' => 5,
  5432. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5433. ]);
  5434. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5435. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5436. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5437. $this->assertNotContains('year', $result);
  5438. $this->assertNotContains('month', $result);
  5439. $this->assertNotContains('day', $result);
  5440. $result = $this->Form->time('start_time', [
  5441. 'timeFormat' => 12,
  5442. 'interval' => 5,
  5443. 'value' => '2014-03-08 16:30:00'
  5444. ]);
  5445. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5446. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5447. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5448. $this->assertNotContains('year', $result);
  5449. $this->assertNotContains('month', $result);
  5450. $this->assertNotContains('day', $result);
  5451. }
  5452. /**
  5453. * testTimeFormat24NoMeridian method
  5454. *
  5455. * Ensure that timeFormat=24 has no merdian.
  5456. *
  5457. * @return void.
  5458. */
  5459. public function testTimeFormat24NoMeridian()
  5460. {
  5461. $result = $this->Form->time('start_time', [
  5462. 'timeFormat' => 24,
  5463. 'interval' => 5,
  5464. 'value' => '2014-03-08 16:30:00'
  5465. ]);
  5466. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5467. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5468. $this->assertNotContains('meridian', $result);
  5469. $this->assertNotContains('pm', $result);
  5470. $this->assertNotContains('year', $result);
  5471. $this->assertNotContains('month', $result);
  5472. $this->assertNotContains('day', $result);
  5473. }
  5474. /**
  5475. * testDate method
  5476. *
  5477. * Test the date type.
  5478. *
  5479. * @return void
  5480. */
  5481. public function testDate()
  5482. {
  5483. $result = $this->Form->date('start_day', [
  5484. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5485. ]);
  5486. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5487. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5488. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5489. $this->assertNotContains('hour', $result);
  5490. $this->assertNotContains('minute', $result);
  5491. $this->assertNotContains('second', $result);
  5492. $this->assertNotContains('meridian', $result);
  5493. }
  5494. /**
  5495. * testDateTime method
  5496. *
  5497. * Test generation of date/time select elements.
  5498. *
  5499. * @return void
  5500. */
  5501. public function testDateTime()
  5502. {
  5503. extract($this->dateRegex);
  5504. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5505. $now = strtotime('now');
  5506. $expected = [
  5507. ['select' => ['name' => 'Contact[date][year]']],
  5508. ['option' => ['value' => '']],
  5509. '/option',
  5510. $yearsRegex,
  5511. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5512. date('Y', $now),
  5513. '/option',
  5514. '*/select',
  5515. ['select' => ['name' => 'Contact[date][month]']],
  5516. ['option' => ['value' => '']],
  5517. '/option',
  5518. $monthsRegex,
  5519. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5520. date('F', $now),
  5521. '/option',
  5522. '*/select',
  5523. ['select' => ['name' => 'Contact[date][day]']],
  5524. ['option' => ['value' => '']],
  5525. '/option',
  5526. $daysRegex,
  5527. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5528. date('j', $now),
  5529. '/option',
  5530. '*/select',
  5531. ['select' => ['name' => 'Contact[date][hour]']],
  5532. ['option' => ['value' => '']],
  5533. '/option',
  5534. $hoursRegex,
  5535. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5536. date('G', $now),
  5537. '/option',
  5538. '*/select',
  5539. ['select' => ['name' => 'Contact[date][minute]']],
  5540. ['option' => ['value' => '']],
  5541. '/option',
  5542. $minutesRegex,
  5543. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5544. date('i', $now),
  5545. '/option',
  5546. '*/select',
  5547. ];
  5548. $this->assertHtml($expected, $result);
  5549. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5550. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5551. $now = strtotime('now');
  5552. $expected = [
  5553. ['select' => ['name' => 'Contact[date][year]']],
  5554. $yearsRegex,
  5555. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5556. date('Y', $now),
  5557. '/option',
  5558. '*/select',
  5559. ['select' => ['name' => 'Contact[date][month]']],
  5560. $monthsRegex,
  5561. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5562. date('F', $now),
  5563. '/option',
  5564. '*/select',
  5565. ['select' => ['name' => 'Contact[date][day]']],
  5566. $daysRegex,
  5567. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5568. date('j', $now),
  5569. '/option',
  5570. '*/select',
  5571. ['select' => ['name' => 'Contact[date][hour]']],
  5572. $hoursRegex,
  5573. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5574. date('G', $now),
  5575. '/option',
  5576. '*/select',
  5577. ['select' => ['name' => 'Contact[date][minute]']],
  5578. $minutesRegex,
  5579. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5580. date('i', $now),
  5581. '/option',
  5582. '*/select',
  5583. ];
  5584. $this->assertHtml($expected, $result);
  5585. }
  5586. /**
  5587. * testDateTimeSecured method
  5588. *
  5589. * Test that datetime fields are added to protected fields list.
  5590. *
  5591. * @return void
  5592. */
  5593. public function testDateTimeSecured()
  5594. {
  5595. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5596. $this->Form->dateTime('Contact.date');
  5597. $expected = [
  5598. 'Contact.date.year',
  5599. 'Contact.date.month',
  5600. 'Contact.date.day',
  5601. 'Contact.date.hour',
  5602. 'Contact.date.minute',
  5603. ];
  5604. $this->assertEquals($expected, $this->Form->fields);
  5605. $this->Form->fields = [];
  5606. $this->Form->date('Contact.published');
  5607. $expected = [
  5608. 'Contact.published.year',
  5609. 'Contact.published.month',
  5610. 'Contact.published.day',
  5611. ];
  5612. $this->assertEquals($expected, $this->Form->fields);
  5613. }
  5614. /**
  5615. * testDateTimeSecuredDisabled method
  5616. *
  5617. * Test that datetime fields are added to protected fields list.
  5618. *
  5619. * @return void
  5620. */
  5621. public function testDateTimeSecuredDisabled()
  5622. {
  5623. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5624. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5625. $expected = [];
  5626. $this->assertEquals($expected, $this->Form->fields);
  5627. $this->Form->fields = [];
  5628. $this->Form->date('Contact.published', ['secure' => false]);
  5629. $expected = [];
  5630. $this->assertEquals($expected, $this->Form->fields);
  5631. }
  5632. /**
  5633. * testDatetimeEmpty method
  5634. *
  5635. * Test empty defaulting to true for datetime.
  5636. *
  5637. * @return void
  5638. */
  5639. public function testDatetimeEmpty()
  5640. {
  5641. extract($this->dateRegex);
  5642. $result = $this->Form->dateTime('Contact.date', [
  5643. 'timeFormat' => 12,
  5644. 'empty' => true,
  5645. 'default' => true
  5646. ]);
  5647. $expected = [
  5648. ['select' => ['name' => 'Contact[date][year]']],
  5649. $yearsRegex,
  5650. ['option' => ['value' => '']],
  5651. '/option',
  5652. '*/select',
  5653. ['select' => ['name' => 'Contact[date][month]']],
  5654. $monthsRegex,
  5655. ['option' => ['value' => '']],
  5656. '/option',
  5657. '*/select',
  5658. ['select' => ['name' => 'Contact[date][day]']],
  5659. $daysRegex,
  5660. ['option' => ['value' => '']],
  5661. '/option',
  5662. '*/select',
  5663. ['select' => ['name' => 'Contact[date][hour]']],
  5664. $hoursRegex,
  5665. ['option' => ['value' => '']],
  5666. '/option',
  5667. '*/select',
  5668. ['select' => ['name' => 'Contact[date][minute]']],
  5669. $minutesRegex,
  5670. ['option' => ['value' => '']],
  5671. '/option',
  5672. '*/select',
  5673. ['select' => ['name' => 'Contact[date][meridian]']],
  5674. $meridianRegex,
  5675. ['option' => ['value' => '']],
  5676. '/option',
  5677. '*/select'
  5678. ];
  5679. $this->assertHtml($expected, $result);
  5680. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5681. }
  5682. /**
  5683. * testDatetimeMinuteInterval method
  5684. *
  5685. * Test datetime with interval option.
  5686. *
  5687. * @return void
  5688. */
  5689. public function testDatetimeMinuteInterval()
  5690. {
  5691. extract($this->dateRegex);
  5692. $result = $this->Form->dateTime('Contact.date', [
  5693. 'interval' => 5,
  5694. 'value' => ''
  5695. ]);
  5696. $expected = [
  5697. ['select' => ['name' => 'Contact[date][year]']],
  5698. $yearsRegex,
  5699. ['option' => ['selected' => 'selected', 'value' => '']],
  5700. '/option',
  5701. '*/select',
  5702. ['select' => ['name' => 'Contact[date][month]']],
  5703. $monthsRegex,
  5704. ['option' => ['selected' => 'selected', 'value' => '']],
  5705. '/option',
  5706. '*/select',
  5707. ['select' => ['name' => 'Contact[date][day]']],
  5708. $daysRegex,
  5709. ['option' => ['selected' => 'selected', 'value' => '']],
  5710. '/option',
  5711. '*/select',
  5712. ['select' => ['name' => 'Contact[date][hour]']],
  5713. $hoursRegex,
  5714. ['option' => ['selected' => 'selected', 'value' => '']],
  5715. '/option',
  5716. '*/select',
  5717. ['select' => ['name' => 'Contact[date][minute]']],
  5718. $minutesRegex,
  5719. ['option' => ['selected' => 'selected', 'value' => '']],
  5720. '/option',
  5721. ['option' => ['value' => '00']],
  5722. '00',
  5723. '/option',
  5724. ['option' => ['value' => '05']],
  5725. '05',
  5726. '/option',
  5727. ['option' => ['value' => '10']],
  5728. '10',
  5729. '/option',
  5730. '*/select',
  5731. ];
  5732. $this->assertHtml($expected, $result);
  5733. }
  5734. /**
  5735. * testDateTimeRounding method
  5736. *
  5737. * Test dateTime with rounding.
  5738. *
  5739. * @return void
  5740. */
  5741. public function testDateTimeRounding()
  5742. {
  5743. $this->Form->request->data['Contact'] = [
  5744. 'date' => [
  5745. 'day' => '13',
  5746. 'month' => '12',
  5747. 'year' => '2010',
  5748. 'hour' => '04',
  5749. 'minute' => '19',
  5750. 'meridian' => 'AM'
  5751. ]
  5752. ];
  5753. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5754. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5755. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5756. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5757. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5758. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5759. }
  5760. /**
  5761. * testDatetimeWithDefault method
  5762. *
  5763. * Test that datetime() and default values work.
  5764. *
  5765. * @return void
  5766. */
  5767. public function testDatetimeWithDefault()
  5768. {
  5769. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5770. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5771. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5772. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5773. $result = $this->Form->dateTime('Contact.updated', [
  5774. 'default' => '2009-06-01 11:15:30'
  5775. ]);
  5776. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5777. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5778. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5779. }
  5780. /**
  5781. * testDateTimeAllZeros method
  5782. *
  5783. * Test datetime() with all zeros.
  5784. *
  5785. * @return void
  5786. */
  5787. public function testDateTimeAllZeros()
  5788. {
  5789. $result = $this->Form->dateTime('Contact.date', [
  5790. 'timeFormat' => false,
  5791. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5792. 'value' => '0000-00-00'
  5793. ]);
  5794. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5795. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5796. }
  5797. /**
  5798. * testDateTimeEmptyAsArray method
  5799. *
  5800. * @return void
  5801. */
  5802. public function testDateTimeEmptyAsArray()
  5803. {
  5804. $result = $this->Form->dateTime('Contact.date', [
  5805. 'empty' => [
  5806. 'day' => 'DAY',
  5807. 'month' => 'MONTH',
  5808. 'year' => 'YEAR',
  5809. 'hour' => 'HOUR',
  5810. 'minute' => 'MINUTE',
  5811. 'meridian' => false
  5812. ],
  5813. 'default' => true
  5814. ]);
  5815. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5816. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5817. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5818. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5819. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5820. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5821. $result = $this->Form->dateTime('Contact.date', [
  5822. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5823. 'default' => true
  5824. ]);
  5825. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5826. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5827. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5828. }
  5829. /**
  5830. * testFormDateTimeMulti method
  5831. *
  5832. * Test multiple datetime element generation.
  5833. *
  5834. * @return void
  5835. */
  5836. public function testFormDateTimeMulti()
  5837. {
  5838. extract($this->dateRegex);
  5839. $result = $this->Form->dateTime('Contact.1.updated');
  5840. $this->assertContains('Contact[1][updated][month]', $result);
  5841. $this->assertContains('Contact[1][updated][day]', $result);
  5842. $this->assertContains('Contact[1][updated][year]', $result);
  5843. $this->assertContains('Contact[1][updated][hour]', $result);
  5844. $this->assertContains('Contact[1][updated][minute]', $result);
  5845. }
  5846. /**
  5847. * testDateTimeLabelIdMatchesFirstControl method
  5848. *
  5849. * When changing the date format, the label should always focus the first select box when
  5850. * clicked.
  5851. *
  5852. * @return void
  5853. */
  5854. public function testDateTimeLabelIdMatchesFirstControl()
  5855. {
  5856. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5857. $this->assertContains('<label>Date</label>', $result);
  5858. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5859. $this->assertContains('<label>Date</label>', $result);
  5860. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5861. $this->assertContains('<label>Date</label>', $result);
  5862. }
  5863. /**
  5864. * testDateTimeSecondOptions method
  5865. *
  5866. * Test datetime second=true.
  5867. *
  5868. * @return void
  5869. */
  5870. public function testDateTimeSecondOptions()
  5871. {
  5872. $result = $this->Form->dateTime('updated', ['second' => true]);
  5873. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5874. $result = $this->Form->dateTime('updated', ['second' => []]);
  5875. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5876. $result = $this->Form->dateTime('updated', ['second' => null]);
  5877. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5878. $result = $this->Form->dateTime('updated', ['second' => false]);
  5879. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5880. }
  5881. /**
  5882. * testMonth method
  5883. *
  5884. * Test generation of a month input.
  5885. *
  5886. * @return void
  5887. */
  5888. public function testMonth()
  5889. {
  5890. $result = $this->Form->month('Model.field', ['value' => '']);
  5891. $expected = [
  5892. ['select' => ['name' => 'Model[field][month]']],
  5893. ['option' => ['value' => '', 'selected' => 'selected']],
  5894. '/option',
  5895. ['option' => ['value' => '01']],
  5896. date('F', strtotime('2008-01-01 00:00:00')),
  5897. '/option',
  5898. ['option' => ['value' => '02']],
  5899. date('F', strtotime('2008-02-01 00:00:00')),
  5900. '/option',
  5901. '*/select',
  5902. ];
  5903. $this->assertHtml($expected, $result);
  5904. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5905. $expected = [
  5906. ['select' => ['name' => 'Model[field][month]']],
  5907. ['option' => ['selected' => 'selected', 'value' => '']],
  5908. '/option',
  5909. ['option' => ['value' => '01']],
  5910. date('F', strtotime('2008-01-01 00:00:00')),
  5911. '/option',
  5912. ['option' => ['value' => '02']],
  5913. date('F', strtotime('2008-02-01 00:00:00')),
  5914. '/option',
  5915. '*/select',
  5916. ];
  5917. $this->assertHtml($expected, $result);
  5918. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5919. $expected = [
  5920. ['select' => ['name' => 'Model[field][month]']],
  5921. ['option' => ['selected' => 'selected', 'value' => '']],
  5922. '/option',
  5923. ['option' => ['value' => '01']],
  5924. '1',
  5925. '/option',
  5926. ['option' => ['value' => '02']],
  5927. '2',
  5928. '/option',
  5929. '*/select',
  5930. ];
  5931. $this->assertHtml($expected, $result);
  5932. $monthNames = [
  5933. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5934. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5935. ];
  5936. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5937. $expected = [
  5938. ['select' => ['name' => 'Model[field][month]']],
  5939. ['option' => ['value' => '']],
  5940. '/option',
  5941. ['option' => ['value' => '01', 'selected' => 'selected']],
  5942. 'Jan',
  5943. '/option',
  5944. ['option' => ['value' => '02']],
  5945. 'Feb',
  5946. '/option',
  5947. '*/select',
  5948. ];
  5949. $this->assertHtml($expected, $result);
  5950. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5951. $result = $this->Form->month('Project.release');
  5952. $expected = [
  5953. ['select' => ['name' => 'Project[release][month]']],
  5954. ['option' => ['value' => '']],
  5955. '/option',
  5956. ['option' => ['value' => '01']],
  5957. 'January',
  5958. '/option',
  5959. ['option' => ['value' => '02', 'selected' => 'selected']],
  5960. 'February',
  5961. '/option',
  5962. '*/select',
  5963. ];
  5964. $this->assertHtml($expected, $result);
  5965. $result = $this->Form->month('Contact.published', [
  5966. 'empty' => 'Published on',
  5967. ]);
  5968. $this->assertContains('Published on', $result);
  5969. }
  5970. /**
  5971. * testDay method
  5972. *
  5973. * Test generation of a day input.
  5974. *
  5975. * @return void
  5976. */
  5977. public function testDay()
  5978. {
  5979. extract($this->dateRegex);
  5980. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  5981. $expected = [
  5982. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  5983. ['option' => ['selected' => 'selected', 'value' => '']],
  5984. '/option',
  5985. ['option' => ['value' => '01']],
  5986. '1',
  5987. '/option',
  5988. ['option' => ['value' => '02']],
  5989. '2',
  5990. '/option',
  5991. $daysRegex,
  5992. '/select',
  5993. ];
  5994. $this->assertHtml($expected, $result);
  5995. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5996. $result = $this->Form->day('Model.field');
  5997. $expected = [
  5998. ['select' => ['name' => 'Model[field][day]']],
  5999. ['option' => ['value' => '']],
  6000. '/option',
  6001. ['option' => ['value' => '01']],
  6002. '1',
  6003. '/option',
  6004. ['option' => ['value' => '02']],
  6005. '2',
  6006. '/option',
  6007. $daysRegex,
  6008. ['option' => ['value' => '10', 'selected' => 'selected']],
  6009. '10',
  6010. '/option',
  6011. $daysRegex,
  6012. '/select',
  6013. ];
  6014. $this->assertHtml($expected, $result);
  6015. $this->Form->request->data['Model']['field'] = '';
  6016. $result = $this->Form->day('Model.field', ['value' => '10']);
  6017. $expected = [
  6018. ['select' => ['name' => 'Model[field][day]']],
  6019. ['option' => ['value' => '']],
  6020. '/option',
  6021. ['option' => ['value' => '01']],
  6022. '1',
  6023. '/option',
  6024. ['option' => ['value' => '02']],
  6025. '2',
  6026. '/option',
  6027. $daysRegex,
  6028. ['option' => ['value' => '10', 'selected' => 'selected']],
  6029. '10',
  6030. '/option',
  6031. $daysRegex,
  6032. '/select',
  6033. ];
  6034. $this->assertHtml($expected, $result);
  6035. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6036. $result = $this->Form->day('Project.release');
  6037. $expected = [
  6038. ['select' => ['name' => 'Project[release][day]']],
  6039. ['option' => ['value' => '']],
  6040. '/option',
  6041. ['option' => ['value' => '01']],
  6042. '1',
  6043. '/option',
  6044. ['option' => ['value' => '02']],
  6045. '2',
  6046. '/option',
  6047. $daysRegex,
  6048. ['option' => ['value' => '10', 'selected' => 'selected']],
  6049. '10',
  6050. '/option',
  6051. $daysRegex,
  6052. '/select',
  6053. ];
  6054. $this->assertHtml($expected, $result);
  6055. $result = $this->Form->day('Contact.published', [
  6056. 'empty' => 'Published on',
  6057. ]);
  6058. $this->assertContains('Published on', $result);
  6059. }
  6060. /**
  6061. * testMinute method
  6062. *
  6063. * Test generation of a minute input.
  6064. *
  6065. * @return void
  6066. */
  6067. public function testMinute()
  6068. {
  6069. extract($this->dateRegex);
  6070. $result = $this->Form->minute('Model.field', ['value' => '']);
  6071. $expected = [
  6072. ['select' => ['name' => 'Model[field][minute]']],
  6073. ['option' => ['selected' => 'selected', 'value' => '']],
  6074. '/option',
  6075. ['option' => ['value' => '00']],
  6076. '00',
  6077. '/option',
  6078. ['option' => ['value' => '01']],
  6079. '01',
  6080. '/option',
  6081. ['option' => ['value' => '02']],
  6082. '02',
  6083. '/option',
  6084. $minutesRegex,
  6085. '/select',
  6086. ];
  6087. $this->assertHtml($expected, $result);
  6088. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6089. $result = $this->Form->minute('Model.field');
  6090. $expected = [
  6091. ['select' => ['name' => 'Model[field][minute]']],
  6092. ['option' => ['value' => '']],
  6093. '/option',
  6094. ['option' => ['value' => '00']],
  6095. '00',
  6096. '/option',
  6097. ['option' => ['value' => '01']],
  6098. '01',
  6099. '/option',
  6100. ['option' => ['value' => '02']],
  6101. '02',
  6102. '/option',
  6103. $minutesRegex,
  6104. ['option' => ['value' => '12', 'selected' => 'selected']],
  6105. '12',
  6106. '/option',
  6107. $minutesRegex,
  6108. '/select',
  6109. ];
  6110. $this->assertHtml($expected, $result);
  6111. $this->Form->request->data['Model']['field'] = '';
  6112. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6113. $expected = [
  6114. ['select' => ['name' => 'Model[field][minute]']],
  6115. ['option' => ['selected' => 'selected', 'value' => '']],
  6116. '/option',
  6117. ['option' => ['value' => '00']],
  6118. '00',
  6119. '/option',
  6120. ['option' => ['value' => '05']],
  6121. '05',
  6122. '/option',
  6123. ['option' => ['value' => '10']],
  6124. '10',
  6125. '/option',
  6126. $minutesRegex,
  6127. '/select',
  6128. ];
  6129. $this->assertHtml($expected, $result);
  6130. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6131. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6132. $expected = [
  6133. ['select' => ['name' => 'Model[field][minute]']],
  6134. ['option' => ['value' => '']],
  6135. '/option',
  6136. ['option' => ['value' => '00']],
  6137. '00',
  6138. '/option',
  6139. ['option' => ['value' => '05']],
  6140. '05',
  6141. '/option',
  6142. ['option' => ['value' => '10', 'selected' => 'selected']],
  6143. '10',
  6144. '/option',
  6145. $minutesRegex,
  6146. '/select',
  6147. ];
  6148. $this->assertHtml($expected, $result);
  6149. }
  6150. /**
  6151. * testMeridian method
  6152. *
  6153. * Test generating an input for the meridian.
  6154. *
  6155. * @return void
  6156. */
  6157. public function testMeridian()
  6158. {
  6159. extract($this->dateRegex);
  6160. $now = new \DateTime();
  6161. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6162. $expected = [
  6163. ['select' => ['name' => 'Model[field][meridian]']],
  6164. ['option' => ['value' => '']],
  6165. '/option',
  6166. $meridianRegex,
  6167. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6168. $now->format('a'),
  6169. '/option',
  6170. '*/select'
  6171. ];
  6172. $this->assertHtml($expected, $result);
  6173. }
  6174. /**
  6175. * testHour method
  6176. *
  6177. * Test generation of an hour input.
  6178. *
  6179. * @return void
  6180. */
  6181. public function testHour()
  6182. {
  6183. extract($this->dateRegex);
  6184. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6185. $expected = [
  6186. ['select' => ['name' => 'Model[field][hour]']],
  6187. ['option' => ['selected' => 'selected', 'value' => '']],
  6188. '/option',
  6189. ['option' => ['value' => '01']],
  6190. '1',
  6191. '/option',
  6192. ['option' => ['value' => '02']],
  6193. '2',
  6194. '/option',
  6195. $hoursRegex,
  6196. '/select',
  6197. ];
  6198. $this->assertHtml($expected, $result);
  6199. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6200. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6201. $expected = [
  6202. ['select' => ['name' => 'Model[field][hour]']],
  6203. ['option' => ['value' => '']],
  6204. '/option',
  6205. ['option' => ['value' => '01']],
  6206. '1',
  6207. '/option',
  6208. ['option' => ['value' => '02']],
  6209. '2',
  6210. '/option',
  6211. $hoursRegex,
  6212. ['option' => ['value' => '12', 'selected' => 'selected']],
  6213. '12',
  6214. '/option',
  6215. '/select',
  6216. ];
  6217. $this->assertHtml($expected, $result);
  6218. $this->Form->request->data['Model']['field'] = '';
  6219. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6220. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6221. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6222. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6223. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6224. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6225. $expected = [
  6226. ['select' => ['name' => 'Model[field][hour]']],
  6227. ['option' => ['value' => '']],
  6228. '/option',
  6229. ['option' => ['value' => '00', 'selected' => 'selected']],
  6230. '0',
  6231. '/option',
  6232. ['option' => ['value' => '01']],
  6233. '1',
  6234. '/option',
  6235. ['option' => ['value' => '02']],
  6236. '2',
  6237. '/option',
  6238. $hoursRegex,
  6239. '/select',
  6240. ];
  6241. $this->assertHtml($expected, $result);
  6242. unset($this->Form->request->data['Model']['field']);
  6243. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6244. $thisHour = date('H');
  6245. $optValue = date('G');
  6246. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6247. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6248. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6249. $expected = [
  6250. ['select' => ['name' => 'Model[field][hour]']],
  6251. ['option' => ['value' => '']],
  6252. '/option',
  6253. ['option' => ['value' => '00']],
  6254. '0',
  6255. '/option',
  6256. ['option' => ['value' => '01', 'selected' => 'selected']],
  6257. '1',
  6258. '/option',
  6259. ['option' => ['value' => '02']],
  6260. '2',
  6261. '/option',
  6262. $hoursRegex,
  6263. '/select',
  6264. ];
  6265. $this->assertHtml($expected, $result);
  6266. }
  6267. /**
  6268. * testYear method
  6269. *
  6270. * Test generation of a year input.
  6271. *
  6272. * @return void
  6273. */
  6274. public function testYear()
  6275. {
  6276. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6277. $expected = [
  6278. ['select' => ['name' => 'Model[field][year]']],
  6279. ['option' => ['selected' => 'selected', 'value' => '']],
  6280. '/option',
  6281. ['option' => ['value' => '2007']],
  6282. '2007',
  6283. '/option',
  6284. ['option' => ['value' => '2006']],
  6285. '2006',
  6286. '/option',
  6287. '/select',
  6288. ];
  6289. $this->assertHtml($expected, $result);
  6290. $result = $this->Form->year('Model.field', [
  6291. 'value' => '',
  6292. 'minYear' => 2006,
  6293. 'maxYear' => 2007,
  6294. 'orderYear' => 'asc'
  6295. ]);
  6296. $expected = [
  6297. ['select' => ['name' => 'Model[field][year]']],
  6298. ['option' => ['selected' => 'selected', 'value' => '']],
  6299. '/option',
  6300. ['option' => ['value' => '2006']],
  6301. '2006',
  6302. '/option',
  6303. ['option' => ['value' => '2007']],
  6304. '2007',
  6305. '/option',
  6306. '/select',
  6307. ];
  6308. $this->assertHtml($expected, $result);
  6309. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6310. $result = $this->Form->year('Contact.published', [
  6311. 'empty' => false,
  6312. 'minYear' => 2006,
  6313. 'maxYear' => 2007,
  6314. ]);
  6315. $expected = [
  6316. ['select' => ['name' => 'Contact[published][year]']],
  6317. ['option' => ['value' => '2007']],
  6318. '2007',
  6319. '/option',
  6320. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6321. '2006',
  6322. '/option',
  6323. '/select',
  6324. ];
  6325. $this->assertHtml($expected, $result);
  6326. $result = $this->Form->year('Contact.published', [
  6327. 'empty' => 'Published on',
  6328. ]);
  6329. $this->assertContains('Published on', $result);
  6330. }
  6331. /**
  6332. * testControlDatetimePreEpoch method
  6333. *
  6334. * Test minYear being prior to the unix epoch.
  6335. *
  6336. * @return void
  6337. */
  6338. public function testControlDatetimePreEpoch()
  6339. {
  6340. $start = date('Y') - 80;
  6341. $end = date('Y') - 18;
  6342. $result = $this->Form->control('birth_year', [
  6343. 'type' => 'date',
  6344. 'label' => 'Birth Year',
  6345. 'minYear' => $start,
  6346. 'maxYear' => $end,
  6347. 'month' => false,
  6348. 'day' => false,
  6349. ]);
  6350. $this->assertContains('value="' . $start . '">' . $start, $result);
  6351. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6352. $this->assertNotContains('value="00">00', $result);
  6353. }
  6354. /**
  6355. * testYearAutoExpandRange method
  6356. *
  6357. * @return void
  6358. */
  6359. public function testYearAutoExpandRange()
  6360. {
  6361. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6362. $result = $this->Form->year('User.birthday');
  6363. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6364. $result = $matches[1];
  6365. $expected = range(date('Y') + 5, 1930);
  6366. $this->assertEquals($expected, $result);
  6367. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6368. $result = $this->Form->year('Project.release');
  6369. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6370. $result = $matches[1];
  6371. $expected = range(2050, date('Y') - 5);
  6372. $this->assertEquals($expected, $result);
  6373. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6374. $result = $this->Form->year('Project.release', [
  6375. 'minYear' => 1890,
  6376. 'maxYear' => 1900
  6377. ]);
  6378. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6379. $result = $matches[1];
  6380. $expected = range(1900, 1881);
  6381. $this->assertEquals($expected, $result);
  6382. }
  6383. /**
  6384. * testControlDate method
  6385. *
  6386. * Test that control() accepts the type of date and passes options in.
  6387. *
  6388. * @return void
  6389. */
  6390. public function testControlDate()
  6391. {
  6392. $this->Form->request->data = [
  6393. 'month_year' => ['month' => date('m')],
  6394. ];
  6395. $this->Form->create($this->article);
  6396. $result = $this->Form->control('month_year', [
  6397. 'label' => false,
  6398. 'type' => 'date',
  6399. 'minYear' => 2006,
  6400. 'maxYear' => 2008
  6401. ]);
  6402. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6403. $this->assertNotContains('value="2008" selected="selected"', $result);
  6404. }
  6405. /**
  6406. * testControlLabelFalse method
  6407. *
  6408. * Test the label option being set to false.
  6409. *
  6410. * @return void
  6411. */
  6412. public function testControlLabelFalse()
  6413. {
  6414. $this->Form->create($this->article);
  6415. $result = $this->Form->control('title', ['label' => false]);
  6416. $expected = [
  6417. 'div' => ['class' => 'input text required'],
  6418. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6419. '/div'
  6420. ];
  6421. $this->assertHtml($expected, $result);
  6422. }
  6423. /**
  6424. * testControlDateMaxYear method
  6425. *
  6426. * Let's say we want to only allow users born from 2006 to 2008 to register
  6427. * This being the first signup page, we still don't have any data.
  6428. *
  6429. * @return void
  6430. */
  6431. public function testControlDateMaxYear()
  6432. {
  6433. $this->Form->request->data = [];
  6434. $this->Form->create($this->article);
  6435. $result = $this->Form->control('birthday', [
  6436. 'label' => false,
  6437. 'type' => 'date',
  6438. 'minYear' => 2006,
  6439. 'maxYear' => 2008,
  6440. 'default' => true
  6441. ]);
  6442. $this->assertContains('value="2008" selected="selected"', $result);
  6443. $this->assertContains('value="2006"', $result);
  6444. $this->assertNotContains('value="2005"', $result);
  6445. $this->assertNotContains('value="2009"', $result);
  6446. }
  6447. /**
  6448. * testTextArea method
  6449. *
  6450. * Test generation of a textarea input.
  6451. *
  6452. * @return void
  6453. */
  6454. public function testTextArea()
  6455. {
  6456. $this->Form->request->data = ['field' => 'some test data'];
  6457. $result = $this->Form->textarea('field');
  6458. $expected = [
  6459. 'textarea' => ['name' => 'field', 'rows' => 5],
  6460. 'some test data',
  6461. '/textarea',
  6462. ];
  6463. $this->assertHtml($expected, $result);
  6464. $result = $this->Form->textarea('user.bio');
  6465. $expected = [
  6466. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6467. '/textarea',
  6468. ];
  6469. $this->assertHtml($expected, $result);
  6470. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6471. $result = $this->Form->textarea('field');
  6472. $expected = [
  6473. 'textarea' => ['name' => 'field', 'rows' => 5],
  6474. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6475. '/textarea',
  6476. ];
  6477. $this->assertHtml($expected, $result);
  6478. $this->Form->request->data = [
  6479. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6480. ];
  6481. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6482. $expected = [
  6483. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6484. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6485. '/textarea',
  6486. ];
  6487. $this->assertHtml($expected, $result);
  6488. $result = $this->Form->textarea('0.OtherModel.field');
  6489. $expected = [
  6490. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6491. '/textarea'
  6492. ];
  6493. $this->assertHtml($expected, $result);
  6494. }
  6495. /**
  6496. * testTextAreaWithStupidCharacters method
  6497. *
  6498. * Test text area with non-ascii characters.
  6499. *
  6500. * @return void
  6501. */
  6502. public function testTextAreaWithStupidCharacters()
  6503. {
  6504. $result = $this->Form->textarea('Post.content', [
  6505. 'value' => "GREAT®",
  6506. 'rows' => '15',
  6507. 'cols' => '75'
  6508. ]);
  6509. $expected = [
  6510. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6511. 'GREAT®',
  6512. '/textarea',
  6513. ];
  6514. $this->assertHtml($expected, $result);
  6515. }
  6516. /**
  6517. * testTextAreaMaxLength method
  6518. *
  6519. * Test textareas maxlength read from schema.
  6520. *
  6521. * @return void
  6522. */
  6523. public function testTextAreaMaxLength()
  6524. {
  6525. $this->Form->create([
  6526. 'schema' => [
  6527. 'stuff' => ['type' => 'string', 'length' => 10],
  6528. ]
  6529. ]);
  6530. $result = $this->Form->control('other', ['type' => 'textarea']);
  6531. $expected = [
  6532. 'div' => ['class' => 'input textarea'],
  6533. 'label' => ['for' => 'other'],
  6534. 'Other',
  6535. '/label',
  6536. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6537. '/textarea',
  6538. '/div'
  6539. ];
  6540. $this->assertHtml($expected, $result);
  6541. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6542. $expected = [
  6543. 'div' => ['class' => 'input textarea'],
  6544. 'label' => ['for' => 'stuff'],
  6545. 'Stuff',
  6546. '/label',
  6547. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6548. '/textarea',
  6549. '/div'
  6550. ];
  6551. $this->assertHtml($expected, $result);
  6552. }
  6553. /**
  6554. * testHiddenField method
  6555. *
  6556. * Test generation of a hidden input.
  6557. *
  6558. * @return void
  6559. */
  6560. public function testHiddenField()
  6561. {
  6562. $this->article['errors'] = [
  6563. 'field' => true
  6564. ];
  6565. $this->Form->request->data['field'] = 'test';
  6566. $this->Form->create($this->article);
  6567. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6568. $expected = [
  6569. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6570. $this->assertHtml($expected, $result);
  6571. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6572. $expected = [
  6573. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6574. ];
  6575. $this->assertHtml($expected, $result);
  6576. }
  6577. /**
  6578. * testFileUploadField method
  6579. *
  6580. * Test generation of a file upload input.
  6581. *
  6582. * @return void
  6583. */
  6584. public function testFileUploadField()
  6585. {
  6586. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6587. $result = $this->Form->file('Model.upload');
  6588. $this->assertHtml($expected, $result);
  6589. $this->Form->request->data['Model']['upload'] = [
  6590. 'name' => '', 'type' => '', 'tmp_name' => '',
  6591. 'error' => 4, 'size' => 0
  6592. ];
  6593. $result = $this->Form->file('Model.upload');
  6594. $this->assertHtml($expected, $result);
  6595. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6596. $result = $this->Form->file('Model.upload');
  6597. $this->assertHtml($expected, $result);
  6598. }
  6599. /**
  6600. * testFileUploadOnOtherModel method
  6601. *
  6602. * Test File upload input on a model not used in create().
  6603. *
  6604. * @return void
  6605. */
  6606. public function testFileUploadOnOtherModel()
  6607. {
  6608. $this->Form->create($this->article, ['type' => 'file']);
  6609. $result = $this->Form->file('ValidateProfile.city');
  6610. $expected = [
  6611. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6612. ];
  6613. $this->assertHtml($expected, $result);
  6614. }
  6615. /**
  6616. * testButton method
  6617. *
  6618. * Test generation of a form button.
  6619. *
  6620. * @return void
  6621. */
  6622. public function testButton()
  6623. {
  6624. $result = $this->Form->button('Hi');
  6625. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6626. $this->assertHtml($expected, $result);
  6627. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6628. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6629. $this->assertHtml($expected, $result);
  6630. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6631. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6632. $this->assertHtml($expected, $result);
  6633. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6634. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6635. $this->assertHtml($expected, $result);
  6636. $result = $this->Form->button('No type', ['type' => false]);
  6637. $expected = ['button' => [], 'No type', '/button'];
  6638. $this->assertHtml($expected, $result);
  6639. $result = $this->Form->button('Upload Text', [
  6640. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6641. 'escape' => false
  6642. ]);
  6643. $this->assertNotRegExp('/\&039/', $result);
  6644. }
  6645. /**
  6646. * testButtonUnlockedByDefault method
  6647. *
  6648. * Test that button() makes unlocked fields by default.
  6649. *
  6650. * @return void
  6651. */
  6652. public function testButtonUnlockedByDefault()
  6653. {
  6654. $this->Form->request->params['_csrfToken'] = 'secured';
  6655. $this->Form->button('Save', ['name' => 'save']);
  6656. $this->Form->button('Clear');
  6657. $result = $this->Form->unlockField();
  6658. $this->assertEquals(['save'], $result);
  6659. }
  6660. /**
  6661. * testPostButton method
  6662. *
  6663. * @return void
  6664. */
  6665. public function testPostButton()
  6666. {
  6667. $result = $this->Form->postButton('Hi', '/controller/action');
  6668. $expected = [
  6669. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6670. 'div' => ['style' => 'display:none;'],
  6671. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6672. '/div',
  6673. 'button' => ['type' => 'submit'],
  6674. 'Hi',
  6675. '/button',
  6676. '/form'
  6677. ];
  6678. $this->assertHtml($expected, $result);
  6679. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6680. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6681. }
  6682. /**
  6683. * testPostButtonMethodType method
  6684. *
  6685. * @return void
  6686. */
  6687. public function testPostButtonMethodType()
  6688. {
  6689. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6690. $expected = [
  6691. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6692. 'div' => ['style' => 'display:none;'],
  6693. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6694. '/div',
  6695. 'button' => ['type' => 'submit'],
  6696. 'Hi',
  6697. '/button',
  6698. '/form'
  6699. ];
  6700. $this->assertHtml($expected, $result);
  6701. }
  6702. /**
  6703. * testPostButtonFormOptions method
  6704. *
  6705. * @return void
  6706. */
  6707. public function testPostButtonFormOptions()
  6708. {
  6709. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6710. $expected = [
  6711. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6712. 'div' => ['style' => 'display:none;'],
  6713. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6714. '/div',
  6715. 'button' => ['type' => 'submit'],
  6716. 'Hi',
  6717. '/button',
  6718. '/form'
  6719. ];
  6720. $this->assertHtml($expected, $result);
  6721. }
  6722. /**
  6723. * testPostButtonNestedData method
  6724. *
  6725. * Test using postButton with N dimensional data.
  6726. *
  6727. * @return void
  6728. */
  6729. public function testPostButtonNestedData()
  6730. {
  6731. $data = [
  6732. 'one' => [
  6733. 'two' => [
  6734. 3, 4, 5
  6735. ]
  6736. ]
  6737. ];
  6738. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6739. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6740. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6741. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6742. }
  6743. /**
  6744. * testSecurePostButton method
  6745. *
  6746. * Test that postButton adds _Token fields.
  6747. *
  6748. * @return void
  6749. */
  6750. public function testSecurePostButton()
  6751. {
  6752. $this->Form->request->params['_csrfToken'] = 'testkey';
  6753. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6754. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6755. $tokenDebug = urlencode(json_encode([
  6756. '/posts/delete/1',
  6757. [],
  6758. []
  6759. ]));
  6760. $expected = [
  6761. 'form' => [
  6762. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6763. ],
  6764. ['div' => ['style' => 'display:none;']],
  6765. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6766. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6767. '/div',
  6768. 'button' => ['type' => 'submit'],
  6769. 'Delete',
  6770. '/button',
  6771. ['div' => ['style' => 'display:none;']],
  6772. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6773. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6774. ['input' => [
  6775. 'type' => 'hidden', 'name' => '_Token[debug]',
  6776. 'value' => $tokenDebug,
  6777. ]],
  6778. '/div',
  6779. '/form',
  6780. ];
  6781. $this->assertHtml($expected, $result);
  6782. }
  6783. /**
  6784. * testPostLink method
  6785. *
  6786. * @return void
  6787. */
  6788. public function testPostLink()
  6789. {
  6790. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6791. $expected = [
  6792. 'form' => [
  6793. 'method' => 'post', 'action' => '/posts/delete/1',
  6794. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6795. ],
  6796. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6797. '/form',
  6798. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6799. 'Delete',
  6800. '/a'
  6801. ];
  6802. $this->assertHtml($expected, $result);
  6803. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6804. $expected = [
  6805. 'form' => [
  6806. 'method' => 'post', 'action' => '/posts/delete/1',
  6807. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6808. ],
  6809. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6810. '/form',
  6811. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6812. 'Delete',
  6813. '/a'
  6814. ];
  6815. $this->assertHtml($expected, $result);
  6816. $result = $this->Form->postLink(
  6817. 'Delete',
  6818. '/posts/delete/1',
  6819. ['target' => '_blank', 'class' => 'btn btn-danger']
  6820. );
  6821. $expected = [
  6822. 'form' => [
  6823. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6824. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6825. ],
  6826. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6827. '/form',
  6828. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6829. 'Delete',
  6830. '/a'
  6831. ];
  6832. $this->assertHtml($expected, $result);
  6833. }
  6834. /**
  6835. * testPostLinkWithConfirm method
  6836. *
  6837. * Test the confirm option for postLink().
  6838. *
  6839. * @return void
  6840. */
  6841. public function testPostLinkWithConfirm()
  6842. {
  6843. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6844. $expected = [
  6845. 'form' => [
  6846. 'method' => 'post', 'action' => '/posts/delete/1',
  6847. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6848. ],
  6849. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6850. '/form',
  6851. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6852. 'Delete',
  6853. '/a'
  6854. ];
  6855. $this->assertHtml($expected, $result);
  6856. $result = $this->Form->postLink(
  6857. 'Delete',
  6858. '/posts/delete/1',
  6859. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6860. );
  6861. $expected = [
  6862. 'form' => [
  6863. 'method' => 'post', 'action' => '/posts/delete/1',
  6864. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6865. ],
  6866. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6867. '/form',
  6868. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6869. 'Delete',
  6870. '/a'
  6871. ];
  6872. $this->assertHtml($expected, $result);
  6873. }
  6874. /**
  6875. * testPostLinkWithQuery method
  6876. *
  6877. * Test postLink() with query string args.
  6878. *
  6879. * @return void
  6880. */
  6881. public function testPostLinkWithQuery()
  6882. {
  6883. $result = $this->Form->postLink(
  6884. 'Delete',
  6885. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6886. );
  6887. $expected = [
  6888. 'form' => [
  6889. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6890. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6891. ],
  6892. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6893. '/form',
  6894. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6895. 'Delete',
  6896. '/a'
  6897. ];
  6898. $this->assertHtml($expected, $result);
  6899. }
  6900. /**
  6901. * testPostLinkWithData method
  6902. *
  6903. * Test postLink with additional data.
  6904. *
  6905. * @return void
  6906. */
  6907. public function testPostLinkWithData()
  6908. {
  6909. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6910. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6911. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6912. $this->Form->create($entity);
  6913. $this->Form->end();
  6914. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6915. $this->assertContains(
  6916. '<input type="hidden" name="name" value="show"',
  6917. $result,
  6918. 'should not contain entity data.'
  6919. );
  6920. }
  6921. /**
  6922. * testPostLinkSecurityHash method
  6923. *
  6924. * Test that security hashes for postLink include the url.
  6925. *
  6926. * @return void
  6927. */
  6928. public function testPostLinkSecurityHash()
  6929. {
  6930. $hash = Security::hash(
  6931. '/posts/delete/1' .
  6932. serialize(['id' => '1']) .
  6933. '' .
  6934. Security::salt()
  6935. );
  6936. $hash .= '%3Aid';
  6937. $this->Form->request->params['_Token']['key'] = 'test';
  6938. $result = $this->Form->postLink(
  6939. 'Delete',
  6940. '/posts/delete/1',
  6941. ['data' => ['id' => 1]]
  6942. );
  6943. $tokenDebug = urlencode(json_encode([
  6944. '/posts/delete/1',
  6945. [
  6946. 'id' => 1
  6947. ],
  6948. []
  6949. ]));
  6950. $expected = [
  6951. 'form' => [
  6952. 'method' => 'post', 'action' => '/posts/delete/1',
  6953. 'name', 'style' => 'display:none;'
  6954. ],
  6955. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6956. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6957. 'div' => ['style' => 'display:none;'],
  6958. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6959. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6960. ['input' => [
  6961. 'type' => 'hidden', 'name' => '_Token[debug]',
  6962. 'value' => $tokenDebug,
  6963. ]],
  6964. '/div',
  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. * testPostLinkSecurityHashBlockMode method
  6974. *
  6975. * Test that postLink doesn't modify the fields in the containing form.
  6976. *
  6977. * postLink() calls inside open forms should not modify the field list
  6978. * for the form.
  6979. *
  6980. * @return void
  6981. */
  6982. public function testPostLinkSecurityHashBlockMode()
  6983. {
  6984. $hash = Security::hash(
  6985. '/posts/delete/1' .
  6986. serialize([]) .
  6987. '' .
  6988. Security::salt()
  6989. );
  6990. $hash .= '%3A';
  6991. $this->Form->request->params['_Token']['key'] = 'test';
  6992. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  6993. $this->Form->control('title');
  6994. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6995. $result = $this->View->fetch('postLink');
  6996. $this->assertEquals(['title'], $this->Form->fields);
  6997. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  6998. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  6999. }
  7000. /**
  7001. * testPostLinkSecurityHashNoDebugMode method
  7002. *
  7003. * Test that security does not include debug token if debug is false.
  7004. *
  7005. * @return void
  7006. */
  7007. public function testPostLinkSecurityHashNoDebugMode()
  7008. {
  7009. Configure::write('debug', false);
  7010. $hash = Security::hash(
  7011. '/posts/delete/1' .
  7012. serialize(['id' => '1']) .
  7013. '' .
  7014. Security::salt()
  7015. );
  7016. $hash .= '%3Aid';
  7017. $this->Form->request->params['_Token']['key'] = 'test';
  7018. $result = $this->Form->postLink(
  7019. 'Delete',
  7020. '/posts/delete/1',
  7021. ['data' => ['id' => 1]]
  7022. );
  7023. $expected = [
  7024. 'form' => [
  7025. 'method' => 'post', 'action' => '/posts/delete/1',
  7026. 'name', 'style' => 'display:none;'
  7027. ],
  7028. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7029. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7030. 'div' => ['style' => 'display:none;'],
  7031. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  7032. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  7033. '/div',
  7034. '/form',
  7035. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7036. 'Delete',
  7037. '/a'
  7038. ];
  7039. $this->assertHtml($expected, $result);
  7040. }
  7041. /**
  7042. * testPostLinkNestedData method
  7043. *
  7044. * Test using postLink with N dimensional data.
  7045. *
  7046. * @return void
  7047. */
  7048. public function testPostLinkNestedData()
  7049. {
  7050. $data = [
  7051. 'one' => [
  7052. 'two' => [
  7053. 3, 4, 5
  7054. ]
  7055. ]
  7056. ];
  7057. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7058. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7059. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7060. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7061. }
  7062. /**
  7063. * testPostLinkAfterGetForm method
  7064. *
  7065. * Test creating postLinks after a GET form.
  7066. *
  7067. * @return void
  7068. */
  7069. public function testPostLinkAfterGetForm()
  7070. {
  7071. $this->Form->request->params['_csrfToken'] = 'testkey';
  7072. $this->Form->request->params['_Token'] = 'val';
  7073. $this->Form->create($this->article, ['type' => 'get']);
  7074. $this->Form->end();
  7075. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7076. $tokenDebug = urlencode(json_encode([
  7077. '/posts/delete/1',
  7078. [],
  7079. []
  7080. ]));
  7081. $expected = [
  7082. 'form' => [
  7083. 'method' => 'post', 'action' => '/posts/delete/1',
  7084. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7085. ],
  7086. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7087. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  7088. 'div' => ['style' => 'display:none;'],
  7089. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  7090. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  7091. ['input' => [
  7092. 'type' => 'hidden', 'name' => '_Token[debug]',
  7093. 'value' => $tokenDebug,
  7094. ]],
  7095. '/div',
  7096. '/form',
  7097. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7098. 'Delete',
  7099. '/a'
  7100. ];
  7101. $this->assertHtml($expected, $result);
  7102. }
  7103. /**
  7104. * testPostLinkFormBuffer method
  7105. *
  7106. * Test that postLink adds form tags to view block.
  7107. *
  7108. * @return void
  7109. */
  7110. public function testPostLinkFormBuffer()
  7111. {
  7112. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7113. $expected = [
  7114. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7115. 'Delete',
  7116. '/a'
  7117. ];
  7118. $this->assertHtml($expected, $result);
  7119. $result = $this->View->fetch('postLink');
  7120. $expected = [
  7121. 'form' => [
  7122. 'method' => 'post', 'action' => '/posts/delete/1',
  7123. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7124. ],
  7125. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7126. '/form'
  7127. ];
  7128. $this->assertHtml($expected, $result);
  7129. $result = $this->Form->postLink(
  7130. 'Delete',
  7131. '/posts/delete/2',
  7132. ['block' => true, 'method' => 'DELETE']
  7133. );
  7134. $expected = [
  7135. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7136. 'Delete',
  7137. '/a'
  7138. ];
  7139. $this->assertHtml($expected, $result);
  7140. $result = $this->View->fetch('postLink');
  7141. $expected = [
  7142. 'form' => [
  7143. 'method' => 'post', 'action' => '/posts/delete/1',
  7144. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7145. ],
  7146. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7147. '/form',
  7148. [
  7149. 'form' => [
  7150. 'method' => 'post', 'action' => '/posts/delete/2',
  7151. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7152. ],
  7153. ],
  7154. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7155. '/form'
  7156. ];
  7157. $this->assertHtml($expected, $result);
  7158. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7159. $expected = [
  7160. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7161. 'Delete',
  7162. '/a'
  7163. ];
  7164. $this->assertHtml($expected, $result);
  7165. $result = $this->View->fetch('foobar');
  7166. $expected = [
  7167. 'form' => [
  7168. 'method' => 'post', 'action' => '/posts/delete/1',
  7169. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7170. ],
  7171. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7172. '/form'
  7173. ];
  7174. $this->assertHtml($expected, $result);
  7175. }
  7176. /**
  7177. * testSubmitButton method
  7178. *
  7179. * @return void
  7180. */
  7181. public function testSubmitButton()
  7182. {
  7183. $result = $this->Form->submit('');
  7184. $expected = [
  7185. 'div' => ['class' => 'submit'],
  7186. 'input' => ['type' => 'submit', 'value' => ''],
  7187. '/div'
  7188. ];
  7189. $this->assertHtml($expected, $result);
  7190. $result = $this->Form->submit('Test Submit');
  7191. $expected = [
  7192. 'div' => ['class' => 'submit'],
  7193. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7194. '/div'
  7195. ];
  7196. $this->assertHtml($expected, $result);
  7197. $result = $this->Form->submit('Next >');
  7198. $expected = [
  7199. 'div' => ['class' => 'submit'],
  7200. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7201. '/div'
  7202. ];
  7203. $this->assertHtml($expected, $result);
  7204. $result = $this->Form->submit('Next >', ['escape' => false]);
  7205. $expected = [
  7206. 'div' => ['class' => 'submit'],
  7207. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7208. '/div'
  7209. ];
  7210. $this->assertHtml($expected, $result);
  7211. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7212. $expected = [
  7213. 'div' => ['class' => 'submit'],
  7214. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7215. '/div'
  7216. ];
  7217. $this->assertHtml($expected, $result);
  7218. }
  7219. /**
  7220. * testSubmitImage method
  7221. *
  7222. * Test image submit types.
  7223. *
  7224. * @return void
  7225. */
  7226. public function testSubmitImage()
  7227. {
  7228. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7229. $expected = [
  7230. 'div' => ['class' => 'submit'],
  7231. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7232. '/div'
  7233. ];
  7234. $this->assertHtml($expected, $result);
  7235. $result = $this->Form->submit('/relative/cake.power.gif');
  7236. $expected = [
  7237. 'div' => ['class' => 'submit'],
  7238. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7239. '/div'
  7240. ];
  7241. $this->assertHtml($expected, $result);
  7242. $result = $this->Form->submit('cake.power.gif');
  7243. $expected = [
  7244. 'div' => ['class' => 'submit'],
  7245. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7246. '/div'
  7247. ];
  7248. $this->assertHtml($expected, $result);
  7249. $result = $this->Form->submit('Not.an.image');
  7250. $expected = [
  7251. 'div' => ['class' => 'submit'],
  7252. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7253. '/div'
  7254. ];
  7255. $this->assertHtml($expected, $result);
  7256. }
  7257. /**
  7258. * testSubmitUnlockedByDefault method
  7259. *
  7260. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7261. * be submitted at a time.
  7262. *
  7263. * @return void
  7264. */
  7265. public function testSubmitUnlockedByDefault()
  7266. {
  7267. $this->Form->request->params['_Token'] = 'secured';
  7268. $this->Form->submit('Go go');
  7269. $this->Form->submit('Save', ['name' => 'save']);
  7270. $result = $this->Form->unlockField();
  7271. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7272. }
  7273. /**
  7274. * testSubmitImageTimestamp method
  7275. *
  7276. * Test submit image with timestamps.
  7277. *
  7278. * @return void
  7279. */
  7280. public function testSubmitImageTimestamp()
  7281. {
  7282. Configure::write('Asset.timestamp', 'force');
  7283. $result = $this->Form->submit('cake.power.gif');
  7284. $expected = [
  7285. 'div' => ['class' => 'submit'],
  7286. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7287. '/div'
  7288. ];
  7289. $this->assertHtml($expected, $result);
  7290. }
  7291. /**
  7292. * testDateTimeWithGetForms method
  7293. *
  7294. * Test that datetime() works with GET style forms.
  7295. *
  7296. * @return void
  7297. */
  7298. public function testDateTimeWithGetForms()
  7299. {
  7300. extract($this->dateRegex);
  7301. $this->Form->create($this->article, ['type' => 'get']);
  7302. $result = $this->Form->datetime('created');
  7303. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7304. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7305. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7306. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7307. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7308. }
  7309. /**
  7310. * testForMagicControlNonExistingNorValidated method
  7311. *
  7312. * @return void
  7313. */
  7314. public function testForMagicControlNonExistingNorValidated()
  7315. {
  7316. $this->Form->create($this->article);
  7317. $this->Form->templates(['inputContainer' => '{{content}}']);
  7318. $result = $this->Form->control('non_existing_nor_validated');
  7319. $expected = [
  7320. 'label' => ['for' => 'non-existing-nor-validated'],
  7321. 'Non Existing Nor Validated',
  7322. '/label',
  7323. 'input' => [
  7324. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7325. 'id' => 'non-existing-nor-validated'
  7326. ]
  7327. ];
  7328. $this->assertHtml($expected, $result);
  7329. $result = $this->Form->control('non_existing_nor_validated', [
  7330. 'val' => 'my value'
  7331. ]);
  7332. $expected = [
  7333. 'label' => ['for' => 'non-existing-nor-validated'],
  7334. 'Non Existing Nor Validated',
  7335. '/label',
  7336. 'input' => [
  7337. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7338. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7339. ]
  7340. ];
  7341. $this->assertHtml($expected, $result);
  7342. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7343. $result = $this->Form->control('non_existing_nor_validated');
  7344. $expected = [
  7345. 'label' => ['for' => 'non-existing-nor-validated'],
  7346. 'Non Existing Nor Validated',
  7347. '/label',
  7348. 'input' => [
  7349. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7350. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7351. ]
  7352. ];
  7353. $this->assertHtml($expected, $result);
  7354. }
  7355. /**
  7356. * testFormMagicControlLabel method
  7357. *
  7358. * @return void
  7359. */
  7360. public function testFormMagicControlLabel()
  7361. {
  7362. TableRegistry::get('Contacts', [
  7363. 'className' => __NAMESPACE__ . '\ContactsTable'
  7364. ]);
  7365. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7366. $this->Form->templates(['inputContainer' => '{{content}}']);
  7367. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7368. $expected = [
  7369. 'label' => ['for' => 'contacts-name'],
  7370. 'My label',
  7371. '/label',
  7372. 'input' => [
  7373. 'type' => 'text', 'name' => 'Contacts[name]',
  7374. 'id' => 'contacts-name', 'maxlength' => '255'
  7375. ]
  7376. ];
  7377. $this->assertHtml($expected, $result);
  7378. $result = $this->Form->control('name', [
  7379. 'label' => ['class' => 'mandatory']
  7380. ]);
  7381. $expected = [
  7382. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7383. 'Name',
  7384. '/label',
  7385. 'input' => [
  7386. 'type' => 'text', 'name' => 'name',
  7387. 'id' => 'name', 'maxlength' => '255'
  7388. ]
  7389. ];
  7390. $this->assertHtml($expected, $result);
  7391. $result = $this->Form->control('name', [
  7392. 'div' => false,
  7393. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7394. ]);
  7395. $expected = [
  7396. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7397. 'My label',
  7398. '/label',
  7399. 'input' => [
  7400. 'type' => 'text', 'name' => 'name',
  7401. 'id' => 'name', 'maxlength' => '255'
  7402. ]
  7403. ];
  7404. $this->assertHtml($expected, $result);
  7405. $result = $this->Form->control('Contact.name', [
  7406. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7407. ]);
  7408. $expected = [
  7409. 'label' => ['for' => 'my_id'],
  7410. 'Name',
  7411. '/label',
  7412. 'input' => [
  7413. 'type' => 'text', 'name' => 'Contact[name]',
  7414. 'id' => 'my_id', 'maxlength' => '255'
  7415. ]
  7416. ];
  7417. $this->assertHtml($expected, $result);
  7418. $result = $this->Form->control('1.id');
  7419. $expected = ['input' => [
  7420. 'type' => 'hidden', 'name' => '1[id]',
  7421. 'id' => '1-id'
  7422. ]];
  7423. $this->assertHtml($expected, $result);
  7424. $result = $this->Form->control("1.name");
  7425. $expected = [
  7426. 'label' => ['for' => '1-name'],
  7427. 'Name',
  7428. '/label',
  7429. 'input' => [
  7430. 'type' => 'text', 'name' => '1[name]',
  7431. 'id' => '1-name', 'maxlength' => '255'
  7432. ]
  7433. ];
  7434. $this->assertHtml($expected, $result);
  7435. }
  7436. /**
  7437. * testFormEnd method
  7438. *
  7439. * @return void
  7440. */
  7441. public function testFormEnd()
  7442. {
  7443. $this->assertEquals('</form>', $this->Form->end());
  7444. }
  7445. /**
  7446. * testMultiRecordForm method
  7447. *
  7448. * Test the generation of fields for a multi record form.
  7449. *
  7450. * @return void
  7451. */
  7452. public function testMultiRecordForm()
  7453. {
  7454. $this->loadFixtures('Articles', 'Comments');
  7455. $articles = TableRegistry::get('Articles');
  7456. $articles->hasMany('Comments');
  7457. $comment = new Entity(['comment' => 'Value']);
  7458. $article = new Article(['comments' => [$comment]]);
  7459. $this->Form->create([$article]);
  7460. $result = $this->Form->control('0.comments.1.comment');
  7461. //@codingStandardsIgnoreStart
  7462. $expected = [
  7463. 'div' => ['class' => 'input textarea'],
  7464. 'label' => ['for' => '0-comments-1-comment'],
  7465. 'Comment',
  7466. '/label',
  7467. 'textarea' => [
  7468. 'name',
  7469. 'id' => '0-comments-1-comment',
  7470. 'rows' => 5
  7471. ],
  7472. '/textarea',
  7473. '/div'
  7474. ];
  7475. //@codingStandardsIgnoreEnd
  7476. $this->assertHtml($expected, $result);
  7477. $result = $this->Form->control('0.comments.0.comment');
  7478. //@codingStandardsIgnoreStart
  7479. $expected = [
  7480. 'div' => ['class' => 'input textarea'],
  7481. 'label' => ['for' => '0-comments-0-comment'],
  7482. 'Comment',
  7483. '/label',
  7484. 'textarea' => [
  7485. 'name',
  7486. 'id' => '0-comments-0-comment',
  7487. 'rows' => 5
  7488. ],
  7489. 'Value',
  7490. '/textarea',
  7491. '/div'
  7492. ];
  7493. //@codingStandardsIgnoreEnd
  7494. $this->assertHtml($expected, $result);
  7495. $comment->errors('comment', ['Not valid']);
  7496. $result = $this->Form->control('0.comments.0.comment');
  7497. //@codingStandardsIgnoreStart
  7498. $expected = [
  7499. 'div' => ['class' => 'input textarea error'],
  7500. 'label' => ['for' => '0-comments-0-comment'],
  7501. 'Comment',
  7502. '/label',
  7503. 'textarea' => [
  7504. 'name',
  7505. 'class' => 'form-error',
  7506. 'id' => '0-comments-0-comment',
  7507. 'rows' => 5
  7508. ],
  7509. 'Value',
  7510. '/textarea',
  7511. ['div' => ['class' => 'error-message']],
  7512. 'Not valid',
  7513. '/div',
  7514. '/div'
  7515. ];
  7516. //@codingStandardsIgnoreEnd
  7517. $this->assertHtml($expected, $result);
  7518. TableRegistry::get('Comments')
  7519. ->validator('default')
  7520. ->allowEmpty('comment', false);
  7521. $result = $this->Form->control('0.comments.1.comment');
  7522. //@codingStandardsIgnoreStart
  7523. $expected = [
  7524. 'div' => ['class' => 'input textarea required'],
  7525. 'label' => ['for' => '0-comments-1-comment'],
  7526. 'Comment',
  7527. '/label',
  7528. 'textarea' => [
  7529. 'name',
  7530. 'required' => 'required',
  7531. 'id' => '0-comments-1-comment',
  7532. 'rows' => 5
  7533. ],
  7534. '/textarea',
  7535. '/div'
  7536. ];
  7537. //@codingStandardsIgnoreEnd
  7538. $this->assertHtml($expected, $result);
  7539. }
  7540. /**
  7541. * testHtml5Controls method
  7542. *
  7543. * Test that some html5 inputs + FormHelper::__call() work.
  7544. *
  7545. * @return void
  7546. */
  7547. public function testHtml5Controls()
  7548. {
  7549. $result = $this->Form->email('User.email');
  7550. $expected = [
  7551. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7552. ];
  7553. $this->assertHtml($expected, $result);
  7554. $result = $this->Form->search('User.query');
  7555. $expected = [
  7556. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7557. ];
  7558. $this->assertHtml($expected, $result);
  7559. $result = $this->Form->search('User.query', ['value' => 'test']);
  7560. $expected = [
  7561. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7562. ];
  7563. $this->assertHtml($expected, $result);
  7564. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7565. $expected = [
  7566. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7567. ];
  7568. $this->assertHtml($expected, $result);
  7569. }
  7570. /**
  7571. * testHtml5ControlWithControl method
  7572. *
  7573. * Test accessing html5 inputs through control().
  7574. *
  7575. * @return void
  7576. */
  7577. public function testHtml5ControlWithControl()
  7578. {
  7579. $this->Form->create();
  7580. $this->Form->templates(['inputContainer' => '{{content}}']);
  7581. $result = $this->Form->control('website', [
  7582. 'type' => 'url',
  7583. 'val' => 'http://domain.tld',
  7584. 'label' => false
  7585. ]);
  7586. $expected = [
  7587. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7588. ];
  7589. $this->assertHtml($expected, $result);
  7590. }
  7591. /**
  7592. * testHtml5ControlException method
  7593. *
  7594. * Test errors when field name is missing.
  7595. *
  7596. * @expectedException \Cake\Core\Exception\Exception
  7597. * @return void
  7598. */
  7599. public function testHtml5ControlException()
  7600. {
  7601. $this->Form->email();
  7602. }
  7603. /**
  7604. * testRequiredAttribute method
  7605. *
  7606. * Tests that formhelper sets required attributes.
  7607. *
  7608. * @return void
  7609. */
  7610. public function testRequiredAttribute()
  7611. {
  7612. $this->article['required'] = [
  7613. 'title' => true,
  7614. 'body' => false,
  7615. ];
  7616. $this->Form->create($this->article);
  7617. $result = $this->Form->control('title');
  7618. $expected = [
  7619. 'div' => ['class' => 'input text required'],
  7620. 'label' => ['for' => 'title'],
  7621. 'Title',
  7622. '/label',
  7623. 'input' => [
  7624. 'type' => 'text',
  7625. 'name' => 'title',
  7626. 'id' => 'title',
  7627. 'required' => 'required',
  7628. ],
  7629. '/div'
  7630. ];
  7631. $this->assertHtml($expected, $result);
  7632. $result = $this->Form->control('title', ['required' => false]);
  7633. $this->assertNotContains('required', $result);
  7634. $result = $this->Form->control('body');
  7635. $expected = [
  7636. 'div' => ['class' => 'input text'],
  7637. 'label' => ['for' => 'body'],
  7638. 'Body',
  7639. '/label',
  7640. 'input' => [
  7641. 'type' => 'text',
  7642. 'name' => 'body',
  7643. 'id' => 'body',
  7644. ],
  7645. '/div'
  7646. ];
  7647. $this->assertHtml($expected, $result);
  7648. $result = $this->Form->control('body', ['required' => true]);
  7649. $this->assertContains('required', $result);
  7650. }
  7651. /**
  7652. * testControlsNotNested method
  7653. *
  7654. * Tests that it is possible to put inputs outside of the label.
  7655. *
  7656. * @return void
  7657. */
  7658. public function testControlsNotNested()
  7659. {
  7660. $this->Form->templates([
  7661. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7662. 'formGroup' => '{{input}}{{label}}',
  7663. ]);
  7664. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7665. $expected = [
  7666. 'div' => ['class' => 'input checkbox'],
  7667. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7668. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7669. 'label' => ['for' => 'foo'],
  7670. 'Foo',
  7671. '/label',
  7672. '/div'
  7673. ];
  7674. $this->assertHtml($expected, $result);
  7675. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7676. $expected = [
  7677. 'div' => ['class' => 'input checkbox'],
  7678. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7679. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7680. '/div'
  7681. ];
  7682. $this->assertHtml($expected, $result);
  7683. $result = $this->Form->control('confirm', [
  7684. 'type' => 'radio',
  7685. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7686. ]);
  7687. $expected = [
  7688. 'div' => ['class' => 'input radio'],
  7689. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7690. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7691. ['label' => ['for' => 'confirm-y']],
  7692. 'Yes',
  7693. '/label',
  7694. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7695. ['label' => ['for' => 'confirm-n']],
  7696. 'No',
  7697. '/label',
  7698. '<label',
  7699. 'Confirm',
  7700. '/label',
  7701. '/div',
  7702. ];
  7703. $this->assertHtml($expected, $result);
  7704. $result = $this->Form->select('category', ['1', '2'], [
  7705. 'multiple' => 'checkbox',
  7706. 'name' => 'fish',
  7707. ]);
  7708. $expected = [
  7709. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7710. ['div' => ['class' => 'checkbox']],
  7711. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7712. ['label' => ['for' => 'fish-0']],
  7713. '1',
  7714. '/label',
  7715. '/div',
  7716. ['div' => ['class' => 'checkbox']],
  7717. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7718. ['label' => ['for' => 'fish-1']],
  7719. '2',
  7720. '/label',
  7721. '/div'
  7722. ];
  7723. $this->assertHtml($expected, $result);
  7724. }
  7725. /**
  7726. * testControlContainerTemplates method
  7727. *
  7728. * Test that *Container templates are used by input.
  7729. *
  7730. * @return void
  7731. */
  7732. public function testControlContainerTemplates()
  7733. {
  7734. $this->Form->templates([
  7735. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7736. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7737. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7738. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7739. ]);
  7740. $this->article['errors'] = [
  7741. 'Article' => ['published' => 'error message']
  7742. ];
  7743. $this->Form->create($this->article);
  7744. $result = $this->Form->control('accept', [
  7745. 'type' => 'checkbox'
  7746. ]);
  7747. $expected = [
  7748. 'div' => ['class' => 'check'],
  7749. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7750. 'label' => ['for' => 'accept'],
  7751. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7752. 'Accept',
  7753. '/label',
  7754. '/div'
  7755. ];
  7756. $this->assertHtml($expected, $result);
  7757. $result = $this->Form->control('accept', [
  7758. 'type' => 'radio',
  7759. 'options' => ['Y', 'N']
  7760. ]);
  7761. $this->assertContains('<div class="rad">', $result);
  7762. $result = $this->Form->control('Article.published', [
  7763. 'type' => 'radio',
  7764. 'options' => ['Y', 'N']
  7765. ]);
  7766. $this->assertContains('<div class="rad err">', $result);
  7767. $result = $this->Form->control('Article.created', [
  7768. 'type' => 'datetime',
  7769. ]);
  7770. $this->assertContains('<div class="dt">', $result);
  7771. }
  7772. /**
  7773. * testFormGroupTemplates method
  7774. *
  7775. * Test that *Container templates are used by input.
  7776. *
  7777. * @return void
  7778. */
  7779. public function testFormGroupTemplates()
  7780. {
  7781. $this->Form->templates([
  7782. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7783. ]);
  7784. $this->Form->create($this->article);
  7785. $result = $this->Form->control('accept', [
  7786. 'type' => 'radio',
  7787. 'options' => ['Y', 'N']
  7788. ]);
  7789. $this->assertContains('<div class="radio">', $result);
  7790. }
  7791. /**
  7792. * testResetTemplates method
  7793. *
  7794. * Test resetting templates.
  7795. *
  7796. * @return void
  7797. */
  7798. public function testResetTemplates()
  7799. {
  7800. $this->Form->templates(['input' => '<input/>']);
  7801. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7802. $this->assertNull($this->Form->resetTemplates());
  7803. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7804. }
  7805. /**
  7806. * testContext method
  7807. *
  7808. * Test the context method.
  7809. *
  7810. * @return void
  7811. */
  7812. public function testContext()
  7813. {
  7814. $result = $this->Form->context();
  7815. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7816. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7817. $this->assertSame($mock, $this->Form->context($mock));
  7818. $this->assertSame($mock, $this->Form->context());
  7819. }
  7820. /**
  7821. * testAutoDomId method
  7822. *
  7823. * @return void
  7824. */
  7825. public function testAutoDomId()
  7826. {
  7827. $result = $this->Form->text('field', ['id' => true]);
  7828. $expected = [
  7829. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7830. ];
  7831. $this->assertHtml($expected, $result);
  7832. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7833. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7834. $expected = [
  7835. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7836. ['label' => ['for' => 'field-0']],
  7837. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7838. 'option A',
  7839. '/label',
  7840. ['label' => ['for' => 'field-1']],
  7841. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7842. 'option B',
  7843. '/label',
  7844. ];
  7845. $this->assertHtml($expected, $result);
  7846. $result = $this->Form->select(
  7847. 'multi_field',
  7848. ['first', 'second'],
  7849. ['multiple' => 'checkbox', 'id' => true]
  7850. );
  7851. $expected = [
  7852. 'input' => [
  7853. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7854. ],
  7855. ['div' => ['class' => 'checkbox']],
  7856. ['label' => ['for' => 'multi-field-0']],
  7857. ['input' => [
  7858. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7859. 'value' => '0', 'id' => 'multi-field-0'
  7860. ]],
  7861. 'first',
  7862. '/label',
  7863. '/div',
  7864. ['div' => ['class' => 'checkbox']],
  7865. ['label' => ['for' => 'multi-field-1']],
  7866. ['input' => [
  7867. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7868. 'value' => '1', 'id' => 'multi-field-1'
  7869. ]],
  7870. 'second',
  7871. '/label',
  7872. '/div',
  7873. ];
  7874. $this->assertHtml($expected, $result);
  7875. }
  7876. /**
  7877. * Test the basic setters and getters for value sources
  7878. *
  7879. * @return void
  7880. */
  7881. public function testFormValueSourcesSettersGetters()
  7882. {
  7883. $expected = ['context'];
  7884. $result = $this->Form->getValueSources();
  7885. $this->assertEquals($expected, $result);
  7886. $expected = null;
  7887. $result = $this->Form->getSourceValue('id');
  7888. $this->assertEquals($expected, $result);
  7889. $expected = ['query', 'data', 'context'];
  7890. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7891. $result = $this->Form->getValueSources();
  7892. $this->assertEquals($expected, $result);
  7893. $this->Form->request->data['id'] = '1';
  7894. $this->Form->request->query['id'] = '2';
  7895. $this->Form->setValueSources(['context']);
  7896. $expected = '1';
  7897. $result = $this->Form->getSourceValue('id');
  7898. $this->assertEquals($expected, $result);
  7899. $this->Form->setValueSources('query');
  7900. $expected = ['query'];
  7901. $result = $this->Form->getValueSources();
  7902. $this->assertEquals($expected, $result);
  7903. $expected = '2';
  7904. $result = $this->Form->getSourceValue('id');
  7905. $this->assertEquals($expected, $result);
  7906. $this->Form->setValueSources(['data']);
  7907. $expected = '1';
  7908. $result = $this->Form->getSourceValue('id');
  7909. $this->assertEquals($expected, $result);
  7910. $this->Form->setValueSources(['query', 'data']);
  7911. $expected = '2';
  7912. $result = $this->Form->getSourceValue('id');
  7913. $this->assertEquals($expected, $result);
  7914. }
  7915. /**
  7916. * Tests the different input rendering values based on sources values switching
  7917. *
  7918. * @return void
  7919. */
  7920. public function testFormValueSourcesSingleSwitchRendering()
  7921. {
  7922. $this->loadFixtures('Articles');
  7923. $articles = TableRegistry::get('Articles');
  7924. $article = new Article();
  7925. $articles->patchEntity($article, ['id' => '3']);
  7926. $this->Form->create($article);
  7927. $this->Form->setValueSources(['context']);
  7928. $result = $this->Form->control('id');
  7929. $expected = [
  7930. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7931. ];
  7932. $this->assertHtml($expected, $result);
  7933. $this->Form->request->query['id'] = '5';
  7934. $this->Form->setValueSources(['query']);
  7935. $result = $this->Form->control('id');
  7936. $expected = [
  7937. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7938. ];
  7939. $this->assertHtml($expected, $result);
  7940. $this->Form->request->query['id'] = '5a';
  7941. $this->Form->request->data['id'] = '5b';
  7942. $this->Form->setValueSources(['context']);
  7943. $this->Form->create($article);
  7944. $result = $this->Form->control('id');
  7945. $expected = [
  7946. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7947. ];
  7948. $this->assertHtml($expected, $result);
  7949. $this->Form->setValueSources(['data']);
  7950. $this->Form->create($article);
  7951. $result = $this->Form->control('id');
  7952. $expected = [
  7953. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7954. ];
  7955. $this->assertHtml($expected, $result);
  7956. $this->Form->setValueSources(['query']);
  7957. $result = $this->Form->control('id');
  7958. $expected = [
  7959. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7960. ];
  7961. $this->assertHtml($expected, $result);
  7962. }
  7963. /**
  7964. * Tests the different input rendering values based on sources values switching while supplying
  7965. * an entity (base context) and multiple sources (such as data, query)
  7966. *
  7967. * @return void
  7968. */
  7969. public function testFormValueSourcesListSwitchRendering()
  7970. {
  7971. $this->loadFixtures('Articles');
  7972. $articles = TableRegistry::get('Articles');
  7973. $article = new Article();
  7974. $articles->patchEntity($article, ['id' => '3']);
  7975. $this->Form->request->query['id'] = '9';
  7976. $this->Form->create($article);
  7977. $this->Form->setValueSources(['context', 'query']);
  7978. $result = $this->Form->control('id');
  7979. $expected = [
  7980. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7981. ];
  7982. $this->assertHtml($expected, $result);
  7983. $this->Form->setValueSources(['query', 'context']);
  7984. $result = $this->Form->control('id');
  7985. $expected = [
  7986. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7987. ];
  7988. $this->assertHtml($expected, $result);
  7989. $this->Form->setValueSources(['data', 'query', 'context']);
  7990. $result = $this->Form->control('id');
  7991. $expected = [
  7992. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7993. ];
  7994. $this->assertHtml($expected, $result);
  7995. $this->Form->request->data['id'] = '8';
  7996. $this->Form->request->query['id'] = '9';
  7997. $this->Form->setValueSources(['data', 'query', 'context']);
  7998. $result = $this->Form->control('id');
  7999. $expected = [
  8000. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8001. ];
  8002. $this->assertHtml($expected, $result);
  8003. }
  8004. /**
  8005. * Test the different form input renderings based on values sources switchings through form options
  8006. *
  8007. * @return void
  8008. */
  8009. public function testFormValueSourcesSwitchViaOptionsRendering()
  8010. {
  8011. $this->loadFixtures('Articles');
  8012. $articles = TableRegistry::get('Articles');
  8013. $article = new Article();
  8014. $articles->patchEntity($article, ['id' => '3']);
  8015. $this->Form->request->data['id'] = '4';
  8016. $this->Form->request->query['id'] = '5';
  8017. $this->Form->create($article, ['valueSources' => 'query']);
  8018. $result = $this->Form->control('id');
  8019. $expected = [
  8020. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8021. ];
  8022. $this->assertHtml($expected, $result);
  8023. $result = $this->Form->getSourceValue('id');
  8024. $this->assertEquals('5', $result);
  8025. $this->Form->setValueSources(['context']);
  8026. $this->Form->create($article, ['valueSources' => 'query']);
  8027. $result = $this->Form->control('id');
  8028. $expected = [
  8029. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8030. ];
  8031. $this->assertHtml($expected, $result);
  8032. $result = $this->Form->getSourceValue('id');
  8033. $this->assertEquals('5', $result);
  8034. $this->Form->setValueSources(['query']);
  8035. $this->Form->create($article, ['valueSources' => 'data']);
  8036. $result = $this->Form->control('id');
  8037. $expected = [
  8038. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8039. ];
  8040. $this->assertHtml($expected, $result);
  8041. $result = $this->Form->getSourceValue('id');
  8042. $this->assertEquals('4', $result);
  8043. $this->Form->setValueSources(['query']);
  8044. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8045. $result = $this->Form->control('id');
  8046. $expected = [
  8047. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8048. ];
  8049. $this->assertHtml($expected, $result);
  8050. $result = $this->Form->getSourceValue('id');
  8051. $this->assertEquals('4', $result);
  8052. }
  8053. /**
  8054. * Test the different form input renderings based on values sources switchings through form options
  8055. *
  8056. * @return void
  8057. */
  8058. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8059. {
  8060. $this->loadFixtures('Articles');
  8061. $articles = TableRegistry::get('Articles');
  8062. $article = new Article();
  8063. $articles->patchEntity($article, ['id' => '3']);
  8064. $this->Form->request->data['id'] = '10';
  8065. $this->Form->request->query['id'] = '11';
  8066. $this->Form->setValueSources(['context'])
  8067. ->create($article, ['valueSources' => ['query', 'data']]);
  8068. $result = $this->Form->control('id');
  8069. $expected = [
  8070. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8071. ];
  8072. $this->assertHtml($expected, $result);
  8073. $result = $this->Form->getSourceValue('id');
  8074. $this->assertEquals('11', $result);
  8075. unset($this->Form->request->query['id']);
  8076. $this->Form->setValueSources(['context'])
  8077. ->create($article, ['valueSources' => ['query', 'data']]);
  8078. $result = $this->Form->control('id');
  8079. $expected = [
  8080. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8081. ];
  8082. $this->assertHtml($expected, $result);
  8083. $result = $this->Form->getSourceValue('id');
  8084. $this->assertEquals('10', $result);
  8085. }
  8086. /**
  8087. * Test the different form values sources resetting through From::end();
  8088. *
  8089. * @return void
  8090. */
  8091. public function testFormValueSourcesResetViaEnd()
  8092. {
  8093. $expected = ['context'];
  8094. $result = $this->Form->getValueSources();
  8095. $this->assertEquals($expected, $result);
  8096. $expected = ['query', 'context', 'data'];
  8097. $this->Form->setValueSources(['query', 'context', 'data']);
  8098. $result = $this->Form->getValueSources();
  8099. $this->assertEquals($expected, $result);
  8100. $this->Form->create();
  8101. $result = $this->Form->getValueSources();
  8102. $this->assertEquals($expected, $result);
  8103. $this->Form->end();
  8104. $result = $this->Form->getValueSources();
  8105. $this->assertEquals(['context'], $result);
  8106. }
  8107. /**
  8108. * Test sources values defaults handling
  8109. *
  8110. * @return void
  8111. */
  8112. public function testFormValueSourcesDefaults()
  8113. {
  8114. $this->Form->request->query['password'] = 'open Sesame';
  8115. $this->Form->create();
  8116. $result = $this->Form->password('password');
  8117. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8118. $this->assertHtml($expected, $result);
  8119. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8120. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8121. $this->assertHtml($expected, $result);
  8122. $this->Form->setValueSources('query');
  8123. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8124. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8125. $this->assertHtml($expected, $result);
  8126. $this->Form->setValueSources('data');
  8127. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8128. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8129. $this->assertHtml($expected, $result);
  8130. }
  8131. /**
  8132. * Test sources values schema defaults handling
  8133. *
  8134. * @return void
  8135. */
  8136. public function testSourcesValueDoesntExistPassThrough()
  8137. {
  8138. $this->Form->request->query['category'] = 'sesame-cookies';
  8139. $articles = TableRegistry::get('Articles');
  8140. $entity = $articles->newEntity();
  8141. $this->Form->create($entity);
  8142. $this->Form->setValueSources(['query', 'context']);
  8143. $result = $this->Form->getSourceValue('category');
  8144. $this->assertEquals('sesame-cookies', $result);
  8145. $this->Form->setValueSources(['context', 'query']);
  8146. $result = $this->Form->getSourceValue('category');
  8147. $this->assertEquals('sesame-cookies', $result);
  8148. }
  8149. /**
  8150. * testNestedLabelInput method
  8151. *
  8152. * Test the `nestedInput` parameter
  8153. *
  8154. * @return void
  8155. */
  8156. public function testNestedLabelInput()
  8157. {
  8158. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8159. $expected = [
  8160. 'div' => ['class' => 'input text'],
  8161. 'label' => ['for' => 'foo'],
  8162. ['input' => [
  8163. 'type' => 'text',
  8164. 'name' => 'foo',
  8165. 'id' => 'foo'
  8166. ]],
  8167. 'Foo',
  8168. '/label',
  8169. '/div'
  8170. ];
  8171. $this->assertHtml($expected, $result);
  8172. }
  8173. /**
  8174. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8175. *
  8176. * This test makes sure `false` excludes the label from the render
  8177. *
  8178. * @return void
  8179. */
  8180. public function testInputLabelManipulationDisableLabels()
  8181. {
  8182. $result = $this->Form->input('test', [
  8183. 'type' => 'radio',
  8184. 'options' => ['A', 'B'],
  8185. 'labelOptions' => false
  8186. ]);
  8187. $expected = [
  8188. ['div' => ['class' => 'input radio']],
  8189. '<label',
  8190. 'Test',
  8191. '/label',
  8192. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8193. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8194. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8195. '/div',
  8196. ];
  8197. $this->assertHtml($expected, $result);
  8198. $result = $this->Form->input('checkbox1', [
  8199. 'label' => 'My checkboxes',
  8200. 'multiple' => 'checkbox',
  8201. 'type' => 'select',
  8202. 'options' => [
  8203. ['text' => 'First Checkbox', 'value' => 1],
  8204. ['text' => 'Second Checkbox', 'value' => 2]
  8205. ],
  8206. 'labelOptions' => false
  8207. ]);
  8208. $expected = [
  8209. ['div' => ['class' => 'input select']],
  8210. ['label' => ['for' => 'checkbox1']],
  8211. 'My checkboxes',
  8212. '/label',
  8213. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8214. ['div' => ['class' => 'checkbox']],
  8215. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8216. '/div',
  8217. ['div' => ['class' => 'checkbox']],
  8218. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8219. '/div',
  8220. '/div'
  8221. ];
  8222. $this->assertHtml($expected, $result);
  8223. }
  8224. /**
  8225. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8226. *
  8227. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8228. * added to the class if checked.
  8229. *
  8230. * Also checks to make sure any custom attributes are rendered correctly
  8231. *
  8232. * @return void
  8233. */
  8234. public function testInputLabelManipulationRadios()
  8235. {
  8236. $result = $this->Form->input('test', [
  8237. 'type' => 'radio',
  8238. 'options' => ['A', 'B'],
  8239. 'labelOptions' => ['class' => 'custom-class']
  8240. ]);
  8241. $expected = [
  8242. ['div' => ['class' => 'input radio']],
  8243. '<label',
  8244. 'Test',
  8245. '/label',
  8246. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8247. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8248. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8249. 'A',
  8250. '/label',
  8251. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8252. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8253. 'B',
  8254. '/label',
  8255. '/div',
  8256. ];
  8257. $this->assertHtml($expected, $result);
  8258. $result = $this->Form->input('test', [
  8259. 'type' => 'radio',
  8260. 'options' => ['A', 'B'],
  8261. 'value' => 1,
  8262. 'labelOptions' => ['class' => 'custom-class']
  8263. ]);
  8264. $expected = [
  8265. ['div' => ['class' => 'input radio']],
  8266. '<label',
  8267. 'Test',
  8268. '/label',
  8269. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8270. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8271. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8272. 'A',
  8273. '/label',
  8274. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8275. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8276. 'B',
  8277. '/label',
  8278. '/div',
  8279. ];
  8280. $this->assertHtml($expected, $result);
  8281. $result = $this->Form->input('test', [
  8282. 'type' => 'radio',
  8283. 'options' => ['A', 'B'],
  8284. 'value' => 1,
  8285. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8286. ]);
  8287. $expected = [
  8288. ['div' => ['class' => 'input radio']],
  8289. '<label',
  8290. 'Test',
  8291. '/label',
  8292. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8293. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8294. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8295. 'A',
  8296. '/label',
  8297. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8298. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8299. 'B',
  8300. '/label',
  8301. '/div',
  8302. ];
  8303. $this->assertHtml($expected, $result);
  8304. $result = $this->Form->radio('test', ['A', 'B'], [
  8305. 'label' => [
  8306. 'class' => ['custom-class', 'another-class'],
  8307. 'data-name' => 'bob'
  8308. ],
  8309. 'value' => 1
  8310. ]);
  8311. $expected = [
  8312. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8313. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8314. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8315. 'A',
  8316. '/label',
  8317. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8318. ['input' => [
  8319. 'type' => 'radio',
  8320. 'name' => 'test',
  8321. 'value' => '1',
  8322. 'id' => 'test-1',
  8323. 'checked' => 'checked'
  8324. ]],
  8325. 'B',
  8326. '/label',
  8327. ];
  8328. $this->assertHtml($expected, $result);
  8329. }
  8330. /**
  8331. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8332. *
  8333. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8334. * added to the class if checked.
  8335. *
  8336. * Also checks to make sure any custom attributes are rendered correctly
  8337. *
  8338. * @return void
  8339. */
  8340. public function testInputLabelManipulationCheckboxes()
  8341. {
  8342. $result = $this->Form->input('checkbox1', [
  8343. 'label' => 'My checkboxes',
  8344. 'multiple' => 'checkbox',
  8345. 'type' => 'select',
  8346. 'options' => [
  8347. ['text' => 'First Checkbox', 'value' => 1],
  8348. ['text' => 'Second Checkbox', 'value' => 2]
  8349. ],
  8350. 'labelOptions' => ['class' => 'custom-class'],
  8351. 'value' => ['1']
  8352. ]);
  8353. $expected = [
  8354. ['div' => ['class' => 'input select']],
  8355. ['label' => ['for' => 'checkbox1']],
  8356. 'My checkboxes',
  8357. '/label',
  8358. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8359. ['div' => ['class' => 'checkbox']],
  8360. ['label' => [
  8361. 'class' => 'custom-class selected',
  8362. 'for' => 'checkbox1-1'
  8363. ]],
  8364. ['input' => [
  8365. 'type' => 'checkbox',
  8366. 'name' => 'checkbox1[]',
  8367. 'value' => '1',
  8368. 'id' => 'checkbox1-1',
  8369. 'checked' => 'checked'
  8370. ]],
  8371. 'First Checkbox',
  8372. '/label',
  8373. '/div',
  8374. ['div' => ['class' => 'checkbox']],
  8375. ['label' => [
  8376. 'class' => 'custom-class',
  8377. 'for' => 'checkbox1-2'
  8378. ]],
  8379. ['input' => [
  8380. 'type' => 'checkbox',
  8381. 'name' => 'checkbox1[]',
  8382. 'value' => '2',
  8383. 'id' => 'checkbox1-2'
  8384. ]],
  8385. 'Second Checkbox',
  8386. '/label',
  8387. '/div',
  8388. '/div'
  8389. ];
  8390. $this->assertHtml($expected, $result);
  8391. $result = $this->Form->input('checkbox1', [
  8392. 'label' => 'My checkboxes',
  8393. 'multiple' => 'checkbox',
  8394. 'type' => 'select',
  8395. 'options' => [
  8396. ['text' => 'First Checkbox', 'value' => 1],
  8397. ['text' => 'Second Checkbox', 'value' => 2]
  8398. ],
  8399. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8400. 'value' => ['1']
  8401. ]);
  8402. $expected = [
  8403. ['div' => ['class' => 'input select']],
  8404. ['label' => ['for' => 'checkbox1']],
  8405. 'My checkboxes',
  8406. '/label',
  8407. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8408. ['div' => ['class' => 'checkbox']],
  8409. ['label' => [
  8410. 'class' => 'custom-class another-class selected',
  8411. 'data-name' => 'bob',
  8412. 'for' => 'checkbox1-1'
  8413. ]],
  8414. ['input' => [
  8415. 'type' => 'checkbox',
  8416. 'name' => 'checkbox1[]',
  8417. 'value' => '1',
  8418. 'id' => 'checkbox1-1',
  8419. 'checked' => 'checked'
  8420. ]],
  8421. 'First Checkbox',
  8422. '/label',
  8423. '/div',
  8424. ['div' => ['class' => 'checkbox']],
  8425. ['label' => [
  8426. 'class' => 'custom-class another-class',
  8427. 'data-name' => 'bob',
  8428. 'for' => 'checkbox1-2'
  8429. ]],
  8430. ['input' => [
  8431. 'type' => 'checkbox',
  8432. 'name' => 'checkbox1[]',
  8433. 'value' => '2',
  8434. 'id' => 'checkbox1-2'
  8435. ]],
  8436. 'Second Checkbox',
  8437. '/label',
  8438. '/div',
  8439. '/div'
  8440. ];
  8441. $this->assertHtml($expected, $result);
  8442. }
  8443. }