FormHelperTest.php 240 KB

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