FormHelperTest.php 241 KB

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