FormHelperTest.php 242 KB

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