FormHelperTest.php 240 KB

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