FormHelperTest.php 239 KB

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