FormHelperTest.php 324 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825
  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. * @param array $config
  62. * @return void
  63. */
  64. public function initialize(array $config)
  65. {
  66. $this->setSchema($this->_schema);
  67. }
  68. }
  69. /**
  70. * ValidateUser class
  71. */
  72. class ValidateUsersTable extends Table
  73. {
  74. /**
  75. * schema method
  76. *
  77. * @var array
  78. */
  79. protected $_schema = [
  80. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  81. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  82. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  83. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  84. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  85. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  86. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  87. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  88. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  89. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  90. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  91. ];
  92. /**
  93. * Initializes the schema
  94. *
  95. * @param array $config
  96. * @return void
  97. */
  98. public function initialize(array $config)
  99. {
  100. $this->setSchema($this->_schema);
  101. }
  102. }
  103. /**
  104. * FormHelperTest class
  105. *
  106. * @property \Cake\View\Helper\FormHelper $Form
  107. * @property \Cake\View\View $View
  108. */
  109. class FormHelperTest extends TestCase
  110. {
  111. /**
  112. * Fixtures to be used
  113. *
  114. * @var array
  115. */
  116. public $fixtures = ['core.Articles', 'core.Comments'];
  117. /**
  118. * Do not load the fixtures by default
  119. *
  120. * @var bool
  121. */
  122. public $autoFixtures = false;
  123. /**
  124. * @var array
  125. */
  126. protected $article = [];
  127. /**
  128. * setUp method
  129. *
  130. * @return void
  131. */
  132. public function setUp()
  133. {
  134. parent::setUp();
  135. Configure::write('Config.language', 'eng');
  136. Configure::write('App.base', '');
  137. static::setAppNamespace('Cake\Test\TestCase\View\Helper');
  138. $request = new ServerRequest([
  139. 'webroot' => '',
  140. 'base' => '',
  141. 'url' => '/articles/add',
  142. 'params' => [
  143. 'controller' => 'articles',
  144. 'action' => 'add',
  145. ]
  146. ]);
  147. $this->View = new View($request);
  148. $this->Form = new FormHelper($this->View);
  149. $this->dateRegex = [
  150. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  151. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  152. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  153. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  154. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  155. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  156. ];
  157. $this->article = [
  158. 'schema' => [
  159. 'id' => ['type' => 'integer'],
  160. 'author_id' => ['type' => 'integer', 'null' => true],
  161. 'title' => ['type' => 'string', 'null' => true],
  162. 'body' => 'text',
  163. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  164. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  165. ],
  166. 'required' => [
  167. 'author_id' => true,
  168. 'title' => true,
  169. ]
  170. ];
  171. Security::setSalt('foo!');
  172. Router::connect('/:controller', ['action' => 'index']);
  173. Router::connect('/:controller/:action/*');
  174. }
  175. /**
  176. * tearDown method
  177. *
  178. * @return void
  179. */
  180. public function tearDown()
  181. {
  182. parent::tearDown();
  183. unset($this->Form, $this->Controller, $this->View);
  184. $this->getTableLocator()->clear();
  185. }
  186. /**
  187. * Test construct() with the templates option.
  188. *
  189. * @return void
  190. */
  191. public function testConstructTemplatesFile()
  192. {
  193. $helper = new FormHelper($this->View, [
  194. 'templates' => 'htmlhelper_tags'
  195. ]);
  196. $result = $helper->control('name');
  197. $this->assertContains('<input', $result);
  198. }
  199. /**
  200. * Test that when specifying custom widgets the config array for that widget
  201. * is overwritten instead of merged.
  202. *
  203. * @return void
  204. */
  205. public function testConstructWithWidgets()
  206. {
  207. $config = [
  208. 'widgets' => [
  209. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  210. ]
  211. ];
  212. $helper = new FormHelper($this->View, $config);
  213. $locator = $helper->getWidgetLocator();
  214. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('datetime'));
  215. }
  216. /**
  217. * Test that when specifying custom widgets config file and it should be
  218. * added to widgets array. WidgetLocator will load widgets in constructor.
  219. *
  220. * @return void
  221. */
  222. public function testConstructWithWidgetsConfig()
  223. {
  224. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  225. $locator = $helper->getWidgetLocator();
  226. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $locator->get('text'));
  227. }
  228. /**
  229. * Test setting the widget locator
  230. *
  231. * @return void
  232. */
  233. public function testSetAndGetWidgetLocator()
  234. {
  235. $helper = new FormHelper($this->View);
  236. $locator = new WidgetLocator($helper->templater(), $this->View);
  237. $helper->setWidgetLocator($locator);
  238. $this->assertSame($locator, $helper->getWidgetLocator());
  239. }
  240. /**
  241. * Test overridding grouped input types which controls generation of "for"
  242. * attribute of labels.
  243. *
  244. * @return void
  245. */
  246. public function testConstructWithGroupedInputTypes()
  247. {
  248. $helper = new FormHelper($this->View, [
  249. 'groupedInputTypes' => ['radio'],
  250. ]);
  251. $result = $helper->control('when', ['type' => 'datetime']);
  252. $this->assertContains('<label for="when">When</label>', $result);
  253. }
  254. /**
  255. * Test registering a new widget class and rendering it.
  256. *
  257. * @return void
  258. */
  259. public function testAddWidgetAndRenderWidget()
  260. {
  261. $data = [
  262. 'val' => 1
  263. ];
  264. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  265. $this->Form->addWidget('test', $mock);
  266. $mock->expects($this->once())
  267. ->method('render')
  268. ->with($data)
  269. ->will($this->returnValue('HTML'));
  270. $result = $this->Form->widget('test', $data);
  271. $this->assertEquals('HTML', $result);
  272. }
  273. /**
  274. * Test that secureFields() of widget is called after calling render(),
  275. * not before.
  276. *
  277. * @return void
  278. */
  279. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  280. {
  281. $data = [
  282. 'val' => 1,
  283. 'name' => 'test'
  284. ];
  285. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  286. $this->Form->addWidget('test', $mock);
  287. $mock->expects($this->at(0))
  288. ->method('render')
  289. ->with($data)
  290. ->will($this->returnValue('HTML'));
  291. $mock->expects($this->at(1))
  292. ->method('secureFields')
  293. ->with($data)
  294. ->will($this->returnValue(['test']));
  295. $result = $this->Form->widget('test', $data + ['secure' => true]);
  296. $this->assertEquals('HTML', $result);
  297. }
  298. /**
  299. * Test that empty string is not added to secure fields list when
  300. * rendering input widget without name.
  301. *
  302. * @return void
  303. */
  304. public function testRenderingWidgetWithEmptyName()
  305. {
  306. $this->assertEquals([], $this->Form->fields);
  307. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  308. $this->assertEquals('<select name=""></select>', $result);
  309. $this->assertEquals([], $this->Form->fields);
  310. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  311. $this->assertEquals('<select name="0"></select>', $result);
  312. $this->assertEquals(['0'], $this->Form->fields);
  313. }
  314. /**
  315. * Test registering an invalid widget class.
  316. *
  317. * @return void
  318. */
  319. public function testAddWidgetInvalid()
  320. {
  321. $this->expectException(\RuntimeException::class);
  322. $mock = new \StdClass();
  323. $this->Form->addWidget('test', $mock);
  324. $this->Form->widget('test');
  325. }
  326. /**
  327. * Test adding a new context class.
  328. *
  329. * @return void
  330. */
  331. public function testAddContextProvider()
  332. {
  333. $context = 'My data';
  334. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  335. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  336. $this->assertInstanceOf('Cake\Http\ServerRequest', $request);
  337. $this->assertEquals($context, $data['entity']);
  338. return $stub;
  339. });
  340. $this->Form->create($context);
  341. $result = $this->Form->context();
  342. $this->assertSame($stub, $result);
  343. }
  344. /**
  345. * Test replacing a context class.
  346. *
  347. * @return void
  348. */
  349. public function testAddContextProviderReplace()
  350. {
  351. $entity = new Article();
  352. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  353. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  354. return $stub;
  355. });
  356. $this->Form->create($entity);
  357. $result = $this->Form->context();
  358. $this->assertSame($stub, $result);
  359. }
  360. /**
  361. * Test overriding a context class.
  362. *
  363. * @return void
  364. */
  365. public function testAddContextProviderAdd()
  366. {
  367. $entity = new Article();
  368. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  369. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  370. if ($data['entity'] instanceof Entity) {
  371. return $stub;
  372. }
  373. });
  374. $this->Form->create($entity);
  375. $result = $this->Form->context();
  376. $this->assertSame($stub, $result);
  377. }
  378. /**
  379. * Test adding an invalid context class.
  380. *
  381. * @return void
  382. */
  383. public function testAddContextProviderInvalid()
  384. {
  385. $this->expectException(\RuntimeException::class);
  386. $this->expectExceptionMessage('Context providers must return object implementing Cake\View\Form\ContextInterface. Got "stdClass" instead.');
  387. $context = 'My data';
  388. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  389. return new \StdClass();
  390. });
  391. $this->Form->create($context);
  392. }
  393. /**
  394. * Provides context options for create().
  395. *
  396. * @return array
  397. */
  398. public function contextSelectionProvider()
  399. {
  400. $entity = new Article();
  401. $collection = new Collection([$entity]);
  402. $emptyCollection = new Collection([]);
  403. $arrayObject = new \ArrayObject([]);
  404. $data = [
  405. 'schema' => [
  406. 'title' => ['type' => 'string']
  407. ]
  408. ];
  409. $form = new Form();
  410. $custom = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  411. return [
  412. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  413. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  414. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  415. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  416. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  417. 'form' => [$form, 'Cake\View\Form\FormContext'],
  418. 'none' => [null, 'Cake\View\Form\NullContext'],
  419. 'false' => [false, 'Cake\View\Form\NullContext'],
  420. 'custom' => [$custom, get_class($custom)]
  421. ];
  422. }
  423. /**
  424. * Test default context selection in create()
  425. *
  426. * @dataProvider contextSelectionProvider
  427. * @return void
  428. */
  429. public function testCreateContextSelectionBuiltIn($data, $class)
  430. {
  431. $this->loadFixtures('Articles');
  432. $this->Form->create($data);
  433. $this->assertInstanceOf($class, $this->Form->context());
  434. }
  435. /**
  436. * Data provider for type option.
  437. *
  438. * @return array
  439. */
  440. public static function requestTypeProvider()
  441. {
  442. return [
  443. // type, method, override
  444. ['post', 'post', 'POST'],
  445. ['put', 'post', 'PUT'],
  446. ['patch', 'post', 'PATCH'],
  447. ['delete', 'post', 'DELETE'],
  448. ];
  449. }
  450. /**
  451. * Test creating file forms.
  452. *
  453. * @return void
  454. */
  455. public function testCreateFile()
  456. {
  457. $encoding = strtolower(Configure::read('App.encoding'));
  458. $result = $this->Form->create(false, ['type' => 'file']);
  459. $expected = [
  460. 'form' => [
  461. 'method' => 'post', 'action' => '/articles/add',
  462. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  463. ],
  464. 'div' => ['style' => 'display:none;'],
  465. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  466. '/div'
  467. ];
  468. $this->assertHtml($expected, $result);
  469. }
  470. /**
  471. * Test creating GET forms.
  472. *
  473. * @return void
  474. */
  475. public function testCreateGet()
  476. {
  477. $encoding = strtolower(Configure::read('App.encoding'));
  478. $result = $this->Form->create(false, ['type' => 'get']);
  479. $expected = ['form' => [
  480. 'method' => 'get', 'action' => '/articles/add',
  481. 'accept-charset' => $encoding
  482. ]];
  483. $this->assertHtml($expected, $result);
  484. }
  485. /**
  486. * Test explicit method/enctype options.
  487. *
  488. * Explicit method overwrites inferred method from 'type'
  489. *
  490. * @return void
  491. */
  492. public function testCreateExplicitMethodEnctype()
  493. {
  494. $encoding = strtolower(Configure::read('App.encoding'));
  495. $result = $this->Form->create(false, [
  496. 'type' => 'get',
  497. 'method' => 'put',
  498. 'enctype' => 'multipart/form-data'
  499. ]);
  500. $expected = ['form' => [
  501. 'method' => 'put',
  502. 'action' => '/articles/add',
  503. 'enctype' => 'multipart/form-data',
  504. 'accept-charset' => $encoding
  505. ]];
  506. $this->assertHtml($expected, $result);
  507. }
  508. /**
  509. * Test create() with the templates option.
  510. *
  511. * @return void
  512. */
  513. public function testCreateTemplatesArray()
  514. {
  515. $result = $this->Form->create($this->article, [
  516. 'templates' => [
  517. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  518. ]
  519. ]);
  520. $expected = [
  521. 'form' => [
  522. 'class' => 'form-horizontal',
  523. 'method' => 'post',
  524. 'action' => '/articles/add',
  525. 'accept-charset' => 'utf-8'
  526. ]
  527. ];
  528. $this->assertHtml($expected, $result);
  529. }
  530. /**
  531. * Test create() with the templates option.
  532. *
  533. * @return void
  534. */
  535. public function testCreateTemplatesFile()
  536. {
  537. $result = $this->Form->create($this->article, [
  538. 'templates' => 'htmlhelper_tags',
  539. ]);
  540. $expected = [
  541. 'start form',
  542. 'div' => ['class' => 'hidden'],
  543. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  544. '/div'
  545. ];
  546. $this->assertHtml($expected, $result);
  547. }
  548. /**
  549. * Test that create() and end() restore templates.
  550. *
  551. * @return void
  552. */
  553. public function testCreateEndRestoreTemplates()
  554. {
  555. $this->Form->create($this->article, [
  556. 'templates' => ['input' => 'custom input element']
  557. ]);
  558. $this->Form->end();
  559. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  560. }
  561. /**
  562. * Test using template vars in various templates used by control() method.
  563. *
  564. * @return void
  565. */
  566. public function testControlTemplateVars()
  567. {
  568. $result = $this->Form->control('text', [
  569. 'templates' => [
  570. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  571. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  572. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  573. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  574. ],
  575. 'templateVars' => [
  576. 'forinput' => 'in-input',
  577. 'forlabel' => 'in-label',
  578. 'forgroup' => 'in-group',
  579. 'forcontainer' => 'in-container'
  580. ]
  581. ]);
  582. $expected = [
  583. 'div' => ['class'],
  584. 'label' => ['for'],
  585. 'Text in-label',
  586. '/label',
  587. 'in-group',
  588. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  589. 'in-container',
  590. '/div',
  591. ];
  592. $this->assertHtml($expected, $result);
  593. }
  594. /**
  595. * Test ensuring template variables work in template files loaded
  596. * during control().
  597. *
  598. * @return void
  599. */
  600. public function testControlTemplatesFromFile()
  601. {
  602. $result = $this->Form->control('title', [
  603. 'templates' => 'test_templates',
  604. 'templateVars' => [
  605. 'forcontainer' => 'container-data'
  606. ]
  607. ]);
  608. $expected = [
  609. 'div' => ['class'],
  610. 'label' => ['for'],
  611. 'Title',
  612. '/label',
  613. 'input' => ['name', 'type' => 'text', 'id'],
  614. 'container-data',
  615. '/div',
  616. ];
  617. $this->assertHtml($expected, $result);
  618. }
  619. /**
  620. * Test using template vars in inputSubmit and submitContainer template.
  621. *
  622. * @return void
  623. */
  624. public function testSubmitTemplateVars()
  625. {
  626. $this->Form->setTemplates([
  627. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  628. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  629. ]);
  630. $result = $this->Form->submit('Submit', [
  631. 'templateVars' => [
  632. 'forinput' => 'in-input',
  633. 'forcontainer' => 'in-container'
  634. ]
  635. ]);
  636. $expected = [
  637. 'div' => ['class'],
  638. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  639. 'in-container',
  640. '/div',
  641. ];
  642. $this->assertHtml($expected, $result);
  643. }
  644. /**
  645. * test the create() method
  646. *
  647. * @dataProvider requestTypeProvider
  648. * @return void
  649. */
  650. public function testCreateTypeOptions($type, $method, $override)
  651. {
  652. $encoding = strtolower(Configure::read('App.encoding'));
  653. $result = $this->Form->create(false, ['type' => $type]);
  654. $expected = [
  655. 'form' => [
  656. 'method' => $method, 'action' => '/articles/add',
  657. 'accept-charset' => $encoding
  658. ],
  659. 'div' => ['style' => 'display:none;'],
  660. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  661. '/div'
  662. ];
  663. $this->assertHtml($expected, $result);
  664. }
  665. /**
  666. * Test using template vars in Create (formStart template)
  667. *
  668. * @return void
  669. */
  670. public function testCreateTemplateVars()
  671. {
  672. $result = $this->Form->create($this->article, [
  673. 'templates' => [
  674. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  675. ],
  676. 'templateVars' => ['header' => 'headertext']
  677. ]);
  678. $expected = [
  679. 'h4' => ['class'],
  680. 'headertext',
  681. '/h4',
  682. 'form' => [
  683. 'method' => 'post',
  684. 'action' => '/articles/add',
  685. 'accept-charset' => 'utf-8'
  686. ]
  687. ];
  688. $this->assertHtml($expected, $result);
  689. }
  690. /**
  691. * Test opening a form for an update operation.
  692. *
  693. * @return void
  694. */
  695. public function testCreateUpdateForm()
  696. {
  697. $encoding = strtolower(Configure::read('App.encoding'));
  698. $this->View->setRequest($this->View->getRequest()
  699. ->withRequestTarget('/articles/edit/1')
  700. ->withParam('action', 'edit'));
  701. $this->article['defaults']['id'] = 1;
  702. $result = $this->Form->create($this->article);
  703. $expected = [
  704. 'form' => [
  705. 'method' => 'post',
  706. 'action' => '/articles/edit/1',
  707. 'accept-charset' => $encoding
  708. ],
  709. 'div' => ['style' => 'display:none;'],
  710. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  711. '/div'
  712. ];
  713. $this->assertHtml($expected, $result);
  714. }
  715. /**
  716. * test create() with automatic url generation
  717. *
  718. * @return void
  719. */
  720. public function testCreateAutoUrl()
  721. {
  722. $encoding = strtolower(Configure::read('App.encoding'));
  723. $this->View->setRequest($this->View->getRequest()
  724. ->withRequestTarget('/articles/delete/10')
  725. ->withParam('action', 'delete'));
  726. $result = $this->Form->create($this->article);
  727. $expected = [
  728. 'form' => [
  729. 'method' => 'post', 'action' => '/articles/delete/10',
  730. 'accept-charset' => $encoding
  731. ],
  732. 'div' => ['style' => 'display:none;'],
  733. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  734. '/div'
  735. ];
  736. $this->assertHtml($expected, $result);
  737. $this->article['defaults'] = ['id' => 1];
  738. $this->View->setRequest($this->View->getRequest()
  739. ->withRequestTarget('/articles/edit/1')
  740. ->withParam('action', 'delete'));
  741. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  742. $expected = [
  743. 'form' => [
  744. 'method' => 'post',
  745. 'action' => '/articles/edit/1',
  746. 'accept-charset' => $encoding
  747. ],
  748. 'div' => ['style' => 'display:none;'],
  749. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  750. '/div'
  751. ];
  752. $this->assertHtml($expected, $result);
  753. $this->View->setRequest($this->View->getRequest()
  754. ->withParam('action', 'add'));
  755. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  756. $expected = [
  757. 'form' => [
  758. 'method' => 'post',
  759. 'action' => '/articles/publish/1',
  760. 'accept-charset' => $encoding
  761. ],
  762. 'div' => ['style' => 'display:none;'],
  763. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  764. '/div'
  765. ];
  766. $this->assertHtml($expected, $result);
  767. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  768. $expected = [
  769. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  770. 'div' => ['style' => 'display:none;'],
  771. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  772. '/div'
  773. ];
  774. $this->assertHtml($expected, $result);
  775. $this->View->setRequest($this->View->getRequest()
  776. ->withParam('controller', 'Pages'));
  777. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  778. $expected = [
  779. 'form' => [
  780. 'method' => 'post', 'action' => '/Pages/signup/1',
  781. 'accept-charset' => $encoding
  782. ],
  783. 'div' => ['style' => 'display:none;'],
  784. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  785. '/div'
  786. ];
  787. $this->assertHtml($expected, $result);
  788. }
  789. /**
  790. * Test create() with no URL (no "action" attribute for <form> tag)
  791. *
  792. * @return void
  793. */
  794. public function testCreateNoUrl()
  795. {
  796. $result = $this->Form->create(false, ['url' => false]);
  797. $expected = [
  798. 'form' => [
  799. 'method' => 'post',
  800. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  801. ],
  802. 'div' => ['style' => 'display:none;'],
  803. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  804. '/div'
  805. ];
  806. $this->assertHtml($expected, $result);
  807. }
  808. /**
  809. * test create() with a custom route
  810. *
  811. * @return void
  812. */
  813. public function testCreateCustomRoute()
  814. {
  815. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  816. $encoding = strtolower(Configure::read('App.encoding'));
  817. $this->View->setRequest($this->View->getRequest()
  818. ->withParam('controller', 'users'));
  819. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  820. $expected = [
  821. 'form' => [
  822. 'method' => 'post', 'action' => '/login',
  823. 'accept-charset' => $encoding
  824. ],
  825. 'div' => ['style' => 'display:none;'],
  826. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  827. '/div'
  828. ];
  829. $this->assertHtml($expected, $result);
  830. Router::connect(
  831. '/new-article',
  832. ['controller' => 'articles', 'action' => 'myaction'],
  833. ['_name' => 'my-route']
  834. );
  835. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  836. $expected = [
  837. 'form' => [
  838. 'method' => 'post', 'action' => '/new-article',
  839. 'accept-charset' => $encoding,
  840. ],
  841. 'div' => ['style' => 'display:none;'],
  842. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  843. '/div'
  844. ];
  845. $this->assertHtml($expected, $result);
  846. }
  847. /**
  848. * test automatic accept-charset overriding
  849. *
  850. * @return void
  851. */
  852. public function testCreateWithAcceptCharset()
  853. {
  854. $result = $this->Form->create(
  855. $this->article,
  856. [
  857. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  858. ]
  859. );
  860. $expected = [
  861. 'form' => [
  862. 'method' => 'post', 'action' => '/articles',
  863. 'accept-charset' => 'iso-8859-1'
  864. ],
  865. 'div' => ['style' => 'display:none;'],
  866. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  867. '/div'
  868. ];
  869. $this->assertHtml($expected, $result);
  870. }
  871. /**
  872. * Test base form URL when url param is passed with multiple parameters (&)
  873. *
  874. */
  875. public function testCreateQueryStringRequest()
  876. {
  877. $encoding = strtolower(Configure::read('App.encoding'));
  878. $result = $this->Form->create($this->article, [
  879. 'type' => 'post',
  880. 'escape' => false,
  881. 'url' => [
  882. 'controller' => 'controller',
  883. 'action' => 'action',
  884. '?' => ['param1' => 'value1', 'param2' => 'value2']
  885. ]
  886. ]);
  887. $expected = [
  888. 'form' => [
  889. 'method' => 'post',
  890. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  891. 'accept-charset' => $encoding
  892. ],
  893. 'div' => ['style' => 'display:none;'],
  894. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  895. '/div'
  896. ];
  897. $this->assertHtml($expected, $result);
  898. $result = $this->Form->create($this->article, [
  899. 'type' => 'post',
  900. 'url' => [
  901. 'controller' => 'controller',
  902. 'action' => 'action',
  903. '?' => ['param1' => 'value1', 'param2' => 'value2']
  904. ]
  905. ]);
  906. $this->assertHtml($expected, $result);
  907. }
  908. /**
  909. * test that create() doesn't cause errors by multiple id's being in the primary key
  910. * as could happen with multiple select or checkboxes.
  911. *
  912. * @return void
  913. */
  914. public function testCreateWithMultipleIdInData()
  915. {
  916. $encoding = strtolower(Configure::read('App.encoding'));
  917. $this->View->setRequest($this->View->getRequest()->withData('Article.id', [1, 2]));
  918. $result = $this->Form->create($this->article);
  919. $expected = [
  920. 'form' => [
  921. 'method' => 'post',
  922. 'action' => '/articles/add',
  923. 'accept-charset' => $encoding
  924. ],
  925. 'div' => ['style' => 'display:none;'],
  926. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  927. '/div'
  928. ];
  929. $this->assertHtml($expected, $result);
  930. }
  931. /**
  932. * test that create() doesn't add in extra passed params.
  933. *
  934. * @return void
  935. */
  936. public function testCreatePassedArgs()
  937. {
  938. $encoding = strtolower(Configure::read('App.encoding'));
  939. $this->View->setRequest($this->View->getRequest()->withData('Article.id', 1));
  940. $result = $this->Form->create($this->article, [
  941. 'type' => 'post',
  942. 'escape' => false,
  943. 'url' => [
  944. 'action' => 'edit',
  945. 'myparam'
  946. ]
  947. ]);
  948. $expected = [
  949. 'form' => [
  950. 'method' => 'post',
  951. 'action' => '/articles/edit/myparam',
  952. 'accept-charset' => $encoding
  953. ],
  954. 'div' => ['style' => 'display:none;'],
  955. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  956. '/div'
  957. ];
  958. $this->assertHtml($expected, $result);
  959. }
  960. /**
  961. * test creating a get form, and get form inputs.
  962. *
  963. * @return void
  964. */
  965. public function testGetFormCreate()
  966. {
  967. $encoding = strtolower(Configure::read('App.encoding'));
  968. $result = $this->Form->create($this->article, ['type' => 'get']);
  969. $expected = ['form' => [
  970. 'method' => 'get', 'action' => '/articles/add',
  971. 'accept-charset' => $encoding
  972. ]];
  973. $this->assertHtml($expected, $result);
  974. $result = $this->Form->text('title');
  975. $expected = ['input' => [
  976. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  977. ]];
  978. $this->assertHtml($expected, $result);
  979. $result = $this->Form->password('password');
  980. $expected = ['input' => [
  981. 'name' => 'password', 'type' => 'password'
  982. ]];
  983. $this->assertHtml($expected, $result);
  984. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  985. $result = $this->Form->text('user_form');
  986. $expected = ['input' => [
  987. 'name' => 'user_form', 'type' => 'text'
  988. ]];
  989. $this->assertHtml($expected, $result);
  990. }
  991. /**
  992. * test get form, and inputs when the model param is false
  993. *
  994. * @return void
  995. */
  996. public function testGetFormWithFalseModel()
  997. {
  998. $encoding = strtolower(Configure::read('App.encoding'));
  999. $this->View->setRequest($this->View->getRequest()->withParam('controller', 'contact_test'));
  1000. $result = $this->Form->create(false, [
  1001. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  1002. ]);
  1003. $expected = ['form' => [
  1004. 'method' => 'get', 'action' => '/contact_test/add',
  1005. 'accept-charset' => $encoding
  1006. ]];
  1007. $this->assertHtml($expected, $result);
  1008. $result = $this->Form->text('reason');
  1009. $expected = [
  1010. 'input' => ['type' => 'text', 'name' => 'reason']
  1011. ];
  1012. $this->assertHtml($expected, $result);
  1013. }
  1014. /**
  1015. * testFormCreateWithSecurity method
  1016. *
  1017. * Test form->create() with security key.
  1018. *
  1019. * @return void
  1020. */
  1021. public function testCreateWithSecurity()
  1022. {
  1023. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1024. $encoding = strtolower(Configure::read('App.encoding'));
  1025. $result = $this->Form->create($this->article, [
  1026. 'url' => '/articles/publish',
  1027. ]);
  1028. $expected = [
  1029. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  1030. 'div' => ['style' => 'display:none;'],
  1031. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1032. ['input' => [
  1033. 'type' => 'hidden',
  1034. 'name' => '_csrfToken',
  1035. 'value' => 'testKey',
  1036. 'autocomplete' => 'off',
  1037. ]],
  1038. '/div'
  1039. ];
  1040. $this->assertHtml($expected, $result);
  1041. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1042. $expected['form']['id'] = 'MyForm';
  1043. $this->assertHtml($expected, $result);
  1044. }
  1045. /**
  1046. * testFormCreateGetNoSecurity method
  1047. *
  1048. * Test form->create() with no security key as its a get form
  1049. *
  1050. * @return void
  1051. */
  1052. public function testCreateEndGetNoSecurity()
  1053. {
  1054. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1055. $article = new Article();
  1056. $result = $this->Form->create($article, [
  1057. 'type' => 'get',
  1058. 'url' => '/contacts/add'
  1059. ]);
  1060. $this->assertNotContains('testKey', $result);
  1061. $result = $this->Form->end();
  1062. $this->assertNotContains('testKey', $result);
  1063. }
  1064. /**
  1065. * test that create() clears the fields property so it starts fresh
  1066. *
  1067. * @return void
  1068. */
  1069. public function testCreateClearingFields()
  1070. {
  1071. $this->Form->fields = ['model_id'];
  1072. $this->Form->create($this->article);
  1073. $this->assertEquals([], $this->Form->fields);
  1074. }
  1075. /**
  1076. * Tests form hash generation with model-less data
  1077. *
  1078. * @return void
  1079. */
  1080. public function testValidateHashNoModel()
  1081. {
  1082. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1083. $fields = ['anything'];
  1084. $result = $this->Form->secure($fields);
  1085. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1086. $this->assertContains($hash, $result);
  1087. }
  1088. /**
  1089. * Tests that hidden fields generated for checkboxes don't get locked
  1090. *
  1091. * @return void
  1092. */
  1093. public function testNoCheckboxLocking()
  1094. {
  1095. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1096. $this->assertSame([], $this->Form->fields);
  1097. $this->Form->checkbox('check', ['value' => '1']);
  1098. $this->assertSame($this->Form->fields, ['check']);
  1099. }
  1100. /**
  1101. * testFormSecurityFields method
  1102. *
  1103. * Test generation of secure form hash generation.
  1104. *
  1105. * @return void
  1106. */
  1107. public function testFormSecurityFields()
  1108. {
  1109. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1110. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1111. $result = $this->Form->secure($fields);
  1112. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1113. $hash .= ':' . 'Model.valid';
  1114. $hash = urlencode($hash);
  1115. $tokenDebug = urlencode(json_encode([
  1116. '',
  1117. $fields,
  1118. []
  1119. ]));
  1120. $expected = [
  1121. 'div' => ['style' => 'display:none;'],
  1122. ['input' => [
  1123. 'type' => 'hidden',
  1124. 'name' => '_Token[fields]',
  1125. 'value' => $hash,
  1126. 'autocomplete' => 'off',
  1127. ]],
  1128. ['input' => [
  1129. 'type' => 'hidden',
  1130. 'name' => '_Token[unlocked]',
  1131. 'value' => '',
  1132. 'autocomplete' => 'off',
  1133. ]],
  1134. ['input' => [
  1135. 'type' => 'hidden',
  1136. 'name' => '_Token[debug]',
  1137. 'value' => $tokenDebug,
  1138. 'autocomplete' => 'off',
  1139. ]],
  1140. '/div'
  1141. ];
  1142. $this->assertHtml($expected, $result);
  1143. }
  1144. /**
  1145. * testFormSecurityFields method
  1146. *
  1147. * Test debug token is not generated if debug is false
  1148. *
  1149. * @return void
  1150. */
  1151. public function testFormSecurityFieldsNoDebugMode()
  1152. {
  1153. Configure::write('debug', false);
  1154. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1155. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1156. $result = $this->Form->secure($fields);
  1157. $hash = hash_hmac('sha1', serialize($fields) . session_id(), Security::getSalt());
  1158. $hash .= ':' . 'Model.valid';
  1159. $hash = urlencode($hash);
  1160. $expected = [
  1161. 'div' => ['style' => 'display:none;'],
  1162. ['input' => [
  1163. 'type' => 'hidden',
  1164. 'name' => '_Token[fields]',
  1165. 'autocomplete' => 'off',
  1166. 'value' => $hash
  1167. ]],
  1168. ['input' => [
  1169. 'type' => 'hidden',
  1170. 'name' => '_Token[unlocked]',
  1171. 'autocomplete' => 'off',
  1172. 'value' => '',
  1173. ]],
  1174. '/div'
  1175. ];
  1176. $this->assertHtml($expected, $result);
  1177. }
  1178. /**
  1179. * Tests correct generation of number fields for smallint
  1180. *
  1181. * @return void
  1182. */
  1183. public function testTextFieldGenerationForSmallint()
  1184. {
  1185. $this->article['schema'] = [
  1186. 'foo' => [
  1187. 'type' => 'smallinteger',
  1188. 'null' => false,
  1189. 'default' => null,
  1190. 'length' => 10
  1191. ]
  1192. ];
  1193. $this->Form->create($this->article);
  1194. $result = $this->Form->control('foo');
  1195. $this->assertContains('class="input number"', $result);
  1196. $this->assertContains('type="number"', $result);
  1197. }
  1198. /**
  1199. * Tests correct generation of number fields for tinyint
  1200. *
  1201. * @return void
  1202. */
  1203. public function testTextFieldGenerationForTinyint()
  1204. {
  1205. $this->article['schema'] = [
  1206. 'foo' => [
  1207. 'type' => 'tinyinteger',
  1208. 'null' => false,
  1209. 'default' => null,
  1210. 'length' => 10
  1211. ]
  1212. ];
  1213. $this->Form->create($this->article);
  1214. $result = $this->Form->control('foo');
  1215. $this->assertContains('class="input number"', $result);
  1216. $this->assertContains('type="number"', $result);
  1217. }
  1218. /**
  1219. * Tests correct generation of number fields for double and float fields
  1220. *
  1221. * @return void
  1222. */
  1223. public function testTextFieldGenerationForFloats()
  1224. {
  1225. $this->article['schema'] = [
  1226. 'foo' => [
  1227. 'type' => 'float',
  1228. 'null' => false,
  1229. 'default' => null,
  1230. 'length' => 10
  1231. ]
  1232. ];
  1233. $this->Form->create($this->article);
  1234. $result = $this->Form->control('foo');
  1235. $expected = [
  1236. 'div' => ['class' => 'input number'],
  1237. 'label' => ['for' => 'foo'],
  1238. 'Foo',
  1239. '/label',
  1240. ['input' => [
  1241. 'type' => 'number',
  1242. 'name' => 'foo',
  1243. 'id' => 'foo',
  1244. 'step' => 'any'
  1245. ]],
  1246. '/div'
  1247. ];
  1248. $this->assertHtml($expected, $result);
  1249. $result = $this->Form->control('foo', ['step' => 0.5]);
  1250. $expected = [
  1251. 'div' => ['class' => 'input number'],
  1252. 'label' => ['for' => 'foo'],
  1253. 'Foo',
  1254. '/label',
  1255. ['input' => [
  1256. 'type' => 'number',
  1257. 'name' => 'foo',
  1258. 'id' => 'foo',
  1259. 'step' => '0.5'
  1260. ]],
  1261. '/div'
  1262. ];
  1263. $this->assertHtml($expected, $result);
  1264. }
  1265. /**
  1266. * Tests correct generation of number fields for integer fields
  1267. *
  1268. * @return void
  1269. */
  1270. public function testTextFieldTypeNumberGenerationForIntegers()
  1271. {
  1272. $this->getTableLocator()->get('Contacts', [
  1273. 'className' => __NAMESPACE__ . '\ContactsTable'
  1274. ]);
  1275. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1276. $result = $this->Form->control('age');
  1277. $expected = [
  1278. 'div' => ['class' => 'input number'],
  1279. 'label' => ['for' => 'age'],
  1280. 'Age',
  1281. '/label',
  1282. ['input' => [
  1283. 'type' => 'number', 'name' => 'age',
  1284. 'id' => 'age'
  1285. ]],
  1286. '/div'
  1287. ];
  1288. $this->assertHtml($expected, $result);
  1289. }
  1290. /**
  1291. * Tests correct generation of file upload fields for binary fields
  1292. *
  1293. * @return void
  1294. */
  1295. public function testFileUploadFieldTypeGenerationForBinaries()
  1296. {
  1297. $table = $this->getTableLocator()->get('Contacts', [
  1298. 'className' => __NAMESPACE__ . '\ContactsTable'
  1299. ]);
  1300. $table->setSchema(['foo' => [
  1301. 'type' => 'binary',
  1302. 'null' => false,
  1303. 'default' => null,
  1304. 'length' => 1024
  1305. ]]);
  1306. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1307. $result = $this->Form->control('foo');
  1308. $expected = [
  1309. 'div' => ['class' => 'input file'],
  1310. 'label' => ['for' => 'foo'],
  1311. 'Foo',
  1312. '/label',
  1313. ['input' => [
  1314. 'type' => 'file', 'name' => 'foo',
  1315. 'id' => 'foo'
  1316. ]],
  1317. '/div'
  1318. ];
  1319. $this->assertHtml($expected, $result);
  1320. }
  1321. /**
  1322. * testFormSecurityMultipleFields method
  1323. *
  1324. * Test secure() with multiple row form. Ensure hash is correct.
  1325. *
  1326. * @return void
  1327. */
  1328. public function testFormSecurityMultipleFields()
  1329. {
  1330. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'foo'));
  1331. $fields = [
  1332. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1333. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1334. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1335. ];
  1336. $result = $this->Form->secure($fields);
  1337. $hash = '8670192c3f040bf58680479060b4755b7a5c3596' .
  1338. '%3AModel.0.hidden%7CModel.0.valid%7CModel.1.hidden%7CModel.1.valid';
  1339. $tokenDebug = urlencode(json_encode([
  1340. '',
  1341. $fields,
  1342. []
  1343. ]));
  1344. $expected = [
  1345. 'div' => ['style' => 'display:none;'],
  1346. ['input' => [
  1347. 'type' => 'hidden',
  1348. 'name' => '_Token[fields]',
  1349. 'value' => $hash,
  1350. 'autocomplete' => 'off',
  1351. ]],
  1352. ['input' => [
  1353. 'type' => 'hidden',
  1354. 'name' => '_Token[unlocked]',
  1355. 'autocomplete' => 'off',
  1356. 'value' => ''
  1357. ]],
  1358. ['input' => [
  1359. 'type' => 'hidden',
  1360. 'name' => '_Token[debug]',
  1361. 'value' => $tokenDebug,
  1362. 'autocomplete' => 'off',
  1363. ]],
  1364. '/div'
  1365. ];
  1366. $this->assertHtml($expected, $result);
  1367. }
  1368. /**
  1369. * testFormSecurityMultipleSubmitButtons
  1370. *
  1371. * test form submit generation and ensure that _Token is only created on end()
  1372. *
  1373. * @return void
  1374. */
  1375. public function testFormSecurityMultipleSubmitButtons()
  1376. {
  1377. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1378. $this->Form->create($this->article);
  1379. $this->Form->text('Address.title');
  1380. $this->Form->text('Address.first_name');
  1381. $result = $this->Form->submit('Save', ['name' => 'save']);
  1382. $expected = [
  1383. 'div' => ['class' => 'submit'],
  1384. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1385. '/div',
  1386. ];
  1387. $this->assertHtml($expected, $result);
  1388. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1389. $expected = [
  1390. 'div' => ['class' => 'submit'],
  1391. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1392. '/div',
  1393. ];
  1394. $this->assertHtml($expected, $result);
  1395. $result = $this->Form->end();
  1396. $tokenDebug = urlencode(json_encode([
  1397. '/articles/add',
  1398. [
  1399. 'Address.title',
  1400. 'Address.first_name',
  1401. ],
  1402. ['save', 'cancel']
  1403. ]));
  1404. $expected = [
  1405. 'div' => ['style' => 'display:none;'],
  1406. ['input' => [
  1407. 'type' => 'hidden',
  1408. 'name' => '_Token[fields]',
  1409. 'autocomplete',
  1410. 'value'
  1411. ]],
  1412. ['input' => [
  1413. 'type' => 'hidden',
  1414. 'name' => '_Token[unlocked]',
  1415. 'value' => 'cancel%7Csave',
  1416. 'autocomplete' => 'off',
  1417. ]],
  1418. ['input' => [
  1419. 'type' => 'hidden',
  1420. 'name' => '_Token[debug]',
  1421. 'value' => $tokenDebug,
  1422. 'autocomplete' => 'off',
  1423. ]],
  1424. '/div'
  1425. ];
  1426. $this->assertHtml($expected, $result);
  1427. }
  1428. /**
  1429. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1430. *
  1431. * @return void
  1432. */
  1433. public function testSecurityButtonNestedNamed()
  1434. {
  1435. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  1436. $this->Form->create('Addresses');
  1437. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1438. $result = $this->Form->unlockField();
  1439. $this->assertEquals(['Address.button'], $result);
  1440. }
  1441. /**
  1442. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1443. *
  1444. * @return void
  1445. */
  1446. public function testSecuritySubmitNestedNamed()
  1447. {
  1448. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1449. $this->Form->create($this->article);
  1450. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1451. $result = $this->Form->unlockField();
  1452. $this->assertEquals(['Address.button'], $result);
  1453. }
  1454. /**
  1455. * Test that the correct fields are unlocked for image submits with no names.
  1456. *
  1457. * @return void
  1458. */
  1459. public function testSecuritySubmitImageNoName()
  1460. {
  1461. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1462. $this->Form->create(false);
  1463. $result = $this->Form->submit('save.png');
  1464. $expected = [
  1465. 'div' => ['class' => 'submit'],
  1466. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1467. '/div'
  1468. ];
  1469. $this->assertHtml($expected, $result);
  1470. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1471. }
  1472. /**
  1473. * Test that the correct fields are unlocked for image submits with names.
  1474. *
  1475. * @return void
  1476. */
  1477. public function testSecuritySubmitImageName()
  1478. {
  1479. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1480. $this->Form->create(null);
  1481. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1482. $expected = [
  1483. 'div' => ['class' => 'submit'],
  1484. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1485. '/div'
  1486. ];
  1487. $this->assertHtml($expected, $result);
  1488. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1489. }
  1490. /**
  1491. * testFormSecurityMultipleControlFields method
  1492. *
  1493. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1494. *
  1495. * @return void
  1496. */
  1497. public function testFormSecurityMultipleControlFields()
  1498. {
  1499. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1500. $this->Form->create();
  1501. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1502. $this->Form->control('Addresses.0.title');
  1503. $this->Form->control('Addresses.0.first_name');
  1504. $this->Form->control('Addresses.0.last_name');
  1505. $this->Form->control('Addresses.0.address');
  1506. $this->Form->control('Addresses.0.city');
  1507. $this->Form->control('Addresses.0.phone');
  1508. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1509. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1510. $this->Form->control('Addresses.1.title');
  1511. $this->Form->control('Addresses.1.first_name');
  1512. $this->Form->control('Addresses.1.last_name');
  1513. $this->Form->control('Addresses.1.address');
  1514. $this->Form->control('Addresses.1.city');
  1515. $this->Form->control('Addresses.1.phone');
  1516. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1517. $result = $this->Form->secure($this->Form->fields);
  1518. $hash = 'a4fe49bde94894a01375e7aa2873ea8114a96471%3AAddresses.0.id%7CAddresses.1.id';
  1519. $tokenDebug = urlencode(json_encode([
  1520. '/articles/add',
  1521. [
  1522. 'Addresses.0.id' => '123456',
  1523. 'Addresses.0.title',
  1524. 'Addresses.0.first_name',
  1525. 'Addresses.0.last_name',
  1526. 'Addresses.0.address',
  1527. 'Addresses.0.city',
  1528. 'Addresses.0.phone',
  1529. 'Addresses.0.primary',
  1530. 'Addresses.1.id' => '654321',
  1531. 'Addresses.1.title',
  1532. 'Addresses.1.first_name',
  1533. 'Addresses.1.last_name',
  1534. 'Addresses.1.address',
  1535. 'Addresses.1.city',
  1536. 'Addresses.1.phone',
  1537. 'Addresses.1.primary',
  1538. ],
  1539. []
  1540. ]));
  1541. $expected = [
  1542. 'div' => ['style' => 'display:none;'],
  1543. ['input' => [
  1544. 'type' => 'hidden',
  1545. 'name' => '_Token[fields]',
  1546. 'value' => $hash,
  1547. 'autocomplete' => 'off',
  1548. ]],
  1549. ['input' => [
  1550. 'type' => 'hidden',
  1551. 'name' => '_Token[unlocked]',
  1552. 'autocomplete' => 'off',
  1553. 'value' => ''
  1554. ]],
  1555. ['input' => [
  1556. 'type' => 'hidden',
  1557. 'name' => '_Token[debug]',
  1558. 'value' => $tokenDebug,
  1559. 'autocomplete' => 'off',
  1560. ]],
  1561. '/div'
  1562. ];
  1563. $this->assertHtml($expected, $result);
  1564. }
  1565. /**
  1566. * testFormSecurityArrayFields method
  1567. *
  1568. * Test form security with Model.field.0 style inputs.
  1569. *
  1570. * @return void
  1571. */
  1572. public function testFormSecurityArrayFields()
  1573. {
  1574. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1575. $this->Form->create();
  1576. $this->Form->text('Address.primary.1');
  1577. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1578. $this->Form->text('Address.secondary.1.0');
  1579. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1580. }
  1581. /**
  1582. * testFormSecurityMultipleControlDisabledFields method
  1583. *
  1584. * Test secure form generation with multiple records and disabled fields.
  1585. *
  1586. * @return void
  1587. */
  1588. public function testFormSecurityMultipleControlDisabledFields()
  1589. {
  1590. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1591. 'unlockedFields' => ['first_name', 'address']
  1592. ]));
  1593. $this->Form->create();
  1594. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1595. $this->Form->text('Addresses.0.title');
  1596. $this->Form->text('Addresses.0.first_name');
  1597. $this->Form->text('Addresses.0.last_name');
  1598. $this->Form->text('Addresses.0.address');
  1599. $this->Form->text('Addresses.0.city');
  1600. $this->Form->text('Addresses.0.phone');
  1601. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1602. $this->Form->text('Addresses.1.title');
  1603. $this->Form->text('Addresses.1.first_name');
  1604. $this->Form->text('Addresses.1.last_name');
  1605. $this->Form->text('Addresses.1.address');
  1606. $this->Form->text('Addresses.1.city');
  1607. $this->Form->text('Addresses.1.phone');
  1608. $result = $this->Form->secure($this->Form->fields);
  1609. $hash = '43c4db25e4162c5e4edd9dea51f5f9d9d92215ec%3AAddresses.0.id%7CAddresses.1.id';
  1610. $tokenDebug = urlencode(json_encode([
  1611. '/articles/add',
  1612. [
  1613. 'Addresses.0.id' => '123456',
  1614. 'Addresses.0.title',
  1615. 'Addresses.0.last_name',
  1616. 'Addresses.0.city',
  1617. 'Addresses.0.phone',
  1618. 'Addresses.1.id' => '654321',
  1619. 'Addresses.1.title',
  1620. 'Addresses.1.last_name',
  1621. 'Addresses.1.city',
  1622. 'Addresses.1.phone'
  1623. ],
  1624. [
  1625. 'first_name',
  1626. 'address'
  1627. ]
  1628. ]));
  1629. $expected = [
  1630. 'div' => ['style' => 'display:none;'],
  1631. ['input' => [
  1632. 'type' => 'hidden',
  1633. 'name' => '_Token[fields]',
  1634. 'autocomplete' => 'off',
  1635. 'value' => $hash
  1636. ]],
  1637. ['input' => [
  1638. 'type' => 'hidden',
  1639. 'name' => '_Token[unlocked]',
  1640. 'autocomplete' => 'off',
  1641. 'value' => 'address%7Cfirst_name',
  1642. ]],
  1643. ['input' => [
  1644. 'type' => 'hidden',
  1645. 'name' => '_Token[debug]',
  1646. 'autocomplete' => 'off',
  1647. 'value' => $tokenDebug,
  1648. ]],
  1649. '/div'
  1650. ];
  1651. $this->assertHtml($expected, $result);
  1652. }
  1653. /**
  1654. * testFormSecurityControlDisabledFields method
  1655. *
  1656. * Test single record form with disabled fields.
  1657. *
  1658. * @return void
  1659. */
  1660. public function testFormSecurityControlUnlockedFields()
  1661. {
  1662. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1663. 'unlockedFields' => ['first_name', 'address']
  1664. ]));
  1665. $this->Form->create();
  1666. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1667. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1668. $this->Form->text('Addresses.title');
  1669. $this->Form->text('Addresses.first_name');
  1670. $this->Form->text('Addresses.last_name');
  1671. $this->Form->text('Addresses.address');
  1672. $this->Form->text('Addresses.city');
  1673. $this->Form->text('Addresses.phone');
  1674. $result = $this->Form->fields;
  1675. $expected = [
  1676. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1677. 'Addresses.city', 'Addresses.phone'
  1678. ];
  1679. $this->assertEquals($expected, $result);
  1680. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1681. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1682. $tokenDebug = urlencode(json_encode([
  1683. '/articles/add',
  1684. [
  1685. 'Addresses.id' => '123456',
  1686. 'Addresses.title',
  1687. 'Addresses.last_name',
  1688. 'Addresses.city',
  1689. 'Addresses.phone'
  1690. ],
  1691. [
  1692. 'first_name',
  1693. 'address'
  1694. ]
  1695. ]));
  1696. $expected = [
  1697. 'div' => ['style' => 'display:none;'],
  1698. ['input' => [
  1699. 'type' => 'hidden',
  1700. 'name' => '_Token[fields]',
  1701. 'value' => $hash,
  1702. 'autocomplete' => 'off',
  1703. 'data-foo' => 'bar',
  1704. ]],
  1705. ['input' => [
  1706. 'type' => 'hidden',
  1707. 'name' => '_Token[unlocked]',
  1708. 'value' => 'address%7Cfirst_name',
  1709. 'autocomplete' => 'off',
  1710. 'data-foo' => 'bar',
  1711. ]],
  1712. ['input' => [
  1713. 'type' => 'hidden', 'name' => '_Token[debug]',
  1714. 'value' => $tokenDebug,
  1715. 'autocomplete' => 'off',
  1716. 'data-foo' => 'bar'
  1717. ]],
  1718. '/div'
  1719. ];
  1720. $this->assertHtml($expected, $result);
  1721. }
  1722. /**
  1723. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1724. *
  1725. * Test single record form with debugSecurity param.
  1726. *
  1727. * @return void
  1728. */
  1729. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1730. {
  1731. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1732. 'unlockedFields' => ['first_name', 'address']
  1733. ]));
  1734. $this->Form->create();
  1735. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1736. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1737. $this->Form->text('Addresses.title');
  1738. $this->Form->text('Addresses.first_name');
  1739. $this->Form->text('Addresses.last_name');
  1740. $this->Form->text('Addresses.address');
  1741. $this->Form->text('Addresses.city');
  1742. $this->Form->text('Addresses.phone');
  1743. $result = $this->Form->fields;
  1744. $expected = [
  1745. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1746. 'Addresses.city', 'Addresses.phone'
  1747. ];
  1748. $this->assertEquals($expected, $result);
  1749. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1750. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1751. $tokenDebug = urlencode(json_encode([
  1752. '/articles/add',
  1753. [
  1754. 'Addresses.id' => '123456',
  1755. 'Addresses.title',
  1756. 'Addresses.last_name',
  1757. 'Addresses.city',
  1758. 'Addresses.phone'
  1759. ],
  1760. [
  1761. 'first_name',
  1762. 'address'
  1763. ]
  1764. ]));
  1765. $expected = [
  1766. 'div' => ['style' => 'display:none;'],
  1767. ['input' => [
  1768. 'type' => 'hidden',
  1769. 'name' => '_Token[fields]',
  1770. 'value' => $hash,
  1771. 'autocomplete' => 'off',
  1772. 'data-foo' => 'bar',
  1773. ]],
  1774. ['input' => [
  1775. 'type' => 'hidden',
  1776. 'name' => '_Token[unlocked]',
  1777. 'value' => 'address%7Cfirst_name',
  1778. 'autocomplete' => 'off',
  1779. 'data-foo' => 'bar',
  1780. ]],
  1781. ['input' => [
  1782. 'type' => 'hidden', 'name' => '_Token[debug]',
  1783. 'value' => $tokenDebug,
  1784. 'autocomplete' => 'off',
  1785. 'data-foo' => 'bar'
  1786. ]],
  1787. '/div'
  1788. ];
  1789. $this->assertHtml($expected, $result);
  1790. }
  1791. /**
  1792. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1793. *
  1794. * Debug is false, debugSecurity is true -> no debug
  1795. *
  1796. * @return void
  1797. */
  1798. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1799. {
  1800. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1801. 'unlockedFields' => ['first_name', 'address']
  1802. ]));
  1803. $this->Form->create();
  1804. $this->assertEquals($this->View->getRequest()->getParam('_Token.unlockedFields'), $this->Form->unlockField());
  1805. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1806. $this->Form->text('Addresses.title');
  1807. $this->Form->text('Addresses.first_name');
  1808. $this->Form->text('Addresses.last_name');
  1809. $this->Form->text('Addresses.address');
  1810. $this->Form->text('Addresses.city');
  1811. $this->Form->text('Addresses.phone');
  1812. $result = $this->Form->fields;
  1813. $expected = [
  1814. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1815. 'Addresses.city', 'Addresses.phone'
  1816. ];
  1817. $this->assertEquals($expected, $result);
  1818. Configure::write('debug', false);
  1819. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1820. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1821. $expected = [
  1822. 'div' => ['style' => 'display:none;'],
  1823. ['input' => [
  1824. 'type' => 'hidden',
  1825. 'name' => '_Token[fields]',
  1826. 'value' => $hash,
  1827. 'autocomplete' => 'off',
  1828. 'data-foo' => 'bar',
  1829. ]],
  1830. ['input' => [
  1831. 'type' => 'hidden',
  1832. 'name' => '_Token[unlocked]',
  1833. 'value' => 'address%7Cfirst_name',
  1834. 'autocomplete' => 'off',
  1835. 'data-foo' => 'bar',
  1836. ]],
  1837. '/div'
  1838. ];
  1839. $this->assertHtml($expected, $result);
  1840. }
  1841. /**
  1842. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1843. *
  1844. * Test single record form with debugSecurity param.
  1845. *
  1846. * @return void
  1847. */
  1848. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1849. {
  1850. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  1851. 'unlockedFields' => ['first_name', 'address']
  1852. ]));
  1853. $this->Form->create();
  1854. $this->assertEquals(
  1855. $this->View->getRequest()->getParam('_Token.unlockedFields'),
  1856. $this->Form->unlockField()
  1857. );
  1858. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1859. $this->Form->text('Addresses.title');
  1860. $this->Form->text('Addresses.first_name');
  1861. $this->Form->text('Addresses.last_name');
  1862. $this->Form->text('Addresses.address');
  1863. $this->Form->text('Addresses.city');
  1864. $this->Form->text('Addresses.phone');
  1865. $result = $this->Form->fields;
  1866. $expected = [
  1867. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1868. 'Addresses.city', 'Addresses.phone'
  1869. ];
  1870. $this->assertEquals($expected, $result);
  1871. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1872. $hash = 'f98315a7d5515e5ae32e35f7d680207c085fae69%3AAddresses.id';
  1873. $expected = [
  1874. 'div' => ['style' => 'display:none;'],
  1875. ['input' => [
  1876. 'type' => 'hidden',
  1877. 'name' => '_Token[fields]',
  1878. 'value' => $hash,
  1879. 'autocomplete' => 'off',
  1880. 'data-foo' => 'bar',
  1881. ]],
  1882. ['input' => [
  1883. 'type' => 'hidden',
  1884. 'name' => '_Token[unlocked]',
  1885. 'value' => 'address%7Cfirst_name',
  1886. 'autocomplete' => 'off',
  1887. 'data-foo' => 'bar',
  1888. ]],
  1889. '/div'
  1890. ];
  1891. $this->assertHtml($expected, $result);
  1892. }
  1893. /**
  1894. * testFormSecureWithCustomNameAttribute method
  1895. *
  1896. * Test securing inputs with custom name attributes.
  1897. *
  1898. * @return void
  1899. */
  1900. public function testFormSecureWithCustomNameAttribute()
  1901. {
  1902. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  1903. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1904. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1905. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1906. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1907. }
  1908. /**
  1909. * testFormSecuredControl method
  1910. *
  1911. * Test generation of entire secure form, assertions made on control() output.
  1912. *
  1913. * @return void
  1914. */
  1915. public function testFormSecuredControl()
  1916. {
  1917. $this->View->setRequest($this->View->getRequest()
  1918. ->withParam('_Token', 'stuff')
  1919. ->withParam('_csrfToken', 'testKey'));
  1920. $this->article['schema'] = [
  1921. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1922. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1923. ];
  1924. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1925. $encoding = strtolower(Configure::read('App.encoding'));
  1926. $expected = [
  1927. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1928. 'div' => ['style' => 'display:none;'],
  1929. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1930. ['input' => [
  1931. 'type' => 'hidden',
  1932. 'name' => '_csrfToken',
  1933. 'value' => 'testKey',
  1934. 'autocomplete' => 'off'
  1935. ]],
  1936. '/div'
  1937. ];
  1938. $this->assertHtml($expected, $result);
  1939. $result = $this->Form->control('ratio');
  1940. $expected = [
  1941. 'div' => ['class'],
  1942. 'label' => ['for'],
  1943. 'Ratio',
  1944. '/label',
  1945. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1946. '/div',
  1947. ];
  1948. $this->assertHtml($expected, $result);
  1949. $result = $this->Form->control('population');
  1950. $expected = [
  1951. 'div' => ['class'],
  1952. 'label' => ['for'],
  1953. 'Population',
  1954. '/label',
  1955. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1956. '/div',
  1957. ];
  1958. $this->assertHtml($expected, $result);
  1959. $result = $this->Form->control('published', ['type' => 'text']);
  1960. $expected = [
  1961. 'div' => ['class' => 'input text'],
  1962. 'label' => ['for' => 'published'],
  1963. 'Published',
  1964. '/label',
  1965. ['input' => [
  1966. 'type' => 'text',
  1967. 'name' => 'published',
  1968. 'id' => 'published'
  1969. ]],
  1970. '/div'
  1971. ];
  1972. $this->assertHtml($expected, $result);
  1973. $result = $this->Form->control('other', ['type' => 'text']);
  1974. $expected = [
  1975. 'div' => ['class' => 'input text'],
  1976. 'label' => ['for' => 'other'],
  1977. 'Other',
  1978. '/label',
  1979. ['input' => [
  1980. 'type' => 'text',
  1981. 'name' => 'other',
  1982. 'id',
  1983. ]],
  1984. '/div'
  1985. ];
  1986. $this->assertHtml($expected, $result);
  1987. $result = $this->Form->hidden('stuff');
  1988. $expected = [
  1989. 'input' => [
  1990. 'type' => 'hidden',
  1991. 'name' => 'stuff'
  1992. ]
  1993. ];
  1994. $this->assertHtml($expected, $result);
  1995. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1996. $expected = ['input' => [
  1997. 'type' => 'hidden',
  1998. 'name' => 'hidden',
  1999. 'value' => '0'
  2000. ]];
  2001. $this->assertHtml($expected, $result);
  2002. $result = $this->Form->control('something', ['type' => 'checkbox']);
  2003. $expected = [
  2004. 'div' => ['class' => 'input checkbox'],
  2005. ['input' => [
  2006. 'type' => 'hidden',
  2007. 'name' => 'something',
  2008. 'value' => '0'
  2009. ]],
  2010. 'label' => ['for' => 'something'],
  2011. ['input' => [
  2012. 'type' => 'checkbox',
  2013. 'name' => 'something',
  2014. 'value' => '1',
  2015. 'id' => 'something'
  2016. ]],
  2017. 'Something',
  2018. '/label',
  2019. '/div'
  2020. ];
  2021. $this->assertHtml($expected, $result);
  2022. $result = $this->Form->fields;
  2023. $expectedFields = [
  2024. 'ratio',
  2025. 'population',
  2026. 'published',
  2027. 'other',
  2028. 'stuff' => '',
  2029. 'hidden' => '0',
  2030. 'something'
  2031. ];
  2032. $this->assertEquals($expectedFields, $result);
  2033. $result = $this->Form->secure($this->Form->fields);
  2034. $tokenDebug = urlencode(json_encode([
  2035. '/articles/add',
  2036. $expectedFields,
  2037. []
  2038. ]));
  2039. $expected = [
  2040. 'div' => ['style' => 'display:none;'],
  2041. ['input' => [
  2042. 'type' => 'hidden',
  2043. 'name' => '_Token[fields]',
  2044. 'value',
  2045. 'autocomplete'
  2046. ]],
  2047. ['input' => [
  2048. 'type' => 'hidden',
  2049. 'name' => '_Token[unlocked]',
  2050. 'value' => '',
  2051. 'autocomplete' => 'off'
  2052. ]],
  2053. ['input' => [
  2054. 'type' => 'hidden', 'name' => '_Token[debug]',
  2055. 'value' => $tokenDebug,
  2056. 'autocomplete' => 'off'
  2057. ]],
  2058. '/div'
  2059. ];
  2060. $this->assertHtml($expected, $result);
  2061. }
  2062. /**
  2063. * testSecuredControlCustomName method
  2064. *
  2065. * Test secured inputs with custom names.
  2066. *
  2067. * @return void
  2068. */
  2069. public function testSecuredControlCustomName()
  2070. {
  2071. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2072. $this->assertEquals([], $this->Form->fields);
  2073. $this->Form->text('text_input', [
  2074. 'name' => 'Option[General.default_role]',
  2075. ]);
  2076. $expected = ['Option.General.default_role'];
  2077. $this->assertEquals($expected, $this->Form->fields);
  2078. $this->Form->select('select_box', [1, 2], [
  2079. 'name' => 'Option[General.select_role]',
  2080. ]);
  2081. $expected[] = 'Option.General.select_role';
  2082. $this->assertEquals($expected, $this->Form->fields);
  2083. $this->Form->text('other.things[]');
  2084. $expected[] = 'other.things';
  2085. $this->assertEquals($expected, $this->Form->fields);
  2086. }
  2087. /**
  2088. * testSecuredControlDuplicate method
  2089. *
  2090. * Test that a hidden field followed by a visible field
  2091. * undoes the hidden field locking.
  2092. *
  2093. * @return void
  2094. */
  2095. public function testSecuredControlDuplicate()
  2096. {
  2097. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2098. $this->assertEquals([], $this->Form->fields);
  2099. $this->Form->control('text_val', [
  2100. 'type' => 'hidden',
  2101. 'value' => 'some text',
  2102. ]);
  2103. $expected = ['text_val' => 'some text'];
  2104. $this->assertEquals($expected, $this->Form->fields);
  2105. $this->Form->control('text_val', [
  2106. 'type' => 'text',
  2107. ]);
  2108. $expected = ['text_val'];
  2109. $this->assertEquals($expected, $this->Form->fields);
  2110. }
  2111. /**
  2112. * testFormSecuredFileControl method
  2113. *
  2114. * Tests that the correct keys are added to the field hash index.
  2115. *
  2116. * @return void
  2117. */
  2118. public function testFormSecuredFileControl()
  2119. {
  2120. $this->assertEquals([], $this->Form->fields);
  2121. $this->Form->file('Attachment.file');
  2122. $expected = [
  2123. 'Attachment.file.name', 'Attachment.file.type',
  2124. 'Attachment.file.tmp_name', 'Attachment.file.error',
  2125. 'Attachment.file.size'
  2126. ];
  2127. $this->assertEquals($expected, $this->Form->fields);
  2128. }
  2129. /**
  2130. * testFormSecuredMultipleSelect method
  2131. *
  2132. * Test that multiple selects keys are added to field hash.
  2133. *
  2134. * @return void
  2135. */
  2136. public function testFormSecuredMultipleSelect()
  2137. {
  2138. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'testKey'));
  2139. $this->assertEquals([], $this->Form->fields);
  2140. $options = ['1' => 'one', '2' => 'two'];
  2141. $this->Form->select('Model.select', $options);
  2142. $expected = ['Model.select'];
  2143. $this->assertEquals($expected, $this->Form->fields);
  2144. $this->Form->fields = [];
  2145. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2146. $this->assertEquals($expected, $this->Form->fields);
  2147. }
  2148. /**
  2149. * testFormSecuredRadio method
  2150. *
  2151. * @return void
  2152. */
  2153. public function testFormSecuredRadio()
  2154. {
  2155. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2156. $this->assertEquals([], $this->Form->fields);
  2157. $options = ['1' => 'option1', '2' => 'option2'];
  2158. $this->Form->radio('Test.test', $options);
  2159. $expected = ['Test.test'];
  2160. $this->assertEquals($expected, $this->Form->fields);
  2161. $this->Form->radio('Test.all', $options, [
  2162. 'disabled' => ['option1', 'option2']
  2163. ]);
  2164. $expected = ['Test.test', 'Test.all' => ''];
  2165. $this->assertEquals($expected, $this->Form->fields);
  2166. $this->Form->radio('Test.some', $options, [
  2167. 'disabled' => ['option1']
  2168. ]);
  2169. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2170. $this->assertEquals($expected, $this->Form->fields);
  2171. }
  2172. /**
  2173. * testFormSecuredAndDisabledNotAssoc method
  2174. *
  2175. * Test that when disabled is in a list based attribute array it works.
  2176. *
  2177. * @return void
  2178. */
  2179. public function testFormSecuredAndDisabledNotAssoc()
  2180. {
  2181. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2182. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2183. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2184. $this->Form->text('Model.text', ['disabled']);
  2185. $this->Form->textarea('Model.textarea', ['disabled']);
  2186. $this->Form->password('Model.password', ['disabled']);
  2187. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2188. $expected = [
  2189. 'Model.radio' => ''
  2190. ];
  2191. $this->assertEquals($expected, $this->Form->fields);
  2192. }
  2193. /**
  2194. * testFormSecuredAndDisabled method
  2195. *
  2196. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2197. * hashing.
  2198. *
  2199. * @return void
  2200. */
  2201. public function testFormSecuredAndDisabled()
  2202. {
  2203. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2204. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2205. $this->Form->text('Model.text', ['disabled' => true]);
  2206. $this->Form->password('Model.text', ['disabled' => true]);
  2207. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2208. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2209. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2210. $this->Form->year('Model.year', ['disabled' => true]);
  2211. $this->Form->month('Model.month', ['disabled' => true]);
  2212. $this->Form->day('Model.day', ['disabled' => true]);
  2213. $this->Form->hour('Model.hour', ['disabled' => true]);
  2214. $this->Form->minute('Model.minute', ['disabled' => true]);
  2215. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2216. $expected = [
  2217. 'Model.radio' => ''
  2218. ];
  2219. $this->assertEquals($expected, $this->Form->fields);
  2220. }
  2221. /**
  2222. * testDisableSecurityUsingForm method
  2223. *
  2224. * @return void
  2225. */
  2226. public function testDisableSecurityUsingForm()
  2227. {
  2228. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2229. 'disabledFields' => []
  2230. ]));
  2231. $this->Form->create();
  2232. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2233. $this->Form->text('Addresses.title');
  2234. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2235. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2236. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2237. $result = $this->Form->fields;
  2238. $expected = [
  2239. 'Addresses.id' => '123456', 'Addresses.title',
  2240. ];
  2241. $this->assertEquals($expected, $result);
  2242. }
  2243. /**
  2244. * testUnlockFieldAddsToList method
  2245. *
  2246. * Test disableField.
  2247. *
  2248. * @return void
  2249. */
  2250. public function testUnlockFieldAddsToList()
  2251. {
  2252. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2253. 'unlockedFields' => []
  2254. ]));
  2255. $this->Form->unlockField('Contact.name');
  2256. $this->Form->text('Contact.name');
  2257. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2258. $this->assertEquals([], $this->Form->fields);
  2259. }
  2260. /**
  2261. * testUnlockFieldRemovingFromFields method
  2262. *
  2263. * Test unlockField removing from fields array.
  2264. *
  2265. * @return void
  2266. */
  2267. public function testUnlockFieldRemovingFromFields()
  2268. {
  2269. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2270. 'unlockedFields' => []
  2271. ]));
  2272. $this->Form->create($this->article);
  2273. $this->Form->hidden('Article.id', ['value' => 1]);
  2274. $this->Form->text('Article.title');
  2275. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2276. $this->assertContains('Article.title', $this->Form->fields, 'Field should be secured.');
  2277. $this->Form->unlockField('Article.title');
  2278. $this->Form->unlockField('Article.id');
  2279. $this->assertEquals([], $this->Form->fields);
  2280. }
  2281. /**
  2282. * testResetUnlockFields method
  2283. *
  2284. * Test reset unlockFields, when create new form.
  2285. *
  2286. * @return void
  2287. */
  2288. public function testResetUnlockFields()
  2289. {
  2290. $this->View->setRequest($this->View->getRequest()->withParam('_Token', [
  2291. 'key' => 'testKey',
  2292. 'unlockedFields' => []
  2293. ]));
  2294. $this->Form->unlockField('Contact.id');
  2295. $this->Form->create('Contact');
  2296. $this->Form->hidden('Contact.id', ['value' => 1]);
  2297. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2298. $this->Form->end();
  2299. $this->Form->create('Contact');
  2300. $this->Form->hidden('Contact.id', ['value' => 1]);
  2301. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2302. }
  2303. /**
  2304. * testSecuredFormUrlIgnoresHost method
  2305. *
  2306. * Test that only the path + query elements of a form's URL show up in their hash.
  2307. *
  2308. * @return void
  2309. */
  2310. public function testSecuredFormUrlIgnoresHost()
  2311. {
  2312. $this->View->setRequest($this->View->getRequest()->withParam('_Token', ['key' => 'testKey']));
  2313. $expected = '2548654895b160d724042ed269a2a863fd9d66ee%3A';
  2314. $this->Form->create($this->article, [
  2315. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2316. ]);
  2317. $result = $this->Form->secure();
  2318. $this->assertContains($expected, $result);
  2319. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2320. $result = $this->Form->secure();
  2321. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2322. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2323. $result = $this->Form->secure();
  2324. $this->assertContains($expected, $result, 'URL path + query should work.');
  2325. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2326. $result = $this->Form->secure();
  2327. $this->assertNotContains($expected, $result, 'URL is different');
  2328. }
  2329. /**
  2330. * testSecuredFormUrlHasHtmlAndIdentifier method
  2331. *
  2332. * Test that URL, HTML and identifier show up in their hashes.
  2333. *
  2334. * @return void
  2335. */
  2336. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2337. {
  2338. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  2339. $expected = '0a913f45b887b4d9cc2650ef1edc50183896959c%3A';
  2340. $this->Form->create($this->article, [
  2341. 'url' => [
  2342. 'controller' => 'articles',
  2343. 'action' => 'view',
  2344. '?' => [
  2345. 'page' => 1,
  2346. 'limit' => 10,
  2347. 'html' => '<>"',
  2348. ],
  2349. '#' => 'result',
  2350. ],
  2351. ]);
  2352. $result = $this->Form->secure();
  2353. $this->assertContains($expected, $result);
  2354. $this->Form->create($this->article, [
  2355. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2356. ]);
  2357. $result = $this->Form->secure();
  2358. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2359. $this->Form->create($this->article, [
  2360. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2361. ]);
  2362. $result = $this->Form->secure();
  2363. $this->assertContains($expected, $result, 'URL path + query should work.');
  2364. }
  2365. /**
  2366. * testErrorMessageDisplay method
  2367. *
  2368. * Test error message display.
  2369. *
  2370. * @return void
  2371. */
  2372. public function testErrorMessageDisplay()
  2373. {
  2374. $this->article['errors'] = [
  2375. 'Article' => [
  2376. 'title' => 'error message',
  2377. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2378. ]
  2379. ];
  2380. $this->Form->create($this->article);
  2381. $result = $this->Form->control('Article.title');
  2382. $expected = [
  2383. 'div' => ['class' => 'input text error'],
  2384. 'label' => ['for' => 'article-title'],
  2385. 'Title',
  2386. '/label',
  2387. 'input' => [
  2388. 'type' => 'text', 'name' => 'Article[title]',
  2389. 'id' => 'article-title', 'class' => 'form-error'
  2390. ],
  2391. ['div' => ['class' => 'error-message']],
  2392. 'error message',
  2393. '/div',
  2394. '/div'
  2395. ];
  2396. $this->assertHtml($expected, $result);
  2397. $result = $this->Form->control('Article.title', [
  2398. 'templates' => [
  2399. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2400. ]
  2401. ]);
  2402. $expected = [
  2403. 'div' => ['class' => 'input text error'],
  2404. 'label' => ['for' => 'article-title'],
  2405. 'Title',
  2406. '/label',
  2407. 'input' => [
  2408. 'type' => 'text', 'name' => 'Article[title]',
  2409. 'id' => 'article-title', 'class' => 'form-error'
  2410. ],
  2411. '/div'
  2412. ];
  2413. $this->assertHtml($expected, $result);
  2414. $result = $this->Form->control('Article.content');
  2415. $expected = [
  2416. 'div' => ['class' => 'input text error'],
  2417. 'label' => ['for' => 'article-content'],
  2418. 'Content',
  2419. '/label',
  2420. 'input' => [
  2421. 'type' => 'text', 'name' => 'Article[content]',
  2422. 'id' => 'article-content', 'class' => 'form-error'
  2423. ],
  2424. ['div' => ['class' => 'error-message']],
  2425. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2426. '/div',
  2427. '/div'
  2428. ];
  2429. $this->assertHtml($expected, $result);
  2430. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2431. $expected = [
  2432. 'div' => ['class' => 'input text error'],
  2433. 'label' => ['for' => 'article-content'],
  2434. 'Content',
  2435. '/label',
  2436. 'input' => [
  2437. 'type' => 'text', 'name' => 'Article[content]',
  2438. 'id' => 'article-content', 'class' => 'form-error'
  2439. ],
  2440. ['div' => ['class' => 'error-message']],
  2441. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2442. '/div',
  2443. '/div'
  2444. ];
  2445. $this->assertHtml($expected, $result);
  2446. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2447. $expected = [
  2448. 'div' => ['class' => 'input text error'],
  2449. 'label' => ['for' => 'article-content'],
  2450. 'Content',
  2451. '/label',
  2452. 'input' => [
  2453. 'type' => 'text', 'name' => 'Article[content]',
  2454. 'id' => 'article-content', 'class' => 'form-error'
  2455. ],
  2456. ['div' => ['class' => 'error-message']],
  2457. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2458. '/div',
  2459. '/div'
  2460. ];
  2461. $this->assertHtml($expected, $result);
  2462. }
  2463. /**
  2464. * testEmptyErrorValidation method
  2465. *
  2466. * Test validation errors, when validation message is an empty string.
  2467. *
  2468. * @return void
  2469. */
  2470. public function testEmptyErrorValidation()
  2471. {
  2472. $this->article['errors'] = [
  2473. 'Article' => ['title' => '']
  2474. ];
  2475. $this->Form->create($this->article);
  2476. $result = $this->Form->control('Article.title');
  2477. $expected = [
  2478. 'div' => ['class' => 'input text error'],
  2479. 'label' => ['for' => 'article-title'],
  2480. 'Title',
  2481. '/label',
  2482. 'input' => [
  2483. 'type' => 'text', 'name' => 'Article[title]',
  2484. 'id' => 'article-title', 'class' => 'form-error'
  2485. ],
  2486. ['div' => ['class' => 'error-message']],
  2487. [],
  2488. '/div',
  2489. '/div'
  2490. ];
  2491. $this->assertHtml($expected, $result);
  2492. }
  2493. /**
  2494. * testEmptyControlErrorValidation method
  2495. *
  2496. * Test validation errors, when calling control() overriding validation message by an empty string.
  2497. *
  2498. * @return void
  2499. */
  2500. public function testEmptyControlErrorValidation()
  2501. {
  2502. $this->article['errors'] = [
  2503. 'Article' => ['title' => 'error message']
  2504. ];
  2505. $this->Form->create($this->article);
  2506. $result = $this->Form->control('Article.title', ['error' => '']);
  2507. $expected = [
  2508. 'div' => ['class' => 'input text error'],
  2509. 'label' => ['for' => 'article-title'],
  2510. 'Title',
  2511. '/label',
  2512. 'input' => [
  2513. 'type' => 'text', 'name' => 'Article[title]',
  2514. 'id' => 'article-title', 'class' => 'form-error'
  2515. ],
  2516. ['div' => ['class' => 'error-message']],
  2517. [],
  2518. '/div',
  2519. '/div'
  2520. ];
  2521. $this->assertHtml($expected, $result);
  2522. }
  2523. /**
  2524. * testControlErrorMessage method
  2525. *
  2526. * Test validation errors, when calling control() overriding validation messages.
  2527. *
  2528. * @return void
  2529. */
  2530. public function testControlErrorMessage()
  2531. {
  2532. $this->article['errors'] = [
  2533. 'title' => ['error message']
  2534. ];
  2535. $this->Form->create($this->article);
  2536. $result = $this->Form->control('title', [
  2537. 'error' => 'Custom error!'
  2538. ]);
  2539. $expected = [
  2540. 'div' => ['class' => 'input text required error'],
  2541. 'label' => ['for' => 'title'],
  2542. 'Title',
  2543. '/label',
  2544. 'input' => [
  2545. 'type' => 'text', 'name' => 'title',
  2546. 'id' => 'title', 'class' => 'form-error',
  2547. 'required' => 'required',
  2548. ],
  2549. ['div' => ['class' => 'error-message']],
  2550. 'Custom error!',
  2551. '/div',
  2552. '/div'
  2553. ];
  2554. $this->assertHtml($expected, $result);
  2555. $result = $this->Form->control('title', [
  2556. 'error' => ['error message' => 'Custom error!']
  2557. ]);
  2558. $expected = [
  2559. 'div' => ['class' => 'input text required error'],
  2560. 'label' => ['for' => 'title'],
  2561. 'Title',
  2562. '/label',
  2563. 'input' => [
  2564. 'type' => 'text',
  2565. 'name' => 'title',
  2566. 'id' => 'title',
  2567. 'class' => 'form-error',
  2568. 'required' => 'required'
  2569. ],
  2570. ['div' => ['class' => 'error-message']],
  2571. 'Custom error!',
  2572. '/div',
  2573. '/div'
  2574. ];
  2575. $this->assertHtml($expected, $result);
  2576. }
  2577. /**
  2578. * testFormValidationAssociated method
  2579. *
  2580. * Tests displaying errors for nested entities.
  2581. *
  2582. * @return void
  2583. */
  2584. public function testFormValidationAssociated()
  2585. {
  2586. $nested = new Entity(['foo' => 'bar']);
  2587. $nested->setError('foo', ['not a valid bar']);
  2588. $entity = new Entity(['nested' => $nested]);
  2589. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2590. $result = $this->Form->error('nested.foo');
  2591. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2592. }
  2593. /**
  2594. * testFormValidationAssociatedSecondLevel method
  2595. *
  2596. * Test form error display with associated model.
  2597. *
  2598. * @return void
  2599. */
  2600. public function testFormValidationAssociatedSecondLevel()
  2601. {
  2602. $inner = new Entity(['bar' => 'baz']);
  2603. $nested = new Entity(['foo' => $inner]);
  2604. $entity = new Entity(['nested' => $nested]);
  2605. $inner->setError('bar', ['not a valid one']);
  2606. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2607. $result = $this->Form->error('nested.foo.bar');
  2608. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2609. }
  2610. /**
  2611. * testFormValidationMultiRecord method
  2612. *
  2613. * Test form error display with multiple records.
  2614. *
  2615. * @return void
  2616. */
  2617. public function testFormValidationMultiRecord()
  2618. {
  2619. $one = new Entity();
  2620. $two = new Entity();
  2621. $this->getTableLocator()->get('Contacts', [
  2622. 'className' => __NAMESPACE__ . '\ContactsTable'
  2623. ]);
  2624. $one->set('email', '');
  2625. $one->setError('email', ['invalid email']);
  2626. $two->set('name', '');
  2627. $two->setError('name', ['This is wrong']);
  2628. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2629. $result = $this->Form->control('0.email');
  2630. $expected = [
  2631. 'div' => ['class' => 'input email error'],
  2632. 'label' => ['for' => '0-email'],
  2633. 'Email',
  2634. '/label',
  2635. 'input' => [
  2636. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2637. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2638. ],
  2639. ['div' => ['class' => 'error-message']],
  2640. 'invalid email',
  2641. '/div',
  2642. '/div'
  2643. ];
  2644. $this->assertHtml($expected, $result);
  2645. $result = $this->Form->control('1.name');
  2646. $expected = [
  2647. 'div' => ['class' => 'input text error'],
  2648. 'label' => ['for' => '1-name'],
  2649. 'Name',
  2650. '/label',
  2651. 'input' => [
  2652. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2653. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2654. ],
  2655. ['div' => ['class' => 'error-message']],
  2656. 'This is wrong',
  2657. '/div',
  2658. '/div'
  2659. ];
  2660. $this->assertHtml($expected, $result);
  2661. }
  2662. /**
  2663. * testControl method
  2664. *
  2665. * Test various incarnations of control().
  2666. *
  2667. * @return void
  2668. */
  2669. public function testControl()
  2670. {
  2671. $this->getTableLocator()->get('ValidateUsers', [
  2672. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2673. ]);
  2674. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2675. $result = $this->Form->control('ValidateUsers.balance');
  2676. $expected = [
  2677. 'div' => ['class'],
  2678. 'label' => ['for'],
  2679. 'Balance',
  2680. '/label',
  2681. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2682. '/div',
  2683. ];
  2684. $this->assertHtml($expected, $result);
  2685. $result = $this->Form->control('ValidateUser.cost_decimal');
  2686. $expected = [
  2687. 'div' => ['class'],
  2688. 'label' => ['for'],
  2689. 'Cost Decimal',
  2690. '/label',
  2691. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2692. '/div',
  2693. ];
  2694. $this->assertHtml($expected, $result);
  2695. $result = $this->Form->control('ValidateUser.null_decimal');
  2696. $expected = [
  2697. 'div' => ['class'],
  2698. 'label' => ['for'],
  2699. 'Null Decimal',
  2700. '/label',
  2701. 'input' => ['name', 'type' => 'number', 'id'],
  2702. '/div',
  2703. ];
  2704. $this->assertHtml($expected, $result);
  2705. }
  2706. /**
  2707. * testControlCustomization method
  2708. *
  2709. * Tests the input method and passing custom options.
  2710. *
  2711. * @return void
  2712. */
  2713. public function testControlCustomization()
  2714. {
  2715. $this->getTableLocator()->get('Contacts', [
  2716. 'className' => __NAMESPACE__ . '\ContactsTable'
  2717. ]);
  2718. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2719. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2720. $expected = [
  2721. 'div' => ['class' => 'input email'],
  2722. 'label' => ['for' => 'custom'],
  2723. 'Email',
  2724. '/label',
  2725. ['input' => [
  2726. 'type' => 'email', 'name' => 'Contact[email]',
  2727. 'id' => 'custom', 'maxlength' => 255
  2728. ]],
  2729. '/div'
  2730. ];
  2731. $this->assertHtml($expected, $result);
  2732. $result = $this->Form->control('Contact.email', [
  2733. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2734. ]);
  2735. $expected = [
  2736. '<div',
  2737. 'label' => ['for' => 'contact-email'],
  2738. 'Email',
  2739. '/label',
  2740. ['input' => [
  2741. 'type' => 'email', 'name' => 'Contact[email]',
  2742. 'id' => 'contact-email', 'maxlength' => 255
  2743. ]],
  2744. '/div'
  2745. ];
  2746. $this->assertHtml($expected, $result);
  2747. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2748. $expected = [
  2749. 'div' => ['class' => 'input text'],
  2750. 'label' => ['for' => 'contact-email'],
  2751. 'Email',
  2752. '/label',
  2753. ['input' => [
  2754. 'type' => 'text', 'name' => 'Contact[email]',
  2755. 'id' => 'contact-email', 'maxlength' => '255'
  2756. ]],
  2757. '/div'
  2758. ];
  2759. $this->assertHtml($expected, $result);
  2760. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2761. $expected = [
  2762. 'div' => ['class' => 'input text'],
  2763. 'label' => ['for' => 'contact-5-email'],
  2764. 'Email',
  2765. '/label',
  2766. ['input' => [
  2767. 'type' => 'text', 'name' => 'Contact[5][email]',
  2768. 'id' => 'contact-5-email', 'maxlength' => '255'
  2769. ]],
  2770. '/div'
  2771. ];
  2772. $this->assertHtml($expected, $result);
  2773. $result = $this->Form->control('Contact.password');
  2774. $expected = [
  2775. 'div' => ['class' => 'input password'],
  2776. 'label' => ['for' => 'contact-password'],
  2777. 'Password',
  2778. '/label',
  2779. ['input' => [
  2780. 'type' => 'password', 'name' => 'Contact[password]',
  2781. 'id' => 'contact-password'
  2782. ]],
  2783. '/div'
  2784. ];
  2785. $this->assertHtml($expected, $result);
  2786. $result = $this->Form->control('Contact.email', [
  2787. 'type' => 'file', 'class' => 'textbox'
  2788. ]);
  2789. $expected = [
  2790. 'div' => ['class' => 'input file'],
  2791. 'label' => ['for' => 'contact-email'],
  2792. 'Email',
  2793. '/label',
  2794. ['input' => [
  2795. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2796. 'id' => 'contact-email'
  2797. ]],
  2798. '/div'
  2799. ];
  2800. $this->assertHtml($expected, $result);
  2801. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2802. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2803. $result = $this->Form->control('phone');
  2804. $expected = [
  2805. 'div' => ['class' => 'input tel'],
  2806. 'label' => ['for' => 'phone'],
  2807. 'Phone',
  2808. '/label',
  2809. ['input' => [
  2810. 'type' => 'tel', 'name' => 'phone',
  2811. 'value' => 'Hello &amp; World &gt; weird chars',
  2812. 'id' => 'phone', 'maxlength' => 255
  2813. ]],
  2814. '/div'
  2815. ];
  2816. $this->assertHtml($expected, $result);
  2817. $this->View->setRequest(
  2818. $this->View->getRequest()->withData('Model.0.OtherModel.field', 'My value')
  2819. );
  2820. $this->Form->create();
  2821. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2822. $expected = [
  2823. 'div' => ['class' => 'input text'],
  2824. 'label' => ['for' => 'myId'],
  2825. 'Field',
  2826. '/label',
  2827. 'input' => [
  2828. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2829. 'value' => 'My value', 'id' => 'myId'
  2830. ],
  2831. '/div'
  2832. ];
  2833. $this->assertHtml($expected, $result);
  2834. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  2835. $this->Form->create();
  2836. $entity->setError('field', 'Badness!');
  2837. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2838. $result = $this->Form->control('field');
  2839. $expected = [
  2840. 'div' => ['class' => 'input text error'],
  2841. 'label' => ['for' => 'field'],
  2842. 'Field',
  2843. '/label',
  2844. 'input' => [
  2845. 'type' => 'text', 'name' => 'field',
  2846. 'id' => 'field', 'class' => 'form-error'
  2847. ],
  2848. ['div' => ['class' => 'error-message']],
  2849. 'Badness!',
  2850. '/div',
  2851. '/div'
  2852. ];
  2853. $this->assertHtml($expected, $result);
  2854. $result = $this->Form->control('field', [
  2855. 'templates' => [
  2856. 'inputContainerError' => '{{content}}{{error}}',
  2857. 'error' => '<span class="error-message">{{content}}</span>'
  2858. ]
  2859. ]);
  2860. $expected = [
  2861. 'label' => ['for' => 'field'],
  2862. 'Field',
  2863. '/label',
  2864. 'input' => [
  2865. 'type' => 'text', 'name' => 'field',
  2866. 'id' => 'field', 'class' => 'form-error'
  2867. ],
  2868. ['span' => ['class' => 'error-message']],
  2869. 'Badness!',
  2870. '/span'
  2871. ];
  2872. $this->assertHtml($expected, $result);
  2873. $entity->setError('field', ['minLength'], true);
  2874. $result = $this->Form->control('field', [
  2875. 'error' => [
  2876. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2877. 'maxLength' => 'login too large'
  2878. ]
  2879. ]);
  2880. $expected = [
  2881. 'div' => ['class' => 'input text error'],
  2882. 'label' => ['for' => 'field'],
  2883. 'Field',
  2884. '/label',
  2885. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2886. ['div' => ['class' => 'error-message']],
  2887. 'Le login doit contenir au moins 2 caractères',
  2888. '/div',
  2889. '/div'
  2890. ];
  2891. $this->assertHtml($expected, $result);
  2892. $entity->setError('field', ['maxLength'], true);
  2893. $result = $this->Form->control('field', [
  2894. 'error' => [
  2895. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2896. 'maxLength' => 'login too large',
  2897. ]
  2898. ]);
  2899. $expected = [
  2900. 'div' => ['class' => 'input text error'],
  2901. 'label' => ['for' => 'field'],
  2902. 'Field',
  2903. '/label',
  2904. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2905. ['div' => ['class' => 'error-message']],
  2906. 'login too large',
  2907. '/div',
  2908. '/div'
  2909. ];
  2910. $this->assertHtml($expected, $result);
  2911. }
  2912. /**
  2913. * testControlWithTemplateFile method
  2914. *
  2915. * Test that control() accepts a template file.
  2916. *
  2917. * @return void
  2918. */
  2919. public function testControlWithTemplateFile()
  2920. {
  2921. $result = $this->Form->control('field', [
  2922. 'templates' => 'htmlhelper_tags'
  2923. ]);
  2924. $expected = [
  2925. 'label' => ['for' => 'field'],
  2926. 'Field',
  2927. '/label',
  2928. 'input' => [
  2929. 'type' => 'text', 'name' => 'field',
  2930. 'id' => 'field'
  2931. ],
  2932. ];
  2933. $this->assertHtml($expected, $result);
  2934. }
  2935. /**
  2936. * testNestedControlsEndWithBrackets method
  2937. *
  2938. * Test that nested inputs end with brackets.
  2939. *
  2940. * @return void
  2941. */
  2942. public function testNestedControlsEndWithBrackets()
  2943. {
  2944. $result = $this->Form->text('nested.text[]');
  2945. $expected = [
  2946. 'input' => [
  2947. 'type' => 'text', 'name' => 'nested[text][]'
  2948. ],
  2949. ];
  2950. $this->assertHtml($expected, $result);
  2951. $result = $this->Form->file('nested.file[]');
  2952. $expected = [
  2953. 'input' => [
  2954. 'type' => 'file', 'name' => 'nested[file][]'
  2955. ],
  2956. ];
  2957. $this->assertHtml($expected, $result);
  2958. }
  2959. /**
  2960. * testCreateIdPrefix method
  2961. *
  2962. * Test id prefix.
  2963. *
  2964. * @return void
  2965. */
  2966. public function testCreateIdPrefix()
  2967. {
  2968. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2969. $result = $this->Form->control('field');
  2970. $expected = [
  2971. 'div' => ['class' => 'input text'],
  2972. 'label' => ['for' => 'prefix-field'],
  2973. 'Field',
  2974. '/label',
  2975. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2976. '/div'
  2977. ];
  2978. $this->assertHtml($expected, $result);
  2979. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2980. $expected = [
  2981. 'div' => ['class' => 'input text'],
  2982. 'label' => ['for' => 'custom-id'],
  2983. 'Field',
  2984. '/label',
  2985. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2986. '/div'
  2987. ];
  2988. $this->assertHtml($expected, $result);
  2989. $result = $this->Form->radio('Model.field', ['option A']);
  2990. $expected = [
  2991. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2992. 'label' => ['for' => 'prefix-model-field-0'],
  2993. ['input' => [
  2994. 'type' => 'radio',
  2995. 'name' => 'Model[field]',
  2996. 'value' => '0',
  2997. 'id' => 'prefix-model-field-0'
  2998. ]],
  2999. 'option A',
  3000. '/label'
  3001. ];
  3002. $this->assertHtml($expected, $result);
  3003. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  3004. $expected = [
  3005. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3006. 'label' => ['for' => 'prefix-model-field-0'],
  3007. ['input' => [
  3008. 'type' => 'radio',
  3009. 'name' => 'Model[field]',
  3010. 'value' => '0',
  3011. 'id' => 'prefix-model-field-0'
  3012. ]],
  3013. 'option A',
  3014. '/label'
  3015. ];
  3016. $this->assertHtml($expected, $result);
  3017. $result = $this->Form->select(
  3018. 'Model.multi_field',
  3019. ['first'],
  3020. ['multiple' => 'checkbox']
  3021. );
  3022. $expected = [
  3023. 'input' => [
  3024. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3025. ],
  3026. ['div' => ['class' => 'checkbox']],
  3027. ['label' => ['for' => 'prefix-model-multi-field-0']],
  3028. ['input' => [
  3029. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3030. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  3031. ]],
  3032. 'first',
  3033. '/label',
  3034. '/div',
  3035. ];
  3036. $this->assertHtml($expected, $result);
  3037. $this->Form->end();
  3038. $result = $this->Form->control('field');
  3039. $expected = [
  3040. 'div' => ['class' => 'input text'],
  3041. 'label' => ['for' => 'field'],
  3042. 'Field',
  3043. '/label',
  3044. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  3045. '/div'
  3046. ];
  3047. $this->assertHtml($expected, $result);
  3048. }
  3049. /**
  3050. * testControlZero method
  3051. *
  3052. * Test that inputs with 0 can be created.
  3053. *
  3054. * @return void
  3055. */
  3056. public function testControlZero()
  3057. {
  3058. $this->getTableLocator()->get('Contacts', [
  3059. 'className' => __NAMESPACE__ . '\ContactsTable'
  3060. ]);
  3061. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  3062. $result = $this->Form->control('0');
  3063. $expected = [
  3064. 'div' => ['class' => 'input text'],
  3065. 'label' => ['for' => '0'], '/label',
  3066. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  3067. '/div'
  3068. ];
  3069. $this->assertHtml($expected, $result);
  3070. }
  3071. /**
  3072. * testControlCheckbox method
  3073. *
  3074. * Test control() with checkbox creation.
  3075. *
  3076. * @return void
  3077. */
  3078. public function testControlCheckbox()
  3079. {
  3080. $articles = $this->getTableLocator()->get('Articles');
  3081. $articles->getSchema()->addColumn('active', ['type' => 'boolean', 'default' => null]);
  3082. $article = $articles->newEntity();
  3083. $this->Form->create($article);
  3084. $result = $this->Form->control('Articles.active');
  3085. $expected = [
  3086. 'div' => ['class' => 'input checkbox'],
  3087. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3088. 'label' => ['for' => 'articles-active'],
  3089. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active']],
  3090. 'Active',
  3091. '/label',
  3092. '/div'
  3093. ];
  3094. $this->assertHtml($expected, $result);
  3095. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => true]);
  3096. $expected = [
  3097. 'div' => ['class' => 'input checkbox'],
  3098. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3099. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3100. '/div'
  3101. ];
  3102. $this->assertHtml($expected, $result);
  3103. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => 1]);
  3104. $expected = [
  3105. 'div' => ['class' => 'input checkbox'],
  3106. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3107. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3108. '/div'
  3109. ];
  3110. $this->assertHtml($expected, $result);
  3111. $result = $this->Form->control('Articles.active', ['label' => false, 'checked' => '1']);
  3112. $expected = [
  3113. 'div' => ['class' => 'input checkbox'],
  3114. 'input' => ['type' => 'hidden', 'name' => 'Articles[active]', 'value' => '0'],
  3115. ['input' => ['type' => 'checkbox', 'name' => 'Articles[active]', 'value' => '1', 'id' => 'articles-active', 'checked' => 'checked']],
  3116. '/div'
  3117. ];
  3118. $this->assertHtml($expected, $result);
  3119. $result = $this->Form->control('Articles.disabled', [
  3120. 'label' => 'Disabled',
  3121. 'type' => 'checkbox',
  3122. 'data-foo' => 'disabled'
  3123. ]);
  3124. $expected = [
  3125. 'div' => ['class' => 'input checkbox'],
  3126. 'input' => ['type' => 'hidden', 'name' => 'Articles[disabled]', 'value' => '0'],
  3127. 'label' => ['for' => 'articles-disabled'],
  3128. ['input' => [
  3129. 'type' => 'checkbox',
  3130. 'name' => 'Articles[disabled]',
  3131. 'value' => '1',
  3132. 'id' => 'articles-disabled',
  3133. 'data-foo' => 'disabled'
  3134. ]],
  3135. 'Disabled',
  3136. '/label',
  3137. '/div'
  3138. ];
  3139. $this->assertHtml($expected, $result);
  3140. $result = $this->Form->control('Articles.confirm', [
  3141. 'label' => 'Confirm <b>me</b>!',
  3142. 'type' => 'checkbox',
  3143. 'escape' => false
  3144. ]);
  3145. $expected = [
  3146. 'div' => ['class' => 'input checkbox'],
  3147. 'input' => ['type' => 'hidden', 'name' => 'Articles[confirm]', 'value' => '0'],
  3148. 'label' => ['for' => 'articles-confirm'],
  3149. ['input' => [
  3150. 'type' => 'checkbox',
  3151. 'name' => 'Articles[confirm]',
  3152. 'value' => '1',
  3153. 'id' => 'articles-confirm',
  3154. ]],
  3155. 'Confirm <b>me</b>!',
  3156. '/label',
  3157. '/div'
  3158. ];
  3159. $this->assertHtml($expected, $result);
  3160. }
  3161. /**
  3162. * testControlHidden method
  3163. *
  3164. * Test that control() does not create wrapping div and label tag for hidden fields.
  3165. *
  3166. * @return void
  3167. */
  3168. public function testControlHidden()
  3169. {
  3170. $this->getTableLocator()->get('ValidateUsers', [
  3171. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3172. ]);
  3173. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3174. $result = $this->Form->control('ValidateUser.id');
  3175. $expected = [
  3176. 'input' => ['name', 'type' => 'hidden', 'id']
  3177. ];
  3178. $this->assertHtml($expected, $result);
  3179. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3180. $expected = [
  3181. 'input' => ['name', 'type' => 'hidden', 'id']
  3182. ];
  3183. $this->assertHtml($expected, $result);
  3184. }
  3185. /**
  3186. * testControlDatetime method
  3187. *
  3188. * Test form->control() with datetime.
  3189. *
  3190. * @return void
  3191. */
  3192. public function testControlDatetime()
  3193. {
  3194. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3195. ->setMethods(['datetime'])
  3196. ->setConstructorArgs([new View()])
  3197. ->getMock();
  3198. $this->Form->expects($this->once())->method('datetime')
  3199. ->with('prueba', [
  3200. 'type' => 'datetime',
  3201. 'timeFormat' => 24,
  3202. 'minYear' => 2008,
  3203. 'maxYear' => 2011,
  3204. 'interval' => 15,
  3205. 'options' => null,
  3206. 'empty' => false,
  3207. 'id' => 'prueba',
  3208. 'required' => false,
  3209. 'templateVars' => []
  3210. ])
  3211. ->will($this->returnValue('This is it!'));
  3212. $result = $this->Form->control('prueba', [
  3213. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3214. 'maxYear' => 2011, 'interval' => 15
  3215. ]);
  3216. $expected = [
  3217. 'div' => ['class' => 'input datetime'],
  3218. '<label',
  3219. 'Prueba',
  3220. '/label',
  3221. 'This is it!',
  3222. '/div'
  3223. ];
  3224. $this->assertHtml($expected, $result);
  3225. }
  3226. /**
  3227. * testControlDatetimeIdPrefix method
  3228. *
  3229. * Test form->control() with datetime with id prefix.
  3230. *
  3231. * @return void
  3232. */
  3233. public function testControlDatetimeIdPrefix()
  3234. {
  3235. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3236. ->setMethods(['datetime'])
  3237. ->setConstructorArgs([new View()])
  3238. ->getMock();
  3239. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3240. $this->Form->expects($this->once())->method('datetime')
  3241. ->with('prueba', [
  3242. 'type' => 'datetime',
  3243. 'timeFormat' => 24,
  3244. 'minYear' => 2008,
  3245. 'maxYear' => 2011,
  3246. 'interval' => 15,
  3247. 'options' => null,
  3248. 'empty' => false,
  3249. 'id' => 'prefix-prueba',
  3250. 'required' => false,
  3251. 'templateVars' => []
  3252. ])
  3253. ->will($this->returnValue('This is it!'));
  3254. $result = $this->Form->control('prueba', [
  3255. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3256. 'maxYear' => 2011, 'interval' => 15
  3257. ]);
  3258. $expected = [
  3259. 'div' => ['class' => 'input datetime'],
  3260. '<label',
  3261. 'Prueba',
  3262. '/label',
  3263. 'This is it!',
  3264. '/div'
  3265. ];
  3266. $this->assertHtml($expected, $result);
  3267. }
  3268. /**
  3269. * testControlCheckboxWithDisabledElements method
  3270. *
  3271. * Test generating checkboxes with disabled elements.
  3272. *
  3273. * @return void
  3274. */
  3275. public function testControlCheckboxWithDisabledElements()
  3276. {
  3277. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3278. $result = $this->Form->control('Contact.multiple', [
  3279. 'multiple' => 'checkbox',
  3280. 'disabled' => 'disabled',
  3281. 'options' => $options
  3282. ]);
  3283. $expected = [
  3284. ['div' => ['class' => 'input select']],
  3285. ['label' => ['for' => 'contact-multiple']],
  3286. 'Multiple',
  3287. '/label',
  3288. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'disabled' => 'disabled', 'value' => '']],
  3289. ['div' => ['class' => 'checkbox']],
  3290. ['label' => ['for' => 'contact-multiple-1']],
  3291. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 1, 'disabled' => 'disabled', 'id' => 'contact-multiple-1']],
  3292. 'One',
  3293. '/label',
  3294. '/div',
  3295. ['div' => ['class' => 'checkbox']],
  3296. ['label' => ['for' => 'contact-multiple-2']],
  3297. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 2, 'disabled' => 'disabled', 'id' => 'contact-multiple-2']],
  3298. 'Two',
  3299. '/label',
  3300. '/div',
  3301. ['div' => ['class' => 'checkbox']],
  3302. ['label' => ['for' => 'contact-multiple-3']],
  3303. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 3, 'disabled' => 'disabled', 'id' => 'contact-multiple-3']],
  3304. 'Three',
  3305. '/label',
  3306. '/div',
  3307. '/div'
  3308. ];
  3309. $this->assertHtml($expected, $result);
  3310. // make sure 50 does only disable 50, and not 50f5c0cf
  3311. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3312. $disabled = [50];
  3313. $expected = [
  3314. ['div' => ['class' => 'input select']],
  3315. ['label' => ['for' => 'contact-multiple']],
  3316. 'Multiple',
  3317. '/label',
  3318. ['input' => ['type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '']],
  3319. ['div' => ['class' => 'checkbox']],
  3320. ['label' => ['for' => 'contact-multiple-50']],
  3321. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => 50, 'disabled' => 'disabled', 'id' => 'contact-multiple-50']],
  3322. 'Fifty',
  3323. '/label',
  3324. '/div',
  3325. ['div' => ['class' => 'checkbox']],
  3326. ['label' => ['for' => 'contact-multiple-50f5c0cf']],
  3327. ['input' => ['type' => 'checkbox', 'name' => 'Contact[multiple][]', 'value' => '50f5c0cf', 'id' => 'contact-multiple-50f5c0cf']],
  3328. 'Stringy',
  3329. '/label',
  3330. '/div',
  3331. '/div'
  3332. ];
  3333. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3334. $this->assertHtml($expected, $result);
  3335. }
  3336. /**
  3337. * testControlWithLeadingInteger method
  3338. *
  3339. * Test input name with leading integer, ensure attributes are generated correctly.
  3340. *
  3341. * @return void
  3342. */
  3343. public function testControlWithLeadingInteger()
  3344. {
  3345. $result = $this->Form->text('0.Node.title');
  3346. $expected = [
  3347. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3348. ];
  3349. $this->assertHtml($expected, $result);
  3350. }
  3351. /**
  3352. * testControlSelectType method
  3353. *
  3354. * Test form->control() with select type inputs.
  3355. *
  3356. * @return void
  3357. */
  3358. public function testControlSelectType()
  3359. {
  3360. $result = $this->Form->control(
  3361. 'email',
  3362. [
  3363. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3364. );
  3365. $expected = [
  3366. 'div' => ['class' => 'input select'],
  3367. 'label' => ['for' => 'email'],
  3368. 'Email',
  3369. '/label',
  3370. ['select' => ['name' => 'email', 'id' => 'email']],
  3371. ['option' => ['value' => '']],
  3372. '/option',
  3373. ['option' => ['value' => 'è']],
  3374. 'Firést',
  3375. '/option',
  3376. ['option' => ['value' => 'é']],
  3377. 'Secoènd',
  3378. '/option',
  3379. '/select',
  3380. '/div'
  3381. ];
  3382. $this->assertHtml($expected, $result);
  3383. $result = $this->Form->control(
  3384. 'email',
  3385. [
  3386. 'options' => ['First', 'Second'], 'empty' => true]
  3387. );
  3388. $expected = [
  3389. 'div' => ['class' => 'input select'],
  3390. 'label' => ['for' => 'email'],
  3391. 'Email',
  3392. '/label',
  3393. ['select' => ['name' => 'email', 'id' => 'email']],
  3394. ['option' => ['value' => '']],
  3395. '/option',
  3396. ['option' => ['value' => '0']],
  3397. 'First',
  3398. '/option',
  3399. ['option' => ['value' => '1']],
  3400. 'Second',
  3401. '/option',
  3402. '/select',
  3403. '/div'
  3404. ];
  3405. $this->assertHtml($expected, $result);
  3406. $result = $this->Form->control('email', [
  3407. 'type' => 'select',
  3408. 'options' => new \ArrayObject(['First', 'Second']),
  3409. 'empty' => true
  3410. ]);
  3411. $this->assertHtml($expected, $result);
  3412. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3413. $this->View->setRequest(
  3414. $this->View->getRequest()->withData('Model', ['user_id' => 'value'])
  3415. );
  3416. $this->Form->create();
  3417. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3418. $expected = [
  3419. 'div' => ['class' => 'input select'],
  3420. 'label' => ['for' => 'model-user-id'],
  3421. 'User',
  3422. '/label',
  3423. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3424. ['option' => ['value' => '']],
  3425. '/option',
  3426. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3427. 'good',
  3428. '/option',
  3429. ['option' => ['value' => 'other']],
  3430. 'bad',
  3431. '/option',
  3432. '/select',
  3433. '/div'
  3434. ];
  3435. $this->assertHtml($expected, $result);
  3436. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3437. $this->View->setRequest(
  3438. $this->View->getRequest()->withData('Thing', ['user_id' => null])
  3439. );
  3440. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3441. $expected = [
  3442. 'div' => ['class' => 'input select'],
  3443. 'label' => ['for' => 'thing-user-id'],
  3444. 'User',
  3445. '/label',
  3446. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3447. ['option' => ['value' => '']],
  3448. 'Some Empty',
  3449. '/option',
  3450. ['option' => ['value' => 'value']],
  3451. 'good',
  3452. '/option',
  3453. ['option' => ['value' => 'other']],
  3454. 'bad',
  3455. '/option',
  3456. '/select',
  3457. '/div'
  3458. ];
  3459. $this->assertHtml($expected, $result);
  3460. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3461. $this->View->setRequest(
  3462. $this->View->getRequest()->withData('Thing', ['user_id' => 'value'])
  3463. );
  3464. $this->Form->create();
  3465. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3466. $expected = [
  3467. 'div' => ['class' => 'input select'],
  3468. 'label' => ['for' => 'thing-user-id'],
  3469. 'User',
  3470. '/label',
  3471. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3472. ['option' => ['value' => '']],
  3473. 'Some Empty',
  3474. '/option',
  3475. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3476. 'good',
  3477. '/option',
  3478. ['option' => ['value' => 'other']],
  3479. 'bad',
  3480. '/option',
  3481. '/select',
  3482. '/div'
  3483. ];
  3484. $this->assertHtml($expected, $result);
  3485. $result = $this->Form->control('Publisher.id', [
  3486. 'label' => 'Publisher',
  3487. 'type' => 'select',
  3488. 'multiple' => 'checkbox',
  3489. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3490. ]);
  3491. $expected = [
  3492. ['div' => ['class' => 'input select']],
  3493. ['label' => ['for' => 'publisher-id']],
  3494. 'Publisher',
  3495. '/label',
  3496. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3497. ['div' => ['class' => 'checkbox']],
  3498. ['label' => ['for' => 'publisher-id-value-1']],
  3499. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3500. 'Label 1',
  3501. '/label',
  3502. '/div',
  3503. ['div' => ['class' => 'checkbox']],
  3504. ['label' => ['for' => 'publisher-id-value-2']],
  3505. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3506. 'Label 2',
  3507. '/label',
  3508. '/div',
  3509. '/div'
  3510. ];
  3511. $this->assertHtml($expected, $result);
  3512. }
  3513. /**
  3514. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3515. *
  3516. * Test that control() and a non standard primary key makes a hidden input by default.
  3517. *
  3518. * @return void
  3519. */
  3520. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3521. {
  3522. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3523. $this->Form->create($this->article);
  3524. $result = $this->Form->control('title');
  3525. $expected = [
  3526. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3527. ];
  3528. $this->assertHtml($expected, $result);
  3529. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3530. $this->Form->create($this->article);
  3531. $result = $this->Form->control('title');
  3532. $expected = [
  3533. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3534. ];
  3535. $this->assertHtml($expected, $result);
  3536. $result = $this->Form->control('body');
  3537. $expected = [
  3538. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3539. ];
  3540. $this->assertHtml($expected, $result);
  3541. }
  3542. /**
  3543. * testControlOverridingMagicSelectType method
  3544. *
  3545. * Test that overriding the magic select type widget is possible.
  3546. *
  3547. * @return void
  3548. */
  3549. public function testControlOverridingMagicSelectType()
  3550. {
  3551. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3552. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3553. $expected = [
  3554. 'div' => ['class' => 'input text'],
  3555. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3556. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3557. '/div'
  3558. ];
  3559. $this->assertHtml($expected, $result);
  3560. //Check that magic types still work for plural/singular vars
  3561. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3562. $result = $this->Form->control('Model.type');
  3563. $expected = [
  3564. 'div' => ['class' => 'input select'],
  3565. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3566. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3567. ['option' => ['value' => 'value']], 'good', '/option',
  3568. ['option' => ['value' => 'other']], 'bad', '/option',
  3569. '/select',
  3570. '/div'
  3571. ];
  3572. $this->assertHtml($expected, $result);
  3573. }
  3574. /**
  3575. * testControlMagicTypeDoesNotOverride method
  3576. *
  3577. * Test that inferred types do not override developer input.
  3578. *
  3579. * @return void
  3580. */
  3581. public function testControlMagicTypeDoesNotOverride()
  3582. {
  3583. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3584. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3585. $expected = [
  3586. 'div' => ['class' => 'input checkbox'],
  3587. ['input' => [
  3588. 'type' => 'hidden',
  3589. 'name' => 'Model[user]',
  3590. 'value' => 0,
  3591. ]],
  3592. 'label' => ['for' => 'model-user'],
  3593. ['input' => [
  3594. 'name' => 'Model[user]',
  3595. 'type' => 'checkbox',
  3596. 'id' => 'model-user',
  3597. 'value' => 1
  3598. ]],
  3599. 'User',
  3600. '/label',
  3601. '/div'
  3602. ];
  3603. $this->assertHtml($expected, $result);
  3604. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3605. $options = [
  3606. 1 => 'blue',
  3607. 2 => 'red'
  3608. ];
  3609. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3610. $expected = [
  3611. 'div' => ['class' => 'input select'],
  3612. 'label' => ['for' => 'tags-ids'],
  3613. 'Tags',
  3614. '/label',
  3615. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3616. ['div' => ['class' => 'checkbox']],
  3617. ['label' => ['for' => 'tags-ids-1']],
  3618. ['input' => [
  3619. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3620. 'value' => '1', 'name' => 'tags[_ids][]'
  3621. ]],
  3622. 'blue',
  3623. '/label',
  3624. '/div',
  3625. ['div' => ['class' => 'checkbox']],
  3626. ['label' => ['for' => 'tags-ids-2']],
  3627. ['input' => [
  3628. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3629. 'value' => '2', 'name' => 'tags[_ids][]'
  3630. ]],
  3631. 'red',
  3632. '/label',
  3633. '/div',
  3634. '/div'
  3635. ];
  3636. $this->assertHtml($expected, $result);
  3637. }
  3638. /**
  3639. * testControlMagicSelectForTypeNumber method
  3640. *
  3641. * Test that magic control() selects are created for type=number.
  3642. *
  3643. * @return void
  3644. */
  3645. public function testControlMagicSelectForTypeNumber()
  3646. {
  3647. $this->getTableLocator()->get('ValidateUsers', [
  3648. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3649. ]);
  3650. $entity = new Entity(['balance' => 1]);
  3651. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3652. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3653. $result = $this->Form->control('balance');
  3654. $expected = [
  3655. 'div' => ['class' => 'input select'],
  3656. 'label' => ['for' => 'balance'],
  3657. 'Balance',
  3658. '/label',
  3659. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3660. ['option' => ['value' => '0']],
  3661. 'nothing',
  3662. '/option',
  3663. ['option' => ['value' => '1', 'selected' => 'selected']],
  3664. 'some',
  3665. '/option',
  3666. ['option' => ['value' => '100']],
  3667. 'a lot',
  3668. '/option',
  3669. '/select',
  3670. '/div'
  3671. ];
  3672. $this->assertHtml($expected, $result);
  3673. }
  3674. /**
  3675. * testInvalidControlTypeOption method
  3676. *
  3677. * Test invalid 'input' type option to control() function.
  3678. *
  3679. * @return void
  3680. */
  3681. public function testInvalidControlTypeOption()
  3682. {
  3683. $this->expectException(\RuntimeException::class);
  3684. $this->expectExceptionMessage('Invalid type \'input\' used for field \'text\'');
  3685. $this->Form->control('text', ['type' => 'input']);
  3686. }
  3687. /**
  3688. * testControlMagicSelectChangeToRadio method
  3689. *
  3690. * Test that magic control() selects can easily be converted into radio types without error.
  3691. *
  3692. * @return void
  3693. */
  3694. public function testControlMagicSelectChangeToRadio()
  3695. {
  3696. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3697. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3698. $this->assertContains('input type="radio"', $result);
  3699. }
  3700. /**
  3701. * testFormControlSubmit method
  3702. *
  3703. * Test correct results for form::control() and type submit.
  3704. *
  3705. * @return void
  3706. */
  3707. public function testFormControlSubmit()
  3708. {
  3709. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3710. $expected = [
  3711. 'div' => ['class' => 'submit'],
  3712. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3713. '/div'
  3714. ];
  3715. $this->assertHtml($expected, $result);
  3716. }
  3717. /**
  3718. * testFormControls method
  3719. *
  3720. * Test correct results from Form::controls().
  3721. *
  3722. * @return void
  3723. */
  3724. public function testFormControlsLegendFieldset()
  3725. {
  3726. $this->Form->create($this->article);
  3727. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3728. $expected = [
  3729. '<fieldset',
  3730. '<legend',
  3731. 'The Legend',
  3732. '/legend',
  3733. '*/fieldset',
  3734. ];
  3735. $this->assertHtml($expected, $result);
  3736. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3737. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3738. $this->assertContains('<fieldset>', $result);
  3739. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3740. $this->assertNotContains('<legend>', $result);
  3741. $this->assertNotContains('<fieldset>', $result);
  3742. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3743. $this->assertNotContains('<legend>', $result);
  3744. $this->assertNotContains('<fieldset>', $result);
  3745. $this->Form->create($this->article);
  3746. $this->View->setRequest($this->View->getRequest()
  3747. ->withParam('prefix', 'admin')
  3748. ->withParam('action', 'admin_edit')
  3749. ->withParam('controller', 'articles'));
  3750. $result = $this->Form->allControls();
  3751. $expected = [
  3752. '<fieldset',
  3753. '<legend',
  3754. 'New Article',
  3755. '/legend',
  3756. '*/fieldset',
  3757. ];
  3758. $this->assertHtml($expected, $result);
  3759. $this->Form->create($this->article);
  3760. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3761. $expected = [
  3762. '<fieldset',
  3763. '<legend',
  3764. 'The Legend',
  3765. '/legend',
  3766. '*/fieldset',
  3767. ];
  3768. $this->assertHtml($expected, $result);
  3769. $this->Form->create($this->article);
  3770. $result = $this->Form->allControls([], [
  3771. 'fieldset' => [
  3772. 'class' => 'some-class some-other-class',
  3773. 'disabled' => true,
  3774. 'data-param' => 'a-param'
  3775. ],
  3776. 'legend' => 'The Legend'
  3777. ]);
  3778. $expected = [
  3779. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3780. '<legend',
  3781. 'The Legend',
  3782. '/legend',
  3783. '*/fieldset',
  3784. ];
  3785. $this->assertHtml($expected, $result);
  3786. }
  3787. /**
  3788. * testFormControls method
  3789. *
  3790. * Test the controls() method.
  3791. *
  3792. * @return void
  3793. */
  3794. public function testFormControls()
  3795. {
  3796. $this->Form->create($this->article);
  3797. $result = $this->Form->allControls();
  3798. $expected = [
  3799. '<fieldset',
  3800. '<legend', 'New Article', '/legend',
  3801. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3802. ['div' => ['class' => 'input select required']],
  3803. '*/div',
  3804. ['div' => ['class' => 'input text required']],
  3805. '*/div',
  3806. ['div' => ['class' => 'input text']],
  3807. '*/div',
  3808. ['div' => ['class' => 'input text']],
  3809. '*/div',
  3810. '/fieldset',
  3811. ];
  3812. $this->assertHtml($expected, $result);
  3813. $result = $this->Form->allControls([
  3814. 'published' => ['type' => 'boolean']
  3815. ]);
  3816. $expected = [
  3817. '<fieldset',
  3818. '<legend', 'New Article', '/legend',
  3819. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3820. ['div' => ['class' => 'input select required']],
  3821. '*/div',
  3822. ['div' => ['class' => 'input text required']],
  3823. '*/div',
  3824. ['div' => ['class' => 'input text']],
  3825. '*/div',
  3826. ['div' => ['class' => 'input boolean']],
  3827. '*/div',
  3828. '/fieldset',
  3829. ];
  3830. $this->assertHtml($expected, $result);
  3831. $this->Form->create($this->article);
  3832. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3833. $expected = [
  3834. 'fieldset' => [],
  3835. 'legend' => [],
  3836. 'Hello',
  3837. '/legend',
  3838. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3839. ['div' => ['class' => 'input select required']],
  3840. '*/div',
  3841. ['div' => ['class' => 'input text required']],
  3842. '*/div',
  3843. ['div' => ['class' => 'input text']],
  3844. '*/div',
  3845. ['div' => ['class' => 'input text']],
  3846. '*/div',
  3847. '/fieldset'
  3848. ];
  3849. $this->assertHtml($expected, $result);
  3850. $this->Form->create(false);
  3851. $expected = [
  3852. 'fieldset' => [],
  3853. ['div' => ['class' => 'input text']],
  3854. 'label' => ['for' => 'foo'],
  3855. 'Foo',
  3856. '/label',
  3857. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3858. '*/div',
  3859. '/fieldset'
  3860. ];
  3861. $result = $this->Form->allControls(
  3862. ['foo' => ['type' => 'text']],
  3863. ['legend' => false]
  3864. );
  3865. $this->assertHtml($expected, $result);
  3866. }
  3867. /**
  3868. * testFormControlsBlacklist method
  3869. *
  3870. * @return void
  3871. */
  3872. public function testFormControlsBlacklist()
  3873. {
  3874. $this->Form->create($this->article);
  3875. $result = $this->Form->allControls([
  3876. 'id' => false
  3877. ]);
  3878. $expected = [
  3879. '<fieldset',
  3880. '<legend', 'New Article', '/legend',
  3881. ['div' => ['class' => 'input select required']],
  3882. '*/div',
  3883. ['div' => ['class' => 'input text required']],
  3884. '*/div',
  3885. ['div' => ['class' => 'input text']],
  3886. '*/div',
  3887. ['div' => ['class' => 'input text']],
  3888. '*/div',
  3889. '/fieldset',
  3890. ];
  3891. $this->assertHtml($expected, $result);
  3892. $this->Form->create($this->article);
  3893. $result = $this->Form->allControls([
  3894. 'id' => []
  3895. ]);
  3896. $expected = [
  3897. '<fieldset',
  3898. '<legend', 'New Article', '/legend',
  3899. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3900. ['div' => ['class' => 'input select required']],
  3901. '*/div',
  3902. ['div' => ['class' => 'input text required']],
  3903. '*/div',
  3904. ['div' => ['class' => 'input text']],
  3905. '*/div',
  3906. ['div' => ['class' => 'input text']],
  3907. '*/div',
  3908. '/fieldset',
  3909. ];
  3910. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3911. }
  3912. /**
  3913. * testSelectAsCheckbox method
  3914. *
  3915. * Test multi-select widget with checkbox formatting.
  3916. *
  3917. * @return void
  3918. */
  3919. public function testSelectAsCheckbox()
  3920. {
  3921. $result = $this->Form->select(
  3922. 'Model.multi_field',
  3923. ['first', 'second', 'third'],
  3924. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3925. );
  3926. $expected = [
  3927. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3928. ['div' => ['class' => 'checkbox']],
  3929. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3930. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3931. 'first',
  3932. '/label',
  3933. '/div',
  3934. ['div' => ['class' => 'checkbox']],
  3935. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3936. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3937. 'second',
  3938. '/label',
  3939. '/div',
  3940. ['div' => ['class' => 'checkbox']],
  3941. ['label' => ['for' => 'model-multi-field-2']],
  3942. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3943. 'third',
  3944. '/label',
  3945. '/div',
  3946. ];
  3947. $this->assertHtml($expected, $result);
  3948. $result = $this->Form->select(
  3949. 'Model.multi_field',
  3950. ['1/2' => 'half'],
  3951. ['multiple' => 'checkbox']
  3952. );
  3953. $expected = [
  3954. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3955. ['div' => ['class' => 'checkbox']],
  3956. ['label' => ['for' => 'model-multi-field-1-2']],
  3957. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3958. 'half',
  3959. '/label',
  3960. '/div',
  3961. ];
  3962. $this->assertHtml($expected, $result);
  3963. }
  3964. /**
  3965. * testLabel method
  3966. *
  3967. * Test label generation.
  3968. *
  3969. * @return void
  3970. */
  3971. public function testLabel()
  3972. {
  3973. $result = $this->Form->label('Person.name');
  3974. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3975. $this->assertHtml($expected, $result);
  3976. $result = $this->Form->label('Person.name');
  3977. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3978. $this->assertHtml($expected, $result);
  3979. $result = $this->Form->label('Person.first_name');
  3980. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3981. $this->assertHtml($expected, $result);
  3982. $result = $this->Form->label('Person.first_name', 'Your first name');
  3983. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3984. $this->assertHtml($expected, $result);
  3985. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3986. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3987. $this->assertHtml($expected, $result);
  3988. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3989. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3990. $this->assertHtml($expected, $result);
  3991. $result = $this->Form->label('Person.first_name', '');
  3992. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3993. $this->assertHtml($expected, $result);
  3994. $result = $this->Form->label('Person.2.name', '');
  3995. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3996. $this->assertHtml($expected, $result);
  3997. }
  3998. /**
  3999. * testLabelContainControl method
  4000. *
  4001. * Test that label() can accept an input with the correct template vars.
  4002. *
  4003. * @return void
  4004. */
  4005. public function testLabelContainControl()
  4006. {
  4007. $this->Form->setTemplates([
  4008. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4009. ]);
  4010. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  4011. 'input' => '<input type="checkbox" name="accept_tos"/>'
  4012. ]);
  4013. $expected = [
  4014. 'label' => ['for' => 'person-accept-terms'],
  4015. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  4016. 'Accept',
  4017. '/label',
  4018. ];
  4019. $this->assertHtml($expected, $result);
  4020. }
  4021. /**
  4022. * testTextbox method
  4023. *
  4024. * Test textbox element generation.
  4025. *
  4026. * @return void
  4027. */
  4028. public function testTextbox()
  4029. {
  4030. $result = $this->Form->text('Model.field');
  4031. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  4032. $this->assertHtml($expected, $result);
  4033. $result = $this->Form->text('Model.field', ['type' => 'password']);
  4034. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  4035. $this->assertHtml($expected, $result);
  4036. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  4037. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  4038. $this->assertHtml($expected, $result);
  4039. }
  4040. /**
  4041. * testTextBoxDataAndError method
  4042. *
  4043. * Test that text() hooks up with request data and error fields.
  4044. *
  4045. * @return void
  4046. */
  4047. public function testTextBoxDataAndError()
  4048. {
  4049. $this->article['errors'] = [
  4050. 'Contact' => ['text' => 'wrong']
  4051. ];
  4052. $this->View->setRequest($this->View->getRequest()
  4053. ->withData('Model.text', 'test <strong>HTML</strong> values')
  4054. ->withData('Contact.text', 'test'));
  4055. $this->Form->create($this->article);
  4056. $result = $this->Form->text('Model.text');
  4057. $expected = [
  4058. 'input' => [
  4059. 'type' => 'text',
  4060. 'name' => 'Model[text]',
  4061. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  4062. ]
  4063. ];
  4064. $this->assertHtml($expected, $result);
  4065. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  4066. $expected = [
  4067. 'input' => [
  4068. 'type' => 'text',
  4069. 'name' => 'Contact[text]',
  4070. 'value' => 'test',
  4071. 'id' => 'theID',
  4072. 'class' => 'form-error'
  4073. ]
  4074. ];
  4075. $this->assertHtml($expected, $result);
  4076. }
  4077. /**
  4078. * testDefaultValue method
  4079. *
  4080. * Test default value setting.
  4081. *
  4082. * @return void
  4083. */
  4084. public function testTextDefaultValue()
  4085. {
  4086. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 'test'));
  4087. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4088. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  4089. $this->assertHtml($expected, $result);
  4090. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4091. $this->Form->create();
  4092. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  4093. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  4094. $this->assertHtml($expected, $result);
  4095. $Articles = $this->getTableLocator()->get('Articles');
  4096. $title = $Articles->getSchema()->getColumn('title');
  4097. $Articles->getSchema()->addColumn(
  4098. 'title',
  4099. ['default' => 'default title'] + $title
  4100. );
  4101. $entity = $Articles->newEntity();
  4102. $this->Form->create($entity);
  4103. // Get default value from schema
  4104. $result = $this->Form->text('title');
  4105. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  4106. $this->assertHtml($expected, $result);
  4107. // Don't get value from schema
  4108. $result = $this->Form->text('title', ['schemaDefault' => false]);
  4109. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4110. $this->assertHtml($expected, $result);
  4111. // Custom default value overrides default value from schema
  4112. $result = $this->Form->text('title', ['default' => 'override default']);
  4113. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  4114. $this->assertHtml($expected, $result);
  4115. // Default value from schema is used only for new entities.
  4116. $entity->isNew(false);
  4117. $result = $this->Form->text('title');
  4118. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  4119. $this->assertHtml($expected, $result);
  4120. }
  4121. /**
  4122. * testError method
  4123. *
  4124. * Test field error generation.
  4125. *
  4126. * @return void
  4127. */
  4128. public function testError()
  4129. {
  4130. $this->article['errors'] = [
  4131. 'Article' => ['field' => 'email']
  4132. ];
  4133. $this->Form->create($this->article);
  4134. $result = $this->Form->error('Article.field');
  4135. $expected = [
  4136. ['div' => ['class' => 'error-message']],
  4137. 'email',
  4138. '/div',
  4139. ];
  4140. $this->assertHtml($expected, $result);
  4141. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>');
  4142. $expected = [
  4143. ['div' => ['class' => 'error-message']],
  4144. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  4145. '/div',
  4146. ];
  4147. $this->assertHtml($expected, $result);
  4148. $result = $this->Form->error('Article.field', '<strong>Badness!</strong>', ['escape' => false]);
  4149. $expected = [
  4150. ['div' => ['class' => 'error-message']],
  4151. '<strong', 'Badness!', '/strong',
  4152. '/div',
  4153. ];
  4154. $this->assertHtml($expected, $result);
  4155. }
  4156. /**
  4157. * testErrorRuleName method
  4158. *
  4159. * Test error translation can use rule names for translating.
  4160. *
  4161. * @return void
  4162. */
  4163. public function testErrorRuleName()
  4164. {
  4165. $this->article['errors'] = [
  4166. 'Article' => [
  4167. 'field' => ['email' => 'Your email was not good']
  4168. ]
  4169. ];
  4170. $this->Form->create($this->article);
  4171. $result = $this->Form->error('Article.field');
  4172. $expected = [
  4173. ['div' => ['class' => 'error-message']],
  4174. 'Your email was not good',
  4175. '/div',
  4176. ];
  4177. $this->assertHtml($expected, $result);
  4178. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4179. $expected = [
  4180. ['div' => ['class' => 'error-message']],
  4181. 'Email in use',
  4182. '/div',
  4183. ];
  4184. $this->assertHtml($expected, $result);
  4185. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4186. $expected = [
  4187. ['div' => ['class' => 'error-message']],
  4188. 'Email in use',
  4189. '/div',
  4190. ];
  4191. $this->assertHtml($expected, $result);
  4192. $result = $this->Form->error('Article.field', [
  4193. 'email' => 'Key is preferred',
  4194. 'Your email was not good' => 'Email in use'
  4195. ]);
  4196. $expected = [
  4197. ['div' => ['class' => 'error-message']],
  4198. 'Key is preferred',
  4199. '/div',
  4200. ];
  4201. $this->assertHtml($expected, $result);
  4202. }
  4203. /**
  4204. * testErrorMessages method
  4205. *
  4206. * Test error with nested lists.
  4207. *
  4208. * @return void
  4209. */
  4210. public function testErrorMessages()
  4211. {
  4212. $this->article['errors'] = [
  4213. 'Article' => ['field' => 'email']
  4214. ];
  4215. $this->Form->create($this->article);
  4216. $result = $this->Form->error('Article.field', [
  4217. 'email' => 'No good!'
  4218. ]);
  4219. $expected = [
  4220. 'div' => ['class' => 'error-message'],
  4221. 'No good!',
  4222. '/div'
  4223. ];
  4224. $this->assertHtml($expected, $result);
  4225. }
  4226. /**
  4227. * testErrorMultipleMessages method
  4228. *
  4229. * Test error() with multiple messages.
  4230. *
  4231. * @return void
  4232. */
  4233. public function testErrorMultipleMessages()
  4234. {
  4235. $this->article['errors'] = [
  4236. 'field' => ['notBlank', 'email', 'Something else']
  4237. ];
  4238. $this->Form->create($this->article);
  4239. $result = $this->Form->error('field', [
  4240. 'notBlank' => 'Cannot be empty',
  4241. 'email' => 'No good!'
  4242. ]);
  4243. $expected = [
  4244. 'div' => ['class' => 'error-message'],
  4245. 'ul' => [],
  4246. '<li', 'Cannot be empty', '/li',
  4247. '<li', 'No good!', '/li',
  4248. '<li', 'Something else', '/li',
  4249. '/ul',
  4250. '/div'
  4251. ];
  4252. $this->assertHtml($expected, $result);
  4253. }
  4254. /**
  4255. * testPassword method
  4256. *
  4257. * Test password element generation.
  4258. *
  4259. * @return void
  4260. */
  4261. public function testPassword()
  4262. {
  4263. $this->article['errors'] = [
  4264. 'Contact' => [
  4265. 'passwd' => 1
  4266. ]
  4267. ];
  4268. $this->Form->create($this->article);
  4269. $result = $this->Form->password('Contact.field');
  4270. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4271. $this->assertHtml($expected, $result);
  4272. $this->View->setRequest($this->View->getRequest()->withData('Contact.passwd', 'test'));
  4273. $this->Form->create($this->article);
  4274. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4275. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4276. $this->assertHtml($expected, $result);
  4277. }
  4278. /**
  4279. * testRadio method
  4280. *
  4281. * Test radio element set generation.
  4282. *
  4283. * @return void
  4284. */
  4285. public function testRadio()
  4286. {
  4287. $result = $this->Form->radio('Model.field', ['option A']);
  4288. $expected = [
  4289. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4290. 'label' => ['for' => 'model-field-0'],
  4291. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4292. 'option A',
  4293. '/label'
  4294. ];
  4295. $this->assertHtml($expected, $result);
  4296. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4297. $this->assertHtml($expected, $result);
  4298. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4299. $expected = [
  4300. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4301. ['label' => ['for' => 'model-field-0']],
  4302. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4303. 'option A',
  4304. '/label',
  4305. ['label' => ['for' => 'model-field-1']],
  4306. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4307. 'option B',
  4308. '/label',
  4309. ];
  4310. $this->assertHtml($expected, $result);
  4311. $result = $this->Form->radio(
  4312. 'Employee.gender',
  4313. ['male' => 'Male', 'female' => 'Female'],
  4314. ['form' => 'my-form']
  4315. );
  4316. $expected = [
  4317. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4318. ['label' => ['for' => 'employee-gender-male']],
  4319. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4320. 'Male',
  4321. '/label',
  4322. ['label' => ['for' => 'employee-gender-female']],
  4323. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4324. 'Female',
  4325. '/label',
  4326. ];
  4327. $this->assertHtml($expected, $result);
  4328. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4329. $expected = [
  4330. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4331. ['label' => ['for' => 'model-custom-0']],
  4332. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4333. 'option A',
  4334. '/label',
  4335. ['label' => ['for' => 'model-custom-1']],
  4336. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4337. 'option B',
  4338. '/label',
  4339. ];
  4340. $this->assertHtml($expected, $result);
  4341. $result = $this->Form->radio(
  4342. 'Employee.gender',
  4343. [
  4344. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4345. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4346. ]
  4347. );
  4348. $expected = [
  4349. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4350. ['label' => ['for' => 'employee-gender-male']],
  4351. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4352. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4353. 'Male',
  4354. '/label',
  4355. ['label' => ['for' => 'employee-gender-female']],
  4356. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4357. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4358. 'Female',
  4359. '/label',
  4360. ];
  4361. $this->assertHtml($expected, $result);
  4362. }
  4363. /**
  4364. * Test radio with complex options and empty disabled data.
  4365. *
  4366. * @return void
  4367. */
  4368. public function testRadioComplexDisabled()
  4369. {
  4370. $options = [
  4371. ['value' => 'r', 'text' => 'red'],
  4372. ['value' => 'b', 'text' => 'blue'],
  4373. ];
  4374. $attrs = ['disabled' => []];
  4375. $result = $this->Form->radio('Model.field', $options, $attrs);
  4376. $expected = [
  4377. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4378. ['label' => ['for' => 'model-field-r']],
  4379. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'r', 'id' => 'model-field-r']],
  4380. 'red',
  4381. '/label',
  4382. ['label' => ['for' => 'model-field-b']],
  4383. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'b', 'id' => 'model-field-b']],
  4384. 'blue',
  4385. '/label',
  4386. ];
  4387. $this->assertHtml($expected, $result);
  4388. $attrs = ['disabled' => ['r']];
  4389. $result = $this->Form->radio('Model.field', $options, $attrs);
  4390. $this->assertContains('disabled="disabled"', $result);
  4391. }
  4392. /**
  4393. * testRadioDefaultValue method
  4394. *
  4395. * Test default value setting on radio() method.
  4396. *
  4397. * @return void
  4398. */
  4399. public function testRadioDefaultValue()
  4400. {
  4401. $Articles = $this->getTableLocator()->get('Articles');
  4402. $title = $Articles->getSchema()->getColumn('title');
  4403. $Articles->getSchema()->addColumn(
  4404. 'title',
  4405. ['default' => '1'] + $title
  4406. );
  4407. $this->Form->create($Articles->newEntity());
  4408. $result = $this->Form->radio('title', ['option A', 'option B']);
  4409. $expected = [
  4410. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4411. ['label' => ['for' => 'title-0']],
  4412. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4413. 'option A',
  4414. '/label',
  4415. ['label' => ['for' => 'title-1']],
  4416. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4417. 'option B',
  4418. '/label',
  4419. ];
  4420. $this->assertHtml($expected, $result);
  4421. }
  4422. /**
  4423. * Test setting a hiddenField value on radio buttons.
  4424. *
  4425. * @return void
  4426. */
  4427. public function testRadioHiddenFieldValue()
  4428. {
  4429. $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
  4430. $expected = [
  4431. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
  4432. 'label' => ['for' => 'title-0'],
  4433. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4434. 'option A',
  4435. '/label',
  4436. ];
  4437. $this->assertHtml($expected, $result);
  4438. }
  4439. /**
  4440. * testControlRadio method
  4441. *
  4442. * Test that input works with radio types.
  4443. *
  4444. * @return void
  4445. */
  4446. public function testControlRadio()
  4447. {
  4448. $result = $this->Form->control('test', [
  4449. 'type' => 'radio',
  4450. 'options' => ['A', 'B'],
  4451. ]);
  4452. $expected = [
  4453. ['div' => ['class' => 'input radio']],
  4454. '<label',
  4455. 'Test',
  4456. '/label',
  4457. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4458. ['label' => ['for' => 'test-0']],
  4459. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4460. 'A',
  4461. '/label',
  4462. ['label' => ['for' => 'test-1']],
  4463. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4464. 'B',
  4465. '/label',
  4466. '/div',
  4467. ];
  4468. $this->assertHtml($expected, $result);
  4469. $result = $this->Form->control('test', [
  4470. 'type' => 'radio',
  4471. 'options' => ['A', 'B'],
  4472. 'value' => '0'
  4473. ]);
  4474. $expected = [
  4475. ['div' => ['class' => 'input radio']],
  4476. '<label',
  4477. 'Test',
  4478. '/label',
  4479. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4480. ['label' => ['for' => 'test-0']],
  4481. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4482. 'A',
  4483. '/label',
  4484. ['label' => ['for' => 'test-1']],
  4485. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4486. 'B',
  4487. '/label',
  4488. '/div',
  4489. ];
  4490. $this->assertHtml($expected, $result);
  4491. $result = $this->Form->control('test', [
  4492. 'type' => 'radio',
  4493. 'options' => ['A', 'B'],
  4494. 'label' => false
  4495. ]);
  4496. $expected = [
  4497. ['div' => ['class' => 'input radio']],
  4498. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4499. ['label' => ['for' => 'test-0']],
  4500. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4501. 'A',
  4502. '/label',
  4503. ['label' => ['for' => 'test-1']],
  4504. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4505. 'B',
  4506. '/label',
  4507. '/div',
  4508. ];
  4509. $this->assertHtml($expected, $result);
  4510. }
  4511. /**
  4512. * testRadioNoLabel method
  4513. *
  4514. * Test that radio() works with label = false.
  4515. *
  4516. * @return void
  4517. */
  4518. public function testRadioNoLabel()
  4519. {
  4520. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4521. $expected = [
  4522. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4523. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4524. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4525. ];
  4526. $this->assertHtml($expected, $result);
  4527. }
  4528. /**
  4529. * testRadioControlInsideLabel method
  4530. *
  4531. * Test generating radio input inside label ala twitter bootstrap.
  4532. *
  4533. * @return void
  4534. */
  4535. public function testRadioControlInsideLabel()
  4536. {
  4537. $this->Form->setTemplates([
  4538. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4539. 'radioWrapper' => '{{label}}'
  4540. ]);
  4541. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4542. //@codingStandardsIgnoreStart
  4543. $expected = [
  4544. ['input' => [
  4545. 'type' => 'hidden',
  4546. 'name' => 'Model[field]',
  4547. 'value' => ''
  4548. ]],
  4549. ['label' => ['for' => 'model-field-0']],
  4550. ['input' => [
  4551. 'type' => 'radio',
  4552. 'name' => 'Model[field]',
  4553. 'value' => '0',
  4554. 'id' => 'model-field-0'
  4555. ]],
  4556. 'option A',
  4557. '/label',
  4558. ['label' => ['for' => 'model-field-1']],
  4559. ['input' => [
  4560. 'type' => 'radio',
  4561. 'name' => 'Model[field]',
  4562. 'value' => '1',
  4563. 'id' => 'model-field-1'
  4564. ]],
  4565. 'option B',
  4566. '/label',
  4567. ];
  4568. //@codingStandardsIgnoreEnd
  4569. $this->assertHtml($expected, $result);
  4570. }
  4571. /**
  4572. * testRadioHiddenControlDisabling method
  4573. *
  4574. * Test disabling the hidden input for radio buttons.
  4575. *
  4576. * @return void
  4577. */
  4578. public function testRadioHiddenControlDisabling()
  4579. {
  4580. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4581. $expected = [
  4582. 'label' => ['for' => 'model-1-field-0'],
  4583. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4584. 'option A',
  4585. '/label'
  4586. ];
  4587. $this->assertHtml($expected, $result);
  4588. }
  4589. /**
  4590. * testRadioOutOfRange method
  4591. *
  4592. * Test radio element set generation.
  4593. *
  4594. * @return void
  4595. */
  4596. public function testRadioOutOfRange()
  4597. {
  4598. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4599. $expected = [
  4600. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4601. 'label' => ['for' => 'model-field-v'],
  4602. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4603. 'value',
  4604. '/label'
  4605. ];
  4606. $this->assertHtml($expected, $result);
  4607. }
  4608. /**
  4609. * testSelect method
  4610. *
  4611. * Test select element generation.
  4612. *
  4613. * @return void
  4614. */
  4615. public function testSelect()
  4616. {
  4617. $result = $this->Form->select('Model.field', []);
  4618. $expected = [
  4619. 'select' => ['name' => 'Model[field]'],
  4620. '/select'
  4621. ];
  4622. $this->assertHtml($expected, $result);
  4623. $this->View->setRequest($this->View->getRequest()->withData('Model', ['field' => 'value']));
  4624. $this->Form->create();
  4625. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4626. $expected = [
  4627. 'select' => ['name' => 'Model[field]'],
  4628. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4629. 'good',
  4630. '/option',
  4631. ['option' => ['value' => 'other']],
  4632. 'bad',
  4633. '/option',
  4634. '/select'
  4635. ];
  4636. $this->assertHtml($expected, $result);
  4637. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4638. $this->assertHtml($expected, $result);
  4639. $this->View->setRequest($this->View->getRequest()->withParsedBody([]));
  4640. $this->Form->create();
  4641. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4642. $expected = [
  4643. 'select' => ['name' => 'Model[field]'],
  4644. ['option' => ['value' => 'value']],
  4645. 'good',
  4646. '/option',
  4647. ['option' => ['value' => 'other']],
  4648. 'bad',
  4649. '/option',
  4650. '/select'
  4651. ];
  4652. $this->assertHtml($expected, $result);
  4653. $options = [
  4654. ['value' => 'first', 'text' => 'First'],
  4655. ['value' => 'first', 'text' => 'Another First'],
  4656. ];
  4657. $result = $this->Form->select(
  4658. 'Model.field',
  4659. $options,
  4660. ['escape' => false, 'empty' => false]
  4661. );
  4662. $expected = [
  4663. 'select' => ['name' => 'Model[field]'],
  4664. ['option' => ['value' => 'first']],
  4665. 'First',
  4666. '/option',
  4667. ['option' => ['value' => 'first']],
  4668. 'Another First',
  4669. '/option',
  4670. '/select'
  4671. ];
  4672. $this->assertHtml($expected, $result);
  4673. $this->View->setRequest($this->View->getRequest()->withParsedBody(['Model' => ['contact_id' => 228]]));
  4674. $this->Form->create();
  4675. $result = $this->Form->select(
  4676. 'Model.contact_id',
  4677. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4678. ['escape' => false, 'empty' => 'pick something']
  4679. );
  4680. $expected = [
  4681. 'select' => ['name' => 'Model[contact_id]'],
  4682. ['option' => ['value' => '']], 'pick something', '/option',
  4683. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4684. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4685. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4686. '/select'
  4687. ];
  4688. $this->assertHtml($expected, $result);
  4689. $this->View->setRequest($this->View->getRequest()->withData('Model.field', 0));
  4690. $this->Form->create();
  4691. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4692. $expected = [
  4693. 'select' => ['name' => 'Model[field]'],
  4694. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4695. ['option' => ['value' => '1']], 'Yes', '/option',
  4696. '/select'
  4697. ];
  4698. $this->assertHtml($expected, $result);
  4699. }
  4700. /**
  4701. * testSelectEscapeHtml method
  4702. *
  4703. * Test that select() escapes HTML.
  4704. *
  4705. * @return void
  4706. */
  4707. public function testSelectEscapeHtml()
  4708. {
  4709. $result = $this->Form->select(
  4710. 'Model.field',
  4711. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4712. ['empty' => false]
  4713. );
  4714. $expected = [
  4715. 'select' => ['name' => 'Model[field]'],
  4716. ['option' => ['value' => 'first']],
  4717. 'first &quot;html&quot; &lt;chars&gt;',
  4718. '/option',
  4719. ['option' => ['value' => 'second']],
  4720. 'value',
  4721. '/option',
  4722. '/select'
  4723. ];
  4724. $this->assertHtml($expected, $result);
  4725. $result = $this->Form->select(
  4726. 'Model.field',
  4727. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4728. ['escape' => false, 'empty' => false]
  4729. );
  4730. $expected = [
  4731. 'select' => ['name' => 'Model[field]'],
  4732. ['option' => ['value' => 'first']],
  4733. 'first "html" <chars>',
  4734. '/option',
  4735. ['option' => ['value' => 'second']],
  4736. 'value',
  4737. '/option',
  4738. '/select'
  4739. ];
  4740. $this->assertHtml($expected, $result);
  4741. }
  4742. /**
  4743. * testSelectRequired method
  4744. *
  4745. * Test select() with required and disabled attributes.
  4746. *
  4747. * @return void
  4748. */
  4749. public function testSelectRequired()
  4750. {
  4751. $this->article['required'] = [
  4752. 'user_id' => true
  4753. ];
  4754. $this->Form->create($this->article);
  4755. $result = $this->Form->select('user_id', ['option A']);
  4756. $expected = [
  4757. 'select' => [
  4758. 'name' => 'user_id',
  4759. 'required' => 'required'
  4760. ],
  4761. ['option' => ['value' => '0']], 'option A', '/option',
  4762. '/select'
  4763. ];
  4764. $this->assertHtml($expected, $result);
  4765. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4766. $expected = [
  4767. 'select' => [
  4768. 'name' => 'user_id',
  4769. 'disabled' => 'disabled'
  4770. ],
  4771. ['option' => ['value' => '0']], 'option A', '/option',
  4772. '/select'
  4773. ];
  4774. $this->assertHtml($expected, $result);
  4775. }
  4776. /**
  4777. * testNestedSelect method
  4778. *
  4779. * Test select element generation with optgroups.
  4780. *
  4781. * @return void
  4782. */
  4783. public function testNestedSelect()
  4784. {
  4785. $result = $this->Form->select(
  4786. 'Model.field',
  4787. [1 => 'One', 2 => 'Two', 'Three' => [
  4788. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4789. ]],
  4790. ['empty' => false]
  4791. );
  4792. $expected = [
  4793. 'select' => ['name' => 'Model[field]'],
  4794. ['option' => ['value' => 1]],
  4795. 'One',
  4796. '/option',
  4797. ['option' => ['value' => 2]],
  4798. 'Two',
  4799. '/option',
  4800. ['optgroup' => ['label' => 'Three']],
  4801. ['option' => ['value' => 3]],
  4802. 'Three',
  4803. '/option',
  4804. ['option' => ['value' => 4]],
  4805. 'Four',
  4806. '/option',
  4807. ['option' => ['value' => 5]],
  4808. 'Five',
  4809. '/option',
  4810. '/optgroup',
  4811. '/select'
  4812. ];
  4813. $this->assertHtml($expected, $result);
  4814. }
  4815. /**
  4816. * testSelectMultiple method
  4817. *
  4818. * Test generation of multiple select elements.
  4819. *
  4820. * @return void
  4821. */
  4822. public function testSelectMultiple()
  4823. {
  4824. $options = ['first', 'second', 'third'];
  4825. $result = $this->Form->select(
  4826. 'Model.multi_field',
  4827. $options,
  4828. ['form' => 'my-form', 'multiple' => true]
  4829. );
  4830. $expected = [
  4831. 'input' => [
  4832. 'type' => 'hidden',
  4833. 'name' => 'Model[multi_field]',
  4834. 'value' => '',
  4835. 'form' => 'my-form',
  4836. ],
  4837. 'select' => [
  4838. 'name' => 'Model[multi_field][]',
  4839. 'multiple' => 'multiple',
  4840. 'form' => 'my-form',
  4841. ],
  4842. ['option' => ['value' => '0']],
  4843. 'first',
  4844. '/option',
  4845. ['option' => ['value' => '1']],
  4846. 'second',
  4847. '/option',
  4848. ['option' => ['value' => '2']],
  4849. 'third',
  4850. '/option',
  4851. '/select'
  4852. ];
  4853. $this->assertHtml($expected, $result);
  4854. $result = $this->Form->select(
  4855. 'Model.multi_field',
  4856. $options,
  4857. ['multiple' => 'multiple', 'form' => 'my-form']
  4858. );
  4859. $this->assertHtml($expected, $result);
  4860. $result = $this->Form->select(
  4861. 'Model.multi_field',
  4862. $options,
  4863. ['form' => 'my-form', 'multiple' => false]
  4864. );
  4865. $this->assertNotContains('multiple', $result);
  4866. }
  4867. /**
  4868. * testCheckboxZeroValue method
  4869. *
  4870. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4871. *
  4872. * @return void
  4873. */
  4874. public function testCheckboxZeroValue()
  4875. {
  4876. $result = $this->Form->control('User.get_spam', [
  4877. 'type' => 'checkbox',
  4878. 'value' => '0',
  4879. 'hiddenField' => '1',
  4880. ]);
  4881. $expected = [
  4882. 'div' => ['class' => 'input checkbox'],
  4883. 'label' => ['for' => 'user-get-spam'],
  4884. ['input' => [
  4885. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4886. 'value' => '1'
  4887. ]],
  4888. ['input' => [
  4889. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4890. 'value' => '0', 'id' => 'user-get-spam'
  4891. ]],
  4892. 'Get Spam',
  4893. '/label',
  4894. '/div'
  4895. ];
  4896. $this->assertHtml($expected, $result);
  4897. }
  4898. /**
  4899. * testHabtmSelectBox method
  4900. *
  4901. * Test generation of habtm select boxes.
  4902. *
  4903. * @return void
  4904. */
  4905. public function testHabtmSelectBox()
  4906. {
  4907. $this->loadFixtures('Articles');
  4908. $options = [
  4909. 1 => 'blue',
  4910. 2 => 'red',
  4911. 3 => 'green'
  4912. ];
  4913. $tags = [
  4914. new Entity(['id' => 1, 'name' => 'blue']),
  4915. new Entity(['id' => 3, 'name' => 'green'])
  4916. ];
  4917. $article = new Article(['tags' => $tags]);
  4918. $this->Form->create($article);
  4919. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4920. $expected = [
  4921. 'div' => ['class' => 'input select'],
  4922. 'label' => ['for' => 'tags-ids'],
  4923. 'Tags',
  4924. '/label',
  4925. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4926. 'select' => [
  4927. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4928. 'multiple' => 'multiple'
  4929. ],
  4930. ['option' => ['value' => '1', 'selected' => 'selected']],
  4931. 'blue',
  4932. '/option',
  4933. ['option' => ['value' => '2']],
  4934. 'red',
  4935. '/option',
  4936. ['option' => ['value' => '3', 'selected' => 'selected']],
  4937. 'green',
  4938. '/option',
  4939. '/select',
  4940. '/div'
  4941. ];
  4942. $this->assertHtml($expected, $result);
  4943. // make sure only 50 is selected, and not 50f5c0cf
  4944. $options = [
  4945. '1' => 'blue',
  4946. '50f5c0cf' => 'red',
  4947. '50' => 'green'
  4948. ];
  4949. $tags = [
  4950. new Entity(['id' => 1, 'name' => 'blue']),
  4951. new Entity(['id' => 50, 'name' => 'green'])
  4952. ];
  4953. $article = new Article(['tags' => $tags]);
  4954. $this->Form->create($article);
  4955. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4956. $expected = [
  4957. 'div' => ['class' => 'input select'],
  4958. 'label' => ['for' => 'tags-ids'],
  4959. 'Tags',
  4960. '/label',
  4961. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4962. 'select' => [
  4963. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4964. 'multiple' => 'multiple'
  4965. ],
  4966. ['option' => ['value' => '1', 'selected' => 'selected']],
  4967. 'blue',
  4968. '/option',
  4969. ['option' => ['value' => '50f5c0cf']],
  4970. 'red',
  4971. '/option',
  4972. ['option' => ['value' => '50', 'selected' => 'selected']],
  4973. 'green',
  4974. '/option',
  4975. '/select',
  4976. '/div'
  4977. ];
  4978. $this->assertHtml($expected, $result);
  4979. $spacecraft = [
  4980. 1 => 'Orion',
  4981. 2 => 'Helios'
  4982. ];
  4983. $this->View->viewVars['spacecraft'] = $spacecraft;
  4984. $this->Form->create();
  4985. $result = $this->Form->control('spacecraft._ids');
  4986. $expected = [
  4987. 'div' => ['class' => 'input select'],
  4988. 'label' => ['for' => 'spacecraft-ids'],
  4989. 'Spacecraft',
  4990. '/label',
  4991. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4992. 'select' => [
  4993. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4994. 'multiple' => 'multiple'
  4995. ],
  4996. ['option' => ['value' => '1']],
  4997. 'Orion',
  4998. '/option',
  4999. ['option' => ['value' => '2']],
  5000. 'Helios',
  5001. '/option',
  5002. '/select',
  5003. '/div'
  5004. ];
  5005. $this->assertHtml($expected, $result);
  5006. }
  5007. /**
  5008. * testErrorsForBelongsToManySelect method
  5009. *
  5010. * Tests that errors for belongsToMany select fields are being
  5011. * picked up properly.
  5012. *
  5013. * @return void
  5014. */
  5015. public function testErrorsForBelongsToManySelect()
  5016. {
  5017. $spacecraft = [
  5018. 1 => 'Orion',
  5019. 2 => 'Helios'
  5020. ];
  5021. $this->View->viewVars['spacecraft'] = $spacecraft;
  5022. $article = new Article();
  5023. $article->setError('spacecraft', ['Invalid']);
  5024. $this->Form->create($article);
  5025. $result = $this->Form->control('spacecraft._ids');
  5026. $expected = [
  5027. ['div' => ['class' => 'input select error']],
  5028. 'label' => ['for' => 'spacecraft-ids'],
  5029. 'Spacecraft',
  5030. '/label',
  5031. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  5032. 'select' => [
  5033. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  5034. 'multiple' => 'multiple'
  5035. ],
  5036. ['option' => ['value' => '1']],
  5037. 'Orion',
  5038. '/option',
  5039. ['option' => ['value' => '2']],
  5040. 'Helios',
  5041. '/option',
  5042. '/select',
  5043. ['div' => ['class' => 'error-message']],
  5044. 'Invalid',
  5045. '/div',
  5046. '/div'
  5047. ];
  5048. $this->assertHtml($expected, $result);
  5049. }
  5050. /**
  5051. * testSelectMultipleCheckboxes method
  5052. *
  5053. * Test generation of multi select elements in checkbox format.
  5054. *
  5055. * @return void
  5056. */
  5057. public function testSelectMultipleCheckboxes()
  5058. {
  5059. $result = $this->Form->select(
  5060. 'Model.multi_field',
  5061. ['first', 'second', 'third'],
  5062. ['multiple' => 'checkbox']
  5063. );
  5064. $expected = [
  5065. 'input' => [
  5066. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5067. ],
  5068. ['div' => ['class' => 'checkbox']],
  5069. ['label' => ['for' => 'model-multi-field-0']],
  5070. ['input' => [
  5071. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5072. 'value' => '0', 'id' => 'model-multi-field-0'
  5073. ]],
  5074. 'first',
  5075. '/label',
  5076. '/div',
  5077. ['div' => ['class' => 'checkbox']],
  5078. ['label' => ['for' => 'model-multi-field-1']],
  5079. ['input' => [
  5080. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5081. 'value' => '1', 'id' => 'model-multi-field-1'
  5082. ]],
  5083. 'second',
  5084. '/label',
  5085. '/div',
  5086. ['div' => ['class' => 'checkbox']],
  5087. ['label' => ['for' => 'model-multi-field-2']],
  5088. ['input' => [
  5089. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5090. 'value' => '2', 'id' => 'model-multi-field-2'
  5091. ]],
  5092. 'third',
  5093. '/label',
  5094. '/div'
  5095. ];
  5096. $this->assertHtml($expected, $result);
  5097. $result = $this->Form->select(
  5098. 'Model.multi_field',
  5099. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  5100. ['multiple' => 'checkbox']
  5101. );
  5102. $expected = [
  5103. 'input' => [
  5104. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5105. ],
  5106. ['div' => ['class' => 'checkbox']],
  5107. ['label' => ['for' => 'model-multi-field-a+']],
  5108. ['input' => [
  5109. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5110. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  5111. ]],
  5112. 'first',
  5113. '/label',
  5114. '/div',
  5115. ['div' => ['class' => 'checkbox']],
  5116. ['label' => ['for' => 'model-multi-field-a++']],
  5117. ['input' => [
  5118. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5119. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  5120. ]],
  5121. 'second',
  5122. '/label',
  5123. '/div',
  5124. ['div' => ['class' => 'checkbox']],
  5125. ['label' => ['for' => 'model-multi-field-a+++']],
  5126. ['input' => [
  5127. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5128. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  5129. ]],
  5130. 'third',
  5131. '/label',
  5132. '/div'
  5133. ];
  5134. $this->assertHtml($expected, $result);
  5135. $result = $this->Form->select(
  5136. 'Model.multi_field',
  5137. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  5138. ['multiple' => 'checkbox']
  5139. );
  5140. $expected = [
  5141. 'input' => [
  5142. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  5143. ],
  5144. ['div' => ['class' => 'checkbox']],
  5145. ['label' => ['for' => 'model-multi-field-a-b']],
  5146. ['input' => [
  5147. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5148. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  5149. ]],
  5150. 'first',
  5151. '/label',
  5152. '/div',
  5153. ['div' => ['class' => 'checkbox']],
  5154. ['label' => ['for' => 'model-multi-field-a-b1']],
  5155. ['input' => [
  5156. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5157. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  5158. ]],
  5159. 'second',
  5160. '/label',
  5161. '/div',
  5162. ['div' => ['class' => 'checkbox']],
  5163. ['label' => ['for' => 'model-multi-field-a-b2']],
  5164. ['input' => [
  5165. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5166. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  5167. ]],
  5168. 'third',
  5169. '/label',
  5170. '/div'
  5171. ];
  5172. $this->assertHtml($expected, $result);
  5173. }
  5174. /**
  5175. * testSelectMultipleCheckboxRequestData method
  5176. *
  5177. * Ensure that multiCheckbox reads from the request data.
  5178. *
  5179. * @return void
  5180. */
  5181. public function testSelectMultipleCheckboxRequestData()
  5182. {
  5183. $this->View->setRequest($this->View->getRequest()->withData('Model', ['tags' => [1]]));
  5184. $result = $this->Form->select(
  5185. 'Model.tags',
  5186. ['1' => 'first', 'Array' => 'Array'],
  5187. ['multiple' => 'checkbox']
  5188. );
  5189. $expected = [
  5190. 'input' => [
  5191. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  5192. ],
  5193. ['div' => ['class' => 'checkbox']],
  5194. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  5195. ['input' => [
  5196. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5197. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  5198. ]],
  5199. 'first',
  5200. '/label',
  5201. '/div',
  5202. ['div' => ['class' => 'checkbox']],
  5203. ['label' => ['for' => 'model-tags-array']],
  5204. ['input' => [
  5205. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  5206. 'value' => 'Array', 'id' => 'model-tags-array'
  5207. ]],
  5208. 'Array',
  5209. '/label',
  5210. '/div'
  5211. ];
  5212. $this->assertHtml($expected, $result);
  5213. }
  5214. /**
  5215. * testSelectMultipleCheckboxSecurity method
  5216. *
  5217. * Checks the security hash array generated for multiple-input checkbox elements.
  5218. *
  5219. * @return void
  5220. */
  5221. public function testSelectMultipleCheckboxSecurity()
  5222. {
  5223. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testKey'));
  5224. $this->assertEquals([], $this->Form->fields);
  5225. $this->Form->select(
  5226. 'Model.multi_field',
  5227. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5228. ['multiple' => 'checkbox']
  5229. );
  5230. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5231. $result = $this->Form->secure($this->Form->fields);
  5232. $key = '8af36fb34e6f2ef8ba0eb473bb4365ec232f3fe5%3A';
  5233. $this->assertContains('"' . $key . '"', $result);
  5234. }
  5235. /**
  5236. * testSelectMultipleSecureWithNoOptions method
  5237. *
  5238. * Multiple select elements should always be secured as they always participate
  5239. * in the POST data.
  5240. *
  5241. * @return void
  5242. */
  5243. public function testSelectMultipleSecureWithNoOptions()
  5244. {
  5245. $this->assertEquals([], $this->Form->fields);
  5246. $this->Form->select(
  5247. 'Model.select',
  5248. [],
  5249. ['multiple' => true]
  5250. );
  5251. $this->assertEquals(['Model.select'], $this->Form->fields);
  5252. }
  5253. /**
  5254. * testSelectNoSecureWithNoOptions method
  5255. *
  5256. * When a select box has no options it should not be added to the fields list
  5257. * as it always fail post validation.
  5258. *
  5259. * @return void
  5260. */
  5261. public function testSelectNoSecureWithNoOptions()
  5262. {
  5263. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'testkey'));
  5264. $this->assertEquals([], $this->Form->fields);
  5265. $this->Form->select(
  5266. 'Model.select',
  5267. []
  5268. );
  5269. $this->assertEquals([], $this->Form->fields);
  5270. $this->Form->select(
  5271. 'Model.user_id',
  5272. [],
  5273. ['empty' => true]
  5274. );
  5275. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5276. }
  5277. /**
  5278. * testControlMultipleCheckboxes method
  5279. *
  5280. * Test control() resulting in multi select elements being generated.
  5281. *
  5282. * @return void
  5283. */
  5284. public function testControlMultipleCheckboxes()
  5285. {
  5286. $result = $this->Form->control('Model.multi_field', [
  5287. 'options' => ['first', 'second', 'third'],
  5288. 'multiple' => 'checkbox'
  5289. ]);
  5290. $expected = [
  5291. ['div' => ['class' => 'input select']],
  5292. ['label' => ['for' => 'model-multi-field']],
  5293. 'Multi Field',
  5294. '/label',
  5295. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5296. ['div' => ['class' => 'checkbox']],
  5297. ['label' => ['for' => 'model-multi-field-0']],
  5298. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5299. 'first',
  5300. '/label',
  5301. '/div',
  5302. ['div' => ['class' => 'checkbox']],
  5303. ['label' => ['for' => 'model-multi-field-1']],
  5304. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5305. 'second',
  5306. '/label',
  5307. '/div',
  5308. ['div' => ['class' => 'checkbox']],
  5309. ['label' => ['for' => 'model-multi-field-2']],
  5310. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5311. 'third',
  5312. '/label',
  5313. '/div',
  5314. '/div'
  5315. ];
  5316. $this->assertHtml($expected, $result);
  5317. $result = $this->Form->control('Model.multi_field', [
  5318. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5319. 'multiple' => 'checkbox'
  5320. ]);
  5321. $expected = [
  5322. ['div' => ['class' => 'input select']],
  5323. ['label' => ['for' => 'model-multi-field']],
  5324. 'Multi Field',
  5325. '/label',
  5326. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5327. ['div' => ['class' => 'checkbox']],
  5328. ['label' => ['for' => 'model-multi-field-a']],
  5329. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5330. 'first',
  5331. '/label',
  5332. '/div',
  5333. ['div' => ['class' => 'checkbox']],
  5334. ['label' => ['for' => 'model-multi-field-b']],
  5335. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5336. 'second',
  5337. '/label',
  5338. '/div',
  5339. ['div' => ['class' => 'checkbox']],
  5340. ['label' => ['for' => 'model-multi-field-c']],
  5341. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5342. 'third',
  5343. '/label',
  5344. '/div',
  5345. '/div'
  5346. ];
  5347. $this->assertHtml($expected, $result);
  5348. }
  5349. /**
  5350. * testSelectHiddenFieldOmission method
  5351. *
  5352. * Test that select() with 'hiddenField' => false omits the hidden field.
  5353. *
  5354. * @return void
  5355. */
  5356. public function testSelectHiddenFieldOmission()
  5357. {
  5358. $result = $this->Form->select(
  5359. 'Model.multi_field',
  5360. ['first', 'second'],
  5361. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5362. );
  5363. $this->assertNotContains('type="hidden"', $result);
  5364. }
  5365. /**
  5366. * testSelectCheckboxMultipleOverrideName method
  5367. *
  5368. * Test that select() with multiple = checkbox works with overriding name attribute.
  5369. *
  5370. * @return void
  5371. */
  5372. public function testSelectCheckboxMultipleOverrideName()
  5373. {
  5374. $result = $this->Form->select('category', ['1', '2'], [
  5375. 'multiple' => 'checkbox',
  5376. 'name' => 'fish',
  5377. ]);
  5378. $expected = [
  5379. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5380. ['div' => ['class' => 'checkbox']],
  5381. ['label' => ['for' => 'fish-0']],
  5382. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5383. '1',
  5384. '/label',
  5385. '/div',
  5386. ['div' => ['class' => 'checkbox']],
  5387. ['label' => ['for' => 'fish-1']],
  5388. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5389. '2',
  5390. '/label',
  5391. '/div'
  5392. ];
  5393. $this->assertHtml($expected, $result);
  5394. $result = $this->Form->multiCheckbox(
  5395. 'category',
  5396. new Collection(['1', '2']),
  5397. ['name' => 'fish']
  5398. );
  5399. $this->assertHtml($expected, $result);
  5400. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5401. 'name' => 'fish',
  5402. ]);
  5403. $this->assertHtml($expected, $result);
  5404. }
  5405. /**
  5406. * testControlMultiCheckbox method
  5407. *
  5408. * Test that control() works with multicheckbox.
  5409. *
  5410. * @return void
  5411. */
  5412. public function testControlMultiCheckbox()
  5413. {
  5414. $result = $this->Form->control('category', [
  5415. 'type' => 'multicheckbox',
  5416. 'options' => ['1', '2'],
  5417. ]);
  5418. $expected = [
  5419. ['div' => ['class' => 'input multicheckbox']],
  5420. '<label',
  5421. 'Category',
  5422. '/label',
  5423. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5424. ['div' => ['class' => 'checkbox']],
  5425. ['label' => ['for' => 'category-0']],
  5426. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5427. '1',
  5428. '/label',
  5429. '/div',
  5430. ['div' => ['class' => 'checkbox']],
  5431. ['label' => ['for' => 'category-1']],
  5432. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5433. '2',
  5434. '/label',
  5435. '/div',
  5436. '/div',
  5437. ];
  5438. $this->assertHtml($expected, $result);
  5439. }
  5440. /**
  5441. * testCheckbox method
  5442. *
  5443. * Test generation of checkboxes.
  5444. *
  5445. * @return void
  5446. */
  5447. public function testCheckbox()
  5448. {
  5449. $result = $this->Form->checkbox('Model.field');
  5450. $expected = [
  5451. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5452. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5453. ];
  5454. $this->assertHtml($expected, $result);
  5455. $result = $this->Form->checkbox('Model.field', [
  5456. 'id' => 'theID',
  5457. 'value' => 'myvalue',
  5458. 'form' => 'my-form',
  5459. ]);
  5460. $expected = [
  5461. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5462. ['input' => [
  5463. 'type' => 'checkbox', 'name' => 'Model[field]',
  5464. 'value' => 'myvalue', 'id' => 'theID',
  5465. 'form' => 'my-form',
  5466. ]]
  5467. ];
  5468. $this->assertHtml($expected, $result);
  5469. }
  5470. /**
  5471. * testCheckboxDefaultValue method
  5472. *
  5473. * Test default value setting on checkbox() method.
  5474. *
  5475. * @return void
  5476. */
  5477. public function testCheckboxDefaultValue()
  5478. {
  5479. $this->View->setRequest($this->View->getRequest()->withData('Model.field', false));
  5480. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5481. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5482. $this->assertHtml($expected, $result);
  5483. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5484. $this->Form->create();
  5485. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5486. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5487. $this->assertHtml($expected, $result);
  5488. $this->View->setRequest($this->View->getRequest()->withData('Model.field', true));
  5489. $this->Form->create();
  5490. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5491. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5492. $this->assertHtml($expected, $result);
  5493. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  5494. $this->Form->create();
  5495. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5496. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5497. $this->assertHtml($expected, $result);
  5498. $Articles = $this->getTableLocator()->get('Articles');
  5499. $Articles->getSchema()->addColumn(
  5500. 'published',
  5501. ['type' => 'boolean', 'null' => false, 'default' => true]
  5502. );
  5503. $this->Form->create($Articles->newEntity());
  5504. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5505. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5506. $this->assertHtml($expected, $result);
  5507. }
  5508. /**
  5509. * testCheckboxCheckedAndError method
  5510. *
  5511. * Test checkbox being checked or having errors.
  5512. *
  5513. * @return void
  5514. */
  5515. public function testCheckboxCheckedAndError()
  5516. {
  5517. $this->article['errors'] = [
  5518. 'published' => true
  5519. ];
  5520. $this->View->setRequest($this->View->getRequest()->withData('published', 'myvalue'));
  5521. $this->Form->create($this->article);
  5522. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5523. $expected = [
  5524. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5525. ['input' => [
  5526. 'type' => 'checkbox',
  5527. 'name' => 'published',
  5528. 'value' => 'myvalue',
  5529. 'id' => 'theID',
  5530. 'checked' => 'checked',
  5531. 'class' => 'form-error'
  5532. ]]
  5533. ];
  5534. $this->assertHtml($expected, $result);
  5535. $this->View->setRequest($this->View->getRequest()->withData('published', ''));
  5536. $this->Form->create($this->article);
  5537. $result = $this->Form->checkbox('published');
  5538. $expected = [
  5539. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5540. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5541. ];
  5542. $this->assertHtml($expected, $result);
  5543. }
  5544. /**
  5545. * testCheckboxCustomNameAttribute method
  5546. *
  5547. * Test checkbox() with a custom name attribute.
  5548. *
  5549. * @return void
  5550. */
  5551. public function testCheckboxCustomNameAttribute()
  5552. {
  5553. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5554. $expected = [
  5555. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5556. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5557. ];
  5558. $this->assertHtml($expected, $result);
  5559. }
  5560. /**
  5561. * testCheckboxHiddenField method
  5562. *
  5563. * Test that the hidden input for checkboxes can be omitted or set to a
  5564. * specific value.
  5565. *
  5566. * @return void
  5567. */
  5568. public function testCheckboxHiddenField()
  5569. {
  5570. $result = $this->Form->checkbox('UserForm.something', [
  5571. 'hiddenField' => false
  5572. ]);
  5573. $expected = [
  5574. 'input' => [
  5575. 'type' => 'checkbox',
  5576. 'name' => 'UserForm[something]',
  5577. 'value' => '1'
  5578. ],
  5579. ];
  5580. $this->assertHtml($expected, $result);
  5581. $result = $this->Form->checkbox('UserForm.something', [
  5582. 'value' => 'Y',
  5583. 'hiddenField' => 'N',
  5584. ]);
  5585. $expected = [
  5586. ['input' => [
  5587. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5588. 'value' => 'N'
  5589. ]],
  5590. ['input' => [
  5591. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5592. 'value' => 'Y'
  5593. ]],
  5594. ];
  5595. $this->assertHtml($expected, $result);
  5596. }
  5597. /**
  5598. * testTime method
  5599. *
  5600. * Test the time type.
  5601. *
  5602. * @return void
  5603. */
  5604. public function testTime()
  5605. {
  5606. $result = $this->Form->time('start_time', [
  5607. 'timeFormat' => 12,
  5608. 'interval' => 5,
  5609. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5610. ]);
  5611. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5612. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5613. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5614. $this->assertNotContains('year', $result);
  5615. $this->assertNotContains('month', $result);
  5616. $this->assertNotContains('day', $result);
  5617. $result = $this->Form->time('start_time', [
  5618. 'timeFormat' => 12,
  5619. 'interval' => 5,
  5620. 'value' => '2014-03-08 16:30:00'
  5621. ]);
  5622. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5623. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5624. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5625. $this->assertNotContains('year', $result);
  5626. $this->assertNotContains('month', $result);
  5627. $this->assertNotContains('day', $result);
  5628. }
  5629. /**
  5630. * testTimeFormat24NoMeridian method
  5631. *
  5632. * Ensure that timeFormat=24 has no merdian.
  5633. *
  5634. * @return void
  5635. */
  5636. public function testTimeFormat24NoMeridian()
  5637. {
  5638. $result = $this->Form->time('start_time', [
  5639. 'timeFormat' => 24,
  5640. 'interval' => 5,
  5641. 'value' => '2014-03-08 16:30:00'
  5642. ]);
  5643. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5644. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5645. $this->assertNotContains('meridian', $result);
  5646. $this->assertNotContains('pm', $result);
  5647. $this->assertNotContains('year', $result);
  5648. $this->assertNotContains('month', $result);
  5649. $this->assertNotContains('day', $result);
  5650. }
  5651. /**
  5652. * testDate method
  5653. *
  5654. * Test the date type.
  5655. *
  5656. * @return void
  5657. */
  5658. public function testDate()
  5659. {
  5660. $result = $this->Form->date('start_day', [
  5661. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5662. ]);
  5663. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5664. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5665. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5666. $this->assertNotContains('hour', $result);
  5667. $this->assertNotContains('minute', $result);
  5668. $this->assertNotContains('second', $result);
  5669. $this->assertNotContains('meridian', $result);
  5670. }
  5671. /**
  5672. * testDateTime method
  5673. *
  5674. * Test generation of date/time select elements.
  5675. *
  5676. * @return void
  5677. */
  5678. public function testDateTime()
  5679. {
  5680. extract($this->dateRegex);
  5681. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5682. $now = strtotime('now');
  5683. $expected = [
  5684. ['select' => ['name' => 'Contact[date][year]']],
  5685. ['option' => ['value' => '']],
  5686. '/option',
  5687. $yearsRegex,
  5688. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5689. date('Y', $now),
  5690. '/option',
  5691. '*/select',
  5692. ['select' => ['name' => 'Contact[date][month]']],
  5693. ['option' => ['value' => '']],
  5694. '/option',
  5695. $monthsRegex,
  5696. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5697. date('F', $now),
  5698. '/option',
  5699. '*/select',
  5700. ['select' => ['name' => 'Contact[date][day]']],
  5701. ['option' => ['value' => '']],
  5702. '/option',
  5703. $daysRegex,
  5704. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5705. date('j', $now),
  5706. '/option',
  5707. '*/select',
  5708. ['select' => ['name' => 'Contact[date][hour]']],
  5709. ['option' => ['value' => '']],
  5710. '/option',
  5711. $hoursRegex,
  5712. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5713. date('G', $now),
  5714. '/option',
  5715. '*/select',
  5716. ['select' => ['name' => 'Contact[date][minute]']],
  5717. ['option' => ['value' => '']],
  5718. '/option',
  5719. $minutesRegex,
  5720. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5721. date('i', $now),
  5722. '/option',
  5723. '*/select',
  5724. ];
  5725. $this->assertHtml($expected, $result);
  5726. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5727. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5728. $now = strtotime('now');
  5729. $expected = [
  5730. ['select' => ['name' => 'Contact[date][year]']],
  5731. $yearsRegex,
  5732. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5733. date('Y', $now),
  5734. '/option',
  5735. '*/select',
  5736. ['select' => ['name' => 'Contact[date][month]']],
  5737. $monthsRegex,
  5738. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5739. date('F', $now),
  5740. '/option',
  5741. '*/select',
  5742. ['select' => ['name' => 'Contact[date][day]']],
  5743. $daysRegex,
  5744. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5745. date('j', $now),
  5746. '/option',
  5747. '*/select',
  5748. ['select' => ['name' => 'Contact[date][hour]']],
  5749. $hoursRegex,
  5750. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5751. date('G', $now),
  5752. '/option',
  5753. '*/select',
  5754. ['select' => ['name' => 'Contact[date][minute]']],
  5755. $minutesRegex,
  5756. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5757. date('i', $now),
  5758. '/option',
  5759. '*/select',
  5760. ];
  5761. $this->assertHtml($expected, $result);
  5762. }
  5763. /**
  5764. * testDateTimeSecured method
  5765. *
  5766. * Test that datetime fields are added to protected fields list.
  5767. *
  5768. * @return void
  5769. */
  5770. public function testDateTimeSecured()
  5771. {
  5772. $this->View->setRequest(
  5773. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5774. );
  5775. $this->Form->dateTime('Contact.date');
  5776. $expected = [
  5777. 'Contact.date.year',
  5778. 'Contact.date.month',
  5779. 'Contact.date.day',
  5780. 'Contact.date.hour',
  5781. 'Contact.date.minute',
  5782. ];
  5783. $this->assertEquals($expected, $this->Form->fields);
  5784. $this->Form->fields = [];
  5785. $this->Form->date('Contact.published');
  5786. $expected = [
  5787. 'Contact.published.year',
  5788. 'Contact.published.month',
  5789. 'Contact.published.day',
  5790. ];
  5791. $this->assertEquals($expected, $this->Form->fields);
  5792. }
  5793. /**
  5794. * testDateTimeSecuredDisabled method
  5795. *
  5796. * Test that datetime fields are added to protected fields list.
  5797. *
  5798. * @return void
  5799. */
  5800. public function testDateTimeSecuredDisabled()
  5801. {
  5802. $this->View->setRequest(
  5803. $this->View->getRequest()->withParam('_Token', ['unlockedFields' => []])
  5804. );
  5805. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5806. $expected = [];
  5807. $this->assertEquals($expected, $this->Form->fields);
  5808. $this->Form->fields = [];
  5809. $this->Form->date('Contact.published', ['secure' => false]);
  5810. $expected = [];
  5811. $this->assertEquals($expected, $this->Form->fields);
  5812. }
  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. * Presence check for array form empty options
  5863. *
  5864. * @return void
  5865. */
  5866. public function testDateTimeEmptyAsArrayPresence()
  5867. {
  5868. $result = $this->Form->dateTime('Contact.date', [
  5869. 'empty' => [
  5870. 'day' => 'DAY',
  5871. 'month' => 'MONTH',
  5872. 'year' => 'YEAR',
  5873. 'hour' => 'HOUR',
  5874. 'minute' => 'MINUTE',
  5875. 'meridian' => false
  5876. ],
  5877. 'default' => true
  5878. ]);
  5879. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5880. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5881. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5882. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5883. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5884. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5885. }
  5886. /**
  5887. * Test datetime with array empty value, ensuring
  5888. * empty options aren't duplicated.
  5889. *
  5890. * @return void
  5891. */
  5892. public function testDatetimeEmptyArrayForm()
  5893. {
  5894. extract($this->dateRegex);
  5895. $result = $this->Form->dateTime('Contact.date', [
  5896. 'minYear' => '2017',
  5897. 'maxYear' => '2019',
  5898. 'empty' => [
  5899. 'year' => 'pick year',
  5900. 'month' => 'pick month',
  5901. ],
  5902. 'hour' => false,
  5903. 'minute' => false,
  5904. 'second' => false,
  5905. 'meridian' => false,
  5906. ]);
  5907. $expected = [
  5908. ['select' => ['name' => 'Contact[date][year]']],
  5909. ['option' => ['value' => '', 'selected' => 'selected']], 'pick year', '/option',
  5910. '*/select',
  5911. ['select' => ['name' => 'Contact[date][month]']],
  5912. ['option' => ['value' => '', 'selected' => 'selected']], 'pick month', '/option',
  5913. $monthsRegex,
  5914. '*/select',
  5915. ['select' => ['name' => 'Contact[date][day]']],
  5916. $daysRegex,
  5917. '*/select',
  5918. ];
  5919. $this->assertHtml($expected, $result);
  5920. }
  5921. /**
  5922. * testDatetimeMinuteInterval method
  5923. *
  5924. * Test datetime with interval option.
  5925. *
  5926. * @return void
  5927. */
  5928. public function testDatetimeMinuteInterval()
  5929. {
  5930. extract($this->dateRegex);
  5931. $result = $this->Form->dateTime('Contact.date', [
  5932. 'interval' => 5,
  5933. 'value' => ''
  5934. ]);
  5935. $expected = [
  5936. ['select' => ['name' => 'Contact[date][year]']],
  5937. $yearsRegex,
  5938. ['option' => ['selected' => 'selected', 'value' => '']],
  5939. '/option',
  5940. '*/select',
  5941. ['select' => ['name' => 'Contact[date][month]']],
  5942. $monthsRegex,
  5943. ['option' => ['selected' => 'selected', 'value' => '']],
  5944. '/option',
  5945. '*/select',
  5946. ['select' => ['name' => 'Contact[date][day]']],
  5947. $daysRegex,
  5948. ['option' => ['selected' => 'selected', 'value' => '']],
  5949. '/option',
  5950. '*/select',
  5951. ['select' => ['name' => 'Contact[date][hour]']],
  5952. $hoursRegex,
  5953. ['option' => ['selected' => 'selected', 'value' => '']],
  5954. '/option',
  5955. '*/select',
  5956. ['select' => ['name' => 'Contact[date][minute]']],
  5957. $minutesRegex,
  5958. ['option' => ['selected' => 'selected', 'value' => '']],
  5959. '/option',
  5960. ['option' => ['value' => '00']],
  5961. '00',
  5962. '/option',
  5963. ['option' => ['value' => '05']],
  5964. '05',
  5965. '/option',
  5966. ['option' => ['value' => '10']],
  5967. '10',
  5968. '/option',
  5969. '*/select',
  5970. ];
  5971. $this->assertHtml($expected, $result);
  5972. }
  5973. /**
  5974. * testDateTimeRounding method
  5975. *
  5976. * Test dateTime with rounding.
  5977. *
  5978. * @return void
  5979. */
  5980. public function testDateTimeRounding()
  5981. {
  5982. $this->View->setRequest($this->View->getRequest()->withData('Contact', [
  5983. 'date' => [
  5984. 'day' => '13',
  5985. 'month' => '12',
  5986. 'year' => '2010',
  5987. 'hour' => '04',
  5988. 'minute' => '19',
  5989. 'meridian' => 'AM'
  5990. ]
  5991. ]));
  5992. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5993. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5994. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5995. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5996. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5997. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5998. }
  5999. /**
  6000. * testDatetimeWithDefault method
  6001. *
  6002. * Test that datetime() and default values work.
  6003. *
  6004. * @return void
  6005. */
  6006. public function testDatetimeWithDefault()
  6007. {
  6008. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  6009. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  6010. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  6011. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  6012. $result = $this->Form->dateTime('Contact.updated', [
  6013. 'default' => '2009-06-01 11:15:30'
  6014. ]);
  6015. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  6016. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  6017. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  6018. }
  6019. /**
  6020. * testDateTimeAllZeros method
  6021. *
  6022. * Test datetime() with all zeros.
  6023. *
  6024. * @return void
  6025. */
  6026. public function testDateTimeAllZeros()
  6027. {
  6028. $result = $this->Form->dateTime('Contact.date', [
  6029. 'timeFormat' => false,
  6030. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  6031. 'value' => '0000-00-00'
  6032. ]);
  6033. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  6034. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  6035. }
  6036. /**
  6037. * testFormDateTimeMulti method
  6038. *
  6039. * Test multiple datetime element generation.
  6040. *
  6041. * @return void
  6042. */
  6043. public function testFormDateTimeMulti()
  6044. {
  6045. extract($this->dateRegex);
  6046. $result = $this->Form->dateTime('Contact.1.updated');
  6047. $this->assertContains('Contact[1][updated][month]', $result);
  6048. $this->assertContains('Contact[1][updated][day]', $result);
  6049. $this->assertContains('Contact[1][updated][year]', $result);
  6050. $this->assertContains('Contact[1][updated][hour]', $result);
  6051. $this->assertContains('Contact[1][updated][minute]', $result);
  6052. }
  6053. /**
  6054. * testDateTimeLabelIdMatchesFirstControl method
  6055. *
  6056. * When changing the date format, the label should always focus the first select box when
  6057. * clicked.
  6058. *
  6059. * @return void
  6060. */
  6061. public function testDateTimeLabelIdMatchesFirstControl()
  6062. {
  6063. $result = $this->Form->control('Model.date', ['type' => 'date']);
  6064. $this->assertContains('<label>Date</label>', $result);
  6065. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  6066. $this->assertContains('<label>Date</label>', $result);
  6067. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  6068. $this->assertContains('<label>Date</label>', $result);
  6069. }
  6070. /**
  6071. * testDateTimeSecondOptions method
  6072. *
  6073. * Test datetime second=true.
  6074. *
  6075. * @return void
  6076. */
  6077. public function testDateTimeSecondOptions()
  6078. {
  6079. $result = $this->Form->dateTime('updated', ['second' => true]);
  6080. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6081. $result = $this->Form->dateTime('updated', ['second' => []]);
  6082. $this->assertContains('updated[second]', $result, 'Should have seconds');
  6083. $result = $this->Form->dateTime('updated', ['second' => null]);
  6084. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6085. $result = $this->Form->dateTime('updated', ['second' => false]);
  6086. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  6087. }
  6088. /**
  6089. * testMonth method
  6090. *
  6091. * Test generation of a month input.
  6092. *
  6093. * @return void
  6094. */
  6095. public function testMonth()
  6096. {
  6097. $result = $this->Form->month('Model.field', ['value' => '']);
  6098. $expected = [
  6099. ['select' => ['name' => 'Model[field][month]']],
  6100. ['option' => ['value' => '', 'selected' => 'selected']],
  6101. '/option',
  6102. ['option' => ['value' => '01']],
  6103. date('F', strtotime('2008-01-01 00:00:00')),
  6104. '/option',
  6105. ['option' => ['value' => '02']],
  6106. date('F', strtotime('2008-02-01 00:00:00')),
  6107. '/option',
  6108. '*/select',
  6109. ];
  6110. $this->assertHtml($expected, $result);
  6111. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  6112. $expected = [
  6113. ['select' => ['name' => 'Model[field][month]']],
  6114. ['option' => ['selected' => 'selected', 'value' => '']],
  6115. '/option',
  6116. ['option' => ['value' => '01']],
  6117. date('F', strtotime('2008-01-01 00:00:00')),
  6118. '/option',
  6119. ['option' => ['value' => '02']],
  6120. date('F', strtotime('2008-02-01 00:00:00')),
  6121. '/option',
  6122. '*/select',
  6123. ];
  6124. $this->assertHtml($expected, $result);
  6125. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  6126. $expected = [
  6127. ['select' => ['name' => 'Model[field][month]']],
  6128. ['option' => ['selected' => 'selected', 'value' => '']],
  6129. '/option',
  6130. ['option' => ['value' => '01']],
  6131. '1',
  6132. '/option',
  6133. ['option' => ['value' => '02']],
  6134. '2',
  6135. '/option',
  6136. '*/select',
  6137. ];
  6138. $this->assertHtml($expected, $result);
  6139. $monthNames = [
  6140. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6141. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  6142. ];
  6143. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  6144. $expected = [
  6145. ['select' => ['name' => 'Model[field][month]']],
  6146. ['option' => ['value' => '']],
  6147. '/option',
  6148. ['option' => ['value' => '01', 'selected' => 'selected']],
  6149. 'Jan',
  6150. '/option',
  6151. ['option' => ['value' => '02']],
  6152. 'Feb',
  6153. '/option',
  6154. '*/select',
  6155. ];
  6156. $this->assertHtml($expected, $result);
  6157. $this->View->setRequest(
  6158. $this->View->getRequest()->withData('Project.release', '2050-02-10')
  6159. );
  6160. $this->Form->create();
  6161. $result = $this->Form->month('Project.release');
  6162. $expected = [
  6163. ['select' => ['name' => 'Project[release][month]']],
  6164. ['option' => ['value' => '']],
  6165. '/option',
  6166. ['option' => ['value' => '01']],
  6167. 'January',
  6168. '/option',
  6169. ['option' => ['value' => '02', 'selected' => 'selected']],
  6170. 'February',
  6171. '/option',
  6172. '*/select',
  6173. ];
  6174. $this->assertHtml($expected, $result);
  6175. $result = $this->Form->month('Contact.published', [
  6176. 'empty' => 'Published on',
  6177. ]);
  6178. $this->assertContains('Published on', $result);
  6179. }
  6180. /**
  6181. * testDay method
  6182. *
  6183. * Test generation of a day input.
  6184. *
  6185. * @return void
  6186. */
  6187. public function testDay()
  6188. {
  6189. extract($this->dateRegex);
  6190. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  6191. $expected = [
  6192. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  6193. ['option' => ['selected' => 'selected', 'value' => '']],
  6194. '/option',
  6195. ['option' => ['value' => '01']],
  6196. '1',
  6197. '/option',
  6198. ['option' => ['value' => '02']],
  6199. '2',
  6200. '/option',
  6201. $daysRegex,
  6202. '/select',
  6203. ];
  6204. $this->assertHtml($expected, $result);
  6205. $this->View->setRequest(
  6206. $this->View->getRequest()->withData('Model.field', '2006-10-10 23:12:32')
  6207. );
  6208. $this->Form->create();
  6209. $result = $this->Form->day('Model.field');
  6210. $expected = [
  6211. ['select' => ['name' => 'Model[field][day]']],
  6212. ['option' => ['value' => '']],
  6213. '/option',
  6214. ['option' => ['value' => '01']],
  6215. '1',
  6216. '/option',
  6217. ['option' => ['value' => '02']],
  6218. '2',
  6219. '/option',
  6220. $daysRegex,
  6221. ['option' => ['value' => '10', 'selected' => 'selected']],
  6222. '10',
  6223. '/option',
  6224. $daysRegex,
  6225. '/select',
  6226. ];
  6227. $this->assertHtml($expected, $result);
  6228. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6229. $this->Form->create();
  6230. $result = $this->Form->day('Model.field', ['value' => '10']);
  6231. $expected = [
  6232. ['select' => ['name' => 'Model[field][day]']],
  6233. ['option' => ['value' => '']],
  6234. '/option',
  6235. ['option' => ['value' => '01']],
  6236. '1',
  6237. '/option',
  6238. ['option' => ['value' => '02']],
  6239. '2',
  6240. '/option',
  6241. $daysRegex,
  6242. ['option' => ['value' => '10', 'selected' => 'selected']],
  6243. '10',
  6244. '/option',
  6245. $daysRegex,
  6246. '/select',
  6247. ];
  6248. $this->assertHtml($expected, $result);
  6249. $this->View->setRequest(
  6250. $this->View->getRequest()->withData('Project.release', '2050-10-10')
  6251. );
  6252. $this->Form->create();
  6253. $result = $this->Form->day('Project.release');
  6254. $expected = [
  6255. ['select' => ['name' => 'Project[release][day]']],
  6256. ['option' => ['value' => '']],
  6257. '/option',
  6258. ['option' => ['value' => '01']],
  6259. '1',
  6260. '/option',
  6261. ['option' => ['value' => '02']],
  6262. '2',
  6263. '/option',
  6264. $daysRegex,
  6265. ['option' => ['value' => '10', 'selected' => 'selected']],
  6266. '10',
  6267. '/option',
  6268. $daysRegex,
  6269. '/select',
  6270. ];
  6271. $this->assertHtml($expected, $result);
  6272. $result = $this->Form->day('Contact.published', [
  6273. 'empty' => 'Published on',
  6274. ]);
  6275. $this->assertContains('Published on', $result);
  6276. }
  6277. /**
  6278. * testMinute method
  6279. *
  6280. * Test generation of a minute input.
  6281. *
  6282. * @return void
  6283. */
  6284. public function testMinute()
  6285. {
  6286. extract($this->dateRegex);
  6287. $result = $this->Form->minute('Model.field', ['value' => '']);
  6288. $expected = [
  6289. ['select' => ['name' => 'Model[field][minute]']],
  6290. ['option' => ['selected' => 'selected', 'value' => '']],
  6291. '/option',
  6292. ['option' => ['value' => '00']],
  6293. '00',
  6294. '/option',
  6295. ['option' => ['value' => '01']],
  6296. '01',
  6297. '/option',
  6298. ['option' => ['value' => '02']],
  6299. '02',
  6300. '/option',
  6301. $minutesRegex,
  6302. '/select',
  6303. ];
  6304. $this->assertHtml($expected, $result);
  6305. $this->View->setRequest(
  6306. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:12:32')
  6307. );
  6308. $this->Form->create();
  6309. $result = $this->Form->minute('Model.field');
  6310. $expected = [
  6311. ['select' => ['name' => 'Model[field][minute]']],
  6312. ['option' => ['value' => '']],
  6313. '/option',
  6314. ['option' => ['value' => '00']],
  6315. '00',
  6316. '/option',
  6317. ['option' => ['value' => '01']],
  6318. '01',
  6319. '/option',
  6320. ['option' => ['value' => '02']],
  6321. '02',
  6322. '/option',
  6323. $minutesRegex,
  6324. ['option' => ['value' => '12', 'selected' => 'selected']],
  6325. '12',
  6326. '/option',
  6327. $minutesRegex,
  6328. '/select',
  6329. ];
  6330. $this->assertHtml($expected, $result);
  6331. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6332. $this->Form->create();
  6333. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6334. $expected = [
  6335. ['select' => ['name' => 'Model[field][minute]']],
  6336. ['option' => ['selected' => 'selected', 'value' => '']],
  6337. '/option',
  6338. ['option' => ['value' => '00']],
  6339. '00',
  6340. '/option',
  6341. ['option' => ['value' => '05']],
  6342. '05',
  6343. '/option',
  6344. ['option' => ['value' => '10']],
  6345. '10',
  6346. '/option',
  6347. $minutesRegex,
  6348. '/select',
  6349. ];
  6350. $this->assertHtml($expected, $result);
  6351. $this->View->setRequest(
  6352. $this->View->getRequest()->withData('Model.field', '2006-10-10 00:10:32')
  6353. );
  6354. $this->Form->create();
  6355. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6356. $expected = [
  6357. ['select' => ['name' => 'Model[field][minute]']],
  6358. ['option' => ['value' => '']],
  6359. '/option',
  6360. ['option' => ['value' => '00']],
  6361. '00',
  6362. '/option',
  6363. ['option' => ['value' => '05']],
  6364. '05',
  6365. '/option',
  6366. ['option' => ['value' => '10', 'selected' => 'selected']],
  6367. '10',
  6368. '/option',
  6369. $minutesRegex,
  6370. '/select',
  6371. ];
  6372. $this->assertHtml($expected, $result);
  6373. }
  6374. /**
  6375. * testMeridian method
  6376. *
  6377. * Test generating an input for the meridian.
  6378. *
  6379. * @return void
  6380. */
  6381. public function testMeridian()
  6382. {
  6383. extract($this->dateRegex);
  6384. $now = new \DateTime();
  6385. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6386. $expected = [
  6387. ['select' => ['name' => 'Model[field][meridian]']],
  6388. ['option' => ['value' => '']],
  6389. '/option',
  6390. $meridianRegex,
  6391. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6392. $now->format('a'),
  6393. '/option',
  6394. '*/select'
  6395. ];
  6396. $this->assertHtml($expected, $result);
  6397. }
  6398. /**
  6399. * testHour method
  6400. *
  6401. * Test generation of an hour input.
  6402. *
  6403. * @return void
  6404. */
  6405. public function testHour()
  6406. {
  6407. extract($this->dateRegex);
  6408. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6409. $expected = [
  6410. ['select' => ['name' => 'Model[field][hour]']],
  6411. ['option' => ['selected' => 'selected', 'value' => '']],
  6412. '/option',
  6413. ['option' => ['value' => '01']],
  6414. '1',
  6415. '/option',
  6416. ['option' => ['value' => '02']],
  6417. '2',
  6418. '/option',
  6419. $hoursRegex,
  6420. '/select',
  6421. ];
  6422. $this->assertHtml($expected, $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' => 12]);
  6428. $expected = [
  6429. ['select' => ['name' => 'Model[field][hour]']],
  6430. ['option' => ['value' => '']],
  6431. '/option',
  6432. ['option' => ['value' => '01']],
  6433. '1',
  6434. '/option',
  6435. ['option' => ['value' => '02']],
  6436. '2',
  6437. '/option',
  6438. $hoursRegex,
  6439. ['option' => ['value' => '12', 'selected' => 'selected']],
  6440. '12',
  6441. '/option',
  6442. '/select',
  6443. ];
  6444. $this->assertHtml($expected, $result);
  6445. $this->View->setRequest($this->View->getRequest()->withData('Model.field', ''));
  6446. $this->Form->create();
  6447. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6448. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6449. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6450. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6451. $this->View->setRequest(
  6452. $this->View->getRequest()->withData('Model.field', '2006-10-10 00: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', 'selected' => 'selected']],
  6461. '0',
  6462. '/option',
  6463. ['option' => ['value' => '01']],
  6464. '1',
  6465. '/option',
  6466. ['option' => ['value' => '02']],
  6467. '2',
  6468. '/option',
  6469. $hoursRegex,
  6470. '/select',
  6471. ];
  6472. $this->assertHtml($expected, $result);
  6473. $this->View->setRequest($this->View->getRequest()->withData('Model.field', null));
  6474. $this->Form->create();
  6475. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6476. $thisHour = date('H');
  6477. $optValue = date('G');
  6478. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6479. $this->View->setRequest(
  6480. $this->View->getRequest()->withData('Model.field', '2050-10-10 01:12:32')
  6481. );
  6482. $this->Form->create();
  6483. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6484. $expected = [
  6485. ['select' => ['name' => 'Model[field][hour]']],
  6486. ['option' => ['value' => '']],
  6487. '/option',
  6488. ['option' => ['value' => '00']],
  6489. '0',
  6490. '/option',
  6491. ['option' => ['value' => '01', 'selected' => 'selected']],
  6492. '1',
  6493. '/option',
  6494. ['option' => ['value' => '02']],
  6495. '2',
  6496. '/option',
  6497. $hoursRegex,
  6498. '/select',
  6499. ];
  6500. $this->assertHtml($expected, $result);
  6501. }
  6502. /**
  6503. * testYear method
  6504. *
  6505. * Test generation of a year input.
  6506. *
  6507. * @return void
  6508. */
  6509. public function testYear()
  6510. {
  6511. $this->View->setRequest(
  6512. $this->View->getRequest()->withData('Contact.published', '2006-10-10')
  6513. );
  6514. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6515. $expected = [
  6516. ['select' => ['name' => 'Model[field][year]']],
  6517. ['option' => ['selected' => 'selected', 'value' => '']],
  6518. '/option',
  6519. ['option' => ['value' => '2007']],
  6520. '2007',
  6521. '/option',
  6522. ['option' => ['value' => '2006']],
  6523. '2006',
  6524. '/option',
  6525. '/select',
  6526. ];
  6527. $this->assertHtml($expected, $result);
  6528. $result = $this->Form->year('Model.field', [
  6529. 'value' => '',
  6530. 'minYear' => 2006,
  6531. 'maxYear' => 2007,
  6532. 'orderYear' => 'asc'
  6533. ]);
  6534. $expected = [
  6535. ['select' => ['name' => 'Model[field][year]']],
  6536. ['option' => ['selected' => 'selected', 'value' => '']],
  6537. '/option',
  6538. ['option' => ['value' => '2006']],
  6539. '2006',
  6540. '/option',
  6541. ['option' => ['value' => '2007']],
  6542. '2007',
  6543. '/option',
  6544. '/select',
  6545. ];
  6546. $this->assertHtml($expected, $result);
  6547. $result = $this->Form->year('Contact.published', [
  6548. 'empty' => false,
  6549. 'minYear' => 2006,
  6550. 'maxYear' => 2007,
  6551. ]);
  6552. $expected = [
  6553. ['select' => ['name' => 'Contact[published][year]']],
  6554. ['option' => ['value' => '2007']],
  6555. '2007',
  6556. '/option',
  6557. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6558. '2006',
  6559. '/option',
  6560. '/select',
  6561. ];
  6562. $this->assertHtml($expected, $result);
  6563. $result = $this->Form->year('Contact.published', [
  6564. 'empty' => 'Published on',
  6565. ]);
  6566. $this->assertContains('Published on', $result);
  6567. }
  6568. /**
  6569. * testControlDatetimePreEpoch method
  6570. *
  6571. * Test minYear being prior to the unix epoch.
  6572. *
  6573. * @return void
  6574. */
  6575. public function testControlDatetimePreEpoch()
  6576. {
  6577. $start = date('Y') - 80;
  6578. $end = date('Y') - 18;
  6579. $result = $this->Form->control('birth_year', [
  6580. 'type' => 'date',
  6581. 'label' => 'Birth Year',
  6582. 'minYear' => $start,
  6583. 'maxYear' => $end,
  6584. 'month' => false,
  6585. 'day' => false,
  6586. ]);
  6587. $this->assertContains('value="' . $start . '">' . $start, $result);
  6588. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6589. $this->assertNotContains('value="00">00', $result);
  6590. }
  6591. /**
  6592. * test control() datetime & required attributes
  6593. *
  6594. * @return void
  6595. */
  6596. public function testControlDatetimeRequired()
  6597. {
  6598. $result = $this->Form->control('birthday', [
  6599. 'type' => 'date',
  6600. 'required' => true
  6601. ]);
  6602. $this->assertContains(
  6603. '<select name="birthday[year]" required="required"',
  6604. $result
  6605. );
  6606. }
  6607. /**
  6608. * testYearAutoExpandRange method
  6609. *
  6610. * @return void
  6611. */
  6612. public function testYearAutoExpandRange()
  6613. {
  6614. $this->View->setRequest($this->View->getRequest()->withData('User.birthday', '1930-10-10'));
  6615. $result = $this->Form->year('User.birthday');
  6616. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6617. $result = $matches[1];
  6618. $expected = range(date('Y') + 5, 1930);
  6619. $this->assertEquals($expected, $result);
  6620. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '2050-10-10'));
  6621. $this->Form->create();
  6622. $result = $this->Form->year('Project.release');
  6623. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6624. $result = $matches[1];
  6625. $expected = range(2050, date('Y') - 5);
  6626. $this->assertEquals($expected, $result);
  6627. $this->View->setRequest($this->View->getRequest()->withData('Project.release', '1881-10-10'));
  6628. $this->Form->create();
  6629. $result = $this->Form->year('Project.release', [
  6630. 'minYear' => 1890,
  6631. 'maxYear' => 1900
  6632. ]);
  6633. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6634. $result = $matches[1];
  6635. $expected = range(1900, 1881);
  6636. $this->assertEquals($expected, $result);
  6637. }
  6638. /**
  6639. * testControlDate method
  6640. *
  6641. * Test that control() accepts the type of date and passes options in.
  6642. *
  6643. * @return void
  6644. */
  6645. public function testControlDate()
  6646. {
  6647. $this->View->setRequest($this->View->getRequest()->withParsedBody([
  6648. 'month_year' => ['month' => date('m')],
  6649. ]));
  6650. $this->Form->create($this->article);
  6651. $result = $this->Form->control('month_year', [
  6652. 'label' => false,
  6653. 'type' => 'date',
  6654. 'minYear' => 2006,
  6655. 'maxYear' => 2008
  6656. ]);
  6657. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6658. $this->assertNotContains('value="2008" selected="selected"', $result);
  6659. }
  6660. /**
  6661. * testControlLabelFalse method
  6662. *
  6663. * Test the label option being set to false.
  6664. *
  6665. * @return void
  6666. */
  6667. public function testControlLabelFalse()
  6668. {
  6669. $this->Form->create($this->article);
  6670. $result = $this->Form->control('title', ['label' => false]);
  6671. $expected = [
  6672. 'div' => ['class' => 'input text required'],
  6673. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6674. '/div'
  6675. ];
  6676. $this->assertHtml($expected, $result);
  6677. }
  6678. /**
  6679. * testControlDateMaxYear method
  6680. *
  6681. * Let's say we want to only allow users born from 2006 to 2008 to register
  6682. * This being the first signup page, we still don't have any data.
  6683. *
  6684. * @return void
  6685. */
  6686. public function testControlDateMaxYear()
  6687. {
  6688. $this->Form->create($this->article);
  6689. $result = $this->Form->control('birthday', [
  6690. 'label' => false,
  6691. 'type' => 'date',
  6692. 'minYear' => 2006,
  6693. 'maxYear' => 2008,
  6694. 'default' => true
  6695. ]);
  6696. $this->assertContains('value="2008" selected="selected"', $result);
  6697. $this->assertContains('value="2006"', $result);
  6698. $this->assertNotContains('value="2005"', $result);
  6699. $this->assertNotContains('value="2009"', $result);
  6700. }
  6701. /**
  6702. * testTextArea method
  6703. *
  6704. * Test generation of a textarea input.
  6705. *
  6706. * @return void
  6707. */
  6708. public function testTextArea()
  6709. {
  6710. $this->View->setRequest($this->View->getRequest()->withData('field', 'some test data'));
  6711. $result = $this->Form->textarea('field');
  6712. $expected = [
  6713. 'textarea' => ['name' => 'field', 'rows' => 5],
  6714. 'some test data',
  6715. '/textarea',
  6716. ];
  6717. $this->assertHtml($expected, $result);
  6718. $result = $this->Form->textarea('user.bio');
  6719. $expected = [
  6720. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6721. '/textarea',
  6722. ];
  6723. $this->assertHtml($expected, $result);
  6724. $this->View->setRequest($this->View->getRequest()
  6725. ->withData('field', 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6726. $this->Form->create();
  6727. $result = $this->Form->textarea('field');
  6728. $expected = [
  6729. 'textarea' => ['name' => 'field', 'rows' => 5],
  6730. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6731. '/textarea',
  6732. ];
  6733. $this->assertHtml($expected, $result);
  6734. $this->View->setRequest($this->View->getRequest()->withData(
  6735. 'Model.field',
  6736. 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  6737. ));
  6738. $this->Form->create();
  6739. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6740. $expected = [
  6741. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6742. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6743. '/textarea',
  6744. ];
  6745. $this->assertHtml($expected, $result);
  6746. $result = $this->Form->textarea('0.OtherModel.field');
  6747. $expected = [
  6748. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6749. '/textarea'
  6750. ];
  6751. $this->assertHtml($expected, $result);
  6752. }
  6753. /**
  6754. * testTextAreaWithStupidCharacters method
  6755. *
  6756. * Test text area with non-ascii characters.
  6757. *
  6758. * @return void
  6759. */
  6760. public function testTextAreaWithStupidCharacters()
  6761. {
  6762. $result = $this->Form->textarea('Post.content', [
  6763. 'value' => 'GREAT®',
  6764. 'rows' => '15',
  6765. 'cols' => '75'
  6766. ]);
  6767. $expected = [
  6768. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6769. 'GREAT®',
  6770. '/textarea',
  6771. ];
  6772. $this->assertHtml($expected, $result);
  6773. }
  6774. /**
  6775. * testTextAreaMaxLength method
  6776. *
  6777. * Test textareas maxlength read from schema.
  6778. *
  6779. * @return void
  6780. */
  6781. public function testTextAreaMaxLength()
  6782. {
  6783. $this->Form->create([
  6784. 'schema' => [
  6785. 'stuff' => ['type' => 'string', 'length' => 10],
  6786. ]
  6787. ]);
  6788. $result = $this->Form->control('other', ['type' => 'textarea']);
  6789. $expected = [
  6790. 'div' => ['class' => 'input textarea'],
  6791. 'label' => ['for' => 'other'],
  6792. 'Other',
  6793. '/label',
  6794. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6795. '/textarea',
  6796. '/div'
  6797. ];
  6798. $this->assertHtml($expected, $result);
  6799. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6800. $expected = [
  6801. 'div' => ['class' => 'input textarea'],
  6802. 'label' => ['for' => 'stuff'],
  6803. 'Stuff',
  6804. '/label',
  6805. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6806. '/textarea',
  6807. '/div'
  6808. ];
  6809. $this->assertHtml($expected, $result);
  6810. }
  6811. /**
  6812. * testHiddenField method
  6813. *
  6814. * Test generation of a hidden input.
  6815. *
  6816. * @return void
  6817. */
  6818. public function testHiddenField()
  6819. {
  6820. $this->article['errors'] = [
  6821. 'field' => true
  6822. ];
  6823. $this->View->setRequest($this->View->getRequest()->withData('field', 'test'));
  6824. $this->Form->create($this->article);
  6825. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6826. $expected = [
  6827. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6828. $this->assertHtml($expected, $result);
  6829. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6830. $expected = [
  6831. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6832. ];
  6833. $this->assertHtml($expected, $result);
  6834. }
  6835. /**
  6836. * testFileUploadField method
  6837. *
  6838. * Test generation of a file upload input.
  6839. *
  6840. * @return void
  6841. */
  6842. public function testFileUploadField()
  6843. {
  6844. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6845. $result = $this->Form->file('Model.upload');
  6846. $this->assertHtml($expected, $result);
  6847. $this->View->setRequest($this->View->getRequest()->withData('Model.upload', [
  6848. 'name' => '', 'type' => '', 'tmp_name' => '',
  6849. 'error' => 4, 'size' => 0
  6850. ]));
  6851. $result = $this->Form->file('Model.upload');
  6852. $this->assertHtml($expected, $result);
  6853. $this->View->setRequest(
  6854. $this->View->getRequest()->withData('Model.upload', 'no data should be set in value')
  6855. );
  6856. $result = $this->Form->file('Model.upload');
  6857. $this->assertHtml($expected, $result);
  6858. }
  6859. /**
  6860. * testFileUploadOnOtherModel method
  6861. *
  6862. * Test File upload input on a model not used in create().
  6863. *
  6864. * @return void
  6865. */
  6866. public function testFileUploadOnOtherModel()
  6867. {
  6868. $this->Form->create($this->article, ['type' => 'file']);
  6869. $result = $this->Form->file('ValidateProfile.city');
  6870. $expected = [
  6871. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6872. ];
  6873. $this->assertHtml($expected, $result);
  6874. }
  6875. /**
  6876. * testButton method
  6877. *
  6878. * Test generation of a form button.
  6879. *
  6880. * @return void
  6881. */
  6882. public function testButton()
  6883. {
  6884. $result = $this->Form->button('Hi');
  6885. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6886. $this->assertHtml($expected, $result);
  6887. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6888. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6889. $this->assertHtml($expected, $result);
  6890. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6891. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6892. $this->assertHtml($expected, $result);
  6893. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6894. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6895. $this->assertHtml($expected, $result);
  6896. $result = $this->Form->button('No type', ['type' => false]);
  6897. $expected = ['button' => [], 'No type', '/button'];
  6898. $this->assertHtml($expected, $result);
  6899. $result = $this->Form->button('Upload Text', [
  6900. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6901. 'escape' => false
  6902. ]);
  6903. $this->assertNotRegExp('/\&039/', $result);
  6904. }
  6905. /**
  6906. * testButtonUnlockedByDefault method
  6907. *
  6908. * Test that button() makes unlocked fields by default.
  6909. *
  6910. * @return void
  6911. */
  6912. public function testButtonUnlockedByDefault()
  6913. {
  6914. $this->View->setRequest($this->View->getRequest()->withParam('_csrfToken', 'secured'));
  6915. $this->Form->button('Save', ['name' => 'save']);
  6916. $this->Form->button('Clear');
  6917. $result = $this->Form->unlockField();
  6918. $this->assertEquals(['save'], $result);
  6919. }
  6920. /**
  6921. * Test generation of a form button with confirm message.
  6922. *
  6923. * @return void
  6924. */
  6925. public function testButtonWithConfirm()
  6926. {
  6927. $result = $this->Form->button('Hi', ['confirm' => 'Confirm me!']);
  6928. $expected = ['button' => ['type' => 'submit', 'onclick' => 'if (confirm(&quot;Confirm me!&quot;)) { return true; } return false;'], 'Hi', '/button'];
  6929. $this->assertHtml($expected, $result);
  6930. }
  6931. /**
  6932. * testPostButton method
  6933. *
  6934. * @return void
  6935. */
  6936. public function testPostButton()
  6937. {
  6938. $result = $this->Form->postButton('Hi', '/controller/action');
  6939. $expected = [
  6940. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6941. 'div' => ['style' => 'display:none;'],
  6942. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6943. '/div',
  6944. 'button' => ['type' => 'submit'],
  6945. 'Hi',
  6946. '/button',
  6947. '/form'
  6948. ];
  6949. $this->assertHtml($expected, $result);
  6950. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6951. $this->assertContains('<input type="hidden" name="extra" value="value"', $result);
  6952. }
  6953. /**
  6954. * testPostButtonMethodType method
  6955. *
  6956. * @return void
  6957. */
  6958. public function testPostButtonMethodType()
  6959. {
  6960. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6961. $expected = [
  6962. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6963. 'div' => ['style' => 'display:none;'],
  6964. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6965. '/div',
  6966. 'button' => ['type' => 'submit'],
  6967. 'Hi',
  6968. '/button',
  6969. '/form'
  6970. ];
  6971. $this->assertHtml($expected, $result);
  6972. }
  6973. /**
  6974. * testPostButtonFormOptions method
  6975. *
  6976. * @return void
  6977. */
  6978. public function testPostButtonFormOptions()
  6979. {
  6980. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6981. $expected = [
  6982. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6983. 'div' => ['style' => 'display:none;'],
  6984. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6985. '/div',
  6986. 'button' => ['type' => 'submit'],
  6987. 'Hi',
  6988. '/button',
  6989. '/form'
  6990. ];
  6991. $this->assertHtml($expected, $result);
  6992. }
  6993. /**
  6994. * testPostButtonNestedData method
  6995. *
  6996. * Test using postButton with N dimensional data.
  6997. *
  6998. * @return void
  6999. */
  7000. public function testPostButtonNestedData()
  7001. {
  7002. $data = [
  7003. 'one' => [
  7004. 'two' => [
  7005. 3, 4, 5
  7006. ]
  7007. ]
  7008. ];
  7009. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  7010. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7011. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7012. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7013. }
  7014. /**
  7015. * testSecurePostButton method
  7016. *
  7017. * Test that postButton adds _Token fields.
  7018. *
  7019. * @return void
  7020. */
  7021. public function testSecurePostButton()
  7022. {
  7023. $this->View->setRequest($this->View->getRequest()
  7024. ->withParam('_csrfToken', 'testkey')
  7025. ->withParam('_Token.unlockedFields', []));
  7026. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  7027. $tokenDebug = urlencode(json_encode([
  7028. '/posts/delete/1',
  7029. [],
  7030. []
  7031. ]));
  7032. $expected = [
  7033. 'form' => [
  7034. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  7035. ],
  7036. ['div' => ['style' => 'display:none;']],
  7037. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7038. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7039. '/div',
  7040. 'button' => ['type' => 'submit'],
  7041. 'Delete',
  7042. '/button',
  7043. ['div' => ['style' => 'display:none;']],
  7044. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7045. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7046. ['input' => [
  7047. 'type' => 'hidden',
  7048. 'name' => '_Token[debug]',
  7049. 'value' => $tokenDebug,
  7050. 'autocomplete' => 'off',
  7051. ]],
  7052. '/div',
  7053. '/form',
  7054. ];
  7055. $this->assertHtml($expected, $result);
  7056. }
  7057. /**
  7058. * testPostLink method
  7059. *
  7060. * @return void
  7061. */
  7062. public function testPostLink()
  7063. {
  7064. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7065. $expected = [
  7066. 'form' => [
  7067. 'method' => 'post', 'action' => '/posts/delete/1',
  7068. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7069. ],
  7070. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7071. '/form',
  7072. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7073. 'Delete',
  7074. '/a'
  7075. ];
  7076. $this->assertHtml($expected, $result);
  7077. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  7078. $expected = [
  7079. 'form' => [
  7080. 'method' => 'post', 'action' => '/posts/delete/1',
  7081. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7082. ],
  7083. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  7084. '/form',
  7085. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7086. 'Delete',
  7087. '/a'
  7088. ];
  7089. $this->assertHtml($expected, $result);
  7090. $result = $this->Form->postLink(
  7091. 'Delete',
  7092. '/posts/delete/1',
  7093. ['target' => '_blank', 'class' => 'btn btn-danger']
  7094. );
  7095. $expected = [
  7096. 'form' => [
  7097. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  7098. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7099. ],
  7100. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7101. '/form',
  7102. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7103. 'Delete',
  7104. '/a'
  7105. ];
  7106. $this->assertHtml($expected, $result);
  7107. }
  7108. /**
  7109. * testPostLinkWithConfirm method
  7110. *
  7111. * Test the confirm option for postLink().
  7112. *
  7113. * @return void
  7114. */
  7115. public function testPostLinkWithConfirm()
  7116. {
  7117. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  7118. $expected = [
  7119. 'form' => [
  7120. 'method' => 'post', 'action' => '/posts/delete/1',
  7121. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7122. ],
  7123. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7124. '/form',
  7125. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  7126. 'Delete',
  7127. '/a'
  7128. ];
  7129. $this->assertHtml($expected, $result);
  7130. $result = $this->Form->postLink(
  7131. 'Delete',
  7132. '/posts/delete/1',
  7133. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  7134. );
  7135. $expected = [
  7136. 'form' => [
  7137. 'method' => 'post', 'action' => '/posts/delete/1',
  7138. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7139. ],
  7140. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7141. '/form',
  7142. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  7143. 'Delete',
  7144. '/a'
  7145. ];
  7146. $this->assertHtml($expected, $result);
  7147. $this->Form->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { $(\'form[name="{{formName}}"]\').submit();};']);
  7148. $result = $this->Form->postLink(
  7149. 'Delete',
  7150. '/posts/delete/1',
  7151. ['escape' => false, 'confirm' => 'Confirm this deletion?']
  7152. );
  7153. $expected = [
  7154. 'form' => [
  7155. 'method' => 'post', 'action' => '/posts/delete/1',
  7156. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7157. ],
  7158. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7159. '/form',
  7160. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm this deletion\?"\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/'],
  7161. 'Delete',
  7162. '/a'
  7163. ];
  7164. $this->assertHtml($expected, $result);
  7165. }
  7166. /**
  7167. * testPostLinkWithQuery method
  7168. *
  7169. * Test postLink() with query string args.
  7170. *
  7171. * @return void
  7172. */
  7173. public function testPostLinkWithQuery()
  7174. {
  7175. $result = $this->Form->postLink(
  7176. 'Delete',
  7177. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  7178. );
  7179. $expected = [
  7180. 'form' => [
  7181. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  7182. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7183. ],
  7184. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7185. '/form',
  7186. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7187. 'Delete',
  7188. '/a'
  7189. ];
  7190. $this->assertHtml($expected, $result);
  7191. }
  7192. /**
  7193. * testPostLinkWithData method
  7194. *
  7195. * Test postLink with additional data.
  7196. *
  7197. * @return void
  7198. */
  7199. public function testPostLinkWithData()
  7200. {
  7201. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  7202. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  7203. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  7204. $this->Form->create($entity);
  7205. $this->Form->end();
  7206. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  7207. $this->assertContains(
  7208. '<input type="hidden" name="name" value="show"',
  7209. $result,
  7210. 'should not contain entity data.'
  7211. );
  7212. }
  7213. /**
  7214. * testPostLinkSecurityHash method
  7215. *
  7216. * Test that security hashes for postLink include the url.
  7217. *
  7218. * @return void
  7219. */
  7220. public function testPostLinkSecurityHash()
  7221. {
  7222. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7223. $hash .= '%3Aid';
  7224. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7225. $result = $this->Form->postLink(
  7226. 'Delete',
  7227. '/posts/delete/1',
  7228. ['data' => ['id' => 1]]
  7229. );
  7230. $tokenDebug = urlencode(json_encode([
  7231. '/posts/delete/1',
  7232. [
  7233. 'id' => 1
  7234. ],
  7235. []
  7236. ]));
  7237. $expected = [
  7238. 'form' => [
  7239. 'method' => 'post', 'action' => '/posts/delete/1',
  7240. 'name', 'style' => 'display:none;'
  7241. ],
  7242. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7243. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7244. 'div' => ['style' => 'display:none;'],
  7245. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7246. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7247. ['input' => [
  7248. 'type' => 'hidden',
  7249. 'name' => '_Token[debug]',
  7250. 'value' => $tokenDebug,
  7251. 'autocomplete' => 'off',
  7252. ]],
  7253. '/div',
  7254. '/form',
  7255. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7256. 'Delete',
  7257. '/a'
  7258. ];
  7259. $this->assertHtml($expected, $result);
  7260. }
  7261. /**
  7262. * testPostLinkSecurityHashBlockMode method
  7263. *
  7264. * Test that postLink doesn't modify the fields in the containing form.
  7265. *
  7266. * postLink() calls inside open forms should not modify the field list
  7267. * for the form.
  7268. *
  7269. * @return void
  7270. */
  7271. public function testPostLinkSecurityHashBlockMode()
  7272. {
  7273. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize([]) . session_id(), Security::getSalt());
  7274. $hash .= '%3A';
  7275. $this->View->setRequest($this->View->getRequest()->withParam('_Token.key', 'test'));
  7276. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  7277. $this->Form->control('title');
  7278. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7279. $result = $this->View->fetch('postLink');
  7280. $this->assertEquals(['title'], $this->Form->fields);
  7281. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  7282. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  7283. }
  7284. /**
  7285. * testPostLinkSecurityHashNoDebugMode method
  7286. *
  7287. * Test that security does not include debug token if debug is false.
  7288. *
  7289. * @return void
  7290. */
  7291. public function testPostLinkSecurityHashNoDebugMode()
  7292. {
  7293. Configure::write('debug', false);
  7294. $hash = hash_hmac('sha1', '/posts/delete/1' . serialize(['id' => '1']) . session_id(), Security::getSalt());
  7295. $hash .= '%3Aid';
  7296. $this->View->setRequest($this->View->getRequest()
  7297. ->withParam('_Token.key', 'test'));
  7298. $result = $this->Form->postLink(
  7299. 'Delete',
  7300. '/posts/delete/1',
  7301. ['data' => ['id' => 1]]
  7302. );
  7303. $expected = [
  7304. 'form' => [
  7305. 'method' => 'post', 'action' => '/posts/delete/1',
  7306. 'name', 'style' => 'display:none;'
  7307. ],
  7308. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7309. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  7310. 'div' => ['style' => 'display:none;'],
  7311. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash, 'autocomplete' => 'off']],
  7312. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7313. '/div',
  7314. '/form',
  7315. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7316. 'Delete',
  7317. '/a'
  7318. ];
  7319. $this->assertHtml($expected, $result);
  7320. }
  7321. /**
  7322. * testPostLinkNestedData method
  7323. *
  7324. * Test using postLink with N dimensional data.
  7325. *
  7326. * @return void
  7327. */
  7328. public function testPostLinkNestedData()
  7329. {
  7330. $data = [
  7331. 'one' => [
  7332. 'two' => [
  7333. 3, 4, 5
  7334. ]
  7335. ]
  7336. ];
  7337. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7338. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7339. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7340. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7341. }
  7342. /**
  7343. * testPostLinkAfterGetForm method
  7344. *
  7345. * Test creating postLinks after a GET form.
  7346. *
  7347. * @return void
  7348. */
  7349. public function testPostLinkAfterGetForm()
  7350. {
  7351. $this->View->setRequest($this->View->getRequest()
  7352. ->withParam('_csrfToken', 'testkey')
  7353. ->withParam('_Token', 'val'));
  7354. $this->Form->create($this->article, ['type' => 'get']);
  7355. $this->Form->end();
  7356. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7357. $tokenDebug = urlencode(json_encode([
  7358. '/posts/delete/1',
  7359. [],
  7360. []
  7361. ]));
  7362. $expected = [
  7363. 'form' => [
  7364. 'method' => 'post', 'action' => '/posts/delete/1',
  7365. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7366. ],
  7367. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7368. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'autocomplete' => 'off']],
  7369. 'div' => ['style' => 'display:none;'],
  7370. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'autocomplete' => 'off']],
  7371. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'autocomplete' => 'off']],
  7372. ['input' => [
  7373. 'type' => 'hidden', 'name' => '_Token[debug]',
  7374. 'value' => $tokenDebug,
  7375. 'autocomplete' => 'off'
  7376. ]],
  7377. '/div',
  7378. '/form',
  7379. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7380. 'Delete',
  7381. '/a'
  7382. ];
  7383. $this->assertHtml($expected, $result);
  7384. }
  7385. /**
  7386. * testPostLinkFormBuffer method
  7387. *
  7388. * Test that postLink adds form tags to view block.
  7389. *
  7390. * @return void
  7391. */
  7392. public function testPostLinkFormBuffer()
  7393. {
  7394. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7395. $expected = [
  7396. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7397. 'Delete',
  7398. '/a'
  7399. ];
  7400. $this->assertHtml($expected, $result);
  7401. $result = $this->View->fetch('postLink');
  7402. $expected = [
  7403. 'form' => [
  7404. 'method' => 'post', 'action' => '/posts/delete/1',
  7405. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7406. ],
  7407. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7408. '/form'
  7409. ];
  7410. $this->assertHtml($expected, $result);
  7411. $result = $this->Form->postLink(
  7412. 'Delete',
  7413. '/posts/delete/2',
  7414. ['block' => true, 'method' => 'DELETE']
  7415. );
  7416. $expected = [
  7417. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7418. 'Delete',
  7419. '/a'
  7420. ];
  7421. $this->assertHtml($expected, $result);
  7422. $result = $this->View->fetch('postLink');
  7423. $expected = [
  7424. 'form' => [
  7425. 'method' => 'post', 'action' => '/posts/delete/1',
  7426. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7427. ],
  7428. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7429. '/form',
  7430. [
  7431. 'form' => [
  7432. 'method' => 'post', 'action' => '/posts/delete/2',
  7433. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7434. ],
  7435. ],
  7436. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7437. '/form'
  7438. ];
  7439. $this->assertHtml($expected, $result);
  7440. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7441. $expected = [
  7442. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7443. 'Delete',
  7444. '/a'
  7445. ];
  7446. $this->assertHtml($expected, $result);
  7447. $result = $this->View->fetch('foobar');
  7448. $expected = [
  7449. 'form' => [
  7450. 'method' => 'post', 'action' => '/posts/delete/1',
  7451. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7452. ],
  7453. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7454. '/form'
  7455. ];
  7456. $this->assertHtml($expected, $result);
  7457. }
  7458. /**
  7459. * testSubmitButton method
  7460. *
  7461. * @return void
  7462. */
  7463. public function testSubmitButton()
  7464. {
  7465. $result = $this->Form->submit('');
  7466. $expected = [
  7467. 'div' => ['class' => 'submit'],
  7468. 'input' => ['type' => 'submit', 'value' => ''],
  7469. '/div'
  7470. ];
  7471. $this->assertHtml($expected, $result);
  7472. $result = $this->Form->submit('Test Submit');
  7473. $expected = [
  7474. 'div' => ['class' => 'submit'],
  7475. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7476. '/div'
  7477. ];
  7478. $this->assertHtml($expected, $result);
  7479. $result = $this->Form->submit('Next >');
  7480. $expected = [
  7481. 'div' => ['class' => 'submit'],
  7482. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7483. '/div'
  7484. ];
  7485. $this->assertHtml($expected, $result);
  7486. $result = $this->Form->submit('Next >', ['escape' => false]);
  7487. $expected = [
  7488. 'div' => ['class' => 'submit'],
  7489. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7490. '/div'
  7491. ];
  7492. $this->assertHtml($expected, $result);
  7493. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7494. $expected = [
  7495. 'div' => ['class' => 'submit'],
  7496. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7497. '/div'
  7498. ];
  7499. $this->assertHtml($expected, $result);
  7500. }
  7501. /**
  7502. * testSubmitImage method
  7503. *
  7504. * Test image submit types.
  7505. *
  7506. * @return void
  7507. */
  7508. public function testSubmitImage()
  7509. {
  7510. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7511. $expected = [
  7512. 'div' => ['class' => 'submit'],
  7513. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7514. '/div'
  7515. ];
  7516. $this->assertHtml($expected, $result);
  7517. $result = $this->Form->submit('/relative/cake.power.gif');
  7518. $expected = [
  7519. 'div' => ['class' => 'submit'],
  7520. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7521. '/div'
  7522. ];
  7523. $this->assertHtml($expected, $result);
  7524. $result = $this->Form->submit('cake.power.gif');
  7525. $expected = [
  7526. 'div' => ['class' => 'submit'],
  7527. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7528. '/div'
  7529. ];
  7530. $this->assertHtml($expected, $result);
  7531. $result = $this->Form->submit('Not.an.image');
  7532. $expected = [
  7533. 'div' => ['class' => 'submit'],
  7534. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7535. '/div'
  7536. ];
  7537. $this->assertHtml($expected, $result);
  7538. }
  7539. /**
  7540. * testSubmitUnlockedByDefault method
  7541. *
  7542. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7543. * be submitted at a time.
  7544. *
  7545. * @return void
  7546. */
  7547. public function testSubmitUnlockedByDefault()
  7548. {
  7549. $this->View->setRequest($this->View->getRequest()->withParam('_Token', 'secured'));
  7550. $this->Form->submit('Go go');
  7551. $this->Form->submit('Save', ['name' => 'save']);
  7552. $result = $this->Form->unlockField();
  7553. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7554. }
  7555. /**
  7556. * testSubmitImageTimestamp method
  7557. *
  7558. * Test submit image with timestamps.
  7559. *
  7560. * @return void
  7561. */
  7562. public function testSubmitImageTimestamp()
  7563. {
  7564. Configure::write('Asset.timestamp', 'force');
  7565. $result = $this->Form->submit('cake.power.gif');
  7566. $expected = [
  7567. 'div' => ['class' => 'submit'],
  7568. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7569. '/div'
  7570. ];
  7571. $this->assertHtml($expected, $result);
  7572. }
  7573. /**
  7574. * testDateTimeWithGetForms method
  7575. *
  7576. * Test that datetime() works with GET style forms.
  7577. *
  7578. * @return void
  7579. */
  7580. public function testDateTimeWithGetForms()
  7581. {
  7582. extract($this->dateRegex);
  7583. $this->Form->create($this->article, ['type' => 'get']);
  7584. $result = $this->Form->datetime('created');
  7585. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7586. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7587. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7588. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7589. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7590. }
  7591. /**
  7592. * testForMagicControlNonExistingNorValidated method
  7593. *
  7594. * @return void
  7595. */
  7596. public function testForMagicControlNonExistingNorValidated()
  7597. {
  7598. $this->Form->create($this->article);
  7599. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  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. 'id' => 'non-existing-nor-validated'
  7608. ]
  7609. ];
  7610. $this->assertHtml($expected, $result);
  7611. $result = $this->Form->control('non_existing_nor_validated', [
  7612. 'val' => 'my value'
  7613. ]);
  7614. $expected = [
  7615. 'label' => ['for' => 'non-existing-nor-validated'],
  7616. 'Non Existing Nor Validated',
  7617. '/label',
  7618. 'input' => [
  7619. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7620. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7621. ]
  7622. ];
  7623. $this->assertHtml($expected, $result);
  7624. $this->View->setRequest(
  7625. $this->View->getRequest()->withData('non_existing_nor_validated', 'CakePHP magic')
  7626. );
  7627. $this->Form->create($this->article);
  7628. $result = $this->Form->control('non_existing_nor_validated');
  7629. $expected = [
  7630. 'label' => ['for' => 'non-existing-nor-validated'],
  7631. 'Non Existing Nor Validated',
  7632. '/label',
  7633. 'input' => [
  7634. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7635. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7636. ]
  7637. ];
  7638. $this->assertHtml($expected, $result);
  7639. }
  7640. /**
  7641. * testFormMagicControlLabel method
  7642. *
  7643. * @return void
  7644. */
  7645. public function testFormMagicControlLabel()
  7646. {
  7647. $this->getTableLocator()->get('Contacts', [
  7648. 'className' => __NAMESPACE__ . '\ContactsTable'
  7649. ]);
  7650. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7651. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7652. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7653. $expected = [
  7654. 'label' => ['for' => 'contacts-name'],
  7655. 'My label',
  7656. '/label',
  7657. 'input' => [
  7658. 'type' => 'text', 'name' => 'Contacts[name]',
  7659. 'id' => 'contacts-name', 'maxlength' => '255'
  7660. ]
  7661. ];
  7662. $this->assertHtml($expected, $result);
  7663. $result = $this->Form->control('name', [
  7664. 'label' => ['class' => 'mandatory']
  7665. ]);
  7666. $expected = [
  7667. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7668. 'Name',
  7669. '/label',
  7670. 'input' => [
  7671. 'type' => 'text', 'name' => 'name',
  7672. 'id' => 'name', 'maxlength' => '255'
  7673. ]
  7674. ];
  7675. $this->assertHtml($expected, $result);
  7676. $result = $this->Form->control('name', [
  7677. 'div' => false,
  7678. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7679. ]);
  7680. $expected = [
  7681. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7682. 'My label',
  7683. '/label',
  7684. 'input' => [
  7685. 'type' => 'text', 'name' => 'name',
  7686. 'id' => 'name', 'maxlength' => '255'
  7687. ]
  7688. ];
  7689. $this->assertHtml($expected, $result);
  7690. $result = $this->Form->control('Contact.name', [
  7691. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7692. ]);
  7693. $expected = [
  7694. 'label' => ['for' => 'my_id'],
  7695. 'Name',
  7696. '/label',
  7697. 'input' => [
  7698. 'type' => 'text', 'name' => 'Contact[name]',
  7699. 'id' => 'my_id', 'maxlength' => '255'
  7700. ]
  7701. ];
  7702. $this->assertHtml($expected, $result);
  7703. $result = $this->Form->control('1.id');
  7704. $expected = ['input' => [
  7705. 'type' => 'hidden', 'name' => '1[id]',
  7706. 'id' => '1-id'
  7707. ]];
  7708. $this->assertHtml($expected, $result);
  7709. $result = $this->Form->control('1.name');
  7710. $expected = [
  7711. 'label' => ['for' => '1-name'],
  7712. 'Name',
  7713. '/label',
  7714. 'input' => [
  7715. 'type' => 'text', 'name' => '1[name]',
  7716. 'id' => '1-name', 'maxlength' => '255'
  7717. ]
  7718. ];
  7719. $this->assertHtml($expected, $result);
  7720. }
  7721. /**
  7722. * testFormEnd method
  7723. *
  7724. * @return void
  7725. */
  7726. public function testFormEnd()
  7727. {
  7728. $this->assertEquals('</form>', $this->Form->end());
  7729. }
  7730. /**
  7731. * testMultiRecordForm method
  7732. *
  7733. * Test the generation of fields for a multi record form.
  7734. *
  7735. * @return void
  7736. */
  7737. public function testMultiRecordForm()
  7738. {
  7739. $this->loadFixtures('Articles', 'Comments');
  7740. $articles = $this->getTableLocator()->get('Articles');
  7741. $articles->hasMany('Comments');
  7742. $comment = new Entity(['comment' => 'Value']);
  7743. $article = new Article(['comments' => [$comment]]);
  7744. $this->Form->create([$article]);
  7745. $result = $this->Form->control('0.comments.1.comment');
  7746. //@codingStandardsIgnoreStart
  7747. $expected = [
  7748. 'div' => ['class' => 'input textarea'],
  7749. 'label' => ['for' => '0-comments-1-comment'],
  7750. 'Comment',
  7751. '/label',
  7752. 'textarea' => [
  7753. 'name',
  7754. 'id' => '0-comments-1-comment',
  7755. 'rows' => 5
  7756. ],
  7757. '/textarea',
  7758. '/div'
  7759. ];
  7760. //@codingStandardsIgnoreEnd
  7761. $this->assertHtml($expected, $result);
  7762. $result = $this->Form->control('0.comments.0.comment');
  7763. //@codingStandardsIgnoreStart
  7764. $expected = [
  7765. 'div' => ['class' => 'input textarea'],
  7766. 'label' => ['for' => '0-comments-0-comment'],
  7767. 'Comment',
  7768. '/label',
  7769. 'textarea' => [
  7770. 'name',
  7771. 'id' => '0-comments-0-comment',
  7772. 'rows' => 5
  7773. ],
  7774. 'Value',
  7775. '/textarea',
  7776. '/div'
  7777. ];
  7778. //@codingStandardsIgnoreEnd
  7779. $this->assertHtml($expected, $result);
  7780. $comment->setError('comment', ['Not valid']);
  7781. $result = $this->Form->control('0.comments.0.comment');
  7782. //@codingStandardsIgnoreStart
  7783. $expected = [
  7784. 'div' => ['class' => 'input textarea error'],
  7785. 'label' => ['for' => '0-comments-0-comment'],
  7786. 'Comment',
  7787. '/label',
  7788. 'textarea' => [
  7789. 'name',
  7790. 'class' => 'form-error',
  7791. 'id' => '0-comments-0-comment',
  7792. 'rows' => 5
  7793. ],
  7794. 'Value',
  7795. '/textarea',
  7796. ['div' => ['class' => 'error-message']],
  7797. 'Not valid',
  7798. '/div',
  7799. '/div'
  7800. ];
  7801. //@codingStandardsIgnoreEnd
  7802. $this->assertHtml($expected, $result);
  7803. $this->getTableLocator()->get('Comments')
  7804. ->getValidator('default')
  7805. ->allowEmpty('comment', false);
  7806. $result = $this->Form->control('0.comments.1.comment');
  7807. //@codingStandardsIgnoreStart
  7808. $expected = [
  7809. 'div' => ['class' => 'input textarea required'],
  7810. 'label' => ['for' => '0-comments-1-comment'],
  7811. 'Comment',
  7812. '/label',
  7813. 'textarea' => [
  7814. 'name',
  7815. 'required' => 'required',
  7816. 'id' => '0-comments-1-comment',
  7817. 'rows' => 5,
  7818. ],
  7819. '/textarea',
  7820. '/div'
  7821. ];
  7822. //@codingStandardsIgnoreEnd
  7823. $this->assertHtml($expected, $result);
  7824. }
  7825. /**
  7826. * testHtml5Controls method
  7827. *
  7828. * Test that some html5 inputs + FormHelper::__call() work.
  7829. *
  7830. * @return void
  7831. */
  7832. public function testHtml5Controls()
  7833. {
  7834. $result = $this->Form->email('User.email');
  7835. $expected = [
  7836. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7837. ];
  7838. $this->assertHtml($expected, $result);
  7839. $result = $this->Form->search('User.query');
  7840. $expected = [
  7841. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7842. ];
  7843. $this->assertHtml($expected, $result);
  7844. $result = $this->Form->search('User.query', ['value' => 'test']);
  7845. $expected = [
  7846. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7847. ];
  7848. $this->assertHtml($expected, $result);
  7849. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7850. $expected = [
  7851. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7852. ];
  7853. $this->assertHtml($expected, $result);
  7854. }
  7855. /**
  7856. * testHtml5ControlWithControl method
  7857. *
  7858. * Test accessing html5 inputs through control().
  7859. *
  7860. * @return void
  7861. */
  7862. public function testHtml5ControlWithControl()
  7863. {
  7864. $this->Form->create();
  7865. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7866. $result = $this->Form->control('website', [
  7867. 'type' => 'url',
  7868. 'val' => 'http://domain.tld',
  7869. 'label' => false
  7870. ]);
  7871. $expected = [
  7872. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7873. ];
  7874. $this->assertHtml($expected, $result);
  7875. }
  7876. /**
  7877. * testHtml5ControlException method
  7878. *
  7879. * Test errors when field name is missing.
  7880. *
  7881. * @return void
  7882. */
  7883. public function testHtml5ControlException()
  7884. {
  7885. $this->expectException(\Cake\Core\Exception\Exception::class);
  7886. $this->Form->email();
  7887. }
  7888. /**
  7889. * tests fields that are required use custom validation messages
  7890. *
  7891. * @return void
  7892. */
  7893. public function testHtml5ErrorMessage()
  7894. {
  7895. $this->Form->setConfig('autoSetCustomValidity', true);
  7896. $validator = (new \Cake\Validation\Validator())
  7897. ->requirePresence('email', true, 'Custom error message')
  7898. ->requirePresence('password')
  7899. ->alphaNumeric('password')
  7900. ->notBlank('phone');
  7901. $table = $this->getTableLocator()->get('Contacts', [
  7902. 'className' => __NAMESPACE__ . '\ContactsTable'
  7903. ]);
  7904. $table->setValidator('default', $validator);
  7905. $contact = new Entity();
  7906. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7907. $this->Form->setTemplates(['inputContainer' => '{{content}}']);
  7908. $result = $this->Form->control('password');
  7909. $expected = [
  7910. 'label' => ['for' => 'password'],
  7911. 'Password',
  7912. '/label',
  7913. 'input' => [
  7914. 'id' => 'password',
  7915. 'name' => 'password',
  7916. 'type' => 'password',
  7917. 'value' => '',
  7918. 'required' => 'required',
  7919. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7920. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;This field is required&#039;)',
  7921. ]
  7922. ];
  7923. $this->assertHtml($expected, $result);
  7924. $result = $this->Form->control('phone');
  7925. $expected = [
  7926. 'label' => ['for' => 'phone'],
  7927. 'Phone',
  7928. '/label',
  7929. 'input' => [
  7930. 'id' => 'phone',
  7931. 'name' => 'phone',
  7932. 'type' => 'tel',
  7933. 'value' => '',
  7934. 'maxlength' => 255,
  7935. 'required' => 'required',
  7936. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7937. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;This field cannot be left empty&#039;)',
  7938. ]
  7939. ];
  7940. $this->assertHtml($expected, $result);
  7941. $result = $this->Form->control('email');
  7942. $expected = [
  7943. 'label' => ['for' => 'email'],
  7944. 'Email',
  7945. '/label',
  7946. 'input' => [
  7947. 'id' => 'email',
  7948. 'name' => 'email',
  7949. 'type' => 'email',
  7950. 'value' => '',
  7951. 'maxlength' => 255,
  7952. 'required' => 'required',
  7953. 'oninput' => 'this.setCustomValidity(&#039;&#039;)',
  7954. 'oninvalid' => 'this.setCustomValidity(&#039;&#039;); if (!this.validity.valid) this.setCustomValidity(&#039;Custom error message&#039;)',
  7955. ]
  7956. ];
  7957. $this->assertHtml($expected, $result);
  7958. }
  7959. /**
  7960. * tests that custom validation messages are in templateVars
  7961. *
  7962. * @return void
  7963. */
  7964. public function testHtml5ErrorMessageInTemplateVars()
  7965. {
  7966. $validator = (new \Cake\Validation\Validator())
  7967. ->requirePresence('email', true, 'Custom error "message" & entities')
  7968. ->requirePresence('password')
  7969. ->alphaNumeric('password')
  7970. ->notBlank('phone');
  7971. $table = $this->getTableLocator()->get('Contacts', [
  7972. 'className' => __NAMESPACE__ . '\ContactsTable'
  7973. ]);
  7974. $table->setValidator('default', $validator);
  7975. $contact = new Entity();
  7976. $this->Form->create($contact, ['context' => ['table' => 'Contacts']]);
  7977. $this->Form->setTemplates([
  7978. 'input' => '<input type="{{type}}" name="{{name}}"{{attrs}} data-message="{{customValidityMessage}}" {{custom}}/>',
  7979. 'inputContainer' => '{{content}}'
  7980. ]);
  7981. $result = $this->Form->control('password');
  7982. $expected = [
  7983. 'label' => ['for' => 'password'],
  7984. 'Password',
  7985. '/label',
  7986. 'input' => [
  7987. 'id' => 'password',
  7988. 'name' => 'password',
  7989. 'type' => 'password',
  7990. 'value' => '',
  7991. 'required' => 'required',
  7992. 'data-message' => 'This field is required',
  7993. ]
  7994. ];
  7995. $this->assertHtml($expected, $result);
  7996. $result = $this->Form->control('phone');
  7997. $expected = [
  7998. 'label' => ['for' => 'phone'],
  7999. 'Phone',
  8000. '/label',
  8001. 'input' => [
  8002. 'id' => 'phone',
  8003. 'name' => 'phone',
  8004. 'type' => 'tel',
  8005. 'value' => '',
  8006. 'maxlength' => 255,
  8007. 'required' => 'required',
  8008. 'data-message' => 'This field cannot be left empty',
  8009. ],
  8010. ];
  8011. $this->assertHtml($expected, $result);
  8012. $result = $this->Form->control('email', [
  8013. 'templateVars' => [
  8014. 'custom' => 'data-custom="1"'
  8015. ]
  8016. ]);
  8017. $expected = [
  8018. 'label' => ['for' => 'email'],
  8019. 'Email',
  8020. '/label',
  8021. 'input' => [
  8022. 'id' => 'email',
  8023. 'name' => 'email',
  8024. 'type' => 'email',
  8025. 'value' => '',
  8026. 'maxlength' => 255,
  8027. 'required' => 'required',
  8028. 'data-message' => 'Custom error &quot;message&quot; &amp; entities',
  8029. 'data-custom' => '1',
  8030. ],
  8031. ];
  8032. $this->assertHtml($expected, $result);
  8033. }
  8034. /**
  8035. * testRequiredAttribute method
  8036. *
  8037. * Tests that formhelper sets required attributes.
  8038. *
  8039. * @return void
  8040. */
  8041. public function testRequiredAttribute()
  8042. {
  8043. $this->article['required'] = [
  8044. 'title' => true,
  8045. 'body' => false,
  8046. ];
  8047. $this->Form->create($this->article);
  8048. $result = $this->Form->control('title');
  8049. $expected = [
  8050. 'div' => ['class' => 'input text required'],
  8051. 'label' => ['for' => 'title'],
  8052. 'Title',
  8053. '/label',
  8054. 'input' => [
  8055. 'type' => 'text',
  8056. 'name' => 'title',
  8057. 'id' => 'title',
  8058. 'required' => 'required',
  8059. ],
  8060. '/div'
  8061. ];
  8062. $this->assertHtml($expected, $result);
  8063. $result = $this->Form->control('title', ['required' => false]);
  8064. $this->assertNotContains('required', $result);
  8065. $result = $this->Form->control('body');
  8066. $expected = [
  8067. 'div' => ['class' => 'input text'],
  8068. 'label' => ['for' => 'body'],
  8069. 'Body',
  8070. '/label',
  8071. 'input' => [
  8072. 'type' => 'text',
  8073. 'name' => 'body',
  8074. 'id' => 'body',
  8075. ],
  8076. '/div'
  8077. ];
  8078. $this->assertHtml($expected, $result);
  8079. $result = $this->Form->control('body', ['required' => true]);
  8080. $this->assertContains('required', $result);
  8081. }
  8082. /**
  8083. * testControlsNotNested method
  8084. *
  8085. * Tests that it is possible to put inputs outside of the label.
  8086. *
  8087. * @return void
  8088. */
  8089. public function testControlsNotNested()
  8090. {
  8091. $this->Form->setTemplates([
  8092. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  8093. 'formGroup' => '{{input}}{{label}}',
  8094. ]);
  8095. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  8096. $expected = [
  8097. 'div' => ['class' => 'input checkbox'],
  8098. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8099. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8100. 'label' => ['for' => 'foo'],
  8101. 'Foo',
  8102. '/label',
  8103. '/div'
  8104. ];
  8105. $this->assertHtml($expected, $result);
  8106. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  8107. $expected = [
  8108. 'div' => ['class' => 'input checkbox'],
  8109. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  8110. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  8111. '/div'
  8112. ];
  8113. $this->assertHtml($expected, $result);
  8114. $result = $this->Form->control('confirm', [
  8115. 'type' => 'radio',
  8116. 'options' => ['Y' => 'Yes', 'N' => 'No']
  8117. ]);
  8118. $expected = [
  8119. 'div' => ['class' => 'input radio'],
  8120. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  8121. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  8122. ['label' => ['for' => 'confirm-y']],
  8123. 'Yes',
  8124. '/label',
  8125. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  8126. ['label' => ['for' => 'confirm-n']],
  8127. 'No',
  8128. '/label',
  8129. '<label',
  8130. 'Confirm',
  8131. '/label',
  8132. '/div',
  8133. ];
  8134. $this->assertHtml($expected, $result);
  8135. $result = $this->Form->select('category', ['1', '2'], [
  8136. 'multiple' => 'checkbox',
  8137. 'name' => 'fish',
  8138. ]);
  8139. $expected = [
  8140. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  8141. ['div' => ['class' => 'checkbox']],
  8142. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  8143. ['label' => ['for' => 'fish-0']],
  8144. '1',
  8145. '/label',
  8146. '/div',
  8147. ['div' => ['class' => 'checkbox']],
  8148. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  8149. ['label' => ['for' => 'fish-1']],
  8150. '2',
  8151. '/label',
  8152. '/div'
  8153. ];
  8154. $this->assertHtml($expected, $result);
  8155. }
  8156. /**
  8157. * testControlContainerTemplates method
  8158. *
  8159. * Test that *Container templates are used by input.
  8160. *
  8161. * @return void
  8162. */
  8163. public function testControlContainerTemplates()
  8164. {
  8165. $this->Form->setTemplates([
  8166. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  8167. 'radioContainer' => '<div class="rad">{{content}}</div>',
  8168. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  8169. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  8170. ]);
  8171. $this->article['errors'] = [
  8172. 'Article' => ['published' => 'error message']
  8173. ];
  8174. $this->Form->create($this->article);
  8175. $result = $this->Form->control('accept', [
  8176. 'type' => 'checkbox'
  8177. ]);
  8178. $expected = [
  8179. 'div' => ['class' => 'check'],
  8180. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  8181. 'label' => ['for' => 'accept'],
  8182. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  8183. 'Accept',
  8184. '/label',
  8185. '/div'
  8186. ];
  8187. $this->assertHtml($expected, $result);
  8188. $result = $this->Form->control('accept', [
  8189. 'type' => 'radio',
  8190. 'options' => ['Y', 'N']
  8191. ]);
  8192. $this->assertContains('<div class="rad">', $result);
  8193. $result = $this->Form->control('Article.published', [
  8194. 'type' => 'radio',
  8195. 'options' => ['Y', 'N']
  8196. ]);
  8197. $this->assertContains('<div class="rad err">', $result);
  8198. $result = $this->Form->control('Article.created', [
  8199. 'type' => 'datetime',
  8200. ]);
  8201. $this->assertContains('<div class="dt">', $result);
  8202. }
  8203. /**
  8204. * testFormGroupTemplates method
  8205. *
  8206. * Test that *Container templates are used by input.
  8207. *
  8208. * @return void
  8209. */
  8210. public function testFormGroupTemplates()
  8211. {
  8212. $this->Form->setTemplates([
  8213. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  8214. ]);
  8215. $this->Form->create($this->article);
  8216. $result = $this->Form->control('accept', [
  8217. 'type' => 'radio',
  8218. 'options' => ['Y', 'N']
  8219. ]);
  8220. $this->assertContains('<div class="radio">', $result);
  8221. }
  8222. /**
  8223. * testResetTemplates method
  8224. *
  8225. * Test resetting templates.
  8226. *
  8227. * @return void
  8228. */
  8229. public function testResetTemplates()
  8230. {
  8231. $this->Form->setTemplates(['input' => '<input/>']);
  8232. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  8233. $this->Form->resetTemplates();
  8234. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  8235. }
  8236. /**
  8237. * testContext method
  8238. *
  8239. * Test the context method.
  8240. *
  8241. * @return void
  8242. */
  8243. public function testContext()
  8244. {
  8245. $result = $this->Form->context();
  8246. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  8247. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  8248. $this->assertSame($mock, $this->Form->context($mock));
  8249. $this->assertSame($mock, $this->Form->context());
  8250. }
  8251. /**
  8252. * testAutoDomId method
  8253. *
  8254. * @return void
  8255. */
  8256. public function testAutoDomId()
  8257. {
  8258. $result = $this->Form->text('field', ['id' => true]);
  8259. $expected = [
  8260. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  8261. ];
  8262. $this->assertHtml($expected, $result);
  8263. // Ensure id => doesn't cause problem when multiple inputs are generated.
  8264. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  8265. $expected = [
  8266. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  8267. ['label' => ['for' => 'field-0']],
  8268. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  8269. 'option A',
  8270. '/label',
  8271. ['label' => ['for' => 'field-1']],
  8272. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  8273. 'option B',
  8274. '/label',
  8275. ];
  8276. $this->assertHtml($expected, $result);
  8277. $result = $this->Form->select(
  8278. 'multi_field',
  8279. ['first', 'second'],
  8280. ['multiple' => 'checkbox', 'id' => true]
  8281. );
  8282. $expected = [
  8283. 'input' => [
  8284. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  8285. ],
  8286. ['div' => ['class' => 'checkbox']],
  8287. ['label' => ['for' => 'multi-field-0']],
  8288. ['input' => [
  8289. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8290. 'value' => '0', 'id' => 'multi-field-0'
  8291. ]],
  8292. 'first',
  8293. '/label',
  8294. '/div',
  8295. ['div' => ['class' => 'checkbox']],
  8296. ['label' => ['for' => 'multi-field-1']],
  8297. ['input' => [
  8298. 'type' => 'checkbox', 'name' => 'multi_field[]',
  8299. 'value' => '1', 'id' => 'multi-field-1'
  8300. ]],
  8301. 'second',
  8302. '/label',
  8303. '/div',
  8304. ];
  8305. $this->assertHtml($expected, $result);
  8306. }
  8307. /**
  8308. * Test the basic setters and getters for value sources
  8309. *
  8310. * @return void
  8311. */
  8312. public function testFormValueSourcesSettersGetters()
  8313. {
  8314. $this->View->setRequest($this->View->getRequest()
  8315. ->withData('id', '1')
  8316. ->withQueryParams(['id' => '2']));
  8317. $expected = ['context'];
  8318. $result = $this->Form->getValueSources();
  8319. $this->assertEquals($expected, $result);
  8320. $expected = ['query', 'data', 'context'];
  8321. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  8322. $result = $this->Form->getValueSources();
  8323. $this->assertEquals($expected, $result);
  8324. $this->Form->setValueSources(['context']);
  8325. $expected = '1';
  8326. $result = $this->Form->getSourceValue('id');
  8327. $this->assertEquals($expected, $result);
  8328. $this->Form->setValueSources('query');
  8329. $expected = ['query'];
  8330. $result = $this->Form->getValueSources();
  8331. $this->assertEquals($expected, $result);
  8332. $expected = '2';
  8333. $result = $this->Form->getSourceValue('id');
  8334. $this->assertEquals($expected, $result);
  8335. $this->Form->setValueSources(['data']);
  8336. $expected = '1';
  8337. $result = $this->Form->getSourceValue('id');
  8338. $this->assertEquals($expected, $result);
  8339. $this->Form->setValueSources(['query', 'data']);
  8340. $expected = '2';
  8341. $result = $this->Form->getSourceValue('id');
  8342. $this->assertEquals($expected, $result);
  8343. }
  8344. /**
  8345. * Tests the different input rendering values based on sources values switching
  8346. *
  8347. * @return void
  8348. */
  8349. public function testFormValueSourcesSingleSwitchRendering()
  8350. {
  8351. $this->loadFixtures('Articles');
  8352. $articles = $this->getTableLocator()->get('Articles');
  8353. $article = new Article();
  8354. $articles->patchEntity($article, ['id' => '3']);
  8355. $this->Form->create($article);
  8356. $this->Form->setValueSources(['context']);
  8357. $result = $this->Form->control('id');
  8358. $expected = [
  8359. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8360. ];
  8361. $this->assertHtml($expected, $result);
  8362. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => 5]));
  8363. $this->Form->setValueSources(['query']);
  8364. $result = $this->Form->control('id');
  8365. $expected = [
  8366. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8367. ];
  8368. $this->assertHtml($expected, $result);
  8369. $this->View->setRequest($this->View->getRequest()
  8370. ->withQueryParams(['id' => '5a'])
  8371. ->withData('id', '5b'));
  8372. $this->Form->setValueSources(['context']);
  8373. $this->Form->create($article);
  8374. $result = $this->Form->control('id');
  8375. $expected = [
  8376. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8377. ];
  8378. $this->assertHtml($expected, $result);
  8379. $this->Form->setValueSources(['data']);
  8380. $this->Form->create($article);
  8381. $result = $this->Form->control('id');
  8382. $expected = [
  8383. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  8384. ];
  8385. $this->assertHtml($expected, $result);
  8386. $this->Form->setValueSources(['query']);
  8387. $result = $this->Form->control('id');
  8388. $expected = [
  8389. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  8390. ];
  8391. $this->assertHtml($expected, $result);
  8392. }
  8393. /**
  8394. * Tests the different input rendering values based on sources values switching while supplying
  8395. * an entity (base context) and multiple sources (such as data, query)
  8396. *
  8397. * @return void
  8398. */
  8399. public function testFormValueSourcesListSwitchRendering()
  8400. {
  8401. $this->loadFixtures('Articles');
  8402. $articles = $this->getTableLocator()->get('Articles');
  8403. $article = new Article();
  8404. $articles->patchEntity($article, ['id' => '3']);
  8405. $this->View->setRequest($this->View->getRequest()->withQueryParams(['id' => '9']));
  8406. $this->Form->create($article);
  8407. $this->Form->setValueSources(['context', 'query']);
  8408. $result = $this->Form->control('id');
  8409. $expected = [
  8410. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  8411. ];
  8412. $this->assertHtml($expected, $result);
  8413. $this->Form->setValueSources(['query', 'context']);
  8414. $result = $this->Form->control('id');
  8415. $expected = [
  8416. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8417. ];
  8418. $this->assertHtml($expected, $result);
  8419. $this->Form->setValueSources(['data', 'query', 'context']);
  8420. $result = $this->Form->control('id');
  8421. $expected = [
  8422. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  8423. ];
  8424. $this->assertHtml($expected, $result);
  8425. $this->View->setRequest($this->View->getRequest()
  8426. ->withData('id', '8')
  8427. ->withQueryParams(['id' => '9']));
  8428. $this->Form->setValueSources(['data', 'query', 'context']);
  8429. $result = $this->Form->control('id');
  8430. $expected = [
  8431. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  8432. ];
  8433. $this->assertHtml($expected, $result);
  8434. }
  8435. /**
  8436. * Test the different form input renderings based on values sources switchings through form options
  8437. *
  8438. * @return void
  8439. */
  8440. public function testFormValueSourcesSwitchViaOptionsRendering()
  8441. {
  8442. $this->loadFixtures('Articles');
  8443. $articles = $this->getTableLocator()->get('Articles');
  8444. $article = new Article();
  8445. $articles->patchEntity($article, ['id' => '3']);
  8446. $this->View->setRequest(
  8447. $this->View->getRequest()->withData('id', 4)->withQueryParams(['id' => 5])
  8448. );
  8449. $this->Form->create($article, ['valueSources' => 'query']);
  8450. $result = $this->Form->control('id');
  8451. $expected = [
  8452. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8453. ];
  8454. $this->assertHtml($expected, $result);
  8455. $result = $this->Form->getSourceValue('id');
  8456. $this->assertEquals('5', $result);
  8457. $this->Form->setValueSources(['context']);
  8458. $this->Form->create($article, ['valueSources' => 'query']);
  8459. $result = $this->Form->control('id');
  8460. $expected = [
  8461. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  8462. ];
  8463. $this->assertHtml($expected, $result);
  8464. $result = $this->Form->getSourceValue('id');
  8465. $this->assertEquals('5', $result);
  8466. $this->Form->setValueSources(['query']);
  8467. $this->Form->create($article, ['valueSources' => 'data']);
  8468. $result = $this->Form->control('id');
  8469. $expected = [
  8470. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8471. ];
  8472. $this->assertHtml($expected, $result);
  8473. $result = $this->Form->getSourceValue('id');
  8474. $this->assertEquals('4', $result);
  8475. $this->Form->setValueSources(['query']);
  8476. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8477. $result = $this->Form->control('id');
  8478. $expected = [
  8479. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8480. ];
  8481. $this->assertHtml($expected, $result);
  8482. $result = $this->Form->getSourceValue('id');
  8483. $this->assertEquals('4', $result);
  8484. }
  8485. /**
  8486. * Test the different form input renderings based on values sources switchings through form options
  8487. *
  8488. * @return void
  8489. */
  8490. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8491. {
  8492. $this->loadFixtures('Articles');
  8493. $articles = $this->getTableLocator()->get('Articles');
  8494. $article = new Article();
  8495. $articles->patchEntity($article, ['id' => '3']);
  8496. $this->View->setRequest(
  8497. $this->View->getRequest()->withData('id', 10)->withQueryParams(['id' => 11])
  8498. );
  8499. $this->Form->setValueSources(['context'])
  8500. ->create($article, ['valueSources' => ['query', 'data']]);
  8501. $result = $this->Form->control('id');
  8502. $expected = [
  8503. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8504. ];
  8505. $this->assertHtml($expected, $result);
  8506. $result = $this->Form->getSourceValue('id');
  8507. $this->assertEquals('11', $result);
  8508. $this->View->setRequest($this->View->getRequest()->withQueryParams([]));
  8509. $this->Form->setValueSources(['context'])
  8510. ->create($article, ['valueSources' => ['query', 'data']]);
  8511. $result = $this->Form->control('id');
  8512. $expected = [
  8513. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8514. ];
  8515. $this->assertHtml($expected, $result);
  8516. $result = $this->Form->getSourceValue('id');
  8517. $this->assertEquals('10', $result);
  8518. }
  8519. /**
  8520. * Test the different form values sources resetting through From::end();
  8521. *
  8522. * @return void
  8523. */
  8524. public function testFormValueSourcesResetViaEnd()
  8525. {
  8526. $expected = ['context'];
  8527. $result = $this->Form->getValueSources();
  8528. $this->assertEquals($expected, $result);
  8529. $expected = ['query', 'context', 'data'];
  8530. $this->Form->setValueSources(['query', 'context', 'data']);
  8531. $result = $this->Form->getValueSources();
  8532. $this->assertEquals($expected, $result);
  8533. $this->Form->create();
  8534. $result = $this->Form->getValueSources();
  8535. $this->assertEquals($expected, $result);
  8536. $this->Form->end();
  8537. $result = $this->Form->getValueSources();
  8538. $this->assertEquals(['context'], $result);
  8539. }
  8540. /**
  8541. * Test sources values defaults handling
  8542. *
  8543. * @return void
  8544. */
  8545. public function testFormValueSourcesDefaults()
  8546. {
  8547. $this->View->setRequest(
  8548. $this->View->getRequest()->withQueryParams(['password' => 'open Sesame'])
  8549. );
  8550. $this->Form->create();
  8551. $result = $this->Form->password('password');
  8552. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8553. $this->assertHtml($expected, $result);
  8554. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8555. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8556. $this->assertHtml($expected, $result);
  8557. $this->Form->setValueSources('query');
  8558. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8559. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8560. $this->assertHtml($expected, $result);
  8561. $this->Form->setValueSources('data');
  8562. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8563. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8564. $this->assertHtml($expected, $result);
  8565. }
  8566. /**
  8567. * Test sources values schema defaults handling
  8568. *
  8569. * @return void
  8570. */
  8571. public function testSourcesValueDoesntExistPassThrough()
  8572. {
  8573. $this->View->setRequest($this->View->getRequest()->withQueryParams(['category' => 'sesame-cookies']));
  8574. $articles = $this->getTableLocator()->get('Articles');
  8575. $entity = $articles->newEntity();
  8576. $this->Form->create($entity);
  8577. $this->Form->setValueSources(['query', 'context']);
  8578. $result = $this->Form->getSourceValue('category');
  8579. $this->assertEquals('sesame-cookies', $result);
  8580. $this->Form->setValueSources(['context', 'query']);
  8581. $result = $this->Form->getSourceValue('category');
  8582. $this->assertEquals('sesame-cookies', $result);
  8583. }
  8584. /**
  8585. * testNestedLabelInput method
  8586. *
  8587. * Test the `nestedInput` parameter
  8588. *
  8589. * @group deprecated
  8590. * @return void
  8591. */
  8592. public function testNestedLabelInput()
  8593. {
  8594. $this->deprecated(function () {
  8595. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8596. $expected = [
  8597. 'div' => ['class' => 'input text'],
  8598. 'label' => ['for' => 'foo'],
  8599. ['input' => [
  8600. 'type' => 'text',
  8601. 'name' => 'foo',
  8602. 'id' => 'foo'
  8603. ]],
  8604. 'Foo',
  8605. '/label',
  8606. '/div'
  8607. ];
  8608. $this->assertHtml($expected, $result);
  8609. });
  8610. }
  8611. /**
  8612. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8613. *
  8614. * This test makes sure `false` excludes the label from the render
  8615. *
  8616. * @return void
  8617. */
  8618. public function testControlLabelManipulationDisableLabels()
  8619. {
  8620. $result = $this->Form->control('test', [
  8621. 'type' => 'radio',
  8622. 'options' => ['A', 'B'],
  8623. 'labelOptions' => false
  8624. ]);
  8625. $expected = [
  8626. ['div' => ['class' => 'input radio']],
  8627. '<label',
  8628. 'Test',
  8629. '/label',
  8630. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8631. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8632. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8633. '/div',
  8634. ];
  8635. $this->assertHtml($expected, $result);
  8636. $result = $this->Form->control('checkbox1', [
  8637. 'label' => 'My checkboxes',
  8638. 'multiple' => 'checkbox',
  8639. 'type' => 'select',
  8640. 'options' => [
  8641. ['text' => 'First Checkbox', 'value' => 1],
  8642. ['text' => 'Second Checkbox', 'value' => 2]
  8643. ],
  8644. 'labelOptions' => false
  8645. ]);
  8646. $expected = [
  8647. ['div' => ['class' => 'input select']],
  8648. ['label' => ['for' => 'checkbox1']],
  8649. 'My checkboxes',
  8650. '/label',
  8651. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8652. ['div' => ['class' => 'checkbox']],
  8653. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8654. '/div',
  8655. ['div' => ['class' => 'checkbox']],
  8656. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8657. '/div',
  8658. '/div'
  8659. ];
  8660. $this->assertHtml($expected, $result);
  8661. }
  8662. /**
  8663. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8664. *
  8665. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8666. * added to the class if checked.
  8667. *
  8668. * Also checks to make sure any custom attributes are rendered correctly
  8669. *
  8670. * @return void
  8671. */
  8672. public function testControlLabelManipulationRadios()
  8673. {
  8674. $result = $this->Form->control('test', [
  8675. 'type' => 'radio',
  8676. 'options' => ['A', 'B'],
  8677. 'labelOptions' => ['class' => 'custom-class']
  8678. ]);
  8679. $expected = [
  8680. ['div' => ['class' => 'input radio']],
  8681. '<label',
  8682. 'Test',
  8683. '/label',
  8684. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8685. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8686. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8687. 'A',
  8688. '/label',
  8689. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8690. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8691. 'B',
  8692. '/label',
  8693. '/div',
  8694. ];
  8695. $this->assertHtml($expected, $result);
  8696. $result = $this->Form->control('test', [
  8697. 'type' => 'radio',
  8698. 'options' => ['A', 'B'],
  8699. 'value' => 1,
  8700. 'labelOptions' => ['class' => 'custom-class']
  8701. ]);
  8702. $expected = [
  8703. ['div' => ['class' => 'input radio']],
  8704. '<label',
  8705. 'Test',
  8706. '/label',
  8707. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8708. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8709. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8710. 'A',
  8711. '/label',
  8712. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8713. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8714. 'B',
  8715. '/label',
  8716. '/div',
  8717. ];
  8718. $this->assertHtml($expected, $result);
  8719. $result = $this->Form->control('test', [
  8720. 'type' => 'radio',
  8721. 'options' => ['A', 'B'],
  8722. 'value' => 1,
  8723. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8724. ]);
  8725. $expected = [
  8726. ['div' => ['class' => 'input radio']],
  8727. '<label',
  8728. 'Test',
  8729. '/label',
  8730. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8731. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8732. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8733. 'A',
  8734. '/label',
  8735. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8736. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8737. 'B',
  8738. '/label',
  8739. '/div',
  8740. ];
  8741. $this->assertHtml($expected, $result);
  8742. $result = $this->Form->radio('test', ['A', 'B'], [
  8743. 'label' => [
  8744. 'class' => ['custom-class', 'another-class'],
  8745. 'data-name' => 'bob'
  8746. ],
  8747. 'value' => 1
  8748. ]);
  8749. $expected = [
  8750. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8751. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8752. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8753. 'A',
  8754. '/label',
  8755. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8756. ['input' => [
  8757. 'type' => 'radio',
  8758. 'name' => 'test',
  8759. 'value' => '1',
  8760. 'id' => 'test-1',
  8761. 'checked' => 'checked'
  8762. ]],
  8763. 'B',
  8764. '/label',
  8765. ];
  8766. $this->assertHtml($expected, $result);
  8767. }
  8768. /**
  8769. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8770. *
  8771. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8772. * added to the class if checked.
  8773. *
  8774. * Also checks to make sure any custom attributes are rendered correctly
  8775. *
  8776. * @return void
  8777. */
  8778. public function testControlLabelManipulationCheckboxes()
  8779. {
  8780. $result = $this->Form->control('checkbox1', [
  8781. 'label' => 'My checkboxes',
  8782. 'multiple' => 'checkbox',
  8783. 'type' => 'select',
  8784. 'options' => [
  8785. ['text' => 'First Checkbox', 'value' => 1],
  8786. ['text' => 'Second Checkbox', 'value' => 2]
  8787. ],
  8788. 'labelOptions' => ['class' => 'custom-class'],
  8789. 'value' => ['1']
  8790. ]);
  8791. $expected = [
  8792. ['div' => ['class' => 'input select']],
  8793. ['label' => ['for' => 'checkbox1']],
  8794. 'My checkboxes',
  8795. '/label',
  8796. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8797. ['div' => ['class' => 'checkbox']],
  8798. ['label' => [
  8799. 'class' => 'custom-class selected',
  8800. 'for' => 'checkbox1-1'
  8801. ]],
  8802. ['input' => [
  8803. 'type' => 'checkbox',
  8804. 'name' => 'checkbox1[]',
  8805. 'value' => '1',
  8806. 'id' => 'checkbox1-1',
  8807. 'checked' => 'checked'
  8808. ]],
  8809. 'First Checkbox',
  8810. '/label',
  8811. '/div',
  8812. ['div' => ['class' => 'checkbox']],
  8813. ['label' => [
  8814. 'class' => 'custom-class',
  8815. 'for' => 'checkbox1-2'
  8816. ]],
  8817. ['input' => [
  8818. 'type' => 'checkbox',
  8819. 'name' => 'checkbox1[]',
  8820. 'value' => '2',
  8821. 'id' => 'checkbox1-2'
  8822. ]],
  8823. 'Second Checkbox',
  8824. '/label',
  8825. '/div',
  8826. '/div'
  8827. ];
  8828. $this->assertHtml($expected, $result);
  8829. $result = $this->Form->control('checkbox1', [
  8830. 'label' => 'My checkboxes',
  8831. 'multiple' => 'checkbox',
  8832. 'type' => 'select',
  8833. 'options' => [
  8834. ['text' => 'First Checkbox', 'value' => 1],
  8835. ['text' => 'Second Checkbox', 'value' => 2]
  8836. ],
  8837. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8838. 'value' => ['1']
  8839. ]);
  8840. $expected = [
  8841. ['div' => ['class' => 'input select']],
  8842. ['label' => ['for' => 'checkbox1']],
  8843. 'My checkboxes',
  8844. '/label',
  8845. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8846. ['div' => ['class' => 'checkbox']],
  8847. ['label' => [
  8848. 'class' => 'custom-class another-class selected',
  8849. 'data-name' => 'bob',
  8850. 'for' => 'checkbox1-1'
  8851. ]],
  8852. ['input' => [
  8853. 'type' => 'checkbox',
  8854. 'name' => 'checkbox1[]',
  8855. 'value' => '1',
  8856. 'id' => 'checkbox1-1',
  8857. 'checked' => 'checked'
  8858. ]],
  8859. 'First Checkbox',
  8860. '/label',
  8861. '/div',
  8862. ['div' => ['class' => 'checkbox']],
  8863. ['label' => [
  8864. 'class' => 'custom-class another-class',
  8865. 'data-name' => 'bob',
  8866. 'for' => 'checkbox1-2'
  8867. ]],
  8868. ['input' => [
  8869. 'type' => 'checkbox',
  8870. 'name' => 'checkbox1[]',
  8871. 'value' => '2',
  8872. 'id' => 'checkbox1-2'
  8873. ]],
  8874. 'Second Checkbox',
  8875. '/label',
  8876. '/div',
  8877. '/div'
  8878. ];
  8879. $this->assertHtml($expected, $result);
  8880. }
  8881. /**
  8882. * testControlMaxLengthArrayContext method
  8883. *
  8884. * Test control() with maxlength attribute in Array Context.
  8885. *
  8886. * @return void
  8887. */
  8888. public function testControlMaxLengthArrayContext()
  8889. {
  8890. $this->article['schema'] = [
  8891. 'title' => ['length' => 10]
  8892. ];
  8893. $this->Form->create($this->article);
  8894. $result = $this->Form->control('title');
  8895. $expected = [
  8896. 'div' => ['class'],
  8897. 'label' => ['for'],
  8898. 'Title',
  8899. '/label',
  8900. 'input' => [
  8901. 'id',
  8902. 'name' => 'title',
  8903. 'type' => 'text',
  8904. 'required' => 'required',
  8905. 'maxlength' => 10,
  8906. ],
  8907. '/div',
  8908. ];
  8909. $this->assertHtml($expected, $result);
  8910. }
  8911. /**
  8912. * testControlMaxLengthEntityContext method
  8913. *
  8914. * Test control() with maxlength attribute in Entity Context.
  8915. *
  8916. * @return void
  8917. */
  8918. public function testControlMaxLengthEntityContext()
  8919. {
  8920. $this->article['schema']['title']['length'] = 45;
  8921. $validator = new Validator();
  8922. $validator->maxLength('title', 10);
  8923. $article = new EntityContext(
  8924. new ServerRequest(),
  8925. [
  8926. 'entity' => new Entity($this->article),
  8927. 'table' => new Table([
  8928. 'schema' => $this->article['schema'],
  8929. 'validator' => $validator
  8930. ])
  8931. ]
  8932. );
  8933. $this->Form->create($article);
  8934. $result = $this->Form->control('title');
  8935. $expected = [
  8936. 'div' => ['class'],
  8937. 'label' => ['for'],
  8938. 'Title',
  8939. '/label',
  8940. 'input' => [
  8941. 'id',
  8942. 'name' => 'title',
  8943. 'type' => 'text',
  8944. 'required' => 'required',
  8945. 'maxlength' => 10,
  8946. ],
  8947. '/div',
  8948. ];
  8949. $this->assertHtml($expected, $result);
  8950. $this->article['schema']['title']['length'] = 45;
  8951. $validator = new Validator();
  8952. $validator->maxLength('title', 55);
  8953. $article = new EntityContext(
  8954. new ServerRequest(),
  8955. [
  8956. 'entity' => new Entity($this->article),
  8957. 'table' => new Table([
  8958. 'schema' => $this->article['schema'],
  8959. 'validator' => $validator
  8960. ])
  8961. ]
  8962. );
  8963. $this->Form->create($article);
  8964. $result = $this->Form->control('title');
  8965. $expected = [
  8966. 'div' => ['class'],
  8967. 'label' => ['for'],
  8968. 'Title',
  8969. '/label',
  8970. 'input' => [
  8971. 'id',
  8972. 'name' => 'title',
  8973. 'type' => 'text',
  8974. 'required' => 'required',
  8975. 'maxlength' => 55, // Length set in validator should take precedence over schema.
  8976. ],
  8977. '/div',
  8978. ];
  8979. $this->assertHtml($expected, $result);
  8980. $this->article['schema']['title']['length'] = 45;
  8981. $validator = new Validator();
  8982. $validator->maxLength('title', 55);
  8983. $article = new EntityContext(
  8984. new ServerRequest(),
  8985. [
  8986. 'entity' => new Entity($this->article),
  8987. 'table' => new Table([
  8988. 'schema' => $this->article['schema'],
  8989. 'validator' => $validator
  8990. ])
  8991. ]
  8992. );
  8993. $this->Form->create($article);
  8994. $result = $this->Form->control('title', ['maxlength' => 10]);
  8995. $expected = [
  8996. 'div' => ['class'],
  8997. 'label' => ['for'],
  8998. 'Title',
  8999. '/label',
  9000. 'input' => [
  9001. 'id',
  9002. 'name' => 'title',
  9003. 'type' => 'text',
  9004. 'required' => 'required',
  9005. 'maxlength' => 10, // Length set in options should take highest precedence.
  9006. ],
  9007. '/div',
  9008. ];
  9009. $this->assertHtml($expected, $result);
  9010. }
  9011. /**
  9012. * testControlMinMaxLengthEntityContext method
  9013. *
  9014. * Test control() with maxlength attribute in Entity Context sets the minimum val.
  9015. *
  9016. * @return void
  9017. */
  9018. public function testControlMinMaxLengthEntityContext()
  9019. {
  9020. $validator = new Validator();
  9021. $validator->maxLength('title', 10);
  9022. $article = new EntityContext(
  9023. new ServerRequest(),
  9024. [
  9025. 'entity' => new Entity($this->article),
  9026. 'table' => new Table([
  9027. 'schema' => $this->article['schema'],
  9028. 'validator' => $validator
  9029. ])
  9030. ]
  9031. );
  9032. $this->Form->create($article);
  9033. $result = $this->Form->control('title');
  9034. $expected = [
  9035. 'div' => ['class'],
  9036. 'label' => ['for'],
  9037. 'Title',
  9038. '/label',
  9039. 'input' => [
  9040. 'id',
  9041. 'name' => 'title',
  9042. 'type' => 'text',
  9043. 'required' => 'required',
  9044. 'maxlength' => 10,
  9045. ],
  9046. '/div',
  9047. ];
  9048. $this->assertHtml($expected, $result);
  9049. }
  9050. /**
  9051. * testControlMaxLengthFormContext method
  9052. *
  9053. * Test control() with maxlength attribute in Form Context.
  9054. *
  9055. * @return void
  9056. */
  9057. public function testControlMaxLengthFormContext()
  9058. {
  9059. $validator = new Validator();
  9060. $validator->maxLength('title', 10);
  9061. $form = new Form();
  9062. $form->setValidator('default', $validator);
  9063. $this->Form->create($form);
  9064. $result = $this->Form->control('title');
  9065. $expected = [
  9066. 'div' => ['class'],
  9067. 'label' => ['for'],
  9068. 'Title',
  9069. '/label',
  9070. 'input' => [
  9071. 'id',
  9072. 'name' => 'title',
  9073. 'type' => 'text',
  9074. 'required' => 'required',
  9075. 'maxlength' => 10,
  9076. ],
  9077. '/div',
  9078. ];
  9079. $this->assertHtml($expected, $result);
  9080. }
  9081. }