FormHelperTest.php 244 KB

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