FormHelperTest.php 304 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Core\Configure;
  18. use Cake\Form\Form;
  19. use Cake\Http\ServerRequest;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Security;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\View;
  28. /**
  29. * Test stub.
  30. */
  31. class Article extends Entity
  32. {
  33. }
  34. /**
  35. * Contact class
  36. */
  37. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->schema($this->_schema);
  64. }
  65. }
  66. /**
  67. * ValidateUser class
  68. */
  69. class ValidateUsersTable extends Table
  70. {
  71. /**
  72. * schema method
  73. *
  74. * @var array
  75. */
  76. protected $_schema = [
  77. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  78. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  79. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  81. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  82. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  83. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  84. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  85. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  86. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  87. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  88. ];
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config)
  95. {
  96. $this->schema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property \Cake\View\Helper\FormHelper $Form
  103. * @property \Cake\View\View $View
  104. */
  105. class FormHelperTest extends TestCase
  106. {
  107. /**
  108. * Fixtures to be used
  109. *
  110. * @var array
  111. */
  112. public $fixtures = ['core.articles', 'core.comments'];
  113. /**
  114. * Do not load the fixtures by default
  115. *
  116. * @var bool
  117. */
  118. public $autoFixtures = false;
  119. /**
  120. * @var array
  121. */
  122. protected $article = [];
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp()
  129. {
  130. parent::setUp();
  131. Configure::write('Config.language', 'eng');
  132. Configure::write('App.base', '');
  133. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  134. $this->View = new View();
  135. $this->Form = new FormHelper($this->View);
  136. $request = new ServerRequest([
  137. 'webroot' => '',
  138. 'base' => '',
  139. 'url' => '/articles/add',
  140. 'params' => [
  141. 'controller' => 'articles',
  142. 'action' => 'add',
  143. ]
  144. ]);
  145. $this->Form->Url->request = $this->Form->request = $request;
  146. $this->dateRegex = [
  147. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  148. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  149. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  152. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  153. ];
  154. $this->article = [
  155. 'schema' => [
  156. 'id' => ['type' => 'integer'],
  157. 'author_id' => ['type' => 'integer', 'null' => true],
  158. 'title' => ['type' => 'string', 'null' => true],
  159. 'body' => 'text',
  160. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  161. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  162. ],
  163. 'required' => [
  164. 'author_id' => true,
  165. 'title' => true,
  166. ]
  167. ];
  168. Security::salt('foo!');
  169. Router::connect('/:controller', ['action' => 'index']);
  170. Router::connect('/:controller/:action/*');
  171. }
  172. /**
  173. * tearDown method
  174. *
  175. * @return void
  176. */
  177. public function tearDown()
  178. {
  179. parent::tearDown();
  180. unset($this->Form, $this->Controller, $this->View);
  181. TableRegistry::clear();
  182. }
  183. /**
  184. * Test construct() with the templates option.
  185. *
  186. * @return void
  187. */
  188. public function testConstructTemplatesFile()
  189. {
  190. $helper = new FormHelper($this->View, [
  191. 'templates' => 'htmlhelper_tags'
  192. ]);
  193. $result = $helper->control('name');
  194. $this->assertContains('<input', $result);
  195. }
  196. /**
  197. * Test that when specifying custom widgets the config array for that widget
  198. * is overwritten instead of merged.
  199. *
  200. * @return void
  201. */
  202. public function testConstructWithWidgets()
  203. {
  204. $config = [
  205. 'widgets' => [
  206. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  207. ]
  208. ];
  209. $helper = new FormHelper($this->View, $config);
  210. $registry = $helper->widgetRegistry();
  211. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  212. }
  213. /**
  214. * Test that when specifying custom widgets config file and it should be
  215. * added to widgets array. WidgetRegistry will load widgets in constructor.
  216. *
  217. * @return void
  218. */
  219. public function testConstructWithWidgetsConfig()
  220. {
  221. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  222. $registry = $helper->widgetRegistry();
  223. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  224. }
  225. /**
  226. * Test registering a new widget class and rendering it.
  227. *
  228. * @return void
  229. */
  230. public function testAddWidgetAndRenderWidget()
  231. {
  232. $data = [
  233. 'val' => 1
  234. ];
  235. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  236. $this->Form->addWidget('test', $mock);
  237. $mock->expects($this->once())
  238. ->method('render')
  239. ->with($data)
  240. ->will($this->returnValue('HTML'));
  241. $result = $this->Form->widget('test', $data);
  242. $this->assertEquals('HTML', $result);
  243. }
  244. /**
  245. * Test that secureFields() of widget is called after calling render(),
  246. * not before.
  247. *
  248. * @return void
  249. */
  250. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  251. {
  252. $data = [
  253. 'val' => 1,
  254. 'name' => 'test'
  255. ];
  256. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  257. $this->Form->addWidget('test', $mock);
  258. $mock->expects($this->at(0))
  259. ->method('render')
  260. ->with($data)
  261. ->will($this->returnValue('HTML'));
  262. $mock->expects($this->at(1))
  263. ->method('secureFields')
  264. ->with($data)
  265. ->will($this->returnValue(['test']));
  266. $result = $this->Form->widget('test', $data + ['secure' => true]);
  267. $this->assertEquals('HTML', $result);
  268. }
  269. /**
  270. * Test that empty string is not added to secure fields list when
  271. * rendering input widget without name.
  272. *
  273. * @return void
  274. */
  275. public function testRenderingWidgetWithEmptyName()
  276. {
  277. $this->assertEquals([], $this->Form->fields);
  278. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  279. $this->assertEquals('<select name=""></select>', $result);
  280. $this->assertEquals([], $this->Form->fields);
  281. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  282. $this->assertEquals('<select name="0"></select>', $result);
  283. $this->assertEquals(['0'], $this->Form->fields);
  284. }
  285. /**
  286. * Test registering an invalid widget class.
  287. *
  288. * @expectedException \RuntimeException
  289. * @return void
  290. */
  291. public function testAddWidgetInvalid()
  292. {
  293. $mock = new \StdClass();
  294. $this->Form->addWidget('test', $mock);
  295. $this->Form->widget('test');
  296. }
  297. /**
  298. * Test adding a new context class.
  299. *
  300. * @return void
  301. */
  302. public function testAddContextProvider()
  303. {
  304. $context = 'My data';
  305. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  306. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  307. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  308. $this->assertEquals($context, $data['entity']);
  309. return $stub;
  310. });
  311. $this->Form->create($context);
  312. $result = $this->Form->context();
  313. $this->assertSame($stub, $result);
  314. }
  315. /**
  316. * Test replacing a context class.
  317. *
  318. * @return void
  319. */
  320. public function testAddContextProviderReplace()
  321. {
  322. $entity = new Article();
  323. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  324. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  325. return $stub;
  326. });
  327. $this->Form->create($entity);
  328. $result = $this->Form->context();
  329. $this->assertSame($stub, $result);
  330. }
  331. /**
  332. * Test overriding a context class.
  333. *
  334. * @return void
  335. */
  336. public function testAddContextProviderAdd()
  337. {
  338. $entity = new Article();
  339. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  340. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  341. if ($data['entity'] instanceof Entity) {
  342. return $stub;
  343. }
  344. });
  345. $this->Form->create($entity);
  346. $result = $this->Form->context();
  347. $this->assertSame($stub, $result);
  348. }
  349. /**
  350. * Test adding an invalid context class.
  351. *
  352. * @expectedException \RuntimeException
  353. * @expectedExceptionMessage Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.
  354. * @return void
  355. */
  356. public function testAddContextProviderInvalid()
  357. {
  358. $context = 'My data';
  359. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  360. return new \StdClass();
  361. });
  362. $this->Form->create($context);
  363. }
  364. /**
  365. * Provides context options for create().
  366. *
  367. * @return array
  368. */
  369. public function contextSelectionProvider()
  370. {
  371. $entity = new Article();
  372. $collection = new Collection([$entity]);
  373. $emptyCollection = new Collection([]);
  374. $arrayObject = new \ArrayObject([]);
  375. $data = [
  376. 'schema' => [
  377. 'title' => ['type' => 'string']
  378. ]
  379. ];
  380. $form = new Form();
  381. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  382. return [
  383. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  384. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  385. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  386. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  387. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  388. 'form' => [$form, 'Cake\View\Form\FormContext'],
  389. 'none' => [null, 'Cake\View\Form\NullContext'],
  390. 'false' => [false, 'Cake\View\Form\NullContext'],
  391. 'custom' => [$custom, get_class($custom)]
  392. ];
  393. }
  394. /**
  395. * Test default context selection in create()
  396. *
  397. * @dataProvider contextSelectionProvider
  398. * @return void
  399. */
  400. public function testCreateContextSelectionBuiltIn($data, $class)
  401. {
  402. $this->loadFixtures('Articles');
  403. $this->Form->create($data);
  404. $this->assertInstanceOf($class, $this->Form->context());
  405. }
  406. /**
  407. * Data provider for type option.
  408. *
  409. * @return array
  410. */
  411. public static function requestTypeProvider()
  412. {
  413. return [
  414. // type, method, override
  415. ['post', 'post', 'POST'],
  416. ['put', 'post', 'PUT'],
  417. ['patch', 'post', 'PATCH'],
  418. ['delete', 'post', 'DELETE'],
  419. ];
  420. }
  421. /**
  422. * Test creating file forms.
  423. *
  424. * @return void
  425. */
  426. public function testCreateFile()
  427. {
  428. $encoding = strtolower(Configure::read('App.encoding'));
  429. $result = $this->Form->create(false, ['type' => 'file']);
  430. $expected = [
  431. 'form' => [
  432. 'method' => 'post', 'action' => '/articles/add',
  433. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  434. ],
  435. 'div' => ['style' => 'display:none;'],
  436. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  437. '/div'
  438. ];
  439. $this->assertHtml($expected, $result);
  440. }
  441. /**
  442. * Test creating GET forms.
  443. *
  444. * @return void
  445. */
  446. public function testCreateGet()
  447. {
  448. $encoding = strtolower(Configure::read('App.encoding'));
  449. $result = $this->Form->create(false, ['type' => 'get']);
  450. $expected = ['form' => [
  451. 'method' => 'get', 'action' => '/articles/add',
  452. 'accept-charset' => $encoding
  453. ]];
  454. $this->assertHtml($expected, $result);
  455. }
  456. /**
  457. * Test explicit method/enctype options.
  458. *
  459. * Explicit method overwrites inferred method from 'type'
  460. *
  461. * @return void
  462. */
  463. public function testCreateExplicitMethodEnctype()
  464. {
  465. $encoding = strtolower(Configure::read('App.encoding'));
  466. $result = $this->Form->create(false, [
  467. 'type' => 'get',
  468. 'method' => 'put',
  469. 'enctype' => 'multipart/form-data'
  470. ]);
  471. $expected = ['form' => [
  472. 'method' => 'put',
  473. 'action' => '/articles/add',
  474. 'enctype' => 'multipart/form-data',
  475. 'accept-charset' => $encoding
  476. ]];
  477. $this->assertHtml($expected, $result);
  478. }
  479. /**
  480. * Test create() with the templates option.
  481. *
  482. * @return void
  483. */
  484. public function testCreateTemplatesArray()
  485. {
  486. $result = $this->Form->create($this->article, [
  487. 'templates' => [
  488. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  489. ]
  490. ]);
  491. $expected = [
  492. 'form' => [
  493. 'class' => 'form-horizontal',
  494. 'method' => 'post',
  495. 'action' => '/articles/add',
  496. 'accept-charset' => 'utf-8'
  497. ]
  498. ];
  499. $this->assertHtml($expected, $result);
  500. }
  501. /**
  502. * Test create() with the templates option.
  503. *
  504. * @return void
  505. */
  506. public function testCreateTemplatesFile()
  507. {
  508. $result = $this->Form->create($this->article, [
  509. 'templates' => 'htmlhelper_tags',
  510. ]);
  511. $expected = [
  512. 'start form',
  513. 'div' => ['class' => 'hidden'],
  514. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  515. '/div'
  516. ];
  517. $this->assertHtml($expected, $result);
  518. }
  519. /**
  520. * Test that create() and end() restore templates.
  521. *
  522. * @return void
  523. */
  524. public function testCreateEndRestoreTemplates()
  525. {
  526. $this->Form->create($this->article, [
  527. 'templates' => ['input' => 'custom input element']
  528. ]);
  529. $this->Form->end();
  530. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  531. }
  532. /**
  533. * Test using template vars in various templates used by control() method.
  534. *
  535. * @return void
  536. */
  537. public function testControlTemplateVars()
  538. {
  539. $result = $this->Form->control('text', [
  540. 'templates' => [
  541. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  542. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  543. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  544. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  545. ],
  546. 'templateVars' => [
  547. 'forinput' => 'in-input',
  548. 'forlabel' => 'in-label',
  549. 'forgroup' => 'in-group',
  550. 'forcontainer' => 'in-container'
  551. ]
  552. ]);
  553. $expected = [
  554. 'div' => ['class'],
  555. 'label' => ['for'],
  556. 'Text in-label',
  557. '/label',
  558. 'in-group',
  559. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  560. 'in-container',
  561. '/div',
  562. ];
  563. $this->assertHtml($expected, $result);
  564. }
  565. /**
  566. * Test ensuring template variables work in template files loaded
  567. * during control().
  568. *
  569. * @return void
  570. */
  571. public function testControlTemplatesFromFile()
  572. {
  573. $result = $this->Form->control('title', [
  574. 'templates' => 'test_templates',
  575. 'templateVars' => [
  576. 'forcontainer' => 'container-data'
  577. ]
  578. ]);
  579. $expected = [
  580. 'div' => ['class'],
  581. 'label' => ['for'],
  582. 'Title',
  583. '/label',
  584. 'input' => ['name', 'type' => 'text', 'id'],
  585. 'container-data',
  586. '/div',
  587. ];
  588. $this->assertHtml($expected, $result);
  589. }
  590. /**
  591. * Test using template vars in inputSubmit and submitContainer template.
  592. *
  593. * @return void
  594. */
  595. public function testSubmitTemplateVars()
  596. {
  597. $this->Form->templates([
  598. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  599. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  600. ]);
  601. $result = $this->Form->submit('Submit', [
  602. 'templateVars' => [
  603. 'forinput' => 'in-input',
  604. 'forcontainer' => 'in-container'
  605. ]
  606. ]);
  607. $expected = [
  608. 'div' => ['class'],
  609. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  610. 'in-container',
  611. '/div',
  612. ];
  613. $this->assertHtml($expected, $result);
  614. }
  615. /**
  616. * test the create() method
  617. *
  618. * @dataProvider requestTypeProvider
  619. * @return void
  620. */
  621. public function testCreateTypeOptions($type, $method, $override)
  622. {
  623. $encoding = strtolower(Configure::read('App.encoding'));
  624. $result = $this->Form->create(false, ['type' => $type]);
  625. $expected = [
  626. 'form' => [
  627. 'method' => $method, 'action' => '/articles/add',
  628. 'accept-charset' => $encoding
  629. ],
  630. 'div' => ['style' => 'display:none;'],
  631. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  632. '/div'
  633. ];
  634. $this->assertHtml($expected, $result);
  635. }
  636. /**
  637. * Test using template vars in Create (formStart template)
  638. *
  639. * @return void
  640. */
  641. public function testCreateTemplateVars()
  642. {
  643. $result = $this->Form->create($this->article, [
  644. 'templates' => [
  645. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  646. ],
  647. 'templateVars' => ['header' => 'headertext']
  648. ]);
  649. $expected = [
  650. 'h4' => ['class'],
  651. 'headertext',
  652. '/h4',
  653. 'form' => [
  654. 'method' => 'post',
  655. 'action' => '/articles/add',
  656. 'accept-charset' => 'utf-8'
  657. ]
  658. ];
  659. $this->assertHtml($expected, $result);
  660. }
  661. /**
  662. * Test opening a form for an update operation.
  663. *
  664. * @return void
  665. */
  666. public function testCreateUpdateForm()
  667. {
  668. $encoding = strtolower(Configure::read('App.encoding'));
  669. $this->Form->request->here = '/articles/edit/1';
  670. $this->Form->request->params['action'] = 'edit';
  671. $this->article['defaults']['id'] = 1;
  672. $result = $this->Form->create($this->article);
  673. $expected = [
  674. 'form' => [
  675. 'method' => 'post',
  676. 'action' => '/articles/edit/1',
  677. 'accept-charset' => $encoding
  678. ],
  679. 'div' => ['style' => 'display:none;'],
  680. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  681. '/div'
  682. ];
  683. $this->assertHtml($expected, $result);
  684. }
  685. /**
  686. * test create() with automatic url generation
  687. *
  688. * @return void
  689. */
  690. public function testCreateAutoUrl()
  691. {
  692. $encoding = strtolower(Configure::read('App.encoding'));
  693. $this->Form->request->params['action'] = 'delete';
  694. $this->Form->request->here = '/articles/delete/10';
  695. $this->Form->request->base = '';
  696. $result = $this->Form->create($this->article);
  697. $expected = [
  698. 'form' => [
  699. 'method' => 'post', 'action' => '/articles/delete/10',
  700. 'accept-charset' => $encoding
  701. ],
  702. 'div' => ['style' => 'display:none;'],
  703. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  704. '/div'
  705. ];
  706. $this->assertHtml($expected, $result);
  707. $this->article['defaults'] = ['id' => 1];
  708. $this->Form->request->here = '/articles/edit/1';
  709. $this->Form->request->params['action'] = 'delete';
  710. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  711. $expected = [
  712. 'form' => [
  713. 'method' => 'post',
  714. 'action' => '/articles/edit/1',
  715. 'accept-charset' => $encoding
  716. ],
  717. 'div' => ['style' => 'display:none;'],
  718. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  719. '/div'
  720. ];
  721. $this->assertHtml($expected, $result);
  722. $this->Form->request->params['action'] = 'add';
  723. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  724. $expected = [
  725. 'form' => [
  726. 'method' => 'post',
  727. 'action' => '/articles/publish/1',
  728. 'accept-charset' => $encoding
  729. ],
  730. 'div' => ['style' => 'display:none;'],
  731. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  732. '/div'
  733. ];
  734. $this->assertHtml($expected, $result);
  735. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  736. $expected = [
  737. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  738. 'div' => ['style' => 'display:none;'],
  739. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  740. '/div'
  741. ];
  742. $this->assertHtml($expected, $result);
  743. $this->Form->request->params['controller'] = 'Pages';
  744. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  745. $expected = [
  746. 'form' => [
  747. 'method' => 'post', 'action' => '/Pages/signup/1',
  748. 'accept-charset' => $encoding
  749. ],
  750. 'div' => ['style' => 'display:none;'],
  751. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  752. '/div'
  753. ];
  754. $this->assertHtml($expected, $result);
  755. }
  756. /**
  757. * Test create() with no URL (no "action" attribute for <form> tag)
  758. *
  759. * @return void
  760. */
  761. public function testCreateNoUrl()
  762. {
  763. $result = $this->Form->create(false, ['url' => false]);
  764. $expected = [
  765. 'form' => [
  766. 'method' => 'post',
  767. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  768. ],
  769. 'div' => ['style' => 'display:none;'],
  770. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  771. '/div'
  772. ];
  773. $this->assertHtml($expected, $result);
  774. }
  775. /**
  776. * test create() with a custom route
  777. *
  778. * @return void
  779. */
  780. public function testCreateCustomRoute()
  781. {
  782. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  783. $encoding = strtolower(Configure::read('App.encoding'));
  784. $this->Form->request->params['controller'] = 'users';
  785. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  786. $expected = [
  787. 'form' => [
  788. 'method' => 'post', 'action' => '/login',
  789. 'accept-charset' => $encoding
  790. ],
  791. 'div' => ['style' => 'display:none;'],
  792. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  793. '/div'
  794. ];
  795. $this->assertHtml($expected, $result);
  796. Router::connect(
  797. '/new-article',
  798. ['controller' => 'articles', 'action' => 'myaction'],
  799. ['_name' => 'my-route']
  800. );
  801. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  802. $expected = [
  803. 'form' => [
  804. 'method' => 'post', 'action' => '/new-article',
  805. 'accept-charset' => $encoding,
  806. ],
  807. 'div' => ['style' => 'display:none;'],
  808. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  809. '/div'
  810. ];
  811. $this->assertHtml($expected, $result);
  812. }
  813. /**
  814. * test automatic accept-charset overriding
  815. *
  816. * @return void
  817. */
  818. public function testCreateWithAcceptCharset()
  819. {
  820. $result = $this->Form->create(
  821. $this->article,
  822. [
  823. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  824. ]
  825. );
  826. $expected = [
  827. 'form' => [
  828. 'method' => 'post', 'action' => '/articles',
  829. 'accept-charset' => 'iso-8859-1'
  830. ],
  831. 'div' => ['style' => 'display:none;'],
  832. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  833. '/div'
  834. ];
  835. $this->assertHtml($expected, $result);
  836. }
  837. /**
  838. * Test base form URL when url param is passed with multiple parameters (&)
  839. *
  840. */
  841. public function testCreateQueryStringRequest()
  842. {
  843. $encoding = strtolower(Configure::read('App.encoding'));
  844. $result = $this->Form->create($this->article, [
  845. 'type' => 'post',
  846. 'escape' => false,
  847. 'url' => [
  848. 'controller' => 'controller',
  849. 'action' => 'action',
  850. '?' => ['param1' => 'value1', 'param2' => 'value2']
  851. ]
  852. ]);
  853. $expected = [
  854. 'form' => [
  855. 'method' => 'post',
  856. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  857. 'accept-charset' => $encoding
  858. ],
  859. 'div' => ['style' => 'display:none;'],
  860. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  861. '/div'
  862. ];
  863. $this->assertHtml($expected, $result);
  864. $result = $this->Form->create($this->article, [
  865. 'type' => 'post',
  866. 'url' => [
  867. 'controller' => 'controller',
  868. 'action' => 'action',
  869. '?' => ['param1' => 'value1', 'param2' => 'value2']
  870. ]
  871. ]);
  872. $this->assertHtml($expected, $result);
  873. }
  874. /**
  875. * test that create() doesn't cause errors by multiple id's being in the primary key
  876. * as could happen with multiple select or checkboxes.
  877. *
  878. * @return void
  879. */
  880. public function testCreateWithMultipleIdInData()
  881. {
  882. $encoding = strtolower(Configure::read('App.encoding'));
  883. $this->Form->request->data['Article']['id'] = [1, 2];
  884. $result = $this->Form->create($this->article);
  885. $expected = [
  886. 'form' => [
  887. 'method' => 'post',
  888. 'action' => '/articles/add',
  889. 'accept-charset' => $encoding
  890. ],
  891. 'div' => ['style' => 'display:none;'],
  892. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  893. '/div'
  894. ];
  895. $this->assertHtml($expected, $result);
  896. }
  897. /**
  898. * test that create() doesn't add in extra passed params.
  899. *
  900. * @return void
  901. */
  902. public function testCreatePassedArgs()
  903. {
  904. $encoding = strtolower(Configure::read('App.encoding'));
  905. $this->Form->request->data['Article']['id'] = 1;
  906. $result = $this->Form->create($this->article, [
  907. 'type' => 'post',
  908. 'escape' => false,
  909. 'url' => [
  910. 'action' => 'edit',
  911. 'myparam'
  912. ]
  913. ]);
  914. $expected = [
  915. 'form' => [
  916. 'method' => 'post',
  917. 'action' => '/articles/edit/myparam',
  918. 'accept-charset' => $encoding
  919. ],
  920. 'div' => ['style' => 'display:none;'],
  921. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  922. '/div'
  923. ];
  924. $this->assertHtml($expected, $result);
  925. }
  926. /**
  927. * test creating a get form, and get form inputs.
  928. *
  929. * @return void
  930. */
  931. public function testGetFormCreate()
  932. {
  933. $encoding = strtolower(Configure::read('App.encoding'));
  934. $result = $this->Form->create($this->article, ['type' => 'get']);
  935. $expected = ['form' => [
  936. 'method' => 'get', 'action' => '/articles/add',
  937. 'accept-charset' => $encoding
  938. ]];
  939. $this->assertHtml($expected, $result);
  940. $result = $this->Form->text('title');
  941. $expected = ['input' => [
  942. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  943. ]];
  944. $this->assertHtml($expected, $result);
  945. $result = $this->Form->password('password');
  946. $expected = ['input' => [
  947. 'name' => 'password', 'type' => 'password'
  948. ]];
  949. $this->assertHtml($expected, $result);
  950. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  951. $result = $this->Form->text('user_form');
  952. $expected = ['input' => [
  953. 'name' => 'user_form', 'type' => 'text'
  954. ]];
  955. $this->assertHtml($expected, $result);
  956. }
  957. /**
  958. * test get form, and inputs when the model param is false
  959. *
  960. * @return void
  961. */
  962. public function testGetFormWithFalseModel()
  963. {
  964. $encoding = strtolower(Configure::read('App.encoding'));
  965. $this->Form->request->params['controller'] = 'contact_test';
  966. $result = $this->Form->create(false, [
  967. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  968. ]);
  969. $expected = ['form' => [
  970. 'method' => 'get', 'action' => '/contact_test/add',
  971. 'accept-charset' => $encoding
  972. ]];
  973. $this->assertHtml($expected, $result);
  974. $result = $this->Form->text('reason');
  975. $expected = [
  976. 'input' => ['type' => 'text', 'name' => 'reason']
  977. ];
  978. $this->assertHtml($expected, $result);
  979. }
  980. /**
  981. * testFormCreateWithSecurity method
  982. *
  983. * Test form->create() with security key.
  984. *
  985. * @return void
  986. */
  987. public function testCreateWithSecurity()
  988. {
  989. $this->Form->request->params['_csrfToken'] = 'testKey';
  990. $encoding = strtolower(Configure::read('App.encoding'));
  991. $result = $this->Form->create($this->article, [
  992. 'url' => '/articles/publish',
  993. ]);
  994. $expected = [
  995. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  996. 'div' => ['style' => 'display:none;'],
  997. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  998. ['input' => [
  999. 'type' => 'hidden',
  1000. 'name' => '_csrfToken',
  1001. 'value' => 'testKey',
  1002. 'autocomplete' => 'off',
  1003. ]],
  1004. '/div'
  1005. ];
  1006. $this->assertHtml($expected, $result);
  1007. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1008. $expected['form']['id'] = 'MyForm';
  1009. $this->assertHtml($expected, $result);
  1010. }
  1011. /**
  1012. * testFormCreateGetNoSecurity method
  1013. *
  1014. * Test form->create() with no security key as its a get form
  1015. *
  1016. * @return void
  1017. */
  1018. public function testCreateEndGetNoSecurity()
  1019. {
  1020. $this->Form->request->params['_csrfToken'] = 'testKey';
  1021. $article = new Article();
  1022. $result = $this->Form->create($article, [
  1023. 'type' => 'get',
  1024. 'url' => '/contacts/add'
  1025. ]);
  1026. $this->assertNotContains('testKey', $result);
  1027. $result = $this->Form->end();
  1028. $this->assertNotContains('testKey', $result);
  1029. }
  1030. /**
  1031. * test that create() clears the fields property so it starts fresh
  1032. *
  1033. * @return void
  1034. */
  1035. public function testCreateClearingFields()
  1036. {
  1037. $this->Form->fields = ['model_id'];
  1038. $this->Form->create($this->article);
  1039. $this->assertEquals([], $this->Form->fields);
  1040. }
  1041. /**
  1042. * Tests form hash generation with model-less data
  1043. *
  1044. * @return void
  1045. */
  1046. public function testValidateHashNoModel()
  1047. {
  1048. $this->Form->request->params['_Token'] = 'foo';
  1049. $fields = ['anything'];
  1050. $result = $this->Form->secure($fields);
  1051. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::salt());
  1052. $this->assertContains($hash, $result);
  1053. }
  1054. /**
  1055. * Tests that hidden fields generated for checkboxes don't get locked
  1056. *
  1057. * @return void
  1058. */
  1059. public function testNoCheckboxLocking()
  1060. {
  1061. $this->Form->request->params['_Token'] = 'foo';
  1062. $this->assertSame([], $this->Form->fields);
  1063. $this->Form->checkbox('check', ['value' => '1']);
  1064. $this->assertSame($this->Form->fields, ['check']);
  1065. }
  1066. /**
  1067. * testFormSecurityFields method
  1068. *
  1069. * Test generation of secure form hash generation.
  1070. *
  1071. * @return void
  1072. */
  1073. public function testFormSecurityFields()
  1074. {
  1075. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1076. $this->Form->request->params['_Token'] = 'testKey';
  1077. $result = $this->Form->secure($fields);
  1078. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::salt());
  1079. $hash .= ':' . 'Model.valid';
  1080. $hash = urlencode($hash);
  1081. $tokenDebug = urlencode(json_encode([
  1082. '',
  1083. $fields,
  1084. []
  1085. ]));
  1086. $expected = [
  1087. 'div' => ['style' => 'display:none;'],
  1088. ['input' => [
  1089. 'type' => 'hidden',
  1090. 'name' => '_Token[fields]',
  1091. 'value' => $hash,
  1092. 'autocomplete' => 'off',
  1093. ]],
  1094. ['input' => [
  1095. 'type' => 'hidden',
  1096. 'name' => '_Token[unlocked]',
  1097. 'value' => '',
  1098. 'autocomplete' => 'off',
  1099. ]],
  1100. ['input' => [
  1101. 'type' => 'hidden',
  1102. 'name' => '_Token[debug]',
  1103. 'value' => $tokenDebug,
  1104. 'autocomplete' => 'off',
  1105. ]],
  1106. '/div'
  1107. ];
  1108. $this->assertHtml($expected, $result);
  1109. }
  1110. /**
  1111. * testFormSecurityFields method
  1112. *
  1113. * Test debug token is not generated if debug is false
  1114. *
  1115. * @return void
  1116. */
  1117. public function testFormSecurityFieldsNoDebugMode()
  1118. {
  1119. Configure::write('debug', false);
  1120. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1121. $this->Form->request->params['_Token'] = 'testKey';
  1122. $result = $this->Form->secure($fields);
  1123. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::salt());
  1124. $hash .= ':' . 'Model.valid';
  1125. $hash = urlencode($hash);
  1126. $expected = [
  1127. 'div' => ['style' => 'display:none;'],
  1128. ['input' => [
  1129. 'type' => 'hidden',
  1130. 'name' => '_Token[fields]',
  1131. 'autocomplete' => 'off',
  1132. 'value' => $hash
  1133. ]],
  1134. ['input' => [
  1135. 'type' => 'hidden',
  1136. 'name' => '_Token[unlocked]',
  1137. 'autocomplete' => 'off',
  1138. 'value' => '',
  1139. ]],
  1140. '/div'
  1141. ];
  1142. $this->assertHtml($expected, $result);
  1143. }
  1144. /**
  1145. * Tests correct generation of number fields for smallint
  1146. *
  1147. * @return void
  1148. */
  1149. public function testTextFieldGenerationForSmallint()
  1150. {
  1151. $this->article['schema'] = [
  1152. 'foo' => [
  1153. 'type' => 'smallinteger',
  1154. 'null' => false,
  1155. 'default' => null,
  1156. 'length' => 10
  1157. ]
  1158. ];
  1159. $this->Form->create($this->article);
  1160. $result = $this->Form->control('foo');
  1161. $this->assertContains('class="input number"', $result);
  1162. $this->assertContains('type="number"', $result);
  1163. }
  1164. /**
  1165. * Tests correct generation of number fields for tinyint
  1166. *
  1167. * @return void
  1168. */
  1169. public function testTextFieldGenerationForTinyint()
  1170. {
  1171. $this->article['schema'] = [
  1172. 'foo' => [
  1173. 'type' => 'tinyinteger',
  1174. 'null' => false,
  1175. 'default' => null,
  1176. 'length' => 10
  1177. ]
  1178. ];
  1179. $this->Form->create($this->article);
  1180. $result = $this->Form->control('foo');
  1181. $this->assertContains('class="input number"', $result);
  1182. $this->assertContains('type="number"', $result);
  1183. }
  1184. /**
  1185. * Tests correct generation of number fields for double and float fields
  1186. *
  1187. * @return void
  1188. */
  1189. public function testTextFieldGenerationForFloats()
  1190. {
  1191. $this->article['schema'] = [
  1192. 'foo' => [
  1193. 'type' => 'float',
  1194. 'null' => false,
  1195. 'default' => null,
  1196. 'length' => 10
  1197. ]
  1198. ];
  1199. $this->Form->create($this->article);
  1200. $result = $this->Form->control('foo');
  1201. $expected = [
  1202. 'div' => ['class' => 'input number'],
  1203. 'label' => ['for' => 'foo'],
  1204. 'Foo',
  1205. '/label',
  1206. ['input' => [
  1207. 'type' => 'number',
  1208. 'name' => 'foo',
  1209. 'id' => 'foo',
  1210. 'step' => 'any'
  1211. ]],
  1212. '/div'
  1213. ];
  1214. $this->assertHtml($expected, $result);
  1215. $result = $this->Form->control('foo', ['step' => 0.5]);
  1216. $expected = [
  1217. 'div' => ['class' => 'input number'],
  1218. 'label' => ['for' => 'foo'],
  1219. 'Foo',
  1220. '/label',
  1221. ['input' => [
  1222. 'type' => 'number',
  1223. 'name' => 'foo',
  1224. 'id' => 'foo',
  1225. 'step' => '0.5'
  1226. ]],
  1227. '/div'
  1228. ];
  1229. $this->assertHtml($expected, $result);
  1230. }
  1231. /**
  1232. * Tests correct generation of number fields for integer fields
  1233. *
  1234. * @return void
  1235. */
  1236. public function testTextFieldTypeNumberGenerationForIntegers()
  1237. {
  1238. TableRegistry::get('Contacts', [
  1239. 'className' => __NAMESPACE__ . '\ContactsTable'
  1240. ]);
  1241. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1242. $result = $this->Form->control('age');
  1243. $expected = [
  1244. 'div' => ['class' => 'input number'],
  1245. 'label' => ['for' => 'age'],
  1246. 'Age',
  1247. '/label',
  1248. ['input' => [
  1249. 'type' => 'number', 'name' => 'age',
  1250. 'id' => 'age'
  1251. ]],
  1252. '/div'
  1253. ];
  1254. $this->assertHtml($expected, $result);
  1255. }
  1256. /**
  1257. * Tests correct generation of file upload fields for binary fields
  1258. *
  1259. * @return void
  1260. */
  1261. public function testFileUploadFieldTypeGenerationForBinaries()
  1262. {
  1263. $table = TableRegistry::get('Contacts', [
  1264. 'className' => __NAMESPACE__ . '\ContactsTable'
  1265. ]);
  1266. $table->schema(['foo' => [
  1267. 'type' => 'binary',
  1268. 'null' => false,
  1269. 'default' => null,
  1270. 'length' => 1024
  1271. ]]);
  1272. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1273. $result = $this->Form->control('foo');
  1274. $expected = [
  1275. 'div' => ['class' => 'input file'],
  1276. 'label' => ['for' => 'foo'],
  1277. 'Foo',
  1278. '/label',
  1279. ['input' => [
  1280. 'type' => 'file', 'name' => 'foo',
  1281. 'id' => 'foo'
  1282. ]],
  1283. '/div'
  1284. ];
  1285. $this->assertHtml($expected, $result);
  1286. }
  1287. /**
  1288. * testFormSecurityMultipleFields method
  1289. *
  1290. * Test secure() with multiple row form. Ensure hash is correct.
  1291. *
  1292. * @return void
  1293. */
  1294. public function testFormSecurityMultipleFields()
  1295. {
  1296. $this->Form->request->params['_Token'] = 'foo';
  1297. $fields = [
  1298. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1299. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1300. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1301. ];
  1302. $result = $this->Form->secure($fields);
  1303. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1304. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1305. $tokenDebug = urlencode(json_encode([
  1306. '',
  1307. $fields,
  1308. []
  1309. ]));
  1310. $expected = [
  1311. 'div' => ['style' => 'display:none;'],
  1312. ['input' => [
  1313. 'type' => 'hidden',
  1314. 'name' => '_Token[fields]',
  1315. 'value' => $hash,
  1316. 'autocomplete' => 'off',
  1317. ]],
  1318. ['input' => [
  1319. 'type' => 'hidden',
  1320. 'name' => '_Token[unlocked]',
  1321. 'autocomplete' => 'off',
  1322. 'value' => ''
  1323. ]],
  1324. ['input' => [
  1325. 'type' => 'hidden',
  1326. 'name' => '_Token[debug]',
  1327. 'value' => $tokenDebug,
  1328. 'autocomplete' => 'off',
  1329. ]],
  1330. '/div'
  1331. ];
  1332. $this->assertHtml($expected, $result);
  1333. }
  1334. /**
  1335. * testFormSecurityMultipleSubmitButtons
  1336. *
  1337. * test form submit generation and ensure that _Token is only created on end()
  1338. *
  1339. * @return void
  1340. */
  1341. public function testFormSecurityMultipleSubmitButtons()
  1342. {
  1343. $this->Form->request->params['_Token'] = 'testKey';
  1344. $this->Form->create($this->article);
  1345. $this->Form->text('Address.title');
  1346. $this->Form->text('Address.first_name');
  1347. $result = $this->Form->submit('Save', ['name' => 'save']);
  1348. $expected = [
  1349. 'div' => ['class' => 'submit'],
  1350. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1351. '/div',
  1352. ];
  1353. $this->assertHtml($expected, $result);
  1354. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1355. $expected = [
  1356. 'div' => ['class' => 'submit'],
  1357. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1358. '/div',
  1359. ];
  1360. $this->assertHtml($expected, $result);
  1361. $result = $this->Form->end();
  1362. $tokenDebug = urlencode(json_encode([
  1363. '/articles/add',
  1364. [
  1365. 'Address.title',
  1366. 'Address.first_name',
  1367. ],
  1368. ['save', 'cancel']
  1369. ]));
  1370. $expected = [
  1371. 'div' => ['style' => 'display:none;'],
  1372. ['input' => [
  1373. 'type' => 'hidden',
  1374. 'name' => '_Token[fields]',
  1375. 'autocomplete',
  1376. 'value'
  1377. ]],
  1378. ['input' => [
  1379. 'type' => 'hidden',
  1380. 'name' => '_Token[unlocked]',
  1381. 'value' => 'cancel%7Csave',
  1382. 'autocomplete' => 'off',
  1383. ]],
  1384. ['input' => [
  1385. 'type' => 'hidden',
  1386. 'name' => '_Token[debug]',
  1387. 'value' => $tokenDebug,
  1388. 'autocomplete' => 'off',
  1389. ]],
  1390. '/div'
  1391. ];
  1392. $this->assertHtml($expected, $result);
  1393. }
  1394. /**
  1395. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1396. *
  1397. * @return void
  1398. */
  1399. public function testSecurityButtonNestedNamed()
  1400. {
  1401. $key = 'testKey';
  1402. $this->Form->request->params['_csrfToken'] = $key;
  1403. $this->Form->create('Addresses');
  1404. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1405. $result = $this->Form->unlockField();
  1406. $this->assertEquals(['Address.button'], $result);
  1407. }
  1408. /**
  1409. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1410. *
  1411. * @return void
  1412. */
  1413. public function testSecuritySubmitNestedNamed()
  1414. {
  1415. $this->Form->request->params['_Token'] = 'testKey';
  1416. $this->Form->create($this->article);
  1417. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1418. $result = $this->Form->unlockField();
  1419. $this->assertEquals(['Address.button'], $result);
  1420. }
  1421. /**
  1422. * Test that the correct fields are unlocked for image submits with no names.
  1423. *
  1424. * @return void
  1425. */
  1426. public function testSecuritySubmitImageNoName()
  1427. {
  1428. $this->Form->request->params['_Token'] = 'testKey';
  1429. $this->Form->create(false);
  1430. $result = $this->Form->submit('save.png');
  1431. $expected = [
  1432. 'div' => ['class' => 'submit'],
  1433. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1434. '/div'
  1435. ];
  1436. $this->assertHtml($expected, $result);
  1437. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1438. }
  1439. /**
  1440. * Test that the correct fields are unlocked for image submits with names.
  1441. *
  1442. * @return void
  1443. */
  1444. public function testSecuritySubmitImageName()
  1445. {
  1446. $this->Form->request->params['_Token'] = 'testKey';
  1447. $this->Form->create(null);
  1448. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1449. $expected = [
  1450. 'div' => ['class' => 'submit'],
  1451. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1452. '/div'
  1453. ];
  1454. $this->assertHtml($expected, $result);
  1455. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1456. }
  1457. /**
  1458. * testFormSecurityMultipleControlFields method
  1459. *
  1460. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1461. *
  1462. * @return void
  1463. */
  1464. public function testFormSecurityMultipleControlFields()
  1465. {
  1466. $this->Form->request->params['_Token'] = 'testKey';
  1467. $this->Form->create();
  1468. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1469. $this->Form->control('Addresses.0.title');
  1470. $this->Form->control('Addresses.0.first_name');
  1471. $this->Form->control('Addresses.0.last_name');
  1472. $this->Form->control('Addresses.0.address');
  1473. $this->Form->control('Addresses.0.city');
  1474. $this->Form->control('Addresses.0.phone');
  1475. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1476. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1477. $this->Form->control('Addresses.1.title');
  1478. $this->Form->control('Addresses.1.first_name');
  1479. $this->Form->control('Addresses.1.last_name');
  1480. $this->Form->control('Addresses.1.address');
  1481. $this->Form->control('Addresses.1.city');
  1482. $this->Form->control('Addresses.1.phone');
  1483. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1484. $result = $this->Form->secure($this->Form->fields);
  1485. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1486. $tokenDebug = urlencode(json_encode([
  1487. '/articles/add',
  1488. [
  1489. 'Addresses.0.id' => '123456',
  1490. 'Addresses.0.title',
  1491. 'Addresses.0.first_name',
  1492. 'Addresses.0.last_name',
  1493. 'Addresses.0.address',
  1494. 'Addresses.0.city',
  1495. 'Addresses.0.phone',
  1496. 'Addresses.0.primary',
  1497. 'Addresses.1.id' => '654321',
  1498. 'Addresses.1.title',
  1499. 'Addresses.1.first_name',
  1500. 'Addresses.1.last_name',
  1501. 'Addresses.1.address',
  1502. 'Addresses.1.city',
  1503. 'Addresses.1.phone',
  1504. 'Addresses.1.primary',
  1505. ],
  1506. []
  1507. ]));
  1508. $expected = [
  1509. 'div' => ['style' => 'display:none;'],
  1510. ['input' => [
  1511. 'type' => 'hidden',
  1512. 'name' => '_Token[fields]',
  1513. 'value' => $hash,
  1514. 'autocomplete' => 'off',
  1515. ]],
  1516. ['input' => [
  1517. 'type' => 'hidden',
  1518. 'name' => '_Token[unlocked]',
  1519. 'autocomplete' => 'off',
  1520. 'value' => ''
  1521. ]],
  1522. ['input' => [
  1523. 'type' => 'hidden',
  1524. 'name' => '_Token[debug]',
  1525. 'value' => $tokenDebug,
  1526. 'autocomplete' => 'off',
  1527. ]],
  1528. '/div'
  1529. ];
  1530. $this->assertHtml($expected, $result);
  1531. }
  1532. /**
  1533. * testFormSecurityArrayFields method
  1534. *
  1535. * Test form security with Model.field.0 style inputs.
  1536. *
  1537. * @return void
  1538. */
  1539. public function testFormSecurityArrayFields()
  1540. {
  1541. $this->Form->request->params['_Token'] = 'testKey';
  1542. $this->Form->create();
  1543. $this->Form->text('Address.primary.1');
  1544. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1545. $this->Form->text('Address.secondary.1.0');
  1546. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1547. }
  1548. /**
  1549. * testFormSecurityMultipleControlDisabledFields method
  1550. *
  1551. * Test secure form generation with multiple records and disabled fields.
  1552. *
  1553. * @return void
  1554. */
  1555. public function testFormSecurityMultipleControlDisabledFields()
  1556. {
  1557. $this->Form->request->params['_Token'] = [
  1558. 'unlockedFields' => ['first_name', 'address']
  1559. ];
  1560. $this->Form->create();
  1561. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1562. $this->Form->text('Addresses.0.title');
  1563. $this->Form->text('Addresses.0.first_name');
  1564. $this->Form->text('Addresses.0.last_name');
  1565. $this->Form->text('Addresses.0.address');
  1566. $this->Form->text('Addresses.0.city');
  1567. $this->Form->text('Addresses.0.phone');
  1568. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1569. $this->Form->text('Addresses.1.title');
  1570. $this->Form->text('Addresses.1.first_name');
  1571. $this->Form->text('Addresses.1.last_name');
  1572. $this->Form->text('Addresses.1.address');
  1573. $this->Form->text('Addresses.1.city');
  1574. $this->Form->text('Addresses.1.phone');
  1575. $result = $this->Form->secure($this->Form->fields);
  1576. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1577. $tokenDebug = urlencode(json_encode([
  1578. '/articles/add',
  1579. [
  1580. 'Addresses.0.id' => '123456',
  1581. 'Addresses.0.title',
  1582. 'Addresses.0.last_name',
  1583. 'Addresses.0.city',
  1584. 'Addresses.0.phone',
  1585. 'Addresses.1.id' => '654321',
  1586. 'Addresses.1.title',
  1587. 'Addresses.1.last_name',
  1588. 'Addresses.1.city',
  1589. 'Addresses.1.phone'
  1590. ],
  1591. [
  1592. 'first_name',
  1593. 'address'
  1594. ]
  1595. ]));
  1596. $expected = [
  1597. 'div' => ['style' => 'display:none;'],
  1598. ['input' => [
  1599. 'type' => 'hidden',
  1600. 'name' => '_Token[fields]',
  1601. 'autocomplete' => 'off',
  1602. 'value' => $hash
  1603. ]],
  1604. ['input' => [
  1605. 'type' => 'hidden',
  1606. 'name' => '_Token[unlocked]',
  1607. 'autocomplete' => 'off',
  1608. 'value' => 'address%7Cfirst_name',
  1609. ]],
  1610. ['input' => [
  1611. 'type' => 'hidden',
  1612. 'name' => '_Token[debug]',
  1613. 'autocomplete' => 'off',
  1614. 'value' => $tokenDebug,
  1615. ]],
  1616. '/div'
  1617. ];
  1618. $this->assertHtml($expected, $result);
  1619. }
  1620. /**
  1621. * testFormSecurityControlDisabledFields method
  1622. *
  1623. * Test single record form with disabled fields.
  1624. *
  1625. * @return void
  1626. */
  1627. public function testFormSecurityControlUnlockedFields()
  1628. {
  1629. $this->Form->request->params['_Token'] = [
  1630. 'unlockedFields' => ['first_name', 'address']
  1631. ];
  1632. $this->Form->create();
  1633. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1634. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1635. $this->Form->text('Addresses.title');
  1636. $this->Form->text('Addresses.first_name');
  1637. $this->Form->text('Addresses.last_name');
  1638. $this->Form->text('Addresses.address');
  1639. $this->Form->text('Addresses.city');
  1640. $this->Form->text('Addresses.phone');
  1641. $result = $this->Form->fields;
  1642. $expected = [
  1643. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1644. 'Addresses.city', 'Addresses.phone'
  1645. ];
  1646. $this->assertEquals($expected, $result);
  1647. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1648. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1649. $tokenDebug = urlencode(json_encode([
  1650. '/articles/add',
  1651. [
  1652. 'Addresses.id' => '123456',
  1653. 'Addresses.title',
  1654. 'Addresses.last_name',
  1655. 'Addresses.city',
  1656. 'Addresses.phone'
  1657. ],
  1658. [
  1659. 'first_name',
  1660. 'address'
  1661. ]
  1662. ]));
  1663. $expected = [
  1664. 'div' => ['style' => 'display:none;'],
  1665. ['input' => [
  1666. 'type' => 'hidden',
  1667. 'name' => '_Token[fields]',
  1668. 'value' => $hash,
  1669. 'autocomplete' => 'off',
  1670. 'data-foo' => 'bar',
  1671. ]],
  1672. ['input' => [
  1673. 'type' => 'hidden',
  1674. 'name' => '_Token[unlocked]',
  1675. 'value' => 'address%7Cfirst_name',
  1676. 'autocomplete' => 'off',
  1677. 'data-foo' => 'bar',
  1678. ]],
  1679. ['input' => [
  1680. 'type' => 'hidden', 'name' => '_Token[debug]',
  1681. 'value' => $tokenDebug,
  1682. 'autocomplete' => 'off',
  1683. 'data-foo' => 'bar'
  1684. ]],
  1685. '/div'
  1686. ];
  1687. $this->assertHtml($expected, $result);
  1688. }
  1689. /**
  1690. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1691. *
  1692. * Test single record form with debugSecurity param.
  1693. *
  1694. * @return void
  1695. */
  1696. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1697. {
  1698. $this->Form->request->params['_Token'] = [
  1699. 'unlockedFields' => ['first_name', 'address']
  1700. ];
  1701. $this->Form->create();
  1702. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1703. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1704. $this->Form->text('Addresses.title');
  1705. $this->Form->text('Addresses.first_name');
  1706. $this->Form->text('Addresses.last_name');
  1707. $this->Form->text('Addresses.address');
  1708. $this->Form->text('Addresses.city');
  1709. $this->Form->text('Addresses.phone');
  1710. $result = $this->Form->fields;
  1711. $expected = [
  1712. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1713. 'Addresses.city', 'Addresses.phone'
  1714. ];
  1715. $this->assertEquals($expected, $result);
  1716. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1717. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1718. $tokenDebug = urlencode(json_encode([
  1719. '/articles/add',
  1720. [
  1721. 'Addresses.id' => '123456',
  1722. 'Addresses.title',
  1723. 'Addresses.last_name',
  1724. 'Addresses.city',
  1725. 'Addresses.phone'
  1726. ],
  1727. [
  1728. 'first_name',
  1729. 'address'
  1730. ]
  1731. ]));
  1732. $expected = [
  1733. 'div' => ['style' => 'display:none;'],
  1734. ['input' => [
  1735. 'type' => 'hidden',
  1736. 'name' => '_Token[fields]',
  1737. 'value' => $hash,
  1738. 'autocomplete' => 'off',
  1739. 'data-foo' => 'bar',
  1740. ]],
  1741. ['input' => [
  1742. 'type' => 'hidden',
  1743. 'name' => '_Token[unlocked]',
  1744. 'value' => 'address%7Cfirst_name',
  1745. 'autocomplete' => 'off',
  1746. 'data-foo' => 'bar',
  1747. ]],
  1748. ['input' => [
  1749. 'type' => 'hidden', 'name' => '_Token[debug]',
  1750. 'value' => $tokenDebug,
  1751. 'autocomplete' => 'off',
  1752. 'data-foo' => 'bar'
  1753. ]],
  1754. '/div'
  1755. ];
  1756. $this->assertHtml($expected, $result);
  1757. }
  1758. /**
  1759. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1760. *
  1761. * Debug is false, debugSecurity is true -> no debug
  1762. *
  1763. * @return void
  1764. */
  1765. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1766. {
  1767. $this->Form->request->params['_Token'] = [
  1768. 'unlockedFields' => ['first_name', 'address']
  1769. ];
  1770. $this->Form->create();
  1771. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1772. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1773. $this->Form->text('Addresses.title');
  1774. $this->Form->text('Addresses.first_name');
  1775. $this->Form->text('Addresses.last_name');
  1776. $this->Form->text('Addresses.address');
  1777. $this->Form->text('Addresses.city');
  1778. $this->Form->text('Addresses.phone');
  1779. $result = $this->Form->fields;
  1780. $expected = [
  1781. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1782. 'Addresses.city', 'Addresses.phone'
  1783. ];
  1784. $this->assertEquals($expected, $result);
  1785. Configure::write('debug', false);
  1786. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1787. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1788. $expected = [
  1789. 'div' => ['style' => 'display:none;'],
  1790. ['input' => [
  1791. 'type' => 'hidden',
  1792. 'name' => '_Token[fields]',
  1793. 'value' => $hash,
  1794. 'autocomplete' => 'off',
  1795. 'data-foo' => 'bar',
  1796. ]],
  1797. ['input' => [
  1798. 'type' => 'hidden',
  1799. 'name' => '_Token[unlocked]',
  1800. 'value' => 'address%7Cfirst_name',
  1801. 'autocomplete' => 'off',
  1802. 'data-foo' => 'bar',
  1803. ]],
  1804. '/div'
  1805. ];
  1806. $this->assertHtml($expected, $result);
  1807. }
  1808. /**
  1809. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1810. *
  1811. * Test single record form with debugSecurity param.
  1812. *
  1813. * @return void
  1814. */
  1815. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1816. {
  1817. $this->Form->request->params['_Token'] = [
  1818. 'unlockedFields' => ['first_name', 'address']
  1819. ];
  1820. $this->Form->create();
  1821. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1822. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1823. $this->Form->text('Addresses.title');
  1824. $this->Form->text('Addresses.first_name');
  1825. $this->Form->text('Addresses.last_name');
  1826. $this->Form->text('Addresses.address');
  1827. $this->Form->text('Addresses.city');
  1828. $this->Form->text('Addresses.phone');
  1829. $result = $this->Form->fields;
  1830. $expected = [
  1831. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1832. 'Addresses.city', 'Addresses.phone'
  1833. ];
  1834. $this->assertEquals($expected, $result);
  1835. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1836. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1837. $expected = [
  1838. 'div' => ['style' => 'display:none;'],
  1839. ['input' => [
  1840. 'type' => 'hidden',
  1841. 'name' => '_Token[fields]',
  1842. 'value' => $hash,
  1843. 'autocomplete' => 'off',
  1844. 'data-foo' => 'bar',
  1845. ]],
  1846. ['input' => [
  1847. 'type' => 'hidden',
  1848. 'name' => '_Token[unlocked]',
  1849. 'value' => 'address%7Cfirst_name',
  1850. 'autocomplete' => 'off',
  1851. 'data-foo' => 'bar',
  1852. ]],
  1853. '/div'
  1854. ];
  1855. $this->assertHtml($expected, $result);
  1856. }
  1857. /**
  1858. * testFormSecureWithCustomNameAttribute method
  1859. *
  1860. * Test securing inputs with custom name attributes.
  1861. *
  1862. * @return void
  1863. */
  1864. public function testFormSecureWithCustomNameAttribute()
  1865. {
  1866. $this->Form->request->params['_Token'] = 'testKey';
  1867. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1868. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1869. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1870. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1871. }
  1872. /**
  1873. * testFormSecuredControl method
  1874. *
  1875. * Test generation of entire secure form, assertions made on control() output.
  1876. *
  1877. * @return void
  1878. */
  1879. public function testFormSecuredControl()
  1880. {
  1881. $this->Form->request->params['_csrfToken'] = 'testKey';
  1882. $this->Form->request->params['_Token'] = 'stuff';
  1883. $this->article['schema'] = [
  1884. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1885. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1886. ];
  1887. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1888. $encoding = strtolower(Configure::read('App.encoding'));
  1889. $expected = [
  1890. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1891. 'div' => ['style' => 'display:none;'],
  1892. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1893. ['input' => [
  1894. 'type' => 'hidden',
  1895. 'name' => '_csrfToken',
  1896. 'value' => 'testKey',
  1897. 'autocomplete' => 'off'
  1898. ]],
  1899. '/div'
  1900. ];
  1901. $this->assertHtml($expected, $result);
  1902. $result = $this->Form->control('ratio');
  1903. $expected = [
  1904. 'div' => ['class'],
  1905. 'label' => ['for'],
  1906. 'Ratio',
  1907. '/label',
  1908. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1909. '/div',
  1910. ];
  1911. $this->assertHtml($expected, $result);
  1912. $result = $this->Form->control('population');
  1913. $expected = [
  1914. 'div' => ['class'],
  1915. 'label' => ['for'],
  1916. 'Population',
  1917. '/label',
  1918. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1919. '/div',
  1920. ];
  1921. $this->assertHtml($expected, $result);
  1922. $result = $this->Form->control('published', ['type' => 'text']);
  1923. $expected = [
  1924. 'div' => ['class' => 'input text'],
  1925. 'label' => ['for' => 'published'],
  1926. 'Published',
  1927. '/label',
  1928. ['input' => [
  1929. 'type' => 'text',
  1930. 'name' => 'published',
  1931. 'id' => 'published'
  1932. ]],
  1933. '/div'
  1934. ];
  1935. $this->assertHtml($expected, $result);
  1936. $result = $this->Form->control('other', ['type' => 'text']);
  1937. $expected = [
  1938. 'div' => ['class' => 'input text'],
  1939. 'label' => ['for' => 'other'],
  1940. 'Other',
  1941. '/label',
  1942. ['input' => [
  1943. 'type' => 'text',
  1944. 'name' => 'other',
  1945. 'id',
  1946. ]],
  1947. '/div'
  1948. ];
  1949. $this->assertHtml($expected, $result);
  1950. $result = $this->Form->hidden('stuff');
  1951. $expected = [
  1952. 'input' => [
  1953. 'type' => 'hidden',
  1954. 'name' => 'stuff'
  1955. ]
  1956. ];
  1957. $this->assertHtml($expected, $result);
  1958. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1959. $expected = ['input' => [
  1960. 'type' => 'hidden',
  1961. 'name' => 'hidden',
  1962. 'value' => '0'
  1963. ]];
  1964. $this->assertHtml($expected, $result);
  1965. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1966. $expected = [
  1967. 'div' => ['class' => 'input checkbox'],
  1968. ['input' => [
  1969. 'type' => 'hidden',
  1970. 'name' => 'something',
  1971. 'value' => '0'
  1972. ]],
  1973. 'label' => ['for' => 'something'],
  1974. ['input' => [
  1975. 'type' => 'checkbox',
  1976. 'name' => 'something',
  1977. 'value' => '1',
  1978. 'id' => 'something'
  1979. ]],
  1980. 'Something',
  1981. '/label',
  1982. '/div'
  1983. ];
  1984. $this->assertHtml($expected, $result);
  1985. $result = $this->Form->fields;
  1986. $expectedFields = [
  1987. 'ratio',
  1988. 'population',
  1989. 'published',
  1990. 'other',
  1991. 'stuff' => '',
  1992. 'hidden' => '0',
  1993. 'something'
  1994. ];
  1995. $this->assertEquals($expectedFields, $result);
  1996. $result = $this->Form->secure($this->Form->fields);
  1997. $tokenDebug = urlencode(json_encode([
  1998. '/articles/add',
  1999. $expectedFields,
  2000. []
  2001. ]));
  2002. $expected = [
  2003. 'div' => ['style' => 'display:none;'],
  2004. ['input' => [
  2005. 'type' => 'hidden',
  2006. 'name' => '_Token[fields]',
  2007. 'value',
  2008. 'autocomplete'
  2009. ]],
  2010. ['input' => [
  2011. 'type' => 'hidden',
  2012. 'name' => '_Token[unlocked]',
  2013. 'value' => '',
  2014. 'autocomplete' => 'off'
  2015. ]],
  2016. ['input' => [
  2017. 'type' => 'hidden', 'name' => '_Token[debug]',
  2018. 'value' => $tokenDebug,
  2019. 'autocomplete' => 'off'
  2020. ]],
  2021. '/div'
  2022. ];
  2023. $this->assertHtml($expected, $result);
  2024. }
  2025. /**
  2026. * testSecuredControlCustomName method
  2027. *
  2028. * Test secured inputs with custom names.
  2029. *
  2030. * @return void
  2031. */
  2032. public function testSecuredControlCustomName()
  2033. {
  2034. $this->Form->request->params['_Token'] = 'testKey';
  2035. $this->assertEquals([], $this->Form->fields);
  2036. $this->Form->text('text_input', [
  2037. 'name' => 'Option[General.default_role]',
  2038. ]);
  2039. $expected = ['Option.General.default_role'];
  2040. $this->assertEquals($expected, $this->Form->fields);
  2041. $this->Form->select('select_box', [1, 2], [
  2042. 'name' => 'Option[General.select_role]',
  2043. ]);
  2044. $expected[] = 'Option.General.select_role';
  2045. $this->assertEquals($expected, $this->Form->fields);
  2046. $this->Form->text('other.things[]');
  2047. $expected[] = 'other.things';
  2048. $this->assertEquals($expected, $this->Form->fields);
  2049. }
  2050. /**
  2051. * testSecuredControlDuplicate method
  2052. *
  2053. * Test that a hidden field followed by a visible field
  2054. * undoes the hidden field locking.
  2055. *
  2056. * @return void
  2057. */
  2058. public function testSecuredControlDuplicate()
  2059. {
  2060. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2061. $this->assertEquals([], $this->Form->fields);
  2062. $this->Form->control('text_val', [
  2063. 'type' => 'hidden',
  2064. 'value' => 'some text',
  2065. ]);
  2066. $expected = ['text_val' => 'some text'];
  2067. $this->assertEquals($expected, $this->Form->fields);
  2068. $this->Form->control('text_val', [
  2069. 'type' => 'text',
  2070. ]);
  2071. $expected = ['text_val'];
  2072. $this->assertEquals($expected, $this->Form->fields);
  2073. }
  2074. /**
  2075. * testFormSecuredFileControl method
  2076. *
  2077. * Tests that the correct keys are added to the field hash index.
  2078. *
  2079. * @return void
  2080. */
  2081. public function testFormSecuredFileControl()
  2082. {
  2083. $this->assertEquals([], $this->Form->fields);
  2084. $this->Form->file('Attachment.file');
  2085. $expected = [
  2086. 'Attachment.file.name', 'Attachment.file.type',
  2087. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2088. 'Attachment.file.size'
  2089. ];
  2090. $this->assertEquals($expected, $this->Form->fields);
  2091. }
  2092. /**
  2093. * testFormSecuredMultipleSelect method
  2094. *
  2095. * Test that multiple selects keys are added to field hash.
  2096. *
  2097. * @return void
  2098. */
  2099. public function testFormSecuredMultipleSelect()
  2100. {
  2101. $this->Form->request->params['_csrfToken'] = 'testKey';
  2102. $this->assertEquals([], $this->Form->fields);
  2103. $options = ['1' => 'one', '2' => 'two'];
  2104. $this->Form->select('Model.select', $options);
  2105. $expected = ['Model.select'];
  2106. $this->assertEquals($expected, $this->Form->fields);
  2107. $this->Form->fields = [];
  2108. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2109. $this->assertEquals($expected, $this->Form->fields);
  2110. }
  2111. /**
  2112. * testFormSecuredRadio method
  2113. *
  2114. * @return void
  2115. */
  2116. public function testFormSecuredRadio()
  2117. {
  2118. $this->Form->request->params['_Token'] = 'testKey';
  2119. $this->assertEquals([], $this->Form->fields);
  2120. $options = ['1' => 'option1', '2' => 'option2'];
  2121. $this->Form->radio('Test.test', $options);
  2122. $expected = ['Test.test'];
  2123. $this->assertEquals($expected, $this->Form->fields);
  2124. $this->Form->radio('Test.all', $options, [
  2125. 'disabled' => ['option1', 'option2']
  2126. ]);
  2127. $expected = ['Test.test', 'Test.all' => ''];
  2128. $this->assertEquals($expected, $this->Form->fields);
  2129. $this->Form->radio('Test.some', $options, [
  2130. 'disabled' => ['option1']
  2131. ]);
  2132. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2133. $this->assertEquals($expected, $this->Form->fields);
  2134. }
  2135. /**
  2136. * testFormSecuredAndDisabledNotAssoc method
  2137. *
  2138. * Test that when disabled is in a list based attribute array it works.
  2139. *
  2140. * @return void
  2141. */
  2142. public function testFormSecuredAndDisabledNotAssoc()
  2143. {
  2144. $this->Form->request->params['_Token'] = 'testKey';
  2145. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2146. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2147. $this->Form->text('Model.text', ['disabled']);
  2148. $this->Form->textarea('Model.textarea', ['disabled']);
  2149. $this->Form->password('Model.password', ['disabled']);
  2150. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2151. $expected = [
  2152. 'Model.radio' => ''
  2153. ];
  2154. $this->assertEquals($expected, $this->Form->fields);
  2155. }
  2156. /**
  2157. * testFormSecuredAndDisabled method
  2158. *
  2159. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2160. * hashing.
  2161. *
  2162. * @return void
  2163. */
  2164. public function testFormSecuredAndDisabled()
  2165. {
  2166. $this->Form->request->params['_Token'] = 'testKey';
  2167. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2168. $this->Form->text('Model.text', ['disabled' => true]);
  2169. $this->Form->password('Model.text', ['disabled' => true]);
  2170. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2171. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2172. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2173. $this->Form->year('Model.year', ['disabled' => true]);
  2174. $this->Form->month('Model.month', ['disabled' => true]);
  2175. $this->Form->day('Model.day', ['disabled' => true]);
  2176. $this->Form->hour('Model.hour', ['disabled' => true]);
  2177. $this->Form->minute('Model.minute', ['disabled' => true]);
  2178. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2179. $expected = [
  2180. 'Model.radio' => ''
  2181. ];
  2182. $this->assertEquals($expected, $this->Form->fields);
  2183. }
  2184. /**
  2185. * testDisableSecurityUsingForm method
  2186. *
  2187. * @return void
  2188. */
  2189. public function testDisableSecurityUsingForm()
  2190. {
  2191. $this->Form->request->params['_Token'] = [
  2192. 'disabledFields' => []
  2193. ];
  2194. $this->Form->create();
  2195. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2196. $this->Form->text('Addresses.title');
  2197. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2198. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2199. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2200. $result = $this->Form->fields;
  2201. $expected = [
  2202. 'Addresses.id' => '123456', 'Addresses.title',
  2203. ];
  2204. $this->assertEquals($expected, $result);
  2205. }
  2206. /**
  2207. * testUnlockFieldAddsToList method
  2208. *
  2209. * Test disableField.
  2210. *
  2211. * @return void
  2212. */
  2213. public function testUnlockFieldAddsToList()
  2214. {
  2215. $this->Form->request->params['_Token'] = [
  2216. 'unlockedFields' => []
  2217. ];
  2218. $this->Form->unlockField('Contact.name');
  2219. $this->Form->text('Contact.name');
  2220. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2221. $this->assertEquals([], $this->Form->fields);
  2222. }
  2223. /**
  2224. * testUnlockFieldRemovingFromFields method
  2225. *
  2226. * Test unlockField removing from fields array.
  2227. *
  2228. * @return void
  2229. */
  2230. public function testUnlockFieldRemovingFromFields()
  2231. {
  2232. $this->Form->request->params['_Token'] = [
  2233. 'unlockedFields' => []
  2234. ];
  2235. $this->Form->create($this->article);
  2236. $this->Form->hidden('Article.id', ['value' => 1]);
  2237. $this->Form->text('Article.title');
  2238. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2239. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2240. $this->Form->unlockField('Article.title');
  2241. $this->Form->unlockField('Article.id');
  2242. $this->assertEquals([], $this->Form->fields);
  2243. }
  2244. /**
  2245. * testResetUnlockFields method
  2246. *
  2247. * Test reset unlockFields, when create new form.
  2248. *
  2249. * @return void
  2250. */
  2251. public function testResetUnlockFields()
  2252. {
  2253. $this->Form->request->params['_Token'] = [
  2254. 'key' => 'testKey',
  2255. 'unlockedFields' => []
  2256. ];
  2257. $this->Form->unlockField('Contact.id');
  2258. $this->Form->create('Contact');
  2259. $this->Form->hidden('Contact.id', ['value' => 1]);
  2260. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2261. $this->Form->end();
  2262. $this->Form->create('Contact');
  2263. $this->Form->hidden('Contact.id', ['value' => 1]);
  2264. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2265. }
  2266. /**
  2267. * testSecuredFormUrlIgnoresHost method
  2268. *
  2269. * Test that only the path + query elements of a form's URL show up in their hash.
  2270. *
  2271. * @return void
  2272. */
  2273. public function testSecuredFormUrlIgnoresHost()
  2274. {
  2275. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2276. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2277. $this->Form->create($this->article, [
  2278. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2279. ]);
  2280. $result = $this->Form->secure();
  2281. $this->assertContains($expected, $result);
  2282. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2283. $result = $this->Form->secure();
  2284. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2285. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2286. $result = $this->Form->secure();
  2287. $this->assertContains($expected, $result, 'URL path + query should work.');
  2288. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2289. $result = $this->Form->secure();
  2290. $this->assertNotContains($expected, $result, 'URL is different');
  2291. }
  2292. /**
  2293. * testSecuredFormUrlHasHtmlAndIdentifier method
  2294. *
  2295. * Test that URL, HTML and identifier show up in their hashes.
  2296. *
  2297. * @return void
  2298. */
  2299. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2300. {
  2301. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2302. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2303. $this->Form->create($this->article, [
  2304. 'url' => [
  2305. 'controller' => 'articles',
  2306. 'action' => 'view',
  2307. '?' => [
  2308. 'page' => 1,
  2309. 'limit' => 10,
  2310. 'html' => '<>"',
  2311. ],
  2312. '#' => 'result',
  2313. ],
  2314. ]);
  2315. $result = $this->Form->secure();
  2316. $this->assertContains($expected, $result);
  2317. $this->Form->create($this->article, [
  2318. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2319. ]);
  2320. $result = $this->Form->secure();
  2321. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2322. $this->Form->create($this->article, [
  2323. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2324. ]);
  2325. $result = $this->Form->secure();
  2326. $this->assertContains($expected, $result, 'URL path + query should work.');
  2327. }
  2328. /**
  2329. * testErrorMessageDisplay method
  2330. *
  2331. * Test error message display.
  2332. *
  2333. * @return void
  2334. */
  2335. public function testErrorMessageDisplay()
  2336. {
  2337. $this->article['errors'] = [
  2338. 'Article' => [
  2339. 'title' => 'error message',
  2340. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2341. ]
  2342. ];
  2343. $this->Form->create($this->article);
  2344. $result = $this->Form->control('Article.title');
  2345. $expected = [
  2346. 'div' => ['class' => 'input text error'],
  2347. 'label' => ['for' => 'article-title'],
  2348. 'Title',
  2349. '/label',
  2350. 'input' => [
  2351. 'type' => 'text', 'name' => 'Article[title]',
  2352. 'id' => 'article-title', 'class' => 'form-error'
  2353. ],
  2354. ['div' => ['class' => 'error-message']],
  2355. 'error message',
  2356. '/div',
  2357. '/div'
  2358. ];
  2359. $this->assertHtml($expected, $result);
  2360. $result = $this->Form->control('Article.title', [
  2361. 'templates' => [
  2362. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2363. ]
  2364. ]);
  2365. $expected = [
  2366. 'div' => ['class' => 'input text error'],
  2367. 'label' => ['for' => 'article-title'],
  2368. 'Title',
  2369. '/label',
  2370. 'input' => [
  2371. 'type' => 'text', 'name' => 'Article[title]',
  2372. 'id' => 'article-title', 'class' => 'form-error'
  2373. ],
  2374. '/div'
  2375. ];
  2376. $this->assertHtml($expected, $result);
  2377. $result = $this->Form->control('Article.content');
  2378. $expected = [
  2379. 'div' => ['class' => 'input text error'],
  2380. 'label' => ['for' => 'article-content'],
  2381. 'Content',
  2382. '/label',
  2383. 'input' => [
  2384. 'type' => 'text', 'name' => 'Article[content]',
  2385. 'id' => 'article-content', 'class' => 'form-error'
  2386. ],
  2387. ['div' => ['class' => 'error-message']],
  2388. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2389. '/div',
  2390. '/div'
  2391. ];
  2392. $this->assertHtml($expected, $result);
  2393. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2394. $expected = [
  2395. 'div' => ['class' => 'input text error'],
  2396. 'label' => ['for' => 'article-content'],
  2397. 'Content',
  2398. '/label',
  2399. 'input' => [
  2400. 'type' => 'text', 'name' => 'Article[content]',
  2401. 'id' => 'article-content', 'class' => 'form-error'
  2402. ],
  2403. ['div' => ['class' => 'error-message']],
  2404. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2405. '/div',
  2406. '/div'
  2407. ];
  2408. $this->assertHtml($expected, $result);
  2409. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2410. $expected = [
  2411. 'div' => ['class' => 'input text error'],
  2412. 'label' => ['for' => 'article-content'],
  2413. 'Content',
  2414. '/label',
  2415. 'input' => [
  2416. 'type' => 'text', 'name' => 'Article[content]',
  2417. 'id' => 'article-content', 'class' => 'form-error'
  2418. ],
  2419. ['div' => ['class' => 'error-message']],
  2420. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2421. '/div',
  2422. '/div'
  2423. ];
  2424. $this->assertHtml($expected, $result);
  2425. }
  2426. /**
  2427. * testEmptyErrorValidation method
  2428. *
  2429. * Test validation errors, when validation message is an empty string.
  2430. *
  2431. * @return void
  2432. */
  2433. public function testEmptyErrorValidation()
  2434. {
  2435. $this->article['errors'] = [
  2436. 'Article' => ['title' => '']
  2437. ];
  2438. $this->Form->create($this->article);
  2439. $result = $this->Form->control('Article.title');
  2440. $expected = [
  2441. 'div' => ['class' => 'input text error'],
  2442. 'label' => ['for' => 'article-title'],
  2443. 'Title',
  2444. '/label',
  2445. 'input' => [
  2446. 'type' => 'text', 'name' => 'Article[title]',
  2447. 'id' => 'article-title', 'class' => 'form-error'
  2448. ],
  2449. ['div' => ['class' => 'error-message']],
  2450. [],
  2451. '/div',
  2452. '/div'
  2453. ];
  2454. $this->assertHtml($expected, $result);
  2455. }
  2456. /**
  2457. * testEmptyControlErrorValidation method
  2458. *
  2459. * Test validation errors, when calling control() overriding validation message by an empty string.
  2460. *
  2461. * @return void
  2462. */
  2463. public function testEmptyControlErrorValidation()
  2464. {
  2465. $this->article['errors'] = [
  2466. 'Article' => ['title' => 'error message']
  2467. ];
  2468. $this->Form->create($this->article);
  2469. $result = $this->Form->control('Article.title', ['error' => '']);
  2470. $expected = [
  2471. 'div' => ['class' => 'input text error'],
  2472. 'label' => ['for' => 'article-title'],
  2473. 'Title',
  2474. '/label',
  2475. 'input' => [
  2476. 'type' => 'text', 'name' => 'Article[title]',
  2477. 'id' => 'article-title', 'class' => 'form-error'
  2478. ],
  2479. ['div' => ['class' => 'error-message']],
  2480. [],
  2481. '/div',
  2482. '/div'
  2483. ];
  2484. $this->assertHtml($expected, $result);
  2485. }
  2486. /**
  2487. * testControlErrorMessage method
  2488. *
  2489. * Test validation errors, when calling control() overriding validation messages.
  2490. *
  2491. * @return void
  2492. */
  2493. public function testControlErrorMessage()
  2494. {
  2495. $this->article['errors'] = [
  2496. 'title' => ['error message']
  2497. ];
  2498. $this->Form->create($this->article);
  2499. $result = $this->Form->control('title', [
  2500. 'error' => 'Custom error!'
  2501. ]);
  2502. $expected = [
  2503. 'div' => ['class' => 'input text required error'],
  2504. 'label' => ['for' => 'title'],
  2505. 'Title',
  2506. '/label',
  2507. 'input' => [
  2508. 'type' => 'text', 'name' => 'title',
  2509. 'id' => 'title', 'class' => 'form-error',
  2510. 'required' => 'required',
  2511. ],
  2512. ['div' => ['class' => 'error-message']],
  2513. 'Custom error!',
  2514. '/div',
  2515. '/div'
  2516. ];
  2517. $this->assertHtml($expected, $result);
  2518. $result = $this->Form->control('title', [
  2519. 'error' => ['error message' => 'Custom error!']
  2520. ]);
  2521. $expected = [
  2522. 'div' => ['class' => 'input text required error'],
  2523. 'label' => ['for' => 'title'],
  2524. 'Title',
  2525. '/label',
  2526. 'input' => [
  2527. 'type' => 'text',
  2528. 'name' => 'title',
  2529. 'id' => 'title',
  2530. 'class' => 'form-error',
  2531. 'required' => 'required'
  2532. ],
  2533. ['div' => ['class' => 'error-message']],
  2534. 'Custom error!',
  2535. '/div',
  2536. '/div'
  2537. ];
  2538. $this->assertHtml($expected, $result);
  2539. }
  2540. /**
  2541. * testFormValidationAssociated method
  2542. *
  2543. * Tests displaying errors for nested entities.
  2544. *
  2545. * @return void
  2546. */
  2547. public function testFormValidationAssociated()
  2548. {
  2549. $nested = new Entity(['foo' => 'bar']);
  2550. $nested->errors('foo', ['not a valid bar']);
  2551. $entity = new Entity(['nested' => $nested]);
  2552. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2553. $result = $this->Form->error('nested.foo');
  2554. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2555. }
  2556. /**
  2557. * testFormValidationAssociatedSecondLevel method
  2558. *
  2559. * Test form error display with associated model.
  2560. *
  2561. * @return void
  2562. */
  2563. public function testFormValidationAssociatedSecondLevel()
  2564. {
  2565. $inner = new Entity(['bar' => 'baz']);
  2566. $nested = new Entity(['foo' => $inner]);
  2567. $entity = new Entity(['nested' => $nested]);
  2568. $inner->errors('bar', ['not a valid one']);
  2569. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2570. $result = $this->Form->error('nested.foo.bar');
  2571. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2572. }
  2573. /**
  2574. * testFormValidationMultiRecord method
  2575. *
  2576. * Test form error display with multiple records.
  2577. *
  2578. * @return void
  2579. */
  2580. public function testFormValidationMultiRecord()
  2581. {
  2582. $one = new Entity();
  2583. $two = new Entity();
  2584. TableRegistry::get('Contacts', [
  2585. 'className' => __NAMESPACE__ . '\ContactsTable'
  2586. ]);
  2587. $one->set('email', '');
  2588. $one->errors('email', ['invalid email']);
  2589. $two->set('name', '');
  2590. $two->errors('name', ['This is wrong']);
  2591. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2592. $result = $this->Form->control('0.email');
  2593. $expected = [
  2594. 'div' => ['class' => 'input email error'],
  2595. 'label' => ['for' => '0-email'],
  2596. 'Email',
  2597. '/label',
  2598. 'input' => [
  2599. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2600. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2601. ],
  2602. ['div' => ['class' => 'error-message']],
  2603. 'invalid email',
  2604. '/div',
  2605. '/div'
  2606. ];
  2607. $this->assertHtml($expected, $result);
  2608. $result = $this->Form->control('1.name');
  2609. $expected = [
  2610. 'div' => ['class' => 'input text error'],
  2611. 'label' => ['for' => '1-name'],
  2612. 'Name',
  2613. '/label',
  2614. 'input' => [
  2615. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2616. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2617. ],
  2618. ['div' => ['class' => 'error-message']],
  2619. 'This is wrong',
  2620. '/div',
  2621. '/div'
  2622. ];
  2623. $this->assertHtml($expected, $result);
  2624. }
  2625. /**
  2626. * testControl method
  2627. *
  2628. * Test various incarnations of control().
  2629. *
  2630. * @return void
  2631. */
  2632. public function testControl()
  2633. {
  2634. TableRegistry::get('ValidateUsers', [
  2635. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2636. ]);
  2637. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2638. $result = $this->Form->control('ValidateUsers.balance');
  2639. $expected = [
  2640. 'div' => ['class'],
  2641. 'label' => ['for'],
  2642. 'Balance',
  2643. '/label',
  2644. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2645. '/div',
  2646. ];
  2647. $this->assertHtml($expected, $result);
  2648. $result = $this->Form->control('ValidateUser.cost_decimal');
  2649. $expected = [
  2650. 'div' => ['class'],
  2651. 'label' => ['for'],
  2652. 'Cost Decimal',
  2653. '/label',
  2654. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2655. '/div',
  2656. ];
  2657. $this->assertHtml($expected, $result);
  2658. $result = $this->Form->control('ValidateUser.null_decimal');
  2659. $expected = [
  2660. 'div' => ['class'],
  2661. 'label' => ['for'],
  2662. 'Null Decimal',
  2663. '/label',
  2664. 'input' => ['name', 'type' => 'number', 'id'],
  2665. '/div',
  2666. ];
  2667. $this->assertHtml($expected, $result);
  2668. }
  2669. /**
  2670. * testControlCustomization method
  2671. *
  2672. * Tests the input method and passing custom options.
  2673. *
  2674. * @return void
  2675. */
  2676. public function testControlCustomization()
  2677. {
  2678. TableRegistry::get('Contacts', [
  2679. 'className' => __NAMESPACE__ . '\ContactsTable'
  2680. ]);
  2681. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2682. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2683. $expected = [
  2684. 'div' => ['class' => 'input email'],
  2685. 'label' => ['for' => 'custom'],
  2686. 'Email',
  2687. '/label',
  2688. ['input' => [
  2689. 'type' => 'email', 'name' => 'Contact[email]',
  2690. 'id' => 'custom', 'maxlength' => 255
  2691. ]],
  2692. '/div'
  2693. ];
  2694. $this->assertHtml($expected, $result);
  2695. $result = $this->Form->control('Contact.email', [
  2696. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2697. ]);
  2698. $expected = [
  2699. '<div',
  2700. 'label' => ['for' => 'contact-email'],
  2701. 'Email',
  2702. '/label',
  2703. ['input' => [
  2704. 'type' => 'email', 'name' => 'Contact[email]',
  2705. 'id' => 'contact-email', 'maxlength' => 255
  2706. ]],
  2707. '/div'
  2708. ];
  2709. $this->assertHtml($expected, $result);
  2710. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2711. $expected = [
  2712. 'div' => ['class' => 'input text'],
  2713. 'label' => ['for' => 'contact-email'],
  2714. 'Email',
  2715. '/label',
  2716. ['input' => [
  2717. 'type' => 'text', 'name' => 'Contact[email]',
  2718. 'id' => 'contact-email', 'maxlength' => '255'
  2719. ]],
  2720. '/div'
  2721. ];
  2722. $this->assertHtml($expected, $result);
  2723. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2724. $expected = [
  2725. 'div' => ['class' => 'input text'],
  2726. 'label' => ['for' => 'contact-5-email'],
  2727. 'Email',
  2728. '/label',
  2729. ['input' => [
  2730. 'type' => 'text', 'name' => 'Contact[5][email]',
  2731. 'id' => 'contact-5-email', 'maxlength' => '255'
  2732. ]],
  2733. '/div'
  2734. ];
  2735. $this->assertHtml($expected, $result);
  2736. $result = $this->Form->control('Contact.password');
  2737. $expected = [
  2738. 'div' => ['class' => 'input password'],
  2739. 'label' => ['for' => 'contact-password'],
  2740. 'Password',
  2741. '/label',
  2742. ['input' => [
  2743. 'type' => 'password', 'name' => 'Contact[password]',
  2744. 'id' => 'contact-password'
  2745. ]],
  2746. '/div'
  2747. ];
  2748. $this->assertHtml($expected, $result);
  2749. $result = $this->Form->control('Contact.email', [
  2750. 'type' => 'file', 'class' => 'textbox'
  2751. ]);
  2752. $expected = [
  2753. 'div' => ['class' => 'input file'],
  2754. 'label' => ['for' => 'contact-email'],
  2755. 'Email',
  2756. '/label',
  2757. ['input' => [
  2758. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2759. 'id' => 'contact-email'
  2760. ]],
  2761. '/div'
  2762. ];
  2763. $this->assertHtml($expected, $result);
  2764. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2765. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2766. $result = $this->Form->control('phone');
  2767. $expected = [
  2768. 'div' => ['class' => 'input tel'],
  2769. 'label' => ['for' => 'phone'],
  2770. 'Phone',
  2771. '/label',
  2772. ['input' => [
  2773. 'type' => 'tel', 'name' => 'phone',
  2774. 'value' => 'Hello &amp; World &gt; weird chars',
  2775. 'id' => 'phone', 'maxlength' => 255
  2776. ]],
  2777. '/div'
  2778. ];
  2779. $this->assertHtml($expected, $result);
  2780. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2781. $this->Form->create();
  2782. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2783. $expected = [
  2784. 'div' => ['class' => 'input text'],
  2785. 'label' => ['for' => 'myId'],
  2786. 'Field',
  2787. '/label',
  2788. 'input' => [
  2789. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2790. 'value' => 'My value', 'id' => 'myId'
  2791. ],
  2792. '/div'
  2793. ];
  2794. $this->assertHtml($expected, $result);
  2795. $this->Form->request->data = [];
  2796. $entity->errors('field', 'Badness!');
  2797. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2798. $result = $this->Form->control('field');
  2799. $expected = [
  2800. 'div' => ['class' => 'input text error'],
  2801. 'label' => ['for' => 'field'],
  2802. 'Field',
  2803. '/label',
  2804. 'input' => [
  2805. 'type' => 'text', 'name' => 'field',
  2806. 'id' => 'field', 'class' => 'form-error'
  2807. ],
  2808. ['div' => ['class' => 'error-message']],
  2809. 'Badness!',
  2810. '/div',
  2811. '/div'
  2812. ];
  2813. $this->assertHtml($expected, $result);
  2814. $result = $this->Form->control('field', [
  2815. 'templates' => [
  2816. 'inputContainerError' => '{{content}}{{error}}',
  2817. 'error' => '<span class="error-message">{{content}}</span>'
  2818. ]
  2819. ]);
  2820. $expected = [
  2821. 'label' => ['for' => 'field'],
  2822. 'Field',
  2823. '/label',
  2824. 'input' => [
  2825. 'type' => 'text', 'name' => 'field',
  2826. 'id' => 'field', 'class' => 'form-error'
  2827. ],
  2828. ['span' => ['class' => 'error-message']],
  2829. 'Badness!',
  2830. '/span'
  2831. ];
  2832. $this->assertHtml($expected, $result);
  2833. $entity->errors('field', ['minLength'], true);
  2834. $result = $this->Form->control('field', [
  2835. 'error' => [
  2836. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2837. 'maxLength' => 'login too large'
  2838. ]
  2839. ]);
  2840. $expected = [
  2841. 'div' => ['class' => 'input text error'],
  2842. 'label' => ['for' => 'field'],
  2843. 'Field',
  2844. '/label',
  2845. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2846. ['div' => ['class' => 'error-message']],
  2847. 'Le login doit contenir au moins 2 caractères',
  2848. '/div',
  2849. '/div'
  2850. ];
  2851. $this->assertHtml($expected, $result);
  2852. $entity->errors('field', ['maxLength'], true);
  2853. $result = $this->Form->control('field', [
  2854. 'error' => [
  2855. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2856. 'maxLength' => 'login too large',
  2857. ]
  2858. ]);
  2859. $expected = [
  2860. 'div' => ['class' => 'input text error'],
  2861. 'label' => ['for' => 'field'],
  2862. 'Field',
  2863. '/label',
  2864. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2865. ['div' => ['class' => 'error-message']],
  2866. 'login too large',
  2867. '/div',
  2868. '/div'
  2869. ];
  2870. $this->assertHtml($expected, $result);
  2871. }
  2872. /**
  2873. * testControlWithTemplateFile method
  2874. *
  2875. * Test that control() accepts a template file.
  2876. *
  2877. * @return void
  2878. */
  2879. public function testControlWithTemplateFile()
  2880. {
  2881. $result = $this->Form->control('field', [
  2882. 'templates' => 'htmlhelper_tags'
  2883. ]);
  2884. $expected = [
  2885. 'label' => ['for' => 'field'],
  2886. 'Field',
  2887. '/label',
  2888. 'input' => [
  2889. 'type' => 'text', 'name' => 'field',
  2890. 'id' => 'field'
  2891. ],
  2892. ];
  2893. $this->assertHtml($expected, $result);
  2894. }
  2895. /**
  2896. * testNestedControlsEndWithBrackets method
  2897. *
  2898. * Test that nested inputs end with brackets.
  2899. *
  2900. * @return void
  2901. */
  2902. public function testNestedControlsEndWithBrackets()
  2903. {
  2904. $result = $this->Form->text('nested.text[]');
  2905. $expected = [
  2906. 'input' => [
  2907. 'type' => 'text', 'name' => 'nested[text][]'
  2908. ],
  2909. ];
  2910. $this->assertHtml($expected, $result);
  2911. $result = $this->Form->file('nested.file[]');
  2912. $expected = [
  2913. 'input' => [
  2914. 'type' => 'file', 'name' => 'nested[file][]'
  2915. ],
  2916. ];
  2917. $this->assertHtml($expected, $result);
  2918. }
  2919. /**
  2920. * testCreateIdPrefix method
  2921. *
  2922. * Test id prefix.
  2923. *
  2924. * @return void
  2925. */
  2926. public function testCreateIdPrefix()
  2927. {
  2928. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2929. $result = $this->Form->control('field');
  2930. $expected = [
  2931. 'div' => ['class' => 'input text'],
  2932. 'label' => ['for' => 'prefix-field'],
  2933. 'Field',
  2934. '/label',
  2935. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2936. '/div'
  2937. ];
  2938. $this->assertHtml($expected, $result);
  2939. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2940. $expected = [
  2941. 'div' => ['class' => 'input text'],
  2942. 'label' => ['for' => 'custom-id'],
  2943. 'Field',
  2944. '/label',
  2945. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2946. '/div'
  2947. ];
  2948. $this->assertHtml($expected, $result);
  2949. $result = $this->Form->radio('Model.field', ['option A']);
  2950. $expected = [
  2951. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2952. 'label' => ['for' => 'prefix-model-field-0'],
  2953. ['input' => [
  2954. 'type' => 'radio',
  2955. 'name' => 'Model[field]',
  2956. 'value' => '0',
  2957. 'id' => 'prefix-model-field-0'
  2958. ]],
  2959. 'option A',
  2960. '/label'
  2961. ];
  2962. $this->assertHtml($expected, $result);
  2963. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2964. $expected = [
  2965. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2966. 'label' => ['for' => 'prefix-model-field-0'],
  2967. ['input' => [
  2968. 'type' => 'radio',
  2969. 'name' => 'Model[field]',
  2970. 'value' => '0',
  2971. 'id' => 'prefix-model-field-0'
  2972. ]],
  2973. 'option A',
  2974. '/label'
  2975. ];
  2976. $this->assertHtml($expected, $result);
  2977. $result = $this->Form->select(
  2978. 'Model.multi_field',
  2979. ['first'],
  2980. ['multiple' => 'checkbox']
  2981. );
  2982. $expected = [
  2983. 'input' => [
  2984. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2985. ],
  2986. ['div' => ['class' => 'checkbox']],
  2987. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2988. ['input' => [
  2989. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2990. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2991. ]],
  2992. 'first',
  2993. '/label',
  2994. '/div',
  2995. ];
  2996. $this->assertHtml($expected, $result);
  2997. $this->Form->end();
  2998. $result = $this->Form->control('field');
  2999. $expected = [
  3000. 'div' => ['class' => 'input text'],
  3001. 'label' => ['for' => 'field'],
  3002. 'Field',
  3003. '/label',
  3004. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3005. '/div'
  3006. ];
  3007. $this->assertHtml($expected, $result);
  3008. }
  3009. /**
  3010. * testControlZero method
  3011. *
  3012. * Test that inputs with 0 can be created.
  3013. *
  3014. * @return void
  3015. */
  3016. public function testControlZero()
  3017. {
  3018. TableRegistry::get('Contacts', [
  3019. 'className' => __NAMESPACE__ . '\ContactsTable'
  3020. ]);
  3021. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3022. $result = $this->Form->control('0');
  3023. $expected = [
  3024. 'div' => ['class' => 'input text'],
  3025. 'label' => ['for' => '0'], '/label',
  3026. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3027. '/div'
  3028. ];
  3029. $this->assertHtml($expected, $result);
  3030. }
  3031. /**
  3032. * testControlCheckbox method
  3033. *
  3034. * Test control() with checkbox creation.
  3035. *
  3036. * @return void
  3037. */
  3038. public function testControlCheckbox()
  3039. {
  3040. $articles = TableRegistry::get('Articles');
  3041. $articles->schema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3042. $article = $articles->newEntity();
  3043. $this->Form->create($article);
  3044. $result = $this->Form->control('Articles.active');
  3045. $expected = [
  3046. 'div' => ['class' => 'input checkbox'],
  3047. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3048. 'label' => ['for' => 'articles-active'],
  3049. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3050. 'Active',
  3051. '/label',
  3052. '/div'
  3053. ];
  3054. $this->assertHtml($expected, $result);
  3055. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3056. $expected = [
  3057. 'div' => ['class' => 'input checkbox'],
  3058. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3059. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3060. '/div'
  3061. ];
  3062. $this->assertHtml($expected, $result);
  3063. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3064. $expected = [
  3065. 'div' => ['class' => 'input checkbox'],
  3066. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3067. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3068. '/div'
  3069. ];
  3070. $this->assertHtml($expected, $result);
  3071. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3072. $expected = [
  3073. 'div' => ['class' => 'input checkbox'],
  3074. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3075. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3076. '/div'
  3077. ];
  3078. $this->assertHtml($expected, $result);
  3079. $result = $this->Form->control('Articles.disabled', [
  3080. 'label' => 'Disabled',
  3081. 'type' => 'checkbox',
  3082. 'data-foo' => 'disabled'
  3083. ]);
  3084. $expected = [
  3085. 'div' => ['class' => 'input checkbox'],
  3086. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3087. 'label' => ['for' => 'articles-disabled'],
  3088. ['input' => [
  3089. 'type' => 'checkbox',
  3090. 'name' => 'Articles[disabled]',
  3091. 'value' => '1',
  3092. 'id' => 'articles-disabled',
  3093. 'data-foo' => 'disabled'
  3094. ]],
  3095. 'Disabled',
  3096. '/label',
  3097. '/div'
  3098. ];
  3099. $this->assertHtml($expected, $result);
  3100. $result = $this->Form->control('Articles.confirm', [
  3101. 'label' => 'Confirm <b>me</b>!',
  3102. 'type' => 'checkbox',
  3103. 'escape' => false
  3104. ]);
  3105. $expected = [
  3106. 'div' => ['class' => 'input checkbox'],
  3107. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3108. 'label' => ['for' => 'articles-confirm'],
  3109. ['input' => [
  3110. 'type' => 'checkbox',
  3111. 'name' => 'Articles[confirm]',
  3112. 'value' => '1',
  3113. 'id' => 'articles-confirm',
  3114. ]],
  3115. 'Confirm <b>me</b>!',
  3116. '/label',
  3117. '/div'
  3118. ];
  3119. $this->assertHtml($expected, $result);
  3120. }
  3121. /**
  3122. * testControlHidden method
  3123. *
  3124. * Test that control() does not create wrapping div and label tag for hidden fields.
  3125. *
  3126. * @return void
  3127. */
  3128. public function testControlHidden()
  3129. {
  3130. TableRegistry::get('ValidateUsers', [
  3131. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3132. ]);
  3133. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3134. $result = $this->Form->control('ValidateUser.id');
  3135. $expected = [
  3136. 'input' => ['name', 'type' => 'hidden', 'id']
  3137. ];
  3138. $this->assertHtml($expected, $result);
  3139. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3140. $expected = [
  3141. 'input' => ['name', 'type' => 'hidden', 'id']
  3142. ];
  3143. $this->assertHtml($expected, $result);
  3144. }
  3145. /**
  3146. * testControlDatetime method
  3147. *
  3148. * Test form->control() with datetime.
  3149. *
  3150. * @return void
  3151. */
  3152. public function testControlDatetime()
  3153. {
  3154. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3155. ->setMethods(['datetime'])
  3156. ->setConstructorArgs([new View()])
  3157. ->getMock();
  3158. $this->Form->expects($this->once())->method('datetime')
  3159. ->with('prueba', [
  3160. 'type' => 'datetime',
  3161. 'timeFormat' => 24,
  3162. 'minYear' => 2008,
  3163. 'maxYear' => 2011,
  3164. 'interval' => 15,
  3165. 'options' => null,
  3166. 'empty' => false,
  3167. 'id' => 'prueba',
  3168. 'required' => false,
  3169. 'templateVars' => []
  3170. ])
  3171. ->will($this->returnValue('This is it!'));
  3172. $result = $this->Form->control('prueba', [
  3173. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3174. 'maxYear' => 2011, 'interval' => 15
  3175. ]);
  3176. $expected = [
  3177. 'div' => ['class' => 'input datetime'],
  3178. '<label',
  3179. 'Prueba',
  3180. '/label',
  3181. 'This is it!',
  3182. '/div'
  3183. ];
  3184. $this->assertHtml($expected, $result);
  3185. }
  3186. /**
  3187. * testControlDatetimeIdPrefix method
  3188. *
  3189. * Test form->control() with datetime with id prefix.
  3190. *
  3191. * @return void
  3192. */
  3193. public function testControlDatetimeIdPrefix()
  3194. {
  3195. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3196. ->setMethods(['datetime'])
  3197. ->setConstructorArgs([new View()])
  3198. ->getMock();
  3199. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3200. $this->Form->expects($this->once())->method('datetime')
  3201. ->with('prueba', [
  3202. 'type' => 'datetime',
  3203. 'timeFormat' => 24,
  3204. 'minYear' => 2008,
  3205. 'maxYear' => 2011,
  3206. 'interval' => 15,
  3207. 'options' => null,
  3208. 'empty' => false,
  3209. 'id' => 'prefix-prueba',
  3210. 'required' => false,
  3211. 'templateVars' => []
  3212. ])
  3213. ->will($this->returnValue('This is it!'));
  3214. $result = $this->Form->control('prueba', [
  3215. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3216. 'maxYear' => 2011, 'interval' => 15
  3217. ]);
  3218. $expected = [
  3219. 'div' => ['class' => 'input datetime'],
  3220. '<label',
  3221. 'Prueba',
  3222. '/label',
  3223. 'This is it!',
  3224. '/div'
  3225. ];
  3226. $this->assertHtml($expected, $result);
  3227. }
  3228. /**
  3229. * testControlCheckboxWithDisabledElements method
  3230. *
  3231. * Test generating checkboxes with disabled elements.
  3232. *
  3233. * @return void
  3234. */
  3235. public function testControlCheckboxWithDisabledElements()
  3236. {
  3237. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3238. $result = $this->Form->control('Contact.multiple', [
  3239. 'multiple' => 'checkbox',
  3240. 'disabled' => 'disabled',
  3241. 'options' => $options
  3242. ]);
  3243. $expected = [
  3244. ['div' => ['class' => 'input select']],
  3245. ['label' => ['for' => 'contact-multiple']],
  3246. 'Multiple',
  3247. '/label',
  3248. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3249. ['div' => ['class' => 'checkbox']],
  3250. ['label' => ['for' => 'contact-multiple-1']],
  3251. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3252. 'One',
  3253. '/label',
  3254. '/div',
  3255. ['div' => ['class' => 'checkbox']],
  3256. ['label' => ['for' => 'contact-multiple-2']],
  3257. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3258. 'Two',
  3259. '/label',
  3260. '/div',
  3261. ['div' => ['class' => 'checkbox']],
  3262. ['label' => ['for' => 'contact-multiple-3']],
  3263. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3264. 'Three',
  3265. '/label',
  3266. '/div',
  3267. '/div'
  3268. ];
  3269. $this->assertHtml($expected, $result);
  3270. // make sure 50 does only disable 50, and not 50f5c0cf
  3271. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3272. $disabled = [50];
  3273. $expected = [
  3274. ['div' => ['class' => 'input select']],
  3275. ['label' => ['for' => 'contact-multiple']],
  3276. 'Multiple',
  3277. '/label',
  3278. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3279. ['div' => ['class' => 'checkbox']],
  3280. ['label' => ['for' => 'contact-multiple-50']],
  3281. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3282. 'Fifty',
  3283. '/label',
  3284. '/div',
  3285. ['div' => ['class' => 'checkbox']],
  3286. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3287. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3288. 'Stringy',
  3289. '/label',
  3290. '/div',
  3291. '/div'
  3292. ];
  3293. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3294. $this->assertHtml($expected, $result);
  3295. }
  3296. /**
  3297. * testControlWithLeadingInteger method
  3298. *
  3299. * Test input name with leading integer, ensure attributes are generated correctly.
  3300. *
  3301. * @return void
  3302. */
  3303. public function testControlWithLeadingInteger()
  3304. {
  3305. $result = $this->Form->text('0.Node.title');
  3306. $expected = [
  3307. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3308. ];
  3309. $this->assertHtml($expected, $result);
  3310. }
  3311. /**
  3312. * testControlSelectType method
  3313. *
  3314. * Test form->control() with select type inputs.
  3315. *
  3316. * @return void
  3317. */
  3318. public function testControlSelectType()
  3319. {
  3320. $result = $this->Form->control(
  3321. 'email',
  3322. [
  3323. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3324. );
  3325. $expected = [
  3326. 'div' => ['class' => 'input select'],
  3327. 'label' => ['for' => 'email'],
  3328. 'Email',
  3329. '/label',
  3330. ['select' => ['name' => 'email', 'id' => 'email']],
  3331. ['option' => ['value' => '']],
  3332. '/option',
  3333. ['option' => ['value' => 'è']],
  3334. 'Firést',
  3335. '/option',
  3336. ['option' => ['value' => 'é']],
  3337. 'Secoènd',
  3338. '/option',
  3339. '/select',
  3340. '/div'
  3341. ];
  3342. $this->assertHtml($expected, $result);
  3343. $result = $this->Form->control(
  3344. 'email',
  3345. [
  3346. 'options' => ['First', 'Second'], 'empty' => true]
  3347. );
  3348. $expected = [
  3349. 'div' => ['class' => 'input select'],
  3350. 'label' => ['for' => 'email'],
  3351. 'Email',
  3352. '/label',
  3353. ['select' => ['name' => 'email', 'id' => 'email']],
  3354. ['option' => ['value' => '']],
  3355. '/option',
  3356. ['option' => ['value' => '0']],
  3357. 'First',
  3358. '/option',
  3359. ['option' => ['value' => '1']],
  3360. 'Second',
  3361. '/option',
  3362. '/select',
  3363. '/div'
  3364. ];
  3365. $this->assertHtml($expected, $result);
  3366. $result = $this->Form->control('email', [
  3367. 'type' => 'select',
  3368. 'options' => new \ArrayObject(['First', 'Second']),
  3369. 'empty' => true
  3370. ]);
  3371. $this->assertHtml($expected, $result);
  3372. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3373. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3374. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3375. $expected = [
  3376. 'div' => ['class' => 'input select'],
  3377. 'label' => ['for' => 'model-user-id'],
  3378. 'User',
  3379. '/label',
  3380. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3381. ['option' => ['value' => '']],
  3382. '/option',
  3383. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3384. 'good',
  3385. '/option',
  3386. ['option' => ['value' => 'other']],
  3387. 'bad',
  3388. '/option',
  3389. '/select',
  3390. '/div'
  3391. ];
  3392. $this->assertHtml($expected, $result);
  3393. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3394. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3395. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3396. $expected = [
  3397. 'div' => ['class' => 'input select'],
  3398. 'label' => ['for' => 'thing-user-id'],
  3399. 'User',
  3400. '/label',
  3401. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3402. ['option' => ['value' => '']],
  3403. 'Some Empty',
  3404. '/option',
  3405. ['option' => ['value' => 'value']],
  3406. 'good',
  3407. '/option',
  3408. ['option' => ['value' => 'other']],
  3409. 'bad',
  3410. '/option',
  3411. '/select',
  3412. '/div'
  3413. ];
  3414. $this->assertHtml($expected, $result);
  3415. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3416. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3417. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3418. $expected = [
  3419. 'div' => ['class' => 'input select'],
  3420. 'label' => ['for' => 'thing-user-id'],
  3421. 'User',
  3422. '/label',
  3423. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3424. ['option' => ['value' => '']],
  3425. 'Some Empty',
  3426. '/option',
  3427. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3428. 'good',
  3429. '/option',
  3430. ['option' => ['value' => 'other']],
  3431. 'bad',
  3432. '/option',
  3433. '/select',
  3434. '/div'
  3435. ];
  3436. $this->assertHtml($expected, $result);
  3437. $this->Form->data = [];
  3438. $result = $this->Form->control('Publisher.id', [
  3439. 'label' => 'Publisher',
  3440. 'type' => 'select',
  3441. 'multiple' => 'checkbox',
  3442. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3443. ]);
  3444. $expected = [
  3445. ['div' => ['class' => 'input select']],
  3446. ['label' => ['for' => 'publisher-id']],
  3447. 'Publisher',
  3448. '/label',
  3449. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3450. ['div' => ['class' => 'checkbox']],
  3451. ['label' => ['for' => 'publisher-id-value-1']],
  3452. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3453. 'Label 1',
  3454. '/label',
  3455. '/div',
  3456. ['div' => ['class' => 'checkbox']],
  3457. ['label' => ['for' => 'publisher-id-value-2']],
  3458. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3459. 'Label 2',
  3460. '/label',
  3461. '/div',
  3462. '/div'
  3463. ];
  3464. $this->assertHtml($expected, $result);
  3465. }
  3466. /**
  3467. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3468. *
  3469. * Test that control() and a non standard primary key makes a hidden input by default.
  3470. *
  3471. * @return void
  3472. */
  3473. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3474. {
  3475. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3476. $this->Form->create($this->article);
  3477. $result = $this->Form->control('title');
  3478. $expected = [
  3479. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3480. ];
  3481. $this->assertHtml($expected, $result);
  3482. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3483. $this->Form->create($this->article);
  3484. $result = $this->Form->control('title');
  3485. $expected = [
  3486. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3487. ];
  3488. $this->assertHtml($expected, $result);
  3489. $result = $this->Form->control('body');
  3490. $expected = [
  3491. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3492. ];
  3493. $this->assertHtml($expected, $result);
  3494. }
  3495. /**
  3496. * testControlOverridingMagicSelectType method
  3497. *
  3498. * Test that overriding the magic select type widget is possible.
  3499. *
  3500. * @return void
  3501. */
  3502. public function testControlOverridingMagicSelectType()
  3503. {
  3504. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3505. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3506. $expected = [
  3507. 'div' => ['class' => 'input text'],
  3508. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3509. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3510. '/div'
  3511. ];
  3512. $this->assertHtml($expected, $result);
  3513. //Check that magic types still work for plural/singular vars
  3514. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3515. $result = $this->Form->control('Model.type');
  3516. $expected = [
  3517. 'div' => ['class' => 'input select'],
  3518. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3519. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3520. ['option' => ['value' => 'value']], 'good', '/option',
  3521. ['option' => ['value' => 'other']], 'bad', '/option',
  3522. '/select',
  3523. '/div'
  3524. ];
  3525. $this->assertHtml($expected, $result);
  3526. }
  3527. /**
  3528. * testControlMagicTypeDoesNotOverride method
  3529. *
  3530. * Test that inferred types do not override developer input.
  3531. *
  3532. * @return void
  3533. */
  3534. public function testControlMagicTypeDoesNotOverride()
  3535. {
  3536. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3537. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3538. $expected = [
  3539. 'div' => ['class' => 'input checkbox'],
  3540. ['input' => [
  3541. 'type' => 'hidden',
  3542. 'name' => 'Model[user]',
  3543. 'value' => 0,
  3544. ]],
  3545. 'label' => ['for' => 'model-user'],
  3546. ['input' => [
  3547. 'name' => 'Model[user]',
  3548. 'type' => 'checkbox',
  3549. 'id' => 'model-user',
  3550. 'value' => 1
  3551. ]],
  3552. 'User',
  3553. '/label',
  3554. '/div'
  3555. ];
  3556. $this->assertHtml($expected, $result);
  3557. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3558. $options = [
  3559. 1 => 'blue',
  3560. 2 => 'red'
  3561. ];
  3562. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3563. $expected = [
  3564. 'div' => ['class' => 'input select'],
  3565. 'label' => ['for' => 'tags-ids'],
  3566. 'Tags',
  3567. '/label',
  3568. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3569. ['div' => ['class' => 'checkbox']],
  3570. ['label' => ['for' => 'tags-ids-1']],
  3571. ['input' => [
  3572. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3573. 'value' => '1', 'name' => 'tags[_ids][]'
  3574. ]],
  3575. 'blue',
  3576. '/label',
  3577. '/div',
  3578. ['div' => ['class' => 'checkbox']],
  3579. ['label' => ['for' => 'tags-ids-2']],
  3580. ['input' => [
  3581. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3582. 'value' => '2', 'name' => 'tags[_ids][]'
  3583. ]],
  3584. 'red',
  3585. '/label',
  3586. '/div',
  3587. '/div'
  3588. ];
  3589. $this->assertHtml($expected, $result);
  3590. }
  3591. /**
  3592. * testControlMagicSelectForTypeNumber method
  3593. *
  3594. * Test that magic control() selects are created for type=number.
  3595. *
  3596. * @return void
  3597. */
  3598. public function testControlMagicSelectForTypeNumber()
  3599. {
  3600. TableRegistry::get('ValidateUsers', [
  3601. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3602. ]);
  3603. $entity = new Entity(['balance' => 1]);
  3604. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3605. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3606. $result = $this->Form->control('balance');
  3607. $expected = [
  3608. 'div' => ['class' => 'input select'],
  3609. 'label' => ['for' => 'balance'],
  3610. 'Balance',
  3611. '/label',
  3612. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3613. ['option' => ['value' => '0']],
  3614. 'nothing',
  3615. '/option',
  3616. ['option' => ['value' => '1', 'selected' => 'selected']],
  3617. 'some',
  3618. '/option',
  3619. ['option' => ['value' => '100']],
  3620. 'a lot',
  3621. '/option',
  3622. '/select',
  3623. '/div'
  3624. ];
  3625. $this->assertHtml($expected, $result);
  3626. }
  3627. /**
  3628. * testInvalidControlTypeOption method
  3629. *
  3630. * Test invalid 'input' type option to control() function.
  3631. *
  3632. * @expectedException \RuntimeException
  3633. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3634. * @return void
  3635. */
  3636. public function testInvalidControlTypeOption()
  3637. {
  3638. $this->Form->control('text', ['type' => 'input']);
  3639. }
  3640. /**
  3641. * testControlMagicSelectChangeToRadio method
  3642. *
  3643. * Test that magic control() selects can easily be converted into radio types without error.
  3644. *
  3645. * @return void
  3646. */
  3647. public function testControlMagicSelectChangeToRadio()
  3648. {
  3649. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3650. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3651. $this->assertContains('input type="radio"', $result);
  3652. }
  3653. /**
  3654. * testFormControlSubmit method
  3655. *
  3656. * Test correct results for form::control() and type submit.
  3657. *
  3658. * @return void
  3659. */
  3660. public function testFormControlSubmit()
  3661. {
  3662. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3663. $expected = [
  3664. 'div' => ['class' => 'submit'],
  3665. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3666. '/div'
  3667. ];
  3668. $this->assertHtml($expected, $result);
  3669. }
  3670. /**
  3671. * testFormControls method
  3672. *
  3673. * Test correct results from Form::controls().
  3674. *
  3675. * @return void
  3676. */
  3677. public function testFormControlsLegendFieldset()
  3678. {
  3679. $this->Form->create($this->article);
  3680. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3681. $expected = [
  3682. '<fieldset',
  3683. '<legend',
  3684. 'The Legend',
  3685. '/legend',
  3686. '*/fieldset',
  3687. ];
  3688. $this->assertHtml($expected, $result);
  3689. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3690. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3691. $this->assertContains('<fieldset>', $result);
  3692. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3693. $this->assertNotContains('<legend>', $result);
  3694. $this->assertNotContains('<fieldset>', $result);
  3695. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3696. $this->assertNotContains('<legend>', $result);
  3697. $this->assertNotContains('<fieldset>', $result);
  3698. $this->Form->create($this->article);
  3699. $this->Form->request->params['prefix'] = 'admin';
  3700. $this->Form->request->params['action'] = 'admin_edit';
  3701. $this->Form->request->params['controller'] = 'articles';
  3702. $result = $this->Form->allControls();
  3703. $expected = [
  3704. '<fieldset',
  3705. '<legend',
  3706. 'New Article',
  3707. '/legend',
  3708. '*/fieldset',
  3709. ];
  3710. $this->assertHtml($expected, $result);
  3711. $this->Form->create($this->article);
  3712. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3713. $expected = [
  3714. '<fieldset',
  3715. '<legend',
  3716. 'The Legend',
  3717. '/legend',
  3718. '*/fieldset',
  3719. ];
  3720. $this->assertHtml($expected, $result);
  3721. $this->Form->create($this->article);
  3722. $result = $this->Form->allControls([], [
  3723. 'fieldset' => [
  3724. 'class' => 'some-class some-other-class',
  3725. 'disabled' => true,
  3726. 'data-param' => 'a-param'
  3727. ],
  3728. 'legend' => 'The Legend'
  3729. ]);
  3730. $expected = [
  3731. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3732. '<legend',
  3733. 'The Legend',
  3734. '/legend',
  3735. '*/fieldset',
  3736. ];
  3737. $this->assertHtml($expected, $result);
  3738. }
  3739. /**
  3740. * testFormControls method
  3741. *
  3742. * Test the controls() method.
  3743. *
  3744. * @return void
  3745. */
  3746. public function testFormControls()
  3747. {
  3748. $this->Form->create($this->article);
  3749. $result = $this->Form->allControls();
  3750. $expected = [
  3751. '<fieldset',
  3752. '<legend', 'New Article', '/legend',
  3753. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3754. ['div' => ['class' => 'input select required']],
  3755. '*/div',
  3756. ['div' => ['class' => 'input text required']],
  3757. '*/div',
  3758. ['div' => ['class' => 'input text']],
  3759. '*/div',
  3760. ['div' => ['class' => 'input text']],
  3761. '*/div',
  3762. '/fieldset',
  3763. ];
  3764. $this->assertHtml($expected, $result);
  3765. $result = $this->Form->allControls([
  3766. 'published' => ['type' => 'boolean']
  3767. ]);
  3768. $expected = [
  3769. '<fieldset',
  3770. '<legend', 'New Article', '/legend',
  3771. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3772. ['div' => ['class' => 'input select required']],
  3773. '*/div',
  3774. ['div' => ['class' => 'input text required']],
  3775. '*/div',
  3776. ['div' => ['class' => 'input text']],
  3777. '*/div',
  3778. ['div' => ['class' => 'input boolean']],
  3779. '*/div',
  3780. '/fieldset',
  3781. ];
  3782. $this->assertHtml($expected, $result);
  3783. $this->Form->create($this->article);
  3784. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3785. $expected = [
  3786. 'fieldset' => [],
  3787. 'legend' => [],
  3788. 'Hello',
  3789. '/legend',
  3790. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3791. ['div' => ['class' => 'input select required']],
  3792. '*/div',
  3793. ['div' => ['class' => 'input text required']],
  3794. '*/div',
  3795. ['div' => ['class' => 'input text']],
  3796. '*/div',
  3797. ['div' => ['class' => 'input text']],
  3798. '*/div',
  3799. '/fieldset'
  3800. ];
  3801. $this->assertHtml($expected, $result);
  3802. $this->Form->create(false);
  3803. $expected = [
  3804. 'fieldset' => [],
  3805. ['div' => ['class' => 'input text']],
  3806. 'label' => ['for' => 'foo'],
  3807. 'Foo',
  3808. '/label',
  3809. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3810. '*/div',
  3811. '/fieldset'
  3812. ];
  3813. $result = $this->Form->allControls(
  3814. ['foo' => ['type' => 'text']],
  3815. ['legend' => false]
  3816. );
  3817. $this->assertHtml($expected, $result);
  3818. }
  3819. /**
  3820. * testFormControlsBlacklist method
  3821. *
  3822. * @return void
  3823. */
  3824. public function testFormControlsBlacklist()
  3825. {
  3826. $this->Form->create($this->article);
  3827. $result = $this->Form->allControls([
  3828. 'id' => false
  3829. ]);
  3830. $expected = [
  3831. '<fieldset',
  3832. '<legend', 'New Article', '/legend',
  3833. ['div' => ['class' => 'input select required']],
  3834. '*/div',
  3835. ['div' => ['class' => 'input text required']],
  3836. '*/div',
  3837. ['div' => ['class' => 'input text']],
  3838. '*/div',
  3839. ['div' => ['class' => 'input text']],
  3840. '*/div',
  3841. '/fieldset',
  3842. ];
  3843. $this->assertHtml($expected, $result);
  3844. $this->Form->create($this->article);
  3845. $result = $this->Form->allControls([
  3846. 'id' => []
  3847. ]);
  3848. $expected = [
  3849. '<fieldset',
  3850. '<legend', 'New Article', '/legend',
  3851. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3852. ['div' => ['class' => 'input select required']],
  3853. '*/div',
  3854. ['div' => ['class' => 'input text required']],
  3855. '*/div',
  3856. ['div' => ['class' => 'input text']],
  3857. '*/div',
  3858. ['div' => ['class' => 'input text']],
  3859. '*/div',
  3860. '/fieldset',
  3861. ];
  3862. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3863. }
  3864. /**
  3865. * testSelectAsCheckbox method
  3866. *
  3867. * Test multi-select widget with checkbox formatting.
  3868. *
  3869. * @return void
  3870. */
  3871. public function testSelectAsCheckbox()
  3872. {
  3873. $result = $this->Form->select(
  3874. 'Model.multi_field',
  3875. ['first', 'second', 'third'],
  3876. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3877. );
  3878. $expected = [
  3879. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3880. ['div' => ['class' => 'checkbox']],
  3881. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3882. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3883. 'first',
  3884. '/label',
  3885. '/div',
  3886. ['div' => ['class' => 'checkbox']],
  3887. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3888. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3889. 'second',
  3890. '/label',
  3891. '/div',
  3892. ['div' => ['class' => 'checkbox']],
  3893. ['label' => ['for' => 'model-multi-field-2']],
  3894. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3895. 'third',
  3896. '/label',
  3897. '/div',
  3898. ];
  3899. $this->assertHtml($expected, $result);
  3900. $result = $this->Form->select(
  3901. 'Model.multi_field',
  3902. ['1/2' => 'half'],
  3903. ['multiple' => 'checkbox']
  3904. );
  3905. $expected = [
  3906. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3907. ['div' => ['class' => 'checkbox']],
  3908. ['label' => ['for' => 'model-multi-field-1-2']],
  3909. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3910. 'half',
  3911. '/label',
  3912. '/div',
  3913. ];
  3914. $this->assertHtml($expected, $result);
  3915. }
  3916. /**
  3917. * testLabel method
  3918. *
  3919. * Test label generation.
  3920. *
  3921. * @return void
  3922. */
  3923. public function testLabel()
  3924. {
  3925. $result = $this->Form->label('Person.name');
  3926. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3927. $this->assertHtml($expected, $result);
  3928. $result = $this->Form->label('Person.name');
  3929. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3930. $this->assertHtml($expected, $result);
  3931. $result = $this->Form->label('Person.first_name');
  3932. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3933. $this->assertHtml($expected, $result);
  3934. $result = $this->Form->label('Person.first_name', 'Your first name');
  3935. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3936. $this->assertHtml($expected, $result);
  3937. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3938. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3939. $this->assertHtml($expected, $result);
  3940. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3941. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3942. $this->assertHtml($expected, $result);
  3943. $result = $this->Form->label('Person.first_name', '');
  3944. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3945. $this->assertHtml($expected, $result);
  3946. $result = $this->Form->label('Person.2.name', '');
  3947. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3948. $this->assertHtml($expected, $result);
  3949. }
  3950. /**
  3951. * testLabelContainControl method
  3952. *
  3953. * Test that label() can accept an input with the correct template vars.
  3954. *
  3955. * @return void
  3956. */
  3957. public function testLabelContainControl()
  3958. {
  3959. $this->Form->templates([
  3960. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3961. ]);
  3962. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3963. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3964. ]);
  3965. $expected = [
  3966. 'label' => ['for' => 'person-accept-terms'],
  3967. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3968. 'Accept',
  3969. '/label',
  3970. ];
  3971. $this->assertHtml($expected, $result);
  3972. }
  3973. /**
  3974. * testTextbox method
  3975. *
  3976. * Test textbox element generation.
  3977. *
  3978. * @return void
  3979. */
  3980. public function testTextbox()
  3981. {
  3982. $result = $this->Form->text('Model.field');
  3983. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3984. $this->assertHtml($expected, $result);
  3985. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3986. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3987. $this->assertHtml($expected, $result);
  3988. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3989. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3990. $this->assertHtml($expected, $result);
  3991. }
  3992. /**
  3993. * testTextBoxDataAndError method
  3994. *
  3995. * Test that text() hooks up with request data and error fields.
  3996. *
  3997. * @return void
  3998. */
  3999. public function testTextBoxDataAndError()
  4000. {
  4001. $this->article['errors'] = [
  4002. 'Contact' => ['text' => 'wrong']
  4003. ];
  4004. $this->Form->create($this->article);
  4005. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  4006. $result = $this->Form->text('Model.text');
  4007. $expected = [
  4008. 'input' => [
  4009. 'type' => 'text',
  4010. 'name' => 'Model[text]',
  4011. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4012. ]
  4013. ];
  4014. $this->assertHtml($expected, $result);
  4015. $this->Form->request->data['Contact']['text'] = 'test';
  4016. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4017. $expected = [
  4018. 'input' => [
  4019. 'type' => 'text',
  4020. 'name' => 'Contact[text]',
  4021. 'value' => 'test',
  4022. 'id' => 'theID',
  4023. 'class' => 'form-error'
  4024. ]
  4025. ];
  4026. $this->assertHtml($expected, $result);
  4027. }
  4028. /**
  4029. * testDefaultValue method
  4030. *
  4031. * Test default value setting.
  4032. *
  4033. * @return void
  4034. */
  4035. public function testTextDefaultValue()
  4036. {
  4037. $this->Form->request->data['Model']['field'] = 'test';
  4038. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4039. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4040. $this->assertHtml($expected, $result);
  4041. unset($this->Form->request->data['Model']['field']);
  4042. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4043. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4044. $this->assertHtml($expected, $result);
  4045. $Articles = TableRegistry::get('Articles');
  4046. $title = $Articles->schema()->getColumn('title');
  4047. $Articles->schema()->addColumn(
  4048. 'title',
  4049. ['default' => 'default title'] + $title
  4050. );
  4051. $entity = $Articles->newEntity();
  4052. $this->Form->create($entity);
  4053. // Get default value from schema
  4054. $result = $this->Form->text('title');
  4055. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4056. $this->assertHtml($expected, $result);
  4057. // Don't get value from schema
  4058. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4059. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4060. $this->assertHtml($expected, $result);
  4061. // Custom default value overrides default value from schema
  4062. $result = $this->Form->text('title', ['default' => 'override default']);
  4063. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4064. $this->assertHtml($expected, $result);
  4065. // Default value from schema is used only for new entities.
  4066. $entity->isNew(false);
  4067. $result = $this->Form->text('title');
  4068. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4069. $this->assertHtml($expected, $result);
  4070. }
  4071. /**
  4072. * testError method
  4073. *
  4074. * Test field error generation.
  4075. *
  4076. * @return void
  4077. */
  4078. public function testError()
  4079. {
  4080. $this->article['errors'] = [
  4081. 'Article' => ['field' => 'email']
  4082. ];
  4083. $this->Form->create($this->article);
  4084. $result = $this->Form->error('Article.field');
  4085. $expected = [
  4086. ['div' => ['class' => 'error-message']],
  4087. 'email',
  4088. '/div',
  4089. ];
  4090. $this->assertHtml($expected, $result);
  4091. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4092. $expected = [
  4093. ['div' => ['class' => 'error-message']],
  4094. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4095. '/div',
  4096. ];
  4097. $this->assertHtml($expected, $result);
  4098. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4099. $expected = [
  4100. ['div' => ['class' => 'error-message']],
  4101. '<strong', 'Badness!', '/strong',
  4102. '/div',
  4103. ];
  4104. $this->assertHtml($expected, $result);
  4105. }
  4106. /**
  4107. * testErrorRuleName method
  4108. *
  4109. * Test error translation can use rule names for translating.
  4110. *
  4111. * @return void
  4112. */
  4113. public function testErrorRuleName()
  4114. {
  4115. $this->article['errors'] = [
  4116. 'Article' => [
  4117. 'field' => ['email' => 'Your email was not good']
  4118. ]
  4119. ];
  4120. $this->Form->create($this->article);
  4121. $result = $this->Form->error('Article.field');
  4122. $expected = [
  4123. ['div' => ['class' => 'error-message']],
  4124. 'Your email was not good',
  4125. '/div',
  4126. ];
  4127. $this->assertHtml($expected, $result);
  4128. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4129. $expected = [
  4130. ['div' => ['class' => 'error-message']],
  4131. 'Email in use',
  4132. '/div',
  4133. ];
  4134. $this->assertHtml($expected, $result);
  4135. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4136. $expected = [
  4137. ['div' => ['class' => 'error-message']],
  4138. 'Email in use',
  4139. '/div',
  4140. ];
  4141. $this->assertHtml($expected, $result);
  4142. $result = $this->Form->error('Article.field', [
  4143. 'email' => 'Key is preferred',
  4144. 'Your email was not good' => 'Email in use'
  4145. ]);
  4146. $expected = [
  4147. ['div' => ['class' => 'error-message']],
  4148. 'Key is preferred',
  4149. '/div',
  4150. ];
  4151. $this->assertHtml($expected, $result);
  4152. }
  4153. /**
  4154. * testErrorMessages method
  4155. *
  4156. * Test error with nested lists.
  4157. *
  4158. * @return void
  4159. */
  4160. public function testErrorMessages()
  4161. {
  4162. $this->article['errors'] = [
  4163. 'Article' => ['field' => 'email']
  4164. ];
  4165. $this->Form->create($this->article);
  4166. $result = $this->Form->error('Article.field', [
  4167. 'email' => 'No good!'
  4168. ]);
  4169. $expected = [
  4170. 'div' => ['class' => 'error-message'],
  4171. 'No good!',
  4172. '/div'
  4173. ];
  4174. $this->assertHtml($expected, $result);
  4175. }
  4176. /**
  4177. * testErrorMultipleMessages method
  4178. *
  4179. * Test error() with multiple messages.
  4180. *
  4181. * @return void
  4182. */
  4183. public function testErrorMultipleMessages()
  4184. {
  4185. $this->article['errors'] = [
  4186. 'field' => ['notBlank', 'email', 'Something else']
  4187. ];
  4188. $this->Form->create($this->article);
  4189. $result = $this->Form->error('field', [
  4190. 'notBlank' => 'Cannot be empty',
  4191. 'email' => 'No good!'
  4192. ]);
  4193. $expected = [
  4194. 'div' => ['class' => 'error-message'],
  4195. 'ul' => [],
  4196. '<li', 'Cannot be empty', '/li',
  4197. '<li', 'No good!', '/li',
  4198. '<li', 'Something else', '/li',
  4199. '/ul',
  4200. '/div'
  4201. ];
  4202. $this->assertHtml($expected, $result);
  4203. }
  4204. /**
  4205. * testPassword method
  4206. *
  4207. * Test password element generation.
  4208. *
  4209. * @return void
  4210. */
  4211. public function testPassword()
  4212. {
  4213. $this->article['errors'] = [
  4214. 'Contact' => [
  4215. 'passwd' => 1
  4216. ]
  4217. ];
  4218. $this->Form->create($this->article);
  4219. $result = $this->Form->password('Contact.field');
  4220. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4221. $this->assertHtml($expected, $result);
  4222. $this->Form->request->data['Contact']['passwd'] = 'test';
  4223. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4224. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4225. $this->assertHtml($expected, $result);
  4226. }
  4227. /**
  4228. * testRadio method
  4229. *
  4230. * Test radio element set generation.
  4231. *
  4232. * @return void
  4233. */
  4234. public function testRadio()
  4235. {
  4236. $result = $this->Form->radio('Model.field', ['option A']);
  4237. $expected = [
  4238. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4239. 'label' => ['for' => 'model-field-0'],
  4240. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4241. 'option A',
  4242. '/label'
  4243. ];
  4244. $this->assertHtml($expected, $result);
  4245. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4246. $this->assertHtml($expected, $result);
  4247. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4248. $expected = [
  4249. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4250. ['label' => ['for' => 'model-field-0']],
  4251. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4252. 'option A',
  4253. '/label',
  4254. ['label' => ['for' => 'model-field-1']],
  4255. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4256. 'option B',
  4257. '/label',
  4258. ];
  4259. $this->assertHtml($expected, $result);
  4260. $result = $this->Form->radio(
  4261. 'Employee.gender',
  4262. ['male' => 'Male', 'female' => 'Female'],
  4263. ['form' => 'my-form']
  4264. );
  4265. $expected = [
  4266. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4267. ['label' => ['for' => 'employee-gender-male']],
  4268. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4269. 'Male',
  4270. '/label',
  4271. ['label' => ['for' => 'employee-gender-female']],
  4272. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4273. 'Female',
  4274. '/label',
  4275. ];
  4276. $this->assertHtml($expected, $result);
  4277. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4278. $expected = [
  4279. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4280. ['label' => ['for' => 'model-custom-0']],
  4281. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4282. 'option A',
  4283. '/label',
  4284. ['label' => ['for' => 'model-custom-1']],
  4285. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4286. 'option B',
  4287. '/label',
  4288. ];
  4289. $this->assertHtml($expected, $result);
  4290. $result = $this->Form->radio(
  4291. 'Employee.gender',
  4292. [
  4293. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4294. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4295. ]
  4296. );
  4297. $expected = [
  4298. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4299. ['label' => ['for' => 'employee-gender-male']],
  4300. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4301. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4302. 'Male',
  4303. '/label',
  4304. ['label' => ['for' => 'employee-gender-female']],
  4305. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4306. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4307. 'Female',
  4308. '/label',
  4309. ];
  4310. $this->assertHtml($expected, $result);
  4311. }
  4312. /**
  4313. * Test radio with complex options and empty disabled data.
  4314. *
  4315. * @return void
  4316. */
  4317. public function testRadioComplexDisabled()
  4318. {
  4319. $options = [
  4320. ['value' => 'r', 'text' => 'red'],
  4321. ['value' => 'b', 'text' => 'blue'],
  4322. ];
  4323. $attrs = ['disabled' => []];
  4324. $result = $this->Form->radio('Model.field', $options, $attrs);
  4325. $expected = [
  4326. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4327. ['label' => ['for' => 'model-field-r']],
  4328. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4329. 'red',
  4330. '/label',
  4331. ['label' => ['for' => 'model-field-b']],
  4332. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4333. 'blue',
  4334. '/label',
  4335. ];
  4336. $this->assertHtml($expected, $result);
  4337. $attrs = ['disabled' => ['r']];
  4338. $result = $this->Form->radio('Model.field', $options, $attrs);
  4339. $this->assertContains('disabled="disabled"', $result);
  4340. }
  4341. /**
  4342. * testRadioDefaultValue method
  4343. *
  4344. * Test default value setting on radio() method.
  4345. *
  4346. * @return void
  4347. */
  4348. public function testRadioDefaultValue()
  4349. {
  4350. $Articles = TableRegistry::get('Articles');
  4351. $title = $Articles->schema()->getColumn('title');
  4352. $Articles->schema()->addColumn(
  4353. 'title',
  4354. ['default' => '1'] + $title
  4355. );
  4356. $this->Form->create($Articles->newEntity());
  4357. $result = $this->Form->radio('title', ['option A', 'option B']);
  4358. $expected = [
  4359. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4360. ['label' => ['for' => 'title-0']],
  4361. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4362. 'option A',
  4363. '/label',
  4364. ['label' => ['for' => 'title-1']],
  4365. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4366. 'option B',
  4367. '/label',
  4368. ];
  4369. $this->assertHtml($expected, $result);
  4370. }
  4371. /**
  4372. * Test setting a hiddenField value on radio buttons.
  4373. *
  4374. * @return void
  4375. */
  4376. public function testRadioHiddenFieldValue()
  4377. {
  4378. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4379. $expected = [
  4380. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4381. 'label' => ['for' => 'title-0'],
  4382. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4383. 'option A',
  4384. '/label',
  4385. ];
  4386. $this->assertHtml($expected, $result);
  4387. }
  4388. /**
  4389. * testControlRadio method
  4390. *
  4391. * Test that input works with radio types.
  4392. *
  4393. * @return void
  4394. */
  4395. public function testControlRadio()
  4396. {
  4397. $result = $this->Form->control('test', [
  4398. 'type' => 'radio',
  4399. 'options' => ['A', 'B'],
  4400. ]);
  4401. $expected = [
  4402. ['div' => ['class' => 'input radio']],
  4403. '<label',
  4404. 'Test',
  4405. '/label',
  4406. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4407. ['label' => ['for' => 'test-0']],
  4408. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4409. 'A',
  4410. '/label',
  4411. ['label' => ['for' => 'test-1']],
  4412. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4413. 'B',
  4414. '/label',
  4415. '/div',
  4416. ];
  4417. $this->assertHtml($expected, $result);
  4418. $result = $this->Form->control('test', [
  4419. 'type' => 'radio',
  4420. 'options' => ['A', 'B'],
  4421. 'value' => '0'
  4422. ]);
  4423. $expected = [
  4424. ['div' => ['class' => 'input radio']],
  4425. '<label',
  4426. 'Test',
  4427. '/label',
  4428. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4429. ['label' => ['for' => 'test-0']],
  4430. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4431. 'A',
  4432. '/label',
  4433. ['label' => ['for' => 'test-1']],
  4434. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4435. 'B',
  4436. '/label',
  4437. '/div',
  4438. ];
  4439. $this->assertHtml($expected, $result);
  4440. $result = $this->Form->control('test', [
  4441. 'type' => 'radio',
  4442. 'options' => ['A', 'B'],
  4443. 'label' => false
  4444. ]);
  4445. $expected = [
  4446. ['div' => ['class' => 'input radio']],
  4447. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4448. ['label' => ['for' => 'test-0']],
  4449. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4450. 'A',
  4451. '/label',
  4452. ['label' => ['for' => 'test-1']],
  4453. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4454. 'B',
  4455. '/label',
  4456. '/div',
  4457. ];
  4458. $this->assertHtml($expected, $result);
  4459. }
  4460. /**
  4461. * testRadioNoLabel method
  4462. *
  4463. * Test that radio() works with label = false.
  4464. *
  4465. * @return void
  4466. */
  4467. public function testRadioNoLabel()
  4468. {
  4469. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4470. $expected = [
  4471. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4472. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4473. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4474. ];
  4475. $this->assertHtml($expected, $result);
  4476. }
  4477. /**
  4478. * testRadioControlInsideLabel method
  4479. *
  4480. * Test generating radio input inside label ala twitter bootstrap.
  4481. *
  4482. * @return void
  4483. */
  4484. public function testRadioControlInsideLabel()
  4485. {
  4486. $this->Form->templates([
  4487. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4488. 'radioWrapper' => '{{label}}'
  4489. ]);
  4490. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4491. //@codingStandardsIgnoreStart
  4492. $expected = [
  4493. ['input' => [
  4494. 'type' => 'hidden',
  4495. 'name' => 'Model[field]',
  4496. 'value' => ''
  4497. ]],
  4498. ['label' => ['for' => 'model-field-0']],
  4499. ['input' => [
  4500. 'type' => 'radio',
  4501. 'name' => 'Model[field]',
  4502. 'value' => '0',
  4503. 'id' => 'model-field-0'
  4504. ]],
  4505. 'option A',
  4506. '/label',
  4507. ['label' => ['for' => 'model-field-1']],
  4508. ['input' => [
  4509. 'type' => 'radio',
  4510. 'name' => 'Model[field]',
  4511. 'value' => '1',
  4512. 'id' => 'model-field-1'
  4513. ]],
  4514. 'option B',
  4515. '/label',
  4516. ];
  4517. //@codingStandardsIgnoreEnd
  4518. $this->assertHtml($expected, $result);
  4519. }
  4520. /**
  4521. * testRadioHiddenControlDisabling method
  4522. *
  4523. * Test disabling the hidden input for radio buttons.
  4524. *
  4525. * @return void
  4526. */
  4527. public function testRadioHiddenControlDisabling()
  4528. {
  4529. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4530. $expected = [
  4531. 'label' => ['for' => 'model-1-field-0'],
  4532. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4533. 'option A',
  4534. '/label'
  4535. ];
  4536. $this->assertHtml($expected, $result);
  4537. }
  4538. /**
  4539. * testRadioOutOfRange method
  4540. *
  4541. * Test radio element set generation.
  4542. *
  4543. * @return void
  4544. */
  4545. public function testRadioOutOfRange()
  4546. {
  4547. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4548. $expected = [
  4549. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4550. 'label' => ['for' => 'model-field-v'],
  4551. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4552. 'value',
  4553. '/label'
  4554. ];
  4555. $this->assertHtml($expected, $result);
  4556. }
  4557. /**
  4558. * testSelect method
  4559. *
  4560. * Test select element generation.
  4561. *
  4562. * @return void
  4563. */
  4564. public function testSelect()
  4565. {
  4566. $result = $this->Form->select('Model.field', []);
  4567. $expected = [
  4568. 'select' => ['name' => 'Model[field]'],
  4569. '/select'
  4570. ];
  4571. $this->assertHtml($expected, $result);
  4572. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4573. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4574. $expected = [
  4575. 'select' => ['name' => 'Model[field]'],
  4576. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4577. 'good',
  4578. '/option',
  4579. ['option' => ['value' => 'other']],
  4580. 'bad',
  4581. '/option',
  4582. '/select'
  4583. ];
  4584. $this->assertHtml($expected, $result);
  4585. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4586. $this->assertHtml($expected, $result);
  4587. $this->Form->request->data = [];
  4588. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4589. $expected = [
  4590. 'select' => ['name' => 'Model[field]'],
  4591. ['option' => ['value' => 'value']],
  4592. 'good',
  4593. '/option',
  4594. ['option' => ['value' => 'other']],
  4595. 'bad',
  4596. '/option',
  4597. '/select'
  4598. ];
  4599. $this->assertHtml($expected, $result);
  4600. $options = [
  4601. ['value' => 'first', 'text' => 'First'],
  4602. ['value' => 'first', 'text' => 'Another First'],
  4603. ];
  4604. $result = $this->Form->select(
  4605. 'Model.field',
  4606. $options,
  4607. ['escape' => false, 'empty' => false]
  4608. );
  4609. $expected = [
  4610. 'select' => ['name' => 'Model[field]'],
  4611. ['option' => ['value' => 'first']],
  4612. 'First',
  4613. '/option',
  4614. ['option' => ['value' => 'first']],
  4615. 'Another First',
  4616. '/option',
  4617. '/select'
  4618. ];
  4619. $this->assertHtml($expected, $result);
  4620. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4621. $result = $this->Form->select(
  4622. 'Model.contact_id',
  4623. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4624. ['escape' => false, 'empty' => 'pick something']
  4625. );
  4626. $expected = [
  4627. 'select' => ['name' => 'Model[contact_id]'],
  4628. ['option' => ['value' => '']], 'pick something', '/option',
  4629. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4630. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4631. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4632. '/select'
  4633. ];
  4634. $this->assertHtml($expected, $result);
  4635. $this->Form->request->data['Model']['field'] = 0;
  4636. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4637. $expected = [
  4638. 'select' => ['name' => 'Model[field]'],
  4639. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4640. ['option' => ['value' => '1']], 'Yes', '/option',
  4641. '/select'
  4642. ];
  4643. $this->assertHtml($expected, $result);
  4644. }
  4645. /**
  4646. * testSelectEscapeHtml method
  4647. *
  4648. * Test that select() escapes HTML.
  4649. *
  4650. * @return void
  4651. */
  4652. public function testSelectEscapeHtml()
  4653. {
  4654. $result = $this->Form->select(
  4655. 'Model.field',
  4656. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4657. ['empty' => false]
  4658. );
  4659. $expected = [
  4660. 'select' => ['name' => 'Model[field]'],
  4661. ['option' => ['value' => 'first']],
  4662. 'first &quot;html&quot; &lt;chars&gt;',
  4663. '/option',
  4664. ['option' => ['value' => 'second']],
  4665. 'value',
  4666. '/option',
  4667. '/select'
  4668. ];
  4669. $this->assertHtml($expected, $result);
  4670. $result = $this->Form->select(
  4671. 'Model.field',
  4672. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4673. ['escape' => false, 'empty' => false]
  4674. );
  4675. $expected = [
  4676. 'select' => ['name' => 'Model[field]'],
  4677. ['option' => ['value' => 'first']],
  4678. 'first "html" <chars>',
  4679. '/option',
  4680. ['option' => ['value' => 'second']],
  4681. 'value',
  4682. '/option',
  4683. '/select'
  4684. ];
  4685. $this->assertHtml($expected, $result);
  4686. }
  4687. /**
  4688. * testSelectRequired method
  4689. *
  4690. * Test select() with required and disabled attributes.
  4691. *
  4692. * @return void
  4693. */
  4694. public function testSelectRequired()
  4695. {
  4696. $this->article['required'] = [
  4697. 'user_id' => true
  4698. ];
  4699. $this->Form->create($this->article);
  4700. $result = $this->Form->select('user_id', ['option A']);
  4701. $expected = [
  4702. 'select' => [
  4703. 'name' => 'user_id',
  4704. 'required' => 'required'
  4705. ],
  4706. ['option' => ['value' => '0']], 'option A', '/option',
  4707. '/select'
  4708. ];
  4709. $this->assertHtml($expected, $result);
  4710. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4711. $expected = [
  4712. 'select' => [
  4713. 'name' => 'user_id',
  4714. 'disabled' => 'disabled'
  4715. ],
  4716. ['option' => ['value' => '0']], 'option A', '/option',
  4717. '/select'
  4718. ];
  4719. $this->assertHtml($expected, $result);
  4720. }
  4721. /**
  4722. * testNestedSelect method
  4723. *
  4724. * Test select element generation with optgroups.
  4725. *
  4726. * @return void
  4727. */
  4728. public function testNestedSelect()
  4729. {
  4730. $result = $this->Form->select(
  4731. 'Model.field',
  4732. [1 => 'One', 2 => 'Two', 'Three' => [
  4733. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4734. ]],
  4735. ['empty' => false]
  4736. );
  4737. $expected = [
  4738. 'select' => ['name' => 'Model[field]'],
  4739. ['option' => ['value' => 1]],
  4740. 'One',
  4741. '/option',
  4742. ['option' => ['value' => 2]],
  4743. 'Two',
  4744. '/option',
  4745. ['optgroup' => ['label' => 'Three']],
  4746. ['option' => ['value' => 3]],
  4747. 'Three',
  4748. '/option',
  4749. ['option' => ['value' => 4]],
  4750. 'Four',
  4751. '/option',
  4752. ['option' => ['value' => 5]],
  4753. 'Five',
  4754. '/option',
  4755. '/optgroup',
  4756. '/select'
  4757. ];
  4758. $this->assertHtml($expected, $result);
  4759. }
  4760. /**
  4761. * testSelectMultiple method
  4762. *
  4763. * Test generation of multiple select elements.
  4764. *
  4765. * @return void
  4766. */
  4767. public function testSelectMultiple()
  4768. {
  4769. $options = ['first', 'second', 'third'];
  4770. $result = $this->Form->select(
  4771. 'Model.multi_field',
  4772. $options,
  4773. ['form' => 'my-form', 'multiple' => true]
  4774. );
  4775. $expected = [
  4776. 'input' => [
  4777. 'type' => 'hidden',
  4778. 'name' => 'Model[multi_field]',
  4779. 'value' => '',
  4780. 'form' => 'my-form',
  4781. ],
  4782. 'select' => [
  4783. 'name' => 'Model[multi_field][]',
  4784. 'multiple' => 'multiple',
  4785. 'form' => 'my-form',
  4786. ],
  4787. ['option' => ['value' => '0']],
  4788. 'first',
  4789. '/option',
  4790. ['option' => ['value' => '1']],
  4791. 'second',
  4792. '/option',
  4793. ['option' => ['value' => '2']],
  4794. 'third',
  4795. '/option',
  4796. '/select'
  4797. ];
  4798. $this->assertHtml($expected, $result);
  4799. $result = $this->Form->select(
  4800. 'Model.multi_field',
  4801. $options,
  4802. ['multiple' => 'multiple', 'form' => 'my-form']
  4803. );
  4804. $this->assertHtml($expected, $result);
  4805. }
  4806. /**
  4807. * testCheckboxZeroValue method
  4808. *
  4809. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4810. *
  4811. * @return void
  4812. */
  4813. public function testCheckboxZeroValue()
  4814. {
  4815. $result = $this->Form->control('User.get_spam', [
  4816. 'type' => 'checkbox',
  4817. 'value' => '0',
  4818. 'hiddenField' => '1',
  4819. ]);
  4820. $expected = [
  4821. 'div' => ['class' => 'input checkbox'],
  4822. 'label' => ['for' => 'user-get-spam'],
  4823. ['input' => [
  4824. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4825. 'value' => '1'
  4826. ]],
  4827. ['input' => [
  4828. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4829. 'value' => '0', 'id' => 'user-get-spam'
  4830. ]],
  4831. 'Get Spam',
  4832. '/label',
  4833. '/div'
  4834. ];
  4835. $this->assertHtml($expected, $result);
  4836. }
  4837. /**
  4838. * testHabtmSelectBox method
  4839. *
  4840. * Test generation of habtm select boxes.
  4841. *
  4842. * @return void
  4843. */
  4844. public function testHabtmSelectBox()
  4845. {
  4846. $this->loadFixtures('Articles');
  4847. $options = [
  4848. 1 => 'blue',
  4849. 2 => 'red',
  4850. 3 => 'green'
  4851. ];
  4852. $tags = [
  4853. new Entity(['id' => 1, 'name' => 'blue']),
  4854. new Entity(['id' => 3, 'name' => 'green'])
  4855. ];
  4856. $article = new Article(['tags' => $tags]);
  4857. $this->Form->create($article);
  4858. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4859. $expected = [
  4860. 'div' => ['class' => 'input select'],
  4861. 'label' => ['for' => 'tags-ids'],
  4862. 'Tags',
  4863. '/label',
  4864. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4865. 'select' => [
  4866. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4867. 'multiple' => 'multiple'
  4868. ],
  4869. ['option' => ['value' => '1', 'selected' => 'selected']],
  4870. 'blue',
  4871. '/option',
  4872. ['option' => ['value' => '2']],
  4873. 'red',
  4874. '/option',
  4875. ['option' => ['value' => '3', 'selected' => 'selected']],
  4876. 'green',
  4877. '/option',
  4878. '/select',
  4879. '/div'
  4880. ];
  4881. $this->assertHtml($expected, $result);
  4882. // make sure only 50 is selected, and not 50f5c0cf
  4883. $options = [
  4884. '1' => 'blue',
  4885. '50f5c0cf' => 'red',
  4886. '50' => 'green'
  4887. ];
  4888. $tags = [
  4889. new Entity(['id' => 1, 'name' => 'blue']),
  4890. new Entity(['id' => 50, 'name' => 'green'])
  4891. ];
  4892. $article = new Article(['tags' => $tags]);
  4893. $this->Form->create($article);
  4894. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4895. $expected = [
  4896. 'div' => ['class' => 'input select'],
  4897. 'label' => ['for' => 'tags-ids'],
  4898. 'Tags',
  4899. '/label',
  4900. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4901. 'select' => [
  4902. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4903. 'multiple' => 'multiple'
  4904. ],
  4905. ['option' => ['value' => '1', 'selected' => 'selected']],
  4906. 'blue',
  4907. '/option',
  4908. ['option' => ['value' => '50f5c0cf']],
  4909. 'red',
  4910. '/option',
  4911. ['option' => ['value' => '50', 'selected' => 'selected']],
  4912. 'green',
  4913. '/option',
  4914. '/select',
  4915. '/div'
  4916. ];
  4917. $this->assertHtml($expected, $result);
  4918. $spacecraft = [
  4919. 1 => 'Orion',
  4920. 2 => 'Helios'
  4921. ];
  4922. $this->View->viewVars['spacecraft'] = $spacecraft;
  4923. $this->Form->create();
  4924. $result = $this->Form->control('spacecraft._ids');
  4925. $expected = [
  4926. 'div' => ['class' => 'input select'],
  4927. 'label' => ['for' => 'spacecraft-ids'],
  4928. 'Spacecraft',
  4929. '/label',
  4930. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4931. 'select' => [
  4932. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4933. 'multiple' => 'multiple'
  4934. ],
  4935. ['option' => ['value' => '1']],
  4936. 'Orion',
  4937. '/option',
  4938. ['option' => ['value' => '2']],
  4939. 'Helios',
  4940. '/option',
  4941. '/select',
  4942. '/div'
  4943. ];
  4944. $this->assertHtml($expected, $result);
  4945. }
  4946. /**
  4947. * testErrorsForBelongsToManySelect method
  4948. *
  4949. * Tests that errors for belongsToMany select fields are being
  4950. * picked up properly.
  4951. *
  4952. * @return void
  4953. */
  4954. public function testErrorsForBelongsToManySelect()
  4955. {
  4956. $spacecraft = [
  4957. 1 => 'Orion',
  4958. 2 => 'Helios'
  4959. ];
  4960. $this->View->viewVars['spacecraft'] = $spacecraft;
  4961. $article = new Article();
  4962. $article->errors('spacecraft', ['Invalid']);
  4963. $this->Form->create($article);
  4964. $result = $this->Form->control('spacecraft._ids');
  4965. $expected = [
  4966. ['div' => ['class' => 'input select error']],
  4967. 'label' => ['for' => 'spacecraft-ids'],
  4968. 'Spacecraft',
  4969. '/label',
  4970. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4971. 'select' => [
  4972. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4973. 'multiple' => 'multiple'
  4974. ],
  4975. ['option' => ['value' => '1']],
  4976. 'Orion',
  4977. '/option',
  4978. ['option' => ['value' => '2']],
  4979. 'Helios',
  4980. '/option',
  4981. '/select',
  4982. ['div' => ['class' => 'error-message']],
  4983. 'Invalid',
  4984. '/div',
  4985. '/div'
  4986. ];
  4987. $this->assertHtml($expected, $result);
  4988. }
  4989. /**
  4990. * testSelectMultipleCheckboxes method
  4991. *
  4992. * Test generation of multi select elements in checkbox format.
  4993. *
  4994. * @return void
  4995. */
  4996. public function testSelectMultipleCheckboxes()
  4997. {
  4998. $result = $this->Form->select(
  4999. 'Model.multi_field',
  5000. ['first', 'second', 'third'],
  5001. ['multiple' => 'checkbox']
  5002. );
  5003. $expected = [
  5004. 'input' => [
  5005. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5006. ],
  5007. ['div' => ['class' => 'checkbox']],
  5008. ['label' => ['for' => 'model-multi-field-0']],
  5009. ['input' => [
  5010. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5011. 'value' => '0', 'id' => 'model-multi-field-0'
  5012. ]],
  5013. 'first',
  5014. '/label',
  5015. '/div',
  5016. ['div' => ['class' => 'checkbox']],
  5017. ['label' => ['for' => 'model-multi-field-1']],
  5018. ['input' => [
  5019. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5020. 'value' => '1', 'id' => 'model-multi-field-1'
  5021. ]],
  5022. 'second',
  5023. '/label',
  5024. '/div',
  5025. ['div' => ['class' => 'checkbox']],
  5026. ['label' => ['for' => 'model-multi-field-2']],
  5027. ['input' => [
  5028. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5029. 'value' => '2', 'id' => 'model-multi-field-2'
  5030. ]],
  5031. 'third',
  5032. '/label',
  5033. '/div'
  5034. ];
  5035. $this->assertHtml($expected, $result);
  5036. $result = $this->Form->select(
  5037. 'Model.multi_field',
  5038. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5039. ['multiple' => 'checkbox']
  5040. );
  5041. $expected = [
  5042. 'input' => [
  5043. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5044. ],
  5045. ['div' => ['class' => 'checkbox']],
  5046. ['label' => ['for' => 'model-multi-field-a+']],
  5047. ['input' => [
  5048. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5049. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5050. ]],
  5051. 'first',
  5052. '/label',
  5053. '/div',
  5054. ['div' => ['class' => 'checkbox']],
  5055. ['label' => ['for' => 'model-multi-field-a++']],
  5056. ['input' => [
  5057. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5058. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5059. ]],
  5060. 'second',
  5061. '/label',
  5062. '/div',
  5063. ['div' => ['class' => 'checkbox']],
  5064. ['label' => ['for' => 'model-multi-field-a+++']],
  5065. ['input' => [
  5066. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5067. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5068. ]],
  5069. 'third',
  5070. '/label',
  5071. '/div'
  5072. ];
  5073. $this->assertHtml($expected, $result);
  5074. $result = $this->Form->select(
  5075. 'Model.multi_field',
  5076. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5077. ['multiple' => 'checkbox']
  5078. );
  5079. $expected = [
  5080. 'input' => [
  5081. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5082. ],
  5083. ['div' => ['class' => 'checkbox']],
  5084. ['label' => ['for' => 'model-multi-field-a-b']],
  5085. ['input' => [
  5086. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5087. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5088. ]],
  5089. 'first',
  5090. '/label',
  5091. '/div',
  5092. ['div' => ['class' => 'checkbox']],
  5093. ['label' => ['for' => 'model-multi-field-a-b1']],
  5094. ['input' => [
  5095. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5096. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5097. ]],
  5098. 'second',
  5099. '/label',
  5100. '/div',
  5101. ['div' => ['class' => 'checkbox']],
  5102. ['label' => ['for' => 'model-multi-field-a-b2']],
  5103. ['input' => [
  5104. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5105. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5106. ]],
  5107. 'third',
  5108. '/label',
  5109. '/div'
  5110. ];
  5111. $this->assertHtml($expected, $result);
  5112. }
  5113. /**
  5114. * testSelectMultipleCheckboxRequestData method
  5115. *
  5116. * Ensure that multiCheckbox reads from the request data.
  5117. *
  5118. * @return void
  5119. */
  5120. public function testSelectMultipleCheckboxRequestData()
  5121. {
  5122. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  5123. $result = $this->Form->select(
  5124. 'Model.tags',
  5125. ['1' => 'first', 'Array' => 'Array'],
  5126. ['multiple' => 'checkbox']
  5127. );
  5128. $expected = [
  5129. 'input' => [
  5130. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5131. ],
  5132. ['div' => ['class' => 'checkbox']],
  5133. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5134. ['input' => [
  5135. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5136. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5137. ]],
  5138. 'first',
  5139. '/label',
  5140. '/div',
  5141. ['div' => ['class' => 'checkbox']],
  5142. ['label' => ['for' => 'model-tags-array']],
  5143. ['input' => [
  5144. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5145. 'value' => 'Array', 'id' => 'model-tags-array'
  5146. ]],
  5147. 'Array',
  5148. '/label',
  5149. '/div'
  5150. ];
  5151. $this->assertHtml($expected, $result);
  5152. }
  5153. /**
  5154. * testSelectMultipleCheckboxSecurity method
  5155. *
  5156. * Checks the security hash array generated for multiple-input checkbox elements.
  5157. *
  5158. * @return void
  5159. */
  5160. public function testSelectMultipleCheckboxSecurity()
  5161. {
  5162. $this->Form->request->params['_Token'] = 'testKey';
  5163. $this->assertEquals([], $this->Form->fields);
  5164. $this->Form->select(
  5165. 'Model.multi_field',
  5166. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5167. ['multiple' => 'checkbox']
  5168. );
  5169. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5170. $result = $this->Form->secure($this->Form->fields);
  5171. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5172. $this->assertContains('"' . $key . '"', $result);
  5173. }
  5174. /**
  5175. * testSelectMultipleSecureWithNoOptions method
  5176. *
  5177. * Multiple select elements should always be secured as they always participate
  5178. * in the POST data.
  5179. *
  5180. * @return void
  5181. */
  5182. public function testSelectMultipleSecureWithNoOptions()
  5183. {
  5184. $this->assertEquals([], $this->Form->fields);
  5185. $this->Form->select(
  5186. 'Model.select',
  5187. [],
  5188. ['multiple' => true]
  5189. );
  5190. $this->assertEquals(['Model.select'], $this->Form->fields);
  5191. }
  5192. /**
  5193. * testSelectNoSecureWithNoOptions method
  5194. *
  5195. * When a select box has no options it should not be added to the fields list
  5196. * as it always fail post validation.
  5197. *
  5198. * @return void
  5199. */
  5200. public function testSelectNoSecureWithNoOptions()
  5201. {
  5202. $this->Form->request->params['_Token'] = 'testkey';
  5203. $this->assertEquals([], $this->Form->fields);
  5204. $this->Form->select(
  5205. 'Model.select',
  5206. []
  5207. );
  5208. $this->assertEquals([], $this->Form->fields);
  5209. $this->Form->select(
  5210. 'Model.user_id',
  5211. [],
  5212. ['empty' => true]
  5213. );
  5214. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5215. }
  5216. /**
  5217. * testControlMultipleCheckboxes method
  5218. *
  5219. * Test control() resulting in multi select elements being generated.
  5220. *
  5221. * @return void
  5222. */
  5223. public function testControlMultipleCheckboxes()
  5224. {
  5225. $result = $this->Form->control('Model.multi_field', [
  5226. 'options' => ['first', 'second', 'third'],
  5227. 'multiple' => 'checkbox'
  5228. ]);
  5229. $expected = [
  5230. ['div' => ['class' => 'input select']],
  5231. ['label' => ['for' => 'model-multi-field']],
  5232. 'Multi Field',
  5233. '/label',
  5234. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5235. ['div' => ['class' => 'checkbox']],
  5236. ['label' => ['for' => 'model-multi-field-0']],
  5237. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5238. 'first',
  5239. '/label',
  5240. '/div',
  5241. ['div' => ['class' => 'checkbox']],
  5242. ['label' => ['for' => 'model-multi-field-1']],
  5243. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5244. 'second',
  5245. '/label',
  5246. '/div',
  5247. ['div' => ['class' => 'checkbox']],
  5248. ['label' => ['for' => 'model-multi-field-2']],
  5249. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5250. 'third',
  5251. '/label',
  5252. '/div',
  5253. '/div'
  5254. ];
  5255. $this->assertHtml($expected, $result);
  5256. $result = $this->Form->control('Model.multi_field', [
  5257. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5258. 'multiple' => 'checkbox'
  5259. ]);
  5260. $expected = [
  5261. ['div' => ['class' => 'input select']],
  5262. ['label' => ['for' => 'model-multi-field']],
  5263. 'Multi Field',
  5264. '/label',
  5265. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5266. ['div' => ['class' => 'checkbox']],
  5267. ['label' => ['for' => 'model-multi-field-a']],
  5268. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5269. 'first',
  5270. '/label',
  5271. '/div',
  5272. ['div' => ['class' => 'checkbox']],
  5273. ['label' => ['for' => 'model-multi-field-b']],
  5274. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5275. 'second',
  5276. '/label',
  5277. '/div',
  5278. ['div' => ['class' => 'checkbox']],
  5279. ['label' => ['for' => 'model-multi-field-c']],
  5280. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5281. 'third',
  5282. '/label',
  5283. '/div',
  5284. '/div'
  5285. ];
  5286. $this->assertHtml($expected, $result);
  5287. }
  5288. /**
  5289. * testSelectHiddenFieldOmission method
  5290. *
  5291. * Test that select() with 'hiddenField' => false omits the hidden field.
  5292. *
  5293. * @return void
  5294. */
  5295. public function testSelectHiddenFieldOmission()
  5296. {
  5297. $result = $this->Form->select(
  5298. 'Model.multi_field',
  5299. ['first', 'second'],
  5300. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5301. );
  5302. $this->assertNotContains('type="hidden"', $result);
  5303. }
  5304. /**
  5305. * testSelectCheckboxMultipleOverrideName method
  5306. *
  5307. * Test that select() with multiple = checkbox works with overriding name attribute.
  5308. *
  5309. * @return void
  5310. */
  5311. public function testSelectCheckboxMultipleOverrideName()
  5312. {
  5313. $result = $this->Form->select('category', ['1', '2'], [
  5314. 'multiple' => 'checkbox',
  5315. 'name' => 'fish',
  5316. ]);
  5317. $expected = [
  5318. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5319. ['div' => ['class' => 'checkbox']],
  5320. ['label' => ['for' => 'fish-0']],
  5321. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5322. '1',
  5323. '/label',
  5324. '/div',
  5325. ['div' => ['class' => 'checkbox']],
  5326. ['label' => ['for' => 'fish-1']],
  5327. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5328. '2',
  5329. '/label',
  5330. '/div'
  5331. ];
  5332. $this->assertHtml($expected, $result);
  5333. $result = $this->Form->multiCheckbox(
  5334. 'category',
  5335. new Collection(['1', '2']),
  5336. ['name' => 'fish']
  5337. );
  5338. $this->assertHtml($expected, $result);
  5339. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5340. 'name' => 'fish',
  5341. ]);
  5342. $this->assertHtml($expected, $result);
  5343. }
  5344. /**
  5345. * testControlMultiCheckbox method
  5346. *
  5347. * Test that control() works with multicheckbox.
  5348. *
  5349. * @return void
  5350. */
  5351. public function testControlMultiCheckbox()
  5352. {
  5353. $result = $this->Form->control('category', [
  5354. 'type' => 'multicheckbox',
  5355. 'options' => ['1', '2'],
  5356. ]);
  5357. $expected = [
  5358. ['div' => ['class' => 'input multicheckbox']],
  5359. '<label',
  5360. 'Category',
  5361. '/label',
  5362. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5363. ['div' => ['class' => 'checkbox']],
  5364. ['label' => ['for' => 'category-0']],
  5365. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5366. '1',
  5367. '/label',
  5368. '/div',
  5369. ['div' => ['class' => 'checkbox']],
  5370. ['label' => ['for' => 'category-1']],
  5371. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5372. '2',
  5373. '/label',
  5374. '/div',
  5375. '/div',
  5376. ];
  5377. $this->assertHtml($expected, $result);
  5378. }
  5379. /**
  5380. * testCheckbox method
  5381. *
  5382. * Test generation of checkboxes.
  5383. *
  5384. * @return void
  5385. */
  5386. public function testCheckbox()
  5387. {
  5388. $result = $this->Form->checkbox('Model.field');
  5389. $expected = [
  5390. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5391. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5392. ];
  5393. $this->assertHtml($expected, $result);
  5394. $result = $this->Form->checkbox('Model.field', [
  5395. 'id' => 'theID',
  5396. 'value' => 'myvalue',
  5397. 'form' => 'my-form',
  5398. ]);
  5399. $expected = [
  5400. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5401. ['input' => [
  5402. 'type' => 'checkbox', 'name' => 'Model[field]',
  5403. 'value' => 'myvalue', 'id' => 'theID',
  5404. 'form' => 'my-form',
  5405. ]]
  5406. ];
  5407. $this->assertHtml($expected, $result);
  5408. }
  5409. /**
  5410. * testCheckboxDefaultValue method
  5411. *
  5412. * Test default value setting on checkbox() method.
  5413. *
  5414. * @return void
  5415. */
  5416. public function testCheckboxDefaultValue()
  5417. {
  5418. $this->Form->request->data['Model']['field'] = false;
  5419. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5420. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5421. $this->assertHtml($expected, $result);
  5422. unset($this->Form->request->data['Model']['field']);
  5423. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5424. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5425. $this->assertHtml($expected, $result);
  5426. $this->Form->request->data['Model']['field'] = true;
  5427. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5428. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5429. $this->assertHtml($expected, $result);
  5430. unset($this->Form->request->data['Model']['field']);
  5431. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5432. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5433. $this->assertHtml($expected, $result);
  5434. $Articles = TableRegistry::get('Articles');
  5435. $Articles->schema()->addColumn(
  5436. 'published',
  5437. ['type' => 'boolean', 'null' => false, 'default' => true]
  5438. );
  5439. $this->Form->create($Articles->newEntity());
  5440. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5441. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5442. $this->assertHtml($expected, $result);
  5443. }
  5444. /**
  5445. * testCheckboxCheckedAndError method
  5446. *
  5447. * Test checkbox being checked or having errors.
  5448. *
  5449. * @return void
  5450. */
  5451. public function testCheckboxCheckedAndError()
  5452. {
  5453. $this->article['errors'] = [
  5454. 'published' => true
  5455. ];
  5456. $this->Form->request->data['published'] = 'myvalue';
  5457. $this->Form->create($this->article);
  5458. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5459. $expected = [
  5460. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5461. ['input' => [
  5462. 'type' => 'checkbox',
  5463. 'name' => 'published',
  5464. 'value' => 'myvalue',
  5465. 'id' => 'theID',
  5466. 'checked' => 'checked',
  5467. 'class' => 'form-error'
  5468. ]]
  5469. ];
  5470. $this->assertHtml($expected, $result);
  5471. $this->Form->request->data['published'] = '';
  5472. $result = $this->Form->checkbox('published');
  5473. $expected = [
  5474. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5475. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5476. ];
  5477. $this->assertHtml($expected, $result);
  5478. }
  5479. /**
  5480. * testCheckboxCustomNameAttribute method
  5481. *
  5482. * Test checkbox() with a custom name attribute.
  5483. *
  5484. * @return void
  5485. */
  5486. public function testCheckboxCustomNameAttribute()
  5487. {
  5488. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5489. $expected = [
  5490. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5491. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5492. ];
  5493. $this->assertHtml($expected, $result);
  5494. }
  5495. /**
  5496. * testCheckboxHiddenField method
  5497. *
  5498. * Test that the hidden input for checkboxes can be omitted or set to a
  5499. * specific value.
  5500. *
  5501. * @return void
  5502. */
  5503. public function testCheckboxHiddenField()
  5504. {
  5505. $result = $this->Form->checkbox('UserForm.something', [
  5506. 'hiddenField' => false
  5507. ]);
  5508. $expected = [
  5509. 'input' => [
  5510. 'type' => 'checkbox',
  5511. 'name' => 'UserForm[something]',
  5512. 'value' => '1'
  5513. ],
  5514. ];
  5515. $this->assertHtml($expected, $result);
  5516. $result = $this->Form->checkbox('UserForm.something', [
  5517. 'value' => 'Y',
  5518. 'hiddenField' => 'N',
  5519. ]);
  5520. $expected = [
  5521. ['input' => [
  5522. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5523. 'value' => 'N'
  5524. ]],
  5525. ['input' => [
  5526. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5527. 'value' => 'Y'
  5528. ]],
  5529. ];
  5530. $this->assertHtml($expected, $result);
  5531. }
  5532. /**
  5533. * testTime method
  5534. *
  5535. * Test the time type.
  5536. *
  5537. * @return void
  5538. */
  5539. public function testTime()
  5540. {
  5541. $result = $this->Form->time('start_time', [
  5542. 'timeFormat' => 12,
  5543. 'interval' => 5,
  5544. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5545. ]);
  5546. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5547. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5548. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5549. $this->assertNotContains('year', $result);
  5550. $this->assertNotContains('month', $result);
  5551. $this->assertNotContains('day', $result);
  5552. $result = $this->Form->time('start_time', [
  5553. 'timeFormat' => 12,
  5554. 'interval' => 5,
  5555. 'value' => '2014-03-08 16:30:00'
  5556. ]);
  5557. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5558. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5559. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5560. $this->assertNotContains('year', $result);
  5561. $this->assertNotContains('month', $result);
  5562. $this->assertNotContains('day', $result);
  5563. }
  5564. /**
  5565. * testTimeFormat24NoMeridian method
  5566. *
  5567. * Ensure that timeFormat=24 has no merdian.
  5568. *
  5569. * @return void.
  5570. */
  5571. public function testTimeFormat24NoMeridian()
  5572. {
  5573. $result = $this->Form->time('start_time', [
  5574. 'timeFormat' => 24,
  5575. 'interval' => 5,
  5576. 'value' => '2014-03-08 16:30:00'
  5577. ]);
  5578. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5579. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5580. $this->assertNotContains('meridian', $result);
  5581. $this->assertNotContains('pm', $result);
  5582. $this->assertNotContains('year', $result);
  5583. $this->assertNotContains('month', $result);
  5584. $this->assertNotContains('day', $result);
  5585. }
  5586. /**
  5587. * testDate method
  5588. *
  5589. * Test the date type.
  5590. *
  5591. * @return void
  5592. */
  5593. public function testDate()
  5594. {
  5595. $result = $this->Form->date('start_day', [
  5596. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5597. ]);
  5598. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5599. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5600. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5601. $this->assertNotContains('hour', $result);
  5602. $this->assertNotContains('minute', $result);
  5603. $this->assertNotContains('second', $result);
  5604. $this->assertNotContains('meridian', $result);
  5605. }
  5606. /**
  5607. * testDateTime method
  5608. *
  5609. * Test generation of date/time select elements.
  5610. *
  5611. * @return void
  5612. */
  5613. public function testDateTime()
  5614. {
  5615. extract($this->dateRegex);
  5616. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5617. $now = strtotime('now');
  5618. $expected = [
  5619. ['select' => ['name' => 'Contact[date][year]']],
  5620. ['option' => ['value' => '']],
  5621. '/option',
  5622. $yearsRegex,
  5623. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5624. date('Y', $now),
  5625. '/option',
  5626. '*/select',
  5627. ['select' => ['name' => 'Contact[date][month]']],
  5628. ['option' => ['value' => '']],
  5629. '/option',
  5630. $monthsRegex,
  5631. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5632. date('F', $now),
  5633. '/option',
  5634. '*/select',
  5635. ['select' => ['name' => 'Contact[date][day]']],
  5636. ['option' => ['value' => '']],
  5637. '/option',
  5638. $daysRegex,
  5639. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5640. date('j', $now),
  5641. '/option',
  5642. '*/select',
  5643. ['select' => ['name' => 'Contact[date][hour]']],
  5644. ['option' => ['value' => '']],
  5645. '/option',
  5646. $hoursRegex,
  5647. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5648. date('G', $now),
  5649. '/option',
  5650. '*/select',
  5651. ['select' => ['name' => 'Contact[date][minute]']],
  5652. ['option' => ['value' => '']],
  5653. '/option',
  5654. $minutesRegex,
  5655. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5656. date('i', $now),
  5657. '/option',
  5658. '*/select',
  5659. ];
  5660. $this->assertHtml($expected, $result);
  5661. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5662. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5663. $now = strtotime('now');
  5664. $expected = [
  5665. ['select' => ['name' => 'Contact[date][year]']],
  5666. $yearsRegex,
  5667. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5668. date('Y', $now),
  5669. '/option',
  5670. '*/select',
  5671. ['select' => ['name' => 'Contact[date][month]']],
  5672. $monthsRegex,
  5673. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5674. date('F', $now),
  5675. '/option',
  5676. '*/select',
  5677. ['select' => ['name' => 'Contact[date][day]']],
  5678. $daysRegex,
  5679. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5680. date('j', $now),
  5681. '/option',
  5682. '*/select',
  5683. ['select' => ['name' => 'Contact[date][hour]']],
  5684. $hoursRegex,
  5685. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5686. date('G', $now),
  5687. '/option',
  5688. '*/select',
  5689. ['select' => ['name' => 'Contact[date][minute]']],
  5690. $minutesRegex,
  5691. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5692. date('i', $now),
  5693. '/option',
  5694. '*/select',
  5695. ];
  5696. $this->assertHtml($expected, $result);
  5697. }
  5698. /**
  5699. * testDateTimeSecured method
  5700. *
  5701. * Test that datetime fields are added to protected fields list.
  5702. *
  5703. * @return void
  5704. */
  5705. public function testDateTimeSecured()
  5706. {
  5707. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5708. $this->Form->dateTime('Contact.date');
  5709. $expected = [
  5710. 'Contact.date.year',
  5711. 'Contact.date.month',
  5712. 'Contact.date.day',
  5713. 'Contact.date.hour',
  5714. 'Contact.date.minute',
  5715. ];
  5716. $this->assertEquals($expected, $this->Form->fields);
  5717. $this->Form->fields = [];
  5718. $this->Form->date('Contact.published');
  5719. $expected = [
  5720. 'Contact.published.year',
  5721. 'Contact.published.month',
  5722. 'Contact.published.day',
  5723. ];
  5724. $this->assertEquals($expected, $this->Form->fields);
  5725. }
  5726. /**
  5727. * testDateTimeSecuredDisabled method
  5728. *
  5729. * Test that datetime fields are added to protected fields list.
  5730. *
  5731. * @return void
  5732. */
  5733. public function testDateTimeSecuredDisabled()
  5734. {
  5735. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5736. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5737. $expected = [];
  5738. $this->assertEquals($expected, $this->Form->fields);
  5739. $this->Form->fields = [];
  5740. $this->Form->date('Contact.published', ['secure' => false]);
  5741. $expected = [];
  5742. $this->assertEquals($expected, $this->Form->fields);
  5743. }
  5744. /**
  5745. * testDatetimeEmpty method
  5746. *
  5747. * Test empty defaulting to true for datetime.
  5748. *
  5749. * @return void
  5750. */
  5751. public function testDatetimeEmpty()
  5752. {
  5753. extract($this->dateRegex);
  5754. $result = $this->Form->dateTime('Contact.date', [
  5755. 'timeFormat' => 12,
  5756. 'empty' => true,
  5757. 'default' => true
  5758. ]);
  5759. $expected = [
  5760. ['select' => ['name' => 'Contact[date][year]']],
  5761. $yearsRegex,
  5762. ['option' => ['value' => '']],
  5763. '/option',
  5764. '*/select',
  5765. ['select' => ['name' => 'Contact[date][month]']],
  5766. $monthsRegex,
  5767. ['option' => ['value' => '']],
  5768. '/option',
  5769. '*/select',
  5770. ['select' => ['name' => 'Contact[date][day]']],
  5771. $daysRegex,
  5772. ['option' => ['value' => '']],
  5773. '/option',
  5774. '*/select',
  5775. ['select' => ['name' => 'Contact[date][hour]']],
  5776. $hoursRegex,
  5777. ['option' => ['value' => '']],
  5778. '/option',
  5779. '*/select',
  5780. ['select' => ['name' => 'Contact[date][minute]']],
  5781. $minutesRegex,
  5782. ['option' => ['value' => '']],
  5783. '/option',
  5784. '*/select',
  5785. ['select' => ['name' => 'Contact[date][meridian]']],
  5786. $meridianRegex,
  5787. ['option' => ['value' => '']],
  5788. '/option',
  5789. '*/select'
  5790. ];
  5791. $this->assertHtml($expected, $result);
  5792. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5793. }
  5794. /**
  5795. * testDatetimeMinuteInterval method
  5796. *
  5797. * Test datetime with interval option.
  5798. *
  5799. * @return void
  5800. */
  5801. public function testDatetimeMinuteInterval()
  5802. {
  5803. extract($this->dateRegex);
  5804. $result = $this->Form->dateTime('Contact.date', [
  5805. 'interval' => 5,
  5806. 'value' => ''
  5807. ]);
  5808. $expected = [
  5809. ['select' => ['name' => 'Contact[date][year]']],
  5810. $yearsRegex,
  5811. ['option' => ['selected' => 'selected', 'value' => '']],
  5812. '/option',
  5813. '*/select',
  5814. ['select' => ['name' => 'Contact[date][month]']],
  5815. $monthsRegex,
  5816. ['option' => ['selected' => 'selected', 'value' => '']],
  5817. '/option',
  5818. '*/select',
  5819. ['select' => ['name' => 'Contact[date][day]']],
  5820. $daysRegex,
  5821. ['option' => ['selected' => 'selected', 'value' => '']],
  5822. '/option',
  5823. '*/select',
  5824. ['select' => ['name' => 'Contact[date][hour]']],
  5825. $hoursRegex,
  5826. ['option' => ['selected' => 'selected', 'value' => '']],
  5827. '/option',
  5828. '*/select',
  5829. ['select' => ['name' => 'Contact[date][minute]']],
  5830. $minutesRegex,
  5831. ['option' => ['selected' => 'selected', 'value' => '']],
  5832. '/option',
  5833. ['option' => ['value' => '00']],
  5834. '00',
  5835. '/option',
  5836. ['option' => ['value' => '05']],
  5837. '05',
  5838. '/option',
  5839. ['option' => ['value' => '10']],
  5840. '10',
  5841. '/option',
  5842. '*/select',
  5843. ];
  5844. $this->assertHtml($expected, $result);
  5845. }
  5846. /**
  5847. * testDateTimeRounding method
  5848. *
  5849. * Test dateTime with rounding.
  5850. *
  5851. * @return void
  5852. */
  5853. public function testDateTimeRounding()
  5854. {
  5855. $this->Form->request->data['Contact'] = [
  5856. 'date' => [
  5857. 'day' => '13',
  5858. 'month' => '12',
  5859. 'year' => '2010',
  5860. 'hour' => '04',
  5861. 'minute' => '19',
  5862. 'meridian' => 'AM'
  5863. ]
  5864. ];
  5865. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5866. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5867. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5868. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5869. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5870. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5871. }
  5872. /**
  5873. * testDatetimeWithDefault method
  5874. *
  5875. * Test that datetime() and default values work.
  5876. *
  5877. * @return void
  5878. */
  5879. public function testDatetimeWithDefault()
  5880. {
  5881. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5882. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5883. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5884. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5885. $result = $this->Form->dateTime('Contact.updated', [
  5886. 'default' => '2009-06-01 11:15:30'
  5887. ]);
  5888. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5889. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5890. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5891. }
  5892. /**
  5893. * testDateTimeAllZeros method
  5894. *
  5895. * Test datetime() with all zeros.
  5896. *
  5897. * @return void
  5898. */
  5899. public function testDateTimeAllZeros()
  5900. {
  5901. $result = $this->Form->dateTime('Contact.date', [
  5902. 'timeFormat' => false,
  5903. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5904. 'value' => '0000-00-00'
  5905. ]);
  5906. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5907. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5908. }
  5909. /**
  5910. * testDateTimeEmptyAsArray method
  5911. *
  5912. * @return void
  5913. */
  5914. public function testDateTimeEmptyAsArray()
  5915. {
  5916. $result = $this->Form->dateTime('Contact.date', [
  5917. 'empty' => [
  5918. 'day' => 'DAY',
  5919. 'month' => 'MONTH',
  5920. 'year' => 'YEAR',
  5921. 'hour' => 'HOUR',
  5922. 'minute' => 'MINUTE',
  5923. 'meridian' => false
  5924. ],
  5925. 'default' => true
  5926. ]);
  5927. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5928. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5929. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5930. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5931. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5932. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5933. $result = $this->Form->dateTime('Contact.date', [
  5934. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5935. 'default' => true
  5936. ]);
  5937. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5938. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5939. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5940. }
  5941. /**
  5942. * testFormDateTimeMulti method
  5943. *
  5944. * Test multiple datetime element generation.
  5945. *
  5946. * @return void
  5947. */
  5948. public function testFormDateTimeMulti()
  5949. {
  5950. extract($this->dateRegex);
  5951. $result = $this->Form->dateTime('Contact.1.updated');
  5952. $this->assertContains('Contact[1][updated][month]', $result);
  5953. $this->assertContains('Contact[1][updated][day]', $result);
  5954. $this->assertContains('Contact[1][updated][year]', $result);
  5955. $this->assertContains('Contact[1][updated][hour]', $result);
  5956. $this->assertContains('Contact[1][updated][minute]', $result);
  5957. }
  5958. /**
  5959. * testDateTimeLabelIdMatchesFirstControl method
  5960. *
  5961. * When changing the date format, the label should always focus the first select box when
  5962. * clicked.
  5963. *
  5964. * @return void
  5965. */
  5966. public function testDateTimeLabelIdMatchesFirstControl()
  5967. {
  5968. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5969. $this->assertContains('<label>Date</label>', $result);
  5970. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5971. $this->assertContains('<label>Date</label>', $result);
  5972. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5973. $this->assertContains('<label>Date</label>', $result);
  5974. }
  5975. /**
  5976. * testDateTimeSecondOptions method
  5977. *
  5978. * Test datetime second=true.
  5979. *
  5980. * @return void
  5981. */
  5982. public function testDateTimeSecondOptions()
  5983. {
  5984. $result = $this->Form->dateTime('updated', ['second' => true]);
  5985. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5986. $result = $this->Form->dateTime('updated', ['second' => []]);
  5987. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5988. $result = $this->Form->dateTime('updated', ['second' => null]);
  5989. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5990. $result = $this->Form->dateTime('updated', ['second' => false]);
  5991. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5992. }
  5993. /**
  5994. * testMonth method
  5995. *
  5996. * Test generation of a month input.
  5997. *
  5998. * @return void
  5999. */
  6000. public function testMonth()
  6001. {
  6002. $result = $this->Form->month('Model.field', ['value' => '']);
  6003. $expected = [
  6004. ['select' => ['name' => 'Model[field][month]']],
  6005. ['option' => ['value' => '', 'selected' => 'selected']],
  6006. '/option',
  6007. ['option' => ['value' => '01']],
  6008. date('F', strtotime('2008-01-01 00:00:00')),
  6009. '/option',
  6010. ['option' => ['value' => '02']],
  6011. date('F', strtotime('2008-02-01 00:00:00')),
  6012. '/option',
  6013. '*/select',
  6014. ];
  6015. $this->assertHtml($expected, $result);
  6016. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6017. $expected = [
  6018. ['select' => ['name' => 'Model[field][month]']],
  6019. ['option' => ['selected' => 'selected', 'value' => '']],
  6020. '/option',
  6021. ['option' => ['value' => '01']],
  6022. date('F', strtotime('2008-01-01 00:00:00')),
  6023. '/option',
  6024. ['option' => ['value' => '02']],
  6025. date('F', strtotime('2008-02-01 00:00:00')),
  6026. '/option',
  6027. '*/select',
  6028. ];
  6029. $this->assertHtml($expected, $result);
  6030. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6031. $expected = [
  6032. ['select' => ['name' => 'Model[field][month]']],
  6033. ['option' => ['selected' => 'selected', 'value' => '']],
  6034. '/option',
  6035. ['option' => ['value' => '01']],
  6036. '1',
  6037. '/option',
  6038. ['option' => ['value' => '02']],
  6039. '2',
  6040. '/option',
  6041. '*/select',
  6042. ];
  6043. $this->assertHtml($expected, $result);
  6044. $monthNames = [
  6045. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6046. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6047. ];
  6048. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6049. $expected = [
  6050. ['select' => ['name' => 'Model[field][month]']],
  6051. ['option' => ['value' => '']],
  6052. '/option',
  6053. ['option' => ['value' => '01', 'selected' => 'selected']],
  6054. 'Jan',
  6055. '/option',
  6056. ['option' => ['value' => '02']],
  6057. 'Feb',
  6058. '/option',
  6059. '*/select',
  6060. ];
  6061. $this->assertHtml($expected, $result);
  6062. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6063. $result = $this->Form->month('Project.release');
  6064. $expected = [
  6065. ['select' => ['name' => 'Project[release][month]']],
  6066. ['option' => ['value' => '']],
  6067. '/option',
  6068. ['option' => ['value' => '01']],
  6069. 'January',
  6070. '/option',
  6071. ['option' => ['value' => '02', 'selected' => 'selected']],
  6072. 'February',
  6073. '/option',
  6074. '*/select',
  6075. ];
  6076. $this->assertHtml($expected, $result);
  6077. $result = $this->Form->month('Contact.published', [
  6078. 'empty' => 'Published on',
  6079. ]);
  6080. $this->assertContains('Published on', $result);
  6081. }
  6082. /**
  6083. * testDay method
  6084. *
  6085. * Test generation of a day input.
  6086. *
  6087. * @return void
  6088. */
  6089. public function testDay()
  6090. {
  6091. extract($this->dateRegex);
  6092. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6093. $expected = [
  6094. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6095. ['option' => ['selected' => 'selected', 'value' => '']],
  6096. '/option',
  6097. ['option' => ['value' => '01']],
  6098. '1',
  6099. '/option',
  6100. ['option' => ['value' => '02']],
  6101. '2',
  6102. '/option',
  6103. $daysRegex,
  6104. '/select',
  6105. ];
  6106. $this->assertHtml($expected, $result);
  6107. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6108. $result = $this->Form->day('Model.field');
  6109. $expected = [
  6110. ['select' => ['name' => 'Model[field][day]']],
  6111. ['option' => ['value' => '']],
  6112. '/option',
  6113. ['option' => ['value' => '01']],
  6114. '1',
  6115. '/option',
  6116. ['option' => ['value' => '02']],
  6117. '2',
  6118. '/option',
  6119. $daysRegex,
  6120. ['option' => ['value' => '10', 'selected' => 'selected']],
  6121. '10',
  6122. '/option',
  6123. $daysRegex,
  6124. '/select',
  6125. ];
  6126. $this->assertHtml($expected, $result);
  6127. $this->Form->request->data['Model']['field'] = '';
  6128. $result = $this->Form->day('Model.field', ['value' => '10']);
  6129. $expected = [
  6130. ['select' => ['name' => 'Model[field][day]']],
  6131. ['option' => ['value' => '']],
  6132. '/option',
  6133. ['option' => ['value' => '01']],
  6134. '1',
  6135. '/option',
  6136. ['option' => ['value' => '02']],
  6137. '2',
  6138. '/option',
  6139. $daysRegex,
  6140. ['option' => ['value' => '10', 'selected' => 'selected']],
  6141. '10',
  6142. '/option',
  6143. $daysRegex,
  6144. '/select',
  6145. ];
  6146. $this->assertHtml($expected, $result);
  6147. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6148. $result = $this->Form->day('Project.release');
  6149. $expected = [
  6150. ['select' => ['name' => 'Project[release][day]']],
  6151. ['option' => ['value' => '']],
  6152. '/option',
  6153. ['option' => ['value' => '01']],
  6154. '1',
  6155. '/option',
  6156. ['option' => ['value' => '02']],
  6157. '2',
  6158. '/option',
  6159. $daysRegex,
  6160. ['option' => ['value' => '10', 'selected' => 'selected']],
  6161. '10',
  6162. '/option',
  6163. $daysRegex,
  6164. '/select',
  6165. ];
  6166. $this->assertHtml($expected, $result);
  6167. $result = $this->Form->day('Contact.published', [
  6168. 'empty' => 'Published on',
  6169. ]);
  6170. $this->assertContains('Published on', $result);
  6171. }
  6172. /**
  6173. * testMinute method
  6174. *
  6175. * Test generation of a minute input.
  6176. *
  6177. * @return void
  6178. */
  6179. public function testMinute()
  6180. {
  6181. extract($this->dateRegex);
  6182. $result = $this->Form->minute('Model.field', ['value' => '']);
  6183. $expected = [
  6184. ['select' => ['name' => 'Model[field][minute]']],
  6185. ['option' => ['selected' => 'selected', 'value' => '']],
  6186. '/option',
  6187. ['option' => ['value' => '00']],
  6188. '00',
  6189. '/option',
  6190. ['option' => ['value' => '01']],
  6191. '01',
  6192. '/option',
  6193. ['option' => ['value' => '02']],
  6194. '02',
  6195. '/option',
  6196. $minutesRegex,
  6197. '/select',
  6198. ];
  6199. $this->assertHtml($expected, $result);
  6200. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6201. $result = $this->Form->minute('Model.field');
  6202. $expected = [
  6203. ['select' => ['name' => 'Model[field][minute]']],
  6204. ['option' => ['value' => '']],
  6205. '/option',
  6206. ['option' => ['value' => '00']],
  6207. '00',
  6208. '/option',
  6209. ['option' => ['value' => '01']],
  6210. '01',
  6211. '/option',
  6212. ['option' => ['value' => '02']],
  6213. '02',
  6214. '/option',
  6215. $minutesRegex,
  6216. ['option' => ['value' => '12', 'selected' => 'selected']],
  6217. '12',
  6218. '/option',
  6219. $minutesRegex,
  6220. '/select',
  6221. ];
  6222. $this->assertHtml($expected, $result);
  6223. $this->Form->request->data['Model']['field'] = '';
  6224. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6225. $expected = [
  6226. ['select' => ['name' => 'Model[field][minute]']],
  6227. ['option' => ['selected' => 'selected', 'value' => '']],
  6228. '/option',
  6229. ['option' => ['value' => '00']],
  6230. '00',
  6231. '/option',
  6232. ['option' => ['value' => '05']],
  6233. '05',
  6234. '/option',
  6235. ['option' => ['value' => '10']],
  6236. '10',
  6237. '/option',
  6238. $minutesRegex,
  6239. '/select',
  6240. ];
  6241. $this->assertHtml($expected, $result);
  6242. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6243. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6244. $expected = [
  6245. ['select' => ['name' => 'Model[field][minute]']],
  6246. ['option' => ['value' => '']],
  6247. '/option',
  6248. ['option' => ['value' => '00']],
  6249. '00',
  6250. '/option',
  6251. ['option' => ['value' => '05']],
  6252. '05',
  6253. '/option',
  6254. ['option' => ['value' => '10', 'selected' => 'selected']],
  6255. '10',
  6256. '/option',
  6257. $minutesRegex,
  6258. '/select',
  6259. ];
  6260. $this->assertHtml($expected, $result);
  6261. }
  6262. /**
  6263. * testMeridian method
  6264. *
  6265. * Test generating an input for the meridian.
  6266. *
  6267. * @return void
  6268. */
  6269. public function testMeridian()
  6270. {
  6271. extract($this->dateRegex);
  6272. $now = new \DateTime();
  6273. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6274. $expected = [
  6275. ['select' => ['name' => 'Model[field][meridian]']],
  6276. ['option' => ['value' => '']],
  6277. '/option',
  6278. $meridianRegex,
  6279. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6280. $now->format('a'),
  6281. '/option',
  6282. '*/select'
  6283. ];
  6284. $this->assertHtml($expected, $result);
  6285. }
  6286. /**
  6287. * testHour method
  6288. *
  6289. * Test generation of an hour input.
  6290. *
  6291. * @return void
  6292. */
  6293. public function testHour()
  6294. {
  6295. extract($this->dateRegex);
  6296. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6297. $expected = [
  6298. ['select' => ['name' => 'Model[field][hour]']],
  6299. ['option' => ['selected' => 'selected', 'value' => '']],
  6300. '/option',
  6301. ['option' => ['value' => '01']],
  6302. '1',
  6303. '/option',
  6304. ['option' => ['value' => '02']],
  6305. '2',
  6306. '/option',
  6307. $hoursRegex,
  6308. '/select',
  6309. ];
  6310. $this->assertHtml($expected, $result);
  6311. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6312. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6313. $expected = [
  6314. ['select' => ['name' => 'Model[field][hour]']],
  6315. ['option' => ['value' => '']],
  6316. '/option',
  6317. ['option' => ['value' => '01']],
  6318. '1',
  6319. '/option',
  6320. ['option' => ['value' => '02']],
  6321. '2',
  6322. '/option',
  6323. $hoursRegex,
  6324. ['option' => ['value' => '12', 'selected' => 'selected']],
  6325. '12',
  6326. '/option',
  6327. '/select',
  6328. ];
  6329. $this->assertHtml($expected, $result);
  6330. $this->Form->request->data['Model']['field'] = '';
  6331. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6332. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6333. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6334. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6335. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6336. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6337. $expected = [
  6338. ['select' => ['name' => 'Model[field][hour]']],
  6339. ['option' => ['value' => '']],
  6340. '/option',
  6341. ['option' => ['value' => '00', 'selected' => 'selected']],
  6342. '0',
  6343. '/option',
  6344. ['option' => ['value' => '01']],
  6345. '1',
  6346. '/option',
  6347. ['option' => ['value' => '02']],
  6348. '2',
  6349. '/option',
  6350. $hoursRegex,
  6351. '/select',
  6352. ];
  6353. $this->assertHtml($expected, $result);
  6354. unset($this->Form->request->data['Model']['field']);
  6355. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6356. $thisHour = date('H');
  6357. $optValue = date('G');
  6358. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6359. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6360. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6361. $expected = [
  6362. ['select' => ['name' => 'Model[field][hour]']],
  6363. ['option' => ['value' => '']],
  6364. '/option',
  6365. ['option' => ['value' => '00']],
  6366. '0',
  6367. '/option',
  6368. ['option' => ['value' => '01', 'selected' => 'selected']],
  6369. '1',
  6370. '/option',
  6371. ['option' => ['value' => '02']],
  6372. '2',
  6373. '/option',
  6374. $hoursRegex,
  6375. '/select',
  6376. ];
  6377. $this->assertHtml($expected, $result);
  6378. }
  6379. /**
  6380. * testYear method
  6381. *
  6382. * Test generation of a year input.
  6383. *
  6384. * @return void
  6385. */
  6386. public function testYear()
  6387. {
  6388. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6389. $expected = [
  6390. ['select' => ['name' => 'Model[field][year]']],
  6391. ['option' => ['selected' => 'selected', 'value' => '']],
  6392. '/option',
  6393. ['option' => ['value' => '2007']],
  6394. '2007',
  6395. '/option',
  6396. ['option' => ['value' => '2006']],
  6397. '2006',
  6398. '/option',
  6399. '/select',
  6400. ];
  6401. $this->assertHtml($expected, $result);
  6402. $result = $this->Form->year('Model.field', [
  6403. 'value' => '',
  6404. 'minYear' => 2006,
  6405. 'maxYear' => 2007,
  6406. 'orderYear' => 'asc'
  6407. ]);
  6408. $expected = [
  6409. ['select' => ['name' => 'Model[field][year]']],
  6410. ['option' => ['selected' => 'selected', 'value' => '']],
  6411. '/option',
  6412. ['option' => ['value' => '2006']],
  6413. '2006',
  6414. '/option',
  6415. ['option' => ['value' => '2007']],
  6416. '2007',
  6417. '/option',
  6418. '/select',
  6419. ];
  6420. $this->assertHtml($expected, $result);
  6421. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6422. $result = $this->Form->year('Contact.published', [
  6423. 'empty' => false,
  6424. 'minYear' => 2006,
  6425. 'maxYear' => 2007,
  6426. ]);
  6427. $expected = [
  6428. ['select' => ['name' => 'Contact[published][year]']],
  6429. ['option' => ['value' => '2007']],
  6430. '2007',
  6431. '/option',
  6432. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6433. '2006',
  6434. '/option',
  6435. '/select',
  6436. ];
  6437. $this->assertHtml($expected, $result);
  6438. $result = $this->Form->year('Contact.published', [
  6439. 'empty' => 'Published on',
  6440. ]);
  6441. $this->assertContains('Published on', $result);
  6442. }
  6443. /**
  6444. * testControlDatetimePreEpoch method
  6445. *
  6446. * Test minYear being prior to the unix epoch.
  6447. *
  6448. * @return void
  6449. */
  6450. public function testControlDatetimePreEpoch()
  6451. {
  6452. $start = date('Y') - 80;
  6453. $end = date('Y') - 18;
  6454. $result = $this->Form->control('birth_year', [
  6455. 'type' => 'date',
  6456. 'label' => 'Birth Year',
  6457. 'minYear' => $start,
  6458. 'maxYear' => $end,
  6459. 'month' => false,
  6460. 'day' => false,
  6461. ]);
  6462. $this->assertContains('value="' . $start . '">' . $start, $result);
  6463. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6464. $this->assertNotContains('value="00">00', $result);
  6465. }
  6466. /**
  6467. * testYearAutoExpandRange method
  6468. *
  6469. * @return void
  6470. */
  6471. public function testYearAutoExpandRange()
  6472. {
  6473. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6474. $result = $this->Form->year('User.birthday');
  6475. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6476. $result = $matches[1];
  6477. $expected = range(date('Y') + 5, 1930);
  6478. $this->assertEquals($expected, $result);
  6479. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6480. $result = $this->Form->year('Project.release');
  6481. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6482. $result = $matches[1];
  6483. $expected = range(2050, date('Y') - 5);
  6484. $this->assertEquals($expected, $result);
  6485. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6486. $result = $this->Form->year('Project.release', [
  6487. 'minYear' => 1890,
  6488. 'maxYear' => 1900
  6489. ]);
  6490. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6491. $result = $matches[1];
  6492. $expected = range(1900, 1881);
  6493. $this->assertEquals($expected, $result);
  6494. }
  6495. /**
  6496. * testControlDate method
  6497. *
  6498. * Test that control() accepts the type of date and passes options in.
  6499. *
  6500. * @return void
  6501. */
  6502. public function testControlDate()
  6503. {
  6504. $this->Form->request->data = [
  6505. 'month_year' => ['month' => date('m')],
  6506. ];
  6507. $this->Form->create($this->article);
  6508. $result = $this->Form->control('month_year', [
  6509. 'label' => false,
  6510. 'type' => 'date',
  6511. 'minYear' => 2006,
  6512. 'maxYear' => 2008
  6513. ]);
  6514. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6515. $this->assertNotContains('value="2008" selected="selected"', $result);
  6516. }
  6517. /**
  6518. * testControlLabelFalse method
  6519. *
  6520. * Test the label option being set to false.
  6521. *
  6522. * @return void
  6523. */
  6524. public function testControlLabelFalse()
  6525. {
  6526. $this->Form->create($this->article);
  6527. $result = $this->Form->control('title', ['label' => false]);
  6528. $expected = [
  6529. 'div' => ['class' => 'input text required'],
  6530. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6531. '/div'
  6532. ];
  6533. $this->assertHtml($expected, $result);
  6534. }
  6535. /**
  6536. * testControlDateMaxYear method
  6537. *
  6538. * Let's say we want to only allow users born from 2006 to 2008 to register
  6539. * This being the first signup page, we still don't have any data.
  6540. *
  6541. * @return void
  6542. */
  6543. public function testControlDateMaxYear()
  6544. {
  6545. $this->Form->request->data = [];
  6546. $this->Form->create($this->article);
  6547. $result = $this->Form->control('birthday', [
  6548. 'label' => false,
  6549. 'type' => 'date',
  6550. 'minYear' => 2006,
  6551. 'maxYear' => 2008,
  6552. 'default' => true
  6553. ]);
  6554. $this->assertContains('value="2008" selected="selected"', $result);
  6555. $this->assertContains('value="2006"', $result);
  6556. $this->assertNotContains('value="2005"', $result);
  6557. $this->assertNotContains('value="2009"', $result);
  6558. }
  6559. /**
  6560. * testTextArea method
  6561. *
  6562. * Test generation of a textarea input.
  6563. *
  6564. * @return void
  6565. */
  6566. public function testTextArea()
  6567. {
  6568. $this->Form->request->data = ['field' => 'some test data'];
  6569. $result = $this->Form->textarea('field');
  6570. $expected = [
  6571. 'textarea' => ['name' => 'field', 'rows' => 5],
  6572. 'some test data',
  6573. '/textarea',
  6574. ];
  6575. $this->assertHtml($expected, $result);
  6576. $result = $this->Form->textarea('user.bio');
  6577. $expected = [
  6578. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6579. '/textarea',
  6580. ];
  6581. $this->assertHtml($expected, $result);
  6582. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6583. $result = $this->Form->textarea('field');
  6584. $expected = [
  6585. 'textarea' => ['name' => 'field', 'rows' => 5],
  6586. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6587. '/textarea',
  6588. ];
  6589. $this->assertHtml($expected, $result);
  6590. $this->Form->request->data = [
  6591. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6592. ];
  6593. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6594. $expected = [
  6595. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6596. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6597. '/textarea',
  6598. ];
  6599. $this->assertHtml($expected, $result);
  6600. $result = $this->Form->textarea('0.OtherModel.field');
  6601. $expected = [
  6602. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6603. '/textarea'
  6604. ];
  6605. $this->assertHtml($expected, $result);
  6606. }
  6607. /**
  6608. * testTextAreaWithStupidCharacters method
  6609. *
  6610. * Test text area with non-ascii characters.
  6611. *
  6612. * @return void
  6613. */
  6614. public function testTextAreaWithStupidCharacters()
  6615. {
  6616. $result = $this->Form->textarea('Post.content', [
  6617. 'value' => 'GREAT®',
  6618. 'rows' => '15',
  6619. 'cols' => '75'
  6620. ]);
  6621. $expected = [
  6622. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6623. 'GREAT®',
  6624. '/textarea',
  6625. ];
  6626. $this->assertHtml($expected, $result);
  6627. }
  6628. /**
  6629. * testTextAreaMaxLength method
  6630. *
  6631. * Test textareas maxlength read from schema.
  6632. *
  6633. * @return void
  6634. */
  6635. public function testTextAreaMaxLength()
  6636. {
  6637. $this->Form->create([
  6638. 'schema' => [
  6639. 'stuff' => ['type' => 'string', 'length' => 10],
  6640. ]
  6641. ]);
  6642. $result = $this->Form->control('other', ['type' => 'textarea']);
  6643. $expected = [
  6644. 'div' => ['class' => 'input textarea'],
  6645. 'label' => ['for' => 'other'],
  6646. 'Other',
  6647. '/label',
  6648. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6649. '/textarea',
  6650. '/div'
  6651. ];
  6652. $this->assertHtml($expected, $result);
  6653. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6654. $expected = [
  6655. 'div' => ['class' => 'input textarea'],
  6656. 'label' => ['for' => 'stuff'],
  6657. 'Stuff',
  6658. '/label',
  6659. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6660. '/textarea',
  6661. '/div'
  6662. ];
  6663. $this->assertHtml($expected, $result);
  6664. }
  6665. /**
  6666. * testHiddenField method
  6667. *
  6668. * Test generation of a hidden input.
  6669. *
  6670. * @return void
  6671. */
  6672. public function testHiddenField()
  6673. {
  6674. $this->article['errors'] = [
  6675. 'field' => true
  6676. ];
  6677. $this->Form->request->data['field'] = 'test';
  6678. $this->Form->create($this->article);
  6679. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6680. $expected = [
  6681. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6682. $this->assertHtml($expected, $result);
  6683. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6684. $expected = [
  6685. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6686. ];
  6687. $this->assertHtml($expected, $result);
  6688. }
  6689. /**
  6690. * testFileUploadField method
  6691. *
  6692. * Test generation of a file upload input.
  6693. *
  6694. * @return void
  6695. */
  6696. public function testFileUploadField()
  6697. {
  6698. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6699. $result = $this->Form->file('Model.upload');
  6700. $this->assertHtml($expected, $result);
  6701. $this->Form->request->data['Model']['upload'] = [
  6702. 'name' => '', 'type' => '', 'tmp_name' => '',
  6703. 'error' => 4, 'size' => 0
  6704. ];
  6705. $result = $this->Form->file('Model.upload');
  6706. $this->assertHtml($expected, $result);
  6707. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6708. $result = $this->Form->file('Model.upload');
  6709. $this->assertHtml($expected, $result);
  6710. }
  6711. /**
  6712. * testFileUploadOnOtherModel method
  6713. *
  6714. * Test File upload input on a model not used in create().
  6715. *
  6716. * @return void
  6717. */
  6718. public function testFileUploadOnOtherModel()
  6719. {
  6720. $this->Form->create($this->article, ['type' => 'file']);
  6721. $result = $this->Form->file('ValidateProfile.city');
  6722. $expected = [
  6723. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6724. ];
  6725. $this->assertHtml($expected, $result);
  6726. }
  6727. /**
  6728. * testButton method
  6729. *
  6730. * Test generation of a form button.
  6731. *
  6732. * @return void
  6733. */
  6734. public function testButton()
  6735. {
  6736. $result = $this->Form->button('Hi');
  6737. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6738. $this->assertHtml($expected, $result);
  6739. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6740. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6741. $this->assertHtml($expected, $result);
  6742. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6743. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6744. $this->assertHtml($expected, $result);
  6745. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6746. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6747. $this->assertHtml($expected, $result);
  6748. $result = $this->Form->button('No type', ['type' => false]);
  6749. $expected = ['button' => [], 'No type', '/button'];
  6750. $this->assertHtml($expected, $result);
  6751. $result = $this->Form->button('Upload Text', [
  6752. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6753. 'escape' => false
  6754. ]);
  6755. $this->assertNotRegExp('/\&039/', $result);
  6756. }
  6757. /**
  6758. * testButtonUnlockedByDefault method
  6759. *
  6760. * Test that button() makes unlocked fields by default.
  6761. *
  6762. * @return void
  6763. */
  6764. public function testButtonUnlockedByDefault()
  6765. {
  6766. $this->Form->request->params['_csrfToken'] = 'secured';
  6767. $this->Form->button('Save', ['name' => 'save']);
  6768. $this->Form->button('Clear');
  6769. $result = $this->Form->unlockField();
  6770. $this->assertEquals(['save'], $result);
  6771. }
  6772. /**
  6773. * testPostButton method
  6774. *
  6775. * @return void
  6776. */
  6777. public function testPostButton()
  6778. {
  6779. $result = $this->Form->postButton('Hi', '/controller/action');
  6780. $expected = [
  6781. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6782. 'div' => ['style' => 'display:none;'],
  6783. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6784. '/div',
  6785. 'button' => ['type' => 'submit'],
  6786. 'Hi',
  6787. '/button',
  6788. '/form'
  6789. ];
  6790. $this->assertHtml($expected, $result);
  6791. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6792. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6793. }
  6794. /**
  6795. * testPostButtonMethodType method
  6796. *
  6797. * @return void
  6798. */
  6799. public function testPostButtonMethodType()
  6800. {
  6801. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6802. $expected = [
  6803. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6804. 'div' => ['style' => 'display:none;'],
  6805. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6806. '/div',
  6807. 'button' => ['type' => 'submit'],
  6808. 'Hi',
  6809. '/button',
  6810. '/form'
  6811. ];
  6812. $this->assertHtml($expected, $result);
  6813. }
  6814. /**
  6815. * testPostButtonFormOptions method
  6816. *
  6817. * @return void
  6818. */
  6819. public function testPostButtonFormOptions()
  6820. {
  6821. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6822. $expected = [
  6823. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6824. 'div' => ['style' => 'display:none;'],
  6825. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6826. '/div',
  6827. 'button' => ['type' => 'submit'],
  6828. 'Hi',
  6829. '/button',
  6830. '/form'
  6831. ];
  6832. $this->assertHtml($expected, $result);
  6833. }
  6834. /**
  6835. * testPostButtonNestedData method
  6836. *
  6837. * Test using postButton with N dimensional data.
  6838. *
  6839. * @return void
  6840. */
  6841. public function testPostButtonNestedData()
  6842. {
  6843. $data = [
  6844. 'one' => [
  6845. 'two' => [
  6846. 3, 4, 5
  6847. ]
  6848. ]
  6849. ];
  6850. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6851. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6852. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6853. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6854. }
  6855. /**
  6856. * testSecurePostButton method
  6857. *
  6858. * Test that postButton adds _Token fields.
  6859. *
  6860. * @return void
  6861. */
  6862. public function testSecurePostButton()
  6863. {
  6864. $this->Form->request->params['_csrfToken'] = 'testkey';
  6865. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6866. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6867. $tokenDebug = urlencode(json_encode([
  6868. '/posts/delete/1',
  6869. [],
  6870. []
  6871. ]));
  6872. $expected = [
  6873. 'form' => [
  6874. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6875. ],
  6876. ['div' => ['style' => 'display:none;']],
  6877. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6878. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  6879. '/div',
  6880. 'button' => ['type' => 'submit'],
  6881. 'Delete',
  6882. '/button',
  6883. ['div' => ['style' => 'display:none;']],
  6884. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  6885. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  6886. ['input' => [
  6887. 'type' => 'hidden',
  6888. 'name' => '_Token[debug]',
  6889. 'value' => $tokenDebug,
  6890. 'autocomplete' => 'off',
  6891. ]],
  6892. '/div',
  6893. '/form',
  6894. ];
  6895. $this->assertHtml($expected, $result);
  6896. }
  6897. /**
  6898. * testPostLink method
  6899. *
  6900. * @return void
  6901. */
  6902. public function testPostLink()
  6903. {
  6904. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6905. $expected = [
  6906. 'form' => [
  6907. 'method' => 'post', 'action' => '/posts/delete/1',
  6908. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6909. ],
  6910. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6911. '/form',
  6912. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6913. 'Delete',
  6914. '/a'
  6915. ];
  6916. $this->assertHtml($expected, $result);
  6917. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  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' => 'DELETE'],
  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(
  6931. 'Delete',
  6932. '/posts/delete/1',
  6933. ['target' => '_blank', 'class' => 'btn btn-danger']
  6934. );
  6935. $expected = [
  6936. 'form' => [
  6937. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6938. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6939. ],
  6940. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6941. '/form',
  6942. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6943. 'Delete',
  6944. '/a'
  6945. ];
  6946. $this->assertHtml($expected, $result);
  6947. }
  6948. /**
  6949. * testPostLinkWithConfirm method
  6950. *
  6951. * Test the confirm option for postLink().
  6952. *
  6953. * @return void
  6954. */
  6955. public function testPostLinkWithConfirm()
  6956. {
  6957. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6958. $expected = [
  6959. 'form' => [
  6960. 'method' => 'post', 'action' => '/posts/delete/1',
  6961. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6962. ],
  6963. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6964. '/form',
  6965. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6966. 'Delete',
  6967. '/a'
  6968. ];
  6969. $this->assertHtml($expected, $result);
  6970. $result = $this->Form->postLink(
  6971. 'Delete',
  6972. '/posts/delete/1',
  6973. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6974. );
  6975. $expected = [
  6976. 'form' => [
  6977. 'method' => 'post', 'action' => '/posts/delete/1',
  6978. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6979. ],
  6980. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6981. '/form',
  6982. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6983. 'Delete',
  6984. '/a'
  6985. ];
  6986. $this->assertHtml($expected, $result);
  6987. }
  6988. /**
  6989. * testPostLinkWithQuery method
  6990. *
  6991. * Test postLink() with query string args.
  6992. *
  6993. * @return void
  6994. */
  6995. public function testPostLinkWithQuery()
  6996. {
  6997. $result = $this->Form->postLink(
  6998. 'Delete',
  6999. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7000. );
  7001. $expected = [
  7002. 'form' => [
  7003. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7004. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7005. ],
  7006. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7007. '/form',
  7008. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7009. 'Delete',
  7010. '/a'
  7011. ];
  7012. $this->assertHtml($expected, $result);
  7013. }
  7014. /**
  7015. * testPostLinkWithData method
  7016. *
  7017. * Test postLink with additional data.
  7018. *
  7019. * @return void
  7020. */
  7021. public function testPostLinkWithData()
  7022. {
  7023. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7024. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7025. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7026. $this->Form->create($entity);
  7027. $this->Form->end();
  7028. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7029. $this->assertContains(
  7030. '<input type="hidden" name="name" value="show"',
  7031. $result,
  7032. 'should not contain entity data.'
  7033. );
  7034. }
  7035. /**
  7036. * testPostLinkSecurityHash method
  7037. *
  7038. * Test that security hashes for postLink include the url.
  7039. *
  7040. * @return void
  7041. */
  7042. public function testPostLinkSecurityHash()
  7043. {
  7044. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7045. $hash .= '%3Aid';
  7046. $this->Form->request->params['_Token']['key'] = 'test';
  7047. $result = $this->Form->postLink(
  7048. 'Delete',
  7049. '/posts/delete/1',
  7050. ['data' => ['id' => 1]]
  7051. );
  7052. $tokenDebug = urlencode(json_encode([
  7053. '/posts/delete/1',
  7054. [
  7055. 'id' => 1
  7056. ],
  7057. []
  7058. ]));
  7059. $expected = [
  7060. 'form' => [
  7061. 'method' => 'post', 'action' => '/posts/delete/1',
  7062. 'name', 'style' => 'display:none;'
  7063. ],
  7064. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7065. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7066. 'div' => ['style' => 'display:none;'],
  7067. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7068. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7069. ['input' => [
  7070. 'type' => 'hidden',
  7071. 'name' => '_Token[debug]',
  7072. 'value' => $tokenDebug,
  7073. 'autocomplete' => 'off',
  7074. ]],
  7075. '/div',
  7076. '/form',
  7077. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7078. 'Delete',
  7079. '/a'
  7080. ];
  7081. $this->assertHtml($expected, $result);
  7082. }
  7083. /**
  7084. * testPostLinkSecurityHashBlockMode method
  7085. *
  7086. * Test that postLink doesn't modify the fields in the containing form.
  7087. *
  7088. * postLink() calls inside open forms should not modify the field list
  7089. * for the form.
  7090. *
  7091. * @return void
  7092. */
  7093. public function testPostLinkSecurityHashBlockMode()
  7094. {
  7095. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7096. $hash .= '%3A';
  7097. $this->Form->request->params['_Token']['key'] = 'test';
  7098. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7099. $this->Form->control('title');
  7100. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7101. $result = $this->View->fetch('postLink');
  7102. $this->assertEquals(['title'], $this->Form->fields);
  7103. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7104. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7105. }
  7106. /**
  7107. * testPostLinkSecurityHashNoDebugMode method
  7108. *
  7109. * Test that security does not include debug token if debug is false.
  7110. *
  7111. * @return void
  7112. */
  7113. public function testPostLinkSecurityHashNoDebugMode()
  7114. {
  7115. Configure::write('debug', false);
  7116. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7117. $hash .= '%3Aid';
  7118. $this->Form->request->params['_Token']['key'] = 'test';
  7119. $result = $this->Form->postLink(
  7120. 'Delete',
  7121. '/posts/delete/1',
  7122. ['data' => ['id' => 1]]
  7123. );
  7124. $expected = [
  7125. 'form' => [
  7126. 'method' => 'post', 'action' => '/posts/delete/1',
  7127. 'name', 'style' => 'display:none;'
  7128. ],
  7129. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7130. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7131. 'div' => ['style' => 'display:none;'],
  7132. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7133. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7134. '/div',
  7135. '/form',
  7136. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7137. 'Delete',
  7138. '/a'
  7139. ];
  7140. $this->assertHtml($expected, $result);
  7141. }
  7142. /**
  7143. * testPostLinkNestedData method
  7144. *
  7145. * Test using postLink with N dimensional data.
  7146. *
  7147. * @return void
  7148. */
  7149. public function testPostLinkNestedData()
  7150. {
  7151. $data = [
  7152. 'one' => [
  7153. 'two' => [
  7154. 3, 4, 5
  7155. ]
  7156. ]
  7157. ];
  7158. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7159. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7160. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7161. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7162. }
  7163. /**
  7164. * testPostLinkAfterGetForm method
  7165. *
  7166. * Test creating postLinks after a GET form.
  7167. *
  7168. * @return void
  7169. */
  7170. public function testPostLinkAfterGetForm()
  7171. {
  7172. $this->Form->request->params['_csrfToken'] = 'testkey';
  7173. $this->Form->request->params['_Token'] = 'val';
  7174. $this->Form->create($this->article, ['type' => 'get']);
  7175. $this->Form->end();
  7176. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7177. $tokenDebug = urlencode(json_encode([
  7178. '/posts/delete/1',
  7179. [],
  7180. []
  7181. ]));
  7182. $expected = [
  7183. 'form' => [
  7184. 'method' => 'post', 'action' => '/posts/delete/1',
  7185. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7186. ],
  7187. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7188. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7189. 'div' => ['style' => 'display:none;'],
  7190. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7191. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7192. ['input' => [
  7193. 'type' => 'hidden', 'name' => '_Token[debug]',
  7194. 'value' => $tokenDebug,
  7195. 'autocomplete' => 'off'
  7196. ]],
  7197. '/div',
  7198. '/form',
  7199. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7200. 'Delete',
  7201. '/a'
  7202. ];
  7203. $this->assertHtml($expected, $result);
  7204. }
  7205. /**
  7206. * testPostLinkFormBuffer method
  7207. *
  7208. * Test that postLink adds form tags to view block.
  7209. *
  7210. * @return void
  7211. */
  7212. public function testPostLinkFormBuffer()
  7213. {
  7214. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7215. $expected = [
  7216. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7217. 'Delete',
  7218. '/a'
  7219. ];
  7220. $this->assertHtml($expected, $result);
  7221. $result = $this->View->fetch('postLink');
  7222. $expected = [
  7223. 'form' => [
  7224. 'method' => 'post', 'action' => '/posts/delete/1',
  7225. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7226. ],
  7227. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7228. '/form'
  7229. ];
  7230. $this->assertHtml($expected, $result);
  7231. $result = $this->Form->postLink(
  7232. 'Delete',
  7233. '/posts/delete/2',
  7234. ['block' => true, 'method' => 'DELETE']
  7235. );
  7236. $expected = [
  7237. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7238. 'Delete',
  7239. '/a'
  7240. ];
  7241. $this->assertHtml($expected, $result);
  7242. $result = $this->View->fetch('postLink');
  7243. $expected = [
  7244. 'form' => [
  7245. 'method' => 'post', 'action' => '/posts/delete/1',
  7246. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7247. ],
  7248. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7249. '/form',
  7250. [
  7251. 'form' => [
  7252. 'method' => 'post', 'action' => '/posts/delete/2',
  7253. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7254. ],
  7255. ],
  7256. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7257. '/form'
  7258. ];
  7259. $this->assertHtml($expected, $result);
  7260. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7261. $expected = [
  7262. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7263. 'Delete',
  7264. '/a'
  7265. ];
  7266. $this->assertHtml($expected, $result);
  7267. $result = $this->View->fetch('foobar');
  7268. $expected = [
  7269. 'form' => [
  7270. 'method' => 'post', 'action' => '/posts/delete/1',
  7271. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7272. ],
  7273. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7274. '/form'
  7275. ];
  7276. $this->assertHtml($expected, $result);
  7277. }
  7278. /**
  7279. * testSubmitButton method
  7280. *
  7281. * @return void
  7282. */
  7283. public function testSubmitButton()
  7284. {
  7285. $result = $this->Form->submit('');
  7286. $expected = [
  7287. 'div' => ['class' => 'submit'],
  7288. 'input' => ['type' => 'submit', 'value' => ''],
  7289. '/div'
  7290. ];
  7291. $this->assertHtml($expected, $result);
  7292. $result = $this->Form->submit('Test Submit');
  7293. $expected = [
  7294. 'div' => ['class' => 'submit'],
  7295. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7296. '/div'
  7297. ];
  7298. $this->assertHtml($expected, $result);
  7299. $result = $this->Form->submit('Next >');
  7300. $expected = [
  7301. 'div' => ['class' => 'submit'],
  7302. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7303. '/div'
  7304. ];
  7305. $this->assertHtml($expected, $result);
  7306. $result = $this->Form->submit('Next >', ['escape' => false]);
  7307. $expected = [
  7308. 'div' => ['class' => 'submit'],
  7309. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7310. '/div'
  7311. ];
  7312. $this->assertHtml($expected, $result);
  7313. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7314. $expected = [
  7315. 'div' => ['class' => 'submit'],
  7316. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7317. '/div'
  7318. ];
  7319. $this->assertHtml($expected, $result);
  7320. }
  7321. /**
  7322. * testSubmitImage method
  7323. *
  7324. * Test image submit types.
  7325. *
  7326. * @return void
  7327. */
  7328. public function testSubmitImage()
  7329. {
  7330. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7331. $expected = [
  7332. 'div' => ['class' => 'submit'],
  7333. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7334. '/div'
  7335. ];
  7336. $this->assertHtml($expected, $result);
  7337. $result = $this->Form->submit('/relative/cake.power.gif');
  7338. $expected = [
  7339. 'div' => ['class' => 'submit'],
  7340. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7341. '/div'
  7342. ];
  7343. $this->assertHtml($expected, $result);
  7344. $result = $this->Form->submit('cake.power.gif');
  7345. $expected = [
  7346. 'div' => ['class' => 'submit'],
  7347. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7348. '/div'
  7349. ];
  7350. $this->assertHtml($expected, $result);
  7351. $result = $this->Form->submit('Not.an.image');
  7352. $expected = [
  7353. 'div' => ['class' => 'submit'],
  7354. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7355. '/div'
  7356. ];
  7357. $this->assertHtml($expected, $result);
  7358. }
  7359. /**
  7360. * testSubmitUnlockedByDefault method
  7361. *
  7362. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7363. * be submitted at a time.
  7364. *
  7365. * @return void
  7366. */
  7367. public function testSubmitUnlockedByDefault()
  7368. {
  7369. $this->Form->request->params['_Token'] = 'secured';
  7370. $this->Form->submit('Go go');
  7371. $this->Form->submit('Save', ['name' => 'save']);
  7372. $result = $this->Form->unlockField();
  7373. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7374. }
  7375. /**
  7376. * testSubmitImageTimestamp method
  7377. *
  7378. * Test submit image with timestamps.
  7379. *
  7380. * @return void
  7381. */
  7382. public function testSubmitImageTimestamp()
  7383. {
  7384. Configure::write('Asset.timestamp', 'force');
  7385. $result = $this->Form->submit('cake.power.gif');
  7386. $expected = [
  7387. 'div' => ['class' => 'submit'],
  7388. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7389. '/div'
  7390. ];
  7391. $this->assertHtml($expected, $result);
  7392. }
  7393. /**
  7394. * testDateTimeWithGetForms method
  7395. *
  7396. * Test that datetime() works with GET style forms.
  7397. *
  7398. * @return void
  7399. */
  7400. public function testDateTimeWithGetForms()
  7401. {
  7402. extract($this->dateRegex);
  7403. $this->Form->create($this->article, ['type' => 'get']);
  7404. $result = $this->Form->datetime('created');
  7405. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7406. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7407. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7408. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7409. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7410. }
  7411. /**
  7412. * testForMagicControlNonExistingNorValidated method
  7413. *
  7414. * @return void
  7415. */
  7416. public function testForMagicControlNonExistingNorValidated()
  7417. {
  7418. $this->Form->create($this->article);
  7419. $this->Form->templates(['inputContainer' => '{{content}}']);
  7420. $result = $this->Form->control('non_existing_nor_validated');
  7421. $expected = [
  7422. 'label' => ['for' => 'non-existing-nor-validated'],
  7423. 'Non Existing Nor Validated',
  7424. '/label',
  7425. 'input' => [
  7426. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7427. 'id' => 'non-existing-nor-validated'
  7428. ]
  7429. ];
  7430. $this->assertHtml($expected, $result);
  7431. $result = $this->Form->control('non_existing_nor_validated', [
  7432. 'val' => 'my value'
  7433. ]);
  7434. $expected = [
  7435. 'label' => ['for' => 'non-existing-nor-validated'],
  7436. 'Non Existing Nor Validated',
  7437. '/label',
  7438. 'input' => [
  7439. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7440. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7441. ]
  7442. ];
  7443. $this->assertHtml($expected, $result);
  7444. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7445. $result = $this->Form->control('non_existing_nor_validated');
  7446. $expected = [
  7447. 'label' => ['for' => 'non-existing-nor-validated'],
  7448. 'Non Existing Nor Validated',
  7449. '/label',
  7450. 'input' => [
  7451. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7452. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7453. ]
  7454. ];
  7455. $this->assertHtml($expected, $result);
  7456. }
  7457. /**
  7458. * testFormMagicControlLabel method
  7459. *
  7460. * @return void
  7461. */
  7462. public function testFormMagicControlLabel()
  7463. {
  7464. TableRegistry::get('Contacts', [
  7465. 'className' => __NAMESPACE__ . '\ContactsTable'
  7466. ]);
  7467. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7468. $this->Form->templates(['inputContainer' => '{{content}}']);
  7469. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7470. $expected = [
  7471. 'label' => ['for' => 'contacts-name'],
  7472. 'My label',
  7473. '/label',
  7474. 'input' => [
  7475. 'type' => 'text', 'name' => 'Contacts[name]',
  7476. 'id' => 'contacts-name', 'maxlength' => '255'
  7477. ]
  7478. ];
  7479. $this->assertHtml($expected, $result);
  7480. $result = $this->Form->control('name', [
  7481. 'label' => ['class' => 'mandatory']
  7482. ]);
  7483. $expected = [
  7484. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7485. 'Name',
  7486. '/label',
  7487. 'input' => [
  7488. 'type' => 'text', 'name' => 'name',
  7489. 'id' => 'name', 'maxlength' => '255'
  7490. ]
  7491. ];
  7492. $this->assertHtml($expected, $result);
  7493. $result = $this->Form->control('name', [
  7494. 'div' => false,
  7495. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7496. ]);
  7497. $expected = [
  7498. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7499. 'My label',
  7500. '/label',
  7501. 'input' => [
  7502. 'type' => 'text', 'name' => 'name',
  7503. 'id' => 'name', 'maxlength' => '255'
  7504. ]
  7505. ];
  7506. $this->assertHtml($expected, $result);
  7507. $result = $this->Form->control('Contact.name', [
  7508. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7509. ]);
  7510. $expected = [
  7511. 'label' => ['for' => 'my_id'],
  7512. 'Name',
  7513. '/label',
  7514. 'input' => [
  7515. 'type' => 'text', 'name' => 'Contact[name]',
  7516. 'id' => 'my_id', 'maxlength' => '255'
  7517. ]
  7518. ];
  7519. $this->assertHtml($expected, $result);
  7520. $result = $this->Form->control('1.id');
  7521. $expected = ['input' => [
  7522. 'type' => 'hidden', 'name' => '1[id]',
  7523. 'id' => '1-id'
  7524. ]];
  7525. $this->assertHtml($expected, $result);
  7526. $result = $this->Form->control('1.name');
  7527. $expected = [
  7528. 'label' => ['for' => '1-name'],
  7529. 'Name',
  7530. '/label',
  7531. 'input' => [
  7532. 'type' => 'text', 'name' => '1[name]',
  7533. 'id' => '1-name', 'maxlength' => '255'
  7534. ]
  7535. ];
  7536. $this->assertHtml($expected, $result);
  7537. }
  7538. /**
  7539. * testFormEnd method
  7540. *
  7541. * @return void
  7542. */
  7543. public function testFormEnd()
  7544. {
  7545. $this->assertEquals('</form>', $this->Form->end());
  7546. }
  7547. /**
  7548. * testMultiRecordForm method
  7549. *
  7550. * Test the generation of fields for a multi record form.
  7551. *
  7552. * @return void
  7553. */
  7554. public function testMultiRecordForm()
  7555. {
  7556. $this->loadFixtures('Articles', 'Comments');
  7557. $articles = TableRegistry::get('Articles');
  7558. $articles->hasMany('Comments');
  7559. $comment = new Entity(['comment' => 'Value']);
  7560. $article = new Article(['comments' => [$comment]]);
  7561. $this->Form->create([$article]);
  7562. $result = $this->Form->control('0.comments.1.comment');
  7563. //@codingStandardsIgnoreStart
  7564. $expected = [
  7565. 'div' => ['class' => 'input textarea'],
  7566. 'label' => ['for' => '0-comments-1-comment'],
  7567. 'Comment',
  7568. '/label',
  7569. 'textarea' => [
  7570. 'name',
  7571. 'id' => '0-comments-1-comment',
  7572. 'rows' => 5
  7573. ],
  7574. '/textarea',
  7575. '/div'
  7576. ];
  7577. //@codingStandardsIgnoreEnd
  7578. $this->assertHtml($expected, $result);
  7579. $result = $this->Form->control('0.comments.0.comment');
  7580. //@codingStandardsIgnoreStart
  7581. $expected = [
  7582. 'div' => ['class' => 'input textarea'],
  7583. 'label' => ['for' => '0-comments-0-comment'],
  7584. 'Comment',
  7585. '/label',
  7586. 'textarea' => [
  7587. 'name',
  7588. 'id' => '0-comments-0-comment',
  7589. 'rows' => 5
  7590. ],
  7591. 'Value',
  7592. '/textarea',
  7593. '/div'
  7594. ];
  7595. //@codingStandardsIgnoreEnd
  7596. $this->assertHtml($expected, $result);
  7597. $comment->errors('comment', ['Not valid']);
  7598. $result = $this->Form->control('0.comments.0.comment');
  7599. //@codingStandardsIgnoreStart
  7600. $expected = [
  7601. 'div' => ['class' => 'input textarea error'],
  7602. 'label' => ['for' => '0-comments-0-comment'],
  7603. 'Comment',
  7604. '/label',
  7605. 'textarea' => [
  7606. 'name',
  7607. 'class' => 'form-error',
  7608. 'id' => '0-comments-0-comment',
  7609. 'rows' => 5
  7610. ],
  7611. 'Value',
  7612. '/textarea',
  7613. ['div' => ['class' => 'error-message']],
  7614. 'Not valid',
  7615. '/div',
  7616. '/div'
  7617. ];
  7618. //@codingStandardsIgnoreEnd
  7619. $this->assertHtml($expected, $result);
  7620. TableRegistry::get('Comments')
  7621. ->getValidator('default')
  7622. ->allowEmpty('comment', false);
  7623. $result = $this->Form->control('0.comments.1.comment');
  7624. //@codingStandardsIgnoreStart
  7625. $expected = [
  7626. 'div' => ['class' => 'input textarea required'],
  7627. 'label' => ['for' => '0-comments-1-comment'],
  7628. 'Comment',
  7629. '/label',
  7630. 'textarea' => [
  7631. 'name',
  7632. 'required' => 'required',
  7633. 'id' => '0-comments-1-comment',
  7634. 'rows' => 5
  7635. ],
  7636. '/textarea',
  7637. '/div'
  7638. ];
  7639. //@codingStandardsIgnoreEnd
  7640. $this->assertHtml($expected, $result);
  7641. }
  7642. /**
  7643. * testHtml5Controls method
  7644. *
  7645. * Test that some html5 inputs + FormHelper::__call() work.
  7646. *
  7647. * @return void
  7648. */
  7649. public function testHtml5Controls()
  7650. {
  7651. $result = $this->Form->email('User.email');
  7652. $expected = [
  7653. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7654. ];
  7655. $this->assertHtml($expected, $result);
  7656. $result = $this->Form->search('User.query');
  7657. $expected = [
  7658. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7659. ];
  7660. $this->assertHtml($expected, $result);
  7661. $result = $this->Form->search('User.query', ['value' => 'test']);
  7662. $expected = [
  7663. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7664. ];
  7665. $this->assertHtml($expected, $result);
  7666. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7667. $expected = [
  7668. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7669. ];
  7670. $this->assertHtml($expected, $result);
  7671. }
  7672. /**
  7673. * testHtml5ControlWithControl method
  7674. *
  7675. * Test accessing html5 inputs through control().
  7676. *
  7677. * @return void
  7678. */
  7679. public function testHtml5ControlWithControl()
  7680. {
  7681. $this->Form->create();
  7682. $this->Form->templates(['inputContainer' => '{{content}}']);
  7683. $result = $this->Form->control('website', [
  7684. 'type' => 'url',
  7685. 'val' => 'http://domain.tld',
  7686. 'label' => false
  7687. ]);
  7688. $expected = [
  7689. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7690. ];
  7691. $this->assertHtml($expected, $result);
  7692. }
  7693. /**
  7694. * testHtml5ControlException method
  7695. *
  7696. * Test errors when field name is missing.
  7697. *
  7698. * @expectedException \Cake\Core\Exception\Exception
  7699. * @return void
  7700. */
  7701. public function testHtml5ControlException()
  7702. {
  7703. $this->Form->email();
  7704. }
  7705. /**
  7706. * testRequiredAttribute method
  7707. *
  7708. * Tests that formhelper sets required attributes.
  7709. *
  7710. * @return void
  7711. */
  7712. public function testRequiredAttribute()
  7713. {
  7714. $this->article['required'] = [
  7715. 'title' => true,
  7716. 'body' => false,
  7717. ];
  7718. $this->Form->create($this->article);
  7719. $result = $this->Form->control('title');
  7720. $expected = [
  7721. 'div' => ['class' => 'input text required'],
  7722. 'label' => ['for' => 'title'],
  7723. 'Title',
  7724. '/label',
  7725. 'input' => [
  7726. 'type' => 'text',
  7727. 'name' => 'title',
  7728. 'id' => 'title',
  7729. 'required' => 'required',
  7730. ],
  7731. '/div'
  7732. ];
  7733. $this->assertHtml($expected, $result);
  7734. $result = $this->Form->control('title', ['required' => false]);
  7735. $this->assertNotContains('required', $result);
  7736. $result = $this->Form->control('body');
  7737. $expected = [
  7738. 'div' => ['class' => 'input text'],
  7739. 'label' => ['for' => 'body'],
  7740. 'Body',
  7741. '/label',
  7742. 'input' => [
  7743. 'type' => 'text',
  7744. 'name' => 'body',
  7745. 'id' => 'body',
  7746. ],
  7747. '/div'
  7748. ];
  7749. $this->assertHtml($expected, $result);
  7750. $result = $this->Form->control('body', ['required' => true]);
  7751. $this->assertContains('required', $result);
  7752. }
  7753. /**
  7754. * testControlsNotNested method
  7755. *
  7756. * Tests that it is possible to put inputs outside of the label.
  7757. *
  7758. * @return void
  7759. */
  7760. public function testControlsNotNested()
  7761. {
  7762. $this->Form->templates([
  7763. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7764. 'formGroup' => '{{input}}{{label}}',
  7765. ]);
  7766. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7767. $expected = [
  7768. 'div' => ['class' => 'input checkbox'],
  7769. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7770. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7771. 'label' => ['for' => 'foo'],
  7772. 'Foo',
  7773. '/label',
  7774. '/div'
  7775. ];
  7776. $this->assertHtml($expected, $result);
  7777. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7778. $expected = [
  7779. 'div' => ['class' => 'input checkbox'],
  7780. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7781. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7782. '/div'
  7783. ];
  7784. $this->assertHtml($expected, $result);
  7785. $result = $this->Form->control('confirm', [
  7786. 'type' => 'radio',
  7787. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7788. ]);
  7789. $expected = [
  7790. 'div' => ['class' => 'input radio'],
  7791. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7792. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7793. ['label' => ['for' => 'confirm-y']],
  7794. 'Yes',
  7795. '/label',
  7796. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7797. ['label' => ['for' => 'confirm-n']],
  7798. 'No',
  7799. '/label',
  7800. '<label',
  7801. 'Confirm',
  7802. '/label',
  7803. '/div',
  7804. ];
  7805. $this->assertHtml($expected, $result);
  7806. $result = $this->Form->select('category', ['1', '2'], [
  7807. 'multiple' => 'checkbox',
  7808. 'name' => 'fish',
  7809. ]);
  7810. $expected = [
  7811. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7812. ['div' => ['class' => 'checkbox']],
  7813. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7814. ['label' => ['for' => 'fish-0']],
  7815. '1',
  7816. '/label',
  7817. '/div',
  7818. ['div' => ['class' => 'checkbox']],
  7819. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7820. ['label' => ['for' => 'fish-1']],
  7821. '2',
  7822. '/label',
  7823. '/div'
  7824. ];
  7825. $this->assertHtml($expected, $result);
  7826. }
  7827. /**
  7828. * testControlContainerTemplates method
  7829. *
  7830. * Test that *Container templates are used by input.
  7831. *
  7832. * @return void
  7833. */
  7834. public function testControlContainerTemplates()
  7835. {
  7836. $this->Form->templates([
  7837. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7838. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7839. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7840. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7841. ]);
  7842. $this->article['errors'] = [
  7843. 'Article' => ['published' => 'error message']
  7844. ];
  7845. $this->Form->create($this->article);
  7846. $result = $this->Form->control('accept', [
  7847. 'type' => 'checkbox'
  7848. ]);
  7849. $expected = [
  7850. 'div' => ['class' => 'check'],
  7851. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7852. 'label' => ['for' => 'accept'],
  7853. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7854. 'Accept',
  7855. '/label',
  7856. '/div'
  7857. ];
  7858. $this->assertHtml($expected, $result);
  7859. $result = $this->Form->control('accept', [
  7860. 'type' => 'radio',
  7861. 'options' => ['Y', 'N']
  7862. ]);
  7863. $this->assertContains('<div class="rad">', $result);
  7864. $result = $this->Form->control('Article.published', [
  7865. 'type' => 'radio',
  7866. 'options' => ['Y', 'N']
  7867. ]);
  7868. $this->assertContains('<div class="rad err">', $result);
  7869. $result = $this->Form->control('Article.created', [
  7870. 'type' => 'datetime',
  7871. ]);
  7872. $this->assertContains('<div class="dt">', $result);
  7873. }
  7874. /**
  7875. * testFormGroupTemplates method
  7876. *
  7877. * Test that *Container templates are used by input.
  7878. *
  7879. * @return void
  7880. */
  7881. public function testFormGroupTemplates()
  7882. {
  7883. $this->Form->templates([
  7884. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7885. ]);
  7886. $this->Form->create($this->article);
  7887. $result = $this->Form->control('accept', [
  7888. 'type' => 'radio',
  7889. 'options' => ['Y', 'N']
  7890. ]);
  7891. $this->assertContains('<div class="radio">', $result);
  7892. }
  7893. /**
  7894. * testResetTemplates method
  7895. *
  7896. * Test resetting templates.
  7897. *
  7898. * @return void
  7899. */
  7900. public function testResetTemplates()
  7901. {
  7902. $this->Form->templates(['input' => '<input/>']);
  7903. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7904. $this->Form->resetTemplates();
  7905. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7906. }
  7907. /**
  7908. * testContext method
  7909. *
  7910. * Test the context method.
  7911. *
  7912. * @return void
  7913. */
  7914. public function testContext()
  7915. {
  7916. $result = $this->Form->context();
  7917. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7918. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7919. $this->assertSame($mock, $this->Form->context($mock));
  7920. $this->assertSame($mock, $this->Form->context());
  7921. }
  7922. /**
  7923. * testAutoDomId method
  7924. *
  7925. * @return void
  7926. */
  7927. public function testAutoDomId()
  7928. {
  7929. $result = $this->Form->text('field', ['id' => true]);
  7930. $expected = [
  7931. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7932. ];
  7933. $this->assertHtml($expected, $result);
  7934. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7935. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7936. $expected = [
  7937. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7938. ['label' => ['for' => 'field-0']],
  7939. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7940. 'option A',
  7941. '/label',
  7942. ['label' => ['for' => 'field-1']],
  7943. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7944. 'option B',
  7945. '/label',
  7946. ];
  7947. $this->assertHtml($expected, $result);
  7948. $result = $this->Form->select(
  7949. 'multi_field',
  7950. ['first', 'second'],
  7951. ['multiple' => 'checkbox', 'id' => true]
  7952. );
  7953. $expected = [
  7954. 'input' => [
  7955. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7956. ],
  7957. ['div' => ['class' => 'checkbox']],
  7958. ['label' => ['for' => 'multi-field-0']],
  7959. ['input' => [
  7960. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7961. 'value' => '0', 'id' => 'multi-field-0'
  7962. ]],
  7963. 'first',
  7964. '/label',
  7965. '/div',
  7966. ['div' => ['class' => 'checkbox']],
  7967. ['label' => ['for' => 'multi-field-1']],
  7968. ['input' => [
  7969. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7970. 'value' => '1', 'id' => 'multi-field-1'
  7971. ]],
  7972. 'second',
  7973. '/label',
  7974. '/div',
  7975. ];
  7976. $this->assertHtml($expected, $result);
  7977. }
  7978. /**
  7979. * Test the basic setters and getters for value sources
  7980. *
  7981. * @return void
  7982. */
  7983. public function testFormValueSourcesSettersGetters()
  7984. {
  7985. $expected = ['context'];
  7986. $result = $this->Form->getValueSources();
  7987. $this->assertEquals($expected, $result);
  7988. $expected = null;
  7989. $result = $this->Form->getSourceValue('id');
  7990. $this->assertEquals($expected, $result);
  7991. $expected = ['query', 'data', 'context'];
  7992. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7993. $result = $this->Form->getValueSources();
  7994. $this->assertEquals($expected, $result);
  7995. $this->Form->request->data['id'] = '1';
  7996. $this->Form->request->query['id'] = '2';
  7997. $this->Form->setValueSources(['context']);
  7998. $expected = '1';
  7999. $result = $this->Form->getSourceValue('id');
  8000. $this->assertEquals($expected, $result);
  8001. $this->Form->setValueSources('query');
  8002. $expected = ['query'];
  8003. $result = $this->Form->getValueSources();
  8004. $this->assertEquals($expected, $result);
  8005. $expected = '2';
  8006. $result = $this->Form->getSourceValue('id');
  8007. $this->assertEquals($expected, $result);
  8008. $this->Form->setValueSources(['data']);
  8009. $expected = '1';
  8010. $result = $this->Form->getSourceValue('id');
  8011. $this->assertEquals($expected, $result);
  8012. $this->Form->setValueSources(['query', 'data']);
  8013. $expected = '2';
  8014. $result = $this->Form->getSourceValue('id');
  8015. $this->assertEquals($expected, $result);
  8016. }
  8017. /**
  8018. * Tests the different input rendering values based on sources values switching
  8019. *
  8020. * @return void
  8021. */
  8022. public function testFormValueSourcesSingleSwitchRendering()
  8023. {
  8024. $this->loadFixtures('Articles');
  8025. $articles = TableRegistry::get('Articles');
  8026. $article = new Article();
  8027. $articles->patchEntity($article, ['id' => '3']);
  8028. $this->Form->create($article);
  8029. $this->Form->setValueSources(['context']);
  8030. $result = $this->Form->control('id');
  8031. $expected = [
  8032. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8033. ];
  8034. $this->assertHtml($expected, $result);
  8035. $this->Form->request->query['id'] = '5';
  8036. $this->Form->setValueSources(['query']);
  8037. $result = $this->Form->control('id');
  8038. $expected = [
  8039. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8040. ];
  8041. $this->assertHtml($expected, $result);
  8042. $this->Form->request->query['id'] = '5a';
  8043. $this->Form->request->data['id'] = '5b';
  8044. $this->Form->setValueSources(['context']);
  8045. $this->Form->create($article);
  8046. $result = $this->Form->control('id');
  8047. $expected = [
  8048. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8049. ];
  8050. $this->assertHtml($expected, $result);
  8051. $this->Form->setValueSources(['data']);
  8052. $this->Form->create($article);
  8053. $result = $this->Form->control('id');
  8054. $expected = [
  8055. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8056. ];
  8057. $this->assertHtml($expected, $result);
  8058. $this->Form->setValueSources(['query']);
  8059. $result = $this->Form->control('id');
  8060. $expected = [
  8061. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8062. ];
  8063. $this->assertHtml($expected, $result);
  8064. }
  8065. /**
  8066. * Tests the different input rendering values based on sources values switching while supplying
  8067. * an entity (base context) and multiple sources (such as data, query)
  8068. *
  8069. * @return void
  8070. */
  8071. public function testFormValueSourcesListSwitchRendering()
  8072. {
  8073. $this->loadFixtures('Articles');
  8074. $articles = TableRegistry::get('Articles');
  8075. $article = new Article();
  8076. $articles->patchEntity($article, ['id' => '3']);
  8077. $this->Form->request->query['id'] = '9';
  8078. $this->Form->create($article);
  8079. $this->Form->setValueSources(['context', 'query']);
  8080. $result = $this->Form->control('id');
  8081. $expected = [
  8082. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8083. ];
  8084. $this->assertHtml($expected, $result);
  8085. $this->Form->setValueSources(['query', 'context']);
  8086. $result = $this->Form->control('id');
  8087. $expected = [
  8088. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8089. ];
  8090. $this->assertHtml($expected, $result);
  8091. $this->Form->setValueSources(['data', 'query', 'context']);
  8092. $result = $this->Form->control('id');
  8093. $expected = [
  8094. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8095. ];
  8096. $this->assertHtml($expected, $result);
  8097. $this->Form->request->data['id'] = '8';
  8098. $this->Form->request->query['id'] = '9';
  8099. $this->Form->setValueSources(['data', 'query', 'context']);
  8100. $result = $this->Form->control('id');
  8101. $expected = [
  8102. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8103. ];
  8104. $this->assertHtml($expected, $result);
  8105. }
  8106. /**
  8107. * Test the different form input renderings based on values sources switchings through form options
  8108. *
  8109. * @return void
  8110. */
  8111. public function testFormValueSourcesSwitchViaOptionsRendering()
  8112. {
  8113. $this->loadFixtures('Articles');
  8114. $articles = TableRegistry::get('Articles');
  8115. $article = new Article();
  8116. $articles->patchEntity($article, ['id' => '3']);
  8117. $this->Form->request->data['id'] = '4';
  8118. $this->Form->request->query['id'] = '5';
  8119. $this->Form->create($article, ['valueSources' => 'query']);
  8120. $result = $this->Form->control('id');
  8121. $expected = [
  8122. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8123. ];
  8124. $this->assertHtml($expected, $result);
  8125. $result = $this->Form->getSourceValue('id');
  8126. $this->assertEquals('5', $result);
  8127. $this->Form->setValueSources(['context']);
  8128. $this->Form->create($article, ['valueSources' => 'query']);
  8129. $result = $this->Form->control('id');
  8130. $expected = [
  8131. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8132. ];
  8133. $this->assertHtml($expected, $result);
  8134. $result = $this->Form->getSourceValue('id');
  8135. $this->assertEquals('5', $result);
  8136. $this->Form->setValueSources(['query']);
  8137. $this->Form->create($article, ['valueSources' => 'data']);
  8138. $result = $this->Form->control('id');
  8139. $expected = [
  8140. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8141. ];
  8142. $this->assertHtml($expected, $result);
  8143. $result = $this->Form->getSourceValue('id');
  8144. $this->assertEquals('4', $result);
  8145. $this->Form->setValueSources(['query']);
  8146. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8147. $result = $this->Form->control('id');
  8148. $expected = [
  8149. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8150. ];
  8151. $this->assertHtml($expected, $result);
  8152. $result = $this->Form->getSourceValue('id');
  8153. $this->assertEquals('4', $result);
  8154. }
  8155. /**
  8156. * Test the different form input renderings based on values sources switchings through form options
  8157. *
  8158. * @return void
  8159. */
  8160. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8161. {
  8162. $this->loadFixtures('Articles');
  8163. $articles = TableRegistry::get('Articles');
  8164. $article = new Article();
  8165. $articles->patchEntity($article, ['id' => '3']);
  8166. $this->Form->request->data['id'] = '10';
  8167. $this->Form->request->query['id'] = '11';
  8168. $this->Form->setValueSources(['context'])
  8169. ->create($article, ['valueSources' => ['query', 'data']]);
  8170. $result = $this->Form->control('id');
  8171. $expected = [
  8172. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8173. ];
  8174. $this->assertHtml($expected, $result);
  8175. $result = $this->Form->getSourceValue('id');
  8176. $this->assertEquals('11', $result);
  8177. unset($this->Form->request->query['id']);
  8178. $this->Form->setValueSources(['context'])
  8179. ->create($article, ['valueSources' => ['query', 'data']]);
  8180. $result = $this->Form->control('id');
  8181. $expected = [
  8182. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8183. ];
  8184. $this->assertHtml($expected, $result);
  8185. $result = $this->Form->getSourceValue('id');
  8186. $this->assertEquals('10', $result);
  8187. }
  8188. /**
  8189. * Test the different form values sources resetting through From::end();
  8190. *
  8191. * @return void
  8192. */
  8193. public function testFormValueSourcesResetViaEnd()
  8194. {
  8195. $expected = ['context'];
  8196. $result = $this->Form->getValueSources();
  8197. $this->assertEquals($expected, $result);
  8198. $expected = ['query', 'context', 'data'];
  8199. $this->Form->setValueSources(['query', 'context', 'data']);
  8200. $result = $this->Form->getValueSources();
  8201. $this->assertEquals($expected, $result);
  8202. $this->Form->create();
  8203. $result = $this->Form->getValueSources();
  8204. $this->assertEquals($expected, $result);
  8205. $this->Form->end();
  8206. $result = $this->Form->getValueSources();
  8207. $this->assertEquals(['context'], $result);
  8208. }
  8209. /**
  8210. * Test sources values defaults handling
  8211. *
  8212. * @return void
  8213. */
  8214. public function testFormValueSourcesDefaults()
  8215. {
  8216. $this->Form->request->query['password'] = 'open Sesame';
  8217. $this->Form->create();
  8218. $result = $this->Form->password('password');
  8219. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8220. $this->assertHtml($expected, $result);
  8221. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8222. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8223. $this->assertHtml($expected, $result);
  8224. $this->Form->setValueSources('query');
  8225. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8226. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8227. $this->assertHtml($expected, $result);
  8228. $this->Form->setValueSources('data');
  8229. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8230. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8231. $this->assertHtml($expected, $result);
  8232. }
  8233. /**
  8234. * Test sources values schema defaults handling
  8235. *
  8236. * @return void
  8237. */
  8238. public function testSourcesValueDoesntExistPassThrough()
  8239. {
  8240. $this->Form->request->query['category'] = 'sesame-cookies';
  8241. $articles = TableRegistry::get('Articles');
  8242. $entity = $articles->newEntity();
  8243. $this->Form->create($entity);
  8244. $this->Form->setValueSources(['query', 'context']);
  8245. $result = $this->Form->getSourceValue('category');
  8246. $this->assertEquals('sesame-cookies', $result);
  8247. $this->Form->setValueSources(['context', 'query']);
  8248. $result = $this->Form->getSourceValue('category');
  8249. $this->assertEquals('sesame-cookies', $result);
  8250. }
  8251. /**
  8252. * testNestedLabelInput method
  8253. *
  8254. * Test the `nestedInput` parameter
  8255. *
  8256. * @return void
  8257. */
  8258. public function testNestedLabelInput()
  8259. {
  8260. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8261. $expected = [
  8262. 'div' => ['class' => 'input text'],
  8263. 'label' => ['for' => 'foo'],
  8264. ['input' => [
  8265. 'type' => 'text',
  8266. 'name' => 'foo',
  8267. 'id' => 'foo'
  8268. ]],
  8269. 'Foo',
  8270. '/label',
  8271. '/div'
  8272. ];
  8273. $this->assertHtml($expected, $result);
  8274. }
  8275. /**
  8276. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8277. *
  8278. * This test makes sure `false` excludes the label from the render
  8279. *
  8280. * @return void
  8281. */
  8282. public function testInputLabelManipulationDisableLabels()
  8283. {
  8284. $result = $this->Form->input('test', [
  8285. 'type' => 'radio',
  8286. 'options' => ['A', 'B'],
  8287. 'labelOptions' => false
  8288. ]);
  8289. $expected = [
  8290. ['div' => ['class' => 'input radio']],
  8291. '<label',
  8292. 'Test',
  8293. '/label',
  8294. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8295. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8296. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8297. '/div',
  8298. ];
  8299. $this->assertHtml($expected, $result);
  8300. $result = $this->Form->input('checkbox1', [
  8301. 'label' => 'My checkboxes',
  8302. 'multiple' => 'checkbox',
  8303. 'type' => 'select',
  8304. 'options' => [
  8305. ['text' => 'First Checkbox', 'value' => 1],
  8306. ['text' => 'Second Checkbox', 'value' => 2]
  8307. ],
  8308. 'labelOptions' => false
  8309. ]);
  8310. $expected = [
  8311. ['div' => ['class' => 'input select']],
  8312. ['label' => ['for' => 'checkbox1']],
  8313. 'My checkboxes',
  8314. '/label',
  8315. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8316. ['div' => ['class' => 'checkbox']],
  8317. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8318. '/div',
  8319. ['div' => ['class' => 'checkbox']],
  8320. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8321. '/div',
  8322. '/div'
  8323. ];
  8324. $this->assertHtml($expected, $result);
  8325. }
  8326. /**
  8327. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8328. *
  8329. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8330. * added to the class if checked.
  8331. *
  8332. * Also checks to make sure any custom attributes are rendered correctly
  8333. *
  8334. * @return void
  8335. */
  8336. public function testInputLabelManipulationRadios()
  8337. {
  8338. $result = $this->Form->input('test', [
  8339. 'type' => 'radio',
  8340. 'options' => ['A', 'B'],
  8341. 'labelOptions' => ['class' => 'custom-class']
  8342. ]);
  8343. $expected = [
  8344. ['div' => ['class' => 'input radio']],
  8345. '<label',
  8346. 'Test',
  8347. '/label',
  8348. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8349. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8350. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8351. 'A',
  8352. '/label',
  8353. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8354. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8355. 'B',
  8356. '/label',
  8357. '/div',
  8358. ];
  8359. $this->assertHtml($expected, $result);
  8360. $result = $this->Form->input('test', [
  8361. 'type' => 'radio',
  8362. 'options' => ['A', 'B'],
  8363. 'value' => 1,
  8364. 'labelOptions' => ['class' => 'custom-class']
  8365. ]);
  8366. $expected = [
  8367. ['div' => ['class' => 'input radio']],
  8368. '<label',
  8369. 'Test',
  8370. '/label',
  8371. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8372. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8373. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8374. 'A',
  8375. '/label',
  8376. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8377. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8378. 'B',
  8379. '/label',
  8380. '/div',
  8381. ];
  8382. $this->assertHtml($expected, $result);
  8383. $result = $this->Form->input('test', [
  8384. 'type' => 'radio',
  8385. 'options' => ['A', 'B'],
  8386. 'value' => 1,
  8387. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8388. ]);
  8389. $expected = [
  8390. ['div' => ['class' => 'input radio']],
  8391. '<label',
  8392. 'Test',
  8393. '/label',
  8394. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8395. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8396. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8397. 'A',
  8398. '/label',
  8399. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8400. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8401. 'B',
  8402. '/label',
  8403. '/div',
  8404. ];
  8405. $this->assertHtml($expected, $result);
  8406. $result = $this->Form->radio('test', ['A', 'B'], [
  8407. 'label' => [
  8408. 'class' => ['custom-class', 'another-class'],
  8409. 'data-name' => 'bob'
  8410. ],
  8411. 'value' => 1
  8412. ]);
  8413. $expected = [
  8414. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8415. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8416. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8417. 'A',
  8418. '/label',
  8419. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8420. ['input' => [
  8421. 'type' => 'radio',
  8422. 'name' => 'test',
  8423. 'value' => '1',
  8424. 'id' => 'test-1',
  8425. 'checked' => 'checked'
  8426. ]],
  8427. 'B',
  8428. '/label',
  8429. ];
  8430. $this->assertHtml($expected, $result);
  8431. }
  8432. /**
  8433. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8434. *
  8435. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8436. * added to the class if checked.
  8437. *
  8438. * Also checks to make sure any custom attributes are rendered correctly
  8439. *
  8440. * @return void
  8441. */
  8442. public function testInputLabelManipulationCheckboxes()
  8443. {
  8444. $result = $this->Form->input('checkbox1', [
  8445. 'label' => 'My checkboxes',
  8446. 'multiple' => 'checkbox',
  8447. 'type' => 'select',
  8448. 'options' => [
  8449. ['text' => 'First Checkbox', 'value' => 1],
  8450. ['text' => 'Second Checkbox', 'value' => 2]
  8451. ],
  8452. 'labelOptions' => ['class' => 'custom-class'],
  8453. 'value' => ['1']
  8454. ]);
  8455. $expected = [
  8456. ['div' => ['class' => 'input select']],
  8457. ['label' => ['for' => 'checkbox1']],
  8458. 'My checkboxes',
  8459. '/label',
  8460. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8461. ['div' => ['class' => 'checkbox']],
  8462. ['label' => [
  8463. 'class' => 'custom-class selected',
  8464. 'for' => 'checkbox1-1'
  8465. ]],
  8466. ['input' => [
  8467. 'type' => 'checkbox',
  8468. 'name' => 'checkbox1[]',
  8469. 'value' => '1',
  8470. 'id' => 'checkbox1-1',
  8471. 'checked' => 'checked'
  8472. ]],
  8473. 'First Checkbox',
  8474. '/label',
  8475. '/div',
  8476. ['div' => ['class' => 'checkbox']],
  8477. ['label' => [
  8478. 'class' => 'custom-class',
  8479. 'for' => 'checkbox1-2'
  8480. ]],
  8481. ['input' => [
  8482. 'type' => 'checkbox',
  8483. 'name' => 'checkbox1[]',
  8484. 'value' => '2',
  8485. 'id' => 'checkbox1-2'
  8486. ]],
  8487. 'Second Checkbox',
  8488. '/label',
  8489. '/div',
  8490. '/div'
  8491. ];
  8492. $this->assertHtml($expected, $result);
  8493. $result = $this->Form->input('checkbox1', [
  8494. 'label' => 'My checkboxes',
  8495. 'multiple' => 'checkbox',
  8496. 'type' => 'select',
  8497. 'options' => [
  8498. ['text' => 'First Checkbox', 'value' => 1],
  8499. ['text' => 'Second Checkbox', 'value' => 2]
  8500. ],
  8501. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8502. 'value' => ['1']
  8503. ]);
  8504. $expected = [
  8505. ['div' => ['class' => 'input select']],
  8506. ['label' => ['for' => 'checkbox1']],
  8507. 'My checkboxes',
  8508. '/label',
  8509. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8510. ['div' => ['class' => 'checkbox']],
  8511. ['label' => [
  8512. 'class' => 'custom-class another-class selected',
  8513. 'data-name' => 'bob',
  8514. 'for' => 'checkbox1-1'
  8515. ]],
  8516. ['input' => [
  8517. 'type' => 'checkbox',
  8518. 'name' => 'checkbox1[]',
  8519. 'value' => '1',
  8520. 'id' => 'checkbox1-1',
  8521. 'checked' => 'checked'
  8522. ]],
  8523. 'First Checkbox',
  8524. '/label',
  8525. '/div',
  8526. ['div' => ['class' => 'checkbox']],
  8527. ['label' => [
  8528. 'class' => 'custom-class another-class',
  8529. 'data-name' => 'bob',
  8530. 'for' => 'checkbox1-2'
  8531. ]],
  8532. ['input' => [
  8533. 'type' => 'checkbox',
  8534. 'name' => 'checkbox1[]',
  8535. 'value' => '2',
  8536. 'id' => 'checkbox1-2'
  8537. ]],
  8538. 'Second Checkbox',
  8539. '/label',
  8540. '/div',
  8541. '/div'
  8542. ];
  8543. $this->assertHtml($expected, $result);
  8544. }
  8545. }