FormHelperTest.php 298 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Core\Configure;
  18. use Cake\Form\Form;
  19. use Cake\Network\Request;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Table;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\Security;
  26. use Cake\View\Helper\FormHelper;
  27. use Cake\View\View;
  28. /**
  29. * Test stub.
  30. */
  31. class Article extends Entity
  32. {
  33. }
  34. /**
  35. * Contact class
  36. */
  37. class ContactsTable extends Table
  38. {
  39. /**
  40. * Default schema
  41. *
  42. * @var array
  43. */
  44. protected $_schema = [
  45. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  46. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  47. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  48. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  49. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  50. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  51. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  52. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  53. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  54. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  55. ];
  56. /**
  57. * Initializes the schema
  58. *
  59. * @return void
  60. */
  61. public function initialize(array $config)
  62. {
  63. $this->schema($this->_schema);
  64. }
  65. }
  66. /**
  67. * ValidateUser class
  68. */
  69. class ValidateUsersTable extends Table
  70. {
  71. /**
  72. * schema method
  73. *
  74. * @var array
  75. */
  76. protected $_schema = [
  77. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  78. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  79. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  80. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  81. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  82. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  83. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  84. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  85. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  86. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  87. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  88. ];
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config)
  95. {
  96. $this->schema($this->_schema);
  97. }
  98. }
  99. /**
  100. * FormHelperTest class
  101. *
  102. * @property \Cake\View\Helper\FormHelper $Form
  103. */
  104. class FormHelperTest extends TestCase
  105. {
  106. /**
  107. * Fixtures to be used
  108. *
  109. * @var array
  110. */
  111. public $fixtures = ['core.articles', 'core.comments'];
  112. /**
  113. * Do not load the fixtures by default
  114. *
  115. * @var bool
  116. */
  117. public $autoFixtures = false;
  118. /**
  119. * setUp method
  120. *
  121. * @return void
  122. */
  123. public function setUp()
  124. {
  125. parent::setUp();
  126. Configure::write('Config.language', 'eng');
  127. Configure::write('App.base', '');
  128. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  129. $this->View = new View();
  130. $this->Form = new FormHelper($this->View);
  131. $request = new Request([
  132. 'webroot' => '',
  133. 'base' => '',
  134. 'url' => '/articles/add',
  135. 'params' => [
  136. 'controller' => 'articles',
  137. 'action' => 'add',
  138. ]
  139. ]);
  140. $this->Form->Url->request = $this->Form->request = $request;
  141. $this->dateRegex = [
  142. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  143. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  144. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  145. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  146. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  147. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  148. ];
  149. $this->article = [
  150. 'schema' => [
  151. 'id' => ['type' => 'integer'],
  152. 'author_id' => ['type' => 'integer', 'null' => true],
  153. 'title' => ['type' => 'string', 'null' => true],
  154. 'body' => 'text',
  155. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  156. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  157. ],
  158. 'required' => [
  159. 'author_id' => true,
  160. 'title' => true,
  161. ]
  162. ];
  163. Security::salt('foo!');
  164. Router::connect('/:controller', ['action' => 'index']);
  165. Router::connect('/:controller/:action/*');
  166. }
  167. /**
  168. * tearDown method
  169. *
  170. * @return void
  171. */
  172. public function tearDown()
  173. {
  174. parent::tearDown();
  175. unset($this->Form, $this->Controller, $this->View);
  176. TableRegistry::clear();
  177. }
  178. /**
  179. * Test construct() with the templates option.
  180. *
  181. * @return void
  182. */
  183. public function testConstructTemplatesFile()
  184. {
  185. $helper = new FormHelper($this->View, [
  186. 'templates' => 'htmlhelper_tags'
  187. ]);
  188. $result = $helper->control('name');
  189. $this->assertContains('<input', $result);
  190. }
  191. /**
  192. * Test that when specifying custom widgets the config array for that widget
  193. * is overwritten instead of merged.
  194. *
  195. * @return void
  196. */
  197. public function testConstructWithWidgets()
  198. {
  199. $config = [
  200. 'widgets' => [
  201. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  202. ]
  203. ];
  204. $helper = new FormHelper($this->View, $config);
  205. $registry = $helper->widgetRegistry();
  206. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  207. }
  208. /**
  209. * Test that when specifying custom widgets config file and it should be
  210. * added to widgets array. WidgetRegistry will load widgets in constructor.
  211. *
  212. * @return void
  213. */
  214. public function testConstructWithWidgetsConfig()
  215. {
  216. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  217. $registry = $helper->widgetRegistry();
  218. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  219. }
  220. /**
  221. * Test registering a new widget class and rendering it.
  222. *
  223. * @return void
  224. */
  225. public function testAddWidgetAndRenderWidget()
  226. {
  227. $data = [
  228. 'val' => 1
  229. ];
  230. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  231. $this->Form->addWidget('test', $mock);
  232. $mock->expects($this->once())
  233. ->method('render')
  234. ->with($data)
  235. ->will($this->returnValue('HTML'));
  236. $result = $this->Form->widget('test', $data);
  237. $this->assertEquals('HTML', $result);
  238. }
  239. /**
  240. * Test that secureFields() of widget is called after calling render(),
  241. * not before.
  242. *
  243. * @return void
  244. */
  245. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  246. {
  247. $data = [
  248. 'val' => 1,
  249. 'name' => 'test'
  250. ];
  251. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  252. $this->Form->addWidget('test', $mock);
  253. $mock->expects($this->at(0))
  254. ->method('render')
  255. ->with($data)
  256. ->will($this->returnValue('HTML'));
  257. $mock->expects($this->at(1))
  258. ->method('secureFields')
  259. ->with($data)
  260. ->will($this->returnValue(['test']));
  261. $result = $this->Form->widget('test', $data + ['secure' => true]);
  262. $this->assertEquals('HTML', $result);
  263. }
  264. /**
  265. * Test that empty string is not added to secure fields list when
  266. * rendering input widget without name.
  267. *
  268. * @return void
  269. */
  270. public function testRenderingWidgetWithEmptyName()
  271. {
  272. $this->assertEquals([], $this->Form->fields);
  273. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  274. $this->assertEquals('<select name=""></select>', $result);
  275. $this->assertEquals([], $this->Form->fields);
  276. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  277. $this->assertEquals('<select name="0"></select>', $result);
  278. $this->assertEquals(['0'], $this->Form->fields);
  279. }
  280. /**
  281. * Test registering an invalid widget class.
  282. *
  283. * @expectedException \RuntimeException
  284. * @return void
  285. */
  286. public function testAddWidgetInvalid()
  287. {
  288. $mock = new \StdClass();
  289. $this->Form->addWidget('test', $mock);
  290. $this->Form->widget('test');
  291. }
  292. /**
  293. * Test adding a new context class.
  294. *
  295. * @return void
  296. */
  297. public function testAddContextProvider()
  298. {
  299. $context = 'My data';
  300. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  301. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  302. $this->assertInstanceOf('Cake\Network\Request', $request);
  303. $this->assertEquals($context, $data['entity']);
  304. return $stub;
  305. });
  306. $this->Form->create($context);
  307. $result = $this->Form->context();
  308. $this->assertSame($stub, $result);
  309. }
  310. /**
  311. * Test replacing a context class.
  312. *
  313. * @return void
  314. */
  315. public function testAddContextProviderReplace()
  316. {
  317. $entity = new Article();
  318. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  319. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  320. return $stub;
  321. });
  322. $this->Form->create($entity);
  323. $result = $this->Form->context();
  324. $this->assertSame($stub, $result);
  325. }
  326. /**
  327. * Test overriding a context class.
  328. *
  329. * @return void
  330. */
  331. public function testAddContextProviderAdd()
  332. {
  333. $entity = new Article();
  334. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  335. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  336. if ($data['entity'] instanceof Entity) {
  337. return $stub;
  338. }
  339. });
  340. $this->Form->create($entity);
  341. $result = $this->Form->context();
  342. $this->assertSame($stub, $result);
  343. }
  344. /**
  345. * Test adding an invalid context class.
  346. *
  347. * @expectedException \RuntimeException
  348. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  349. * @return void
  350. */
  351. public function testAddContextProviderInvalid()
  352. {
  353. $context = 'My data';
  354. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  355. return new \StdClass();
  356. });
  357. $this->Form->create($context);
  358. }
  359. /**
  360. * Provides context options for create().
  361. *
  362. * @return array
  363. */
  364. public function contextSelectionProvider()
  365. {
  366. $entity = new Article();
  367. $collection = new Collection([$entity]);
  368. $emptyCollection = new Collection([]);
  369. $arrayObject = new \ArrayObject([]);
  370. $data = [
  371. 'schema' => [
  372. 'title' => ['type' => 'string']
  373. ]
  374. ];
  375. $form = new Form();
  376. return [
  377. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  378. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  379. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  380. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  381. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  382. 'form' => [$form, 'Cake\View\Form\FormContext'],
  383. 'none' => [null, 'Cake\View\Form\NullContext'],
  384. 'false' => [false, 'Cake\View\Form\NullContext'],
  385. ];
  386. }
  387. /**
  388. * Test default context selection in create()
  389. *
  390. * @dataProvider contextSelectionProvider
  391. * @return void
  392. */
  393. public function testCreateContextSelectionBuiltIn($data, $class)
  394. {
  395. $this->loadFixtures('Articles');
  396. $this->Form->create($data);
  397. $this->assertInstanceOf($class, $this->Form->context());
  398. }
  399. /**
  400. * Data provider for type option.
  401. *
  402. * @return array
  403. */
  404. public static function requestTypeProvider()
  405. {
  406. return [
  407. // type, method, override
  408. ['post', 'post', 'POST'],
  409. ['put', 'post', 'PUT'],
  410. ['patch', 'post', 'PATCH'],
  411. ['delete', 'post', 'DELETE'],
  412. ];
  413. }
  414. /**
  415. * Test creating file forms.
  416. *
  417. * @return void
  418. */
  419. public function testCreateFile()
  420. {
  421. $encoding = strtolower(Configure::read('App.encoding'));
  422. $result = $this->Form->create(false, ['type' => 'file']);
  423. $expected = [
  424. 'form' => [
  425. 'method' => 'post', 'action' => '/articles/add',
  426. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  427. ],
  428. 'div' => ['style' => 'display:none;'],
  429. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  430. '/div'
  431. ];
  432. $this->assertHtml($expected, $result);
  433. }
  434. /**
  435. * Test creating GET forms.
  436. *
  437. * @return void
  438. */
  439. public function testCreateGet()
  440. {
  441. $encoding = strtolower(Configure::read('App.encoding'));
  442. $result = $this->Form->create(false, ['type' => 'get']);
  443. $expected = ['form' => [
  444. 'method' => 'get', 'action' => '/articles/add',
  445. 'accept-charset' => $encoding
  446. ]];
  447. $this->assertHtml($expected, $result);
  448. }
  449. /**
  450. * Test explicit method/enctype options.
  451. *
  452. * Explicit method overwrites inferred method from 'type'
  453. *
  454. * @return void
  455. */
  456. public function testCreateExplicitMethodEnctype()
  457. {
  458. $encoding = strtolower(Configure::read('App.encoding'));
  459. $result = $this->Form->create(false, [
  460. 'type' => 'get',
  461. 'method' => 'put',
  462. 'enctype' => 'multipart/form-data'
  463. ]);
  464. $expected = ['form' => [
  465. 'method' => 'put',
  466. 'action' => '/articles/add',
  467. 'enctype' => 'multipart/form-data',
  468. 'accept-charset' => $encoding
  469. ]];
  470. $this->assertHtml($expected, $result);
  471. }
  472. /**
  473. * Test create() with the templates option.
  474. *
  475. * @return void
  476. */
  477. public function testCreateTemplatesArray()
  478. {
  479. $result = $this->Form->create($this->article, [
  480. 'templates' => [
  481. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  482. ]
  483. ]);
  484. $expected = [
  485. 'form' => [
  486. 'class' => 'form-horizontal',
  487. 'method' => 'post',
  488. 'action' => '/articles/add',
  489. 'accept-charset' => 'utf-8'
  490. ]
  491. ];
  492. $this->assertHtml($expected, $result);
  493. }
  494. /**
  495. * Test create() with the templates option.
  496. *
  497. * @return void
  498. */
  499. public function testCreateTemplatesFile()
  500. {
  501. $result = $this->Form->create($this->article, [
  502. 'templates' => 'htmlhelper_tags',
  503. ]);
  504. $expected = [
  505. 'start form',
  506. 'div' => ['class' => 'hidden'],
  507. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  508. '/div'
  509. ];
  510. $this->assertHtml($expected, $result);
  511. }
  512. /**
  513. * Test that create() and end() restore templates.
  514. *
  515. * @return void
  516. */
  517. public function testCreateEndRestoreTemplates()
  518. {
  519. $this->Form->create($this->article, [
  520. 'templates' => ['input' => 'custom input element']
  521. ]);
  522. $this->Form->end();
  523. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  524. }
  525. /**
  526. * Test using template vars in various templates used by control() method.
  527. *
  528. * @return void
  529. */
  530. public function testControlTemplateVars()
  531. {
  532. $result = $this->Form->control('text', [
  533. 'templates' => [
  534. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  535. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  536. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  537. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  538. ],
  539. 'templateVars' => [
  540. 'forinput' => 'in-input',
  541. 'forlabel' => 'in-label',
  542. 'forgroup' => 'in-group',
  543. 'forcontainer' => 'in-container'
  544. ]
  545. ]);
  546. $expected = [
  547. 'div' => ['class'],
  548. 'label' => ['for'],
  549. 'Text in-label',
  550. '/label',
  551. 'in-group',
  552. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  553. 'in-container',
  554. '/div',
  555. ];
  556. $this->assertHtml($expected, $result);
  557. }
  558. /**
  559. * Test ensuring template variables work in template files loaded
  560. * during control().
  561. *
  562. * @return void
  563. */
  564. public function testControlTemplatesFromFile()
  565. {
  566. $result = $this->Form->control('title', [
  567. 'templates' => 'test_templates',
  568. 'templateVars' => [
  569. 'forcontainer' => 'container-data'
  570. ]
  571. ]);
  572. $expected = [
  573. 'div' => ['class'],
  574. 'label' => ['for'],
  575. 'Title',
  576. '/label',
  577. 'input' => ['name', 'type' => 'text', 'id'],
  578. 'container-data',
  579. '/div',
  580. ];
  581. $this->assertHtml($expected, $result);
  582. }
  583. /**
  584. * Test using template vars in inputSubmit and submitContainer template.
  585. *
  586. * @return void
  587. */
  588. public function testSubmitTemplateVars()
  589. {
  590. $this->Form->templates([
  591. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  592. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  593. ]);
  594. $result = $this->Form->submit('Submit', [
  595. 'templateVars' => [
  596. 'forinput' => 'in-input',
  597. 'forcontainer' => 'in-container'
  598. ]
  599. ]);
  600. $expected = [
  601. 'div' => ['class'],
  602. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  603. 'in-container',
  604. '/div',
  605. ];
  606. $this->assertHtml($expected, $result);
  607. }
  608. /**
  609. * test the create() method
  610. *
  611. * @dataProvider requestTypeProvider
  612. * @return void
  613. */
  614. public function testCreateTypeOptions($type, $method, $override)
  615. {
  616. $encoding = strtolower(Configure::read('App.encoding'));
  617. $result = $this->Form->create(false, ['type' => $type]);
  618. $expected = [
  619. 'form' => [
  620. 'method' => $method, 'action' => '/articles/add',
  621. 'accept-charset' => $encoding
  622. ],
  623. 'div' => ['style' => 'display:none;'],
  624. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  625. '/div'
  626. ];
  627. $this->assertHtml($expected, $result);
  628. }
  629. /**
  630. * Test using template vars in Create (formStart template)
  631. *
  632. * @return void
  633. */
  634. public function testCreateTemplateVars()
  635. {
  636. $result = $this->Form->create($this->article, [
  637. 'templates' => [
  638. 'formStart' => '<h4 class="mb">{{header}}</h4><form{{attrs}}>',
  639. ],
  640. 'templateVars' => ['header' => 'headertext']
  641. ]);
  642. $expected = [
  643. 'h4' => ['class'],
  644. 'headertext',
  645. '/h4',
  646. 'form' => [
  647. 'method' => 'post',
  648. 'action' => '/articles/add',
  649. 'accept-charset' => 'utf-8'
  650. ]
  651. ];
  652. $this->assertHtml($expected, $result);
  653. }
  654. /**
  655. * Test opening a form for an update operation.
  656. *
  657. * @return void
  658. */
  659. public function testCreateUpdateForm()
  660. {
  661. $encoding = strtolower(Configure::read('App.encoding'));
  662. $this->Form->request->here = '/articles/edit/1';
  663. $this->Form->request->params['action'] = 'edit';
  664. $this->article['defaults']['id'] = 1;
  665. $result = $this->Form->create($this->article);
  666. $expected = [
  667. 'form' => [
  668. 'method' => 'post',
  669. 'action' => '/articles/edit/1',
  670. 'accept-charset' => $encoding
  671. ],
  672. 'div' => ['style' => 'display:none;'],
  673. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  674. '/div'
  675. ];
  676. $this->assertHtml($expected, $result);
  677. }
  678. /**
  679. * test create() with automatic url generation
  680. *
  681. * @return void
  682. */
  683. public function testCreateAutoUrl()
  684. {
  685. $encoding = strtolower(Configure::read('App.encoding'));
  686. $this->Form->request->params['action'] = 'delete';
  687. $this->Form->request->here = '/articles/delete/10';
  688. $this->Form->request->base = '';
  689. $result = $this->Form->create($this->article);
  690. $expected = [
  691. 'form' => [
  692. 'method' => 'post', 'action' => '/articles/delete/10',
  693. 'accept-charset' => $encoding
  694. ],
  695. 'div' => ['style' => 'display:none;'],
  696. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  697. '/div'
  698. ];
  699. $this->assertHtml($expected, $result);
  700. $this->article['defaults'] = ['id' => 1];
  701. $this->Form->request->here = '/articles/edit/1';
  702. $this->Form->request->params['action'] = 'delete';
  703. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  704. $expected = [
  705. 'form' => [
  706. 'method' => 'post',
  707. 'action' => '/articles/edit/1',
  708. 'accept-charset' => $encoding
  709. ],
  710. 'div' => ['style' => 'display:none;'],
  711. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  712. '/div'
  713. ];
  714. $this->assertHtml($expected, $result);
  715. $this->Form->request->params['action'] = 'add';
  716. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  717. $expected = [
  718. 'form' => [
  719. 'method' => 'post',
  720. 'action' => '/articles/publish/1',
  721. 'accept-charset' => $encoding
  722. ],
  723. 'div' => ['style' => 'display:none;'],
  724. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  725. '/div'
  726. ];
  727. $this->assertHtml($expected, $result);
  728. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  729. $expected = [
  730. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  731. 'div' => ['style' => 'display:none;'],
  732. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  733. '/div'
  734. ];
  735. $this->assertHtml($expected, $result);
  736. $this->Form->request->params['controller'] = 'Pages';
  737. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  738. $expected = [
  739. 'form' => [
  740. 'method' => 'post', 'action' => '/Pages/signup/1',
  741. 'accept-charset' => $encoding
  742. ],
  743. 'div' => ['style' => 'display:none;'],
  744. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  745. '/div'
  746. ];
  747. $this->assertHtml($expected, $result);
  748. }
  749. /**
  750. * Test create() with no URL (no "action" attribute for <form> tag)
  751. *
  752. * @return void
  753. */
  754. public function testCreateNoUrl()
  755. {
  756. $result = $this->Form->create(false, ['url' => false]);
  757. $expected = [
  758. 'form' => [
  759. 'method' => 'post',
  760. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  761. ],
  762. 'div' => ['style' => 'display:none;'],
  763. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  764. '/div'
  765. ];
  766. $this->assertHtml($expected, $result);
  767. }
  768. /**
  769. * test create() with a custom route
  770. *
  771. * @return void
  772. */
  773. public function testCreateCustomRoute()
  774. {
  775. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  776. $encoding = strtolower(Configure::read('App.encoding'));
  777. $this->Form->request->params['controller'] = 'users';
  778. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  779. $expected = [
  780. 'form' => [
  781. 'method' => 'post', 'action' => '/login',
  782. 'accept-charset' => $encoding
  783. ],
  784. 'div' => ['style' => 'display:none;'],
  785. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  786. '/div'
  787. ];
  788. $this->assertHtml($expected, $result);
  789. Router::connect(
  790. '/new-article',
  791. ['controller' => 'articles', 'action' => 'myaction'],
  792. ['_name' => 'my-route']
  793. );
  794. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  795. $expected = [
  796. 'form' => [
  797. 'method' => 'post', 'action' => '/new-article',
  798. 'accept-charset' => $encoding,
  799. ],
  800. 'div' => ['style' => 'display:none;'],
  801. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  802. '/div'
  803. ];
  804. $this->assertHtml($expected, $result);
  805. }
  806. /**
  807. * test automatic accept-charset overriding
  808. *
  809. * @return void
  810. */
  811. public function testCreateWithAcceptCharset()
  812. {
  813. $result = $this->Form->create(
  814. $this->article,
  815. [
  816. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  817. ]
  818. );
  819. $expected = [
  820. 'form' => [
  821. 'method' => 'post', 'action' => '/articles',
  822. 'accept-charset' => 'iso-8859-1'
  823. ],
  824. 'div' => ['style' => 'display:none;'],
  825. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  826. '/div'
  827. ];
  828. $this->assertHtml($expected, $result);
  829. }
  830. /**
  831. * Test base form URL when url param is passed with multiple parameters (&)
  832. *
  833. */
  834. public function testCreateQueryStringRequest()
  835. {
  836. $encoding = strtolower(Configure::read('App.encoding'));
  837. $result = $this->Form->create($this->article, [
  838. 'type' => 'post',
  839. 'escape' => false,
  840. 'url' => [
  841. 'controller' => 'controller',
  842. 'action' => 'action',
  843. '?' => ['param1' => 'value1', 'param2' => 'value2']
  844. ]
  845. ]);
  846. $expected = [
  847. 'form' => [
  848. 'method' => 'post',
  849. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  850. 'accept-charset' => $encoding
  851. ],
  852. 'div' => ['style' => 'display:none;'],
  853. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  854. '/div'
  855. ];
  856. $this->assertHtml($expected, $result);
  857. $result = $this->Form->create($this->article, [
  858. 'type' => 'post',
  859. 'url' => [
  860. 'controller' => 'controller',
  861. 'action' => 'action',
  862. '?' => ['param1' => 'value1', 'param2' => 'value2']
  863. ]
  864. ]);
  865. $this->assertHtml($expected, $result);
  866. }
  867. /**
  868. * test that create() doesn't cause errors by multiple id's being in the primary key
  869. * as could happen with multiple select or checkboxes.
  870. *
  871. * @return void
  872. */
  873. public function testCreateWithMultipleIdInData()
  874. {
  875. $encoding = strtolower(Configure::read('App.encoding'));
  876. $this->Form->request->data['Article']['id'] = [1, 2];
  877. $result = $this->Form->create($this->article);
  878. $expected = [
  879. 'form' => [
  880. 'method' => 'post',
  881. 'action' => '/articles/add',
  882. 'accept-charset' => $encoding
  883. ],
  884. 'div' => ['style' => 'display:none;'],
  885. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  886. '/div'
  887. ];
  888. $this->assertHtml($expected, $result);
  889. }
  890. /**
  891. * test that create() doesn't add in extra passed params.
  892. *
  893. * @return void
  894. */
  895. public function testCreatePassedArgs()
  896. {
  897. $encoding = strtolower(Configure::read('App.encoding'));
  898. $this->Form->request->data['Article']['id'] = 1;
  899. $result = $this->Form->create($this->article, [
  900. 'type' => 'post',
  901. 'escape' => false,
  902. 'url' => [
  903. 'action' => 'edit',
  904. 'myparam'
  905. ]
  906. ]);
  907. $expected = [
  908. 'form' => [
  909. 'method' => 'post',
  910. 'action' => '/articles/edit/myparam',
  911. 'accept-charset' => $encoding
  912. ],
  913. 'div' => ['style' => 'display:none;'],
  914. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  915. '/div'
  916. ];
  917. $this->assertHtml($expected, $result);
  918. }
  919. /**
  920. * test creating a get form, and get form inputs.
  921. *
  922. * @return void
  923. */
  924. public function testGetFormCreate()
  925. {
  926. $encoding = strtolower(Configure::read('App.encoding'));
  927. $result = $this->Form->create($this->article, ['type' => 'get']);
  928. $expected = ['form' => [
  929. 'method' => 'get', 'action' => '/articles/add',
  930. 'accept-charset' => $encoding
  931. ]];
  932. $this->assertHtml($expected, $result);
  933. $result = $this->Form->text('title');
  934. $expected = ['input' => [
  935. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  936. ]];
  937. $this->assertHtml($expected, $result);
  938. $result = $this->Form->password('password');
  939. $expected = ['input' => [
  940. 'name' => 'password', 'type' => 'password'
  941. ]];
  942. $this->assertHtml($expected, $result);
  943. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  944. $result = $this->Form->text('user_form');
  945. $expected = ['input' => [
  946. 'name' => 'user_form', 'type' => 'text'
  947. ]];
  948. $this->assertHtml($expected, $result);
  949. }
  950. /**
  951. * test get form, and inputs when the model param is false
  952. *
  953. * @return void
  954. */
  955. public function testGetFormWithFalseModel()
  956. {
  957. $encoding = strtolower(Configure::read('App.encoding'));
  958. $this->Form->request->params['controller'] = 'contact_test';
  959. $result = $this->Form->create(false, [
  960. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  961. ]);
  962. $expected = ['form' => [
  963. 'method' => 'get', 'action' => '/contact_test/add',
  964. 'accept-charset' => $encoding
  965. ]];
  966. $this->assertHtml($expected, $result);
  967. $result = $this->Form->text('reason');
  968. $expected = [
  969. 'input' => ['type' => 'text', 'name' => 'reason']
  970. ];
  971. $this->assertHtml($expected, $result);
  972. }
  973. /**
  974. * testFormCreateWithSecurity method
  975. *
  976. * Test form->create() with security key.
  977. *
  978. * @return void
  979. */
  980. public function testCreateWithSecurity()
  981. {
  982. $this->Form->request->params['_csrfToken'] = 'testKey';
  983. $encoding = strtolower(Configure::read('App.encoding'));
  984. $result = $this->Form->create($this->article, [
  985. 'url' => '/articles/publish',
  986. ]);
  987. $expected = [
  988. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  989. 'div' => ['style' => 'display:none;'],
  990. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  991. ['input' => [
  992. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  993. ]],
  994. '/div'
  995. ];
  996. $this->assertHtml($expected, $result);
  997. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  998. $expected['form']['id'] = 'MyForm';
  999. $this->assertHtml($expected, $result);
  1000. }
  1001. /**
  1002. * testFormCreateGetNoSecurity method
  1003. *
  1004. * Test form->create() with no security key as its a get form
  1005. *
  1006. * @return void
  1007. */
  1008. public function testCreateEndGetNoSecurity()
  1009. {
  1010. $this->Form->request->params['_csrfToken'] = 'testKey';
  1011. $article = new Article();
  1012. $result = $this->Form->create($article, [
  1013. 'type' => 'get',
  1014. 'url' => '/contacts/add'
  1015. ]);
  1016. $this->assertNotContains('testKey', $result);
  1017. $result = $this->Form->end();
  1018. $this->assertNotContains('testKey', $result);
  1019. }
  1020. /**
  1021. * test that create() clears the fields property so it starts fresh
  1022. *
  1023. * @return void
  1024. */
  1025. public function testCreateClearingFields()
  1026. {
  1027. $this->Form->fields = ['model_id'];
  1028. $this->Form->create($this->article);
  1029. $this->assertEquals([], $this->Form->fields);
  1030. }
  1031. /**
  1032. * Tests form hash generation with model-less data
  1033. *
  1034. * @return void
  1035. */
  1036. public function testValidateHashNoModel()
  1037. {
  1038. $this->Form->request->params['_Token'] = 'foo';
  1039. $result = $this->Form->secure(['anything']);
  1040. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1041. }
  1042. /**
  1043. * Tests that hidden fields generated for checkboxes don't get locked
  1044. *
  1045. * @return void
  1046. */
  1047. public function testNoCheckboxLocking()
  1048. {
  1049. $this->Form->request->params['_Token'] = 'foo';
  1050. $this->assertSame([], $this->Form->fields);
  1051. $this->Form->checkbox('check', ['value' => '1']);
  1052. $this->assertSame($this->Form->fields, ['check']);
  1053. }
  1054. /**
  1055. * testFormSecurityFields method
  1056. *
  1057. * Test generation of secure form hash generation.
  1058. *
  1059. * @return void
  1060. */
  1061. public function testFormSecurityFields()
  1062. {
  1063. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1064. $this->Form->request->params['_Token'] = 'testKey';
  1065. $result = $this->Form->secure($fields);
  1066. $hash = Security::hash(serialize($fields) . Security::salt());
  1067. $hash .= ':' . 'Model.valid';
  1068. $hash = urlencode($hash);
  1069. $tokenDebug = urlencode(json_encode([
  1070. '',
  1071. $fields,
  1072. []
  1073. ]));
  1074. $expected = [
  1075. 'div' => ['style' => 'display:none;'],
  1076. ['input' => [
  1077. 'type' => 'hidden',
  1078. 'name' => '_Token[fields]',
  1079. 'value' => $hash
  1080. ]],
  1081. ['input' => [
  1082. 'type' => 'hidden',
  1083. 'name' => '_Token[unlocked]',
  1084. 'value' => '',
  1085. ]],
  1086. ['input' => [
  1087. 'type' => 'hidden',
  1088. 'name' => '_Token[debug]',
  1089. 'value' => $tokenDebug
  1090. ]],
  1091. '/div'
  1092. ];
  1093. $this->assertHtml($expected, $result);
  1094. }
  1095. /**
  1096. * testFormSecurityFields method
  1097. *
  1098. * Test debug token is not generated if debug is false
  1099. *
  1100. * @return void
  1101. */
  1102. public function testFormSecurityFieldsNoDebugMode()
  1103. {
  1104. Configure::write('debug', false);
  1105. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1106. $this->Form->request->params['_Token'] = 'testKey';
  1107. $result = $this->Form->secure($fields);
  1108. $hash = Security::hash(serialize($fields) . Security::salt());
  1109. $hash .= ':' . 'Model.valid';
  1110. $hash = urlencode($hash);
  1111. $expected = [
  1112. 'div' => ['style' => 'display:none;'],
  1113. ['input' => [
  1114. 'type' => 'hidden',
  1115. 'name' => '_Token[fields]',
  1116. 'value' => $hash
  1117. ]],
  1118. ['input' => [
  1119. 'type' => 'hidden',
  1120. 'name' => '_Token[unlocked]',
  1121. 'value' => '',
  1122. ]],
  1123. '/div'
  1124. ];
  1125. $this->assertHtml($expected, $result);
  1126. }
  1127. /**
  1128. * Tests correct generation of number fields for double and float fields
  1129. *
  1130. * @return void
  1131. */
  1132. public function testTextFieldGenerationForFloats()
  1133. {
  1134. $this->article['schema'] = [
  1135. 'foo' => [
  1136. 'type' => 'float',
  1137. 'null' => false,
  1138. 'default' => null,
  1139. 'length' => 10
  1140. ]
  1141. ];
  1142. $this->Form->create($this->article);
  1143. $result = $this->Form->control('foo');
  1144. $expected = [
  1145. 'div' => ['class' => 'input number'],
  1146. 'label' => ['for' => 'foo'],
  1147. 'Foo',
  1148. '/label',
  1149. ['input' => [
  1150. 'type' => 'number',
  1151. 'name' => 'foo',
  1152. 'id' => 'foo',
  1153. 'step' => 'any'
  1154. ]],
  1155. '/div'
  1156. ];
  1157. $this->assertHtml($expected, $result);
  1158. $result = $this->Form->control('foo', ['step' => 0.5]);
  1159. $expected = [
  1160. 'div' => ['class' => 'input number'],
  1161. 'label' => ['for' => 'foo'],
  1162. 'Foo',
  1163. '/label',
  1164. ['input' => [
  1165. 'type' => 'number',
  1166. 'name' => 'foo',
  1167. 'id' => 'foo',
  1168. 'step' => '0.5'
  1169. ]],
  1170. '/div'
  1171. ];
  1172. $this->assertHtml($expected, $result);
  1173. }
  1174. /**
  1175. * Tests correct generation of number fields for integer fields
  1176. *
  1177. * @return void
  1178. */
  1179. public function testTextFieldTypeNumberGenerationForIntegers()
  1180. {
  1181. TableRegistry::get('Contacts', [
  1182. 'className' => __NAMESPACE__ . '\ContactsTable'
  1183. ]);
  1184. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1185. $result = $this->Form->control('age');
  1186. $expected = [
  1187. 'div' => ['class' => 'input number'],
  1188. 'label' => ['for' => 'age'],
  1189. 'Age',
  1190. '/label',
  1191. ['input' => [
  1192. 'type' => 'number', 'name' => 'age',
  1193. 'id' => 'age'
  1194. ]],
  1195. '/div'
  1196. ];
  1197. $this->assertHtml($expected, $result);
  1198. }
  1199. /**
  1200. * Tests correct generation of file upload fields for binary fields
  1201. *
  1202. * @return void
  1203. */
  1204. public function testFileUploadFieldTypeGenerationForBinaries()
  1205. {
  1206. $table = TableRegistry::get('Contacts', [
  1207. 'className' => __NAMESPACE__ . '\ContactsTable'
  1208. ]);
  1209. $table->schema(['foo' => [
  1210. 'type' => 'binary',
  1211. 'null' => false,
  1212. 'default' => null,
  1213. 'length' => 1024
  1214. ]]);
  1215. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1216. $result = $this->Form->control('foo');
  1217. $expected = [
  1218. 'div' => ['class' => 'input file'],
  1219. 'label' => ['for' => 'foo'],
  1220. 'Foo',
  1221. '/label',
  1222. ['input' => [
  1223. 'type' => 'file', 'name' => 'foo',
  1224. 'id' => 'foo'
  1225. ]],
  1226. '/div'
  1227. ];
  1228. $this->assertHtml($expected, $result);
  1229. }
  1230. /**
  1231. * testFormSecurityMultipleFields method
  1232. *
  1233. * Test secure() with multiple row form. Ensure hash is correct.
  1234. *
  1235. * @return void
  1236. */
  1237. public function testFormSecurityMultipleFields()
  1238. {
  1239. $this->Form->request->params['_Token'] = 'foo';
  1240. $fields = [
  1241. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1242. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1243. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1244. ];
  1245. $result = $this->Form->secure($fields);
  1246. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1247. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1248. $tokenDebug = urlencode(json_encode([
  1249. '',
  1250. $fields,
  1251. []
  1252. ]));
  1253. $expected = [
  1254. 'div' => ['style' => 'display:none;'],
  1255. ['input' => [
  1256. 'type' => 'hidden', 'name' => '_Token[fields]',
  1257. 'value' => $hash
  1258. ]],
  1259. ['input' => [
  1260. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1261. 'value' => ''
  1262. ]],
  1263. ['input' => [
  1264. 'type' => 'hidden', 'name' => '_Token[debug]',
  1265. 'value' => $tokenDebug,
  1266. ]],
  1267. '/div'
  1268. ];
  1269. $this->assertHtml($expected, $result);
  1270. }
  1271. /**
  1272. * testFormSecurityMultipleSubmitButtons
  1273. *
  1274. * test form submit generation and ensure that _Token is only created on end()
  1275. *
  1276. * @return void
  1277. */
  1278. public function testFormSecurityMultipleSubmitButtons()
  1279. {
  1280. $this->Form->request->params['_Token'] = 'testKey';
  1281. $this->Form->create($this->article);
  1282. $this->Form->text('Address.title');
  1283. $this->Form->text('Address.first_name');
  1284. $result = $this->Form->submit('Save', ['name' => 'save']);
  1285. $expected = [
  1286. 'div' => ['class' => 'submit'],
  1287. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1288. '/div',
  1289. ];
  1290. $this->assertHtml($expected, $result);
  1291. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1292. $expected = [
  1293. 'div' => ['class' => 'submit'],
  1294. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1295. '/div',
  1296. ];
  1297. $this->assertHtml($expected, $result);
  1298. $result = $this->Form->end();
  1299. $tokenDebug = urlencode(json_encode([
  1300. '/articles/add',
  1301. [
  1302. 'Address.title',
  1303. 'Address.first_name',
  1304. ],
  1305. ['save', 'cancel']
  1306. ]));
  1307. $expected = [
  1308. 'div' => ['style' => 'display:none;'],
  1309. ['input' => [
  1310. 'type' => 'hidden',
  1311. 'name' => '_Token[fields]',
  1312. 'value'
  1313. ]],
  1314. ['input' => [
  1315. 'type' => 'hidden',
  1316. 'name' => '_Token[unlocked]',
  1317. 'value' => 'cancel%7Csave'
  1318. ]],
  1319. ['input' => [
  1320. 'type' => 'hidden', 'name' => '_Token[debug]',
  1321. 'value' => $tokenDebug,
  1322. ]],
  1323. '/div'
  1324. ];
  1325. $this->assertHtml($expected, $result);
  1326. }
  1327. /**
  1328. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1329. *
  1330. * @return void
  1331. */
  1332. public function testSecurityButtonNestedNamed()
  1333. {
  1334. $key = 'testKey';
  1335. $this->Form->request->params['_csrfToken'] = $key;
  1336. $this->Form->create('Addresses');
  1337. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1338. $result = $this->Form->unlockField();
  1339. $this->assertEquals(['Address.button'], $result);
  1340. }
  1341. /**
  1342. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1343. *
  1344. * @return void
  1345. */
  1346. public function testSecuritySubmitNestedNamed()
  1347. {
  1348. $this->Form->request->params['_Token'] = 'testKey';
  1349. $this->Form->create($this->article);
  1350. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1351. $result = $this->Form->unlockField();
  1352. $this->assertEquals(['Address.button'], $result);
  1353. }
  1354. /**
  1355. * Test that the correct fields are unlocked for image submits with no names.
  1356. *
  1357. * @return void
  1358. */
  1359. public function testSecuritySubmitImageNoName()
  1360. {
  1361. $this->Form->request->params['_Token'] = 'testKey';
  1362. $this->Form->create(false);
  1363. $result = $this->Form->submit('save.png');
  1364. $expected = [
  1365. 'div' => ['class' => 'submit'],
  1366. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1367. '/div'
  1368. ];
  1369. $this->assertHtml($expected, $result);
  1370. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1371. }
  1372. /**
  1373. * Test that the correct fields are unlocked for image submits with names.
  1374. *
  1375. * @return void
  1376. */
  1377. public function testSecuritySubmitImageName()
  1378. {
  1379. $this->Form->request->params['_Token'] = 'testKey';
  1380. $this->Form->create(null);
  1381. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1382. $expected = [
  1383. 'div' => ['class' => 'submit'],
  1384. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1385. '/div'
  1386. ];
  1387. $this->assertHtml($expected, $result);
  1388. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1389. }
  1390. /**
  1391. * testFormSecurityMultipleControlFields method
  1392. *
  1393. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1394. *
  1395. * @return void
  1396. */
  1397. public function testFormSecurityMultipleControlFields()
  1398. {
  1399. $this->Form->request->params['_Token'] = 'testKey';
  1400. $this->Form->create();
  1401. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1402. $this->Form->control('Addresses.0.title');
  1403. $this->Form->control('Addresses.0.first_name');
  1404. $this->Form->control('Addresses.0.last_name');
  1405. $this->Form->control('Addresses.0.address');
  1406. $this->Form->control('Addresses.0.city');
  1407. $this->Form->control('Addresses.0.phone');
  1408. $this->Form->control('Addresses.0.primary', ['type' => 'checkbox']);
  1409. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1410. $this->Form->control('Addresses.1.title');
  1411. $this->Form->control('Addresses.1.first_name');
  1412. $this->Form->control('Addresses.1.last_name');
  1413. $this->Form->control('Addresses.1.address');
  1414. $this->Form->control('Addresses.1.city');
  1415. $this->Form->control('Addresses.1.phone');
  1416. $this->Form->control('Addresses.1.primary', ['type' => 'checkbox']);
  1417. $result = $this->Form->secure($this->Form->fields);
  1418. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1419. $tokenDebug = urlencode(json_encode([
  1420. '/articles/add',
  1421. [
  1422. 'Addresses.0.id' => '123456',
  1423. 'Addresses.0.title',
  1424. 'Addresses.0.first_name',
  1425. 'Addresses.0.last_name',
  1426. 'Addresses.0.address',
  1427. 'Addresses.0.city',
  1428. 'Addresses.0.phone',
  1429. 'Addresses.0.primary',
  1430. 'Addresses.1.id' => '654321',
  1431. 'Addresses.1.title',
  1432. 'Addresses.1.first_name',
  1433. 'Addresses.1.last_name',
  1434. 'Addresses.1.address',
  1435. 'Addresses.1.city',
  1436. 'Addresses.1.phone',
  1437. 'Addresses.1.primary',
  1438. ],
  1439. []
  1440. ]));
  1441. $expected = [
  1442. 'div' => ['style' => 'display:none;'],
  1443. ['input' => [
  1444. 'type' => 'hidden', 'name' => '_Token[fields]',
  1445. 'value' => $hash
  1446. ]],
  1447. ['input' => [
  1448. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1449. 'value' => ''
  1450. ]],
  1451. ['input' => [
  1452. 'type' => 'hidden', 'name' => '_Token[debug]',
  1453. 'value' => $tokenDebug,
  1454. ]],
  1455. '/div'
  1456. ];
  1457. $this->assertHtml($expected, $result);
  1458. }
  1459. /**
  1460. * testFormSecurityArrayFields method
  1461. *
  1462. * Test form security with Model.field.0 style inputs.
  1463. *
  1464. * @return void
  1465. */
  1466. public function testFormSecurityArrayFields()
  1467. {
  1468. $this->Form->request->params['_Token'] = 'testKey';
  1469. $this->Form->create();
  1470. $this->Form->text('Address.primary.1');
  1471. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1472. $this->Form->text('Address.secondary.1.0');
  1473. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1474. }
  1475. /**
  1476. * testFormSecurityMultipleControlDisabledFields method
  1477. *
  1478. * Test secure form generation with multiple records and disabled fields.
  1479. *
  1480. * @return void
  1481. */
  1482. public function testFormSecurityMultipleControlDisabledFields()
  1483. {
  1484. $this->Form->request->params['_Token'] = [
  1485. 'unlockedFields' => ['first_name', 'address']
  1486. ];
  1487. $this->Form->create();
  1488. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1489. $this->Form->text('Addresses.0.title');
  1490. $this->Form->text('Addresses.0.first_name');
  1491. $this->Form->text('Addresses.0.last_name');
  1492. $this->Form->text('Addresses.0.address');
  1493. $this->Form->text('Addresses.0.city');
  1494. $this->Form->text('Addresses.0.phone');
  1495. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1496. $this->Form->text('Addresses.1.title');
  1497. $this->Form->text('Addresses.1.first_name');
  1498. $this->Form->text('Addresses.1.last_name');
  1499. $this->Form->text('Addresses.1.address');
  1500. $this->Form->text('Addresses.1.city');
  1501. $this->Form->text('Addresses.1.phone');
  1502. $result = $this->Form->secure($this->Form->fields);
  1503. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1504. $tokenDebug = urlencode(json_encode([
  1505. '/articles/add',
  1506. [
  1507. 'Addresses.0.id' => '123456',
  1508. 'Addresses.0.title',
  1509. 'Addresses.0.last_name',
  1510. 'Addresses.0.city',
  1511. 'Addresses.0.phone',
  1512. 'Addresses.1.id' => '654321',
  1513. 'Addresses.1.title',
  1514. 'Addresses.1.last_name',
  1515. 'Addresses.1.city',
  1516. 'Addresses.1.phone'
  1517. ],
  1518. [
  1519. 'first_name',
  1520. 'address'
  1521. ]
  1522. ]));
  1523. $expected = [
  1524. 'div' => ['style' => 'display:none;'],
  1525. ['input' => [
  1526. 'type' => 'hidden',
  1527. 'name' => '_Token[fields]',
  1528. 'value' => $hash
  1529. ]],
  1530. ['input' => [
  1531. 'type' => 'hidden',
  1532. 'name' => '_Token[unlocked]',
  1533. 'value' => 'address%7Cfirst_name',
  1534. ]],
  1535. ['input' => [
  1536. 'type' => 'hidden', 'name' => '_Token[debug]',
  1537. 'value' => $tokenDebug,
  1538. ]],
  1539. '/div'
  1540. ];
  1541. $this->assertHtml($expected, $result);
  1542. }
  1543. /**
  1544. * testFormSecurityControlDisabledFields method
  1545. *
  1546. * Test single record form with disabled fields.
  1547. *
  1548. * @return void
  1549. */
  1550. public function testFormSecurityControlUnlockedFields()
  1551. {
  1552. $this->Form->request->params['_Token'] = [
  1553. 'unlockedFields' => ['first_name', 'address']
  1554. ];
  1555. $this->Form->create();
  1556. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1557. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1558. $this->Form->text('Addresses.title');
  1559. $this->Form->text('Addresses.first_name');
  1560. $this->Form->text('Addresses.last_name');
  1561. $this->Form->text('Addresses.address');
  1562. $this->Form->text('Addresses.city');
  1563. $this->Form->text('Addresses.phone');
  1564. $result = $this->Form->fields;
  1565. $expected = [
  1566. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1567. 'Addresses.city', 'Addresses.phone'
  1568. ];
  1569. $this->assertEquals($expected, $result);
  1570. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1571. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1572. $tokenDebug = urlencode(json_encode([
  1573. '/articles/add',
  1574. [
  1575. 'Addresses.id' => '123456',
  1576. 'Addresses.title',
  1577. 'Addresses.last_name',
  1578. 'Addresses.city',
  1579. 'Addresses.phone'
  1580. ],
  1581. [
  1582. 'first_name',
  1583. 'address'
  1584. ]
  1585. ]));
  1586. $expected = [
  1587. 'div' => ['style' => 'display:none;'],
  1588. ['input' => [
  1589. 'type' => 'hidden',
  1590. 'name' => '_Token[fields]',
  1591. 'value' => $hash,
  1592. 'data-foo' => 'bar',
  1593. ]],
  1594. ['input' => [
  1595. 'type' => 'hidden',
  1596. 'name' => '_Token[unlocked]',
  1597. 'value' => 'address%7Cfirst_name',
  1598. 'data-foo' => 'bar',
  1599. ]],
  1600. ['input' => [
  1601. 'type' => 'hidden', 'name' => '_Token[debug]',
  1602. 'value' => $tokenDebug,
  1603. 'data-foo' => 'bar'
  1604. ]],
  1605. '/div'
  1606. ];
  1607. $this->assertHtml($expected, $result);
  1608. }
  1609. /**
  1610. * testFormSecurityControlUnlockedFieldsDebugSecurityTrue method
  1611. *
  1612. * Test single record form with debugSecurity param.
  1613. *
  1614. * @return void
  1615. */
  1616. public function testFormSecurityControlUnlockedFieldsDebugSecurityTrue()
  1617. {
  1618. $this->Form->request->params['_Token'] = [
  1619. 'unlockedFields' => ['first_name', 'address']
  1620. ];
  1621. $this->Form->create();
  1622. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1623. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1624. $this->Form->text('Addresses.title');
  1625. $this->Form->text('Addresses.first_name');
  1626. $this->Form->text('Addresses.last_name');
  1627. $this->Form->text('Addresses.address');
  1628. $this->Form->text('Addresses.city');
  1629. $this->Form->text('Addresses.phone');
  1630. $result = $this->Form->fields;
  1631. $expected = [
  1632. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1633. 'Addresses.city', 'Addresses.phone'
  1634. ];
  1635. $this->assertEquals($expected, $result);
  1636. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1637. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1638. $tokenDebug = urlencode(json_encode([
  1639. '/articles/add',
  1640. [
  1641. 'Addresses.id' => '123456',
  1642. 'Addresses.title',
  1643. 'Addresses.last_name',
  1644. 'Addresses.city',
  1645. 'Addresses.phone'
  1646. ],
  1647. [
  1648. 'first_name',
  1649. 'address'
  1650. ]
  1651. ]));
  1652. $expected = [
  1653. 'div' => ['style' => 'display:none;'],
  1654. ['input' => [
  1655. 'type' => 'hidden',
  1656. 'name' => '_Token[fields]',
  1657. 'value' => $hash,
  1658. 'data-foo' => 'bar',
  1659. ]],
  1660. ['input' => [
  1661. 'type' => 'hidden',
  1662. 'name' => '_Token[unlocked]',
  1663. 'value' => 'address%7Cfirst_name',
  1664. 'data-foo' => 'bar',
  1665. ]],
  1666. ['input' => [
  1667. 'type' => 'hidden', 'name' => '_Token[debug]',
  1668. 'value' => $tokenDebug,
  1669. 'data-foo' => 'bar'
  1670. ]],
  1671. '/div'
  1672. ];
  1673. $this->assertHtml($expected, $result);
  1674. }
  1675. /**
  1676. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1677. *
  1678. * Debug is false, debugSecurity is true -> no debug
  1679. *
  1680. * @return void
  1681. */
  1682. public function testFormSecurityControlUnlockedFieldsDebugSecurityDebugFalse()
  1683. {
  1684. $this->Form->request->params['_Token'] = [
  1685. 'unlockedFields' => ['first_name', 'address']
  1686. ];
  1687. $this->Form->create();
  1688. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1689. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1690. $this->Form->text('Addresses.title');
  1691. $this->Form->text('Addresses.first_name');
  1692. $this->Form->text('Addresses.last_name');
  1693. $this->Form->text('Addresses.address');
  1694. $this->Form->text('Addresses.city');
  1695. $this->Form->text('Addresses.phone');
  1696. $result = $this->Form->fields;
  1697. $expected = [
  1698. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1699. 'Addresses.city', 'Addresses.phone'
  1700. ];
  1701. $this->assertEquals($expected, $result);
  1702. Configure::write('debug', false);
  1703. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1704. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1705. $expected = [
  1706. 'div' => ['style' => 'display:none;'],
  1707. ['input' => [
  1708. 'type' => 'hidden',
  1709. 'name' => '_Token[fields]',
  1710. 'value' => $hash,
  1711. 'data-foo' => 'bar',
  1712. ]],
  1713. ['input' => [
  1714. 'type' => 'hidden',
  1715. 'name' => '_Token[unlocked]',
  1716. 'value' => 'address%7Cfirst_name',
  1717. 'data-foo' => 'bar',
  1718. ]],
  1719. '/div'
  1720. ];
  1721. $this->assertHtml($expected, $result);
  1722. }
  1723. /**
  1724. * testFormSecurityControlUnlockedFieldsDebugSecurityFalse method
  1725. *
  1726. * Test single record form with debugSecurity param.
  1727. *
  1728. * @return void
  1729. */
  1730. public function testFormSecurityControlUnlockedFieldsDebugSecurityFalse()
  1731. {
  1732. $this->Form->request->params['_Token'] = [
  1733. 'unlockedFields' => ['first_name', 'address']
  1734. ];
  1735. $this->Form->create();
  1736. $this->assertEquals($this->Form->request->params['_Token']['unlockedFields'], $this->Form->unlockField());
  1737. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1738. $this->Form->text('Addresses.title');
  1739. $this->Form->text('Addresses.first_name');
  1740. $this->Form->text('Addresses.last_name');
  1741. $this->Form->text('Addresses.address');
  1742. $this->Form->text('Addresses.city');
  1743. $this->Form->text('Addresses.phone');
  1744. $result = $this->Form->fields;
  1745. $expected = [
  1746. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1747. 'Addresses.city', 'Addresses.phone'
  1748. ];
  1749. $this->assertEquals($expected, $result);
  1750. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  1751. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1752. $expected = [
  1753. 'div' => ['style' => 'display:none;'],
  1754. ['input' => [
  1755. 'type' => 'hidden',
  1756. 'name' => '_Token[fields]',
  1757. 'value' => $hash,
  1758. 'data-foo' => 'bar',
  1759. ]],
  1760. ['input' => [
  1761. 'type' => 'hidden',
  1762. 'name' => '_Token[unlocked]',
  1763. 'value' => 'address%7Cfirst_name',
  1764. 'data-foo' => 'bar',
  1765. ]],
  1766. '/div'
  1767. ];
  1768. $this->assertHtml($expected, $result);
  1769. }
  1770. /**
  1771. * testFormSecureWithCustomNameAttribute method
  1772. *
  1773. * Test securing inputs with custom name attributes.
  1774. *
  1775. * @return void
  1776. */
  1777. public function testFormSecureWithCustomNameAttribute()
  1778. {
  1779. $this->Form->request->params['_Token'] = 'testKey';
  1780. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1781. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1782. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1783. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1784. }
  1785. /**
  1786. * testFormSecuredControl method
  1787. *
  1788. * Test generation of entire secure form, assertions made on control() output.
  1789. *
  1790. * @return void
  1791. */
  1792. public function testFormSecuredControl()
  1793. {
  1794. $this->Form->request->params['_csrfToken'] = 'testKey';
  1795. $this->Form->request->params['_Token'] = 'stuff';
  1796. $this->article['schema'] = [
  1797. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1798. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1799. ];
  1800. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1801. $encoding = strtolower(Configure::read('App.encoding'));
  1802. $expected = [
  1803. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1804. 'div' => ['style' => 'display:none;'],
  1805. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1806. ['input' => [
  1807. 'type' => 'hidden',
  1808. 'name' => '_csrfToken',
  1809. 'value' => 'testKey'
  1810. ]],
  1811. '/div'
  1812. ];
  1813. $this->assertHtml($expected, $result);
  1814. $result = $this->Form->control('ratio');
  1815. $expected = [
  1816. 'div' => ['class'],
  1817. 'label' => ['for'],
  1818. 'Ratio',
  1819. '/label',
  1820. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1821. '/div',
  1822. ];
  1823. $this->assertHtml($expected, $result);
  1824. $result = $this->Form->control('population');
  1825. $expected = [
  1826. 'div' => ['class'],
  1827. 'label' => ['for'],
  1828. 'Population',
  1829. '/label',
  1830. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1831. '/div',
  1832. ];
  1833. $this->assertHtml($expected, $result);
  1834. $result = $this->Form->control('published', ['type' => 'text']);
  1835. $expected = [
  1836. 'div' => ['class' => 'input text'],
  1837. 'label' => ['for' => 'published'],
  1838. 'Published',
  1839. '/label',
  1840. ['input' => [
  1841. 'type' => 'text',
  1842. 'name' => 'published',
  1843. 'id' => 'published'
  1844. ]],
  1845. '/div'
  1846. ];
  1847. $this->assertHtml($expected, $result);
  1848. $result = $this->Form->control('other', ['type' => 'text']);
  1849. $expected = [
  1850. 'div' => ['class' => 'input text'],
  1851. 'label' => ['for' => 'other'],
  1852. 'Other',
  1853. '/label',
  1854. ['input' => [
  1855. 'type' => 'text',
  1856. 'name' => 'other',
  1857. 'id',
  1858. ]],
  1859. '/div'
  1860. ];
  1861. $this->assertHtml($expected, $result);
  1862. $result = $this->Form->hidden('stuff');
  1863. $expected = [
  1864. 'input' => [
  1865. 'type' => 'hidden',
  1866. 'name' => 'stuff'
  1867. ]
  1868. ];
  1869. $this->assertHtml($expected, $result);
  1870. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1871. $expected = ['input' => [
  1872. 'type' => 'hidden',
  1873. 'name' => 'hidden',
  1874. 'value' => '0'
  1875. ]];
  1876. $this->assertHtml($expected, $result);
  1877. $result = $this->Form->control('something', ['type' => 'checkbox']);
  1878. $expected = [
  1879. 'div' => ['class' => 'input checkbox'],
  1880. ['input' => [
  1881. 'type' => 'hidden',
  1882. 'name' => 'something',
  1883. 'value' => '0'
  1884. ]],
  1885. 'label' => ['for' => 'something'],
  1886. ['input' => [
  1887. 'type' => 'checkbox',
  1888. 'name' => 'something',
  1889. 'value' => '1',
  1890. 'id' => 'something'
  1891. ]],
  1892. 'Something',
  1893. '/label',
  1894. '/div'
  1895. ];
  1896. $this->assertHtml($expected, $result);
  1897. $result = $this->Form->fields;
  1898. $expectedFields = [
  1899. 'ratio',
  1900. 'population',
  1901. 'published',
  1902. 'other',
  1903. 'stuff' => '',
  1904. 'hidden' => '0',
  1905. 'something'
  1906. ];
  1907. $this->assertEquals($expectedFields, $result);
  1908. $result = $this->Form->secure($this->Form->fields);
  1909. $tokenDebug = urlencode(json_encode([
  1910. '/articles/add',
  1911. $expectedFields,
  1912. []
  1913. ]));
  1914. $expected = [
  1915. 'div' => ['style' => 'display:none;'],
  1916. ['input' => [
  1917. 'type' => 'hidden',
  1918. 'name' => '_Token[fields]',
  1919. 'value'
  1920. ]],
  1921. ['input' => [
  1922. 'type' => 'hidden',
  1923. 'name' => '_Token[unlocked]',
  1924. 'value' => ''
  1925. ]],
  1926. ['input' => [
  1927. 'type' => 'hidden', 'name' => '_Token[debug]',
  1928. 'value' => $tokenDebug,
  1929. ]],
  1930. '/div'
  1931. ];
  1932. $this->assertHtml($expected, $result);
  1933. }
  1934. /**
  1935. * testSecuredControlCustomName method
  1936. *
  1937. * Test secured inputs with custom names.
  1938. *
  1939. * @return void
  1940. */
  1941. public function testSecuredControlCustomName()
  1942. {
  1943. $this->Form->request->params['_Token'] = 'testKey';
  1944. $this->assertEquals([], $this->Form->fields);
  1945. $this->Form->text('text_input', [
  1946. 'name' => 'Option[General.default_role]',
  1947. ]);
  1948. $expected = ['Option.General.default_role'];
  1949. $this->assertEquals($expected, $this->Form->fields);
  1950. $this->Form->select('select_box', [1, 2], [
  1951. 'name' => 'Option[General.select_role]',
  1952. ]);
  1953. $expected[] = 'Option.General.select_role';
  1954. $this->assertEquals($expected, $this->Form->fields);
  1955. $this->Form->text('other.things[]');
  1956. $expected[] = 'other.things';
  1957. $this->assertEquals($expected, $this->Form->fields);
  1958. }
  1959. /**
  1960. * testSecuredControlDuplicate method
  1961. *
  1962. * Test that a hidden field followed by a visible field
  1963. * undoes the hidden field locking.
  1964. *
  1965. * @return void
  1966. */
  1967. public function testSecuredControlDuplicate()
  1968. {
  1969. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  1970. $this->assertEquals([], $this->Form->fields);
  1971. $this->Form->control('text_val', [
  1972. 'type' => 'hidden',
  1973. 'value' => 'some text',
  1974. ]);
  1975. $expected = ['text_val' => 'some text'];
  1976. $this->assertEquals($expected, $this->Form->fields);
  1977. $this->Form->control('text_val', [
  1978. 'type' => 'text',
  1979. ]);
  1980. $expected = ['text_val'];
  1981. $this->assertEquals($expected, $this->Form->fields);
  1982. }
  1983. /**
  1984. * testFormSecuredFileControl method
  1985. *
  1986. * Tests that the correct keys are added to the field hash index.
  1987. *
  1988. * @return void
  1989. */
  1990. public function testFormSecuredFileControl()
  1991. {
  1992. $this->assertEquals([], $this->Form->fields);
  1993. $this->Form->file('Attachment.file');
  1994. $expected = [
  1995. 'Attachment.file.name', 'Attachment.file.type',
  1996. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1997. 'Attachment.file.size'
  1998. ];
  1999. $this->assertEquals($expected, $this->Form->fields);
  2000. }
  2001. /**
  2002. * testFormSecuredMultipleSelect method
  2003. *
  2004. * Test that multiple selects keys are added to field hash.
  2005. *
  2006. * @return void
  2007. */
  2008. public function testFormSecuredMultipleSelect()
  2009. {
  2010. $this->Form->request->params['_csrfToken'] = 'testKey';
  2011. $this->assertEquals([], $this->Form->fields);
  2012. $options = ['1' => 'one', '2' => 'two'];
  2013. $this->Form->select('Model.select', $options);
  2014. $expected = ['Model.select'];
  2015. $this->assertEquals($expected, $this->Form->fields);
  2016. $this->Form->fields = [];
  2017. $this->Form->select('Model.select', $options, ['multiple' => true]);
  2018. $this->assertEquals($expected, $this->Form->fields);
  2019. }
  2020. /**
  2021. * testFormSecuredRadio method
  2022. *
  2023. * @return void
  2024. */
  2025. public function testFormSecuredRadio()
  2026. {
  2027. $this->Form->request->params['_Token'] = 'testKey';
  2028. $this->assertEquals([], $this->Form->fields);
  2029. $options = ['1' => 'option1', '2' => 'option2'];
  2030. $this->Form->radio('Test.test', $options);
  2031. $expected = ['Test.test'];
  2032. $this->assertEquals($expected, $this->Form->fields);
  2033. $this->Form->radio('Test.all', $options, [
  2034. 'disabled' => ['option1', 'option2']
  2035. ]);
  2036. $expected = ['Test.test', 'Test.all' => ''];
  2037. $this->assertEquals($expected, $this->Form->fields);
  2038. $this->Form->radio('Test.some', $options, [
  2039. 'disabled' => ['option1']
  2040. ]);
  2041. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  2042. $this->assertEquals($expected, $this->Form->fields);
  2043. }
  2044. /**
  2045. * testFormSecuredAndDisabledNotAssoc method
  2046. *
  2047. * Test that when disabled is in a list based attribute array it works.
  2048. *
  2049. * @return void
  2050. */
  2051. public function testFormSecuredAndDisabledNotAssoc()
  2052. {
  2053. $this->Form->request->params['_Token'] = 'testKey';
  2054. $this->Form->select('Model.select', [1, 2], ['disabled']);
  2055. $this->Form->checkbox('Model.checkbox', ['disabled']);
  2056. $this->Form->text('Model.text', ['disabled']);
  2057. $this->Form->textarea('Model.textarea', ['disabled']);
  2058. $this->Form->password('Model.password', ['disabled']);
  2059. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2060. $expected = [
  2061. 'Model.radio' => ''
  2062. ];
  2063. $this->assertEquals($expected, $this->Form->fields);
  2064. }
  2065. /**
  2066. * testFormSecuredAndDisabled method
  2067. *
  2068. * Test that forms with disabled inputs + secured forms leave off the inputs from the form
  2069. * hashing.
  2070. *
  2071. * @return void
  2072. */
  2073. public function testFormSecuredAndDisabled()
  2074. {
  2075. $this->Form->request->params['_Token'] = 'testKey';
  2076. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2077. $this->Form->text('Model.text', ['disabled' => true]);
  2078. $this->Form->password('Model.text', ['disabled' => true]);
  2079. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2080. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2081. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2082. $this->Form->year('Model.year', ['disabled' => true]);
  2083. $this->Form->month('Model.month', ['disabled' => true]);
  2084. $this->Form->day('Model.day', ['disabled' => true]);
  2085. $this->Form->hour('Model.hour', ['disabled' => true]);
  2086. $this->Form->minute('Model.minute', ['disabled' => true]);
  2087. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2088. $expected = [
  2089. 'Model.radio' => ''
  2090. ];
  2091. $this->assertEquals($expected, $this->Form->fields);
  2092. }
  2093. /**
  2094. * testDisableSecurityUsingForm method
  2095. *
  2096. * @return void
  2097. */
  2098. public function testDisableSecurityUsingForm()
  2099. {
  2100. $this->Form->request->params['_Token'] = [
  2101. 'disabledFields' => []
  2102. ];
  2103. $this->Form->create();
  2104. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2105. $this->Form->text('Addresses.title');
  2106. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2107. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2108. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2109. $result = $this->Form->fields;
  2110. $expected = [
  2111. 'Addresses.id' => '123456', 'Addresses.title',
  2112. ];
  2113. $this->assertEquals($expected, $result);
  2114. }
  2115. /**
  2116. * testUnlockFieldAddsToList method
  2117. *
  2118. * Test disableField.
  2119. *
  2120. * @return void
  2121. */
  2122. public function testUnlockFieldAddsToList()
  2123. {
  2124. $this->Form->request->params['_Token'] = [
  2125. 'unlockedFields' => []
  2126. ];
  2127. $this->Form->unlockField('Contact.name');
  2128. $this->Form->text('Contact.name');
  2129. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2130. $this->assertEquals([], $this->Form->fields);
  2131. }
  2132. /**
  2133. * testUnlockFieldRemovingFromFields method
  2134. *
  2135. * Test unlockField removing from fields array.
  2136. *
  2137. * @return void
  2138. */
  2139. public function testUnlockFieldRemovingFromFields()
  2140. {
  2141. $this->Form->request->params['_Token'] = [
  2142. 'unlockedFields' => []
  2143. ];
  2144. $this->Form->create($this->article);
  2145. $this->Form->hidden('Article.id', ['value' => 1]);
  2146. $this->Form->text('Article.title');
  2147. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2148. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2149. $this->Form->unlockField('Article.title');
  2150. $this->Form->unlockField('Article.id');
  2151. $this->assertEquals([], $this->Form->fields);
  2152. }
  2153. /**
  2154. * testResetUnlockFields method
  2155. *
  2156. * Test reset unlockFields, when create new form.
  2157. *
  2158. * @return void
  2159. */
  2160. public function testResetUnlockFields()
  2161. {
  2162. $this->Form->request->params['_Token'] = [
  2163. 'key' => 'testKey',
  2164. 'unlockedFields' => []
  2165. ];
  2166. $this->Form->unlockField('Contact.id');
  2167. $this->Form->create('Contact');
  2168. $this->Form->hidden('Contact.id', ['value' => 1]);
  2169. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2170. $this->Form->end();
  2171. $this->Form->create('Contact');
  2172. $this->Form->hidden('Contact.id', ['value' => 1]);
  2173. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2174. }
  2175. /**
  2176. * testSecuredFormUrlIgnoresHost method
  2177. *
  2178. * Test that only the path + query elements of a form's URL show up in their hash.
  2179. *
  2180. * @return void
  2181. */
  2182. public function testSecuredFormUrlIgnoresHost()
  2183. {
  2184. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2185. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2186. $this->Form->create($this->article, [
  2187. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2188. ]);
  2189. $result = $this->Form->secure();
  2190. $this->assertContains($expected, $result);
  2191. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2192. $result = $this->Form->secure();
  2193. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2194. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2195. $result = $this->Form->secure();
  2196. $this->assertContains($expected, $result, 'URL path + query should work.');
  2197. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2198. $result = $this->Form->secure();
  2199. $this->assertNotContains($expected, $result, 'URL is different');
  2200. }
  2201. /**
  2202. * testSecuredFormUrlHasHtmlAndIdentifier method
  2203. *
  2204. * Test that URL, HTML and identifier show up in their hashes.
  2205. *
  2206. * @return void
  2207. */
  2208. public function testSecuredFormUrlHasHtmlAndIdentifier()
  2209. {
  2210. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  2211. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2212. $this->Form->create($this->article, [
  2213. 'url' => [
  2214. 'controller' => 'articles',
  2215. 'action' => 'view',
  2216. '?' => [
  2217. 'page' => 1,
  2218. 'limit' => 10,
  2219. 'html' => '<>"',
  2220. ],
  2221. '#' => 'result',
  2222. ],
  2223. ]);
  2224. $result = $this->Form->secure();
  2225. $this->assertContains($expected, $result);
  2226. $this->Form->create($this->article, [
  2227. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2228. ]);
  2229. $result = $this->Form->secure();
  2230. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2231. $this->Form->create($this->article, [
  2232. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2233. ]);
  2234. $result = $this->Form->secure();
  2235. $this->assertContains($expected, $result, 'URL path + query should work.');
  2236. }
  2237. /**
  2238. * testErrorMessageDisplay method
  2239. *
  2240. * Test error message display.
  2241. *
  2242. * @return void
  2243. */
  2244. public function testErrorMessageDisplay()
  2245. {
  2246. $this->article['errors'] = [
  2247. 'Article' => [
  2248. 'title' => 'error message',
  2249. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2250. ]
  2251. ];
  2252. $this->Form->create($this->article);
  2253. $result = $this->Form->control('Article.title');
  2254. $expected = [
  2255. 'div' => ['class' => 'input text error'],
  2256. 'label' => ['for' => 'article-title'],
  2257. 'Title',
  2258. '/label',
  2259. 'input' => [
  2260. 'type' => 'text', 'name' => 'Article[title]',
  2261. 'id' => 'article-title', 'class' => 'form-error'
  2262. ],
  2263. ['div' => ['class' => 'error-message']],
  2264. 'error message',
  2265. '/div',
  2266. '/div'
  2267. ];
  2268. $this->assertHtml($expected, $result);
  2269. $result = $this->Form->control('Article.title', [
  2270. 'templates' => [
  2271. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2272. ]
  2273. ]);
  2274. $expected = [
  2275. 'div' => ['class' => 'input text error'],
  2276. 'label' => ['for' => 'article-title'],
  2277. 'Title',
  2278. '/label',
  2279. 'input' => [
  2280. 'type' => 'text', 'name' => 'Article[title]',
  2281. 'id' => 'article-title', 'class' => 'form-error'
  2282. ],
  2283. '/div'
  2284. ];
  2285. $this->assertHtml($expected, $result);
  2286. $result = $this->Form->control('Article.content');
  2287. $expected = [
  2288. 'div' => ['class' => 'input text error'],
  2289. 'label' => ['for' => 'article-content'],
  2290. 'Content',
  2291. '/label',
  2292. 'input' => [
  2293. 'type' => 'text', 'name' => 'Article[content]',
  2294. 'id' => 'article-content', 'class' => 'form-error'
  2295. ],
  2296. ['div' => ['class' => 'error-message']],
  2297. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2298. '/div',
  2299. '/div'
  2300. ];
  2301. $this->assertHtml($expected, $result);
  2302. $result = $this->Form->control('Article.content', ['error' => ['escape' => true]]);
  2303. $expected = [
  2304. 'div' => ['class' => 'input text error'],
  2305. 'label' => ['for' => 'article-content'],
  2306. 'Content',
  2307. '/label',
  2308. 'input' => [
  2309. 'type' => 'text', 'name' => 'Article[content]',
  2310. 'id' => 'article-content', 'class' => 'form-error'
  2311. ],
  2312. ['div' => ['class' => 'error-message']],
  2313. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2314. '/div',
  2315. '/div'
  2316. ];
  2317. $this->assertHtml($expected, $result);
  2318. $result = $this->Form->control('Article.content', ['error' => ['escape' => false]]);
  2319. $expected = [
  2320. 'div' => ['class' => 'input text error'],
  2321. 'label' => ['for' => 'article-content'],
  2322. 'Content',
  2323. '/label',
  2324. 'input' => [
  2325. 'type' => 'text', 'name' => 'Article[content]',
  2326. 'id' => 'article-content', 'class' => 'form-error'
  2327. ],
  2328. ['div' => ['class' => 'error-message']],
  2329. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2330. '/div',
  2331. '/div'
  2332. ];
  2333. $this->assertHtml($expected, $result);
  2334. }
  2335. /**
  2336. * testEmptyErrorValidation method
  2337. *
  2338. * Test validation errors, when validation message is an empty string.
  2339. *
  2340. * @return void
  2341. */
  2342. public function testEmptyErrorValidation()
  2343. {
  2344. $this->article['errors'] = [
  2345. 'Article' => ['title' => '']
  2346. ];
  2347. $this->Form->create($this->article);
  2348. $result = $this->Form->control('Article.title');
  2349. $expected = [
  2350. 'div' => ['class' => 'input text error'],
  2351. 'label' => ['for' => 'article-title'],
  2352. 'Title',
  2353. '/label',
  2354. 'input' => [
  2355. 'type' => 'text', 'name' => 'Article[title]',
  2356. 'id' => 'article-title', 'class' => 'form-error'
  2357. ],
  2358. ['div' => ['class' => 'error-message']],
  2359. [],
  2360. '/div',
  2361. '/div'
  2362. ];
  2363. $this->assertHtml($expected, $result);
  2364. }
  2365. /**
  2366. * testEmptyControlErrorValidation method
  2367. *
  2368. * Test validation errors, when calling control() overriding validation message by an empty string.
  2369. *
  2370. * @return void
  2371. */
  2372. public function testEmptyControlErrorValidation()
  2373. {
  2374. $this->article['errors'] = [
  2375. 'Article' => ['title' => 'error message']
  2376. ];
  2377. $this->Form->create($this->article);
  2378. $result = $this->Form->control('Article.title', ['error' => '']);
  2379. $expected = [
  2380. 'div' => ['class' => 'input text error'],
  2381. 'label' => ['for' => 'article-title'],
  2382. 'Title',
  2383. '/label',
  2384. 'input' => [
  2385. 'type' => 'text', 'name' => 'Article[title]',
  2386. 'id' => 'article-title', 'class' => 'form-error'
  2387. ],
  2388. ['div' => ['class' => 'error-message']],
  2389. [],
  2390. '/div',
  2391. '/div'
  2392. ];
  2393. $this->assertHtml($expected, $result);
  2394. }
  2395. /**
  2396. * testControlErrorMessage method
  2397. *
  2398. * Test validation errors, when calling control() overriding validation messages.
  2399. *
  2400. * @return void
  2401. */
  2402. public function testControlErrorMessage()
  2403. {
  2404. $this->article['errors'] = [
  2405. 'title' => ['error message']
  2406. ];
  2407. $this->Form->create($this->article);
  2408. $result = $this->Form->control('title', [
  2409. 'error' => 'Custom error!'
  2410. ]);
  2411. $expected = [
  2412. 'div' => ['class' => 'input text required error'],
  2413. 'label' => ['for' => 'title'],
  2414. 'Title',
  2415. '/label',
  2416. 'input' => [
  2417. 'type' => 'text', 'name' => 'title',
  2418. 'id' => 'title', 'class' => 'form-error',
  2419. 'required' => 'required',
  2420. ],
  2421. ['div' => ['class' => 'error-message']],
  2422. 'Custom error!',
  2423. '/div',
  2424. '/div'
  2425. ];
  2426. $this->assertHtml($expected, $result);
  2427. $result = $this->Form->control('title', [
  2428. 'error' => ['error message' => 'Custom error!']
  2429. ]);
  2430. $expected = [
  2431. 'div' => ['class' => 'input text required error'],
  2432. 'label' => ['for' => 'title'],
  2433. 'Title',
  2434. '/label',
  2435. 'input' => [
  2436. 'type' => 'text',
  2437. 'name' => 'title',
  2438. 'id' => 'title',
  2439. 'class' => 'form-error',
  2440. 'required' => 'required'
  2441. ],
  2442. ['div' => ['class' => 'error-message']],
  2443. 'Custom error!',
  2444. '/div',
  2445. '/div'
  2446. ];
  2447. $this->assertHtml($expected, $result);
  2448. }
  2449. /**
  2450. * testFormValidationAssociated method
  2451. *
  2452. * Tests displaying errors for nested entities.
  2453. *
  2454. * @return void
  2455. */
  2456. public function testFormValidationAssociated()
  2457. {
  2458. $nested = new Entity(['foo' => 'bar']);
  2459. $nested->errors('foo', ['not a valid bar']);
  2460. $entity = new Entity(['nested' => $nested]);
  2461. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2462. $result = $this->Form->error('nested.foo');
  2463. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2464. }
  2465. /**
  2466. * testFormValidationAssociatedSecondLevel method
  2467. *
  2468. * Test form error display with associated model.
  2469. *
  2470. * @return void
  2471. */
  2472. public function testFormValidationAssociatedSecondLevel()
  2473. {
  2474. $inner = new Entity(['bar' => 'baz']);
  2475. $nested = new Entity(['foo' => $inner]);
  2476. $entity = new Entity(['nested' => $nested]);
  2477. $inner->errors('bar', ['not a valid one']);
  2478. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2479. $result = $this->Form->error('nested.foo.bar');
  2480. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2481. }
  2482. /**
  2483. * testFormValidationMultiRecord method
  2484. *
  2485. * Test form error display with multiple records.
  2486. *
  2487. * @return void
  2488. */
  2489. public function testFormValidationMultiRecord()
  2490. {
  2491. $one = new Entity();
  2492. $two = new Entity();
  2493. TableRegistry::get('Contacts', [
  2494. 'className' => __NAMESPACE__ . '\ContactsTable'
  2495. ]);
  2496. $one->set('email', '');
  2497. $one->errors('email', ['invalid email']);
  2498. $two->set('name', '');
  2499. $two->errors('name', ['This is wrong']);
  2500. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2501. $result = $this->Form->control('0.email');
  2502. $expected = [
  2503. 'div' => ['class' => 'input email error'],
  2504. 'label' => ['for' => '0-email'],
  2505. 'Email',
  2506. '/label',
  2507. 'input' => [
  2508. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2509. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2510. ],
  2511. ['div' => ['class' => 'error-message']],
  2512. 'invalid email',
  2513. '/div',
  2514. '/div'
  2515. ];
  2516. $this->assertHtml($expected, $result);
  2517. $result = $this->Form->control('1.name');
  2518. $expected = [
  2519. 'div' => ['class' => 'input text error'],
  2520. 'label' => ['for' => '1-name'],
  2521. 'Name',
  2522. '/label',
  2523. 'input' => [
  2524. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2525. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2526. ],
  2527. ['div' => ['class' => 'error-message']],
  2528. 'This is wrong',
  2529. '/div',
  2530. '/div'
  2531. ];
  2532. $this->assertHtml($expected, $result);
  2533. }
  2534. /**
  2535. * testControl method
  2536. *
  2537. * Test various incarnations of control().
  2538. *
  2539. * @return void
  2540. */
  2541. public function testControl()
  2542. {
  2543. TableRegistry::get('ValidateUsers', [
  2544. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2545. ]);
  2546. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2547. $result = $this->Form->control('ValidateUsers.balance');
  2548. $expected = [
  2549. 'div' => ['class'],
  2550. 'label' => ['for'],
  2551. 'Balance',
  2552. '/label',
  2553. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2554. '/div',
  2555. ];
  2556. $this->assertHtml($expected, $result);
  2557. $result = $this->Form->control('ValidateUser.cost_decimal');
  2558. $expected = [
  2559. 'div' => ['class'],
  2560. 'label' => ['for'],
  2561. 'Cost Decimal',
  2562. '/label',
  2563. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2564. '/div',
  2565. ];
  2566. $this->assertHtml($expected, $result);
  2567. $result = $this->Form->control('ValidateUser.null_decimal');
  2568. $expected = [
  2569. 'div' => ['class'],
  2570. 'label' => ['for'],
  2571. 'Null Decimal',
  2572. '/label',
  2573. 'input' => ['name', 'type' => 'number', 'id'],
  2574. '/div',
  2575. ];
  2576. $this->assertHtml($expected, $result);
  2577. }
  2578. /**
  2579. * testControlCustomization method
  2580. *
  2581. * Tests the input method and passing custom options.
  2582. *
  2583. * @return void
  2584. */
  2585. public function testControlCustomization()
  2586. {
  2587. TableRegistry::get('Contacts', [
  2588. 'className' => __NAMESPACE__ . '\ContactsTable'
  2589. ]);
  2590. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2591. $result = $this->Form->control('Contact.email', ['id' => 'custom']);
  2592. $expected = [
  2593. 'div' => ['class' => 'input email'],
  2594. 'label' => ['for' => 'custom'],
  2595. 'Email',
  2596. '/label',
  2597. ['input' => [
  2598. 'type' => 'email', 'name' => 'Contact[email]',
  2599. 'id' => 'custom', 'maxlength' => 255
  2600. ]],
  2601. '/div'
  2602. ];
  2603. $this->assertHtml($expected, $result);
  2604. $result = $this->Form->control('Contact.email', [
  2605. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2606. ]);
  2607. $expected = [
  2608. '<div',
  2609. 'label' => ['for' => 'contact-email'],
  2610. 'Email',
  2611. '/label',
  2612. ['input' => [
  2613. 'type' => 'email', 'name' => 'Contact[email]',
  2614. 'id' => 'contact-email', 'maxlength' => 255
  2615. ]],
  2616. '/div'
  2617. ];
  2618. $this->assertHtml($expected, $result);
  2619. $result = $this->Form->control('Contact.email', ['type' => 'text']);
  2620. $expected = [
  2621. 'div' => ['class' => 'input text'],
  2622. 'label' => ['for' => 'contact-email'],
  2623. 'Email',
  2624. '/label',
  2625. ['input' => [
  2626. 'type' => 'text', 'name' => 'Contact[email]',
  2627. 'id' => 'contact-email', 'maxlength' => '255'
  2628. ]],
  2629. '/div'
  2630. ];
  2631. $this->assertHtml($expected, $result);
  2632. $result = $this->Form->control('Contact.5.email', ['type' => 'text']);
  2633. $expected = [
  2634. 'div' => ['class' => 'input text'],
  2635. 'label' => ['for' => 'contact-5-email'],
  2636. 'Email',
  2637. '/label',
  2638. ['input' => [
  2639. 'type' => 'text', 'name' => 'Contact[5][email]',
  2640. 'id' => 'contact-5-email', 'maxlength' => '255'
  2641. ]],
  2642. '/div'
  2643. ];
  2644. $this->assertHtml($expected, $result);
  2645. $result = $this->Form->control('Contact.password');
  2646. $expected = [
  2647. 'div' => ['class' => 'input password'],
  2648. 'label' => ['for' => 'contact-password'],
  2649. 'Password',
  2650. '/label',
  2651. ['input' => [
  2652. 'type' => 'password', 'name' => 'Contact[password]',
  2653. 'id' => 'contact-password'
  2654. ]],
  2655. '/div'
  2656. ];
  2657. $this->assertHtml($expected, $result);
  2658. $result = $this->Form->control('Contact.email', [
  2659. 'type' => 'file', 'class' => 'textbox'
  2660. ]);
  2661. $expected = [
  2662. 'div' => ['class' => 'input file'],
  2663. 'label' => ['for' => 'contact-email'],
  2664. 'Email',
  2665. '/label',
  2666. ['input' => [
  2667. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2668. 'id' => 'contact-email'
  2669. ]],
  2670. '/div'
  2671. ];
  2672. $this->assertHtml($expected, $result);
  2673. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2674. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2675. $result = $this->Form->control('phone');
  2676. $expected = [
  2677. 'div' => ['class' => 'input tel'],
  2678. 'label' => ['for' => 'phone'],
  2679. 'Phone',
  2680. '/label',
  2681. ['input' => [
  2682. 'type' => 'tel', 'name' => 'phone',
  2683. 'value' => 'Hello &amp; World &gt; weird chars',
  2684. 'id' => 'phone', 'maxlength' => 255
  2685. ]],
  2686. '/div'
  2687. ];
  2688. $this->assertHtml($expected, $result);
  2689. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2690. $this->Form->create();
  2691. $result = $this->Form->control('Model.0.OtherModel.field', ['id' => 'myId']);
  2692. $expected = [
  2693. 'div' => ['class' => 'input text'],
  2694. 'label' => ['for' => 'myId'],
  2695. 'Field',
  2696. '/label',
  2697. 'input' => [
  2698. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2699. 'value' => 'My value', 'id' => 'myId'
  2700. ],
  2701. '/div'
  2702. ];
  2703. $this->assertHtml($expected, $result);
  2704. $this->Form->request->data = [];
  2705. $entity->errors('field', 'Badness!');
  2706. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2707. $result = $this->Form->control('field');
  2708. $expected = [
  2709. 'div' => ['class' => 'input text error'],
  2710. 'label' => ['for' => 'field'],
  2711. 'Field',
  2712. '/label',
  2713. 'input' => [
  2714. 'type' => 'text', 'name' => 'field',
  2715. 'id' => 'field', 'class' => 'form-error'
  2716. ],
  2717. ['div' => ['class' => 'error-message']],
  2718. 'Badness!',
  2719. '/div',
  2720. '/div'
  2721. ];
  2722. $this->assertHtml($expected, $result);
  2723. $result = $this->Form->control('field', [
  2724. 'templates' => [
  2725. 'inputContainerError' => '{{content}}{{error}}',
  2726. 'error' => '<span class="error-message">{{content}}</span>'
  2727. ]
  2728. ]);
  2729. $expected = [
  2730. 'label' => ['for' => 'field'],
  2731. 'Field',
  2732. '/label',
  2733. 'input' => [
  2734. 'type' => 'text', 'name' => 'field',
  2735. 'id' => 'field', 'class' => 'form-error'
  2736. ],
  2737. ['span' => ['class' => 'error-message']],
  2738. 'Badness!',
  2739. '/span'
  2740. ];
  2741. $this->assertHtml($expected, $result);
  2742. $entity->errors('field', ['minLength'], true);
  2743. $result = $this->Form->control('field', [
  2744. 'error' => [
  2745. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2746. 'maxLength' => 'login too large'
  2747. ]
  2748. ]);
  2749. $expected = [
  2750. 'div' => ['class' => 'input text error'],
  2751. 'label' => ['for' => 'field'],
  2752. 'Field',
  2753. '/label',
  2754. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2755. ['div' => ['class' => 'error-message']],
  2756. 'Le login doit contenir au moins 2 caractères',
  2757. '/div',
  2758. '/div'
  2759. ];
  2760. $this->assertHtml($expected, $result);
  2761. $entity->errors('field', ['maxLength'], true);
  2762. $result = $this->Form->control('field', [
  2763. 'error' => [
  2764. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2765. 'maxLength' => 'login too large',
  2766. ]
  2767. ]);
  2768. $expected = [
  2769. 'div' => ['class' => 'input text error'],
  2770. 'label' => ['for' => 'field'],
  2771. 'Field',
  2772. '/label',
  2773. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2774. ['div' => ['class' => 'error-message']],
  2775. 'login too large',
  2776. '/div',
  2777. '/div'
  2778. ];
  2779. $this->assertHtml($expected, $result);
  2780. }
  2781. /**
  2782. * testControlWithTemplateFile method
  2783. *
  2784. * Test that control() accepts a template file.
  2785. *
  2786. * @return void
  2787. */
  2788. public function testControlWithTemplateFile()
  2789. {
  2790. $result = $this->Form->control('field', [
  2791. 'templates' => 'htmlhelper_tags'
  2792. ]);
  2793. $expected = [
  2794. 'label' => ['for' => 'field'],
  2795. 'Field',
  2796. '/label',
  2797. 'input' => [
  2798. 'type' => 'text', 'name' => 'field',
  2799. 'id' => 'field'
  2800. ],
  2801. ];
  2802. $this->assertHtml($expected, $result);
  2803. }
  2804. /**
  2805. * testNestedControlsEndWithBrackets method
  2806. *
  2807. * Test that nested inputs end with brackets.
  2808. *
  2809. * @return void
  2810. */
  2811. public function testNestedControlsEndWithBrackets()
  2812. {
  2813. $result = $this->Form->text('nested.text[]');
  2814. $expected = [
  2815. 'input' => [
  2816. 'type' => 'text', 'name' => 'nested[text][]'
  2817. ],
  2818. ];
  2819. $this->assertHtml($expected, $result);
  2820. $result = $this->Form->file('nested.file[]');
  2821. $expected = [
  2822. 'input' => [
  2823. 'type' => 'file', 'name' => 'nested[file][]'
  2824. ],
  2825. ];
  2826. $this->assertHtml($expected, $result);
  2827. }
  2828. /**
  2829. * testCreateIdPrefix method
  2830. *
  2831. * Test id prefix.
  2832. *
  2833. * @return void
  2834. */
  2835. public function testCreateIdPrefix()
  2836. {
  2837. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2838. $result = $this->Form->control('field');
  2839. $expected = [
  2840. 'div' => ['class' => 'input text'],
  2841. 'label' => ['for' => 'prefix-field'],
  2842. 'Field',
  2843. '/label',
  2844. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2845. '/div'
  2846. ];
  2847. $this->assertHtml($expected, $result);
  2848. $result = $this->Form->control('field', ['id' => 'custom-id']);
  2849. $expected = [
  2850. 'div' => ['class' => 'input text'],
  2851. 'label' => ['for' => 'custom-id'],
  2852. 'Field',
  2853. '/label',
  2854. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2855. '/div'
  2856. ];
  2857. $this->assertHtml($expected, $result);
  2858. $result = $this->Form->radio('Model.field', ['option A']);
  2859. $expected = [
  2860. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2861. 'label' => ['for' => 'prefix-model-field-0'],
  2862. ['input' => [
  2863. 'type' => 'radio',
  2864. 'name' => 'Model[field]',
  2865. 'value' => '0',
  2866. 'id' => 'prefix-model-field-0'
  2867. ]],
  2868. 'option A',
  2869. '/label'
  2870. ];
  2871. $this->assertHtml($expected, $result);
  2872. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2873. $expected = [
  2874. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2875. 'label' => ['for' => 'prefix-model-field-0'],
  2876. ['input' => [
  2877. 'type' => 'radio',
  2878. 'name' => 'Model[field]',
  2879. 'value' => '0',
  2880. 'id' => 'prefix-model-field-0'
  2881. ]],
  2882. 'option A',
  2883. '/label'
  2884. ];
  2885. $this->assertHtml($expected, $result);
  2886. $result = $this->Form->select(
  2887. 'Model.multi_field',
  2888. ['first'],
  2889. ['multiple' => 'checkbox']
  2890. );
  2891. $expected = [
  2892. 'input' => [
  2893. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2894. ],
  2895. ['div' => ['class' => 'checkbox']],
  2896. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2897. ['input' => [
  2898. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2899. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2900. ]],
  2901. 'first',
  2902. '/label',
  2903. '/div',
  2904. ];
  2905. $this->assertHtml($expected, $result);
  2906. $this->Form->end();
  2907. $result = $this->Form->control('field');
  2908. $expected = [
  2909. 'div' => ['class' => 'input text'],
  2910. 'label' => ['for' => 'field'],
  2911. 'Field',
  2912. '/label',
  2913. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2914. '/div'
  2915. ];
  2916. $this->assertHtml($expected, $result);
  2917. }
  2918. /**
  2919. * testControlZero method
  2920. *
  2921. * Test that inputs with 0 can be created.
  2922. *
  2923. * @return void
  2924. */
  2925. public function testControlZero()
  2926. {
  2927. TableRegistry::get('Contacts', [
  2928. 'className' => __NAMESPACE__ . '\ContactsTable'
  2929. ]);
  2930. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2931. $result = $this->Form->control('0');
  2932. $expected = [
  2933. 'div' => ['class' => 'input text'],
  2934. 'label' => ['for' => '0'], '/label',
  2935. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2936. '/div'
  2937. ];
  2938. $this->assertHtml($expected, $result);
  2939. }
  2940. /**
  2941. * testControlCheckbox method
  2942. *
  2943. * Test control() with checkbox creation.
  2944. *
  2945. * @return void
  2946. */
  2947. public function testControlCheckbox()
  2948. {
  2949. $result = $this->Form->control('User.active', ['label' => false, 'checked' => true]);
  2950. $expected = [
  2951. 'div' => ['class' => 'input checkbox'],
  2952. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2953. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2954. '/div'
  2955. ];
  2956. $this->assertHtml($expected, $result);
  2957. $result = $this->Form->control('User.active', ['label' => false, 'checked' => 1]);
  2958. $expected = [
  2959. 'div' => ['class' => 'input checkbox'],
  2960. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2961. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2962. '/div'
  2963. ];
  2964. $this->assertHtml($expected, $result);
  2965. $result = $this->Form->control('User.active', ['label' => false, 'checked' => '1']);
  2966. $expected = [
  2967. 'div' => ['class' => 'input checkbox'],
  2968. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2969. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2970. '/div'
  2971. ];
  2972. $this->assertHtml($expected, $result);
  2973. $result = $this->Form->control('User.disabled', [
  2974. 'label' => 'Disabled',
  2975. 'type' => 'checkbox',
  2976. 'data-foo' => 'disabled'
  2977. ]);
  2978. $expected = [
  2979. 'div' => ['class' => 'input checkbox'],
  2980. 'input' => ['type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'],
  2981. 'label' => ['for' => 'user-disabled'],
  2982. ['input' => [
  2983. 'type' => 'checkbox',
  2984. 'name' => 'User[disabled]',
  2985. 'value' => '1',
  2986. 'id' => 'user-disabled',
  2987. 'data-foo' => 'disabled'
  2988. ]],
  2989. 'Disabled',
  2990. '/label',
  2991. '/div'
  2992. ];
  2993. $this->assertHtml($expected, $result);
  2994. $result = $this->Form->control('User.confirm', [
  2995. 'label' => 'Confirm <b>me</b>!',
  2996. 'type' => 'checkbox',
  2997. 'escape' => false
  2998. ]);
  2999. $expected = [
  3000. 'div' => ['class' => 'input checkbox'],
  3001. 'input' => ['type' => 'hidden', 'name' => 'User[confirm]', 'value' => '0'],
  3002. 'label' => ['for' => 'user-confirm'],
  3003. ['input' => [
  3004. 'type' => 'checkbox',
  3005. 'name' => 'User[confirm]',
  3006. 'value' => '1',
  3007. 'id' => 'user-confirm',
  3008. ]],
  3009. 'Confirm <b>me</b>!',
  3010. '/label',
  3011. '/div'
  3012. ];
  3013. $this->assertHtml($expected, $result);
  3014. }
  3015. /**
  3016. * testControlHidden method
  3017. *
  3018. * Test that control() does not create wrapping div and label tag for hidden fields.
  3019. *
  3020. * @return void
  3021. */
  3022. public function testControlHidden()
  3023. {
  3024. TableRegistry::get('ValidateUsers', [
  3025. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3026. ]);
  3027. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  3028. $result = $this->Form->control('ValidateUser.id');
  3029. $expected = [
  3030. 'input' => ['name', 'type' => 'hidden', 'id']
  3031. ];
  3032. $this->assertHtml($expected, $result);
  3033. $result = $this->Form->control('ValidateUser.custom', ['type' => 'hidden']);
  3034. $expected = [
  3035. 'input' => ['name', 'type' => 'hidden', 'id']
  3036. ];
  3037. $this->assertHtml($expected, $result);
  3038. }
  3039. /**
  3040. * testControlDatetime method
  3041. *
  3042. * Test form->control() with datetime.
  3043. *
  3044. * @return void
  3045. */
  3046. public function testControlDatetime()
  3047. {
  3048. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3049. ->setMethods(['datetime'])
  3050. ->setConstructorArgs([new View()])
  3051. ->getMock();
  3052. $this->Form->expects($this->once())->method('datetime')
  3053. ->with('prueba', [
  3054. 'type' => 'datetime',
  3055. 'timeFormat' => 24,
  3056. 'minYear' => 2008,
  3057. 'maxYear' => 2011,
  3058. 'interval' => 15,
  3059. 'options' => null,
  3060. 'empty' => false,
  3061. 'id' => 'prueba',
  3062. 'required' => false,
  3063. 'templateVars' => []
  3064. ])
  3065. ->will($this->returnValue('This is it!'));
  3066. $result = $this->Form->control('prueba', [
  3067. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3068. 'maxYear' => 2011, 'interval' => 15
  3069. ]);
  3070. $expected = [
  3071. 'div' => ['class' => 'input datetime'],
  3072. '<label',
  3073. 'Prueba',
  3074. '/label',
  3075. 'This is it!',
  3076. '/div'
  3077. ];
  3078. $this->assertHtml($expected, $result);
  3079. }
  3080. /**
  3081. * testControlDatetimeIdPrefix method
  3082. *
  3083. * Test form->control() with datetime with id prefix.
  3084. *
  3085. * @return void
  3086. */
  3087. public function testControlDatetimeIdPrefix()
  3088. {
  3089. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  3090. ->setMethods(['datetime'])
  3091. ->setConstructorArgs([new View()])
  3092. ->getMock();
  3093. $this->Form->create(false, ['idPrefix' => 'prefix']);
  3094. $this->Form->expects($this->once())->method('datetime')
  3095. ->with('prueba', [
  3096. 'type' => 'datetime',
  3097. 'timeFormat' => 24,
  3098. 'minYear' => 2008,
  3099. 'maxYear' => 2011,
  3100. 'interval' => 15,
  3101. 'options' => null,
  3102. 'empty' => false,
  3103. 'id' => 'prefix-prueba',
  3104. 'required' => false,
  3105. 'templateVars' => []
  3106. ])
  3107. ->will($this->returnValue('This is it!'));
  3108. $result = $this->Form->control('prueba', [
  3109. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  3110. 'maxYear' => 2011, 'interval' => 15
  3111. ]);
  3112. $expected = [
  3113. 'div' => ['class' => 'input datetime'],
  3114. '<label',
  3115. 'Prueba',
  3116. '/label',
  3117. 'This is it!',
  3118. '/div'
  3119. ];
  3120. $this->assertHtml($expected, $result);
  3121. }
  3122. /**
  3123. * testControlCheckboxWithDisabledElements method
  3124. *
  3125. * Test generating checkboxes with disabled elements.
  3126. *
  3127. * @return void
  3128. */
  3129. public function testControlCheckboxWithDisabledElements()
  3130. {
  3131. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3132. $result = $this->Form->control('Contact.multiple', [
  3133. 'multiple' => 'checkbox',
  3134. 'disabled' => 'disabled',
  3135. 'options' => $options
  3136. ]);
  3137. $expected = [
  3138. ['div' => ['class' => 'input select']],
  3139. ['label' => ['for' => "contact-multiple"]],
  3140. 'Multiple',
  3141. '/label',
  3142. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  3143. ['div' => ['class' => 'checkbox']],
  3144. ['label' => ['for' => "contact-multiple-1"]],
  3145. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  3146. 'One',
  3147. '/label',
  3148. '/div',
  3149. ['div' => ['class' => 'checkbox']],
  3150. ['label' => ['for' => "contact-multiple-2"]],
  3151. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  3152. 'Two',
  3153. '/label',
  3154. '/div',
  3155. ['div' => ['class' => 'checkbox']],
  3156. ['label' => ['for' => "contact-multiple-3"]],
  3157. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  3158. 'Three',
  3159. '/label',
  3160. '/div',
  3161. '/div'
  3162. ];
  3163. $this->assertHtml($expected, $result);
  3164. // make sure 50 does only disable 50, and not 50f5c0cf
  3165. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3166. $disabled = [50];
  3167. $expected = [
  3168. ['div' => ['class' => 'input select']],
  3169. ['label' => ['for' => "contact-multiple"]],
  3170. 'Multiple',
  3171. '/label',
  3172. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  3173. ['div' => ['class' => 'checkbox']],
  3174. ['label' => ['for' => "contact-multiple-50"]],
  3175. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  3176. 'Fifty',
  3177. '/label',
  3178. '/div',
  3179. ['div' => ['class' => 'checkbox']],
  3180. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  3181. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  3182. 'Stringy',
  3183. '/label',
  3184. '/div',
  3185. '/div'
  3186. ];
  3187. $result = $this->Form->control('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3188. $this->assertHtml($expected, $result);
  3189. }
  3190. /**
  3191. * testControlWithLeadingInteger method
  3192. *
  3193. * Test input name with leading integer, ensure attributes are generated correctly.
  3194. *
  3195. * @return void
  3196. */
  3197. public function testControlWithLeadingInteger()
  3198. {
  3199. $result = $this->Form->text('0.Node.title');
  3200. $expected = [
  3201. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3202. ];
  3203. $this->assertHtml($expected, $result);
  3204. }
  3205. /**
  3206. * testControlSelectType method
  3207. *
  3208. * Test form->control() with select type inputs.
  3209. *
  3210. * @return void
  3211. */
  3212. public function testControlSelectType()
  3213. {
  3214. $result = $this->Form->control(
  3215. 'email',
  3216. [
  3217. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3218. );
  3219. $expected = [
  3220. 'div' => ['class' => 'input select'],
  3221. 'label' => ['for' => 'email'],
  3222. 'Email',
  3223. '/label',
  3224. ['select' => ['name' => 'email', 'id' => 'email']],
  3225. ['option' => ['value' => '']],
  3226. '/option',
  3227. ['option' => ['value' => 'è']],
  3228. 'Firést',
  3229. '/option',
  3230. ['option' => ['value' => 'é']],
  3231. 'Secoènd',
  3232. '/option',
  3233. '/select',
  3234. '/div'
  3235. ];
  3236. $this->assertHtml($expected, $result);
  3237. $result = $this->Form->control(
  3238. 'email',
  3239. [
  3240. 'options' => ['First', 'Second'], 'empty' => true]
  3241. );
  3242. $expected = [
  3243. 'div' => ['class' => 'input select'],
  3244. 'label' => ['for' => 'email'],
  3245. 'Email',
  3246. '/label',
  3247. ['select' => ['name' => 'email', 'id' => 'email']],
  3248. ['option' => ['value' => '']],
  3249. '/option',
  3250. ['option' => ['value' => '0']],
  3251. 'First',
  3252. '/option',
  3253. ['option' => ['value' => '1']],
  3254. 'Second',
  3255. '/option',
  3256. '/select',
  3257. '/div'
  3258. ];
  3259. $this->assertHtml($expected, $result);
  3260. $result = $this->Form->control('email', [
  3261. 'type' => 'select',
  3262. 'options' => new \ArrayObject(['First', 'Second']),
  3263. 'empty' => true
  3264. ]);
  3265. $this->assertHtml($expected, $result);
  3266. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3267. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3268. $result = $this->Form->control('Model.user_id', ['empty' => true]);
  3269. $expected = [
  3270. 'div' => ['class' => 'input select'],
  3271. 'label' => ['for' => 'model-user-id'],
  3272. 'User',
  3273. '/label',
  3274. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3275. ['option' => ['value' => '']],
  3276. '/option',
  3277. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3278. 'good',
  3279. '/option',
  3280. ['option' => ['value' => 'other']],
  3281. 'bad',
  3282. '/option',
  3283. '/select',
  3284. '/div'
  3285. ];
  3286. $this->assertHtml($expected, $result);
  3287. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3288. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3289. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3290. $expected = [
  3291. 'div' => ['class' => 'input select'],
  3292. 'label' => ['for' => 'thing-user-id'],
  3293. 'User',
  3294. '/label',
  3295. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3296. ['option' => ['value' => '']],
  3297. 'Some Empty',
  3298. '/option',
  3299. ['option' => ['value' => 'value']],
  3300. 'good',
  3301. '/option',
  3302. ['option' => ['value' => 'other']],
  3303. 'bad',
  3304. '/option',
  3305. '/select',
  3306. '/div'
  3307. ];
  3308. $this->assertHtml($expected, $result);
  3309. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3310. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3311. $result = $this->Form->control('Thing.user_id', ['empty' => 'Some Empty']);
  3312. $expected = [
  3313. 'div' => ['class' => 'input select'],
  3314. 'label' => ['for' => 'thing-user-id'],
  3315. 'User',
  3316. '/label',
  3317. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3318. ['option' => ['value' => '']],
  3319. 'Some Empty',
  3320. '/option',
  3321. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3322. 'good',
  3323. '/option',
  3324. ['option' => ['value' => 'other']],
  3325. 'bad',
  3326. '/option',
  3327. '/select',
  3328. '/div'
  3329. ];
  3330. $this->assertHtml($expected, $result);
  3331. $this->Form->data = [];
  3332. $result = $this->Form->control('Publisher.id', [
  3333. 'label' => 'Publisher',
  3334. 'type' => 'select',
  3335. 'multiple' => 'checkbox',
  3336. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3337. ]);
  3338. $expected = [
  3339. ['div' => ['class' => 'input select']],
  3340. ['label' => ['for' => 'publisher-id']],
  3341. 'Publisher',
  3342. '/label',
  3343. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3344. ['div' => ['class' => 'checkbox']],
  3345. ['label' => ['for' => 'publisher-id-value-1']],
  3346. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3347. 'Label 1',
  3348. '/label',
  3349. '/div',
  3350. ['div' => ['class' => 'checkbox']],
  3351. ['label' => ['for' => 'publisher-id-value-2']],
  3352. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3353. 'Label 2',
  3354. '/label',
  3355. '/div',
  3356. '/div'
  3357. ];
  3358. $this->assertHtml($expected, $result);
  3359. }
  3360. /**
  3361. * testControlWithNonStandardPrimaryKeyMakesHidden method
  3362. *
  3363. * Test that control() and a non standard primary key makes a hidden input by default.
  3364. *
  3365. * @return void
  3366. */
  3367. public function testControlWithNonStandardPrimaryKeyMakesHidden()
  3368. {
  3369. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3370. $this->Form->create($this->article);
  3371. $result = $this->Form->control('title');
  3372. $expected = [
  3373. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3374. ];
  3375. $this->assertHtml($expected, $result);
  3376. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3377. $this->Form->create($this->article);
  3378. $result = $this->Form->control('title');
  3379. $expected = [
  3380. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3381. ];
  3382. $this->assertHtml($expected, $result);
  3383. $result = $this->Form->control('body');
  3384. $expected = [
  3385. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3386. ];
  3387. $this->assertHtml($expected, $result);
  3388. }
  3389. /**
  3390. * testControlOverridingMagicSelectType method
  3391. *
  3392. * Test that overriding the magic select type widget is possible.
  3393. *
  3394. * @return void
  3395. */
  3396. public function testControlOverridingMagicSelectType()
  3397. {
  3398. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3399. $result = $this->Form->control('Model.user_id', ['type' => 'text']);
  3400. $expected = [
  3401. 'div' => ['class' => 'input text'],
  3402. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3403. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3404. '/div'
  3405. ];
  3406. $this->assertHtml($expected, $result);
  3407. //Check that magic types still work for plural/singular vars
  3408. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3409. $result = $this->Form->control('Model.type');
  3410. $expected = [
  3411. 'div' => ['class' => 'input select'],
  3412. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3413. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3414. ['option' => ['value' => 'value']], 'good', '/option',
  3415. ['option' => ['value' => 'other']], 'bad', '/option',
  3416. '/select',
  3417. '/div'
  3418. ];
  3419. $this->assertHtml($expected, $result);
  3420. }
  3421. /**
  3422. * testControlMagicTypeDoesNotOverride method
  3423. *
  3424. * Test that inferred types do not override developer input.
  3425. *
  3426. * @return void
  3427. */
  3428. public function testControlMagicTypeDoesNotOverride()
  3429. {
  3430. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3431. $result = $this->Form->control('Model.user', ['type' => 'checkbox']);
  3432. $expected = [
  3433. 'div' => ['class' => 'input checkbox'],
  3434. ['input' => [
  3435. 'type' => 'hidden',
  3436. 'name' => 'Model[user]',
  3437. 'value' => 0,
  3438. ]],
  3439. 'label' => ['for' => 'model-user'],
  3440. ['input' => [
  3441. 'name' => 'Model[user]',
  3442. 'type' => 'checkbox',
  3443. 'id' => 'model-user',
  3444. 'value' => 1
  3445. ]],
  3446. 'User',
  3447. '/label',
  3448. '/div'
  3449. ];
  3450. $this->assertHtml($expected, $result);
  3451. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3452. $options = [
  3453. 1 => 'blue',
  3454. 2 => 'red'
  3455. ];
  3456. $result = $this->Form->control('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3457. $expected = [
  3458. 'div' => ['class' => 'input select'],
  3459. 'label' => ['for' => 'tags-ids'],
  3460. 'Tags',
  3461. '/label',
  3462. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3463. ['div' => ['class' => 'checkbox']],
  3464. ['label' => ['for' => 'tags-ids-1']],
  3465. ['input' => [
  3466. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3467. 'value' => '1', 'name' => 'tags[_ids][]'
  3468. ]],
  3469. 'blue',
  3470. '/label',
  3471. '/div',
  3472. ['div' => ['class' => 'checkbox']],
  3473. ['label' => ['for' => 'tags-ids-2']],
  3474. ['input' => [
  3475. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3476. 'value' => '2', 'name' => 'tags[_ids][]'
  3477. ]],
  3478. 'red',
  3479. '/label',
  3480. '/div',
  3481. '/div'
  3482. ];
  3483. $this->assertHtml($expected, $result);
  3484. }
  3485. /**
  3486. * testControlMagicSelectForTypeNumber method
  3487. *
  3488. * Test that magic control() selects are created for type=number.
  3489. *
  3490. * @return void
  3491. */
  3492. public function testControlMagicSelectForTypeNumber()
  3493. {
  3494. TableRegistry::get('ValidateUsers', [
  3495. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3496. ]);
  3497. $entity = new Entity(['balance' => 1]);
  3498. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3499. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3500. $result = $this->Form->control('balance');
  3501. $expected = [
  3502. 'div' => ['class' => 'input select'],
  3503. 'label' => ['for' => 'balance'],
  3504. 'Balance',
  3505. '/label',
  3506. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3507. ['option' => ['value' => '0']],
  3508. 'nothing',
  3509. '/option',
  3510. ['option' => ['value' => '1', 'selected' => 'selected']],
  3511. 'some',
  3512. '/option',
  3513. ['option' => ['value' => '100']],
  3514. 'a lot',
  3515. '/option',
  3516. '/select',
  3517. '/div'
  3518. ];
  3519. $this->assertHtml($expected, $result);
  3520. }
  3521. /**
  3522. * testInvalidControlTypeOption method
  3523. *
  3524. * Test invalid 'input' type option to control() function.
  3525. *
  3526. * @expectedException \RuntimeException
  3527. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3528. * @return void
  3529. */
  3530. public function testInvalidControlTypeOption()
  3531. {
  3532. $this->Form->control('text', ['type' => 'input']);
  3533. }
  3534. /**
  3535. * testControlMagicSelectChangeToRadio method
  3536. *
  3537. * Test that magic control() selects can easily be converted into radio types without error.
  3538. *
  3539. * @return void
  3540. */
  3541. public function testControlMagicSelectChangeToRadio()
  3542. {
  3543. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3544. $result = $this->Form->control('Model.user_id', ['type' => 'radio']);
  3545. $this->assertContains('input type="radio"', $result);
  3546. }
  3547. /**
  3548. * testFormControlSubmit method
  3549. *
  3550. * Test correct results for form::control() and type submit.
  3551. *
  3552. * @return void
  3553. */
  3554. public function testFormControlSubmit()
  3555. {
  3556. $result = $this->Form->control('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3557. $expected = [
  3558. 'div' => ['class' => 'submit'],
  3559. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3560. '/div'
  3561. ];
  3562. $this->assertHtml($expected, $result);
  3563. }
  3564. /**
  3565. * testFormControls method
  3566. *
  3567. * Test correct results from Form::controls().
  3568. *
  3569. * @return void
  3570. */
  3571. public function testFormControlsLegendFieldset()
  3572. {
  3573. $this->Form->create($this->article);
  3574. $result = $this->Form->allControls([], ['legend' => 'The Legend']);
  3575. $expected = [
  3576. '<fieldset',
  3577. '<legend',
  3578. 'The Legend',
  3579. '/legend',
  3580. '*/fieldset',
  3581. ];
  3582. $this->assertHtml($expected, $result);
  3583. $result = $this->Form->allControls([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3584. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3585. $this->assertContains('<fieldset>', $result);
  3586. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => false]);
  3587. $this->assertNotContains('<legend>', $result);
  3588. $this->assertNotContains('<fieldset>', $result);
  3589. $result = $this->Form->allControls([], ['fieldset' => false, 'legend' => 'Hello']);
  3590. $this->assertNotContains('<legend>', $result);
  3591. $this->assertNotContains('<fieldset>', $result);
  3592. $this->Form->create($this->article);
  3593. $this->Form->request->params['prefix'] = 'admin';
  3594. $this->Form->request->params['action'] = 'admin_edit';
  3595. $this->Form->request->params['controller'] = 'articles';
  3596. $result = $this->Form->allControls();
  3597. $expected = [
  3598. '<fieldset',
  3599. '<legend',
  3600. 'New Article',
  3601. '/legend',
  3602. '*/fieldset',
  3603. ];
  3604. $this->assertHtml($expected, $result);
  3605. $this->Form->create($this->article);
  3606. $result = $this->Form->allControls([], ['fieldset' => [], 'legend' => 'The Legend']);
  3607. $expected = [
  3608. '<fieldset',
  3609. '<legend',
  3610. 'The Legend',
  3611. '/legend',
  3612. '*/fieldset',
  3613. ];
  3614. $this->assertHtml($expected, $result);
  3615. $this->Form->create($this->article);
  3616. $result = $this->Form->allControls([], [
  3617. 'fieldset' => [
  3618. 'class' => 'some-class some-other-class',
  3619. 'disabled' => true,
  3620. 'data-param' => 'a-param'
  3621. ],
  3622. 'legend' => 'The Legend'
  3623. ]);
  3624. $expected = [
  3625. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3626. '<legend',
  3627. 'The Legend',
  3628. '/legend',
  3629. '*/fieldset',
  3630. ];
  3631. $this->assertHtml($expected, $result);
  3632. }
  3633. /**
  3634. * testFormControls method
  3635. *
  3636. * Test the controls() method.
  3637. *
  3638. * @return void
  3639. */
  3640. public function testFormControls()
  3641. {
  3642. $this->Form->create($this->article);
  3643. $result = $this->Form->allControls();
  3644. $expected = [
  3645. '<fieldset',
  3646. '<legend', 'New Article', '/legend',
  3647. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3648. ['div' => ['class' => 'input select required']],
  3649. '*/div',
  3650. ['div' => ['class' => 'input text required']],
  3651. '*/div',
  3652. ['div' => ['class' => 'input text']],
  3653. '*/div',
  3654. ['div' => ['class' => 'input text']],
  3655. '*/div',
  3656. '/fieldset',
  3657. ];
  3658. $this->assertHtml($expected, $result);
  3659. $result = $this->Form->allControls([
  3660. 'published' => ['type' => 'boolean']
  3661. ]);
  3662. $expected = [
  3663. '<fieldset',
  3664. '<legend', 'New Article', '/legend',
  3665. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3666. ['div' => ['class' => 'input select required']],
  3667. '*/div',
  3668. ['div' => ['class' => 'input text required']],
  3669. '*/div',
  3670. ['div' => ['class' => 'input text']],
  3671. '*/div',
  3672. ['div' => ['class' => 'input boolean']],
  3673. '*/div',
  3674. '/fieldset',
  3675. ];
  3676. $this->assertHtml($expected, $result);
  3677. $this->Form->create($this->article);
  3678. $result = $this->Form->allControls([], ['legend' => 'Hello']);
  3679. $expected = [
  3680. 'fieldset' => [],
  3681. 'legend' => [],
  3682. 'Hello',
  3683. '/legend',
  3684. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3685. ['div' => ['class' => 'input select required']],
  3686. '*/div',
  3687. ['div' => ['class' => 'input text required']],
  3688. '*/div',
  3689. ['div' => ['class' => 'input text']],
  3690. '*/div',
  3691. ['div' => ['class' => 'input text']],
  3692. '*/div',
  3693. '/fieldset'
  3694. ];
  3695. $this->assertHtml($expected, $result);
  3696. $this->Form->create(false);
  3697. $expected = [
  3698. 'fieldset' => [],
  3699. ['div' => ['class' => 'input text']],
  3700. 'label' => ['for' => 'foo'],
  3701. 'Foo',
  3702. '/label',
  3703. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3704. '*/div',
  3705. '/fieldset'
  3706. ];
  3707. $result = $this->Form->allControls(
  3708. ['foo' => ['type' => 'text']],
  3709. ['legend' => false]
  3710. );
  3711. $this->assertHtml($expected, $result);
  3712. }
  3713. /**
  3714. * testFormControlsBlacklist method
  3715. *
  3716. * @return void
  3717. */
  3718. public function testFormControlsBlacklist()
  3719. {
  3720. $this->Form->create($this->article);
  3721. $result = $this->Form->allControls([
  3722. 'id' => false
  3723. ]);
  3724. $expected = [
  3725. '<fieldset',
  3726. '<legend', 'New Article', '/legend',
  3727. ['div' => ['class' => 'input select required']],
  3728. '*/div',
  3729. ['div' => ['class' => 'input text required']],
  3730. '*/div',
  3731. ['div' => ['class' => 'input text']],
  3732. '*/div',
  3733. ['div' => ['class' => 'input text']],
  3734. '*/div',
  3735. '/fieldset',
  3736. ];
  3737. $this->assertHtml($expected, $result);
  3738. $this->Form->create($this->article);
  3739. $result = $this->Form->allControls([
  3740. 'id' => []
  3741. ]);
  3742. $expected = [
  3743. '<fieldset',
  3744. '<legend', 'New Article', '/legend',
  3745. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3746. ['div' => ['class' => 'input select required']],
  3747. '*/div',
  3748. ['div' => ['class' => 'input text required']],
  3749. '*/div',
  3750. ['div' => ['class' => 'input text']],
  3751. '*/div',
  3752. ['div' => ['class' => 'input text']],
  3753. '*/div',
  3754. '/fieldset',
  3755. ];
  3756. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3757. }
  3758. /**
  3759. * testSelectAsCheckbox method
  3760. *
  3761. * Test multi-select widget with checkbox formatting.
  3762. *
  3763. * @return void
  3764. */
  3765. public function testSelectAsCheckbox()
  3766. {
  3767. $result = $this->Form->select(
  3768. 'Model.multi_field',
  3769. ['first', 'second', 'third'],
  3770. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3771. );
  3772. $expected = [
  3773. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3774. ['div' => ['class' => 'checkbox']],
  3775. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3776. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3777. 'first',
  3778. '/label',
  3779. '/div',
  3780. ['div' => ['class' => 'checkbox']],
  3781. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3782. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3783. 'second',
  3784. '/label',
  3785. '/div',
  3786. ['div' => ['class' => 'checkbox']],
  3787. ['label' => ['for' => 'model-multi-field-2']],
  3788. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3789. 'third',
  3790. '/label',
  3791. '/div',
  3792. ];
  3793. $this->assertHtml($expected, $result);
  3794. $result = $this->Form->select(
  3795. 'Model.multi_field',
  3796. ['1/2' => 'half'],
  3797. ['multiple' => 'checkbox']
  3798. );
  3799. $expected = [
  3800. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3801. ['div' => ['class' => 'checkbox']],
  3802. ['label' => ['for' => 'model-multi-field-1-2']],
  3803. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3804. 'half',
  3805. '/label',
  3806. '/div',
  3807. ];
  3808. $this->assertHtml($expected, $result);
  3809. }
  3810. /**
  3811. * testLabel method
  3812. *
  3813. * Test label generation.
  3814. *
  3815. * @return void
  3816. */
  3817. public function testLabel()
  3818. {
  3819. $result = $this->Form->label('Person.name');
  3820. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3821. $this->assertHtml($expected, $result);
  3822. $result = $this->Form->label('Person.name');
  3823. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3824. $this->assertHtml($expected, $result);
  3825. $result = $this->Form->label('Person.first_name');
  3826. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3827. $this->assertHtml($expected, $result);
  3828. $result = $this->Form->label('Person.first_name', 'Your first name');
  3829. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3830. $this->assertHtml($expected, $result);
  3831. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3832. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3833. $this->assertHtml($expected, $result);
  3834. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3835. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3836. $this->assertHtml($expected, $result);
  3837. $result = $this->Form->label('Person.first_name', '');
  3838. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3839. $this->assertHtml($expected, $result);
  3840. $result = $this->Form->label('Person.2.name', '');
  3841. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3842. $this->assertHtml($expected, $result);
  3843. }
  3844. /**
  3845. * testLabelContainControl method
  3846. *
  3847. * Test that label() can accept an input with the correct template vars.
  3848. *
  3849. * @return void
  3850. */
  3851. public function testLabelContainControl()
  3852. {
  3853. $this->Form->templates([
  3854. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3855. ]);
  3856. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3857. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3858. ]);
  3859. $expected = [
  3860. 'label' => ['for' => 'person-accept-terms'],
  3861. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3862. 'Accept',
  3863. '/label',
  3864. ];
  3865. $this->assertHtml($expected, $result);
  3866. }
  3867. /**
  3868. * testTextbox method
  3869. *
  3870. * Test textbox element generation.
  3871. *
  3872. * @return void
  3873. */
  3874. public function testTextbox()
  3875. {
  3876. $result = $this->Form->text('Model.field');
  3877. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3878. $this->assertHtml($expected, $result);
  3879. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3880. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3881. $this->assertHtml($expected, $result);
  3882. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3883. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3884. $this->assertHtml($expected, $result);
  3885. }
  3886. /**
  3887. * testTextBoxDataAndError method
  3888. *
  3889. * Test that text() hooks up with request data and error fields.
  3890. *
  3891. * @return void
  3892. */
  3893. public function testTextBoxDataAndError()
  3894. {
  3895. $this->article['errors'] = [
  3896. 'Contact' => ['text' => 'wrong']
  3897. ];
  3898. $this->Form->create($this->article);
  3899. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3900. $result = $this->Form->text('Model.text');
  3901. $expected = [
  3902. 'input' => [
  3903. 'type' => 'text',
  3904. 'name' => 'Model[text]',
  3905. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3906. ]
  3907. ];
  3908. $this->assertHtml($expected, $result);
  3909. $this->Form->request->data['Contact']['text'] = 'test';
  3910. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3911. $expected = [
  3912. 'input' => [
  3913. 'type' => 'text',
  3914. 'name' => 'Contact[text]',
  3915. 'value' => 'test',
  3916. 'id' => 'theID',
  3917. 'class' => 'form-error'
  3918. ]
  3919. ];
  3920. $this->assertHtml($expected, $result);
  3921. }
  3922. /**
  3923. * testDefaultValue method
  3924. *
  3925. * Test default value setting.
  3926. *
  3927. * @return void
  3928. */
  3929. public function testTextDefaultValue()
  3930. {
  3931. $this->Form->request->data['Model']['field'] = 'test';
  3932. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3933. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3934. $this->assertHtml($expected, $result);
  3935. unset($this->Form->request->data['Model']['field']);
  3936. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3937. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3938. $this->assertHtml($expected, $result);
  3939. $Articles = TableRegistry::get('Articles');
  3940. $title = $Articles->schema()->column('title');
  3941. $Articles->schema()->addColumn(
  3942. 'title',
  3943. ['default' => 'default title'] + $title
  3944. );
  3945. $entity = $Articles->newEntity();
  3946. $this->Form->create($entity);
  3947. // Get default value from schema
  3948. $result = $this->Form->text('title');
  3949. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  3950. $this->assertHtml($expected, $result);
  3951. // Don't get value from schema
  3952. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3953. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3954. $this->assertHtml($expected, $result);
  3955. // Custom default value overrides default value from schema
  3956. $result = $this->Form->text('title', ['default' => 'override default']);
  3957. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  3958. $this->assertHtml($expected, $result);
  3959. // Default value from schema is used only for new entities.
  3960. $entity->isNew(false);
  3961. $result = $this->Form->text('title');
  3962. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3963. $this->assertHtml($expected, $result);
  3964. }
  3965. /**
  3966. * testError method
  3967. *
  3968. * Test field error generation.
  3969. *
  3970. * @return void
  3971. */
  3972. public function testError()
  3973. {
  3974. $this->article['errors'] = [
  3975. 'Article' => ['field' => 'email']
  3976. ];
  3977. $this->Form->create($this->article);
  3978. $result = $this->Form->error('Article.field');
  3979. $expected = [
  3980. ['div' => ['class' => 'error-message']],
  3981. 'email',
  3982. '/div',
  3983. ];
  3984. $this->assertHtml($expected, $result);
  3985. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3986. $expected = [
  3987. ['div' => ['class' => 'error-message']],
  3988. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3989. '/div',
  3990. ];
  3991. $this->assertHtml($expected, $result);
  3992. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3993. $expected = [
  3994. ['div' => ['class' => 'error-message']],
  3995. '<strong', 'Badness!', '/strong',
  3996. '/div',
  3997. ];
  3998. $this->assertHtml($expected, $result);
  3999. }
  4000. /**
  4001. * testErrorRuleName method
  4002. *
  4003. * Test error translation can use rule names for translating.
  4004. *
  4005. * @return void
  4006. */
  4007. public function testErrorRuleName()
  4008. {
  4009. $this->article['errors'] = [
  4010. 'Article' => [
  4011. 'field' => ['email' => 'Your email was not good']
  4012. ]
  4013. ];
  4014. $this->Form->create($this->article);
  4015. $result = $this->Form->error('Article.field');
  4016. $expected = [
  4017. ['div' => ['class' => 'error-message']],
  4018. 'Your email was not good',
  4019. '/div',
  4020. ];
  4021. $this->assertHtml($expected, $result);
  4022. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  4023. $expected = [
  4024. ['div' => ['class' => 'error-message']],
  4025. 'Email in use',
  4026. '/div',
  4027. ];
  4028. $this->assertHtml($expected, $result);
  4029. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  4030. $expected = [
  4031. ['div' => ['class' => 'error-message']],
  4032. 'Email in use',
  4033. '/div',
  4034. ];
  4035. $this->assertHtml($expected, $result);
  4036. $result = $this->Form->error('Article.field', [
  4037. 'email' => 'Key is preferred',
  4038. 'Your email was not good' => 'Email in use'
  4039. ]);
  4040. $expected = [
  4041. ['div' => ['class' => 'error-message']],
  4042. 'Key is preferred',
  4043. '/div',
  4044. ];
  4045. $this->assertHtml($expected, $result);
  4046. }
  4047. /**
  4048. * testErrorMessages method
  4049. *
  4050. * Test error with nested lists.
  4051. *
  4052. * @return void
  4053. */
  4054. public function testErrorMessages()
  4055. {
  4056. $this->article['errors'] = [
  4057. 'Article' => ['field' => 'email']
  4058. ];
  4059. $this->Form->create($this->article);
  4060. $result = $this->Form->error('Article.field', [
  4061. 'email' => 'No good!'
  4062. ]);
  4063. $expected = [
  4064. 'div' => ['class' => 'error-message'],
  4065. 'No good!',
  4066. '/div'
  4067. ];
  4068. $this->assertHtml($expected, $result);
  4069. }
  4070. /**
  4071. * testErrorMultipleMessages method
  4072. *
  4073. * Test error() with multiple messages.
  4074. *
  4075. * @return void
  4076. */
  4077. public function testErrorMultipleMessages()
  4078. {
  4079. $this->article['errors'] = [
  4080. 'field' => ['notBlank', 'email', 'Something else']
  4081. ];
  4082. $this->Form->create($this->article);
  4083. $result = $this->Form->error('field', [
  4084. 'notBlank' => 'Cannot be empty',
  4085. 'email' => 'No good!'
  4086. ]);
  4087. $expected = [
  4088. 'div' => ['class' => 'error-message'],
  4089. 'ul' => [],
  4090. '<li', 'Cannot be empty', '/li',
  4091. '<li', 'No good!', '/li',
  4092. '<li', 'Something else', '/li',
  4093. '/ul',
  4094. '/div'
  4095. ];
  4096. $this->assertHtml($expected, $result);
  4097. }
  4098. /**
  4099. * testPassword method
  4100. *
  4101. * Test password element generation.
  4102. *
  4103. * @return void
  4104. */
  4105. public function testPassword()
  4106. {
  4107. $this->article['errors'] = [
  4108. 'Contact' => [
  4109. 'passwd' => 1
  4110. ]
  4111. ];
  4112. $this->Form->create($this->article);
  4113. $result = $this->Form->password('Contact.field');
  4114. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  4115. $this->assertHtml($expected, $result);
  4116. $this->Form->request->data['Contact']['passwd'] = 'test';
  4117. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  4118. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  4119. $this->assertHtml($expected, $result);
  4120. }
  4121. /**
  4122. * testRadio method
  4123. *
  4124. * Test radio element set generation.
  4125. *
  4126. * @return void
  4127. */
  4128. public function testRadio()
  4129. {
  4130. $result = $this->Form->radio('Model.field', ['option A']);
  4131. $expected = [
  4132. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4133. 'label' => ['for' => 'model-field-0'],
  4134. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4135. 'option A',
  4136. '/label'
  4137. ];
  4138. $this->assertHtml($expected, $result);
  4139. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  4140. $this->assertHtml($expected, $result);
  4141. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4142. $expected = [
  4143. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4144. ['label' => ['for' => 'model-field-0']],
  4145. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4146. 'option A',
  4147. '/label',
  4148. ['label' => ['for' => 'model-field-1']],
  4149. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4150. 'option B',
  4151. '/label',
  4152. ];
  4153. $this->assertHtml($expected, $result);
  4154. $result = $this->Form->radio(
  4155. 'Employee.gender',
  4156. ['male' => 'Male', 'female' => 'Female'],
  4157. ['form' => 'my-form']
  4158. );
  4159. $expected = [
  4160. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4161. ['label' => ['for' => 'employee-gender-male']],
  4162. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4163. 'Male',
  4164. '/label',
  4165. ['label' => ['for' => 'employee-gender-female']],
  4166. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4167. 'Female',
  4168. '/label',
  4169. ];
  4170. $this->assertHtml($expected, $result);
  4171. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4172. $expected = [
  4173. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4174. ['label' => ['for' => 'model-custom-0']],
  4175. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4176. 'option A',
  4177. '/label',
  4178. ['label' => ['for' => 'model-custom-1']],
  4179. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4180. 'option B',
  4181. '/label',
  4182. ];
  4183. $this->assertHtml($expected, $result);
  4184. $result = $this->Form->radio(
  4185. 'Employee.gender',
  4186. [
  4187. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4188. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4189. ]
  4190. );
  4191. $expected = [
  4192. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4193. ['label' => ['for' => 'employee-gender-male']],
  4194. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4195. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4196. 'Male',
  4197. '/label',
  4198. ['label' => ['for' => 'employee-gender-female']],
  4199. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4200. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4201. 'Female',
  4202. '/label',
  4203. ];
  4204. $this->assertHtml($expected, $result);
  4205. }
  4206. /**
  4207. * testRadioDefaultValue method
  4208. *
  4209. * Test default value setting on radio() method.
  4210. *
  4211. * @return void
  4212. */
  4213. public function testRadioDefaultValue()
  4214. {
  4215. $Articles = TableRegistry::get('Articles');
  4216. $title = $Articles->schema()->column('title');
  4217. $Articles->schema()->addColumn(
  4218. 'title',
  4219. ['default' => '1'] + $title
  4220. );
  4221. $this->Form->create($Articles->newEntity());
  4222. $result = $this->Form->radio('title', ['option A', 'option B']);
  4223. $expected = [
  4224. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4225. ['label' => ['for' => 'title-0']],
  4226. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4227. 'option A',
  4228. '/label',
  4229. ['label' => ['for' => 'title-1']],
  4230. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4231. 'option B',
  4232. '/label',
  4233. ];
  4234. $this->assertHtml($expected, $result);
  4235. }
  4236. /**
  4237. * testControlRadio method
  4238. *
  4239. * Test that input works with radio types.
  4240. *
  4241. * @return void
  4242. */
  4243. public function testControlRadio()
  4244. {
  4245. $result = $this->Form->control('test', [
  4246. 'type' => 'radio',
  4247. 'options' => ['A', 'B'],
  4248. ]);
  4249. $expected = [
  4250. ['div' => ['class' => 'input radio']],
  4251. '<label',
  4252. 'Test',
  4253. '/label',
  4254. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4255. ['label' => ['for' => 'test-0']],
  4256. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4257. 'A',
  4258. '/label',
  4259. ['label' => ['for' => 'test-1']],
  4260. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4261. 'B',
  4262. '/label',
  4263. '/div',
  4264. ];
  4265. $this->assertHtml($expected, $result);
  4266. $result = $this->Form->control('test', [
  4267. 'type' => 'radio',
  4268. 'options' => ['A', 'B'],
  4269. 'value' => '0'
  4270. ]);
  4271. $expected = [
  4272. ['div' => ['class' => 'input radio']],
  4273. '<label',
  4274. 'Test',
  4275. '/label',
  4276. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4277. ['label' => ['for' => 'test-0']],
  4278. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4279. 'A',
  4280. '/label',
  4281. ['label' => ['for' => 'test-1']],
  4282. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4283. 'B',
  4284. '/label',
  4285. '/div',
  4286. ];
  4287. $this->assertHtml($expected, $result);
  4288. $result = $this->Form->control('test', [
  4289. 'type' => 'radio',
  4290. 'options' => ['A', 'B'],
  4291. 'label' => false
  4292. ]);
  4293. $expected = [
  4294. ['div' => ['class' => 'input radio']],
  4295. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4296. ['label' => ['for' => 'test-0']],
  4297. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4298. 'A',
  4299. '/label',
  4300. ['label' => ['for' => 'test-1']],
  4301. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4302. 'B',
  4303. '/label',
  4304. '/div',
  4305. ];
  4306. $this->assertHtml($expected, $result);
  4307. }
  4308. /**
  4309. * testRadioNoLabel method
  4310. *
  4311. * Test that radio() works with label = false.
  4312. *
  4313. * @return void
  4314. */
  4315. public function testRadioNoLabel()
  4316. {
  4317. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4318. $expected = [
  4319. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4320. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4321. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4322. ];
  4323. $this->assertHtml($expected, $result);
  4324. }
  4325. /**
  4326. * testRadioControlInsideLabel method
  4327. *
  4328. * Test generating radio input inside label ala twitter bootstrap.
  4329. *
  4330. * @return void
  4331. */
  4332. public function testRadioControlInsideLabel()
  4333. {
  4334. $this->Form->templates([
  4335. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4336. 'radioWrapper' => '{{label}}'
  4337. ]);
  4338. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4339. //@codingStandardsIgnoreStart
  4340. $expected = [
  4341. ['input' => [
  4342. 'type' => 'hidden',
  4343. 'name' => 'Model[field]',
  4344. 'value' => ''
  4345. ]],
  4346. ['label' => ['for' => 'model-field-0']],
  4347. ['input' => [
  4348. 'type' => 'radio',
  4349. 'name' => 'Model[field]',
  4350. 'value' => '0',
  4351. 'id' => 'model-field-0'
  4352. ]],
  4353. 'option A',
  4354. '/label',
  4355. ['label' => ['for' => 'model-field-1']],
  4356. ['input' => [
  4357. 'type' => 'radio',
  4358. 'name' => 'Model[field]',
  4359. 'value' => '1',
  4360. 'id' => 'model-field-1'
  4361. ]],
  4362. 'option B',
  4363. '/label',
  4364. ];
  4365. //@codingStandardsIgnoreEnd
  4366. $this->assertHtml($expected, $result);
  4367. }
  4368. /**
  4369. * testRadioHiddenControlDisabling method
  4370. *
  4371. * Test disabling the hidden input for radio buttons.
  4372. *
  4373. * @return void
  4374. */
  4375. public function testRadioHiddenControlDisabling()
  4376. {
  4377. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4378. $expected = [
  4379. 'label' => ['for' => 'model-1-field-0'],
  4380. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4381. 'option A',
  4382. '/label'
  4383. ];
  4384. $this->assertHtml($expected, $result);
  4385. }
  4386. /**
  4387. * testRadioOutOfRange method
  4388. *
  4389. * Test radio element set generation.
  4390. *
  4391. * @return void
  4392. */
  4393. public function testRadioOutOfRange()
  4394. {
  4395. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4396. $expected = [
  4397. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4398. 'label' => ['for' => 'model-field-v'],
  4399. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4400. 'value',
  4401. '/label'
  4402. ];
  4403. $this->assertHtml($expected, $result);
  4404. }
  4405. /**
  4406. * testSelect method
  4407. *
  4408. * Test select element generation.
  4409. *
  4410. * @return void
  4411. */
  4412. public function testSelect()
  4413. {
  4414. $result = $this->Form->select('Model.field', []);
  4415. $expected = [
  4416. 'select' => ['name' => 'Model[field]'],
  4417. '/select'
  4418. ];
  4419. $this->assertHtml($expected, $result);
  4420. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4421. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4422. $expected = [
  4423. 'select' => ['name' => 'Model[field]'],
  4424. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4425. 'good',
  4426. '/option',
  4427. ['option' => ['value' => 'other']],
  4428. 'bad',
  4429. '/option',
  4430. '/select'
  4431. ];
  4432. $this->assertHtml($expected, $result);
  4433. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4434. $this->assertHtml($expected, $result);
  4435. $this->Form->request->data = [];
  4436. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4437. $expected = [
  4438. 'select' => ['name' => 'Model[field]'],
  4439. ['option' => ['value' => 'value']],
  4440. 'good',
  4441. '/option',
  4442. ['option' => ['value' => 'other']],
  4443. 'bad',
  4444. '/option',
  4445. '/select'
  4446. ];
  4447. $this->assertHtml($expected, $result);
  4448. $options = [
  4449. ['value' => 'first', 'text' => 'First'],
  4450. ['value' => 'first', 'text' => 'Another First'],
  4451. ];
  4452. $result = $this->Form->select(
  4453. 'Model.field',
  4454. $options,
  4455. ['escape' => false, 'empty' => false]
  4456. );
  4457. $expected = [
  4458. 'select' => ['name' => 'Model[field]'],
  4459. ['option' => ['value' => 'first']],
  4460. 'First',
  4461. '/option',
  4462. ['option' => ['value' => 'first']],
  4463. 'Another First',
  4464. '/option',
  4465. '/select'
  4466. ];
  4467. $this->assertHtml($expected, $result);
  4468. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4469. $result = $this->Form->select(
  4470. 'Model.contact_id',
  4471. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4472. ['escape' => false, 'empty' => 'pick something']
  4473. );
  4474. $expected = [
  4475. 'select' => ['name' => 'Model[contact_id]'],
  4476. ['option' => ['value' => '']], 'pick something', '/option',
  4477. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4478. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4479. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4480. '/select'
  4481. ];
  4482. $this->assertHtml($expected, $result);
  4483. $this->Form->request->data['Model']['field'] = 0;
  4484. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4485. $expected = [
  4486. 'select' => ['name' => 'Model[field]'],
  4487. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4488. ['option' => ['value' => '1']], 'Yes', '/option',
  4489. '/select'
  4490. ];
  4491. $this->assertHtml($expected, $result);
  4492. }
  4493. /**
  4494. * testSelectEscapeHtml method
  4495. *
  4496. * Test that select() escapes HTML.
  4497. *
  4498. * @return void
  4499. */
  4500. public function testSelectEscapeHtml()
  4501. {
  4502. $result = $this->Form->select(
  4503. 'Model.field',
  4504. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4505. ['empty' => false]
  4506. );
  4507. $expected = [
  4508. 'select' => ['name' => 'Model[field]'],
  4509. ['option' => ['value' => 'first']],
  4510. 'first &quot;html&quot; &lt;chars&gt;',
  4511. '/option',
  4512. ['option' => ['value' => 'second']],
  4513. 'value',
  4514. '/option',
  4515. '/select'
  4516. ];
  4517. $this->assertHtml($expected, $result);
  4518. $result = $this->Form->select(
  4519. 'Model.field',
  4520. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4521. ['escape' => false, 'empty' => false]
  4522. );
  4523. $expected = [
  4524. 'select' => ['name' => 'Model[field]'],
  4525. ['option' => ['value' => 'first']],
  4526. 'first "html" <chars>',
  4527. '/option',
  4528. ['option' => ['value' => 'second']],
  4529. 'value',
  4530. '/option',
  4531. '/select'
  4532. ];
  4533. $this->assertHtml($expected, $result);
  4534. }
  4535. /**
  4536. * testSelectRequired method
  4537. *
  4538. * Test select() with required and disabled attributes.
  4539. *
  4540. * @return void
  4541. */
  4542. public function testSelectRequired()
  4543. {
  4544. $this->article['required'] = [
  4545. 'user_id' => true
  4546. ];
  4547. $this->Form->create($this->article);
  4548. $result = $this->Form->select('user_id', ['option A']);
  4549. $expected = [
  4550. 'select' => [
  4551. 'name' => 'user_id',
  4552. 'required' => 'required'
  4553. ],
  4554. ['option' => ['value' => '0']], 'option A', '/option',
  4555. '/select'
  4556. ];
  4557. $this->assertHtml($expected, $result);
  4558. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4559. $expected = [
  4560. 'select' => [
  4561. 'name' => 'user_id',
  4562. 'disabled' => 'disabled'
  4563. ],
  4564. ['option' => ['value' => '0']], 'option A', '/option',
  4565. '/select'
  4566. ];
  4567. $this->assertHtml($expected, $result);
  4568. }
  4569. /**
  4570. * testNestedSelect method
  4571. *
  4572. * Test select element generation with optgroups.
  4573. *
  4574. * @return void
  4575. */
  4576. public function testNestedSelect()
  4577. {
  4578. $result = $this->Form->select(
  4579. 'Model.field',
  4580. [1 => 'One', 2 => 'Two', 'Three' => [
  4581. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4582. ]],
  4583. ['empty' => false]
  4584. );
  4585. $expected = [
  4586. 'select' => ['name' => 'Model[field]'],
  4587. ['option' => ['value' => 1]],
  4588. 'One',
  4589. '/option',
  4590. ['option' => ['value' => 2]],
  4591. 'Two',
  4592. '/option',
  4593. ['optgroup' => ['label' => 'Three']],
  4594. ['option' => ['value' => 3]],
  4595. 'Three',
  4596. '/option',
  4597. ['option' => ['value' => 4]],
  4598. 'Four',
  4599. '/option',
  4600. ['option' => ['value' => 5]],
  4601. 'Five',
  4602. '/option',
  4603. '/optgroup',
  4604. '/select'
  4605. ];
  4606. $this->assertHtml($expected, $result);
  4607. }
  4608. /**
  4609. * testSelectMultiple method
  4610. *
  4611. * Test generation of multiple select elements.
  4612. *
  4613. * @return void
  4614. */
  4615. public function testSelectMultiple()
  4616. {
  4617. $options = ['first', 'second', 'third'];
  4618. $result = $this->Form->select(
  4619. 'Model.multi_field',
  4620. $options,
  4621. ['form' => 'my-form', 'multiple' => true]
  4622. );
  4623. $expected = [
  4624. 'input' => [
  4625. 'type' => 'hidden',
  4626. 'name' => 'Model[multi_field]',
  4627. 'value' => '',
  4628. 'form' => 'my-form',
  4629. ],
  4630. 'select' => [
  4631. 'name' => 'Model[multi_field][]',
  4632. 'multiple' => 'multiple',
  4633. 'form' => 'my-form',
  4634. ],
  4635. ['option' => ['value' => '0']],
  4636. 'first',
  4637. '/option',
  4638. ['option' => ['value' => '1']],
  4639. 'second',
  4640. '/option',
  4641. ['option' => ['value' => '2']],
  4642. 'third',
  4643. '/option',
  4644. '/select'
  4645. ];
  4646. $this->assertHtml($expected, $result);
  4647. $result = $this->Form->select(
  4648. 'Model.multi_field',
  4649. $options,
  4650. ['multiple' => 'multiple', 'form' => 'my-form']
  4651. );
  4652. $this->assertHtml($expected, $result);
  4653. }
  4654. /**
  4655. * testCheckboxZeroValue method
  4656. *
  4657. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4658. *
  4659. * @return void
  4660. */
  4661. public function testCheckboxZeroValue()
  4662. {
  4663. $result = $this->Form->control('User.get_spam', [
  4664. 'type' => 'checkbox',
  4665. 'value' => '0',
  4666. 'hiddenField' => '1',
  4667. ]);
  4668. $expected = [
  4669. 'div' => ['class' => 'input checkbox'],
  4670. 'label' => ['for' => 'user-get-spam'],
  4671. ['input' => [
  4672. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4673. 'value' => '1'
  4674. ]],
  4675. ['input' => [
  4676. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4677. 'value' => '0', 'id' => 'user-get-spam'
  4678. ]],
  4679. 'Get Spam',
  4680. '/label',
  4681. '/div'
  4682. ];
  4683. $this->assertHtml($expected, $result);
  4684. }
  4685. /**
  4686. * testHabtmSelectBox method
  4687. *
  4688. * Test generation of habtm select boxes.
  4689. *
  4690. * @return void
  4691. */
  4692. public function testHabtmSelectBox()
  4693. {
  4694. $this->loadFixtures('Articles');
  4695. $options = [
  4696. 1 => 'blue',
  4697. 2 => 'red',
  4698. 3 => 'green'
  4699. ];
  4700. $tags = [
  4701. new Entity(['id' => 1, 'name' => 'blue']),
  4702. new Entity(['id' => 3, 'name' => 'green'])
  4703. ];
  4704. $article = new Article(['tags' => $tags]);
  4705. $this->Form->create($article);
  4706. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4707. $expected = [
  4708. 'div' => ['class' => 'input select'],
  4709. 'label' => ['for' => 'tags-ids'],
  4710. 'Tags',
  4711. '/label',
  4712. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4713. 'select' => [
  4714. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4715. 'multiple' => 'multiple'
  4716. ],
  4717. ['option' => ['value' => '1', 'selected' => 'selected']],
  4718. 'blue',
  4719. '/option',
  4720. ['option' => ['value' => '2']],
  4721. 'red',
  4722. '/option',
  4723. ['option' => ['value' => '3', 'selected' => 'selected']],
  4724. 'green',
  4725. '/option',
  4726. '/select',
  4727. '/div'
  4728. ];
  4729. $this->assertHtml($expected, $result);
  4730. // make sure only 50 is selected, and not 50f5c0cf
  4731. $options = [
  4732. '1' => 'blue',
  4733. '50f5c0cf' => 'red',
  4734. '50' => 'green'
  4735. ];
  4736. $tags = [
  4737. new Entity(['id' => 1, 'name' => 'blue']),
  4738. new Entity(['id' => 50, 'name' => 'green'])
  4739. ];
  4740. $article = new Article(['tags' => $tags]);
  4741. $this->Form->create($article);
  4742. $result = $this->Form->control('tags._ids', ['options' => $options]);
  4743. $expected = [
  4744. 'div' => ['class' => 'input select'],
  4745. 'label' => ['for' => 'tags-ids'],
  4746. 'Tags',
  4747. '/label',
  4748. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4749. 'select' => [
  4750. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4751. 'multiple' => 'multiple'
  4752. ],
  4753. ['option' => ['value' => '1', 'selected' => 'selected']],
  4754. 'blue',
  4755. '/option',
  4756. ['option' => ['value' => '50f5c0cf']],
  4757. 'red',
  4758. '/option',
  4759. ['option' => ['value' => '50', 'selected' => 'selected']],
  4760. 'green',
  4761. '/option',
  4762. '/select',
  4763. '/div'
  4764. ];
  4765. $this->assertHtml($expected, $result);
  4766. $spacecraft = [
  4767. 1 => 'Orion',
  4768. 2 => 'Helios'
  4769. ];
  4770. $this->View->viewVars['spacecraft'] = $spacecraft;
  4771. $this->Form->create();
  4772. $result = $this->Form->control('spacecraft._ids');
  4773. $expected = [
  4774. 'div' => ['class' => 'input select'],
  4775. 'label' => ['for' => 'spacecraft-ids'],
  4776. 'Spacecraft',
  4777. '/label',
  4778. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4779. 'select' => [
  4780. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4781. 'multiple' => 'multiple'
  4782. ],
  4783. ['option' => ['value' => '1']],
  4784. 'Orion',
  4785. '/option',
  4786. ['option' => ['value' => '2']],
  4787. 'Helios',
  4788. '/option',
  4789. '/select',
  4790. '/div'
  4791. ];
  4792. $this->assertHtml($expected, $result);
  4793. }
  4794. /**
  4795. * testErrorsForBelongsToManySelect method
  4796. *
  4797. * Tests that errors for belongsToMany select fields are being
  4798. * picked up properly.
  4799. *
  4800. * @return void
  4801. */
  4802. public function testErrorsForBelongsToManySelect()
  4803. {
  4804. $spacecraft = [
  4805. 1 => 'Orion',
  4806. 2 => 'Helios'
  4807. ];
  4808. $this->View->viewVars['spacecraft'] = $spacecraft;
  4809. $article = new Article();
  4810. $article->errors('spacecraft', ['Invalid']);
  4811. $this->Form->create($article);
  4812. $result = $this->Form->control('spacecraft._ids');
  4813. $expected = [
  4814. ['div' => ['class' => 'input select error']],
  4815. 'label' => ['for' => 'spacecraft-ids'],
  4816. 'Spacecraft',
  4817. '/label',
  4818. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4819. 'select' => [
  4820. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4821. 'multiple' => 'multiple'
  4822. ],
  4823. ['option' => ['value' => '1']],
  4824. 'Orion',
  4825. '/option',
  4826. ['option' => ['value' => '2']],
  4827. 'Helios',
  4828. '/option',
  4829. '/select',
  4830. ['div' => ['class' => 'error-message']],
  4831. 'Invalid',
  4832. '/div',
  4833. '/div'
  4834. ];
  4835. $this->assertHtml($expected, $result);
  4836. }
  4837. /**
  4838. * testSelectMultipleCheckboxes method
  4839. *
  4840. * Test generation of multi select elements in checkbox format.
  4841. *
  4842. * @return void
  4843. */
  4844. public function testSelectMultipleCheckboxes()
  4845. {
  4846. $result = $this->Form->select(
  4847. 'Model.multi_field',
  4848. ['first', 'second', 'third'],
  4849. ['multiple' => 'checkbox']
  4850. );
  4851. $expected = [
  4852. 'input' => [
  4853. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4854. ],
  4855. ['div' => ['class' => 'checkbox']],
  4856. ['label' => ['for' => 'model-multi-field-0']],
  4857. ['input' => [
  4858. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4859. 'value' => '0', 'id' => 'model-multi-field-0'
  4860. ]],
  4861. 'first',
  4862. '/label',
  4863. '/div',
  4864. ['div' => ['class' => 'checkbox']],
  4865. ['label' => ['for' => 'model-multi-field-1']],
  4866. ['input' => [
  4867. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4868. 'value' => '1', 'id' => 'model-multi-field-1'
  4869. ]],
  4870. 'second',
  4871. '/label',
  4872. '/div',
  4873. ['div' => ['class' => 'checkbox']],
  4874. ['label' => ['for' => 'model-multi-field-2']],
  4875. ['input' => [
  4876. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4877. 'value' => '2', 'id' => 'model-multi-field-2'
  4878. ]],
  4879. 'third',
  4880. '/label',
  4881. '/div'
  4882. ];
  4883. $this->assertHtml($expected, $result);
  4884. $result = $this->Form->select(
  4885. 'Model.multi_field',
  4886. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4887. ['multiple' => 'checkbox']
  4888. );
  4889. $expected = [
  4890. 'input' => [
  4891. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4892. ],
  4893. ['div' => ['class' => 'checkbox']],
  4894. ['label' => ['for' => 'model-multi-field-a+']],
  4895. ['input' => [
  4896. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4897. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4898. ]],
  4899. 'first',
  4900. '/label',
  4901. '/div',
  4902. ['div' => ['class' => 'checkbox']],
  4903. ['label' => ['for' => 'model-multi-field-a++']],
  4904. ['input' => [
  4905. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4906. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4907. ]],
  4908. 'second',
  4909. '/label',
  4910. '/div',
  4911. ['div' => ['class' => 'checkbox']],
  4912. ['label' => ['for' => 'model-multi-field-a+++']],
  4913. ['input' => [
  4914. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4915. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4916. ]],
  4917. 'third',
  4918. '/label',
  4919. '/div'
  4920. ];
  4921. $this->assertHtml($expected, $result);
  4922. $result = $this->Form->select(
  4923. 'Model.multi_field',
  4924. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4925. ['multiple' => 'checkbox']
  4926. );
  4927. $expected = [
  4928. 'input' => [
  4929. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4930. ],
  4931. ['div' => ['class' => 'checkbox']],
  4932. ['label' => ['for' => 'model-multi-field-a-b']],
  4933. ['input' => [
  4934. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4935. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4936. ]],
  4937. 'first',
  4938. '/label',
  4939. '/div',
  4940. ['div' => ['class' => 'checkbox']],
  4941. ['label' => ['for' => 'model-multi-field-a-b1']],
  4942. ['input' => [
  4943. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4944. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4945. ]],
  4946. 'second',
  4947. '/label',
  4948. '/div',
  4949. ['div' => ['class' => 'checkbox']],
  4950. ['label' => ['for' => 'model-multi-field-a-b2']],
  4951. ['input' => [
  4952. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4953. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4954. ]],
  4955. 'third',
  4956. '/label',
  4957. '/div'
  4958. ];
  4959. $this->assertHtml($expected, $result);
  4960. }
  4961. /**
  4962. * testSelectMultipleCheckboxRequestData method
  4963. *
  4964. * Ensure that multiCheckbox reads from the request data.
  4965. *
  4966. * @return void
  4967. */
  4968. public function testSelectMultipleCheckboxRequestData()
  4969. {
  4970. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  4971. $result = $this->Form->select(
  4972. 'Model.tags',
  4973. ['1' => 'first', 'Array' => 'Array'],
  4974. ['multiple' => 'checkbox']
  4975. );
  4976. $expected = [
  4977. 'input' => [
  4978. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4979. ],
  4980. ['div' => ['class' => 'checkbox']],
  4981. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4982. ['input' => [
  4983. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4984. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4985. ]],
  4986. 'first',
  4987. '/label',
  4988. '/div',
  4989. ['div' => ['class' => 'checkbox']],
  4990. ['label' => ['for' => 'model-tags-array']],
  4991. ['input' => [
  4992. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4993. 'value' => 'Array', 'id' => 'model-tags-array'
  4994. ]],
  4995. 'Array',
  4996. '/label',
  4997. '/div'
  4998. ];
  4999. $this->assertHtml($expected, $result);
  5000. }
  5001. /**
  5002. * testSelectMultipleCheckboxSecurity method
  5003. *
  5004. * Checks the security hash array generated for multiple-input checkbox elements.
  5005. *
  5006. * @return void
  5007. */
  5008. public function testSelectMultipleCheckboxSecurity()
  5009. {
  5010. $this->Form->request->params['_Token'] = 'testKey';
  5011. $this->assertEquals([], $this->Form->fields);
  5012. $this->Form->select(
  5013. 'Model.multi_field',
  5014. ['1' => 'first', '2' => 'second', '3' => 'third'],
  5015. ['multiple' => 'checkbox']
  5016. );
  5017. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  5018. $result = $this->Form->secure($this->Form->fields);
  5019. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5020. $this->assertRegExp('/"' . $key . '"/', $result);
  5021. }
  5022. /**
  5023. * testSelectMultipleSecureWithNoOptions method
  5024. *
  5025. * Multiple select elements should always be secured as they always participate
  5026. * in the POST data.
  5027. *
  5028. * @return void
  5029. */
  5030. public function testSelectMultipleSecureWithNoOptions()
  5031. {
  5032. $this->assertEquals([], $this->Form->fields);
  5033. $this->Form->select(
  5034. 'Model.select',
  5035. [],
  5036. ['multiple' => true]
  5037. );
  5038. $this->assertEquals(['Model.select'], $this->Form->fields);
  5039. }
  5040. /**
  5041. * testSelectNoSecureWithNoOptions method
  5042. *
  5043. * When a select box has no options it should not be added to the fields list
  5044. * as it always fail post validation.
  5045. *
  5046. * @return void
  5047. */
  5048. public function testSelectNoSecureWithNoOptions()
  5049. {
  5050. $this->Form->request->params['_Token'] = 'testkey';
  5051. $this->assertEquals([], $this->Form->fields);
  5052. $this->Form->select(
  5053. 'Model.select',
  5054. []
  5055. );
  5056. $this->assertEquals([], $this->Form->fields);
  5057. $this->Form->select(
  5058. 'Model.user_id',
  5059. [],
  5060. ['empty' => true]
  5061. );
  5062. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  5063. }
  5064. /**
  5065. * testControlMultipleCheckboxes method
  5066. *
  5067. * Test control() resulting in multi select elements being generated.
  5068. *
  5069. * @return void
  5070. */
  5071. public function testControlMultipleCheckboxes()
  5072. {
  5073. $result = $this->Form->control('Model.multi_field', [
  5074. 'options' => ['first', 'second', 'third'],
  5075. 'multiple' => 'checkbox'
  5076. ]);
  5077. $expected = [
  5078. ['div' => ['class' => 'input select']],
  5079. ['label' => ['for' => 'model-multi-field']],
  5080. 'Multi Field',
  5081. '/label',
  5082. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5083. ['div' => ['class' => 'checkbox']],
  5084. ['label' => ['for' => 'model-multi-field-0']],
  5085. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  5086. 'first',
  5087. '/label',
  5088. '/div',
  5089. ['div' => ['class' => 'checkbox']],
  5090. ['label' => ['for' => 'model-multi-field-1']],
  5091. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  5092. 'second',
  5093. '/label',
  5094. '/div',
  5095. ['div' => ['class' => 'checkbox']],
  5096. ['label' => ['for' => 'model-multi-field-2']],
  5097. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  5098. 'third',
  5099. '/label',
  5100. '/div',
  5101. '/div'
  5102. ];
  5103. $this->assertHtml($expected, $result);
  5104. $result = $this->Form->control('Model.multi_field', [
  5105. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  5106. 'multiple' => 'checkbox'
  5107. ]);
  5108. $expected = [
  5109. ['div' => ['class' => 'input select']],
  5110. ['label' => ['for' => 'model-multi-field']],
  5111. 'Multi Field',
  5112. '/label',
  5113. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  5114. ['div' => ['class' => 'checkbox']],
  5115. ['label' => ['for' => 'model-multi-field-a']],
  5116. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  5117. 'first',
  5118. '/label',
  5119. '/div',
  5120. ['div' => ['class' => 'checkbox']],
  5121. ['label' => ['for' => 'model-multi-field-b']],
  5122. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  5123. 'second',
  5124. '/label',
  5125. '/div',
  5126. ['div' => ['class' => 'checkbox']],
  5127. ['label' => ['for' => 'model-multi-field-c']],
  5128. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  5129. 'third',
  5130. '/label',
  5131. '/div',
  5132. '/div'
  5133. ];
  5134. $this->assertHtml($expected, $result);
  5135. }
  5136. /**
  5137. * testSelectHiddenFieldOmission method
  5138. *
  5139. * Test that select() with 'hiddenField' => false omits the hidden field.
  5140. *
  5141. * @return void
  5142. */
  5143. public function testSelectHiddenFieldOmission()
  5144. {
  5145. $result = $this->Form->select(
  5146. 'Model.multi_field',
  5147. ['first', 'second'],
  5148. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  5149. );
  5150. $this->assertNotContains('type="hidden"', $result);
  5151. }
  5152. /**
  5153. * testSelectCheckboxMultipleOverrideName method
  5154. *
  5155. * Test that select() with multiple = checkbox works with overriding name attribute.
  5156. *
  5157. * @return void
  5158. */
  5159. public function testSelectCheckboxMultipleOverrideName()
  5160. {
  5161. $result = $this->Form->select('category', ['1', '2'], [
  5162. 'multiple' => 'checkbox',
  5163. 'name' => 'fish',
  5164. ]);
  5165. $expected = [
  5166. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  5167. ['div' => ['class' => 'checkbox']],
  5168. ['label' => ['for' => 'fish-0']],
  5169. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  5170. '1',
  5171. '/label',
  5172. '/div',
  5173. ['div' => ['class' => 'checkbox']],
  5174. ['label' => ['for' => 'fish-1']],
  5175. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  5176. '2',
  5177. '/label',
  5178. '/div'
  5179. ];
  5180. $this->assertHtml($expected, $result);
  5181. $result = $this->Form->multiCheckbox(
  5182. 'category',
  5183. new Collection(['1', '2']),
  5184. ['name' => 'fish']
  5185. );
  5186. $this->assertHtml($expected, $result);
  5187. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5188. 'name' => 'fish',
  5189. ]);
  5190. $this->assertHtml($expected, $result);
  5191. }
  5192. /**
  5193. * testControlMultiCheckbox method
  5194. *
  5195. * Test that control() works with multicheckbox.
  5196. *
  5197. * @return void
  5198. */
  5199. public function testControlMultiCheckbox()
  5200. {
  5201. $result = $this->Form->control('category', [
  5202. 'type' => 'multicheckbox',
  5203. 'options' => ['1', '2'],
  5204. ]);
  5205. $expected = [
  5206. ['div' => ['class' => 'input multicheckbox']],
  5207. '<label',
  5208. 'Category',
  5209. '/label',
  5210. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5211. ['div' => ['class' => 'checkbox']],
  5212. ['label' => ['for' => 'category-0']],
  5213. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5214. '1',
  5215. '/label',
  5216. '/div',
  5217. ['div' => ['class' => 'checkbox']],
  5218. ['label' => ['for' => 'category-1']],
  5219. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5220. '2',
  5221. '/label',
  5222. '/div',
  5223. '/div',
  5224. ];
  5225. $this->assertHtml($expected, $result);
  5226. }
  5227. /**
  5228. * testCheckbox method
  5229. *
  5230. * Test generation of checkboxes.
  5231. *
  5232. * @return void
  5233. */
  5234. public function testCheckbox()
  5235. {
  5236. $result = $this->Form->checkbox('Model.field');
  5237. $expected = [
  5238. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5239. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5240. ];
  5241. $this->assertHtml($expected, $result);
  5242. $result = $this->Form->checkbox('Model.field', [
  5243. 'id' => 'theID',
  5244. 'value' => 'myvalue',
  5245. 'form' => 'my-form',
  5246. ]);
  5247. $expected = [
  5248. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5249. ['input' => [
  5250. 'type' => 'checkbox', 'name' => 'Model[field]',
  5251. 'value' => 'myvalue', 'id' => 'theID',
  5252. 'form' => 'my-form',
  5253. ]]
  5254. ];
  5255. $this->assertHtml($expected, $result);
  5256. }
  5257. /**
  5258. * testCheckboxDefaultValue method
  5259. *
  5260. * Test default value setting on checkbox() method.
  5261. *
  5262. * @return void
  5263. */
  5264. public function testCheckboxDefaultValue()
  5265. {
  5266. $this->Form->request->data['Model']['field'] = false;
  5267. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5268. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5269. $this->assertHtml($expected, $result);
  5270. unset($this->Form->request->data['Model']['field']);
  5271. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5272. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5273. $this->assertHtml($expected, $result);
  5274. $this->Form->request->data['Model']['field'] = true;
  5275. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5276. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5277. $this->assertHtml($expected, $result);
  5278. unset($this->Form->request->data['Model']['field']);
  5279. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5280. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5281. $this->assertHtml($expected, $result);
  5282. $Articles = TableRegistry::get('Articles');
  5283. $Articles->schema()->addColumn(
  5284. 'published',
  5285. ['type' => 'boolean', 'null' => false, 'default' => true]
  5286. );
  5287. $this->Form->create($Articles->newEntity());
  5288. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5289. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5290. $this->assertHtml($expected, $result);
  5291. }
  5292. /**
  5293. * testCheckboxCheckedAndError method
  5294. *
  5295. * Test checkbox being checked or having errors.
  5296. *
  5297. * @return void
  5298. */
  5299. public function testCheckboxCheckedAndError()
  5300. {
  5301. $this->article['errors'] = [
  5302. 'published' => true
  5303. ];
  5304. $this->Form->request->data['published'] = 'myvalue';
  5305. $this->Form->create($this->article);
  5306. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5307. $expected = [
  5308. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5309. ['input' => [
  5310. 'type' => 'checkbox',
  5311. 'name' => 'published',
  5312. 'value' => 'myvalue',
  5313. 'id' => 'theID',
  5314. 'checked' => 'checked',
  5315. 'class' => 'form-error'
  5316. ]]
  5317. ];
  5318. $this->assertHtml($expected, $result);
  5319. $this->Form->request->data['published'] = '';
  5320. $result = $this->Form->checkbox('published');
  5321. $expected = [
  5322. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5323. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5324. ];
  5325. $this->assertHtml($expected, $result);
  5326. }
  5327. /**
  5328. * testCheckboxCustomNameAttribute method
  5329. *
  5330. * Test checkbox() with a custom name attribute.
  5331. *
  5332. * @return void
  5333. */
  5334. public function testCheckboxCustomNameAttribute()
  5335. {
  5336. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5337. $expected = [
  5338. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5339. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5340. ];
  5341. $this->assertHtml($expected, $result);
  5342. }
  5343. /**
  5344. * testCheckboxHiddenField method
  5345. *
  5346. * Test that the hidden input for checkboxes can be omitted or set to a
  5347. * specific value.
  5348. *
  5349. * @return void
  5350. */
  5351. public function testCheckboxHiddenField()
  5352. {
  5353. $result = $this->Form->checkbox('UserForm.something', [
  5354. 'hiddenField' => false
  5355. ]);
  5356. $expected = [
  5357. 'input' => [
  5358. 'type' => 'checkbox',
  5359. 'name' => 'UserForm[something]',
  5360. 'value' => '1'
  5361. ],
  5362. ];
  5363. $this->assertHtml($expected, $result);
  5364. $result = $this->Form->checkbox('UserForm.something', [
  5365. 'value' => 'Y',
  5366. 'hiddenField' => 'N',
  5367. ]);
  5368. $expected = [
  5369. ['input' => [
  5370. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5371. 'value' => 'N'
  5372. ]],
  5373. ['input' => [
  5374. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5375. 'value' => 'Y'
  5376. ]],
  5377. ];
  5378. $this->assertHtml($expected, $result);
  5379. }
  5380. /**
  5381. * testTime method
  5382. *
  5383. * Test the time type.
  5384. *
  5385. * @return void
  5386. */
  5387. public function testTime()
  5388. {
  5389. $result = $this->Form->time('start_time', [
  5390. 'timeFormat' => 12,
  5391. 'interval' => 5,
  5392. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5393. ]);
  5394. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5395. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5396. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5397. $this->assertNotContains('year', $result);
  5398. $this->assertNotContains('month', $result);
  5399. $this->assertNotContains('day', $result);
  5400. $result = $this->Form->time('start_time', [
  5401. 'timeFormat' => 12,
  5402. 'interval' => 5,
  5403. 'value' => '2014-03-08 16:30:00'
  5404. ]);
  5405. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5406. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5407. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5408. $this->assertNotContains('year', $result);
  5409. $this->assertNotContains('month', $result);
  5410. $this->assertNotContains('day', $result);
  5411. }
  5412. /**
  5413. * testTimeFormat24NoMeridian method
  5414. *
  5415. * Ensure that timeFormat=24 has no merdian.
  5416. *
  5417. * @return void.
  5418. */
  5419. public function testTimeFormat24NoMeridian()
  5420. {
  5421. $result = $this->Form->time('start_time', [
  5422. 'timeFormat' => 24,
  5423. 'interval' => 5,
  5424. 'value' => '2014-03-08 16:30:00'
  5425. ]);
  5426. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5427. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5428. $this->assertNotContains('meridian', $result);
  5429. $this->assertNotContains('pm', $result);
  5430. $this->assertNotContains('year', $result);
  5431. $this->assertNotContains('month', $result);
  5432. $this->assertNotContains('day', $result);
  5433. }
  5434. /**
  5435. * testDate method
  5436. *
  5437. * Test the date type.
  5438. *
  5439. * @return void
  5440. */
  5441. public function testDate()
  5442. {
  5443. $result = $this->Form->date('start_day', [
  5444. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5445. ]);
  5446. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5447. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5448. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5449. $this->assertNotContains('hour', $result);
  5450. $this->assertNotContains('minute', $result);
  5451. $this->assertNotContains('second', $result);
  5452. $this->assertNotContains('meridian', $result);
  5453. }
  5454. /**
  5455. * testDateTime method
  5456. *
  5457. * Test generation of date/time select elements.
  5458. *
  5459. * @return void
  5460. */
  5461. public function testDateTime()
  5462. {
  5463. extract($this->dateRegex);
  5464. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5465. $now = strtotime('now');
  5466. $expected = [
  5467. ['select' => ['name' => 'Contact[date][year]']],
  5468. ['option' => ['value' => '']],
  5469. '/option',
  5470. $yearsRegex,
  5471. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5472. date('Y', $now),
  5473. '/option',
  5474. '*/select',
  5475. ['select' => ['name' => 'Contact[date][month]']],
  5476. ['option' => ['value' => '']],
  5477. '/option',
  5478. $monthsRegex,
  5479. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5480. date('F', $now),
  5481. '/option',
  5482. '*/select',
  5483. ['select' => ['name' => 'Contact[date][day]']],
  5484. ['option' => ['value' => '']],
  5485. '/option',
  5486. $daysRegex,
  5487. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5488. date('j', $now),
  5489. '/option',
  5490. '*/select',
  5491. ['select' => ['name' => 'Contact[date][hour]']],
  5492. ['option' => ['value' => '']],
  5493. '/option',
  5494. $hoursRegex,
  5495. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5496. date('G', $now),
  5497. '/option',
  5498. '*/select',
  5499. ['select' => ['name' => 'Contact[date][minute]']],
  5500. ['option' => ['value' => '']],
  5501. '/option',
  5502. $minutesRegex,
  5503. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5504. date('i', $now),
  5505. '/option',
  5506. '*/select',
  5507. ];
  5508. $this->assertHtml($expected, $result);
  5509. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5510. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5511. $now = strtotime('now');
  5512. $expected = [
  5513. ['select' => ['name' => 'Contact[date][year]']],
  5514. $yearsRegex,
  5515. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5516. date('Y', $now),
  5517. '/option',
  5518. '*/select',
  5519. ['select' => ['name' => 'Contact[date][month]']],
  5520. $monthsRegex,
  5521. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5522. date('F', $now),
  5523. '/option',
  5524. '*/select',
  5525. ['select' => ['name' => 'Contact[date][day]']],
  5526. $daysRegex,
  5527. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5528. date('j', $now),
  5529. '/option',
  5530. '*/select',
  5531. ['select' => ['name' => 'Contact[date][hour]']],
  5532. $hoursRegex,
  5533. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5534. date('G', $now),
  5535. '/option',
  5536. '*/select',
  5537. ['select' => ['name' => 'Contact[date][minute]']],
  5538. $minutesRegex,
  5539. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5540. date('i', $now),
  5541. '/option',
  5542. '*/select',
  5543. ];
  5544. $this->assertHtml($expected, $result);
  5545. }
  5546. /**
  5547. * testDateTimeSecured method
  5548. *
  5549. * Test that datetime fields are added to protected fields list.
  5550. *
  5551. * @return void
  5552. */
  5553. public function testDateTimeSecured()
  5554. {
  5555. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5556. $this->Form->dateTime('Contact.date');
  5557. $expected = [
  5558. 'Contact.date.year',
  5559. 'Contact.date.month',
  5560. 'Contact.date.day',
  5561. 'Contact.date.hour',
  5562. 'Contact.date.minute',
  5563. ];
  5564. $this->assertEquals($expected, $this->Form->fields);
  5565. $this->Form->fields = [];
  5566. $this->Form->date('Contact.published');
  5567. $expected = [
  5568. 'Contact.published.year',
  5569. 'Contact.published.month',
  5570. 'Contact.published.day',
  5571. ];
  5572. $this->assertEquals($expected, $this->Form->fields);
  5573. }
  5574. /**
  5575. * testDateTimeSecuredDisabled method
  5576. *
  5577. * Test that datetime fields are added to protected fields list.
  5578. *
  5579. * @return void
  5580. */
  5581. public function testDateTimeSecuredDisabled()
  5582. {
  5583. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5584. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5585. $expected = [];
  5586. $this->assertEquals($expected, $this->Form->fields);
  5587. $this->Form->fields = [];
  5588. $this->Form->date('Contact.published', ['secure' => false]);
  5589. $expected = [];
  5590. $this->assertEquals($expected, $this->Form->fields);
  5591. }
  5592. /**
  5593. * testDatetimeEmpty method
  5594. *
  5595. * Test empty defaulting to true for datetime.
  5596. *
  5597. * @return void
  5598. */
  5599. public function testDatetimeEmpty()
  5600. {
  5601. extract($this->dateRegex);
  5602. $result = $this->Form->dateTime('Contact.date', [
  5603. 'timeFormat' => 12,
  5604. 'empty' => true,
  5605. 'default' => true
  5606. ]);
  5607. $expected = [
  5608. ['select' => ['name' => 'Contact[date][year]']],
  5609. $yearsRegex,
  5610. ['option' => ['value' => '']],
  5611. '/option',
  5612. '*/select',
  5613. ['select' => ['name' => 'Contact[date][month]']],
  5614. $monthsRegex,
  5615. ['option' => ['value' => '']],
  5616. '/option',
  5617. '*/select',
  5618. ['select' => ['name' => 'Contact[date][day]']],
  5619. $daysRegex,
  5620. ['option' => ['value' => '']],
  5621. '/option',
  5622. '*/select',
  5623. ['select' => ['name' => 'Contact[date][hour]']],
  5624. $hoursRegex,
  5625. ['option' => ['value' => '']],
  5626. '/option',
  5627. '*/select',
  5628. ['select' => ['name' => 'Contact[date][minute]']],
  5629. $minutesRegex,
  5630. ['option' => ['value' => '']],
  5631. '/option',
  5632. '*/select',
  5633. ['select' => ['name' => 'Contact[date][meridian]']],
  5634. $meridianRegex,
  5635. ['option' => ['value' => '']],
  5636. '/option',
  5637. '*/select'
  5638. ];
  5639. $this->assertHtml($expected, $result);
  5640. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5641. }
  5642. /**
  5643. * testDatetimeMinuteInterval method
  5644. *
  5645. * Test datetime with interval option.
  5646. *
  5647. * @return void
  5648. */
  5649. public function testDatetimeMinuteInterval()
  5650. {
  5651. extract($this->dateRegex);
  5652. $result = $this->Form->dateTime('Contact.date', [
  5653. 'interval' => 5,
  5654. 'value' => ''
  5655. ]);
  5656. $expected = [
  5657. ['select' => ['name' => 'Contact[date][year]']],
  5658. $yearsRegex,
  5659. ['option' => ['selected' => 'selected', 'value' => '']],
  5660. '/option',
  5661. '*/select',
  5662. ['select' => ['name' => 'Contact[date][month]']],
  5663. $monthsRegex,
  5664. ['option' => ['selected' => 'selected', 'value' => '']],
  5665. '/option',
  5666. '*/select',
  5667. ['select' => ['name' => 'Contact[date][day]']],
  5668. $daysRegex,
  5669. ['option' => ['selected' => 'selected', 'value' => '']],
  5670. '/option',
  5671. '*/select',
  5672. ['select' => ['name' => 'Contact[date][hour]']],
  5673. $hoursRegex,
  5674. ['option' => ['selected' => 'selected', 'value' => '']],
  5675. '/option',
  5676. '*/select',
  5677. ['select' => ['name' => 'Contact[date][minute]']],
  5678. $minutesRegex,
  5679. ['option' => ['selected' => 'selected', 'value' => '']],
  5680. '/option',
  5681. ['option' => ['value' => '00']],
  5682. '00',
  5683. '/option',
  5684. ['option' => ['value' => '05']],
  5685. '05',
  5686. '/option',
  5687. ['option' => ['value' => '10']],
  5688. '10',
  5689. '/option',
  5690. '*/select',
  5691. ];
  5692. $this->assertHtml($expected, $result);
  5693. }
  5694. /**
  5695. * testDateTimeRounding method
  5696. *
  5697. * Test dateTime with rounding.
  5698. *
  5699. * @return void
  5700. */
  5701. public function testDateTimeRounding()
  5702. {
  5703. $this->Form->request->data['Contact'] = [
  5704. 'date' => [
  5705. 'day' => '13',
  5706. 'month' => '12',
  5707. 'year' => '2010',
  5708. 'hour' => '04',
  5709. 'minute' => '19',
  5710. 'meridian' => 'AM'
  5711. ]
  5712. ];
  5713. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5714. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5715. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5716. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5717. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5718. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5719. }
  5720. /**
  5721. * testDatetimeWithDefault method
  5722. *
  5723. * Test that datetime() and default values work.
  5724. *
  5725. * @return void
  5726. */
  5727. public function testDatetimeWithDefault()
  5728. {
  5729. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5730. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5731. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5732. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5733. $result = $this->Form->dateTime('Contact.updated', [
  5734. 'default' => '2009-06-01 11:15:30'
  5735. ]);
  5736. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5737. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5738. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5739. }
  5740. /**
  5741. * testDateTimeAllZeros method
  5742. *
  5743. * Test datetime() with all zeros.
  5744. *
  5745. * @return void
  5746. */
  5747. public function testDateTimeAllZeros()
  5748. {
  5749. $result = $this->Form->dateTime('Contact.date', [
  5750. 'timeFormat' => false,
  5751. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5752. 'value' => '0000-00-00'
  5753. ]);
  5754. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5755. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5756. }
  5757. /**
  5758. * testDateTimeEmptyAsArray method
  5759. *
  5760. * @return void
  5761. */
  5762. public function testDateTimeEmptyAsArray()
  5763. {
  5764. $result = $this->Form->dateTime('Contact.date', [
  5765. 'empty' => [
  5766. 'day' => 'DAY',
  5767. 'month' => 'MONTH',
  5768. 'year' => 'YEAR',
  5769. 'hour' => 'HOUR',
  5770. 'minute' => 'MINUTE',
  5771. 'meridian' => false
  5772. ],
  5773. 'default' => true
  5774. ]);
  5775. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5776. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5777. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5778. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5779. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5780. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5781. $result = $this->Form->dateTime('Contact.date', [
  5782. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5783. 'default' => true
  5784. ]);
  5785. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5786. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5787. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5788. }
  5789. /**
  5790. * testFormDateTimeMulti method
  5791. *
  5792. * Test multiple datetime element generation.
  5793. *
  5794. * @return void
  5795. */
  5796. public function testFormDateTimeMulti()
  5797. {
  5798. extract($this->dateRegex);
  5799. $result = $this->Form->dateTime('Contact.1.updated');
  5800. $this->assertContains('Contact[1][updated][month]', $result);
  5801. $this->assertContains('Contact[1][updated][day]', $result);
  5802. $this->assertContains('Contact[1][updated][year]', $result);
  5803. $this->assertContains('Contact[1][updated][hour]', $result);
  5804. $this->assertContains('Contact[1][updated][minute]', $result);
  5805. }
  5806. /**
  5807. * testDateTimeLabelIdMatchesFirstControl method
  5808. *
  5809. * When changing the date format, the label should always focus the first select box when
  5810. * clicked.
  5811. *
  5812. * @return void
  5813. */
  5814. public function testDateTimeLabelIdMatchesFirstControl()
  5815. {
  5816. $result = $this->Form->control('Model.date', ['type' => 'date']);
  5817. $this->assertContains('<label>Date</label>', $result);
  5818. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5819. $this->assertContains('<label>Date</label>', $result);
  5820. $result = $this->Form->control('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5821. $this->assertContains('<label>Date</label>', $result);
  5822. }
  5823. /**
  5824. * testDateTimeSecondOptions method
  5825. *
  5826. * Test datetime second=true.
  5827. *
  5828. * @return void
  5829. */
  5830. public function testDateTimeSecondOptions()
  5831. {
  5832. $result = $this->Form->dateTime('updated', ['second' => true]);
  5833. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5834. $result = $this->Form->dateTime('updated', ['second' => []]);
  5835. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5836. $result = $this->Form->dateTime('updated', ['second' => null]);
  5837. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5838. $result = $this->Form->dateTime('updated', ['second' => false]);
  5839. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5840. }
  5841. /**
  5842. * testMonth method
  5843. *
  5844. * Test generation of a month input.
  5845. *
  5846. * @return void
  5847. */
  5848. public function testMonth()
  5849. {
  5850. $result = $this->Form->month('Model.field', ['value' => '']);
  5851. $expected = [
  5852. ['select' => ['name' => 'Model[field][month]']],
  5853. ['option' => ['value' => '', 'selected' => 'selected']],
  5854. '/option',
  5855. ['option' => ['value' => '01']],
  5856. date('F', strtotime('2008-01-01 00:00:00')),
  5857. '/option',
  5858. ['option' => ['value' => '02']],
  5859. date('F', strtotime('2008-02-01 00:00:00')),
  5860. '/option',
  5861. '*/select',
  5862. ];
  5863. $this->assertHtml($expected, $result);
  5864. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5865. $expected = [
  5866. ['select' => ['name' => 'Model[field][month]']],
  5867. ['option' => ['selected' => 'selected', 'value' => '']],
  5868. '/option',
  5869. ['option' => ['value' => '01']],
  5870. date('F', strtotime('2008-01-01 00:00:00')),
  5871. '/option',
  5872. ['option' => ['value' => '02']],
  5873. date('F', strtotime('2008-02-01 00:00:00')),
  5874. '/option',
  5875. '*/select',
  5876. ];
  5877. $this->assertHtml($expected, $result);
  5878. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5879. $expected = [
  5880. ['select' => ['name' => 'Model[field][month]']],
  5881. ['option' => ['selected' => 'selected', 'value' => '']],
  5882. '/option',
  5883. ['option' => ['value' => '01']],
  5884. '1',
  5885. '/option',
  5886. ['option' => ['value' => '02']],
  5887. '2',
  5888. '/option',
  5889. '*/select',
  5890. ];
  5891. $this->assertHtml($expected, $result);
  5892. $monthNames = [
  5893. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5894. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5895. ];
  5896. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5897. $expected = [
  5898. ['select' => ['name' => 'Model[field][month]']],
  5899. ['option' => ['value' => '']],
  5900. '/option',
  5901. ['option' => ['value' => '01', 'selected' => 'selected']],
  5902. 'Jan',
  5903. '/option',
  5904. ['option' => ['value' => '02']],
  5905. 'Feb',
  5906. '/option',
  5907. '*/select',
  5908. ];
  5909. $this->assertHtml($expected, $result);
  5910. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5911. $result = $this->Form->month('Project.release');
  5912. $expected = [
  5913. ['select' => ['name' => 'Project[release][month]']],
  5914. ['option' => ['value' => '']],
  5915. '/option',
  5916. ['option' => ['value' => '01']],
  5917. 'January',
  5918. '/option',
  5919. ['option' => ['value' => '02', 'selected' => 'selected']],
  5920. 'February',
  5921. '/option',
  5922. '*/select',
  5923. ];
  5924. $this->assertHtml($expected, $result);
  5925. $result = $this->Form->month('Contact.published', [
  5926. 'empty' => 'Published on',
  5927. ]);
  5928. $this->assertContains('Published on', $result);
  5929. }
  5930. /**
  5931. * testDay method
  5932. *
  5933. * Test generation of a day input.
  5934. *
  5935. * @return void
  5936. */
  5937. public function testDay()
  5938. {
  5939. extract($this->dateRegex);
  5940. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  5941. $expected = [
  5942. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  5943. ['option' => ['selected' => 'selected', 'value' => '']],
  5944. '/option',
  5945. ['option' => ['value' => '01']],
  5946. '1',
  5947. '/option',
  5948. ['option' => ['value' => '02']],
  5949. '2',
  5950. '/option',
  5951. $daysRegex,
  5952. '/select',
  5953. ];
  5954. $this->assertHtml($expected, $result);
  5955. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5956. $result = $this->Form->day('Model.field');
  5957. $expected = [
  5958. ['select' => ['name' => 'Model[field][day]']],
  5959. ['option' => ['value' => '']],
  5960. '/option',
  5961. ['option' => ['value' => '01']],
  5962. '1',
  5963. '/option',
  5964. ['option' => ['value' => '02']],
  5965. '2',
  5966. '/option',
  5967. $daysRegex,
  5968. ['option' => ['value' => '10', 'selected' => 'selected']],
  5969. '10',
  5970. '/option',
  5971. $daysRegex,
  5972. '/select',
  5973. ];
  5974. $this->assertHtml($expected, $result);
  5975. $this->Form->request->data['Model']['field'] = '';
  5976. $result = $this->Form->day('Model.field', ['value' => '10']);
  5977. $expected = [
  5978. ['select' => ['name' => 'Model[field][day]']],
  5979. ['option' => ['value' => '']],
  5980. '/option',
  5981. ['option' => ['value' => '01']],
  5982. '1',
  5983. '/option',
  5984. ['option' => ['value' => '02']],
  5985. '2',
  5986. '/option',
  5987. $daysRegex,
  5988. ['option' => ['value' => '10', 'selected' => 'selected']],
  5989. '10',
  5990. '/option',
  5991. $daysRegex,
  5992. '/select',
  5993. ];
  5994. $this->assertHtml($expected, $result);
  5995. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5996. $result = $this->Form->day('Project.release');
  5997. $expected = [
  5998. ['select' => ['name' => 'Project[release][day]']],
  5999. ['option' => ['value' => '']],
  6000. '/option',
  6001. ['option' => ['value' => '01']],
  6002. '1',
  6003. '/option',
  6004. ['option' => ['value' => '02']],
  6005. '2',
  6006. '/option',
  6007. $daysRegex,
  6008. ['option' => ['value' => '10', 'selected' => 'selected']],
  6009. '10',
  6010. '/option',
  6011. $daysRegex,
  6012. '/select',
  6013. ];
  6014. $this->assertHtml($expected, $result);
  6015. $result = $this->Form->day('Contact.published', [
  6016. 'empty' => 'Published on',
  6017. ]);
  6018. $this->assertContains('Published on', $result);
  6019. }
  6020. /**
  6021. * testMinute method
  6022. *
  6023. * Test generation of a minute input.
  6024. *
  6025. * @return void
  6026. */
  6027. public function testMinute()
  6028. {
  6029. extract($this->dateRegex);
  6030. $result = $this->Form->minute('Model.field', ['value' => '']);
  6031. $expected = [
  6032. ['select' => ['name' => 'Model[field][minute]']],
  6033. ['option' => ['selected' => 'selected', 'value' => '']],
  6034. '/option',
  6035. ['option' => ['value' => '00']],
  6036. '00',
  6037. '/option',
  6038. ['option' => ['value' => '01']],
  6039. '01',
  6040. '/option',
  6041. ['option' => ['value' => '02']],
  6042. '02',
  6043. '/option',
  6044. $minutesRegex,
  6045. '/select',
  6046. ];
  6047. $this->assertHtml($expected, $result);
  6048. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6049. $result = $this->Form->minute('Model.field');
  6050. $expected = [
  6051. ['select' => ['name' => 'Model[field][minute]']],
  6052. ['option' => ['value' => '']],
  6053. '/option',
  6054. ['option' => ['value' => '00']],
  6055. '00',
  6056. '/option',
  6057. ['option' => ['value' => '01']],
  6058. '01',
  6059. '/option',
  6060. ['option' => ['value' => '02']],
  6061. '02',
  6062. '/option',
  6063. $minutesRegex,
  6064. ['option' => ['value' => '12', 'selected' => 'selected']],
  6065. '12',
  6066. '/option',
  6067. $minutesRegex,
  6068. '/select',
  6069. ];
  6070. $this->assertHtml($expected, $result);
  6071. $this->Form->request->data['Model']['field'] = '';
  6072. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6073. $expected = [
  6074. ['select' => ['name' => 'Model[field][minute]']],
  6075. ['option' => ['selected' => 'selected', 'value' => '']],
  6076. '/option',
  6077. ['option' => ['value' => '00']],
  6078. '00',
  6079. '/option',
  6080. ['option' => ['value' => '05']],
  6081. '05',
  6082. '/option',
  6083. ['option' => ['value' => '10']],
  6084. '10',
  6085. '/option',
  6086. $minutesRegex,
  6087. '/select',
  6088. ];
  6089. $this->assertHtml($expected, $result);
  6090. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6091. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  6092. $expected = [
  6093. ['select' => ['name' => 'Model[field][minute]']],
  6094. ['option' => ['value' => '']],
  6095. '/option',
  6096. ['option' => ['value' => '00']],
  6097. '00',
  6098. '/option',
  6099. ['option' => ['value' => '05']],
  6100. '05',
  6101. '/option',
  6102. ['option' => ['value' => '10', 'selected' => 'selected']],
  6103. '10',
  6104. '/option',
  6105. $minutesRegex,
  6106. '/select',
  6107. ];
  6108. $this->assertHtml($expected, $result);
  6109. }
  6110. /**
  6111. * testMeridian method
  6112. *
  6113. * Test generating an input for the meridian.
  6114. *
  6115. * @return void
  6116. */
  6117. public function testMeridian()
  6118. {
  6119. extract($this->dateRegex);
  6120. $now = new \DateTime();
  6121. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  6122. $expected = [
  6123. ['select' => ['name' => 'Model[field][meridian]']],
  6124. ['option' => ['value' => '']],
  6125. '/option',
  6126. $meridianRegex,
  6127. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  6128. $now->format('a'),
  6129. '/option',
  6130. '*/select'
  6131. ];
  6132. $this->assertHtml($expected, $result);
  6133. }
  6134. /**
  6135. * testHour method
  6136. *
  6137. * Test generation of an hour input.
  6138. *
  6139. * @return void
  6140. */
  6141. public function testHour()
  6142. {
  6143. extract($this->dateRegex);
  6144. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  6145. $expected = [
  6146. ['select' => ['name' => 'Model[field][hour]']],
  6147. ['option' => ['selected' => 'selected', 'value' => '']],
  6148. '/option',
  6149. ['option' => ['value' => '01']],
  6150. '1',
  6151. '/option',
  6152. ['option' => ['value' => '02']],
  6153. '2',
  6154. '/option',
  6155. $hoursRegex,
  6156. '/select',
  6157. ];
  6158. $this->assertHtml($expected, $result);
  6159. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6160. $result = $this->Form->hour('Model.field', ['format' => 12]);
  6161. $expected = [
  6162. ['select' => ['name' => 'Model[field][hour]']],
  6163. ['option' => ['value' => '']],
  6164. '/option',
  6165. ['option' => ['value' => '01']],
  6166. '1',
  6167. '/option',
  6168. ['option' => ['value' => '02']],
  6169. '2',
  6170. '/option',
  6171. $hoursRegex,
  6172. ['option' => ['value' => '12', 'selected' => 'selected']],
  6173. '12',
  6174. '/option',
  6175. '/select',
  6176. ];
  6177. $this->assertHtml($expected, $result);
  6178. $this->Form->request->data['Model']['field'] = '';
  6179. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  6180. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6181. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  6182. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6183. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6184. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6185. $expected = [
  6186. ['select' => ['name' => 'Model[field][hour]']],
  6187. ['option' => ['value' => '']],
  6188. '/option',
  6189. ['option' => ['value' => '00', 'selected' => 'selected']],
  6190. '0',
  6191. '/option',
  6192. ['option' => ['value' => '01']],
  6193. '1',
  6194. '/option',
  6195. ['option' => ['value' => '02']],
  6196. '2',
  6197. '/option',
  6198. $hoursRegex,
  6199. '/select',
  6200. ];
  6201. $this->assertHtml($expected, $result);
  6202. unset($this->Form->request->data['Model']['field']);
  6203. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  6204. $thisHour = date('H');
  6205. $optValue = date('G');
  6206. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6207. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6208. $result = $this->Form->hour('Model.field', ['format' => 24]);
  6209. $expected = [
  6210. ['select' => ['name' => 'Model[field][hour]']],
  6211. ['option' => ['value' => '']],
  6212. '/option',
  6213. ['option' => ['value' => '00']],
  6214. '0',
  6215. '/option',
  6216. ['option' => ['value' => '01', 'selected' => 'selected']],
  6217. '1',
  6218. '/option',
  6219. ['option' => ['value' => '02']],
  6220. '2',
  6221. '/option',
  6222. $hoursRegex,
  6223. '/select',
  6224. ];
  6225. $this->assertHtml($expected, $result);
  6226. }
  6227. /**
  6228. * testYear method
  6229. *
  6230. * Test generation of a year input.
  6231. *
  6232. * @return void
  6233. */
  6234. public function testYear()
  6235. {
  6236. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6237. $expected = [
  6238. ['select' => ['name' => 'Model[field][year]']],
  6239. ['option' => ['selected' => 'selected', 'value' => '']],
  6240. '/option',
  6241. ['option' => ['value' => '2007']],
  6242. '2007',
  6243. '/option',
  6244. ['option' => ['value' => '2006']],
  6245. '2006',
  6246. '/option',
  6247. '/select',
  6248. ];
  6249. $this->assertHtml($expected, $result);
  6250. $result = $this->Form->year('Model.field', [
  6251. 'value' => '',
  6252. 'minYear' => 2006,
  6253. 'maxYear' => 2007,
  6254. 'orderYear' => 'asc'
  6255. ]);
  6256. $expected = [
  6257. ['select' => ['name' => 'Model[field][year]']],
  6258. ['option' => ['selected' => 'selected', 'value' => '']],
  6259. '/option',
  6260. ['option' => ['value' => '2006']],
  6261. '2006',
  6262. '/option',
  6263. ['option' => ['value' => '2007']],
  6264. '2007',
  6265. '/option',
  6266. '/select',
  6267. ];
  6268. $this->assertHtml($expected, $result);
  6269. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6270. $result = $this->Form->year('Contact.published', [
  6271. 'empty' => false,
  6272. 'minYear' => 2006,
  6273. 'maxYear' => 2007,
  6274. ]);
  6275. $expected = [
  6276. ['select' => ['name' => 'Contact[published][year]']],
  6277. ['option' => ['value' => '2007']],
  6278. '2007',
  6279. '/option',
  6280. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6281. '2006',
  6282. '/option',
  6283. '/select',
  6284. ];
  6285. $this->assertHtml($expected, $result);
  6286. $result = $this->Form->year('Contact.published', [
  6287. 'empty' => 'Published on',
  6288. ]);
  6289. $this->assertContains('Published on', $result);
  6290. }
  6291. /**
  6292. * testControlDatetimePreEpoch method
  6293. *
  6294. * Test minYear being prior to the unix epoch.
  6295. *
  6296. * @return void
  6297. */
  6298. public function testControlDatetimePreEpoch()
  6299. {
  6300. $start = date('Y') - 80;
  6301. $end = date('Y') - 18;
  6302. $result = $this->Form->control('birth_year', [
  6303. 'type' => 'date',
  6304. 'label' => 'Birth Year',
  6305. 'minYear' => $start,
  6306. 'maxYear' => $end,
  6307. 'month' => false,
  6308. 'day' => false,
  6309. ]);
  6310. $this->assertContains('value="' . $start . '">' . $start, $result);
  6311. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6312. $this->assertNotContains('value="00">00', $result);
  6313. }
  6314. /**
  6315. * testYearAutoExpandRange method
  6316. *
  6317. * @return void
  6318. */
  6319. public function testYearAutoExpandRange()
  6320. {
  6321. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6322. $result = $this->Form->year('User.birthday');
  6323. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6324. $result = $matches[1];
  6325. $expected = range(date('Y') + 5, 1930);
  6326. $this->assertEquals($expected, $result);
  6327. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6328. $result = $this->Form->year('Project.release');
  6329. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6330. $result = $matches[1];
  6331. $expected = range(2050, date('Y') - 5);
  6332. $this->assertEquals($expected, $result);
  6333. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6334. $result = $this->Form->year('Project.release', [
  6335. 'minYear' => 1890,
  6336. 'maxYear' => 1900
  6337. ]);
  6338. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6339. $result = $matches[1];
  6340. $expected = range(1900, 1881);
  6341. $this->assertEquals($expected, $result);
  6342. }
  6343. /**
  6344. * testControlDate method
  6345. *
  6346. * Test that control() accepts the type of date and passes options in.
  6347. *
  6348. * @return void
  6349. */
  6350. public function testControlDate()
  6351. {
  6352. $this->Form->request->data = [
  6353. 'month_year' => ['month' => date('m')],
  6354. ];
  6355. $this->Form->create($this->article);
  6356. $result = $this->Form->control('month_year', [
  6357. 'label' => false,
  6358. 'type' => 'date',
  6359. 'minYear' => 2006,
  6360. 'maxYear' => 2008
  6361. ]);
  6362. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6363. $this->assertNotContains('value="2008" selected="selected"', $result);
  6364. }
  6365. /**
  6366. * testControlLabelFalse method
  6367. *
  6368. * Test the label option being set to false.
  6369. *
  6370. * @return void
  6371. */
  6372. public function testControlLabelFalse()
  6373. {
  6374. $this->Form->create($this->article);
  6375. $result = $this->Form->control('title', ['label' => false]);
  6376. $expected = [
  6377. 'div' => ['class' => 'input text required'],
  6378. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6379. '/div'
  6380. ];
  6381. $this->assertHtml($expected, $result);
  6382. }
  6383. /**
  6384. * testControlDateMaxYear method
  6385. *
  6386. * Let's say we want to only allow users born from 2006 to 2008 to register
  6387. * This being the first signup page, we still don't have any data.
  6388. *
  6389. * @return void
  6390. */
  6391. public function testControlDateMaxYear()
  6392. {
  6393. $this->Form->request->data = [];
  6394. $this->Form->create($this->article);
  6395. $result = $this->Form->control('birthday', [
  6396. 'label' => false,
  6397. 'type' => 'date',
  6398. 'minYear' => 2006,
  6399. 'maxYear' => 2008,
  6400. 'default' => true
  6401. ]);
  6402. $this->assertContains('value="2008" selected="selected"', $result);
  6403. $this->assertContains('value="2006"', $result);
  6404. $this->assertNotContains('value="2005"', $result);
  6405. $this->assertNotContains('value="2009"', $result);
  6406. }
  6407. /**
  6408. * testTextArea method
  6409. *
  6410. * Test generation of a textarea input.
  6411. *
  6412. * @return void
  6413. */
  6414. public function testTextArea()
  6415. {
  6416. $this->Form->request->data = ['field' => 'some test data'];
  6417. $result = $this->Form->textarea('field');
  6418. $expected = [
  6419. 'textarea' => ['name' => 'field', 'rows' => 5],
  6420. 'some test data',
  6421. '/textarea',
  6422. ];
  6423. $this->assertHtml($expected, $result);
  6424. $result = $this->Form->textarea('user.bio');
  6425. $expected = [
  6426. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6427. '/textarea',
  6428. ];
  6429. $this->assertHtml($expected, $result);
  6430. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6431. $result = $this->Form->textarea('field');
  6432. $expected = [
  6433. 'textarea' => ['name' => 'field', 'rows' => 5],
  6434. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6435. '/textarea',
  6436. ];
  6437. $this->assertHtml($expected, $result);
  6438. $this->Form->request->data = [
  6439. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6440. ];
  6441. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6442. $expected = [
  6443. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6444. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6445. '/textarea',
  6446. ];
  6447. $this->assertHtml($expected, $result);
  6448. $result = $this->Form->textarea('0.OtherModel.field');
  6449. $expected = [
  6450. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6451. '/textarea'
  6452. ];
  6453. $this->assertHtml($expected, $result);
  6454. }
  6455. /**
  6456. * testTextAreaWithStupidCharacters method
  6457. *
  6458. * Test text area with non-ascii characters.
  6459. *
  6460. * @return void
  6461. */
  6462. public function testTextAreaWithStupidCharacters()
  6463. {
  6464. $result = $this->Form->textarea('Post.content', [
  6465. 'value' => "GREAT®",
  6466. 'rows' => '15',
  6467. 'cols' => '75'
  6468. ]);
  6469. $expected = [
  6470. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6471. 'GREAT®',
  6472. '/textarea',
  6473. ];
  6474. $this->assertHtml($expected, $result);
  6475. }
  6476. /**
  6477. * testTextAreaMaxLength method
  6478. *
  6479. * Test textareas maxlength read from schema.
  6480. *
  6481. * @return void
  6482. */
  6483. public function testTextAreaMaxLength()
  6484. {
  6485. $this->Form->create([
  6486. 'schema' => [
  6487. 'stuff' => ['type' => 'string', 'length' => 10],
  6488. ]
  6489. ]);
  6490. $result = $this->Form->control('other', ['type' => 'textarea']);
  6491. $expected = [
  6492. 'div' => ['class' => 'input textarea'],
  6493. 'label' => ['for' => 'other'],
  6494. 'Other',
  6495. '/label',
  6496. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6497. '/textarea',
  6498. '/div'
  6499. ];
  6500. $this->assertHtml($expected, $result);
  6501. $result = $this->Form->control('stuff', ['type' => 'textarea']);
  6502. $expected = [
  6503. 'div' => ['class' => 'input textarea'],
  6504. 'label' => ['for' => 'stuff'],
  6505. 'Stuff',
  6506. '/label',
  6507. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6508. '/textarea',
  6509. '/div'
  6510. ];
  6511. $this->assertHtml($expected, $result);
  6512. }
  6513. /**
  6514. * testHiddenField method
  6515. *
  6516. * Test generation of a hidden input.
  6517. *
  6518. * @return void
  6519. */
  6520. public function testHiddenField()
  6521. {
  6522. $this->article['errors'] = [
  6523. 'field' => true
  6524. ];
  6525. $this->Form->request->data['field'] = 'test';
  6526. $this->Form->create($this->article);
  6527. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6528. $expected = [
  6529. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6530. $this->assertHtml($expected, $result);
  6531. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6532. $expected = [
  6533. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6534. ];
  6535. $this->assertHtml($expected, $result);
  6536. }
  6537. /**
  6538. * testFileUploadField method
  6539. *
  6540. * Test generation of a file upload input.
  6541. *
  6542. * @return void
  6543. */
  6544. public function testFileUploadField()
  6545. {
  6546. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6547. $result = $this->Form->file('Model.upload');
  6548. $this->assertHtml($expected, $result);
  6549. $this->Form->request->data['Model']['upload'] = [
  6550. 'name' => '', 'type' => '', 'tmp_name' => '',
  6551. 'error' => 4, 'size' => 0
  6552. ];
  6553. $result = $this->Form->file('Model.upload');
  6554. $this->assertHtml($expected, $result);
  6555. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6556. $result = $this->Form->file('Model.upload');
  6557. $this->assertHtml($expected, $result);
  6558. }
  6559. /**
  6560. * testFileUploadOnOtherModel method
  6561. *
  6562. * Test File upload input on a model not used in create().
  6563. *
  6564. * @return void
  6565. */
  6566. public function testFileUploadOnOtherModel()
  6567. {
  6568. $this->Form->create($this->article, ['type' => 'file']);
  6569. $result = $this->Form->file('ValidateProfile.city');
  6570. $expected = [
  6571. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6572. ];
  6573. $this->assertHtml($expected, $result);
  6574. }
  6575. /**
  6576. * testButton method
  6577. *
  6578. * Test generation of a form button.
  6579. *
  6580. * @return void
  6581. */
  6582. public function testButton()
  6583. {
  6584. $result = $this->Form->button('Hi');
  6585. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6586. $this->assertHtml($expected, $result);
  6587. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6588. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6589. $this->assertHtml($expected, $result);
  6590. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6591. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6592. $this->assertHtml($expected, $result);
  6593. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6594. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6595. $this->assertHtml($expected, $result);
  6596. $result = $this->Form->button('No type', ['type' => false]);
  6597. $expected = ['button' => [], 'No type', '/button'];
  6598. $this->assertHtml($expected, $result);
  6599. $result = $this->Form->button('Upload Text', [
  6600. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6601. 'escape' => false
  6602. ]);
  6603. $this->assertNotRegExp('/\&039/', $result);
  6604. }
  6605. /**
  6606. * testButtonUnlockedByDefault method
  6607. *
  6608. * Test that button() makes unlocked fields by default.
  6609. *
  6610. * @return void
  6611. */
  6612. public function testButtonUnlockedByDefault()
  6613. {
  6614. $this->Form->request->params['_csrfToken'] = 'secured';
  6615. $this->Form->button('Save', ['name' => 'save']);
  6616. $this->Form->button('Clear');
  6617. $result = $this->Form->unlockField();
  6618. $this->assertEquals(['save'], $result);
  6619. }
  6620. /**
  6621. * testPostButton method
  6622. *
  6623. * @return void
  6624. */
  6625. public function testPostButton()
  6626. {
  6627. $result = $this->Form->postButton('Hi', '/controller/action');
  6628. $expected = [
  6629. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6630. 'div' => ['style' => 'display:none;'],
  6631. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6632. '/div',
  6633. 'button' => ['type' => 'submit'],
  6634. 'Hi',
  6635. '/button',
  6636. '/form'
  6637. ];
  6638. $this->assertHtml($expected, $result);
  6639. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6640. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6641. }
  6642. /**
  6643. * testPostButtonMethodType method
  6644. *
  6645. * @return void
  6646. */
  6647. public function testPostButtonMethodType()
  6648. {
  6649. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6650. $expected = [
  6651. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6652. 'div' => ['style' => 'display:none;'],
  6653. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6654. '/div',
  6655. 'button' => ['type' => 'submit'],
  6656. 'Hi',
  6657. '/button',
  6658. '/form'
  6659. ];
  6660. $this->assertHtml($expected, $result);
  6661. }
  6662. /**
  6663. * testPostButtonFormOptions method
  6664. *
  6665. * @return void
  6666. */
  6667. public function testPostButtonFormOptions()
  6668. {
  6669. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6670. $expected = [
  6671. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6672. 'div' => ['style' => 'display:none;'],
  6673. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6674. '/div',
  6675. 'button' => ['type' => 'submit'],
  6676. 'Hi',
  6677. '/button',
  6678. '/form'
  6679. ];
  6680. $this->assertHtml($expected, $result);
  6681. }
  6682. /**
  6683. * testPostButtonNestedData method
  6684. *
  6685. * Test using postButton with N dimensional data.
  6686. *
  6687. * @return void
  6688. */
  6689. public function testPostButtonNestedData()
  6690. {
  6691. $data = [
  6692. 'one' => [
  6693. 'two' => [
  6694. 3, 4, 5
  6695. ]
  6696. ]
  6697. ];
  6698. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6699. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6700. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6701. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6702. }
  6703. /**
  6704. * testSecurePostButton method
  6705. *
  6706. * Test that postButton adds _Token fields.
  6707. *
  6708. * @return void
  6709. */
  6710. public function testSecurePostButton()
  6711. {
  6712. $this->Form->request->params['_csrfToken'] = 'testkey';
  6713. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6714. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6715. $tokenDebug = urlencode(json_encode([
  6716. '/posts/delete/1',
  6717. [],
  6718. []
  6719. ]));
  6720. $expected = [
  6721. 'form' => [
  6722. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6723. ],
  6724. ['div' => ['style' => 'display:none;']],
  6725. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6726. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6727. '/div',
  6728. 'button' => ['type' => 'submit'],
  6729. 'Delete',
  6730. '/button',
  6731. ['div' => ['style' => 'display:none;']],
  6732. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6733. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6734. ['input' => [
  6735. 'type' => 'hidden', 'name' => '_Token[debug]',
  6736. 'value' => $tokenDebug,
  6737. ]],
  6738. '/div',
  6739. '/form',
  6740. ];
  6741. $this->assertHtml($expected, $result);
  6742. }
  6743. /**
  6744. * testPostLink method
  6745. *
  6746. * @return void
  6747. */
  6748. public function testPostLink()
  6749. {
  6750. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6751. $expected = [
  6752. 'form' => [
  6753. 'method' => 'post', 'action' => '/posts/delete/1',
  6754. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6755. ],
  6756. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6757. '/form',
  6758. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6759. 'Delete',
  6760. '/a'
  6761. ];
  6762. $this->assertHtml($expected, $result);
  6763. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6764. $expected = [
  6765. 'form' => [
  6766. 'method' => 'post', 'action' => '/posts/delete/1',
  6767. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6768. ],
  6769. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6770. '/form',
  6771. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6772. 'Delete',
  6773. '/a'
  6774. ];
  6775. $this->assertHtml($expected, $result);
  6776. $result = $this->Form->postLink(
  6777. 'Delete',
  6778. '/posts/delete/1',
  6779. ['target' => '_blank', 'class' => 'btn btn-danger']
  6780. );
  6781. $expected = [
  6782. 'form' => [
  6783. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6784. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6785. ],
  6786. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6787. '/form',
  6788. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6789. 'Delete',
  6790. '/a'
  6791. ];
  6792. $this->assertHtml($expected, $result);
  6793. }
  6794. /**
  6795. * testPostLinkWithConfirm method
  6796. *
  6797. * Test the confirm option for postLink().
  6798. *
  6799. * @return void
  6800. */
  6801. public function testPostLinkWithConfirm()
  6802. {
  6803. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6804. $expected = [
  6805. 'form' => [
  6806. 'method' => 'post', 'action' => '/posts/delete/1',
  6807. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6808. ],
  6809. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6810. '/form',
  6811. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6812. 'Delete',
  6813. '/a'
  6814. ];
  6815. $this->assertHtml($expected, $result);
  6816. $result = $this->Form->postLink(
  6817. 'Delete',
  6818. '/posts/delete/1',
  6819. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6820. );
  6821. $expected = [
  6822. 'form' => [
  6823. 'method' => 'post', 'action' => '/posts/delete/1',
  6824. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6825. ],
  6826. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6827. '/form',
  6828. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\\\nthis \\\&quot;deletion\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6829. 'Delete',
  6830. '/a'
  6831. ];
  6832. $this->assertHtml($expected, $result);
  6833. }
  6834. /**
  6835. * testPostLinkWithQuery method
  6836. *
  6837. * Test postLink() with query string args.
  6838. *
  6839. * @return void
  6840. */
  6841. public function testPostLinkWithQuery()
  6842. {
  6843. $result = $this->Form->postLink(
  6844. 'Delete',
  6845. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6846. );
  6847. $expected = [
  6848. 'form' => [
  6849. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6850. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6851. ],
  6852. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6853. '/form',
  6854. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6855. 'Delete',
  6856. '/a'
  6857. ];
  6858. $this->assertHtml($expected, $result);
  6859. }
  6860. /**
  6861. * testPostLinkWithData method
  6862. *
  6863. * Test postLink with additional data.
  6864. *
  6865. * @return void
  6866. */
  6867. public function testPostLinkWithData()
  6868. {
  6869. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6870. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6871. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6872. $this->Form->create($entity);
  6873. $this->Form->end();
  6874. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6875. $this->assertContains(
  6876. '<input type="hidden" name="name" value="show"',
  6877. $result,
  6878. 'should not contain entity data.'
  6879. );
  6880. }
  6881. /**
  6882. * testPostLinkSecurityHash method
  6883. *
  6884. * Test that security hashes for postLink include the url.
  6885. *
  6886. * @return void
  6887. */
  6888. public function testPostLinkSecurityHash()
  6889. {
  6890. $hash = Security::hash(
  6891. '/posts/delete/1' .
  6892. serialize(['id' => '1']) .
  6893. '' .
  6894. Security::salt()
  6895. );
  6896. $hash .= '%3Aid';
  6897. $this->Form->request->params['_Token']['key'] = 'test';
  6898. $result = $this->Form->postLink(
  6899. 'Delete',
  6900. '/posts/delete/1',
  6901. ['data' => ['id' => 1]]
  6902. );
  6903. $tokenDebug = urlencode(json_encode([
  6904. '/posts/delete/1',
  6905. [
  6906. 'id' => 1
  6907. ],
  6908. []
  6909. ]));
  6910. $expected = [
  6911. 'form' => [
  6912. 'method' => 'post', 'action' => '/posts/delete/1',
  6913. 'name', 'style' => 'display:none;'
  6914. ],
  6915. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6916. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6917. 'div' => ['style' => 'display:none;'],
  6918. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6919. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6920. ['input' => [
  6921. 'type' => 'hidden', 'name' => '_Token[debug]',
  6922. 'value' => $tokenDebug,
  6923. ]],
  6924. '/div',
  6925. '/form',
  6926. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6927. 'Delete',
  6928. '/a'
  6929. ];
  6930. $this->assertHtml($expected, $result);
  6931. }
  6932. /**
  6933. * testPostLinkSecurityHashBlockMode method
  6934. *
  6935. * Test that postLink doesn't modify the fields in the containing form.
  6936. *
  6937. * postLink() calls inside open forms should not modify the field list
  6938. * for the form.
  6939. *
  6940. * @return void
  6941. */
  6942. public function testPostLinkSecurityHashBlockMode()
  6943. {
  6944. $hash = Security::hash(
  6945. '/posts/delete/1' .
  6946. serialize([]) .
  6947. '' .
  6948. Security::salt()
  6949. );
  6950. $hash .= '%3A';
  6951. $this->Form->request->params['_Token']['key'] = 'test';
  6952. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  6953. $this->Form->control('title');
  6954. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6955. $result = $this->View->fetch('postLink');
  6956. $this->assertEquals(['title'], $this->Form->fields);
  6957. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  6958. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  6959. }
  6960. /**
  6961. * testPostLinkSecurityHashNoDebugMode method
  6962. *
  6963. * Test that security does not include debug token if debug is false.
  6964. *
  6965. * @return void
  6966. */
  6967. public function testPostLinkSecurityHashNoDebugMode()
  6968. {
  6969. Configure::write('debug', false);
  6970. $hash = Security::hash(
  6971. '/posts/delete/1' .
  6972. serialize(['id' => '1']) .
  6973. '' .
  6974. Security::salt()
  6975. );
  6976. $hash .= '%3Aid';
  6977. $this->Form->request->params['_Token']['key'] = 'test';
  6978. $result = $this->Form->postLink(
  6979. 'Delete',
  6980. '/posts/delete/1',
  6981. ['data' => ['id' => 1]]
  6982. );
  6983. $expected = [
  6984. 'form' => [
  6985. 'method' => 'post', 'action' => '/posts/delete/1',
  6986. 'name', 'style' => 'display:none;'
  6987. ],
  6988. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6989. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6990. 'div' => ['style' => 'display:none;'],
  6991. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6992. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6993. '/div',
  6994. '/form',
  6995. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6996. 'Delete',
  6997. '/a'
  6998. ];
  6999. $this->assertHtml($expected, $result);
  7000. }
  7001. /**
  7002. * testPostLinkNestedData method
  7003. *
  7004. * Test using postLink with N dimensional data.
  7005. *
  7006. * @return void
  7007. */
  7008. public function testPostLinkNestedData()
  7009. {
  7010. $data = [
  7011. 'one' => [
  7012. 'two' => [
  7013. 3, 4, 5
  7014. ]
  7015. ]
  7016. ];
  7017. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  7018. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  7019. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  7020. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  7021. }
  7022. /**
  7023. * testPostLinkAfterGetForm method
  7024. *
  7025. * Test creating postLinks after a GET form.
  7026. *
  7027. * @return void
  7028. */
  7029. public function testPostLinkAfterGetForm()
  7030. {
  7031. $this->Form->request->params['_csrfToken'] = 'testkey';
  7032. $this->Form->request->params['_Token'] = 'val';
  7033. $this->Form->create($this->article, ['type' => 'get']);
  7034. $this->Form->end();
  7035. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7036. $tokenDebug = urlencode(json_encode([
  7037. '/posts/delete/1',
  7038. [],
  7039. []
  7040. ]));
  7041. $expected = [
  7042. 'form' => [
  7043. 'method' => 'post', 'action' => '/posts/delete/1',
  7044. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7045. ],
  7046. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  7047. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  7048. 'div' => ['style' => 'display:none;'],
  7049. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  7050. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  7051. ['input' => [
  7052. 'type' => 'hidden', 'name' => '_Token[debug]',
  7053. 'value' => $tokenDebug,
  7054. ]],
  7055. '/div',
  7056. '/form',
  7057. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7058. 'Delete',
  7059. '/a'
  7060. ];
  7061. $this->assertHtml($expected, $result);
  7062. }
  7063. /**
  7064. * testPostLinkFormBuffer method
  7065. *
  7066. * Test that postLink adds form tags to view block.
  7067. *
  7068. * @return void
  7069. */
  7070. public function testPostLinkFormBuffer()
  7071. {
  7072. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  7073. $expected = [
  7074. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7075. 'Delete',
  7076. '/a'
  7077. ];
  7078. $this->assertHtml($expected, $result);
  7079. $result = $this->View->fetch('postLink');
  7080. $expected = [
  7081. 'form' => [
  7082. 'method' => 'post', 'action' => '/posts/delete/1',
  7083. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7084. ],
  7085. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7086. '/form'
  7087. ];
  7088. $this->assertHtml($expected, $result);
  7089. $result = $this->Form->postLink(
  7090. 'Delete',
  7091. '/posts/delete/2',
  7092. ['block' => true, 'method' => 'DELETE']
  7093. );
  7094. $expected = [
  7095. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7096. 'Delete',
  7097. '/a'
  7098. ];
  7099. $this->assertHtml($expected, $result);
  7100. $result = $this->View->fetch('postLink');
  7101. $expected = [
  7102. 'form' => [
  7103. 'method' => 'post', 'action' => '/posts/delete/1',
  7104. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7105. ],
  7106. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7107. '/form',
  7108. [
  7109. 'form' => [
  7110. 'method' => 'post', 'action' => '/posts/delete/2',
  7111. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7112. ],
  7113. ],
  7114. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  7115. '/form'
  7116. ];
  7117. $this->assertHtml($expected, $result);
  7118. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  7119. $expected = [
  7120. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  7121. 'Delete',
  7122. '/a'
  7123. ];
  7124. $this->assertHtml($expected, $result);
  7125. $result = $this->View->fetch('foobar');
  7126. $expected = [
  7127. 'form' => [
  7128. 'method' => 'post', 'action' => '/posts/delete/1',
  7129. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7130. ],
  7131. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  7132. '/form'
  7133. ];
  7134. $this->assertHtml($expected, $result);
  7135. }
  7136. /**
  7137. * testSubmitButton method
  7138. *
  7139. * @return void
  7140. */
  7141. public function testSubmitButton()
  7142. {
  7143. $result = $this->Form->submit('');
  7144. $expected = [
  7145. 'div' => ['class' => 'submit'],
  7146. 'input' => ['type' => 'submit', 'value' => ''],
  7147. '/div'
  7148. ];
  7149. $this->assertHtml($expected, $result);
  7150. $result = $this->Form->submit('Test Submit');
  7151. $expected = [
  7152. 'div' => ['class' => 'submit'],
  7153. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  7154. '/div'
  7155. ];
  7156. $this->assertHtml($expected, $result);
  7157. $result = $this->Form->submit('Next >');
  7158. $expected = [
  7159. 'div' => ['class' => 'submit'],
  7160. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  7161. '/div'
  7162. ];
  7163. $this->assertHtml($expected, $result);
  7164. $result = $this->Form->submit('Next >', ['escape' => false]);
  7165. $expected = [
  7166. 'div' => ['class' => 'submit'],
  7167. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  7168. '/div'
  7169. ];
  7170. $this->assertHtml($expected, $result);
  7171. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  7172. $expected = [
  7173. 'div' => ['class' => 'submit'],
  7174. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  7175. '/div'
  7176. ];
  7177. $this->assertHtml($expected, $result);
  7178. }
  7179. /**
  7180. * testSubmitImage method
  7181. *
  7182. * Test image submit types.
  7183. *
  7184. * @return void
  7185. */
  7186. public function testSubmitImage()
  7187. {
  7188. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7189. $expected = [
  7190. 'div' => ['class' => 'submit'],
  7191. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  7192. '/div'
  7193. ];
  7194. $this->assertHtml($expected, $result);
  7195. $result = $this->Form->submit('/relative/cake.power.gif');
  7196. $expected = [
  7197. 'div' => ['class' => 'submit'],
  7198. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  7199. '/div'
  7200. ];
  7201. $this->assertHtml($expected, $result);
  7202. $result = $this->Form->submit('cake.power.gif');
  7203. $expected = [
  7204. 'div' => ['class' => 'submit'],
  7205. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  7206. '/div'
  7207. ];
  7208. $this->assertHtml($expected, $result);
  7209. $result = $this->Form->submit('Not.an.image');
  7210. $expected = [
  7211. 'div' => ['class' => 'submit'],
  7212. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  7213. '/div'
  7214. ];
  7215. $this->assertHtml($expected, $result);
  7216. }
  7217. /**
  7218. * testSubmitUnlockedByDefault method
  7219. *
  7220. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7221. * be submitted at a time.
  7222. *
  7223. * @return void
  7224. */
  7225. public function testSubmitUnlockedByDefault()
  7226. {
  7227. $this->Form->request->params['_Token'] = 'secured';
  7228. $this->Form->submit('Go go');
  7229. $this->Form->submit('Save', ['name' => 'save']);
  7230. $result = $this->Form->unlockField();
  7231. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  7232. }
  7233. /**
  7234. * testSubmitImageTimestamp method
  7235. *
  7236. * Test submit image with timestamps.
  7237. *
  7238. * @return void
  7239. */
  7240. public function testSubmitImageTimestamp()
  7241. {
  7242. Configure::write('Asset.timestamp', 'force');
  7243. $result = $this->Form->submit('cake.power.gif');
  7244. $expected = [
  7245. 'div' => ['class' => 'submit'],
  7246. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  7247. '/div'
  7248. ];
  7249. $this->assertHtml($expected, $result);
  7250. }
  7251. /**
  7252. * testDateTimeWithGetForms method
  7253. *
  7254. * Test that datetime() works with GET style forms.
  7255. *
  7256. * @return void
  7257. */
  7258. public function testDateTimeWithGetForms()
  7259. {
  7260. extract($this->dateRegex);
  7261. $this->Form->create($this->article, ['type' => 'get']);
  7262. $result = $this->Form->datetime('created');
  7263. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  7264. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  7265. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  7266. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  7267. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  7268. }
  7269. /**
  7270. * testForMagicControlNonExistingNorValidated method
  7271. *
  7272. * @return void
  7273. */
  7274. public function testForMagicControlNonExistingNorValidated()
  7275. {
  7276. $this->Form->create($this->article);
  7277. $this->Form->templates(['inputContainer' => '{{content}}']);
  7278. $result = $this->Form->control('non_existing_nor_validated');
  7279. $expected = [
  7280. 'label' => ['for' => 'non-existing-nor-validated'],
  7281. 'Non Existing Nor Validated',
  7282. '/label',
  7283. 'input' => [
  7284. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7285. 'id' => 'non-existing-nor-validated'
  7286. ]
  7287. ];
  7288. $this->assertHtml($expected, $result);
  7289. $result = $this->Form->control('non_existing_nor_validated', [
  7290. 'val' => 'my value'
  7291. ]);
  7292. $expected = [
  7293. 'label' => ['for' => 'non-existing-nor-validated'],
  7294. 'Non Existing Nor Validated',
  7295. '/label',
  7296. 'input' => [
  7297. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7298. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7299. ]
  7300. ];
  7301. $this->assertHtml($expected, $result);
  7302. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7303. $result = $this->Form->control('non_existing_nor_validated');
  7304. $expected = [
  7305. 'label' => ['for' => 'non-existing-nor-validated'],
  7306. 'Non Existing Nor Validated',
  7307. '/label',
  7308. 'input' => [
  7309. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7310. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7311. ]
  7312. ];
  7313. $this->assertHtml($expected, $result);
  7314. }
  7315. /**
  7316. * testFormMagicControlLabel method
  7317. *
  7318. * @return void
  7319. */
  7320. public function testFormMagicControlLabel()
  7321. {
  7322. TableRegistry::get('Contacts', [
  7323. 'className' => __NAMESPACE__ . '\ContactsTable'
  7324. ]);
  7325. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7326. $this->Form->templates(['inputContainer' => '{{content}}']);
  7327. $result = $this->Form->control('Contacts.name', ['label' => 'My label']);
  7328. $expected = [
  7329. 'label' => ['for' => 'contacts-name'],
  7330. 'My label',
  7331. '/label',
  7332. 'input' => [
  7333. 'type' => 'text', 'name' => 'Contacts[name]',
  7334. 'id' => 'contacts-name', 'maxlength' => '255'
  7335. ]
  7336. ];
  7337. $this->assertHtml($expected, $result);
  7338. $result = $this->Form->control('name', [
  7339. 'label' => ['class' => 'mandatory']
  7340. ]);
  7341. $expected = [
  7342. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7343. 'Name',
  7344. '/label',
  7345. 'input' => [
  7346. 'type' => 'text', 'name' => 'name',
  7347. 'id' => 'name', 'maxlength' => '255'
  7348. ]
  7349. ];
  7350. $this->assertHtml($expected, $result);
  7351. $result = $this->Form->control('name', [
  7352. 'div' => false,
  7353. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7354. ]);
  7355. $expected = [
  7356. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7357. 'My label',
  7358. '/label',
  7359. 'input' => [
  7360. 'type' => 'text', 'name' => 'name',
  7361. 'id' => 'name', 'maxlength' => '255'
  7362. ]
  7363. ];
  7364. $this->assertHtml($expected, $result);
  7365. $result = $this->Form->control('Contact.name', [
  7366. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7367. ]);
  7368. $expected = [
  7369. 'label' => ['for' => 'my_id'],
  7370. 'Name',
  7371. '/label',
  7372. 'input' => [
  7373. 'type' => 'text', 'name' => 'Contact[name]',
  7374. 'id' => 'my_id', 'maxlength' => '255'
  7375. ]
  7376. ];
  7377. $this->assertHtml($expected, $result);
  7378. $result = $this->Form->control('1.id');
  7379. $expected = ['input' => [
  7380. 'type' => 'hidden', 'name' => '1[id]',
  7381. 'id' => '1-id'
  7382. ]];
  7383. $this->assertHtml($expected, $result);
  7384. $result = $this->Form->control("1.name");
  7385. $expected = [
  7386. 'label' => ['for' => '1-name'],
  7387. 'Name',
  7388. '/label',
  7389. 'input' => [
  7390. 'type' => 'text', 'name' => '1[name]',
  7391. 'id' => '1-name', 'maxlength' => '255'
  7392. ]
  7393. ];
  7394. $this->assertHtml($expected, $result);
  7395. }
  7396. /**
  7397. * testFormEnd method
  7398. *
  7399. * @return void
  7400. */
  7401. public function testFormEnd()
  7402. {
  7403. $this->assertEquals('</form>', $this->Form->end());
  7404. }
  7405. /**
  7406. * testMultiRecordForm method
  7407. *
  7408. * Test the generation of fields for a multi record form.
  7409. *
  7410. * @return void
  7411. */
  7412. public function testMultiRecordForm()
  7413. {
  7414. $this->loadFixtures('Articles', 'Comments');
  7415. $articles = TableRegistry::get('Articles');
  7416. $articles->hasMany('Comments');
  7417. $comment = new Entity(['comment' => 'Value']);
  7418. $article = new Article(['comments' => [$comment]]);
  7419. $this->Form->create([$article]);
  7420. $result = $this->Form->control('0.comments.1.comment');
  7421. //@codingStandardsIgnoreStart
  7422. $expected = [
  7423. 'div' => ['class' => 'input textarea'],
  7424. 'label' => ['for' => '0-comments-1-comment'],
  7425. 'Comment',
  7426. '/label',
  7427. 'textarea' => [
  7428. 'name',
  7429. 'id' => '0-comments-1-comment',
  7430. 'rows' => 5
  7431. ],
  7432. '/textarea',
  7433. '/div'
  7434. ];
  7435. //@codingStandardsIgnoreEnd
  7436. $this->assertHtml($expected, $result);
  7437. $result = $this->Form->control('0.comments.0.comment');
  7438. //@codingStandardsIgnoreStart
  7439. $expected = [
  7440. 'div' => ['class' => 'input textarea'],
  7441. 'label' => ['for' => '0-comments-0-comment'],
  7442. 'Comment',
  7443. '/label',
  7444. 'textarea' => [
  7445. 'name',
  7446. 'id' => '0-comments-0-comment',
  7447. 'rows' => 5
  7448. ],
  7449. 'Value',
  7450. '/textarea',
  7451. '/div'
  7452. ];
  7453. //@codingStandardsIgnoreEnd
  7454. $this->assertHtml($expected, $result);
  7455. $comment->errors('comment', ['Not valid']);
  7456. $result = $this->Form->control('0.comments.0.comment');
  7457. //@codingStandardsIgnoreStart
  7458. $expected = [
  7459. 'div' => ['class' => 'input textarea error'],
  7460. 'label' => ['for' => '0-comments-0-comment'],
  7461. 'Comment',
  7462. '/label',
  7463. 'textarea' => [
  7464. 'name',
  7465. 'class' => 'form-error',
  7466. 'id' => '0-comments-0-comment',
  7467. 'rows' => 5
  7468. ],
  7469. 'Value',
  7470. '/textarea',
  7471. ['div' => ['class' => 'error-message']],
  7472. 'Not valid',
  7473. '/div',
  7474. '/div'
  7475. ];
  7476. //@codingStandardsIgnoreEnd
  7477. $this->assertHtml($expected, $result);
  7478. TableRegistry::get('Comments')
  7479. ->validator('default')
  7480. ->allowEmpty('comment', false);
  7481. $result = $this->Form->control('0.comments.1.comment');
  7482. //@codingStandardsIgnoreStart
  7483. $expected = [
  7484. 'div' => ['class' => 'input textarea required'],
  7485. 'label' => ['for' => '0-comments-1-comment'],
  7486. 'Comment',
  7487. '/label',
  7488. 'textarea' => [
  7489. 'name',
  7490. 'required' => 'required',
  7491. 'id' => '0-comments-1-comment',
  7492. 'rows' => 5
  7493. ],
  7494. '/textarea',
  7495. '/div'
  7496. ];
  7497. //@codingStandardsIgnoreEnd
  7498. $this->assertHtml($expected, $result);
  7499. }
  7500. /**
  7501. * testHtml5Controls method
  7502. *
  7503. * Test that some html5 inputs + FormHelper::__call() work.
  7504. *
  7505. * @return void
  7506. */
  7507. public function testHtml5Controls()
  7508. {
  7509. $result = $this->Form->email('User.email');
  7510. $expected = [
  7511. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7512. ];
  7513. $this->assertHtml($expected, $result);
  7514. $result = $this->Form->search('User.query');
  7515. $expected = [
  7516. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7517. ];
  7518. $this->assertHtml($expected, $result);
  7519. $result = $this->Form->search('User.query', ['value' => 'test']);
  7520. $expected = [
  7521. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7522. ];
  7523. $this->assertHtml($expected, $result);
  7524. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7525. $expected = [
  7526. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7527. ];
  7528. $this->assertHtml($expected, $result);
  7529. }
  7530. /**
  7531. * testHtml5ControlWithControl method
  7532. *
  7533. * Test accessing html5 inputs through control().
  7534. *
  7535. * @return void
  7536. */
  7537. public function testHtml5ControlWithControl()
  7538. {
  7539. $this->Form->create();
  7540. $this->Form->templates(['inputContainer' => '{{content}}']);
  7541. $result = $this->Form->control('website', [
  7542. 'type' => 'url',
  7543. 'val' => 'http://domain.tld',
  7544. 'label' => false
  7545. ]);
  7546. $expected = [
  7547. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7548. ];
  7549. $this->assertHtml($expected, $result);
  7550. }
  7551. /**
  7552. * testHtml5ControlException method
  7553. *
  7554. * Test errors when field name is missing.
  7555. *
  7556. * @expectedException \Cake\Core\Exception\Exception
  7557. * @return void
  7558. */
  7559. public function testHtml5ControlException()
  7560. {
  7561. $this->Form->email();
  7562. }
  7563. /**
  7564. * testRequiredAttribute method
  7565. *
  7566. * Tests that formhelper sets required attributes.
  7567. *
  7568. * @return void
  7569. */
  7570. public function testRequiredAttribute()
  7571. {
  7572. $this->article['required'] = [
  7573. 'title' => true,
  7574. 'body' => false,
  7575. ];
  7576. $this->Form->create($this->article);
  7577. $result = $this->Form->control('title');
  7578. $expected = [
  7579. 'div' => ['class' => 'input text required'],
  7580. 'label' => ['for' => 'title'],
  7581. 'Title',
  7582. '/label',
  7583. 'input' => [
  7584. 'type' => 'text',
  7585. 'name' => 'title',
  7586. 'id' => 'title',
  7587. 'required' => 'required',
  7588. ],
  7589. '/div'
  7590. ];
  7591. $this->assertHtml($expected, $result);
  7592. $result = $this->Form->control('title', ['required' => false]);
  7593. $this->assertNotContains('required', $result);
  7594. $result = $this->Form->control('body');
  7595. $expected = [
  7596. 'div' => ['class' => 'input text'],
  7597. 'label' => ['for' => 'body'],
  7598. 'Body',
  7599. '/label',
  7600. 'input' => [
  7601. 'type' => 'text',
  7602. 'name' => 'body',
  7603. 'id' => 'body',
  7604. ],
  7605. '/div'
  7606. ];
  7607. $this->assertHtml($expected, $result);
  7608. $result = $this->Form->control('body', ['required' => true]);
  7609. $this->assertContains('required', $result);
  7610. }
  7611. /**
  7612. * testControlsNotNested method
  7613. *
  7614. * Tests that it is possible to put inputs outside of the label.
  7615. *
  7616. * @return void
  7617. */
  7618. public function testControlsNotNested()
  7619. {
  7620. $this->Form->templates([
  7621. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7622. 'formGroup' => '{{input}}{{label}}',
  7623. ]);
  7624. $result = $this->Form->control('foo', ['type' => 'checkbox']);
  7625. $expected = [
  7626. 'div' => ['class' => 'input checkbox'],
  7627. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7628. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7629. 'label' => ['for' => 'foo'],
  7630. 'Foo',
  7631. '/label',
  7632. '/div'
  7633. ];
  7634. $this->assertHtml($expected, $result);
  7635. $result = $this->Form->control('foo', ['type' => 'checkbox', 'label' => false]);
  7636. $expected = [
  7637. 'div' => ['class' => 'input checkbox'],
  7638. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7639. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7640. '/div'
  7641. ];
  7642. $this->assertHtml($expected, $result);
  7643. $result = $this->Form->control('confirm', [
  7644. 'type' => 'radio',
  7645. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7646. ]);
  7647. $expected = [
  7648. 'div' => ['class' => 'input radio'],
  7649. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7650. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7651. ['label' => ['for' => 'confirm-y']],
  7652. 'Yes',
  7653. '/label',
  7654. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7655. ['label' => ['for' => 'confirm-n']],
  7656. 'No',
  7657. '/label',
  7658. '<label',
  7659. 'Confirm',
  7660. '/label',
  7661. '/div',
  7662. ];
  7663. $this->assertHtml($expected, $result);
  7664. $result = $this->Form->select('category', ['1', '2'], [
  7665. 'multiple' => 'checkbox',
  7666. 'name' => 'fish',
  7667. ]);
  7668. $expected = [
  7669. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7670. ['div' => ['class' => 'checkbox']],
  7671. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7672. ['label' => ['for' => 'fish-0']],
  7673. '1',
  7674. '/label',
  7675. '/div',
  7676. ['div' => ['class' => 'checkbox']],
  7677. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7678. ['label' => ['for' => 'fish-1']],
  7679. '2',
  7680. '/label',
  7681. '/div'
  7682. ];
  7683. $this->assertHtml($expected, $result);
  7684. }
  7685. /**
  7686. * testControlContainerTemplates method
  7687. *
  7688. * Test that *Container templates are used by input.
  7689. *
  7690. * @return void
  7691. */
  7692. public function testControlContainerTemplates()
  7693. {
  7694. $this->Form->templates([
  7695. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7696. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7697. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7698. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7699. ]);
  7700. $this->article['errors'] = [
  7701. 'Article' => ['published' => 'error message']
  7702. ];
  7703. $this->Form->create($this->article);
  7704. $result = $this->Form->control('accept', [
  7705. 'type' => 'checkbox'
  7706. ]);
  7707. $expected = [
  7708. 'div' => ['class' => 'check'],
  7709. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7710. 'label' => ['for' => 'accept'],
  7711. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7712. 'Accept',
  7713. '/label',
  7714. '/div'
  7715. ];
  7716. $this->assertHtml($expected, $result);
  7717. $result = $this->Form->control('accept', [
  7718. 'type' => 'radio',
  7719. 'options' => ['Y', 'N']
  7720. ]);
  7721. $this->assertContains('<div class="rad">', $result);
  7722. $result = $this->Form->control('Article.published', [
  7723. 'type' => 'radio',
  7724. 'options' => ['Y', 'N']
  7725. ]);
  7726. $this->assertContains('<div class="rad err">', $result);
  7727. $result = $this->Form->control('Article.created', [
  7728. 'type' => 'datetime',
  7729. ]);
  7730. $this->assertContains('<div class="dt">', $result);
  7731. }
  7732. /**
  7733. * testFormGroupTemplates method
  7734. *
  7735. * Test that *Container templates are used by input.
  7736. *
  7737. * @return void
  7738. */
  7739. public function testFormGroupTemplates()
  7740. {
  7741. $this->Form->templates([
  7742. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7743. ]);
  7744. $this->Form->create($this->article);
  7745. $result = $this->Form->control('accept', [
  7746. 'type' => 'radio',
  7747. 'options' => ['Y', 'N']
  7748. ]);
  7749. $this->assertContains('<div class="radio">', $result);
  7750. }
  7751. /**
  7752. * testResetTemplates method
  7753. *
  7754. * Test resetting templates.
  7755. *
  7756. * @return void
  7757. */
  7758. public function testResetTemplates()
  7759. {
  7760. $this->Form->templates(['input' => '<input/>']);
  7761. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7762. $this->assertNull($this->Form->resetTemplates());
  7763. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7764. }
  7765. /**
  7766. * testContext method
  7767. *
  7768. * Test the context method.
  7769. *
  7770. * @return void
  7771. */
  7772. public function testContext()
  7773. {
  7774. $result = $this->Form->context();
  7775. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7776. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7777. $this->assertSame($mock, $this->Form->context($mock));
  7778. $this->assertSame($mock, $this->Form->context());
  7779. }
  7780. /**
  7781. * testAutoDomId method
  7782. *
  7783. * @return void
  7784. */
  7785. public function testAutoDomId()
  7786. {
  7787. $result = $this->Form->text('field', ['id' => true]);
  7788. $expected = [
  7789. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7790. ];
  7791. $this->assertHtml($expected, $result);
  7792. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7793. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7794. $expected = [
  7795. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7796. ['label' => ['for' => 'field-0']],
  7797. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7798. 'option A',
  7799. '/label',
  7800. ['label' => ['for' => 'field-1']],
  7801. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7802. 'option B',
  7803. '/label',
  7804. ];
  7805. $this->assertHtml($expected, $result);
  7806. $result = $this->Form->select(
  7807. 'multi_field',
  7808. ['first', 'second'],
  7809. ['multiple' => 'checkbox', 'id' => true]
  7810. );
  7811. $expected = [
  7812. 'input' => [
  7813. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7814. ],
  7815. ['div' => ['class' => 'checkbox']],
  7816. ['label' => ['for' => 'multi-field-0']],
  7817. ['input' => [
  7818. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7819. 'value' => '0', 'id' => 'multi-field-0'
  7820. ]],
  7821. 'first',
  7822. '/label',
  7823. '/div',
  7824. ['div' => ['class' => 'checkbox']],
  7825. ['label' => ['for' => 'multi-field-1']],
  7826. ['input' => [
  7827. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7828. 'value' => '1', 'id' => 'multi-field-1'
  7829. ]],
  7830. 'second',
  7831. '/label',
  7832. '/div',
  7833. ];
  7834. $this->assertHtml($expected, $result);
  7835. }
  7836. /**
  7837. * Test the basic setters and getters for value sources
  7838. *
  7839. * @return void
  7840. */
  7841. public function testFormValueSourcesSettersGetters()
  7842. {
  7843. $expected = ['context'];
  7844. $result = $this->Form->getValueSources();
  7845. $this->assertEquals($expected, $result);
  7846. $expected = null;
  7847. $result = $this->Form->getSourceValue('id');
  7848. $this->assertEquals($expected, $result);
  7849. $expected = ['query', 'data', 'context'];
  7850. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7851. $result = $this->Form->getValueSources();
  7852. $this->assertEquals($expected, $result);
  7853. $this->Form->request->data['id'] = '1';
  7854. $this->Form->request->query['id'] = '2';
  7855. $this->Form->setValueSources(['context']);
  7856. $expected = '1';
  7857. $result = $this->Form->getSourceValue('id');
  7858. $this->assertEquals($expected, $result);
  7859. $this->Form->setValueSources('query');
  7860. $expected = ['query'];
  7861. $result = $this->Form->getValueSources();
  7862. $this->assertEquals($expected, $result);
  7863. $expected = '2';
  7864. $result = $this->Form->getSourceValue('id');
  7865. $this->assertEquals($expected, $result);
  7866. $this->Form->setValueSources(['data']);
  7867. $expected = '1';
  7868. $result = $this->Form->getSourceValue('id');
  7869. $this->assertEquals($expected, $result);
  7870. $this->Form->setValueSources(['query', 'data']);
  7871. $expected = '2';
  7872. $result = $this->Form->getSourceValue('id');
  7873. $this->assertEquals($expected, $result);
  7874. }
  7875. /**
  7876. * Tests the different input rendering values based on sources values switching
  7877. *
  7878. * @return void
  7879. */
  7880. public function testFormValueSourcesSingleSwitchRendering()
  7881. {
  7882. $this->loadFixtures('Articles');
  7883. $articles = TableRegistry::get('Articles');
  7884. $article = new Article();
  7885. $articles->patchEntity($article, ['id' => '3']);
  7886. $this->Form->create($article);
  7887. $this->Form->setValueSources(['context']);
  7888. $result = $this->Form->control('id');
  7889. $expected = [
  7890. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7891. ];
  7892. $this->assertHtml($expected, $result);
  7893. $this->Form->request->query['id'] = '5';
  7894. $this->Form->setValueSources(['query']);
  7895. $result = $this->Form->control('id');
  7896. $expected = [
  7897. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7898. ];
  7899. $this->assertHtml($expected, $result);
  7900. $this->Form->request->query['id'] = '5a';
  7901. $this->Form->request->data['id'] = '5b';
  7902. $this->Form->setValueSources(['context']);
  7903. $this->Form->create($article);
  7904. $result = $this->Form->control('id');
  7905. $expected = [
  7906. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7907. ];
  7908. $this->assertHtml($expected, $result);
  7909. $this->Form->setValueSources(['data']);
  7910. $this->Form->create($article);
  7911. $result = $this->Form->control('id');
  7912. $expected = [
  7913. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7914. ];
  7915. $this->assertHtml($expected, $result);
  7916. $this->Form->setValueSources(['query']);
  7917. $result = $this->Form->control('id');
  7918. $expected = [
  7919. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7920. ];
  7921. $this->assertHtml($expected, $result);
  7922. }
  7923. /**
  7924. * Tests the different input rendering values based on sources values switching while supplying
  7925. * an entity (base context) and multiple sources (such as data, query)
  7926. *
  7927. * @return void
  7928. */
  7929. public function testFormValueSourcesListSwitchRendering()
  7930. {
  7931. $this->loadFixtures('Articles');
  7932. $articles = TableRegistry::get('Articles');
  7933. $article = new Article();
  7934. $articles->patchEntity($article, ['id' => '3']);
  7935. $this->Form->request->query['id'] = '9';
  7936. $this->Form->create($article);
  7937. $this->Form->setValueSources(['context', 'query']);
  7938. $result = $this->Form->control('id');
  7939. $expected = [
  7940. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7941. ];
  7942. $this->assertHtml($expected, $result);
  7943. $this->Form->setValueSources(['query', 'context']);
  7944. $result = $this->Form->control('id');
  7945. $expected = [
  7946. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7947. ];
  7948. $this->assertHtml($expected, $result);
  7949. $this->Form->setValueSources(['data', 'query', 'context']);
  7950. $result = $this->Form->control('id');
  7951. $expected = [
  7952. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7953. ];
  7954. $this->assertHtml($expected, $result);
  7955. $this->Form->request->data['id'] = '8';
  7956. $this->Form->request->query['id'] = '9';
  7957. $this->Form->setValueSources(['data', 'query', 'context']);
  7958. $result = $this->Form->control('id');
  7959. $expected = [
  7960. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7961. ];
  7962. $this->assertHtml($expected, $result);
  7963. }
  7964. /**
  7965. * Test the different form input renderings based on values sources switchings through form options
  7966. *
  7967. * @return void
  7968. */
  7969. public function testFormValueSourcesSwitchViaOptionsRendering()
  7970. {
  7971. $this->loadFixtures('Articles');
  7972. $articles = TableRegistry::get('Articles');
  7973. $article = new Article();
  7974. $articles->patchEntity($article, ['id' => '3']);
  7975. $this->Form->request->data['id'] = '4';
  7976. $this->Form->request->query['id'] = '5';
  7977. $this->Form->create($article, ['valueSources' => 'query']);
  7978. $result = $this->Form->control('id');
  7979. $expected = [
  7980. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7981. ];
  7982. $this->assertHtml($expected, $result);
  7983. $result = $this->Form->getSourceValue('id');
  7984. $this->assertEquals('5', $result);
  7985. $this->Form->setValueSources(['context']);
  7986. $this->Form->create($article, ['valueSources' => 'query']);
  7987. $result = $this->Form->control('id');
  7988. $expected = [
  7989. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7990. ];
  7991. $this->assertHtml($expected, $result);
  7992. $result = $this->Form->getSourceValue('id');
  7993. $this->assertEquals('5', $result);
  7994. $this->Form->setValueSources(['query']);
  7995. $this->Form->create($article, ['valueSources' => 'data']);
  7996. $result = $this->Form->control('id');
  7997. $expected = [
  7998. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7999. ];
  8000. $this->assertHtml($expected, $result);
  8001. $result = $this->Form->getSourceValue('id');
  8002. $this->assertEquals('4', $result);
  8003. $this->Form->setValueSources(['query']);
  8004. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  8005. $result = $this->Form->control('id');
  8006. $expected = [
  8007. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  8008. ];
  8009. $this->assertHtml($expected, $result);
  8010. $result = $this->Form->getSourceValue('id');
  8011. $this->assertEquals('4', $result);
  8012. }
  8013. /**
  8014. * Test the different form input renderings based on values sources switchings through form options
  8015. *
  8016. * @return void
  8017. */
  8018. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  8019. {
  8020. $this->loadFixtures('Articles');
  8021. $articles = TableRegistry::get('Articles');
  8022. $article = new Article();
  8023. $articles->patchEntity($article, ['id' => '3']);
  8024. $this->Form->request->data['id'] = '10';
  8025. $this->Form->request->query['id'] = '11';
  8026. $this->Form->setValueSources(['context'])
  8027. ->create($article, ['valueSources' => ['query', 'data']]);
  8028. $result = $this->Form->control('id');
  8029. $expected = [
  8030. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  8031. ];
  8032. $this->assertHtml($expected, $result);
  8033. $result = $this->Form->getSourceValue('id');
  8034. $this->assertEquals('11', $result);
  8035. unset($this->Form->request->query['id']);
  8036. $this->Form->setValueSources(['context'])
  8037. ->create($article, ['valueSources' => ['query', 'data']]);
  8038. $result = $this->Form->control('id');
  8039. $expected = [
  8040. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  8041. ];
  8042. $this->assertHtml($expected, $result);
  8043. $result = $this->Form->getSourceValue('id');
  8044. $this->assertEquals('10', $result);
  8045. }
  8046. /**
  8047. * Test the different form values sources resetting through From::end();
  8048. *
  8049. * @return void
  8050. */
  8051. public function testFormValueSourcesResetViaEnd()
  8052. {
  8053. $expected = ['context'];
  8054. $result = $this->Form->getValueSources();
  8055. $this->assertEquals($expected, $result);
  8056. $expected = ['query', 'context', 'data'];
  8057. $this->Form->setValueSources(['query', 'context', 'data']);
  8058. $result = $this->Form->getValueSources();
  8059. $this->assertEquals($expected, $result);
  8060. $this->Form->create();
  8061. $result = $this->Form->getValueSources();
  8062. $this->assertEquals($expected, $result);
  8063. $this->Form->end();
  8064. $result = $this->Form->getValueSources();
  8065. $this->assertEquals(['context'], $result);
  8066. }
  8067. /**
  8068. * Test sources values defaults handling
  8069. *
  8070. * @return void
  8071. */
  8072. public function testFormValueSourcesDefaults()
  8073. {
  8074. $this->Form->request->query['password'] = 'open Sesame';
  8075. $this->Form->create();
  8076. $result = $this->Form->password('password');
  8077. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  8078. $this->assertHtml($expected, $result);
  8079. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8080. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8081. $this->assertHtml($expected, $result);
  8082. $this->Form->setValueSources('query');
  8083. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8084. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  8085. $this->assertHtml($expected, $result);
  8086. $this->Form->setValueSources('data');
  8087. $result = $this->Form->password('password', ['default' => 'helloworld']);
  8088. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  8089. $this->assertHtml($expected, $result);
  8090. }
  8091. /**
  8092. * Test sources values schema defaults handling
  8093. *
  8094. * @return void
  8095. */
  8096. public function testSourcesValueDoesntExistPassThrough()
  8097. {
  8098. $this->Form->request->query['category'] = 'sesame-cookies';
  8099. $articles = TableRegistry::get('Articles');
  8100. $entity = $articles->newEntity();
  8101. $this->Form->create($entity);
  8102. $this->Form->setValueSources(['query', 'context']);
  8103. $result = $this->Form->getSourceValue('category');
  8104. $this->assertEquals('sesame-cookies', $result);
  8105. $this->Form->setValueSources(['context', 'query']);
  8106. $result = $this->Form->getSourceValue('category');
  8107. $this->assertEquals('sesame-cookies', $result);
  8108. }
  8109. /**
  8110. * testNestedLabelInput method
  8111. *
  8112. * Test the `nestedInput` parameter
  8113. *
  8114. * @return void
  8115. */
  8116. public function testNestedLabelInput()
  8117. {
  8118. $result = $this->Form->input('foo', ['nestedInput' => true]);
  8119. $expected = [
  8120. 'div' => ['class' => 'input text'],
  8121. 'label' => ['for' => 'foo'],
  8122. ['input' => [
  8123. 'type' => 'text',
  8124. 'name' => 'foo',
  8125. 'id' => 'foo'
  8126. ]],
  8127. 'Foo',
  8128. '/label',
  8129. '/div'
  8130. ];
  8131. $this->assertHtml($expected, $result);
  8132. }
  8133. /**
  8134. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget and RadioWidget
  8135. *
  8136. * This test makes sure `false` excludes the label from the render
  8137. *
  8138. * @return void
  8139. */
  8140. public function testInputLabelManipulationDisableLabels()
  8141. {
  8142. $result = $this->Form->input('test', [
  8143. 'type' => 'radio',
  8144. 'options' => ['A', 'B'],
  8145. 'labelOptions' => false
  8146. ]);
  8147. $expected = [
  8148. ['div' => ['class' => 'input radio']],
  8149. '<label',
  8150. 'Test',
  8151. '/label',
  8152. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8153. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8154. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8155. '/div',
  8156. ];
  8157. $this->assertHtml($expected, $result);
  8158. $result = $this->Form->input('checkbox1', [
  8159. 'label' => 'My checkboxes',
  8160. 'multiple' => 'checkbox',
  8161. 'type' => 'select',
  8162. 'options' => [
  8163. ['text' => 'First Checkbox', 'value' => 1],
  8164. ['text' => 'Second Checkbox', 'value' => 2]
  8165. ],
  8166. 'labelOptions' => false
  8167. ]);
  8168. $expected = [
  8169. ['div' => ['class' => 'input select']],
  8170. ['label' => ['for' => 'checkbox1']],
  8171. 'My checkboxes',
  8172. '/label',
  8173. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8174. ['div' => ['class' => 'checkbox']],
  8175. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '1', 'id' => 'checkbox1-1']],
  8176. '/div',
  8177. ['div' => ['class' => 'checkbox']],
  8178. ['input' => ['type' => 'checkbox', 'name' => 'checkbox1[]', 'value' => '2', 'id' => 'checkbox1-2']],
  8179. '/div',
  8180. '/div'
  8181. ];
  8182. $this->assertHtml($expected, $result);
  8183. }
  8184. /**
  8185. * Tests to make sure `labelOptions` is rendered correctly by RadioWidget
  8186. *
  8187. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8188. * added to the class if checked.
  8189. *
  8190. * Also checks to make sure any custom attributes are rendered correctly
  8191. *
  8192. * @return void
  8193. */
  8194. public function testInputLabelManipulationRadios()
  8195. {
  8196. $result = $this->Form->input('test', [
  8197. 'type' => 'radio',
  8198. 'options' => ['A', 'B'],
  8199. 'labelOptions' => ['class' => 'custom-class']
  8200. ]);
  8201. $expected = [
  8202. ['div' => ['class' => 'input radio']],
  8203. '<label',
  8204. 'Test',
  8205. '/label',
  8206. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8207. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8208. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8209. 'A',
  8210. '/label',
  8211. ['label' => ['for' => 'test-1', 'class' => 'custom-class']],
  8212. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  8213. 'B',
  8214. '/label',
  8215. '/div',
  8216. ];
  8217. $this->assertHtml($expected, $result);
  8218. $result = $this->Form->input('test', [
  8219. 'type' => 'radio',
  8220. 'options' => ['A', 'B'],
  8221. 'value' => 1,
  8222. 'labelOptions' => ['class' => 'custom-class']
  8223. ]);
  8224. $expected = [
  8225. ['div' => ['class' => 'input radio']],
  8226. '<label',
  8227. 'Test',
  8228. '/label',
  8229. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8230. ['label' => ['for' => 'test-0', 'class' => 'custom-class']],
  8231. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8232. 'A',
  8233. '/label',
  8234. ['label' => ['for' => 'test-1', 'class' => 'custom-class selected']],
  8235. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8236. 'B',
  8237. '/label',
  8238. '/div',
  8239. ];
  8240. $this->assertHtml($expected, $result);
  8241. $result = $this->Form->input('test', [
  8242. 'type' => 'radio',
  8243. 'options' => ['A', 'B'],
  8244. 'value' => 1,
  8245. 'labelOptions' => ['class' => ['custom-class', 'custom-class-array']]
  8246. ]);
  8247. $expected = [
  8248. ['div' => ['class' => 'input radio']],
  8249. '<label',
  8250. 'Test',
  8251. '/label',
  8252. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  8253. ['label' => ['for' => 'test-0', 'class' => 'custom-class custom-class-array']],
  8254. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8255. 'A',
  8256. '/label',
  8257. ['label' => ['for' => 'test-1', 'class' => 'custom-class custom-class-array selected']],
  8258. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1', 'checked' => 'checked']],
  8259. 'B',
  8260. '/label',
  8261. '/div',
  8262. ];
  8263. $this->assertHtml($expected, $result);
  8264. $result = $this->Form->radio('test', ['A', 'B'], [
  8265. 'label' => [
  8266. 'class' => ['custom-class', 'another-class'],
  8267. 'data-name' => 'bob'
  8268. ],
  8269. 'value' => 1
  8270. ]);
  8271. $expected = [
  8272. 'input' => ['type' => 'hidden', 'name' => 'test', 'value' => ''],
  8273. ['label' => ['class' => 'custom-class another-class', 'data-name' => 'bob', 'for' => 'test-0']],
  8274. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  8275. 'A',
  8276. '/label',
  8277. ['label' => ['class' => 'custom-class another-class selected', 'data-name' => 'bob', 'for' => 'test-1']],
  8278. ['input' => [
  8279. 'type' => 'radio',
  8280. 'name' => 'test',
  8281. 'value' => '1',
  8282. 'id' => 'test-1',
  8283. 'checked' => 'checked'
  8284. ]],
  8285. 'B',
  8286. '/label',
  8287. ];
  8288. $this->assertHtml($expected, $result);
  8289. }
  8290. /**
  8291. * Tests to make sure `labelOptions` is rendered correctly by MultiCheckboxWidget
  8292. *
  8293. * This test checks rendering of class (as string and array) also makes sure 'selected' is
  8294. * added to the class if checked.
  8295. *
  8296. * Also checks to make sure any custom attributes are rendered correctly
  8297. *
  8298. * @return void
  8299. */
  8300. public function testInputLabelManipulationCheckboxes()
  8301. {
  8302. $result = $this->Form->input('checkbox1', [
  8303. 'label' => 'My checkboxes',
  8304. 'multiple' => 'checkbox',
  8305. 'type' => 'select',
  8306. 'options' => [
  8307. ['text' => 'First Checkbox', 'value' => 1],
  8308. ['text' => 'Second Checkbox', 'value' => 2]
  8309. ],
  8310. 'labelOptions' => ['class' => 'custom-class'],
  8311. 'value' => ['1']
  8312. ]);
  8313. $expected = [
  8314. ['div' => ['class' => 'input select']],
  8315. ['label' => ['for' => 'checkbox1']],
  8316. 'My checkboxes',
  8317. '/label',
  8318. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8319. ['div' => ['class' => 'checkbox']],
  8320. ['label' => [
  8321. 'class' => 'custom-class selected',
  8322. 'for' => 'checkbox1-1'
  8323. ]],
  8324. ['input' => [
  8325. 'type' => 'checkbox',
  8326. 'name' => 'checkbox1[]',
  8327. 'value' => '1',
  8328. 'id' => 'checkbox1-1',
  8329. 'checked' => 'checked'
  8330. ]],
  8331. 'First Checkbox',
  8332. '/label',
  8333. '/div',
  8334. ['div' => ['class' => 'checkbox']],
  8335. ['label' => [
  8336. 'class' => 'custom-class',
  8337. 'for' => 'checkbox1-2'
  8338. ]],
  8339. ['input' => [
  8340. 'type' => 'checkbox',
  8341. 'name' => 'checkbox1[]',
  8342. 'value' => '2',
  8343. 'id' => 'checkbox1-2'
  8344. ]],
  8345. 'Second Checkbox',
  8346. '/label',
  8347. '/div',
  8348. '/div'
  8349. ];
  8350. $this->assertHtml($expected, $result);
  8351. $result = $this->Form->input('checkbox1', [
  8352. 'label' => 'My checkboxes',
  8353. 'multiple' => 'checkbox',
  8354. 'type' => 'select',
  8355. 'options' => [
  8356. ['text' => 'First Checkbox', 'value' => 1],
  8357. ['text' => 'Second Checkbox', 'value' => 2]
  8358. ],
  8359. 'labelOptions' => ['class' => ['custom-class', 'another-class'], 'data-name' => 'bob'],
  8360. 'value' => ['1']
  8361. ]);
  8362. $expected = [
  8363. ['div' => ['class' => 'input select']],
  8364. ['label' => ['for' => 'checkbox1']],
  8365. 'My checkboxes',
  8366. '/label',
  8367. 'input' => ['type' => 'hidden', 'name' => 'checkbox1', 'value' => ''],
  8368. ['div' => ['class' => 'checkbox']],
  8369. ['label' => [
  8370. 'class' => 'custom-class another-class selected',
  8371. 'data-name' => 'bob',
  8372. 'for' => 'checkbox1-1'
  8373. ]],
  8374. ['input' => [
  8375. 'type' => 'checkbox',
  8376. 'name' => 'checkbox1[]',
  8377. 'value' => '1',
  8378. 'id' => 'checkbox1-1',
  8379. 'checked' => 'checked'
  8380. ]],
  8381. 'First Checkbox',
  8382. '/label',
  8383. '/div',
  8384. ['div' => ['class' => 'checkbox']],
  8385. ['label' => [
  8386. 'class' => 'custom-class another-class',
  8387. 'data-name' => 'bob',
  8388. 'for' => 'checkbox1-2'
  8389. ]],
  8390. ['input' => [
  8391. 'type' => 'checkbox',
  8392. 'name' => 'checkbox1[]',
  8393. 'value' => '2',
  8394. 'id' => 'checkbox1-2'
  8395. ]],
  8396. 'Second Checkbox',
  8397. '/label',
  8398. '/div',
  8399. '/div'
  8400. ];
  8401. $this->assertHtml($expected, $result);
  8402. }
  8403. }