FormHelperTest.php 242 KB

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