FormHelperTest.php 305 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Core\Configure;
  18. use Cake\Form\Form;
  19. use Cake\Http\ServerRequest;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Security;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\View;
  28. /**
  29. * Test stub.
  30. */
  31. class Article extends Entity
  32. {
  33. }
  34. /**
  35. * Contact class
  36. */
  37. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->setSchema($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->setSchema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property \Cake\View\Helper\FormHelper $Form
  103. * @property \Cake\View\View $View
  104. */
  105. class FormHelperTest extends TestCase
  106. {
  107. /**
  108. * Fixtures to be used
  109. *
  110. * @var array
  111. */
  112. public $fixtures = ['core.articles', 'core.comments'];
  113. /**
  114. * Do not load the fixtures by default
  115. *
  116. * @var bool
  117. */
  118. public $autoFixtures = false;
  119. /**
  120. * @var array
  121. */
  122. protected $article = [];
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp()
  129. {
  130. parent::setUp();
  131. Configure::write('Config.language', 'eng');
  132. Configure::write('App.base', '');
  133. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  134. $this->View = new View();
  135. $this->Form = new FormHelper($this->View);
  136. $request = new ServerRequest([
  137. 'webroot' => '',
  138. 'base' => '',
  139. 'url' => '/articles/add',
  140. 'params' => [
  141. 'controller' => 'articles',
  142. 'action' => 'add',
  143. ]
  144. ]);
  145. $this->Form->Url->request = $this->Form->request = $request;
  146. $this->dateRegex = [
  147. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  148. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  149. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  152. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  153. ];
  154. $this->article = [
  155. 'schema' => [
  156. 'id' => ['type' => 'integer'],
  157. 'author_id' => ['type' => 'integer', 'null' => true],
  158. 'title' => ['type' => 'string', 'null' => true],
  159. 'body' => 'text',
  160. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  161. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  162. ],
  163. 'required' => [
  164. 'author_id' => true,
  165. 'title' => true,
  166. ]
  167. ];
  168. Security::setSalt('foo!');
  169. Router::connect('/:controller', ['action' => 'index']);
  170. Router::connect('/:controller/:action/*');
  171. }
  172. /**
  173. * tearDown method
  174. *
  175. * @return void
  176. */
  177. public function tearDown()
  178. {
  179. parent::tearDown();
  180. unset($this->Form, $this->Controller, $this->View);
  181. TableRegistry::clear();
  182. }
  183. /**
  184. * Test construct() with the templates option.
  185. *
  186. * @return void
  187. */
  188. public function testConstructTemplatesFile()
  189. {
  190. $helper = new FormHelper($this->View, [
  191. 'templates' => 'htmlhelper_tags'
  192. ]);
  193. $result = $helper->control('name');
  194. $this->assertContains('<input', $result);
  195. }
  196. /**
  197. * Test that when specifying custom widgets the config array for that widget
  198. * is overwritten instead of merged.
  199. *
  200. * @return void
  201. */
  202. public function testConstructWithWidgets()
  203. {
  204. $config = [
  205. 'widgets' => [
  206. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  207. ]
  208. ];
  209. $helper = new FormHelper($this->View, $config);
  210. $registry = $helper->widgetRegistry();
  211. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  212. }
  213. /**
  214. * Test that when specifying custom widgets config file and it should be
  215. * added to widgets array. WidgetRegistry will load widgets in constructor.
  216. *
  217. * @return void
  218. */
  219. public function testConstructWithWidgetsConfig()
  220. {
  221. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  222. $registry = $helper->widgetRegistry();
  223. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  224. }
  225. /**
  226. * Test registering a new widget class and rendering it.
  227. *
  228. * @return void
  229. */
  230. public function testAddWidgetAndRenderWidget()
  231. {
  232. $data = [
  233. 'val' => 1
  234. ];
  235. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  236. $this->Form->addWidget('test', $mock);
  237. $mock->expects($this->once())
  238. ->method('render')
  239. ->with($data)
  240. ->will($this->returnValue('HTML'));
  241. $result = $this->Form->widget('test', $data);
  242. $this->assertEquals('HTML', $result);
  243. }
  244. /**
  245. * Test that secureFields() of widget is called after calling render(),
  246. * not before.
  247. *
  248. * @return void
  249. */
  250. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  251. {
  252. $data = [
  253. 'val' => 1,
  254. 'name' => 'test'
  255. ];
  256. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  257. $this->Form->addWidget('test', $mock);
  258. $mock->expects($this->at(0))
  259. ->method('render')
  260. ->with($data)
  261. ->will($this->returnValue('HTML'));
  262. $mock->expects($this->at(1))
  263. ->method('secureFields')
  264. ->with($data)
  265. ->will($this->returnValue(['test']));
  266. $result = $this->Form->widget('test', $data + ['secure' => true]);
  267. $this->assertEquals('HTML', $result);
  268. }
  269. /**
  270. * Test that empty string is not added to secure fields list when
  271. * rendering input widget without name.
  272. *
  273. * @return void
  274. */
  275. public function testRenderingWidgetWithEmptyName()
  276. {
  277. $this->assertEquals([], $this->Form->fields);
  278. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  279. $this->assertEquals('<select name=""></select>', $result);
  280. $this->assertEquals([], $this->Form->fields);
  281. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  282. $this->assertEquals('<select name="0"></select>', $result);
  283. $this->assertEquals(['0'], $this->Form->fields);
  284. }
  285. /**
  286. * Test registering an invalid widget class.
  287. *
  288. * @return void
  289. */
  290. public function testAddWidgetInvalid()
  291. {
  292. $this->expectException(\RuntimeException::class);
  293. $mock = new \StdClass();
  294. $this->Form->addWidget('test', $mock);
  295. $this->Form->widget('test');
  296. }
  297. /**
  298. * Test adding a new context class.
  299. *
  300. * @return void
  301. */
  302. public function testAddContextProvider()
  303. {
  304. $context = 'My data';
  305. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  306. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  307. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  308. $this->assertEquals($context, $data['entity']);
  309. return $stub;
  310. });
  311. $this->Form->create($context);
  312. $result = $this->Form->context();
  313. $this->assertSame($stub, $result);
  314. }
  315. /**
  316. * Test replacing a context class.
  317. *
  318. * @return void
  319. */
  320. public function testAddContextProviderReplace()
  321. {
  322. $entity = new Article();
  323. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  324. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  325. return $stub;
  326. });
  327. $this->Form->create($entity);
  328. $result = $this->Form->context();
  329. $this->assertSame($stub, $result);
  330. }
  331. /**
  332. * Test overriding a context class.
  333. *
  334. * @return void
  335. */
  336. public function testAddContextProviderAdd()
  337. {
  338. $entity = new Article();
  339. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  340. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  341. if ($data['entity'] instanceof Entity) {
  342. return $stub;
  343. }
  344. });
  345. $this->Form->create($entity);
  346. $result = $this->Form->context();
  347. $this->assertSame($stub, $result);
  348. }
  349. /**
  350. * Test adding an invalid context class.
  351. *
  352. * @return void
  353. */
  354. public function testAddContextProviderInvalid()
  355. {
  356. $this->expectException(\RuntimeException::class);
  357. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  358. $context = 'My data';
  359. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  360. return new \StdClass();
  361. });
  362. $this->Form->create($context);
  363. }
  364. /**
  365. * Provides context options for create().
  366. *
  367. * @return array
  368. */
  369. public function contextSelectionProvider()
  370. {
  371. $entity = new Article();
  372. $collection = new Collection([$entity]);
  373. $emptyCollection = new Collection([]);
  374. $arrayObject = new \ArrayObject([]);
  375. $data = [
  376. 'schema' => [
  377. 'title' => ['type' => 'string']
  378. ]
  379. ];
  380. $form = new Form();
  381. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  382. return [
  383. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  384. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  385. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  386. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  387. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  388. 'form' => [$form, 'Cake\View\Form\FormContext'],
  389. 'none' => [null, 'Cake\View\Form\NullContext'],
  390. 'false' => [false, 'Cake\View\Form\NullContext'],
  391. 'custom' => [$custom, get_class($custom)]
  392. ];
  393. }
  394. /**
  395. * Test default context selection in create()
  396. *
  397. * @dataProvider contextSelectionProvider
  398. * @return void
  399. */
  400. public function testCreateContextSelectionBuiltIn($data, $class)
  401. {
  402. $this->loadFixtures('Articles');
  403. $this->Form->create($data);
  404. $this->assertInstanceOf($class, $this->Form->context());
  405. }
  406. /**
  407. * Data provider for type option.
  408. *
  409. * @return array
  410. */
  411. public static function requestTypeProvider()
  412. {
  413. return [
  414. // type, method, override
  415. ['post', 'post', 'POST'],
  416. ['put', 'post', 'PUT'],
  417. ['patch', 'post', 'PATCH'],
  418. ['delete', 'post', 'DELETE'],
  419. ];
  420. }
  421. /**
  422. * Test creating file forms.
  423. *
  424. * @return void
  425. */
  426. public function testCreateFile()
  427. {
  428. $encoding = strtolower(Configure::read('App.encoding'));
  429. $result = $this->Form->create(false, ['type' => 'file']);
  430. $expected = [
  431. 'form' => [
  432. 'method' => 'post', 'action' => '/articles/add',
  433. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  434. ],
  435. 'div' => ['style' => 'display:none;'],
  436. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  437. '/div'
  438. ];
  439. $this->assertHtml($expected, $result);
  440. }
  441. /**
  442. * Test creating GET forms.
  443. *
  444. * @return void
  445. */
  446. public function testCreateGet()
  447. {
  448. $encoding = strtolower(Configure::read('App.encoding'));
  449. $result = $this->Form->create(false, ['type' => 'get']);
  450. $expected = ['form' => [
  451. 'method' => 'get', 'action' => '/articles/add',
  452. 'accept-charset' => $encoding
  453. ]];
  454. $this->assertHtml($expected, $result);
  455. }
  456. /**
  457. * Test explicit method/enctype options.
  458. *
  459. * Explicit method overwrites inferred method from 'type'
  460. *
  461. * @return void
  462. */
  463. public function testCreateExplicitMethodEnctype()
  464. {
  465. $encoding = strtolower(Configure::read('App.encoding'));
  466. $result = $this->Form->create(false, [
  467. 'type' => 'get',
  468. 'method' => 'put',
  469. 'enctype' => 'multipart/form-data'
  470. ]);
  471. $expected = ['form' => [
  472. 'method' => 'put',
  473. 'action' => '/articles/add',
  474. 'enctype' => 'multipart/form-data',
  475. 'accept-charset' => $encoding
  476. ]];
  477. $this->assertHtml($expected, $result);
  478. }
  479. /**
  480. * Test create() with the templates option.
  481. *
  482. * @return void
  483. */
  484. public function testCreateTemplatesArray()
  485. {
  486. $result = $this->Form->create($this->article, [
  487. 'templates' => [
  488. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  489. ]
  490. ]);
  491. $expected = [
  492. 'form' => [
  493. 'class' => 'form-horizontal',
  494. 'method' => 'post',
  495. 'action' => '/articles/add',
  496. 'accept-charset' => 'utf-8'
  497. ]
  498. ];
  499. $this->assertHtml($expected, $result);
  500. }
  501. /**
  502. * Test create() with the templates option.
  503. *
  504. * @return void
  505. */
  506. public function testCreateTemplatesFile()
  507. {
  508. $result = $this->Form->create($this->article, [
  509. 'templates' => 'htmlhelper_tags',
  510. ]);
  511. $expected = [
  512. 'start form',
  513. 'div' => ['class' => 'hidden'],
  514. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  515. '/div'
  516. ];
  517. $this->assertHtml($expected, $result);
  518. }
  519. /**
  520. * Test that create() and end() restore templates.
  521. *
  522. * @return void
  523. */
  524. public function testCreateEndRestoreTemplates()
  525. {
  526. $this->Form->create($this->article, [
  527. 'templates' => ['input' => 'custom input element']
  528. ]);
  529. $this->Form->end();
  530. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  531. }
  532. /**
  533. * Test using template vars in various templates used by control() method.
  534. *
  535. * @return void
  536. */
  537. public function testControlTemplateVars()
  538. {
  539. $result = $this->Form->control('text', [
  540. 'templates' => [
  541. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  542. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  543. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  544. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  545. ],
  546. 'templateVars' => [
  547. 'forinput' => 'in-input',
  548. 'forlabel' => 'in-label',
  549. 'forgroup' => 'in-group',
  550. 'forcontainer' => 'in-container'
  551. ]
  552. ]);
  553. $expected = [
  554. 'div' => ['class'],
  555. 'label' => ['for'],
  556. 'Text in-label',
  557. '/label',
  558. 'in-group',
  559. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  560. 'in-container',
  561. '/div',
  562. ];
  563. $this->assertHtml($expected, $result);
  564. }
  565. /**
  566. * Test ensuring template variables work in template files loaded
  567. * during control().
  568. *
  569. * @return void
  570. */
  571. public function testControlTemplatesFromFile()
  572. {
  573. $result = $this->Form->control('title', [
  574. 'templates' => 'test_templates',
  575. 'templateVars' => [
  576. 'forcontainer' => 'container-data'
  577. ]
  578. ]);
  579. $expected = [
  580. 'div' => ['class'],
  581. 'label' => ['for'],
  582. 'Title',
  583. '/label',
  584. 'input' => ['name', 'type' => 'text', 'id'],
  585. 'container-data',
  586. '/div',
  587. ];
  588. $this->assertHtml($expected, $result);
  589. }
  590. /**
  591. * Test using template vars in inputSubmit and submitContainer template.
  592. *
  593. * @return void
  594. */
  595. public function testSubmitTemplateVars()
  596. {
  597. $this->Form->setTemplates([
  598. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  599. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  600. ]);
  601. $result = $this->Form->submit('Submit', [
  602. 'templateVars' => [
  603. 'forinput' => 'in-input',
  604. 'forcontainer' => 'in-container'
  605. ]
  606. ]);
  607. $expected = [
  608. 'div' => ['class'],
  609. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  610. 'in-container',
  611. '/div',
  612. ];
  613. $this->assertHtml($expected, $result);
  614. }
  615. /**
  616. * test the create() method
  617. *
  618. * @dataProvider requestTypeProvider
  619. * @return void
  620. */
  621. public function testCreateTypeOptions($type, $method, $override)
  622. {
  623. $encoding = strtolower(Configure::read('App.encoding'));
  624. $result = $this->Form->create(false, ['type' => $type]);
  625. $expected = [
  626. 'form' => [
  627. 'method' => $method, 'action' => '/articles/add',
  628. 'accept-charset' => $encoding
  629. ],
  630. 'div' => ['style' => 'display:none;'],
  631. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  632. '/div'
  633. ];
  634. $this->assertHtml($expected, $result);
  635. }
  636. /**
  637. * Test using template vars in Create (formStart template)
  638. *
  639. * @return void
  640. */
  641. public function testCreateTemplateVars()
  642. {
  643. $result = $this->Form->create($this->article, [
  644. 'templates' => [
  645. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  646. ],
  647. 'templateVars' => ['header' => 'headertext']
  648. ]);
  649. $expected = [
  650. 'h4' => ['class'],
  651. 'headertext',
  652. '/h4',
  653. 'form' => [
  654. 'method' => 'post',
  655. 'action' => '/articles/add',
  656. 'accept-charset' => 'utf-8'
  657. ]
  658. ];
  659. $this->assertHtml($expected, $result);
  660. }
  661. /**
  662. * Test opening a form for an update operation.
  663. *
  664. * @return void
  665. */
  666. public function testCreateUpdateForm()
  667. {
  668. $encoding = strtolower(Configure::read('App.encoding'));
  669. $this->Form->request = $this->Form->request
  670. ->withRequestTarget('/articles/edit/1')
  671. ->withParam('action', 'edit');
  672. $this->article['defaults']['id'] = 1;
  673. $result = $this->Form->create($this->article);
  674. $expected = [
  675. 'form' => [
  676. 'method' => 'post',
  677. 'action' => '/articles/edit/1',
  678. 'accept-charset' => $encoding
  679. ],
  680. 'div' => ['style' => 'display:none;'],
  681. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  682. '/div'
  683. ];
  684. $this->assertHtml($expected, $result);
  685. }
  686. /**
  687. * test create() with automatic url generation
  688. *
  689. * @return void
  690. */
  691. public function testCreateAutoUrl()
  692. {
  693. $encoding = strtolower(Configure::read('App.encoding'));
  694. $this->Form->request = $this->Form->request
  695. ->withRequestTarget('/articles/delete/10')
  696. ->withParam('action', 'delete');
  697. $result = $this->Form->create($this->article);
  698. $expected = [
  699. 'form' => [
  700. 'method' => 'post', 'action' => '/articles/delete/10',
  701. 'accept-charset' => $encoding
  702. ],
  703. 'div' => ['style' => 'display:none;'],
  704. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  705. '/div'
  706. ];
  707. $this->assertHtml($expected, $result);
  708. $this->article['defaults'] = ['id' => 1];
  709. $this->Form->request->here = '/articles/edit/1';
  710. $this->Form->request->params['action'] = 'delete';
  711. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  712. $expected = [
  713. 'form' => [
  714. 'method' => 'post',
  715. 'action' => '/articles/edit/1',
  716. 'accept-charset' => $encoding
  717. ],
  718. 'div' => ['style' => 'display:none;'],
  719. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  720. '/div'
  721. ];
  722. $this->assertHtml($expected, $result);
  723. $this->Form->request->params['action'] = 'add';
  724. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  725. $expected = [
  726. 'form' => [
  727. 'method' => 'post',
  728. 'action' => '/articles/publish/1',
  729. 'accept-charset' => $encoding
  730. ],
  731. 'div' => ['style' => 'display:none;'],
  732. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  733. '/div'
  734. ];
  735. $this->assertHtml($expected, $result);
  736. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  737. $expected = [
  738. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  739. 'div' => ['style' => 'display:none;'],
  740. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  741. '/div'
  742. ];
  743. $this->assertHtml($expected, $result);
  744. $this->Form->request->params['controller'] = 'Pages';
  745. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  746. $expected = [
  747. 'form' => [
  748. 'method' => 'post', 'action' => '/Pages/signup/1',
  749. 'accept-charset' => $encoding
  750. ],
  751. 'div' => ['style' => 'display:none;'],
  752. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  753. '/div'
  754. ];
  755. $this->assertHtml($expected, $result);
  756. }
  757. /**
  758. * Test create() with no URL (no "action" attribute for <form> tag)
  759. *
  760. * @return void
  761. */
  762. public function testCreateNoUrl()
  763. {
  764. $result = $this->Form->create(false, ['url' => false]);
  765. $expected = [
  766. 'form' => [
  767. 'method' => 'post',
  768. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  769. ],
  770. 'div' => ['style' => 'display:none;'],
  771. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  772. '/div'
  773. ];
  774. $this->assertHtml($expected, $result);
  775. }
  776. /**
  777. * test create() with a custom route
  778. *
  779. * @return void
  780. */
  781. public function testCreateCustomRoute()
  782. {
  783. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  784. $encoding = strtolower(Configure::read('App.encoding'));
  785. $this->Form->request->params['controller'] = 'users';
  786. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  787. $expected = [
  788. 'form' => [
  789. 'method' => 'post', 'action' => '/login',
  790. 'accept-charset' => $encoding
  791. ],
  792. 'div' => ['style' => 'display:none;'],
  793. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  794. '/div'
  795. ];
  796. $this->assertHtml($expected, $result);
  797. Router::connect(
  798. '/new-article',
  799. ['controller' => 'articles', 'action' => 'myaction'],
  800. ['_name' => 'my-route']
  801. );
  802. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  803. $expected = [
  804. 'form' => [
  805. 'method' => 'post', 'action' => '/new-article',
  806. 'accept-charset' => $encoding,
  807. ],
  808. 'div' => ['style' => 'display:none;'],
  809. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  810. '/div'
  811. ];
  812. $this->assertHtml($expected, $result);
  813. }
  814. /**
  815. * test automatic accept-charset overriding
  816. *
  817. * @return void
  818. */
  819. public function testCreateWithAcceptCharset()
  820. {
  821. $result = $this->Form->create(
  822. $this->article,
  823. [
  824. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  825. ]
  826. );
  827. $expected = [
  828. 'form' => [
  829. 'method' => 'post', 'action' => '/articles',
  830. 'accept-charset' => 'iso-8859-1'
  831. ],
  832. 'div' => ['style' => 'display:none;'],
  833. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  834. '/div'
  835. ];
  836. $this->assertHtml($expected, $result);
  837. }
  838. /**
  839. * Test base form URL when url param is passed with multiple parameters (&)
  840. *
  841. */
  842. public function testCreateQueryStringRequest()
  843. {
  844. $encoding = strtolower(Configure::read('App.encoding'));
  845. $result = $this->Form->create($this->article, [
  846. 'type' => 'post',
  847. 'escape' => false,
  848. 'url' => [
  849. 'controller' => 'controller',
  850. 'action' => 'action',
  851. '?' => ['param1' => 'value1', 'param2' => 'value2']
  852. ]
  853. ]);
  854. $expected = [
  855. 'form' => [
  856. 'method' => 'post',
  857. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  858. 'accept-charset' => $encoding
  859. ],
  860. 'div' => ['style' => 'display:none;'],
  861. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  862. '/div'
  863. ];
  864. $this->assertHtml($expected, $result);
  865. $result = $this->Form->create($this->article, [
  866. 'type' => 'post',
  867. 'url' => [
  868. 'controller' => 'controller',
  869. 'action' => 'action',
  870. '?' => ['param1' => 'value1', 'param2' => 'value2']
  871. ]
  872. ]);
  873. $this->assertHtml($expected, $result);
  874. }
  875. /**
  876. * test that create() doesn't cause errors by multiple id's being in the primary key
  877. * as could happen with multiple select or checkboxes.
  878. *
  879. * @return void
  880. */
  881. public function testCreateWithMultipleIdInData()
  882. {
  883. $encoding = strtolower(Configure::read('App.encoding'));
  884. $this->Form->request->data['Article']['id'] = [1, 2];
  885. $result = $this->Form->create($this->article);
  886. $expected = [
  887. 'form' => [
  888. 'method' => 'post',
  889. 'action' => '/articles/add',
  890. 'accept-charset' => $encoding
  891. ],
  892. 'div' => ['style' => 'display:none;'],
  893. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  894. '/div'
  895. ];
  896. $this->assertHtml($expected, $result);
  897. }
  898. /**
  899. * test that create() doesn't add in extra passed params.
  900. *
  901. * @return void
  902. */
  903. public function testCreatePassedArgs()
  904. {
  905. $encoding = strtolower(Configure::read('App.encoding'));
  906. $this->Form->request->data['Article']['id'] = 1;
  907. $result = $this->Form->create($this->article, [
  908. 'type' => 'post',
  909. 'escape' => false,
  910. 'url' => [
  911. 'action' => 'edit',
  912. 'myparam'
  913. ]
  914. ]);
  915. $expected = [
  916. 'form' => [
  917. 'method' => 'post',
  918. 'action' => '/articles/edit/myparam',
  919. 'accept-charset' => $encoding
  920. ],
  921. 'div' => ['style' => 'display:none;'],
  922. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  923. '/div'
  924. ];
  925. $this->assertHtml($expected, $result);
  926. }
  927. /**
  928. * test creating a get form, and get form inputs.
  929. *
  930. * @return void
  931. */
  932. public function testGetFormCreate()
  933. {
  934. $encoding = strtolower(Configure::read('App.encoding'));
  935. $result = $this->Form->create($this->article, ['type' => 'get']);
  936. $expected = ['form' => [
  937. 'method' => 'get', 'action' => '/articles/add',
  938. 'accept-charset' => $encoding
  939. ]];
  940. $this->assertHtml($expected, $result);
  941. $result = $this->Form->text('title');
  942. $expected = ['input' => [
  943. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  944. ]];
  945. $this->assertHtml($expected, $result);
  946. $result = $this->Form->password('password');
  947. $expected = ['input' => [
  948. 'name' => 'password', 'type' => 'password'
  949. ]];
  950. $this->assertHtml($expected, $result);
  951. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  952. $result = $this->Form->text('user_form');
  953. $expected = ['input' => [
  954. 'name' => 'user_form', 'type' => 'text'
  955. ]];
  956. $this->assertHtml($expected, $result);
  957. }
  958. /**
  959. * test get form, and inputs when the model param is false
  960. *
  961. * @return void
  962. */
  963. public function testGetFormWithFalseModel()
  964. {
  965. $encoding = strtolower(Configure::read('App.encoding'));
  966. $this->Form->request->params['controller'] = 'contact_test';
  967. $result = $this->Form->create(false, [
  968. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  969. ]);
  970. $expected = ['form' => [
  971. 'method' => 'get', 'action' => '/contact_test/add',
  972. 'accept-charset' => $encoding
  973. ]];
  974. $this->assertHtml($expected, $result);
  975. $result = $this->Form->text('reason');
  976. $expected = [
  977. 'input' => ['type' => 'text', 'name' => 'reason']
  978. ];
  979. $this->assertHtml($expected, $result);
  980. }
  981. /**
  982. * testFormCreateWithSecurity method
  983. *
  984. * Test form->create() with security key.
  985. *
  986. * @return void
  987. */
  988. public function testCreateWithSecurity()
  989. {
  990. $this->Form->request->params['_csrfToken'] = 'testKey';
  991. $encoding = strtolower(Configure::read('App.encoding'));
  992. $result = $this->Form->create($this->article, [
  993. 'url' => '/articles/publish',
  994. ]);
  995. $expected = [
  996. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  997. 'div' => ['style' => 'display:none;'],
  998. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  999. ['input' => [
  1000. 'type' => 'hidden',
  1001. 'name' => '_csrfToken',
  1002. 'value' => 'testKey',
  1003. 'autocomplete' => 'off',
  1004. ]],
  1005. '/div'
  1006. ];
  1007. $this->assertHtml($expected, $result);
  1008. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1009. $expected['form']['id'] = 'MyForm';
  1010. $this->assertHtml($expected, $result);
  1011. }
  1012. /**
  1013. * testFormCreateGetNoSecurity method
  1014. *
  1015. * Test form->create() with no security key as its a get form
  1016. *
  1017. * @return void
  1018. */
  1019. public function testCreateEndGetNoSecurity()
  1020. {
  1021. $this->Form->request->params['_csrfToken'] = 'testKey';
  1022. $article = new Article();
  1023. $result = $this->Form->create($article, [
  1024. 'type' => 'get',
  1025. 'url' => '/contacts/add'
  1026. ]);
  1027. $this->assertNotContains('testKey', $result);
  1028. $result = $this->Form->end();
  1029. $this->assertNotContains('testKey', $result);
  1030. }
  1031. /**
  1032. * test that create() clears the fields property so it starts fresh
  1033. *
  1034. * @return void
  1035. */
  1036. public function testCreateClearingFields()
  1037. {
  1038. $this->Form->fields = ['model_id'];
  1039. $this->Form->create($this->article);
  1040. $this->assertEquals([], $this->Form->fields);
  1041. }
  1042. /**
  1043. * Tests form hash generation with model-less data
  1044. *
  1045. * @return void
  1046. */
  1047. public function testValidateHashNoModel()
  1048. {
  1049. $this->Form->request->params['_Token'] = 'foo';
  1050. $fields = ['anything'];
  1051. $result = $this->Form->secure($fields);
  1052. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1053. $this->assertContains($hash, $result);
  1054. }
  1055. /**
  1056. * Tests that hidden fields generated for checkboxes don't get locked
  1057. *
  1058. * @return void
  1059. */
  1060. public function testNoCheckboxLocking()
  1061. {
  1062. $this->Form->request->params['_Token'] = 'foo';
  1063. $this->assertSame([], $this->Form->fields);
  1064. $this->Form->checkbox('check', ['value' => '1']);
  1065. $this->assertSame($this->Form->fields, ['check']);
  1066. }
  1067. /**
  1068. * testFormSecurityFields method
  1069. *
  1070. * Test generation of secure form hash generation.
  1071. *
  1072. * @return void
  1073. */
  1074. public function testFormSecurityFields()
  1075. {
  1076. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1077. $this->Form->request->params['_Token'] = 'testKey';
  1078. $result = $this->Form->secure($fields);
  1079. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1080. $hash .= ':' . 'Model.valid';
  1081. $hash = urlencode($hash);
  1082. $tokenDebug = urlencode(json_encode([
  1083. '',
  1084. $fields,
  1085. []
  1086. ]));
  1087. $expected = [
  1088. 'div' => ['style' => 'display:none;'],
  1089. ['input' => [
  1090. 'type' => 'hidden',
  1091. 'name' => '_Token[fields]',
  1092. 'value' => $hash,
  1093. 'autocomplete' => 'off',
  1094. ]],
  1095. ['input' => [
  1096. 'type' => 'hidden',
  1097. 'name' => '_Token[unlocked]',
  1098. 'value' => '',
  1099. 'autocomplete' => 'off',
  1100. ]],
  1101. ['input' => [
  1102. 'type' => 'hidden',
  1103. 'name' => '_Token[debug]',
  1104. 'value' => $tokenDebug,
  1105. 'autocomplete' => 'off',
  1106. ]],
  1107. '/div'
  1108. ];
  1109. $this->assertHtml($expected, $result);
  1110. }
  1111. /**
  1112. * testFormSecurityFields method
  1113. *
  1114. * Test debug token is not generated if debug is false
  1115. *
  1116. * @return void
  1117. */
  1118. public function testFormSecurityFieldsNoDebugMode()
  1119. {
  1120. Configure::write('debug', false);
  1121. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1122. $this->Form->request->params['_Token'] = 'testKey';
  1123. $result = $this->Form->secure($fields);
  1124. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1125. $hash .= ':' . 'Model.valid';
  1126. $hash = urlencode($hash);
  1127. $expected = [
  1128. 'div' => ['style' => 'display:none;'],
  1129. ['input' => [
  1130. 'type' => 'hidden',
  1131. 'name' => '_Token[fields]',
  1132. 'autocomplete' => 'off',
  1133. 'value' => $hash
  1134. ]],
  1135. ['input' => [
  1136. 'type' => 'hidden',
  1137. 'name' => '_Token[unlocked]',
  1138. 'autocomplete' => 'off',
  1139. 'value' => '',
  1140. ]],
  1141. '/div'
  1142. ];
  1143. $this->assertHtml($expected, $result);
  1144. }
  1145. /**
  1146. * Tests correct generation of number fields for smallint
  1147. *
  1148. * @return void
  1149. */
  1150. public function testTextFieldGenerationForSmallint()
  1151. {
  1152. $this->article['schema'] = [
  1153. 'foo' => [
  1154. 'type' => 'smallinteger',
  1155. 'null' => false,
  1156. 'default' => null,
  1157. 'length' => 10
  1158. ]
  1159. ];
  1160. $this->Form->create($this->article);
  1161. $result = $this->Form->control('foo');
  1162. $this->assertContains('class="input number"', $result);
  1163. $this->assertContains('type="number"', $result);
  1164. }
  1165. /**
  1166. * Tests correct generation of number fields for tinyint
  1167. *
  1168. * @return void
  1169. */
  1170. public function testTextFieldGenerationForTinyint()
  1171. {
  1172. $this->article['schema'] = [
  1173. 'foo' => [
  1174. 'type' => 'tinyinteger',
  1175. 'null' => false,
  1176. 'default' => null,
  1177. 'length' => 10
  1178. ]
  1179. ];
  1180. $this->Form->create($this->article);
  1181. $result = $this->Form->control('foo');
  1182. $this->assertContains('class="input number"', $result);
  1183. $this->assertContains('type="number"', $result);
  1184. }
  1185. /**
  1186. * Tests correct generation of number fields for double and float fields
  1187. *
  1188. * @return void
  1189. */
  1190. public function testTextFieldGenerationForFloats()
  1191. {
  1192. $this->article['schema'] = [
  1193. 'foo' => [
  1194. 'type' => 'float',
  1195. 'null' => false,
  1196. 'default' => null,
  1197. 'length' => 10
  1198. ]
  1199. ];
  1200. $this->Form->create($this->article);
  1201. $result = $this->Form->control('foo');
  1202. $expected = [
  1203. 'div' => ['class' => 'input number'],
  1204. 'label' => ['for' => 'foo'],
  1205. 'Foo',
  1206. '/label',
  1207. ['input' => [
  1208. 'type' => 'number',
  1209. 'name' => 'foo',
  1210. 'id' => 'foo',
  1211. 'step' => 'any'
  1212. ]],
  1213. '/div'
  1214. ];
  1215. $this->assertHtml($expected, $result);
  1216. $result = $this->Form->control('foo', ['step' => 0.5]);
  1217. $expected = [
  1218. 'div' => ['class' => 'input number'],
  1219. 'label' => ['for' => 'foo'],
  1220. 'Foo',
  1221. '/label',
  1222. ['input' => [
  1223. 'type' => 'number',
  1224. 'name' => 'foo',
  1225. 'id' => 'foo',
  1226. 'step' => '0.5'
  1227. ]],
  1228. '/div'
  1229. ];
  1230. $this->assertHtml($expected, $result);
  1231. }
  1232. /**
  1233. * Tests correct generation of number fields for integer fields
  1234. *
  1235. * @return void
  1236. */
  1237. public function testTextFieldTypeNumberGenerationForIntegers()
  1238. {
  1239. TableRegistry::get('Contacts', [
  1240. 'className' => __NAMESPACE__ . '\ContactsTable'
  1241. ]);
  1242. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1243. $result = $this->Form->control('age');
  1244. $expected = [
  1245. 'div' => ['class' => 'input number'],
  1246. 'label' => ['for' => 'age'],
  1247. 'Age',
  1248. '/label',
  1249. ['input' => [
  1250. 'type' => 'number', 'name' => 'age',
  1251. 'id' => 'age'
  1252. ]],
  1253. '/div'
  1254. ];
  1255. $this->assertHtml($expected, $result);
  1256. }
  1257. /**
  1258. * Tests correct generation of file upload fields for binary fields
  1259. *
  1260. * @return void
  1261. */
  1262. public function testFileUploadFieldTypeGenerationForBinaries()
  1263. {
  1264. $table = TableRegistry::get('Contacts', [
  1265. 'className' => __NAMESPACE__ . '\ContactsTable'
  1266. ]);
  1267. $table->setSchema(['foo' => [
  1268. 'type' => 'binary',
  1269. 'null' => false,
  1270. 'default' => null,
  1271. 'length' => 1024
  1272. ]]);
  1273. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1274. $result = $this->Form->control('foo');
  1275. $expected = [
  1276. 'div' => ['class' => 'input file'],
  1277. 'label' => ['for' => 'foo'],
  1278. 'Foo',
  1279. '/label',
  1280. ['input' => [
  1281. 'type' => 'file', 'name' => 'foo',
  1282. 'id' => 'foo'
  1283. ]],
  1284. '/div'
  1285. ];
  1286. $this->assertHtml($expected, $result);
  1287. }
  1288. /**
  1289. * testFormSecurityMultipleFields method
  1290. *
  1291. * Test secure() with multiple row form. Ensure hash is correct.
  1292. *
  1293. * @return void
  1294. */
  1295. public function testFormSecurityMultipleFields()
  1296. {
  1297. $this->Form->request->params['_Token'] = 'foo';
  1298. $fields = [
  1299. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1300. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1301. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1302. ];
  1303. $result = $this->Form->secure($fields);
  1304. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1305. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1306. $tokenDebug = urlencode(json_encode([
  1307. '',
  1308. $fields,
  1309. []
  1310. ]));
  1311. $expected = [
  1312. 'div' => ['style' => 'display:none;'],
  1313. ['input' => [
  1314. 'type' => 'hidden',
  1315. 'name' => '_Token[fields]',
  1316. 'value' => $hash,
  1317. 'autocomplete' => 'off',
  1318. ]],
  1319. ['input' => [
  1320. 'type' => 'hidden',
  1321. 'name' => '_Token[unlocked]',
  1322. 'autocomplete' => 'off',
  1323. 'value' => ''
  1324. ]],
  1325. ['input' => [
  1326. 'type' => 'hidden',
  1327. 'name' => '_Token[debug]',
  1328. 'value' => $tokenDebug,
  1329. 'autocomplete' => 'off',
  1330. ]],
  1331. '/div'
  1332. ];
  1333. $this->assertHtml($expected, $result);
  1334. }
  1335. /**
  1336. * testFormSecurityMultipleSubmitButtons
  1337. *
  1338. * test form submit generation and ensure that _Token is only created on end()
  1339. *
  1340. * @return void
  1341. */
  1342. public function testFormSecurityMultipleSubmitButtons()
  1343. {
  1344. $this->Form->request->params['_Token'] = 'testKey';
  1345. $this->Form->create($this->article);
  1346. $this->Form->text('Address.title');
  1347. $this->Form->text('Address.first_name');
  1348. $result = $this->Form->submit('Save', ['name' => 'save']);
  1349. $expected = [
  1350. 'div' => ['class' => 'submit'],
  1351. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1352. '/div',
  1353. ];
  1354. $this->assertHtml($expected, $result);
  1355. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1356. $expected = [
  1357. 'div' => ['class' => 'submit'],
  1358. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1359. '/div',
  1360. ];
  1361. $this->assertHtml($expected, $result);
  1362. $result = $this->Form->end();
  1363. $tokenDebug = urlencode(json_encode([
  1364. '/articles/add',
  1365. [
  1366. 'Address.title',
  1367. 'Address.first_name',
  1368. ],
  1369. ['save', 'cancel']
  1370. ]));
  1371. $expected = [
  1372. 'div' => ['style' => 'display:none;'],
  1373. ['input' => [
  1374. 'type' => 'hidden',
  1375. 'name' => '_Token[fields]',
  1376. 'autocomplete',
  1377. 'value'
  1378. ]],
  1379. ['input' => [
  1380. 'type' => 'hidden',
  1381. 'name' => '_Token[unlocked]',
  1382. 'value' => 'cancel%7Csave',
  1383. 'autocomplete' => 'off',
  1384. ]],
  1385. ['input' => [
  1386. 'type' => 'hidden',
  1387. 'name' => '_Token[debug]',
  1388. 'value' => $tokenDebug,
  1389. 'autocomplete' => 'off',
  1390. ]],
  1391. '/div'
  1392. ];
  1393. $this->assertHtml($expected, $result);
  1394. }
  1395. /**
  1396. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1397. *
  1398. * @return void
  1399. */
  1400. public function testSecurityButtonNestedNamed()
  1401. {
  1402. $key = 'testKey';
  1403. $this->Form->request->params['_csrfToken'] = $key;
  1404. $this->Form->create('Addresses');
  1405. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1406. $result = $this->Form->unlockField();
  1407. $this->assertEquals(['Address.button'], $result);
  1408. }
  1409. /**
  1410. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1411. *
  1412. * @return void
  1413. */
  1414. public function testSecuritySubmitNestedNamed()
  1415. {
  1416. $this->Form->request->params['_Token'] = 'testKey';
  1417. $this->Form->create($this->article);
  1418. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1419. $result = $this->Form->unlockField();
  1420. $this->assertEquals(['Address.button'], $result);
  1421. }
  1422. /**
  1423. * Test that the correct fields are unlocked for image submits with no names.
  1424. *
  1425. * @return void
  1426. */
  1427. public function testSecuritySubmitImageNoName()
  1428. {
  1429. $this->Form->request->params['_Token'] = 'testKey';
  1430. $this->Form->create(false);
  1431. $result = $this->Form->submit('save.png');
  1432. $expected = [
  1433. 'div' => ['class' => 'submit'],
  1434. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1435. '/div'
  1436. ];
  1437. $this->assertHtml($expected, $result);
  1438. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1439. }
  1440. /**
  1441. * Test that the correct fields are unlocked for image submits with names.
  1442. *
  1443. * @return void
  1444. */
  1445. public function testSecuritySubmitImageName()
  1446. {
  1447. $this->Form->request->params['_Token'] = 'testKey';
  1448. $this->Form->create(null);
  1449. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1450. $expected = [
  1451. 'div' => ['class' => 'submit'],
  1452. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1453. '/div'
  1454. ];
  1455. $this->assertHtml($expected, $result);
  1456. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1457. }
  1458. /**
  1459. * testFormSecurityMultipleControlFields method
  1460. *
  1461. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1462. *
  1463. * @return void
  1464. */
  1465. public function testFormSecurityMultipleControlFields()
  1466. {
  1467. $this->Form->request->params['_Token'] = 'testKey';
  1468. $this->Form->create();
  1469. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1470. $this->Form->control('Addresses.0.title');
  1471. $this->Form->control('Addresses.0.first_name');
  1472. $this->Form->control('Addresses.0.last_name');
  1473. $this->Form->control('Addresses.0.address');
  1474. $this->Form->control('Addresses.0.city');
  1475. $this->Form->control('Addresses.0.phone');
  1476. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1477. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1478. $this->Form->control('Addresses.1.title');
  1479. $this->Form->control('Addresses.1.first_name');
  1480. $this->Form->control('Addresses.1.last_name');
  1481. $this->Form->control('Addresses.1.address');
  1482. $this->Form->control('Addresses.1.city');
  1483. $this->Form->control('Addresses.1.phone');
  1484. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1485. $result = $this->Form->secure($this->Form->fields);
  1486. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1487. $tokenDebug = urlencode(json_encode([
  1488. '/articles/add',
  1489. [
  1490. 'Addresses.0.id' => '123456',
  1491. 'Addresses.0.title',
  1492. 'Addresses.0.first_name',
  1493. 'Addresses.0.last_name',
  1494. 'Addresses.0.address',
  1495. 'Addresses.0.city',
  1496. 'Addresses.0.phone',
  1497. 'Addresses.0.primary',
  1498. 'Addresses.1.id' => '654321',
  1499. 'Addresses.1.title',
  1500. 'Addresses.1.first_name',
  1501. 'Addresses.1.last_name',
  1502. 'Addresses.1.address',
  1503. 'Addresses.1.city',
  1504. 'Addresses.1.phone',
  1505. 'Addresses.1.primary',
  1506. ],
  1507. []
  1508. ]));
  1509. $expected = [
  1510. 'div' => ['style' => 'display:none;'],
  1511. ['input' => [
  1512. 'type' => 'hidden',
  1513. 'name' => '_Token[fields]',
  1514. 'value' => $hash,
  1515. 'autocomplete' => 'off',
  1516. ]],
  1517. ['input' => [
  1518. 'type' => 'hidden',
  1519. 'name' => '_Token[unlocked]',
  1520. 'autocomplete' => 'off',
  1521. 'value' => ''
  1522. ]],
  1523. ['input' => [
  1524. 'type' => 'hidden',
  1525. 'name' => '_Token[debug]',
  1526. 'value' => $tokenDebug,
  1527. 'autocomplete' => 'off',
  1528. ]],
  1529. '/div'
  1530. ];
  1531. $this->assertHtml($expected, $result);
  1532. }
  1533. /**
  1534. * testFormSecurityArrayFields method
  1535. *
  1536. * Test form security with Model.field.0 style inputs.
  1537. *
  1538. * @return void
  1539. */
  1540. public function testFormSecurityArrayFields()
  1541. {
  1542. $this->Form->request->params['_Token'] = 'testKey';
  1543. $this->Form->create();
  1544. $this->Form->text('Address.primary.1');
  1545. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1546. $this->Form->text('Address.secondary.1.0');
  1547. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1548. }
  1549. /**
  1550. * testFormSecurityMultipleControlDisabledFields method
  1551. *
  1552. * Test secure form generation with multiple records and disabled fields.
  1553. *
  1554. * @return void
  1555. */
  1556. public function testFormSecurityMultipleControlDisabledFields()
  1557. {
  1558. $this->Form->request->params['_Token'] = [
  1559. 'unlockedFields' => ['first_name', 'address']
  1560. ];
  1561. $this->Form->create();
  1562. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1563. $this->Form->text('Addresses.0.title');
  1564. $this->Form->text('Addresses.0.first_name');
  1565. $this->Form->text('Addresses.0.last_name');
  1566. $this->Form->text('Addresses.0.address');
  1567. $this->Form->text('Addresses.0.city');
  1568. $this->Form->text('Addresses.0.phone');
  1569. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1570. $this->Form->text('Addresses.1.title');
  1571. $this->Form->text('Addresses.1.first_name');
  1572. $this->Form->text('Addresses.1.last_name');
  1573. $this->Form->text('Addresses.1.address');
  1574. $this->Form->text('Addresses.1.city');
  1575. $this->Form->text('Addresses.1.phone');
  1576. $result = $this->Form->secure($this->Form->fields);
  1577. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1578. $tokenDebug = urlencode(json_encode([
  1579. '/articles/add',
  1580. [
  1581. 'Addresses.0.id' => '123456',
  1582. 'Addresses.0.title',
  1583. 'Addresses.0.last_name',
  1584. 'Addresses.0.city',
  1585. 'Addresses.0.phone',
  1586. 'Addresses.1.id' => '654321',
  1587. 'Addresses.1.title',
  1588. 'Addresses.1.last_name',
  1589. 'Addresses.1.city',
  1590. 'Addresses.1.phone'
  1591. ],
  1592. [
  1593. 'first_name',
  1594. 'address'
  1595. ]
  1596. ]));
  1597. $expected = [
  1598. 'div' => ['style' => 'display:none;'],
  1599. ['input' => [
  1600. 'type' => 'hidden',
  1601. 'name' => '_Token[fields]',
  1602. 'autocomplete' => 'off',
  1603. 'value' => $hash
  1604. ]],
  1605. ['input' => [
  1606. 'type' => 'hidden',
  1607. 'name' => '_Token[unlocked]',
  1608. 'autocomplete' => 'off',
  1609. 'value' => 'address%7Cfirst_name',
  1610. ]],
  1611. ['input' => [
  1612. 'type' => 'hidden',
  1613. 'name' => '_Token[debug]',
  1614. 'autocomplete' => 'off',
  1615. 'value' => $tokenDebug,
  1616. ]],
  1617. '/div'
  1618. ];
  1619. $this->assertHtml($expected, $result);
  1620. }
  1621. /**
  1622. * testFormSecurityControlDisabledFields method
  1623. *
  1624. * Test single record form with disabled fields.
  1625. *
  1626. * @return void
  1627. */
  1628. public function testFormSecurityControlUnlockedFields()
  1629. {
  1630. $this->Form->request->params['_Token'] = [
  1631. 'unlockedFields' => ['first_name', 'address']
  1632. ];
  1633. $this->Form->create();
  1634. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1635. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1636. $this->Form->text('Addresses.title');
  1637. $this->Form->text('Addresses.first_name');
  1638. $this->Form->text('Addresses.last_name');
  1639. $this->Form->text('Addresses.address');
  1640. $this->Form->text('Addresses.city');
  1641. $this->Form->text('Addresses.phone');
  1642. $result = $this->Form->fields;
  1643. $expected = [
  1644. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1645. 'Addresses.city', 'Addresses.phone'
  1646. ];
  1647. $this->assertEquals($expected, $result);
  1648. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1649. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1650. $tokenDebug = urlencode(json_encode([
  1651. '/articles/add',
  1652. [
  1653. 'Addresses.id' => '123456',
  1654. 'Addresses.title',
  1655. 'Addresses.last_name',
  1656. 'Addresses.city',
  1657. 'Addresses.phone'
  1658. ],
  1659. [
  1660. 'first_name',
  1661. 'address'
  1662. ]
  1663. ]));
  1664. $expected = [
  1665. 'div' => ['style' => 'display:none;'],
  1666. ['input' => [
  1667. 'type' => 'hidden',
  1668. 'name' => '_Token[fields]',
  1669. 'value' => $hash,
  1670. 'autocomplete' => 'off',
  1671. 'data-foo' => 'bar',
  1672. ]],
  1673. ['input' => [
  1674. 'type' => 'hidden',
  1675. 'name' => '_Token[unlocked]',
  1676. 'value' => 'address%7Cfirst_name',
  1677. 'autocomplete' => 'off',
  1678. 'data-foo' => 'bar',
  1679. ]],
  1680. ['input' => [
  1681. 'type' => 'hidden', 'name' => '_Token[debug]',
  1682. 'value' => $tokenDebug,
  1683. 'autocomplete' => 'off',
  1684. 'data-foo' => 'bar'
  1685. ]],
  1686. '/div'
  1687. ];
  1688. $this->assertHtml($expected, $result);
  1689. }
  1690. /**
  1691. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1692. *
  1693. * Test single record form with debugSecurity param.
  1694. *
  1695. * @return void
  1696. */
  1697. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1698. {
  1699. $this->Form->request->params['_Token'] = [
  1700. 'unlockedFields' => ['first_name', 'address']
  1701. ];
  1702. $this->Form->create();
  1703. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1704. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1705. $this->Form->text('Addresses.title');
  1706. $this->Form->text('Addresses.first_name');
  1707. $this->Form->text('Addresses.last_name');
  1708. $this->Form->text('Addresses.address');
  1709. $this->Form->text('Addresses.city');
  1710. $this->Form->text('Addresses.phone');
  1711. $result = $this->Form->fields;
  1712. $expected = [
  1713. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1714. 'Addresses.city', 'Addresses.phone'
  1715. ];
  1716. $this->assertEquals($expected, $result);
  1717. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1718. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1719. $tokenDebug = urlencode(json_encode([
  1720. '/articles/add',
  1721. [
  1722. 'Addresses.id' => '123456',
  1723. 'Addresses.title',
  1724. 'Addresses.last_name',
  1725. 'Addresses.city',
  1726. 'Addresses.phone'
  1727. ],
  1728. [
  1729. 'first_name',
  1730. 'address'
  1731. ]
  1732. ]));
  1733. $expected = [
  1734. 'div' => ['style' => 'display:none;'],
  1735. ['input' => [
  1736. 'type' => 'hidden',
  1737. 'name' => '_Token[fields]',
  1738. 'value' => $hash,
  1739. 'autocomplete' => 'off',
  1740. 'data-foo' => 'bar',
  1741. ]],
  1742. ['input' => [
  1743. 'type' => 'hidden',
  1744. 'name' => '_Token[unlocked]',
  1745. 'value' => 'address%7Cfirst_name',
  1746. 'autocomplete' => 'off',
  1747. 'data-foo' => 'bar',
  1748. ]],
  1749. ['input' => [
  1750. 'type' => 'hidden', 'name' => '_Token[debug]',
  1751. 'value' => $tokenDebug,
  1752. 'autocomplete' => 'off',
  1753. 'data-foo' => 'bar'
  1754. ]],
  1755. '/div'
  1756. ];
  1757. $this->assertHtml($expected, $result);
  1758. }
  1759. /**
  1760. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1761. *
  1762. * Debug is false, debugSecurity is true -> no debug
  1763. *
  1764. * @return void
  1765. */
  1766. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1767. {
  1768. $this->Form->request->params['_Token'] = [
  1769. 'unlockedFields' => ['first_name', 'address']
  1770. ];
  1771. $this->Form->create();
  1772. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1773. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1774. $this->Form->text('Addresses.title');
  1775. $this->Form->text('Addresses.first_name');
  1776. $this->Form->text('Addresses.last_name');
  1777. $this->Form->text('Addresses.address');
  1778. $this->Form->text('Addresses.city');
  1779. $this->Form->text('Addresses.phone');
  1780. $result = $this->Form->fields;
  1781. $expected = [
  1782. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1783. 'Addresses.city', 'Addresses.phone'
  1784. ];
  1785. $this->assertEquals($expected, $result);
  1786. Configure::write('debug', false);
  1787. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1788. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1789. $expected = [
  1790. 'div' => ['style' => 'display:none;'],
  1791. ['input' => [
  1792. 'type' => 'hidden',
  1793. 'name' => '_Token[fields]',
  1794. 'value' => $hash,
  1795. 'autocomplete' => 'off',
  1796. 'data-foo' => 'bar',
  1797. ]],
  1798. ['input' => [
  1799. 'type' => 'hidden',
  1800. 'name' => '_Token[unlocked]',
  1801. 'value' => 'address%7Cfirst_name',
  1802. 'autocomplete' => 'off',
  1803. 'data-foo' => 'bar',
  1804. ]],
  1805. '/div'
  1806. ];
  1807. $this->assertHtml($expected, $result);
  1808. }
  1809. /**
  1810. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1811. *
  1812. * Test single record form with debugSecurity param.
  1813. *
  1814. * @return void
  1815. */
  1816. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1817. {
  1818. $this->Form->request->params['_Token'] = [
  1819. 'unlockedFields' => ['first_name', 'address']
  1820. ];
  1821. $this->Form->create();
  1822. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1823. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1824. $this->Form->text('Addresses.title');
  1825. $this->Form->text('Addresses.first_name');
  1826. $this->Form->text('Addresses.last_name');
  1827. $this->Form->text('Addresses.address');
  1828. $this->Form->text('Addresses.city');
  1829. $this->Form->text('Addresses.phone');
  1830. $result = $this->Form->fields;
  1831. $expected = [
  1832. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1833. 'Addresses.city', 'Addresses.phone'
  1834. ];
  1835. $this->assertEquals($expected, $result);
  1836. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1837. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1838. $expected = [
  1839. 'div' => ['style' => 'display:none;'],
  1840. ['input' => [
  1841. 'type' => 'hidden',
  1842. 'name' => '_Token[fields]',
  1843. 'value' => $hash,
  1844. 'autocomplete' => 'off',
  1845. 'data-foo' => 'bar',
  1846. ]],
  1847. ['input' => [
  1848. 'type' => 'hidden',
  1849. 'name' => '_Token[unlocked]',
  1850. 'value' => 'address%7Cfirst_name',
  1851. 'autocomplete' => 'off',
  1852. 'data-foo' => 'bar',
  1853. ]],
  1854. '/div'
  1855. ];
  1856. $this->assertHtml($expected, $result);
  1857. }
  1858. /**
  1859. * testFormSecureWithCustomNameAttribute method
  1860. *
  1861. * Test securing inputs with custom name attributes.
  1862. *
  1863. * @return void
  1864. */
  1865. public function testFormSecureWithCustomNameAttribute()
  1866. {
  1867. $this->Form->request->params['_Token'] = 'testKey';
  1868. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1869. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1870. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1871. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1872. }
  1873. /**
  1874. * testFormSecuredControl method
  1875. *
  1876. * Test generation of entire secure form, assertions made on control() output.
  1877. *
  1878. * @return void
  1879. */
  1880. public function testFormSecuredControl()
  1881. {
  1882. $this->Form->request->params['_csrfToken'] = 'testKey';
  1883. $this->Form->request->params['_Token'] = 'stuff';
  1884. $this->article['schema'] = [
  1885. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1886. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1887. ];
  1888. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1889. $encoding = strtolower(Configure::read('App.encoding'));
  1890. $expected = [
  1891. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1892. 'div' => ['style' => 'display:none;'],
  1893. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1894. ['input' => [
  1895. 'type' => 'hidden',
  1896. 'name' => '_csrfToken',
  1897. 'value' => 'testKey',
  1898. 'autocomplete' => 'off'
  1899. ]],
  1900. '/div'
  1901. ];
  1902. $this->assertHtml($expected, $result);
  1903. $result = $this->Form->control('ratio');
  1904. $expected = [
  1905. 'div' => ['class'],
  1906. 'label' => ['for'],
  1907. 'Ratio',
  1908. '/label',
  1909. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1910. '/div',
  1911. ];
  1912. $this->assertHtml($expected, $result);
  1913. $result = $this->Form->control('population');
  1914. $expected = [
  1915. 'div' => ['class'],
  1916. 'label' => ['for'],
  1917. 'Population',
  1918. '/label',
  1919. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1920. '/div',
  1921. ];
  1922. $this->assertHtml($expected, $result);
  1923. $result = $this->Form->control('published', ['type' => 'text']);
  1924. $expected = [
  1925. 'div' => ['class' => 'input text'],
  1926. 'label' => ['for' => 'published'],
  1927. 'Published',
  1928. '/label',
  1929. ['input' => [
  1930. 'type' => 'text',
  1931. 'name' => 'published',
  1932. 'id' => 'published'
  1933. ]],
  1934. '/div'
  1935. ];
  1936. $this->assertHtml($expected, $result);
  1937. $result = $this->Form->control('other', ['type' => 'text']);
  1938. $expected = [
  1939. 'div' => ['class' => 'input text'],
  1940. 'label' => ['for' => 'other'],
  1941. 'Other',
  1942. '/label',
  1943. ['input' => [
  1944. 'type' => 'text',
  1945. 'name' => 'other',
  1946. 'id',
  1947. ]],
  1948. '/div'
  1949. ];
  1950. $this->assertHtml($expected, $result);
  1951. $result = $this->Form->hidden('stuff');
  1952. $expected = [
  1953. 'input' => [
  1954. 'type' => 'hidden',
  1955. 'name' => 'stuff'
  1956. ]
  1957. ];
  1958. $this->assertHtml($expected, $result);
  1959. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1960. $expected = ['input' => [
  1961. 'type' => 'hidden',
  1962. 'name' => 'hidden',
  1963. 'value' => '0'
  1964. ]];
  1965. $this->assertHtml($expected, $result);
  1966. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1967. $expected = [
  1968. 'div' => ['class' => 'input checkbox'],
  1969. ['input' => [
  1970. 'type' => 'hidden',
  1971. 'name' => 'something',
  1972. 'value' => '0'
  1973. ]],
  1974. 'label' => ['for' => 'something'],
  1975. ['input' => [
  1976. 'type' => 'checkbox',
  1977. 'name' => 'something',
  1978. 'value' => '1',
  1979. 'id' => 'something'
  1980. ]],
  1981. 'Something',
  1982. '/label',
  1983. '/div'
  1984. ];
  1985. $this->assertHtml($expected, $result);
  1986. $result = $this->Form->fields;
  1987. $expectedFields = [
  1988. 'ratio',
  1989. 'population',
  1990. 'published',
  1991. 'other',
  1992. 'stuff' => '',
  1993. 'hidden' => '0',
  1994. 'something'
  1995. ];
  1996. $this->assertEquals($expectedFields, $result);
  1997. $result = $this->Form->secure($this->Form->fields);
  1998. $tokenDebug = urlencode(json_encode([
  1999. '/articles/add',
  2000. $expectedFields,
  2001. []
  2002. ]));
  2003. $expected = [
  2004. 'div' => ['style' => 'display:none;'],
  2005. ['input' => [
  2006. 'type' => 'hidden',
  2007. 'name' => '_Token[fields]',
  2008. 'value',
  2009. 'autocomplete'
  2010. ]],
  2011. ['input' => [
  2012. 'type' => 'hidden',
  2013. 'name' => '_Token[unlocked]',
  2014. 'value' => '',
  2015. 'autocomplete' => 'off'
  2016. ]],
  2017. ['input' => [
  2018. 'type' => 'hidden', 'name' => '_Token[debug]',
  2019. 'value' => $tokenDebug,
  2020. 'autocomplete' => 'off'
  2021. ]],
  2022. '/div'
  2023. ];
  2024. $this->assertHtml($expected, $result);
  2025. }
  2026. /**
  2027. * testSecuredControlCustomName method
  2028. *
  2029. * Test secured inputs with custom names.
  2030. *
  2031. * @return void
  2032. */
  2033. public function testSecuredControlCustomName()
  2034. {
  2035. $this->Form->request->params['_Token'] = 'testKey';
  2036. $this->assertEquals([], $this->Form->fields);
  2037. $this->Form->text('text_input', [
  2038. 'name' => 'Option[General.default_role]',
  2039. ]);
  2040. $expected = ['Option.General.default_role'];
  2041. $this->assertEquals($expected, $this->Form->fields);
  2042. $this->Form->select('select_box', [1, 2], [
  2043. 'name' => 'Option[General.select_role]',
  2044. ]);
  2045. $expected[] = 'Option.General.select_role';
  2046. $this->assertEquals($expected, $this->Form->fields);
  2047. $this->Form->text('other.things[]');
  2048. $expected[] = 'other.things';
  2049. $this->assertEquals($expected, $this->Form->fields);
  2050. }
  2051. /**
  2052. * testSecuredControlDuplicate method
  2053. *
  2054. * Test that a hidden field followed by a visible field
  2055. * undoes the hidden field locking.
  2056. *
  2057. * @return void
  2058. */
  2059. public function testSecuredControlDuplicate()
  2060. {
  2061. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2062. $this->assertEquals([], $this->Form->fields);
  2063. $this->Form->control('text_val', [
  2064. 'type' => 'hidden',
  2065. 'value' => 'some text',
  2066. ]);
  2067. $expected = ['text_val' => 'some text'];
  2068. $this->assertEquals($expected, $this->Form->fields);
  2069. $this->Form->control('text_val', [
  2070. 'type' => 'text',
  2071. ]);
  2072. $expected = ['text_val'];
  2073. $this->assertEquals($expected, $this->Form->fields);
  2074. }
  2075. /**
  2076. * testFormSecuredFileControl method
  2077. *
  2078. * Tests that the correct keys are added to the field hash index.
  2079. *
  2080. * @return void
  2081. */
  2082. public function testFormSecuredFileControl()
  2083. {
  2084. $this->assertEquals([], $this->Form->fields);
  2085. $this->Form->file('Attachment.file');
  2086. $expected = [
  2087. 'Attachment.file.name', 'Attachment.file.type',
  2088. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2089. 'Attachment.file.size'
  2090. ];
  2091. $this->assertEquals($expected, $this->Form->fields);
  2092. }
  2093. /**
  2094. * testFormSecuredMultipleSelect method
  2095. *
  2096. * Test that multiple selects keys are added to field hash.
  2097. *
  2098. * @return void
  2099. */
  2100. public function testFormSecuredMultipleSelect()
  2101. {
  2102. $this->Form->request->params['_csrfToken'] = 'testKey';
  2103. $this->assertEquals([], $this->Form->fields);
  2104. $options = ['1' => 'one', '2' => 'two'];
  2105. $this->Form->select('Model.select', $options);
  2106. $expected = ['Model.select'];
  2107. $this->assertEquals($expected, $this->Form->fields);
  2108. $this->Form->fields = [];
  2109. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2110. $this->assertEquals($expected, $this->Form->fields);
  2111. }
  2112. /**
  2113. * testFormSecuredRadio method
  2114. *
  2115. * @return void
  2116. */
  2117. public function testFormSecuredRadio()
  2118. {
  2119. $this->Form->request->params['_Token'] = 'testKey';
  2120. $this->assertEquals([], $this->Form->fields);
  2121. $options = ['1' => 'option1', '2' => 'option2'];
  2122. $this->Form->radio('Test.test', $options);
  2123. $expected = ['Test.test'];
  2124. $this->assertEquals($expected, $this->Form->fields);
  2125. $this->Form->radio('Test.all', $options, [
  2126. 'disabled' => ['option1', 'option2']
  2127. ]);
  2128. $expected = ['Test.test', 'Test.all' => ''];
  2129. $this->assertEquals($expected, $this->Form->fields);
  2130. $this->Form->radio('Test.some', $options, [
  2131. 'disabled' => ['option1']
  2132. ]);
  2133. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2134. $this->assertEquals($expected, $this->Form->fields);
  2135. }
  2136. /**
  2137. * testFormSecuredAndDisabledNotAssoc method
  2138. *
  2139. * Test that when disabled is in a list based attribute array it works.
  2140. *
  2141. * @return void
  2142. */
  2143. public function testFormSecuredAndDisabledNotAssoc()
  2144. {
  2145. $this->Form->request->params['_Token'] = 'testKey';
  2146. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2147. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2148. $this->Form->text('Model.text', ['disabled']);
  2149. $this->Form->textarea('Model.textarea', ['disabled']);
  2150. $this->Form->password('Model.password', ['disabled']);
  2151. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2152. $expected = [
  2153. 'Model.radio' => ''
  2154. ];
  2155. $this->assertEquals($expected, $this->Form->fields);
  2156. }
  2157. /**
  2158. * testFormSecuredAndDisabled method
  2159. *
  2160. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2161. * hashing.
  2162. *
  2163. * @return void
  2164. */
  2165. public function testFormSecuredAndDisabled()
  2166. {
  2167. $this->Form->request->params['_Token'] = 'testKey';
  2168. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2169. $this->Form->text('Model.text', ['disabled' => true]);
  2170. $this->Form->password('Model.text', ['disabled' => true]);
  2171. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2172. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2173. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2174. $this->Form->year('Model.year', ['disabled' => true]);
  2175. $this->Form->month('Model.month', ['disabled' => true]);
  2176. $this->Form->day('Model.day', ['disabled' => true]);
  2177. $this->Form->hour('Model.hour', ['disabled' => true]);
  2178. $this->Form->minute('Model.minute', ['disabled' => true]);
  2179. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2180. $expected = [
  2181. 'Model.radio' => ''
  2182. ];
  2183. $this->assertEquals($expected, $this->Form->fields);
  2184. }
  2185. /**
  2186. * testDisableSecurityUsingForm method
  2187. *
  2188. * @return void
  2189. */
  2190. public function testDisableSecurityUsingForm()
  2191. {
  2192. $this->Form->request->params['_Token'] = [
  2193. 'disabledFields' => []
  2194. ];
  2195. $this->Form->create();
  2196. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2197. $this->Form->text('Addresses.title');
  2198. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2199. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2200. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2201. $result = $this->Form->fields;
  2202. $expected = [
  2203. 'Addresses.id' => '123456', 'Addresses.title',
  2204. ];
  2205. $this->assertEquals($expected, $result);
  2206. }
  2207. /**
  2208. * testUnlockFieldAddsToList method
  2209. *
  2210. * Test disableField.
  2211. *
  2212. * @return void
  2213. */
  2214. public function testUnlockFieldAddsToList()
  2215. {
  2216. $this->Form->request->params['_Token'] = [
  2217. 'unlockedFields' => []
  2218. ];
  2219. $this->Form->unlockField('Contact.name');
  2220. $this->Form->text('Contact.name');
  2221. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2222. $this->assertEquals([], $this->Form->fields);
  2223. }
  2224. /**
  2225. * testUnlockFieldRemovingFromFields method
  2226. *
  2227. * Test unlockField removing from fields array.
  2228. *
  2229. * @return void
  2230. */
  2231. public function testUnlockFieldRemovingFromFields()
  2232. {
  2233. $this->Form->request->params['_Token'] = [
  2234. 'unlockedFields' => []
  2235. ];
  2236. $this->Form->create($this->article);
  2237. $this->Form->hidden('Article.id', ['value' => 1]);
  2238. $this->Form->text('Article.title');
  2239. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2240. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2241. $this->Form->unlockField('Article.title');
  2242. $this->Form->unlockField('Article.id');
  2243. $this->assertEquals([], $this->Form->fields);
  2244. }
  2245. /**
  2246. * testResetUnlockFields method
  2247. *
  2248. * Test reset unlockFields, when create new form.
  2249. *
  2250. * @return void
  2251. */
  2252. public function testResetUnlockFields()
  2253. {
  2254. $this->Form->request->params['_Token'] = [
  2255. 'key' => 'testKey',
  2256. 'unlockedFields' => []
  2257. ];
  2258. $this->Form->unlockField('Contact.id');
  2259. $this->Form->create('Contact');
  2260. $this->Form->hidden('Contact.id', ['value' => 1]);
  2261. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2262. $this->Form->end();
  2263. $this->Form->create('Contact');
  2264. $this->Form->hidden('Contact.id', ['value' => 1]);
  2265. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2266. }
  2267. /**
  2268. * testSecuredFormUrlIgnoresHost method
  2269. *
  2270. * Test that only the path + query elements of a form's URL show up in their hash.
  2271. *
  2272. * @return void
  2273. */
  2274. public function testSecuredFormUrlIgnoresHost()
  2275. {
  2276. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2277. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2278. $this->Form->create($this->article, [
  2279. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2280. ]);
  2281. $result = $this->Form->secure();
  2282. $this->assertContains($expected, $result);
  2283. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2284. $result = $this->Form->secure();
  2285. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2286. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2287. $result = $this->Form->secure();
  2288. $this->assertContains($expected, $result, 'URL path + query should work.');
  2289. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2290. $result = $this->Form->secure();
  2291. $this->assertNotContains($expected, $result, 'URL is different');
  2292. }
  2293. /**
  2294. * testSecuredFormUrlHasHtmlAndIdentifier method
  2295. *
  2296. * Test that URL, HTML and identifier show up in their hashes.
  2297. *
  2298. * @return void
  2299. */
  2300. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2301. {
  2302. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2303. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2304. $this->Form->create($this->article, [
  2305. 'url' => [
  2306. 'controller' => 'articles',
  2307. 'action' => 'view',
  2308. '?' => [
  2309. 'page' => 1,
  2310. 'limit' => 10,
  2311. 'html' => '<>"',
  2312. ],
  2313. '#' => 'result',
  2314. ],
  2315. ]);
  2316. $result = $this->Form->secure();
  2317. $this->assertContains($expected, $result);
  2318. $this->Form->create($this->article, [
  2319. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2320. ]);
  2321. $result = $this->Form->secure();
  2322. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2323. $this->Form->create($this->article, [
  2324. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2325. ]);
  2326. $result = $this->Form->secure();
  2327. $this->assertContains($expected, $result, 'URL path + query should work.');
  2328. }
  2329. /**
  2330. * testErrorMessageDisplay method
  2331. *
  2332. * Test error message display.
  2333. *
  2334. * @return void
  2335. */
  2336. public function testErrorMessageDisplay()
  2337. {
  2338. $this->article['errors'] = [
  2339. 'Article' => [
  2340. 'title' => 'error message',
  2341. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2342. ]
  2343. ];
  2344. $this->Form->create($this->article);
  2345. $result = $this->Form->control('Article.title');
  2346. $expected = [
  2347. 'div' => ['class' => 'input text error'],
  2348. 'label' => ['for' => 'article-title'],
  2349. 'Title',
  2350. '/label',
  2351. 'input' => [
  2352. 'type' => 'text', 'name' => 'Article[title]',
  2353. 'id' => 'article-title', 'class' => 'form-error'
  2354. ],
  2355. ['div' => ['class' => 'error-message']],
  2356. 'error message',
  2357. '/div',
  2358. '/div'
  2359. ];
  2360. $this->assertHtml($expected, $result);
  2361. $result = $this->Form->control('Article.title', [
  2362. 'templates' => [
  2363. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2364. ]
  2365. ]);
  2366. $expected = [
  2367. 'div' => ['class' => 'input text error'],
  2368. 'label' => ['for' => 'article-title'],
  2369. 'Title',
  2370. '/label',
  2371. 'input' => [
  2372. 'type' => 'text', 'name' => 'Article[title]',
  2373. 'id' => 'article-title', 'class' => 'form-error'
  2374. ],
  2375. '/div'
  2376. ];
  2377. $this->assertHtml($expected, $result);
  2378. $result = $this->Form->control('Article.content');
  2379. $expected = [
  2380. 'div' => ['class' => 'input text error'],
  2381. 'label' => ['for' => 'article-content'],
  2382. 'Content',
  2383. '/label',
  2384. 'input' => [
  2385. 'type' => 'text', 'name' => 'Article[content]',
  2386. 'id' => 'article-content', 'class' => 'form-error'
  2387. ],
  2388. ['div' => ['class' => 'error-message']],
  2389. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2390. '/div',
  2391. '/div'
  2392. ];
  2393. $this->assertHtml($expected, $result);
  2394. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2395. $expected = [
  2396. 'div' => ['class' => 'input text error'],
  2397. 'label' => ['for' => 'article-content'],
  2398. 'Content',
  2399. '/label',
  2400. 'input' => [
  2401. 'type' => 'text', 'name' => 'Article[content]',
  2402. 'id' => 'article-content', 'class' => 'form-error'
  2403. ],
  2404. ['div' => ['class' => 'error-message']],
  2405. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2406. '/div',
  2407. '/div'
  2408. ];
  2409. $this->assertHtml($expected, $result);
  2410. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2411. $expected = [
  2412. 'div' => ['class' => 'input text error'],
  2413. 'label' => ['for' => 'article-content'],
  2414. 'Content',
  2415. '/label',
  2416. 'input' => [
  2417. 'type' => 'text', 'name' => 'Article[content]',
  2418. 'id' => 'article-content', 'class' => 'form-error'
  2419. ],
  2420. ['div' => ['class' => 'error-message']],
  2421. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2422. '/div',
  2423. '/div'
  2424. ];
  2425. $this->assertHtml($expected, $result);
  2426. }
  2427. /**
  2428. * testEmptyErrorValidation method
  2429. *
  2430. * Test validation errors, when validation message is an empty string.
  2431. *
  2432. * @return void
  2433. */
  2434. public function testEmptyErrorValidation()
  2435. {
  2436. $this->article['errors'] = [
  2437. 'Article' => ['title' => '']
  2438. ];
  2439. $this->Form->create($this->article);
  2440. $result = $this->Form->control('Article.title');
  2441. $expected = [
  2442. 'div' => ['class' => 'input text error'],
  2443. 'label' => ['for' => 'article-title'],
  2444. 'Title',
  2445. '/label',
  2446. 'input' => [
  2447. 'type' => 'text', 'name' => 'Article[title]',
  2448. 'id' => 'article-title', 'class' => 'form-error'
  2449. ],
  2450. ['div' => ['class' => 'error-message']],
  2451. [],
  2452. '/div',
  2453. '/div'
  2454. ];
  2455. $this->assertHtml($expected, $result);
  2456. }
  2457. /**
  2458. * testEmptyControlErrorValidation method
  2459. *
  2460. * Test validation errors, when calling control() overriding validation message by an empty string.
  2461. *
  2462. * @return void
  2463. */
  2464. public function testEmptyControlErrorValidation()
  2465. {
  2466. $this->article['errors'] = [
  2467. 'Article' => ['title' => 'error message']
  2468. ];
  2469. $this->Form->create($this->article);
  2470. $result = $this->Form->control('Article.title', ['error' => '']);
  2471. $expected = [
  2472. 'div' => ['class' => 'input text error'],
  2473. 'label' => ['for' => 'article-title'],
  2474. 'Title',
  2475. '/label',
  2476. 'input' => [
  2477. 'type' => 'text', 'name' => 'Article[title]',
  2478. 'id' => 'article-title', 'class' => 'form-error'
  2479. ],
  2480. ['div' => ['class' => 'error-message']],
  2481. [],
  2482. '/div',
  2483. '/div'
  2484. ];
  2485. $this->assertHtml($expected, $result);
  2486. }
  2487. /**
  2488. * testControlErrorMessage method
  2489. *
  2490. * Test validation errors, when calling control() overriding validation messages.
  2491. *
  2492. * @return void
  2493. */
  2494. public function testControlErrorMessage()
  2495. {
  2496. $this->article['errors'] = [
  2497. 'title' => ['error message']
  2498. ];
  2499. $this->Form->create($this->article);
  2500. $result = $this->Form->control('title', [
  2501. 'error' => 'Custom error!'
  2502. ]);
  2503. $expected = [
  2504. 'div' => ['class' => 'input text required error'],
  2505. 'label' => ['for' => 'title'],
  2506. 'Title',
  2507. '/label',
  2508. 'input' => [
  2509. 'type' => 'text', 'name' => 'title',
  2510. 'id' => 'title', 'class' => 'form-error',
  2511. 'required' => 'required',
  2512. ],
  2513. ['div' => ['class' => 'error-message']],
  2514. 'Custom error!',
  2515. '/div',
  2516. '/div'
  2517. ];
  2518. $this->assertHtml($expected, $result);
  2519. $result = $this->Form->control('title', [
  2520. 'error' => ['error message' => 'Custom error!']
  2521. ]);
  2522. $expected = [
  2523. 'div' => ['class' => 'input text required error'],
  2524. 'label' => ['for' => 'title'],
  2525. 'Title',
  2526. '/label',
  2527. 'input' => [
  2528. 'type' => 'text',
  2529. 'name' => 'title',
  2530. 'id' => 'title',
  2531. 'class' => 'form-error',
  2532. 'required' => 'required'
  2533. ],
  2534. ['div' => ['class' => 'error-message']],
  2535. 'Custom error!',
  2536. '/div',
  2537. '/div'
  2538. ];
  2539. $this->assertHtml($expected, $result);
  2540. }
  2541. /**
  2542. * testFormValidationAssociated method
  2543. *
  2544. * Tests displaying errors for nested entities.
  2545. *
  2546. * @return void
  2547. */
  2548. public function testFormValidationAssociated()
  2549. {
  2550. $nested = new Entity(['foo' => 'bar']);
  2551. $nested->setError('foo', ['not a valid bar']);
  2552. $entity = new Entity(['nested' => $nested]);
  2553. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2554. $result = $this->Form->error('nested.foo');
  2555. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2556. }
  2557. /**
  2558. * testFormValidationAssociatedSecondLevel method
  2559. *
  2560. * Test form error display with associated model.
  2561. *
  2562. * @return void
  2563. */
  2564. public function testFormValidationAssociatedSecondLevel()
  2565. {
  2566. $inner = new Entity(['bar' => 'baz']);
  2567. $nested = new Entity(['foo' => $inner]);
  2568. $entity = new Entity(['nested' => $nested]);
  2569. $inner->setError('bar', ['not a valid one']);
  2570. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2571. $result = $this->Form->error('nested.foo.bar');
  2572. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2573. }
  2574. /**
  2575. * testFormValidationMultiRecord method
  2576. *
  2577. * Test form error display with multiple records.
  2578. *
  2579. * @return void
  2580. */
  2581. public function testFormValidationMultiRecord()
  2582. {
  2583. $one = new Entity();
  2584. $two = new Entity();
  2585. TableRegistry::get('Contacts', [
  2586. 'className' => __NAMESPACE__ . '\ContactsTable'
  2587. ]);
  2588. $one->set('email', '');
  2589. $one->setError('email', ['invalid email']);
  2590. $two->set('name', '');
  2591. $two->setError('name', ['This is wrong']);
  2592. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2593. $result = $this->Form->control('0.email');
  2594. $expected = [
  2595. 'div' => ['class' => 'input email error'],
  2596. 'label' => ['for' => '0-email'],
  2597. 'Email',
  2598. '/label',
  2599. 'input' => [
  2600. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2601. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2602. ],
  2603. ['div' => ['class' => 'error-message']],
  2604. 'invalid email',
  2605. '/div',
  2606. '/div'
  2607. ];
  2608. $this->assertHtml($expected, $result);
  2609. $result = $this->Form->control('1.name');
  2610. $expected = [
  2611. 'div' => ['class' => 'input text error'],
  2612. 'label' => ['for' => '1-name'],
  2613. 'Name',
  2614. '/label',
  2615. 'input' => [
  2616. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2617. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2618. ],
  2619. ['div' => ['class' => 'error-message']],
  2620. 'This is wrong',
  2621. '/div',
  2622. '/div'
  2623. ];
  2624. $this->assertHtml($expected, $result);
  2625. }
  2626. /**
  2627. * testControl method
  2628. *
  2629. * Test various incarnations of control().
  2630. *
  2631. * @return void
  2632. */
  2633. public function testControl()
  2634. {
  2635. TableRegistry::get('ValidateUsers', [
  2636. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2637. ]);
  2638. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2639. $result = $this->Form->control('ValidateUsers.balance');
  2640. $expected = [
  2641. 'div' => ['class'],
  2642. 'label' => ['for'],
  2643. 'Balance',
  2644. '/label',
  2645. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2646. '/div',
  2647. ];
  2648. $this->assertHtml($expected, $result);
  2649. $result = $this->Form->control('ValidateUser.cost_decimal');
  2650. $expected = [
  2651. 'div' => ['class'],
  2652. 'label' => ['for'],
  2653. 'Cost Decimal',
  2654. '/label',
  2655. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2656. '/div',
  2657. ];
  2658. $this->assertHtml($expected, $result);
  2659. $result = $this->Form->control('ValidateUser.null_decimal');
  2660. $expected = [
  2661. 'div' => ['class'],
  2662. 'label' => ['for'],
  2663. 'Null Decimal',
  2664. '/label',
  2665. 'input' => ['name', 'type' => 'number', 'id'],
  2666. '/div',
  2667. ];
  2668. $this->assertHtml($expected, $result);
  2669. }
  2670. /**
  2671. * testControlCustomization method
  2672. *
  2673. * Tests the input method and passing custom options.
  2674. *
  2675. * @return void
  2676. */
  2677. public function testControlCustomization()
  2678. {
  2679. TableRegistry::get('Contacts', [
  2680. 'className' => __NAMESPACE__ . '\ContactsTable'
  2681. ]);
  2682. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2683. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2684. $expected = [
  2685. 'div' => ['class' => 'input email'],
  2686. 'label' => ['for' => 'custom'],
  2687. 'Email',
  2688. '/label',
  2689. ['input' => [
  2690. 'type' => 'email', 'name' => 'Contact[email]',
  2691. 'id' => 'custom', 'maxlength' => 255
  2692. ]],
  2693. '/div'
  2694. ];
  2695. $this->assertHtml($expected, $result);
  2696. $result = $this->Form->control('Contact.email', [
  2697. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2698. ]);
  2699. $expected = [
  2700. '<div',
  2701. 'label' => ['for' => 'contact-email'],
  2702. 'Email',
  2703. '/label',
  2704. ['input' => [
  2705. 'type' => 'email', 'name' => 'Contact[email]',
  2706. 'id' => 'contact-email', 'maxlength' => 255
  2707. ]],
  2708. '/div'
  2709. ];
  2710. $this->assertHtml($expected, $result);
  2711. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2712. $expected = [
  2713. 'div' => ['class' => 'input text'],
  2714. 'label' => ['for' => 'contact-email'],
  2715. 'Email',
  2716. '/label',
  2717. ['input' => [
  2718. 'type' => 'text', 'name' => 'Contact[email]',
  2719. 'id' => 'contact-email', 'maxlength' => '255'
  2720. ]],
  2721. '/div'
  2722. ];
  2723. $this->assertHtml($expected, $result);
  2724. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2725. $expected = [
  2726. 'div' => ['class' => 'input text'],
  2727. 'label' => ['for' => 'contact-5-email'],
  2728. 'Email',
  2729. '/label',
  2730. ['input' => [
  2731. 'type' => 'text', 'name' => 'Contact[5][email]',
  2732. 'id' => 'contact-5-email', 'maxlength' => '255'
  2733. ]],
  2734. '/div'
  2735. ];
  2736. $this->assertHtml($expected, $result);
  2737. $result = $this->Form->control('Contact.password');
  2738. $expected = [
  2739. 'div' => ['class' => 'input password'],
  2740. 'label' => ['for' => 'contact-password'],
  2741. 'Password',
  2742. '/label',
  2743. ['input' => [
  2744. 'type' => 'password', 'name' => 'Contact[password]',
  2745. 'id' => 'contact-password'
  2746. ]],
  2747. '/div'
  2748. ];
  2749. $this->assertHtml($expected, $result);
  2750. $result = $this->Form->control('Contact.email', [
  2751. 'type' => 'file', 'class' => 'textbox'
  2752. ]);
  2753. $expected = [
  2754. 'div' => ['class' => 'input file'],
  2755. 'label' => ['for' => 'contact-email'],
  2756. 'Email',
  2757. '/label',
  2758. ['input' => [
  2759. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2760. 'id' => 'contact-email'
  2761. ]],
  2762. '/div'
  2763. ];
  2764. $this->assertHtml($expected, $result);
  2765. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2766. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2767. $result = $this->Form->control('phone');
  2768. $expected = [
  2769. 'div' => ['class' => 'input tel'],
  2770. 'label' => ['for' => 'phone'],
  2771. 'Phone',
  2772. '/label',
  2773. ['input' => [
  2774. 'type' => 'tel', 'name' => 'phone',
  2775. 'value' => 'Hello &amp; World &gt; weird chars',
  2776. 'id' => 'phone', 'maxlength' => 255
  2777. ]],
  2778. '/div'
  2779. ];
  2780. $this->assertHtml($expected, $result);
  2781. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2782. $this->Form->create();
  2783. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2784. $expected = [
  2785. 'div' => ['class' => 'input text'],
  2786. 'label' => ['for' => 'myId'],
  2787. 'Field',
  2788. '/label',
  2789. 'input' => [
  2790. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2791. 'value' => 'My value', 'id' => 'myId'
  2792. ],
  2793. '/div'
  2794. ];
  2795. $this->assertHtml($expected, $result);
  2796. $this->Form->request->data = [];
  2797. $entity->setError('field', 'Badness!');
  2798. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2799. $result = $this->Form->control('field');
  2800. $expected = [
  2801. 'div' => ['class' => 'input text error'],
  2802. 'label' => ['for' => 'field'],
  2803. 'Field',
  2804. '/label',
  2805. 'input' => [
  2806. 'type' => 'text', 'name' => 'field',
  2807. 'id' => 'field', 'class' => 'form-error'
  2808. ],
  2809. ['div' => ['class' => 'error-message']],
  2810. 'Badness!',
  2811. '/div',
  2812. '/div'
  2813. ];
  2814. $this->assertHtml($expected, $result);
  2815. $result = $this->Form->control('field', [
  2816. 'templates' => [
  2817. 'inputContainerError' => '{{content}}{{error}}',
  2818. 'error' => '<span class="error-message">{{content}}</span>'
  2819. ]
  2820. ]);
  2821. $expected = [
  2822. 'label' => ['for' => 'field'],
  2823. 'Field',
  2824. '/label',
  2825. 'input' => [
  2826. 'type' => 'text', 'name' => 'field',
  2827. 'id' => 'field', 'class' => 'form-error'
  2828. ],
  2829. ['span' => ['class' => 'error-message']],
  2830. 'Badness!',
  2831. '/span'
  2832. ];
  2833. $this->assertHtml($expected, $result);
  2834. $entity->setError('field', ['minLength'], true);
  2835. $result = $this->Form->control('field', [
  2836. 'error' => [
  2837. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2838. 'maxLength' => 'login too large'
  2839. ]
  2840. ]);
  2841. $expected = [
  2842. 'div' => ['class' => 'input text error'],
  2843. 'label' => ['for' => 'field'],
  2844. 'Field',
  2845. '/label',
  2846. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2847. ['div' => ['class' => 'error-message']],
  2848. 'Le login doit contenir au moins 2 caractères',
  2849. '/div',
  2850. '/div'
  2851. ];
  2852. $this->assertHtml($expected, $result);
  2853. $entity->setError('field', ['maxLength'], true);
  2854. $result = $this->Form->control('field', [
  2855. 'error' => [
  2856. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2857. 'maxLength' => 'login too large',
  2858. ]
  2859. ]);
  2860. $expected = [
  2861. 'div' => ['class' => 'input text error'],
  2862. 'label' => ['for' => 'field'],
  2863. 'Field',
  2864. '/label',
  2865. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2866. ['div' => ['class' => 'error-message']],
  2867. 'login too large',
  2868. '/div',
  2869. '/div'
  2870. ];
  2871. $this->assertHtml($expected, $result);
  2872. }
  2873. /**
  2874. * testControlWithTemplateFile method
  2875. *
  2876. * Test that control() accepts a template file.
  2877. *
  2878. * @return void
  2879. */
  2880. public function testControlWithTemplateFile()
  2881. {
  2882. $result = $this->Form->control('field', [
  2883. 'templates' => 'htmlhelper_tags'
  2884. ]);
  2885. $expected = [
  2886. 'label' => ['for' => 'field'],
  2887. 'Field',
  2888. '/label',
  2889. 'input' => [
  2890. 'type' => 'text', 'name' => 'field',
  2891. 'id' => 'field'
  2892. ],
  2893. ];
  2894. $this->assertHtml($expected, $result);
  2895. }
  2896. /**
  2897. * testNestedControlsEndWithBrackets method
  2898. *
  2899. * Test that nested inputs end with brackets.
  2900. *
  2901. * @return void
  2902. */
  2903. public function testNestedControlsEndWithBrackets()
  2904. {
  2905. $result = $this->Form->text('nested.text[]');
  2906. $expected = [
  2907. 'input' => [
  2908. 'type' => 'text', 'name' => 'nested[text][]'
  2909. ],
  2910. ];
  2911. $this->assertHtml($expected, $result);
  2912. $result = $this->Form->file('nested.file[]');
  2913. $expected = [
  2914. 'input' => [
  2915. 'type' => 'file', 'name' => 'nested[file][]'
  2916. ],
  2917. ];
  2918. $this->assertHtml($expected, $result);
  2919. }
  2920. /**
  2921. * testCreateIdPrefix method
  2922. *
  2923. * Test id prefix.
  2924. *
  2925. * @return void
  2926. */
  2927. public function testCreateIdPrefix()
  2928. {
  2929. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2930. $result = $this->Form->control('field');
  2931. $expected = [
  2932. 'div' => ['class' => 'input text'],
  2933. 'label' => ['for' => 'prefix-field'],
  2934. 'Field',
  2935. '/label',
  2936. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2937. '/div'
  2938. ];
  2939. $this->assertHtml($expected, $result);
  2940. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2941. $expected = [
  2942. 'div' => ['class' => 'input text'],
  2943. 'label' => ['for' => 'custom-id'],
  2944. 'Field',
  2945. '/label',
  2946. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2947. '/div'
  2948. ];
  2949. $this->assertHtml($expected, $result);
  2950. $result = $this->Form->radio('Model.field', ['option A']);
  2951. $expected = [
  2952. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2953. 'label' => ['for' => 'prefix-model-field-0'],
  2954. ['input' => [
  2955. 'type' => 'radio',
  2956. 'name' => 'Model[field]',
  2957. 'value' => '0',
  2958. 'id' => 'prefix-model-field-0'
  2959. ]],
  2960. 'option A',
  2961. '/label'
  2962. ];
  2963. $this->assertHtml($expected, $result);
  2964. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2965. $expected = [
  2966. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2967. 'label' => ['for' => 'prefix-model-field-0'],
  2968. ['input' => [
  2969. 'type' => 'radio',
  2970. 'name' => 'Model[field]',
  2971. 'value' => '0',
  2972. 'id' => 'prefix-model-field-0'
  2973. ]],
  2974. 'option A',
  2975. '/label'
  2976. ];
  2977. $this->assertHtml($expected, $result);
  2978. $result = $this->Form->select(
  2979. 'Model.multi_field',
  2980. ['first'],
  2981. ['multiple' => 'checkbox']
  2982. );
  2983. $expected = [
  2984. 'input' => [
  2985. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2986. ],
  2987. ['div' => ['class' => 'checkbox']],
  2988. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2989. ['input' => [
  2990. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2991. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2992. ]],
  2993. 'first',
  2994. '/label',
  2995. '/div',
  2996. ];
  2997. $this->assertHtml($expected, $result);
  2998. $this->Form->end();
  2999. $result = $this->Form->control('field');
  3000. $expected = [
  3001. 'div' => ['class' => 'input text'],
  3002. 'label' => ['for' => 'field'],
  3003. 'Field',
  3004. '/label',
  3005. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3006. '/div'
  3007. ];
  3008. $this->assertHtml($expected, $result);
  3009. }
  3010. /**
  3011. * testControlZero method
  3012. *
  3013. * Test that inputs with 0 can be created.
  3014. *
  3015. * @return void
  3016. */
  3017. public function testControlZero()
  3018. {
  3019. TableRegistry::get('Contacts', [
  3020. 'className' => __NAMESPACE__ . '\ContactsTable'
  3021. ]);
  3022. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3023. $result = $this->Form->control('0');
  3024. $expected = [
  3025. 'div' => ['class' => 'input text'],
  3026. 'label' => ['for' => '0'], '/label',
  3027. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3028. '/div'
  3029. ];
  3030. $this->assertHtml($expected, $result);
  3031. }
  3032. /**
  3033. * testControlCheckbox method
  3034. *
  3035. * Test control() with checkbox creation.
  3036. *
  3037. * @return void
  3038. */
  3039. public function testControlCheckbox()
  3040. {
  3041. $articles = TableRegistry::get('Articles');
  3042. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3043. $article = $articles->newEntity();
  3044. $this->Form->create($article);
  3045. $result = $this->Form->control('Articles.active');
  3046. $expected = [
  3047. 'div' => ['class' => 'input checkbox'],
  3048. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3049. 'label' => ['for' => 'articles-active'],
  3050. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3051. 'Active',
  3052. '/label',
  3053. '/div'
  3054. ];
  3055. $this->assertHtml($expected, $result);
  3056. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3057. $expected = [
  3058. 'div' => ['class' => 'input checkbox'],
  3059. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3060. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3061. '/div'
  3062. ];
  3063. $this->assertHtml($expected, $result);
  3064. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3065. $expected = [
  3066. 'div' => ['class' => 'input checkbox'],
  3067. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3068. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3069. '/div'
  3070. ];
  3071. $this->assertHtml($expected, $result);
  3072. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3073. $expected = [
  3074. 'div' => ['class' => 'input checkbox'],
  3075. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3076. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3077. '/div'
  3078. ];
  3079. $this->assertHtml($expected, $result);
  3080. $result = $this->Form->control('Articles.disabled', [
  3081. 'label' => 'Disabled',
  3082. 'type' => 'checkbox',
  3083. 'data-foo' => 'disabled'
  3084. ]);
  3085. $expected = [
  3086. 'div' => ['class' => 'input checkbox'],
  3087. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3088. 'label' => ['for' => 'articles-disabled'],
  3089. ['input' => [
  3090. 'type' => 'checkbox',
  3091. 'name' => 'Articles[disabled]',
  3092. 'value' => '1',
  3093. 'id' => 'articles-disabled',
  3094. 'data-foo' => 'disabled'
  3095. ]],
  3096. 'Disabled',
  3097. '/label',
  3098. '/div'
  3099. ];
  3100. $this->assertHtml($expected, $result);
  3101. $result = $this->Form->control('Articles.confirm', [
  3102. 'label' => 'Confirm <b>me</b>!',
  3103. 'type' => 'checkbox',
  3104. 'escape' => false
  3105. ]);
  3106. $expected = [
  3107. 'div' => ['class' => 'input checkbox'],
  3108. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3109. 'label' => ['for' => 'articles-confirm'],
  3110. ['input' => [
  3111. 'type' => 'checkbox',
  3112. 'name' => 'Articles[confirm]',
  3113. 'value' => '1',
  3114. 'id' => 'articles-confirm',
  3115. ]],
  3116. 'Confirm <b>me</b>!',
  3117. '/label',
  3118. '/div'
  3119. ];
  3120. $this->assertHtml($expected, $result);
  3121. }
  3122. /**
  3123. * testControlHidden method
  3124. *
  3125. * Test that control() does not create wrapping div and label tag for hidden fields.
  3126. *
  3127. * @return void
  3128. */
  3129. public function testControlHidden()
  3130. {
  3131. TableRegistry::get('ValidateUsers', [
  3132. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3133. ]);
  3134. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3135. $result = $this->Form->control('ValidateUser.id');
  3136. $expected = [
  3137. 'input' => ['name', 'type' => 'hidden', 'id']
  3138. ];
  3139. $this->assertHtml($expected, $result);
  3140. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3141. $expected = [
  3142. 'input' => ['name', 'type' => 'hidden', 'id']
  3143. ];
  3144. $this->assertHtml($expected, $result);
  3145. }
  3146. /**
  3147. * testControlDatetime method
  3148. *
  3149. * Test form->control() with datetime.
  3150. *
  3151. * @return void
  3152. */
  3153. public function testControlDatetime()
  3154. {
  3155. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3156. ->setMethods(['datetime'])
  3157. ->setConstructorArgs([new View()])
  3158. ->getMock();
  3159. $this->Form->expects($this->once())->method('datetime')
  3160. ->with('prueba', [
  3161. 'type' => 'datetime',
  3162. 'timeFormat' => 24,
  3163. 'minYear' => 2008,
  3164. 'maxYear' => 2011,
  3165. 'interval' => 15,
  3166. 'options' => null,
  3167. 'empty' => false,
  3168. 'id' => 'prueba',
  3169. 'required' => false,
  3170. 'templateVars' => []
  3171. ])
  3172. ->will($this->returnValue('This is it!'));
  3173. $result = $this->Form->control('prueba', [
  3174. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3175. 'maxYear' => 2011, 'interval' => 15
  3176. ]);
  3177. $expected = [
  3178. 'div' => ['class' => 'input datetime'],
  3179. '<label',
  3180. 'Prueba',
  3181. '/label',
  3182. 'This is it!',
  3183. '/div'
  3184. ];
  3185. $this->assertHtml($expected, $result);
  3186. }
  3187. /**
  3188. * testControlDatetimeIdPrefix method
  3189. *
  3190. * Test form->control() with datetime with id prefix.
  3191. *
  3192. * @return void
  3193. */
  3194. public function testControlDatetimeIdPrefix()
  3195. {
  3196. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3197. ->setMethods(['datetime'])
  3198. ->setConstructorArgs([new View()])
  3199. ->getMock();
  3200. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3201. $this->Form->expects($this->once())->method('datetime')
  3202. ->with('prueba', [
  3203. 'type' => 'datetime',
  3204. 'timeFormat' => 24,
  3205. 'minYear' => 2008,
  3206. 'maxYear' => 2011,
  3207. 'interval' => 15,
  3208. 'options' => null,
  3209. 'empty' => false,
  3210. 'id' => 'prefix-prueba',
  3211. 'required' => false,
  3212. 'templateVars' => []
  3213. ])
  3214. ->will($this->returnValue('This is it!'));
  3215. $result = $this->Form->control('prueba', [
  3216. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3217. 'maxYear' => 2011, 'interval' => 15
  3218. ]);
  3219. $expected = [
  3220. 'div' => ['class' => 'input datetime'],
  3221. '<label',
  3222. 'Prueba',
  3223. '/label',
  3224. 'This is it!',
  3225. '/div'
  3226. ];
  3227. $this->assertHtml($expected, $result);
  3228. }
  3229. /**
  3230. * testControlCheckboxWithDisabledElements method
  3231. *
  3232. * Test generating checkboxes with disabled elements.
  3233. *
  3234. * @return void
  3235. */
  3236. public function testControlCheckboxWithDisabledElements()
  3237. {
  3238. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3239. $result = $this->Form->control('Contact.multiple', [
  3240. 'multiple' => 'checkbox',
  3241. 'disabled' => 'disabled',
  3242. 'options' => $options
  3243. ]);
  3244. $expected = [
  3245. ['div' => ['class' => 'input select']],
  3246. ['label' => ['for' => 'contact-multiple']],
  3247. 'Multiple',
  3248. '/label',
  3249. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3250. ['div' => ['class' => 'checkbox']],
  3251. ['label' => ['for' => 'contact-multiple-1']],
  3252. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3253. 'One',
  3254. '/label',
  3255. '/div',
  3256. ['div' => ['class' => 'checkbox']],
  3257. ['label' => ['for' => 'contact-multiple-2']],
  3258. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3259. 'Two',
  3260. '/label',
  3261. '/div',
  3262. ['div' => ['class' => 'checkbox']],
  3263. ['label' => ['for' => 'contact-multiple-3']],
  3264. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3265. 'Three',
  3266. '/label',
  3267. '/div',
  3268. '/div'
  3269. ];
  3270. $this->assertHtml($expected, $result);
  3271. // make sure 50 does only disable 50, and not 50f5c0cf
  3272. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3273. $disabled = [50];
  3274. $expected = [
  3275. ['div' => ['class' => 'input select']],
  3276. ['label' => ['for' => 'contact-multiple']],
  3277. 'Multiple',
  3278. '/label',
  3279. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3280. ['div' => ['class' => 'checkbox']],
  3281. ['label' => ['for' => 'contact-multiple-50']],
  3282. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3283. 'Fifty',
  3284. '/label',
  3285. '/div',
  3286. ['div' => ['class' => 'checkbox']],
  3287. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3288. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3289. 'Stringy',
  3290. '/label',
  3291. '/div',
  3292. '/div'
  3293. ];
  3294. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3295. $this->assertHtml($expected, $result);
  3296. }
  3297. /**
  3298. * testControlWithLeadingInteger method
  3299. *
  3300. * Test input name with leading integer, ensure attributes are generated correctly.
  3301. *
  3302. * @return void
  3303. */
  3304. public function testControlWithLeadingInteger()
  3305. {
  3306. $result = $this->Form->text('0.Node.title');
  3307. $expected = [
  3308. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3309. ];
  3310. $this->assertHtml($expected, $result);
  3311. }
  3312. /**
  3313. * testControlSelectType method
  3314. *
  3315. * Test form->control() with select type inputs.
  3316. *
  3317. * @return void
  3318. */
  3319. public function testControlSelectType()
  3320. {
  3321. $result = $this->Form->control(
  3322. 'email',
  3323. [
  3324. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3325. );
  3326. $expected = [
  3327. 'div' => ['class' => 'input select'],
  3328. 'label' => ['for' => 'email'],
  3329. 'Email',
  3330. '/label',
  3331. ['select' => ['name' => 'email', 'id' => 'email']],
  3332. ['option' => ['value' => '']],
  3333. '/option',
  3334. ['option' => ['value' => 'è']],
  3335. 'Firést',
  3336. '/option',
  3337. ['option' => ['value' => 'é']],
  3338. 'Secoènd',
  3339. '/option',
  3340. '/select',
  3341. '/div'
  3342. ];
  3343. $this->assertHtml($expected, $result);
  3344. $result = $this->Form->control(
  3345. 'email',
  3346. [
  3347. 'options' => ['First', 'Second'], 'empty' => true]
  3348. );
  3349. $expected = [
  3350. 'div' => ['class' => 'input select'],
  3351. 'label' => ['for' => 'email'],
  3352. 'Email',
  3353. '/label',
  3354. ['select' => ['name' => 'email', 'id' => 'email']],
  3355. ['option' => ['value' => '']],
  3356. '/option',
  3357. ['option' => ['value' => '0']],
  3358. 'First',
  3359. '/option',
  3360. ['option' => ['value' => '1']],
  3361. 'Second',
  3362. '/option',
  3363. '/select',
  3364. '/div'
  3365. ];
  3366. $this->assertHtml($expected, $result);
  3367. $result = $this->Form->control('email', [
  3368. 'type' => 'select',
  3369. 'options' => new \ArrayObject(['First', 'Second']),
  3370. 'empty' => true
  3371. ]);
  3372. $this->assertHtml($expected, $result);
  3373. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3374. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3375. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3376. $expected = [
  3377. 'div' => ['class' => 'input select'],
  3378. 'label' => ['for' => 'model-user-id'],
  3379. 'User',
  3380. '/label',
  3381. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3382. ['option' => ['value' => '']],
  3383. '/option',
  3384. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3385. 'good',
  3386. '/option',
  3387. ['option' => ['value' => 'other']],
  3388. 'bad',
  3389. '/option',
  3390. '/select',
  3391. '/div'
  3392. ];
  3393. $this->assertHtml($expected, $result);
  3394. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3395. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3396. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3397. $expected = [
  3398. 'div' => ['class' => 'input select'],
  3399. 'label' => ['for' => 'thing-user-id'],
  3400. 'User',
  3401. '/label',
  3402. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3403. ['option' => ['value' => '']],
  3404. 'Some Empty',
  3405. '/option',
  3406. ['option' => ['value' => 'value']],
  3407. 'good',
  3408. '/option',
  3409. ['option' => ['value' => 'other']],
  3410. 'bad',
  3411. '/option',
  3412. '/select',
  3413. '/div'
  3414. ];
  3415. $this->assertHtml($expected, $result);
  3416. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3417. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3418. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3419. $expected = [
  3420. 'div' => ['class' => 'input select'],
  3421. 'label' => ['for' => 'thing-user-id'],
  3422. 'User',
  3423. '/label',
  3424. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3425. ['option' => ['value' => '']],
  3426. 'Some Empty',
  3427. '/option',
  3428. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3429. 'good',
  3430. '/option',
  3431. ['option' => ['value' => 'other']],
  3432. 'bad',
  3433. '/option',
  3434. '/select',
  3435. '/div'
  3436. ];
  3437. $this->assertHtml($expected, $result);
  3438. $this->Form->data = [];
  3439. $result = $this->Form->control('Publisher.id', [
  3440. 'label' => 'Publisher',
  3441. 'type' => 'select',
  3442. 'multiple' => 'checkbox',
  3443. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3444. ]);
  3445. $expected = [
  3446. ['div' => ['class' => 'input select']],
  3447. ['label' => ['for' => 'publisher-id']],
  3448. 'Publisher',
  3449. '/label',
  3450. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3451. ['div' => ['class' => 'checkbox']],
  3452. ['label' => ['for' => 'publisher-id-value-1']],
  3453. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3454. 'Label 1',
  3455. '/label',
  3456. '/div',
  3457. ['div' => ['class' => 'checkbox']],
  3458. ['label' => ['for' => 'publisher-id-value-2']],
  3459. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3460. 'Label 2',
  3461. '/label',
  3462. '/div',
  3463. '/div'
  3464. ];
  3465. $this->assertHtml($expected, $result);
  3466. }
  3467. /**
  3468. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3469. *
  3470. * Test that control() and a non standard primary key makes a hidden input by default.
  3471. *
  3472. * @return void
  3473. */
  3474. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3475. {
  3476. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3477. $this->Form->create($this->article);
  3478. $result = $this->Form->control('title');
  3479. $expected = [
  3480. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3481. ];
  3482. $this->assertHtml($expected, $result);
  3483. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3484. $this->Form->create($this->article);
  3485. $result = $this->Form->control('title');
  3486. $expected = [
  3487. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3488. ];
  3489. $this->assertHtml($expected, $result);
  3490. $result = $this->Form->control('body');
  3491. $expected = [
  3492. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3493. ];
  3494. $this->assertHtml($expected, $result);
  3495. }
  3496. /**
  3497. * testControlOverridingMagicSelectType method
  3498. *
  3499. * Test that overriding the magic select type widget is possible.
  3500. *
  3501. * @return void
  3502. */
  3503. public function testControlOverridingMagicSelectType()
  3504. {
  3505. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3506. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3507. $expected = [
  3508. 'div' => ['class' => 'input text'],
  3509. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3510. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3511. '/div'
  3512. ];
  3513. $this->assertHtml($expected, $result);
  3514. //Check that magic types still work for plural/singular vars
  3515. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3516. $result = $this->Form->control('Model.type');
  3517. $expected = [
  3518. 'div' => ['class' => 'input select'],
  3519. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3520. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3521. ['option' => ['value' => 'value']], 'good', '/option',
  3522. ['option' => ['value' => 'other']], 'bad', '/option',
  3523. '/select',
  3524. '/div'
  3525. ];
  3526. $this->assertHtml($expected, $result);
  3527. }
  3528. /**
  3529. * testControlMagicTypeDoesNotOverride method
  3530. *
  3531. * Test that inferred types do not override developer input.
  3532. *
  3533. * @return void
  3534. */
  3535. public function testControlMagicTypeDoesNotOverride()
  3536. {
  3537. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3538. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3539. $expected = [
  3540. 'div' => ['class' => 'input checkbox'],
  3541. ['input' => [
  3542. 'type' => 'hidden',
  3543. 'name' => 'Model[user]',
  3544. 'value' => 0,
  3545. ]],
  3546. 'label' => ['for' => 'model-user'],
  3547. ['input' => [
  3548. 'name' => 'Model[user]',
  3549. 'type' => 'checkbox',
  3550. 'id' => 'model-user',
  3551. 'value' => 1
  3552. ]],
  3553. 'User',
  3554. '/label',
  3555. '/div'
  3556. ];
  3557. $this->assertHtml($expected, $result);
  3558. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3559. $options = [
  3560. 1 => 'blue',
  3561. 2 => 'red'
  3562. ];
  3563. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3564. $expected = [
  3565. 'div' => ['class' => 'input select'],
  3566. 'label' => ['for' => 'tags-ids'],
  3567. 'Tags',
  3568. '/label',
  3569. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3570. ['div' => ['class' => 'checkbox']],
  3571. ['label' => ['for' => 'tags-ids-1']],
  3572. ['input' => [
  3573. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3574. 'value' => '1', 'name' => 'tags[_ids][]'
  3575. ]],
  3576. 'blue',
  3577. '/label',
  3578. '/div',
  3579. ['div' => ['class' => 'checkbox']],
  3580. ['label' => ['for' => 'tags-ids-2']],
  3581. ['input' => [
  3582. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3583. 'value' => '2', 'name' => 'tags[_ids][]'
  3584. ]],
  3585. 'red',
  3586. '/label',
  3587. '/div',
  3588. '/div'
  3589. ];
  3590. $this->assertHtml($expected, $result);
  3591. }
  3592. /**
  3593. * testControlMagicSelectForTypeNumber method
  3594. *
  3595. * Test that magic control() selects are created for type=number.
  3596. *
  3597. * @return void
  3598. */
  3599. public function testControlMagicSelectForTypeNumber()
  3600. {
  3601. TableRegistry::get('ValidateUsers', [
  3602. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3603. ]);
  3604. $entity = new Entity(['balance' => 1]);
  3605. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3606. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3607. $result = $this->Form->control('balance');
  3608. $expected = [
  3609. 'div' => ['class' => 'input select'],
  3610. 'label' => ['for' => 'balance'],
  3611. 'Balance',
  3612. '/label',
  3613. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3614. ['option' => ['value' => '0']],
  3615. 'nothing',
  3616. '/option',
  3617. ['option' => ['value' => '1', 'selected' => 'selected']],
  3618. 'some',
  3619. '/option',
  3620. ['option' => ['value' => '100']],
  3621. 'a lot',
  3622. '/option',
  3623. '/select',
  3624. '/div'
  3625. ];
  3626. $this->assertHtml($expected, $result);
  3627. }
  3628. /**
  3629. * testInvalidControlTypeOption method
  3630. *
  3631. * Test invalid 'input' type option to control() function.
  3632. *
  3633. * @return void
  3634. */
  3635. public function testInvalidControlTypeOption()
  3636. {
  3637. $this->expectException(\RuntimeException::class);
  3638. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3639. $this->Form->control('text', ['type' => 'input']);
  3640. }
  3641. /**
  3642. * testControlMagicSelectChangeToRadio method
  3643. *
  3644. * Test that magic control() selects can easily be converted into radio types without error.
  3645. *
  3646. * @return void
  3647. */
  3648. public function testControlMagicSelectChangeToRadio()
  3649. {
  3650. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3651. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3652. $this->assertContains('input type="radio"', $result);
  3653. }
  3654. /**
  3655. * testFormControlSubmit method
  3656. *
  3657. * Test correct results for form::control() and type submit.
  3658. *
  3659. * @return void
  3660. */
  3661. public function testFormControlSubmit()
  3662. {
  3663. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3664. $expected = [
  3665. 'div' => ['class' => 'submit'],
  3666. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3667. '/div'
  3668. ];
  3669. $this->assertHtml($expected, $result);
  3670. }
  3671. /**
  3672. * testFormControls method
  3673. *
  3674. * Test correct results from Form::controls().
  3675. *
  3676. * @return void
  3677. */
  3678. public function testFormControlsLegendFieldset()
  3679. {
  3680. $this->Form->create($this->article);
  3681. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3682. $expected = [
  3683. '<fieldset',
  3684. '<legend',
  3685. 'The Legend',
  3686. '/legend',
  3687. '*/fieldset',
  3688. ];
  3689. $this->assertHtml($expected, $result);
  3690. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3691. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3692. $this->assertContains('<fieldset>', $result);
  3693. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3694. $this->assertNotContains('<legend>', $result);
  3695. $this->assertNotContains('<fieldset>', $result);
  3696. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3697. $this->assertNotContains('<legend>', $result);
  3698. $this->assertNotContains('<fieldset>', $result);
  3699. $this->Form->create($this->article);
  3700. $this->Form->request->params['prefix'] = 'admin';
  3701. $this->Form->request->params['action'] = 'admin_edit';
  3702. $this->Form->request->params['controller'] = 'articles';
  3703. $result = $this->Form->allControls();
  3704. $expected = [
  3705. '<fieldset',
  3706. '<legend',
  3707. 'New Article',
  3708. '/legend',
  3709. '*/fieldset',
  3710. ];
  3711. $this->assertHtml($expected, $result);
  3712. $this->Form->create($this->article);
  3713. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3714. $expected = [
  3715. '<fieldset',
  3716. '<legend',
  3717. 'The Legend',
  3718. '/legend',
  3719. '*/fieldset',
  3720. ];
  3721. $this->assertHtml($expected, $result);
  3722. $this->Form->create($this->article);
  3723. $result = $this->Form->allControls([], [
  3724. 'fieldset' => [
  3725. 'class' => 'some-class some-other-class',
  3726. 'disabled' => true,
  3727. 'data-param' => 'a-param'
  3728. ],
  3729. 'legend' => 'The Legend'
  3730. ]);
  3731. $expected = [
  3732. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3733. '<legend',
  3734. 'The Legend',
  3735. '/legend',
  3736. '*/fieldset',
  3737. ];
  3738. $this->assertHtml($expected, $result);
  3739. }
  3740. /**
  3741. * testFormControls method
  3742. *
  3743. * Test the controls() method.
  3744. *
  3745. * @return void
  3746. */
  3747. public function testFormControls()
  3748. {
  3749. $this->Form->create($this->article);
  3750. $result = $this->Form->allControls();
  3751. $expected = [
  3752. '<fieldset',
  3753. '<legend', 'New Article', '/legend',
  3754. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3755. ['div' => ['class' => 'input select required']],
  3756. '*/div',
  3757. ['div' => ['class' => 'input text required']],
  3758. '*/div',
  3759. ['div' => ['class' => 'input text']],
  3760. '*/div',
  3761. ['div' => ['class' => 'input text']],
  3762. '*/div',
  3763. '/fieldset',
  3764. ];
  3765. $this->assertHtml($expected, $result);
  3766. $result = $this->Form->allControls([
  3767. 'published' => ['type' => 'boolean']
  3768. ]);
  3769. $expected = [
  3770. '<fieldset',
  3771. '<legend', 'New Article', '/legend',
  3772. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3773. ['div' => ['class' => 'input select required']],
  3774. '*/div',
  3775. ['div' => ['class' => 'input text required']],
  3776. '*/div',
  3777. ['div' => ['class' => 'input text']],
  3778. '*/div',
  3779. ['div' => ['class' => 'input boolean']],
  3780. '*/div',
  3781. '/fieldset',
  3782. ];
  3783. $this->assertHtml($expected, $result);
  3784. $this->Form->create($this->article);
  3785. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3786. $expected = [
  3787. 'fieldset' => [],
  3788. 'legend' => [],
  3789. 'Hello',
  3790. '/legend',
  3791. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3792. ['div' => ['class' => 'input select required']],
  3793. '*/div',
  3794. ['div' => ['class' => 'input text required']],
  3795. '*/div',
  3796. ['div' => ['class' => 'input text']],
  3797. '*/div',
  3798. ['div' => ['class' => 'input text']],
  3799. '*/div',
  3800. '/fieldset'
  3801. ];
  3802. $this->assertHtml($expected, $result);
  3803. $this->Form->create(false);
  3804. $expected = [
  3805. 'fieldset' => [],
  3806. ['div' => ['class' => 'input text']],
  3807. 'label' => ['for' => 'foo'],
  3808. 'Foo',
  3809. '/label',
  3810. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3811. '*/div',
  3812. '/fieldset'
  3813. ];
  3814. $result = $this->Form->allControls(
  3815. ['foo' => ['type' => 'text']],
  3816. ['legend' => false]
  3817. );
  3818. $this->assertHtml($expected, $result);
  3819. }
  3820. /**
  3821. * testFormControlsBlacklist method
  3822. *
  3823. * @return void
  3824. */
  3825. public function testFormControlsBlacklist()
  3826. {
  3827. $this->Form->create($this->article);
  3828. $result = $this->Form->allControls([
  3829. 'id' => false
  3830. ]);
  3831. $expected = [
  3832. '<fieldset',
  3833. '<legend', 'New Article', '/legend',
  3834. ['div' => ['class' => 'input select required']],
  3835. '*/div',
  3836. ['div' => ['class' => 'input text required']],
  3837. '*/div',
  3838. ['div' => ['class' => 'input text']],
  3839. '*/div',
  3840. ['div' => ['class' => 'input text']],
  3841. '*/div',
  3842. '/fieldset',
  3843. ];
  3844. $this->assertHtml($expected, $result);
  3845. $this->Form->create($this->article);
  3846. $result = $this->Form->allControls([
  3847. 'id' => []
  3848. ]);
  3849. $expected = [
  3850. '<fieldset',
  3851. '<legend', 'New Article', '/legend',
  3852. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3853. ['div' => ['class' => 'input select required']],
  3854. '*/div',
  3855. ['div' => ['class' => 'input text required']],
  3856. '*/div',
  3857. ['div' => ['class' => 'input text']],
  3858. '*/div',
  3859. ['div' => ['class' => 'input text']],
  3860. '*/div',
  3861. '/fieldset',
  3862. ];
  3863. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3864. }
  3865. /**
  3866. * testSelectAsCheckbox method
  3867. *
  3868. * Test multi-select widget with checkbox formatting.
  3869. *
  3870. * @return void
  3871. */
  3872. public function testSelectAsCheckbox()
  3873. {
  3874. $result = $this->Form->select(
  3875. 'Model.multi_field',
  3876. ['first', 'second', 'third'],
  3877. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3878. );
  3879. $expected = [
  3880. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3881. ['div' => ['class' => 'checkbox']],
  3882. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3883. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3884. 'first',
  3885. '/label',
  3886. '/div',
  3887. ['div' => ['class' => 'checkbox']],
  3888. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3889. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3890. 'second',
  3891. '/label',
  3892. '/div',
  3893. ['div' => ['class' => 'checkbox']],
  3894. ['label' => ['for' => 'model-multi-field-2']],
  3895. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3896. 'third',
  3897. '/label',
  3898. '/div',
  3899. ];
  3900. $this->assertHtml($expected, $result);
  3901. $result = $this->Form->select(
  3902. 'Model.multi_field',
  3903. ['1/2' => 'half'],
  3904. ['multiple' => 'checkbox']
  3905. );
  3906. $expected = [
  3907. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3908. ['div' => ['class' => 'checkbox']],
  3909. ['label' => ['for' => 'model-multi-field-1-2']],
  3910. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3911. 'half',
  3912. '/label',
  3913. '/div',
  3914. ];
  3915. $this->assertHtml($expected, $result);
  3916. }
  3917. /**
  3918. * testLabel method
  3919. *
  3920. * Test label generation.
  3921. *
  3922. * @return void
  3923. */
  3924. public function testLabel()
  3925. {
  3926. $result = $this->Form->label('Person.name');
  3927. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3928. $this->assertHtml($expected, $result);
  3929. $result = $this->Form->label('Person.name');
  3930. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3931. $this->assertHtml($expected, $result);
  3932. $result = $this->Form->label('Person.first_name');
  3933. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3934. $this->assertHtml($expected, $result);
  3935. $result = $this->Form->label('Person.first_name', 'Your first name');
  3936. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3937. $this->assertHtml($expected, $result);
  3938. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3939. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3940. $this->assertHtml($expected, $result);
  3941. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3942. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3943. $this->assertHtml($expected, $result);
  3944. $result = $this->Form->label('Person.first_name', '');
  3945. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3946. $this->assertHtml($expected, $result);
  3947. $result = $this->Form->label('Person.2.name', '');
  3948. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3949. $this->assertHtml($expected, $result);
  3950. }
  3951. /**
  3952. * testLabelContainControl method
  3953. *
  3954. * Test that label() can accept an input with the correct template vars.
  3955. *
  3956. * @return void
  3957. */
  3958. public function testLabelContainControl()
  3959. {
  3960. $this->Form->setTemplates([
  3961. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3962. ]);
  3963. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3964. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3965. ]);
  3966. $expected = [
  3967. 'label' => ['for' => 'person-accept-terms'],
  3968. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3969. 'Accept',
  3970. '/label',
  3971. ];
  3972. $this->assertHtml($expected, $result);
  3973. }
  3974. /**
  3975. * testTextbox method
  3976. *
  3977. * Test textbox element generation.
  3978. *
  3979. * @return void
  3980. */
  3981. public function testTextbox()
  3982. {
  3983. $result = $this->Form->text('Model.field');
  3984. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3985. $this->assertHtml($expected, $result);
  3986. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3987. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3988. $this->assertHtml($expected, $result);
  3989. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3990. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3991. $this->assertHtml($expected, $result);
  3992. }
  3993. /**
  3994. * testTextBoxDataAndError method
  3995. *
  3996. * Test that text() hooks up with request data and error fields.
  3997. *
  3998. * @return void
  3999. */
  4000. public function testTextBoxDataAndError()
  4001. {
  4002. $this->article['errors'] = [
  4003. 'Contact' => ['text' => 'wrong']
  4004. ];
  4005. $this->Form->create($this->article);
  4006. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  4007. $result = $this->Form->text('Model.text');
  4008. $expected = [
  4009. 'input' => [
  4010. 'type' => 'text',
  4011. 'name' => 'Model[text]',
  4012. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4013. ]
  4014. ];
  4015. $this->assertHtml($expected, $result);
  4016. $this->Form->request->data['Contact']['text'] = 'test';
  4017. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4018. $expected = [
  4019. 'input' => [
  4020. 'type' => 'text',
  4021. 'name' => 'Contact[text]',
  4022. 'value' => 'test',
  4023. 'id' => 'theID',
  4024. 'class' => 'form-error'
  4025. ]
  4026. ];
  4027. $this->assertHtml($expected, $result);
  4028. }
  4029. /**
  4030. * testDefaultValue method
  4031. *
  4032. * Test default value setting.
  4033. *
  4034. * @return void
  4035. */
  4036. public function testTextDefaultValue()
  4037. {
  4038. $this->Form->request->data['Model']['field'] = 'test';
  4039. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4040. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4041. $this->assertHtml($expected, $result);
  4042. unset($this->Form->request->data['Model']['field']);
  4043. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4044. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4045. $this->assertHtml($expected, $result);
  4046. $Articles = TableRegistry::get('Articles');
  4047. $title = $Articles->getSchema()->getColumn('title');
  4048. $Articles->getSchema()->addColumn(
  4049. 'title',
  4050. ['default' => 'default title'] + $title
  4051. );
  4052. $entity = $Articles->newEntity();
  4053. $this->Form->create($entity);
  4054. // Get default value from schema
  4055. $result = $this->Form->text('title');
  4056. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4057. $this->assertHtml($expected, $result);
  4058. // Don't get value from schema
  4059. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4060. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4061. $this->assertHtml($expected, $result);
  4062. // Custom default value overrides default value from schema
  4063. $result = $this->Form->text('title', ['default' => 'override default']);
  4064. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4065. $this->assertHtml($expected, $result);
  4066. // Default value from schema is used only for new entities.
  4067. $entity->isNew(false);
  4068. $result = $this->Form->text('title');
  4069. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4070. $this->assertHtml($expected, $result);
  4071. }
  4072. /**
  4073. * testError method
  4074. *
  4075. * Test field error generation.
  4076. *
  4077. * @return void
  4078. */
  4079. public function testError()
  4080. {
  4081. $this->article['errors'] = [
  4082. 'Article' => ['field' => 'email']
  4083. ];
  4084. $this->Form->create($this->article);
  4085. $result = $this->Form->error('Article.field');
  4086. $expected = [
  4087. ['div' => ['class' => 'error-message']],
  4088. 'email',
  4089. '/div',
  4090. ];
  4091. $this->assertHtml($expected, $result);
  4092. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4093. $expected = [
  4094. ['div' => ['class' => 'error-message']],
  4095. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4096. '/div',
  4097. ];
  4098. $this->assertHtml($expected, $result);
  4099. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4100. $expected = [
  4101. ['div' => ['class' => 'error-message']],
  4102. '<strong', 'Badness!', '/strong',
  4103. '/div',
  4104. ];
  4105. $this->assertHtml($expected, $result);
  4106. }
  4107. /**
  4108. * testErrorRuleName method
  4109. *
  4110. * Test error translation can use rule names for translating.
  4111. *
  4112. * @return void
  4113. */
  4114. public function testErrorRuleName()
  4115. {
  4116. $this->article['errors'] = [
  4117. 'Article' => [
  4118. 'field' => ['email' => 'Your email was not good']
  4119. ]
  4120. ];
  4121. $this->Form->create($this->article);
  4122. $result = $this->Form->error('Article.field');
  4123. $expected = [
  4124. ['div' => ['class' => 'error-message']],
  4125. 'Your email was not good',
  4126. '/div',
  4127. ];
  4128. $this->assertHtml($expected, $result);
  4129. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4130. $expected = [
  4131. ['div' => ['class' => 'error-message']],
  4132. 'Email in use',
  4133. '/div',
  4134. ];
  4135. $this->assertHtml($expected, $result);
  4136. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4137. $expected = [
  4138. ['div' => ['class' => 'error-message']],
  4139. 'Email in use',
  4140. '/div',
  4141. ];
  4142. $this->assertHtml($expected, $result);
  4143. $result = $this->Form->error('Article.field', [
  4144. 'email' => 'Key is preferred',
  4145. 'Your email was not good' => 'Email in use'
  4146. ]);
  4147. $expected = [
  4148. ['div' => ['class' => 'error-message']],
  4149. 'Key is preferred',
  4150. '/div',
  4151. ];
  4152. $this->assertHtml($expected, $result);
  4153. }
  4154. /**
  4155. * testErrorMessages method
  4156. *
  4157. * Test error with nested lists.
  4158. *
  4159. * @return void
  4160. */
  4161. public function testErrorMessages()
  4162. {
  4163. $this->article['errors'] = [
  4164. 'Article' => ['field' => 'email']
  4165. ];
  4166. $this->Form->create($this->article);
  4167. $result = $this->Form->error('Article.field', [
  4168. 'email' => 'No good!'
  4169. ]);
  4170. $expected = [
  4171. 'div' => ['class' => 'error-message'],
  4172. 'No good!',
  4173. '/div'
  4174. ];
  4175. $this->assertHtml($expected, $result);
  4176. }
  4177. /**
  4178. * testErrorMultipleMessages method
  4179. *
  4180. * Test error() with multiple messages.
  4181. *
  4182. * @return void
  4183. */
  4184. public function testErrorMultipleMessages()
  4185. {
  4186. $this->article['errors'] = [
  4187. 'field' => ['notBlank', 'email', 'Something else']
  4188. ];
  4189. $this->Form->create($this->article);
  4190. $result = $this->Form->error('field', [
  4191. 'notBlank' => 'Cannot be empty',
  4192. 'email' => 'No good!'
  4193. ]);
  4194. $expected = [
  4195. 'div' => ['class' => 'error-message'],
  4196. 'ul' => [],
  4197. '<li', 'Cannot be empty', '/li',
  4198. '<li', 'No good!', '/li',
  4199. '<li', 'Something else', '/li',
  4200. '/ul',
  4201. '/div'
  4202. ];
  4203. $this->assertHtml($expected, $result);
  4204. }
  4205. /**
  4206. * testPassword method
  4207. *
  4208. * Test password element generation.
  4209. *
  4210. * @return void
  4211. */
  4212. public function testPassword()
  4213. {
  4214. $this->article['errors'] = [
  4215. 'Contact' => [
  4216. 'passwd' => 1
  4217. ]
  4218. ];
  4219. $this->Form->create($this->article);
  4220. $result = $this->Form->password('Contact.field');
  4221. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4222. $this->assertHtml($expected, $result);
  4223. $this->Form->request->data['Contact']['passwd'] = 'test';
  4224. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4225. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4226. $this->assertHtml($expected, $result);
  4227. }
  4228. /**
  4229. * testRadio method
  4230. *
  4231. * Test radio element set generation.
  4232. *
  4233. * @return void
  4234. */
  4235. public function testRadio()
  4236. {
  4237. $result = $this->Form->radio('Model.field', ['option A']);
  4238. $expected = [
  4239. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4240. 'label' => ['for' => 'model-field-0'],
  4241. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4242. 'option A',
  4243. '/label'
  4244. ];
  4245. $this->assertHtml($expected, $result);
  4246. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4247. $this->assertHtml($expected, $result);
  4248. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4249. $expected = [
  4250. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4251. ['label' => ['for' => 'model-field-0']],
  4252. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4253. 'option A',
  4254. '/label',
  4255. ['label' => ['for' => 'model-field-1']],
  4256. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4257. 'option B',
  4258. '/label',
  4259. ];
  4260. $this->assertHtml($expected, $result);
  4261. $result = $this->Form->radio(
  4262. 'Employee.gender',
  4263. ['male' => 'Male', 'female' => 'Female'],
  4264. ['form' => 'my-form']
  4265. );
  4266. $expected = [
  4267. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4268. ['label' => ['for' => 'employee-gender-male']],
  4269. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4270. 'Male',
  4271. '/label',
  4272. ['label' => ['for' => 'employee-gender-female']],
  4273. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4274. 'Female',
  4275. '/label',
  4276. ];
  4277. $this->assertHtml($expected, $result);
  4278. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4279. $expected = [
  4280. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4281. ['label' => ['for' => 'model-custom-0']],
  4282. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4283. 'option A',
  4284. '/label',
  4285. ['label' => ['for' => 'model-custom-1']],
  4286. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4287. 'option B',
  4288. '/label',
  4289. ];
  4290. $this->assertHtml($expected, $result);
  4291. $result = $this->Form->radio(
  4292. 'Employee.gender',
  4293. [
  4294. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4295. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4296. ]
  4297. );
  4298. $expected = [
  4299. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4300. ['label' => ['for' => 'employee-gender-male']],
  4301. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4302. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4303. 'Male',
  4304. '/label',
  4305. ['label' => ['for' => 'employee-gender-female']],
  4306. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4307. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4308. 'Female',
  4309. '/label',
  4310. ];
  4311. $this->assertHtml($expected, $result);
  4312. }
  4313. /**
  4314. * Test radio with complex options and empty disabled data.
  4315. *
  4316. * @return void
  4317. */
  4318. public function testRadioComplexDisabled()
  4319. {
  4320. $options = [
  4321. ['value' => 'r', 'text' => 'red'],
  4322. ['value' => 'b', 'text' => 'blue'],
  4323. ];
  4324. $attrs = ['disabled' => []];
  4325. $result = $this->Form->radio('Model.field', $options, $attrs);
  4326. $expected = [
  4327. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4328. ['label' => ['for' => 'model-field-r']],
  4329. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4330. 'red',
  4331. '/label',
  4332. ['label' => ['for' => 'model-field-b']],
  4333. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4334. 'blue',
  4335. '/label',
  4336. ];
  4337. $this->assertHtml($expected, $result);
  4338. $attrs = ['disabled' => ['r']];
  4339. $result = $this->Form->radio('Model.field', $options, $attrs);
  4340. $this->assertContains('disabled="disabled"', $result);
  4341. }
  4342. /**
  4343. * testRadioDefaultValue method
  4344. *
  4345. * Test default value setting on radio() method.
  4346. *
  4347. * @return void
  4348. */
  4349. public function testRadioDefaultValue()
  4350. {
  4351. $Articles = TableRegistry::get('Articles');
  4352. $title = $Articles->getSchema()->getColumn('title');
  4353. $Articles->getSchema()->addColumn(
  4354. 'title',
  4355. ['default' => '1'] + $title
  4356. );
  4357. $this->Form->create($Articles->newEntity());
  4358. $result = $this->Form->radio('title', ['option A', 'option B']);
  4359. $expected = [
  4360. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4361. ['label' => ['for' => 'title-0']],
  4362. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4363. 'option A',
  4364. '/label',
  4365. ['label' => ['for' => 'title-1']],
  4366. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4367. 'option B',
  4368. '/label',
  4369. ];
  4370. $this->assertHtml($expected, $result);
  4371. }
  4372. /**
  4373. * Test setting a hiddenField value on radio buttons.
  4374. *
  4375. * @return void
  4376. */
  4377. public function testRadioHiddenFieldValue()
  4378. {
  4379. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4380. $expected = [
  4381. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4382. 'label' => ['for' => 'title-0'],
  4383. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4384. 'option A',
  4385. '/label',
  4386. ];
  4387. $this->assertHtml($expected, $result);
  4388. }
  4389. /**
  4390. * testControlRadio method
  4391. *
  4392. * Test that input works with radio types.
  4393. *
  4394. * @return void
  4395. */
  4396. public function testControlRadio()
  4397. {
  4398. $result = $this->Form->control('test', [
  4399. 'type' => 'radio',
  4400. 'options' => ['A', 'B'],
  4401. ]);
  4402. $expected = [
  4403. ['div' => ['class' => 'input radio']],
  4404. '<label',
  4405. 'Test',
  4406. '/label',
  4407. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4408. ['label' => ['for' => 'test-0']],
  4409. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4410. 'A',
  4411. '/label',
  4412. ['label' => ['for' => 'test-1']],
  4413. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4414. 'B',
  4415. '/label',
  4416. '/div',
  4417. ];
  4418. $this->assertHtml($expected, $result);
  4419. $result = $this->Form->control('test', [
  4420. 'type' => 'radio',
  4421. 'options' => ['A', 'B'],
  4422. 'value' => '0'
  4423. ]);
  4424. $expected = [
  4425. ['div' => ['class' => 'input radio']],
  4426. '<label',
  4427. 'Test',
  4428. '/label',
  4429. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4430. ['label' => ['for' => 'test-0']],
  4431. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4432. 'A',
  4433. '/label',
  4434. ['label' => ['for' => 'test-1']],
  4435. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4436. 'B',
  4437. '/label',
  4438. '/div',
  4439. ];
  4440. $this->assertHtml($expected, $result);
  4441. $result = $this->Form->control('test', [
  4442. 'type' => 'radio',
  4443. 'options' => ['A', 'B'],
  4444. 'label' => false
  4445. ]);
  4446. $expected = [
  4447. ['div' => ['class' => 'input radio']],
  4448. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4449. ['label' => ['for' => 'test-0']],
  4450. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4451. 'A',
  4452. '/label',
  4453. ['label' => ['for' => 'test-1']],
  4454. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4455. 'B',
  4456. '/label',
  4457. '/div',
  4458. ];
  4459. $this->assertHtml($expected, $result);
  4460. }
  4461. /**
  4462. * testRadioNoLabel method
  4463. *
  4464. * Test that radio() works with label = false.
  4465. *
  4466. * @return void
  4467. */
  4468. public function testRadioNoLabel()
  4469. {
  4470. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4471. $expected = [
  4472. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4473. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4474. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4475. ];
  4476. $this->assertHtml($expected, $result);
  4477. }
  4478. /**
  4479. * testRadioControlInsideLabel method
  4480. *
  4481. * Test generating radio input inside label ala twitter bootstrap.
  4482. *
  4483. * @return void
  4484. */
  4485. public function testRadioControlInsideLabel()
  4486. {
  4487. $this->Form->setTemplates([
  4488. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4489. 'radioWrapper' => '{{label}}'
  4490. ]);
  4491. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4492. //@codingStandardsIgnoreStart
  4493. $expected = [
  4494. ['input' => [
  4495. 'type' => 'hidden',
  4496. 'name' => 'Model[field]',
  4497. 'value' => ''
  4498. ]],
  4499. ['label' => ['for' => 'model-field-0']],
  4500. ['input' => [
  4501. 'type' => 'radio',
  4502. 'name' => 'Model[field]',
  4503. 'value' => '0',
  4504. 'id' => 'model-field-0'
  4505. ]],
  4506. 'option A',
  4507. '/label',
  4508. ['label' => ['for' => 'model-field-1']],
  4509. ['input' => [
  4510. 'type' => 'radio',
  4511. 'name' => 'Model[field]',
  4512. 'value' => '1',
  4513. 'id' => 'model-field-1'
  4514. ]],
  4515. 'option B',
  4516. '/label',
  4517. ];
  4518. //@codingStandardsIgnoreEnd
  4519. $this->assertHtml($expected, $result);
  4520. }
  4521. /**
  4522. * testRadioHiddenControlDisabling method
  4523. *
  4524. * Test disabling the hidden input for radio buttons.
  4525. *
  4526. * @return void
  4527. */
  4528. public function testRadioHiddenControlDisabling()
  4529. {
  4530. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4531. $expected = [
  4532. 'label' => ['for' => 'model-1-field-0'],
  4533. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4534. 'option A',
  4535. '/label'
  4536. ];
  4537. $this->assertHtml($expected, $result);
  4538. }
  4539. /**
  4540. * testRadioOutOfRange method
  4541. *
  4542. * Test radio element set generation.
  4543. *
  4544. * @return void
  4545. */
  4546. public function testRadioOutOfRange()
  4547. {
  4548. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4549. $expected = [
  4550. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4551. 'label' => ['for' => 'model-field-v'],
  4552. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4553. 'value',
  4554. '/label'
  4555. ];
  4556. $this->assertHtml($expected, $result);
  4557. }
  4558. /**
  4559. * testSelect method
  4560. *
  4561. * Test select element generation.
  4562. *
  4563. * @return void
  4564. */
  4565. public function testSelect()
  4566. {
  4567. $result = $this->Form->select('Model.field', []);
  4568. $expected = [
  4569. 'select' => ['name' => 'Model[field]'],
  4570. '/select'
  4571. ];
  4572. $this->assertHtml($expected, $result);
  4573. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4574. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4575. $expected = [
  4576. 'select' => ['name' => 'Model[field]'],
  4577. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4578. 'good',
  4579. '/option',
  4580. ['option' => ['value' => 'other']],
  4581. 'bad',
  4582. '/option',
  4583. '/select'
  4584. ];
  4585. $this->assertHtml($expected, $result);
  4586. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4587. $this->assertHtml($expected, $result);
  4588. $this->Form->request->data = [];
  4589. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4590. $expected = [
  4591. 'select' => ['name' => 'Model[field]'],
  4592. ['option' => ['value' => 'value']],
  4593. 'good',
  4594. '/option',
  4595. ['option' => ['value' => 'other']],
  4596. 'bad',
  4597. '/option',
  4598. '/select'
  4599. ];
  4600. $this->assertHtml($expected, $result);
  4601. $options = [
  4602. ['value' => 'first', 'text' => 'First'],
  4603. ['value' => 'first', 'text' => 'Another First'],
  4604. ];
  4605. $result = $this->Form->select(
  4606. 'Model.field',
  4607. $options,
  4608. ['escape' => false, 'empty' => false]
  4609. );
  4610. $expected = [
  4611. 'select' => ['name' => 'Model[field]'],
  4612. ['option' => ['value' => 'first']],
  4613. 'First',
  4614. '/option',
  4615. ['option' => ['value' => 'first']],
  4616. 'Another First',
  4617. '/option',
  4618. '/select'
  4619. ];
  4620. $this->assertHtml($expected, $result);
  4621. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4622. $result = $this->Form->select(
  4623. 'Model.contact_id',
  4624. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4625. ['escape' => false, 'empty' => 'pick something']
  4626. );
  4627. $expected = [
  4628. 'select' => ['name' => 'Model[contact_id]'],
  4629. ['option' => ['value' => '']], 'pick something', '/option',
  4630. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4631. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4632. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4633. '/select'
  4634. ];
  4635. $this->assertHtml($expected, $result);
  4636. $this->Form->request->data['Model']['field'] = 0;
  4637. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4638. $expected = [
  4639. 'select' => ['name' => 'Model[field]'],
  4640. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4641. ['option' => ['value' => '1']], 'Yes', '/option',
  4642. '/select'
  4643. ];
  4644. $this->assertHtml($expected, $result);
  4645. }
  4646. /**
  4647. * testSelectEscapeHtml method
  4648. *
  4649. * Test that select() escapes HTML.
  4650. *
  4651. * @return void
  4652. */
  4653. public function testSelectEscapeHtml()
  4654. {
  4655. $result = $this->Form->select(
  4656. 'Model.field',
  4657. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4658. ['empty' => false]
  4659. );
  4660. $expected = [
  4661. 'select' => ['name' => 'Model[field]'],
  4662. ['option' => ['value' => 'first']],
  4663. 'first &quot;html&quot; &lt;chars&gt;',
  4664. '/option',
  4665. ['option' => ['value' => 'second']],
  4666. 'value',
  4667. '/option',
  4668. '/select'
  4669. ];
  4670. $this->assertHtml($expected, $result);
  4671. $result = $this->Form->select(
  4672. 'Model.field',
  4673. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4674. ['escape' => false, 'empty' => false]
  4675. );
  4676. $expected = [
  4677. 'select' => ['name' => 'Model[field]'],
  4678. ['option' => ['value' => 'first']],
  4679. 'first "html" <chars>',
  4680. '/option',
  4681. ['option' => ['value' => 'second']],
  4682. 'value',
  4683. '/option',
  4684. '/select'
  4685. ];
  4686. $this->assertHtml($expected, $result);
  4687. }
  4688. /**
  4689. * testSelectRequired method
  4690. *
  4691. * Test select() with required and disabled attributes.
  4692. *
  4693. * @return void
  4694. */
  4695. public function testSelectRequired()
  4696. {
  4697. $this->article['required'] = [
  4698. 'user_id' => true
  4699. ];
  4700. $this->Form->create($this->article);
  4701. $result = $this->Form->select('user_id', ['option A']);
  4702. $expected = [
  4703. 'select' => [
  4704. 'name' => 'user_id',
  4705. 'required' => 'required'
  4706. ],
  4707. ['option' => ['value' => '0']], 'option A', '/option',
  4708. '/select'
  4709. ];
  4710. $this->assertHtml($expected, $result);
  4711. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4712. $expected = [
  4713. 'select' => [
  4714. 'name' => 'user_id',
  4715. 'disabled' => 'disabled'
  4716. ],
  4717. ['option' => ['value' => '0']], 'option A', '/option',
  4718. '/select'
  4719. ];
  4720. $this->assertHtml($expected, $result);
  4721. }
  4722. /**
  4723. * testNestedSelect method
  4724. *
  4725. * Test select element generation with optgroups.
  4726. *
  4727. * @return void
  4728. */
  4729. public function testNestedSelect()
  4730. {
  4731. $result = $this->Form->select(
  4732. 'Model.field',
  4733. [1 => 'One', 2 => 'Two', 'Three' => [
  4734. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4735. ]],
  4736. ['empty' => false]
  4737. );
  4738. $expected = [
  4739. 'select' => ['name' => 'Model[field]'],
  4740. ['option' => ['value' => 1]],
  4741. 'One',
  4742. '/option',
  4743. ['option' => ['value' => 2]],
  4744. 'Two',
  4745. '/option',
  4746. ['optgroup' => ['label' => 'Three']],
  4747. ['option' => ['value' => 3]],
  4748. 'Three',
  4749. '/option',
  4750. ['option' => ['value' => 4]],
  4751. 'Four',
  4752. '/option',
  4753. ['option' => ['value' => 5]],
  4754. 'Five',
  4755. '/option',
  4756. '/optgroup',
  4757. '/select'
  4758. ];
  4759. $this->assertHtml($expected, $result);
  4760. }
  4761. /**
  4762. * testSelectMultiple method
  4763. *
  4764. * Test generation of multiple select elements.
  4765. *
  4766. * @return void
  4767. */
  4768. public function testSelectMultiple()
  4769. {
  4770. $options = ['first', 'second', 'third'];
  4771. $result = $this->Form->select(
  4772. 'Model.multi_field',
  4773. $options,
  4774. ['form' => 'my-form', 'multiple' => true]
  4775. );
  4776. $expected = [
  4777. 'input' => [
  4778. 'type' => 'hidden',
  4779. 'name' => 'Model[multi_field]',
  4780. 'value' => '',
  4781. 'form' => 'my-form',
  4782. ],
  4783. 'select' => [
  4784. 'name' => 'Model[multi_field][]',
  4785. 'multiple' => 'multiple',
  4786. 'form' => 'my-form',
  4787. ],
  4788. ['option' => ['value' => '0']],
  4789. 'first',
  4790. '/option',
  4791. ['option' => ['value' => '1']],
  4792. 'second',
  4793. '/option',
  4794. ['option' => ['value' => '2']],
  4795. 'third',
  4796. '/option',
  4797. '/select'
  4798. ];
  4799. $this->assertHtml($expected, $result);
  4800. $result = $this->Form->select(
  4801. 'Model.multi_field',
  4802. $options,
  4803. ['multiple' => 'multiple', 'form' => 'my-form']
  4804. );
  4805. $this->assertHtml($expected, $result);
  4806. }
  4807. /**
  4808. * testCheckboxZeroValue method
  4809. *
  4810. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4811. *
  4812. * @return void
  4813. */
  4814. public function testCheckboxZeroValue()
  4815. {
  4816. $result = $this->Form->control('User.get_spam', [
  4817. 'type' => 'checkbox',
  4818. 'value' => '0',
  4819. 'hiddenField' => '1',
  4820. ]);
  4821. $expected = [
  4822. 'div' => ['class' => 'input checkbox'],
  4823. 'label' => ['for' => 'user-get-spam'],
  4824. ['input' => [
  4825. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4826. 'value' => '1'
  4827. ]],
  4828. ['input' => [
  4829. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4830. 'value' => '0', 'id' => 'user-get-spam'
  4831. ]],
  4832. 'Get Spam',
  4833. '/label',
  4834. '/div'
  4835. ];
  4836. $this->assertHtml($expected, $result);
  4837. }
  4838. /**
  4839. * testHabtmSelectBox method
  4840. *
  4841. * Test generation of habtm select boxes.
  4842. *
  4843. * @return void
  4844. */
  4845. public function testHabtmSelectBox()
  4846. {
  4847. $this->loadFixtures('Articles');
  4848. $options = [
  4849. 1 => 'blue',
  4850. 2 => 'red',
  4851. 3 => 'green'
  4852. ];
  4853. $tags = [
  4854. new Entity(['id' => 1, 'name' => 'blue']),
  4855. new Entity(['id' => 3, 'name' => 'green'])
  4856. ];
  4857. $article = new Article(['tags' => $tags]);
  4858. $this->Form->create($article);
  4859. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4860. $expected = [
  4861. 'div' => ['class' => 'input select'],
  4862. 'label' => ['for' => 'tags-ids'],
  4863. 'Tags',
  4864. '/label',
  4865. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4866. 'select' => [
  4867. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4868. 'multiple' => 'multiple'
  4869. ],
  4870. ['option' => ['value' => '1', 'selected' => 'selected']],
  4871. 'blue',
  4872. '/option',
  4873. ['option' => ['value' => '2']],
  4874. 'red',
  4875. '/option',
  4876. ['option' => ['value' => '3', 'selected' => 'selected']],
  4877. 'green',
  4878. '/option',
  4879. '/select',
  4880. '/div'
  4881. ];
  4882. $this->assertHtml($expected, $result);
  4883. // make sure only 50 is selected, and not 50f5c0cf
  4884. $options = [
  4885. '1' => 'blue',
  4886. '50f5c0cf' => 'red',
  4887. '50' => 'green'
  4888. ];
  4889. $tags = [
  4890. new Entity(['id' => 1, 'name' => 'blue']),
  4891. new Entity(['id' => 50, 'name' => 'green'])
  4892. ];
  4893. $article = new Article(['tags' => $tags]);
  4894. $this->Form->create($article);
  4895. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4896. $expected = [
  4897. 'div' => ['class' => 'input select'],
  4898. 'label' => ['for' => 'tags-ids'],
  4899. 'Tags',
  4900. '/label',
  4901. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4902. 'select' => [
  4903. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4904. 'multiple' => 'multiple'
  4905. ],
  4906. ['option' => ['value' => '1', 'selected' => 'selected']],
  4907. 'blue',
  4908. '/option',
  4909. ['option' => ['value' => '50f5c0cf']],
  4910. 'red',
  4911. '/option',
  4912. ['option' => ['value' => '50', 'selected' => 'selected']],
  4913. 'green',
  4914. '/option',
  4915. '/select',
  4916. '/div'
  4917. ];
  4918. $this->assertHtml($expected, $result);
  4919. $spacecraft = [
  4920. 1 => 'Orion',
  4921. 2 => 'Helios'
  4922. ];
  4923. $this->View->viewVars['spacecraft'] = $spacecraft;
  4924. $this->Form->create();
  4925. $result = $this->Form->control('spacecraft._ids');
  4926. $expected = [
  4927. 'div' => ['class' => 'input select'],
  4928. 'label' => ['for' => 'spacecraft-ids'],
  4929. 'Spacecraft',
  4930. '/label',
  4931. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4932. 'select' => [
  4933. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4934. 'multiple' => 'multiple'
  4935. ],
  4936. ['option' => ['value' => '1']],
  4937. 'Orion',
  4938. '/option',
  4939. ['option' => ['value' => '2']],
  4940. 'Helios',
  4941. '/option',
  4942. '/select',
  4943. '/div'
  4944. ];
  4945. $this->assertHtml($expected, $result);
  4946. }
  4947. /**
  4948. * testErrorsForBelongsToManySelect method
  4949. *
  4950. * Tests that errors for belongsToMany select fields are being
  4951. * picked up properly.
  4952. *
  4953. * @return void
  4954. */
  4955. public function testErrorsForBelongsToManySelect()
  4956. {
  4957. $spacecraft = [
  4958. 1 => 'Orion',
  4959. 2 => 'Helios'
  4960. ];
  4961. $this->View->viewVars['spacecraft'] = $spacecraft;
  4962. $article = new Article();
  4963. $article->setError('spacecraft', ['Invalid']);
  4964. $this->Form->create($article);
  4965. $result = $this->Form->control('spacecraft._ids');
  4966. $expected = [
  4967. ['div' => ['class' => 'input select error']],
  4968. 'label' => ['for' => 'spacecraft-ids'],
  4969. 'Spacecraft',
  4970. '/label',
  4971. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4972. 'select' => [
  4973. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4974. 'multiple' => 'multiple'
  4975. ],
  4976. ['option' => ['value' => '1']],
  4977. 'Orion',
  4978. '/option',
  4979. ['option' => ['value' => '2']],
  4980. 'Helios',
  4981. '/option',
  4982. '/select',
  4983. ['div' => ['class' => 'error-message']],
  4984. 'Invalid',
  4985. '/div',
  4986. '/div'
  4987. ];
  4988. $this->assertHtml($expected, $result);
  4989. }
  4990. /**
  4991. * testSelectMultipleCheckboxes method
  4992. *
  4993. * Test generation of multi select elements in checkbox format.
  4994. *
  4995. * @return void
  4996. */
  4997. public function testSelectMultipleCheckboxes()
  4998. {
  4999. $result = $this->Form->select(
  5000. 'Model.multi_field',
  5001. ['first', 'second', 'third'],
  5002. ['multiple' => 'checkbox']
  5003. );
  5004. $expected = [
  5005. 'input' => [
  5006. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5007. ],
  5008. ['div' => ['class' => 'checkbox']],
  5009. ['label' => ['for' => 'model-multi-field-0']],
  5010. ['input' => [
  5011. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5012. 'value' => '0', 'id' => 'model-multi-field-0'
  5013. ]],
  5014. 'first',
  5015. '/label',
  5016. '/div',
  5017. ['div' => ['class' => 'checkbox']],
  5018. ['label' => ['for' => 'model-multi-field-1']],
  5019. ['input' => [
  5020. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5021. 'value' => '1', 'id' => 'model-multi-field-1'
  5022. ]],
  5023. 'second',
  5024. '/label',
  5025. '/div',
  5026. ['div' => ['class' => 'checkbox']],
  5027. ['label' => ['for' => 'model-multi-field-2']],
  5028. ['input' => [
  5029. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5030. 'value' => '2', 'id' => 'model-multi-field-2'
  5031. ]],
  5032. 'third',
  5033. '/label',
  5034. '/div'
  5035. ];
  5036. $this->assertHtml($expected, $result);
  5037. $result = $this->Form->select(
  5038. 'Model.multi_field',
  5039. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5040. ['multiple' => 'checkbox']
  5041. );
  5042. $expected = [
  5043. 'input' => [
  5044. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5045. ],
  5046. ['div' => ['class' => 'checkbox']],
  5047. ['label' => ['for' => 'model-multi-field-a+']],
  5048. ['input' => [
  5049. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5050. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5051. ]],
  5052. 'first',
  5053. '/label',
  5054. '/div',
  5055. ['div' => ['class' => 'checkbox']],
  5056. ['label' => ['for' => 'model-multi-field-a++']],
  5057. ['input' => [
  5058. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5059. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5060. ]],
  5061. 'second',
  5062. '/label',
  5063. '/div',
  5064. ['div' => ['class' => 'checkbox']],
  5065. ['label' => ['for' => 'model-multi-field-a+++']],
  5066. ['input' => [
  5067. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5068. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5069. ]],
  5070. 'third',
  5071. '/label',
  5072. '/div'
  5073. ];
  5074. $this->assertHtml($expected, $result);
  5075. $result = $this->Form->select(
  5076. 'Model.multi_field',
  5077. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5078. ['multiple' => 'checkbox']
  5079. );
  5080. $expected = [
  5081. 'input' => [
  5082. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5083. ],
  5084. ['div' => ['class' => 'checkbox']],
  5085. ['label' => ['for' => 'model-multi-field-a-b']],
  5086. ['input' => [
  5087. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5088. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5089. ]],
  5090. 'first',
  5091. '/label',
  5092. '/div',
  5093. ['div' => ['class' => 'checkbox']],
  5094. ['label' => ['for' => 'model-multi-field-a-b1']],
  5095. ['input' => [
  5096. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5097. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5098. ]],
  5099. 'second',
  5100. '/label',
  5101. '/div',
  5102. ['div' => ['class' => 'checkbox']],
  5103. ['label' => ['for' => 'model-multi-field-a-b2']],
  5104. ['input' => [
  5105. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5106. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5107. ]],
  5108. 'third',
  5109. '/label',
  5110. '/div'
  5111. ];
  5112. $this->assertHtml($expected, $result);
  5113. }
  5114. /**
  5115. * testSelectMultipleCheckboxRequestData method
  5116. *
  5117. * Ensure that multiCheckbox reads from the request data.
  5118. *
  5119. * @return void
  5120. */
  5121. public function testSelectMultipleCheckboxRequestData()
  5122. {
  5123. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5124. $result = $this->Form->select(
  5125. 'Model.tags',
  5126. ['1' => 'first', 'Array' => 'Array'],
  5127. ['multiple' => 'checkbox']
  5128. );
  5129. $expected = [
  5130. 'input' => [
  5131. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5132. ],
  5133. ['div' => ['class' => 'checkbox']],
  5134. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5135. ['input' => [
  5136. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5137. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5138. ]],
  5139. 'first',
  5140. '/label',
  5141. '/div',
  5142. ['div' => ['class' => 'checkbox']],
  5143. ['label' => ['for' => 'model-tags-array']],
  5144. ['input' => [
  5145. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5146. 'value' => 'Array', 'id' => 'model-tags-array'
  5147. ]],
  5148. 'Array',
  5149. '/label',
  5150. '/div'
  5151. ];
  5152. $this->assertHtml($expected, $result);
  5153. }
  5154. /**
  5155. * testSelectMultipleCheckboxSecurity method
  5156. *
  5157. * Checks the security hash array generated for multiple-input checkbox elements.
  5158. *
  5159. * @return void
  5160. */
  5161. public function testSelectMultipleCheckboxSecurity()
  5162. {
  5163. $this->Form->request->params['_Token'] = 'testKey';
  5164. $this->assertEquals([], $this->Form->fields);
  5165. $this->Form->select(
  5166. 'Model.multi_field',
  5167. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5168. ['multiple' => 'checkbox']
  5169. );
  5170. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5171. $result = $this->Form->secure($this->Form->fields);
  5172. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5173. $this->assertContains('"' . $key . '"', $result);
  5174. }
  5175. /**
  5176. * testSelectMultipleSecureWithNoOptions method
  5177. *
  5178. * Multiple select elements should always be secured as they always participate
  5179. * in the POST data.
  5180. *
  5181. * @return void
  5182. */
  5183. public function testSelectMultipleSecureWithNoOptions()
  5184. {
  5185. $this->assertEquals([], $this->Form->fields);
  5186. $this->Form->select(
  5187. 'Model.select',
  5188. [],
  5189. ['multiple' => true]
  5190. );
  5191. $this->assertEquals(['Model.select'], $this->Form->fields);
  5192. }
  5193. /**
  5194. * testSelectNoSecureWithNoOptions method
  5195. *
  5196. * When a select box has no options it should not be added to the fields list
  5197. * as it always fail post validation.
  5198. *
  5199. * @return void
  5200. */
  5201. public function testSelectNoSecureWithNoOptions()
  5202. {
  5203. $this->Form->request->params['_Token'] = 'testkey';
  5204. $this->assertEquals([], $this->Form->fields);
  5205. $this->Form->select(
  5206. 'Model.select',
  5207. []
  5208. );
  5209. $this->assertEquals([], $this->Form->fields);
  5210. $this->Form->select(
  5211. 'Model.user_id',
  5212. [],
  5213. ['empty' => true]
  5214. );
  5215. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5216. }
  5217. /**
  5218. * testControlMultipleCheckboxes method
  5219. *
  5220. * Test control() resulting in multi select elements being generated.
  5221. *
  5222. * @return void
  5223. */
  5224. public function testControlMultipleCheckboxes()
  5225. {
  5226. $result = $this->Form->control('Model.multi_field', [
  5227. 'options' => ['first', 'second', 'third'],
  5228. 'multiple' => 'checkbox'
  5229. ]);
  5230. $expected = [
  5231. ['div' => ['class' => 'input select']],
  5232. ['label' => ['for' => 'model-multi-field']],
  5233. 'Multi Field',
  5234. '/label',
  5235. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5236. ['div' => ['class' => 'checkbox']],
  5237. ['label' => ['for' => 'model-multi-field-0']],
  5238. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5239. 'first',
  5240. '/label',
  5241. '/div',
  5242. ['div' => ['class' => 'checkbox']],
  5243. ['label' => ['for' => 'model-multi-field-1']],
  5244. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5245. 'second',
  5246. '/label',
  5247. '/div',
  5248. ['div' => ['class' => 'checkbox']],
  5249. ['label' => ['for' => 'model-multi-field-2']],
  5250. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5251. 'third',
  5252. '/label',
  5253. '/div',
  5254. '/div'
  5255. ];
  5256. $this->assertHtml($expected, $result);
  5257. $result = $this->Form->control('Model.multi_field', [
  5258. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5259. 'multiple' => 'checkbox'
  5260. ]);
  5261. $expected = [
  5262. ['div' => ['class' => 'input select']],
  5263. ['label' => ['for' => 'model-multi-field']],
  5264. 'Multi Field',
  5265. '/label',
  5266. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5267. ['div' => ['class' => 'checkbox']],
  5268. ['label' => ['for' => 'model-multi-field-a']],
  5269. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5270. 'first',
  5271. '/label',
  5272. '/div',
  5273. ['div' => ['class' => 'checkbox']],
  5274. ['label' => ['for' => 'model-multi-field-b']],
  5275. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5276. 'second',
  5277. '/label',
  5278. '/div',
  5279. ['div' => ['class' => 'checkbox']],
  5280. ['label' => ['for' => 'model-multi-field-c']],
  5281. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5282. 'third',
  5283. '/label',
  5284. '/div',
  5285. '/div'
  5286. ];
  5287. $this->assertHtml($expected, $result);
  5288. }
  5289. /**
  5290. * testSelectHiddenFieldOmission method
  5291. *
  5292. * Test that select() with 'hiddenField' => false omits the hidden field.
  5293. *
  5294. * @return void
  5295. */
  5296. public function testSelectHiddenFieldOmission()
  5297. {
  5298. $result = $this->Form->select(
  5299. 'Model.multi_field',
  5300. ['first', 'second'],
  5301. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5302. );
  5303. $this->assertNotContains('type="hidden"', $result);
  5304. }
  5305. /**
  5306. * testSelectCheckboxMultipleOverrideName method
  5307. *
  5308. * Test that select() with multiple = checkbox works with overriding name attribute.
  5309. *
  5310. * @return void
  5311. */
  5312. public function testSelectCheckboxMultipleOverrideName()
  5313. {
  5314. $result = $this->Form->select('category', ['1', '2'], [
  5315. 'multiple' => 'checkbox',
  5316. 'name' => 'fish',
  5317. ]);
  5318. $expected = [
  5319. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5320. ['div' => ['class' => 'checkbox']],
  5321. ['label' => ['for' => 'fish-0']],
  5322. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5323. '1',
  5324. '/label',
  5325. '/div',
  5326. ['div' => ['class' => 'checkbox']],
  5327. ['label' => ['for' => 'fish-1']],
  5328. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5329. '2',
  5330. '/label',
  5331. '/div'
  5332. ];
  5333. $this->assertHtml($expected, $result);
  5334. $result = $this->Form->multiCheckbox(
  5335. 'category',
  5336. new Collection(['1', '2']),
  5337. ['name' => 'fish']
  5338. );
  5339. $this->assertHtml($expected, $result);
  5340. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5341. 'name' => 'fish',
  5342. ]);
  5343. $this->assertHtml($expected, $result);
  5344. }
  5345. /**
  5346. * testControlMultiCheckbox method
  5347. *
  5348. * Test that control() works with multicheckbox.
  5349. *
  5350. * @return void
  5351. */
  5352. public function testControlMultiCheckbox()
  5353. {
  5354. $result = $this->Form->control('category', [
  5355. 'type' => 'multicheckbox',
  5356. 'options' => ['1', '2'],
  5357. ]);
  5358. $expected = [
  5359. ['div' => ['class' => 'input multicheckbox']],
  5360. '<label',
  5361. 'Category',
  5362. '/label',
  5363. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5364. ['div' => ['class' => 'checkbox']],
  5365. ['label' => ['for' => 'category-0']],
  5366. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5367. '1',
  5368. '/label',
  5369. '/div',
  5370. ['div' => ['class' => 'checkbox']],
  5371. ['label' => ['for' => 'category-1']],
  5372. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5373. '2',
  5374. '/label',
  5375. '/div',
  5376. '/div',
  5377. ];
  5378. $this->assertHtml($expected, $result);
  5379. }
  5380. /**
  5381. * testCheckbox method
  5382. *
  5383. * Test generation of checkboxes.
  5384. *
  5385. * @return void
  5386. */
  5387. public function testCheckbox()
  5388. {
  5389. $result = $this->Form->checkbox('Model.field');
  5390. $expected = [
  5391. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5392. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5393. ];
  5394. $this->assertHtml($expected, $result);
  5395. $result = $this->Form->checkbox('Model.field', [
  5396. 'id' => 'theID',
  5397. 'value' => 'myvalue',
  5398. 'form' => 'my-form',
  5399. ]);
  5400. $expected = [
  5401. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5402. ['input' => [
  5403. 'type' => 'checkbox', 'name' => 'Model[field]',
  5404. 'value' => 'myvalue', 'id' => 'theID',
  5405. 'form' => 'my-form',
  5406. ]]
  5407. ];
  5408. $this->assertHtml($expected, $result);
  5409. }
  5410. /**
  5411. * testCheckboxDefaultValue method
  5412. *
  5413. * Test default value setting on checkbox() method.
  5414. *
  5415. * @return void
  5416. */
  5417. public function testCheckboxDefaultValue()
  5418. {
  5419. $this->Form->request->data['Model']['field'] = false;
  5420. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5421. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5422. $this->assertHtml($expected, $result);
  5423. unset($this->Form->request->data['Model']['field']);
  5424. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5425. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5426. $this->assertHtml($expected, $result);
  5427. $this->Form->request->data['Model']['field'] = true;
  5428. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5429. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5430. $this->assertHtml($expected, $result);
  5431. unset($this->Form->request->data['Model']['field']);
  5432. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5433. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5434. $this->assertHtml($expected, $result);
  5435. $Articles = TableRegistry::get('Articles');
  5436. $Articles->getSchema()->addColumn(
  5437. 'published',
  5438. ['type' => 'boolean', 'null' => false, 'default' => true]
  5439. );
  5440. $this->Form->create($Articles->newEntity());
  5441. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5442. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5443. $this->assertHtml($expected, $result);
  5444. }
  5445. /**
  5446. * testCheckboxCheckedAndError method
  5447. *
  5448. * Test checkbox being checked or having errors.
  5449. *
  5450. * @return void
  5451. */
  5452. public function testCheckboxCheckedAndError()
  5453. {
  5454. $this->article['errors'] = [
  5455. 'published' => true
  5456. ];
  5457. $this->Form->request->data['published'] = 'myvalue';
  5458. $this->Form->create($this->article);
  5459. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5460. $expected = [
  5461. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5462. ['input' => [
  5463. 'type' => 'checkbox',
  5464. 'name' => 'published',
  5465. 'value' => 'myvalue',
  5466. 'id' => 'theID',
  5467. 'checked' => 'checked',
  5468. 'class' => 'form-error'
  5469. ]]
  5470. ];
  5471. $this->assertHtml($expected, $result);
  5472. $this->Form->request->data['published'] = '';
  5473. $result = $this->Form->checkbox('published');
  5474. $expected = [
  5475. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5476. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5477. ];
  5478. $this->assertHtml($expected, $result);
  5479. }
  5480. /**
  5481. * testCheckboxCustomNameAttribute method
  5482. *
  5483. * Test checkbox() with a custom name attribute.
  5484. *
  5485. * @return void
  5486. */
  5487. public function testCheckboxCustomNameAttribute()
  5488. {
  5489. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5490. $expected = [
  5491. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5492. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5493. ];
  5494. $this->assertHtml($expected, $result);
  5495. }
  5496. /**
  5497. * testCheckboxHiddenField method
  5498. *
  5499. * Test that the hidden input for checkboxes can be omitted or set to a
  5500. * specific value.
  5501. *
  5502. * @return void
  5503. */
  5504. public function testCheckboxHiddenField()
  5505. {
  5506. $result = $this->Form->checkbox('UserForm.something', [
  5507. 'hiddenField' => false
  5508. ]);
  5509. $expected = [
  5510. 'input' => [
  5511. 'type' => 'checkbox',
  5512. 'name' => 'UserForm[something]',
  5513. 'value' => '1'
  5514. ],
  5515. ];
  5516. $this->assertHtml($expected, $result);
  5517. $result = $this->Form->checkbox('UserForm.something', [
  5518. 'value' => 'Y',
  5519. 'hiddenField' => 'N',
  5520. ]);
  5521. $expected = [
  5522. ['input' => [
  5523. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5524. 'value' => 'N'
  5525. ]],
  5526. ['input' => [
  5527. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5528. 'value' => 'Y'
  5529. ]],
  5530. ];
  5531. $this->assertHtml($expected, $result);
  5532. }
  5533. /**
  5534. * testTime method
  5535. *
  5536. * Test the time type.
  5537. *
  5538. * @return void
  5539. */
  5540. public function testTime()
  5541. {
  5542. $result = $this->Form->time('start_time', [
  5543. 'timeFormat' => 12,
  5544. 'interval' => 5,
  5545. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5546. ]);
  5547. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5548. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5549. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5550. $this->assertNotContains('year', $result);
  5551. $this->assertNotContains('month', $result);
  5552. $this->assertNotContains('day', $result);
  5553. $result = $this->Form->time('start_time', [
  5554. 'timeFormat' => 12,
  5555. 'interval' => 5,
  5556. 'value' => '2014-03-08 16:30:00'
  5557. ]);
  5558. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5559. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5560. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5561. $this->assertNotContains('year', $result);
  5562. $this->assertNotContains('month', $result);
  5563. $this->assertNotContains('day', $result);
  5564. }
  5565. /**
  5566. * testTimeFormat24NoMeridian method
  5567. *
  5568. * Ensure that timeFormat=24 has no merdian.
  5569. *
  5570. * @return void.
  5571. */
  5572. public function testTimeFormat24NoMeridian()
  5573. {
  5574. $result = $this->Form->time('start_time', [
  5575. 'timeFormat' => 24,
  5576. 'interval' => 5,
  5577. 'value' => '2014-03-08 16:30:00'
  5578. ]);
  5579. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5580. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5581. $this->assertNotContains('meridian', $result);
  5582. $this->assertNotContains('pm', $result);
  5583. $this->assertNotContains('year', $result);
  5584. $this->assertNotContains('month', $result);
  5585. $this->assertNotContains('day', $result);
  5586. }
  5587. /**
  5588. * testDate method
  5589. *
  5590. * Test the date type.
  5591. *
  5592. * @return void
  5593. */
  5594. public function testDate()
  5595. {
  5596. $result = $this->Form->date('start_day', [
  5597. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5598. ]);
  5599. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5600. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5601. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5602. $this->assertNotContains('hour', $result);
  5603. $this->assertNotContains('minute', $result);
  5604. $this->assertNotContains('second', $result);
  5605. $this->assertNotContains('meridian', $result);
  5606. }
  5607. /**
  5608. * testDateTime method
  5609. *
  5610. * Test generation of date/time select elements.
  5611. *
  5612. * @return void
  5613. */
  5614. public function testDateTime()
  5615. {
  5616. extract($this->dateRegex);
  5617. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5618. $now = strtotime('now');
  5619. $expected = [
  5620. ['select' => ['name' => 'Contact[date][year]']],
  5621. ['option' => ['value' => '']],
  5622. '/option',
  5623. $yearsRegex,
  5624. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5625. date('Y', $now),
  5626. '/option',
  5627. '*/select',
  5628. ['select' => ['name' => 'Contact[date][month]']],
  5629. ['option' => ['value' => '']],
  5630. '/option',
  5631. $monthsRegex,
  5632. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5633. date('F', $now),
  5634. '/option',
  5635. '*/select',
  5636. ['select' => ['name' => 'Contact[date][day]']],
  5637. ['option' => ['value' => '']],
  5638. '/option',
  5639. $daysRegex,
  5640. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5641. date('j', $now),
  5642. '/option',
  5643. '*/select',
  5644. ['select' => ['name' => 'Contact[date][hour]']],
  5645. ['option' => ['value' => '']],
  5646. '/option',
  5647. $hoursRegex,
  5648. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5649. date('G', $now),
  5650. '/option',
  5651. '*/select',
  5652. ['select' => ['name' => 'Contact[date][minute]']],
  5653. ['option' => ['value' => '']],
  5654. '/option',
  5655. $minutesRegex,
  5656. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5657. date('i', $now),
  5658. '/option',
  5659. '*/select',
  5660. ];
  5661. $this->assertHtml($expected, $result);
  5662. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5663. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5664. $now = strtotime('now');
  5665. $expected = [
  5666. ['select' => ['name' => 'Contact[date][year]']],
  5667. $yearsRegex,
  5668. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5669. date('Y', $now),
  5670. '/option',
  5671. '*/select',
  5672. ['select' => ['name' => 'Contact[date][month]']],
  5673. $monthsRegex,
  5674. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5675. date('F', $now),
  5676. '/option',
  5677. '*/select',
  5678. ['select' => ['name' => 'Contact[date][day]']],
  5679. $daysRegex,
  5680. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5681. date('j', $now),
  5682. '/option',
  5683. '*/select',
  5684. ['select' => ['name' => 'Contact[date][hour]']],
  5685. $hoursRegex,
  5686. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5687. date('G', $now),
  5688. '/option',
  5689. '*/select',
  5690. ['select' => ['name' => 'Contact[date][minute]']],
  5691. $minutesRegex,
  5692. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5693. date('i', $now),
  5694. '/option',
  5695. '*/select',
  5696. ];
  5697. $this->assertHtml($expected, $result);
  5698. }
  5699. /**
  5700. * testDateTimeSecured method
  5701. *
  5702. * Test that datetime fields are added to protected fields list.
  5703. *
  5704. * @return void
  5705. */
  5706. public function testDateTimeSecured()
  5707. {
  5708. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5709. $this->Form->dateTime('Contact.date');
  5710. $expected = [
  5711. 'Contact.date.year',
  5712. 'Contact.date.month',
  5713. 'Contact.date.day',
  5714. 'Contact.date.hour',
  5715. 'Contact.date.minute',
  5716. ];
  5717. $this->assertEquals($expected, $this->Form->fields);
  5718. $this->Form->fields = [];
  5719. $this->Form->date('Contact.published');
  5720. $expected = [
  5721. 'Contact.published.year',
  5722. 'Contact.published.month',
  5723. 'Contact.published.day',
  5724. ];
  5725. $this->assertEquals($expected, $this->Form->fields);
  5726. }
  5727. /**
  5728. * testDateTimeSecuredDisabled method
  5729. *
  5730. * Test that datetime fields are added to protected fields list.
  5731. *
  5732. * @return void
  5733. */
  5734. public function testDateTimeSecuredDisabled()
  5735. {
  5736. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5737. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5738. $expected = [];
  5739. $this->assertEquals($expected, $this->Form->fields);
  5740. $this->Form->fields = [];
  5741. $this->Form->date('Contact.published', ['secure' => false]);
  5742. $expected = [];
  5743. $this->assertEquals($expected, $this->Form->fields);
  5744. }
  5745. /**
  5746. * testDatetimeEmpty method
  5747. *
  5748. * Test empty defaulting to true for datetime.
  5749. *
  5750. * @return void
  5751. */
  5752. public function testDatetimeEmpty()
  5753. {
  5754. extract($this->dateRegex);
  5755. $result = $this->Form->dateTime('Contact.date', [
  5756. 'timeFormat' => 12,
  5757. 'empty' => true,
  5758. 'default' => true
  5759. ]);
  5760. $expected = [
  5761. ['select' => ['name' => 'Contact[date][year]']],
  5762. $yearsRegex,
  5763. ['option' => ['value' => '']],
  5764. '/option',
  5765. '*/select',
  5766. ['select' => ['name' => 'Contact[date][month]']],
  5767. $monthsRegex,
  5768. ['option' => ['value' => '']],
  5769. '/option',
  5770. '*/select',
  5771. ['select' => ['name' => 'Contact[date][day]']],
  5772. $daysRegex,
  5773. ['option' => ['value' => '']],
  5774. '/option',
  5775. '*/select',
  5776. ['select' => ['name' => 'Contact[date][hour]']],
  5777. $hoursRegex,
  5778. ['option' => ['value' => '']],
  5779. '/option',
  5780. '*/select',
  5781. ['select' => ['name' => 'Contact[date][minute]']],
  5782. $minutesRegex,
  5783. ['option' => ['value' => '']],
  5784. '/option',
  5785. '*/select',
  5786. ['select' => ['name' => 'Contact[date][meridian]']],
  5787. $meridianRegex,
  5788. ['option' => ['value' => '']],
  5789. '/option',
  5790. '*/select'
  5791. ];
  5792. $this->assertHtml($expected, $result);
  5793. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5794. }
  5795. /**
  5796. * testDatetimeMinuteInterval method
  5797. *
  5798. * Test datetime with interval option.
  5799. *
  5800. * @return void
  5801. */
  5802. public function testDatetimeMinuteInterval()
  5803. {
  5804. extract($this->dateRegex);
  5805. $result = $this->Form->dateTime('Contact.date', [
  5806. 'interval' => 5,
  5807. 'value' => ''
  5808. ]);
  5809. $expected = [
  5810. ['select' => ['name' => 'Contact[date][year]']],
  5811. $yearsRegex,
  5812. ['option' => ['selected' => 'selected', 'value' => '']],
  5813. '/option',
  5814. '*/select',
  5815. ['select' => ['name' => 'Contact[date][month]']],
  5816. $monthsRegex,
  5817. ['option' => ['selected' => 'selected', 'value' => '']],
  5818. '/option',
  5819. '*/select',
  5820. ['select' => ['name' => 'Contact[date][day]']],
  5821. $daysRegex,
  5822. ['option' => ['selected' => 'selected', 'value' => '']],
  5823. '/option',
  5824. '*/select',
  5825. ['select' => ['name' => 'Contact[date][hour]']],
  5826. $hoursRegex,
  5827. ['option' => ['selected' => 'selected', 'value' => '']],
  5828. '/option',
  5829. '*/select',
  5830. ['select' => ['name' => 'Contact[date][minute]']],
  5831. $minutesRegex,
  5832. ['option' => ['selected' => 'selected', 'value' => '']],
  5833. '/option',
  5834. ['option' => ['value' => '00']],
  5835. '00',
  5836. '/option',
  5837. ['option' => ['value' => '05']],
  5838. '05',
  5839. '/option',
  5840. ['option' => ['value' => '10']],
  5841. '10',
  5842. '/option',
  5843. '*/select',
  5844. ];
  5845. $this->assertHtml($expected, $result);
  5846. }
  5847. /**
  5848. * testDateTimeRounding method
  5849. *
  5850. * Test dateTime with rounding.
  5851. *
  5852. * @return void
  5853. */
  5854. public function testDateTimeRounding()
  5855. {
  5856. $this->Form->request->data['Contact'] = [
  5857. 'date' => [
  5858. 'day' => '13',
  5859. 'month' => '12',
  5860. 'year' => '2010',
  5861. 'hour' => '04',
  5862. 'minute' => '19',
  5863. 'meridian' => 'AM'
  5864. ]
  5865. ];
  5866. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5867. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5868. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5869. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5870. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5871. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5872. }
  5873. /**
  5874. * testDatetimeWithDefault method
  5875. *
  5876. * Test that datetime() and default values work.
  5877. *
  5878. * @return void
  5879. */
  5880. public function testDatetimeWithDefault()
  5881. {
  5882. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5883. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5884. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5885. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5886. $result = $this->Form->dateTime('Contact.updated', [
  5887. 'default' => '2009-06-01 11:15:30'
  5888. ]);
  5889. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5890. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5891. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5892. }
  5893. /**
  5894. * testDateTimeAllZeros method
  5895. *
  5896. * Test datetime() with all zeros.
  5897. *
  5898. * @return void
  5899. */
  5900. public function testDateTimeAllZeros()
  5901. {
  5902. $result = $this->Form->dateTime('Contact.date', [
  5903. 'timeFormat' => false,
  5904. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5905. 'value' => '0000-00-00'
  5906. ]);
  5907. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5908. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5909. }
  5910. /**
  5911. * testDateTimeEmptyAsArray method
  5912. *
  5913. * @return void
  5914. */
  5915. public function testDateTimeEmptyAsArray()
  5916. {
  5917. $result = $this->Form->dateTime('Contact.date', [
  5918. 'empty' => [
  5919. 'day' => 'DAY',
  5920. 'month' => 'MONTH',
  5921. 'year' => 'YEAR',
  5922. 'hour' => 'HOUR',
  5923. 'minute' => 'MINUTE',
  5924. 'meridian' => false
  5925. ],
  5926. 'default' => true
  5927. ]);
  5928. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5929. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5930. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5931. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5932. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5933. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5934. $result = $this->Form->dateTime('Contact.date', [
  5935. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5936. 'default' => true
  5937. ]);
  5938. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5939. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5940. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5941. }
  5942. /**
  5943. * testFormDateTimeMulti method
  5944. *
  5945. * Test multiple datetime element generation.
  5946. *
  5947. * @return void
  5948. */
  5949. public function testFormDateTimeMulti()
  5950. {
  5951. extract($this->dateRegex);
  5952. $result = $this->Form->dateTime('Contact.1.updated');
  5953. $this->assertContains('Contact[1][updated][month]', $result);
  5954. $this->assertContains('Contact[1][updated][day]', $result);
  5955. $this->assertContains('Contact[1][updated][year]', $result);
  5956. $this->assertContains('Contact[1][updated][hour]', $result);
  5957. $this->assertContains('Contact[1][updated][minute]', $result);
  5958. }
  5959. /**
  5960. * testDateTimeLabelIdMatchesFirstControl method
  5961. *
  5962. * When changing the date format, the label should always focus the first select box when
  5963. * clicked.
  5964. *
  5965. * @return void
  5966. */
  5967. public function testDateTimeLabelIdMatchesFirstControl()
  5968. {
  5969. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5970. $this->assertContains('<label>Date</label>', $result);
  5971. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5972. $this->assertContains('<label>Date</label>', $result);
  5973. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5974. $this->assertContains('<label>Date</label>', $result);
  5975. }
  5976. /**
  5977. * testDateTimeSecondOptions method
  5978. *
  5979. * Test datetime second=true.
  5980. *
  5981. * @return void
  5982. */
  5983. public function testDateTimeSecondOptions()
  5984. {
  5985. $result = $this->Form->dateTime('updated', ['second' => true]);
  5986. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5987. $result = $this->Form->dateTime('updated', ['second' => []]);
  5988. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5989. $result = $this->Form->dateTime('updated', ['second' => null]);
  5990. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5991. $result = $this->Form->dateTime('updated', ['second' => false]);
  5992. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5993. }
  5994. /**
  5995. * testMonth method
  5996. *
  5997. * Test generation of a month input.
  5998. *
  5999. * @return void
  6000. */
  6001. public function testMonth()
  6002. {
  6003. $result = $this->Form->month('Model.field', ['value' => '']);
  6004. $expected = [
  6005. ['select' => ['name' => 'Model[field][month]']],
  6006. ['option' => ['value' => '', 'selected' => 'selected']],
  6007. '/option',
  6008. ['option' => ['value' => '01']],
  6009. date('F', strtotime('2008-01-01 00:00:00')),
  6010. '/option',
  6011. ['option' => ['value' => '02']],
  6012. date('F', strtotime('2008-02-01 00:00:00')),
  6013. '/option',
  6014. '*/select',
  6015. ];
  6016. $this->assertHtml($expected, $result);
  6017. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6018. $expected = [
  6019. ['select' => ['name' => 'Model[field][month]']],
  6020. ['option' => ['selected' => 'selected', 'value' => '']],
  6021. '/option',
  6022. ['option' => ['value' => '01']],
  6023. date('F', strtotime('2008-01-01 00:00:00')),
  6024. '/option',
  6025. ['option' => ['value' => '02']],
  6026. date('F', strtotime('2008-02-01 00:00:00')),
  6027. '/option',
  6028. '*/select',
  6029. ];
  6030. $this->assertHtml($expected, $result);
  6031. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6032. $expected = [
  6033. ['select' => ['name' => 'Model[field][month]']],
  6034. ['option' => ['selected' => 'selected', 'value' => '']],
  6035. '/option',
  6036. ['option' => ['value' => '01']],
  6037. '1',
  6038. '/option',
  6039. ['option' => ['value' => '02']],
  6040. '2',
  6041. '/option',
  6042. '*/select',
  6043. ];
  6044. $this->assertHtml($expected, $result);
  6045. $monthNames = [
  6046. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6047. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6048. ];
  6049. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6050. $expected = [
  6051. ['select' => ['name' => 'Model[field][month]']],
  6052. ['option' => ['value' => '']],
  6053. '/option',
  6054. ['option' => ['value' => '01', 'selected' => 'selected']],
  6055. 'Jan',
  6056. '/option',
  6057. ['option' => ['value' => '02']],
  6058. 'Feb',
  6059. '/option',
  6060. '*/select',
  6061. ];
  6062. $this->assertHtml($expected, $result);
  6063. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6064. $result = $this->Form->month('Project.release');
  6065. $expected = [
  6066. ['select' => ['name' => 'Project[release][month]']],
  6067. ['option' => ['value' => '']],
  6068. '/option',
  6069. ['option' => ['value' => '01']],
  6070. 'January',
  6071. '/option',
  6072. ['option' => ['value' => '02', 'selected' => 'selected']],
  6073. 'February',
  6074. '/option',
  6075. '*/select',
  6076. ];
  6077. $this->assertHtml($expected, $result);
  6078. $result = $this->Form->month('Contact.published', [
  6079. 'empty' => 'Published on',
  6080. ]);
  6081. $this->assertContains('Published on', $result);
  6082. }
  6083. /**
  6084. * testDay method
  6085. *
  6086. * Test generation of a day input.
  6087. *
  6088. * @return void
  6089. */
  6090. public function testDay()
  6091. {
  6092. extract($this->dateRegex);
  6093. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6094. $expected = [
  6095. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6096. ['option' => ['selected' => 'selected', 'value' => '']],
  6097. '/option',
  6098. ['option' => ['value' => '01']],
  6099. '1',
  6100. '/option',
  6101. ['option' => ['value' => '02']],
  6102. '2',
  6103. '/option',
  6104. $daysRegex,
  6105. '/select',
  6106. ];
  6107. $this->assertHtml($expected, $result);
  6108. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6109. $result = $this->Form->day('Model.field');
  6110. $expected = [
  6111. ['select' => ['name' => 'Model[field][day]']],
  6112. ['option' => ['value' => '']],
  6113. '/option',
  6114. ['option' => ['value' => '01']],
  6115. '1',
  6116. '/option',
  6117. ['option' => ['value' => '02']],
  6118. '2',
  6119. '/option',
  6120. $daysRegex,
  6121. ['option' => ['value' => '10', 'selected' => 'selected']],
  6122. '10',
  6123. '/option',
  6124. $daysRegex,
  6125. '/select',
  6126. ];
  6127. $this->assertHtml($expected, $result);
  6128. $this->Form->request->data['Model']['field'] = '';
  6129. $result = $this->Form->day('Model.field', ['value' => '10']);
  6130. $expected = [
  6131. ['select' => ['name' => 'Model[field][day]']],
  6132. ['option' => ['value' => '']],
  6133. '/option',
  6134. ['option' => ['value' => '01']],
  6135. '1',
  6136. '/option',
  6137. ['option' => ['value' => '02']],
  6138. '2',
  6139. '/option',
  6140. $daysRegex,
  6141. ['option' => ['value' => '10', 'selected' => 'selected']],
  6142. '10',
  6143. '/option',
  6144. $daysRegex,
  6145. '/select',
  6146. ];
  6147. $this->assertHtml($expected, $result);
  6148. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6149. $result = $this->Form->day('Project.release');
  6150. $expected = [
  6151. ['select' => ['name' => 'Project[release][day]']],
  6152. ['option' => ['value' => '']],
  6153. '/option',
  6154. ['option' => ['value' => '01']],
  6155. '1',
  6156. '/option',
  6157. ['option' => ['value' => '02']],
  6158. '2',
  6159. '/option',
  6160. $daysRegex,
  6161. ['option' => ['value' => '10', 'selected' => 'selected']],
  6162. '10',
  6163. '/option',
  6164. $daysRegex,
  6165. '/select',
  6166. ];
  6167. $this->assertHtml($expected, $result);
  6168. $result = $this->Form->day('Contact.published', [
  6169. 'empty' => 'Published on',
  6170. ]);
  6171. $this->assertContains('Published on', $result);
  6172. }
  6173. /**
  6174. * testMinute method
  6175. *
  6176. * Test generation of a minute input.
  6177. *
  6178. * @return void
  6179. */
  6180. public function testMinute()
  6181. {
  6182. extract($this->dateRegex);
  6183. $result = $this->Form->minute('Model.field', ['value' => '']);
  6184. $expected = [
  6185. ['select' => ['name' => 'Model[field][minute]']],
  6186. ['option' => ['selected' => 'selected', 'value' => '']],
  6187. '/option',
  6188. ['option' => ['value' => '00']],
  6189. '00',
  6190. '/option',
  6191. ['option' => ['value' => '01']],
  6192. '01',
  6193. '/option',
  6194. ['option' => ['value' => '02']],
  6195. '02',
  6196. '/option',
  6197. $minutesRegex,
  6198. '/select',
  6199. ];
  6200. $this->assertHtml($expected, $result);
  6201. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6202. $result = $this->Form->minute('Model.field');
  6203. $expected = [
  6204. ['select' => ['name' => 'Model[field][minute]']],
  6205. ['option' => ['value' => '']],
  6206. '/option',
  6207. ['option' => ['value' => '00']],
  6208. '00',
  6209. '/option',
  6210. ['option' => ['value' => '01']],
  6211. '01',
  6212. '/option',
  6213. ['option' => ['value' => '02']],
  6214. '02',
  6215. '/option',
  6216. $minutesRegex,
  6217. ['option' => ['value' => '12', 'selected' => 'selected']],
  6218. '12',
  6219. '/option',
  6220. $minutesRegex,
  6221. '/select',
  6222. ];
  6223. $this->assertHtml($expected, $result);
  6224. $this->Form->request->data['Model']['field'] = '';
  6225. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6226. $expected = [
  6227. ['select' => ['name' => 'Model[field][minute]']],
  6228. ['option' => ['selected' => 'selected', 'value' => '']],
  6229. '/option',
  6230. ['option' => ['value' => '00']],
  6231. '00',
  6232. '/option',
  6233. ['option' => ['value' => '05']],
  6234. '05',
  6235. '/option',
  6236. ['option' => ['value' => '10']],
  6237. '10',
  6238. '/option',
  6239. $minutesRegex,
  6240. '/select',
  6241. ];
  6242. $this->assertHtml($expected, $result);
  6243. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6244. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6245. $expected = [
  6246. ['select' => ['name' => 'Model[field][minute]']],
  6247. ['option' => ['value' => '']],
  6248. '/option',
  6249. ['option' => ['value' => '00']],
  6250. '00',
  6251. '/option',
  6252. ['option' => ['value' => '05']],
  6253. '05',
  6254. '/option',
  6255. ['option' => ['value' => '10', 'selected' => 'selected']],
  6256. '10',
  6257. '/option',
  6258. $minutesRegex,
  6259. '/select',
  6260. ];
  6261. $this->assertHtml($expected, $result);
  6262. }
  6263. /**
  6264. * testMeridian method
  6265. *
  6266. * Test generating an input for the meridian.
  6267. *
  6268. * @return void
  6269. */
  6270. public function testMeridian()
  6271. {
  6272. extract($this->dateRegex);
  6273. $now = new \DateTime();
  6274. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6275. $expected = [
  6276. ['select' => ['name' => 'Model[field][meridian]']],
  6277. ['option' => ['value' => '']],
  6278. '/option',
  6279. $meridianRegex,
  6280. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6281. $now->format('a'),
  6282. '/option',
  6283. '*/select'
  6284. ];
  6285. $this->assertHtml($expected, $result);
  6286. }
  6287. /**
  6288. * testHour method
  6289. *
  6290. * Test generation of an hour input.
  6291. *
  6292. * @return void
  6293. */
  6294. public function testHour()
  6295. {
  6296. extract($this->dateRegex);
  6297. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6298. $expected = [
  6299. ['select' => ['name' => 'Model[field][hour]']],
  6300. ['option' => ['selected' => 'selected', 'value' => '']],
  6301. '/option',
  6302. ['option' => ['value' => '01']],
  6303. '1',
  6304. '/option',
  6305. ['option' => ['value' => '02']],
  6306. '2',
  6307. '/option',
  6308. $hoursRegex,
  6309. '/select',
  6310. ];
  6311. $this->assertHtml($expected, $result);
  6312. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6313. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6314. $expected = [
  6315. ['select' => ['name' => 'Model[field][hour]']],
  6316. ['option' => ['value' => '']],
  6317. '/option',
  6318. ['option' => ['value' => '01']],
  6319. '1',
  6320. '/option',
  6321. ['option' => ['value' => '02']],
  6322. '2',
  6323. '/option',
  6324. $hoursRegex,
  6325. ['option' => ['value' => '12', 'selected' => 'selected']],
  6326. '12',
  6327. '/option',
  6328. '/select',
  6329. ];
  6330. $this->assertHtml($expected, $result);
  6331. $this->Form->request->data['Model']['field'] = '';
  6332. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6333. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6334. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6335. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6336. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6337. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6338. $expected = [
  6339. ['select' => ['name' => 'Model[field][hour]']],
  6340. ['option' => ['value' => '']],
  6341. '/option',
  6342. ['option' => ['value' => '00', 'selected' => 'selected']],
  6343. '0',
  6344. '/option',
  6345. ['option' => ['value' => '01']],
  6346. '1',
  6347. '/option',
  6348. ['option' => ['value' => '02']],
  6349. '2',
  6350. '/option',
  6351. $hoursRegex,
  6352. '/select',
  6353. ];
  6354. $this->assertHtml($expected, $result);
  6355. unset($this->Form->request->data['Model']['field']);
  6356. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6357. $thisHour = date('H');
  6358. $optValue = date('G');
  6359. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6360. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6361. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6362. $expected = [
  6363. ['select' => ['name' => 'Model[field][hour]']],
  6364. ['option' => ['value' => '']],
  6365. '/option',
  6366. ['option' => ['value' => '00']],
  6367. '0',
  6368. '/option',
  6369. ['option' => ['value' => '01', 'selected' => 'selected']],
  6370. '1',
  6371. '/option',
  6372. ['option' => ['value' => '02']],
  6373. '2',
  6374. '/option',
  6375. $hoursRegex,
  6376. '/select',
  6377. ];
  6378. $this->assertHtml($expected, $result);
  6379. }
  6380. /**
  6381. * testYear method
  6382. *
  6383. * Test generation of a year input.
  6384. *
  6385. * @return void
  6386. */
  6387. public function testYear()
  6388. {
  6389. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6390. $expected = [
  6391. ['select' => ['name' => 'Model[field][year]']],
  6392. ['option' => ['selected' => 'selected', 'value' => '']],
  6393. '/option',
  6394. ['option' => ['value' => '2007']],
  6395. '2007',
  6396. '/option',
  6397. ['option' => ['value' => '2006']],
  6398. '2006',
  6399. '/option',
  6400. '/select',
  6401. ];
  6402. $this->assertHtml($expected, $result);
  6403. $result = $this->Form->year('Model.field', [
  6404. 'value' => '',
  6405. 'minYear' => 2006,
  6406. 'maxYear' => 2007,
  6407. 'orderYear' => 'asc'
  6408. ]);
  6409. $expected = [
  6410. ['select' => ['name' => 'Model[field][year]']],
  6411. ['option' => ['selected' => 'selected', 'value' => '']],
  6412. '/option',
  6413. ['option' => ['value' => '2006']],
  6414. '2006',
  6415. '/option',
  6416. ['option' => ['value' => '2007']],
  6417. '2007',
  6418. '/option',
  6419. '/select',
  6420. ];
  6421. $this->assertHtml($expected, $result);
  6422. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6423. $result = $this->Form->year('Contact.published', [
  6424. 'empty' => false,
  6425. 'minYear' => 2006,
  6426. 'maxYear' => 2007,
  6427. ]);
  6428. $expected = [
  6429. ['select' => ['name' => 'Contact[published][year]']],
  6430. ['option' => ['value' => '2007']],
  6431. '2007',
  6432. '/option',
  6433. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6434. '2006',
  6435. '/option',
  6436. '/select',
  6437. ];
  6438. $this->assertHtml($expected, $result);
  6439. $result = $this->Form->year('Contact.published', [
  6440. 'empty' => 'Published on',
  6441. ]);
  6442. $this->assertContains('Published on', $result);
  6443. }
  6444. /**
  6445. * testControlDatetimePreEpoch method
  6446. *
  6447. * Test minYear being prior to the unix epoch.
  6448. *
  6449. * @return void
  6450. */
  6451. public function testControlDatetimePreEpoch()
  6452. {
  6453. $start = date('Y') - 80;
  6454. $end = date('Y') - 18;
  6455. $result = $this->Form->control('birth_year', [
  6456. 'type' => 'date',
  6457. 'label' => 'Birth Year',
  6458. 'minYear' => $start,
  6459. 'maxYear' => $end,
  6460. 'month' => false,
  6461. 'day' => false,
  6462. ]);
  6463. $this->assertContains('value="' . $start . '">' . $start, $result);
  6464. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6465. $this->assertNotContains('value="00">00', $result);
  6466. }
  6467. /**
  6468. * testYearAutoExpandRange method
  6469. *
  6470. * @return void
  6471. */
  6472. public function testYearAutoExpandRange()
  6473. {
  6474. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6475. $result = $this->Form->year('User.birthday');
  6476. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6477. $result = $matches[1];
  6478. $expected = range(date('Y') + 5, 1930);
  6479. $this->assertEquals($expected, $result);
  6480. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6481. $result = $this->Form->year('Project.release');
  6482. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6483. $result = $matches[1];
  6484. $expected = range(2050, date('Y') - 5);
  6485. $this->assertEquals($expected, $result);
  6486. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6487. $result = $this->Form->year('Project.release', [
  6488. 'minYear' => 1890,
  6489. 'maxYear' => 1900
  6490. ]);
  6491. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6492. $result = $matches[1];
  6493. $expected = range(1900, 1881);
  6494. $this->assertEquals($expected, $result);
  6495. }
  6496. /**
  6497. * testControlDate method
  6498. *
  6499. * Test that control() accepts the type of date and passes options in.
  6500. *
  6501. * @return void
  6502. */
  6503. public function testControlDate()
  6504. {
  6505. $this->Form->request->data = [
  6506. 'month_year' => ['month' => date('m')],
  6507. ];
  6508. $this->Form->create($this->article);
  6509. $result = $this->Form->control('month_year', [
  6510. 'label' => false,
  6511. 'type' => 'date',
  6512. 'minYear' => 2006,
  6513. 'maxYear' => 2008
  6514. ]);
  6515. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6516. $this->assertNotContains('value="2008" selected="selected"', $result);
  6517. }
  6518. /**
  6519. * testControlLabelFalse method
  6520. *
  6521. * Test the label option being set to false.
  6522. *
  6523. * @return void
  6524. */
  6525. public function testControlLabelFalse()
  6526. {
  6527. $this->Form->create($this->article);
  6528. $result = $this->Form->control('title', ['label' => false]);
  6529. $expected = [
  6530. 'div' => ['class' => 'input text required'],
  6531. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6532. '/div'
  6533. ];
  6534. $this->assertHtml($expected, $result);
  6535. }
  6536. /**
  6537. * testControlDateMaxYear method
  6538. *
  6539. * Let's say we want to only allow users born from 2006 to 2008 to register
  6540. * This being the first signup page, we still don't have any data.
  6541. *
  6542. * @return void
  6543. */
  6544. public function testControlDateMaxYear()
  6545. {
  6546. $this->Form->request->data = [];
  6547. $this->Form->create($this->article);
  6548. $result = $this->Form->control('birthday', [
  6549. 'label' => false,
  6550. 'type' => 'date',
  6551. 'minYear' => 2006,
  6552. 'maxYear' => 2008,
  6553. 'default' => true
  6554. ]);
  6555. $this->assertContains('value="2008" selected="selected"', $result);
  6556. $this->assertContains('value="2006"', $result);
  6557. $this->assertNotContains('value="2005"', $result);
  6558. $this->assertNotContains('value="2009"', $result);
  6559. }
  6560. /**
  6561. * testTextArea method
  6562. *
  6563. * Test generation of a textarea input.
  6564. *
  6565. * @return void
  6566. */
  6567. public function testTextArea()
  6568. {
  6569. $this->Form->request->data = ['field' => 'some test data'];
  6570. $result = $this->Form->textarea('field');
  6571. $expected = [
  6572. 'textarea' => ['name' => 'field', 'rows' => 5],
  6573. 'some test data',
  6574. '/textarea',
  6575. ];
  6576. $this->assertHtml($expected, $result);
  6577. $result = $this->Form->textarea('user.bio');
  6578. $expected = [
  6579. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6580. '/textarea',
  6581. ];
  6582. $this->assertHtml($expected, $result);
  6583. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6584. $result = $this->Form->textarea('field');
  6585. $expected = [
  6586. 'textarea' => ['name' => 'field', 'rows' => 5],
  6587. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6588. '/textarea',
  6589. ];
  6590. $this->assertHtml($expected, $result);
  6591. $this->Form->request->data = [
  6592. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6593. ];
  6594. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6595. $expected = [
  6596. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6597. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6598. '/textarea',
  6599. ];
  6600. $this->assertHtml($expected, $result);
  6601. $result = $this->Form->textarea('0.OtherModel.field');
  6602. $expected = [
  6603. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6604. '/textarea'
  6605. ];
  6606. $this->assertHtml($expected, $result);
  6607. }
  6608. /**
  6609. * testTextAreaWithStupidCharacters method
  6610. *
  6611. * Test text area with non-ascii characters.
  6612. *
  6613. * @return void
  6614. */
  6615. public function testTextAreaWithStupidCharacters()
  6616. {
  6617. $result = $this->Form->textarea('Post.content', [
  6618. 'value' => 'GREAT®',
  6619. 'rows' => '15',
  6620. 'cols' => '75'
  6621. ]);
  6622. $expected = [
  6623. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6624. 'GREAT®',
  6625. '/textarea',
  6626. ];
  6627. $this->assertHtml($expected, $result);
  6628. }
  6629. /**
  6630. * testTextAreaMaxLength method
  6631. *
  6632. * Test textareas maxlength read from schema.
  6633. *
  6634. * @return void
  6635. */
  6636. public function testTextAreaMaxLength()
  6637. {
  6638. $this->Form->create([
  6639. 'schema' => [
  6640. 'stuff' => ['type' => 'string', 'length' => 10],
  6641. ]
  6642. ]);
  6643. $result = $this->Form->control('other', ['type' => 'textarea']);
  6644. $expected = [
  6645. 'div' => ['class' => 'input textarea'],
  6646. 'label' => ['for' => 'other'],
  6647. 'Other',
  6648. '/label',
  6649. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6650. '/textarea',
  6651. '/div'
  6652. ];
  6653. $this->assertHtml($expected, $result);
  6654. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6655. $expected = [
  6656. 'div' => ['class' => 'input textarea'],
  6657. 'label' => ['for' => 'stuff'],
  6658. 'Stuff',
  6659. '/label',
  6660. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6661. '/textarea',
  6662. '/div'
  6663. ];
  6664. $this->assertHtml($expected, $result);
  6665. }
  6666. /**
  6667. * testHiddenField method
  6668. *
  6669. * Test generation of a hidden input.
  6670. *
  6671. * @return void
  6672. */
  6673. public function testHiddenField()
  6674. {
  6675. $this->article['errors'] = [
  6676. 'field' => true
  6677. ];
  6678. $this->Form->request->data['field'] = 'test';
  6679. $this->Form->create($this->article);
  6680. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6681. $expected = [
  6682. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6683. $this->assertHtml($expected, $result);
  6684. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6685. $expected = [
  6686. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6687. ];
  6688. $this->assertHtml($expected, $result);
  6689. }
  6690. /**
  6691. * testFileUploadField method
  6692. *
  6693. * Test generation of a file upload input.
  6694. *
  6695. * @return void
  6696. */
  6697. public function testFileUploadField()
  6698. {
  6699. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6700. $result = $this->Form->file('Model.upload');
  6701. $this->assertHtml($expected, $result);
  6702. $this->Form->request = $this->Form->request->withData('Model.upload', [
  6703. 'name' => '', 'type' => '', 'tmp_name' => '',
  6704. 'error' => 4, 'size' => 0
  6705. ]);
  6706. $result = $this->Form->file('Model.upload');
  6707. $this->assertHtml($expected, $result);
  6708. $this->Form->request = $this->Form->request->withData('Model.upload', 'no data should be set in value');
  6709. $result = $this->Form->file('Model.upload');
  6710. $this->assertHtml($expected, $result);
  6711. }
  6712. /**
  6713. * testFileUploadOnOtherModel method
  6714. *
  6715. * Test File upload input on a model not used in create().
  6716. *
  6717. * @return void
  6718. */
  6719. public function testFileUploadOnOtherModel()
  6720. {
  6721. $this->Form->create($this->article, ['type' => 'file']);
  6722. $result = $this->Form->file('ValidateProfile.city');
  6723. $expected = [
  6724. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6725. ];
  6726. $this->assertHtml($expected, $result);
  6727. }
  6728. /**
  6729. * testButton method
  6730. *
  6731. * Test generation of a form button.
  6732. *
  6733. * @return void
  6734. */
  6735. public function testButton()
  6736. {
  6737. $result = $this->Form->button('Hi');
  6738. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6739. $this->assertHtml($expected, $result);
  6740. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6741. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6742. $this->assertHtml($expected, $result);
  6743. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6744. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6745. $this->assertHtml($expected, $result);
  6746. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6747. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6748. $this->assertHtml($expected, $result);
  6749. $result = $this->Form->button('No type', ['type' => false]);
  6750. $expected = ['button' => [], 'No type', '/button'];
  6751. $this->assertHtml($expected, $result);
  6752. $result = $this->Form->button('Upload Text', [
  6753. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6754. 'escape' => false
  6755. ]);
  6756. $this->assertNotRegExp('/\&039/', $result);
  6757. }
  6758. /**
  6759. * testButtonUnlockedByDefault method
  6760. *
  6761. * Test that button() makes unlocked fields by default.
  6762. *
  6763. * @return void
  6764. */
  6765. public function testButtonUnlockedByDefault()
  6766. {
  6767. $this->Form->request = $this->Form->request->withParam('_csrfToken', 'secured');
  6768. $this->Form->button('Save', ['name' => 'save']);
  6769. $this->Form->button('Clear');
  6770. $result = $this->Form->unlockField();
  6771. $this->assertEquals(['save'], $result);
  6772. }
  6773. /**
  6774. * Test generation of a form button with confirm message.
  6775. *
  6776. * @return void
  6777. */
  6778. public function testButtonWithConfirm()
  6779. {
  6780. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6781. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6782. $this->assertHtml($expected, $result);
  6783. }
  6784. /**
  6785. * testPostButton method
  6786. *
  6787. * @return void
  6788. */
  6789. public function testPostButton()
  6790. {
  6791. $result = $this->Form->postButton('Hi', '/controller/action');
  6792. $expected = [
  6793. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6794. 'div' => ['style' => 'display:none;'],
  6795. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6796. '/div',
  6797. 'button' => ['type' => 'submit'],
  6798. 'Hi',
  6799. '/button',
  6800. '/form'
  6801. ];
  6802. $this->assertHtml($expected, $result);
  6803. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6804. $this->assertNotFalse(strpos($result, '<input type="hidden" name="extra" value="value"'));
  6805. }
  6806. /**
  6807. * testPostButtonMethodType method
  6808. *
  6809. * @return void
  6810. */
  6811. public function testPostButtonMethodType()
  6812. {
  6813. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6814. $expected = [
  6815. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6816. 'div' => ['style' => 'display:none;'],
  6817. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6818. '/div',
  6819. 'button' => ['type' => 'submit'],
  6820. 'Hi',
  6821. '/button',
  6822. '/form'
  6823. ];
  6824. $this->assertHtml($expected, $result);
  6825. }
  6826. /**
  6827. * testPostButtonFormOptions method
  6828. *
  6829. * @return void
  6830. */
  6831. public function testPostButtonFormOptions()
  6832. {
  6833. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6834. $expected = [
  6835. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6836. 'div' => ['style' => 'display:none;'],
  6837. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6838. '/div',
  6839. 'button' => ['type' => 'submit'],
  6840. 'Hi',
  6841. '/button',
  6842. '/form'
  6843. ];
  6844. $this->assertHtml($expected, $result);
  6845. }
  6846. /**
  6847. * testPostButtonNestedData method
  6848. *
  6849. * Test using postButton with N dimensional data.
  6850. *
  6851. * @return void
  6852. */
  6853. public function testPostButtonNestedData()
  6854. {
  6855. $data = [
  6856. 'one' => [
  6857. 'two' => [
  6858. 3, 4, 5
  6859. ]
  6860. ]
  6861. ];
  6862. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6863. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6864. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6865. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6866. }
  6867. /**
  6868. * testSecurePostButton method
  6869. *
  6870. * Test that postButton adds _Token fields.
  6871. *
  6872. * @return void
  6873. */
  6874. public function testSecurePostButton()
  6875. {
  6876. $this->Form->request = $this->Form->request
  6877. ->withParam('_csrfToken', 'testkey')
  6878. ->withParam('_Token.unlockedFields', []);
  6879. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6880. $tokenDebug = urlencode(json_encode([
  6881. '/posts/delete/1',
  6882. [],
  6883. []
  6884. ]));
  6885. $expected = [
  6886. 'form' => [
  6887. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6888. ],
  6889. ['div' => ['style' => 'display:none;']],
  6890. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6891. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6892. '/div',
  6893. 'button' => ['type' => 'submit'],
  6894. 'Delete',
  6895. '/button',
  6896. ['div' => ['style' => 'display:none;']],
  6897. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6898. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6899. ['input' => [
  6900. 'type' => 'hidden',
  6901. 'name' => '_Token[debug]',
  6902. 'value' => $tokenDebug,
  6903. 'autocomplete' => 'off',
  6904. ]],
  6905. '/div',
  6906. '/form',
  6907. ];
  6908. $this->assertHtml($expected, $result);
  6909. }
  6910. /**
  6911. * testPostLink method
  6912. *
  6913. * @return void
  6914. */
  6915. public function testPostLink()
  6916. {
  6917. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6918. $expected = [
  6919. 'form' => [
  6920. 'method' => 'post', 'action' => '/posts/delete/1',
  6921. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6922. ],
  6923. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6924. '/form',
  6925. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6926. 'Delete',
  6927. '/a'
  6928. ];
  6929. $this->assertHtml($expected, $result);
  6930. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6931. $expected = [
  6932. 'form' => [
  6933. 'method' => 'post', 'action' => '/posts/delete/1',
  6934. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6935. ],
  6936. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6937. '/form',
  6938. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6939. 'Delete',
  6940. '/a'
  6941. ];
  6942. $this->assertHtml($expected, $result);
  6943. $result = $this->Form->postLink(
  6944. 'Delete',
  6945. '/posts/delete/1',
  6946. ['target' => '_blank', 'class' => 'btn btn-danger']
  6947. );
  6948. $expected = [
  6949. 'form' => [
  6950. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6951. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6952. ],
  6953. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6954. '/form',
  6955. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6956. 'Delete',
  6957. '/a'
  6958. ];
  6959. $this->assertHtml($expected, $result);
  6960. }
  6961. /**
  6962. * testPostLinkWithConfirm method
  6963. *
  6964. * Test the confirm option for postLink().
  6965. *
  6966. * @return void
  6967. */
  6968. public function testPostLinkWithConfirm()
  6969. {
  6970. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6971. $expected = [
  6972. 'form' => [
  6973. 'method' => 'post', 'action' => '/posts/delete/1',
  6974. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6975. ],
  6976. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6977. '/form',
  6978. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6979. 'Delete',
  6980. '/a'
  6981. ];
  6982. $this->assertHtml($expected, $result);
  6983. $result = $this->Form->postLink(
  6984. 'Delete',
  6985. '/posts/delete/1',
  6986. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6987. );
  6988. $expected = [
  6989. 'form' => [
  6990. 'method' => 'post', 'action' => '/posts/delete/1',
  6991. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6992. ],
  6993. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6994. '/form',
  6995. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6996. 'Delete',
  6997. '/a'
  6998. ];
  6999. $this->assertHtml($expected, $result);
  7000. }
  7001. /**
  7002. * testPostLinkWithQuery method
  7003. *
  7004. * Test postLink() with query string args.
  7005. *
  7006. * @return void
  7007. */
  7008. public function testPostLinkWithQuery()
  7009. {
  7010. $result = $this->Form->postLink(
  7011. 'Delete',
  7012. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7013. );
  7014. $expected = [
  7015. 'form' => [
  7016. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7017. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7018. ],
  7019. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7020. '/form',
  7021. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7022. 'Delete',
  7023. '/a'
  7024. ];
  7025. $this->assertHtml($expected, $result);
  7026. }
  7027. /**
  7028. * testPostLinkWithData method
  7029. *
  7030. * Test postLink with additional data.
  7031. *
  7032. * @return void
  7033. */
  7034. public function testPostLinkWithData()
  7035. {
  7036. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7037. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7038. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7039. $this->Form->create($entity);
  7040. $this->Form->end();
  7041. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7042. $this->assertContains(
  7043. '<input type="hidden" name="name" value="show"',
  7044. $result,
  7045. 'should not contain entity data.'
  7046. );
  7047. }
  7048. /**
  7049. * testPostLinkSecurityHash method
  7050. *
  7051. * Test that security hashes for postLink include the url.
  7052. *
  7053. * @return void
  7054. */
  7055. public function testPostLinkSecurityHash()
  7056. {
  7057. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7058. $hash .= '%3Aid';
  7059. $this->Form->request = $this->Form->request->withParam('_Token.key', 'test');
  7060. $result = $this->Form->postLink(
  7061. 'Delete',
  7062. '/posts/delete/1',
  7063. ['data' => ['id' => 1]]
  7064. );
  7065. $tokenDebug = urlencode(json_encode([
  7066. '/posts/delete/1',
  7067. [
  7068. 'id' => 1
  7069. ],
  7070. []
  7071. ]));
  7072. $expected = [
  7073. 'form' => [
  7074. 'method' => 'post', 'action' => '/posts/delete/1',
  7075. 'name', 'style' => 'display:none;'
  7076. ],
  7077. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7078. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7079. 'div' => ['style' => 'display:none;'],
  7080. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7081. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7082. ['input' => [
  7083. 'type' => 'hidden',
  7084. 'name' => '_Token[debug]',
  7085. 'value' => $tokenDebug,
  7086. 'autocomplete' => 'off',
  7087. ]],
  7088. '/div',
  7089. '/form',
  7090. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7091. 'Delete',
  7092. '/a'
  7093. ];
  7094. $this->assertHtml($expected, $result);
  7095. }
  7096. /**
  7097. * testPostLinkSecurityHashBlockMode method
  7098. *
  7099. * Test that postLink doesn't modify the fields in the containing form.
  7100. *
  7101. * postLink() calls inside open forms should not modify the field list
  7102. * for the form.
  7103. *
  7104. * @return void
  7105. */
  7106. public function testPostLinkSecurityHashBlockMode()
  7107. {
  7108. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7109. $hash .= '%3A';
  7110. $this->Form->request = $this->Form->request->withParam('_Token.key', 'test');
  7111. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7112. $this->Form->control('title');
  7113. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7114. $result = $this->View->fetch('postLink');
  7115. $this->assertEquals(['title'], $this->Form->fields);
  7116. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7117. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7118. }
  7119. /**
  7120. * testPostLinkSecurityHashNoDebugMode method
  7121. *
  7122. * Test that security does not include debug token if debug is false.
  7123. *
  7124. * @return void
  7125. */
  7126. public function testPostLinkSecurityHashNoDebugMode()
  7127. {
  7128. Configure::write('debug', false);
  7129. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7130. $hash .= '%3Aid';
  7131. $this->Form->request = $this->Form->request
  7132. ->withParam('_Token.key', 'test');
  7133. $result = $this->Form->postLink(
  7134. 'Delete',
  7135. '/posts/delete/1',
  7136. ['data' => ['id' => 1]]
  7137. );
  7138. $expected = [
  7139. 'form' => [
  7140. 'method' => 'post', 'action' => '/posts/delete/1',
  7141. 'name', 'style' => 'display:none;'
  7142. ],
  7143. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7144. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7145. 'div' => ['style' => 'display:none;'],
  7146. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7147. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7148. '/div',
  7149. '/form',
  7150. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7151. 'Delete',
  7152. '/a'
  7153. ];
  7154. $this->assertHtml($expected, $result);
  7155. }
  7156. /**
  7157. * testPostLinkNestedData method
  7158. *
  7159. * Test using postLink with N dimensional data.
  7160. *
  7161. * @return void
  7162. */
  7163. public function testPostLinkNestedData()
  7164. {
  7165. $data = [
  7166. 'one' => [
  7167. 'two' => [
  7168. 3, 4, 5
  7169. ]
  7170. ]
  7171. ];
  7172. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7173. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7174. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7175. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7176. }
  7177. /**
  7178. * testPostLinkAfterGetForm method
  7179. *
  7180. * Test creating postLinks after a GET form.
  7181. *
  7182. * @return void
  7183. */
  7184. public function testPostLinkAfterGetForm()
  7185. {
  7186. $this->Form->request = $this->Form->request
  7187. ->withParam('_csrfToken', 'testkey')
  7188. ->withParam('_Token', 'val');
  7189. $this->Form->create($this->article, ['type' => 'get']);
  7190. $this->Form->end();
  7191. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7192. $tokenDebug = urlencode(json_encode([
  7193. '/posts/delete/1',
  7194. [],
  7195. []
  7196. ]));
  7197. $expected = [
  7198. 'form' => [
  7199. 'method' => 'post', 'action' => '/posts/delete/1',
  7200. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7201. ],
  7202. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7203. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7204. 'div' => ['style' => 'display:none;'],
  7205. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7206. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7207. ['input' => [
  7208. 'type' => 'hidden', 'name' => '_Token[debug]',
  7209. 'value' => $tokenDebug,
  7210. 'autocomplete' => 'off'
  7211. ]],
  7212. '/div',
  7213. '/form',
  7214. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7215. 'Delete',
  7216. '/a'
  7217. ];
  7218. $this->assertHtml($expected, $result);
  7219. }
  7220. /**
  7221. * testPostLinkFormBuffer method
  7222. *
  7223. * Test that postLink adds form tags to view block.
  7224. *
  7225. * @return void
  7226. */
  7227. public function testPostLinkFormBuffer()
  7228. {
  7229. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7230. $expected = [
  7231. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7232. 'Delete',
  7233. '/a'
  7234. ];
  7235. $this->assertHtml($expected, $result);
  7236. $result = $this->View->fetch('postLink');
  7237. $expected = [
  7238. 'form' => [
  7239. 'method' => 'post', 'action' => '/posts/delete/1',
  7240. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7241. ],
  7242. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7243. '/form'
  7244. ];
  7245. $this->assertHtml($expected, $result);
  7246. $result = $this->Form->postLink(
  7247. 'Delete',
  7248. '/posts/delete/2',
  7249. ['block' => true, 'method' => 'DELETE']
  7250. );
  7251. $expected = [
  7252. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7253. 'Delete',
  7254. '/a'
  7255. ];
  7256. $this->assertHtml($expected, $result);
  7257. $result = $this->View->fetch('postLink');
  7258. $expected = [
  7259. 'form' => [
  7260. 'method' => 'post', 'action' => '/posts/delete/1',
  7261. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7262. ],
  7263. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7264. '/form',
  7265. [
  7266. 'form' => [
  7267. 'method' => 'post', 'action' => '/posts/delete/2',
  7268. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7269. ],
  7270. ],
  7271. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7272. '/form'
  7273. ];
  7274. $this->assertHtml($expected, $result);
  7275. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7276. $expected = [
  7277. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7278. 'Delete',
  7279. '/a'
  7280. ];
  7281. $this->assertHtml($expected, $result);
  7282. $result = $this->View->fetch('foobar');
  7283. $expected = [
  7284. 'form' => [
  7285. 'method' => 'post', 'action' => '/posts/delete/1',
  7286. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7287. ],
  7288. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7289. '/form'
  7290. ];
  7291. $this->assertHtml($expected, $result);
  7292. }
  7293. /**
  7294. * testSubmitButton method
  7295. *
  7296. * @return void
  7297. */
  7298. public function testSubmitButton()
  7299. {
  7300. $result = $this->Form->submit('');
  7301. $expected = [
  7302. 'div' => ['class' => 'submit'],
  7303. 'input' => ['type' => 'submit', 'value' => ''],
  7304. '/div'
  7305. ];
  7306. $this->assertHtml($expected, $result);
  7307. $result = $this->Form->submit('Test Submit');
  7308. $expected = [
  7309. 'div' => ['class' => 'submit'],
  7310. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7311. '/div'
  7312. ];
  7313. $this->assertHtml($expected, $result);
  7314. $result = $this->Form->submit('Next >');
  7315. $expected = [
  7316. 'div' => ['class' => 'submit'],
  7317. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7318. '/div'
  7319. ];
  7320. $this->assertHtml($expected, $result);
  7321. $result = $this->Form->submit('Next >', ['escape' => false]);
  7322. $expected = [
  7323. 'div' => ['class' => 'submit'],
  7324. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7325. '/div'
  7326. ];
  7327. $this->assertHtml($expected, $result);
  7328. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7329. $expected = [
  7330. 'div' => ['class' => 'submit'],
  7331. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7332. '/div'
  7333. ];
  7334. $this->assertHtml($expected, $result);
  7335. }
  7336. /**
  7337. * testSubmitImage method
  7338. *
  7339. * Test image submit types.
  7340. *
  7341. * @return void
  7342. */
  7343. public function testSubmitImage()
  7344. {
  7345. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7346. $expected = [
  7347. 'div' => ['class' => 'submit'],
  7348. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7349. '/div'
  7350. ];
  7351. $this->assertHtml($expected, $result);
  7352. $result = $this->Form->submit('/relative/cake.power.gif');
  7353. $expected = [
  7354. 'div' => ['class' => 'submit'],
  7355. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7356. '/div'
  7357. ];
  7358. $this->assertHtml($expected, $result);
  7359. $result = $this->Form->submit('cake.power.gif');
  7360. $expected = [
  7361. 'div' => ['class' => 'submit'],
  7362. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7363. '/div'
  7364. ];
  7365. $this->assertHtml($expected, $result);
  7366. $result = $this->Form->submit('Not.an.image');
  7367. $expected = [
  7368. 'div' => ['class' => 'submit'],
  7369. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7370. '/div'
  7371. ];
  7372. $this->assertHtml($expected, $result);
  7373. }
  7374. /**
  7375. * testSubmitUnlockedByDefault method
  7376. *
  7377. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7378. * be submitted at a time.
  7379. *
  7380. * @return void
  7381. */
  7382. public function testSubmitUnlockedByDefault()
  7383. {
  7384. $this->Form->request = $this->Form->request->withParam('_Token', 'secured');
  7385. $this->Form->submit('Go go');
  7386. $this->Form->submit('Save', ['name' => 'save']);
  7387. $result = $this->Form->unlockField();
  7388. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7389. }
  7390. /**
  7391. * testSubmitImageTimestamp method
  7392. *
  7393. * Test submit image with timestamps.
  7394. *
  7395. * @return void
  7396. */
  7397. public function testSubmitImageTimestamp()
  7398. {
  7399. Configure::write('Asset.timestamp', 'force');
  7400. $result = $this->Form->submit('cake.power.gif');
  7401. $expected = [
  7402. 'div' => ['class' => 'submit'],
  7403. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7404. '/div'
  7405. ];
  7406. $this->assertHtml($expected, $result);
  7407. }
  7408. /**
  7409. * testDateTimeWithGetForms method
  7410. *
  7411. * Test that datetime() works with GET style forms.
  7412. *
  7413. * @return void
  7414. */
  7415. public function testDateTimeWithGetForms()
  7416. {
  7417. extract($this->dateRegex);
  7418. $this->Form->create($this->article, ['type' => 'get']);
  7419. $result = $this->Form->datetime('created');
  7420. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7421. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7422. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7423. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7424. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7425. }
  7426. /**
  7427. * testForMagicControlNonExistingNorValidated method
  7428. *
  7429. * @return void
  7430. */
  7431. public function testForMagicControlNonExistingNorValidated()
  7432. {
  7433. $this->Form->create($this->article);
  7434. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7435. $result = $this->Form->control('non_existing_nor_validated');
  7436. $expected = [
  7437. 'label' => ['for' => 'non-existing-nor-validated'],
  7438. 'Non Existing Nor Validated',
  7439. '/label',
  7440. 'input' => [
  7441. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7442. 'id' => 'non-existing-nor-validated'
  7443. ]
  7444. ];
  7445. $this->assertHtml($expected, $result);
  7446. $result = $this->Form->control('non_existing_nor_validated', [
  7447. 'val' => 'my value'
  7448. ]);
  7449. $expected = [
  7450. 'label' => ['for' => 'non-existing-nor-validated'],
  7451. 'Non Existing Nor Validated',
  7452. '/label',
  7453. 'input' => [
  7454. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7455. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7456. ]
  7457. ];
  7458. $this->assertHtml($expected, $result);
  7459. $this->Form->request = $this->Form->request->withData('non_existing_nor_validated', 'CakePHP magic');
  7460. $this->Form->create($this->article);
  7461. $result = $this->Form->control('non_existing_nor_validated');
  7462. $expected = [
  7463. 'label' => ['for' => 'non-existing-nor-validated'],
  7464. 'Non Existing Nor Validated',
  7465. '/label',
  7466. 'input' => [
  7467. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7468. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7469. ]
  7470. ];
  7471. $this->assertHtml($expected, $result);
  7472. }
  7473. /**
  7474. * testFormMagicControlLabel method
  7475. *
  7476. * @return void
  7477. */
  7478. public function testFormMagicControlLabel()
  7479. {
  7480. TableRegistry::get('Contacts', [
  7481. 'className' => __NAMESPACE__ . '\ContactsTable'
  7482. ]);
  7483. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7484. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7485. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7486. $expected = [
  7487. 'label' => ['for' => 'contacts-name'],
  7488. 'My label',
  7489. '/label',
  7490. 'input' => [
  7491. 'type' => 'text', 'name' => 'Contacts[name]',
  7492. 'id' => 'contacts-name', 'maxlength' => '255'
  7493. ]
  7494. ];
  7495. $this->assertHtml($expected, $result);
  7496. $result = $this->Form->control('name', [
  7497. 'label' => ['class' => 'mandatory']
  7498. ]);
  7499. $expected = [
  7500. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7501. 'Name',
  7502. '/label',
  7503. 'input' => [
  7504. 'type' => 'text', 'name' => 'name',
  7505. 'id' => 'name', 'maxlength' => '255'
  7506. ]
  7507. ];
  7508. $this->assertHtml($expected, $result);
  7509. $result = $this->Form->control('name', [
  7510. 'div' => false,
  7511. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7512. ]);
  7513. $expected = [
  7514. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7515. 'My label',
  7516. '/label',
  7517. 'input' => [
  7518. 'type' => 'text', 'name' => 'name',
  7519. 'id' => 'name', 'maxlength' => '255'
  7520. ]
  7521. ];
  7522. $this->assertHtml($expected, $result);
  7523. $result = $this->Form->control('Contact.name', [
  7524. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7525. ]);
  7526. $expected = [
  7527. 'label' => ['for' => 'my_id'],
  7528. 'Name',
  7529. '/label',
  7530. 'input' => [
  7531. 'type' => 'text', 'name' => 'Contact[name]',
  7532. 'id' => 'my_id', 'maxlength' => '255'
  7533. ]
  7534. ];
  7535. $this->assertHtml($expected, $result);
  7536. $result = $this->Form->control('1.id');
  7537. $expected = ['input' => [
  7538. 'type' => 'hidden', 'name' => '1[id]',
  7539. 'id' => '1-id'
  7540. ]];
  7541. $this->assertHtml($expected, $result);
  7542. $result = $this->Form->control('1.name');
  7543. $expected = [
  7544. 'label' => ['for' => '1-name'],
  7545. 'Name',
  7546. '/label',
  7547. 'input' => [
  7548. 'type' => 'text', 'name' => '1[name]',
  7549. 'id' => '1-name', 'maxlength' => '255'
  7550. ]
  7551. ];
  7552. $this->assertHtml($expected, $result);
  7553. }
  7554. /**
  7555. * testFormEnd method
  7556. *
  7557. * @return void
  7558. */
  7559. public function testFormEnd()
  7560. {
  7561. $this->assertEquals('</form>', $this->Form->end());
  7562. }
  7563. /**
  7564. * testMultiRecordForm method
  7565. *
  7566. * Test the generation of fields for a multi record form.
  7567. *
  7568. * @return void
  7569. */
  7570. public function testMultiRecordForm()
  7571. {
  7572. $this->loadFixtures('Articles', 'Comments');
  7573. $articles = TableRegistry::get('Articles');
  7574. $articles->hasMany('Comments');
  7575. $comment = new Entity(['comment' => 'Value']);
  7576. $article = new Article(['comments' => [$comment]]);
  7577. $this->Form->create([$article]);
  7578. $result = $this->Form->control('0.comments.1.comment');
  7579. //@codingStandardsIgnoreStart
  7580. $expected = [
  7581. 'div' => ['class' => 'input textarea'],
  7582. 'label' => ['for' => '0-comments-1-comment'],
  7583. 'Comment',
  7584. '/label',
  7585. 'textarea' => [
  7586. 'name',
  7587. 'id' => '0-comments-1-comment',
  7588. 'rows' => 5
  7589. ],
  7590. '/textarea',
  7591. '/div'
  7592. ];
  7593. //@codingStandardsIgnoreEnd
  7594. $this->assertHtml($expected, $result);
  7595. $result = $this->Form->control('0.comments.0.comment');
  7596. //@codingStandardsIgnoreStart
  7597. $expected = [
  7598. 'div' => ['class' => 'input textarea'],
  7599. 'label' => ['for' => '0-comments-0-comment'],
  7600. 'Comment',
  7601. '/label',
  7602. 'textarea' => [
  7603. 'name',
  7604. 'id' => '0-comments-0-comment',
  7605. 'rows' => 5
  7606. ],
  7607. 'Value',
  7608. '/textarea',
  7609. '/div'
  7610. ];
  7611. //@codingStandardsIgnoreEnd
  7612. $this->assertHtml($expected, $result);
  7613. $comment->setError('comment', ['Not valid']);
  7614. $result = $this->Form->control('0.comments.0.comment');
  7615. //@codingStandardsIgnoreStart
  7616. $expected = [
  7617. 'div' => ['class' => 'input textarea error'],
  7618. 'label' => ['for' => '0-comments-0-comment'],
  7619. 'Comment',
  7620. '/label',
  7621. 'textarea' => [
  7622. 'name',
  7623. 'class' => 'form-error',
  7624. 'id' => '0-comments-0-comment',
  7625. 'rows' => 5
  7626. ],
  7627. 'Value',
  7628. '/textarea',
  7629. ['div' => ['class' => 'error-message']],
  7630. 'Not valid',
  7631. '/div',
  7632. '/div'
  7633. ];
  7634. //@codingStandardsIgnoreEnd
  7635. $this->assertHtml($expected, $result);
  7636. TableRegistry::get('Comments')
  7637. ->getValidator('default')
  7638. ->allowEmpty('comment', false);
  7639. $result = $this->Form->control('0.comments.1.comment');
  7640. //@codingStandardsIgnoreStart
  7641. $expected = [
  7642. 'div' => ['class' => 'input textarea required'],
  7643. 'label' => ['for' => '0-comments-1-comment'],
  7644. 'Comment',
  7645. '/label',
  7646. 'textarea' => [
  7647. 'name',
  7648. 'required' => 'required',
  7649. 'id' => '0-comments-1-comment',
  7650. 'rows' => 5
  7651. ],
  7652. '/textarea',
  7653. '/div'
  7654. ];
  7655. //@codingStandardsIgnoreEnd
  7656. $this->assertHtml($expected, $result);
  7657. }
  7658. /**
  7659. * testHtml5Controls method
  7660. *
  7661. * Test that some html5 inputs + FormHelper::__call() work.
  7662. *
  7663. * @return void
  7664. */
  7665. public function testHtml5Controls()
  7666. {
  7667. $result = $this->Form->email('User.email');
  7668. $expected = [
  7669. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7670. ];
  7671. $this->assertHtml($expected, $result);
  7672. $result = $this->Form->search('User.query');
  7673. $expected = [
  7674. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7675. ];
  7676. $this->assertHtml($expected, $result);
  7677. $result = $this->Form->search('User.query', ['value' => 'test']);
  7678. $expected = [
  7679. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7680. ];
  7681. $this->assertHtml($expected, $result);
  7682. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7683. $expected = [
  7684. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7685. ];
  7686. $this->assertHtml($expected, $result);
  7687. }
  7688. /**
  7689. * testHtml5ControlWithControl method
  7690. *
  7691. * Test accessing html5 inputs through control().
  7692. *
  7693. * @return void
  7694. */
  7695. public function testHtml5ControlWithControl()
  7696. {
  7697. $this->Form->create();
  7698. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7699. $result = $this->Form->control('website', [
  7700. 'type' => 'url',
  7701. 'val' => 'http://domain.tld',
  7702. 'label' => false
  7703. ]);
  7704. $expected = [
  7705. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7706. ];
  7707. $this->assertHtml($expected, $result);
  7708. }
  7709. /**
  7710. * testHtml5ControlException method
  7711. *
  7712. * Test errors when field name is missing.
  7713. *
  7714. * @return void
  7715. */
  7716. public function testHtml5ControlException()
  7717. {
  7718. $this->expectException(\Cake\Core\Exception\Exception::class);
  7719. $this->Form->email();
  7720. }
  7721. /**
  7722. * testRequiredAttribute method
  7723. *
  7724. * Tests that formhelper sets required attributes.
  7725. *
  7726. * @return void
  7727. */
  7728. public function testRequiredAttribute()
  7729. {
  7730. $this->article['required'] = [
  7731. 'title' => true,
  7732. 'body' => false,
  7733. ];
  7734. $this->Form->create($this->article);
  7735. $result = $this->Form->control('title');
  7736. $expected = [
  7737. 'div' => ['class' => 'input text required'],
  7738. 'label' => ['for' => 'title'],
  7739. 'Title',
  7740. '/label',
  7741. 'input' => [
  7742. 'type' => 'text',
  7743. 'name' => 'title',
  7744. 'id' => 'title',
  7745. 'required' => 'required',
  7746. ],
  7747. '/div'
  7748. ];
  7749. $this->assertHtml($expected, $result);
  7750. $result = $this->Form->control('title', ['required' => false]);
  7751. $this->assertNotContains('required', $result);
  7752. $result = $this->Form->control('body');
  7753. $expected = [
  7754. 'div' => ['class' => 'input text'],
  7755. 'label' => ['for' => 'body'],
  7756. 'Body',
  7757. '/label',
  7758. 'input' => [
  7759. 'type' => 'text',
  7760. 'name' => 'body',
  7761. 'id' => 'body',
  7762. ],
  7763. '/div'
  7764. ];
  7765. $this->assertHtml($expected, $result);
  7766. $result = $this->Form->control('body', ['required' => true]);
  7767. $this->assertContains('required', $result);
  7768. }
  7769. /**
  7770. * testControlsNotNested method
  7771. *
  7772. * Tests that it is possible to put inputs outside of the label.
  7773. *
  7774. * @return void
  7775. */
  7776. public function testControlsNotNested()
  7777. {
  7778. $this->Form->setTemplates([
  7779. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7780. 'formGroup' => '{{input}}{{label}}',
  7781. ]);
  7782. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7783. $expected = [
  7784. 'div' => ['class' => 'input checkbox'],
  7785. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7786. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7787. 'label' => ['for' => 'foo'],
  7788. 'Foo',
  7789. '/label',
  7790. '/div'
  7791. ];
  7792. $this->assertHtml($expected, $result);
  7793. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7794. $expected = [
  7795. 'div' => ['class' => 'input checkbox'],
  7796. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7797. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7798. '/div'
  7799. ];
  7800. $this->assertHtml($expected, $result);
  7801. $result = $this->Form->control('confirm', [
  7802. 'type' => 'radio',
  7803. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7804. ]);
  7805. $expected = [
  7806. 'div' => ['class' => 'input radio'],
  7807. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7808. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7809. ['label' => ['for' => 'confirm-y']],
  7810. 'Yes',
  7811. '/label',
  7812. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7813. ['label' => ['for' => 'confirm-n']],
  7814. 'No',
  7815. '/label',
  7816. '<label',
  7817. 'Confirm',
  7818. '/label',
  7819. '/div',
  7820. ];
  7821. $this->assertHtml($expected, $result);
  7822. $result = $this->Form->select('category', ['1', '2'], [
  7823. 'multiple' => 'checkbox',
  7824. 'name' => 'fish',
  7825. ]);
  7826. $expected = [
  7827. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7828. ['div' => ['class' => 'checkbox']],
  7829. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7830. ['label' => ['for' => 'fish-0']],
  7831. '1',
  7832. '/label',
  7833. '/div',
  7834. ['div' => ['class' => 'checkbox']],
  7835. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7836. ['label' => ['for' => 'fish-1']],
  7837. '2',
  7838. '/label',
  7839. '/div'
  7840. ];
  7841. $this->assertHtml($expected, $result);
  7842. }
  7843. /**
  7844. * testControlContainerTemplates method
  7845. *
  7846. * Test that *Container templates are used by input.
  7847. *
  7848. * @return void
  7849. */
  7850. public function testControlContainerTemplates()
  7851. {
  7852. $this->Form->setTemplates([
  7853. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7854. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7855. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7856. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7857. ]);
  7858. $this->article['errors'] = [
  7859. 'Article' => ['published' => 'error message']
  7860. ];
  7861. $this->Form->create($this->article);
  7862. $result = $this->Form->control('accept', [
  7863. 'type' => 'checkbox'
  7864. ]);
  7865. $expected = [
  7866. 'div' => ['class' => 'check'],
  7867. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7868. 'label' => ['for' => 'accept'],
  7869. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7870. 'Accept',
  7871. '/label',
  7872. '/div'
  7873. ];
  7874. $this->assertHtml($expected, $result);
  7875. $result = $this->Form->control('accept', [
  7876. 'type' => 'radio',
  7877. 'options' => ['Y', 'N']
  7878. ]);
  7879. $this->assertContains('<div class="rad">', $result);
  7880. $result = $this->Form->control('Article.published', [
  7881. 'type' => 'radio',
  7882. 'options' => ['Y', 'N']
  7883. ]);
  7884. $this->assertContains('<div class="rad err">', $result);
  7885. $result = $this->Form->control('Article.created', [
  7886. 'type' => 'datetime',
  7887. ]);
  7888. $this->assertContains('<div class="dt">', $result);
  7889. }
  7890. /**
  7891. * testFormGroupTemplates method
  7892. *
  7893. * Test that *Container templates are used by input.
  7894. *
  7895. * @return void
  7896. */
  7897. public function testFormGroupTemplates()
  7898. {
  7899. $this->Form->setTemplates([
  7900. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7901. ]);
  7902. $this->Form->create($this->article);
  7903. $result = $this->Form->control('accept', [
  7904. 'type' => 'radio',
  7905. 'options' => ['Y', 'N']
  7906. ]);
  7907. $this->assertContains('<div class="radio">', $result);
  7908. }
  7909. /**
  7910. * testResetTemplates method
  7911. *
  7912. * Test resetting templates.
  7913. *
  7914. * @return void
  7915. */
  7916. public function testResetTemplates()
  7917. {
  7918. $this->Form->setTemplates(['input' => '<input/>']);
  7919. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7920. $this->Form->resetTemplates();
  7921. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7922. }
  7923. /**
  7924. * testContext method
  7925. *
  7926. * Test the context method.
  7927. *
  7928. * @return void
  7929. */
  7930. public function testContext()
  7931. {
  7932. $result = $this->Form->context();
  7933. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7934. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7935. $this->assertSame($mock, $this->Form->context($mock));
  7936. $this->assertSame($mock, $this->Form->context());
  7937. }
  7938. /**
  7939. * testAutoDomId method
  7940. *
  7941. * @return void
  7942. */
  7943. public function testAutoDomId()
  7944. {
  7945. $result = $this->Form->text('field', ['id' => true]);
  7946. $expected = [
  7947. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7948. ];
  7949. $this->assertHtml($expected, $result);
  7950. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7951. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7952. $expected = [
  7953. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7954. ['label' => ['for' => 'field-0']],
  7955. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7956. 'option A',
  7957. '/label',
  7958. ['label' => ['for' => 'field-1']],
  7959. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7960. 'option B',
  7961. '/label',
  7962. ];
  7963. $this->assertHtml($expected, $result);
  7964. $result = $this->Form->select(
  7965. 'multi_field',
  7966. ['first', 'second'],
  7967. ['multiple' => 'checkbox', 'id' => true]
  7968. );
  7969. $expected = [
  7970. 'input' => [
  7971. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7972. ],
  7973. ['div' => ['class' => 'checkbox']],
  7974. ['label' => ['for' => 'multi-field-0']],
  7975. ['input' => [
  7976. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7977. 'value' => '0', 'id' => 'multi-field-0'
  7978. ]],
  7979. 'first',
  7980. '/label',
  7981. '/div',
  7982. ['div' => ['class' => 'checkbox']],
  7983. ['label' => ['for' => 'multi-field-1']],
  7984. ['input' => [
  7985. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7986. 'value' => '1', 'id' => 'multi-field-1'
  7987. ]],
  7988. 'second',
  7989. '/label',
  7990. '/div',
  7991. ];
  7992. $this->assertHtml($expected, $result);
  7993. }
  7994. /**
  7995. * Test the basic setters and getters for value sources
  7996. *
  7997. * @return void
  7998. */
  7999. public function testFormValueSourcesSettersGetters()
  8000. {
  8001. $this->Form->request = $this->Form->request
  8002. ->withData('id', '1')
  8003. ->withQueryParams(['id' => '2']);
  8004. $expected = ['context'];
  8005. $result = $this->Form->getValueSources();
  8006. $this->assertEquals($expected, $result);
  8007. $expected = ['query', 'data', 'context'];
  8008. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8009. $result = $this->Form->getValueSources();
  8010. $this->assertEquals($expected, $result);
  8011. $this->Form->setValueSources(['context']);
  8012. $expected = '1';
  8013. $result = $this->Form->getSourceValue('id');
  8014. $this->assertEquals($expected, $result);
  8015. $this->Form->setValueSources('query');
  8016. $expected = ['query'];
  8017. $result = $this->Form->getValueSources();
  8018. $this->assertEquals($expected, $result);
  8019. $expected = '2';
  8020. $result = $this->Form->getSourceValue('id');
  8021. $this->assertEquals($expected, $result);
  8022. $this->Form->setValueSources(['data']);
  8023. $expected = '1';
  8024. $result = $this->Form->getSourceValue('id');
  8025. $this->assertEquals($expected, $result);
  8026. $this->Form->setValueSources(['query', 'data']);
  8027. $expected = '2';
  8028. $result = $this->Form->getSourceValue('id');
  8029. $this->assertEquals($expected, $result);
  8030. }
  8031. /**
  8032. * Tests the different input rendering values based on sources values switching
  8033. *
  8034. * @return void
  8035. */
  8036. public function testFormValueSourcesSingleSwitchRendering()
  8037. {
  8038. $this->loadFixtures('Articles');
  8039. $articles = TableRegistry::get('Articles');
  8040. $article = new Article();
  8041. $articles->patchEntity($article, ['id' => '3']);
  8042. $this->Form->create($article);
  8043. $this->Form->setValueSources(['context']);
  8044. $result = $this->Form->control('id');
  8045. $expected = [
  8046. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8047. ];
  8048. $this->assertHtml($expected, $result);
  8049. $this->Form->request = $this->Form->request->withQueryParams(['id' => 5]);
  8050. $this->Form->setValueSources(['query']);
  8051. $result = $this->Form->control('id');
  8052. $expected = [
  8053. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8054. ];
  8055. $this->assertHtml($expected, $result);
  8056. $this->Form->request = $this->Form->request
  8057. ->withQueryParams(['id' => '5a'])
  8058. ->withData('id', '5b');
  8059. $this->Form->setValueSources(['context']);
  8060. $this->Form->create($article);
  8061. $result = $this->Form->control('id');
  8062. $expected = [
  8063. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8064. ];
  8065. $this->assertHtml($expected, $result);
  8066. $this->Form->setValueSources(['data']);
  8067. $this->Form->create($article);
  8068. $result = $this->Form->control('id');
  8069. $expected = [
  8070. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8071. ];
  8072. $this->assertHtml($expected, $result);
  8073. $this->Form->setValueSources(['query']);
  8074. $result = $this->Form->control('id');
  8075. $expected = [
  8076. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8077. ];
  8078. $this->assertHtml($expected, $result);
  8079. }
  8080. /**
  8081. * Tests the different input rendering values based on sources values switching while supplying
  8082. * an entity (base context) and multiple sources (such as data, query)
  8083. *
  8084. * @return void
  8085. */
  8086. public function testFormValueSourcesListSwitchRendering()
  8087. {
  8088. $this->loadFixtures('Articles');
  8089. $articles = TableRegistry::get('Articles');
  8090. $article = new Article();
  8091. $articles->patchEntity($article, ['id' => '3']);
  8092. $this->Form->request = $this->Form->request->withQueryParams(['id' => '9']);
  8093. $this->Form->create($article);
  8094. $this->Form->setValueSources(['context', 'query']);
  8095. $result = $this->Form->control('id');
  8096. $expected = [
  8097. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8098. ];
  8099. $this->assertHtml($expected, $result);
  8100. $this->Form->setValueSources(['query', 'context']);
  8101. $result = $this->Form->control('id');
  8102. $expected = [
  8103. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8104. ];
  8105. $this->assertHtml($expected, $result);
  8106. $this->Form->setValueSources(['data', 'query', 'context']);
  8107. $result = $this->Form->control('id');
  8108. $expected = [
  8109. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8110. ];
  8111. $this->assertHtml($expected, $result);
  8112. $this->Form->request = $this->Form->request
  8113. ->withData('id', '8')
  8114. ->withQueryParams(['id' => '9']);
  8115. $this->Form->setValueSources(['data', 'query', 'context']);
  8116. $result = $this->Form->control('id');
  8117. $expected = [
  8118. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8119. ];
  8120. $this->assertHtml($expected, $result);
  8121. }
  8122. /**
  8123. * Test the different form input renderings based on values sources switchings through form options
  8124. *
  8125. * @return void
  8126. */
  8127. public function testFormValueSourcesSwitchViaOptionsRendering()
  8128. {
  8129. $this->loadFixtures('Articles');
  8130. $articles = TableRegistry::get('Articles');
  8131. $article = new Article();
  8132. $articles->patchEntity($article, ['id' => '3']);
  8133. $this->Form->request = $this->Form->request->withData('id', 4)->withQueryParams(['id' => 5]);
  8134. $this->Form->create($article, ['valueSources' => 'query']);
  8135. $result = $this->Form->control('id');
  8136. $expected = [
  8137. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8138. ];
  8139. $this->assertHtml($expected, $result);
  8140. $result = $this->Form->getSourceValue('id');
  8141. $this->assertEquals('5', $result);
  8142. $this->Form->setValueSources(['context']);
  8143. $this->Form->create($article, ['valueSources' => 'query']);
  8144. $result = $this->Form->control('id');
  8145. $expected = [
  8146. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8147. ];
  8148. $this->assertHtml($expected, $result);
  8149. $result = $this->Form->getSourceValue('id');
  8150. $this->assertEquals('5', $result);
  8151. $this->Form->setValueSources(['query']);
  8152. $this->Form->create($article, ['valueSources' => 'data']);
  8153. $result = $this->Form->control('id');
  8154. $expected = [
  8155. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8156. ];
  8157. $this->assertHtml($expected, $result);
  8158. $result = $this->Form->getSourceValue('id');
  8159. $this->assertEquals('4', $result);
  8160. $this->Form->setValueSources(['query']);
  8161. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8162. $result = $this->Form->control('id');
  8163. $expected = [
  8164. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8165. ];
  8166. $this->assertHtml($expected, $result);
  8167. $result = $this->Form->getSourceValue('id');
  8168. $this->assertEquals('4', $result);
  8169. }
  8170. /**
  8171. * Test the different form input renderings based on values sources switchings through form options
  8172. *
  8173. * @return void
  8174. */
  8175. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8176. {
  8177. $this->loadFixtures('Articles');
  8178. $articles = TableRegistry::get('Articles');
  8179. $article = new Article();
  8180. $articles->patchEntity($article, ['id' => '3']);
  8181. $this->Form->request = $this->Form->request->withData('id', 10)->withQueryParams(['id' => 11]);
  8182. $this->Form->setValueSources(['context'])
  8183. ->create($article, ['valueSources' => ['query', 'data']]);
  8184. $result = $this->Form->control('id');
  8185. $expected = [
  8186. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8187. ];
  8188. $this->assertHtml($expected, $result);
  8189. $result = $this->Form->getSourceValue('id');
  8190. $this->assertEquals('11', $result);
  8191. $this->Form->request = $this->Form->request->withQueryParams([]);
  8192. $this->Form->setValueSources(['context'])
  8193. ->create($article, ['valueSources' => ['query', 'data']]);
  8194. $result = $this->Form->control('id');
  8195. $expected = [
  8196. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8197. ];
  8198. $this->assertHtml($expected, $result);
  8199. $result = $this->Form->getSourceValue('id');
  8200. $this->assertEquals('10', $result);
  8201. }
  8202. /**
  8203. * Test the different form values sources resetting through From::end();
  8204. *
  8205. * @return void
  8206. */
  8207. public function testFormValueSourcesResetViaEnd()
  8208. {
  8209. $expected = ['context'];
  8210. $result = $this->Form->getValueSources();
  8211. $this->assertEquals($expected, $result);
  8212. $expected = ['query', 'context', 'data'];
  8213. $this->Form->setValueSources(['query', 'context', 'data']);
  8214. $result = $this->Form->getValueSources();
  8215. $this->assertEquals($expected, $result);
  8216. $this->Form->create();
  8217. $result = $this->Form->getValueSources();
  8218. $this->assertEquals($expected, $result);
  8219. $this->Form->end();
  8220. $result = $this->Form->getValueSources();
  8221. $this->assertEquals(['context'], $result);
  8222. }
  8223. /**
  8224. * Test sources values defaults handling
  8225. *
  8226. * @return void
  8227. */
  8228. public function testFormValueSourcesDefaults()
  8229. {
  8230. $this->Form->request = $this->Form->request->withQueryParams(['password' => 'open Sesame']);
  8231. $this->Form->create();
  8232. $result = $this->Form->password('password');
  8233. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8234. $this->assertHtml($expected, $result);
  8235. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8236. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8237. $this->assertHtml($expected, $result);
  8238. $this->Form->setValueSources('query');
  8239. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8240. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8241. $this->assertHtml($expected, $result);
  8242. $this->Form->setValueSources('data');
  8243. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8244. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8245. $this->assertHtml($expected, $result);
  8246. }
  8247. /**
  8248. * Test sources values schema defaults handling
  8249. *
  8250. * @return void
  8251. */
  8252. public function testSourcesValueDoesntExistPassThrough()
  8253. {
  8254. $this->Form->request = $this->Form->request->withQueryParams(['category' => 'sesame-cookies']);
  8255. $articles = TableRegistry::get('Articles');
  8256. $entity = $articles->newEntity();
  8257. $this->Form->create($entity);
  8258. $this->Form->setValueSources(['query', 'context']);
  8259. $result = $this->Form->getSourceValue('category');
  8260. $this->assertEquals('sesame-cookies', $result);
  8261. $this->Form->setValueSources(['context', 'query']);
  8262. $result = $this->Form->getSourceValue('category');
  8263. $this->assertEquals('sesame-cookies', $result);
  8264. }
  8265. /**
  8266. * testNestedLabelInput method
  8267. *
  8268. * Test the `nestedInput` parameter
  8269. *
  8270. * @group deprecated
  8271. * @return void
  8272. */
  8273. public function testNestedLabelInput()
  8274. {
  8275. $this->deprecated(function () {
  8276. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8277. $expected = [
  8278. 'div' => ['class' => 'input text'],
  8279. 'label' => ['for' => 'foo'],
  8280. ['input' => [
  8281. 'type' => 'text',
  8282. 'name' => 'foo',
  8283. 'id' => 'foo'
  8284. ]],
  8285. 'Foo',
  8286. '/label',
  8287. '/div'
  8288. ];
  8289. $this->assertHtml($expected, $result);
  8290. });
  8291. }
  8292. /**
  8293. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8294. *
  8295. * This test makes sure `false` excludes the label from the render
  8296. *
  8297. * @return void
  8298. */
  8299. public function testControlLabelManipulationDisableLabels()
  8300. {
  8301. $result = $this->Form->control('test', [
  8302. 'type' => 'radio',
  8303. 'options' => ['A', 'B'],
  8304. 'labelOptions' => false
  8305. ]);
  8306. $expected = [
  8307. ['div' => ['class' => 'input radio']],
  8308. '<label',
  8309. 'Test',
  8310. '/label',
  8311. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8312. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8313. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8314. '/div',
  8315. ];
  8316. $this->assertHtml($expected, $result);
  8317. $result = $this->Form->control('checkbox1', [
  8318. 'label' => 'My checkboxes',
  8319. 'multiple' => 'checkbox',
  8320. 'type' => 'select',
  8321. 'options' => [
  8322. ['text' => 'First Checkbox', 'value' => 1],
  8323. ['text' => 'Second Checkbox', 'value' => 2]
  8324. ],
  8325. 'labelOptions' => false
  8326. ]);
  8327. $expected = [
  8328. ['div' => ['class' => 'input select']],
  8329. ['label' => ['for' => 'checkbox1']],
  8330. 'My checkboxes',
  8331. '/label',
  8332. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8333. ['div' => ['class' => 'checkbox']],
  8334. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8335. '/div',
  8336. ['div' => ['class' => 'checkbox']],
  8337. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8338. '/div',
  8339. '/div'
  8340. ];
  8341. $this->assertHtml($expected, $result);
  8342. }
  8343. /**
  8344. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8345. *
  8346. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8347. * added to the class if checked.
  8348. *
  8349. * Also checks to make sure any custom attributes are rendered correctly
  8350. *
  8351. * @return void
  8352. */
  8353. public function testControlLabelManipulationRadios()
  8354. {
  8355. $result = $this->Form->control('test', [
  8356. 'type' => 'radio',
  8357. 'options' => ['A', 'B'],
  8358. 'labelOptions' => ['class' => 'custom-class']
  8359. ]);
  8360. $expected = [
  8361. ['div' => ['class' => 'input radio']],
  8362. '<label',
  8363. 'Test',
  8364. '/label',
  8365. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8366. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8367. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8368. 'A',
  8369. '/label',
  8370. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8371. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8372. 'B',
  8373. '/label',
  8374. '/div',
  8375. ];
  8376. $this->assertHtml($expected, $result);
  8377. $result = $this->Form->control('test', [
  8378. 'type' => 'radio',
  8379. 'options' => ['A', 'B'],
  8380. 'value' => 1,
  8381. 'labelOptions' => ['class' => 'custom-class']
  8382. ]);
  8383. $expected = [
  8384. ['div' => ['class' => 'input radio']],
  8385. '<label',
  8386. 'Test',
  8387. '/label',
  8388. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8389. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8390. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8391. 'A',
  8392. '/label',
  8393. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8394. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8395. 'B',
  8396. '/label',
  8397. '/div',
  8398. ];
  8399. $this->assertHtml($expected, $result);
  8400. $result = $this->Form->control('test', [
  8401. 'type' => 'radio',
  8402. 'options' => ['A', 'B'],
  8403. 'value' => 1,
  8404. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8405. ]);
  8406. $expected = [
  8407. ['div' => ['class' => 'input radio']],
  8408. '<label',
  8409. 'Test',
  8410. '/label',
  8411. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8412. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8413. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8414. 'A',
  8415. '/label',
  8416. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8417. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8418. 'B',
  8419. '/label',
  8420. '/div',
  8421. ];
  8422. $this->assertHtml($expected, $result);
  8423. $result = $this->Form->radio('test', ['A', 'B'], [
  8424. 'label' => [
  8425. 'class' => ['custom-class', 'another-class'],
  8426. 'data-name' => 'bob'
  8427. ],
  8428. 'value' => 1
  8429. ]);
  8430. $expected = [
  8431. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8432. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8433. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8434. 'A',
  8435. '/label',
  8436. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8437. ['input' => [
  8438. 'type' => 'radio',
  8439. 'name' => 'test',
  8440. 'value' => '1',
  8441. 'id' => 'test-1',
  8442. 'checked' => 'checked'
  8443. ]],
  8444. 'B',
  8445. '/label',
  8446. ];
  8447. $this->assertHtml($expected, $result);
  8448. }
  8449. /**
  8450. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8451. *
  8452. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8453. * added to the class if checked.
  8454. *
  8455. * Also checks to make sure any custom attributes are rendered correctly
  8456. *
  8457. * @return void
  8458. */
  8459. public function testControlLabelManipulationCheckboxes()
  8460. {
  8461. $result = $this->Form->control('checkbox1', [
  8462. 'label' => 'My checkboxes',
  8463. 'multiple' => 'checkbox',
  8464. 'type' => 'select',
  8465. 'options' => [
  8466. ['text' => 'First Checkbox', 'value' => 1],
  8467. ['text' => 'Second Checkbox', 'value' => 2]
  8468. ],
  8469. 'labelOptions' => ['class' => 'custom-class'],
  8470. 'value' => ['1']
  8471. ]);
  8472. $expected = [
  8473. ['div' => ['class' => 'input select']],
  8474. ['label' => ['for' => 'checkbox1']],
  8475. 'My checkboxes',
  8476. '/label',
  8477. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8478. ['div' => ['class' => 'checkbox']],
  8479. ['label' => [
  8480. 'class' => 'custom-class selected',
  8481. 'for' => 'checkbox1-1'
  8482. ]],
  8483. ['input' => [
  8484. 'type' => 'checkbox',
  8485. 'name' => 'checkbox1[]',
  8486. 'value' => '1',
  8487. 'id' => 'checkbox1-1',
  8488. 'checked' => 'checked'
  8489. ]],
  8490. 'First Checkbox',
  8491. '/label',
  8492. '/div',
  8493. ['div' => ['class' => 'checkbox']],
  8494. ['label' => [
  8495. 'class' => 'custom-class',
  8496. 'for' => 'checkbox1-2'
  8497. ]],
  8498. ['input' => [
  8499. 'type' => 'checkbox',
  8500. 'name' => 'checkbox1[]',
  8501. 'value' => '2',
  8502. 'id' => 'checkbox1-2'
  8503. ]],
  8504. 'Second Checkbox',
  8505. '/label',
  8506. '/div',
  8507. '/div'
  8508. ];
  8509. $this->assertHtml($expected, $result);
  8510. $result = $this->Form->control('checkbox1', [
  8511. 'label' => 'My checkboxes',
  8512. 'multiple' => 'checkbox',
  8513. 'type' => 'select',
  8514. 'options' => [
  8515. ['text' => 'First Checkbox', 'value' => 1],
  8516. ['text' => 'Second Checkbox', 'value' => 2]
  8517. ],
  8518. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8519. 'value' => ['1']
  8520. ]);
  8521. $expected = [
  8522. ['div' => ['class' => 'input select']],
  8523. ['label' => ['for' => 'checkbox1']],
  8524. 'My checkboxes',
  8525. '/label',
  8526. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8527. ['div' => ['class' => 'checkbox']],
  8528. ['label' => [
  8529. 'class' => 'custom-class another-class selected',
  8530. 'data-name' => 'bob',
  8531. 'for' => 'checkbox1-1'
  8532. ]],
  8533. ['input' => [
  8534. 'type' => 'checkbox',
  8535. 'name' => 'checkbox1[]',
  8536. 'value' => '1',
  8537. 'id' => 'checkbox1-1',
  8538. 'checked' => 'checked'
  8539. ]],
  8540. 'First Checkbox',
  8541. '/label',
  8542. '/div',
  8543. ['div' => ['class' => 'checkbox']],
  8544. ['label' => [
  8545. 'class' => 'custom-class another-class',
  8546. 'data-name' => 'bob',
  8547. 'for' => 'checkbox1-2'
  8548. ]],
  8549. ['input' => [
  8550. 'type' => 'checkbox',
  8551. 'name' => 'checkbox1[]',
  8552. 'value' => '2',
  8553. 'id' => 'checkbox1-2'
  8554. ]],
  8555. 'Second Checkbox',
  8556. '/label',
  8557. '/div',
  8558. '/div'
  8559. ];
  8560. $this->assertHtml($expected, $result);
  8561. }
  8562. }