FormHelperTest.php 279 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487
  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 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. Configure::delete('Asset');
  130. $this->View = new View();
  131. $this->Form = new FormHelper($this->View);
  132. $request = new Request('articles/add');
  133. $request->here = '/articles/add';
  134. $request['controller'] = 'articles';
  135. $request['action'] = 'add';
  136. $request->webroot = '';
  137. $request->base = '';
  138. $this->Form->Url->request = $this->Form->request = $request;
  139. $this->dateRegex = [
  140. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  141. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  142. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  143. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  144. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  145. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  146. ];
  147. $this->article = [
  148. 'schema' => [
  149. 'id' => ['type' => 'integer'],
  150. 'author_id' => ['type' => 'integer', 'null' => true],
  151. 'title' => ['type' => 'string', 'null' => true],
  152. 'body' => 'text',
  153. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  154. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  155. ],
  156. 'required' => [
  157. 'author_id' => true,
  158. 'title' => true,
  159. ]
  160. ];
  161. Security::salt('foo!');
  162. Router::connect('/:controller', ['action' => 'index']);
  163. Router::connect('/:controller/:action/*');
  164. }
  165. /**
  166. * tearDown method
  167. *
  168. * @return void
  169. */
  170. public function tearDown()
  171. {
  172. parent::tearDown();
  173. unset($this->Form, $this->Controller, $this->View);
  174. TableRegistry::clear();
  175. }
  176. /**
  177. * Test construct() with the templates option.
  178. *
  179. * @return void
  180. */
  181. public function testConstructTemplatesFile()
  182. {
  183. $helper = new FormHelper($this->View, [
  184. 'templates' => 'htmlhelper_tags'
  185. ]);
  186. $result = $helper->input('name');
  187. $this->assertContains('<input', $result);
  188. }
  189. /**
  190. * Test that when specifying custom widgets the config array for that widget
  191. * is overwritten instead of merged.
  192. *
  193. * @return void
  194. */
  195. public function testConstructWithWidgets()
  196. {
  197. $config = [
  198. 'widgets' => [
  199. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  200. ]
  201. ];
  202. $helper = new FormHelper($this->View, $config);
  203. $registry = $helper->widgetRegistry();
  204. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  205. }
  206. /**
  207. * Test that when specifying custom widgets config file and it should be
  208. * added to widgets array. WidgetRegistry will load widgets in constructor.
  209. *
  210. * @return void
  211. */
  212. public function testConstructWithWidgetsConfig()
  213. {
  214. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  215. $registry = $helper->widgetRegistry();
  216. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  217. }
  218. /**
  219. * Test registering a new widget class and rendering it.
  220. *
  221. * @return void
  222. */
  223. public function testAddWidgetAndRenderWidget()
  224. {
  225. $data = [
  226. 'val' => 1
  227. ];
  228. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  229. $this->assertNull($this->Form->addWidget('test', $mock));
  230. $mock->expects($this->once())
  231. ->method('render')
  232. ->with($data)
  233. ->will($this->returnValue('HTML'));
  234. $result = $this->Form->widget('test', $data);
  235. $this->assertEquals('HTML', $result);
  236. }
  237. /**
  238. * Test that secureFields() of widget is called after calling render(),
  239. * not before.
  240. *
  241. * @return void
  242. */
  243. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  244. {
  245. $data = [
  246. 'val' => 1,
  247. 'name' => 'test'
  248. ];
  249. $mock = $this->getMockBuilder('Cake\View\Widget\WidgetInterface')->getMock();
  250. $this->assertNull($this->Form->addWidget('test', $mock));
  251. $mock->expects($this->at(0))
  252. ->method('render')
  253. ->with($data)
  254. ->will($this->returnValue('HTML'));
  255. $mock->expects($this->at(1))
  256. ->method('secureFields')
  257. ->with($data)
  258. ->will($this->returnValue(['test']));
  259. $result = $this->Form->widget('test', $data + ['secure' => true]);
  260. $this->assertEquals('HTML', $result);
  261. }
  262. /**
  263. * Test that empty string is not added to secure fields list when
  264. * rendering input widget without name.
  265. *
  266. * @return void
  267. */
  268. public function testRenderingWidgetWithEmptyName()
  269. {
  270. $this->assertEquals([], $this->Form->fields);
  271. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  272. $this->assertEquals('<select name=""></select>', $result);
  273. $this->assertEquals([], $this->Form->fields);
  274. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  275. $this->assertEquals('<select name="0"></select>', $result);
  276. $this->assertEquals(['0'], $this->Form->fields);
  277. }
  278. /**
  279. * Test registering an invalid widget class.
  280. *
  281. * @expectedException \RuntimeException
  282. * @return void
  283. */
  284. public function testAddWidgetInvalid()
  285. {
  286. $mock = new \StdClass();
  287. $this->Form->addWidget('test', $mock);
  288. $this->Form->widget('test');
  289. }
  290. /**
  291. * Test adding a new context class.
  292. *
  293. * @return void
  294. */
  295. public function testAddContextProvider()
  296. {
  297. $context = 'My data';
  298. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  299. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  300. $this->assertInstanceOf('Cake\Network\Request', $request);
  301. $this->assertEquals($context, $data['entity']);
  302. return $stub;
  303. });
  304. $this->Form->create($context);
  305. $result = $this->Form->context();
  306. $this->assertSame($stub, $result);
  307. }
  308. /**
  309. * Test replacing a context class.
  310. *
  311. * @return void
  312. */
  313. public function testAddContextProviderReplace()
  314. {
  315. $entity = new Article();
  316. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  317. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  318. return $stub;
  319. });
  320. $this->Form->create($entity);
  321. $result = $this->Form->context();
  322. $this->assertSame($stub, $result);
  323. }
  324. /**
  325. * Test overriding a context class.
  326. *
  327. * @return void
  328. */
  329. public function testAddContextProviderAdd()
  330. {
  331. $entity = new Article();
  332. $stub = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  333. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  334. if ($data['entity'] instanceof Entity) {
  335. return $stub;
  336. }
  337. });
  338. $this->Form->create($entity);
  339. $result = $this->Form->context();
  340. $this->assertSame($stub, $result);
  341. }
  342. /**
  343. * Test adding an invalid context class.
  344. *
  345. * @expectedException \RuntimeException
  346. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  347. * @return void
  348. */
  349. public function testAddContextProviderInvalid()
  350. {
  351. $context = 'My data';
  352. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  353. return new \StdClass();
  354. });
  355. $this->Form->create($context);
  356. }
  357. /**
  358. * Provides context options for create().
  359. *
  360. * @return array
  361. */
  362. public function contextSelectionProvider()
  363. {
  364. $entity = new Article();
  365. $collection = $this->getMockBuilder('Cake\Collection\Collection')
  366. ->setMethods(['extract'])
  367. ->setConstructorArgs([[$entity]])
  368. ->getMock();
  369. $emptyCollection = new Collection([]);
  370. $emptyArray = [];
  371. $arrayObject = new \ArrayObject([]);
  372. $data = [
  373. 'schema' => [
  374. 'title' => ['type' => 'string']
  375. ]
  376. ];
  377. $form = new Form();
  378. return [
  379. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  380. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  381. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  382. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  383. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  384. 'form' => [$form, 'Cake\View\Form\FormContext'],
  385. 'none' => [null, 'Cake\View\Form\NullContext'],
  386. 'false' => [false, 'Cake\View\Form\NullContext'],
  387. ];
  388. }
  389. /**
  390. * Test default context selection in create()
  391. *
  392. * @dataProvider contextSelectionProvider
  393. * @return void
  394. */
  395. public function testCreateContextSelectionBuiltIn($data, $class)
  396. {
  397. $this->loadFixtures('Articles');
  398. $this->Form->create($data);
  399. $this->assertInstanceOf($class, $this->Form->context());
  400. }
  401. /**
  402. * Data provider for type option.
  403. *
  404. * @return array
  405. */
  406. public static function requestTypeProvider()
  407. {
  408. return [
  409. // type, method, override
  410. ['post', 'post', 'POST'],
  411. ['put', 'post', 'PUT'],
  412. ['patch', 'post', 'PATCH'],
  413. ['delete', 'post', 'DELETE'],
  414. ];
  415. }
  416. /**
  417. * Test creating file forms.
  418. *
  419. * @return void
  420. */
  421. public function testCreateFile()
  422. {
  423. $encoding = strtolower(Configure::read('App.encoding'));
  424. $result = $this->Form->create(false, ['type' => 'file']);
  425. $expected = [
  426. 'form' => [
  427. 'method' => 'post', 'action' => '/articles/add',
  428. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  429. ],
  430. 'div' => ['style' => 'display:none;'],
  431. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  432. '/div'
  433. ];
  434. $this->assertHtml($expected, $result);
  435. }
  436. /**
  437. * Test creating GET forms.
  438. *
  439. * @return void
  440. */
  441. public function testCreateGet()
  442. {
  443. $encoding = strtolower(Configure::read('App.encoding'));
  444. $result = $this->Form->create(false, ['type' => 'get']);
  445. $expected = ['form' => [
  446. 'method' => 'get', 'action' => '/articles/add',
  447. 'accept-charset' => $encoding
  448. ]];
  449. $this->assertHtml($expected, $result);
  450. }
  451. /**
  452. * Test create() with the templates option.
  453. *
  454. * @return void
  455. */
  456. public function testCreateTemplatesArray()
  457. {
  458. $result = $this->Form->create($this->article, [
  459. 'templates' => [
  460. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  461. ]
  462. ]);
  463. $expected = [
  464. 'form' => [
  465. 'class' => 'form-horizontal',
  466. 'method' => 'post',
  467. 'action' => '/articles/add',
  468. 'accept-charset' => 'utf-8'
  469. ]
  470. ];
  471. $this->assertHtml($expected, $result);
  472. }
  473. /**
  474. * Test create() with the templates option.
  475. *
  476. * @return void
  477. */
  478. public function testCreateTemplatesFile()
  479. {
  480. $result = $this->Form->create($this->article, [
  481. 'templates' => 'htmlhelper_tags',
  482. ]);
  483. $expected = [
  484. 'start form',
  485. 'div' => ['class' => 'hidden'],
  486. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  487. '/div'
  488. ];
  489. $this->assertHtml($expected, $result);
  490. }
  491. /**
  492. * Test that create() and end() restore templates.
  493. *
  494. * @return void
  495. */
  496. public function testCreateEndRestoreTemplates()
  497. {
  498. $this->Form->create($this->article, [
  499. 'templates' => ['input' => 'custom input element']
  500. ]);
  501. $this->Form->end();
  502. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  503. }
  504. /**
  505. * Test using template vars in various templates used by input() method.
  506. *
  507. * @return void
  508. */
  509. public function testInputTemplateVars()
  510. {
  511. $result = $this->Form->input('text', [
  512. 'templates' => [
  513. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  514. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  515. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  516. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  517. ],
  518. 'templateVars' => [
  519. 'forinput' => 'in-input',
  520. 'forlabel' => 'in-label',
  521. 'forgroup' => 'in-group',
  522. 'forcontainer' => 'in-container'
  523. ]
  524. ]);
  525. $expected = [
  526. 'div' => ['class'],
  527. 'label' => ['for'],
  528. 'Text in-label',
  529. '/label',
  530. 'in-group',
  531. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  532. 'in-container',
  533. '/div',
  534. ];
  535. $this->assertHtml($expected, $result);
  536. }
  537. /**
  538. * Test ensuring template variables work in template files loaded
  539. * during input().
  540. *
  541. * @return void
  542. */
  543. public function testInputTemplatesFromFile()
  544. {
  545. $result = $this->Form->input('title', [
  546. 'templates' => 'test_templates',
  547. 'templateVars' => [
  548. 'forcontainer' => 'container-data'
  549. ]
  550. ]);
  551. $expected = [
  552. 'div' => ['class'],
  553. 'label' => ['for'],
  554. 'Title',
  555. '/label',
  556. 'input' => ['name', 'type' => 'text', 'id'],
  557. 'container-data',
  558. '/div',
  559. ];
  560. $this->assertHtml($expected, $result);
  561. }
  562. /**
  563. * Test using template vars in inputSubmit and submitContainer template.
  564. *
  565. * @return void
  566. */
  567. public function testSubmitTemplateVars()
  568. {
  569. $this->Form->templates([
  570. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  571. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  572. ]);
  573. $result = $this->Form->submit('Submit', [
  574. 'templateVars' => [
  575. 'forinput' => 'in-input',
  576. 'forcontainer' => 'in-container'
  577. ]
  578. ]);
  579. $expected = [
  580. 'div' => ['class'],
  581. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  582. 'in-container',
  583. '/div',
  584. ];
  585. $this->assertHtml($expected, $result);
  586. }
  587. /**
  588. * test the create() method
  589. *
  590. * @dataProvider requestTypeProvider
  591. * @return void
  592. */
  593. public function testCreateTypeOptions($type, $method, $override)
  594. {
  595. $encoding = strtolower(Configure::read('App.encoding'));
  596. $result = $this->Form->create(false, ['type' => $type]);
  597. $expected = [
  598. 'form' => [
  599. 'method' => $method, 'action' => '/articles/add',
  600. 'accept-charset' => $encoding
  601. ],
  602. 'div' => ['style' => 'display:none;'],
  603. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  604. '/div'
  605. ];
  606. $this->assertHtml($expected, $result);
  607. }
  608. /**
  609. * Test opening a form for an update operation.
  610. *
  611. * @return void
  612. */
  613. public function testCreateUpdateForm()
  614. {
  615. $encoding = strtolower(Configure::read('App.encoding'));
  616. $this->Form->request->here = '/articles/edit/1';
  617. $this->Form->request['action'] = 'edit';
  618. $this->article['defaults']['id'] = 1;
  619. $result = $this->Form->create($this->article);
  620. $expected = [
  621. 'form' => [
  622. 'method' => 'post', 'action' => '/articles/edit/1',
  623. 'accept-charset' => $encoding
  624. ],
  625. 'div' => ['style' => 'display:none;'],
  626. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  627. '/div'
  628. ];
  629. $this->assertHtml($expected, $result);
  630. }
  631. /**
  632. * test create() with automatic url generation
  633. *
  634. * @return void
  635. */
  636. public function testCreateAutoUrl()
  637. {
  638. $encoding = strtolower(Configure::read('App.encoding'));
  639. $this->Form->request['action'] = 'delete';
  640. $this->Form->request->here = '/articles/delete/10';
  641. $this->Form->request->base = '';
  642. $result = $this->Form->create($this->article);
  643. $expected = [
  644. 'form' => [
  645. 'method' => 'post', 'action' => '/articles/delete/10',
  646. 'accept-charset' => $encoding
  647. ],
  648. 'div' => ['style' => 'display:none;'],
  649. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  650. '/div'
  651. ];
  652. $this->assertHtml($expected, $result);
  653. $this->article['defaults'] = ['id' => 1];
  654. $this->Form->request->here = '/articles/edit/1';
  655. $this->Form->request['action'] = 'delete';
  656. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  657. $expected = [
  658. 'form' => [
  659. 'method' => 'post',
  660. 'action' => '/articles/edit/1',
  661. 'accept-charset' => $encoding
  662. ],
  663. 'div' => ['style' => 'display:none;'],
  664. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  665. '/div'
  666. ];
  667. $this->assertHtml($expected, $result);
  668. $this->Form->request['action'] = 'add';
  669. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  670. $expected = [
  671. 'form' => [
  672. 'method' => 'post',
  673. 'action' => '/articles/publish/1',
  674. 'accept-charset' => $encoding
  675. ],
  676. 'div' => ['style' => 'display:none;'],
  677. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  678. '/div'
  679. ];
  680. $this->assertHtml($expected, $result);
  681. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  682. $expected = [
  683. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  684. 'div' => ['style' => 'display:none;'],
  685. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  686. '/div'
  687. ];
  688. $this->assertHtml($expected, $result);
  689. $this->Form->request['controller'] = 'Pages';
  690. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  691. $expected = [
  692. 'form' => [
  693. 'method' => 'post', 'action' => '/Pages/signup/1',
  694. 'accept-charset' => $encoding
  695. ],
  696. 'div' => ['style' => 'display:none;'],
  697. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  698. '/div'
  699. ];
  700. $this->assertHtml($expected, $result);
  701. }
  702. /**
  703. * Test create() with no URL (no "action" attribute for <form> tag)
  704. *
  705. * @return void
  706. */
  707. public function testCreateNoUrl()
  708. {
  709. $result = $this->Form->create(false, ['url' => false]);
  710. $expected = [
  711. 'form' => [
  712. 'method' => 'post',
  713. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  714. ],
  715. 'div' => ['style' => 'display:none;'],
  716. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  717. '/div'
  718. ];
  719. $this->assertHtml($expected, $result);
  720. }
  721. /**
  722. * test create() with a custom route
  723. *
  724. * @return void
  725. */
  726. public function testCreateCustomRoute()
  727. {
  728. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  729. $encoding = strtolower(Configure::read('App.encoding'));
  730. $this->Form->request['controller'] = 'users';
  731. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  732. $expected = [
  733. 'form' => [
  734. 'method' => 'post', 'action' => '/login',
  735. 'accept-charset' => $encoding
  736. ],
  737. 'div' => ['style' => 'display:none;'],
  738. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  739. '/div'
  740. ];
  741. $this->assertHtml($expected, $result);
  742. Router::connect(
  743. '/new-article',
  744. ['controller' => 'articles', 'action' => 'myaction'],
  745. ['_name' => 'my-route']
  746. );
  747. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  748. $expected = [
  749. 'form' => [
  750. 'method' => 'post', 'action' => '/new-article',
  751. 'accept-charset' => $encoding,
  752. ],
  753. 'div' => ['style' => 'display:none;'],
  754. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  755. '/div'
  756. ];
  757. $this->assertHtml($expected, $result);
  758. }
  759. /**
  760. * test automatic accept-charset overriding
  761. *
  762. * @return void
  763. */
  764. public function testCreateWithAcceptCharset()
  765. {
  766. $result = $this->Form->create(
  767. $this->article,
  768. [
  769. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  770. ]
  771. );
  772. $expected = [
  773. 'form' => [
  774. 'method' => 'post', 'action' => '/articles',
  775. 'accept-charset' => 'iso-8859-1'
  776. ],
  777. 'div' => ['style' => 'display:none;'],
  778. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  779. '/div'
  780. ];
  781. $this->assertHtml($expected, $result);
  782. }
  783. /**
  784. * Test base form URL when url param is passed with multiple parameters (&)
  785. *
  786. */
  787. public function testCreateQuerystringrequest()
  788. {
  789. $encoding = strtolower(Configure::read('App.encoding'));
  790. $result = $this->Form->create($this->article, [
  791. 'type' => 'post',
  792. 'escape' => false,
  793. 'url' => [
  794. 'controller' => 'controller',
  795. 'action' => 'action',
  796. '?' => ['param1' => 'value1', 'param2' => 'value2']
  797. ]
  798. ]);
  799. $expected = [
  800. 'form' => [
  801. 'method' => 'post',
  802. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  803. 'accept-charset' => $encoding
  804. ],
  805. 'div' => ['style' => 'display:none;'],
  806. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  807. '/div'
  808. ];
  809. $this->assertHtml($expected, $result);
  810. $result = $this->Form->create($this->article, [
  811. 'type' => 'post',
  812. 'url' => [
  813. 'controller' => 'controller',
  814. 'action' => 'action',
  815. '?' => ['param1' => 'value1', 'param2' => 'value2']
  816. ]
  817. ]);
  818. $this->assertHtml($expected, $result);
  819. }
  820. /**
  821. * test that create() doesn't cause errors by multiple id's being in the primary key
  822. * as could happen with multiple select or checkboxes.
  823. *
  824. * @return void
  825. */
  826. public function testCreateWithMultipleIdInData()
  827. {
  828. $encoding = strtolower(Configure::read('App.encoding'));
  829. $this->Form->request->data['Article']['id'] = [1, 2];
  830. $result = $this->Form->create($this->article);
  831. $expected = [
  832. 'form' => [
  833. 'method' => 'post',
  834. 'action' => '/articles/add',
  835. 'accept-charset' => $encoding
  836. ],
  837. 'div' => ['style' => 'display:none;'],
  838. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  839. '/div'
  840. ];
  841. $this->assertHtml($expected, $result);
  842. }
  843. /**
  844. * test that create() doesn't add in extra passed params.
  845. *
  846. * @return void
  847. */
  848. public function testCreatePassedArgs()
  849. {
  850. $encoding = strtolower(Configure::read('App.encoding'));
  851. $this->Form->request->data['Article']['id'] = 1;
  852. $result = $this->Form->create($this->article, [
  853. 'type' => 'post',
  854. 'escape' => false,
  855. 'url' => [
  856. 'action' => 'edit',
  857. 'myparam'
  858. ]
  859. ]);
  860. $expected = [
  861. 'form' => [
  862. 'method' => 'post',
  863. 'action' => '/articles/edit/myparam',
  864. 'accept-charset' => $encoding
  865. ],
  866. 'div' => ['style' => 'display:none;'],
  867. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  868. '/div'
  869. ];
  870. $this->assertHtml($expected, $result);
  871. }
  872. /**
  873. * test creating a get form, and get form inputs.
  874. *
  875. * @return void
  876. */
  877. public function testGetFormCreate()
  878. {
  879. $encoding = strtolower(Configure::read('App.encoding'));
  880. $result = $this->Form->create($this->article, ['type' => 'get']);
  881. $expected = ['form' => [
  882. 'method' => 'get', 'action' => '/articles/add',
  883. 'accept-charset' => $encoding
  884. ]];
  885. $this->assertHtml($expected, $result);
  886. $result = $this->Form->text('title');
  887. $expected = ['input' => [
  888. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  889. ]];
  890. $this->assertHtml($expected, $result);
  891. $result = $this->Form->password('password');
  892. $expected = ['input' => [
  893. 'name' => 'password', 'type' => 'password'
  894. ]];
  895. $this->assertHtml($expected, $result);
  896. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  897. $result = $this->Form->text('user_form');
  898. $expected = ['input' => [
  899. 'name' => 'user_form', 'type' => 'text'
  900. ]];
  901. $this->assertHtml($expected, $result);
  902. }
  903. /**
  904. * test get form, and inputs when the model param is false
  905. *
  906. * @return void
  907. */
  908. public function testGetFormWithFalseModel()
  909. {
  910. $encoding = strtolower(Configure::read('App.encoding'));
  911. $this->Form->request['controller'] = 'contact_test';
  912. $result = $this->Form->create(false, [
  913. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  914. ]);
  915. $expected = ['form' => [
  916. 'method' => 'get', 'action' => '/contact_test/add',
  917. 'accept-charset' => $encoding
  918. ]];
  919. $this->assertHtml($expected, $result);
  920. $result = $this->Form->text('reason');
  921. $expected = [
  922. 'input' => ['type' => 'text', 'name' => 'reason']
  923. ];
  924. $this->assertHtml($expected, $result);
  925. }
  926. /**
  927. * testFormCreateWithSecurity method
  928. *
  929. * Test form->create() with security key.
  930. *
  931. * @return void
  932. */
  933. public function testCreateWithSecurity()
  934. {
  935. $this->Form->request->params['_csrfToken'] = 'testKey';
  936. $encoding = strtolower(Configure::read('App.encoding'));
  937. $result = $this->Form->create($this->article, [
  938. 'url' => '/articles/publish',
  939. ]);
  940. $expected = [
  941. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  942. 'div' => ['style' => 'display:none;'],
  943. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  944. ['input' => [
  945. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  946. ]],
  947. '/div'
  948. ];
  949. $this->assertHtml($expected, $result);
  950. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  951. $expected['form']['id'] = 'MyForm';
  952. $this->assertHtml($expected, $result);
  953. }
  954. /**
  955. * testFormCreateGetNoSecurity method
  956. *
  957. * Test form->create() with no security key as its a get form
  958. *
  959. * @return void
  960. */
  961. public function testCreateEndGetNoSecurity()
  962. {
  963. $this->Form->request->params['_csrfToken'] = 'testKey';
  964. $encoding = strtolower(Configure::read('App.encoding'));
  965. $article = new Article();
  966. $result = $this->Form->create($article, [
  967. 'type' => 'get',
  968. 'url' => '/contacts/add'
  969. ]);
  970. $this->assertNotContains('testKey', $result);
  971. $result = $this->Form->end();
  972. $this->assertNotContains('testKey', $result);
  973. }
  974. /**
  975. * test that create() clears the fields property so it starts fresh
  976. *
  977. * @return void
  978. */
  979. public function testCreateClearingFields()
  980. {
  981. $this->Form->fields = ['model_id'];
  982. $this->Form->create($this->article);
  983. $this->assertEquals([], $this->Form->fields);
  984. }
  985. /**
  986. * Tests form hash generation with model-less data
  987. *
  988. * @return void
  989. */
  990. public function testValidateHashNoModel()
  991. {
  992. $this->Form->request->params['_Token'] = 'foo';
  993. $result = $this->Form->secure(['anything']);
  994. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  995. }
  996. /**
  997. * Tests that hidden fields generated for checkboxes don't get locked
  998. *
  999. * @return void
  1000. */
  1001. public function testNoCheckboxLocking()
  1002. {
  1003. $this->Form->request->params['_Token'] = 'foo';
  1004. $this->assertSame([], $this->Form->fields);
  1005. $this->Form->checkbox('check', ['value' => '1']);
  1006. $this->assertSame($this->Form->fields, ['check']);
  1007. }
  1008. /**
  1009. * testFormSecurityFields method
  1010. *
  1011. * Test generation of secure form hash generation.
  1012. *
  1013. * @return void
  1014. */
  1015. public function testFormSecurityFields()
  1016. {
  1017. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1018. $this->Form->request->params['_Token'] = 'testKey';
  1019. $result = $this->Form->secure($fields);
  1020. $hash = Security::hash(serialize($fields) . Security::salt());
  1021. $hash .= ':' . 'Model.valid';
  1022. $hash = urlencode($hash);
  1023. $tokenDebug = urlencode(json_encode([
  1024. '',
  1025. $fields,
  1026. []
  1027. ]));
  1028. $expected = [
  1029. 'div' => ['style' => 'display:none;'],
  1030. ['input' => [
  1031. 'type' => 'hidden',
  1032. 'name' => '_Token[fields]',
  1033. 'value' => $hash
  1034. ]],
  1035. ['input' => [
  1036. 'type' => 'hidden',
  1037. 'name' => '_Token[unlocked]',
  1038. 'value' => '',
  1039. ]],
  1040. ['input' => [
  1041. 'type' => 'hidden',
  1042. 'name' => '_Token[debug]',
  1043. 'value' => $tokenDebug
  1044. ]],
  1045. '/div'
  1046. ];
  1047. $this->assertHtml($expected, $result);
  1048. }
  1049. /**
  1050. * testFormSecurityFields method
  1051. *
  1052. * Test debug token is not generated if debug is false
  1053. *
  1054. * @return void
  1055. */
  1056. public function testFormSecurityFieldsNoDebugMode()
  1057. {
  1058. Configure::write('debug', false);
  1059. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1060. $this->Form->request->params['_Token'] = 'testKey';
  1061. $result = $this->Form->secure($fields);
  1062. $hash = Security::hash(serialize($fields) . Security::salt());
  1063. $hash .= ':' . 'Model.valid';
  1064. $hash = urlencode($hash);
  1065. $expected = [
  1066. 'div' => ['style' => 'display:none;'],
  1067. ['input' => [
  1068. 'type' => 'hidden',
  1069. 'name' => '_Token[fields]',
  1070. 'value' => $hash
  1071. ]],
  1072. ['input' => [
  1073. 'type' => 'hidden',
  1074. 'name' => '_Token[unlocked]',
  1075. 'value' => '',
  1076. ]],
  1077. '/div'
  1078. ];
  1079. $this->assertHtml($expected, $result);
  1080. }
  1081. /**
  1082. * Tests correct generation of number fields for double and float fields
  1083. *
  1084. * @return void
  1085. */
  1086. public function testTextFieldGenerationForFloats()
  1087. {
  1088. $this->article['schema'] = [
  1089. 'foo' => [
  1090. 'type' => 'float',
  1091. 'null' => false,
  1092. 'default' => null,
  1093. 'length' => 10
  1094. ]
  1095. ];
  1096. $this->Form->create($this->article);
  1097. $result = $this->Form->input('foo');
  1098. $expected = [
  1099. 'div' => ['class' => 'input number'],
  1100. 'label' => ['for' => 'foo'],
  1101. 'Foo',
  1102. '/label',
  1103. ['input' => [
  1104. 'type' => 'number',
  1105. 'name' => 'foo',
  1106. 'id' => 'foo',
  1107. 'step' => 'any'
  1108. ]],
  1109. '/div'
  1110. ];
  1111. $this->assertHtml($expected, $result);
  1112. $result = $this->Form->input('foo', ['step' => 0.5]);
  1113. $expected = [
  1114. 'div' => ['class' => 'input number'],
  1115. 'label' => ['for' => 'foo'],
  1116. 'Foo',
  1117. '/label',
  1118. ['input' => [
  1119. 'type' => 'number',
  1120. 'name' => 'foo',
  1121. 'id' => 'foo',
  1122. 'step' => '0.5'
  1123. ]],
  1124. '/div'
  1125. ];
  1126. $this->assertHtml($expected, $result);
  1127. }
  1128. /**
  1129. * Tests correct generation of number fields for integer fields
  1130. *
  1131. * @return void
  1132. */
  1133. public function testTextFieldTypeNumberGenerationForIntegers()
  1134. {
  1135. TableRegistry::get('Contacts', [
  1136. 'className' => __NAMESPACE__ . '\ContactsTable'
  1137. ]);
  1138. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1139. $result = $this->Form->input('age');
  1140. $expected = [
  1141. 'div' => ['class' => 'input number'],
  1142. 'label' => ['for' => 'age'],
  1143. 'Age',
  1144. '/label',
  1145. ['input' => [
  1146. 'type' => 'number', 'name' => 'age',
  1147. 'id' => 'age'
  1148. ]],
  1149. '/div'
  1150. ];
  1151. $this->assertHtml($expected, $result);
  1152. }
  1153. /**
  1154. * Tests correct generation of file upload fields for binary fields
  1155. *
  1156. * @return void
  1157. */
  1158. public function testFileUploadFieldTypeGenerationForBinaries()
  1159. {
  1160. $table = TableRegistry::get('Contacts', [
  1161. 'className' => __NAMESPACE__ . '\ContactsTable'
  1162. ]);
  1163. $table->schema(['foo' => [
  1164. 'type' => 'binary',
  1165. 'null' => false,
  1166. 'default' => null,
  1167. 'length' => 1024
  1168. ]]);
  1169. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1170. $result = $this->Form->input('foo');
  1171. $expected = [
  1172. 'div' => ['class' => 'input file'],
  1173. 'label' => ['for' => 'foo'],
  1174. 'Foo',
  1175. '/label',
  1176. ['input' => [
  1177. 'type' => 'file', 'name' => 'foo',
  1178. 'id' => 'foo'
  1179. ]],
  1180. '/div'
  1181. ];
  1182. $this->assertHtml($expected, $result);
  1183. }
  1184. /**
  1185. * testFormSecurityMultipleFields method
  1186. *
  1187. * Test secure() with multiple row form. Ensure hash is correct.
  1188. *
  1189. * @return void
  1190. */
  1191. public function testFormSecurityMultipleFields()
  1192. {
  1193. $this->Form->request->params['_Token'] = 'foo';
  1194. $fields = [
  1195. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1196. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1197. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1198. ];
  1199. $result = $this->Form->secure($fields);
  1200. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1201. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1202. $tokenDebug = urlencode(json_encode([
  1203. '',
  1204. $fields,
  1205. []
  1206. ]));
  1207. $expected = [
  1208. 'div' => ['style' => 'display:none;'],
  1209. ['input' => [
  1210. 'type' => 'hidden', 'name' => '_Token[fields]',
  1211. 'value' => $hash
  1212. ]],
  1213. ['input' => [
  1214. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1215. 'value' => ''
  1216. ]],
  1217. ['input' => [
  1218. 'type' => 'hidden', 'name' => '_Token[debug]',
  1219. 'value' => $tokenDebug,
  1220. ]],
  1221. '/div'
  1222. ];
  1223. $this->assertHtml($expected, $result);
  1224. }
  1225. /**
  1226. * testFormSecurityMultipleSubmitButtons
  1227. *
  1228. * test form submit generation and ensure that _Token is only created on end()
  1229. *
  1230. * @return void
  1231. */
  1232. public function testFormSecurityMultipleSubmitButtons()
  1233. {
  1234. $this->Form->request->params['_Token'] = 'testKey';
  1235. $this->Form->create($this->article);
  1236. $this->Form->text('Address.title');
  1237. $this->Form->text('Address.first_name');
  1238. $result = $this->Form->submit('Save', ['name' => 'save']);
  1239. $expected = [
  1240. 'div' => ['class' => 'submit'],
  1241. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1242. '/div',
  1243. ];
  1244. $this->assertHtml($expected, $result);
  1245. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1246. $expected = [
  1247. 'div' => ['class' => 'submit'],
  1248. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1249. '/div',
  1250. ];
  1251. $this->assertHtml($expected, $result);
  1252. $result = $this->Form->end();
  1253. $tokenDebug = urlencode(json_encode([
  1254. '/articles/add',
  1255. [
  1256. 'Address.title',
  1257. 'Address.first_name',
  1258. ],
  1259. ['save', 'cancel']
  1260. ]));
  1261. $expected = [
  1262. 'div' => ['style' => 'display:none;'],
  1263. ['input' => [
  1264. 'type' => 'hidden',
  1265. 'name' => '_Token[fields]',
  1266. 'value'
  1267. ]],
  1268. ['input' => [
  1269. 'type' => 'hidden',
  1270. 'name' => '_Token[unlocked]',
  1271. 'value' => 'cancel%7Csave'
  1272. ]],
  1273. ['input' => [
  1274. 'type' => 'hidden', 'name' => '_Token[debug]',
  1275. 'value' => $tokenDebug,
  1276. ]],
  1277. '/div'
  1278. ];
  1279. $this->assertHtml($expected, $result);
  1280. }
  1281. /**
  1282. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1283. *
  1284. * @return void
  1285. */
  1286. public function testSecurityButtonNestedNamed()
  1287. {
  1288. $key = 'testKey';
  1289. $this->Form->request->params['_csrfToken'] = $key;
  1290. $this->Form->create('Addresses');
  1291. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1292. $result = $this->Form->unlockField();
  1293. $this->assertEquals(['Address.button'], $result);
  1294. }
  1295. /**
  1296. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1297. *
  1298. * @return void
  1299. */
  1300. public function testSecuritySubmitNestedNamed()
  1301. {
  1302. $this->Form->request->params['_Token'] = 'testKey';
  1303. $this->Form->create($this->article);
  1304. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1305. $result = $this->Form->unlockField();
  1306. $this->assertEquals(['Address.button'], $result);
  1307. }
  1308. /**
  1309. * Test that the correct fields are unlocked for image submits with no names.
  1310. *
  1311. * @return void
  1312. */
  1313. public function testSecuritySubmitImageNoName()
  1314. {
  1315. $key = 'testKey';
  1316. $this->Form->request->params['_Token'] = 'testKey';
  1317. $this->Form->create(false);
  1318. $result = $this->Form->submit('save.png');
  1319. $expected = [
  1320. 'div' => ['class' => 'submit'],
  1321. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1322. '/div'
  1323. ];
  1324. $this->assertHtml($expected, $result);
  1325. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1326. }
  1327. /**
  1328. * Test that the correct fields are unlocked for image submits with names.
  1329. *
  1330. * @return void
  1331. */
  1332. public function testSecuritySubmitImageName()
  1333. {
  1334. $this->Form->request->params['_Token'] = 'testKey';
  1335. $this->Form->create(null);
  1336. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1337. $expected = [
  1338. 'div' => ['class' => 'submit'],
  1339. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1340. '/div'
  1341. ];
  1342. $this->assertHtml($expected, $result);
  1343. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1344. }
  1345. /**
  1346. * testFormSecurityMultipleInputFields method
  1347. *
  1348. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1349. *
  1350. * @return void
  1351. */
  1352. public function testFormSecurityMultipleInputFields()
  1353. {
  1354. $this->Form->request->params['_Token'] = 'testKey';
  1355. $this->Form->create();
  1356. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1357. $this->Form->input('Addresses.0.title');
  1358. $this->Form->input('Addresses.0.first_name');
  1359. $this->Form->input('Addresses.0.last_name');
  1360. $this->Form->input('Addresses.0.address');
  1361. $this->Form->input('Addresses.0.city');
  1362. $this->Form->input('Addresses.0.phone');
  1363. $this->Form->input('Addresses.0.primary', ['type' => 'checkbox']);
  1364. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1365. $this->Form->input('Addresses.1.title');
  1366. $this->Form->input('Addresses.1.first_name');
  1367. $this->Form->input('Addresses.1.last_name');
  1368. $this->Form->input('Addresses.1.address');
  1369. $this->Form->input('Addresses.1.city');
  1370. $this->Form->input('Addresses.1.phone');
  1371. $this->Form->input('Addresses.1.primary', ['type' => 'checkbox']);
  1372. $result = $this->Form->secure($this->Form->fields);
  1373. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1374. $tokenDebug = urlencode(json_encode([
  1375. '/articles/add',
  1376. [
  1377. 'Addresses.0.id' => '123456',
  1378. 'Addresses.0.title',
  1379. 'Addresses.0.first_name',
  1380. 'Addresses.0.last_name',
  1381. 'Addresses.0.address',
  1382. 'Addresses.0.city',
  1383. 'Addresses.0.phone',
  1384. 'Addresses.0.primary',
  1385. 'Addresses.1.id' => '654321',
  1386. 'Addresses.1.title',
  1387. 'Addresses.1.first_name',
  1388. 'Addresses.1.last_name',
  1389. 'Addresses.1.address',
  1390. 'Addresses.1.city',
  1391. 'Addresses.1.phone',
  1392. 'Addresses.1.primary',
  1393. ],
  1394. []
  1395. ]));
  1396. $expected = [
  1397. 'div' => ['style' => 'display:none;'],
  1398. ['input' => [
  1399. 'type' => 'hidden', 'name' => '_Token[fields]',
  1400. 'value' => $hash
  1401. ]],
  1402. ['input' => [
  1403. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1404. 'value' => ''
  1405. ]],
  1406. ['input' => [
  1407. 'type' => 'hidden', 'name' => '_Token[debug]',
  1408. 'value' => $tokenDebug,
  1409. ]],
  1410. '/div'
  1411. ];
  1412. $this->assertHtml($expected, $result);
  1413. }
  1414. /**
  1415. * Test form security with Model.field.0 style inputs
  1416. *
  1417. * @return void
  1418. */
  1419. public function testFormSecurityArrayFields()
  1420. {
  1421. $this->Form->request->params['_Token'] = 'testKey';
  1422. $this->Form->create();
  1423. $this->Form->text('Address.primary.1');
  1424. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1425. $this->Form->text('Address.secondary.1.0');
  1426. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1427. }
  1428. /**
  1429. * testFormSecurityMultipleInputDisabledFields method
  1430. *
  1431. * test secure form generation with multiple records and disabled fields.
  1432. *
  1433. * @return void
  1434. */
  1435. public function testFormSecurityMultipleInputDisabledFields()
  1436. {
  1437. $this->Form->request->params['_Token'] = [
  1438. 'unlockedFields' => ['first_name', 'address']
  1439. ];
  1440. $this->Form->create();
  1441. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1442. $this->Form->text('Addresses.0.title');
  1443. $this->Form->text('Addresses.0.first_name');
  1444. $this->Form->text('Addresses.0.last_name');
  1445. $this->Form->text('Addresses.0.address');
  1446. $this->Form->text('Addresses.0.city');
  1447. $this->Form->text('Addresses.0.phone');
  1448. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1449. $this->Form->text('Addresses.1.title');
  1450. $this->Form->text('Addresses.1.first_name');
  1451. $this->Form->text('Addresses.1.last_name');
  1452. $this->Form->text('Addresses.1.address');
  1453. $this->Form->text('Addresses.1.city');
  1454. $this->Form->text('Addresses.1.phone');
  1455. $result = $this->Form->secure($this->Form->fields);
  1456. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1457. $tokenDebug = urlencode(json_encode([
  1458. '/articles/add',
  1459. [
  1460. 'Addresses.0.id' => '123456',
  1461. 'Addresses.0.title',
  1462. 'Addresses.0.last_name',
  1463. 'Addresses.0.city',
  1464. 'Addresses.0.phone',
  1465. 'Addresses.1.id' => '654321',
  1466. 'Addresses.1.title',
  1467. 'Addresses.1.last_name',
  1468. 'Addresses.1.city',
  1469. 'Addresses.1.phone'
  1470. ],
  1471. [
  1472. 'first_name',
  1473. 'address'
  1474. ]
  1475. ]));
  1476. $expected = [
  1477. 'div' => ['style' => 'display:none;'],
  1478. ['input' => [
  1479. 'type' => 'hidden',
  1480. 'name' => '_Token[fields]',
  1481. 'value' => $hash
  1482. ]],
  1483. ['input' => [
  1484. 'type' => 'hidden',
  1485. 'name' => '_Token[unlocked]',
  1486. 'value' => 'address%7Cfirst_name',
  1487. ]],
  1488. ['input' => [
  1489. 'type' => 'hidden', 'name' => '_Token[debug]',
  1490. 'value' => $tokenDebug,
  1491. ]],
  1492. '/div'
  1493. ];
  1494. $this->assertHtml($expected, $result);
  1495. }
  1496. /**
  1497. * testFormSecurityInputDisabledFields method
  1498. *
  1499. * Test single record form with disabled fields.
  1500. *
  1501. * @return void
  1502. */
  1503. public function testFormSecurityInputUnlockedFields()
  1504. {
  1505. $this->Form->request['_Token'] = [
  1506. 'unlockedFields' => ['first_name', 'address']
  1507. ];
  1508. $this->Form->create();
  1509. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1510. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1511. $this->Form->text('Addresses.title');
  1512. $this->Form->text('Addresses.first_name');
  1513. $this->Form->text('Addresses.last_name');
  1514. $this->Form->text('Addresses.address');
  1515. $this->Form->text('Addresses.city');
  1516. $this->Form->text('Addresses.phone');
  1517. $result = $this->Form->fields;
  1518. $expected = [
  1519. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1520. 'Addresses.city', 'Addresses.phone'
  1521. ];
  1522. $this->assertEquals($expected, $result);
  1523. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1524. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1525. $tokenDebug = urlencode(json_encode([
  1526. '/articles/add',
  1527. [
  1528. 'Addresses.id' => '123456',
  1529. 'Addresses.title',
  1530. 'Addresses.last_name',
  1531. 'Addresses.city',
  1532. 'Addresses.phone'
  1533. ],
  1534. [
  1535. 'first_name',
  1536. 'address'
  1537. ]
  1538. ]));
  1539. $expected = [
  1540. 'div' => ['style' => 'display:none;'],
  1541. ['input' => [
  1542. 'type' => 'hidden',
  1543. 'name' => '_Token[fields]',
  1544. 'value' => $hash,
  1545. 'data-foo' => 'bar',
  1546. ]],
  1547. ['input' => [
  1548. 'type' => 'hidden',
  1549. 'name' => '_Token[unlocked]',
  1550. 'value' => 'address%7Cfirst_name',
  1551. 'data-foo' => 'bar',
  1552. ]],
  1553. ['input' => [
  1554. 'type' => 'hidden', 'name' => '_Token[debug]',
  1555. 'value' => $tokenDebug,
  1556. 'data-foo' => 'bar'
  1557. ]],
  1558. '/div'
  1559. ];
  1560. $this->assertHtml($expected, $result);
  1561. }
  1562. /**
  1563. * testFormSecurityInputUnlockedFieldsDebugSecurityTrue method
  1564. *
  1565. * Test single record form with debugSecurity param.
  1566. *
  1567. * @return void
  1568. */
  1569. public function testFormSecurityInputUnlockedFieldsDebugSecurityTrue()
  1570. {
  1571. $this->Form->request['_Token'] = [
  1572. 'unlockedFields' => ['first_name', 'address']
  1573. ];
  1574. $this->Form->create();
  1575. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1576. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1577. $this->Form->text('Addresses.title');
  1578. $this->Form->text('Addresses.first_name');
  1579. $this->Form->text('Addresses.last_name');
  1580. $this->Form->text('Addresses.address');
  1581. $this->Form->text('Addresses.city');
  1582. $this->Form->text('Addresses.phone');
  1583. $result = $this->Form->fields;
  1584. $expected = [
  1585. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1586. 'Addresses.city', 'Addresses.phone'
  1587. ];
  1588. $this->assertEquals($expected, $result);
  1589. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1590. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1591. $tokenDebug = urlencode(json_encode([
  1592. '/articles/add',
  1593. [
  1594. 'Addresses.id' => '123456',
  1595. 'Addresses.title',
  1596. 'Addresses.last_name',
  1597. 'Addresses.city',
  1598. 'Addresses.phone'
  1599. ],
  1600. [
  1601. 'first_name',
  1602. 'address'
  1603. ]
  1604. ]));
  1605. $expected = [
  1606. 'div' => ['style' => 'display:none;'],
  1607. ['input' => [
  1608. 'type' => 'hidden',
  1609. 'name' => '_Token[fields]',
  1610. 'value' => $hash,
  1611. 'data-foo' => 'bar',
  1612. ]],
  1613. ['input' => [
  1614. 'type' => 'hidden',
  1615. 'name' => '_Token[unlocked]',
  1616. 'value' => 'address%7Cfirst_name',
  1617. 'data-foo' => 'bar',
  1618. ]],
  1619. ['input' => [
  1620. 'type' => 'hidden', 'name' => '_Token[debug]',
  1621. 'value' => $tokenDebug,
  1622. 'data-foo' => 'bar'
  1623. ]],
  1624. '/div'
  1625. ];
  1626. $this->assertHtml($expected, $result);
  1627. }
  1628. /**
  1629. * testFormSecurityInputUnlockedFieldsDebugSecurityFalse method
  1630. *
  1631. * Debug is false, debugSecurity is true -> no debug
  1632. *
  1633. * @return void
  1634. */
  1635. public function testFormSecurityInputUnlockedFieldsDebugSecurityDebugFalse()
  1636. {
  1637. $this->Form->request['_Token'] = [
  1638. 'unlockedFields' => ['first_name', 'address']
  1639. ];
  1640. $this->Form->create();
  1641. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1642. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1643. $this->Form->text('Addresses.title');
  1644. $this->Form->text('Addresses.first_name');
  1645. $this->Form->text('Addresses.last_name');
  1646. $this->Form->text('Addresses.address');
  1647. $this->Form->text('Addresses.city');
  1648. $this->Form->text('Addresses.phone');
  1649. $result = $this->Form->fields;
  1650. $expected = [
  1651. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1652. 'Addresses.city', 'Addresses.phone'
  1653. ];
  1654. $this->assertEquals($expected, $result);
  1655. Configure::write('debug', false);
  1656. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => true]);
  1657. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1658. $expected = [
  1659. 'div' => ['style' => 'display:none;'],
  1660. ['input' => [
  1661. 'type' => 'hidden',
  1662. 'name' => '_Token[fields]',
  1663. 'value' => $hash,
  1664. 'data-foo' => 'bar',
  1665. ]],
  1666. ['input' => [
  1667. 'type' => 'hidden',
  1668. 'name' => '_Token[unlocked]',
  1669. 'value' => 'address%7Cfirst_name',
  1670. 'data-foo' => 'bar',
  1671. ]],
  1672. '/div'
  1673. ];
  1674. $this->assertHtml($expected, $result);
  1675. }
  1676. /**
  1677. * testFormSecurityInputUnlockedFieldsDebugSecurityFalse method
  1678. *
  1679. * Test single record form with debugSecurity param.
  1680. *
  1681. * @return void
  1682. */
  1683. public function testFormSecurityInputUnlockedFieldsDebugSecurityFalse()
  1684. {
  1685. $this->Form->request['_Token'] = [
  1686. 'unlockedFields' => ['first_name', 'address']
  1687. ];
  1688. $this->Form->create();
  1689. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1690. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1691. $this->Form->text('Addresses.title');
  1692. $this->Form->text('Addresses.first_name');
  1693. $this->Form->text('Addresses.last_name');
  1694. $this->Form->text('Addresses.address');
  1695. $this->Form->text('Addresses.city');
  1696. $this->Form->text('Addresses.phone');
  1697. $result = $this->Form->fields;
  1698. $expected = [
  1699. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1700. 'Addresses.city', 'Addresses.phone'
  1701. ];
  1702. $this->assertEquals($expected, $result);
  1703. $result = $this->Form->secure($expected, ['data-foo' => 'bar', 'debugSecurity' => false]);
  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. * test securing inputs with custom name attributes.
  1725. *
  1726. * @return void
  1727. */
  1728. public function testFormSecureWithCustomNameAttribute()
  1729. {
  1730. $this->Form->request->params['_Token'] = 'testKey';
  1731. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1732. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1733. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1734. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1735. }
  1736. /**
  1737. * testFormSecuredInput method
  1738. *
  1739. * Test generation of entire secure form, assertions made on input() output.
  1740. *
  1741. * @return void
  1742. */
  1743. public function testFormSecuredInput()
  1744. {
  1745. $this->Form->request->params['_csrfToken'] = 'testKey';
  1746. $this->Form->request->params['_Token'] = 'stuff';
  1747. $this->article['schema'] = [
  1748. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1749. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1750. ];
  1751. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1752. $encoding = strtolower(Configure::read('App.encoding'));
  1753. $expected = [
  1754. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1755. 'div' => ['style' => 'display:none;'],
  1756. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1757. ['input' => [
  1758. 'type' => 'hidden',
  1759. 'name' => '_csrfToken',
  1760. 'value' => 'testKey'
  1761. ]],
  1762. '/div'
  1763. ];
  1764. $this->assertHtml($expected, $result);
  1765. $result = $this->Form->input('ratio');
  1766. $expected = [
  1767. 'div' => ['class'],
  1768. 'label' => ['for'],
  1769. 'Ratio',
  1770. '/label',
  1771. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1772. '/div',
  1773. ];
  1774. $this->assertHtml($expected, $result);
  1775. $result = $this->Form->input('population');
  1776. $expected = [
  1777. 'div' => ['class'],
  1778. 'label' => ['for'],
  1779. 'Population',
  1780. '/label',
  1781. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1782. '/div',
  1783. ];
  1784. $this->assertHtml($expected, $result);
  1785. $result = $this->Form->input('published', ['type' => 'text']);
  1786. $expected = [
  1787. 'div' => ['class' => 'input text'],
  1788. 'label' => ['for' => 'published'],
  1789. 'Published',
  1790. '/label',
  1791. ['input' => [
  1792. 'type' => 'text',
  1793. 'name' => 'published',
  1794. 'id' => 'published'
  1795. ]],
  1796. '/div'
  1797. ];
  1798. $this->assertHtml($expected, $result);
  1799. $result = $this->Form->input('other', ['type' => 'text']);
  1800. $expected = [
  1801. 'div' => ['class' => 'input text'],
  1802. 'label' => ['for' => 'other'],
  1803. 'Other',
  1804. '/label',
  1805. ['input' => [
  1806. 'type' => 'text',
  1807. 'name' => 'other',
  1808. 'id',
  1809. ]],
  1810. '/div'
  1811. ];
  1812. $this->assertHtml($expected, $result);
  1813. $result = $this->Form->hidden('stuff');
  1814. $expected = [
  1815. 'input' => [
  1816. 'type' => 'hidden',
  1817. 'name' => 'stuff'
  1818. ]
  1819. ];
  1820. $this->assertHtml($expected, $result);
  1821. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1822. $expected = ['input' => [
  1823. 'type' => 'hidden',
  1824. 'name' => 'hidden',
  1825. 'value' => '0'
  1826. ]];
  1827. $this->assertHtml($expected, $result);
  1828. $result = $this->Form->input('something', ['type' => 'checkbox']);
  1829. $expected = [
  1830. 'div' => ['class' => 'input checkbox'],
  1831. ['input' => [
  1832. 'type' => 'hidden',
  1833. 'name' => 'something',
  1834. 'value' => '0'
  1835. ]],
  1836. 'label' => ['for' => 'something'],
  1837. ['input' => [
  1838. 'type' => 'checkbox',
  1839. 'name' => 'something',
  1840. 'value' => '1',
  1841. 'id' => 'something'
  1842. ]],
  1843. 'Something',
  1844. '/label',
  1845. '/div'
  1846. ];
  1847. $this->assertHtml($expected, $result);
  1848. $result = $this->Form->fields;
  1849. $expectedFields = [
  1850. 'ratio',
  1851. 'population',
  1852. 'published',
  1853. 'other',
  1854. 'stuff' => '',
  1855. 'hidden' => '0',
  1856. 'something'
  1857. ];
  1858. $this->assertEquals($expectedFields, $result);
  1859. $result = $this->Form->secure($this->Form->fields);
  1860. $tokenDebug = urlencode(json_encode([
  1861. '/articles/add',
  1862. $expectedFields,
  1863. []
  1864. ]));
  1865. $expected = [
  1866. 'div' => ['style' => 'display:none;'],
  1867. ['input' => [
  1868. 'type' => 'hidden',
  1869. 'name' => '_Token[fields]',
  1870. 'value'
  1871. ]],
  1872. ['input' => [
  1873. 'type' => 'hidden',
  1874. 'name' => '_Token[unlocked]',
  1875. 'value' => ''
  1876. ]],
  1877. ['input' => [
  1878. 'type' => 'hidden', 'name' => '_Token[debug]',
  1879. 'value' => $tokenDebug,
  1880. ]],
  1881. '/div'
  1882. ];
  1883. $this->assertHtml($expected, $result);
  1884. }
  1885. /**
  1886. * Test secured inputs with custom names.
  1887. *
  1888. * @return void
  1889. */
  1890. public function testSecuredInputCustomName()
  1891. {
  1892. $this->Form->request->params['_Token'] = 'testKey';
  1893. $this->assertEquals([], $this->Form->fields);
  1894. $this->Form->text('text_input', [
  1895. 'name' => 'Option[General.default_role]',
  1896. ]);
  1897. $expected = ['Option.General.default_role'];
  1898. $this->assertEquals($expected, $this->Form->fields);
  1899. $this->Form->select('select_box', [1, 2], [
  1900. 'name' => 'Option[General.select_role]',
  1901. ]);
  1902. $expected[] = 'Option.General.select_role';
  1903. $this->assertEquals($expected, $this->Form->fields);
  1904. $this->Form->text('other.things[]');
  1905. $expected[] = 'other.things';
  1906. $this->assertEquals($expected, $this->Form->fields);
  1907. }
  1908. /**
  1909. * Test that a hidden field followed by a visible field
  1910. * undoes the hidden field locking.
  1911. *
  1912. * @return void
  1913. */
  1914. public function testSecuredInputDuplicate()
  1915. {
  1916. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  1917. $this->assertEquals([], $this->Form->fields);
  1918. $this->Form->input('text_val', [
  1919. 'type' => 'hidden',
  1920. 'value' => 'some text',
  1921. ]);
  1922. $expected = ['text_val' => 'some text'];
  1923. $this->assertEquals($expected, $this->Form->fields);
  1924. $this->Form->input('text_val', [
  1925. 'type' => 'text',
  1926. ]);
  1927. $expected = ['text_val'];
  1928. $this->assertEquals($expected, $this->Form->fields);
  1929. }
  1930. /**
  1931. * Tests that the correct keys are added to the field hash index
  1932. *
  1933. * @return void
  1934. */
  1935. public function testFormSecuredFileInput()
  1936. {
  1937. $this->assertEquals([], $this->Form->fields);
  1938. $this->Form->file('Attachment.file');
  1939. $expected = [
  1940. 'Attachment.file.name', 'Attachment.file.type',
  1941. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1942. 'Attachment.file.size'
  1943. ];
  1944. $this->assertEquals($expected, $this->Form->fields);
  1945. }
  1946. /**
  1947. * test that multiple selects keys are added to field hash
  1948. *
  1949. * @return void
  1950. */
  1951. public function testFormSecuredMultipleSelect()
  1952. {
  1953. $this->Form->request->params['_csrfToken'] = 'testKey';
  1954. $this->assertEquals([], $this->Form->fields);
  1955. $options = ['1' => 'one', '2' => 'two'];
  1956. $this->Form->select('Model.select', $options);
  1957. $expected = ['Model.select'];
  1958. $this->assertEquals($expected, $this->Form->fields);
  1959. $this->Form->fields = [];
  1960. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1961. $this->assertEquals($expected, $this->Form->fields);
  1962. }
  1963. /**
  1964. * testFormSecuredRadio method
  1965. *
  1966. * @return void
  1967. */
  1968. public function testFormSecuredRadio()
  1969. {
  1970. $this->Form->request->params['_Token'] = 'testKey';
  1971. $this->assertEquals([], $this->Form->fields);
  1972. $options = ['1' => 'option1', '2' => 'option2'];
  1973. $this->Form->radio('Test.test', $options);
  1974. $expected = ['Test.test'];
  1975. $this->assertEquals($expected, $this->Form->fields);
  1976. $this->Form->radio('Test.all', $options, [
  1977. 'disabled' => ['option1', 'option2']
  1978. ]);
  1979. $expected = ['Test.test', 'Test.all' => ''];
  1980. $this->assertEquals($expected, $this->Form->fields);
  1981. $this->Form->radio('Test.some', $options, [
  1982. 'disabled' => ['option1']
  1983. ]);
  1984. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1985. $this->assertEquals($expected, $this->Form->fields);
  1986. }
  1987. /**
  1988. * Test that when disabled is in a list based attribute array it works.
  1989. *
  1990. * @return void
  1991. */
  1992. public function testFormSecuredAndDisabledNotAssoc()
  1993. {
  1994. $this->Form->request->params['_Token'] = 'testKey';
  1995. $this->Form->select('Model.select', [1, 2], ['disabled']);
  1996. $this->Form->checkbox('Model.checkbox', ['disabled']);
  1997. $this->Form->text('Model.text', ['disabled']);
  1998. $this->Form->textarea('Model.textarea', ['disabled']);
  1999. $this->Form->password('Model.password', ['disabled']);
  2000. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  2001. $expected = [
  2002. 'Model.radio' => ''
  2003. ];
  2004. $this->assertEquals($expected, $this->Form->fields);
  2005. }
  2006. /**
  2007. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  2008. * hashing.
  2009. *
  2010. * @return void
  2011. */
  2012. public function testFormSecuredAndDisabled()
  2013. {
  2014. $this->Form->request->params['_Token'] = 'testKey';
  2015. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  2016. $this->Form->text('Model.text', ['disabled' => true]);
  2017. $this->Form->password('Model.text', ['disabled' => true]);
  2018. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  2019. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  2020. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  2021. $this->Form->year('Model.year', ['disabled' => true]);
  2022. $this->Form->month('Model.month', ['disabled' => true]);
  2023. $this->Form->day('Model.day', ['disabled' => true]);
  2024. $this->Form->hour('Model.hour', ['disabled' => true]);
  2025. $this->Form->minute('Model.minute', ['disabled' => true]);
  2026. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  2027. $expected = [
  2028. 'Model.radio' => ''
  2029. ];
  2030. $this->assertEquals($expected, $this->Form->fields);
  2031. }
  2032. /**
  2033. * testDisableSecurityUsingForm method
  2034. *
  2035. * @return void
  2036. */
  2037. public function testDisableSecurityUsingForm()
  2038. {
  2039. $this->Form->request['_Token'] = [
  2040. 'disabledFields' => []
  2041. ];
  2042. $this->Form->create();
  2043. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  2044. $this->Form->text('Addresses.title');
  2045. $this->Form->text('Addresses.first_name', ['secure' => false]);
  2046. $this->Form->textarea('Addresses.city', ['secure' => false]);
  2047. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  2048. $result = $this->Form->fields;
  2049. $expected = [
  2050. 'Addresses.id' => '123456', 'Addresses.title',
  2051. ];
  2052. $this->assertEquals($expected, $result);
  2053. }
  2054. /**
  2055. * test disableField
  2056. *
  2057. * @return void
  2058. */
  2059. public function testUnlockFieldAddsToList()
  2060. {
  2061. $this->Form->request['_Token'] = [
  2062. 'unlockedFields' => []
  2063. ];
  2064. $this->Form->unlockField('Contact.name');
  2065. $this->Form->text('Contact.name');
  2066. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  2067. $this->assertEquals([], $this->Form->fields);
  2068. }
  2069. /**
  2070. * test unlockField removing from fields array.
  2071. *
  2072. * @return void
  2073. */
  2074. public function testUnlockFieldRemovingFromFields()
  2075. {
  2076. $this->Form->request['_Token'] = [
  2077. 'unlockedFields' => []
  2078. ];
  2079. $this->Form->create($this->article);
  2080. $this->Form->hidden('Article.id', ['value' => 1]);
  2081. $this->Form->text('Article.title');
  2082. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  2083. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  2084. $this->Form->unlockField('Article.title');
  2085. $this->Form->unlockField('Article.id');
  2086. $this->assertEquals([], $this->Form->fields);
  2087. }
  2088. /**
  2089. * test reset unlockFields, when create new form.
  2090. *
  2091. * @return void
  2092. */
  2093. public function testResetUnlockFields()
  2094. {
  2095. $this->Form->request['_Token'] = [
  2096. 'key' => 'testKey',
  2097. 'unlockedFields' => []
  2098. ];
  2099. $this->Form->unlockField('Contact.id');
  2100. $this->Form->create('Contact');
  2101. $this->Form->hidden('Contact.id', ['value' => 1]);
  2102. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2103. $this->Form->end();
  2104. $this->Form->create('Contact');
  2105. $this->Form->hidden('Contact.id', ['value' => 1]);
  2106. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2107. }
  2108. /**
  2109. * Test that only the path + query elements of a form's URL show up in their hash.
  2110. *
  2111. * @return void
  2112. */
  2113. public function testSecuredFormUrlIgnoresHost()
  2114. {
  2115. $this->Form->request['_Token'] = ['key' => 'testKey'];
  2116. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2117. $this->Form->create($this->article, [
  2118. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2119. ]);
  2120. $result = $this->Form->secure();
  2121. $this->assertContains($expected, $result);
  2122. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2123. $result = $this->Form->secure();
  2124. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2125. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2126. $result = $this->Form->secure();
  2127. $this->assertContains($expected, $result, 'URL path + query should work.');
  2128. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2129. $result = $this->Form->secure();
  2130. $this->assertNotContains($expected, $result, 'URL is different');
  2131. }
  2132. /**
  2133. * Test that URL, HTML and identifer show up in their hashs.
  2134. *
  2135. * @return void
  2136. */
  2137. public function testSecuredFormUrlHasHtmlAndIdentifer()
  2138. {
  2139. $this->Form->request['_Token'] = ['key' => 'testKey'];
  2140. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2141. $res = $this->Form->create($this->article, [
  2142. 'url' => [
  2143. 'controller' => 'articles',
  2144. 'action' => 'view',
  2145. '?' => [
  2146. 'page' => 1,
  2147. 'limit' => 10,
  2148. 'html' => '<>"',
  2149. ],
  2150. '#' => 'result',
  2151. ],
  2152. ]);
  2153. $result = $this->Form->secure();
  2154. $this->assertContains($expected, $result);
  2155. $this->Form->create($this->article, [
  2156. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2157. ]);
  2158. $result = $this->Form->secure();
  2159. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2160. $this->Form->create($this->article, [
  2161. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2162. ]);
  2163. $result = $this->Form->secure();
  2164. $this->assertContains($expected, $result, 'URL path + query should work.');
  2165. }
  2166. /**
  2167. * test error message display
  2168. *
  2169. * @return void
  2170. */
  2171. public function testErrorMessageDisplay()
  2172. {
  2173. $this->article['errors'] = [
  2174. 'Article' => [
  2175. 'title' => 'error message',
  2176. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2177. ]
  2178. ];
  2179. $this->Form->create($this->article);
  2180. $result = $this->Form->input('Article.title');
  2181. $expected = [
  2182. 'div' => ['class' => 'input text error'],
  2183. 'label' => ['for' => 'article-title'],
  2184. 'Title',
  2185. '/label',
  2186. 'input' => [
  2187. 'type' => 'text', 'name' => 'Article[title]',
  2188. 'id' => 'article-title', 'class' => 'form-error'
  2189. ],
  2190. ['div' => ['class' => 'error-message']],
  2191. 'error message',
  2192. '/div',
  2193. '/div'
  2194. ];
  2195. $this->assertHtml($expected, $result);
  2196. $result = $this->Form->input('Article.title', [
  2197. 'templates' => [
  2198. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2199. ]
  2200. ]);
  2201. $expected = [
  2202. 'div' => ['class' => 'input text error'],
  2203. 'label' => ['for' => 'article-title'],
  2204. 'Title',
  2205. '/label',
  2206. 'input' => [
  2207. 'type' => 'text', 'name' => 'Article[title]',
  2208. 'id' => 'article-title', 'class' => 'form-error'
  2209. ],
  2210. '/div'
  2211. ];
  2212. $this->assertHtml($expected, $result);
  2213. $result = $this->Form->input('Article.content');
  2214. $expected = [
  2215. 'div' => ['class' => 'input text error'],
  2216. 'label' => ['for' => 'article-content'],
  2217. 'Content',
  2218. '/label',
  2219. 'input' => [
  2220. 'type' => 'text', 'name' => 'Article[content]',
  2221. 'id' => 'article-content', 'class' => 'form-error'
  2222. ],
  2223. ['div' => ['class' => 'error-message']],
  2224. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2225. '/div',
  2226. '/div'
  2227. ];
  2228. $this->assertHtml($expected, $result);
  2229. $result = $this->Form->input('Article.content', ['error' => ['escape' => true]]);
  2230. $expected = [
  2231. 'div' => ['class' => 'input text error'],
  2232. 'label' => ['for' => 'article-content'],
  2233. 'Content',
  2234. '/label',
  2235. 'input' => [
  2236. 'type' => 'text', 'name' => 'Article[content]',
  2237. 'id' => 'article-content', 'class' => 'form-error'
  2238. ],
  2239. ['div' => ['class' => 'error-message']],
  2240. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2241. '/div',
  2242. '/div'
  2243. ];
  2244. $this->assertHtml($expected, $result);
  2245. $result = $this->Form->input('Article.content', ['error' => ['escape' => false]]);
  2246. $expected = [
  2247. 'div' => ['class' => 'input text error'],
  2248. 'label' => ['for' => 'article-content'],
  2249. 'Content',
  2250. '/label',
  2251. 'input' => [
  2252. 'type' => 'text', 'name' => 'Article[content]',
  2253. 'id' => 'article-content', 'class' => 'form-error'
  2254. ],
  2255. ['div' => ['class' => 'error-message']],
  2256. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2257. '/div',
  2258. '/div'
  2259. ];
  2260. $this->assertHtml($expected, $result);
  2261. }
  2262. /**
  2263. * Test validation errors, when validation message is an empty string.
  2264. *
  2265. * @return void
  2266. */
  2267. public function testEmptyErrorValidation()
  2268. {
  2269. $this->article['errors'] = [
  2270. 'Article' => ['title' => '']
  2271. ];
  2272. $this->Form->create($this->article);
  2273. $result = $this->Form->input('Article.title');
  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' => ['class' => 'error-message']],
  2284. [],
  2285. '/div',
  2286. '/div'
  2287. ];
  2288. $this->assertHtml($expected, $result);
  2289. }
  2290. /**
  2291. * Test validation errors, when calling input() overriding validation message by an empty string.
  2292. *
  2293. * @return void
  2294. */
  2295. public function testEmptyInputErrorValidation()
  2296. {
  2297. $this->article['errors'] = [
  2298. 'Article' => ['title' => 'error message']
  2299. ];
  2300. $this->Form->create($this->article);
  2301. $result = $this->Form->input('Article.title', ['error' => '']);
  2302. $expected = [
  2303. 'div' => ['class' => 'input text error'],
  2304. 'label' => ['for' => 'article-title'],
  2305. 'Title',
  2306. '/label',
  2307. 'input' => [
  2308. 'type' => 'text', 'name' => 'Article[title]',
  2309. 'id' => 'article-title', 'class' => 'form-error'
  2310. ],
  2311. ['div' => ['class' => 'error-message']],
  2312. [],
  2313. '/div',
  2314. '/div'
  2315. ];
  2316. $this->assertHtml($expected, $result);
  2317. }
  2318. /**
  2319. * Test validation errors, when calling input() overriding validation messages
  2320. *
  2321. * @return void
  2322. */
  2323. public function testInputErrorMessage()
  2324. {
  2325. $this->article['errors'] = [
  2326. 'title' => ['error message']
  2327. ];
  2328. $this->Form->create($this->article);
  2329. $result = $this->Form->input('title', [
  2330. 'error' => 'Custom error!'
  2331. ]);
  2332. $expected = [
  2333. 'div' => ['class' => 'input text required error'],
  2334. 'label' => ['for' => 'title'],
  2335. 'Title',
  2336. '/label',
  2337. 'input' => [
  2338. 'type' => 'text', 'name' => 'title',
  2339. 'id' => 'title', 'class' => 'form-error',
  2340. 'required' => 'required',
  2341. ],
  2342. ['div' => ['class' => 'error-message']],
  2343. 'Custom error!',
  2344. '/div',
  2345. '/div'
  2346. ];
  2347. $this->assertHtml($expected, $result);
  2348. $result = $this->Form->input('title', [
  2349. 'error' => ['error message' => 'Custom error!']
  2350. ]);
  2351. $expected = [
  2352. 'div' => ['class' => 'input text required error'],
  2353. 'label' => ['for' => 'title'],
  2354. 'Title',
  2355. '/label',
  2356. 'input' => [
  2357. 'type' => 'text',
  2358. 'name' => 'title',
  2359. 'id' => 'title',
  2360. 'class' => 'form-error',
  2361. 'required' => 'required'
  2362. ],
  2363. ['div' => ['class' => 'error-message']],
  2364. 'Custom error!',
  2365. '/div',
  2366. '/div'
  2367. ];
  2368. $this->assertHtml($expected, $result);
  2369. }
  2370. /**
  2371. * Tests displaying errors for nested entities
  2372. *
  2373. * @return void
  2374. */
  2375. public function testFormValidationAssociated()
  2376. {
  2377. $nested = new Entity(['foo' => 'bar']);
  2378. $nested->errors('foo', ['not a valid bar']);
  2379. $entity = new Entity(['nested' => $nested]);
  2380. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2381. $result = $this->Form->error('nested.foo');
  2382. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2383. }
  2384. /**
  2385. * testFormValidationAssociatedSecondLevel method
  2386. *
  2387. * test form error display with associated model.
  2388. *
  2389. * @return void
  2390. */
  2391. public function testFormValidationAssociatedSecondLevel()
  2392. {
  2393. $inner = new Entity(['bar' => 'baz']);
  2394. $nested = new Entity(['foo' => $inner]);
  2395. $entity = new Entity(['nested' => $nested]);
  2396. $inner->errors('bar', ['not a valid one']);
  2397. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2398. $result = $this->Form->error('nested.foo.bar');
  2399. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2400. }
  2401. /**
  2402. * testFormValidationMultiRecord method
  2403. *
  2404. * test form error display with multiple records.
  2405. *
  2406. * @return void
  2407. */
  2408. public function testFormValidationMultiRecord()
  2409. {
  2410. $one = new Entity;
  2411. $two = new Entity;
  2412. TableRegistry::get('Contacts', [
  2413. 'className' => __NAMESPACE__ . '\ContactsTable'
  2414. ]);
  2415. $one->set('email', '');
  2416. $one->errors('email', ['invalid email']);
  2417. $two->set('name', '');
  2418. $two->errors('name', ['This is wrong']);
  2419. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2420. $result = $this->Form->input('0.email');
  2421. $expected = [
  2422. 'div' => ['class' => 'input email error'],
  2423. 'label' => ['for' => '0-email'],
  2424. 'Email',
  2425. '/label',
  2426. 'input' => [
  2427. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2428. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2429. ],
  2430. ['div' => ['class' => 'error-message']],
  2431. 'invalid email',
  2432. '/div',
  2433. '/div'
  2434. ];
  2435. $this->assertHtml($expected, $result);
  2436. $result = $this->Form->input('1.name');
  2437. $expected = [
  2438. 'div' => ['class' => 'input text error'],
  2439. 'label' => ['for' => '1-name'],
  2440. 'Name',
  2441. '/label',
  2442. 'input' => [
  2443. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2444. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2445. ],
  2446. ['div' => ['class' => 'error-message']],
  2447. 'This is wrong',
  2448. '/div',
  2449. '/div'
  2450. ];
  2451. $this->assertHtml($expected, $result);
  2452. }
  2453. /**
  2454. * testInput method
  2455. *
  2456. * Test various incarnations of input().
  2457. *
  2458. * @return void
  2459. */
  2460. public function testInput()
  2461. {
  2462. TableRegistry::get('ValidateUsers', [
  2463. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2464. ]);
  2465. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2466. $result = $this->Form->input('ValidateUsers.balance');
  2467. $expected = [
  2468. 'div' => ['class'],
  2469. 'label' => ['for'],
  2470. 'Balance',
  2471. '/label',
  2472. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2473. '/div',
  2474. ];
  2475. $this->assertHtml($expected, $result);
  2476. $result = $this->Form->input('ValidateUser.cost_decimal');
  2477. $expected = [
  2478. 'div' => ['class'],
  2479. 'label' => ['for'],
  2480. 'Cost Decimal',
  2481. '/label',
  2482. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2483. '/div',
  2484. ];
  2485. $this->assertHtml($expected, $result);
  2486. $result = $this->Form->input('ValidateUser.null_decimal');
  2487. $expected = [
  2488. 'div' => ['class'],
  2489. 'label' => ['for'],
  2490. 'Null Decimal',
  2491. '/label',
  2492. 'input' => ['name', 'type' => 'number', 'id'],
  2493. '/div',
  2494. ];
  2495. $this->assertHtml($expected, $result);
  2496. }
  2497. /**
  2498. * Tests the input method and passing custom options
  2499. *
  2500. * @return void
  2501. */
  2502. public function testInputCustomization()
  2503. {
  2504. TableRegistry::get('Contacts', [
  2505. 'className' => __NAMESPACE__ . '\ContactsTable'
  2506. ]);
  2507. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2508. $result = $this->Form->input('Contact.email', ['id' => 'custom']);
  2509. $expected = [
  2510. 'div' => ['class' => 'input email'],
  2511. 'label' => ['for' => 'custom'],
  2512. 'Email',
  2513. '/label',
  2514. ['input' => [
  2515. 'type' => 'email', 'name' => 'Contact[email]',
  2516. 'id' => 'custom', 'maxlength' => 255
  2517. ]],
  2518. '/div'
  2519. ];
  2520. $this->assertHtml($expected, $result);
  2521. $result = $this->Form->input('Contact.email', [
  2522. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2523. ]);
  2524. $expected = [
  2525. '<div',
  2526. 'label' => ['for' => 'contact-email'],
  2527. 'Email',
  2528. '/label',
  2529. ['input' => [
  2530. 'type' => 'email', 'name' => 'Contact[email]',
  2531. 'id' => 'contact-email', 'maxlength' => 255
  2532. ]],
  2533. '/div'
  2534. ];
  2535. $this->assertHtml($expected, $result);
  2536. $result = $this->Form->input('Contact.email', ['type' => 'text']);
  2537. $expected = [
  2538. 'div' => ['class' => 'input text'],
  2539. 'label' => ['for' => 'contact-email'],
  2540. 'Email',
  2541. '/label',
  2542. ['input' => [
  2543. 'type' => 'text', 'name' => 'Contact[email]',
  2544. 'id' => 'contact-email', 'maxlength' => '255'
  2545. ]],
  2546. '/div'
  2547. ];
  2548. $this->assertHtml($expected, $result);
  2549. $result = $this->Form->input('Contact.5.email', ['type' => 'text']);
  2550. $expected = [
  2551. 'div' => ['class' => 'input text'],
  2552. 'label' => ['for' => 'contact-5-email'],
  2553. 'Email',
  2554. '/label',
  2555. ['input' => [
  2556. 'type' => 'text', 'name' => 'Contact[5][email]',
  2557. 'id' => 'contact-5-email', 'maxlength' => '255'
  2558. ]],
  2559. '/div'
  2560. ];
  2561. $this->assertHtml($expected, $result);
  2562. $result = $this->Form->input('Contact.password');
  2563. $expected = [
  2564. 'div' => ['class' => 'input password'],
  2565. 'label' => ['for' => 'contact-password'],
  2566. 'Password',
  2567. '/label',
  2568. ['input' => [
  2569. 'type' => 'password', 'name' => 'Contact[password]',
  2570. 'id' => 'contact-password'
  2571. ]],
  2572. '/div'
  2573. ];
  2574. $this->assertHtml($expected, $result);
  2575. $result = $this->Form->input('Contact.email', [
  2576. 'type' => 'file', 'class' => 'textbox'
  2577. ]);
  2578. $expected = [
  2579. 'div' => ['class' => 'input file'],
  2580. 'label' => ['for' => 'contact-email'],
  2581. 'Email',
  2582. '/label',
  2583. ['input' => [
  2584. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2585. 'id' => 'contact-email'
  2586. ]],
  2587. '/div'
  2588. ];
  2589. $this->assertHtml($expected, $result);
  2590. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2591. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2592. $result = $this->Form->input('phone');
  2593. $expected = [
  2594. 'div' => ['class' => 'input tel'],
  2595. 'label' => ['for' => 'phone'],
  2596. 'Phone',
  2597. '/label',
  2598. ['input' => [
  2599. 'type' => 'tel', 'name' => 'phone',
  2600. 'value' => 'Hello &amp; World &gt; weird chars',
  2601. 'id' => 'phone', 'maxlength' => 255
  2602. ]],
  2603. '/div'
  2604. ];
  2605. $this->assertHtml($expected, $result);
  2606. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2607. $this->Form->create();
  2608. $result = $this->Form->input('Model.0.OtherModel.field', ['id' => 'myId']);
  2609. $expected = [
  2610. 'div' => ['class' => 'input text'],
  2611. 'label' => ['for' => 'myId'],
  2612. 'Field',
  2613. '/label',
  2614. 'input' => [
  2615. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2616. 'value' => 'My value', 'id' => 'myId'
  2617. ],
  2618. '/div'
  2619. ];
  2620. $this->assertHtml($expected, $result);
  2621. $this->Form->request->data = [];
  2622. $entity->errors('field', 'Badness!');
  2623. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2624. $result = $this->Form->input('field');
  2625. $expected = [
  2626. 'div' => ['class' => 'input text error'],
  2627. 'label' => ['for' => 'field'],
  2628. 'Field',
  2629. '/label',
  2630. 'input' => [
  2631. 'type' => 'text', 'name' => 'field',
  2632. 'id' => 'field', 'class' => 'form-error'
  2633. ],
  2634. ['div' => ['class' => 'error-message']],
  2635. 'Badness!',
  2636. '/div',
  2637. '/div'
  2638. ];
  2639. $this->assertHtml($expected, $result);
  2640. $result = $this->Form->input('field', [
  2641. 'templates' => [
  2642. 'inputContainerError' => '{{content}}{{error}}',
  2643. 'error' => '<span class="error-message">{{content}}</span>'
  2644. ]
  2645. ]);
  2646. $expected = [
  2647. 'label' => ['for' => 'field'],
  2648. 'Field',
  2649. '/label',
  2650. 'input' => [
  2651. 'type' => 'text', 'name' => 'field',
  2652. 'id' => 'field', 'class' => 'form-error'
  2653. ],
  2654. ['span' => ['class' => 'error-message']],
  2655. 'Badness!',
  2656. '/span'
  2657. ];
  2658. $this->assertHtml($expected, $result);
  2659. $entity->errors('field', ['minLength'], true);
  2660. $result = $this->Form->input('field', [
  2661. 'error' => [
  2662. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2663. 'maxLength' => 'login too large'
  2664. ]
  2665. ]);
  2666. $expected = [
  2667. 'div' => ['class' => 'input text error'],
  2668. 'label' => ['for' => 'field'],
  2669. 'Field',
  2670. '/label',
  2671. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2672. ['div' => ['class' => 'error-message']],
  2673. 'Le login doit contenir au moins 2 caractères',
  2674. '/div',
  2675. '/div'
  2676. ];
  2677. $this->assertHtml($expected, $result);
  2678. $entity->errors('field', ['maxLength'], true);
  2679. $result = $this->Form->input('field', [
  2680. 'error' => [
  2681. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2682. 'maxLength' => 'login too large',
  2683. ]
  2684. ]);
  2685. $expected = [
  2686. 'div' => ['class' => 'input text error'],
  2687. 'label' => ['for' => 'field'],
  2688. 'Field',
  2689. '/label',
  2690. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2691. ['div' => ['class' => 'error-message']],
  2692. 'login too large',
  2693. '/div',
  2694. '/div'
  2695. ];
  2696. $this->assertHtml($expected, $result);
  2697. }
  2698. /**
  2699. * Test that input() accepts a template file.
  2700. *
  2701. * @return void
  2702. */
  2703. public function testInputWithTemplateFile()
  2704. {
  2705. $result = $this->Form->input('field', [
  2706. 'templates' => 'htmlhelper_tags'
  2707. ]);
  2708. $expected = [
  2709. 'label' => ['for' => 'field'],
  2710. 'Field',
  2711. '/label',
  2712. 'input' => [
  2713. 'type' => 'text', 'name' => 'field',
  2714. 'id' => 'field'
  2715. ],
  2716. ];
  2717. $this->assertHtml($expected, $result);
  2718. }
  2719. /**
  2720. * Test that nested inputs end with brackets
  2721. *
  2722. * @return void
  2723. */
  2724. public function testNestedInputsEndWithBrackets()
  2725. {
  2726. $result = $this->Form->text('nested.text[]');
  2727. $expected = [
  2728. 'input' => [
  2729. 'type' => 'text', 'name' => 'nested[text][]'
  2730. ],
  2731. ];
  2732. $result = $this->Form->file('nested.file[]');
  2733. $expected = [
  2734. 'input' => [
  2735. 'type' => 'file', 'name' => 'nested[file][]'
  2736. ],
  2737. ];
  2738. $this->assertHtml($expected, $result);
  2739. }
  2740. /**
  2741. * Test id prefix
  2742. *
  2743. * @return void
  2744. */
  2745. public function testCreateIdPrefix()
  2746. {
  2747. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2748. $result = $this->Form->input('field');
  2749. $expected = [
  2750. 'div' => ['class' => 'input text'],
  2751. 'label' => ['for' => 'prefix-field'],
  2752. 'Field',
  2753. '/label',
  2754. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2755. '/div'
  2756. ];
  2757. $this->assertHtml($expected, $result);
  2758. $result = $this->Form->input('field', ['id' => 'custom-id']);
  2759. $expected = [
  2760. 'div' => ['class' => 'input text'],
  2761. 'label' => ['for' => 'custom-id'],
  2762. 'Field',
  2763. '/label',
  2764. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2765. '/div'
  2766. ];
  2767. $this->assertHtml($expected, $result);
  2768. $result = $this->Form->radio('Model.field', ['option A']);
  2769. $expected = [
  2770. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2771. 'label' => ['for' => 'prefix-model-field-0'],
  2772. ['input' => [
  2773. 'type' => 'radio',
  2774. 'name' => 'Model[field]',
  2775. 'value' => '0',
  2776. 'id' => 'prefix-model-field-0'
  2777. ]],
  2778. 'option A',
  2779. '/label'
  2780. ];
  2781. $this->assertHtml($expected, $result);
  2782. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2783. $expected = [
  2784. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2785. 'label' => ['for' => 'prefix-model-field-0'],
  2786. ['input' => [
  2787. 'type' => 'radio',
  2788. 'name' => 'Model[field]',
  2789. 'value' => '0',
  2790. 'id' => 'prefix-model-field-0'
  2791. ]],
  2792. 'option A',
  2793. '/label'
  2794. ];
  2795. $this->assertHtml($expected, $result);
  2796. $result = $this->Form->select(
  2797. 'Model.multi_field',
  2798. ['first'],
  2799. ['multiple' => 'checkbox']
  2800. );
  2801. $expected = [
  2802. 'input' => [
  2803. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2804. ],
  2805. ['div' => ['class' => 'checkbox']],
  2806. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2807. ['input' => [
  2808. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2809. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2810. ]],
  2811. 'first',
  2812. '/label',
  2813. '/div',
  2814. ];
  2815. $this->assertHtml($expected, $result);
  2816. $this->Form->end();
  2817. $result = $this->Form->input('field');
  2818. $expected = [
  2819. 'div' => ['class' => 'input text'],
  2820. 'label' => ['for' => 'field'],
  2821. 'Field',
  2822. '/label',
  2823. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2824. '/div'
  2825. ];
  2826. $this->assertHtml($expected, $result);
  2827. }
  2828. /**
  2829. * Test that inputs with 0 can be created.
  2830. *
  2831. * @return void
  2832. */
  2833. public function testInputZero()
  2834. {
  2835. TableRegistry::get('Contacts', [
  2836. 'className' => __NAMESPACE__ . '\ContactsTable'
  2837. ]);
  2838. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2839. $result = $this->Form->input('0');
  2840. $expected = [
  2841. 'div' => ['class' => 'input text'],
  2842. 'label' => ['for' => '0'], '/label',
  2843. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2844. '/div'
  2845. ];
  2846. $this->assertHtml($expected, $result);
  2847. }
  2848. /**
  2849. * test input() with checkbox creation
  2850. *
  2851. * @return void
  2852. */
  2853. public function testInputCheckbox()
  2854. {
  2855. $result = $this->Form->input('User.active', ['label' => false, 'checked' => true]);
  2856. $expected = [
  2857. 'div' => ['class' => 'input checkbox'],
  2858. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2859. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2860. '/div'
  2861. ];
  2862. $this->assertHtml($expected, $result);
  2863. $result = $this->Form->input('User.active', ['label' => false, 'checked' => 1]);
  2864. $expected = [
  2865. 'div' => ['class' => 'input checkbox'],
  2866. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2867. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2868. '/div'
  2869. ];
  2870. $this->assertHtml($expected, $result);
  2871. $result = $this->Form->input('User.active', ['label' => false, 'checked' => '1']);
  2872. $expected = [
  2873. 'div' => ['class' => 'input checkbox'],
  2874. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2875. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2876. '/div'
  2877. ];
  2878. $this->assertHtml($expected, $result);
  2879. $result = $this->Form->input('User.disabled', [
  2880. 'label' => 'Disabled',
  2881. 'type' => 'checkbox',
  2882. 'data-foo' => 'disabled'
  2883. ]);
  2884. $expected = [
  2885. 'div' => ['class' => 'input checkbox'],
  2886. 'input' => ['type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'],
  2887. 'label' => ['for' => 'user-disabled'],
  2888. ['input' => [
  2889. 'type' => 'checkbox',
  2890. 'name' => 'User[disabled]',
  2891. 'value' => '1',
  2892. 'id' => 'user-disabled',
  2893. 'data-foo' => 'disabled'
  2894. ]],
  2895. 'Disabled',
  2896. '/label',
  2897. '/div'
  2898. ];
  2899. $this->assertHtml($expected, $result);
  2900. }
  2901. /**
  2902. * Test that input() does not create wrapping div and label tag for hidden fields
  2903. *
  2904. * @return void
  2905. */
  2906. public function testInputHidden()
  2907. {
  2908. TableRegistry::get('ValidateUsers', [
  2909. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2910. ]);
  2911. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2912. $result = $this->Form->input('ValidateUser.id');
  2913. $expected = [
  2914. 'input' => ['name', 'type' => 'hidden', 'id']
  2915. ];
  2916. $this->assertHtml($expected, $result);
  2917. $result = $this->Form->input('ValidateUser.custom', ['type' => 'hidden']);
  2918. $expected = [
  2919. 'input' => ['name', 'type' => 'hidden', 'id']
  2920. ];
  2921. $this->assertHtml($expected, $result);
  2922. }
  2923. /**
  2924. * test form->input() with datetime
  2925. *
  2926. * @return void
  2927. */
  2928. public function testInputDatetime()
  2929. {
  2930. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  2931. ->setMethods(['datetime'])
  2932. ->setConstructorArgs([new View()])
  2933. ->getMock();
  2934. $this->Form->expects($this->once())->method('datetime')
  2935. ->with('prueba', [
  2936. 'type' => 'datetime',
  2937. 'timeFormat' => 24,
  2938. 'minYear' => 2008,
  2939. 'maxYear' => 2011,
  2940. 'interval' => 15,
  2941. 'options' => null,
  2942. 'empty' => false,
  2943. 'id' => 'prueba',
  2944. 'required' => false,
  2945. 'templateVars' => []
  2946. ])
  2947. ->will($this->returnValue('This is it!'));
  2948. $result = $this->Form->input('prueba', [
  2949. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2950. 'maxYear' => 2011, 'interval' => 15
  2951. ]);
  2952. $expected = [
  2953. 'div' => ['class' => 'input datetime'],
  2954. '<label',
  2955. 'Prueba',
  2956. '/label',
  2957. 'This is it!',
  2958. '/div'
  2959. ];
  2960. $this->assertHtml($expected, $result);
  2961. }
  2962. /**
  2963. * test form->input() with datetime with id prefix
  2964. *
  2965. * @return void
  2966. */
  2967. public function testInputDatetimeIdPrefix()
  2968. {
  2969. $this->Form = $this->getMockBuilder('Cake\View\Helper\FormHelper')
  2970. ->setMethods(['datetime'])
  2971. ->setConstructorArgs([new View()])
  2972. ->getMock();
  2973. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2974. $this->Form->expects($this->once())->method('datetime')
  2975. ->with('prueba', [
  2976. 'type' => 'datetime',
  2977. 'timeFormat' => 24,
  2978. 'minYear' => 2008,
  2979. 'maxYear' => 2011,
  2980. 'interval' => 15,
  2981. 'options' => null,
  2982. 'empty' => false,
  2983. 'id' => 'prefix-prueba',
  2984. 'required' => false,
  2985. 'templateVars' => []
  2986. ])
  2987. ->will($this->returnValue('This is it!'));
  2988. $result = $this->Form->input('prueba', [
  2989. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2990. 'maxYear' => 2011, 'interval' => 15
  2991. ]);
  2992. $expected = [
  2993. 'div' => ['class' => 'input datetime'],
  2994. '<label',
  2995. 'Prueba',
  2996. '/label',
  2997. 'This is it!',
  2998. '/div'
  2999. ];
  3000. $this->assertHtml($expected, $result);
  3001. }
  3002. /**
  3003. * Test generating checkboxes with disabled elements.
  3004. *
  3005. * @return void
  3006. */
  3007. public function testInputCheckboxWithDisabledElements()
  3008. {
  3009. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3010. $result = $this->Form->input('Contact.multiple', [
  3011. 'multiple' => 'checkbox',
  3012. 'disabled' => 'disabled',
  3013. 'options' => $options
  3014. ]);
  3015. $expected = [
  3016. ['div' => ['class' => 'input select']],
  3017. ['label' => ['for' => "contact-multiple"]],
  3018. 'Multiple',
  3019. '/label',
  3020. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  3021. ['div' => ['class' => 'checkbox']],
  3022. ['label' => ['for' => "contact-multiple-1"]],
  3023. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  3024. 'One',
  3025. '/label',
  3026. '/div',
  3027. ['div' => ['class' => 'checkbox']],
  3028. ['label' => ['for' => "contact-multiple-2"]],
  3029. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  3030. 'Two',
  3031. '/label',
  3032. '/div',
  3033. ['div' => ['class' => 'checkbox']],
  3034. ['label' => ['for' => "contact-multiple-3"]],
  3035. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  3036. 'Three',
  3037. '/label',
  3038. '/div',
  3039. '/div'
  3040. ];
  3041. $this->assertHtml($expected, $result);
  3042. // make sure 50 does only disable 50, and not 50f5c0cf
  3043. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3044. $disabled = [50];
  3045. $expected = [
  3046. ['div' => ['class' => 'input select']],
  3047. ['label' => ['for' => "contact-multiple"]],
  3048. 'Multiple',
  3049. '/label',
  3050. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  3051. ['div' => ['class' => 'checkbox']],
  3052. ['label' => ['for' => "contact-multiple-50"]],
  3053. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  3054. 'Fifty',
  3055. '/label',
  3056. '/div',
  3057. ['div' => ['class' => 'checkbox']],
  3058. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  3059. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  3060. 'Stringy',
  3061. '/label',
  3062. '/div',
  3063. '/div'
  3064. ];
  3065. $result = $this->Form->input('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3066. $this->assertHtml($expected, $result);
  3067. }
  3068. /**
  3069. * test input name with leading integer, ensure attributes are generated correctly.
  3070. *
  3071. * @return void
  3072. */
  3073. public function testInputWithLeadingInteger()
  3074. {
  3075. $result = $this->Form->text('0.Node.title');
  3076. $expected = [
  3077. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3078. ];
  3079. $this->assertHtml($expected, $result);
  3080. }
  3081. /**
  3082. * test form->input() with select type inputs.
  3083. *
  3084. * @return void
  3085. */
  3086. public function testInputSelectType()
  3087. {
  3088. $result = $this->Form->input(
  3089. 'email',
  3090. [
  3091. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3092. );
  3093. $expected = [
  3094. 'div' => ['class' => 'input select'],
  3095. 'label' => ['for' => 'email'],
  3096. 'Email',
  3097. '/label',
  3098. ['select' => ['name' => 'email', 'id' => 'email']],
  3099. ['option' => ['value' => '']],
  3100. '/option',
  3101. ['option' => ['value' => 'è']],
  3102. 'Firést',
  3103. '/option',
  3104. ['option' => ['value' => 'é']],
  3105. 'Secoènd',
  3106. '/option',
  3107. '/select',
  3108. '/div'
  3109. ];
  3110. $this->assertHtml($expected, $result);
  3111. $result = $this->Form->input(
  3112. 'email',
  3113. [
  3114. 'options' => ['First', 'Second'], 'empty' => true]
  3115. );
  3116. $expected = [
  3117. 'div' => ['class' => 'input select'],
  3118. 'label' => ['for' => 'email'],
  3119. 'Email',
  3120. '/label',
  3121. ['select' => ['name' => 'email', 'id' => 'email']],
  3122. ['option' => ['value' => '']],
  3123. '/option',
  3124. ['option' => ['value' => '0']],
  3125. 'First',
  3126. '/option',
  3127. ['option' => ['value' => '1']],
  3128. 'Second',
  3129. '/option',
  3130. '/select',
  3131. '/div'
  3132. ];
  3133. $this->assertHtml($expected, $result);
  3134. $result = $this->Form->input('email', [
  3135. 'type' => 'select',
  3136. 'options' => new \ArrayObject(['First', 'Second']),
  3137. 'empty' => true
  3138. ]);
  3139. $this->assertHtml($expected, $result);
  3140. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3141. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3142. $result = $this->Form->input('Model.user_id', ['empty' => true]);
  3143. $expected = [
  3144. 'div' => ['class' => 'input select'],
  3145. 'label' => ['for' => 'model-user-id'],
  3146. 'User',
  3147. '/label',
  3148. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3149. ['option' => ['value' => '']],
  3150. '/option',
  3151. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3152. 'good',
  3153. '/option',
  3154. ['option' => ['value' => 'other']],
  3155. 'bad',
  3156. '/option',
  3157. '/select',
  3158. '/div'
  3159. ];
  3160. $this->assertHtml($expected, $result);
  3161. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3162. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3163. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  3164. $expected = [
  3165. 'div' => ['class' => 'input select'],
  3166. 'label' => ['for' => 'thing-user-id'],
  3167. 'User',
  3168. '/label',
  3169. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3170. ['option' => ['value' => '']],
  3171. 'Some Empty',
  3172. '/option',
  3173. ['option' => ['value' => 'value']],
  3174. 'good',
  3175. '/option',
  3176. ['option' => ['value' => 'other']],
  3177. 'bad',
  3178. '/option',
  3179. '/select',
  3180. '/div'
  3181. ];
  3182. $this->assertHtml($expected, $result);
  3183. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3184. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3185. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  3186. $expected = [
  3187. 'div' => ['class' => 'input select'],
  3188. 'label' => ['for' => 'thing-user-id'],
  3189. 'User',
  3190. '/label',
  3191. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3192. ['option' => ['value' => '']],
  3193. 'Some Empty',
  3194. '/option',
  3195. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3196. 'good',
  3197. '/option',
  3198. ['option' => ['value' => 'other']],
  3199. 'bad',
  3200. '/option',
  3201. '/select',
  3202. '/div'
  3203. ];
  3204. $this->assertHtml($expected, $result);
  3205. $this->Form->data = [];
  3206. $result = $this->Form->input('Publisher.id', [
  3207. 'label' => 'Publisher',
  3208. 'type' => 'select',
  3209. 'multiple' => 'checkbox',
  3210. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3211. ]);
  3212. $expected = [
  3213. ['div' => ['class' => 'input select']],
  3214. ['label' => ['for' => 'publisher-id']],
  3215. 'Publisher',
  3216. '/label',
  3217. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3218. ['div' => ['class' => 'checkbox']],
  3219. ['label' => ['for' => 'publisher-id-value-1']],
  3220. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3221. 'Label 1',
  3222. '/label',
  3223. '/div',
  3224. ['div' => ['class' => 'checkbox']],
  3225. ['label' => ['for' => 'publisher-id-value-2']],
  3226. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3227. 'Label 2',
  3228. '/label',
  3229. '/div',
  3230. '/div'
  3231. ];
  3232. $this->assertHtml($expected, $result);
  3233. }
  3234. /**
  3235. * test that input() and a non standard primary key makes a hidden input by default.
  3236. *
  3237. * @return void
  3238. */
  3239. public function testInputWithNonStandardPrimaryKeyMakesHidden()
  3240. {
  3241. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3242. $this->Form->create($this->article);
  3243. $result = $this->Form->input('title');
  3244. $expected = [
  3245. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3246. ];
  3247. $this->assertHtml($expected, $result);
  3248. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3249. $this->Form->create($this->article);
  3250. $result = $this->Form->input('title');
  3251. $expected = [
  3252. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3253. ];
  3254. $this->assertHtml($expected, $result);
  3255. $result = $this->Form->input('body');
  3256. $expected = [
  3257. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3258. ];
  3259. $this->assertHtml($expected, $result);
  3260. }
  3261. /**
  3262. * test that overriding the magic select type widget is possible
  3263. *
  3264. * @return void
  3265. */
  3266. public function testInputOverridingMagicSelectType()
  3267. {
  3268. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3269. $result = $this->Form->input('Model.user_id', ['type' => 'text']);
  3270. $expected = [
  3271. 'div' => ['class' => 'input text'],
  3272. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3273. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3274. '/div'
  3275. ];
  3276. $this->assertHtml($expected, $result);
  3277. //Check that magic types still work for plural/singular vars
  3278. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3279. $result = $this->Form->input('Model.type');
  3280. $expected = [
  3281. 'div' => ['class' => 'input select'],
  3282. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3283. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3284. ['option' => ['value' => 'value']], 'good', '/option',
  3285. ['option' => ['value' => 'other']], 'bad', '/option',
  3286. '/select',
  3287. '/div'
  3288. ];
  3289. $this->assertHtml($expected, $result);
  3290. }
  3291. /**
  3292. * Test that inferred types do not override developer input
  3293. *
  3294. * @return void
  3295. */
  3296. public function testInputMagicTypeDoesNotOverride()
  3297. {
  3298. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3299. $result = $this->Form->input('Model.user', ['type' => 'checkbox']);
  3300. $expected = [
  3301. 'div' => ['class' => 'input checkbox'],
  3302. ['input' => [
  3303. 'type' => 'hidden',
  3304. 'name' => 'Model[user]',
  3305. 'value' => 0,
  3306. ]],
  3307. 'label' => ['for' => 'model-user'],
  3308. ['input' => [
  3309. 'name' => 'Model[user]',
  3310. 'type' => 'checkbox',
  3311. 'id' => 'model-user',
  3312. 'value' => 1
  3313. ]],
  3314. 'User',
  3315. '/label',
  3316. '/div'
  3317. ];
  3318. $this->assertHtml($expected, $result);
  3319. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3320. $options = [
  3321. 1 => 'blue',
  3322. 2 => 'red'
  3323. ];
  3324. $result = $this->Form->input('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3325. $expected = [
  3326. 'div' => ['class' => 'input select'],
  3327. 'label' => ['for' => 'tags-ids'],
  3328. 'Tags',
  3329. '/label',
  3330. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3331. ['div' => ['class' => 'checkbox']],
  3332. ['label' => ['for' => 'tags-ids-1']],
  3333. ['input' => [
  3334. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3335. 'value' => '1', 'name' => 'tags[_ids][]'
  3336. ]],
  3337. 'blue',
  3338. '/label',
  3339. '/div',
  3340. ['div' => ['class' => 'checkbox']],
  3341. ['label' => ['for' => 'tags-ids-2']],
  3342. ['input' => [
  3343. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3344. 'value' => '2', 'name' => 'tags[_ids][]'
  3345. ]],
  3346. 'red',
  3347. '/label',
  3348. '/div',
  3349. '/div'
  3350. ];
  3351. $this->assertHtml($expected, $result);
  3352. }
  3353. /**
  3354. * Test that magic input() selects are created for type=number
  3355. *
  3356. * @return void
  3357. */
  3358. public function testInputMagicSelectForTypeNumber()
  3359. {
  3360. TableRegistry::get('ValidateUsers', [
  3361. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3362. ]);
  3363. $entity = new Entity(['balance' => 1]);
  3364. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3365. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3366. $result = $this->Form->input('balance');
  3367. $expected = [
  3368. 'div' => ['class' => 'input select'],
  3369. 'label' => ['for' => 'balance'],
  3370. 'Balance',
  3371. '/label',
  3372. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3373. ['option' => ['value' => '0']],
  3374. 'nothing',
  3375. '/option',
  3376. ['option' => ['value' => '1', 'selected' => 'selected']],
  3377. 'some',
  3378. '/option',
  3379. ['option' => ['value' => '100']],
  3380. 'a lot',
  3381. '/option',
  3382. '/select',
  3383. '/div'
  3384. ];
  3385. $this->assertHtml($expected, $result);
  3386. }
  3387. /**
  3388. * Test invalid 'input' type option to input() function.
  3389. *
  3390. * @expectedException \RuntimeException
  3391. * @expectedExceptionMessage Invalid type 'input' used for field 'text'
  3392. * @return void
  3393. */
  3394. public function testInvalidInputTypeOption()
  3395. {
  3396. $this->Form->input('text', ['type' => 'input']);
  3397. }
  3398. /**
  3399. * Test that magic input() selects can easily be converted into radio types without error.
  3400. *
  3401. * @return void
  3402. */
  3403. public function testInputMagicSelectChangeToRadio()
  3404. {
  3405. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3406. $result = $this->Form->input('Model.user_id', ['type' => 'radio']);
  3407. $this->assertContains('input type="radio"', $result);
  3408. }
  3409. /**
  3410. * testFormInputSubmit method
  3411. *
  3412. * test correct results for form::input() and type submit.
  3413. *
  3414. * @return void
  3415. */
  3416. public function testFormInputSubmit()
  3417. {
  3418. $result = $this->Form->input('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3419. $expected = [
  3420. 'div' => ['class' => 'submit'],
  3421. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3422. '/div'
  3423. ];
  3424. $this->assertHtml($expected, $result);
  3425. }
  3426. /**
  3427. * testFormInputs method
  3428. *
  3429. * test correct results from form::inputs().
  3430. *
  3431. * @return void
  3432. */
  3433. public function testFormInputsLegendFieldset()
  3434. {
  3435. $this->Form->create($this->article);
  3436. $result = $this->Form->allInputs([], ['legend' => 'The Legend']);
  3437. $expected = [
  3438. '<fieldset',
  3439. '<legend',
  3440. 'The Legend',
  3441. '/legend',
  3442. '*/fieldset',
  3443. ];
  3444. $this->assertHtml($expected, $result);
  3445. $result = $this->Form->allInputs([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3446. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3447. $this->assertContains('<fieldset>', $result);
  3448. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => false]);
  3449. $this->assertNotContains('<legend>', $result);
  3450. $this->assertNotContains('<fieldset>', $result);
  3451. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => 'Hello']);
  3452. $this->assertNotContains('<legend>', $result);
  3453. $this->assertNotContains('<fieldset>', $result);
  3454. $this->Form->create($this->article);
  3455. $this->Form->request->params['prefix'] = 'admin';
  3456. $this->Form->request->params['action'] = 'admin_edit';
  3457. $this->Form->request->params['controller'] = 'articles';
  3458. $result = $this->Form->allInputs();
  3459. $expected = [
  3460. '<fieldset',
  3461. '<legend',
  3462. 'New Article',
  3463. '/legend',
  3464. '*/fieldset',
  3465. ];
  3466. $this->assertHtml($expected, $result);
  3467. $this->Form->create($this->article);
  3468. $result = $this->Form->allInputs([], ['fieldset' => [], 'legend' => 'The Legend']);
  3469. $expected = [
  3470. '<fieldset',
  3471. '<legend',
  3472. 'The Legend',
  3473. '/legend',
  3474. '*/fieldset',
  3475. ];
  3476. $this->assertHtml($expected, $result);
  3477. $this->Form->create($this->article);
  3478. $result = $this->Form->allInputs([], [
  3479. 'fieldset' => [
  3480. 'class' => 'some-class some-other-class',
  3481. 'disabled' => true,
  3482. 'data-param' => 'a-param'
  3483. ],
  3484. 'legend' => 'The Legend'
  3485. ]);
  3486. $expected = [
  3487. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3488. '<legend',
  3489. 'The Legend',
  3490. '/legend',
  3491. '*/fieldset',
  3492. ];
  3493. $this->assertHtml($expected, $result);
  3494. }
  3495. /**
  3496. * Test the inputs() method.
  3497. *
  3498. * @return void
  3499. */
  3500. public function testFormInputs()
  3501. {
  3502. $this->Form->create($this->article);
  3503. $result = $this->Form->allInputs();
  3504. $expected = [
  3505. '<fieldset',
  3506. '<legend', 'New Article', '/legend',
  3507. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3508. ['div' => ['class' => 'input select required']],
  3509. '*/div',
  3510. ['div' => ['class' => 'input text required']],
  3511. '*/div',
  3512. ['div' => ['class' => 'input text']],
  3513. '*/div',
  3514. ['div' => ['class' => 'input text']],
  3515. '*/div',
  3516. '/fieldset',
  3517. ];
  3518. $this->assertHtml($expected, $result);
  3519. $result = $this->Form->allInputs([
  3520. 'published' => ['type' => 'boolean']
  3521. ]);
  3522. $expected = [
  3523. '<fieldset',
  3524. '<legend', 'New Article', '/legend',
  3525. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3526. ['div' => ['class' => 'input select required']],
  3527. '*/div',
  3528. ['div' => ['class' => 'input text required']],
  3529. '*/div',
  3530. ['div' => ['class' => 'input text']],
  3531. '*/div',
  3532. ['div' => ['class' => 'input boolean']],
  3533. '*/div',
  3534. '/fieldset',
  3535. ];
  3536. $this->assertHtml($expected, $result);
  3537. $this->Form->create($this->article);
  3538. $result = $this->Form->allInputs([], ['legend' => 'Hello']);
  3539. $expected = [
  3540. 'fieldset' => [],
  3541. 'legend' => [],
  3542. 'Hello',
  3543. '/legend',
  3544. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3545. ['div' => ['class' => 'input select required']],
  3546. '*/div',
  3547. ['div' => ['class' => 'input text required']],
  3548. '*/div',
  3549. ['div' => ['class' => 'input text']],
  3550. '*/div',
  3551. ['div' => ['class' => 'input text']],
  3552. '*/div',
  3553. '/fieldset'
  3554. ];
  3555. $this->assertHtml($expected, $result);
  3556. $this->Form->create(false);
  3557. $expected = [
  3558. 'fieldset' => [],
  3559. ['div' => ['class' => 'input text']],
  3560. 'label' => ['for' => 'foo'],
  3561. 'Foo',
  3562. '/label',
  3563. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3564. '*/div',
  3565. '/fieldset'
  3566. ];
  3567. $result = $this->Form->allInputs(
  3568. ['foo' => ['type' => 'text']],
  3569. ['legend' => false]
  3570. );
  3571. $this->assertHtml($expected, $result);
  3572. }
  3573. /**
  3574. * testFormInputsBlacklist
  3575. *
  3576. * @return void
  3577. */
  3578. public function testFormInputsBlacklist()
  3579. {
  3580. $this->Form->create($this->article);
  3581. $result = $this->Form->allInputs([
  3582. 'id' => false
  3583. ]);
  3584. $expected = [
  3585. '<fieldset',
  3586. '<legend', 'New Article', '/legend',
  3587. ['div' => ['class' => 'input select required']],
  3588. '*/div',
  3589. ['div' => ['class' => 'input text required']],
  3590. '*/div',
  3591. ['div' => ['class' => 'input text']],
  3592. '*/div',
  3593. ['div' => ['class' => 'input text']],
  3594. '*/div',
  3595. '/fieldset',
  3596. ];
  3597. $this->assertHtml($expected, $result);
  3598. $this->Form->create($this->article);
  3599. $result = $this->Form->allInputs([
  3600. 'id' => []
  3601. ]);
  3602. $expected = [
  3603. '<fieldset',
  3604. '<legend', 'New Article', '/legend',
  3605. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3606. ['div' => ['class' => 'input select required']],
  3607. '*/div',
  3608. ['div' => ['class' => 'input text required']],
  3609. '*/div',
  3610. ['div' => ['class' => 'input text']],
  3611. '*/div',
  3612. ['div' => ['class' => 'input text']],
  3613. '*/div',
  3614. '/fieldset',
  3615. ];
  3616. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3617. }
  3618. /**
  3619. * testSelectAsCheckbox method
  3620. *
  3621. * test multi-select widget with checkbox formatting.
  3622. *
  3623. * @return void
  3624. */
  3625. public function testSelectAsCheckbox()
  3626. {
  3627. $result = $this->Form->select(
  3628. 'Model.multi_field',
  3629. ['first', 'second', 'third'],
  3630. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3631. );
  3632. $expected = [
  3633. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3634. ['div' => ['class' => 'checkbox']],
  3635. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3636. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3637. 'first',
  3638. '/label',
  3639. '/div',
  3640. ['div' => ['class' => 'checkbox']],
  3641. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3642. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3643. 'second',
  3644. '/label',
  3645. '/div',
  3646. ['div' => ['class' => 'checkbox']],
  3647. ['label' => ['for' => 'model-multi-field-2']],
  3648. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3649. 'third',
  3650. '/label',
  3651. '/div',
  3652. ];
  3653. $this->assertHtml($expected, $result);
  3654. $result = $this->Form->select(
  3655. 'Model.multi_field',
  3656. ['1/2' => 'half'],
  3657. ['multiple' => 'checkbox']
  3658. );
  3659. $expected = [
  3660. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3661. ['div' => ['class' => 'checkbox']],
  3662. ['label' => ['for' => 'model-multi-field-1-2']],
  3663. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3664. 'half',
  3665. '/label',
  3666. '/div',
  3667. ];
  3668. $this->assertHtml($expected, $result);
  3669. }
  3670. /**
  3671. * testLabel method
  3672. *
  3673. * test label generation.
  3674. *
  3675. * @return void
  3676. */
  3677. public function testLabel()
  3678. {
  3679. $result = $this->Form->label('Person.name');
  3680. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3681. $this->assertHtml($expected, $result);
  3682. $result = $this->Form->label('Person.name');
  3683. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3684. $this->assertHtml($expected, $result);
  3685. $result = $this->Form->label('Person.first_name');
  3686. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3687. $this->assertHtml($expected, $result);
  3688. $result = $this->Form->label('Person.first_name', 'Your first name');
  3689. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3690. $this->assertHtml($expected, $result);
  3691. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3692. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3693. $this->assertHtml($expected, $result);
  3694. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3695. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3696. $this->assertHtml($expected, $result);
  3697. $result = $this->Form->label('Person.first_name', '');
  3698. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3699. $this->assertHtml($expected, $result);
  3700. $result = $this->Form->label('Person.2.name', '');
  3701. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3702. $this->assertHtml($expected, $result);
  3703. }
  3704. /**
  3705. * Test that label() can accept an input with the correct template vars.
  3706. *
  3707. * @return void
  3708. */
  3709. public function testLabelContainInput()
  3710. {
  3711. $this->Form->templates([
  3712. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3713. ]);
  3714. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3715. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3716. ]);
  3717. $expected = [
  3718. 'label' => ['for' => 'person-accept-terms'],
  3719. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3720. 'Accept',
  3721. '/label',
  3722. ];
  3723. $this->assertHtml($expected, $result);
  3724. }
  3725. /**
  3726. * testTextbox method
  3727. *
  3728. * test textbox element generation
  3729. *
  3730. * @return void
  3731. */
  3732. public function testTextbox()
  3733. {
  3734. $result = $this->Form->text('Model.field');
  3735. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3736. $this->assertHtml($expected, $result);
  3737. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3738. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3739. $this->assertHtml($expected, $result);
  3740. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3741. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3742. $this->assertHtml($expected, $result);
  3743. }
  3744. /**
  3745. * Test that text() hooks up with request data and error fields.
  3746. *
  3747. * @return void
  3748. */
  3749. public function testTextBoxDataAndError()
  3750. {
  3751. $this->article['errors'] = [
  3752. 'Contact' => ['text' => 'wrong']
  3753. ];
  3754. $this->Form->create($this->article);
  3755. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3756. $result = $this->Form->text('Model.text');
  3757. $expected = [
  3758. 'input' => [
  3759. 'type' => 'text',
  3760. 'name' => 'Model[text]',
  3761. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3762. ]
  3763. ];
  3764. $this->assertHtml($expected, $result);
  3765. $this->Form->request->data['Contact']['text'] = 'test';
  3766. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3767. $expected = [
  3768. 'input' => [
  3769. 'type' => 'text',
  3770. 'name' => 'Contact[text]',
  3771. 'value' => 'test',
  3772. 'id' => 'theID',
  3773. 'class' => 'form-error'
  3774. ]
  3775. ];
  3776. $this->assertHtml($expected, $result);
  3777. }
  3778. /**
  3779. * testDefaultValue method
  3780. *
  3781. * Test default value setting
  3782. *
  3783. * @return void
  3784. */
  3785. public function testTextDefaultValue()
  3786. {
  3787. $this->Form->request->data['Model']['field'] = 'test';
  3788. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3789. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3790. $this->assertHtml($expected, $result);
  3791. unset($this->Form->request->data['Model']['field']);
  3792. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3793. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3794. $this->assertHtml($expected, $result);
  3795. $Articles = TableRegistry::get('Articles');
  3796. $title = $Articles->schema()->column('title');
  3797. $Articles->schema()->addColumn(
  3798. 'title',
  3799. ['default' => 'default title'] + $title
  3800. );
  3801. $entity = $Articles->newEntity();
  3802. $this->Form->create($entity);
  3803. // Get default value from schema
  3804. $result = $this->Form->text('title');
  3805. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'default title']];
  3806. $this->assertHtml($expected, $result);
  3807. // Don't get value from schema
  3808. $result = $this->Form->text('title', ['schemaDefault' => false]);
  3809. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3810. $this->assertHtml($expected, $result);
  3811. // Custom default value overrides default value from schema
  3812. $result = $this->Form->text('title', ['default' => 'override default']);
  3813. $expected = ['input' => ['type' => 'text', 'name' => 'title', 'value' => 'override default']];
  3814. $this->assertHtml($expected, $result);
  3815. // Default value from schema is used only for new entities.
  3816. $entity->isNew(false);
  3817. $result = $this->Form->text('title');
  3818. $expected = ['input' => ['type' => 'text', 'name' => 'title']];
  3819. $this->assertHtml($expected, $result);
  3820. }
  3821. /**
  3822. * testError method
  3823. *
  3824. * Test field error generation
  3825. *
  3826. * @return void
  3827. */
  3828. public function testError()
  3829. {
  3830. $this->article['errors'] = [
  3831. 'Article' => ['field' => 'email']
  3832. ];
  3833. $this->Form->create($this->article);
  3834. $result = $this->Form->error('Article.field');
  3835. $expected = [
  3836. ['div' => ['class' => 'error-message']],
  3837. 'email',
  3838. '/div',
  3839. ];
  3840. $this->assertHtml($expected, $result);
  3841. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3842. $expected = [
  3843. ['div' => ['class' => 'error-message']],
  3844. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3845. '/div',
  3846. ];
  3847. $this->assertHtml($expected, $result);
  3848. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3849. $expected = [
  3850. ['div' => ['class' => 'error-message']],
  3851. '<strong', 'Badness!', '/strong',
  3852. '/div',
  3853. ];
  3854. $this->assertHtml($expected, $result);
  3855. }
  3856. /**
  3857. * test error translation can use rule names for translating.
  3858. *
  3859. * @return void
  3860. */
  3861. public function testErrorRuleName()
  3862. {
  3863. $this->article['errors'] = [
  3864. 'Article' => [
  3865. 'field' => ['email' => 'Your email was not good']
  3866. ]
  3867. ];
  3868. $this->Form->create($this->article);
  3869. $result = $this->Form->error('Article.field');
  3870. $expected = [
  3871. ['div' => ['class' => 'error-message']],
  3872. 'Your email was not good',
  3873. '/div',
  3874. ];
  3875. $this->assertHtml($expected, $result);
  3876. $result = $this->Form->error('Article.field', ['email' => 'Email in use']);
  3877. $expected = [
  3878. ['div' => ['class' => 'error-message']],
  3879. 'Email in use',
  3880. '/div',
  3881. ];
  3882. $this->assertHtml($expected, $result);
  3883. $result = $this->Form->error('Article.field', ['Your email was not good' => 'Email in use']);
  3884. $expected = [
  3885. ['div' => ['class' => 'error-message']],
  3886. 'Email in use',
  3887. '/div',
  3888. ];
  3889. $this->assertHtml($expected, $result);
  3890. $result = $this->Form->error('Article.field', [
  3891. 'email' => 'Key is preferred',
  3892. 'Your email was not good' => 'Email in use'
  3893. ]);
  3894. $expected = [
  3895. ['div' => ['class' => 'error-message']],
  3896. 'Key is preferred',
  3897. '/div',
  3898. ];
  3899. $this->assertHtml($expected, $result);
  3900. }
  3901. /**
  3902. * Test error with nested lists.
  3903. *
  3904. * @return void
  3905. */
  3906. public function testErrorMessages()
  3907. {
  3908. $this->article['errors'] = [
  3909. 'Article' => ['field' => 'email']
  3910. ];
  3911. $this->Form->create($this->article);
  3912. $result = $this->Form->error('Article.field', [
  3913. 'email' => 'No good!'
  3914. ]);
  3915. $expected = [
  3916. 'div' => ['class' => 'error-message'],
  3917. 'No good!',
  3918. '/div'
  3919. ];
  3920. $this->assertHtml($expected, $result);
  3921. }
  3922. /**
  3923. * test error() with multiple messages.
  3924. *
  3925. * @return void
  3926. */
  3927. public function testErrorMultipleMessages()
  3928. {
  3929. $this->article['errors'] = [
  3930. 'field' => ['notBlank', 'email', 'Something else']
  3931. ];
  3932. $this->Form->create($this->article);
  3933. $result = $this->Form->error('field', [
  3934. 'notBlank' => 'Cannot be empty',
  3935. 'email' => 'No good!'
  3936. ]);
  3937. $expected = [
  3938. 'div' => ['class' => 'error-message'],
  3939. 'ul' => [],
  3940. '<li', 'Cannot be empty', '/li',
  3941. '<li', 'No good!', '/li',
  3942. '<li', 'Something else', '/li',
  3943. '/ul',
  3944. '/div'
  3945. ];
  3946. $this->assertHtml($expected, $result);
  3947. }
  3948. /**
  3949. * testPassword method
  3950. *
  3951. * Test password element generation
  3952. *
  3953. * @return void
  3954. */
  3955. public function testPassword()
  3956. {
  3957. $this->article['errors'] = [
  3958. 'Contact' => [
  3959. 'passwd' => 1
  3960. ]
  3961. ];
  3962. $this->Form->create($this->article);
  3963. $result = $this->Form->password('Contact.field');
  3964. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  3965. $this->assertHtml($expected, $result);
  3966. $this->Form->request->data['Contact']['passwd'] = 'test';
  3967. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  3968. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  3969. $this->assertHtml($expected, $result);
  3970. }
  3971. /**
  3972. * testRadio method
  3973. *
  3974. * Test radio element set generation
  3975. *
  3976. * @return void
  3977. */
  3978. public function testRadio()
  3979. {
  3980. $result = $this->Form->radio('Model.field', ['option A']);
  3981. $expected = [
  3982. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3983. 'label' => ['for' => 'model-field-0'],
  3984. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  3985. 'option A',
  3986. '/label'
  3987. ];
  3988. $this->assertHtml($expected, $result);
  3989. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  3990. $this->assertHtml($expected, $result);
  3991. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3992. $expected = [
  3993. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3994. ['label' => ['for' => 'model-field-0']],
  3995. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  3996. 'option A',
  3997. '/label',
  3998. ['label' => ['for' => 'model-field-1']],
  3999. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4000. 'option B',
  4001. '/label',
  4002. ];
  4003. $this->assertHtml($expected, $result);
  4004. $result = $this->Form->radio(
  4005. 'Employee.gender',
  4006. ['male' => 'Male', 'female' => 'Female'],
  4007. ['form' => 'my-form']
  4008. );
  4009. $expected = [
  4010. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  4011. ['label' => ['for' => 'employee-gender-male']],
  4012. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  4013. 'Male',
  4014. '/label',
  4015. ['label' => ['for' => 'employee-gender-female']],
  4016. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  4017. 'Female',
  4018. '/label',
  4019. ];
  4020. $this->assertHtml($expected, $result);
  4021. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  4022. $expected = [
  4023. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  4024. ['label' => ['for' => 'model-custom-0']],
  4025. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  4026. 'option A',
  4027. '/label',
  4028. ['label' => ['for' => 'model-custom-1']],
  4029. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  4030. 'option B',
  4031. '/label',
  4032. ];
  4033. $this->assertHtml($expected, $result);
  4034. $result = $this->Form->radio(
  4035. 'Employee.gender',
  4036. [
  4037. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  4038. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  4039. ]
  4040. );
  4041. $expected = [
  4042. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  4043. ['label' => ['for' => 'employee-gender-male']],
  4044. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  4045. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  4046. 'Male',
  4047. '/label',
  4048. ['label' => ['for' => 'employee-gender-female']],
  4049. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  4050. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  4051. 'Female',
  4052. '/label',
  4053. ];
  4054. $this->assertHtml($expected, $result);
  4055. }
  4056. /**
  4057. * Test default value setting on radio() method
  4058. *
  4059. * @return void
  4060. */
  4061. public function testRadioDefaultValue()
  4062. {
  4063. $Articles = TableRegistry::get('Articles');
  4064. $title = $Articles->schema()->column('title');
  4065. $Articles->schema()->addColumn(
  4066. 'title',
  4067. ['default' => '1'] + $title
  4068. );
  4069. $this->Form->create($Articles->newEntity());
  4070. $result = $this->Form->radio('title', ['option A', 'option B']);
  4071. $expected = [
  4072. ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => '']],
  4073. ['label' => ['for' => 'title-0']],
  4074. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
  4075. 'option A',
  4076. '/label',
  4077. ['label' => ['for' => 'title-1']],
  4078. ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '1', 'id' => 'title-1', 'checked' => 'checked']],
  4079. 'option B',
  4080. '/label',
  4081. ];
  4082. $this->assertHtml($expected, $result);
  4083. }
  4084. /**
  4085. * Test that input works with radio types
  4086. *
  4087. * @return void
  4088. */
  4089. public function testInputRadio()
  4090. {
  4091. $result = $this->Form->input('test', [
  4092. 'type' => 'radio',
  4093. 'options' => ['A', 'B'],
  4094. ]);
  4095. $expected = [
  4096. ['div' => ['class' => 'input radio']],
  4097. '<label',
  4098. 'Test',
  4099. '/label',
  4100. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4101. ['label' => ['for' => 'test-0']],
  4102. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4103. 'A',
  4104. '/label',
  4105. ['label' => ['for' => 'test-1']],
  4106. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4107. 'B',
  4108. '/label',
  4109. '/div',
  4110. ];
  4111. $this->assertHtml($expected, $result);
  4112. $result = $this->Form->input('test', [
  4113. 'type' => 'radio',
  4114. 'options' => ['A', 'B'],
  4115. 'value' => '0'
  4116. ]);
  4117. $expected = [
  4118. ['div' => ['class' => 'input radio']],
  4119. '<label',
  4120. 'Test',
  4121. '/label',
  4122. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4123. ['label' => ['for' => 'test-0']],
  4124. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4125. 'A',
  4126. '/label',
  4127. ['label' => ['for' => 'test-1']],
  4128. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4129. 'B',
  4130. '/label',
  4131. '/div',
  4132. ];
  4133. $this->assertHtml($expected, $result);
  4134. $result = $this->Form->input('test', [
  4135. 'type' => 'radio',
  4136. 'options' => ['A', 'B'],
  4137. 'label' => false
  4138. ]);
  4139. $expected = [
  4140. ['div' => ['class' => 'input radio']],
  4141. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4142. ['label' => ['for' => 'test-0']],
  4143. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4144. 'A',
  4145. '/label',
  4146. ['label' => ['for' => 'test-1']],
  4147. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4148. 'B',
  4149. '/label',
  4150. '/div',
  4151. ];
  4152. $this->assertHtml($expected, $result);
  4153. }
  4154. /**
  4155. * Test that radio() works with label = false
  4156. *
  4157. * @return void
  4158. */
  4159. public function testRadioNoLabel()
  4160. {
  4161. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4162. $expected = [
  4163. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4164. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4165. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4166. ];
  4167. $this->assertHtml($expected, $result);
  4168. }
  4169. /**
  4170. * test generating radio input inside label ala twitter bootstrap
  4171. *
  4172. * @return void
  4173. */
  4174. public function testRadioInputInsideLabel()
  4175. {
  4176. $this->Form->templates([
  4177. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4178. 'radioWrapper' => '{{label}}'
  4179. ]);
  4180. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4181. //@codingStandardsIgnoreStart
  4182. $expected = [
  4183. ['input' => [
  4184. 'type' => 'hidden',
  4185. 'name' => 'Model[field]',
  4186. 'value' => ''
  4187. ]],
  4188. ['label' => ['for' => 'model-field-0']],
  4189. ['input' => [
  4190. 'type' => 'radio',
  4191. 'name' => 'Model[field]',
  4192. 'value' => '0',
  4193. 'id' => 'model-field-0'
  4194. ]],
  4195. 'option A',
  4196. '/label',
  4197. ['label' => ['for' => 'model-field-1']],
  4198. ['input' => [
  4199. 'type' => 'radio',
  4200. 'name' => 'Model[field]',
  4201. 'value' => '1',
  4202. 'id' => 'model-field-1'
  4203. ]],
  4204. 'option B',
  4205. '/label',
  4206. ];
  4207. //@codingStandardsIgnoreEnd
  4208. $this->assertHtml($expected, $result);
  4209. }
  4210. /**
  4211. * test disabling the hidden input for radio buttons
  4212. *
  4213. * @return void
  4214. */
  4215. public function testRadioHiddenInputDisabling()
  4216. {
  4217. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4218. $expected = [
  4219. 'label' => ['for' => 'model-1-field-0'],
  4220. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4221. 'option A',
  4222. '/label'
  4223. ];
  4224. $this->assertHtml($expected, $result);
  4225. }
  4226. /**
  4227. * testRadio method
  4228. *
  4229. * Test radio element set generation
  4230. *
  4231. * @return void
  4232. */
  4233. public function testRadioOutOfRange()
  4234. {
  4235. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4236. $expected = [
  4237. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4238. 'label' => ['for' => 'model-field-v'],
  4239. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4240. 'value',
  4241. '/label'
  4242. ];
  4243. $this->assertHtml($expected, $result);
  4244. }
  4245. /**
  4246. * testSelect method
  4247. *
  4248. * Test select element generation.
  4249. *
  4250. * @return void
  4251. */
  4252. public function testSelect()
  4253. {
  4254. $result = $this->Form->select('Model.field', []);
  4255. $expected = [
  4256. 'select' => ['name' => 'Model[field]'],
  4257. '/select'
  4258. ];
  4259. $this->assertHtml($expected, $result);
  4260. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4261. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4262. $expected = [
  4263. 'select' => ['name' => 'Model[field]'],
  4264. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4265. 'good',
  4266. '/option',
  4267. ['option' => ['value' => 'other']],
  4268. 'bad',
  4269. '/option',
  4270. '/select'
  4271. ];
  4272. $this->assertHtml($expected, $result);
  4273. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4274. $this->assertHtml($expected, $result);
  4275. $this->Form->request->data = [];
  4276. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4277. $expected = [
  4278. 'select' => ['name' => 'Model[field]'],
  4279. ['option' => ['value' => 'value']],
  4280. 'good',
  4281. '/option',
  4282. ['option' => ['value' => 'other']],
  4283. 'bad',
  4284. '/option',
  4285. '/select'
  4286. ];
  4287. $this->assertHtml($expected, $result);
  4288. $options = [
  4289. ['value' => 'first', 'text' => 'First'],
  4290. ['value' => 'first', 'text' => 'Another First'],
  4291. ];
  4292. $result = $this->Form->select(
  4293. 'Model.field',
  4294. $options,
  4295. ['escape' => false, 'empty' => false]
  4296. );
  4297. $expected = [
  4298. 'select' => ['name' => 'Model[field]'],
  4299. ['option' => ['value' => 'first']],
  4300. 'First',
  4301. '/option',
  4302. ['option' => ['value' => 'first']],
  4303. 'Another First',
  4304. '/option',
  4305. '/select'
  4306. ];
  4307. $this->assertHtml($expected, $result);
  4308. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4309. $result = $this->Form->select(
  4310. 'Model.contact_id',
  4311. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4312. ['escape' => false, 'empty' => 'pick something']
  4313. );
  4314. $expected = [
  4315. 'select' => ['name' => 'Model[contact_id]'],
  4316. ['option' => ['value' => '']], 'pick something', '/option',
  4317. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4318. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4319. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4320. '/select'
  4321. ];
  4322. $this->assertHtml($expected, $result);
  4323. $this->Form->request->data['Model']['field'] = 0;
  4324. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4325. $expected = [
  4326. 'select' => ['name' => 'Model[field]'],
  4327. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4328. ['option' => ['value' => '1']], 'Yes', '/option',
  4329. '/select'
  4330. ];
  4331. $this->assertHtml($expected, $result);
  4332. }
  4333. /**
  4334. * Test that select() escapes HTML.
  4335. *
  4336. * @return void
  4337. */
  4338. public function testSelectEscapeHtml()
  4339. {
  4340. $result = $this->Form->select(
  4341. 'Model.field',
  4342. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4343. ['empty' => false]
  4344. );
  4345. $expected = [
  4346. 'select' => ['name' => 'Model[field]'],
  4347. ['option' => ['value' => 'first']],
  4348. 'first &quot;html&quot; &lt;chars&gt;',
  4349. '/option',
  4350. ['option' => ['value' => 'second']],
  4351. 'value',
  4352. '/option',
  4353. '/select'
  4354. ];
  4355. $this->assertHtml($expected, $result);
  4356. $result = $this->Form->select(
  4357. 'Model.field',
  4358. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4359. ['escape' => false, 'empty' => false]
  4360. );
  4361. $expected = [
  4362. 'select' => ['name' => 'Model[field]'],
  4363. ['option' => ['value' => 'first']],
  4364. 'first "html" <chars>',
  4365. '/option',
  4366. ['option' => ['value' => 'second']],
  4367. 'value',
  4368. '/option',
  4369. '/select'
  4370. ];
  4371. $this->assertHtml($expected, $result);
  4372. }
  4373. /**
  4374. * test select() with required and disabled attributes.
  4375. *
  4376. * @return void
  4377. */
  4378. public function testSelectRequired()
  4379. {
  4380. $this->article['required'] = [
  4381. 'user_id' => true
  4382. ];
  4383. $this->Form->create($this->article);
  4384. $result = $this->Form->select('user_id', ['option A']);
  4385. $expected = [
  4386. 'select' => [
  4387. 'name' => 'user_id',
  4388. 'required' => 'required'
  4389. ],
  4390. ['option' => ['value' => '0']], 'option A', '/option',
  4391. '/select'
  4392. ];
  4393. $this->assertHtml($expected, $result);
  4394. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4395. $expected = [
  4396. 'select' => [
  4397. 'name' => 'user_id',
  4398. 'disabled' => 'disabled'
  4399. ],
  4400. ['option' => ['value' => '0']], 'option A', '/option',
  4401. '/select'
  4402. ];
  4403. $this->assertHtml($expected, $result);
  4404. }
  4405. /**
  4406. * testNestedSelect method
  4407. *
  4408. * test select element generation with optgroups
  4409. *
  4410. * @return void
  4411. */
  4412. public function testNestedSelect()
  4413. {
  4414. $result = $this->Form->select(
  4415. 'Model.field',
  4416. [1 => 'One', 2 => 'Two', 'Three' => [
  4417. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4418. ]],
  4419. ['empty' => false]
  4420. );
  4421. $expected = [
  4422. 'select' => ['name' => 'Model[field]'],
  4423. ['option' => ['value' => 1]],
  4424. 'One',
  4425. '/option',
  4426. ['option' => ['value' => 2]],
  4427. 'Two',
  4428. '/option',
  4429. ['optgroup' => ['label' => 'Three']],
  4430. ['option' => ['value' => 3]],
  4431. 'Three',
  4432. '/option',
  4433. ['option' => ['value' => 4]],
  4434. 'Four',
  4435. '/option',
  4436. ['option' => ['value' => 5]],
  4437. 'Five',
  4438. '/option',
  4439. '/optgroup',
  4440. '/select'
  4441. ];
  4442. $this->assertHtml($expected, $result);
  4443. }
  4444. /**
  4445. * testSelectMultiple method
  4446. *
  4447. * test generation of multiple select elements
  4448. *
  4449. * @return void
  4450. */
  4451. public function testSelectMultiple()
  4452. {
  4453. $options = ['first', 'second', 'third'];
  4454. $result = $this->Form->select(
  4455. 'Model.multi_field',
  4456. $options,
  4457. ['form' => 'my-form', 'multiple' => true]
  4458. );
  4459. $expected = [
  4460. 'input' => [
  4461. 'type' => 'hidden',
  4462. 'name' => 'Model[multi_field]',
  4463. 'value' => '',
  4464. 'form' => 'my-form',
  4465. ],
  4466. 'select' => [
  4467. 'name' => 'Model[multi_field][]',
  4468. 'multiple' => 'multiple',
  4469. 'form' => 'my-form',
  4470. ],
  4471. ['option' => ['value' => '0']],
  4472. 'first',
  4473. '/option',
  4474. ['option' => ['value' => '1']],
  4475. 'second',
  4476. '/option',
  4477. ['option' => ['value' => '2']],
  4478. 'third',
  4479. '/option',
  4480. '/select'
  4481. ];
  4482. $this->assertHtml($expected, $result);
  4483. $result = $this->Form->select(
  4484. 'Model.multi_field',
  4485. $options,
  4486. ['multiple' => 'multiple', 'form' => 'my-form']
  4487. );
  4488. $this->assertHtml($expected, $result);
  4489. }
  4490. /**
  4491. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4492. *
  4493. * @return void
  4494. */
  4495. public function testCheckboxZeroValue()
  4496. {
  4497. $result = $this->Form->input('User.get_spam', [
  4498. 'type' => 'checkbox',
  4499. 'value' => '0',
  4500. 'hiddenField' => '1',
  4501. ]);
  4502. $expected = [
  4503. 'div' => ['class' => 'input checkbox'],
  4504. 'label' => ['for' => 'user-get-spam'],
  4505. ['input' => [
  4506. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4507. 'value' => '1'
  4508. ]],
  4509. ['input' => [
  4510. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4511. 'value' => '0', 'id' => 'user-get-spam'
  4512. ]],
  4513. 'Get Spam',
  4514. '/label',
  4515. '/div'
  4516. ];
  4517. $this->assertHtml($expected, $result);
  4518. }
  4519. /**
  4520. * test generation of habtm select boxes.
  4521. *
  4522. * @return void
  4523. */
  4524. public function testHabtmSelectBox()
  4525. {
  4526. $this->loadFixtures('Articles');
  4527. $options = [
  4528. 1 => 'blue',
  4529. 2 => 'red',
  4530. 3 => 'green'
  4531. ];
  4532. $tags = [
  4533. new Entity(['id' => 1, 'name' => 'blue']),
  4534. new Entity(['id' => 3, 'name' => 'green'])
  4535. ];
  4536. $article = new Article(['tags' => $tags]);
  4537. $this->Form->create($article);
  4538. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4539. $expected = [
  4540. 'div' => ['class' => 'input select'],
  4541. 'label' => ['for' => 'tags-ids'],
  4542. 'Tags',
  4543. '/label',
  4544. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4545. 'select' => [
  4546. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4547. 'multiple' => 'multiple'
  4548. ],
  4549. ['option' => ['value' => '1', 'selected' => 'selected']],
  4550. 'blue',
  4551. '/option',
  4552. ['option' => ['value' => '2']],
  4553. 'red',
  4554. '/option',
  4555. ['option' => ['value' => '3', 'selected' => 'selected']],
  4556. 'green',
  4557. '/option',
  4558. '/select',
  4559. '/div'
  4560. ];
  4561. $this->assertHtml($expected, $result);
  4562. // make sure only 50 is selected, and not 50f5c0cf
  4563. $options = [
  4564. '1' => 'blue',
  4565. '50f5c0cf' => 'red',
  4566. '50' => 'green'
  4567. ];
  4568. $tags = [
  4569. new Entity(['id' => 1, 'name' => 'blue']),
  4570. new Entity(['id' => 50, 'name' => 'green'])
  4571. ];
  4572. $article = new Article(['tags' => $tags]);
  4573. $this->Form->create($article);
  4574. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4575. $expected = [
  4576. 'div' => ['class' => 'input select'],
  4577. 'label' => ['for' => 'tags-ids'],
  4578. 'Tags',
  4579. '/label',
  4580. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4581. 'select' => [
  4582. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4583. 'multiple' => 'multiple'
  4584. ],
  4585. ['option' => ['value' => '1', 'selected' => 'selected']],
  4586. 'blue',
  4587. '/option',
  4588. ['option' => ['value' => '50f5c0cf']],
  4589. 'red',
  4590. '/option',
  4591. ['option' => ['value' => '50', 'selected' => 'selected']],
  4592. 'green',
  4593. '/option',
  4594. '/select',
  4595. '/div'
  4596. ];
  4597. $this->assertHtml($expected, $result);
  4598. $spacecraft = [
  4599. 1 => 'Orion',
  4600. 2 => 'Helios'
  4601. ];
  4602. $this->View->viewVars['spacecraft'] = $spacecraft;
  4603. $this->Form->create();
  4604. $result = $this->Form->input('spacecraft._ids');
  4605. $expected = [
  4606. 'div' => ['class' => 'input select'],
  4607. 'label' => ['for' => 'spacecraft-ids'],
  4608. 'Spacecraft',
  4609. '/label',
  4610. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4611. 'select' => [
  4612. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4613. 'multiple' => 'multiple'
  4614. ],
  4615. ['option' => ['value' => '1']],
  4616. 'Orion',
  4617. '/option',
  4618. ['option' => ['value' => '2']],
  4619. 'Helios',
  4620. '/option',
  4621. '/select',
  4622. '/div'
  4623. ];
  4624. $this->assertHtml($expected, $result);
  4625. }
  4626. /**
  4627. * Tests that errors for belongsToMany select fields are being
  4628. * picked up properly.
  4629. *
  4630. * @return void
  4631. */
  4632. public function testErrorsForBelongsToManySelect()
  4633. {
  4634. $spacecraft = [
  4635. 1 => 'Orion',
  4636. 2 => 'Helios'
  4637. ];
  4638. $this->View->viewVars['spacecraft'] = $spacecraft;
  4639. $article = new Article();
  4640. $article->errors('spacecraft', ['Invalid']);
  4641. $this->Form->create($article);
  4642. $result = $this->Form->input('spacecraft._ids');
  4643. $expected = [
  4644. ['div' => ['class' => 'input select error']],
  4645. 'label' => ['for' => 'spacecraft-ids'],
  4646. 'Spacecraft',
  4647. '/label',
  4648. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4649. 'select' => [
  4650. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4651. 'multiple' => 'multiple'
  4652. ],
  4653. ['option' => ['value' => '1']],
  4654. 'Orion',
  4655. '/option',
  4656. ['option' => ['value' => '2']],
  4657. 'Helios',
  4658. '/option',
  4659. '/select',
  4660. ['div' => ['class' => 'error-message']],
  4661. 'Invalid',
  4662. '/div',
  4663. '/div'
  4664. ];
  4665. $this->assertHtml($expected, $result);
  4666. }
  4667. /**
  4668. * test generation of multi select elements in checkbox format
  4669. *
  4670. * @return void
  4671. */
  4672. public function testSelectMultipleCheckboxes()
  4673. {
  4674. $result = $this->Form->select(
  4675. 'Model.multi_field',
  4676. ['first', 'second', 'third'],
  4677. ['multiple' => 'checkbox']
  4678. );
  4679. $expected = [
  4680. 'input' => [
  4681. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4682. ],
  4683. ['div' => ['class' => 'checkbox']],
  4684. ['label' => ['for' => 'model-multi-field-0']],
  4685. ['input' => [
  4686. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4687. 'value' => '0', 'id' => 'model-multi-field-0'
  4688. ]],
  4689. 'first',
  4690. '/label',
  4691. '/div',
  4692. ['div' => ['class' => 'checkbox']],
  4693. ['label' => ['for' => 'model-multi-field-1']],
  4694. ['input' => [
  4695. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4696. 'value' => '1', 'id' => 'model-multi-field-1'
  4697. ]],
  4698. 'second',
  4699. '/label',
  4700. '/div',
  4701. ['div' => ['class' => 'checkbox']],
  4702. ['label' => ['for' => 'model-multi-field-2']],
  4703. ['input' => [
  4704. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4705. 'value' => '2', 'id' => 'model-multi-field-2'
  4706. ]],
  4707. 'third',
  4708. '/label',
  4709. '/div'
  4710. ];
  4711. $this->assertHtml($expected, $result);
  4712. $result = $this->Form->select(
  4713. 'Model.multi_field',
  4714. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4715. ['multiple' => 'checkbox']
  4716. );
  4717. $expected = [
  4718. 'input' => [
  4719. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4720. ],
  4721. ['div' => ['class' => 'checkbox']],
  4722. ['label' => ['for' => 'model-multi-field-a+']],
  4723. ['input' => [
  4724. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4725. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4726. ]],
  4727. 'first',
  4728. '/label',
  4729. '/div',
  4730. ['div' => ['class' => 'checkbox']],
  4731. ['label' => ['for' => 'model-multi-field-a++']],
  4732. ['input' => [
  4733. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4734. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4735. ]],
  4736. 'second',
  4737. '/label',
  4738. '/div',
  4739. ['div' => ['class' => 'checkbox']],
  4740. ['label' => ['for' => 'model-multi-field-a+++']],
  4741. ['input' => [
  4742. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4743. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4744. ]],
  4745. 'third',
  4746. '/label',
  4747. '/div'
  4748. ];
  4749. $this->assertHtml($expected, $result);
  4750. $result = $this->Form->select(
  4751. 'Model.multi_field',
  4752. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4753. ['multiple' => 'checkbox']
  4754. );
  4755. $expected = [
  4756. 'input' => [
  4757. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4758. ],
  4759. ['div' => ['class' => 'checkbox']],
  4760. ['label' => ['for' => 'model-multi-field-a-b']],
  4761. ['input' => [
  4762. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4763. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4764. ]],
  4765. 'first',
  4766. '/label',
  4767. '/div',
  4768. ['div' => ['class' => 'checkbox']],
  4769. ['label' => ['for' => 'model-multi-field-a-b1']],
  4770. ['input' => [
  4771. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4772. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4773. ]],
  4774. 'second',
  4775. '/label',
  4776. '/div',
  4777. ['div' => ['class' => 'checkbox']],
  4778. ['label' => ['for' => 'model-multi-field-a-b2']],
  4779. ['input' => [
  4780. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4781. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4782. ]],
  4783. 'third',
  4784. '/label',
  4785. '/div'
  4786. ];
  4787. $this->assertHtml($expected, $result);
  4788. }
  4789. /**
  4790. * Ensure that multiCheckbox reads from the request data.
  4791. *
  4792. * @return void
  4793. */
  4794. public function testSelectMultipleCheckboxRequestData()
  4795. {
  4796. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  4797. $result = $this->Form->select(
  4798. 'Model.tags',
  4799. ['1' => 'first', 'Array' => 'Array'],
  4800. ['multiple' => 'checkbox']
  4801. );
  4802. $expected = [
  4803. 'input' => [
  4804. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4805. ],
  4806. ['div' => ['class' => 'checkbox']],
  4807. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4808. ['input' => [
  4809. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4810. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4811. ]],
  4812. 'first',
  4813. '/label',
  4814. '/div',
  4815. ['div' => ['class' => 'checkbox']],
  4816. ['label' => ['for' => 'model-tags-array']],
  4817. ['input' => [
  4818. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4819. 'value' => 'Array', 'id' => 'model-tags-array'
  4820. ]],
  4821. 'Array',
  4822. '/label',
  4823. '/div'
  4824. ];
  4825. $this->assertHtml($expected, $result);
  4826. }
  4827. /**
  4828. * Checks the security hash array generated for multiple-input checkbox elements
  4829. *
  4830. * @return void
  4831. */
  4832. public function testSelectMultipleCheckboxSecurity()
  4833. {
  4834. $this->Form->request->params['_Token'] = 'testKey';
  4835. $this->assertEquals([], $this->Form->fields);
  4836. $result = $this->Form->select(
  4837. 'Model.multi_field',
  4838. ['1' => 'first', '2' => 'second', '3' => 'third'],
  4839. ['multiple' => 'checkbox']
  4840. );
  4841. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  4842. $result = $this->Form->secure($this->Form->fields);
  4843. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4844. $this->assertRegExp('/"' . $key . '"/', $result);
  4845. }
  4846. /**
  4847. * Multiple select elements should always be secured as they always participate
  4848. * in the POST data.
  4849. *
  4850. * @return void
  4851. */
  4852. public function testSelectMultipleSecureWithNoOptions()
  4853. {
  4854. $this->assertEquals([], $this->Form->fields);
  4855. $this->Form->select(
  4856. 'Model.select',
  4857. [],
  4858. ['multiple' => true]
  4859. );
  4860. $this->assertEquals(['Model.select'], $this->Form->fields);
  4861. }
  4862. /**
  4863. * When a select box has no options it should not be added to the fields list
  4864. * as it always fail post validation.
  4865. *
  4866. * @return void
  4867. */
  4868. public function testSelectNoSecureWithNoOptions()
  4869. {
  4870. $this->Form->request->params['_Token'] = 'testkey';
  4871. $this->assertEquals([], $this->Form->fields);
  4872. $this->Form->select(
  4873. 'Model.select',
  4874. []
  4875. );
  4876. $this->assertEquals([], $this->Form->fields);
  4877. $this->Form->select(
  4878. 'Model.user_id',
  4879. [],
  4880. ['empty' => true]
  4881. );
  4882. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  4883. }
  4884. /**
  4885. * testInputMultipleCheckboxes method
  4886. *
  4887. * test input() resulting in multi select elements being generated.
  4888. *
  4889. * @return void
  4890. */
  4891. public function testInputMultipleCheckboxes()
  4892. {
  4893. $result = $this->Form->input('Model.multi_field', [
  4894. 'options' => ['first', 'second', 'third'],
  4895. 'multiple' => 'checkbox'
  4896. ]);
  4897. $expected = [
  4898. ['div' => ['class' => 'input select']],
  4899. ['label' => ['for' => 'model-multi-field']],
  4900. 'Multi Field',
  4901. '/label',
  4902. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  4903. ['div' => ['class' => 'checkbox']],
  4904. ['label' => ['for' => 'model-multi-field-0']],
  4905. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  4906. 'first',
  4907. '/label',
  4908. '/div',
  4909. ['div' => ['class' => 'checkbox']],
  4910. ['label' => ['for' => 'model-multi-field-1']],
  4911. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  4912. 'second',
  4913. '/label',
  4914. '/div',
  4915. ['div' => ['class' => 'checkbox']],
  4916. ['label' => ['for' => 'model-multi-field-2']],
  4917. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  4918. 'third',
  4919. '/label',
  4920. '/div',
  4921. '/div'
  4922. ];
  4923. $this->assertHtml($expected, $result);
  4924. $result = $this->Form->input('Model.multi_field', [
  4925. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  4926. 'multiple' => 'checkbox'
  4927. ]);
  4928. $expected = [
  4929. ['div' => ['class' => 'input select']],
  4930. ['label' => ['for' => 'model-multi-field']],
  4931. 'Multi Field',
  4932. '/label',
  4933. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  4934. ['div' => ['class' => 'checkbox']],
  4935. ['label' => ['for' => 'model-multi-field-a']],
  4936. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  4937. 'first',
  4938. '/label',
  4939. '/div',
  4940. ['div' => ['class' => 'checkbox']],
  4941. ['label' => ['for' => 'model-multi-field-b']],
  4942. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  4943. 'second',
  4944. '/label',
  4945. '/div',
  4946. ['div' => ['class' => 'checkbox']],
  4947. ['label' => ['for' => 'model-multi-field-c']],
  4948. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  4949. 'third',
  4950. '/label',
  4951. '/div',
  4952. '/div'
  4953. ];
  4954. $this->assertHtml($expected, $result);
  4955. }
  4956. /**
  4957. * testSelectHiddenFieldOmission method
  4958. *
  4959. * test that select() with 'hiddenField' => false omits the hidden field
  4960. *
  4961. * @return void
  4962. */
  4963. public function testSelectHiddenFieldOmission()
  4964. {
  4965. $result = $this->Form->select(
  4966. 'Model.multi_field',
  4967. ['first', 'second'],
  4968. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  4969. );
  4970. $this->assertNotContains('type="hidden"', $result);
  4971. }
  4972. /**
  4973. * test that select() with multiple = checkbox works with overriding name attribute.
  4974. *
  4975. * @return void
  4976. */
  4977. public function testSelectCheckboxMultipleOverrideName()
  4978. {
  4979. $result = $this->Form->select('category', ['1', '2'], [
  4980. 'multiple' => 'checkbox',
  4981. 'name' => 'fish',
  4982. ]);
  4983. $expected = [
  4984. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  4985. ['div' => ['class' => 'checkbox']],
  4986. ['label' => ['for' => 'fish-0']],
  4987. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  4988. '1',
  4989. '/label',
  4990. '/div',
  4991. ['div' => ['class' => 'checkbox']],
  4992. ['label' => ['for' => 'fish-1']],
  4993. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  4994. '2',
  4995. '/label',
  4996. '/div'
  4997. ];
  4998. $this->assertHtml($expected, $result);
  4999. $result = $this->Form->multiCheckbox(
  5000. 'category',
  5001. new Collection(['1', '2']),
  5002. ['name' => 'fish']
  5003. );
  5004. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  5005. 'name' => 'fish',
  5006. ]);
  5007. $this->assertHtml($expected, $result);
  5008. }
  5009. /**
  5010. * Test that input() works with multicheckbox.
  5011. *
  5012. * @return void
  5013. */
  5014. public function testInputMultiCheckbox()
  5015. {
  5016. $result = $this->Form->input('category', [
  5017. 'type' => 'multicheckbox',
  5018. 'options' => ['1', '2'],
  5019. ]);
  5020. $expected = [
  5021. ['div' => ['class' => 'input multicheckbox']],
  5022. '<label',
  5023. 'Category',
  5024. '/label',
  5025. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  5026. ['div' => ['class' => 'checkbox']],
  5027. ['label' => ['for' => 'category-0']],
  5028. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  5029. '1',
  5030. '/label',
  5031. '/div',
  5032. ['div' => ['class' => 'checkbox']],
  5033. ['label' => ['for' => 'category-1']],
  5034. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  5035. '2',
  5036. '/label',
  5037. '/div',
  5038. '/div',
  5039. ];
  5040. $this->assertHtml($expected, $result);
  5041. }
  5042. /**
  5043. * testCheckbox method
  5044. *
  5045. * Test generation of checkboxes
  5046. *
  5047. * @return void
  5048. */
  5049. public function testCheckbox()
  5050. {
  5051. $result = $this->Form->checkbox('Model.field');
  5052. $expected = [
  5053. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  5054. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  5055. ];
  5056. $this->assertHtml($expected, $result);
  5057. $result = $this->Form->checkbox('Model.field', [
  5058. 'id' => 'theID',
  5059. 'value' => 'myvalue',
  5060. 'form' => 'my-form',
  5061. ]);
  5062. $expected = [
  5063. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  5064. ['input' => [
  5065. 'type' => 'checkbox', 'name' => 'Model[field]',
  5066. 'value' => 'myvalue', 'id' => 'theID',
  5067. 'form' => 'my-form',
  5068. ]]
  5069. ];
  5070. $this->assertHtml($expected, $result);
  5071. }
  5072. /**
  5073. * testCheckboxDefaultValue method
  5074. *
  5075. * Test default value setting on checkbox() method
  5076. *
  5077. * @return void
  5078. */
  5079. public function testCheckboxDefaultValue()
  5080. {
  5081. $this->Form->request->data['Model']['field'] = false;
  5082. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5083. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5084. $this->assertHtml($expected, $result);
  5085. unset($this->Form->request->data['Model']['field']);
  5086. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  5087. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5088. $this->assertHtml($expected, $result);
  5089. $this->Form->request->data['Model']['field'] = true;
  5090. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5091. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  5092. $this->assertHtml($expected, $result);
  5093. unset($this->Form->request->data['Model']['field']);
  5094. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  5095. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  5096. $this->assertHtml($expected, $result);
  5097. $Articles = TableRegistry::get('Articles');
  5098. $Articles->schema()->addColumn(
  5099. 'published',
  5100. ['type' => 'boolean', 'null' => false, 'default' => true]
  5101. );
  5102. $this->Form->create($Articles->newEntity());
  5103. $result = $this->Form->checkbox('published', ['hiddenField' => false]);
  5104. $expected = ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'checked' => 'checked']];
  5105. $this->assertHtml($expected, $result);
  5106. }
  5107. /**
  5108. * Test checkbox being checked or having errors.
  5109. *
  5110. * @return void
  5111. */
  5112. public function testCheckboxCheckedAndError()
  5113. {
  5114. $this->article['errors'] = [
  5115. 'published' => true
  5116. ];
  5117. $this->Form->request->data['published'] = 'myvalue';
  5118. $this->Form->create($this->article);
  5119. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5120. $expected = [
  5121. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5122. ['input' => [
  5123. 'type' => 'checkbox',
  5124. 'name' => 'published',
  5125. 'value' => 'myvalue',
  5126. 'id' => 'theID',
  5127. 'checked' => 'checked',
  5128. 'class' => 'form-error'
  5129. ]]
  5130. ];
  5131. $this->assertHtml($expected, $result);
  5132. $this->Form->request->data['published'] = '';
  5133. $result = $this->Form->checkbox('published');
  5134. $expected = [
  5135. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5136. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5137. ];
  5138. $this->assertHtml($expected, $result);
  5139. }
  5140. /**
  5141. * test checkbox() with a custom name attribute
  5142. *
  5143. * @return void
  5144. */
  5145. public function testCheckboxCustomNameAttribute()
  5146. {
  5147. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5148. $expected = [
  5149. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5150. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5151. ];
  5152. $this->assertHtml($expected, $result);
  5153. }
  5154. /**
  5155. * Test that the hidden input for checkboxes can be omitted or set to a
  5156. * specific value.
  5157. *
  5158. * @return void
  5159. */
  5160. public function testCheckboxHiddenField()
  5161. {
  5162. $result = $this->Form->checkbox('UserForm.something', [
  5163. 'hiddenField' => false
  5164. ]);
  5165. $expected = [
  5166. 'input' => [
  5167. 'type' => 'checkbox',
  5168. 'name' => 'UserForm[something]',
  5169. 'value' => '1'
  5170. ],
  5171. ];
  5172. $this->assertHtml($expected, $result);
  5173. $result = $this->Form->checkbox('UserForm.something', [
  5174. 'value' => 'Y',
  5175. 'hiddenField' => 'N',
  5176. ]);
  5177. $expected = [
  5178. ['input' => [
  5179. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5180. 'value' => 'N'
  5181. ]],
  5182. ['input' => [
  5183. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5184. 'value' => 'Y'
  5185. ]],
  5186. ];
  5187. $this->assertHtml($expected, $result);
  5188. }
  5189. /**
  5190. * Test the time type.
  5191. *
  5192. * @return void
  5193. */
  5194. public function testTime()
  5195. {
  5196. $result = $this->Form->time('start_time', [
  5197. 'timeFormat' => 12,
  5198. 'interval' => 5,
  5199. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5200. ]);
  5201. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5202. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5203. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5204. $this->assertNotContains('year', $result);
  5205. $this->assertNotContains('month', $result);
  5206. $this->assertNotContains('day', $result);
  5207. $result = $this->Form->time('start_time', [
  5208. 'timeFormat' => 12,
  5209. 'interval' => 5,
  5210. 'value' => '2014-03-08 16:30:00'
  5211. ]);
  5212. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5213. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5214. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5215. $this->assertNotContains('year', $result);
  5216. $this->assertNotContains('month', $result);
  5217. $this->assertNotContains('day', $result);
  5218. }
  5219. /**
  5220. * Ensure that timeFormat=24 has no merdian.
  5221. *
  5222. * @return void.
  5223. */
  5224. public function testTimeFormat24NoMeridian()
  5225. {
  5226. $result = $this->Form->time('start_time', [
  5227. 'timeFormat' => 24,
  5228. 'interval' => 5,
  5229. 'value' => '2014-03-08 16:30:00'
  5230. ]);
  5231. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5232. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5233. $this->assertNotContains('meridian', $result);
  5234. $this->assertNotContains('pm', $result);
  5235. $this->assertNotContains('year', $result);
  5236. $this->assertNotContains('month', $result);
  5237. $this->assertNotContains('day', $result);
  5238. }
  5239. /**
  5240. * Test the date type.
  5241. *
  5242. * @return void
  5243. */
  5244. public function testDate()
  5245. {
  5246. $result = $this->Form->date('start_day', [
  5247. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5248. ]);
  5249. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5250. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5251. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5252. $this->assertNotContains('hour', $result);
  5253. $this->assertNotContains('minute', $result);
  5254. $this->assertNotContains('second', $result);
  5255. $this->assertNotContains('meridian', $result);
  5256. }
  5257. /**
  5258. * testDateTime method
  5259. *
  5260. * Test generation of date/time select elements
  5261. *
  5262. * @return void
  5263. */
  5264. public function testDateTime()
  5265. {
  5266. extract($this->dateRegex);
  5267. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5268. $now = strtotime('now');
  5269. $expected = [
  5270. ['select' => ['name' => 'Contact[date][year]']],
  5271. ['option' => ['value' => '']],
  5272. '/option',
  5273. $yearsRegex,
  5274. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5275. date('Y', $now),
  5276. '/option',
  5277. '*/select',
  5278. ['select' => ['name' => 'Contact[date][month]']],
  5279. ['option' => ['value' => '']],
  5280. '/option',
  5281. $monthsRegex,
  5282. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5283. date('F', $now),
  5284. '/option',
  5285. '*/select',
  5286. ['select' => ['name' => 'Contact[date][day]']],
  5287. ['option' => ['value' => '']],
  5288. '/option',
  5289. $daysRegex,
  5290. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5291. date('j', $now),
  5292. '/option',
  5293. '*/select',
  5294. ['select' => ['name' => 'Contact[date][hour]']],
  5295. ['option' => ['value' => '']],
  5296. '/option',
  5297. $hoursRegex,
  5298. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5299. date('G', $now),
  5300. '/option',
  5301. '*/select',
  5302. ['select' => ['name' => 'Contact[date][minute]']],
  5303. ['option' => ['value' => '']],
  5304. '/option',
  5305. $minutesRegex,
  5306. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5307. date('i', $now),
  5308. '/option',
  5309. '*/select',
  5310. ];
  5311. $this->assertHtml($expected, $result);
  5312. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5313. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5314. $now = strtotime('now');
  5315. $expected = [
  5316. ['select' => ['name' => 'Contact[date][year]']],
  5317. $yearsRegex,
  5318. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5319. date('Y', $now),
  5320. '/option',
  5321. '*/select',
  5322. ['select' => ['name' => 'Contact[date][month]']],
  5323. $monthsRegex,
  5324. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5325. date('F', $now),
  5326. '/option',
  5327. '*/select',
  5328. ['select' => ['name' => 'Contact[date][day]']],
  5329. $daysRegex,
  5330. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5331. date('j', $now),
  5332. '/option',
  5333. '*/select',
  5334. ['select' => ['name' => 'Contact[date][hour]']],
  5335. $hoursRegex,
  5336. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5337. date('G', $now),
  5338. '/option',
  5339. '*/select',
  5340. ['select' => ['name' => 'Contact[date][minute]']],
  5341. $minutesRegex,
  5342. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5343. date('i', $now),
  5344. '/option',
  5345. '*/select',
  5346. ];
  5347. $this->assertHtml($expected, $result);
  5348. }
  5349. /**
  5350. * Test that datetime fields are added to protected fields list.
  5351. *
  5352. * @return void
  5353. */
  5354. public function testDateTimeSecured()
  5355. {
  5356. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5357. $this->Form->dateTime('Contact.date');
  5358. $expected = [
  5359. 'Contact.date.year',
  5360. 'Contact.date.month',
  5361. 'Contact.date.day',
  5362. 'Contact.date.hour',
  5363. 'Contact.date.minute',
  5364. ];
  5365. $this->assertEquals($expected, $this->Form->fields);
  5366. $this->Form->fields = [];
  5367. $this->Form->date('Contact.published');
  5368. $expected = [
  5369. 'Contact.published.year',
  5370. 'Contact.published.month',
  5371. 'Contact.published.day',
  5372. ];
  5373. $this->assertEquals($expected, $this->Form->fields);
  5374. }
  5375. /**
  5376. * Test that datetime fields are added to protected fields list.
  5377. *
  5378. * @return void
  5379. */
  5380. public function testDateTimeSecuredDisabled()
  5381. {
  5382. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5383. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5384. $expected = [];
  5385. $this->assertEquals($expected, $this->Form->fields);
  5386. $this->Form->fields = [];
  5387. $this->Form->date('Contact.published', ['secure' => false]);
  5388. $expected = [];
  5389. $this->assertEquals($expected, $this->Form->fields);
  5390. }
  5391. /**
  5392. * Test empty defaulting to true for datetime.
  5393. *
  5394. * @return void
  5395. */
  5396. public function testDatetimeEmpty()
  5397. {
  5398. extract($this->dateRegex);
  5399. $now = strtotime('now');
  5400. $result = $this->Form->dateTime('Contact.date', [
  5401. 'timeFormat' => 12,
  5402. 'empty' => true,
  5403. 'default' => true
  5404. ]);
  5405. $expected = [
  5406. ['select' => ['name' => 'Contact[date][year]']],
  5407. $yearsRegex,
  5408. ['option' => ['value' => '']],
  5409. '/option',
  5410. '*/select',
  5411. ['select' => ['name' => 'Contact[date][month]']],
  5412. $monthsRegex,
  5413. ['option' => ['value' => '']],
  5414. '/option',
  5415. '*/select',
  5416. ['select' => ['name' => 'Contact[date][day]']],
  5417. $daysRegex,
  5418. ['option' => ['value' => '']],
  5419. '/option',
  5420. '*/select',
  5421. ['select' => ['name' => 'Contact[date][hour]']],
  5422. $hoursRegex,
  5423. ['option' => ['value' => '']],
  5424. '/option',
  5425. '*/select',
  5426. ['select' => ['name' => 'Contact[date][minute]']],
  5427. $minutesRegex,
  5428. ['option' => ['value' => '']],
  5429. '/option',
  5430. '*/select',
  5431. ['select' => ['name' => 'Contact[date][meridian]']],
  5432. $meridianRegex,
  5433. ['option' => ['value' => '']],
  5434. '/option',
  5435. '*/select'
  5436. ];
  5437. $this->assertHtml($expected, $result);
  5438. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5439. }
  5440. /**
  5441. * Test datetime with interval option.
  5442. *
  5443. * @return void
  5444. */
  5445. public function testDatetimeMinuteInterval()
  5446. {
  5447. extract($this->dateRegex);
  5448. $now = strtotime('now');
  5449. $result = $this->Form->dateTime('Contact.date', [
  5450. 'interval' => 5,
  5451. 'value' => ''
  5452. ]);
  5453. $expected = [
  5454. ['select' => ['name' => 'Contact[date][year]']],
  5455. $yearsRegex,
  5456. ['option' => ['selected' => 'selected', 'value' => '']],
  5457. '/option',
  5458. '*/select',
  5459. ['select' => ['name' => 'Contact[date][month]']],
  5460. $monthsRegex,
  5461. ['option' => ['selected' => 'selected', 'value' => '']],
  5462. '/option',
  5463. '*/select',
  5464. ['select' => ['name' => 'Contact[date][day]']],
  5465. $daysRegex,
  5466. ['option' => ['selected' => 'selected', 'value' => '']],
  5467. '/option',
  5468. '*/select',
  5469. ['select' => ['name' => 'Contact[date][hour]']],
  5470. $hoursRegex,
  5471. ['option' => ['selected' => 'selected', 'value' => '']],
  5472. '/option',
  5473. '*/select',
  5474. ['select' => ['name' => 'Contact[date][minute]']],
  5475. $minutesRegex,
  5476. ['option' => ['selected' => 'selected', 'value' => '']],
  5477. '/option',
  5478. ['option' => ['value' => '00']],
  5479. '00',
  5480. '/option',
  5481. ['option' => ['value' => '05']],
  5482. '05',
  5483. '/option',
  5484. ['option' => ['value' => '10']],
  5485. '10',
  5486. '/option',
  5487. '*/select',
  5488. ];
  5489. $this->assertHtml($expected, $result);
  5490. }
  5491. /**
  5492. * Test dateTime with rounding
  5493. *
  5494. * @return void
  5495. */
  5496. public function testDateTimeRounding()
  5497. {
  5498. $this->Form->request->data['Contact'] = [
  5499. 'date' => [
  5500. 'day' => '13',
  5501. 'month' => '12',
  5502. 'year' => '2010',
  5503. 'hour' => '04',
  5504. 'minute' => '19',
  5505. 'meridian' => 'AM'
  5506. ]
  5507. ];
  5508. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5509. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5510. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5511. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5512. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5513. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5514. }
  5515. /**
  5516. * test that datetime() and default values work.
  5517. *
  5518. * @return void
  5519. */
  5520. public function testDatetimeWithDefault()
  5521. {
  5522. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5523. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5524. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5525. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5526. $result = $this->Form->dateTime('Contact.updated', [
  5527. 'default' => '2009-06-01 11:15:30'
  5528. ]);
  5529. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5530. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5531. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5532. }
  5533. /**
  5534. * testDateTime all zeros
  5535. *
  5536. * @return void
  5537. */
  5538. public function testDateTimeAllZeros()
  5539. {
  5540. $result = $this->Form->dateTime('Contact.date', [
  5541. 'timeFormat' => false,
  5542. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5543. 'value' => '0000-00-00'
  5544. ]);
  5545. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5546. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5547. }
  5548. /**
  5549. * testDateTimeEmptyAsArray
  5550. *
  5551. * @return void
  5552. */
  5553. public function testDateTimeEmptyAsArray()
  5554. {
  5555. $result = $this->Form->dateTime('Contact.date', [
  5556. 'empty' => [
  5557. 'day' => 'DAY',
  5558. 'month' => 'MONTH',
  5559. 'year' => 'YEAR',
  5560. 'hour' => 'HOUR',
  5561. 'minute' => 'MINUTE',
  5562. 'meridian' => false
  5563. ],
  5564. 'default' => true
  5565. ]);
  5566. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5567. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5568. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5569. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5570. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5571. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5572. $result = $this->Form->dateTime('Contact.date', [
  5573. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5574. 'default' => true
  5575. ]);
  5576. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5577. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5578. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5579. }
  5580. /**
  5581. * testFormDateTimeMulti method
  5582. *
  5583. * test multiple datetime element generation
  5584. *
  5585. * @return void
  5586. */
  5587. public function testFormDateTimeMulti()
  5588. {
  5589. extract($this->dateRegex);
  5590. $result = $this->Form->dateTime('Contact.1.updated');
  5591. $this->assertContains('Contact[1][updated][month]', $result);
  5592. $this->assertContains('Contact[1][updated][day]', $result);
  5593. $this->assertContains('Contact[1][updated][year]', $result);
  5594. $this->assertContains('Contact[1][updated][hour]', $result);
  5595. $this->assertContains('Contact[1][updated][minute]', $result);
  5596. }
  5597. /**
  5598. * When changing the date format, the label should always focus the first select box when
  5599. * clicked.
  5600. *
  5601. * @return void
  5602. */
  5603. public function testDateTimeLabelIdMatchesFirstInput()
  5604. {
  5605. $result = $this->Form->input('Model.date', ['type' => 'date']);
  5606. $this->assertContains('<label>Date</label>', $result);
  5607. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5608. $this->assertContains('<label>Date</label>', $result);
  5609. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5610. $this->assertContains('<label>Date</label>', $result);
  5611. }
  5612. /**
  5613. * test datetime second=true
  5614. *
  5615. * @return void
  5616. */
  5617. public function testDateTimeSecondOptions()
  5618. {
  5619. $result = $this->Form->dateTime('updated', ['second' => true]);
  5620. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5621. $result = $this->Form->dateTime('updated', ['second' => []]);
  5622. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5623. $result = $this->Form->dateTime('updated', ['second' => null]);
  5624. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5625. $result = $this->Form->dateTime('updated', ['second' => false]);
  5626. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5627. }
  5628. /**
  5629. * testMonth method
  5630. *
  5631. * @return void
  5632. */
  5633. public function testMonth()
  5634. {
  5635. $result = $this->Form->month('Model.field', ['value' => '']);
  5636. $expected = [
  5637. ['select' => ['name' => 'Model[field][month]']],
  5638. ['option' => ['value' => '', 'selected' => 'selected']],
  5639. '/option',
  5640. ['option' => ['value' => '01']],
  5641. date('F', strtotime('2008-01-01 00:00:00')),
  5642. '/option',
  5643. ['option' => ['value' => '02']],
  5644. date('F', strtotime('2008-02-01 00:00:00')),
  5645. '/option',
  5646. '*/select',
  5647. ];
  5648. $this->assertHtml($expected, $result);
  5649. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5650. $expected = [
  5651. ['select' => ['name' => 'Model[field][month]']],
  5652. ['option' => ['selected' => 'selected', 'value' => '']],
  5653. '/option',
  5654. ['option' => ['value' => '01']],
  5655. date('F', strtotime('2008-01-01 00:00:00')),
  5656. '/option',
  5657. ['option' => ['value' => '02']],
  5658. date('F', strtotime('2008-02-01 00:00:00')),
  5659. '/option',
  5660. '*/select',
  5661. ];
  5662. $this->assertHtml($expected, $result);
  5663. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5664. $expected = [
  5665. ['select' => ['name' => 'Model[field][month]']],
  5666. ['option' => ['selected' => 'selected', 'value' => '']],
  5667. '/option',
  5668. ['option' => ['value' => '01']],
  5669. '1',
  5670. '/option',
  5671. ['option' => ['value' => '02']],
  5672. '2',
  5673. '/option',
  5674. '*/select',
  5675. ];
  5676. $this->assertHtml($expected, $result);
  5677. $monthNames = [
  5678. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5679. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5680. ];
  5681. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5682. $expected = [
  5683. ['select' => ['name' => 'Model[field][month]']],
  5684. ['option' => ['value' => '']],
  5685. '/option',
  5686. ['option' => ['value' => '01', 'selected' => 'selected']],
  5687. 'Jan',
  5688. '/option',
  5689. ['option' => ['value' => '02']],
  5690. 'Feb',
  5691. '/option',
  5692. '*/select',
  5693. ];
  5694. $this->assertHtml($expected, $result);
  5695. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5696. $result = $this->Form->month('Project.release');
  5697. $expected = [
  5698. ['select' => ['name' => 'Project[release][month]']],
  5699. ['option' => ['value' => '']],
  5700. '/option',
  5701. ['option' => ['value' => '01']],
  5702. 'January',
  5703. '/option',
  5704. ['option' => ['value' => '02', 'selected' => 'selected']],
  5705. 'February',
  5706. '/option',
  5707. '*/select',
  5708. ];
  5709. $this->assertHtml($expected, $result);
  5710. $result = $this->Form->month('Contact.published', [
  5711. 'empty' => 'Published on',
  5712. ]);
  5713. $this->assertContains('Published on', $result);
  5714. }
  5715. /**
  5716. * testDay method
  5717. *
  5718. * @return void
  5719. */
  5720. public function testDay()
  5721. {
  5722. extract($this->dateRegex);
  5723. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  5724. $expected = [
  5725. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  5726. ['option' => ['selected' => 'selected', 'value' => '']],
  5727. '/option',
  5728. ['option' => ['value' => '01']],
  5729. '1',
  5730. '/option',
  5731. ['option' => ['value' => '02']],
  5732. '2',
  5733. '/option',
  5734. $daysRegex,
  5735. '/select',
  5736. ];
  5737. $this->assertHtml($expected, $result);
  5738. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5739. $result = $this->Form->day('Model.field');
  5740. $expected = [
  5741. ['select' => ['name' => 'Model[field][day]']],
  5742. ['option' => ['value' => '']],
  5743. '/option',
  5744. ['option' => ['value' => '01']],
  5745. '1',
  5746. '/option',
  5747. ['option' => ['value' => '02']],
  5748. '2',
  5749. '/option',
  5750. $daysRegex,
  5751. ['option' => ['value' => '10', 'selected' => 'selected']],
  5752. '10',
  5753. '/option',
  5754. $daysRegex,
  5755. '/select',
  5756. ];
  5757. $this->assertHtml($expected, $result);
  5758. $this->Form->request->data['Model']['field'] = '';
  5759. $result = $this->Form->day('Model.field', ['value' => '10']);
  5760. $expected = [
  5761. ['select' => ['name' => 'Model[field][day]']],
  5762. ['option' => ['value' => '']],
  5763. '/option',
  5764. ['option' => ['value' => '01']],
  5765. '1',
  5766. '/option',
  5767. ['option' => ['value' => '02']],
  5768. '2',
  5769. '/option',
  5770. $daysRegex,
  5771. ['option' => ['value' => '10', 'selected' => 'selected']],
  5772. '10',
  5773. '/option',
  5774. $daysRegex,
  5775. '/select',
  5776. ];
  5777. $this->assertHtml($expected, $result);
  5778. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5779. $result = $this->Form->day('Project.release');
  5780. $expected = [
  5781. ['select' => ['name' => 'Project[release][day]']],
  5782. ['option' => ['value' => '']],
  5783. '/option',
  5784. ['option' => ['value' => '01']],
  5785. '1',
  5786. '/option',
  5787. ['option' => ['value' => '02']],
  5788. '2',
  5789. '/option',
  5790. $daysRegex,
  5791. ['option' => ['value' => '10', 'selected' => 'selected']],
  5792. '10',
  5793. '/option',
  5794. $daysRegex,
  5795. '/select',
  5796. ];
  5797. $this->assertHtml($expected, $result);
  5798. $result = $this->Form->day('Contact.published', [
  5799. 'empty' => 'Published on',
  5800. ]);
  5801. $this->assertContains('Published on', $result);
  5802. }
  5803. /**
  5804. * testMinute method
  5805. *
  5806. * @return void
  5807. */
  5808. public function testMinute()
  5809. {
  5810. extract($this->dateRegex);
  5811. $result = $this->Form->minute('Model.field', ['value' => '']);
  5812. $expected = [
  5813. ['select' => ['name' => 'Model[field][minute]']],
  5814. ['option' => ['selected' => 'selected', 'value' => '']],
  5815. '/option',
  5816. ['option' => ['value' => '00']],
  5817. '00',
  5818. '/option',
  5819. ['option' => ['value' => '01']],
  5820. '01',
  5821. '/option',
  5822. ['option' => ['value' => '02']],
  5823. '02',
  5824. '/option',
  5825. $minutesRegex,
  5826. '/select',
  5827. ];
  5828. $this->assertHtml($expected, $result);
  5829. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5830. $result = $this->Form->minute('Model.field');
  5831. $expected = [
  5832. ['select' => ['name' => 'Model[field][minute]']],
  5833. ['option' => ['value' => '']],
  5834. '/option',
  5835. ['option' => ['value' => '00']],
  5836. '00',
  5837. '/option',
  5838. ['option' => ['value' => '01']],
  5839. '01',
  5840. '/option',
  5841. ['option' => ['value' => '02']],
  5842. '02',
  5843. '/option',
  5844. $minutesRegex,
  5845. ['option' => ['value' => '12', 'selected' => 'selected']],
  5846. '12',
  5847. '/option',
  5848. $minutesRegex,
  5849. '/select',
  5850. ];
  5851. $this->assertHtml($expected, $result);
  5852. $this->Form->request->data['Model']['field'] = '';
  5853. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5854. $expected = [
  5855. ['select' => ['name' => 'Model[field][minute]']],
  5856. ['option' => ['selected' => 'selected', 'value' => '']],
  5857. '/option',
  5858. ['option' => ['value' => '00']],
  5859. '00',
  5860. '/option',
  5861. ['option' => ['value' => '05']],
  5862. '05',
  5863. '/option',
  5864. ['option' => ['value' => '10']],
  5865. '10',
  5866. '/option',
  5867. $minutesRegex,
  5868. '/select',
  5869. ];
  5870. $this->assertHtml($expected, $result);
  5871. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5872. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5873. $expected = [
  5874. ['select' => ['name' => 'Model[field][minute]']],
  5875. ['option' => ['value' => '']],
  5876. '/option',
  5877. ['option' => ['value' => '00']],
  5878. '00',
  5879. '/option',
  5880. ['option' => ['value' => '05']],
  5881. '05',
  5882. '/option',
  5883. ['option' => ['value' => '10', 'selected' => 'selected']],
  5884. '10',
  5885. '/option',
  5886. $minutesRegex,
  5887. '/select',
  5888. ];
  5889. $this->assertHtml($expected, $result);
  5890. }
  5891. /**
  5892. * Test generating an input for the meridian.
  5893. *
  5894. * @return void
  5895. */
  5896. public function testMeridian()
  5897. {
  5898. extract($this->dateRegex);
  5899. $now = new \DateTime();
  5900. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  5901. $expected = [
  5902. ['select' => ['name' => 'Model[field][meridian]']],
  5903. ['option' => ['value' => '']],
  5904. '/option',
  5905. $meridianRegex,
  5906. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  5907. $now->format('a'),
  5908. '/option',
  5909. '*/select'
  5910. ];
  5911. $this->assertHtml($expected, $result);
  5912. }
  5913. /**
  5914. * testHour method
  5915. *
  5916. * @return void
  5917. */
  5918. public function testHour()
  5919. {
  5920. extract($this->dateRegex);
  5921. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  5922. $expected = [
  5923. ['select' => ['name' => 'Model[field][hour]']],
  5924. ['option' => ['selected' => 'selected', 'value' => '']],
  5925. '/option',
  5926. ['option' => ['value' => '01']],
  5927. '1',
  5928. '/option',
  5929. ['option' => ['value' => '02']],
  5930. '2',
  5931. '/option',
  5932. $hoursRegex,
  5933. '/select',
  5934. ];
  5935. $this->assertHtml($expected, $result);
  5936. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5937. $result = $this->Form->hour('Model.field', ['format' => 12]);
  5938. $expected = [
  5939. ['select' => ['name' => 'Model[field][hour]']],
  5940. ['option' => ['value' => '']],
  5941. '/option',
  5942. ['option' => ['value' => '01']],
  5943. '1',
  5944. '/option',
  5945. ['option' => ['value' => '02']],
  5946. '2',
  5947. '/option',
  5948. $hoursRegex,
  5949. ['option' => ['value' => '12', 'selected' => 'selected']],
  5950. '12',
  5951. '/option',
  5952. '/select',
  5953. ];
  5954. $this->assertHtml($expected, $result);
  5955. $this->Form->request->data['Model']['field'] = '';
  5956. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  5957. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5958. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  5959. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5960. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5961. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5962. $expected = [
  5963. ['select' => ['name' => 'Model[field][hour]']],
  5964. ['option' => ['value' => '']],
  5965. '/option',
  5966. ['option' => ['value' => '00', 'selected' => 'selected']],
  5967. '0',
  5968. '/option',
  5969. ['option' => ['value' => '01']],
  5970. '1',
  5971. '/option',
  5972. ['option' => ['value' => '02']],
  5973. '2',
  5974. '/option',
  5975. $hoursRegex,
  5976. '/select',
  5977. ];
  5978. $this->assertHtml($expected, $result);
  5979. unset($this->Form->request->data['Model']['field']);
  5980. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  5981. $thisHour = date('H');
  5982. $optValue = date('G');
  5983. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5984. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5985. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5986. $expected = [
  5987. ['select' => ['name' => 'Model[field][hour]']],
  5988. ['option' => ['value' => '']],
  5989. '/option',
  5990. ['option' => ['value' => '00']],
  5991. '0',
  5992. '/option',
  5993. ['option' => ['value' => '01', 'selected' => 'selected']],
  5994. '1',
  5995. '/option',
  5996. ['option' => ['value' => '02']],
  5997. '2',
  5998. '/option',
  5999. $hoursRegex,
  6000. '/select',
  6001. ];
  6002. $this->assertHtml($expected, $result);
  6003. }
  6004. /**
  6005. * testYear method
  6006. *
  6007. * @return void
  6008. */
  6009. public function testYear()
  6010. {
  6011. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  6012. $expected = [
  6013. ['select' => ['name' => 'Model[field][year]']],
  6014. ['option' => ['selected' => 'selected', 'value' => '']],
  6015. '/option',
  6016. ['option' => ['value' => '2007']],
  6017. '2007',
  6018. '/option',
  6019. ['option' => ['value' => '2006']],
  6020. '2006',
  6021. '/option',
  6022. '/select',
  6023. ];
  6024. $this->assertHtml($expected, $result);
  6025. $result = $this->Form->year('Model.field', [
  6026. 'value' => '',
  6027. 'minYear' => 2006,
  6028. 'maxYear' => 2007,
  6029. 'orderYear' => 'asc'
  6030. ]);
  6031. $expected = [
  6032. ['select' => ['name' => 'Model[field][year]']],
  6033. ['option' => ['selected' => 'selected', 'value' => '']],
  6034. '/option',
  6035. ['option' => ['value' => '2006']],
  6036. '2006',
  6037. '/option',
  6038. ['option' => ['value' => '2007']],
  6039. '2007',
  6040. '/option',
  6041. '/select',
  6042. ];
  6043. $this->assertHtml($expected, $result);
  6044. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6045. $result = $this->Form->year('Contact.published', [
  6046. 'empty' => false,
  6047. 'minYear' => 2006,
  6048. 'maxYear' => 2007,
  6049. ]);
  6050. $expected = [
  6051. ['select' => ['name' => 'Contact[published][year]']],
  6052. ['option' => ['value' => '2007']],
  6053. '2007',
  6054. '/option',
  6055. ['option' => ['value' => '2006', 'selected' => 'selected']],
  6056. '2006',
  6057. '/option',
  6058. '/select',
  6059. ];
  6060. $this->assertHtml($expected, $result);
  6061. $result = $this->Form->year('Contact.published', [
  6062. 'empty' => 'Published on',
  6063. ]);
  6064. $this->assertContains('Published on', $result);
  6065. }
  6066. /**
  6067. * Test minYear being prior to the unix epoch
  6068. *
  6069. * @return void
  6070. */
  6071. public function testInputDatetimePreEpoch()
  6072. {
  6073. $start = date('Y') - 80;
  6074. $end = date('Y') - 18;
  6075. $result = $this->Form->input('birth_year', [
  6076. 'type' => 'date',
  6077. 'label' => 'Birth Year',
  6078. 'minYear' => $start,
  6079. 'maxYear' => $end,
  6080. 'month' => false,
  6081. 'day' => false,
  6082. ]);
  6083. $this->assertContains('value="' . $start . '">' . $start, $result);
  6084. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  6085. $this->assertNotContains('value="00">00', $result);
  6086. }
  6087. /**
  6088. * testYearAutoExpandRange method
  6089. *
  6090. * @return void
  6091. */
  6092. public function testYearAutoExpandRange()
  6093. {
  6094. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6095. $result = $this->Form->year('User.birthday');
  6096. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6097. $result = $matches[1];
  6098. $expected = range(date('Y') + 5, 1930);
  6099. $this->assertEquals($expected, $result);
  6100. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6101. $result = $this->Form->year('Project.release');
  6102. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6103. $result = $matches[1];
  6104. $expected = range(2050, date('Y') - 5);
  6105. $this->assertEquals($expected, $result);
  6106. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6107. $result = $this->Form->year('Project.release', [
  6108. 'minYear' => 1890,
  6109. 'maxYear' => 1900
  6110. ]);
  6111. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6112. $result = $matches[1];
  6113. $expected = range(1900, 1881);
  6114. $this->assertEquals($expected, $result);
  6115. }
  6116. /**
  6117. * Test that input() accepts the type of date and passes options in.
  6118. *
  6119. * @return void
  6120. */
  6121. public function testInputDate()
  6122. {
  6123. $this->Form->request->data = [
  6124. 'month_year' => ['month' => date('m')],
  6125. ];
  6126. $this->Form->create($this->article);
  6127. $result = $this->Form->input('month_year', [
  6128. 'label' => false,
  6129. 'type' => 'date',
  6130. 'minYear' => 2006,
  6131. 'maxYear' => 2008
  6132. ]);
  6133. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6134. $this->assertNotContains('value="2008" selected="selected"', $result);
  6135. }
  6136. /**
  6137. * Test the label option being set to false.
  6138. *
  6139. * @return void
  6140. */
  6141. public function testInputLabelFalse()
  6142. {
  6143. $this->Form->create($this->article);
  6144. $result = $this->Form->input('title', ['label' => false]);
  6145. $expected = [
  6146. 'div' => ['class' => 'input text required'],
  6147. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6148. '/div'
  6149. ];
  6150. $this->assertHtml($expected, $result);
  6151. }
  6152. /**
  6153. * testInputDateMaxYear method
  6154. *
  6155. * Let's say we want to only allow users born from 2006 to 2008 to register
  6156. * This being the first signup page, we still don't have any data
  6157. *
  6158. * @return void
  6159. */
  6160. public function testInputDateMaxYear()
  6161. {
  6162. $this->Form->request->data = [];
  6163. $this->Form->create($this->article);
  6164. $result = $this->Form->input('birthday', [
  6165. 'label' => false,
  6166. 'type' => 'date',
  6167. 'minYear' => 2006,
  6168. 'maxYear' => 2008,
  6169. 'default' => true
  6170. ]);
  6171. $this->assertContains('value="2008" selected="selected"', $result);
  6172. $this->assertContains('value="2006"', $result);
  6173. $this->assertNotContains('value="2005"', $result);
  6174. $this->assertNotContains('value="2009"', $result);
  6175. }
  6176. /**
  6177. * testTextArea method
  6178. *
  6179. * @return void
  6180. */
  6181. public function testTextArea()
  6182. {
  6183. $this->Form->request->data = ['field' => 'some test data'];
  6184. $result = $this->Form->textarea('field');
  6185. $expected = [
  6186. 'textarea' => ['name' => 'field', 'rows' => 5],
  6187. 'some test data',
  6188. '/textarea',
  6189. ];
  6190. $this->assertHtml($expected, $result);
  6191. $result = $this->Form->textarea('user.bio');
  6192. $expected = [
  6193. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6194. '/textarea',
  6195. ];
  6196. $this->assertHtml($expected, $result);
  6197. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6198. $result = $this->Form->textarea('field');
  6199. $expected = [
  6200. 'textarea' => ['name' => 'field', 'rows' => 5],
  6201. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6202. '/textarea',
  6203. ];
  6204. $this->assertHtml($expected, $result);
  6205. $this->Form->request->data = [
  6206. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6207. ];
  6208. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6209. $expected = [
  6210. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6211. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6212. '/textarea',
  6213. ];
  6214. $this->assertHtml($expected, $result);
  6215. $result = $this->Form->textarea('0.OtherModel.field');
  6216. $expected = [
  6217. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6218. '/textarea'
  6219. ];
  6220. $this->assertHtml($expected, $result);
  6221. }
  6222. /**
  6223. * testTextAreaWithStupidCharacters method
  6224. *
  6225. * test text area with non-ascii characters
  6226. *
  6227. * @return void
  6228. */
  6229. public function testTextAreaWithStupidCharacters()
  6230. {
  6231. $result = $this->Form->textarea('Post.content', [
  6232. 'value' => "GREAT®",
  6233. 'rows' => '15',
  6234. 'cols' => '75'
  6235. ]);
  6236. $expected = [
  6237. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6238. 'GREAT®',
  6239. '/textarea',
  6240. ];
  6241. $this->assertHtml($expected, $result);
  6242. }
  6243. /**
  6244. * Test textareas waxlength read from schema.
  6245. *
  6246. * @return void
  6247. */
  6248. public function testTextAreaMaxLength()
  6249. {
  6250. $this->Form->create([
  6251. 'schema' => [
  6252. 'stuff' => ['type' => 'string', 'length' => 10],
  6253. ]
  6254. ]);
  6255. $result = $this->Form->input('other', ['type' => 'textarea']);
  6256. $expected = [
  6257. 'div' => ['class' => 'input textarea'],
  6258. 'label' => ['for' => 'other'],
  6259. 'Other',
  6260. '/label',
  6261. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6262. '/textarea',
  6263. '/div'
  6264. ];
  6265. $this->assertHtml($expected, $result);
  6266. $result = $this->Form->input('stuff', ['type' => 'textarea']);
  6267. $expected = [
  6268. 'div' => ['class' => 'input textarea'],
  6269. 'label' => ['for' => 'stuff'],
  6270. 'Stuff',
  6271. '/label',
  6272. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6273. '/textarea',
  6274. '/div'
  6275. ];
  6276. $this->assertHtml($expected, $result);
  6277. }
  6278. /**
  6279. * testHiddenField method
  6280. *
  6281. * @return void
  6282. */
  6283. public function testHiddenField()
  6284. {
  6285. $this->article['errors'] = [
  6286. 'field' => true
  6287. ];
  6288. $this->Form->request->data['field'] = 'test';
  6289. $this->Form->create($this->article);
  6290. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6291. $expected = [
  6292. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6293. $this->assertHtml($expected, $result);
  6294. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6295. $expected = [
  6296. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6297. ];
  6298. $this->assertHtml($expected, $result);
  6299. }
  6300. /**
  6301. * testFileUploadField method
  6302. *
  6303. * @return void
  6304. */
  6305. public function testFileUploadField()
  6306. {
  6307. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6308. $result = $this->Form->file('Model.upload');
  6309. $this->assertHtml($expected, $result);
  6310. $this->Form->request->data['Model']['upload'] = [
  6311. 'name' => '', 'type' => '', 'tmp_name' => '',
  6312. 'error' => 4, 'size' => 0
  6313. ];
  6314. $result = $this->Form->file('Model.upload');
  6315. $this->assertHtml($expected, $result);
  6316. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6317. $result = $this->Form->file('Model.upload');
  6318. $this->assertHtml($expected, $result);
  6319. }
  6320. /**
  6321. * test File upload input on a model not used in create();
  6322. *
  6323. * @return void
  6324. */
  6325. public function testFileUploadOnOtherModel()
  6326. {
  6327. $this->Form->create($this->article, ['type' => 'file']);
  6328. $result = $this->Form->file('ValidateProfile.city');
  6329. $expected = [
  6330. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6331. ];
  6332. $this->assertHtml($expected, $result);
  6333. }
  6334. /**
  6335. * testButton method
  6336. *
  6337. * @return void
  6338. */
  6339. public function testButton()
  6340. {
  6341. $result = $this->Form->button('Hi');
  6342. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6343. $this->assertHtml($expected, $result);
  6344. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6345. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6346. $this->assertHtml($expected, $result);
  6347. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6348. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6349. $this->assertHtml($expected, $result);
  6350. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6351. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6352. $this->assertHtml($expected, $result);
  6353. $result = $this->Form->button('No type', ['type' => false]);
  6354. $expected = ['button' => [], 'No type', '/button'];
  6355. $this->assertHtml($expected, $result);
  6356. $result = $this->Form->button('Upload Text', [
  6357. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6358. 'escape' => false
  6359. ]);
  6360. $this->assertNotRegExp('/\&039/', $result);
  6361. }
  6362. /**
  6363. * Test that button() makes unlocked fields by default.
  6364. *
  6365. * @return void
  6366. */
  6367. public function testButtonUnlockedByDefault()
  6368. {
  6369. $this->Form->request->params['_csrfToken'] = 'secured';
  6370. $this->Form->button('Save', ['name' => 'save']);
  6371. $this->Form->button('Clear');
  6372. $result = $this->Form->unlockField();
  6373. $this->assertEquals(['save'], $result);
  6374. }
  6375. /**
  6376. * testPostButton method
  6377. *
  6378. * @return void
  6379. */
  6380. public function testPostButton()
  6381. {
  6382. $result = $this->Form->postButton('Hi', '/controller/action');
  6383. $expected = [
  6384. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6385. 'div' => ['style' => 'display:none;'],
  6386. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6387. '/div',
  6388. 'button' => ['type' => 'submit'],
  6389. 'Hi',
  6390. '/button',
  6391. '/form'
  6392. ];
  6393. $this->assertHtml($expected, $result);
  6394. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6395. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6396. }
  6397. public function testPostButtonMethodType()
  6398. {
  6399. $result = $this->Form->postButton('Hi', '/controller/action', ['method' => 'patch']);
  6400. $expected = [
  6401. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6402. 'div' => ['style' => 'display:none;'],
  6403. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PATCH'],
  6404. '/div',
  6405. 'button' => ['type' => 'submit'],
  6406. 'Hi',
  6407. '/button',
  6408. '/form'
  6409. ];
  6410. $this->assertHtml($expected, $result);
  6411. }
  6412. public function testPostButtonFormOptions()
  6413. {
  6414. $result = $this->Form->postButton('Hi', '/controller/action', ['form' => ['class' => 'inline']]);
  6415. $expected = [
  6416. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'class' => 'inline'],
  6417. 'div' => ['style' => 'display:none;'],
  6418. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6419. '/div',
  6420. 'button' => ['type' => 'submit'],
  6421. 'Hi',
  6422. '/button',
  6423. '/form'
  6424. ];
  6425. $this->assertHtml($expected, $result);
  6426. }
  6427. /**
  6428. * Test using postButton with N dimensional data.
  6429. *
  6430. * @return void
  6431. */
  6432. public function testPostButtonNestedData()
  6433. {
  6434. $data = [
  6435. 'one' => [
  6436. 'two' => [
  6437. 3, 4, 5
  6438. ]
  6439. ]
  6440. ];
  6441. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6442. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6443. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6444. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6445. }
  6446. /**
  6447. * Test that postButton adds _Token fields.
  6448. *
  6449. * @return void
  6450. */
  6451. public function testSecurePostButton()
  6452. {
  6453. $this->Form->request->params['_csrfToken'] = 'testkey';
  6454. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6455. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6456. $tokenDebug = urlencode(json_encode([
  6457. '/posts/delete/1',
  6458. [],
  6459. []
  6460. ]));
  6461. $expected = [
  6462. 'form' => [
  6463. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6464. ],
  6465. ['div' => ['style' => 'display:none;']],
  6466. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6467. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6468. '/div',
  6469. 'button' => ['type' => 'submit'],
  6470. 'Delete',
  6471. '/button',
  6472. ['div' => ['style' => 'display:none;']],
  6473. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6474. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6475. ['input' => [
  6476. 'type' => 'hidden', 'name' => '_Token[debug]',
  6477. 'value' => $tokenDebug,
  6478. ]],
  6479. '/div',
  6480. '/form',
  6481. ];
  6482. $this->assertHtml($expected, $result);
  6483. }
  6484. /**
  6485. * testPostLink method
  6486. *
  6487. * @return void
  6488. */
  6489. public function testPostLink()
  6490. {
  6491. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6492. $expected = [
  6493. 'form' => [
  6494. 'method' => 'post', 'action' => '/posts/delete/1',
  6495. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6496. ],
  6497. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6498. '/form',
  6499. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6500. 'Delete',
  6501. '/a'
  6502. ];
  6503. $this->assertHtml($expected, $result);
  6504. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6505. $expected = [
  6506. 'form' => [
  6507. 'method' => 'post', 'action' => '/posts/delete/1',
  6508. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6509. ],
  6510. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6511. '/form',
  6512. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6513. 'Delete',
  6514. '/a'
  6515. ];
  6516. $this->assertHtml($expected, $result);
  6517. $result = $this->Form->postLink(
  6518. 'Delete',
  6519. '/posts/delete/1',
  6520. ['target' => '_blank', 'class' => 'btn btn-danger']
  6521. );
  6522. $expected = [
  6523. 'form' => [
  6524. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6525. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6526. ],
  6527. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6528. '/form',
  6529. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6530. 'Delete',
  6531. '/a'
  6532. ];
  6533. $this->assertHtml($expected, $result);
  6534. }
  6535. /**
  6536. * Test the confirm option for postLink()
  6537. *
  6538. * @return void
  6539. */
  6540. public function testPostLinkWithConfirm()
  6541. {
  6542. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6543. $expected = [
  6544. 'form' => [
  6545. 'method' => 'post', 'action' => '/posts/delete/1',
  6546. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6547. ],
  6548. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6549. '/form',
  6550. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6551. 'Delete',
  6552. '/a'
  6553. ];
  6554. $this->assertHtml($expected, $result);
  6555. $result = $this->Form->postLink(
  6556. 'Delete',
  6557. '/posts/delete/1',
  6558. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6559. );
  6560. $expected = [
  6561. 'form' => [
  6562. 'method' => 'post', 'action' => '/posts/delete/1',
  6563. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6564. ],
  6565. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6566. '/form',
  6567. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\nthis \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6568. 'Delete',
  6569. '/a'
  6570. ];
  6571. $this->assertHtml($expected, $result);
  6572. }
  6573. /**
  6574. * test postLink() with query string args.
  6575. *
  6576. * @return void
  6577. */
  6578. public function testPostLinkWithQuery()
  6579. {
  6580. $result = $this->Form->postLink(
  6581. 'Delete',
  6582. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6583. );
  6584. $expected = [
  6585. 'form' => [
  6586. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6587. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6588. ],
  6589. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6590. '/form',
  6591. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6592. 'Delete',
  6593. '/a'
  6594. ];
  6595. $this->assertHtml($expected, $result);
  6596. }
  6597. /**
  6598. * Test postLink with additional data.
  6599. *
  6600. * @return void
  6601. */
  6602. public function testPostLinkWithData()
  6603. {
  6604. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6605. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6606. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6607. $this->Form->create($entity);
  6608. $this->Form->end();
  6609. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6610. $this->assertContains(
  6611. '<input type="hidden" name="name" value="show"',
  6612. $result,
  6613. 'should not contain entity data.'
  6614. );
  6615. }
  6616. /**
  6617. * Test that security hashes for postLink include the url.
  6618. *
  6619. * @return void
  6620. */
  6621. public function testPostLinkSecurityHash()
  6622. {
  6623. $hash = Security::hash(
  6624. '/posts/delete/1' .
  6625. serialize(['id' => '1']) .
  6626. '' .
  6627. Security::salt()
  6628. );
  6629. $hash .= '%3Aid';
  6630. $this->Form->request->params['_Token']['key'] = 'test';
  6631. $result = $this->Form->postLink(
  6632. 'Delete',
  6633. '/posts/delete/1',
  6634. ['data' => ['id' => 1]]
  6635. );
  6636. $tokenDebug = urlencode(json_encode([
  6637. '/posts/delete/1',
  6638. [
  6639. 'id' => 1
  6640. ],
  6641. []
  6642. ]));
  6643. $expected = [
  6644. 'form' => [
  6645. 'method' => 'post', 'action' => '/posts/delete/1',
  6646. 'name', 'style' => 'display:none;'
  6647. ],
  6648. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6649. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6650. 'div' => ['style' => 'display:none;'],
  6651. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6652. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6653. ['input' => [
  6654. 'type' => 'hidden', 'name' => '_Token[debug]',
  6655. 'value' => $tokenDebug,
  6656. ]],
  6657. '/div',
  6658. '/form',
  6659. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6660. 'Delete',
  6661. '/a'
  6662. ];
  6663. $this->assertHtml($expected, $result);
  6664. }
  6665. /**
  6666. * Test that postLink doesn't modify the fields in the containing form.
  6667. *
  6668. * postLink() calls inside open forms should not modify the field list
  6669. * for the form.
  6670. *
  6671. * @return void
  6672. */
  6673. public function testPostLinkSecurityHashBlockMode()
  6674. {
  6675. $hash = Security::hash(
  6676. '/posts/delete/1' .
  6677. serialize([]) .
  6678. '' .
  6679. Security::salt()
  6680. );
  6681. $hash .= '%3A';
  6682. $this->Form->request->params['_Token']['key'] = 'test';
  6683. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  6684. $this->Form->input('title');
  6685. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6686. $result = $this->View->fetch('postLink');
  6687. $this->assertEquals(['title'], $this->Form->fields);
  6688. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  6689. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  6690. }
  6691. /**
  6692. * Test that security does not include debug token if debug is false.
  6693. *
  6694. * @return void
  6695. */
  6696. public function testPostLinkSecurityHashNoDebugMode()
  6697. {
  6698. Configure::write('debug', false);
  6699. $hash = Security::hash(
  6700. '/posts/delete/1' .
  6701. serialize(['id' => '1']) .
  6702. '' .
  6703. Security::salt()
  6704. );
  6705. $hash .= '%3Aid';
  6706. $this->Form->request->params['_Token']['key'] = 'test';
  6707. $result = $this->Form->postLink(
  6708. 'Delete',
  6709. '/posts/delete/1',
  6710. ['data' => ['id' => 1]]
  6711. );
  6712. $expected = [
  6713. 'form' => [
  6714. 'method' => 'post', 'action' => '/posts/delete/1',
  6715. 'name', 'style' => 'display:none;'
  6716. ],
  6717. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6718. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6719. 'div' => ['style' => 'display:none;'],
  6720. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6721. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6722. '/div',
  6723. '/form',
  6724. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6725. 'Delete',
  6726. '/a'
  6727. ];
  6728. $this->assertHtml($expected, $result);
  6729. }
  6730. /**
  6731. * Test using postLink with N dimensional data.
  6732. *
  6733. * @return void
  6734. */
  6735. public function testPostLinkNestedData()
  6736. {
  6737. $data = [
  6738. 'one' => [
  6739. 'two' => [
  6740. 3, 4, 5
  6741. ]
  6742. ]
  6743. ];
  6744. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6745. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6746. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6747. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6748. }
  6749. /**
  6750. * test creating postLinks after a GET form.
  6751. *
  6752. * @return void
  6753. */
  6754. public function testPostLinkAfterGetForm()
  6755. {
  6756. $this->Form->request->params['_csrfToken'] = 'testkey';
  6757. $this->Form->request->params['_Token'] = 'val';
  6758. $this->Form->create($this->article, ['type' => 'get']);
  6759. $this->Form->end();
  6760. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6761. $tokenDebug = urlencode(json_encode([
  6762. '/posts/delete/1',
  6763. [],
  6764. []
  6765. ]));
  6766. $expected = [
  6767. 'form' => [
  6768. 'method' => 'post', 'action' => '/posts/delete/1',
  6769. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6770. ],
  6771. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6772. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6773. 'div' => ['style' => 'display:none;'],
  6774. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6775. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6776. ['input' => [
  6777. 'type' => 'hidden', 'name' => '_Token[debug]',
  6778. 'value' => $tokenDebug,
  6779. ]],
  6780. '/div',
  6781. '/form',
  6782. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6783. 'Delete',
  6784. '/a'
  6785. ];
  6786. $this->assertHtml($expected, $result);
  6787. }
  6788. /**
  6789. * Test that postLink adds form tags to view block
  6790. *
  6791. * @return void
  6792. */
  6793. public function testPostLinkFormBuffer()
  6794. {
  6795. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6796. $expected = [
  6797. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6798. 'Delete',
  6799. '/a'
  6800. ];
  6801. $this->assertHtml($expected, $result);
  6802. $result = $this->View->fetch('postLink');
  6803. $expected = [
  6804. 'form' => [
  6805. 'method' => 'post', 'action' => '/posts/delete/1',
  6806. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6807. ],
  6808. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6809. '/form'
  6810. ];
  6811. $this->assertHtml($expected, $result);
  6812. $result = $this->Form->postLink(
  6813. 'Delete',
  6814. '/posts/delete/2',
  6815. ['block' => true, 'method' => 'DELETE']
  6816. );
  6817. $expected = [
  6818. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6819. 'Delete',
  6820. '/a'
  6821. ];
  6822. $this->assertHtml($expected, $result);
  6823. $result = $this->View->fetch('postLink');
  6824. $expected = [
  6825. 'form' => [
  6826. 'method' => 'post', 'action' => '/posts/delete/1',
  6827. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6828. ],
  6829. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6830. '/form',
  6831. [
  6832. 'form' => [
  6833. 'method' => 'post', 'action' => '/posts/delete/2',
  6834. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6835. ],
  6836. ],
  6837. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6838. '/form'
  6839. ];
  6840. $this->assertHtml($expected, $result);
  6841. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6842. $expected = [
  6843. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6844. 'Delete',
  6845. '/a'
  6846. ];
  6847. $this->assertHtml($expected, $result);
  6848. $result = $this->View->fetch('foobar');
  6849. $expected = [
  6850. 'form' => [
  6851. 'method' => 'post', 'action' => '/posts/delete/1',
  6852. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6853. ],
  6854. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6855. '/form'
  6856. ];
  6857. $this->assertHtml($expected, $result);
  6858. }
  6859. /**
  6860. * testSubmitButton method
  6861. *
  6862. * @return void
  6863. */
  6864. public function testSubmitButton()
  6865. {
  6866. $result = $this->Form->submit('');
  6867. $expected = [
  6868. 'div' => ['class' => 'submit'],
  6869. 'input' => ['type' => 'submit', 'value' => ''],
  6870. '/div'
  6871. ];
  6872. $this->assertHtml($expected, $result);
  6873. $result = $this->Form->submit('Test Submit');
  6874. $expected = [
  6875. 'div' => ['class' => 'submit'],
  6876. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6877. '/div'
  6878. ];
  6879. $this->assertHtml($expected, $result);
  6880. $result = $this->Form->submit('Next >');
  6881. $expected = [
  6882. 'div' => ['class' => 'submit'],
  6883. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6884. '/div'
  6885. ];
  6886. $this->assertHtml($expected, $result);
  6887. $result = $this->Form->submit('Next >', ['escape' => false]);
  6888. $expected = [
  6889. 'div' => ['class' => 'submit'],
  6890. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6891. '/div'
  6892. ];
  6893. $this->assertHtml($expected, $result);
  6894. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6895. $expected = [
  6896. 'div' => ['class' => 'submit'],
  6897. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6898. '/div'
  6899. ];
  6900. $this->assertHtml($expected, $result);
  6901. }
  6902. /**
  6903. * test image submit types.
  6904. *
  6905. * @return void
  6906. */
  6907. public function testSubmitImage()
  6908. {
  6909. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6910. $expected = [
  6911. 'div' => ['class' => 'submit'],
  6912. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6913. '/div'
  6914. ];
  6915. $this->assertHtml($expected, $result);
  6916. $result = $this->Form->submit('/relative/cake.power.gif');
  6917. $expected = [
  6918. 'div' => ['class' => 'submit'],
  6919. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6920. '/div'
  6921. ];
  6922. $this->assertHtml($expected, $result);
  6923. $result = $this->Form->submit('cake.power.gif');
  6924. $expected = [
  6925. 'div' => ['class' => 'submit'],
  6926. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6927. '/div'
  6928. ];
  6929. $this->assertHtml($expected, $result);
  6930. $result = $this->Form->submit('Not.an.image');
  6931. $expected = [
  6932. 'div' => ['class' => 'submit'],
  6933. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6934. '/div'
  6935. ];
  6936. $this->assertHtml($expected, $result);
  6937. }
  6938. /**
  6939. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6940. * be submitted at a time.
  6941. *
  6942. * @return void
  6943. */
  6944. public function testSubmitUnlockedByDefault()
  6945. {
  6946. $this->Form->request->params['_Token'] = 'secured';
  6947. $this->Form->submit('Go go');
  6948. $this->Form->submit('Save', ['name' => 'save']);
  6949. $result = $this->Form->unlockField();
  6950. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6951. }
  6952. /**
  6953. * Test submit image with timestamps.
  6954. *
  6955. * @return void
  6956. */
  6957. public function testSubmitImageTimestamp()
  6958. {
  6959. Configure::write('Asset.timestamp', 'force');
  6960. $result = $this->Form->submit('cake.power.gif');
  6961. $expected = [
  6962. 'div' => ['class' => 'submit'],
  6963. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6964. '/div'
  6965. ];
  6966. $this->assertHtml($expected, $result);
  6967. }
  6968. /**
  6969. * test that datetime() works with GET style forms.
  6970. *
  6971. * @return void
  6972. */
  6973. public function testDateTimeWithGetForms()
  6974. {
  6975. extract($this->dateRegex);
  6976. $this->Form->create($this->article, ['type' => 'get']);
  6977. $result = $this->Form->datetime('created');
  6978. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  6979. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  6980. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  6981. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  6982. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  6983. }
  6984. /**
  6985. * testForMagicInputNonExistingNorValidated method
  6986. *
  6987. * @return void
  6988. */
  6989. public function testForMagicInputNonExistingNorValidated()
  6990. {
  6991. $result = $this->Form->create($this->article);
  6992. $this->Form->templates(['inputContainer' => '{{content}}']);
  6993. $result = $this->Form->input('non_existing_nor_validated');
  6994. $expected = [
  6995. 'label' => ['for' => 'non-existing-nor-validated'],
  6996. 'Non Existing Nor Validated',
  6997. '/label',
  6998. 'input' => [
  6999. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7000. 'id' => 'non-existing-nor-validated'
  7001. ]
  7002. ];
  7003. $this->assertHtml($expected, $result);
  7004. $result = $this->Form->input('non_existing_nor_validated', [
  7005. 'val' => 'my value'
  7006. ]);
  7007. $expected = [
  7008. 'label' => ['for' => 'non-existing-nor-validated'],
  7009. 'Non Existing Nor Validated',
  7010. '/label',
  7011. 'input' => [
  7012. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7013. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  7014. ]
  7015. ];
  7016. $this->assertHtml($expected, $result);
  7017. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  7018. $result = $this->Form->input('non_existing_nor_validated');
  7019. $expected = [
  7020. 'label' => ['for' => 'non-existing-nor-validated'],
  7021. 'Non Existing Nor Validated',
  7022. '/label',
  7023. 'input' => [
  7024. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  7025. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  7026. ]
  7027. ];
  7028. $this->assertHtml($expected, $result);
  7029. }
  7030. /**
  7031. * testFormMagicInputLabel method
  7032. *
  7033. * @return void
  7034. */
  7035. public function testFormMagicInputLabel()
  7036. {
  7037. TableRegistry::get('Contacts', [
  7038. 'className' => __NAMESPACE__ . '\ContactsTable'
  7039. ]);
  7040. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  7041. $this->Form->templates(['inputContainer' => '{{content}}']);
  7042. $result = $this->Form->input('Contacts.name', ['label' => 'My label']);
  7043. $expected = [
  7044. 'label' => ['for' => 'contacts-name'],
  7045. 'My label',
  7046. '/label',
  7047. 'input' => [
  7048. 'type' => 'text', 'name' => 'Contacts[name]',
  7049. 'id' => 'contacts-name', 'maxlength' => '255'
  7050. ]
  7051. ];
  7052. $this->assertHtml($expected, $result);
  7053. $result = $this->Form->input('name', [
  7054. 'label' => ['class' => 'mandatory']
  7055. ]);
  7056. $expected = [
  7057. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7058. 'Name',
  7059. '/label',
  7060. 'input' => [
  7061. 'type' => 'text', 'name' => 'name',
  7062. 'id' => 'name', 'maxlength' => '255'
  7063. ]
  7064. ];
  7065. $this->assertHtml($expected, $result);
  7066. $result = $this->Form->input('name', [
  7067. 'div' => false,
  7068. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  7069. ]);
  7070. $expected = [
  7071. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  7072. 'My label',
  7073. '/label',
  7074. 'input' => [
  7075. 'type' => 'text', 'name' => 'name',
  7076. 'id' => 'name', 'maxlength' => '255'
  7077. ]
  7078. ];
  7079. $this->assertHtml($expected, $result);
  7080. $result = $this->Form->input('Contact.name', [
  7081. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  7082. ]);
  7083. $expected = [
  7084. 'label' => ['for' => 'my_id'],
  7085. 'Name',
  7086. '/label',
  7087. 'input' => [
  7088. 'type' => 'text', 'name' => 'Contact[name]',
  7089. 'id' => 'my_id', 'maxlength' => '255'
  7090. ]
  7091. ];
  7092. $this->assertHtml($expected, $result);
  7093. $result = $this->Form->input('1.id');
  7094. $expected = ['input' => [
  7095. 'type' => 'hidden', 'name' => '1[id]',
  7096. 'id' => '1-id'
  7097. ]];
  7098. $this->assertHtml($expected, $result);
  7099. $result = $this->Form->input("1.name");
  7100. $expected = [
  7101. 'label' => ['for' => '1-name'],
  7102. 'Name',
  7103. '/label',
  7104. 'input' => [
  7105. 'type' => 'text', 'name' => '1[name]',
  7106. 'id' => '1-name', 'maxlength' => '255'
  7107. ]
  7108. ];
  7109. $this->assertHtml($expected, $result);
  7110. }
  7111. /**
  7112. * testFormEnd method
  7113. *
  7114. * @return void
  7115. */
  7116. public function testFormEnd()
  7117. {
  7118. $this->assertEquals('</form>', $this->Form->end());
  7119. }
  7120. /**
  7121. * Test the generation of fields for a multi record form.
  7122. *
  7123. * @return void
  7124. */
  7125. public function testMultiRecordForm()
  7126. {
  7127. $this->loadFixtures('Articles', 'Comments');
  7128. $articles = TableRegistry::get('Articles');
  7129. $articles->hasMany('Comments');
  7130. $comment = new Entity(['comment' => 'Value']);
  7131. $article = new Article(['comments' => [$comment]]);
  7132. $this->Form->create([$article]);
  7133. $result = $this->Form->input('0.comments.1.comment');
  7134. //@codingStandardsIgnoreStart
  7135. $expected = [
  7136. 'div' => ['class' => 'input textarea'],
  7137. 'label' => ['for' => '0-comments-1-comment'],
  7138. 'Comment',
  7139. '/label',
  7140. 'textarea' => [
  7141. 'name',
  7142. 'id' => '0-comments-1-comment',
  7143. 'rows' => 5
  7144. ],
  7145. '/textarea',
  7146. '/div'
  7147. ];
  7148. //@codingStandardsIgnoreEnd
  7149. $this->assertHtml($expected, $result);
  7150. $result = $this->Form->input('0.comments.0.comment');
  7151. //@codingStandardsIgnoreStart
  7152. $expected = [
  7153. 'div' => ['class' => 'input textarea'],
  7154. 'label' => ['for' => '0-comments-0-comment'],
  7155. 'Comment',
  7156. '/label',
  7157. 'textarea' => [
  7158. 'name',
  7159. 'id' => '0-comments-0-comment',
  7160. 'rows' => 5
  7161. ],
  7162. 'Value',
  7163. '/textarea',
  7164. '/div'
  7165. ];
  7166. //@codingStandardsIgnoreEnd
  7167. $this->assertHtml($expected, $result);
  7168. $comment->errors('comment', ['Not valid']);
  7169. $result = $this->Form->input('0.comments.0.comment');
  7170. //@codingStandardsIgnoreStart
  7171. $expected = [
  7172. 'div' => ['class' => 'input textarea error'],
  7173. 'label' => ['for' => '0-comments-0-comment'],
  7174. 'Comment',
  7175. '/label',
  7176. 'textarea' => [
  7177. 'name',
  7178. 'class' => 'form-error',
  7179. 'id' => '0-comments-0-comment',
  7180. 'rows' => 5
  7181. ],
  7182. 'Value',
  7183. '/textarea',
  7184. ['div' => ['class' => 'error-message']],
  7185. 'Not valid',
  7186. '/div',
  7187. '/div'
  7188. ];
  7189. //@codingStandardsIgnoreEnd
  7190. $this->assertHtml($expected, $result);
  7191. TableRegistry::get('Comments')
  7192. ->validator('default')
  7193. ->allowEmpty('comment', false);
  7194. $result = $this->Form->input('0.comments.1.comment');
  7195. //@codingStandardsIgnoreStart
  7196. $expected = [
  7197. 'div' => ['class' => 'input textarea required'],
  7198. 'label' => ['for' => '0-comments-1-comment'],
  7199. 'Comment',
  7200. '/label',
  7201. 'textarea' => [
  7202. 'name',
  7203. 'required' => 'required',
  7204. 'id' => '0-comments-1-comment',
  7205. 'rows' => 5
  7206. ],
  7207. '/textarea',
  7208. '/div'
  7209. ];
  7210. //@codingStandardsIgnoreEnd
  7211. $this->assertHtml($expected, $result);
  7212. }
  7213. /**
  7214. * test that some html5 inputs + FormHelper::__call() work
  7215. *
  7216. * @return void
  7217. */
  7218. public function testHtml5Inputs()
  7219. {
  7220. $result = $this->Form->email('User.email');
  7221. $expected = [
  7222. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7223. ];
  7224. $this->assertHtml($expected, $result);
  7225. $result = $this->Form->search('User.query');
  7226. $expected = [
  7227. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7228. ];
  7229. $this->assertHtml($expected, $result);
  7230. $result = $this->Form->search('User.query', ['value' => 'test']);
  7231. $expected = [
  7232. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7233. ];
  7234. $this->assertHtml($expected, $result);
  7235. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7236. $expected = [
  7237. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7238. ];
  7239. $this->assertHtml($expected, $result);
  7240. }
  7241. /**
  7242. * Test accessing html5 inputs through input().
  7243. *
  7244. * @return void
  7245. */
  7246. public function testHtml5InputWithInput()
  7247. {
  7248. $this->Form->create();
  7249. $this->Form->templates(['inputContainer' => '{{content}}']);
  7250. $result = $this->Form->input('website', [
  7251. 'type' => 'url',
  7252. 'val' => 'http://domain.tld',
  7253. 'label' => false
  7254. ]);
  7255. $expected = [
  7256. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7257. ];
  7258. $this->assertHtml($expected, $result);
  7259. }
  7260. /**
  7261. * Test errors when field name is missing.
  7262. *
  7263. * @expectedException \Cake\Core\Exception\Exception
  7264. * @return void
  7265. */
  7266. public function testHtml5InputException()
  7267. {
  7268. $this->Form->email();
  7269. }
  7270. /**
  7271. * Tests that formhelper sets required attributes.
  7272. *
  7273. * @return void
  7274. */
  7275. public function testRequiredAttribute()
  7276. {
  7277. $this->article['required'] = [
  7278. 'title' => true,
  7279. 'body' => false,
  7280. ];
  7281. $this->Form->create($this->article);
  7282. $result = $this->Form->input('title');
  7283. $expected = [
  7284. 'div' => ['class' => 'input text required'],
  7285. 'label' => ['for' => 'title'],
  7286. 'Title',
  7287. '/label',
  7288. 'input' => [
  7289. 'type' => 'text',
  7290. 'name' => 'title',
  7291. 'id' => 'title',
  7292. 'required' => 'required',
  7293. ],
  7294. '/div'
  7295. ];
  7296. $this->assertHtml($expected, $result);
  7297. $result = $this->Form->input('title', ['required' => false]);
  7298. $this->assertNotContains('required', $result);
  7299. $result = $this->Form->input('body');
  7300. $expected = [
  7301. 'div' => ['class' => 'input text'],
  7302. 'label' => ['for' => 'body'],
  7303. 'Body',
  7304. '/label',
  7305. 'input' => [
  7306. 'type' => 'text',
  7307. 'name' => 'body',
  7308. 'id' => 'body',
  7309. ],
  7310. '/div'
  7311. ];
  7312. $this->assertHtml($expected, $result);
  7313. $result = $this->Form->input('body', ['required' => true]);
  7314. $this->assertContains('required', $result);
  7315. }
  7316. /**
  7317. * Tests that it is possible to put inputs outside of the label
  7318. *
  7319. * @return void
  7320. */
  7321. public function testInputsNotNested()
  7322. {
  7323. $this->Form->templates([
  7324. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7325. 'formGroup' => '{{input}}{{label}}',
  7326. ]);
  7327. $result = $this->Form->input('foo', ['type' => 'checkbox']);
  7328. $expected = [
  7329. 'div' => ['class' => 'input checkbox'],
  7330. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7331. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7332. 'label' => ['for' => 'foo'],
  7333. 'Foo',
  7334. '/label',
  7335. '/div'
  7336. ];
  7337. $this->assertHtml($expected, $result);
  7338. $result = $this->Form->input('foo', ['type' => 'checkbox', 'label' => false]);
  7339. $expected = [
  7340. 'div' => ['class' => 'input checkbox'],
  7341. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7342. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7343. '/div'
  7344. ];
  7345. $this->assertHtml($expected, $result);
  7346. $result = $this->Form->input('confirm', [
  7347. 'type' => 'radio',
  7348. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7349. ]);
  7350. $expected = [
  7351. 'div' => ['class' => 'input radio'],
  7352. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7353. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7354. ['label' => ['for' => 'confirm-y']],
  7355. 'Yes',
  7356. '/label',
  7357. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7358. ['label' => ['for' => 'confirm-n']],
  7359. 'No',
  7360. '/label',
  7361. '<label',
  7362. 'Confirm',
  7363. '/label',
  7364. '/div',
  7365. ];
  7366. $this->assertHtml($expected, $result);
  7367. $result = $this->Form->select('category', ['1', '2'], [
  7368. 'multiple' => 'checkbox',
  7369. 'name' => 'fish',
  7370. ]);
  7371. $expected = [
  7372. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7373. ['div' => ['class' => 'checkbox']],
  7374. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7375. ['label' => ['for' => 'fish-0']],
  7376. '1',
  7377. '/label',
  7378. '/div',
  7379. ['div' => ['class' => 'checkbox']],
  7380. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7381. ['label' => ['for' => 'fish-1']],
  7382. '2',
  7383. '/label',
  7384. '/div'
  7385. ];
  7386. $this->assertHtml($expected, $result);
  7387. }
  7388. /**
  7389. * Test that *Container templates are used by input.
  7390. *
  7391. * @return void
  7392. */
  7393. public function testInputContainerTemplates()
  7394. {
  7395. $this->Form->templates([
  7396. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7397. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7398. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7399. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7400. ]);
  7401. $this->article['errors'] = [
  7402. 'Article' => ['published' => 'error message']
  7403. ];
  7404. $this->Form->create($this->article);
  7405. $result = $this->Form->input('accept', [
  7406. 'type' => 'checkbox'
  7407. ]);
  7408. $expected = [
  7409. 'div' => ['class' => 'check'],
  7410. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7411. 'label' => ['for' => 'accept'],
  7412. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7413. 'Accept',
  7414. '/label',
  7415. '/div'
  7416. ];
  7417. $this->assertHtml($expected, $result);
  7418. $result = $this->Form->input('accept', [
  7419. 'type' => 'radio',
  7420. 'options' => ['Y', 'N']
  7421. ]);
  7422. $this->assertContains('<div class="rad">', $result);
  7423. $result = $this->Form->input('Article.published', [
  7424. 'type' => 'radio',
  7425. 'options' => ['Y', 'N']
  7426. ]);
  7427. $this->assertContains('<div class="rad err">', $result);
  7428. $result = $this->Form->input('Article.created', [
  7429. 'type' => 'datetime',
  7430. ]);
  7431. $this->assertContains('<div class="dt">', $result);
  7432. }
  7433. /**
  7434. * Test that *Container templates are used by input.
  7435. *
  7436. * @return void
  7437. */
  7438. public function testFormGroupTemplates()
  7439. {
  7440. $this->Form->templates([
  7441. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7442. ]);
  7443. $this->Form->create($this->article);
  7444. $result = $this->Form->input('accept', [
  7445. 'type' => 'radio',
  7446. 'options' => ['Y', 'N']
  7447. ]);
  7448. $this->assertContains('<div class="radio">', $result);
  7449. }
  7450. /**
  7451. * Test resetting templates.
  7452. *
  7453. * @return void
  7454. */
  7455. public function testResetTemplates()
  7456. {
  7457. $this->Form->templates(['input' => '<input/>']);
  7458. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7459. $this->assertNull($this->Form->resetTemplates());
  7460. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7461. }
  7462. /**
  7463. * Test the context method.
  7464. *
  7465. * @return void
  7466. */
  7467. public function testContext()
  7468. {
  7469. $result = $this->Form->context();
  7470. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7471. $mock = $this->getMockBuilder('Cake\View\Form\ContextInterface')->getMock();
  7472. $this->assertSame($mock, $this->Form->context($mock));
  7473. $this->assertSame($mock, $this->Form->context());
  7474. }
  7475. /**
  7476. * testAutoDomId
  7477. *
  7478. * @return void
  7479. */
  7480. public function testAutoDomId()
  7481. {
  7482. $result = $this->Form->text('field', ['id' => true]);
  7483. $expected = [
  7484. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7485. ];
  7486. $this->assertHtml($expected, $result);
  7487. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7488. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7489. $expected = [
  7490. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7491. ['label' => ['for' => 'field-0']],
  7492. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7493. 'option A',
  7494. '/label',
  7495. ['label' => ['for' => 'field-1']],
  7496. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7497. 'option B',
  7498. '/label',
  7499. ];
  7500. $this->assertHtml($expected, $result);
  7501. $result = $this->Form->select(
  7502. 'multi_field',
  7503. ['first', 'second'],
  7504. ['multiple' => 'checkbox', 'id' => true]
  7505. );
  7506. $expected = [
  7507. 'input' => [
  7508. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7509. ],
  7510. ['div' => ['class' => 'checkbox']],
  7511. ['label' => ['for' => 'multi-field-0']],
  7512. ['input' => [
  7513. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7514. 'value' => '0', 'id' => 'multi-field-0'
  7515. ]],
  7516. 'first',
  7517. '/label',
  7518. '/div',
  7519. ['div' => ['class' => 'checkbox']],
  7520. ['label' => ['for' => 'multi-field-1']],
  7521. ['input' => [
  7522. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7523. 'value' => '1', 'id' => 'multi-field-1'
  7524. ]],
  7525. 'second',
  7526. '/label',
  7527. '/div',
  7528. ];
  7529. $this->assertHtml($expected, $result);
  7530. }
  7531. /**
  7532. * Test the basic setters and getters for value sources
  7533. *
  7534. * @return void
  7535. */
  7536. public function testFormValueSourcesSettersGetters()
  7537. {
  7538. $expected = ['context'];
  7539. $result = $this->Form->getValueSources();
  7540. $this->assertEquals($expected, $result);
  7541. $expected = null;
  7542. $result = $this->Form->getSourceValue('id');
  7543. $this->assertEquals($expected, $result);
  7544. $expected = ['query', 'data', 'context'];
  7545. $this->Form->setValueSources(['query', 'data', 'invalid', 'context', 'foo']);
  7546. $result = $this->Form->getValueSources();
  7547. $this->assertEquals($expected, $result);
  7548. $this->Form->request->data['id'] = '1';
  7549. $this->Form->request->query['id'] = '2';
  7550. $this->Form->setValueSources(['context']);
  7551. $expected = '1';
  7552. $result = $this->Form->getSourceValue('id');
  7553. $this->assertEquals($expected, $result);
  7554. $this->Form->setValueSources('query');
  7555. $expected = ['query'];
  7556. $result = $this->Form->getValueSources();
  7557. $this->assertEquals($expected, $result);
  7558. $expected = '2';
  7559. $result = $this->Form->getSourceValue('id');
  7560. $this->assertEquals($expected, $result);
  7561. $this->Form->setValueSources(['data']);
  7562. $expected = '1';
  7563. $result = $this->Form->getSourceValue('id');
  7564. $this->assertEquals($expected, $result);
  7565. $this->Form->setValueSources(['query', 'data']);
  7566. $expected = '2';
  7567. $result = $this->Form->getSourceValue('id');
  7568. $this->assertEquals($expected, $result);
  7569. }
  7570. /**
  7571. * Tests the different input rendering values based on sources values switching
  7572. *
  7573. * @return void
  7574. */
  7575. public function testFormValueSourcesSingleSwitchRendering()
  7576. {
  7577. $this->loadFixtures('Articles');
  7578. $articles = TableRegistry::get('Articles');
  7579. $article = new Article();
  7580. $articles->patchEntity($article, ['id' => '3']);
  7581. $this->Form->create($article);
  7582. $this->Form->setValueSources(['context']);
  7583. $result = $this->Form->input('id');
  7584. $expected = [
  7585. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7586. ];
  7587. $this->assertHtml($expected, $result);
  7588. $this->Form->request->query['id'] = '5';
  7589. $this->Form->setValueSources(['query']);
  7590. $result = $this->Form->input('id');
  7591. $expected = [
  7592. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7593. ];
  7594. $this->assertHtml($expected, $result);
  7595. $this->Form->request->query['id'] = '5a';
  7596. $this->Form->request->data['id'] = '5b';
  7597. $this->Form->setValueSources(['context']);
  7598. $this->Form->create($article);
  7599. $result = $this->Form->input('id');
  7600. $expected = [
  7601. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7602. ];
  7603. $this->assertHtml($expected, $result);
  7604. $this->Form->setValueSources(['data']);
  7605. $this->Form->create($article);
  7606. $result = $this->Form->input('id');
  7607. $expected = [
  7608. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5b']],
  7609. ];
  7610. $this->assertHtml($expected, $result);
  7611. $this->Form->setValueSources(['query']);
  7612. $result = $this->Form->input('id');
  7613. $expected = [
  7614. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5a']],
  7615. ];
  7616. $this->assertHtml($expected, $result);
  7617. }
  7618. /**
  7619. * Tests the different input rendering values based on sources values switching while supplying
  7620. * an entity (base context) and multiple sources (such as data, query)
  7621. *
  7622. * @return void
  7623. */
  7624. public function testFormValueSourcesListSwitchRendering()
  7625. {
  7626. $this->loadFixtures('Articles');
  7627. $articles = TableRegistry::get('Articles');
  7628. $article = new Article();
  7629. $articles->patchEntity($article, ['id' => '3']);
  7630. $this->Form->request->query['id'] = '9';
  7631. $this->Form->create($article);
  7632. $this->Form->setValueSources(['context', 'query']);
  7633. $result = $this->Form->input('id');
  7634. $expected = [
  7635. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '3']],
  7636. ];
  7637. $this->assertHtml($expected, $result);
  7638. $this->Form->setValueSources(['query', 'context']);
  7639. $result = $this->Form->input('id');
  7640. $expected = [
  7641. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7642. ];
  7643. $this->assertHtml($expected, $result);
  7644. $this->Form->setValueSources(['data', 'query', 'context']);
  7645. $result = $this->Form->input('id');
  7646. $expected = [
  7647. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '9']],
  7648. ];
  7649. $this->assertHtml($expected, $result);
  7650. $this->Form->request->data['id'] = '8';
  7651. $this->Form->request->query['id'] = '9';
  7652. $this->Form->setValueSources(['data', 'query', 'context']);
  7653. $result = $this->Form->input('id');
  7654. $expected = [
  7655. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '8']],
  7656. ];
  7657. $this->assertHtml($expected, $result);
  7658. }
  7659. /**
  7660. * Test the different form input renderings based on values sources switchings through form options
  7661. *
  7662. * @return void
  7663. */
  7664. public function testFormValueSourcesSwitchViaOptionsRendering()
  7665. {
  7666. $this->loadFixtures('Articles');
  7667. $articles = TableRegistry::get('Articles');
  7668. $article = new Article();
  7669. $articles->patchEntity($article, ['id' => '3']);
  7670. $this->Form->request->data['id'] = '4';
  7671. $this->Form->request->query['id'] = '5';
  7672. $this->Form->create($article, ['valueSources' => 'query']);
  7673. $result = $this->Form->input('id');
  7674. $expected = [
  7675. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7676. ];
  7677. $this->assertHtml($expected, $result);
  7678. $result = $this->Form->getSourceValue('id');
  7679. $this->assertEquals('5', $result);
  7680. $this->Form->setValueSources(['context']);
  7681. $this->Form->create($article, ['valueSources' => 'query']);
  7682. $result = $this->Form->input('id');
  7683. $expected = [
  7684. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '5']],
  7685. ];
  7686. $this->assertHtml($expected, $result);
  7687. $result = $this->Form->getSourceValue('id');
  7688. $this->assertEquals('5', $result);
  7689. $this->Form->setValueSources(['query']);
  7690. $this->Form->create($article, ['valueSources' => 'data']);
  7691. $result = $this->Form->input('id');
  7692. $expected = [
  7693. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7694. ];
  7695. $this->assertHtml($expected, $result);
  7696. $result = $this->Form->getSourceValue('id');
  7697. $this->assertEquals('4', $result);
  7698. $this->Form->setValueSources(['query']);
  7699. $this->Form->create($article, ['valueSources' => ['context', 'data']]);
  7700. $result = $this->Form->input('id');
  7701. $expected = [
  7702. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '4']],
  7703. ];
  7704. $this->assertHtml($expected, $result);
  7705. $result = $this->Form->getSourceValue('id');
  7706. $this->assertEquals('4', $result);
  7707. }
  7708. /**
  7709. * Test the different form input renderings based on values sources switchings through form options
  7710. *
  7711. * @return void
  7712. */
  7713. public function testFormValueSourcesSwitchViaOptionsAndSetterRendering()
  7714. {
  7715. $this->loadFixtures('Articles');
  7716. $articles = TableRegistry::get('Articles');
  7717. $article = new Article();
  7718. $articles->patchEntity($article, ['id' => '3']);
  7719. $this->Form->request->data['id'] = '10';
  7720. $this->Form->request->query['id'] = '11';
  7721. $this->Form->setValueSources(['context'])
  7722. ->create($article, ['valueSources' => ['query', 'data']]);
  7723. $result = $this->Form->input('id');
  7724. $expected = [
  7725. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '11']],
  7726. ];
  7727. $this->assertHtml($expected, $result);
  7728. $result = $this->Form->getSourceValue('id');
  7729. $this->assertEquals('11', $result);
  7730. unset($this->Form->request->query['id']);
  7731. $this->Form->setValueSources(['context'])
  7732. ->create($article, ['valueSources' => ['query', 'data']]);
  7733. $result = $this->Form->input('id');
  7734. $expected = [
  7735. ['input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id', 'value' => '10']],
  7736. ];
  7737. $this->assertHtml($expected, $result);
  7738. $result = $this->Form->getSourceValue('id');
  7739. $this->assertEquals('10', $result);
  7740. }
  7741. /**
  7742. * Test the different form values sources resetting through From::end();
  7743. *
  7744. * @return void
  7745. */
  7746. public function testFormValueSourcesResetViaEnd()
  7747. {
  7748. $expected = ['context'];
  7749. $result = $this->Form->getValueSources();
  7750. $this->assertEquals($expected, $result);
  7751. $expected = ['query', 'context', 'data'];
  7752. $this->Form->setValueSources(['query', 'context', 'data']);
  7753. $result = $this->Form->getValueSources();
  7754. $this->assertEquals($expected, $result);
  7755. $this->Form->create();
  7756. $result = $this->Form->getValueSources();
  7757. $this->assertEquals($expected, $result);
  7758. $this->Form->end();
  7759. $result = $this->Form->getValueSources();
  7760. $this->assertEquals(['context'], $result);
  7761. }
  7762. /**
  7763. * Test sources values defaults handling
  7764. *
  7765. * @return void
  7766. */
  7767. public function testFormValueSourcesDefaults()
  7768. {
  7769. $this->Form->request->query['password'] = 'open Sesame';
  7770. $this->Form->create();
  7771. $result = $this->Form->password('password');
  7772. $expected = ['input' => ['type' => 'password', 'name' => 'password']];
  7773. $this->assertHtml($expected, $result);
  7774. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7775. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7776. $this->assertHtml($expected, $result);
  7777. $this->Form->setValueSources('query');
  7778. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7779. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'open Sesame']];
  7780. $this->assertHtml($expected, $result);
  7781. $this->Form->setValueSources('data');
  7782. $result = $this->Form->password('password', ['default' => 'helloworld']);
  7783. $expected = ['input' => ['type' => 'password', 'name' => 'password', 'value' => 'helloworld']];
  7784. $this->assertHtml($expected, $result);
  7785. }
  7786. /**
  7787. * Test sources values schema defaults handling
  7788. *
  7789. * @return void
  7790. */
  7791. public function testSourcesValueDoesntExistPassThrough()
  7792. {
  7793. $this->Form->request->query['category'] = 'sesame-cookies';
  7794. $articles = TableRegistry::get('Articles');
  7795. $entity = $articles->newEntity();
  7796. $this->Form->create($entity);
  7797. $this->Form->setValueSources(['query', 'context']);
  7798. $result = $this->Form->getSourceValue('category');
  7799. $this->assertEquals('sesame-cookies', $result);
  7800. $this->Form->setValueSources(['context', 'query']);
  7801. $result = $this->Form->getSourceValue('category');
  7802. $this->assertEquals('sesame-cookies', $result);
  7803. }
  7804. }