FormHelperTest.php 323 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801
  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\Routing\Router;
  23. use Cake\TestSuite\TestCase;
  24. use Cake\Utility\Security;
  25. use Cake\Validation\Validator;
  26. use Cake\View\Form\EntityContext;
  27. use Cake\View\Helper\FormHelper;
  28. use Cake\View\View;
  29. use Cake\View\Widget\WidgetLocator;
  30. /**
  31. * Test stub.
  32. */
  33. class Article extends Entity
  34. {
  35. }
  36. /**
  37. * Contact class
  38. */
  39. class ContactsTable extends Table
  40. {
  41. /**
  42. * Default schema
  43. *
  44. * @var array
  45. */
  46. protected $_schema = [
  47. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  48. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  51. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  52. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  53. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  54. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  55. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  56. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  57. ];
  58. /**
  59. * Initializes the schema
  60. *
  61. * @return void
  62. */
  63. public function initialize(array $config)
  64. {
  65. $this->setSchema($this->_schema);
  66. }
  67. }
  68. /**
  69. * ValidateUser class
  70. */
  71. class ValidateUsersTable extends Table
  72. {
  73. /**
  74. * schema method
  75. *
  76. * @var array
  77. */
  78. protected $_schema = [
  79. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  80. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  81. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  82. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  83. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  84. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  85. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  86. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  87. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  88. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  89. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  90. ];
  91. /**
  92. * Initializes the schema
  93. *
  94. * @return void
  95. */
  96. public function initialize(array $config)
  97. {
  98. $this->setSchema($this->_schema);
  99. }
  100. }
  101. /**
  102. * FormHelperTest class
  103. *
  104. * @property \Cake\View\Helper\FormHelper $Form
  105. * @property \Cake\View\View $View
  106. */
  107. class FormHelperTest extends TestCase
  108. {
  109. /**
  110. * Fixtures to be used
  111. *
  112. * @var array
  113. */
  114. public $fixtures = ['core.Articles', 'core.Comments'];
  115. /**
  116. * Do not load the fixtures by default
  117. *
  118. * @var bool
  119. */
  120. public $autoFixtures = false;
  121. /**
  122. * @var array
  123. */
  124. protected $article = [];
  125. /**
  126. * setUp method
  127. *
  128. * @return void
  129. */
  130. public function setUp()
  131. {
  132. parent::setUp();
  133. Configure::write('Config.language', 'eng');
  134. Configure::write('App.base', '');
  135. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  136. $request = new ServerRequest([
  137. 'webroot' => '',
  138. 'base' => '',
  139. 'url' => '/articles/add',
  140. 'params' => [
  141. 'controller' => 'articles',
  142. 'action' => 'add',
  143. ]
  144. ]);
  145. $this->View = new View($request);
  146. $this->Form = new FormHelper($this->View);
  147. $this->dateRegex = [
  148. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  149. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  150. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  152. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  153. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  154. ];
  155. $this->article = [
  156. 'schema' => [
  157. 'id' => ['type' => 'integer'],
  158. 'author_id' => ['type' => 'integer', 'null' => true],
  159. 'title' => ['type' => 'string', 'null' => true],
  160. 'body' => 'text',
  161. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  162. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  163. ],
  164. 'required' => [
  165. 'author_id' => true,
  166. 'title' => true,
  167. ]
  168. ];
  169. Security::setSalt('foo!');
  170. Router::connect('/:controller', ['action' => 'index']);
  171. Router::connect('/:controller/:action/*');
  172. }
  173. /**
  174. * tearDown method
  175. *
  176. * @return void
  177. */
  178. public function tearDown()
  179. {
  180. parent::tearDown();
  181. unset($this->Form, $this->Controller, $this->View);
  182. $this->getTableLocator()->clear();
  183. }
  184. /**
  185. * Test construct() with the templates option.
  186. *
  187. * @return void
  188. */
  189. public function testConstructTemplatesFile()
  190. {
  191. $helper = new FormHelper($this->View, [
  192. 'templates' => 'htmlhelper_tags'
  193. ]);
  194. $result = $helper->control('name');
  195. $this->assertContains('<input', $result);
  196. }
  197. /**
  198. * Test that when specifying custom widgets the config array for that widget
  199. * is overwritten instead of merged.
  200. *
  201. * @return void
  202. */
  203. public function testConstructWithWidgets()
  204. {
  205. $config = [
  206. 'widgets' => [
  207. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  208. ]
  209. ];
  210. $helper = new FormHelper($this->View, $config);
  211. $locator = $helper->getWidgetLocator();
  212. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  213. }
  214. /**
  215. * Test that when specifying custom widgets config file and it should be
  216. * added to widgets array. WidgetLocator will load widgets in constructor.
  217. *
  218. * @return void
  219. */
  220. public function testConstructWithWidgetsConfig()
  221. {
  222. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  223. $locator = $helper->getWidgetLocator();
  224. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  225. }
  226. /**
  227. * Test setting the widget locator
  228. *
  229. * @return void
  230. */
  231. public function testSetAndGetWidgetLocator()
  232. {
  233. $helper = new FormHelper($this->View);
  234. $locator = new WidgetLocator($helper->templater(), $this->View);
  235. $helper->setWidgetLocator($locator);
  236. $this->assertSame($locator, $helper->getWidgetLocator());
  237. }
  238. /**
  239. * Test overridding grouped input types which controls generation of "for"
  240. * attribute of labels.
  241. *
  242. * @return void
  243. */
  244. public function testConstructWithGroupedInputTypes()
  245. {
  246. $helper = new FormHelper($this->View, [
  247. 'groupedInputTypes' => ['radio'],
  248. ]);
  249. $result = $helper->control('when', ['type' => 'datetime']);
  250. $this->assertContains('<label for="when">When</label>', $result);
  251. }
  252. /**
  253. * Test registering a new widget class and rendering it.
  254. *
  255. * @return void
  256. */
  257. public function testAddWidgetAndRenderWidget()
  258. {
  259. $data = [
  260. 'val' => 1
  261. ];
  262. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  263. $this->Form->addWidget('test', $mock);
  264. $mock->expects($this->once())
  265. ->method('render')
  266. ->with($data)
  267. ->will($this->returnValue('HTML'));
  268. $result = $this->Form->widget('test', $data);
  269. $this->assertEquals('HTML', $result);
  270. }
  271. /**
  272. * Test that secureFields() of widget is called after calling render(),
  273. * not before.
  274. *
  275. * @return void
  276. */
  277. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  278. {
  279. $data = [
  280. 'val' => 1,
  281. 'name' => 'test'
  282. ];
  283. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  284. $this->Form->addWidget('test', $mock);
  285. $mock->expects($this->at(0))
  286. ->method('render')
  287. ->with($data)
  288. ->will($this->returnValue('HTML'));
  289. $mock->expects($this->at(1))
  290. ->method('secureFields')
  291. ->with($data)
  292. ->will($this->returnValue(['test']));
  293. $result = $this->Form->widget('test', $data + ['secure' => true]);
  294. $this->assertEquals('HTML', $result);
  295. }
  296. /**
  297. * Test that empty string is not added to secure fields list when
  298. * rendering input widget without name.
  299. *
  300. * @return void
  301. */
  302. public function testRenderingWidgetWithEmptyName()
  303. {
  304. $this->assertEquals([], $this->Form->fields);
  305. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  306. $this->assertEquals('<select name=""></select>', $result);
  307. $this->assertEquals([], $this->Form->fields);
  308. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  309. $this->assertEquals('<select name="0"></select>', $result);
  310. $this->assertEquals(['0'], $this->Form->fields);
  311. }
  312. /**
  313. * Test registering an invalid widget class.
  314. *
  315. * @return void
  316. */
  317. public function testAddWidgetInvalid()
  318. {
  319. $this->expectException(\RuntimeException::class);
  320. $mock = new \StdClass();
  321. $this->Form->addWidget('test', $mock);
  322. $this->Form->widget('test');
  323. }
  324. /**
  325. * Test adding a new context class.
  326. *
  327. * @return void
  328. */
  329. public function testAddContextProvider()
  330. {
  331. $context = 'My data';
  332. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  333. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  334. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  335. $this->assertEquals($context, $data['entity']);
  336. return $stub;
  337. });
  338. $this->Form->create($context);
  339. $result = $this->Form->context();
  340. $this->assertSame($stub, $result);
  341. }
  342. /**
  343. * Test replacing a context class.
  344. *
  345. * @return void
  346. */
  347. public function testAddContextProviderReplace()
  348. {
  349. $entity = new Article();
  350. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  351. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  352. return $stub;
  353. });
  354. $this->Form->create($entity);
  355. $result = $this->Form->context();
  356. $this->assertSame($stub, $result);
  357. }
  358. /**
  359. * Test overriding a context class.
  360. *
  361. * @return void
  362. */
  363. public function testAddContextProviderAdd()
  364. {
  365. $entity = new Article();
  366. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  367. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  368. if ($data['entity'] instanceof Entity) {
  369. return $stub;
  370. }
  371. });
  372. $this->Form->create($entity);
  373. $result = $this->Form->context();
  374. $this->assertSame($stub, $result);
  375. }
  376. /**
  377. * Test adding an invalid context class.
  378. *
  379. * @return void
  380. */
  381. public function testAddContextProviderInvalid()
  382. {
  383. $this->expectException(\RuntimeException::class);
  384. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  385. $context = 'My data';
  386. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  387. return new \StdClass();
  388. });
  389. $this->Form->create($context);
  390. }
  391. /**
  392. * Provides context options for create().
  393. *
  394. * @return array
  395. */
  396. public function contextSelectionProvider()
  397. {
  398. $entity = new Article();
  399. $collection = new Collection([$entity]);
  400. $emptyCollection = new Collection([]);
  401. $arrayObject = new \ArrayObject([]);
  402. $data = [
  403. 'schema' => [
  404. 'title' => ['type' => 'string']
  405. ]
  406. ];
  407. $form = new Form();
  408. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  409. return [
  410. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  411. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  412. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  413. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  414. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  415. 'form' => [$form, 'Cake\View\Form\FormContext'],
  416. 'none' => [null, 'Cake\View\Form\NullContext'],
  417. 'false' => [false, 'Cake\View\Form\NullContext'],
  418. 'custom' => [$custom, get_class($custom)]
  419. ];
  420. }
  421. /**
  422. * Test default context selection in create()
  423. *
  424. * @dataProvider contextSelectionProvider
  425. * @return void
  426. */
  427. public function testCreateContextSelectionBuiltIn($data, $class)
  428. {
  429. $this->loadFixtures('Articles');
  430. $this->Form->create($data);
  431. $this->assertInstanceOf($class, $this->Form->context());
  432. }
  433. /**
  434. * Data provider for type option.
  435. *
  436. * @return array
  437. */
  438. public static function requestTypeProvider()
  439. {
  440. return [
  441. // type, method, override
  442. ['post', 'post', 'POST'],
  443. ['put', 'post', 'PUT'],
  444. ['patch', 'post', 'PATCH'],
  445. ['delete', 'post', 'DELETE'],
  446. ];
  447. }
  448. /**
  449. * Test creating file forms.
  450. *
  451. * @return void
  452. */
  453. public function testCreateFile()
  454. {
  455. $encoding = strtolower(Configure::read('App.encoding'));
  456. $result = $this->Form->create(false, ['type' => 'file']);
  457. $expected = [
  458. 'form' => [
  459. 'method' => 'post', 'action' => '/articles/add',
  460. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  461. ],
  462. 'div' => ['style' => 'display:none;'],
  463. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  464. '/div'
  465. ];
  466. $this->assertHtml($expected, $result);
  467. }
  468. /**
  469. * Test creating GET forms.
  470. *
  471. * @return void
  472. */
  473. public function testCreateGet()
  474. {
  475. $encoding = strtolower(Configure::read('App.encoding'));
  476. $result = $this->Form->create(false, ['type' => 'get']);
  477. $expected = ['form' => [
  478. 'method' => 'get', 'action' => '/articles/add',
  479. 'accept-charset' => $encoding
  480. ]];
  481. $this->assertHtml($expected, $result);
  482. }
  483. /**
  484. * Test explicit method/enctype options.
  485. *
  486. * Explicit method overwrites inferred method from 'type'
  487. *
  488. * @return void
  489. */
  490. public function testCreateExplicitMethodEnctype()
  491. {
  492. $encoding = strtolower(Configure::read('App.encoding'));
  493. $result = $this->Form->create(false, [
  494. 'type' => 'get',
  495. 'method' => 'put',
  496. 'enctype' => 'multipart/form-data'
  497. ]);
  498. $expected = ['form' => [
  499. 'method' => 'put',
  500. 'action' => '/articles/add',
  501. 'enctype' => 'multipart/form-data',
  502. 'accept-charset' => $encoding
  503. ]];
  504. $this->assertHtml($expected, $result);
  505. }
  506. /**
  507. * Test create() with the templates option.
  508. *
  509. * @return void
  510. */
  511. public function testCreateTemplatesArray()
  512. {
  513. $result = $this->Form->create($this->article, [
  514. 'templates' => [
  515. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  516. ]
  517. ]);
  518. $expected = [
  519. 'form' => [
  520. 'class' => 'form-horizontal',
  521. 'method' => 'post',
  522. 'action' => '/articles/add',
  523. 'accept-charset' => 'utf-8'
  524. ]
  525. ];
  526. $this->assertHtml($expected, $result);
  527. }
  528. /**
  529. * Test create() with the templates option.
  530. *
  531. * @return void
  532. */
  533. public function testCreateTemplatesFile()
  534. {
  535. $result = $this->Form->create($this->article, [
  536. 'templates' => 'htmlhelper_tags',
  537. ]);
  538. $expected = [
  539. 'start form',
  540. 'div' => ['class' => 'hidden'],
  541. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  542. '/div'
  543. ];
  544. $this->assertHtml($expected, $result);
  545. }
  546. /**
  547. * Test that create() and end() restore templates.
  548. *
  549. * @return void
  550. */
  551. public function testCreateEndRestoreTemplates()
  552. {
  553. $this->Form->create($this->article, [
  554. 'templates' => ['input' => 'custom input element']
  555. ]);
  556. $this->Form->end();
  557. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  558. }
  559. /**
  560. * Test using template vars in various templates used by control() method.
  561. *
  562. * @return void
  563. */
  564. public function testControlTemplateVars()
  565. {
  566. $result = $this->Form->control('text', [
  567. 'templates' => [
  568. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  569. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  570. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  571. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  572. ],
  573. 'templateVars' => [
  574. 'forinput' => 'in-input',
  575. 'forlabel' => 'in-label',
  576. 'forgroup' => 'in-group',
  577. 'forcontainer' => 'in-container'
  578. ]
  579. ]);
  580. $expected = [
  581. 'div' => ['class'],
  582. 'label' => ['for'],
  583. 'Text in-label',
  584. '/label',
  585. 'in-group',
  586. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  587. 'in-container',
  588. '/div',
  589. ];
  590. $this->assertHtml($expected, $result);
  591. }
  592. /**
  593. * Test ensuring template variables work in template files loaded
  594. * during control().
  595. *
  596. * @return void
  597. */
  598. public function testControlTemplatesFromFile()
  599. {
  600. $result = $this->Form->control('title', [
  601. 'templates' => 'test_templates',
  602. 'templateVars' => [
  603. 'forcontainer' => 'container-data'
  604. ]
  605. ]);
  606. $expected = [
  607. 'div' => ['class'],
  608. 'label' => ['for'],
  609. 'Title',
  610. '/label',
  611. 'input' => ['name', 'type' => 'text', 'id'],
  612. 'container-data',
  613. '/div',
  614. ];
  615. $this->assertHtml($expected, $result);
  616. }
  617. /**
  618. * Test using template vars in inputSubmit and submitContainer template.
  619. *
  620. * @return void
  621. */
  622. public function testSubmitTemplateVars()
  623. {
  624. $this->Form->setTemplates([
  625. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  626. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  627. ]);
  628. $result = $this->Form->submit('Submit', [
  629. 'templateVars' => [
  630. 'forinput' => 'in-input',
  631. 'forcontainer' => 'in-container'
  632. ]
  633. ]);
  634. $expected = [
  635. 'div' => ['class'],
  636. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  637. 'in-container',
  638. '/div',
  639. ];
  640. $this->assertHtml($expected, $result);
  641. }
  642. /**
  643. * test the create() method
  644. *
  645. * @dataProvider requestTypeProvider
  646. * @return void
  647. */
  648. public function testCreateTypeOptions($type, $method, $override)
  649. {
  650. $encoding = strtolower(Configure::read('App.encoding'));
  651. $result = $this->Form->create(false, ['type' => $type]);
  652. $expected = [
  653. 'form' => [
  654. 'method' => $method, 'action' => '/articles/add',
  655. 'accept-charset' => $encoding
  656. ],
  657. 'div' => ['style' => 'display:none;'],
  658. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  659. '/div'
  660. ];
  661. $this->assertHtml($expected, $result);
  662. }
  663. /**
  664. * Test using template vars in Create (formStart template)
  665. *
  666. * @return void
  667. */
  668. public function testCreateTemplateVars()
  669. {
  670. $result = $this->Form->create($this->article, [
  671. 'templates' => [
  672. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  673. ],
  674. 'templateVars' => ['header' => 'headertext']
  675. ]);
  676. $expected = [
  677. 'h4' => ['class'],
  678. 'headertext',
  679. '/h4',
  680. 'form' => [
  681. 'method' => 'post',
  682. 'action' => '/articles/add',
  683. 'accept-charset' => 'utf-8'
  684. ]
  685. ];
  686. $this->assertHtml($expected, $result);
  687. }
  688. /**
  689. * Test opening a form for an update operation.
  690. *
  691. * @return void
  692. */
  693. public function testCreateUpdateForm()
  694. {
  695. $encoding = strtolower(Configure::read('App.encoding'));
  696. $this->View->setRequest($this->View->getRequest()
  697. ->withRequestTarget('/articles/edit/1')
  698. ->withParam('action', 'edit'));
  699. $this->article['defaults']['id'] = 1;
  700. $result = $this->Form->create($this->article);
  701. $expected = [
  702. 'form' => [
  703. 'method' => 'post',
  704. 'action' => '/articles/edit/1',
  705. 'accept-charset' => $encoding
  706. ],
  707. 'div' => ['style' => 'display:none;'],
  708. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  709. '/div'
  710. ];
  711. $this->assertHtml($expected, $result);
  712. }
  713. /**
  714. * test create() with automatic url generation
  715. *
  716. * @return void
  717. */
  718. public function testCreateAutoUrl()
  719. {
  720. $encoding = strtolower(Configure::read('App.encoding'));
  721. $this->View->setRequest($this->View->getRequest()
  722. ->withRequestTarget('/articles/delete/10')
  723. ->withParam('action', 'delete'));
  724. $result = $this->Form->create($this->article);
  725. $expected = [
  726. 'form' => [
  727. 'method' => 'post', 'action' => '/articles/delete/10',
  728. 'accept-charset' => $encoding
  729. ],
  730. 'div' => ['style' => 'display:none;'],
  731. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  732. '/div'
  733. ];
  734. $this->assertHtml($expected, $result);
  735. $this->article['defaults'] = ['id' => 1];
  736. $this->View->setRequest($this->View->getRequest()
  737. ->withRequestTarget('/articles/edit/1')
  738. ->withParam('action', 'delete'));
  739. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  740. $expected = [
  741. 'form' => [
  742. 'method' => 'post',
  743. 'action' => '/articles/edit/1',
  744. 'accept-charset' => $encoding
  745. ],
  746. 'div' => ['style' => 'display:none;'],
  747. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  748. '/div'
  749. ];
  750. $this->assertHtml($expected, $result);
  751. $this->View->setRequest($this->View->getRequest()
  752. ->withParam('action', 'add'));
  753. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  754. $expected = [
  755. 'form' => [
  756. 'method' => 'post',
  757. 'action' => '/articles/publish/1',
  758. 'accept-charset' => $encoding
  759. ],
  760. 'div' => ['style' => 'display:none;'],
  761. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  762. '/div'
  763. ];
  764. $this->assertHtml($expected, $result);
  765. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  766. $expected = [
  767. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  768. 'div' => ['style' => 'display:none;'],
  769. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  770. '/div'
  771. ];
  772. $this->assertHtml($expected, $result);
  773. $this->View->setRequest($this->View->getRequest()
  774. ->withParam('controller', 'Pages'));
  775. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  776. $expected = [
  777. 'form' => [
  778. 'method' => 'post', 'action' => '/Pages/signup/1',
  779. 'accept-charset' => $encoding
  780. ],
  781. 'div' => ['style' => 'display:none;'],
  782. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  783. '/div'
  784. ];
  785. $this->assertHtml($expected, $result);
  786. }
  787. /**
  788. * Test create() with no URL (no "action" attribute for <form> tag)
  789. *
  790. * @return void
  791. */
  792. public function testCreateNoUrl()
  793. {
  794. $result = $this->Form->create(false, ['url' => false]);
  795. $expected = [
  796. 'form' => [
  797. 'method' => 'post',
  798. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  799. ],
  800. 'div' => ['style' => 'display:none;'],
  801. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  802. '/div'
  803. ];
  804. $this->assertHtml($expected, $result);
  805. }
  806. /**
  807. * test create() with a custom route
  808. *
  809. * @return void
  810. */
  811. public function testCreateCustomRoute()
  812. {
  813. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  814. $encoding = strtolower(Configure::read('App.encoding'));
  815. $this->View->setRequest($this->View->getRequest()
  816. ->withParam('controller', 'users'));
  817. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  818. $expected = [
  819. 'form' => [
  820. 'method' => 'post', 'action' => '/login',
  821. 'accept-charset' => $encoding
  822. ],
  823. 'div' => ['style' => 'display:none;'],
  824. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  825. '/div'
  826. ];
  827. $this->assertHtml($expected, $result);
  828. Router::connect(
  829. '/new-article',
  830. ['controller' => 'articles', 'action' => 'myaction'],
  831. ['_name' => 'my-route']
  832. );
  833. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  834. $expected = [
  835. 'form' => [
  836. 'method' => 'post', 'action' => '/new-article',
  837. 'accept-charset' => $encoding,
  838. ],
  839. 'div' => ['style' => 'display:none;'],
  840. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  841. '/div'
  842. ];
  843. $this->assertHtml($expected, $result);
  844. }
  845. /**
  846. * test automatic accept-charset overriding
  847. *
  848. * @return void
  849. */
  850. public function testCreateWithAcceptCharset()
  851. {
  852. $result = $this->Form->create(
  853. $this->article,
  854. [
  855. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  856. ]
  857. );
  858. $expected = [
  859. 'form' => [
  860. 'method' => 'post', 'action' => '/articles',
  861. 'accept-charset' => 'iso-8859-1'
  862. ],
  863. 'div' => ['style' => 'display:none;'],
  864. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  865. '/div'
  866. ];
  867. $this->assertHtml($expected, $result);
  868. }
  869. /**
  870. * Test base form URL when url param is passed with multiple parameters (&)
  871. *
  872. */
  873. public function testCreateQueryStringRequest()
  874. {
  875. $encoding = strtolower(Configure::read('App.encoding'));
  876. $result = $this->Form->create($this->article, [
  877. 'type' => 'post',
  878. 'escape' => false,
  879. 'url' => [
  880. 'controller' => 'controller',
  881. 'action' => 'action',
  882. '?' => ['param1' => 'value1', 'param2' => 'value2']
  883. ]
  884. ]);
  885. $expected = [
  886. 'form' => [
  887. 'method' => 'post',
  888. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  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. $result = $this->Form->create($this->article, [
  897. 'type' => 'post',
  898. 'url' => [
  899. 'controller' => 'controller',
  900. 'action' => 'action',
  901. '?' => ['param1' => 'value1', 'param2' => 'value2']
  902. ]
  903. ]);
  904. $this->assertHtml($expected, $result);
  905. }
  906. /**
  907. * test that create() doesn't cause errors by multiple id's being in the primary key
  908. * as could happen with multiple select or checkboxes.
  909. *
  910. * @return void
  911. */
  912. public function testCreateWithMultipleIdInData()
  913. {
  914. $encoding = strtolower(Configure::read('App.encoding'));
  915. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  916. $result = $this->Form->create($this->article);
  917. $expected = [
  918. 'form' => [
  919. 'method' => 'post',
  920. 'action' => '/articles/add',
  921. 'accept-charset' => $encoding
  922. ],
  923. 'div' => ['style' => 'display:none;'],
  924. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  925. '/div'
  926. ];
  927. $this->assertHtml($expected, $result);
  928. }
  929. /**
  930. * test that create() doesn't add in extra passed params.
  931. *
  932. * @return void
  933. */
  934. public function testCreatePassedArgs()
  935. {
  936. $encoding = strtolower(Configure::read('App.encoding'));
  937. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  938. $result = $this->Form->create($this->article, [
  939. 'type' => 'post',
  940. 'escape' => false,
  941. 'url' => [
  942. 'action' => 'edit',
  943. 'myparam'
  944. ]
  945. ]);
  946. $expected = [
  947. 'form' => [
  948. 'method' => 'post',
  949. 'action' => '/articles/edit/myparam',
  950. 'accept-charset' => $encoding
  951. ],
  952. 'div' => ['style' => 'display:none;'],
  953. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  954. '/div'
  955. ];
  956. $this->assertHtml($expected, $result);
  957. }
  958. /**
  959. * test creating a get form, and get form inputs.
  960. *
  961. * @return void
  962. */
  963. public function testGetFormCreate()
  964. {
  965. $encoding = strtolower(Configure::read('App.encoding'));
  966. $result = $this->Form->create($this->article, ['type' => 'get']);
  967. $expected = ['form' => [
  968. 'method' => 'get', 'action' => '/articles/add',
  969. 'accept-charset' => $encoding
  970. ]];
  971. $this->assertHtml($expected, $result);
  972. $result = $this->Form->text('title');
  973. $expected = ['input' => [
  974. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  975. ]];
  976. $this->assertHtml($expected, $result);
  977. $result = $this->Form->password('password');
  978. $expected = ['input' => [
  979. 'name' => 'password', 'type' => 'password'
  980. ]];
  981. $this->assertHtml($expected, $result);
  982. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  983. $result = $this->Form->text('user_form');
  984. $expected = ['input' => [
  985. 'name' => 'user_form', 'type' => 'text'
  986. ]];
  987. $this->assertHtml($expected, $result);
  988. }
  989. /**
  990. * test get form, and inputs when the model param is false
  991. *
  992. * @return void
  993. */
  994. public function testGetFormWithFalseModel()
  995. {
  996. $encoding = strtolower(Configure::read('App.encoding'));
  997. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'contact_test'));
  998. $result = $this->Form->create(false, [
  999. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  1000. ]);
  1001. $expected = ['form' => [
  1002. 'method' => 'get', 'action' => '/contact_test/add',
  1003. 'accept-charset' => $encoding
  1004. ]];
  1005. $this->assertHtml($expected, $result);
  1006. $result = $this->Form->text('reason');
  1007. $expected = [
  1008. 'input' => ['type' => 'text', 'name' => 'reason']
  1009. ];
  1010. $this->assertHtml($expected, $result);
  1011. }
  1012. /**
  1013. * testFormCreateWithSecurity method
  1014. *
  1015. * Test form->create() with security key.
  1016. *
  1017. * @return void
  1018. */
  1019. public function testCreateWithSecurity()
  1020. {
  1021. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1022. $encoding = strtolower(Configure::read('App.encoding'));
  1023. $result = $this->Form->create($this->article, [
  1024. 'url' => '/articles/publish',
  1025. ]);
  1026. $expected = [
  1027. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  1028. 'div' => ['style' => 'display:none;'],
  1029. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1030. ['input' => [
  1031. 'type' => 'hidden',
  1032. 'name' => '_csrfToken',
  1033. 'value' => 'testKey',
  1034. 'autocomplete' => 'off',
  1035. ]],
  1036. '/div'
  1037. ];
  1038. $this->assertHtml($expected, $result);
  1039. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1040. $expected['form']['id'] = 'MyForm';
  1041. $this->assertHtml($expected, $result);
  1042. }
  1043. /**
  1044. * testFormCreateGetNoSecurity method
  1045. *
  1046. * Test form->create() with no security key as its a get form
  1047. *
  1048. * @return void
  1049. */
  1050. public function testCreateEndGetNoSecurity()
  1051. {
  1052. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1053. $article = new Article();
  1054. $result = $this->Form->create($article, [
  1055. 'type' => 'get',
  1056. 'url' => '/contacts/add'
  1057. ]);
  1058. $this->assertNotContains('testKey', $result);
  1059. $result = $this->Form->end();
  1060. $this->assertNotContains('testKey', $result);
  1061. }
  1062. /**
  1063. * test that create() clears the fields property so it starts fresh
  1064. *
  1065. * @return void
  1066. */
  1067. public function testCreateClearingFields()
  1068. {
  1069. $this->Form->fields = ['model_id'];
  1070. $this->Form->create($this->article);
  1071. $this->assertEquals([], $this->Form->fields);
  1072. }
  1073. /**
  1074. * Tests form hash generation with model-less data
  1075. *
  1076. * @return void
  1077. */
  1078. public function testValidateHashNoModel()
  1079. {
  1080. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1081. $fields = ['anything'];
  1082. $result = $this->Form->secure($fields);
  1083. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1084. $this->assertContains($hash, $result);
  1085. }
  1086. /**
  1087. * Tests that hidden fields generated for checkboxes don't get locked
  1088. *
  1089. * @return void
  1090. */
  1091. public function testNoCheckboxLocking()
  1092. {
  1093. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1094. $this->assertSame([], $this->Form->fields);
  1095. $this->Form->checkbox('check', ['value' => '1']);
  1096. $this->assertSame($this->Form->fields, ['check']);
  1097. }
  1098. /**
  1099. * testFormSecurityFields method
  1100. *
  1101. * Test generation of secure form hash generation.
  1102. *
  1103. * @return void
  1104. */
  1105. public function testFormSecurityFields()
  1106. {
  1107. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1108. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1109. $result = $this->Form->secure($fields);
  1110. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1111. $hash .= ':' . 'Model.valid';
  1112. $hash = urlencode($hash);
  1113. $tokenDebug = urlencode(json_encode([
  1114. '',
  1115. $fields,
  1116. []
  1117. ]));
  1118. $expected = [
  1119. 'div' => ['style' => 'display:none;'],
  1120. ['input' => [
  1121. 'type' => 'hidden',
  1122. 'name' => '_Token[fields]',
  1123. 'value' => $hash,
  1124. 'autocomplete' => 'off',
  1125. ]],
  1126. ['input' => [
  1127. 'type' => 'hidden',
  1128. 'name' => '_Token[unlocked]',
  1129. 'value' => '',
  1130. 'autocomplete' => 'off',
  1131. ]],
  1132. ['input' => [
  1133. 'type' => 'hidden',
  1134. 'name' => '_Token[debug]',
  1135. 'value' => $tokenDebug,
  1136. 'autocomplete' => 'off',
  1137. ]],
  1138. '/div'
  1139. ];
  1140. $this->assertHtml($expected, $result);
  1141. }
  1142. /**
  1143. * testFormSecurityFields method
  1144. *
  1145. * Test debug token is not generated if debug is false
  1146. *
  1147. * @return void
  1148. */
  1149. public function testFormSecurityFieldsNoDebugMode()
  1150. {
  1151. Configure::write('debug', false);
  1152. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1153. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1154. $result = $this->Form->secure($fields);
  1155. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1156. $hash .= ':' . 'Model.valid';
  1157. $hash = urlencode($hash);
  1158. $expected = [
  1159. 'div' => ['style' => 'display:none;'],
  1160. ['input' => [
  1161. 'type' => 'hidden',
  1162. 'name' => '_Token[fields]',
  1163. 'autocomplete' => 'off',
  1164. 'value' => $hash
  1165. ]],
  1166. ['input' => [
  1167. 'type' => 'hidden',
  1168. 'name' => '_Token[unlocked]',
  1169. 'autocomplete' => 'off',
  1170. 'value' => '',
  1171. ]],
  1172. '/div'
  1173. ];
  1174. $this->assertHtml($expected, $result);
  1175. }
  1176. /**
  1177. * Tests correct generation of number fields for smallint
  1178. *
  1179. * @return void
  1180. */
  1181. public function testTextFieldGenerationForSmallint()
  1182. {
  1183. $this->article['schema'] = [
  1184. 'foo' => [
  1185. 'type' => 'smallinteger',
  1186. 'null' => false,
  1187. 'default' => null,
  1188. 'length' => 10
  1189. ]
  1190. ];
  1191. $this->Form->create($this->article);
  1192. $result = $this->Form->control('foo');
  1193. $this->assertContains('class="input number"', $result);
  1194. $this->assertContains('type="number"', $result);
  1195. }
  1196. /**
  1197. * Tests correct generation of number fields for tinyint
  1198. *
  1199. * @return void
  1200. */
  1201. public function testTextFieldGenerationForTinyint()
  1202. {
  1203. $this->article['schema'] = [
  1204. 'foo' => [
  1205. 'type' => 'tinyinteger',
  1206. 'null' => false,
  1207. 'default' => null,
  1208. 'length' => 10
  1209. ]
  1210. ];
  1211. $this->Form->create($this->article);
  1212. $result = $this->Form->control('foo');
  1213. $this->assertContains('class="input number"', $result);
  1214. $this->assertContains('type="number"', $result);
  1215. }
  1216. /**
  1217. * Tests correct generation of number fields for double and float fields
  1218. *
  1219. * @return void
  1220. */
  1221. public function testTextFieldGenerationForFloats()
  1222. {
  1223. $this->article['schema'] = [
  1224. 'foo' => [
  1225. 'type' => 'float',
  1226. 'null' => false,
  1227. 'default' => null,
  1228. 'length' => 10
  1229. ]
  1230. ];
  1231. $this->Form->create($this->article);
  1232. $result = $this->Form->control('foo');
  1233. $expected = [
  1234. 'div' => ['class' => 'input number'],
  1235. 'label' => ['for' => 'foo'],
  1236. 'Foo',
  1237. '/label',
  1238. ['input' => [
  1239. 'type' => 'number',
  1240. 'name' => 'foo',
  1241. 'id' => 'foo',
  1242. 'step' => 'any'
  1243. ]],
  1244. '/div'
  1245. ];
  1246. $this->assertHtml($expected, $result);
  1247. $result = $this->Form->control('foo', ['step' => 0.5]);
  1248. $expected = [
  1249. 'div' => ['class' => 'input number'],
  1250. 'label' => ['for' => 'foo'],
  1251. 'Foo',
  1252. '/label',
  1253. ['input' => [
  1254. 'type' => 'number',
  1255. 'name' => 'foo',
  1256. 'id' => 'foo',
  1257. 'step' => '0.5'
  1258. ]],
  1259. '/div'
  1260. ];
  1261. $this->assertHtml($expected, $result);
  1262. }
  1263. /**
  1264. * Tests correct generation of number fields for integer fields
  1265. *
  1266. * @return void
  1267. */
  1268. public function testTextFieldTypeNumberGenerationForIntegers()
  1269. {
  1270. $this->getTableLocator()->get('Contacts', [
  1271. 'className' => __NAMESPACE__ . '\ContactsTable'
  1272. ]);
  1273. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1274. $result = $this->Form->control('age');
  1275. $expected = [
  1276. 'div' => ['class' => 'input number'],
  1277. 'label' => ['for' => 'age'],
  1278. 'Age',
  1279. '/label',
  1280. ['input' => [
  1281. 'type' => 'number', 'name' => 'age',
  1282. 'id' => 'age'
  1283. ]],
  1284. '/div'
  1285. ];
  1286. $this->assertHtml($expected, $result);
  1287. }
  1288. /**
  1289. * Tests correct generation of file upload fields for binary fields
  1290. *
  1291. * @return void
  1292. */
  1293. public function testFileUploadFieldTypeGenerationForBinaries()
  1294. {
  1295. $table = $this->getTableLocator()->get('Contacts', [
  1296. 'className' => __NAMESPACE__ . '\ContactsTable'
  1297. ]);
  1298. $table->setSchema(['foo' => [
  1299. 'type' => 'binary',
  1300. 'null' => false,
  1301. 'default' => null,
  1302. 'length' => 1024
  1303. ]]);
  1304. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1305. $result = $this->Form->control('foo');
  1306. $expected = [
  1307. 'div' => ['class' => 'input file'],
  1308. 'label' => ['for' => 'foo'],
  1309. 'Foo',
  1310. '/label',
  1311. ['input' => [
  1312. 'type' => 'file', 'name' => 'foo',
  1313. 'id' => 'foo'
  1314. ]],
  1315. '/div'
  1316. ];
  1317. $this->assertHtml($expected, $result);
  1318. }
  1319. /**
  1320. * testFormSecurityMultipleFields method
  1321. *
  1322. * Test secure() with multiple row form. Ensure hash is correct.
  1323. *
  1324. * @return void
  1325. */
  1326. public function testFormSecurityMultipleFields()
  1327. {
  1328. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1329. $fields = [
  1330. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1331. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1332. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1333. ];
  1334. $result = $this->Form->secure($fields);
  1335. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1336. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1337. $tokenDebug = urlencode(json_encode([
  1338. '',
  1339. $fields,
  1340. []
  1341. ]));
  1342. $expected = [
  1343. 'div' => ['style' => 'display:none;'],
  1344. ['input' => [
  1345. 'type' => 'hidden',
  1346. 'name' => '_Token[fields]',
  1347. 'value' => $hash,
  1348. 'autocomplete' => 'off',
  1349. ]],
  1350. ['input' => [
  1351. 'type' => 'hidden',
  1352. 'name' => '_Token[unlocked]',
  1353. 'autocomplete' => 'off',
  1354. 'value' => ''
  1355. ]],
  1356. ['input' => [
  1357. 'type' => 'hidden',
  1358. 'name' => '_Token[debug]',
  1359. 'value' => $tokenDebug,
  1360. 'autocomplete' => 'off',
  1361. ]],
  1362. '/div'
  1363. ];
  1364. $this->assertHtml($expected, $result);
  1365. }
  1366. /**
  1367. * testFormSecurityMultipleSubmitButtons
  1368. *
  1369. * test form submit generation and ensure that _Token is only created on end()
  1370. *
  1371. * @return void
  1372. */
  1373. public function testFormSecurityMultipleSubmitButtons()
  1374. {
  1375. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1376. $this->Form->create($this->article);
  1377. $this->Form->text('Address.title');
  1378. $this->Form->text('Address.first_name');
  1379. $result = $this->Form->submit('Save', ['name' => 'save']);
  1380. $expected = [
  1381. 'div' => ['class' => 'submit'],
  1382. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1383. '/div',
  1384. ];
  1385. $this->assertHtml($expected, $result);
  1386. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1387. $expected = [
  1388. 'div' => ['class' => 'submit'],
  1389. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1390. '/div',
  1391. ];
  1392. $this->assertHtml($expected, $result);
  1393. $result = $this->Form->end();
  1394. $tokenDebug = urlencode(json_encode([
  1395. '/articles/add',
  1396. [
  1397. 'Address.title',
  1398. 'Address.first_name',
  1399. ],
  1400. ['save', 'cancel']
  1401. ]));
  1402. $expected = [
  1403. 'div' => ['style' => 'display:none;'],
  1404. ['input' => [
  1405. 'type' => 'hidden',
  1406. 'name' => '_Token[fields]',
  1407. 'autocomplete',
  1408. 'value'
  1409. ]],
  1410. ['input' => [
  1411. 'type' => 'hidden',
  1412. 'name' => '_Token[unlocked]',
  1413. 'value' => 'cancel%7Csave',
  1414. 'autocomplete' => 'off',
  1415. ]],
  1416. ['input' => [
  1417. 'type' => 'hidden',
  1418. 'name' => '_Token[debug]',
  1419. 'value' => $tokenDebug,
  1420. 'autocomplete' => 'off',
  1421. ]],
  1422. '/div'
  1423. ];
  1424. $this->assertHtml($expected, $result);
  1425. }
  1426. /**
  1427. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1428. *
  1429. * @return void
  1430. */
  1431. public function testSecurityButtonNestedNamed()
  1432. {
  1433. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1434. $this->Form->create('Addresses');
  1435. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1436. $result = $this->Form->unlockField();
  1437. $this->assertEquals(['Address.button'], $result);
  1438. }
  1439. /**
  1440. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1441. *
  1442. * @return void
  1443. */
  1444. public function testSecuritySubmitNestedNamed()
  1445. {
  1446. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1447. $this->Form->create($this->article);
  1448. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1449. $result = $this->Form->unlockField();
  1450. $this->assertEquals(['Address.button'], $result);
  1451. }
  1452. /**
  1453. * Test that the correct fields are unlocked for image submits with no names.
  1454. *
  1455. * @return void
  1456. */
  1457. public function testSecuritySubmitImageNoName()
  1458. {
  1459. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1460. $this->Form->create(false);
  1461. $result = $this->Form->submit('save.png');
  1462. $expected = [
  1463. 'div' => ['class' => 'submit'],
  1464. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1465. '/div'
  1466. ];
  1467. $this->assertHtml($expected, $result);
  1468. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1469. }
  1470. /**
  1471. * Test that the correct fields are unlocked for image submits with names.
  1472. *
  1473. * @return void
  1474. */
  1475. public function testSecuritySubmitImageName()
  1476. {
  1477. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1478. $this->Form->create(null);
  1479. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1480. $expected = [
  1481. 'div' => ['class' => 'submit'],
  1482. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1483. '/div'
  1484. ];
  1485. $this->assertHtml($expected, $result);
  1486. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1487. }
  1488. /**
  1489. * testFormSecurityMultipleControlFields method
  1490. *
  1491. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1492. *
  1493. * @return void
  1494. */
  1495. public function testFormSecurityMultipleControlFields()
  1496. {
  1497. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1498. $this->Form->create();
  1499. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1500. $this->Form->control('Addresses.0.title');
  1501. $this->Form->control('Addresses.0.first_name');
  1502. $this->Form->control('Addresses.0.last_name');
  1503. $this->Form->control('Addresses.0.address');
  1504. $this->Form->control('Addresses.0.city');
  1505. $this->Form->control('Addresses.0.phone');
  1506. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1507. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1508. $this->Form->control('Addresses.1.title');
  1509. $this->Form->control('Addresses.1.first_name');
  1510. $this->Form->control('Addresses.1.last_name');
  1511. $this->Form->control('Addresses.1.address');
  1512. $this->Form->control('Addresses.1.city');
  1513. $this->Form->control('Addresses.1.phone');
  1514. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1515. $result = $this->Form->secure($this->Form->fields);
  1516. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1517. $tokenDebug = urlencode(json_encode([
  1518. '/articles/add',
  1519. [
  1520. 'Addresses.0.id' => '123456',
  1521. 'Addresses.0.title',
  1522. 'Addresses.0.first_name',
  1523. 'Addresses.0.last_name',
  1524. 'Addresses.0.address',
  1525. 'Addresses.0.city',
  1526. 'Addresses.0.phone',
  1527. 'Addresses.0.primary',
  1528. 'Addresses.1.id' => '654321',
  1529. 'Addresses.1.title',
  1530. 'Addresses.1.first_name',
  1531. 'Addresses.1.last_name',
  1532. 'Addresses.1.address',
  1533. 'Addresses.1.city',
  1534. 'Addresses.1.phone',
  1535. 'Addresses.1.primary',
  1536. ],
  1537. []
  1538. ]));
  1539. $expected = [
  1540. 'div' => ['style' => 'display:none;'],
  1541. ['input' => [
  1542. 'type' => 'hidden',
  1543. 'name' => '_Token[fields]',
  1544. 'value' => $hash,
  1545. 'autocomplete' => 'off',
  1546. ]],
  1547. ['input' => [
  1548. 'type' => 'hidden',
  1549. 'name' => '_Token[unlocked]',
  1550. 'autocomplete' => 'off',
  1551. 'value' => ''
  1552. ]],
  1553. ['input' => [
  1554. 'type' => 'hidden',
  1555. 'name' => '_Token[debug]',
  1556. 'value' => $tokenDebug,
  1557. 'autocomplete' => 'off',
  1558. ]],
  1559. '/div'
  1560. ];
  1561. $this->assertHtml($expected, $result);
  1562. }
  1563. /**
  1564. * testFormSecurityArrayFields method
  1565. *
  1566. * Test form security with Model.field.0 style inputs.
  1567. *
  1568. * @return void
  1569. */
  1570. public function testFormSecurityArrayFields()
  1571. {
  1572. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1573. $this->Form->create();
  1574. $this->Form->text('Address.primary.1');
  1575. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1576. $this->Form->text('Address.secondary.1.0');
  1577. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1578. }
  1579. /**
  1580. * testFormSecurityMultipleControlDisabledFields method
  1581. *
  1582. * Test secure form generation with multiple records and disabled fields.
  1583. *
  1584. * @return void
  1585. */
  1586. public function testFormSecurityMultipleControlDisabledFields()
  1587. {
  1588. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1589. 'unlockedFields' => ['first_name', 'address']
  1590. ]));
  1591. $this->Form->create();
  1592. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1593. $this->Form->text('Addresses.0.title');
  1594. $this->Form->text('Addresses.0.first_name');
  1595. $this->Form->text('Addresses.0.last_name');
  1596. $this->Form->text('Addresses.0.address');
  1597. $this->Form->text('Addresses.0.city');
  1598. $this->Form->text('Addresses.0.phone');
  1599. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1600. $this->Form->text('Addresses.1.title');
  1601. $this->Form->text('Addresses.1.first_name');
  1602. $this->Form->text('Addresses.1.last_name');
  1603. $this->Form->text('Addresses.1.address');
  1604. $this->Form->text('Addresses.1.city');
  1605. $this->Form->text('Addresses.1.phone');
  1606. $result = $this->Form->secure($this->Form->fields);
  1607. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1608. $tokenDebug = urlencode(json_encode([
  1609. '/articles/add',
  1610. [
  1611. 'Addresses.0.id' => '123456',
  1612. 'Addresses.0.title',
  1613. 'Addresses.0.last_name',
  1614. 'Addresses.0.city',
  1615. 'Addresses.0.phone',
  1616. 'Addresses.1.id' => '654321',
  1617. 'Addresses.1.title',
  1618. 'Addresses.1.last_name',
  1619. 'Addresses.1.city',
  1620. 'Addresses.1.phone'
  1621. ],
  1622. [
  1623. 'first_name',
  1624. 'address'
  1625. ]
  1626. ]));
  1627. $expected = [
  1628. 'div' => ['style' => 'display:none;'],
  1629. ['input' => [
  1630. 'type' => 'hidden',
  1631. 'name' => '_Token[fields]',
  1632. 'autocomplete' => 'off',
  1633. 'value' => $hash
  1634. ]],
  1635. ['input' => [
  1636. 'type' => 'hidden',
  1637. 'name' => '_Token[unlocked]',
  1638. 'autocomplete' => 'off',
  1639. 'value' => 'address%7Cfirst_name',
  1640. ]],
  1641. ['input' => [
  1642. 'type' => 'hidden',
  1643. 'name' => '_Token[debug]',
  1644. 'autocomplete' => 'off',
  1645. 'value' => $tokenDebug,
  1646. ]],
  1647. '/div'
  1648. ];
  1649. $this->assertHtml($expected, $result);
  1650. }
  1651. /**
  1652. * testFormSecurityControlDisabledFields method
  1653. *
  1654. * Test single record form with disabled fields.
  1655. *
  1656. * @return void
  1657. */
  1658. public function testFormSecurityControlUnlockedFields()
  1659. {
  1660. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1661. 'unlockedFields' => ['first_name', 'address']
  1662. ]));
  1663. $this->Form->create();
  1664. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1665. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1666. $this->Form->text('Addresses.title');
  1667. $this->Form->text('Addresses.first_name');
  1668. $this->Form->text('Addresses.last_name');
  1669. $this->Form->text('Addresses.address');
  1670. $this->Form->text('Addresses.city');
  1671. $this->Form->text('Addresses.phone');
  1672. $result = $this->Form->fields;
  1673. $expected = [
  1674. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1675. 'Addresses.city', 'Addresses.phone'
  1676. ];
  1677. $this->assertEquals($expected, $result);
  1678. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1679. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1680. $tokenDebug = urlencode(json_encode([
  1681. '/articles/add',
  1682. [
  1683. 'Addresses.id' => '123456',
  1684. 'Addresses.title',
  1685. 'Addresses.last_name',
  1686. 'Addresses.city',
  1687. 'Addresses.phone'
  1688. ],
  1689. [
  1690. 'first_name',
  1691. 'address'
  1692. ]
  1693. ]));
  1694. $expected = [
  1695. 'div' => ['style' => 'display:none;'],
  1696. ['input' => [
  1697. 'type' => 'hidden',
  1698. 'name' => '_Token[fields]',
  1699. 'value' => $hash,
  1700. 'autocomplete' => 'off',
  1701. 'data-foo' => 'bar',
  1702. ]],
  1703. ['input' => [
  1704. 'type' => 'hidden',
  1705. 'name' => '_Token[unlocked]',
  1706. 'value' => 'address%7Cfirst_name',
  1707. 'autocomplete' => 'off',
  1708. 'data-foo' => 'bar',
  1709. ]],
  1710. ['input' => [
  1711. 'type' => 'hidden', 'name' => '_Token[debug]',
  1712. 'value' => $tokenDebug,
  1713. 'autocomplete' => 'off',
  1714. 'data-foo' => 'bar'
  1715. ]],
  1716. '/div'
  1717. ];
  1718. $this->assertHtml($expected, $result);
  1719. }
  1720. /**
  1721. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1722. *
  1723. * Test single record form with debugSecurity param.
  1724. *
  1725. * @return void
  1726. */
  1727. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1728. {
  1729. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1730. 'unlockedFields' => ['first_name', 'address']
  1731. ]));
  1732. $this->Form->create();
  1733. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1734. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1735. $this->Form->text('Addresses.title');
  1736. $this->Form->text('Addresses.first_name');
  1737. $this->Form->text('Addresses.last_name');
  1738. $this->Form->text('Addresses.address');
  1739. $this->Form->text('Addresses.city');
  1740. $this->Form->text('Addresses.phone');
  1741. $result = $this->Form->fields;
  1742. $expected = [
  1743. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1744. 'Addresses.city', 'Addresses.phone'
  1745. ];
  1746. $this->assertEquals($expected, $result);
  1747. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1748. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1749. $tokenDebug = urlencode(json_encode([
  1750. '/articles/add',
  1751. [
  1752. 'Addresses.id' => '123456',
  1753. 'Addresses.title',
  1754. 'Addresses.last_name',
  1755. 'Addresses.city',
  1756. 'Addresses.phone'
  1757. ],
  1758. [
  1759. 'first_name',
  1760. 'address'
  1761. ]
  1762. ]));
  1763. $expected = [
  1764. 'div' => ['style' => 'display:none;'],
  1765. ['input' => [
  1766. 'type' => 'hidden',
  1767. 'name' => '_Token[fields]',
  1768. 'value' => $hash,
  1769. 'autocomplete' => 'off',
  1770. 'data-foo' => 'bar',
  1771. ]],
  1772. ['input' => [
  1773. 'type' => 'hidden',
  1774. 'name' => '_Token[unlocked]',
  1775. 'value' => 'address%7Cfirst_name',
  1776. 'autocomplete' => 'off',
  1777. 'data-foo' => 'bar',
  1778. ]],
  1779. ['input' => [
  1780. 'type' => 'hidden', 'name' => '_Token[debug]',
  1781. 'value' => $tokenDebug,
  1782. 'autocomplete' => 'off',
  1783. 'data-foo' => 'bar'
  1784. ]],
  1785. '/div'
  1786. ];
  1787. $this->assertHtml($expected, $result);
  1788. }
  1789. /**
  1790. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1791. *
  1792. * Debug is false, debugSecurity is true -> no debug
  1793. *
  1794. * @return void
  1795. */
  1796. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1797. {
  1798. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1799. 'unlockedFields' => ['first_name', 'address']
  1800. ]));
  1801. $this->Form->create();
  1802. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1803. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1804. $this->Form->text('Addresses.title');
  1805. $this->Form->text('Addresses.first_name');
  1806. $this->Form->text('Addresses.last_name');
  1807. $this->Form->text('Addresses.address');
  1808. $this->Form->text('Addresses.city');
  1809. $this->Form->text('Addresses.phone');
  1810. $result = $this->Form->fields;
  1811. $expected = [
  1812. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1813. 'Addresses.city', 'Addresses.phone'
  1814. ];
  1815. $this->assertEquals($expected, $result);
  1816. Configure::write('debug', false);
  1817. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1818. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1819. $expected = [
  1820. 'div' => ['style' => 'display:none;'],
  1821. ['input' => [
  1822. 'type' => 'hidden',
  1823. 'name' => '_Token[fields]',
  1824. 'value' => $hash,
  1825. 'autocomplete' => 'off',
  1826. 'data-foo' => 'bar',
  1827. ]],
  1828. ['input' => [
  1829. 'type' => 'hidden',
  1830. 'name' => '_Token[unlocked]',
  1831. 'value' => 'address%7Cfirst_name',
  1832. 'autocomplete' => 'off',
  1833. 'data-foo' => 'bar',
  1834. ]],
  1835. '/div'
  1836. ];
  1837. $this->assertHtml($expected, $result);
  1838. }
  1839. /**
  1840. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1841. *
  1842. * Test single record form with debugSecurity param.
  1843. *
  1844. * @return void
  1845. */
  1846. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1847. {
  1848. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1849. 'unlockedFields' => ['first_name', 'address']
  1850. ]));
  1851. $this->Form->create();
  1852. $this->assertEquals(
  1853. $this->View->getRequest()->getParam('_Token.unlockedFields'),
  1854. $this->Form->unlockField()
  1855. );
  1856. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1857. $this->Form->text('Addresses.title');
  1858. $this->Form->text('Addresses.first_name');
  1859. $this->Form->text('Addresses.last_name');
  1860. $this->Form->text('Addresses.address');
  1861. $this->Form->text('Addresses.city');
  1862. $this->Form->text('Addresses.phone');
  1863. $result = $this->Form->fields;
  1864. $expected = [
  1865. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1866. 'Addresses.city', 'Addresses.phone'
  1867. ];
  1868. $this->assertEquals($expected, $result);
  1869. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1870. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1871. $expected = [
  1872. 'div' => ['style' => 'display:none;'],
  1873. ['input' => [
  1874. 'type' => 'hidden',
  1875. 'name' => '_Token[fields]',
  1876. 'value' => $hash,
  1877. 'autocomplete' => 'off',
  1878. 'data-foo' => 'bar',
  1879. ]],
  1880. ['input' => [
  1881. 'type' => 'hidden',
  1882. 'name' => '_Token[unlocked]',
  1883. 'value' => 'address%7Cfirst_name',
  1884. 'autocomplete' => 'off',
  1885. 'data-foo' => 'bar',
  1886. ]],
  1887. '/div'
  1888. ];
  1889. $this->assertHtml($expected, $result);
  1890. }
  1891. /**
  1892. * testFormSecureWithCustomNameAttribute method
  1893. *
  1894. * Test securing inputs with custom name attributes.
  1895. *
  1896. * @return void
  1897. */
  1898. public function testFormSecureWithCustomNameAttribute()
  1899. {
  1900. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1901. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1902. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1903. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1904. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1905. }
  1906. /**
  1907. * testFormSecuredControl method
  1908. *
  1909. * Test generation of entire secure form, assertions made on control() output.
  1910. *
  1911. * @return void
  1912. */
  1913. public function testFormSecuredControl()
  1914. {
  1915. $this->View->setRequest($this->View->getRequest()
  1916. ->withParam('_Token', 'stuff')
  1917. ->withParam('_csrfToken', 'testKey'));
  1918. $this->article['schema'] = [
  1919. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1920. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1921. ];
  1922. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1923. $encoding = strtolower(Configure::read('App.encoding'));
  1924. $expected = [
  1925. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1926. 'div' => ['style' => 'display:none;'],
  1927. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1928. ['input' => [
  1929. 'type' => 'hidden',
  1930. 'name' => '_csrfToken',
  1931. 'value' => 'testKey',
  1932. 'autocomplete' => 'off'
  1933. ]],
  1934. '/div'
  1935. ];
  1936. $this->assertHtml($expected, $result);
  1937. $result = $this->Form->control('ratio');
  1938. $expected = [
  1939. 'div' => ['class'],
  1940. 'label' => ['for'],
  1941. 'Ratio',
  1942. '/label',
  1943. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1944. '/div',
  1945. ];
  1946. $this->assertHtml($expected, $result);
  1947. $result = $this->Form->control('population');
  1948. $expected = [
  1949. 'div' => ['class'],
  1950. 'label' => ['for'],
  1951. 'Population',
  1952. '/label',
  1953. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1954. '/div',
  1955. ];
  1956. $this->assertHtml($expected, $result);
  1957. $result = $this->Form->control('published', ['type' => 'text']);
  1958. $expected = [
  1959. 'div' => ['class' => 'input text'],
  1960. 'label' => ['for' => 'published'],
  1961. 'Published',
  1962. '/label',
  1963. ['input' => [
  1964. 'type' => 'text',
  1965. 'name' => 'published',
  1966. 'id' => 'published'
  1967. ]],
  1968. '/div'
  1969. ];
  1970. $this->assertHtml($expected, $result);
  1971. $result = $this->Form->control('other', ['type' => 'text']);
  1972. $expected = [
  1973. 'div' => ['class' => 'input text'],
  1974. 'label' => ['for' => 'other'],
  1975. 'Other',
  1976. '/label',
  1977. ['input' => [
  1978. 'type' => 'text',
  1979. 'name' => 'other',
  1980. 'id',
  1981. ]],
  1982. '/div'
  1983. ];
  1984. $this->assertHtml($expected, $result);
  1985. $result = $this->Form->hidden('stuff');
  1986. $expected = [
  1987. 'input' => [
  1988. 'type' => 'hidden',
  1989. 'name' => 'stuff'
  1990. ]
  1991. ];
  1992. $this->assertHtml($expected, $result);
  1993. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1994. $expected = ['input' => [
  1995. 'type' => 'hidden',
  1996. 'name' => 'hidden',
  1997. 'value' => '0'
  1998. ]];
  1999. $this->assertHtml($expected, $result);
  2000. $result = $this->Form->control('something', ['type' => 'checkbox']);
  2001. $expected = [
  2002. 'div' => ['class' => 'input checkbox'],
  2003. ['input' => [
  2004. 'type' => 'hidden',
  2005. 'name' => 'something',
  2006. 'value' => '0'
  2007. ]],
  2008. 'label' => ['for' => 'something'],
  2009. ['input' => [
  2010. 'type' => 'checkbox',
  2011. 'name' => 'something',
  2012. 'value' => '1',
  2013. 'id' => 'something'
  2014. ]],
  2015. 'Something',
  2016. '/label',
  2017. '/div'
  2018. ];
  2019. $this->assertHtml($expected, $result);
  2020. $result = $this->Form->fields;
  2021. $expectedFields = [
  2022. 'ratio',
  2023. 'population',
  2024. 'published',
  2025. 'other',
  2026. 'stuff' => '',
  2027. 'hidden' => '0',
  2028. 'something'
  2029. ];
  2030. $this->assertEquals($expectedFields, $result);
  2031. $result = $this->Form->secure($this->Form->fields);
  2032. $tokenDebug = urlencode(json_encode([
  2033. '/articles/add',
  2034. $expectedFields,
  2035. []
  2036. ]));
  2037. $expected = [
  2038. 'div' => ['style' => 'display:none;'],
  2039. ['input' => [
  2040. 'type' => 'hidden',
  2041. 'name' => '_Token[fields]',
  2042. 'value',
  2043. 'autocomplete'
  2044. ]],
  2045. ['input' => [
  2046. 'type' => 'hidden',
  2047. 'name' => '_Token[unlocked]',
  2048. 'value' => '',
  2049. 'autocomplete' => 'off'
  2050. ]],
  2051. ['input' => [
  2052. 'type' => 'hidden', 'name' => '_Token[debug]',
  2053. 'value' => $tokenDebug,
  2054. 'autocomplete' => 'off'
  2055. ]],
  2056. '/div'
  2057. ];
  2058. $this->assertHtml($expected, $result);
  2059. }
  2060. /**
  2061. * testSecuredControlCustomName method
  2062. *
  2063. * Test secured inputs with custom names.
  2064. *
  2065. * @return void
  2066. */
  2067. public function testSecuredControlCustomName()
  2068. {
  2069. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2070. $this->assertEquals([], $this->Form->fields);
  2071. $this->Form->text('text_input', [
  2072. 'name' => 'Option[General.default_role]',
  2073. ]);
  2074. $expected = ['Option.General.default_role'];
  2075. $this->assertEquals($expected, $this->Form->fields);
  2076. $this->Form->select('select_box', [1, 2], [
  2077. 'name' => 'Option[General.select_role]',
  2078. ]);
  2079. $expected[] = 'Option.General.select_role';
  2080. $this->assertEquals($expected, $this->Form->fields);
  2081. $this->Form->text('other.things[]');
  2082. $expected[] = 'other.things';
  2083. $this->assertEquals($expected, $this->Form->fields);
  2084. }
  2085. /**
  2086. * testSecuredControlDuplicate method
  2087. *
  2088. * Test that a hidden field followed by a visible field
  2089. * undoes the hidden field locking.
  2090. *
  2091. * @return void
  2092. */
  2093. public function testSecuredControlDuplicate()
  2094. {
  2095. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2096. $this->assertEquals([], $this->Form->fields);
  2097. $this->Form->control('text_val', [
  2098. 'type' => 'hidden',
  2099. 'value' => 'some text',
  2100. ]);
  2101. $expected = ['text_val' => 'some text'];
  2102. $this->assertEquals($expected, $this->Form->fields);
  2103. $this->Form->control('text_val', [
  2104. 'type' => 'text',
  2105. ]);
  2106. $expected = ['text_val'];
  2107. $this->assertEquals($expected, $this->Form->fields);
  2108. }
  2109. /**
  2110. * testFormSecuredFileControl method
  2111. *
  2112. * Tests that the correct keys are added to the field hash index.
  2113. *
  2114. * @return void
  2115. */
  2116. public function testFormSecuredFileControl()
  2117. {
  2118. $this->assertEquals([], $this->Form->fields);
  2119. $this->Form->file('Attachment.file');
  2120. $expected = [
  2121. 'Attachment.file.name', 'Attachment.file.type',
  2122. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2123. 'Attachment.file.size'
  2124. ];
  2125. $this->assertEquals($expected, $this->Form->fields);
  2126. }
  2127. /**
  2128. * testFormSecuredMultipleSelect method
  2129. *
  2130. * Test that multiple selects keys are added to field hash.
  2131. *
  2132. * @return void
  2133. */
  2134. public function testFormSecuredMultipleSelect()
  2135. {
  2136. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  2137. $this->assertEquals([], $this->Form->fields);
  2138. $options = ['1' => 'one', '2' => 'two'];
  2139. $this->Form->select('Model.select', $options);
  2140. $expected = ['Model.select'];
  2141. $this->assertEquals($expected, $this->Form->fields);
  2142. $this->Form->fields = [];
  2143. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2144. $this->assertEquals($expected, $this->Form->fields);
  2145. }
  2146. /**
  2147. * testFormSecuredRadio method
  2148. *
  2149. * @return void
  2150. */
  2151. public function testFormSecuredRadio()
  2152. {
  2153. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2154. $this->assertEquals([], $this->Form->fields);
  2155. $options = ['1' => 'option1', '2' => 'option2'];
  2156. $this->Form->radio('Test.test', $options);
  2157. $expected = ['Test.test'];
  2158. $this->assertEquals($expected, $this->Form->fields);
  2159. $this->Form->radio('Test.all', $options, [
  2160. 'disabled' => ['option1', 'option2']
  2161. ]);
  2162. $expected = ['Test.test', 'Test.all' => ''];
  2163. $this->assertEquals($expected, $this->Form->fields);
  2164. $this->Form->radio('Test.some', $options, [
  2165. 'disabled' => ['option1']
  2166. ]);
  2167. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2168. $this->assertEquals($expected, $this->Form->fields);
  2169. }
  2170. /**
  2171. * testFormSecuredAndDisabledNotAssoc method
  2172. *
  2173. * Test that when disabled is in a list based attribute array it works.
  2174. *
  2175. * @return void
  2176. */
  2177. public function testFormSecuredAndDisabledNotAssoc()
  2178. {
  2179. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2180. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2181. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2182. $this->Form->text('Model.text', ['disabled']);
  2183. $this->Form->textarea('Model.textarea', ['disabled']);
  2184. $this->Form->password('Model.password', ['disabled']);
  2185. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2186. $expected = [
  2187. 'Model.radio' => ''
  2188. ];
  2189. $this->assertEquals($expected, $this->Form->fields);
  2190. }
  2191. /**
  2192. * testFormSecuredAndDisabled method
  2193. *
  2194. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2195. * hashing.
  2196. *
  2197. * @return void
  2198. */
  2199. public function testFormSecuredAndDisabled()
  2200. {
  2201. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2202. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2203. $this->Form->text('Model.text', ['disabled' => true]);
  2204. $this->Form->password('Model.text', ['disabled' => true]);
  2205. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2206. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2207. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2208. $this->Form->year('Model.year', ['disabled' => true]);
  2209. $this->Form->month('Model.month', ['disabled' => true]);
  2210. $this->Form->day('Model.day', ['disabled' => true]);
  2211. $this->Form->hour('Model.hour', ['disabled' => true]);
  2212. $this->Form->minute('Model.minute', ['disabled' => true]);
  2213. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2214. $expected = [
  2215. 'Model.radio' => ''
  2216. ];
  2217. $this->assertEquals($expected, $this->Form->fields);
  2218. }
  2219. /**
  2220. * testDisableSecurityUsingForm method
  2221. *
  2222. * @return void
  2223. */
  2224. public function testDisableSecurityUsingForm()
  2225. {
  2226. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2227. 'disabledFields' => []
  2228. ]));
  2229. $this->Form->create();
  2230. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2231. $this->Form->text('Addresses.title');
  2232. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2233. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2234. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2235. $result = $this->Form->fields;
  2236. $expected = [
  2237. 'Addresses.id' => '123456', 'Addresses.title',
  2238. ];
  2239. $this->assertEquals($expected, $result);
  2240. }
  2241. /**
  2242. * testUnlockFieldAddsToList method
  2243. *
  2244. * Test disableField.
  2245. *
  2246. * @return void
  2247. */
  2248. public function testUnlockFieldAddsToList()
  2249. {
  2250. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2251. 'unlockedFields' => []
  2252. ]));
  2253. $this->Form->unlockField('Contact.name');
  2254. $this->Form->text('Contact.name');
  2255. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2256. $this->assertEquals([], $this->Form->fields);
  2257. }
  2258. /**
  2259. * testUnlockFieldRemovingFromFields method
  2260. *
  2261. * Test unlockField removing from fields array.
  2262. *
  2263. * @return void
  2264. */
  2265. public function testUnlockFieldRemovingFromFields()
  2266. {
  2267. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2268. 'unlockedFields' => []
  2269. ]));
  2270. $this->Form->create($this->article);
  2271. $this->Form->hidden('Article.id', ['value' => 1]);
  2272. $this->Form->text('Article.title');
  2273. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2274. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2275. $this->Form->unlockField('Article.title');
  2276. $this->Form->unlockField('Article.id');
  2277. $this->assertEquals([], $this->Form->fields);
  2278. }
  2279. /**
  2280. * testResetUnlockFields method
  2281. *
  2282. * Test reset unlockFields, when create new form.
  2283. *
  2284. * @return void
  2285. */
  2286. public function testResetUnlockFields()
  2287. {
  2288. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2289. 'key' => 'testKey',
  2290. 'unlockedFields' => []
  2291. ]));
  2292. $this->Form->unlockField('Contact.id');
  2293. $this->Form->create('Contact');
  2294. $this->Form->hidden('Contact.id', ['value' => 1]);
  2295. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2296. $this->Form->end();
  2297. $this->Form->create('Contact');
  2298. $this->Form->hidden('Contact.id', ['value' => 1]);
  2299. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2300. }
  2301. /**
  2302. * testSecuredFormUrlIgnoresHost method
  2303. *
  2304. * Test that only the path + query elements of a form's URL show up in their hash.
  2305. *
  2306. * @return void
  2307. */
  2308. public function testSecuredFormUrlIgnoresHost()
  2309. {
  2310. $this->View->setRequest($this->View->getRequest()->withParam('_Token', ['key' => 'testKey']));
  2311. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2312. $this->Form->create($this->article, [
  2313. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2314. ]);
  2315. $result = $this->Form->secure();
  2316. $this->assertContains($expected, $result);
  2317. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2318. $result = $this->Form->secure();
  2319. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2320. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2321. $result = $this->Form->secure();
  2322. $this->assertContains($expected, $result, 'URL path + query should work.');
  2323. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2324. $result = $this->Form->secure();
  2325. $this->assertNotContains($expected, $result, 'URL is different');
  2326. }
  2327. /**
  2328. * testSecuredFormUrlHasHtmlAndIdentifier method
  2329. *
  2330. * Test that URL, HTML and identifier show up in their hashes.
  2331. *
  2332. * @return void
  2333. */
  2334. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2335. {
  2336. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2337. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2338. $this->Form->create($this->article, [
  2339. 'url' => [
  2340. 'controller' => 'articles',
  2341. 'action' => 'view',
  2342. '?' => [
  2343. 'page' => 1,
  2344. 'limit' => 10,
  2345. 'html' => '<>"',
  2346. ],
  2347. '#' => 'result',
  2348. ],
  2349. ]);
  2350. $result = $this->Form->secure();
  2351. $this->assertContains($expected, $result);
  2352. $this->Form->create($this->article, [
  2353. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2354. ]);
  2355. $result = $this->Form->secure();
  2356. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2357. $this->Form->create($this->article, [
  2358. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2359. ]);
  2360. $result = $this->Form->secure();
  2361. $this->assertContains($expected, $result, 'URL path + query should work.');
  2362. }
  2363. /**
  2364. * testErrorMessageDisplay method
  2365. *
  2366. * Test error message display.
  2367. *
  2368. * @return void
  2369. */
  2370. public function testErrorMessageDisplay()
  2371. {
  2372. $this->article['errors'] = [
  2373. 'Article' => [
  2374. 'title' => 'error message',
  2375. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2376. ]
  2377. ];
  2378. $this->Form->create($this->article);
  2379. $result = $this->Form->control('Article.title');
  2380. $expected = [
  2381. 'div' => ['class' => 'input text error'],
  2382. 'label' => ['for' => 'article-title'],
  2383. 'Title',
  2384. '/label',
  2385. 'input' => [
  2386. 'type' => 'text', 'name' => 'Article[title]',
  2387. 'id' => 'article-title', 'class' => 'form-error'
  2388. ],
  2389. ['div' => ['class' => 'error-message']],
  2390. 'error message',
  2391. '/div',
  2392. '/div'
  2393. ];
  2394. $this->assertHtml($expected, $result);
  2395. $result = $this->Form->control('Article.title', [
  2396. 'templates' => [
  2397. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2398. ]
  2399. ]);
  2400. $expected = [
  2401. 'div' => ['class' => 'input text error'],
  2402. 'label' => ['for' => 'article-title'],
  2403. 'Title',
  2404. '/label',
  2405. 'input' => [
  2406. 'type' => 'text', 'name' => 'Article[title]',
  2407. 'id' => 'article-title', 'class' => 'form-error'
  2408. ],
  2409. '/div'
  2410. ];
  2411. $this->assertHtml($expected, $result);
  2412. $result = $this->Form->control('Article.content');
  2413. $expected = [
  2414. 'div' => ['class' => 'input text error'],
  2415. 'label' => ['for' => 'article-content'],
  2416. 'Content',
  2417. '/label',
  2418. 'input' => [
  2419. 'type' => 'text', 'name' => 'Article[content]',
  2420. 'id' => 'article-content', 'class' => 'form-error'
  2421. ],
  2422. ['div' => ['class' => 'error-message']],
  2423. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2424. '/div',
  2425. '/div'
  2426. ];
  2427. $this->assertHtml($expected, $result);
  2428. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2429. $expected = [
  2430. 'div' => ['class' => 'input text error'],
  2431. 'label' => ['for' => 'article-content'],
  2432. 'Content',
  2433. '/label',
  2434. 'input' => [
  2435. 'type' => 'text', 'name' => 'Article[content]',
  2436. 'id' => 'article-content', 'class' => 'form-error'
  2437. ],
  2438. ['div' => ['class' => 'error-message']],
  2439. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2440. '/div',
  2441. '/div'
  2442. ];
  2443. $this->assertHtml($expected, $result);
  2444. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2445. $expected = [
  2446. 'div' => ['class' => 'input text error'],
  2447. 'label' => ['for' => 'article-content'],
  2448. 'Content',
  2449. '/label',
  2450. 'input' => [
  2451. 'type' => 'text', 'name' => 'Article[content]',
  2452. 'id' => 'article-content', 'class' => 'form-error'
  2453. ],
  2454. ['div' => ['class' => 'error-message']],
  2455. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2456. '/div',
  2457. '/div'
  2458. ];
  2459. $this->assertHtml($expected, $result);
  2460. }
  2461. /**
  2462. * testEmptyErrorValidation method
  2463. *
  2464. * Test validation errors, when validation message is an empty string.
  2465. *
  2466. * @return void
  2467. */
  2468. public function testEmptyErrorValidation()
  2469. {
  2470. $this->article['errors'] = [
  2471. 'Article' => ['title' => '']
  2472. ];
  2473. $this->Form->create($this->article);
  2474. $result = $this->Form->control('Article.title');
  2475. $expected = [
  2476. 'div' => ['class' => 'input text error'],
  2477. 'label' => ['for' => 'article-title'],
  2478. 'Title',
  2479. '/label',
  2480. 'input' => [
  2481. 'type' => 'text', 'name' => 'Article[title]',
  2482. 'id' => 'article-title', 'class' => 'form-error'
  2483. ],
  2484. ['div' => ['class' => 'error-message']],
  2485. [],
  2486. '/div',
  2487. '/div'
  2488. ];
  2489. $this->assertHtml($expected, $result);
  2490. }
  2491. /**
  2492. * testEmptyControlErrorValidation method
  2493. *
  2494. * Test validation errors, when calling control() overriding validation message by an empty string.
  2495. *
  2496. * @return void
  2497. */
  2498. public function testEmptyControlErrorValidation()
  2499. {
  2500. $this->article['errors'] = [
  2501. 'Article' => ['title' => 'error message']
  2502. ];
  2503. $this->Form->create($this->article);
  2504. $result = $this->Form->control('Article.title', ['error' => '']);
  2505. $expected = [
  2506. 'div' => ['class' => 'input text error'],
  2507. 'label' => ['for' => 'article-title'],
  2508. 'Title',
  2509. '/label',
  2510. 'input' => [
  2511. 'type' => 'text', 'name' => 'Article[title]',
  2512. 'id' => 'article-title', 'class' => 'form-error'
  2513. ],
  2514. ['div' => ['class' => 'error-message']],
  2515. [],
  2516. '/div',
  2517. '/div'
  2518. ];
  2519. $this->assertHtml($expected, $result);
  2520. }
  2521. /**
  2522. * testControlErrorMessage method
  2523. *
  2524. * Test validation errors, when calling control() overriding validation messages.
  2525. *
  2526. * @return void
  2527. */
  2528. public function testControlErrorMessage()
  2529. {
  2530. $this->article['errors'] = [
  2531. 'title' => ['error message']
  2532. ];
  2533. $this->Form->create($this->article);
  2534. $result = $this->Form->control('title', [
  2535. 'error' => 'Custom error!'
  2536. ]);
  2537. $expected = [
  2538. 'div' => ['class' => 'input text required error'],
  2539. 'label' => ['for' => 'title'],
  2540. 'Title',
  2541. '/label',
  2542. 'input' => [
  2543. 'type' => 'text', 'name' => 'title',
  2544. 'id' => 'title', 'class' => 'form-error',
  2545. 'required' => 'required',
  2546. ],
  2547. ['div' => ['class' => 'error-message']],
  2548. 'Custom error!',
  2549. '/div',
  2550. '/div'
  2551. ];
  2552. $this->assertHtml($expected, $result);
  2553. $result = $this->Form->control('title', [
  2554. 'error' => ['error message' => 'Custom error!']
  2555. ]);
  2556. $expected = [
  2557. 'div' => ['class' => 'input text required error'],
  2558. 'label' => ['for' => 'title'],
  2559. 'Title',
  2560. '/label',
  2561. 'input' => [
  2562. 'type' => 'text',
  2563. 'name' => 'title',
  2564. 'id' => 'title',
  2565. 'class' => 'form-error',
  2566. 'required' => 'required'
  2567. ],
  2568. ['div' => ['class' => 'error-message']],
  2569. 'Custom error!',
  2570. '/div',
  2571. '/div'
  2572. ];
  2573. $this->assertHtml($expected, $result);
  2574. }
  2575. /**
  2576. * testFormValidationAssociated method
  2577. *
  2578. * Tests displaying errors for nested entities.
  2579. *
  2580. * @return void
  2581. */
  2582. public function testFormValidationAssociated()
  2583. {
  2584. $nested = new Entity(['foo' => 'bar']);
  2585. $nested->setError('foo', ['not a valid bar']);
  2586. $entity = new Entity(['nested' => $nested]);
  2587. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2588. $result = $this->Form->error('nested.foo');
  2589. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2590. }
  2591. /**
  2592. * testFormValidationAssociatedSecondLevel method
  2593. *
  2594. * Test form error display with associated model.
  2595. *
  2596. * @return void
  2597. */
  2598. public function testFormValidationAssociatedSecondLevel()
  2599. {
  2600. $inner = new Entity(['bar' => 'baz']);
  2601. $nested = new Entity(['foo' => $inner]);
  2602. $entity = new Entity(['nested' => $nested]);
  2603. $inner->setError('bar', ['not a valid one']);
  2604. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2605. $result = $this->Form->error('nested.foo.bar');
  2606. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2607. }
  2608. /**
  2609. * testFormValidationMultiRecord method
  2610. *
  2611. * Test form error display with multiple records.
  2612. *
  2613. * @return void
  2614. */
  2615. public function testFormValidationMultiRecord()
  2616. {
  2617. $one = new Entity();
  2618. $two = new Entity();
  2619. $this->getTableLocator()->get('Contacts', [
  2620. 'className' => __NAMESPACE__ . '\ContactsTable'
  2621. ]);
  2622. $one->set('email', '');
  2623. $one->setError('email', ['invalid email']);
  2624. $two->set('name', '');
  2625. $two->setError('name', ['This is wrong']);
  2626. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2627. $result = $this->Form->control('0.email');
  2628. $expected = [
  2629. 'div' => ['class' => 'input email error'],
  2630. 'label' => ['for' => '0-email'],
  2631. 'Email',
  2632. '/label',
  2633. 'input' => [
  2634. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2635. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2636. ],
  2637. ['div' => ['class' => 'error-message']],
  2638. 'invalid email',
  2639. '/div',
  2640. '/div'
  2641. ];
  2642. $this->assertHtml($expected, $result);
  2643. $result = $this->Form->control('1.name');
  2644. $expected = [
  2645. 'div' => ['class' => 'input text error'],
  2646. 'label' => ['for' => '1-name'],
  2647. 'Name',
  2648. '/label',
  2649. 'input' => [
  2650. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2651. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2652. ],
  2653. ['div' => ['class' => 'error-message']],
  2654. 'This is wrong',
  2655. '/div',
  2656. '/div'
  2657. ];
  2658. $this->assertHtml($expected, $result);
  2659. }
  2660. /**
  2661. * testControl method
  2662. *
  2663. * Test various incarnations of control().
  2664. *
  2665. * @return void
  2666. */
  2667. public function testControl()
  2668. {
  2669. $this->getTableLocator()->get('ValidateUsers', [
  2670. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2671. ]);
  2672. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2673. $result = $this->Form->control('ValidateUsers.balance');
  2674. $expected = [
  2675. 'div' => ['class'],
  2676. 'label' => ['for'],
  2677. 'Balance',
  2678. '/label',
  2679. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2680. '/div',
  2681. ];
  2682. $this->assertHtml($expected, $result);
  2683. $result = $this->Form->control('ValidateUser.cost_decimal');
  2684. $expected = [
  2685. 'div' => ['class'],
  2686. 'label' => ['for'],
  2687. 'Cost Decimal',
  2688. '/label',
  2689. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2690. '/div',
  2691. ];
  2692. $this->assertHtml($expected, $result);
  2693. $result = $this->Form->control('ValidateUser.null_decimal');
  2694. $expected = [
  2695. 'div' => ['class'],
  2696. 'label' => ['for'],
  2697. 'Null Decimal',
  2698. '/label',
  2699. 'input' => ['name', 'type' => 'number', 'id'],
  2700. '/div',
  2701. ];
  2702. $this->assertHtml($expected, $result);
  2703. }
  2704. /**
  2705. * testControlCustomization method
  2706. *
  2707. * Tests the input method and passing custom options.
  2708. *
  2709. * @return void
  2710. */
  2711. public function testControlCustomization()
  2712. {
  2713. $this->getTableLocator()->get('Contacts', [
  2714. 'className' => __NAMESPACE__ . '\ContactsTable'
  2715. ]);
  2716. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2717. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2718. $expected = [
  2719. 'div' => ['class' => 'input email'],
  2720. 'label' => ['for' => 'custom'],
  2721. 'Email',
  2722. '/label',
  2723. ['input' => [
  2724. 'type' => 'email', 'name' => 'Contact[email]',
  2725. 'id' => 'custom', 'maxlength' => 255
  2726. ]],
  2727. '/div'
  2728. ];
  2729. $this->assertHtml($expected, $result);
  2730. $result = $this->Form->control('Contact.email', [
  2731. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2732. ]);
  2733. $expected = [
  2734. '<div',
  2735. 'label' => ['for' => 'contact-email'],
  2736. 'Email',
  2737. '/label',
  2738. ['input' => [
  2739. 'type' => 'email', 'name' => 'Contact[email]',
  2740. 'id' => 'contact-email', 'maxlength' => 255
  2741. ]],
  2742. '/div'
  2743. ];
  2744. $this->assertHtml($expected, $result);
  2745. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2746. $expected = [
  2747. 'div' => ['class' => 'input text'],
  2748. 'label' => ['for' => 'contact-email'],
  2749. 'Email',
  2750. '/label',
  2751. ['input' => [
  2752. 'type' => 'text', 'name' => 'Contact[email]',
  2753. 'id' => 'contact-email', 'maxlength' => '255'
  2754. ]],
  2755. '/div'
  2756. ];
  2757. $this->assertHtml($expected, $result);
  2758. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2759. $expected = [
  2760. 'div' => ['class' => 'input text'],
  2761. 'label' => ['for' => 'contact-5-email'],
  2762. 'Email',
  2763. '/label',
  2764. ['input' => [
  2765. 'type' => 'text', 'name' => 'Contact[5][email]',
  2766. 'id' => 'contact-5-email', 'maxlength' => '255'
  2767. ]],
  2768. '/div'
  2769. ];
  2770. $this->assertHtml($expected, $result);
  2771. $result = $this->Form->control('Contact.password');
  2772. $expected = [
  2773. 'div' => ['class' => 'input password'],
  2774. 'label' => ['for' => 'contact-password'],
  2775. 'Password',
  2776. '/label',
  2777. ['input' => [
  2778. 'type' => 'password', 'name' => 'Contact[password]',
  2779. 'id' => 'contact-password'
  2780. ]],
  2781. '/div'
  2782. ];
  2783. $this->assertHtml($expected, $result);
  2784. $result = $this->Form->control('Contact.email', [
  2785. 'type' => 'file', 'class' => 'textbox'
  2786. ]);
  2787. $expected = [
  2788. 'div' => ['class' => 'input file'],
  2789. 'label' => ['for' => 'contact-email'],
  2790. 'Email',
  2791. '/label',
  2792. ['input' => [
  2793. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2794. 'id' => 'contact-email'
  2795. ]],
  2796. '/div'
  2797. ];
  2798. $this->assertHtml($expected, $result);
  2799. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2800. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2801. $result = $this->Form->control('phone');
  2802. $expected = [
  2803. 'div' => ['class' => 'input tel'],
  2804. 'label' => ['for' => 'phone'],
  2805. 'Phone',
  2806. '/label',
  2807. ['input' => [
  2808. 'type' => 'tel', 'name' => 'phone',
  2809. 'value' => 'Hello &amp; World &gt; weird chars',
  2810. 'id' => 'phone', 'maxlength' => 255
  2811. ]],
  2812. '/div'
  2813. ];
  2814. $this->assertHtml($expected, $result);
  2815. $this->View->setRequest(
  2816. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2817. );
  2818. $this->Form->create();
  2819. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2820. $expected = [
  2821. 'div' => ['class' => 'input text'],
  2822. 'label' => ['for' => 'myId'],
  2823. 'Field',
  2824. '/label',
  2825. 'input' => [
  2826. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2827. 'value' => 'My value', 'id' => 'myId'
  2828. ],
  2829. '/div'
  2830. ];
  2831. $this->assertHtml($expected, $result);
  2832. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2833. $this->Form->create();
  2834. $entity->setError('field', 'Badness!');
  2835. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2836. $result = $this->Form->control('field');
  2837. $expected = [
  2838. 'div' => ['class' => 'input text error'],
  2839. 'label' => ['for' => 'field'],
  2840. 'Field',
  2841. '/label',
  2842. 'input' => [
  2843. 'type' => 'text', 'name' => 'field',
  2844. 'id' => 'field', 'class' => 'form-error'
  2845. ],
  2846. ['div' => ['class' => 'error-message']],
  2847. 'Badness!',
  2848. '/div',
  2849. '/div'
  2850. ];
  2851. $this->assertHtml($expected, $result);
  2852. $result = $this->Form->control('field', [
  2853. 'templates' => [
  2854. 'inputContainerError' => '{{content}}{{error}}',
  2855. 'error' => '<span class="error-message">{{content}}</span>'
  2856. ]
  2857. ]);
  2858. $expected = [
  2859. 'label' => ['for' => 'field'],
  2860. 'Field',
  2861. '/label',
  2862. 'input' => [
  2863. 'type' => 'text', 'name' => 'field',
  2864. 'id' => 'field', 'class' => 'form-error'
  2865. ],
  2866. ['span' => ['class' => 'error-message']],
  2867. 'Badness!',
  2868. '/span'
  2869. ];
  2870. $this->assertHtml($expected, $result);
  2871. $entity->setError('field', ['minLength'], true);
  2872. $result = $this->Form->control('field', [
  2873. 'error' => [
  2874. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2875. 'maxLength' => 'login too large'
  2876. ]
  2877. ]);
  2878. $expected = [
  2879. 'div' => ['class' => 'input text error'],
  2880. 'label' => ['for' => 'field'],
  2881. 'Field',
  2882. '/label',
  2883. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2884. ['div' => ['class' => 'error-message']],
  2885. 'Le login doit contenir au moins 2 caractères',
  2886. '/div',
  2887. '/div'
  2888. ];
  2889. $this->assertHtml($expected, $result);
  2890. $entity->setError('field', ['maxLength'], true);
  2891. $result = $this->Form->control('field', [
  2892. 'error' => [
  2893. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2894. 'maxLength' => 'login too large',
  2895. ]
  2896. ]);
  2897. $expected = [
  2898. 'div' => ['class' => 'input text error'],
  2899. 'label' => ['for' => 'field'],
  2900. 'Field',
  2901. '/label',
  2902. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2903. ['div' => ['class' => 'error-message']],
  2904. 'login too large',
  2905. '/div',
  2906. '/div'
  2907. ];
  2908. $this->assertHtml($expected, $result);
  2909. }
  2910. /**
  2911. * testControlWithTemplateFile method
  2912. *
  2913. * Test that control() accepts a template file.
  2914. *
  2915. * @return void
  2916. */
  2917. public function testControlWithTemplateFile()
  2918. {
  2919. $result = $this->Form->control('field', [
  2920. 'templates' => 'htmlhelper_tags'
  2921. ]);
  2922. $expected = [
  2923. 'label' => ['for' => 'field'],
  2924. 'Field',
  2925. '/label',
  2926. 'input' => [
  2927. 'type' => 'text', 'name' => 'field',
  2928. 'id' => 'field'
  2929. ],
  2930. ];
  2931. $this->assertHtml($expected, $result);
  2932. }
  2933. /**
  2934. * testNestedControlsEndWithBrackets method
  2935. *
  2936. * Test that nested inputs end with brackets.
  2937. *
  2938. * @return void
  2939. */
  2940. public function testNestedControlsEndWithBrackets()
  2941. {
  2942. $result = $this->Form->text('nested.text[]');
  2943. $expected = [
  2944. 'input' => [
  2945. 'type' => 'text', 'name' => 'nested[text][]'
  2946. ],
  2947. ];
  2948. $this->assertHtml($expected, $result);
  2949. $result = $this->Form->file('nested.file[]');
  2950. $expected = [
  2951. 'input' => [
  2952. 'type' => 'file', 'name' => 'nested[file][]'
  2953. ],
  2954. ];
  2955. $this->assertHtml($expected, $result);
  2956. }
  2957. /**
  2958. * testCreateIdPrefix method
  2959. *
  2960. * Test id prefix.
  2961. *
  2962. * @return void
  2963. */
  2964. public function testCreateIdPrefix()
  2965. {
  2966. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2967. $result = $this->Form->control('field');
  2968. $expected = [
  2969. 'div' => ['class' => 'input text'],
  2970. 'label' => ['for' => 'prefix-field'],
  2971. 'Field',
  2972. '/label',
  2973. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2974. '/div'
  2975. ];
  2976. $this->assertHtml($expected, $result);
  2977. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2978. $expected = [
  2979. 'div' => ['class' => 'input text'],
  2980. 'label' => ['for' => 'custom-id'],
  2981. 'Field',
  2982. '/label',
  2983. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2984. '/div'
  2985. ];
  2986. $this->assertHtml($expected, $result);
  2987. $result = $this->Form->radio('Model.field', ['option A']);
  2988. $expected = [
  2989. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2990. 'label' => ['for' => 'prefix-model-field-0'],
  2991. ['input' => [
  2992. 'type' => 'radio',
  2993. 'name' => 'Model[field]',
  2994. 'value' => '0',
  2995. 'id' => 'prefix-model-field-0'
  2996. ]],
  2997. 'option A',
  2998. '/label'
  2999. ];
  3000. $this->assertHtml($expected, $result);
  3001. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  3002. $expected = [
  3003. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3004. 'label' => ['for' => 'prefix-model-field-0'],
  3005. ['input' => [
  3006. 'type' => 'radio',
  3007. 'name' => 'Model[field]',
  3008. 'value' => '0',
  3009. 'id' => 'prefix-model-field-0'
  3010. ]],
  3011. 'option A',
  3012. '/label'
  3013. ];
  3014. $this->assertHtml($expected, $result);
  3015. $result = $this->Form->select(
  3016. 'Model.multi_field',
  3017. ['first'],
  3018. ['multiple' => 'checkbox']
  3019. );
  3020. $expected = [
  3021. 'input' => [
  3022. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3023. ],
  3024. ['div' => ['class' => 'checkbox']],
  3025. ['label' => ['for' => 'prefix-model-multi-field-0']],
  3026. ['input' => [
  3027. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3028. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  3029. ]],
  3030. 'first',
  3031. '/label',
  3032. '/div',
  3033. ];
  3034. $this->assertHtml($expected, $result);
  3035. $this->Form->end();
  3036. $result = $this->Form->control('field');
  3037. $expected = [
  3038. 'div' => ['class' => 'input text'],
  3039. 'label' => ['for' => 'field'],
  3040. 'Field',
  3041. '/label',
  3042. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3043. '/div'
  3044. ];
  3045. $this->assertHtml($expected, $result);
  3046. }
  3047. /**
  3048. * testControlZero method
  3049. *
  3050. * Test that inputs with 0 can be created.
  3051. *
  3052. * @return void
  3053. */
  3054. public function testControlZero()
  3055. {
  3056. $this->getTableLocator()->get('Contacts', [
  3057. 'className' => __NAMESPACE__ . '\ContactsTable'
  3058. ]);
  3059. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3060. $result = $this->Form->control('0');
  3061. $expected = [
  3062. 'div' => ['class' => 'input text'],
  3063. 'label' => ['for' => '0'], '/label',
  3064. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3065. '/div'
  3066. ];
  3067. $this->assertHtml($expected, $result);
  3068. }
  3069. /**
  3070. * testControlCheckbox method
  3071. *
  3072. * Test control() with checkbox creation.
  3073. *
  3074. * @return void
  3075. */
  3076. public function testControlCheckbox()
  3077. {
  3078. $articles = $this->getTableLocator()->get('Articles');
  3079. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3080. $article = $articles->newEntity();
  3081. $this->Form->create($article);
  3082. $result = $this->Form->control('Articles.active');
  3083. $expected = [
  3084. 'div' => ['class' => 'input checkbox'],
  3085. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3086. 'label' => ['for' => 'articles-active'],
  3087. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3088. 'Active',
  3089. '/label',
  3090. '/div'
  3091. ];
  3092. $this->assertHtml($expected, $result);
  3093. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3094. $expected = [
  3095. 'div' => ['class' => 'input checkbox'],
  3096. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3097. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3098. '/div'
  3099. ];
  3100. $this->assertHtml($expected, $result);
  3101. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3102. $expected = [
  3103. 'div' => ['class' => 'input checkbox'],
  3104. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3105. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3106. '/div'
  3107. ];
  3108. $this->assertHtml($expected, $result);
  3109. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3110. $expected = [
  3111. 'div' => ['class' => 'input checkbox'],
  3112. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3113. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3114. '/div'
  3115. ];
  3116. $this->assertHtml($expected, $result);
  3117. $result = $this->Form->control('Articles.disabled', [
  3118. 'label' => 'Disabled',
  3119. 'type' => 'checkbox',
  3120. 'data-foo' => 'disabled'
  3121. ]);
  3122. $expected = [
  3123. 'div' => ['class' => 'input checkbox'],
  3124. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3125. 'label' => ['for' => 'articles-disabled'],
  3126. ['input' => [
  3127. 'type' => 'checkbox',
  3128. 'name' => 'Articles[disabled]',
  3129. 'value' => '1',
  3130. 'id' => 'articles-disabled',
  3131. 'data-foo' => 'disabled'
  3132. ]],
  3133. 'Disabled',
  3134. '/label',
  3135. '/div'
  3136. ];
  3137. $this->assertHtml($expected, $result);
  3138. $result = $this->Form->control('Articles.confirm', [
  3139. 'label' => 'Confirm <b>me</b>!',
  3140. 'type' => 'checkbox',
  3141. 'escape' => false
  3142. ]);
  3143. $expected = [
  3144. 'div' => ['class' => 'input checkbox'],
  3145. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3146. 'label' => ['for' => 'articles-confirm'],
  3147. ['input' => [
  3148. 'type' => 'checkbox',
  3149. 'name' => 'Articles[confirm]',
  3150. 'value' => '1',
  3151. 'id' => 'articles-confirm',
  3152. ]],
  3153. 'Confirm <b>me</b>!',
  3154. '/label',
  3155. '/div'
  3156. ];
  3157. $this->assertHtml($expected, $result);
  3158. }
  3159. /**
  3160. * testControlHidden method
  3161. *
  3162. * Test that control() does not create wrapping div and label tag for hidden fields.
  3163. *
  3164. * @return void
  3165. */
  3166. public function testControlHidden()
  3167. {
  3168. $this->getTableLocator()->get('ValidateUsers', [
  3169. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3170. ]);
  3171. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3172. $result = $this->Form->control('ValidateUser.id');
  3173. $expected = [
  3174. 'input' => ['name', 'type' => 'hidden', 'id']
  3175. ];
  3176. $this->assertHtml($expected, $result);
  3177. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3178. $expected = [
  3179. 'input' => ['name', 'type' => 'hidden', 'id']
  3180. ];
  3181. $this->assertHtml($expected, $result);
  3182. }
  3183. /**
  3184. * testControlDatetime method
  3185. *
  3186. * Test form->control() with datetime.
  3187. *
  3188. * @return void
  3189. */
  3190. public function testControlDatetime()
  3191. {
  3192. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3193. ->setMethods(['datetime'])
  3194. ->setConstructorArgs([new View()])
  3195. ->getMock();
  3196. $this->Form->expects($this->once())->method('datetime')
  3197. ->with('prueba', [
  3198. 'type' => 'datetime',
  3199. 'timeFormat' => 24,
  3200. 'minYear' => 2008,
  3201. 'maxYear' => 2011,
  3202. 'interval' => 15,
  3203. 'options' => null,
  3204. 'empty' => false,
  3205. 'id' => 'prueba',
  3206. 'required' => false,
  3207. 'templateVars' => []
  3208. ])
  3209. ->will($this->returnValue('This is it!'));
  3210. $result = $this->Form->control('prueba', [
  3211. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3212. 'maxYear' => 2011, 'interval' => 15
  3213. ]);
  3214. $expected = [
  3215. 'div' => ['class' => 'input datetime'],
  3216. '<label',
  3217. 'Prueba',
  3218. '/label',
  3219. 'This is it!',
  3220. '/div'
  3221. ];
  3222. $this->assertHtml($expected, $result);
  3223. }
  3224. /**
  3225. * testControlDatetimeIdPrefix method
  3226. *
  3227. * Test form->control() with datetime with id prefix.
  3228. *
  3229. * @return void
  3230. */
  3231. public function testControlDatetimeIdPrefix()
  3232. {
  3233. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3234. ->setMethods(['datetime'])
  3235. ->setConstructorArgs([new View()])
  3236. ->getMock();
  3237. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3238. $this->Form->expects($this->once())->method('datetime')
  3239. ->with('prueba', [
  3240. 'type' => 'datetime',
  3241. 'timeFormat' => 24,
  3242. 'minYear' => 2008,
  3243. 'maxYear' => 2011,
  3244. 'interval' => 15,
  3245. 'options' => null,
  3246. 'empty' => false,
  3247. 'id' => 'prefix-prueba',
  3248. 'required' => false,
  3249. 'templateVars' => []
  3250. ])
  3251. ->will($this->returnValue('This is it!'));
  3252. $result = $this->Form->control('prueba', [
  3253. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3254. 'maxYear' => 2011, 'interval' => 15
  3255. ]);
  3256. $expected = [
  3257. 'div' => ['class' => 'input datetime'],
  3258. '<label',
  3259. 'Prueba',
  3260. '/label',
  3261. 'This is it!',
  3262. '/div'
  3263. ];
  3264. $this->assertHtml($expected, $result);
  3265. }
  3266. /**
  3267. * testControlCheckboxWithDisabledElements method
  3268. *
  3269. * Test generating checkboxes with disabled elements.
  3270. *
  3271. * @return void
  3272. */
  3273. public function testControlCheckboxWithDisabledElements()
  3274. {
  3275. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3276. $result = $this->Form->control('Contact.multiple', [
  3277. 'multiple' => 'checkbox',
  3278. 'disabled' => 'disabled',
  3279. 'options' => $options
  3280. ]);
  3281. $expected = [
  3282. ['div' => ['class' => 'input select']],
  3283. ['label' => ['for' => 'contact-multiple']],
  3284. 'Multiple',
  3285. '/label',
  3286. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3287. ['div' => ['class' => 'checkbox']],
  3288. ['label' => ['for' => 'contact-multiple-1']],
  3289. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3290. 'One',
  3291. '/label',
  3292. '/div',
  3293. ['div' => ['class' => 'checkbox']],
  3294. ['label' => ['for' => 'contact-multiple-2']],
  3295. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3296. 'Two',
  3297. '/label',
  3298. '/div',
  3299. ['div' => ['class' => 'checkbox']],
  3300. ['label' => ['for' => 'contact-multiple-3']],
  3301. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3302. 'Three',
  3303. '/label',
  3304. '/div',
  3305. '/div'
  3306. ];
  3307. $this->assertHtml($expected, $result);
  3308. // make sure 50 does only disable 50, and not 50f5c0cf
  3309. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3310. $disabled = [50];
  3311. $expected = [
  3312. ['div' => ['class' => 'input select']],
  3313. ['label' => ['for' => 'contact-multiple']],
  3314. 'Multiple',
  3315. '/label',
  3316. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3317. ['div' => ['class' => 'checkbox']],
  3318. ['label' => ['for' => 'contact-multiple-50']],
  3319. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3320. 'Fifty',
  3321. '/label',
  3322. '/div',
  3323. ['div' => ['class' => 'checkbox']],
  3324. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3325. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3326. 'Stringy',
  3327. '/label',
  3328. '/div',
  3329. '/div'
  3330. ];
  3331. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3332. $this->assertHtml($expected, $result);
  3333. }
  3334. /**
  3335. * testControlWithLeadingInteger method
  3336. *
  3337. * Test input name with leading integer, ensure attributes are generated correctly.
  3338. *
  3339. * @return void
  3340. */
  3341. public function testControlWithLeadingInteger()
  3342. {
  3343. $result = $this->Form->text('0.Node.title');
  3344. $expected = [
  3345. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3346. ];
  3347. $this->assertHtml($expected, $result);
  3348. }
  3349. /**
  3350. * testControlSelectType method
  3351. *
  3352. * Test form->control() with select type inputs.
  3353. *
  3354. * @return void
  3355. */
  3356. public function testControlSelectType()
  3357. {
  3358. $result = $this->Form->control(
  3359. 'email',
  3360. [
  3361. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3362. );
  3363. $expected = [
  3364. 'div' => ['class' => 'input select'],
  3365. 'label' => ['for' => 'email'],
  3366. 'Email',
  3367. '/label',
  3368. ['select' => ['name' => 'email', 'id' => 'email']],
  3369. ['option' => ['value' => '']],
  3370. '/option',
  3371. ['option' => ['value' => 'è']],
  3372. 'Firést',
  3373. '/option',
  3374. ['option' => ['value' => 'é']],
  3375. 'Secoènd',
  3376. '/option',
  3377. '/select',
  3378. '/div'
  3379. ];
  3380. $this->assertHtml($expected, $result);
  3381. $result = $this->Form->control(
  3382. 'email',
  3383. [
  3384. 'options' => ['First', 'Second'], 'empty' => true]
  3385. );
  3386. $expected = [
  3387. 'div' => ['class' => 'input select'],
  3388. 'label' => ['for' => 'email'],
  3389. 'Email',
  3390. '/label',
  3391. ['select' => ['name' => 'email', 'id' => 'email']],
  3392. ['option' => ['value' => '']],
  3393. '/option',
  3394. ['option' => ['value' => '0']],
  3395. 'First',
  3396. '/option',
  3397. ['option' => ['value' => '1']],
  3398. 'Second',
  3399. '/option',
  3400. '/select',
  3401. '/div'
  3402. ];
  3403. $this->assertHtml($expected, $result);
  3404. $result = $this->Form->control('email', [
  3405. 'type' => 'select',
  3406. 'options' => new \ArrayObject(['First', 'Second']),
  3407. 'empty' => true
  3408. ]);
  3409. $this->assertHtml($expected, $result);
  3410. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3411. $this->View->setRequest(
  3412. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3413. );
  3414. $this->Form->create();
  3415. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3416. $expected = [
  3417. 'div' => ['class' => 'input select'],
  3418. 'label' => ['for' => 'model-user-id'],
  3419. 'User',
  3420. '/label',
  3421. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3422. ['option' => ['value' => '']],
  3423. '/option',
  3424. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3425. 'good',
  3426. '/option',
  3427. ['option' => ['value' => 'other']],
  3428. 'bad',
  3429. '/option',
  3430. '/select',
  3431. '/div'
  3432. ];
  3433. $this->assertHtml($expected, $result);
  3434. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3435. $this->View->setRequest(
  3436. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3437. );
  3438. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3439. $expected = [
  3440. 'div' => ['class' => 'input select'],
  3441. 'label' => ['for' => 'thing-user-id'],
  3442. 'User',
  3443. '/label',
  3444. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3445. ['option' => ['value' => '']],
  3446. 'Some Empty',
  3447. '/option',
  3448. ['option' => ['value' => 'value']],
  3449. 'good',
  3450. '/option',
  3451. ['option' => ['value' => 'other']],
  3452. 'bad',
  3453. '/option',
  3454. '/select',
  3455. '/div'
  3456. ];
  3457. $this->assertHtml($expected, $result);
  3458. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3459. $this->View->setRequest(
  3460. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3461. );
  3462. $this->Form->create();
  3463. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3464. $expected = [
  3465. 'div' => ['class' => 'input select'],
  3466. 'label' => ['for' => 'thing-user-id'],
  3467. 'User',
  3468. '/label',
  3469. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3470. ['option' => ['value' => '']],
  3471. 'Some Empty',
  3472. '/option',
  3473. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3474. 'good',
  3475. '/option',
  3476. ['option' => ['value' => 'other']],
  3477. 'bad',
  3478. '/option',
  3479. '/select',
  3480. '/div'
  3481. ];
  3482. $this->assertHtml($expected, $result);
  3483. $result = $this->Form->control('Publisher.id', [
  3484. 'label' => 'Publisher',
  3485. 'type' => 'select',
  3486. 'multiple' => 'checkbox',
  3487. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3488. ]);
  3489. $expected = [
  3490. ['div' => ['class' => 'input select']],
  3491. ['label' => ['for' => 'publisher-id']],
  3492. 'Publisher',
  3493. '/label',
  3494. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3495. ['div' => ['class' => 'checkbox']],
  3496. ['label' => ['for' => 'publisher-id-value-1']],
  3497. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3498. 'Label 1',
  3499. '/label',
  3500. '/div',
  3501. ['div' => ['class' => 'checkbox']],
  3502. ['label' => ['for' => 'publisher-id-value-2']],
  3503. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3504. 'Label 2',
  3505. '/label',
  3506. '/div',
  3507. '/div'
  3508. ];
  3509. $this->assertHtml($expected, $result);
  3510. }
  3511. /**
  3512. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3513. *
  3514. * Test that control() and a non standard primary key makes a hidden input by default.
  3515. *
  3516. * @return void
  3517. */
  3518. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3519. {
  3520. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3521. $this->Form->create($this->article);
  3522. $result = $this->Form->control('title');
  3523. $expected = [
  3524. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3525. ];
  3526. $this->assertHtml($expected, $result);
  3527. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3528. $this->Form->create($this->article);
  3529. $result = $this->Form->control('title');
  3530. $expected = [
  3531. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3532. ];
  3533. $this->assertHtml($expected, $result);
  3534. $result = $this->Form->control('body');
  3535. $expected = [
  3536. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3537. ];
  3538. $this->assertHtml($expected, $result);
  3539. }
  3540. /**
  3541. * testControlOverridingMagicSelectType method
  3542. *
  3543. * Test that overriding the magic select type widget is possible.
  3544. *
  3545. * @return void
  3546. */
  3547. public function testControlOverridingMagicSelectType()
  3548. {
  3549. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3550. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3551. $expected = [
  3552. 'div' => ['class' => 'input text'],
  3553. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3554. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3555. '/div'
  3556. ];
  3557. $this->assertHtml($expected, $result);
  3558. //Check that magic types still work for plural/singular vars
  3559. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3560. $result = $this->Form->control('Model.type');
  3561. $expected = [
  3562. 'div' => ['class' => 'input select'],
  3563. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3564. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3565. ['option' => ['value' => 'value']], 'good', '/option',
  3566. ['option' => ['value' => 'other']], 'bad', '/option',
  3567. '/select',
  3568. '/div'
  3569. ];
  3570. $this->assertHtml($expected, $result);
  3571. }
  3572. /**
  3573. * testControlMagicTypeDoesNotOverride method
  3574. *
  3575. * Test that inferred types do not override developer input.
  3576. *
  3577. * @return void
  3578. */
  3579. public function testControlMagicTypeDoesNotOverride()
  3580. {
  3581. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3582. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3583. $expected = [
  3584. 'div' => ['class' => 'input checkbox'],
  3585. ['input' => [
  3586. 'type' => 'hidden',
  3587. 'name' => 'Model[user]',
  3588. 'value' => 0,
  3589. ]],
  3590. 'label' => ['for' => 'model-user'],
  3591. ['input' => [
  3592. 'name' => 'Model[user]',
  3593. 'type' => 'checkbox',
  3594. 'id' => 'model-user',
  3595. 'value' => 1
  3596. ]],
  3597. 'User',
  3598. '/label',
  3599. '/div'
  3600. ];
  3601. $this->assertHtml($expected, $result);
  3602. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3603. $options = [
  3604. 1 => 'blue',
  3605. 2 => 'red'
  3606. ];
  3607. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3608. $expected = [
  3609. 'div' => ['class' => 'input select'],
  3610. 'label' => ['for' => 'tags-ids'],
  3611. 'Tags',
  3612. '/label',
  3613. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3614. ['div' => ['class' => 'checkbox']],
  3615. ['label' => ['for' => 'tags-ids-1']],
  3616. ['input' => [
  3617. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3618. 'value' => '1', 'name' => 'tags[_ids][]'
  3619. ]],
  3620. 'blue',
  3621. '/label',
  3622. '/div',
  3623. ['div' => ['class' => 'checkbox']],
  3624. ['label' => ['for' => 'tags-ids-2']],
  3625. ['input' => [
  3626. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3627. 'value' => '2', 'name' => 'tags[_ids][]'
  3628. ]],
  3629. 'red',
  3630. '/label',
  3631. '/div',
  3632. '/div'
  3633. ];
  3634. $this->assertHtml($expected, $result);
  3635. }
  3636. /**
  3637. * testControlMagicSelectForTypeNumber method
  3638. *
  3639. * Test that magic control() selects are created for type=number.
  3640. *
  3641. * @return void
  3642. */
  3643. public function testControlMagicSelectForTypeNumber()
  3644. {
  3645. $this->getTableLocator()->get('ValidateUsers', [
  3646. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3647. ]);
  3648. $entity = new Entity(['balance' => 1]);
  3649. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3650. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3651. $result = $this->Form->control('balance');
  3652. $expected = [
  3653. 'div' => ['class' => 'input select'],
  3654. 'label' => ['for' => 'balance'],
  3655. 'Balance',
  3656. '/label',
  3657. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3658. ['option' => ['value' => '0']],
  3659. 'nothing',
  3660. '/option',
  3661. ['option' => ['value' => '1', 'selected' => 'selected']],
  3662. 'some',
  3663. '/option',
  3664. ['option' => ['value' => '100']],
  3665. 'a lot',
  3666. '/option',
  3667. '/select',
  3668. '/div'
  3669. ];
  3670. $this->assertHtml($expected, $result);
  3671. }
  3672. /**
  3673. * testInvalidControlTypeOption method
  3674. *
  3675. * Test invalid 'input' type option to control() function.
  3676. *
  3677. * @return void
  3678. */
  3679. public function testInvalidControlTypeOption()
  3680. {
  3681. $this->expectException(\RuntimeException::class);
  3682. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3683. $this->Form->control('text', ['type' => 'input']);
  3684. }
  3685. /**
  3686. * testControlMagicSelectChangeToRadio method
  3687. *
  3688. * Test that magic control() selects can easily be converted into radio types without error.
  3689. *
  3690. * @return void
  3691. */
  3692. public function testControlMagicSelectChangeToRadio()
  3693. {
  3694. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3695. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3696. $this->assertContains('input type="radio"', $result);
  3697. }
  3698. /**
  3699. * testFormControlSubmit method
  3700. *
  3701. * Test correct results for form::control() and type submit.
  3702. *
  3703. * @return void
  3704. */
  3705. public function testFormControlSubmit()
  3706. {
  3707. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3708. $expected = [
  3709. 'div' => ['class' => 'submit'],
  3710. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3711. '/div'
  3712. ];
  3713. $this->assertHtml($expected, $result);
  3714. }
  3715. /**
  3716. * testFormControls method
  3717. *
  3718. * Test correct results from Form::controls().
  3719. *
  3720. * @return void
  3721. */
  3722. public function testFormControlsLegendFieldset()
  3723. {
  3724. $this->Form->create($this->article);
  3725. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3726. $expected = [
  3727. '<fieldset',
  3728. '<legend',
  3729. 'The Legend',
  3730. '/legend',
  3731. '*/fieldset',
  3732. ];
  3733. $this->assertHtml($expected, $result);
  3734. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3735. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3736. $this->assertContains('<fieldset>', $result);
  3737. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3738. $this->assertNotContains('<legend>', $result);
  3739. $this->assertNotContains('<fieldset>', $result);
  3740. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3741. $this->assertNotContains('<legend>', $result);
  3742. $this->assertNotContains('<fieldset>', $result);
  3743. $this->Form->create($this->article);
  3744. $this->View->setRequest($this->View->getRequest()
  3745. ->withParam('prefix', 'admin')
  3746. ->withParam('action', 'admin_edit')
  3747. ->withParam('controller', 'articles'));
  3748. $result = $this->Form->allControls();
  3749. $expected = [
  3750. '<fieldset',
  3751. '<legend',
  3752. 'New Article',
  3753. '/legend',
  3754. '*/fieldset',
  3755. ];
  3756. $this->assertHtml($expected, $result);
  3757. $this->Form->create($this->article);
  3758. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3759. $expected = [
  3760. '<fieldset',
  3761. '<legend',
  3762. 'The Legend',
  3763. '/legend',
  3764. '*/fieldset',
  3765. ];
  3766. $this->assertHtml($expected, $result);
  3767. $this->Form->create($this->article);
  3768. $result = $this->Form->allControls([], [
  3769. 'fieldset' => [
  3770. 'class' => 'some-class some-other-class',
  3771. 'disabled' => true,
  3772. 'data-param' => 'a-param'
  3773. ],
  3774. 'legend' => 'The Legend'
  3775. ]);
  3776. $expected = [
  3777. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3778. '<legend',
  3779. 'The Legend',
  3780. '/legend',
  3781. '*/fieldset',
  3782. ];
  3783. $this->assertHtml($expected, $result);
  3784. }
  3785. /**
  3786. * testFormControls method
  3787. *
  3788. * Test the controls() method.
  3789. *
  3790. * @return void
  3791. */
  3792. public function testFormControls()
  3793. {
  3794. $this->Form->create($this->article);
  3795. $result = $this->Form->allControls();
  3796. $expected = [
  3797. '<fieldset',
  3798. '<legend', 'New Article', '/legend',
  3799. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3800. ['div' => ['class' => 'input select required']],
  3801. '*/div',
  3802. ['div' => ['class' => 'input text required']],
  3803. '*/div',
  3804. ['div' => ['class' => 'input text']],
  3805. '*/div',
  3806. ['div' => ['class' => 'input text']],
  3807. '*/div',
  3808. '/fieldset',
  3809. ];
  3810. $this->assertHtml($expected, $result);
  3811. $result = $this->Form->allControls([
  3812. 'published' => ['type' => 'boolean']
  3813. ]);
  3814. $expected = [
  3815. '<fieldset',
  3816. '<legend', 'New Article', '/legend',
  3817. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3818. ['div' => ['class' => 'input select required']],
  3819. '*/div',
  3820. ['div' => ['class' => 'input text required']],
  3821. '*/div',
  3822. ['div' => ['class' => 'input text']],
  3823. '*/div',
  3824. ['div' => ['class' => 'input boolean']],
  3825. '*/div',
  3826. '/fieldset',
  3827. ];
  3828. $this->assertHtml($expected, $result);
  3829. $this->Form->create($this->article);
  3830. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3831. $expected = [
  3832. 'fieldset' => [],
  3833. 'legend' => [],
  3834. 'Hello',
  3835. '/legend',
  3836. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3837. ['div' => ['class' => 'input select required']],
  3838. '*/div',
  3839. ['div' => ['class' => 'input text required']],
  3840. '*/div',
  3841. ['div' => ['class' => 'input text']],
  3842. '*/div',
  3843. ['div' => ['class' => 'input text']],
  3844. '*/div',
  3845. '/fieldset'
  3846. ];
  3847. $this->assertHtml($expected, $result);
  3848. $this->Form->create(false);
  3849. $expected = [
  3850. 'fieldset' => [],
  3851. ['div' => ['class' => 'input text']],
  3852. 'label' => ['for' => 'foo'],
  3853. 'Foo',
  3854. '/label',
  3855. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3856. '*/div',
  3857. '/fieldset'
  3858. ];
  3859. $result = $this->Form->allControls(
  3860. ['foo' => ['type' => 'text']],
  3861. ['legend' => false]
  3862. );
  3863. $this->assertHtml($expected, $result);
  3864. }
  3865. /**
  3866. * testFormControlsBlacklist method
  3867. *
  3868. * @return void
  3869. */
  3870. public function testFormControlsBlacklist()
  3871. {
  3872. $this->Form->create($this->article);
  3873. $result = $this->Form->allControls([
  3874. 'id' => false
  3875. ]);
  3876. $expected = [
  3877. '<fieldset',
  3878. '<legend', 'New Article', '/legend',
  3879. ['div' => ['class' => 'input select required']],
  3880. '*/div',
  3881. ['div' => ['class' => 'input text required']],
  3882. '*/div',
  3883. ['div' => ['class' => 'input text']],
  3884. '*/div',
  3885. ['div' => ['class' => 'input text']],
  3886. '*/div',
  3887. '/fieldset',
  3888. ];
  3889. $this->assertHtml($expected, $result);
  3890. $this->Form->create($this->article);
  3891. $result = $this->Form->allControls([
  3892. 'id' => []
  3893. ]);
  3894. $expected = [
  3895. '<fieldset',
  3896. '<legend', 'New Article', '/legend',
  3897. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3898. ['div' => ['class' => 'input select required']],
  3899. '*/div',
  3900. ['div' => ['class' => 'input text required']],
  3901. '*/div',
  3902. ['div' => ['class' => 'input text']],
  3903. '*/div',
  3904. ['div' => ['class' => 'input text']],
  3905. '*/div',
  3906. '/fieldset',
  3907. ];
  3908. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3909. }
  3910. /**
  3911. * testSelectAsCheckbox method
  3912. *
  3913. * Test multi-select widget with checkbox formatting.
  3914. *
  3915. * @return void
  3916. */
  3917. public function testSelectAsCheckbox()
  3918. {
  3919. $result = $this->Form->select(
  3920. 'Model.multi_field',
  3921. ['first', 'second', 'third'],
  3922. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3923. );
  3924. $expected = [
  3925. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3926. ['div' => ['class' => 'checkbox']],
  3927. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3928. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3929. 'first',
  3930. '/label',
  3931. '/div',
  3932. ['div' => ['class' => 'checkbox']],
  3933. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3934. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3935. 'second',
  3936. '/label',
  3937. '/div',
  3938. ['div' => ['class' => 'checkbox']],
  3939. ['label' => ['for' => 'model-multi-field-2']],
  3940. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3941. 'third',
  3942. '/label',
  3943. '/div',
  3944. ];
  3945. $this->assertHtml($expected, $result);
  3946. $result = $this->Form->select(
  3947. 'Model.multi_field',
  3948. ['1/2' => 'half'],
  3949. ['multiple' => 'checkbox']
  3950. );
  3951. $expected = [
  3952. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3953. ['div' => ['class' => 'checkbox']],
  3954. ['label' => ['for' => 'model-multi-field-1-2']],
  3955. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3956. 'half',
  3957. '/label',
  3958. '/div',
  3959. ];
  3960. $this->assertHtml($expected, $result);
  3961. }
  3962. /**
  3963. * testLabel method
  3964. *
  3965. * Test label generation.
  3966. *
  3967. * @return void
  3968. */
  3969. public function testLabel()
  3970. {
  3971. $result = $this->Form->label('Person.name');
  3972. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3973. $this->assertHtml($expected, $result);
  3974. $result = $this->Form->label('Person.name');
  3975. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3976. $this->assertHtml($expected, $result);
  3977. $result = $this->Form->label('Person.first_name');
  3978. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3979. $this->assertHtml($expected, $result);
  3980. $result = $this->Form->label('Person.first_name', 'Your first name');
  3981. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3982. $this->assertHtml($expected, $result);
  3983. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3984. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3985. $this->assertHtml($expected, $result);
  3986. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3987. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3988. $this->assertHtml($expected, $result);
  3989. $result = $this->Form->label('Person.first_name', '');
  3990. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3991. $this->assertHtml($expected, $result);
  3992. $result = $this->Form->label('Person.2.name', '');
  3993. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3994. $this->assertHtml($expected, $result);
  3995. }
  3996. /**
  3997. * testLabelContainControl method
  3998. *
  3999. * Test that label() can accept an input with the correct template vars.
  4000. *
  4001. * @return void
  4002. */
  4003. public function testLabelContainControl()
  4004. {
  4005. $this->Form->setTemplates([
  4006. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4007. ]);
  4008. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  4009. 'input' => '<input type="checkbox" name="accept_tos"/>'
  4010. ]);
  4011. $expected = [
  4012. 'label' => ['for' => 'person-accept-terms'],
  4013. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  4014. 'Accept',
  4015. '/label',
  4016. ];
  4017. $this->assertHtml($expected, $result);
  4018. }
  4019. /**
  4020. * testTextbox method
  4021. *
  4022. * Test textbox element generation.
  4023. *
  4024. * @return void
  4025. */
  4026. public function testTextbox()
  4027. {
  4028. $result = $this->Form->text('Model.field');
  4029. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  4030. $this->assertHtml($expected, $result);
  4031. $result = $this->Form->text('Model.field', ['type' => 'password']);
  4032. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  4033. $this->assertHtml($expected, $result);
  4034. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  4035. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  4036. $this->assertHtml($expected, $result);
  4037. }
  4038. /**
  4039. * testTextBoxDataAndError method
  4040. *
  4041. * Test that text() hooks up with request data and error fields.
  4042. *
  4043. * @return void
  4044. */
  4045. public function testTextBoxDataAndError()
  4046. {
  4047. $this->article['errors'] = [
  4048. 'Contact' => ['text' => 'wrong']
  4049. ];
  4050. $this->View->setRequest($this->View->getRequest()
  4051. ->withData('Model.text', 'test <strong>HTML</strong> values')
  4052. ->withData('Contact.text', 'test'));
  4053. $this->Form->create($this->article);
  4054. $result = $this->Form->text('Model.text');
  4055. $expected = [
  4056. 'input' => [
  4057. 'type' => 'text',
  4058. 'name' => 'Model[text]',
  4059. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4060. ]
  4061. ];
  4062. $this->assertHtml($expected, $result);
  4063. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4064. $expected = [
  4065. 'input' => [
  4066. 'type' => 'text',
  4067. 'name' => 'Contact[text]',
  4068. 'value' => 'test',
  4069. 'id' => 'theID',
  4070. 'class' => 'form-error'
  4071. ]
  4072. ];
  4073. $this->assertHtml($expected, $result);
  4074. }
  4075. /**
  4076. * testDefaultValue method
  4077. *
  4078. * Test default value setting.
  4079. *
  4080. * @return void
  4081. */
  4082. public function testTextDefaultValue()
  4083. {
  4084. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  4085. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4086. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4087. $this->assertHtml($expected, $result);
  4088. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4089. $this->Form->create();
  4090. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4091. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4092. $this->assertHtml($expected, $result);
  4093. $Articles = $this->getTableLocator()->get('Articles');
  4094. $title = $Articles->getSchema()->getColumn('title');
  4095. $Articles->getSchema()->addColumn(
  4096. 'title',
  4097. ['default' => 'default title'] + $title
  4098. );
  4099. $entity = $Articles->newEntity();
  4100. $this->Form->create($entity);
  4101. // Get default value from schema
  4102. $result = $this->Form->text('title');
  4103. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4104. $this->assertHtml($expected, $result);
  4105. // Don't get value from schema
  4106. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4107. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4108. $this->assertHtml($expected, $result);
  4109. // Custom default value overrides default value from schema
  4110. $result = $this->Form->text('title', ['default' => 'override default']);
  4111. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4112. $this->assertHtml($expected, $result);
  4113. // Default value from schema is used only for new entities.
  4114. $entity->isNew(false);
  4115. $result = $this->Form->text('title');
  4116. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4117. $this->assertHtml($expected, $result);
  4118. }
  4119. /**
  4120. * testError method
  4121. *
  4122. * Test field error generation.
  4123. *
  4124. * @return void
  4125. */
  4126. public function testError()
  4127. {
  4128. $this->article['errors'] = [
  4129. 'Article' => ['field' => 'email']
  4130. ];
  4131. $this->Form->create($this->article);
  4132. $result = $this->Form->error('Article.field');
  4133. $expected = [
  4134. ['div' => ['class' => 'error-message']],
  4135. 'email',
  4136. '/div',
  4137. ];
  4138. $this->assertHtml($expected, $result);
  4139. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4140. $expected = [
  4141. ['div' => ['class' => 'error-message']],
  4142. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4143. '/div',
  4144. ];
  4145. $this->assertHtml($expected, $result);
  4146. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4147. $expected = [
  4148. ['div' => ['class' => 'error-message']],
  4149. '<strong', 'Badness!', '/strong',
  4150. '/div',
  4151. ];
  4152. $this->assertHtml($expected, $result);
  4153. }
  4154. /**
  4155. * testErrorRuleName method
  4156. *
  4157. * Test error translation can use rule names for translating.
  4158. *
  4159. * @return void
  4160. */
  4161. public function testErrorRuleName()
  4162. {
  4163. $this->article['errors'] = [
  4164. 'Article' => [
  4165. 'field' => ['email' => 'Your email was not good']
  4166. ]
  4167. ];
  4168. $this->Form->create($this->article);
  4169. $result = $this->Form->error('Article.field');
  4170. $expected = [
  4171. ['div' => ['class' => 'error-message']],
  4172. 'Your email was not good',
  4173. '/div',
  4174. ];
  4175. $this->assertHtml($expected, $result);
  4176. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4177. $expected = [
  4178. ['div' => ['class' => 'error-message']],
  4179. 'Email in use',
  4180. '/div',
  4181. ];
  4182. $this->assertHtml($expected, $result);
  4183. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4184. $expected = [
  4185. ['div' => ['class' => 'error-message']],
  4186. 'Email in use',
  4187. '/div',
  4188. ];
  4189. $this->assertHtml($expected, $result);
  4190. $result = $this->Form->error('Article.field', [
  4191. 'email' => 'Key is preferred',
  4192. 'Your email was not good' => 'Email in use'
  4193. ]);
  4194. $expected = [
  4195. ['div' => ['class' => 'error-message']],
  4196. 'Key is preferred',
  4197. '/div',
  4198. ];
  4199. $this->assertHtml($expected, $result);
  4200. }
  4201. /**
  4202. * testErrorMessages method
  4203. *
  4204. * Test error with nested lists.
  4205. *
  4206. * @return void
  4207. */
  4208. public function testErrorMessages()
  4209. {
  4210. $this->article['errors'] = [
  4211. 'Article' => ['field' => 'email']
  4212. ];
  4213. $this->Form->create($this->article);
  4214. $result = $this->Form->error('Article.field', [
  4215. 'email' => 'No good!'
  4216. ]);
  4217. $expected = [
  4218. 'div' => ['class' => 'error-message'],
  4219. 'No good!',
  4220. '/div'
  4221. ];
  4222. $this->assertHtml($expected, $result);
  4223. }
  4224. /**
  4225. * testErrorMultipleMessages method
  4226. *
  4227. * Test error() with multiple messages.
  4228. *
  4229. * @return void
  4230. */
  4231. public function testErrorMultipleMessages()
  4232. {
  4233. $this->article['errors'] = [
  4234. 'field' => ['notBlank', 'email', 'Something else']
  4235. ];
  4236. $this->Form->create($this->article);
  4237. $result = $this->Form->error('field', [
  4238. 'notBlank' => 'Cannot be empty',
  4239. 'email' => 'No good!'
  4240. ]);
  4241. $expected = [
  4242. 'div' => ['class' => 'error-message'],
  4243. 'ul' => [],
  4244. '<li', 'Cannot be empty', '/li',
  4245. '<li', 'No good!', '/li',
  4246. '<li', 'Something else', '/li',
  4247. '/ul',
  4248. '/div'
  4249. ];
  4250. $this->assertHtml($expected, $result);
  4251. }
  4252. /**
  4253. * testPassword method
  4254. *
  4255. * Test password element generation.
  4256. *
  4257. * @return void
  4258. */
  4259. public function testPassword()
  4260. {
  4261. $this->article['errors'] = [
  4262. 'Contact' => [
  4263. 'passwd' => 1
  4264. ]
  4265. ];
  4266. $this->Form->create($this->article);
  4267. $result = $this->Form->password('Contact.field');
  4268. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4269. $this->assertHtml($expected, $result);
  4270. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4271. $this->Form->create($this->article);
  4272. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4273. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4274. $this->assertHtml($expected, $result);
  4275. }
  4276. /**
  4277. * testRadio method
  4278. *
  4279. * Test radio element set generation.
  4280. *
  4281. * @return void
  4282. */
  4283. public function testRadio()
  4284. {
  4285. $result = $this->Form->radio('Model.field', ['option A']);
  4286. $expected = [
  4287. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4288. 'label' => ['for' => 'model-field-0'],
  4289. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4290. 'option A',
  4291. '/label'
  4292. ];
  4293. $this->assertHtml($expected, $result);
  4294. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4295. $this->assertHtml($expected, $result);
  4296. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4297. $expected = [
  4298. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4299. ['label' => ['for' => 'model-field-0']],
  4300. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4301. 'option A',
  4302. '/label',
  4303. ['label' => ['for' => 'model-field-1']],
  4304. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4305. 'option B',
  4306. '/label',
  4307. ];
  4308. $this->assertHtml($expected, $result);
  4309. $result = $this->Form->radio(
  4310. 'Employee.gender',
  4311. ['male' => 'Male', 'female' => 'Female'],
  4312. ['form' => 'my-form']
  4313. );
  4314. $expected = [
  4315. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4316. ['label' => ['for' => 'employee-gender-male']],
  4317. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4318. 'Male',
  4319. '/label',
  4320. ['label' => ['for' => 'employee-gender-female']],
  4321. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4322. 'Female',
  4323. '/label',
  4324. ];
  4325. $this->assertHtml($expected, $result);
  4326. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4327. $expected = [
  4328. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4329. ['label' => ['for' => 'model-custom-0']],
  4330. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4331. 'option A',
  4332. '/label',
  4333. ['label' => ['for' => 'model-custom-1']],
  4334. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4335. 'option B',
  4336. '/label',
  4337. ];
  4338. $this->assertHtml($expected, $result);
  4339. $result = $this->Form->radio(
  4340. 'Employee.gender',
  4341. [
  4342. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4343. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4344. ]
  4345. );
  4346. $expected = [
  4347. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4348. ['label' => ['for' => 'employee-gender-male']],
  4349. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4350. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4351. 'Male',
  4352. '/label',
  4353. ['label' => ['for' => 'employee-gender-female']],
  4354. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4355. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4356. 'Female',
  4357. '/label',
  4358. ];
  4359. $this->assertHtml($expected, $result);
  4360. }
  4361. /**
  4362. * Test radio with complex options and empty disabled data.
  4363. *
  4364. * @return void
  4365. */
  4366. public function testRadioComplexDisabled()
  4367. {
  4368. $options = [
  4369. ['value' => 'r', 'text' => 'red'],
  4370. ['value' => 'b', 'text' => 'blue'],
  4371. ];
  4372. $attrs = ['disabled' => []];
  4373. $result = $this->Form->radio('Model.field', $options, $attrs);
  4374. $expected = [
  4375. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4376. ['label' => ['for' => 'model-field-r']],
  4377. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4378. 'red',
  4379. '/label',
  4380. ['label' => ['for' => 'model-field-b']],
  4381. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4382. 'blue',
  4383. '/label',
  4384. ];
  4385. $this->assertHtml($expected, $result);
  4386. $attrs = ['disabled' => ['r']];
  4387. $result = $this->Form->radio('Model.field', $options, $attrs);
  4388. $this->assertContains('disabled="disabled"', $result);
  4389. }
  4390. /**
  4391. * testRadioDefaultValue method
  4392. *
  4393. * Test default value setting on radio() method.
  4394. *
  4395. * @return void
  4396. */
  4397. public function testRadioDefaultValue()
  4398. {
  4399. $Articles = $this->getTableLocator()->get('Articles');
  4400. $title = $Articles->getSchema()->getColumn('title');
  4401. $Articles->getSchema()->addColumn(
  4402. 'title',
  4403. ['default' => '1'] + $title
  4404. );
  4405. $this->Form->create($Articles->newEntity());
  4406. $result = $this->Form->radio('title', ['option A', 'option B']);
  4407. $expected = [
  4408. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4409. ['label' => ['for' => 'title-0']],
  4410. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4411. 'option A',
  4412. '/label',
  4413. ['label' => ['for' => 'title-1']],
  4414. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4415. 'option B',
  4416. '/label',
  4417. ];
  4418. $this->assertHtml($expected, $result);
  4419. }
  4420. /**
  4421. * Test setting a hiddenField value on radio buttons.
  4422. *
  4423. * @return void
  4424. */
  4425. public function testRadioHiddenFieldValue()
  4426. {
  4427. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4428. $expected = [
  4429. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4430. 'label' => ['for' => 'title-0'],
  4431. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4432. 'option A',
  4433. '/label',
  4434. ];
  4435. $this->assertHtml($expected, $result);
  4436. }
  4437. /**
  4438. * testControlRadio method
  4439. *
  4440. * Test that input works with radio types.
  4441. *
  4442. * @return void
  4443. */
  4444. public function testControlRadio()
  4445. {
  4446. $result = $this->Form->control('test', [
  4447. 'type' => 'radio',
  4448. 'options' => ['A', 'B'],
  4449. ]);
  4450. $expected = [
  4451. ['div' => ['class' => 'input radio']],
  4452. '<label',
  4453. 'Test',
  4454. '/label',
  4455. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4456. ['label' => ['for' => 'test-0']],
  4457. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4458. 'A',
  4459. '/label',
  4460. ['label' => ['for' => 'test-1']],
  4461. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4462. 'B',
  4463. '/label',
  4464. '/div',
  4465. ];
  4466. $this->assertHtml($expected, $result);
  4467. $result = $this->Form->control('test', [
  4468. 'type' => 'radio',
  4469. 'options' => ['A', 'B'],
  4470. 'value' => '0'
  4471. ]);
  4472. $expected = [
  4473. ['div' => ['class' => 'input radio']],
  4474. '<label',
  4475. 'Test',
  4476. '/label',
  4477. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4478. ['label' => ['for' => 'test-0']],
  4479. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4480. 'A',
  4481. '/label',
  4482. ['label' => ['for' => 'test-1']],
  4483. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4484. 'B',
  4485. '/label',
  4486. '/div',
  4487. ];
  4488. $this->assertHtml($expected, $result);
  4489. $result = $this->Form->control('test', [
  4490. 'type' => 'radio',
  4491. 'options' => ['A', 'B'],
  4492. 'label' => false
  4493. ]);
  4494. $expected = [
  4495. ['div' => ['class' => 'input radio']],
  4496. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4497. ['label' => ['for' => 'test-0']],
  4498. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4499. 'A',
  4500. '/label',
  4501. ['label' => ['for' => 'test-1']],
  4502. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4503. 'B',
  4504. '/label',
  4505. '/div',
  4506. ];
  4507. $this->assertHtml($expected, $result);
  4508. }
  4509. /**
  4510. * testRadioNoLabel method
  4511. *
  4512. * Test that radio() works with label = false.
  4513. *
  4514. * @return void
  4515. */
  4516. public function testRadioNoLabel()
  4517. {
  4518. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4519. $expected = [
  4520. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4521. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4522. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4523. ];
  4524. $this->assertHtml($expected, $result);
  4525. }
  4526. /**
  4527. * testRadioControlInsideLabel method
  4528. *
  4529. * Test generating radio input inside label ala twitter bootstrap.
  4530. *
  4531. * @return void
  4532. */
  4533. public function testRadioControlInsideLabel()
  4534. {
  4535. $this->Form->setTemplates([
  4536. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4537. 'radioWrapper' => '{{label}}'
  4538. ]);
  4539. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4540. //@codingStandardsIgnoreStart
  4541. $expected = [
  4542. ['input' => [
  4543. 'type' => 'hidden',
  4544. 'name' => 'Model[field]',
  4545. 'value' => ''
  4546. ]],
  4547. ['label' => ['for' => 'model-field-0']],
  4548. ['input' => [
  4549. 'type' => 'radio',
  4550. 'name' => 'Model[field]',
  4551. 'value' => '0',
  4552. 'id' => 'model-field-0'
  4553. ]],
  4554. 'option A',
  4555. '/label',
  4556. ['label' => ['for' => 'model-field-1']],
  4557. ['input' => [
  4558. 'type' => 'radio',
  4559. 'name' => 'Model[field]',
  4560. 'value' => '1',
  4561. 'id' => 'model-field-1'
  4562. ]],
  4563. 'option B',
  4564. '/label',
  4565. ];
  4566. //@codingStandardsIgnoreEnd
  4567. $this->assertHtml($expected, $result);
  4568. }
  4569. /**
  4570. * testRadioHiddenControlDisabling method
  4571. *
  4572. * Test disabling the hidden input for radio buttons.
  4573. *
  4574. * @return void
  4575. */
  4576. public function testRadioHiddenControlDisabling()
  4577. {
  4578. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4579. $expected = [
  4580. 'label' => ['for' => 'model-1-field-0'],
  4581. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4582. 'option A',
  4583. '/label'
  4584. ];
  4585. $this->assertHtml($expected, $result);
  4586. }
  4587. /**
  4588. * testRadioOutOfRange method
  4589. *
  4590. * Test radio element set generation.
  4591. *
  4592. * @return void
  4593. */
  4594. public function testRadioOutOfRange()
  4595. {
  4596. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4597. $expected = [
  4598. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4599. 'label' => ['for' => 'model-field-v'],
  4600. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4601. 'value',
  4602. '/label'
  4603. ];
  4604. $this->assertHtml($expected, $result);
  4605. }
  4606. /**
  4607. * testSelect method
  4608. *
  4609. * Test select element generation.
  4610. *
  4611. * @return void
  4612. */
  4613. public function testSelect()
  4614. {
  4615. $result = $this->Form->select('Model.field', []);
  4616. $expected = [
  4617. 'select' => ['name' => 'Model[field]'],
  4618. '/select'
  4619. ];
  4620. $this->assertHtml($expected, $result);
  4621. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4622. $this->Form->create();
  4623. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4624. $expected = [
  4625. 'select' => ['name' => 'Model[field]'],
  4626. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4627. 'good',
  4628. '/option',
  4629. ['option' => ['value' => 'other']],
  4630. 'bad',
  4631. '/option',
  4632. '/select'
  4633. ];
  4634. $this->assertHtml($expected, $result);
  4635. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4636. $this->assertHtml($expected, $result);
  4637. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4638. $this->Form->create();
  4639. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4640. $expected = [
  4641. 'select' => ['name' => 'Model[field]'],
  4642. ['option' => ['value' => 'value']],
  4643. 'good',
  4644. '/option',
  4645. ['option' => ['value' => 'other']],
  4646. 'bad',
  4647. '/option',
  4648. '/select'
  4649. ];
  4650. $this->assertHtml($expected, $result);
  4651. $options = [
  4652. ['value' => 'first', 'text' => 'First'],
  4653. ['value' => 'first', 'text' => 'Another First'],
  4654. ];
  4655. $result = $this->Form->select(
  4656. 'Model.field',
  4657. $options,
  4658. ['escape' => false, 'empty' => false]
  4659. );
  4660. $expected = [
  4661. 'select' => ['name' => 'Model[field]'],
  4662. ['option' => ['value' => 'first']],
  4663. 'First',
  4664. '/option',
  4665. ['option' => ['value' => 'first']],
  4666. 'Another First',
  4667. '/option',
  4668. '/select'
  4669. ];
  4670. $this->assertHtml($expected, $result);
  4671. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4672. $this->Form->create();
  4673. $result = $this->Form->select(
  4674. 'Model.contact_id',
  4675. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4676. ['escape' => false, 'empty' => 'pick something']
  4677. );
  4678. $expected = [
  4679. 'select' => ['name' => 'Model[contact_id]'],
  4680. ['option' => ['value' => '']], 'pick something', '/option',
  4681. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4682. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4683. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4684. '/select'
  4685. ];
  4686. $this->assertHtml($expected, $result);
  4687. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4688. $this->Form->create();
  4689. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4690. $expected = [
  4691. 'select' => ['name' => 'Model[field]'],
  4692. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4693. ['option' => ['value' => '1']], 'Yes', '/option',
  4694. '/select'
  4695. ];
  4696. $this->assertHtml($expected, $result);
  4697. }
  4698. /**
  4699. * testSelectEscapeHtml method
  4700. *
  4701. * Test that select() escapes HTML.
  4702. *
  4703. * @return void
  4704. */
  4705. public function testSelectEscapeHtml()
  4706. {
  4707. $result = $this->Form->select(
  4708. 'Model.field',
  4709. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4710. ['empty' => false]
  4711. );
  4712. $expected = [
  4713. 'select' => ['name' => 'Model[field]'],
  4714. ['option' => ['value' => 'first']],
  4715. 'first &quot;html&quot; &lt;chars&gt;',
  4716. '/option',
  4717. ['option' => ['value' => 'second']],
  4718. 'value',
  4719. '/option',
  4720. '/select'
  4721. ];
  4722. $this->assertHtml($expected, $result);
  4723. $result = $this->Form->select(
  4724. 'Model.field',
  4725. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4726. ['escape' => false, 'empty' => false]
  4727. );
  4728. $expected = [
  4729. 'select' => ['name' => 'Model[field]'],
  4730. ['option' => ['value' => 'first']],
  4731. 'first "html" <chars>',
  4732. '/option',
  4733. ['option' => ['value' => 'second']],
  4734. 'value',
  4735. '/option',
  4736. '/select'
  4737. ];
  4738. $this->assertHtml($expected, $result);
  4739. }
  4740. /**
  4741. * testSelectRequired method
  4742. *
  4743. * Test select() with required and disabled attributes.
  4744. *
  4745. * @return void
  4746. */
  4747. public function testSelectRequired()
  4748. {
  4749. $this->article['required'] = [
  4750. 'user_id' => true
  4751. ];
  4752. $this->Form->create($this->article);
  4753. $result = $this->Form->select('user_id', ['option A']);
  4754. $expected = [
  4755. 'select' => [
  4756. 'name' => 'user_id',
  4757. 'required' => 'required'
  4758. ],
  4759. ['option' => ['value' => '0']], 'option A', '/option',
  4760. '/select'
  4761. ];
  4762. $this->assertHtml($expected, $result);
  4763. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4764. $expected = [
  4765. 'select' => [
  4766. 'name' => 'user_id',
  4767. 'disabled' => 'disabled'
  4768. ],
  4769. ['option' => ['value' => '0']], 'option A', '/option',
  4770. '/select'
  4771. ];
  4772. $this->assertHtml($expected, $result);
  4773. }
  4774. /**
  4775. * testNestedSelect method
  4776. *
  4777. * Test select element generation with optgroups.
  4778. *
  4779. * @return void
  4780. */
  4781. public function testNestedSelect()
  4782. {
  4783. $result = $this->Form->select(
  4784. 'Model.field',
  4785. [1 => 'One', 2 => 'Two', 'Three' => [
  4786. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4787. ]],
  4788. ['empty' => false]
  4789. );
  4790. $expected = [
  4791. 'select' => ['name' => 'Model[field]'],
  4792. ['option' => ['value' => 1]],
  4793. 'One',
  4794. '/option',
  4795. ['option' => ['value' => 2]],
  4796. 'Two',
  4797. '/option',
  4798. ['optgroup' => ['label' => 'Three']],
  4799. ['option' => ['value' => 3]],
  4800. 'Three',
  4801. '/option',
  4802. ['option' => ['value' => 4]],
  4803. 'Four',
  4804. '/option',
  4805. ['option' => ['value' => 5]],
  4806. 'Five',
  4807. '/option',
  4808. '/optgroup',
  4809. '/select'
  4810. ];
  4811. $this->assertHtml($expected, $result);
  4812. }
  4813. /**
  4814. * testSelectMultiple method
  4815. *
  4816. * Test generation of multiple select elements.
  4817. *
  4818. * @return void
  4819. */
  4820. public function testSelectMultiple()
  4821. {
  4822. $options = ['first', 'second', 'third'];
  4823. $result = $this->Form->select(
  4824. 'Model.multi_field',
  4825. $options,
  4826. ['form' => 'my-form', 'multiple' => true]
  4827. );
  4828. $expected = [
  4829. 'input' => [
  4830. 'type' => 'hidden',
  4831. 'name' => 'Model[multi_field]',
  4832. 'value' => '',
  4833. 'form' => 'my-form',
  4834. ],
  4835. 'select' => [
  4836. 'name' => 'Model[multi_field][]',
  4837. 'multiple' => 'multiple',
  4838. 'form' => 'my-form',
  4839. ],
  4840. ['option' => ['value' => '0']],
  4841. 'first',
  4842. '/option',
  4843. ['option' => ['value' => '1']],
  4844. 'second',
  4845. '/option',
  4846. ['option' => ['value' => '2']],
  4847. 'third',
  4848. '/option',
  4849. '/select'
  4850. ];
  4851. $this->assertHtml($expected, $result);
  4852. $result = $this->Form->select(
  4853. 'Model.multi_field',
  4854. $options,
  4855. ['multiple' => 'multiple', 'form' => 'my-form']
  4856. );
  4857. $this->assertHtml($expected, $result);
  4858. $result = $this->Form->select(
  4859. 'Model.multi_field',
  4860. $options,
  4861. ['form' => 'my-form', 'multiple' => false]
  4862. );
  4863. $this->assertNotContains('multiple', $result);
  4864. }
  4865. /**
  4866. * testCheckboxZeroValue method
  4867. *
  4868. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4869. *
  4870. * @return void
  4871. */
  4872. public function testCheckboxZeroValue()
  4873. {
  4874. $result = $this->Form->control('User.get_spam', [
  4875. 'type' => 'checkbox',
  4876. 'value' => '0',
  4877. 'hiddenField' => '1',
  4878. ]);
  4879. $expected = [
  4880. 'div' => ['class' => 'input checkbox'],
  4881. 'label' => ['for' => 'user-get-spam'],
  4882. ['input' => [
  4883. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4884. 'value' => '1'
  4885. ]],
  4886. ['input' => [
  4887. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4888. 'value' => '0', 'id' => 'user-get-spam'
  4889. ]],
  4890. 'Get Spam',
  4891. '/label',
  4892. '/div'
  4893. ];
  4894. $this->assertHtml($expected, $result);
  4895. }
  4896. /**
  4897. * testHabtmSelectBox method
  4898. *
  4899. * Test generation of habtm select boxes.
  4900. *
  4901. * @return void
  4902. */
  4903. public function testHabtmSelectBox()
  4904. {
  4905. $this->loadFixtures('Articles');
  4906. $options = [
  4907. 1 => 'blue',
  4908. 2 => 'red',
  4909. 3 => 'green'
  4910. ];
  4911. $tags = [
  4912. new Entity(['id' => 1, 'name' => 'blue']),
  4913. new Entity(['id' => 3, 'name' => 'green'])
  4914. ];
  4915. $article = new Article(['tags' => $tags]);
  4916. $this->Form->create($article);
  4917. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4918. $expected = [
  4919. 'div' => ['class' => 'input select'],
  4920. 'label' => ['for' => 'tags-ids'],
  4921. 'Tags',
  4922. '/label',
  4923. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4924. 'select' => [
  4925. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4926. 'multiple' => 'multiple'
  4927. ],
  4928. ['option' => ['value' => '1', 'selected' => 'selected']],
  4929. 'blue',
  4930. '/option',
  4931. ['option' => ['value' => '2']],
  4932. 'red',
  4933. '/option',
  4934. ['option' => ['value' => '3', 'selected' => 'selected']],
  4935. 'green',
  4936. '/option',
  4937. '/select',
  4938. '/div'
  4939. ];
  4940. $this->assertHtml($expected, $result);
  4941. // make sure only 50 is selected, and not 50f5c0cf
  4942. $options = [
  4943. '1' => 'blue',
  4944. '50f5c0cf' => 'red',
  4945. '50' => 'green'
  4946. ];
  4947. $tags = [
  4948. new Entity(['id' => 1, 'name' => 'blue']),
  4949. new Entity(['id' => 50, 'name' => 'green'])
  4950. ];
  4951. $article = new Article(['tags' => $tags]);
  4952. $this->Form->create($article);
  4953. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4954. $expected = [
  4955. 'div' => ['class' => 'input select'],
  4956. 'label' => ['for' => 'tags-ids'],
  4957. 'Tags',
  4958. '/label',
  4959. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4960. 'select' => [
  4961. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4962. 'multiple' => 'multiple'
  4963. ],
  4964. ['option' => ['value' => '1', 'selected' => 'selected']],
  4965. 'blue',
  4966. '/option',
  4967. ['option' => ['value' => '50f5c0cf']],
  4968. 'red',
  4969. '/option',
  4970. ['option' => ['value' => '50', 'selected' => 'selected']],
  4971. 'green',
  4972. '/option',
  4973. '/select',
  4974. '/div'
  4975. ];
  4976. $this->assertHtml($expected, $result);
  4977. $spacecraft = [
  4978. 1 => 'Orion',
  4979. 2 => 'Helios'
  4980. ];
  4981. $this->View->viewVars['spacecraft'] = $spacecraft;
  4982. $this->Form->create();
  4983. $result = $this->Form->control('spacecraft._ids');
  4984. $expected = [
  4985. 'div' => ['class' => 'input select'],
  4986. 'label' => ['for' => 'spacecraft-ids'],
  4987. 'Spacecraft',
  4988. '/label',
  4989. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4990. 'select' => [
  4991. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4992. 'multiple' => 'multiple'
  4993. ],
  4994. ['option' => ['value' => '1']],
  4995. 'Orion',
  4996. '/option',
  4997. ['option' => ['value' => '2']],
  4998. 'Helios',
  4999. '/option',
  5000. '/select',
  5001. '/div'
  5002. ];
  5003. $this->assertHtml($expected, $result);
  5004. }
  5005. /**
  5006. * testErrorsForBelongsToManySelect method
  5007. *
  5008. * Tests that errors for belongsToMany select fields are being
  5009. * picked up properly.
  5010. *
  5011. * @return void
  5012. */
  5013. public function testErrorsForBelongsToManySelect()
  5014. {
  5015. $spacecraft = [
  5016. 1 => 'Orion',
  5017. 2 => 'Helios'
  5018. ];
  5019. $this->View->viewVars['spacecraft'] = $spacecraft;
  5020. $article = new Article();
  5021. $article->setError('spacecraft', ['Invalid']);
  5022. $this->Form->create($article);
  5023. $result = $this->Form->control('spacecraft._ids');
  5024. $expected = [
  5025. ['div' => ['class' => 'input select error']],
  5026. 'label' => ['for' => 'spacecraft-ids'],
  5027. 'Spacecraft',
  5028. '/label',
  5029. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  5030. 'select' => [
  5031. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  5032. 'multiple' => 'multiple'
  5033. ],
  5034. ['option' => ['value' => '1']],
  5035. 'Orion',
  5036. '/option',
  5037. ['option' => ['value' => '2']],
  5038. 'Helios',
  5039. '/option',
  5040. '/select',
  5041. ['div' => ['class' => 'error-message']],
  5042. 'Invalid',
  5043. '/div',
  5044. '/div'
  5045. ];
  5046. $this->assertHtml($expected, $result);
  5047. }
  5048. /**
  5049. * testSelectMultipleCheckboxes method
  5050. *
  5051. * Test generation of multi select elements in checkbox format.
  5052. *
  5053. * @return void
  5054. */
  5055. public function testSelectMultipleCheckboxes()
  5056. {
  5057. $result = $this->Form->select(
  5058. 'Model.multi_field',
  5059. ['first', 'second', 'third'],
  5060. ['multiple' => 'checkbox']
  5061. );
  5062. $expected = [
  5063. 'input' => [
  5064. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5065. ],
  5066. ['div' => ['class' => 'checkbox']],
  5067. ['label' => ['for' => 'model-multi-field-0']],
  5068. ['input' => [
  5069. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5070. 'value' => '0', 'id' => 'model-multi-field-0'
  5071. ]],
  5072. 'first',
  5073. '/label',
  5074. '/div',
  5075. ['div' => ['class' => 'checkbox']],
  5076. ['label' => ['for' => 'model-multi-field-1']],
  5077. ['input' => [
  5078. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5079. 'value' => '1', 'id' => 'model-multi-field-1'
  5080. ]],
  5081. 'second',
  5082. '/label',
  5083. '/div',
  5084. ['div' => ['class' => 'checkbox']],
  5085. ['label' => ['for' => 'model-multi-field-2']],
  5086. ['input' => [
  5087. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5088. 'value' => '2', 'id' => 'model-multi-field-2'
  5089. ]],
  5090. 'third',
  5091. '/label',
  5092. '/div'
  5093. ];
  5094. $this->assertHtml($expected, $result);
  5095. $result = $this->Form->select(
  5096. 'Model.multi_field',
  5097. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5098. ['multiple' => 'checkbox']
  5099. );
  5100. $expected = [
  5101. 'input' => [
  5102. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5103. ],
  5104. ['div' => ['class' => 'checkbox']],
  5105. ['label' => ['for' => 'model-multi-field-a+']],
  5106. ['input' => [
  5107. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5108. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5109. ]],
  5110. 'first',
  5111. '/label',
  5112. '/div',
  5113. ['div' => ['class' => 'checkbox']],
  5114. ['label' => ['for' => 'model-multi-field-a++']],
  5115. ['input' => [
  5116. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5117. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5118. ]],
  5119. 'second',
  5120. '/label',
  5121. '/div',
  5122. ['div' => ['class' => 'checkbox']],
  5123. ['label' => ['for' => 'model-multi-field-a+++']],
  5124. ['input' => [
  5125. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5126. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5127. ]],
  5128. 'third',
  5129. '/label',
  5130. '/div'
  5131. ];
  5132. $this->assertHtml($expected, $result);
  5133. $result = $this->Form->select(
  5134. 'Model.multi_field',
  5135. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5136. ['multiple' => 'checkbox']
  5137. );
  5138. $expected = [
  5139. 'input' => [
  5140. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5141. ],
  5142. ['div' => ['class' => 'checkbox']],
  5143. ['label' => ['for' => 'model-multi-field-a-b']],
  5144. ['input' => [
  5145. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5146. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5147. ]],
  5148. 'first',
  5149. '/label',
  5150. '/div',
  5151. ['div' => ['class' => 'checkbox']],
  5152. ['label' => ['for' => 'model-multi-field-a-b1']],
  5153. ['input' => [
  5154. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5155. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5156. ]],
  5157. 'second',
  5158. '/label',
  5159. '/div',
  5160. ['div' => ['class' => 'checkbox']],
  5161. ['label' => ['for' => 'model-multi-field-a-b2']],
  5162. ['input' => [
  5163. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5164. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5165. ]],
  5166. 'third',
  5167. '/label',
  5168. '/div'
  5169. ];
  5170. $this->assertHtml($expected, $result);
  5171. }
  5172. /**
  5173. * testSelectMultipleCheckboxRequestData method
  5174. *
  5175. * Ensure that multiCheckbox reads from the request data.
  5176. *
  5177. * @return void
  5178. */
  5179. public function testSelectMultipleCheckboxRequestData()
  5180. {
  5181. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5182. $result = $this->Form->select(
  5183. 'Model.tags',
  5184. ['1' => 'first', 'Array' => 'Array'],
  5185. ['multiple' => 'checkbox']
  5186. );
  5187. $expected = [
  5188. 'input' => [
  5189. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5190. ],
  5191. ['div' => ['class' => 'checkbox']],
  5192. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5193. ['input' => [
  5194. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5195. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5196. ]],
  5197. 'first',
  5198. '/label',
  5199. '/div',
  5200. ['div' => ['class' => 'checkbox']],
  5201. ['label' => ['for' => 'model-tags-array']],
  5202. ['input' => [
  5203. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5204. 'value' => 'Array', 'id' => 'model-tags-array'
  5205. ]],
  5206. 'Array',
  5207. '/label',
  5208. '/div'
  5209. ];
  5210. $this->assertHtml($expected, $result);
  5211. }
  5212. /**
  5213. * testSelectMultipleCheckboxSecurity method
  5214. *
  5215. * Checks the security hash array generated for multiple-input checkbox elements.
  5216. *
  5217. * @return void
  5218. */
  5219. public function testSelectMultipleCheckboxSecurity()
  5220. {
  5221. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  5222. $this->assertEquals([], $this->Form->fields);
  5223. $this->Form->select(
  5224. 'Model.multi_field',
  5225. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5226. ['multiple' => 'checkbox']
  5227. );
  5228. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5229. $result = $this->Form->secure($this->Form->fields);
  5230. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5231. $this->assertContains('"' . $key . '"', $result);
  5232. }
  5233. /**
  5234. * testSelectMultipleSecureWithNoOptions method
  5235. *
  5236. * Multiple select elements should always be secured as they always participate
  5237. * in the POST data.
  5238. *
  5239. * @return void
  5240. */
  5241. public function testSelectMultipleSecureWithNoOptions()
  5242. {
  5243. $this->assertEquals([], $this->Form->fields);
  5244. $this->Form->select(
  5245. 'Model.select',
  5246. [],
  5247. ['multiple' => true]
  5248. );
  5249. $this->assertEquals(['Model.select'], $this->Form->fields);
  5250. }
  5251. /**
  5252. * testSelectNoSecureWithNoOptions method
  5253. *
  5254. * When a select box has no options it should not be added to the fields list
  5255. * as it always fail post validation.
  5256. *
  5257. * @return void
  5258. */
  5259. public function testSelectNoSecureWithNoOptions()
  5260. {
  5261. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testkey'));
  5262. $this->assertEquals([], $this->Form->fields);
  5263. $this->Form->select(
  5264. 'Model.select',
  5265. []
  5266. );
  5267. $this->assertEquals([], $this->Form->fields);
  5268. $this->Form->select(
  5269. 'Model.user_id',
  5270. [],
  5271. ['empty' => true]
  5272. );
  5273. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5274. }
  5275. /**
  5276. * testControlMultipleCheckboxes method
  5277. *
  5278. * Test control() resulting in multi select elements being generated.
  5279. *
  5280. * @return void
  5281. */
  5282. public function testControlMultipleCheckboxes()
  5283. {
  5284. $result = $this->Form->control('Model.multi_field', [
  5285. 'options' => ['first', 'second', 'third'],
  5286. 'multiple' => 'checkbox'
  5287. ]);
  5288. $expected = [
  5289. ['div' => ['class' => 'input select']],
  5290. ['label' => ['for' => 'model-multi-field']],
  5291. 'Multi Field',
  5292. '/label',
  5293. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5294. ['div' => ['class' => 'checkbox']],
  5295. ['label' => ['for' => 'model-multi-field-0']],
  5296. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5297. 'first',
  5298. '/label',
  5299. '/div',
  5300. ['div' => ['class' => 'checkbox']],
  5301. ['label' => ['for' => 'model-multi-field-1']],
  5302. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5303. 'second',
  5304. '/label',
  5305. '/div',
  5306. ['div' => ['class' => 'checkbox']],
  5307. ['label' => ['for' => 'model-multi-field-2']],
  5308. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5309. 'third',
  5310. '/label',
  5311. '/div',
  5312. '/div'
  5313. ];
  5314. $this->assertHtml($expected, $result);
  5315. $result = $this->Form->control('Model.multi_field', [
  5316. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5317. 'multiple' => 'checkbox'
  5318. ]);
  5319. $expected = [
  5320. ['div' => ['class' => 'input select']],
  5321. ['label' => ['for' => 'model-multi-field']],
  5322. 'Multi Field',
  5323. '/label',
  5324. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5325. ['div' => ['class' => 'checkbox']],
  5326. ['label' => ['for' => 'model-multi-field-a']],
  5327. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5328. 'first',
  5329. '/label',
  5330. '/div',
  5331. ['div' => ['class' => 'checkbox']],
  5332. ['label' => ['for' => 'model-multi-field-b']],
  5333. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5334. 'second',
  5335. '/label',
  5336. '/div',
  5337. ['div' => ['class' => 'checkbox']],
  5338. ['label' => ['for' => 'model-multi-field-c']],
  5339. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5340. 'third',
  5341. '/label',
  5342. '/div',
  5343. '/div'
  5344. ];
  5345. $this->assertHtml($expected, $result);
  5346. }
  5347. /**
  5348. * testSelectHiddenFieldOmission method
  5349. *
  5350. * Test that select() with 'hiddenField' => false omits the hidden field.
  5351. *
  5352. * @return void
  5353. */
  5354. public function testSelectHiddenFieldOmission()
  5355. {
  5356. $result = $this->Form->select(
  5357. 'Model.multi_field',
  5358. ['first', 'second'],
  5359. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5360. );
  5361. $this->assertNotContains('type="hidden"', $result);
  5362. }
  5363. /**
  5364. * testSelectCheckboxMultipleOverrideName method
  5365. *
  5366. * Test that select() with multiple = checkbox works with overriding name attribute.
  5367. *
  5368. * @return void
  5369. */
  5370. public function testSelectCheckboxMultipleOverrideName()
  5371. {
  5372. $result = $this->Form->select('category', ['1', '2'], [
  5373. 'multiple' => 'checkbox',
  5374. 'name' => 'fish',
  5375. ]);
  5376. $expected = [
  5377. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5378. ['div' => ['class' => 'checkbox']],
  5379. ['label' => ['for' => 'fish-0']],
  5380. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5381. '1',
  5382. '/label',
  5383. '/div',
  5384. ['div' => ['class' => 'checkbox']],
  5385. ['label' => ['for' => 'fish-1']],
  5386. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5387. '2',
  5388. '/label',
  5389. '/div'
  5390. ];
  5391. $this->assertHtml($expected, $result);
  5392. $result = $this->Form->multiCheckbox(
  5393. 'category',
  5394. new Collection(['1', '2']),
  5395. ['name' => 'fish']
  5396. );
  5397. $this->assertHtml($expected, $result);
  5398. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5399. 'name' => 'fish',
  5400. ]);
  5401. $this->assertHtml($expected, $result);
  5402. }
  5403. /**
  5404. * testControlMultiCheckbox method
  5405. *
  5406. * Test that control() works with multicheckbox.
  5407. *
  5408. * @return void
  5409. */
  5410. public function testControlMultiCheckbox()
  5411. {
  5412. $result = $this->Form->control('category', [
  5413. 'type' => 'multicheckbox',
  5414. 'options' => ['1', '2'],
  5415. ]);
  5416. $expected = [
  5417. ['div' => ['class' => 'input multicheckbox']],
  5418. '<label',
  5419. 'Category',
  5420. '/label',
  5421. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5422. ['div' => ['class' => 'checkbox']],
  5423. ['label' => ['for' => 'category-0']],
  5424. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5425. '1',
  5426. '/label',
  5427. '/div',
  5428. ['div' => ['class' => 'checkbox']],
  5429. ['label' => ['for' => 'category-1']],
  5430. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5431. '2',
  5432. '/label',
  5433. '/div',
  5434. '/div',
  5435. ];
  5436. $this->assertHtml($expected, $result);
  5437. }
  5438. /**
  5439. * testCheckbox method
  5440. *
  5441. * Test generation of checkboxes.
  5442. *
  5443. * @return void
  5444. */
  5445. public function testCheckbox()
  5446. {
  5447. $result = $this->Form->checkbox('Model.field');
  5448. $expected = [
  5449. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5450. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5451. ];
  5452. $this->assertHtml($expected, $result);
  5453. $result = $this->Form->checkbox('Model.field', [
  5454. 'id' => 'theID',
  5455. 'value' => 'myvalue',
  5456. 'form' => 'my-form',
  5457. ]);
  5458. $expected = [
  5459. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5460. ['input' => [
  5461. 'type' => 'checkbox', 'name' => 'Model[field]',
  5462. 'value' => 'myvalue', 'id' => 'theID',
  5463. 'form' => 'my-form',
  5464. ]]
  5465. ];
  5466. $this->assertHtml($expected, $result);
  5467. }
  5468. /**
  5469. * testCheckboxDefaultValue method
  5470. *
  5471. * Test default value setting on checkbox() method.
  5472. *
  5473. * @return void
  5474. */
  5475. public function testCheckboxDefaultValue()
  5476. {
  5477. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5478. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5479. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5480. $this->assertHtml($expected, $result);
  5481. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5482. $this->Form->create();
  5483. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5484. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5485. $this->assertHtml($expected, $result);
  5486. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5487. $this->Form->create();
  5488. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5489. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5490. $this->assertHtml($expected, $result);
  5491. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5492. $this->Form->create();
  5493. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5494. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5495. $this->assertHtml($expected, $result);
  5496. $Articles = $this->getTableLocator()->get('Articles');
  5497. $Articles->getSchema()->addColumn(
  5498. 'published',
  5499. ['type' => 'boolean', 'null' => false, 'default' => true]
  5500. );
  5501. $this->Form->create($Articles->newEntity());
  5502. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5503. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5504. $this->assertHtml($expected, $result);
  5505. }
  5506. /**
  5507. * testCheckboxCheckedAndError method
  5508. *
  5509. * Test checkbox being checked or having errors.
  5510. *
  5511. * @return void
  5512. */
  5513. public function testCheckboxCheckedAndError()
  5514. {
  5515. $this->article['errors'] = [
  5516. 'published' => true
  5517. ];
  5518. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5519. $this->Form->create($this->article);
  5520. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5521. $expected = [
  5522. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5523. ['input' => [
  5524. 'type' => 'checkbox',
  5525. 'name' => 'published',
  5526. 'value' => 'myvalue',
  5527. 'id' => 'theID',
  5528. 'checked' => 'checked',
  5529. 'class' => 'form-error'
  5530. ]]
  5531. ];
  5532. $this->assertHtml($expected, $result);
  5533. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5534. $this->Form->create($this->article);
  5535. $result = $this->Form->checkbox('published');
  5536. $expected = [
  5537. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5538. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5539. ];
  5540. $this->assertHtml($expected, $result);
  5541. }
  5542. /**
  5543. * testCheckboxCustomNameAttribute method
  5544. *
  5545. * Test checkbox() with a custom name attribute.
  5546. *
  5547. * @return void
  5548. */
  5549. public function testCheckboxCustomNameAttribute()
  5550. {
  5551. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5552. $expected = [
  5553. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5554. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5555. ];
  5556. $this->assertHtml($expected, $result);
  5557. }
  5558. /**
  5559. * testCheckboxHiddenField method
  5560. *
  5561. * Test that the hidden input for checkboxes can be omitted or set to a
  5562. * specific value.
  5563. *
  5564. * @return void
  5565. */
  5566. public function testCheckboxHiddenField()
  5567. {
  5568. $result = $this->Form->checkbox('UserForm.something', [
  5569. 'hiddenField' => false
  5570. ]);
  5571. $expected = [
  5572. 'input' => [
  5573. 'type' => 'checkbox',
  5574. 'name' => 'UserForm[something]',
  5575. 'value' => '1'
  5576. ],
  5577. ];
  5578. $this->assertHtml($expected, $result);
  5579. $result = $this->Form->checkbox('UserForm.something', [
  5580. 'value' => 'Y',
  5581. 'hiddenField' => 'N',
  5582. ]);
  5583. $expected = [
  5584. ['input' => [
  5585. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5586. 'value' => 'N'
  5587. ]],
  5588. ['input' => [
  5589. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5590. 'value' => 'Y'
  5591. ]],
  5592. ];
  5593. $this->assertHtml($expected, $result);
  5594. }
  5595. /**
  5596. * testTime method
  5597. *
  5598. * Test the time type.
  5599. *
  5600. * @return void
  5601. */
  5602. public function testTime()
  5603. {
  5604. $result = $this->Form->time('start_time', [
  5605. 'timeFormat' => 12,
  5606. 'interval' => 5,
  5607. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5608. ]);
  5609. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5610. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5611. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5612. $this->assertNotContains('year', $result);
  5613. $this->assertNotContains('month', $result);
  5614. $this->assertNotContains('day', $result);
  5615. $result = $this->Form->time('start_time', [
  5616. 'timeFormat' => 12,
  5617. 'interval' => 5,
  5618. 'value' => '2014-03-08 16:30:00'
  5619. ]);
  5620. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5621. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5622. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5623. $this->assertNotContains('year', $result);
  5624. $this->assertNotContains('month', $result);
  5625. $this->assertNotContains('day', $result);
  5626. }
  5627. /**
  5628. * testTimeFormat24NoMeridian method
  5629. *
  5630. * Ensure that timeFormat=24 has no merdian.
  5631. *
  5632. * @return void.
  5633. */
  5634. public function testTimeFormat24NoMeridian()
  5635. {
  5636. $result = $this->Form->time('start_time', [
  5637. 'timeFormat' => 24,
  5638. 'interval' => 5,
  5639. 'value' => '2014-03-08 16:30:00'
  5640. ]);
  5641. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5642. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5643. $this->assertNotContains('meridian', $result);
  5644. $this->assertNotContains('pm', $result);
  5645. $this->assertNotContains('year', $result);
  5646. $this->assertNotContains('month', $result);
  5647. $this->assertNotContains('day', $result);
  5648. }
  5649. /**
  5650. * testDate method
  5651. *
  5652. * Test the date type.
  5653. *
  5654. * @return void
  5655. */
  5656. public function testDate()
  5657. {
  5658. $result = $this->Form->date('start_day', [
  5659. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5660. ]);
  5661. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5662. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5663. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5664. $this->assertNotContains('hour', $result);
  5665. $this->assertNotContains('minute', $result);
  5666. $this->assertNotContains('second', $result);
  5667. $this->assertNotContains('meridian', $result);
  5668. }
  5669. /**
  5670. * testDateTime method
  5671. *
  5672. * Test generation of date/time select elements.
  5673. *
  5674. * @return void
  5675. */
  5676. public function testDateTime()
  5677. {
  5678. extract($this->dateRegex);
  5679. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5680. $now = strtotime('now');
  5681. $expected = [
  5682. ['select' => ['name' => 'Contact[date][year]']],
  5683. ['option' => ['value' => '']],
  5684. '/option',
  5685. $yearsRegex,
  5686. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5687. date('Y', $now),
  5688. '/option',
  5689. '*/select',
  5690. ['select' => ['name' => 'Contact[date][month]']],
  5691. ['option' => ['value' => '']],
  5692. '/option',
  5693. $monthsRegex,
  5694. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5695. date('F', $now),
  5696. '/option',
  5697. '*/select',
  5698. ['select' => ['name' => 'Contact[date][day]']],
  5699. ['option' => ['value' => '']],
  5700. '/option',
  5701. $daysRegex,
  5702. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5703. date('j', $now),
  5704. '/option',
  5705. '*/select',
  5706. ['select' => ['name' => 'Contact[date][hour]']],
  5707. ['option' => ['value' => '']],
  5708. '/option',
  5709. $hoursRegex,
  5710. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5711. date('G', $now),
  5712. '/option',
  5713. '*/select',
  5714. ['select' => ['name' => 'Contact[date][minute]']],
  5715. ['option' => ['value' => '']],
  5716. '/option',
  5717. $minutesRegex,
  5718. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5719. date('i', $now),
  5720. '/option',
  5721. '*/select',
  5722. ];
  5723. $this->assertHtml($expected, $result);
  5724. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5725. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5726. $now = strtotime('now');
  5727. $expected = [
  5728. ['select' => ['name' => 'Contact[date][year]']],
  5729. $yearsRegex,
  5730. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5731. date('Y', $now),
  5732. '/option',
  5733. '*/select',
  5734. ['select' => ['name' => 'Contact[date][month]']],
  5735. $monthsRegex,
  5736. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5737. date('F', $now),
  5738. '/option',
  5739. '*/select',
  5740. ['select' => ['name' => 'Contact[date][day]']],
  5741. $daysRegex,
  5742. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5743. date('j', $now),
  5744. '/option',
  5745. '*/select',
  5746. ['select' => ['name' => 'Contact[date][hour]']],
  5747. $hoursRegex,
  5748. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5749. date('G', $now),
  5750. '/option',
  5751. '*/select',
  5752. ['select' => ['name' => 'Contact[date][minute]']],
  5753. $minutesRegex,
  5754. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5755. date('i', $now),
  5756. '/option',
  5757. '*/select',
  5758. ];
  5759. $this->assertHtml($expected, $result);
  5760. }
  5761. /**
  5762. * testDateTimeSecured method
  5763. *
  5764. * Test that datetime fields are added to protected fields list.
  5765. *
  5766. * @return void
  5767. */
  5768. public function testDateTimeSecured()
  5769. {
  5770. $this->View->setRequest(
  5771. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5772. );
  5773. $this->Form->dateTime('Contact.date');
  5774. $expected = [
  5775. 'Contact.date.year',
  5776. 'Contact.date.month',
  5777. 'Contact.date.day',
  5778. 'Contact.date.hour',
  5779. 'Contact.date.minute',
  5780. ];
  5781. $this->assertEquals($expected, $this->Form->fields);
  5782. $this->Form->fields = [];
  5783. $this->Form->date('Contact.published');
  5784. $expected = [
  5785. 'Contact.published.year',
  5786. 'Contact.published.month',
  5787. 'Contact.published.day',
  5788. ];
  5789. $this->assertEquals($expected, $this->Form->fields);
  5790. }
  5791. /**
  5792. * testDateTimeSecuredDisabled method
  5793. *
  5794. * Test that datetime fields are added to protected fields list.
  5795. *
  5796. * @return void
  5797. */
  5798. public function testDateTimeSecuredDisabled()
  5799. {
  5800. $this->View->setRequest(
  5801. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5802. );
  5803. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5804. $expected = [];
  5805. $this->assertEquals($expected, $this->Form->fields);
  5806. $this->Form->fields = [];
  5807. $this->Form->date('Contact.published', ['secure' => false]);
  5808. $expected = [];
  5809. $this->assertEquals($expected, $this->Form->fields);
  5810. }
  5811. /**
  5812. * testDatetimeEmpty method
  5813. *
  5814. * Test empty defaulting to true for datetime.
  5815. *
  5816. * @return void
  5817. */
  5818. public function testDatetimeEmpty()
  5819. {
  5820. extract($this->dateRegex);
  5821. $result = $this->Form->dateTime('Contact.date', [
  5822. 'timeFormat' => 12,
  5823. 'empty' => true,
  5824. 'default' => true
  5825. ]);
  5826. $expected = [
  5827. ['select' => ['name' => 'Contact[date][year]']],
  5828. $yearsRegex,
  5829. ['option' => ['value' => '']],
  5830. '/option',
  5831. '*/select',
  5832. ['select' => ['name' => 'Contact[date][month]']],
  5833. $monthsRegex,
  5834. ['option' => ['value' => '']],
  5835. '/option',
  5836. '*/select',
  5837. ['select' => ['name' => 'Contact[date][day]']],
  5838. $daysRegex,
  5839. ['option' => ['value' => '']],
  5840. '/option',
  5841. '*/select',
  5842. ['select' => ['name' => 'Contact[date][hour]']],
  5843. $hoursRegex,
  5844. ['option' => ['value' => '']],
  5845. '/option',
  5846. '*/select',
  5847. ['select' => ['name' => 'Contact[date][minute]']],
  5848. $minutesRegex,
  5849. ['option' => ['value' => '']],
  5850. '/option',
  5851. '*/select',
  5852. ['select' => ['name' => 'Contact[date][meridian]']],
  5853. $meridianRegex,
  5854. ['option' => ['value' => '']],
  5855. '/option',
  5856. '*/select'
  5857. ];
  5858. $this->assertHtml($expected, $result);
  5859. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5860. }
  5861. /**
  5862. * testDatetimeMinuteInterval method
  5863. *
  5864. * Test datetime with interval option.
  5865. *
  5866. * @return void
  5867. */
  5868. public function testDatetimeMinuteInterval()
  5869. {
  5870. extract($this->dateRegex);
  5871. $result = $this->Form->dateTime('Contact.date', [
  5872. 'interval' => 5,
  5873. 'value' => ''
  5874. ]);
  5875. $expected = [
  5876. ['select' => ['name' => 'Contact[date][year]']],
  5877. $yearsRegex,
  5878. ['option' => ['selected' => 'selected', 'value' => '']],
  5879. '/option',
  5880. '*/select',
  5881. ['select' => ['name' => 'Contact[date][month]']],
  5882. $monthsRegex,
  5883. ['option' => ['selected' => 'selected', 'value' => '']],
  5884. '/option',
  5885. '*/select',
  5886. ['select' => ['name' => 'Contact[date][day]']],
  5887. $daysRegex,
  5888. ['option' => ['selected' => 'selected', 'value' => '']],
  5889. '/option',
  5890. '*/select',
  5891. ['select' => ['name' => 'Contact[date][hour]']],
  5892. $hoursRegex,
  5893. ['option' => ['selected' => 'selected', 'value' => '']],
  5894. '/option',
  5895. '*/select',
  5896. ['select' => ['name' => 'Contact[date][minute]']],
  5897. $minutesRegex,
  5898. ['option' => ['selected' => 'selected', 'value' => '']],
  5899. '/option',
  5900. ['option' => ['value' => '00']],
  5901. '00',
  5902. '/option',
  5903. ['option' => ['value' => '05']],
  5904. '05',
  5905. '/option',
  5906. ['option' => ['value' => '10']],
  5907. '10',
  5908. '/option',
  5909. '*/select',
  5910. ];
  5911. $this->assertHtml($expected, $result);
  5912. }
  5913. /**
  5914. * testDateTimeRounding method
  5915. *
  5916. * Test dateTime with rounding.
  5917. *
  5918. * @return void
  5919. */
  5920. public function testDateTimeRounding()
  5921. {
  5922. $this->View->setRequest($this->View->getRequest()->withData('Contact', [
  5923. 'date' => [
  5924. 'day' => '13',
  5925. 'month' => '12',
  5926. 'year' => '2010',
  5927. 'hour' => '04',
  5928. 'minute' => '19',
  5929. 'meridian' => 'AM'
  5930. ]
  5931. ]));
  5932. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5933. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5934. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5935. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5936. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5937. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5938. }
  5939. /**
  5940. * testDatetimeWithDefault method
  5941. *
  5942. * Test that datetime() and default values work.
  5943. *
  5944. * @return void
  5945. */
  5946. public function testDatetimeWithDefault()
  5947. {
  5948. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5949. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5950. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5951. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5952. $result = $this->Form->dateTime('Contact.updated', [
  5953. 'default' => '2009-06-01 11:15:30'
  5954. ]);
  5955. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5956. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5957. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5958. }
  5959. /**
  5960. * testDateTimeAllZeros method
  5961. *
  5962. * Test datetime() with all zeros.
  5963. *
  5964. * @return void
  5965. */
  5966. public function testDateTimeAllZeros()
  5967. {
  5968. $result = $this->Form->dateTime('Contact.date', [
  5969. 'timeFormat' => false,
  5970. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5971. 'value' => '0000-00-00'
  5972. ]);
  5973. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5974. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5975. }
  5976. /**
  5977. * testDateTimeEmptyAsArray method
  5978. *
  5979. * @return void
  5980. */
  5981. public function testDateTimeEmptyAsArray()
  5982. {
  5983. $result = $this->Form->dateTime('Contact.date', [
  5984. 'empty' => [
  5985. 'day' => 'DAY',
  5986. 'month' => 'MONTH',
  5987. 'year' => 'YEAR',
  5988. 'hour' => 'HOUR',
  5989. 'minute' => 'MINUTE',
  5990. 'meridian' => false
  5991. ],
  5992. 'default' => true
  5993. ]);
  5994. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5995. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5996. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5997. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5998. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5999. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  6000. $result = $this->Form->dateTime('Contact.date', [
  6001. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  6002. 'default' => true
  6003. ]);
  6004. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  6005. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  6006. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  6007. }
  6008. /**
  6009. * testFormDateTimeMulti method
  6010. *
  6011. * Test multiple datetime element generation.
  6012. *
  6013. * @return void
  6014. */
  6015. public function testFormDateTimeMulti()
  6016. {
  6017. extract($this->dateRegex);
  6018. $result = $this->Form->dateTime('Contact.1.updated');
  6019. $this->assertContains('Contact[1][updated][month]', $result);
  6020. $this->assertContains('Contact[1][updated][day]', $result);
  6021. $this->assertContains('Contact[1][updated][year]', $result);
  6022. $this->assertContains('Contact[1][updated][hour]', $result);
  6023. $this->assertContains('Contact[1][updated][minute]', $result);
  6024. }
  6025. /**
  6026. * testDateTimeLabelIdMatchesFirstControl method
  6027. *
  6028. * When changing the date format, the label should always focus the first select box when
  6029. * clicked.
  6030. *
  6031. * @return void
  6032. */
  6033. public function testDateTimeLabelIdMatchesFirstControl()
  6034. {
  6035. $result = $this->Form->control('Model.date', ['type' => 'date']);
  6036. $this->assertContains('<label>Date</label>', $result);
  6037. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  6038. $this->assertContains('<label>Date</label>', $result);
  6039. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  6040. $this->assertContains('<label>Date</label>', $result);
  6041. }
  6042. /**
  6043. * testDateTimeSecondOptions method
  6044. *
  6045. * Test datetime second=true.
  6046. *
  6047. * @return void
  6048. */
  6049. public function testDateTimeSecondOptions()
  6050. {
  6051. $result = $this->Form->dateTime('updated', ['second' => true]);
  6052. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6053. $result = $this->Form->dateTime('updated', ['second' => []]);
  6054. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6055. $result = $this->Form->dateTime('updated', ['second' => null]);
  6056. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6057. $result = $this->Form->dateTime('updated', ['second' => false]);
  6058. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6059. }
  6060. /**
  6061. * testMonth method
  6062. *
  6063. * Test generation of a month input.
  6064. *
  6065. * @return void
  6066. */
  6067. public function testMonth()
  6068. {
  6069. $result = $this->Form->month('Model.field', ['value' => '']);
  6070. $expected = [
  6071. ['select' => ['name' => 'Model[field][month]']],
  6072. ['option' => ['value' => '', 'selected' => 'selected']],
  6073. '/option',
  6074. ['option' => ['value' => '01']],
  6075. date('F', strtotime('2008-01-01 00:00:00')),
  6076. '/option',
  6077. ['option' => ['value' => '02']],
  6078. date('F', strtotime('2008-02-01 00:00:00')),
  6079. '/option',
  6080. '*/select',
  6081. ];
  6082. $this->assertHtml($expected, $result);
  6083. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6084. $expected = [
  6085. ['select' => ['name' => 'Model[field][month]']],
  6086. ['option' => ['selected' => 'selected', 'value' => '']],
  6087. '/option',
  6088. ['option' => ['value' => '01']],
  6089. date('F', strtotime('2008-01-01 00:00:00')),
  6090. '/option',
  6091. ['option' => ['value' => '02']],
  6092. date('F', strtotime('2008-02-01 00:00:00')),
  6093. '/option',
  6094. '*/select',
  6095. ];
  6096. $this->assertHtml($expected, $result);
  6097. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6098. $expected = [
  6099. ['select' => ['name' => 'Model[field][month]']],
  6100. ['option' => ['selected' => 'selected', 'value' => '']],
  6101. '/option',
  6102. ['option' => ['value' => '01']],
  6103. '1',
  6104. '/option',
  6105. ['option' => ['value' => '02']],
  6106. '2',
  6107. '/option',
  6108. '*/select',
  6109. ];
  6110. $this->assertHtml($expected, $result);
  6111. $monthNames = [
  6112. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6113. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6114. ];
  6115. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6116. $expected = [
  6117. ['select' => ['name' => 'Model[field][month]']],
  6118. ['option' => ['value' => '']],
  6119. '/option',
  6120. ['option' => ['value' => '01', 'selected' => 'selected']],
  6121. 'Jan',
  6122. '/option',
  6123. ['option' => ['value' => '02']],
  6124. 'Feb',
  6125. '/option',
  6126. '*/select',
  6127. ];
  6128. $this->assertHtml($expected, $result);
  6129. $this->View->setRequest(
  6130. $this->View->getRequest()->withData('Project.release', '2050-02-10')
  6131. );
  6132. $this->Form->create();
  6133. $result = $this->Form->month('Project.release');
  6134. $expected = [
  6135. ['select' => ['name' => 'Project[release][month]']],
  6136. ['option' => ['value' => '']],
  6137. '/option',
  6138. ['option' => ['value' => '01']],
  6139. 'January',
  6140. '/option',
  6141. ['option' => ['value' => '02', 'selected' => 'selected']],
  6142. 'February',
  6143. '/option',
  6144. '*/select',
  6145. ];
  6146. $this->assertHtml($expected, $result);
  6147. $result = $this->Form->month('Contact.published', [
  6148. 'empty' => 'Published on',
  6149. ]);
  6150. $this->assertContains('Published on', $result);
  6151. }
  6152. /**
  6153. * testDay method
  6154. *
  6155. * Test generation of a day input.
  6156. *
  6157. * @return void
  6158. */
  6159. public function testDay()
  6160. {
  6161. extract($this->dateRegex);
  6162. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6163. $expected = [
  6164. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6165. ['option' => ['selected' => 'selected', 'value' => '']],
  6166. '/option',
  6167. ['option' => ['value' => '01']],
  6168. '1',
  6169. '/option',
  6170. ['option' => ['value' => '02']],
  6171. '2',
  6172. '/option',
  6173. $daysRegex,
  6174. '/select',
  6175. ];
  6176. $this->assertHtml($expected, $result);
  6177. $this->View->setRequest(
  6178. $this->View->getRequest()->withData('Model.field', '2006-10-10 23:12:32')
  6179. );
  6180. $this->Form->create();
  6181. $result = $this->Form->day('Model.field');
  6182. $expected = [
  6183. ['select' => ['name' => 'Model[field][day]']],
  6184. ['option' => ['value' => '']],
  6185. '/option',
  6186. ['option' => ['value' => '01']],
  6187. '1',
  6188. '/option',
  6189. ['option' => ['value' => '02']],
  6190. '2',
  6191. '/option',
  6192. $daysRegex,
  6193. ['option' => ['value' => '10', 'selected' => 'selected']],
  6194. '10',
  6195. '/option',
  6196. $daysRegex,
  6197. '/select',
  6198. ];
  6199. $this->assertHtml($expected, $result);
  6200. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6201. $this->Form->create();
  6202. $result = $this->Form->day('Model.field', ['value' => '10']);
  6203. $expected = [
  6204. ['select' => ['name' => 'Model[field][day]']],
  6205. ['option' => ['value' => '']],
  6206. '/option',
  6207. ['option' => ['value' => '01']],
  6208. '1',
  6209. '/option',
  6210. ['option' => ['value' => '02']],
  6211. '2',
  6212. '/option',
  6213. $daysRegex,
  6214. ['option' => ['value' => '10', 'selected' => 'selected']],
  6215. '10',
  6216. '/option',
  6217. $daysRegex,
  6218. '/select',
  6219. ];
  6220. $this->assertHtml($expected, $result);
  6221. $this->View->setRequest(
  6222. $this->View->getRequest()->withData('Project.release', '2050-10-10')
  6223. );
  6224. $this->Form->create();
  6225. $result = $this->Form->day('Project.release');
  6226. $expected = [
  6227. ['select' => ['name' => 'Project[release][day]']],
  6228. ['option' => ['value' => '']],
  6229. '/option',
  6230. ['option' => ['value' => '01']],
  6231. '1',
  6232. '/option',
  6233. ['option' => ['value' => '02']],
  6234. '2',
  6235. '/option',
  6236. $daysRegex,
  6237. ['option' => ['value' => '10', 'selected' => 'selected']],
  6238. '10',
  6239. '/option',
  6240. $daysRegex,
  6241. '/select',
  6242. ];
  6243. $this->assertHtml($expected, $result);
  6244. $result = $this->Form->day('Contact.published', [
  6245. 'empty' => 'Published on',
  6246. ]);
  6247. $this->assertContains('Published on', $result);
  6248. }
  6249. /**
  6250. * testMinute method
  6251. *
  6252. * Test generation of a minute input.
  6253. *
  6254. * @return void
  6255. */
  6256. public function testMinute()
  6257. {
  6258. extract($this->dateRegex);
  6259. $result = $this->Form->minute('Model.field', ['value' => '']);
  6260. $expected = [
  6261. ['select' => ['name' => 'Model[field][minute]']],
  6262. ['option' => ['selected' => 'selected', 'value' => '']],
  6263. '/option',
  6264. ['option' => ['value' => '00']],
  6265. '00',
  6266. '/option',
  6267. ['option' => ['value' => '01']],
  6268. '01',
  6269. '/option',
  6270. ['option' => ['value' => '02']],
  6271. '02',
  6272. '/option',
  6273. $minutesRegex,
  6274. '/select',
  6275. ];
  6276. $this->assertHtml($expected, $result);
  6277. $this->View->setRequest(
  6278. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6279. );
  6280. $this->Form->create();
  6281. $result = $this->Form->minute('Model.field');
  6282. $expected = [
  6283. ['select' => ['name' => 'Model[field][minute]']],
  6284. ['option' => ['value' => '']],
  6285. '/option',
  6286. ['option' => ['value' => '00']],
  6287. '00',
  6288. '/option',
  6289. ['option' => ['value' => '01']],
  6290. '01',
  6291. '/option',
  6292. ['option' => ['value' => '02']],
  6293. '02',
  6294. '/option',
  6295. $minutesRegex,
  6296. ['option' => ['value' => '12', 'selected' => 'selected']],
  6297. '12',
  6298. '/option',
  6299. $minutesRegex,
  6300. '/select',
  6301. ];
  6302. $this->assertHtml($expected, $result);
  6303. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6304. $this->Form->create();
  6305. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6306. $expected = [
  6307. ['select' => ['name' => 'Model[field][minute]']],
  6308. ['option' => ['selected' => 'selected', 'value' => '']],
  6309. '/option',
  6310. ['option' => ['value' => '00']],
  6311. '00',
  6312. '/option',
  6313. ['option' => ['value' => '05']],
  6314. '05',
  6315. '/option',
  6316. ['option' => ['value' => '10']],
  6317. '10',
  6318. '/option',
  6319. $minutesRegex,
  6320. '/select',
  6321. ];
  6322. $this->assertHtml($expected, $result);
  6323. $this->View->setRequest(
  6324. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:10:32')
  6325. );
  6326. $this->Form->create();
  6327. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6328. $expected = [
  6329. ['select' => ['name' => 'Model[field][minute]']],
  6330. ['option' => ['value' => '']],
  6331. '/option',
  6332. ['option' => ['value' => '00']],
  6333. '00',
  6334. '/option',
  6335. ['option' => ['value' => '05']],
  6336. '05',
  6337. '/option',
  6338. ['option' => ['value' => '10', 'selected' => 'selected']],
  6339. '10',
  6340. '/option',
  6341. $minutesRegex,
  6342. '/select',
  6343. ];
  6344. $this->assertHtml($expected, $result);
  6345. }
  6346. /**
  6347. * testMeridian method
  6348. *
  6349. * Test generating an input for the meridian.
  6350. *
  6351. * @return void
  6352. */
  6353. public function testMeridian()
  6354. {
  6355. extract($this->dateRegex);
  6356. $now = new \DateTime();
  6357. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6358. $expected = [
  6359. ['select' => ['name' => 'Model[field][meridian]']],
  6360. ['option' => ['value' => '']],
  6361. '/option',
  6362. $meridianRegex,
  6363. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6364. $now->format('a'),
  6365. '/option',
  6366. '*/select'
  6367. ];
  6368. $this->assertHtml($expected, $result);
  6369. }
  6370. /**
  6371. * testHour method
  6372. *
  6373. * Test generation of an hour input.
  6374. *
  6375. * @return void
  6376. */
  6377. public function testHour()
  6378. {
  6379. extract($this->dateRegex);
  6380. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6381. $expected = [
  6382. ['select' => ['name' => 'Model[field][hour]']],
  6383. ['option' => ['selected' => 'selected', 'value' => '']],
  6384. '/option',
  6385. ['option' => ['value' => '01']],
  6386. '1',
  6387. '/option',
  6388. ['option' => ['value' => '02']],
  6389. '2',
  6390. '/option',
  6391. $hoursRegex,
  6392. '/select',
  6393. ];
  6394. $this->assertHtml($expected, $result);
  6395. $this->View->setRequest(
  6396. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6397. );
  6398. $this->Form->create();
  6399. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6400. $expected = [
  6401. ['select' => ['name' => 'Model[field][hour]']],
  6402. ['option' => ['value' => '']],
  6403. '/option',
  6404. ['option' => ['value' => '01']],
  6405. '1',
  6406. '/option',
  6407. ['option' => ['value' => '02']],
  6408. '2',
  6409. '/option',
  6410. $hoursRegex,
  6411. ['option' => ['value' => '12', 'selected' => 'selected']],
  6412. '12',
  6413. '/option',
  6414. '/select',
  6415. ];
  6416. $this->assertHtml($expected, $result);
  6417. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6418. $this->Form->create();
  6419. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6420. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6421. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6422. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6423. $this->View->setRequest(
  6424. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6425. );
  6426. $this->Form->create();
  6427. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6428. $expected = [
  6429. ['select' => ['name' => 'Model[field][hour]']],
  6430. ['option' => ['value' => '']],
  6431. '/option',
  6432. ['option' => ['value' => '00', 'selected' => 'selected']],
  6433. '0',
  6434. '/option',
  6435. ['option' => ['value' => '01']],
  6436. '1',
  6437. '/option',
  6438. ['option' => ['value' => '02']],
  6439. '2',
  6440. '/option',
  6441. $hoursRegex,
  6442. '/select',
  6443. ];
  6444. $this->assertHtml($expected, $result);
  6445. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  6446. $this->Form->create();
  6447. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6448. $thisHour = date('H');
  6449. $optValue = date('G');
  6450. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6451. $this->View->setRequest(
  6452. $this->View->getRequest()->withData('Model.field', '2050-10-10 01:12:32')
  6453. );
  6454. $this->Form->create();
  6455. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6456. $expected = [
  6457. ['select' => ['name' => 'Model[field][hour]']],
  6458. ['option' => ['value' => '']],
  6459. '/option',
  6460. ['option' => ['value' => '00']],
  6461. '0',
  6462. '/option',
  6463. ['option' => ['value' => '01', 'selected' => 'selected']],
  6464. '1',
  6465. '/option',
  6466. ['option' => ['value' => '02']],
  6467. '2',
  6468. '/option',
  6469. $hoursRegex,
  6470. '/select',
  6471. ];
  6472. $this->assertHtml($expected, $result);
  6473. }
  6474. /**
  6475. * testYear method
  6476. *
  6477. * Test generation of a year input.
  6478. *
  6479. * @return void
  6480. */
  6481. public function testYear()
  6482. {
  6483. $this->View->setRequest(
  6484. $this->View->getRequest()->withData('Contact.published', '2006-10-10')
  6485. );
  6486. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6487. $expected = [
  6488. ['select' => ['name' => 'Model[field][year]']],
  6489. ['option' => ['selected' => 'selected', 'value' => '']],
  6490. '/option',
  6491. ['option' => ['value' => '2007']],
  6492. '2007',
  6493. '/option',
  6494. ['option' => ['value' => '2006']],
  6495. '2006',
  6496. '/option',
  6497. '/select',
  6498. ];
  6499. $this->assertHtml($expected, $result);
  6500. $result = $this->Form->year('Model.field', [
  6501. 'value' => '',
  6502. 'minYear' => 2006,
  6503. 'maxYear' => 2007,
  6504. 'orderYear' => 'asc'
  6505. ]);
  6506. $expected = [
  6507. ['select' => ['name' => 'Model[field][year]']],
  6508. ['option' => ['selected' => 'selected', 'value' => '']],
  6509. '/option',
  6510. ['option' => ['value' => '2006']],
  6511. '2006',
  6512. '/option',
  6513. ['option' => ['value' => '2007']],
  6514. '2007',
  6515. '/option',
  6516. '/select',
  6517. ];
  6518. $this->assertHtml($expected, $result);
  6519. $result = $this->Form->year('Contact.published', [
  6520. 'empty' => false,
  6521. 'minYear' => 2006,
  6522. 'maxYear' => 2007,
  6523. ]);
  6524. $expected = [
  6525. ['select' => ['name' => 'Contact[published][year]']],
  6526. ['option' => ['value' => '2007']],
  6527. '2007',
  6528. '/option',
  6529. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6530. '2006',
  6531. '/option',
  6532. '/select',
  6533. ];
  6534. $this->assertHtml($expected, $result);
  6535. $result = $this->Form->year('Contact.published', [
  6536. 'empty' => 'Published on',
  6537. ]);
  6538. $this->assertContains('Published on', $result);
  6539. }
  6540. /**
  6541. * testControlDatetimePreEpoch method
  6542. *
  6543. * Test minYear being prior to the unix epoch.
  6544. *
  6545. * @return void
  6546. */
  6547. public function testControlDatetimePreEpoch()
  6548. {
  6549. $start = date('Y') - 80;
  6550. $end = date('Y') - 18;
  6551. $result = $this->Form->control('birth_year', [
  6552. 'type' => 'date',
  6553. 'label' => 'Birth Year',
  6554. 'minYear' => $start,
  6555. 'maxYear' => $end,
  6556. 'month' => false,
  6557. 'day' => false,
  6558. ]);
  6559. $this->assertContains('value="' . $start . '">' . $start, $result);
  6560. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6561. $this->assertNotContains('value="00">00', $result);
  6562. }
  6563. /**
  6564. * test control() datetime & required attributes
  6565. *
  6566. * @return void
  6567. */
  6568. public function testControlDatetimeRequired()
  6569. {
  6570. $result = $this->Form->control('birthday', [
  6571. 'type' => 'date',
  6572. 'required' => true
  6573. ]);
  6574. $this->assertContains(
  6575. '<select name="birthday[year]" required="required"',
  6576. $result
  6577. );
  6578. }
  6579. /**
  6580. * testYearAutoExpandRange method
  6581. *
  6582. * @return void
  6583. */
  6584. public function testYearAutoExpandRange()
  6585. {
  6586. $this->View->setRequest($this->View->getRequest()->withData('User.birthday', '1930-10-10'));
  6587. $result = $this->Form->year('User.birthday');
  6588. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6589. $result = $matches[1];
  6590. $expected = range(date('Y') + 5, 1930);
  6591. $this->assertEquals($expected, $result);
  6592. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '2050-10-10'));
  6593. $this->Form->create();
  6594. $result = $this->Form->year('Project.release');
  6595. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6596. $result = $matches[1];
  6597. $expected = range(2050, date('Y') - 5);
  6598. $this->assertEquals($expected, $result);
  6599. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '1881-10-10'));
  6600. $this->Form->create();
  6601. $result = $this->Form->year('Project.release', [
  6602. 'minYear' => 1890,
  6603. 'maxYear' => 1900
  6604. ]);
  6605. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6606. $result = $matches[1];
  6607. $expected = range(1900, 1881);
  6608. $this->assertEquals($expected, $result);
  6609. }
  6610. /**
  6611. * testControlDate method
  6612. *
  6613. * Test that control() accepts the type of date and passes options in.
  6614. *
  6615. * @return void
  6616. */
  6617. public function testControlDate()
  6618. {
  6619. $this->View->setRequest($this->View->getRequest()->withParsedBody([
  6620. 'month_year' => ['month' => date('m')],
  6621. ]));
  6622. $this->Form->create($this->article);
  6623. $result = $this->Form->control('month_year', [
  6624. 'label' => false,
  6625. 'type' => 'date',
  6626. 'minYear' => 2006,
  6627. 'maxYear' => 2008
  6628. ]);
  6629. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6630. $this->assertNotContains('value="2008" selected="selected"', $result);
  6631. }
  6632. /**
  6633. * testControlLabelFalse method
  6634. *
  6635. * Test the label option being set to false.
  6636. *
  6637. * @return void
  6638. */
  6639. public function testControlLabelFalse()
  6640. {
  6641. $this->Form->create($this->article);
  6642. $result = $this->Form->control('title', ['label' => false]);
  6643. $expected = [
  6644. 'div' => ['class' => 'input text required'],
  6645. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6646. '/div'
  6647. ];
  6648. $this->assertHtml($expected, $result);
  6649. }
  6650. /**
  6651. * testControlDateMaxYear method
  6652. *
  6653. * Let's say we want to only allow users born from 2006 to 2008 to register
  6654. * This being the first signup page, we still don't have any data.
  6655. *
  6656. * @return void
  6657. */
  6658. public function testControlDateMaxYear()
  6659. {
  6660. $this->Form->create($this->article);
  6661. $result = $this->Form->control('birthday', [
  6662. 'label' => false,
  6663. 'type' => 'date',
  6664. 'minYear' => 2006,
  6665. 'maxYear' => 2008,
  6666. 'default' => true
  6667. ]);
  6668. $this->assertContains('value="2008" selected="selected"', $result);
  6669. $this->assertContains('value="2006"', $result);
  6670. $this->assertNotContains('value="2005"', $result);
  6671. $this->assertNotContains('value="2009"', $result);
  6672. }
  6673. /**
  6674. * testTextArea method
  6675. *
  6676. * Test generation of a textarea input.
  6677. *
  6678. * @return void
  6679. */
  6680. public function testTextArea()
  6681. {
  6682. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  6683. $result = $this->Form->textarea('field');
  6684. $expected = [
  6685. 'textarea' => ['name' => 'field', 'rows' => 5],
  6686. 'some test data',
  6687. '/textarea',
  6688. ];
  6689. $this->assertHtml($expected, $result);
  6690. $result = $this->Form->textarea('user.bio');
  6691. $expected = [
  6692. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6693. '/textarea',
  6694. ];
  6695. $this->assertHtml($expected, $result);
  6696. $this->View->setRequest($this->View->getRequest()
  6697. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6698. $this->Form->create();
  6699. $result = $this->Form->textarea('field');
  6700. $expected = [
  6701. 'textarea' => ['name' => 'field', 'rows' => 5],
  6702. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6703. '/textarea',
  6704. ];
  6705. $this->assertHtml($expected, $result);
  6706. $this->View->setRequest($this->View->getRequest()->withData(
  6707. 'Model.field',
  6708. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6709. ));
  6710. $this->Form->create();
  6711. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6712. $expected = [
  6713. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6714. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6715. '/textarea',
  6716. ];
  6717. $this->assertHtml($expected, $result);
  6718. $result = $this->Form->textarea('0.OtherModel.field');
  6719. $expected = [
  6720. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6721. '/textarea'
  6722. ];
  6723. $this->assertHtml($expected, $result);
  6724. }
  6725. /**
  6726. * testTextAreaWithStupidCharacters method
  6727. *
  6728. * Test text area with non-ascii characters.
  6729. *
  6730. * @return void
  6731. */
  6732. public function testTextAreaWithStupidCharacters()
  6733. {
  6734. $result = $this->Form->textarea('Post.content', [
  6735. 'value' => 'GREAT®',
  6736. 'rows' => '15',
  6737. 'cols' => '75'
  6738. ]);
  6739. $expected = [
  6740. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6741. 'GREAT®',
  6742. '/textarea',
  6743. ];
  6744. $this->assertHtml($expected, $result);
  6745. }
  6746. /**
  6747. * testTextAreaMaxLength method
  6748. *
  6749. * Test textareas maxlength read from schema.
  6750. *
  6751. * @return void
  6752. */
  6753. public function testTextAreaMaxLength()
  6754. {
  6755. $this->Form->create([
  6756. 'schema' => [
  6757. 'stuff' => ['type' => 'string', 'length' => 10],
  6758. ]
  6759. ]);
  6760. $result = $this->Form->control('other', ['type' => 'textarea']);
  6761. $expected = [
  6762. 'div' => ['class' => 'input textarea'],
  6763. 'label' => ['for' => 'other'],
  6764. 'Other',
  6765. '/label',
  6766. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6767. '/textarea',
  6768. '/div'
  6769. ];
  6770. $this->assertHtml($expected, $result);
  6771. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6772. $expected = [
  6773. 'div' => ['class' => 'input textarea'],
  6774. 'label' => ['for' => 'stuff'],
  6775. 'Stuff',
  6776. '/label',
  6777. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6778. '/textarea',
  6779. '/div'
  6780. ];
  6781. $this->assertHtml($expected, $result);
  6782. }
  6783. /**
  6784. * testHiddenField method
  6785. *
  6786. * Test generation of a hidden input.
  6787. *
  6788. * @return void
  6789. */
  6790. public function testHiddenField()
  6791. {
  6792. $this->article['errors'] = [
  6793. 'field' => true
  6794. ];
  6795. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6796. $this->Form->create($this->article);
  6797. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6798. $expected = [
  6799. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6800. $this->assertHtml($expected, $result);
  6801. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6802. $expected = [
  6803. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6804. ];
  6805. $this->assertHtml($expected, $result);
  6806. }
  6807. /**
  6808. * testFileUploadField method
  6809. *
  6810. * Test generation of a file upload input.
  6811. *
  6812. * @return void
  6813. */
  6814. public function testFileUploadField()
  6815. {
  6816. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6817. $result = $this->Form->file('Model.upload');
  6818. $this->assertHtml($expected, $result);
  6819. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6820. 'name' => '', 'type' => '', 'tmp_name' => '',
  6821. 'error' => 4, 'size' => 0
  6822. ]));
  6823. $result = $this->Form->file('Model.upload');
  6824. $this->assertHtml($expected, $result);
  6825. $this->View->setRequest(
  6826. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6827. );
  6828. $result = $this->Form->file('Model.upload');
  6829. $this->assertHtml($expected, $result);
  6830. }
  6831. /**
  6832. * testFileUploadOnOtherModel method
  6833. *
  6834. * Test File upload input on a model not used in create().
  6835. *
  6836. * @return void
  6837. */
  6838. public function testFileUploadOnOtherModel()
  6839. {
  6840. $this->Form->create($this->article, ['type' => 'file']);
  6841. $result = $this->Form->file('ValidateProfile.city');
  6842. $expected = [
  6843. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6844. ];
  6845. $this->assertHtml($expected, $result);
  6846. }
  6847. /**
  6848. * testButton method
  6849. *
  6850. * Test generation of a form button.
  6851. *
  6852. * @return void
  6853. */
  6854. public function testButton()
  6855. {
  6856. $result = $this->Form->button('Hi');
  6857. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6858. $this->assertHtml($expected, $result);
  6859. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6860. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6861. $this->assertHtml($expected, $result);
  6862. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6863. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6864. $this->assertHtml($expected, $result);
  6865. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6866. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6867. $this->assertHtml($expected, $result);
  6868. $result = $this->Form->button('No type', ['type' => false]);
  6869. $expected = ['button' => [], 'No type', '/button'];
  6870. $this->assertHtml($expected, $result);
  6871. $result = $this->Form->button('Upload Text', [
  6872. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6873. 'escape' => false
  6874. ]);
  6875. $this->assertNotRegExp('/\&039/', $result);
  6876. }
  6877. /**
  6878. * testButtonUnlockedByDefault method
  6879. *
  6880. * Test that button() makes unlocked fields by default.
  6881. *
  6882. * @return void
  6883. */
  6884. public function testButtonUnlockedByDefault()
  6885. {
  6886. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'secured'));
  6887. $this->Form->button('Save', ['name' => 'save']);
  6888. $this->Form->button('Clear');
  6889. $result = $this->Form->unlockField();
  6890. $this->assertEquals(['save'], $result);
  6891. }
  6892. /**
  6893. * Test generation of a form button with confirm message.
  6894. *
  6895. * @return void
  6896. */
  6897. public function testButtonWithConfirm()
  6898. {
  6899. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6900. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6901. $this->assertHtml($expected, $result);
  6902. }
  6903. /**
  6904. * testPostButton method
  6905. *
  6906. * @return void
  6907. */
  6908. public function testPostButton()
  6909. {
  6910. $result = $this->Form->postButton('Hi', '/controller/action');
  6911. $expected = [
  6912. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6913. 'div' => ['style' => 'display:none;'],
  6914. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6915. '/div',
  6916. 'button' => ['type' => 'submit'],
  6917. 'Hi',
  6918. '/button',
  6919. '/form'
  6920. ];
  6921. $this->assertHtml($expected, $result);
  6922. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6923. $this->assertContains('<input type="hidden" name="extra" value="value"', $result);
  6924. }
  6925. /**
  6926. * testPostButtonMethodType method
  6927. *
  6928. * @return void
  6929. */
  6930. public function testPostButtonMethodType()
  6931. {
  6932. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6933. $expected = [
  6934. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6935. 'div' => ['style' => 'display:none;'],
  6936. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6937. '/div',
  6938. 'button' => ['type' => 'submit'],
  6939. 'Hi',
  6940. '/button',
  6941. '/form'
  6942. ];
  6943. $this->assertHtml($expected, $result);
  6944. }
  6945. /**
  6946. * testPostButtonFormOptions method
  6947. *
  6948. * @return void
  6949. */
  6950. public function testPostButtonFormOptions()
  6951. {
  6952. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6953. $expected = [
  6954. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6955. 'div' => ['style' => 'display:none;'],
  6956. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6957. '/div',
  6958. 'button' => ['type' => 'submit'],
  6959. 'Hi',
  6960. '/button',
  6961. '/form'
  6962. ];
  6963. $this->assertHtml($expected, $result);
  6964. }
  6965. /**
  6966. * testPostButtonNestedData method
  6967. *
  6968. * Test using postButton with N dimensional data.
  6969. *
  6970. * @return void
  6971. */
  6972. public function testPostButtonNestedData()
  6973. {
  6974. $data = [
  6975. 'one' => [
  6976. 'two' => [
  6977. 3, 4, 5
  6978. ]
  6979. ]
  6980. ];
  6981. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6982. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6983. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6984. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6985. }
  6986. /**
  6987. * testSecurePostButton method
  6988. *
  6989. * Test that postButton adds _Token fields.
  6990. *
  6991. * @return void
  6992. */
  6993. public function testSecurePostButton()
  6994. {
  6995. $this->View->setRequest($this->View->getRequest()
  6996. ->withParam('_csrfToken', 'testkey')
  6997. ->withParam('_Token.unlockedFields', []));
  6998. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6999. $tokenDebug = urlencode(json_encode([
  7000. '/posts/delete/1',
  7001. [],
  7002. []
  7003. ]));
  7004. $expected = [
  7005. 'form' => [
  7006. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  7007. ],
  7008. ['div' => ['style' => 'display:none;']],
  7009. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7010. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7011. '/div',
  7012. 'button' => ['type' => 'submit'],
  7013. 'Delete',
  7014. '/button',
  7015. ['div' => ['style' => 'display:none;']],
  7016. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7017. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7018. ['input' => [
  7019. 'type' => 'hidden',
  7020. 'name' => '_Token[debug]',
  7021. 'value' => $tokenDebug,
  7022. 'autocomplete' => 'off',
  7023. ]],
  7024. '/div',
  7025. '/form',
  7026. ];
  7027. $this->assertHtml($expected, $result);
  7028. }
  7029. /**
  7030. * testPostLink method
  7031. *
  7032. * @return void
  7033. */
  7034. public function testPostLink()
  7035. {
  7036. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7037. $expected = [
  7038. 'form' => [
  7039. 'method' => 'post', 'action' => '/posts/delete/1',
  7040. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7041. ],
  7042. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7043. '/form',
  7044. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7045. 'Delete',
  7046. '/a'
  7047. ];
  7048. $this->assertHtml($expected, $result);
  7049. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  7050. $expected = [
  7051. 'form' => [
  7052. 'method' => 'post', 'action' => '/posts/delete/1',
  7053. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7054. ],
  7055. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  7056. '/form',
  7057. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7058. 'Delete',
  7059. '/a'
  7060. ];
  7061. $this->assertHtml($expected, $result);
  7062. $result = $this->Form->postLink(
  7063. 'Delete',
  7064. '/posts/delete/1',
  7065. ['target' => '_blank', 'class' => 'btn btn-danger']
  7066. );
  7067. $expected = [
  7068. 'form' => [
  7069. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  7070. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7071. ],
  7072. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7073. '/form',
  7074. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7075. 'Delete',
  7076. '/a'
  7077. ];
  7078. $this->assertHtml($expected, $result);
  7079. }
  7080. /**
  7081. * testPostLinkWithConfirm method
  7082. *
  7083. * Test the confirm option for postLink().
  7084. *
  7085. * @return void
  7086. */
  7087. public function testPostLinkWithConfirm()
  7088. {
  7089. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  7090. $expected = [
  7091. 'form' => [
  7092. 'method' => 'post', 'action' => '/posts/delete/1',
  7093. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7094. ],
  7095. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7096. '/form',
  7097. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  7098. 'Delete',
  7099. '/a'
  7100. ];
  7101. $this->assertHtml($expected, $result);
  7102. $result = $this->Form->postLink(
  7103. 'Delete',
  7104. '/posts/delete/1',
  7105. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  7106. );
  7107. $expected = [
  7108. 'form' => [
  7109. 'method' => 'post', 'action' => '/posts/delete/1',
  7110. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7111. ],
  7112. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7113. '/form',
  7114. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7115. 'Delete',
  7116. '/a'
  7117. ];
  7118. $this->assertHtml($expected, $result);
  7119. $this->Form->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { $(\'form[name="{{formName}}"]\').submit();};']);
  7120. $result = $this->Form->postLink(
  7121. 'Delete',
  7122. '/posts/delete/1',
  7123. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  7124. );
  7125. $expected = [
  7126. 'form' => [
  7127. 'method' => 'post', 'action' => '/posts/delete/1',
  7128. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7129. ],
  7130. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7131. '/form',
  7132. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm this deletion\?"\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/'],
  7133. 'Delete',
  7134. '/a'
  7135. ];
  7136. $this->assertHtml($expected, $result);
  7137. }
  7138. /**
  7139. * testPostLinkWithQuery method
  7140. *
  7141. * Test postLink() with query string args.
  7142. *
  7143. * @return void
  7144. */
  7145. public function testPostLinkWithQuery()
  7146. {
  7147. $result = $this->Form->postLink(
  7148. 'Delete',
  7149. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7150. );
  7151. $expected = [
  7152. 'form' => [
  7153. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7154. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7155. ],
  7156. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7157. '/form',
  7158. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7159. 'Delete',
  7160. '/a'
  7161. ];
  7162. $this->assertHtml($expected, $result);
  7163. }
  7164. /**
  7165. * testPostLinkWithData method
  7166. *
  7167. * Test postLink with additional data.
  7168. *
  7169. * @return void
  7170. */
  7171. public function testPostLinkWithData()
  7172. {
  7173. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7174. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7175. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7176. $this->Form->create($entity);
  7177. $this->Form->end();
  7178. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7179. $this->assertContains(
  7180. '<input type="hidden" name="name" value="show"',
  7181. $result,
  7182. 'should not contain entity data.'
  7183. );
  7184. }
  7185. /**
  7186. * testPostLinkSecurityHash method
  7187. *
  7188. * Test that security hashes for postLink include the url.
  7189. *
  7190. * @return void
  7191. */
  7192. public function testPostLinkSecurityHash()
  7193. {
  7194. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7195. $hash .= '%3Aid';
  7196. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7197. $result = $this->Form->postLink(
  7198. 'Delete',
  7199. '/posts/delete/1',
  7200. ['data' => ['id' => 1]]
  7201. );
  7202. $tokenDebug = urlencode(json_encode([
  7203. '/posts/delete/1',
  7204. [
  7205. 'id' => 1
  7206. ],
  7207. []
  7208. ]));
  7209. $expected = [
  7210. 'form' => [
  7211. 'method' => 'post', 'action' => '/posts/delete/1',
  7212. 'name', 'style' => 'display:none;'
  7213. ],
  7214. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7215. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7216. 'div' => ['style' => 'display:none;'],
  7217. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7218. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7219. ['input' => [
  7220. 'type' => 'hidden',
  7221. 'name' => '_Token[debug]',
  7222. 'value' => $tokenDebug,
  7223. 'autocomplete' => 'off',
  7224. ]],
  7225. '/div',
  7226. '/form',
  7227. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7228. 'Delete',
  7229. '/a'
  7230. ];
  7231. $this->assertHtml($expected, $result);
  7232. }
  7233. /**
  7234. * testPostLinkSecurityHashBlockMode method
  7235. *
  7236. * Test that postLink doesn't modify the fields in the containing form.
  7237. *
  7238. * postLink() calls inside open forms should not modify the field list
  7239. * for the form.
  7240. *
  7241. * @return void
  7242. */
  7243. public function testPostLinkSecurityHashBlockMode()
  7244. {
  7245. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7246. $hash .= '%3A';
  7247. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7248. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7249. $this->Form->control('title');
  7250. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7251. $result = $this->View->fetch('postLink');
  7252. $this->assertEquals(['title'], $this->Form->fields);
  7253. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7254. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7255. }
  7256. /**
  7257. * testPostLinkSecurityHashNoDebugMode method
  7258. *
  7259. * Test that security does not include debug token if debug is false.
  7260. *
  7261. * @return void
  7262. */
  7263. public function testPostLinkSecurityHashNoDebugMode()
  7264. {
  7265. Configure::write('debug', false);
  7266. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7267. $hash .= '%3Aid';
  7268. $this->View->setRequest($this->View->getRequest()
  7269. ->withParam('_Token.key', 'test'));
  7270. $result = $this->Form->postLink(
  7271. 'Delete',
  7272. '/posts/delete/1',
  7273. ['data' => ['id' => 1]]
  7274. );
  7275. $expected = [
  7276. 'form' => [
  7277. 'method' => 'post', 'action' => '/posts/delete/1',
  7278. 'name', 'style' => 'display:none;'
  7279. ],
  7280. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7281. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7282. 'div' => ['style' => 'display:none;'],
  7283. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7284. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7285. '/div',
  7286. '/form',
  7287. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7288. 'Delete',
  7289. '/a'
  7290. ];
  7291. $this->assertHtml($expected, $result);
  7292. }
  7293. /**
  7294. * testPostLinkNestedData method
  7295. *
  7296. * Test using postLink with N dimensional data.
  7297. *
  7298. * @return void
  7299. */
  7300. public function testPostLinkNestedData()
  7301. {
  7302. $data = [
  7303. 'one' => [
  7304. 'two' => [
  7305. 3, 4, 5
  7306. ]
  7307. ]
  7308. ];
  7309. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7310. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7311. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7312. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7313. }
  7314. /**
  7315. * testPostLinkAfterGetForm method
  7316. *
  7317. * Test creating postLinks after a GET form.
  7318. *
  7319. * @return void
  7320. */
  7321. public function testPostLinkAfterGetForm()
  7322. {
  7323. $this->View->setRequest($this->View->getRequest()
  7324. ->withParam('_csrfToken', 'testkey')
  7325. ->withParam('_Token', 'val'));
  7326. $this->Form->create($this->article, ['type' => 'get']);
  7327. $this->Form->end();
  7328. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7329. $tokenDebug = urlencode(json_encode([
  7330. '/posts/delete/1',
  7331. [],
  7332. []
  7333. ]));
  7334. $expected = [
  7335. 'form' => [
  7336. 'method' => 'post', 'action' => '/posts/delete/1',
  7337. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7338. ],
  7339. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7340. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7341. 'div' => ['style' => 'display:none;'],
  7342. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7343. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7344. ['input' => [
  7345. 'type' => 'hidden', 'name' => '_Token[debug]',
  7346. 'value' => $tokenDebug,
  7347. 'autocomplete' => 'off'
  7348. ]],
  7349. '/div',
  7350. '/form',
  7351. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7352. 'Delete',
  7353. '/a'
  7354. ];
  7355. $this->assertHtml($expected, $result);
  7356. }
  7357. /**
  7358. * testPostLinkFormBuffer method
  7359. *
  7360. * Test that postLink adds form tags to view block.
  7361. *
  7362. * @return void
  7363. */
  7364. public function testPostLinkFormBuffer()
  7365. {
  7366. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7367. $expected = [
  7368. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7369. 'Delete',
  7370. '/a'
  7371. ];
  7372. $this->assertHtml($expected, $result);
  7373. $result = $this->View->fetch('postLink');
  7374. $expected = [
  7375. 'form' => [
  7376. 'method' => 'post', 'action' => '/posts/delete/1',
  7377. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7378. ],
  7379. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7380. '/form'
  7381. ];
  7382. $this->assertHtml($expected, $result);
  7383. $result = $this->Form->postLink(
  7384. 'Delete',
  7385. '/posts/delete/2',
  7386. ['block' => true, 'method' => 'DELETE']
  7387. );
  7388. $expected = [
  7389. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7390. 'Delete',
  7391. '/a'
  7392. ];
  7393. $this->assertHtml($expected, $result);
  7394. $result = $this->View->fetch('postLink');
  7395. $expected = [
  7396. 'form' => [
  7397. 'method' => 'post', 'action' => '/posts/delete/1',
  7398. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7399. ],
  7400. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7401. '/form',
  7402. [
  7403. 'form' => [
  7404. 'method' => 'post', 'action' => '/posts/delete/2',
  7405. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7406. ],
  7407. ],
  7408. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7409. '/form'
  7410. ];
  7411. $this->assertHtml($expected, $result);
  7412. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7413. $expected = [
  7414. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7415. 'Delete',
  7416. '/a'
  7417. ];
  7418. $this->assertHtml($expected, $result);
  7419. $result = $this->View->fetch('foobar');
  7420. $expected = [
  7421. 'form' => [
  7422. 'method' => 'post', 'action' => '/posts/delete/1',
  7423. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7424. ],
  7425. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7426. '/form'
  7427. ];
  7428. $this->assertHtml($expected, $result);
  7429. }
  7430. /**
  7431. * testSubmitButton method
  7432. *
  7433. * @return void
  7434. */
  7435. public function testSubmitButton()
  7436. {
  7437. $result = $this->Form->submit('');
  7438. $expected = [
  7439. 'div' => ['class' => 'submit'],
  7440. 'input' => ['type' => 'submit', 'value' => ''],
  7441. '/div'
  7442. ];
  7443. $this->assertHtml($expected, $result);
  7444. $result = $this->Form->submit('Test Submit');
  7445. $expected = [
  7446. 'div' => ['class' => 'submit'],
  7447. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7448. '/div'
  7449. ];
  7450. $this->assertHtml($expected, $result);
  7451. $result = $this->Form->submit('Next >');
  7452. $expected = [
  7453. 'div' => ['class' => 'submit'],
  7454. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7455. '/div'
  7456. ];
  7457. $this->assertHtml($expected, $result);
  7458. $result = $this->Form->submit('Next >', ['escape' => false]);
  7459. $expected = [
  7460. 'div' => ['class' => 'submit'],
  7461. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7462. '/div'
  7463. ];
  7464. $this->assertHtml($expected, $result);
  7465. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7466. $expected = [
  7467. 'div' => ['class' => 'submit'],
  7468. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7469. '/div'
  7470. ];
  7471. $this->assertHtml($expected, $result);
  7472. }
  7473. /**
  7474. * testSubmitImage method
  7475. *
  7476. * Test image submit types.
  7477. *
  7478. * @return void
  7479. */
  7480. public function testSubmitImage()
  7481. {
  7482. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7483. $expected = [
  7484. 'div' => ['class' => 'submit'],
  7485. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7486. '/div'
  7487. ];
  7488. $this->assertHtml($expected, $result);
  7489. $result = $this->Form->submit('/relative/cake.power.gif');
  7490. $expected = [
  7491. 'div' => ['class' => 'submit'],
  7492. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7493. '/div'
  7494. ];
  7495. $this->assertHtml($expected, $result);
  7496. $result = $this->Form->submit('cake.power.gif');
  7497. $expected = [
  7498. 'div' => ['class' => 'submit'],
  7499. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7500. '/div'
  7501. ];
  7502. $this->assertHtml($expected, $result);
  7503. $result = $this->Form->submit('Not.an.image');
  7504. $expected = [
  7505. 'div' => ['class' => 'submit'],
  7506. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7507. '/div'
  7508. ];
  7509. $this->assertHtml($expected, $result);
  7510. }
  7511. /**
  7512. * testSubmitUnlockedByDefault method
  7513. *
  7514. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7515. * be submitted at a time.
  7516. *
  7517. * @return void
  7518. */
  7519. public function testSubmitUnlockedByDefault()
  7520. {
  7521. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'secured'));
  7522. $this->Form->submit('Go go');
  7523. $this->Form->submit('Save', ['name' => 'save']);
  7524. $result = $this->Form->unlockField();
  7525. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7526. }
  7527. /**
  7528. * testSubmitImageTimestamp method
  7529. *
  7530. * Test submit image with timestamps.
  7531. *
  7532. * @return void
  7533. */
  7534. public function testSubmitImageTimestamp()
  7535. {
  7536. Configure::write('Asset.timestamp', 'force');
  7537. $result = $this->Form->submit('cake.power.gif');
  7538. $expected = [
  7539. 'div' => ['class' => 'submit'],
  7540. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7541. '/div'
  7542. ];
  7543. $this->assertHtml($expected, $result);
  7544. }
  7545. /**
  7546. * testDateTimeWithGetForms method
  7547. *
  7548. * Test that datetime() works with GET style forms.
  7549. *
  7550. * @return void
  7551. */
  7552. public function testDateTimeWithGetForms()
  7553. {
  7554. extract($this->dateRegex);
  7555. $this->Form->create($this->article, ['type' => 'get']);
  7556. $result = $this->Form->datetime('created');
  7557. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7558. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7559. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7560. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7561. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7562. }
  7563. /**
  7564. * testForMagicControlNonExistingNorValidated method
  7565. *
  7566. * @return void
  7567. */
  7568. public function testForMagicControlNonExistingNorValidated()
  7569. {
  7570. $this->Form->create($this->article);
  7571. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7572. $result = $this->Form->control('non_existing_nor_validated');
  7573. $expected = [
  7574. 'label' => ['for' => 'non-existing-nor-validated'],
  7575. 'Non Existing Nor Validated',
  7576. '/label',
  7577. 'input' => [
  7578. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7579. 'id' => 'non-existing-nor-validated'
  7580. ]
  7581. ];
  7582. $this->assertHtml($expected, $result);
  7583. $result = $this->Form->control('non_existing_nor_validated', [
  7584. 'val' => 'my value'
  7585. ]);
  7586. $expected = [
  7587. 'label' => ['for' => 'non-existing-nor-validated'],
  7588. 'Non Existing Nor Validated',
  7589. '/label',
  7590. 'input' => [
  7591. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7592. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7593. ]
  7594. ];
  7595. $this->assertHtml($expected, $result);
  7596. $this->View->setRequest(
  7597. $this->View->getRequest()->withData('non_existing_nor_validated', 'CakePHP magic')
  7598. );
  7599. $this->Form->create($this->article);
  7600. $result = $this->Form->control('non_existing_nor_validated');
  7601. $expected = [
  7602. 'label' => ['for' => 'non-existing-nor-validated'],
  7603. 'Non Existing Nor Validated',
  7604. '/label',
  7605. 'input' => [
  7606. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7607. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7608. ]
  7609. ];
  7610. $this->assertHtml($expected, $result);
  7611. }
  7612. /**
  7613. * testFormMagicControlLabel method
  7614. *
  7615. * @return void
  7616. */
  7617. public function testFormMagicControlLabel()
  7618. {
  7619. $this->getTableLocator()->get('Contacts', [
  7620. 'className' => __NAMESPACE__ . '\ContactsTable'
  7621. ]);
  7622. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7623. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7624. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7625. $expected = [
  7626. 'label' => ['for' => 'contacts-name'],
  7627. 'My label',
  7628. '/label',
  7629. 'input' => [
  7630. 'type' => 'text', 'name' => 'Contacts[name]',
  7631. 'id' => 'contacts-name', 'maxlength' => '255'
  7632. ]
  7633. ];
  7634. $this->assertHtml($expected, $result);
  7635. $result = $this->Form->control('name', [
  7636. 'label' => ['class' => 'mandatory']
  7637. ]);
  7638. $expected = [
  7639. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7640. 'Name',
  7641. '/label',
  7642. 'input' => [
  7643. 'type' => 'text', 'name' => 'name',
  7644. 'id' => 'name', 'maxlength' => '255'
  7645. ]
  7646. ];
  7647. $this->assertHtml($expected, $result);
  7648. $result = $this->Form->control('name', [
  7649. 'div' => false,
  7650. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7651. ]);
  7652. $expected = [
  7653. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7654. 'My label',
  7655. '/label',
  7656. 'input' => [
  7657. 'type' => 'text', 'name' => 'name',
  7658. 'id' => 'name', 'maxlength' => '255'
  7659. ]
  7660. ];
  7661. $this->assertHtml($expected, $result);
  7662. $result = $this->Form->control('Contact.name', [
  7663. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7664. ]);
  7665. $expected = [
  7666. 'label' => ['for' => 'my_id'],
  7667. 'Name',
  7668. '/label',
  7669. 'input' => [
  7670. 'type' => 'text', 'name' => 'Contact[name]',
  7671. 'id' => 'my_id', 'maxlength' => '255'
  7672. ]
  7673. ];
  7674. $this->assertHtml($expected, $result);
  7675. $result = $this->Form->control('1.id');
  7676. $expected = ['input' => [
  7677. 'type' => 'hidden', 'name' => '1[id]',
  7678. 'id' => '1-id'
  7679. ]];
  7680. $this->assertHtml($expected, $result);
  7681. $result = $this->Form->control('1.name');
  7682. $expected = [
  7683. 'label' => ['for' => '1-name'],
  7684. 'Name',
  7685. '/label',
  7686. 'input' => [
  7687. 'type' => 'text', 'name' => '1[name]',
  7688. 'id' => '1-name', 'maxlength' => '255'
  7689. ]
  7690. ];
  7691. $this->assertHtml($expected, $result);
  7692. }
  7693. /**
  7694. * testFormEnd method
  7695. *
  7696. * @return void
  7697. */
  7698. public function testFormEnd()
  7699. {
  7700. $this->assertEquals('</form>', $this->Form->end());
  7701. }
  7702. /**
  7703. * testMultiRecordForm method
  7704. *
  7705. * Test the generation of fields for a multi record form.
  7706. *
  7707. * @return void
  7708. */
  7709. public function testMultiRecordForm()
  7710. {
  7711. $this->loadFixtures('Articles', 'Comments');
  7712. $articles = $this->getTableLocator()->get('Articles');
  7713. $articles->hasMany('Comments');
  7714. $comment = new Entity(['comment' => 'Value']);
  7715. $article = new Article(['comments' => [$comment]]);
  7716. $this->Form->create([$article]);
  7717. $result = $this->Form->control('0.comments.1.comment');
  7718. //@codingStandardsIgnoreStart
  7719. $expected = [
  7720. 'div' => ['class' => 'input textarea'],
  7721. 'label' => ['for' => '0-comments-1-comment'],
  7722. 'Comment',
  7723. '/label',
  7724. 'textarea' => [
  7725. 'name',
  7726. 'id' => '0-comments-1-comment',
  7727. 'rows' => 5
  7728. ],
  7729. '/textarea',
  7730. '/div'
  7731. ];
  7732. //@codingStandardsIgnoreEnd
  7733. $this->assertHtml($expected, $result);
  7734. $result = $this->Form->control('0.comments.0.comment');
  7735. //@codingStandardsIgnoreStart
  7736. $expected = [
  7737. 'div' => ['class' => 'input textarea'],
  7738. 'label' => ['for' => '0-comments-0-comment'],
  7739. 'Comment',
  7740. '/label',
  7741. 'textarea' => [
  7742. 'name',
  7743. 'id' => '0-comments-0-comment',
  7744. 'rows' => 5
  7745. ],
  7746. 'Value',
  7747. '/textarea',
  7748. '/div'
  7749. ];
  7750. //@codingStandardsIgnoreEnd
  7751. $this->assertHtml($expected, $result);
  7752. $comment->setError('comment', ['Not valid']);
  7753. $result = $this->Form->control('0.comments.0.comment');
  7754. //@codingStandardsIgnoreStart
  7755. $expected = [
  7756. 'div' => ['class' => 'input textarea error'],
  7757. 'label' => ['for' => '0-comments-0-comment'],
  7758. 'Comment',
  7759. '/label',
  7760. 'textarea' => [
  7761. 'name',
  7762. 'class' => 'form-error',
  7763. 'id' => '0-comments-0-comment',
  7764. 'rows' => 5
  7765. ],
  7766. 'Value',
  7767. '/textarea',
  7768. ['div' => ['class' => 'error-message']],
  7769. 'Not valid',
  7770. '/div',
  7771. '/div'
  7772. ];
  7773. //@codingStandardsIgnoreEnd
  7774. $this->assertHtml($expected, $result);
  7775. $this->getTableLocator()->get('Comments')
  7776. ->getValidator('default')
  7777. ->allowEmpty('comment', false);
  7778. $result = $this->Form->control('0.comments.1.comment');
  7779. //@codingStandardsIgnoreStart
  7780. $expected = [
  7781. 'div' => ['class' => 'input textarea required'],
  7782. 'label' => ['for' => '0-comments-1-comment'],
  7783. 'Comment',
  7784. '/label',
  7785. 'textarea' => [
  7786. 'name',
  7787. 'required' => 'required',
  7788. 'id' => '0-comments-1-comment',
  7789. 'rows' => 5,
  7790. ],
  7791. '/textarea',
  7792. '/div'
  7793. ];
  7794. //@codingStandardsIgnoreEnd
  7795. $this->assertHtml($expected, $result);
  7796. }
  7797. /**
  7798. * testHtml5Controls method
  7799. *
  7800. * Test that some html5 inputs + FormHelper::__call() work.
  7801. *
  7802. * @return void
  7803. */
  7804. public function testHtml5Controls()
  7805. {
  7806. $result = $this->Form->email('User.email');
  7807. $expected = [
  7808. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7809. ];
  7810. $this->assertHtml($expected, $result);
  7811. $result = $this->Form->search('User.query');
  7812. $expected = [
  7813. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7814. ];
  7815. $this->assertHtml($expected, $result);
  7816. $result = $this->Form->search('User.query', ['value' => 'test']);
  7817. $expected = [
  7818. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7819. ];
  7820. $this->assertHtml($expected, $result);
  7821. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7822. $expected = [
  7823. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7824. ];
  7825. $this->assertHtml($expected, $result);
  7826. }
  7827. /**
  7828. * testHtml5ControlWithControl method
  7829. *
  7830. * Test accessing html5 inputs through control().
  7831. *
  7832. * @return void
  7833. */
  7834. public function testHtml5ControlWithControl()
  7835. {
  7836. $this->Form->create();
  7837. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7838. $result = $this->Form->control('website', [
  7839. 'type' => 'url',
  7840. 'val' => 'http://domain.tld',
  7841. 'label' => false
  7842. ]);
  7843. $expected = [
  7844. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7845. ];
  7846. $this->assertHtml($expected, $result);
  7847. }
  7848. /**
  7849. * testHtml5ControlException method
  7850. *
  7851. * Test errors when field name is missing.
  7852. *
  7853. * @return void
  7854. */
  7855. public function testHtml5ControlException()
  7856. {
  7857. $this->expectException(\Cake\Core\Exception\Exception::class);
  7858. $this->Form->email();
  7859. }
  7860. /**
  7861. * tests fields that are required use custom validation messages
  7862. *
  7863. * @return void
  7864. */
  7865. public function testHtml5ErrorMessage()
  7866. {
  7867. $this->Form->setConfig('autoSetCustomValidity', true);
  7868. $validator = (new \Cake\Validation\Validator())
  7869. ->requirePresence('email', true, 'Custom error message')
  7870. ->requirePresence('password')
  7871. ->alphaNumeric('password')
  7872. ->notBlank('phone');
  7873. $table = $this->getTableLocator()->get('Contacts', [
  7874. 'className' => __NAMESPACE__ . '\ContactsTable'
  7875. ]);
  7876. $table->setValidator('default', $validator);
  7877. $contact = new Entity();
  7878. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7879. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7880. $result = $this->Form->control('password');
  7881. $expected = [
  7882. 'label' => ['for' => 'password'],
  7883. 'Password',
  7884. '/label',
  7885. 'input' => [
  7886. 'id' => 'password',
  7887. 'name' => 'password',
  7888. 'type' => 'password',
  7889. 'value' => '',
  7890. 'required' => 'required',
  7891. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7892. 'oninvalid' => 'this.setCustomValidity(&#039;This field is required&#039;)',
  7893. ]
  7894. ];
  7895. $this->assertHtml($expected, $result);
  7896. $result = $this->Form->control('phone');
  7897. $expected = [
  7898. 'label' => ['for' => 'phone'],
  7899. 'Phone',
  7900. '/label',
  7901. 'input' => [
  7902. 'id' => 'phone',
  7903. 'name' => 'phone',
  7904. 'type' => 'tel',
  7905. 'value' => '',
  7906. 'maxlength' => 255,
  7907. 'required' => 'required',
  7908. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7909. 'oninvalid' => 'this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  7910. ]
  7911. ];
  7912. $this->assertHtml($expected, $result);
  7913. $result = $this->Form->control('email');
  7914. $expected = [
  7915. 'label' => ['for' => 'email'],
  7916. 'Email',
  7917. '/label',
  7918. 'input' => [
  7919. 'id' => 'email',
  7920. 'name' => 'email',
  7921. 'type' => 'email',
  7922. 'value' => '',
  7923. 'maxlength' => 255,
  7924. 'required' => 'required',
  7925. 'onvalid' => 'this.setCustomValidity(&#039;&#039;)',
  7926. 'oninvalid' => 'this.setCustomValidity(&#039;Custom error message&#039;)',
  7927. ]
  7928. ];
  7929. $this->assertHtml($expected, $result);
  7930. }
  7931. /**
  7932. * tests that custom validation messages are in templateVars
  7933. *
  7934. * @return void
  7935. */
  7936. public function testHtml5ErrorMessageInTemplateVars()
  7937. {
  7938. $validator = (new \Cake\Validation\Validator())
  7939. ->requirePresence('email', true, 'Custom error "message" & entities')
  7940. ->requirePresence('password')
  7941. ->alphaNumeric('password')
  7942. ->notBlank('phone');
  7943. $table = $this->getTableLocator()->get('Contacts', [
  7944. 'className' => __NAMESPACE__ . '\ContactsTable'
  7945. ]);
  7946. $table->setValidator('default', $validator);
  7947. $contact = new Entity();
  7948. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7949. $this->Form->setTemplates([
  7950. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7951. 'inputContainer' => '{{content}}'
  7952. ]);
  7953. $result = $this->Form->control('password');
  7954. $expected = [
  7955. 'label' => ['for' => 'password'],
  7956. 'Password',
  7957. '/label',
  7958. 'input' => [
  7959. 'id' => 'password',
  7960. 'name' => 'password',
  7961. 'type' => 'password',
  7962. 'value' => '',
  7963. 'required' => 'required',
  7964. 'data-message' => 'This field is required',
  7965. ]
  7966. ];
  7967. $this->assertHtml($expected, $result);
  7968. $result = $this->Form->control('phone');
  7969. $expected = [
  7970. 'label' => ['for' => 'phone'],
  7971. 'Phone',
  7972. '/label',
  7973. 'input' => [
  7974. 'id' => 'phone',
  7975. 'name' => 'phone',
  7976. 'type' => 'tel',
  7977. 'value' => '',
  7978. 'maxlength' => 255,
  7979. 'required' => 'required',
  7980. 'data-message' => 'This field cannot be left empty',
  7981. ],
  7982. ];
  7983. $this->assertHtml($expected, $result);
  7984. $result = $this->Form->control('email', [
  7985. 'templateVars' => [
  7986. 'custom' => 'data-custom="1"'
  7987. ]
  7988. ]);
  7989. $expected = [
  7990. 'label' => ['for' => 'email'],
  7991. 'Email',
  7992. '/label',
  7993. 'input' => [
  7994. 'id' => 'email',
  7995. 'name' => 'email',
  7996. 'type' => 'email',
  7997. 'value' => '',
  7998. 'maxlength' => 255,
  7999. 'required' => 'required',
  8000. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  8001. 'data-custom' => '1',
  8002. ],
  8003. ];
  8004. $this->assertHtml($expected, $result);
  8005. }
  8006. /**
  8007. * testRequiredAttribute method
  8008. *
  8009. * Tests that formhelper sets required attributes.
  8010. *
  8011. * @return void
  8012. */
  8013. public function testRequiredAttribute()
  8014. {
  8015. $this->article['required'] = [
  8016. 'title' => true,
  8017. 'body' => false,
  8018. ];
  8019. $this->Form->create($this->article);
  8020. $result = $this->Form->control('title');
  8021. $expected = [
  8022. 'div' => ['class' => 'input text required'],
  8023. 'label' => ['for' => 'title'],
  8024. 'Title',
  8025. '/label',
  8026. 'input' => [
  8027. 'type' => 'text',
  8028. 'name' => 'title',
  8029. 'id' => 'title',
  8030. 'required' => 'required',
  8031. ],
  8032. '/div'
  8033. ];
  8034. $this->assertHtml($expected, $result);
  8035. $result = $this->Form->control('title', ['required' => false]);
  8036. $this->assertNotContains('required', $result);
  8037. $result = $this->Form->control('body');
  8038. $expected = [
  8039. 'div' => ['class' => 'input text'],
  8040. 'label' => ['for' => 'body'],
  8041. 'Body',
  8042. '/label',
  8043. 'input' => [
  8044. 'type' => 'text',
  8045. 'name' => 'body',
  8046. 'id' => 'body',
  8047. ],
  8048. '/div'
  8049. ];
  8050. $this->assertHtml($expected, $result);
  8051. $result = $this->Form->control('body', ['required' => true]);
  8052. $this->assertContains('required', $result);
  8053. }
  8054. /**
  8055. * testControlsNotNested method
  8056. *
  8057. * Tests that it is possible to put inputs outside of the label.
  8058. *
  8059. * @return void
  8060. */
  8061. public function testControlsNotNested()
  8062. {
  8063. $this->Form->setTemplates([
  8064. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  8065. 'formGroup' => '{{input}}{{label}}',
  8066. ]);
  8067. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  8068. $expected = [
  8069. 'div' => ['class' => 'input checkbox'],
  8070. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8071. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8072. 'label' => ['for' => 'foo'],
  8073. 'Foo',
  8074. '/label',
  8075. '/div'
  8076. ];
  8077. $this->assertHtml($expected, $result);
  8078. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  8079. $expected = [
  8080. 'div' => ['class' => 'input checkbox'],
  8081. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8082. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8083. '/div'
  8084. ];
  8085. $this->assertHtml($expected, $result);
  8086. $result = $this->Form->control('confirm', [
  8087. 'type' => 'radio',
  8088. 'options' => ['Y' => 'Yes', 'N' => 'No']
  8089. ]);
  8090. $expected = [
  8091. 'div' => ['class' => 'input radio'],
  8092. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  8093. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  8094. ['label' => ['for' => 'confirm-y']],
  8095. 'Yes',
  8096. '/label',
  8097. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  8098. ['label' => ['for' => 'confirm-n']],
  8099. 'No',
  8100. '/label',
  8101. '<label',
  8102. 'Confirm',
  8103. '/label',
  8104. '/div',
  8105. ];
  8106. $this->assertHtml($expected, $result);
  8107. $result = $this->Form->select('category', ['1', '2'], [
  8108. 'multiple' => 'checkbox',
  8109. 'name' => 'fish',
  8110. ]);
  8111. $expected = [
  8112. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  8113. ['div' => ['class' => 'checkbox']],
  8114. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  8115. ['label' => ['for' => 'fish-0']],
  8116. '1',
  8117. '/label',
  8118. '/div',
  8119. ['div' => ['class' => 'checkbox']],
  8120. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  8121. ['label' => ['for' => 'fish-1']],
  8122. '2',
  8123. '/label',
  8124. '/div'
  8125. ];
  8126. $this->assertHtml($expected, $result);
  8127. }
  8128. /**
  8129. * testControlContainerTemplates method
  8130. *
  8131. * Test that *Container templates are used by input.
  8132. *
  8133. * @return void
  8134. */
  8135. public function testControlContainerTemplates()
  8136. {
  8137. $this->Form->setTemplates([
  8138. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  8139. 'radioContainer' => '<div class="rad">{{content}}</div>',
  8140. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  8141. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  8142. ]);
  8143. $this->article['errors'] = [
  8144. 'Article' => ['published' => 'error message']
  8145. ];
  8146. $this->Form->create($this->article);
  8147. $result = $this->Form->control('accept', [
  8148. 'type' => 'checkbox'
  8149. ]);
  8150. $expected = [
  8151. 'div' => ['class' => 'check'],
  8152. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  8153. 'label' => ['for' => 'accept'],
  8154. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  8155. 'Accept',
  8156. '/label',
  8157. '/div'
  8158. ];
  8159. $this->assertHtml($expected, $result);
  8160. $result = $this->Form->control('accept', [
  8161. 'type' => 'radio',
  8162. 'options' => ['Y', 'N']
  8163. ]);
  8164. $this->assertContains('<div class="rad">', $result);
  8165. $result = $this->Form->control('Article.published', [
  8166. 'type' => 'radio',
  8167. 'options' => ['Y', 'N']
  8168. ]);
  8169. $this->assertContains('<div class="rad err">', $result);
  8170. $result = $this->Form->control('Article.created', [
  8171. 'type' => 'datetime',
  8172. ]);
  8173. $this->assertContains('<div class="dt">', $result);
  8174. }
  8175. /**
  8176. * testFormGroupTemplates method
  8177. *
  8178. * Test that *Container templates are used by input.
  8179. *
  8180. * @return void
  8181. */
  8182. public function testFormGroupTemplates()
  8183. {
  8184. $this->Form->setTemplates([
  8185. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  8186. ]);
  8187. $this->Form->create($this->article);
  8188. $result = $this->Form->control('accept', [
  8189. 'type' => 'radio',
  8190. 'options' => ['Y', 'N']
  8191. ]);
  8192. $this->assertContains('<div class="radio">', $result);
  8193. }
  8194. /**
  8195. * testResetTemplates method
  8196. *
  8197. * Test resetting templates.
  8198. *
  8199. * @return void
  8200. */
  8201. public function testResetTemplates()
  8202. {
  8203. $this->Form->setTemplates(['input' => '<input/>']);
  8204. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  8205. $this->Form->resetTemplates();
  8206. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  8207. }
  8208. /**
  8209. * testContext method
  8210. *
  8211. * Test the context method.
  8212. *
  8213. * @return void
  8214. */
  8215. public function testContext()
  8216. {
  8217. $result = $this->Form->context();
  8218. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  8219. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  8220. $this->assertSame($mock, $this->Form->context($mock));
  8221. $this->assertSame($mock, $this->Form->context());
  8222. }
  8223. /**
  8224. * testAutoDomId method
  8225. *
  8226. * @return void
  8227. */
  8228. public function testAutoDomId()
  8229. {
  8230. $result = $this->Form->text('field', ['id' => true]);
  8231. $expected = [
  8232. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  8233. ];
  8234. $this->assertHtml($expected, $result);
  8235. // Ensure id => doesn't cause problem when multiple inputs are generated.
  8236. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  8237. $expected = [
  8238. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  8239. ['label' => ['for' => 'field-0']],
  8240. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  8241. 'option A',
  8242. '/label',
  8243. ['label' => ['for' => 'field-1']],
  8244. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  8245. 'option B',
  8246. '/label',
  8247. ];
  8248. $this->assertHtml($expected, $result);
  8249. $result = $this->Form->select(
  8250. 'multi_field',
  8251. ['first', 'second'],
  8252. ['multiple' => 'checkbox', 'id' => true]
  8253. );
  8254. $expected = [
  8255. 'input' => [
  8256. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  8257. ],
  8258. ['div' => ['class' => 'checkbox']],
  8259. ['label' => ['for' => 'multi-field-0']],
  8260. ['input' => [
  8261. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8262. 'value' => '0', 'id' => 'multi-field-0'
  8263. ]],
  8264. 'first',
  8265. '/label',
  8266. '/div',
  8267. ['div' => ['class' => 'checkbox']],
  8268. ['label' => ['for' => 'multi-field-1']],
  8269. ['input' => [
  8270. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8271. 'value' => '1', 'id' => 'multi-field-1'
  8272. ]],
  8273. 'second',
  8274. '/label',
  8275. '/div',
  8276. ];
  8277. $this->assertHtml($expected, $result);
  8278. }
  8279. /**
  8280. * Test the basic setters and getters for value sources
  8281. *
  8282. * @return void
  8283. */
  8284. public function testFormValueSourcesSettersGetters()
  8285. {
  8286. $this->View->setRequest($this->View->getRequest()
  8287. ->withData('id', '1')
  8288. ->withQueryParams(['id' => '2']));
  8289. $expected = ['context'];
  8290. $result = $this->Form->getValueSources();
  8291. $this->assertEquals($expected, $result);
  8292. $expected = ['query', 'data', 'context'];
  8293. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8294. $result = $this->Form->getValueSources();
  8295. $this->assertEquals($expected, $result);
  8296. $this->Form->setValueSources(['context']);
  8297. $expected = '1';
  8298. $result = $this->Form->getSourceValue('id');
  8299. $this->assertEquals($expected, $result);
  8300. $this->Form->setValueSources('query');
  8301. $expected = ['query'];
  8302. $result = $this->Form->getValueSources();
  8303. $this->assertEquals($expected, $result);
  8304. $expected = '2';
  8305. $result = $this->Form->getSourceValue('id');
  8306. $this->assertEquals($expected, $result);
  8307. $this->Form->setValueSources(['data']);
  8308. $expected = '1';
  8309. $result = $this->Form->getSourceValue('id');
  8310. $this->assertEquals($expected, $result);
  8311. $this->Form->setValueSources(['query', 'data']);
  8312. $expected = '2';
  8313. $result = $this->Form->getSourceValue('id');
  8314. $this->assertEquals($expected, $result);
  8315. }
  8316. /**
  8317. * Tests the different input rendering values based on sources values switching
  8318. *
  8319. * @return void
  8320. */
  8321. public function testFormValueSourcesSingleSwitchRendering()
  8322. {
  8323. $this->loadFixtures('Articles');
  8324. $articles = $this->getTableLocator()->get('Articles');
  8325. $article = new Article();
  8326. $articles->patchEntity($article, ['id' => '3']);
  8327. $this->Form->create($article);
  8328. $this->Form->setValueSources(['context']);
  8329. $result = $this->Form->control('id');
  8330. $expected = [
  8331. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8332. ];
  8333. $this->assertHtml($expected, $result);
  8334. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  8335. $this->Form->setValueSources(['query']);
  8336. $result = $this->Form->control('id');
  8337. $expected = [
  8338. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8339. ];
  8340. $this->assertHtml($expected, $result);
  8341. $this->View->setRequest($this->View->getRequest()
  8342. ->withQueryParams(['id' => '5a'])
  8343. ->withData('id', '5b'));
  8344. $this->Form->setValueSources(['context']);
  8345. $this->Form->create($article);
  8346. $result = $this->Form->control('id');
  8347. $expected = [
  8348. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8349. ];
  8350. $this->assertHtml($expected, $result);
  8351. $this->Form->setValueSources(['data']);
  8352. $this->Form->create($article);
  8353. $result = $this->Form->control('id');
  8354. $expected = [
  8355. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8356. ];
  8357. $this->assertHtml($expected, $result);
  8358. $this->Form->setValueSources(['query']);
  8359. $result = $this->Form->control('id');
  8360. $expected = [
  8361. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8362. ];
  8363. $this->assertHtml($expected, $result);
  8364. }
  8365. /**
  8366. * Tests the different input rendering values based on sources values switching while supplying
  8367. * an entity (base context) and multiple sources (such as data, query)
  8368. *
  8369. * @return void
  8370. */
  8371. public function testFormValueSourcesListSwitchRendering()
  8372. {
  8373. $this->loadFixtures('Articles');
  8374. $articles = $this->getTableLocator()->get('Articles');
  8375. $article = new Article();
  8376. $articles->patchEntity($article, ['id' => '3']);
  8377. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  8378. $this->Form->create($article);
  8379. $this->Form->setValueSources(['context', 'query']);
  8380. $result = $this->Form->control('id');
  8381. $expected = [
  8382. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8383. ];
  8384. $this->assertHtml($expected, $result);
  8385. $this->Form->setValueSources(['query', 'context']);
  8386. $result = $this->Form->control('id');
  8387. $expected = [
  8388. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8389. ];
  8390. $this->assertHtml($expected, $result);
  8391. $this->Form->setValueSources(['data', 'query', 'context']);
  8392. $result = $this->Form->control('id');
  8393. $expected = [
  8394. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8395. ];
  8396. $this->assertHtml($expected, $result);
  8397. $this->View->setRequest($this->View->getRequest()
  8398. ->withData('id', '8')
  8399. ->withQueryParams(['id' => '9']));
  8400. $this->Form->setValueSources(['data', 'query', 'context']);
  8401. $result = $this->Form->control('id');
  8402. $expected = [
  8403. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8404. ];
  8405. $this->assertHtml($expected, $result);
  8406. }
  8407. /**
  8408. * Test the different form input renderings based on values sources switchings through form options
  8409. *
  8410. * @return void
  8411. */
  8412. public function testFormValueSourcesSwitchViaOptionsRendering()
  8413. {
  8414. $this->loadFixtures('Articles');
  8415. $articles = $this->getTableLocator()->get('Articles');
  8416. $article = new Article();
  8417. $articles->patchEntity($article, ['id' => '3']);
  8418. $this->View->setRequest(
  8419. $this->View->getRequest()->withData('id', 4)->withQueryParams(['id' => 5])
  8420. );
  8421. $this->Form->create($article, ['valueSources' => 'query']);
  8422. $result = $this->Form->control('id');
  8423. $expected = [
  8424. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8425. ];
  8426. $this->assertHtml($expected, $result);
  8427. $result = $this->Form->getSourceValue('id');
  8428. $this->assertEquals('5', $result);
  8429. $this->Form->setValueSources(['context']);
  8430. $this->Form->create($article, ['valueSources' => 'query']);
  8431. $result = $this->Form->control('id');
  8432. $expected = [
  8433. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8434. ];
  8435. $this->assertHtml($expected, $result);
  8436. $result = $this->Form->getSourceValue('id');
  8437. $this->assertEquals('5', $result);
  8438. $this->Form->setValueSources(['query']);
  8439. $this->Form->create($article, ['valueSources' => 'data']);
  8440. $result = $this->Form->control('id');
  8441. $expected = [
  8442. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8443. ];
  8444. $this->assertHtml($expected, $result);
  8445. $result = $this->Form->getSourceValue('id');
  8446. $this->assertEquals('4', $result);
  8447. $this->Form->setValueSources(['query']);
  8448. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8449. $result = $this->Form->control('id');
  8450. $expected = [
  8451. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8452. ];
  8453. $this->assertHtml($expected, $result);
  8454. $result = $this->Form->getSourceValue('id');
  8455. $this->assertEquals('4', $result);
  8456. }
  8457. /**
  8458. * Test the different form input renderings based on values sources switchings through form options
  8459. *
  8460. * @return void
  8461. */
  8462. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8463. {
  8464. $this->loadFixtures('Articles');
  8465. $articles = $this->getTableLocator()->get('Articles');
  8466. $article = new Article();
  8467. $articles->patchEntity($article, ['id' => '3']);
  8468. $this->View->setRequest(
  8469. $this->View->getRequest()->withData('id', 10)->withQueryParams(['id' => 11])
  8470. );
  8471. $this->Form->setValueSources(['context'])
  8472. ->create($article, ['valueSources' => ['query', 'data']]);
  8473. $result = $this->Form->control('id');
  8474. $expected = [
  8475. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8476. ];
  8477. $this->assertHtml($expected, $result);
  8478. $result = $this->Form->getSourceValue('id');
  8479. $this->assertEquals('11', $result);
  8480. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  8481. $this->Form->setValueSources(['context'])
  8482. ->create($article, ['valueSources' => ['query', 'data']]);
  8483. $result = $this->Form->control('id');
  8484. $expected = [
  8485. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8486. ];
  8487. $this->assertHtml($expected, $result);
  8488. $result = $this->Form->getSourceValue('id');
  8489. $this->assertEquals('10', $result);
  8490. }
  8491. /**
  8492. * Test the different form values sources resetting through From::end();
  8493. *
  8494. * @return void
  8495. */
  8496. public function testFormValueSourcesResetViaEnd()
  8497. {
  8498. $expected = ['context'];
  8499. $result = $this->Form->getValueSources();
  8500. $this->assertEquals($expected, $result);
  8501. $expected = ['query', 'context', 'data'];
  8502. $this->Form->setValueSources(['query', 'context', 'data']);
  8503. $result = $this->Form->getValueSources();
  8504. $this->assertEquals($expected, $result);
  8505. $this->Form->create();
  8506. $result = $this->Form->getValueSources();
  8507. $this->assertEquals($expected, $result);
  8508. $this->Form->end();
  8509. $result = $this->Form->getValueSources();
  8510. $this->assertEquals(['context'], $result);
  8511. }
  8512. /**
  8513. * Test sources values defaults handling
  8514. *
  8515. * @return void
  8516. */
  8517. public function testFormValueSourcesDefaults()
  8518. {
  8519. $this->View->setRequest(
  8520. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  8521. );
  8522. $this->Form->create();
  8523. $result = $this->Form->password('password');
  8524. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8525. $this->assertHtml($expected, $result);
  8526. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8527. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8528. $this->assertHtml($expected, $result);
  8529. $this->Form->setValueSources('query');
  8530. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8531. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8532. $this->assertHtml($expected, $result);
  8533. $this->Form->setValueSources('data');
  8534. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8535. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8536. $this->assertHtml($expected, $result);
  8537. }
  8538. /**
  8539. * Test sources values schema defaults handling
  8540. *
  8541. * @return void
  8542. */
  8543. public function testSourcesValueDoesntExistPassThrough()
  8544. {
  8545. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  8546. $articles = $this->getTableLocator()->get('Articles');
  8547. $entity = $articles->newEntity();
  8548. $this->Form->create($entity);
  8549. $this->Form->setValueSources(['query', 'context']);
  8550. $result = $this->Form->getSourceValue('category');
  8551. $this->assertEquals('sesame-cookies', $result);
  8552. $this->Form->setValueSources(['context', 'query']);
  8553. $result = $this->Form->getSourceValue('category');
  8554. $this->assertEquals('sesame-cookies', $result);
  8555. }
  8556. /**
  8557. * testNestedLabelInput method
  8558. *
  8559. * Test the `nestedInput` parameter
  8560. *
  8561. * @group deprecated
  8562. * @return void
  8563. */
  8564. public function testNestedLabelInput()
  8565. {
  8566. $this->deprecated(function () {
  8567. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8568. $expected = [
  8569. 'div' => ['class' => 'input text'],
  8570. 'label' => ['for' => 'foo'],
  8571. ['input' => [
  8572. 'type' => 'text',
  8573. 'name' => 'foo',
  8574. 'id' => 'foo'
  8575. ]],
  8576. 'Foo',
  8577. '/label',
  8578. '/div'
  8579. ];
  8580. $this->assertHtml($expected, $result);
  8581. });
  8582. }
  8583. /**
  8584. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8585. *
  8586. * This test makes sure `false` excludes the label from the render
  8587. *
  8588. * @return void
  8589. */
  8590. public function testControlLabelManipulationDisableLabels()
  8591. {
  8592. $result = $this->Form->control('test', [
  8593. 'type' => 'radio',
  8594. 'options' => ['A', 'B'],
  8595. 'labelOptions' => false
  8596. ]);
  8597. $expected = [
  8598. ['div' => ['class' => 'input radio']],
  8599. '<label',
  8600. 'Test',
  8601. '/label',
  8602. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8603. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8604. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8605. '/div',
  8606. ];
  8607. $this->assertHtml($expected, $result);
  8608. $result = $this->Form->control('checkbox1', [
  8609. 'label' => 'My checkboxes',
  8610. 'multiple' => 'checkbox',
  8611. 'type' => 'select',
  8612. 'options' => [
  8613. ['text' => 'First Checkbox', 'value' => 1],
  8614. ['text' => 'Second Checkbox', 'value' => 2]
  8615. ],
  8616. 'labelOptions' => false
  8617. ]);
  8618. $expected = [
  8619. ['div' => ['class' => 'input select']],
  8620. ['label' => ['for' => 'checkbox1']],
  8621. 'My checkboxes',
  8622. '/label',
  8623. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8624. ['div' => ['class' => 'checkbox']],
  8625. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8626. '/div',
  8627. ['div' => ['class' => 'checkbox']],
  8628. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8629. '/div',
  8630. '/div'
  8631. ];
  8632. $this->assertHtml($expected, $result);
  8633. }
  8634. /**
  8635. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8636. *
  8637. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8638. * added to the class if checked.
  8639. *
  8640. * Also checks to make sure any custom attributes are rendered correctly
  8641. *
  8642. * @return void
  8643. */
  8644. public function testControlLabelManipulationRadios()
  8645. {
  8646. $result = $this->Form->control('test', [
  8647. 'type' => 'radio',
  8648. 'options' => ['A', 'B'],
  8649. 'labelOptions' => ['class' => 'custom-class']
  8650. ]);
  8651. $expected = [
  8652. ['div' => ['class' => 'input radio']],
  8653. '<label',
  8654. 'Test',
  8655. '/label',
  8656. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8657. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8658. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8659. 'A',
  8660. '/label',
  8661. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8662. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8663. 'B',
  8664. '/label',
  8665. '/div',
  8666. ];
  8667. $this->assertHtml($expected, $result);
  8668. $result = $this->Form->control('test', [
  8669. 'type' => 'radio',
  8670. 'options' => ['A', 'B'],
  8671. 'value' => 1,
  8672. 'labelOptions' => ['class' => 'custom-class']
  8673. ]);
  8674. $expected = [
  8675. ['div' => ['class' => 'input radio']],
  8676. '<label',
  8677. 'Test',
  8678. '/label',
  8679. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8680. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8681. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8682. 'A',
  8683. '/label',
  8684. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8685. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8686. 'B',
  8687. '/label',
  8688. '/div',
  8689. ];
  8690. $this->assertHtml($expected, $result);
  8691. $result = $this->Form->control('test', [
  8692. 'type' => 'radio',
  8693. 'options' => ['A', 'B'],
  8694. 'value' => 1,
  8695. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8696. ]);
  8697. $expected = [
  8698. ['div' => ['class' => 'input radio']],
  8699. '<label',
  8700. 'Test',
  8701. '/label',
  8702. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8703. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8704. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8705. 'A',
  8706. '/label',
  8707. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8708. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8709. 'B',
  8710. '/label',
  8711. '/div',
  8712. ];
  8713. $this->assertHtml($expected, $result);
  8714. $result = $this->Form->radio('test', ['A', 'B'], [
  8715. 'label' => [
  8716. 'class' => ['custom-class', 'another-class'],
  8717. 'data-name' => 'bob'
  8718. ],
  8719. 'value' => 1
  8720. ]);
  8721. $expected = [
  8722. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8723. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8724. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8725. 'A',
  8726. '/label',
  8727. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8728. ['input' => [
  8729. 'type' => 'radio',
  8730. 'name' => 'test',
  8731. 'value' => '1',
  8732. 'id' => 'test-1',
  8733. 'checked' => 'checked'
  8734. ]],
  8735. 'B',
  8736. '/label',
  8737. ];
  8738. $this->assertHtml($expected, $result);
  8739. }
  8740. /**
  8741. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8742. *
  8743. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8744. * added to the class if checked.
  8745. *
  8746. * Also checks to make sure any custom attributes are rendered correctly
  8747. *
  8748. * @return void
  8749. */
  8750. public function testControlLabelManipulationCheckboxes()
  8751. {
  8752. $result = $this->Form->control('checkbox1', [
  8753. 'label' => 'My checkboxes',
  8754. 'multiple' => 'checkbox',
  8755. 'type' => 'select',
  8756. 'options' => [
  8757. ['text' => 'First Checkbox', 'value' => 1],
  8758. ['text' => 'Second Checkbox', 'value' => 2]
  8759. ],
  8760. 'labelOptions' => ['class' => 'custom-class'],
  8761. 'value' => ['1']
  8762. ]);
  8763. $expected = [
  8764. ['div' => ['class' => 'input select']],
  8765. ['label' => ['for' => 'checkbox1']],
  8766. 'My checkboxes',
  8767. '/label',
  8768. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8769. ['div' => ['class' => 'checkbox']],
  8770. ['label' => [
  8771. 'class' => 'custom-class selected',
  8772. 'for' => 'checkbox1-1'
  8773. ]],
  8774. ['input' => [
  8775. 'type' => 'checkbox',
  8776. 'name' => 'checkbox1[]',
  8777. 'value' => '1',
  8778. 'id' => 'checkbox1-1',
  8779. 'checked' => 'checked'
  8780. ]],
  8781. 'First Checkbox',
  8782. '/label',
  8783. '/div',
  8784. ['div' => ['class' => 'checkbox']],
  8785. ['label' => [
  8786. 'class' => 'custom-class',
  8787. 'for' => 'checkbox1-2'
  8788. ]],
  8789. ['input' => [
  8790. 'type' => 'checkbox',
  8791. 'name' => 'checkbox1[]',
  8792. 'value' => '2',
  8793. 'id' => 'checkbox1-2'
  8794. ]],
  8795. 'Second Checkbox',
  8796. '/label',
  8797. '/div',
  8798. '/div'
  8799. ];
  8800. $this->assertHtml($expected, $result);
  8801. $result = $this->Form->control('checkbox1', [
  8802. 'label' => 'My checkboxes',
  8803. 'multiple' => 'checkbox',
  8804. 'type' => 'select',
  8805. 'options' => [
  8806. ['text' => 'First Checkbox', 'value' => 1],
  8807. ['text' => 'Second Checkbox', 'value' => 2]
  8808. ],
  8809. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8810. 'value' => ['1']
  8811. ]);
  8812. $expected = [
  8813. ['div' => ['class' => 'input select']],
  8814. ['label' => ['for' => 'checkbox1']],
  8815. 'My checkboxes',
  8816. '/label',
  8817. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8818. ['div' => ['class' => 'checkbox']],
  8819. ['label' => [
  8820. 'class' => 'custom-class another-class selected',
  8821. 'data-name' => 'bob',
  8822. 'for' => 'checkbox1-1'
  8823. ]],
  8824. ['input' => [
  8825. 'type' => 'checkbox',
  8826. 'name' => 'checkbox1[]',
  8827. 'value' => '1',
  8828. 'id' => 'checkbox1-1',
  8829. 'checked' => 'checked'
  8830. ]],
  8831. 'First Checkbox',
  8832. '/label',
  8833. '/div',
  8834. ['div' => ['class' => 'checkbox']],
  8835. ['label' => [
  8836. 'class' => 'custom-class another-class',
  8837. 'data-name' => 'bob',
  8838. 'for' => 'checkbox1-2'
  8839. ]],
  8840. ['input' => [
  8841. 'type' => 'checkbox',
  8842. 'name' => 'checkbox1[]',
  8843. 'value' => '2',
  8844. 'id' => 'checkbox1-2'
  8845. ]],
  8846. 'Second Checkbox',
  8847. '/label',
  8848. '/div',
  8849. '/div'
  8850. ];
  8851. $this->assertHtml($expected, $result);
  8852. }
  8853. /**
  8854. * testControlMaxLengthArrayContext method
  8855. *
  8856. * Test control() with maxlength attribute in Array Context.
  8857. *
  8858. * @return void
  8859. */
  8860. public function testControlMaxLengthArrayContext()
  8861. {
  8862. $this->article['schema'] = [
  8863. 'title' => ['length' => 10]
  8864. ];
  8865. $this->Form->create($this->article);
  8866. $result = $this->Form->control('title');
  8867. $expected = [
  8868. 'div' => ['class'],
  8869. 'label' => ['for'],
  8870. 'Title',
  8871. '/label',
  8872. 'input' => [
  8873. 'id',
  8874. 'name' => 'title',
  8875. 'type' => 'text',
  8876. 'required' => 'required',
  8877. 'maxlength' => 10,
  8878. ],
  8879. '/div',
  8880. ];
  8881. $this->assertHtml($expected, $result);
  8882. }
  8883. /**
  8884. * testControlMaxLengthEntityContext method
  8885. *
  8886. * Test control() with maxlength attribute in Entity Context.
  8887. *
  8888. * @return void
  8889. */
  8890. public function testControlMaxLengthEntityContext()
  8891. {
  8892. $this->article['schema']['title']['length'] = 45;
  8893. $validator = new Validator();
  8894. $validator->maxLength('title', 10);
  8895. $article = new EntityContext(
  8896. new ServerRequest(),
  8897. [
  8898. 'entity' => new Entity($this->article),
  8899. 'table' => new Table([
  8900. 'schema' => $this->article['schema'],
  8901. 'validator' => $validator
  8902. ])
  8903. ]
  8904. );
  8905. $this->Form->create($article);
  8906. $result = $this->Form->control('title');
  8907. $expected = [
  8908. 'div' => ['class'],
  8909. 'label' => ['for'],
  8910. 'Title',
  8911. '/label',
  8912. 'input' => [
  8913. 'id',
  8914. 'name' => 'title',
  8915. 'type' => 'text',
  8916. 'required' => 'required',
  8917. 'maxlength' => 10,
  8918. ],
  8919. '/div',
  8920. ];
  8921. $this->assertHtml($expected, $result);
  8922. $this->article['schema']['title']['length'] = 45;
  8923. $validator = new Validator();
  8924. $validator->maxLength('title', 55);
  8925. $article = new EntityContext(
  8926. new ServerRequest(),
  8927. [
  8928. 'entity' => new Entity($this->article),
  8929. 'table' => new Table([
  8930. 'schema' => $this->article['schema'],
  8931. 'validator' => $validator
  8932. ])
  8933. ]
  8934. );
  8935. $this->Form->create($article);
  8936. $result = $this->Form->control('title');
  8937. $expected = [
  8938. 'div' => ['class'],
  8939. 'label' => ['for'],
  8940. 'Title',
  8941. '/label',
  8942. 'input' => [
  8943. 'id',
  8944. 'name' => 'title',
  8945. 'type' => 'text',
  8946. 'required' => 'required',
  8947. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8948. ],
  8949. '/div',
  8950. ];
  8951. $this->assertHtml($expected, $result);
  8952. $this->article['schema']['title']['length'] = 45;
  8953. $validator = new Validator();
  8954. $validator->maxLength('title', 55);
  8955. $article = new EntityContext(
  8956. new ServerRequest(),
  8957. [
  8958. 'entity' => new Entity($this->article),
  8959. 'table' => new Table([
  8960. 'schema' => $this->article['schema'],
  8961. 'validator' => $validator
  8962. ])
  8963. ]
  8964. );
  8965. $this->Form->create($article);
  8966. $result = $this->Form->control('title', ['maxlength' => 10]);
  8967. $expected = [
  8968. 'div' => ['class'],
  8969. 'label' => ['for'],
  8970. 'Title',
  8971. '/label',
  8972. 'input' => [
  8973. 'id',
  8974. 'name' => 'title',
  8975. 'type' => 'text',
  8976. 'required' => 'required',
  8977. 'maxlength' => 10, // Length set in options should take highest precedence.
  8978. ],
  8979. '/div',
  8980. ];
  8981. $this->assertHtml($expected, $result);
  8982. }
  8983. /**
  8984. * testControlMinMaxLengthEntityContext method
  8985. *
  8986. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  8987. *
  8988. * @return void
  8989. */
  8990. public function testControlMinMaxLengthEntityContext()
  8991. {
  8992. $validator = new Validator();
  8993. $validator->maxLength('title', 10);
  8994. $article = new EntityContext(
  8995. new ServerRequest(),
  8996. [
  8997. 'entity' => new Entity($this->article),
  8998. 'table' => new Table([
  8999. 'schema' => $this->article['schema'],
  9000. 'validator' => $validator
  9001. ])
  9002. ]
  9003. );
  9004. $this->Form->create($article);
  9005. $result = $this->Form->control('title');
  9006. $expected = [
  9007. 'div' => ['class'],
  9008. 'label' => ['for'],
  9009. 'Title',
  9010. '/label',
  9011. 'input' => [
  9012. 'id',
  9013. 'name' => 'title',
  9014. 'type' => 'text',
  9015. 'required' => 'required',
  9016. 'maxlength' => 10,
  9017. ],
  9018. '/div',
  9019. ];
  9020. $this->assertHtml($expected, $result);
  9021. }
  9022. /**
  9023. * testControlMaxLengthFormContext method
  9024. *
  9025. * Test control() with maxlength attribute in Form Context.
  9026. *
  9027. * @return void
  9028. */
  9029. public function testControlMaxLengthFormContext()
  9030. {
  9031. $validator = new Validator();
  9032. $validator->maxLength('title', 10);
  9033. $form = new Form();
  9034. $form->setValidator('default', $validator);
  9035. $this->Form->create($form);
  9036. $result = $this->Form->control('title');
  9037. $expected = [
  9038. 'div' => ['class'],
  9039. 'label' => ['for'],
  9040. 'Title',
  9041. '/label',
  9042. 'input' => [
  9043. 'id',
  9044. 'name' => 'title',
  9045. 'type' => 'text',
  9046. 'required' => 'required',
  9047. 'maxlength' => 10,
  9048. ],
  9049. '/div',
  9050. ];
  9051. $this->assertHtml($expected, $result);
  9052. }
  9053. }