FormHelperTest.php 233 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Plugin;
  21. use Cake\Form\Form;
  22. use Cake\Network\Request;
  23. use Cake\ORM\Entity;
  24. use Cake\ORM\Table;
  25. use Cake\ORM\TableRegistry;
  26. use Cake\Routing\Router;
  27. use Cake\TestSuite\TestCase;
  28. use Cake\Utility\Security;
  29. use Cake\View\Helper\FormHelper;
  30. use Cake\View\Helper\HtmlHelper;
  31. use Cake\View\View;
  32. /**
  33. * Test stub.
  34. */
  35. class Article extends Entity
  36. {
  37. }
  38. /**
  39. * Contact class
  40. *
  41. */
  42. class ContactsTable extends Table
  43. {
  44. /**
  45. * Default schema
  46. *
  47. * @var array
  48. */
  49. protected $_schema = [
  50. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  51. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  52. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  53. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  54. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  55. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  56. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  57. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  58. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  59. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  60. ];
  61. /**
  62. * Initializes the schema
  63. *
  64. * @return void
  65. */
  66. public function initialize(array $config)
  67. {
  68. $this->schema($this->_schema);
  69. }
  70. }
  71. /**
  72. * ValidateUser class
  73. *
  74. */
  75. class ValidateUsersTable extends Table
  76. {
  77. /**
  78. * schema method
  79. *
  80. * @var array
  81. */
  82. protected $_schema = [
  83. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  84. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  85. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  86. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  87. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  88. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  89. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  90. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  91. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  92. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  93. ];
  94. /**
  95. * Initializes the schema
  96. *
  97. * @return void
  98. */
  99. public function initialize(array $config)
  100. {
  101. $this->schema($this->_schema);
  102. }
  103. }
  104. /**
  105. * FormHelperTest class
  106. *
  107. * @property FormHelper $Form
  108. */
  109. class FormHelperTest extends TestCase
  110. {
  111. /**
  112. * Fixtures to be used
  113. *
  114. * @var array
  115. */
  116. public $fixtures = ['core.articles', 'core.comments'];
  117. /**
  118. * Do not load the fixtures by default
  119. *
  120. * @var bool
  121. */
  122. public $autoFixtures = false;
  123. /**
  124. * setUp method
  125. *
  126. * @return void
  127. */
  128. public function setUp()
  129. {
  130. parent::setUp();
  131. Configure::write('Config.language', 'eng');
  132. Configure::write('App.base', '');
  133. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  134. Configure::delete('Asset');
  135. $this->View = new View();
  136. $this->Form = new FormHelper($this->View);
  137. $request = new Request('articles/add');
  138. $request->here = '/articles/add';
  139. $request['controller'] = 'articles';
  140. $request['action'] = 'add';
  141. $request->webroot = '';
  142. $request->base = '';
  143. $this->Form->Url->request = $this->Form->request = $request;
  144. $this->dateRegex = [
  145. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  146. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  147. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  148. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  149. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  150. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  151. ];
  152. $this->article = [
  153. 'schema' => [
  154. 'id' => ['type' => 'integer'],
  155. 'author_id' => ['type' => 'integer', 'null' => true],
  156. 'title' => ['type' => 'string', 'null' => true],
  157. 'body' => 'text',
  158. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  159. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  160. ],
  161. 'required' => [
  162. 'author_id' => true,
  163. 'title' => true,
  164. ]
  165. ];
  166. Security::salt('foo!');
  167. Router::connect('/:controller', ['action' => 'index']);
  168. Router::connect('/:controller/:action/*');
  169. }
  170. /**
  171. * tearDown method
  172. *
  173. * @return void
  174. */
  175. public function tearDown()
  176. {
  177. parent::tearDown();
  178. unset($this->Form, $this->Controller, $this->View);
  179. TableRegistry::clear();
  180. }
  181. /**
  182. * Test construct() with the templates option.
  183. *
  184. * @return void
  185. */
  186. public function testConstructTemplatesFile()
  187. {
  188. $helper = new FormHelper($this->View, [
  189. 'templates' => 'htmlhelper_tags'
  190. ]);
  191. $result = $helper->input('name');
  192. $this->assertContains('<input', $result);
  193. }
  194. /**
  195. * Test that when specifying custom widgets the config array for that widget
  196. * is overwritten instead of merged.
  197. *
  198. * @return void
  199. */
  200. public function testConstructWithWidgets()
  201. {
  202. $config = [
  203. 'widgets' => [
  204. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  205. ]
  206. ];
  207. $helper = new FormHelper($this->View, $config);
  208. $registry = $helper->widgetRegistry();
  209. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  210. }
  211. /**
  212. * Test that when specifying custom widgets config file and it should be
  213. * added to widgets array. WidgetRegistry will load widgets in constructor.
  214. *
  215. * @return void
  216. */
  217. public function testConstructWithWidgetsConfig()
  218. {
  219. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  220. $registry = $helper->widgetRegistry();
  221. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  222. }
  223. /**
  224. * Test registering a new widget class and rendering it.
  225. *
  226. * @return void
  227. */
  228. public function testAddWidgetAndRenderWidget()
  229. {
  230. $data = [
  231. 'val' => 1
  232. ];
  233. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  234. $this->assertNull($this->Form->addWidget('test', $mock));
  235. $mock->expects($this->once())
  236. ->method('render')
  237. ->with($data)
  238. ->will($this->returnValue('HTML'));
  239. $result = $this->Form->widget('test', $data);
  240. $this->assertEquals('HTML', $result);
  241. }
  242. /**
  243. * Test that secureFields() of widget is called after calling render(),
  244. * not before.
  245. *
  246. * @return void
  247. */
  248. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  249. {
  250. $data = [
  251. 'val' => 1,
  252. 'name' => 'test'
  253. ];
  254. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  255. $this->assertNull($this->Form->addWidget('test', $mock));
  256. $mock->expects($this->at(0))
  257. ->method('render')
  258. ->with($data)
  259. ->will($this->returnValue('HTML'));
  260. $mock->expects($this->at(1))
  261. ->method('secureFields')
  262. ->with($data)
  263. ->will($this->returnValue(['test']));
  264. $result = $this->Form->widget('test', $data + ['secure' => true]);
  265. $this->assertEquals('HTML', $result);
  266. }
  267. /**
  268. * Test that empty string is not added to secure fields list when
  269. * rendering input widget without name.
  270. *
  271. * @return void
  272. */
  273. public function testRenderingWidgetWithEmptyName()
  274. {
  275. $this->assertEquals([], $this->Form->fields);
  276. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  277. $this->assertEquals('<select name=""></select>', $result);
  278. $this->assertEquals([], $this->Form->fields);
  279. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  280. $this->assertEquals('<select name="0"></select>', $result);
  281. $this->assertEquals(['0'], $this->Form->fields);
  282. }
  283. /**
  284. * Test registering an invalid widget class.
  285. *
  286. * @expectedException \RuntimeException
  287. * @return void
  288. */
  289. public function testAddWidgetInvalid()
  290. {
  291. $mock = new \StdClass();
  292. $this->Form->addWidget('test', $mock);
  293. $this->Form->widget('test');
  294. }
  295. /**
  296. * Test adding a new context class.
  297. *
  298. * @return void
  299. */
  300. public function testAddContextProvider()
  301. {
  302. $context = 'My data';
  303. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  304. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  305. $this->assertInstanceOf('Cake\Network\Request', $request);
  306. $this->assertEquals($context, $data['entity']);
  307. return $stub;
  308. });
  309. $this->Form->create($context);
  310. $result = $this->Form->context();
  311. $this->assertSame($stub, $result);
  312. }
  313. /**
  314. * Test replacing a context class.
  315. *
  316. * @return void
  317. */
  318. public function testAddContextProviderReplace()
  319. {
  320. $entity = new Article();
  321. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  322. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  323. return $stub;
  324. });
  325. $this->Form->create($entity);
  326. $result = $this->Form->context();
  327. $this->assertSame($stub, $result);
  328. }
  329. /**
  330. * Test overriding a context class.
  331. *
  332. * @return void
  333. */
  334. public function testAddContextProviderAdd()
  335. {
  336. $entity = new Article();
  337. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  338. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  339. if ($data['entity'] instanceof Entity) {
  340. return $stub;
  341. }
  342. });
  343. $this->Form->create($entity);
  344. $result = $this->Form->context();
  345. $this->assertSame($stub, $result);
  346. }
  347. /**
  348. * Test adding an invalid context class.
  349. *
  350. * @expectedException \RuntimeException
  351. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  352. * @return void
  353. */
  354. public function testAddContextProviderInvalid()
  355. {
  356. $context = 'My data';
  357. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  358. return new \StdClass();
  359. });
  360. $this->Form->create($context);
  361. }
  362. /**
  363. * Provides context options for create().
  364. *
  365. * @return array
  366. */
  367. public function contextSelectionProvider()
  368. {
  369. $entity = new Article();
  370. $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
  371. $emptyCollection = new Collection([]);
  372. $emptyArray = [];
  373. $arrayObject = new \ArrayObject([]);
  374. $data = [
  375. 'schema' => [
  376. 'title' => ['type' => 'string']
  377. ]
  378. ];
  379. $form = new Form();
  380. return [
  381. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  382. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  383. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  384. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  385. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  386. 'form' => [$form, 'Cake\View\Form\FormContext'],
  387. 'none' => [null, 'Cake\View\Form\NullContext'],
  388. 'false' => [false, 'Cake\View\Form\NullContext'],
  389. ];
  390. }
  391. /**
  392. * Test default context selection in create()
  393. *
  394. * @dataProvider contextSelectionProvider
  395. * @return void
  396. */
  397. public function testCreateContextSelectionBuiltIn($data, $class)
  398. {
  399. $this->loadFixtures('Articles');
  400. $this->Form->create($data);
  401. $this->assertInstanceOf($class, $this->Form->context());
  402. }
  403. /**
  404. * Data provider for type option.
  405. *
  406. * @return array
  407. */
  408. public static function requestTypeProvider()
  409. {
  410. return [
  411. // type, method, override
  412. ['post', 'post', 'POST'],
  413. ['put', 'post', 'PUT'],
  414. ['patch', 'post', 'PATCH'],
  415. ['delete', 'post', 'DELETE'],
  416. ];
  417. }
  418. /**
  419. * Test creating file forms.
  420. *
  421. * @return void
  422. */
  423. public function testCreateFile()
  424. {
  425. $encoding = strtolower(Configure::read('App.encoding'));
  426. $result = $this->Form->create(false, ['type' => 'file']);
  427. $expected = [
  428. 'form' => [
  429. 'method' => 'post', 'action' => '/articles/add',
  430. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  431. ],
  432. 'div' => ['style' => 'display:none;'],
  433. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  434. '/div'
  435. ];
  436. $this->assertHtml($expected, $result);
  437. }
  438. /**
  439. * Test creating GET forms.
  440. *
  441. * @return void
  442. */
  443. public function testCreateGet()
  444. {
  445. $encoding = strtolower(Configure::read('App.encoding'));
  446. $result = $this->Form->create(false, ['type' => 'get']);
  447. $expected = ['form' => [
  448. 'method' => 'get', 'action' => '/articles/add',
  449. 'accept-charset' => $encoding
  450. ]];
  451. $this->assertHtml($expected, $result);
  452. }
  453. /**
  454. * Test create() with the templates option.
  455. *
  456. * @return void
  457. */
  458. public function testCreateTemplatesArray()
  459. {
  460. $result = $this->Form->create($this->article, [
  461. 'templates' => [
  462. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  463. ]
  464. ]);
  465. $expected = [
  466. 'form' => [
  467. 'class' => 'form-horizontal',
  468. 'method' => 'post',
  469. 'action' => '/articles/add',
  470. 'accept-charset' => 'utf-8'
  471. ]
  472. ];
  473. $this->assertHtml($expected, $result);
  474. }
  475. /**
  476. * Test create() with the templates option.
  477. *
  478. * @return void
  479. */
  480. public function testCreateTemplatesFile()
  481. {
  482. $result = $this->Form->create($this->article, [
  483. 'templates' => 'htmlhelper_tags',
  484. ]);
  485. $expected = [
  486. 'start form',
  487. 'div' => ['class' => 'hidden'],
  488. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  489. '/div'
  490. ];
  491. $this->assertHtml($expected, $result);
  492. }
  493. /**
  494. * Test that create() and end() restore templates.
  495. *
  496. * @return void
  497. */
  498. public function testCreateEndRestoreTemplates()
  499. {
  500. $this->Form->create($this->article, [
  501. 'templates' => ['input' => 'custom input element']
  502. ]);
  503. $this->Form->end();
  504. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  505. }
  506. /**
  507. * test 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' => ['notEmpty', 'email', 'Something else']
  3342. ];
  3343. $this->Form->create($this->article);
  3344. $result = $this->Form->error('field', [
  3345. 'notEmpty' => '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. 'label' => false
  3499. ]);
  3500. $expected = [
  3501. ['div' => ['class' => 'input radio']],
  3502. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  3503. ['label' => ['for' => 'test-0']],
  3504. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  3505. 'A',
  3506. '/label',
  3507. ['label' => ['for' => 'test-1']],
  3508. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  3509. 'B',
  3510. '/label',
  3511. '/div',
  3512. ];
  3513. $this->assertHtml($expected, $result);
  3514. }
  3515. /**
  3516. * Test that radio() works with label = false
  3517. *
  3518. * @return void
  3519. */
  3520. public function testRadioNoLabel()
  3521. {
  3522. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  3523. $expected = [
  3524. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3525. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  3526. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  3527. ];
  3528. $this->assertHtml($expected, $result);
  3529. }
  3530. /**
  3531. * test generating radio input inside label ala twitter bootstrap
  3532. *
  3533. * @return void
  3534. */
  3535. public function testRadioInputInsideLabel()
  3536. {
  3537. $this->Form->templates([
  3538. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3539. 'radioWrapper' => '{{label}}'
  3540. ]);
  3541. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3542. //@codingStandardsIgnoreStart
  3543. $expected = [
  3544. ['input' => [
  3545. 'type' => 'hidden',
  3546. 'name' => 'Model[field]',
  3547. 'value' => ''
  3548. ]],
  3549. ['label' => ['for' => 'model-field-0']],
  3550. ['input' => [
  3551. 'type' => 'radio',
  3552. 'name' => 'Model[field]',
  3553. 'value' => '0',
  3554. 'id' => 'model-field-0'
  3555. ]],
  3556. 'option A',
  3557. '/label',
  3558. ['label' => ['for' => 'model-field-1']],
  3559. ['input' => [
  3560. 'type' => 'radio',
  3561. 'name' => 'Model[field]',
  3562. 'value' => '1',
  3563. 'id' => 'model-field-1'
  3564. ]],
  3565. 'option B',
  3566. '/label',
  3567. ];
  3568. //@codingStandardsIgnoreEnd
  3569. $this->assertHtml($expected, $result);
  3570. }
  3571. /**
  3572. * test disabling the hidden input for radio buttons
  3573. *
  3574. * @return void
  3575. */
  3576. public function testRadioHiddenInputDisabling()
  3577. {
  3578. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  3579. $expected = [
  3580. 'label' => ['for' => 'model-1-field-0'],
  3581. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  3582. 'option A',
  3583. '/label'
  3584. ];
  3585. $this->assertHtml($expected, $result);
  3586. }
  3587. /**
  3588. * testSelect method
  3589. *
  3590. * Test select element generation.
  3591. *
  3592. * @return void
  3593. */
  3594. public function testSelect()
  3595. {
  3596. $result = $this->Form->select('Model.field', []);
  3597. $expected = [
  3598. 'select' => ['name' => 'Model[field]'],
  3599. '/select'
  3600. ];
  3601. $this->assertHtml($expected, $result);
  3602. $this->Form->request->data = ['Model' => ['field' => 'value']];
  3603. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  3604. $expected = [
  3605. 'select' => ['name' => 'Model[field]'],
  3606. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3607. 'good',
  3608. '/option',
  3609. ['option' => ['value' => 'other']],
  3610. 'bad',
  3611. '/option',
  3612. '/select'
  3613. ];
  3614. $this->assertHtml($expected, $result);
  3615. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  3616. $this->assertHtml($expected, $result);
  3617. $this->Form->request->data = [];
  3618. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  3619. $expected = [
  3620. 'select' => ['name' => 'Model[field]'],
  3621. ['option' => ['value' => 'value']],
  3622. 'good',
  3623. '/option',
  3624. ['option' => ['value' => 'other']],
  3625. 'bad',
  3626. '/option',
  3627. '/select'
  3628. ];
  3629. $this->assertHtml($expected, $result);
  3630. $options = [
  3631. ['value' => 'first', 'text' => 'First'],
  3632. ['value' => 'first', 'text' => 'Another First'],
  3633. ];
  3634. $result = $this->Form->select(
  3635. 'Model.field',
  3636. $options,
  3637. ['escape' => false, 'empty' => false]
  3638. );
  3639. $expected = [
  3640. 'select' => ['name' => 'Model[field]'],
  3641. ['option' => ['value' => 'first']],
  3642. 'First',
  3643. '/option',
  3644. ['option' => ['value' => 'first']],
  3645. 'Another First',
  3646. '/option',
  3647. '/select'
  3648. ];
  3649. $this->assertHtml($expected, $result);
  3650. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  3651. $result = $this->Form->select(
  3652. 'Model.contact_id',
  3653. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  3654. ['escape' => false, 'empty' => 'pick something']
  3655. );
  3656. $expected = [
  3657. 'select' => ['name' => 'Model[contact_id]'],
  3658. ['option' => ['value' => '']], 'pick something', '/option',
  3659. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  3660. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  3661. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  3662. '/select'
  3663. ];
  3664. $this->assertHtml($expected, $result);
  3665. $this->Form->request->data['Model']['field'] = 0;
  3666. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  3667. $expected = [
  3668. 'select' => ['name' => 'Model[field]'],
  3669. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  3670. ['option' => ['value' => '1']], 'Yes', '/option',
  3671. '/select'
  3672. ];
  3673. $this->assertHtml($expected, $result);
  3674. }
  3675. /**
  3676. * Test that select() escapes HTML.
  3677. *
  3678. * @return void
  3679. */
  3680. public function testSelectEscapeHtml()
  3681. {
  3682. $result = $this->Form->select(
  3683. 'Model.field',
  3684. ['first' => 'first "html" <chars>', 'second' => 'value'],
  3685. ['empty' => false]
  3686. );
  3687. $expected = [
  3688. 'select' => ['name' => 'Model[field]'],
  3689. ['option' => ['value' => 'first']],
  3690. 'first &quot;html&quot; &lt;chars&gt;',
  3691. '/option',
  3692. ['option' => ['value' => 'second']],
  3693. 'value',
  3694. '/option',
  3695. '/select'
  3696. ];
  3697. $this->assertHtml($expected, $result);
  3698. $result = $this->Form->select(
  3699. 'Model.field',
  3700. ['first' => 'first "html" <chars>', 'second' => 'value'],
  3701. ['escape' => false, 'empty' => false]
  3702. );
  3703. $expected = [
  3704. 'select' => ['name' => 'Model[field]'],
  3705. ['option' => ['value' => 'first']],
  3706. 'first "html" <chars>',
  3707. '/option',
  3708. ['option' => ['value' => 'second']],
  3709. 'value',
  3710. '/option',
  3711. '/select'
  3712. ];
  3713. $this->assertHtml($expected, $result);
  3714. }
  3715. /**
  3716. * test select() with required and disabled attributes.
  3717. *
  3718. * @return void
  3719. */
  3720. public function testSelectRequired()
  3721. {
  3722. $this->article['required'] = [
  3723. 'user_id' => true
  3724. ];
  3725. $this->Form->create($this->article);
  3726. $result = $this->Form->select('user_id', ['option A']);
  3727. $expected = [
  3728. 'select' => [
  3729. 'name' => 'user_id',
  3730. 'required' => 'required'
  3731. ],
  3732. ['option' => ['value' => '0']], 'option A', '/option',
  3733. '/select'
  3734. ];
  3735. $this->assertHtml($expected, $result);
  3736. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  3737. $expected = [
  3738. 'select' => [
  3739. 'name' => 'user_id',
  3740. 'disabled' => 'disabled'
  3741. ],
  3742. ['option' => ['value' => '0']], 'option A', '/option',
  3743. '/select'
  3744. ];
  3745. $this->assertHtml($expected, $result);
  3746. }
  3747. /**
  3748. * testNestedSelect method
  3749. *
  3750. * test select element generation with optgroups
  3751. *
  3752. * @return void
  3753. */
  3754. public function testNestedSelect()
  3755. {
  3756. $result = $this->Form->select(
  3757. 'Model.field',
  3758. [1 => 'One', 2 => 'Two', 'Three' => [
  3759. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3760. ]],
  3761. ['empty' => false]
  3762. );
  3763. $expected = [
  3764. 'select' => ['name' => 'Model[field]'],
  3765. ['option' => ['value' => 1]],
  3766. 'One',
  3767. '/option',
  3768. ['option' => ['value' => 2]],
  3769. 'Two',
  3770. '/option',
  3771. ['optgroup' => ['label' => 'Three']],
  3772. ['option' => ['value' => 3]],
  3773. 'Three',
  3774. '/option',
  3775. ['option' => ['value' => 4]],
  3776. 'Four',
  3777. '/option',
  3778. ['option' => ['value' => 5]],
  3779. 'Five',
  3780. '/option',
  3781. '/optgroup',
  3782. '/select'
  3783. ];
  3784. $this->assertHtml($expected, $result);
  3785. }
  3786. /**
  3787. * testSelectMultiple method
  3788. *
  3789. * test generation of multiple select elements
  3790. *
  3791. * @return void
  3792. */
  3793. public function testSelectMultiple()
  3794. {
  3795. $options = ['first', 'second', 'third'];
  3796. $result = $this->Form->select(
  3797. 'Model.multi_field',
  3798. $options,
  3799. ['form' => 'my-form', 'multiple' => true]
  3800. );
  3801. $expected = [
  3802. 'input' => [
  3803. 'type' => 'hidden',
  3804. 'name' => 'Model[multi_field]',
  3805. 'value' => '',
  3806. 'form' => 'my-form',
  3807. ],
  3808. 'select' => [
  3809. 'name' => 'Model[multi_field][]',
  3810. 'multiple' => 'multiple',
  3811. 'form' => 'my-form',
  3812. ],
  3813. ['option' => ['value' => '0']],
  3814. 'first',
  3815. '/option',
  3816. ['option' => ['value' => '1']],
  3817. 'second',
  3818. '/option',
  3819. ['option' => ['value' => '2']],
  3820. 'third',
  3821. '/option',
  3822. '/select'
  3823. ];
  3824. $this->assertHtml($expected, $result);
  3825. $result = $this->Form->select(
  3826. 'Model.multi_field',
  3827. $options,
  3828. ['multiple' => 'multiple', 'form' => 'my-form']
  3829. );
  3830. $this->assertHtml($expected, $result);
  3831. }
  3832. /**
  3833. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  3834. *
  3835. * @return void
  3836. */
  3837. public function testCheckboxZeroValue()
  3838. {
  3839. $result = $this->Form->input('User.get_spam', [
  3840. 'type' => 'checkbox',
  3841. 'value' => '0',
  3842. 'hiddenField' => '1',
  3843. ]);
  3844. $expected = [
  3845. 'div' => ['class' => 'input checkbox'],
  3846. 'label' => ['for' => 'user-get-spam'],
  3847. ['input' => [
  3848. 'type' => 'hidden', 'name' => 'User[get_spam]',
  3849. 'value' => '1'
  3850. ]],
  3851. ['input' => [
  3852. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  3853. 'value' => '0', 'id' => 'user-get-spam'
  3854. ]],
  3855. 'Get Spam',
  3856. '/label',
  3857. '/div'
  3858. ];
  3859. $this->assertHtml($expected, $result);
  3860. }
  3861. /**
  3862. * test generation of habtm select boxes.
  3863. *
  3864. * @return void
  3865. */
  3866. public function testHabtmSelectBox()
  3867. {
  3868. $this->loadFixtures('Articles');
  3869. $options = [
  3870. 1 => 'blue',
  3871. 2 => 'red',
  3872. 3 => 'green'
  3873. ];
  3874. $tags = [
  3875. new Entity(['id' => 1, 'name' => 'blue']),
  3876. new Entity(['id' => 3, 'name' => 'green'])
  3877. ];
  3878. $article = new Article(['tags' => $tags]);
  3879. $this->Form->create($article);
  3880. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3881. $expected = [
  3882. 'div' => ['class' => 'input select'],
  3883. 'label' => ['for' => 'tags-ids'],
  3884. 'Tags',
  3885. '/label',
  3886. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3887. 'select' => [
  3888. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3889. 'multiple' => 'multiple'
  3890. ],
  3891. ['option' => ['value' => '1', 'selected' => 'selected']],
  3892. 'blue',
  3893. '/option',
  3894. ['option' => ['value' => '2']],
  3895. 'red',
  3896. '/option',
  3897. ['option' => ['value' => '3', 'selected' => 'selected']],
  3898. 'green',
  3899. '/option',
  3900. '/select',
  3901. '/div'
  3902. ];
  3903. $this->assertHtml($expected, $result);
  3904. // make sure only 50 is selected, and not 50f5c0cf
  3905. $options = [
  3906. '1' => 'blue',
  3907. '50f5c0cf' => 'red',
  3908. '50' => 'green'
  3909. ];
  3910. $tags = [
  3911. new Entity(['id' => 1, 'name' => 'blue']),
  3912. new Entity(['id' => 50, 'name' => 'green'])
  3913. ];
  3914. $article = new Article(['tags' => $tags]);
  3915. $this->Form->create($article);
  3916. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3917. $expected = [
  3918. 'div' => ['class' => 'input select'],
  3919. 'label' => ['for' => 'tags-ids'],
  3920. 'Tags',
  3921. '/label',
  3922. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3923. 'select' => [
  3924. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3925. 'multiple' => 'multiple'
  3926. ],
  3927. ['option' => ['value' => '1', 'selected' => 'selected']],
  3928. 'blue',
  3929. '/option',
  3930. ['option' => ['value' => '50f5c0cf']],
  3931. 'red',
  3932. '/option',
  3933. ['option' => ['value' => '50', 'selected' => 'selected']],
  3934. 'green',
  3935. '/option',
  3936. '/select',
  3937. '/div'
  3938. ];
  3939. $this->assertHtml($expected, $result);
  3940. $spacecraft = [
  3941. 1 => 'Orion',
  3942. 2 => 'Helios'
  3943. ];
  3944. $this->View->viewVars['spacecraft'] = $spacecraft;
  3945. $this->Form->create();
  3946. $result = $this->Form->input('spacecraft._ids');
  3947. $expected = [
  3948. 'div' => ['class' => 'input select'],
  3949. 'label' => ['for' => 'spacecraft-ids'],
  3950. 'Spacecraft',
  3951. '/label',
  3952. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  3953. 'select' => [
  3954. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  3955. 'multiple' => 'multiple'
  3956. ],
  3957. ['option' => ['value' => '1']],
  3958. 'Orion',
  3959. '/option',
  3960. ['option' => ['value' => '2']],
  3961. 'Helios',
  3962. '/option',
  3963. '/select',
  3964. '/div'
  3965. ];
  3966. $this->assertHtml($expected, $result);
  3967. }
  3968. /**
  3969. * Tests that errors for belongsToMany select fields are being
  3970. * picked up properly.
  3971. *
  3972. * @return void
  3973. */
  3974. public function testErrorsForBelongsToManySelect()
  3975. {
  3976. $spacecraft = [
  3977. 1 => 'Orion',
  3978. 2 => 'Helios'
  3979. ];
  3980. $this->View->viewVars['spacecraft'] = $spacecraft;
  3981. $article = new Article();
  3982. $article->errors('spacecraft', ['Invalid']);
  3983. $this->Form->create($article);
  3984. $result = $this->Form->input('spacecraft._ids');
  3985. $expected = [
  3986. ['div' => ['class' => 'input select error']],
  3987. 'label' => ['for' => 'spacecraft-ids'],
  3988. 'Spacecraft',
  3989. '/label',
  3990. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  3991. 'select' => [
  3992. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  3993. 'multiple' => 'multiple'
  3994. ],
  3995. ['option' => ['value' => '1']],
  3996. 'Orion',
  3997. '/option',
  3998. ['option' => ['value' => '2']],
  3999. 'Helios',
  4000. '/option',
  4001. '/select',
  4002. ['div' => ['class' => 'error-message']],
  4003. 'Invalid',
  4004. '/div',
  4005. '/div'
  4006. ];
  4007. $this->assertHtml($expected, $result);
  4008. }
  4009. /**
  4010. * test generation of multi select elements in checkbox format
  4011. *
  4012. * @return void
  4013. */
  4014. public function testSelectMultipleCheckboxes()
  4015. {
  4016. $result = $this->Form->select(
  4017. 'Model.multi_field',
  4018. ['first', 'second', 'third'],
  4019. ['multiple' => 'checkbox']
  4020. );
  4021. $expected = [
  4022. 'input' => [
  4023. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4024. ],
  4025. ['div' => ['class' => 'checkbox']],
  4026. ['label' => ['for' => 'model-multi-field-0']],
  4027. ['input' => [
  4028. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4029. 'value' => '0', 'id' => 'model-multi-field-0'
  4030. ]],
  4031. 'first',
  4032. '/label',
  4033. '/div',
  4034. ['div' => ['class' => 'checkbox']],
  4035. ['label' => ['for' => 'model-multi-field-1']],
  4036. ['input' => [
  4037. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4038. 'value' => '1', 'id' => 'model-multi-field-1'
  4039. ]],
  4040. 'second',
  4041. '/label',
  4042. '/div',
  4043. ['div' => ['class' => 'checkbox']],
  4044. ['label' => ['for' => 'model-multi-field-2']],
  4045. ['input' => [
  4046. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4047. 'value' => '2', 'id' => 'model-multi-field-2'
  4048. ]],
  4049. 'third',
  4050. '/label',
  4051. '/div'
  4052. ];
  4053. $this->assertHtml($expected, $result);
  4054. $result = $this->Form->select(
  4055. 'Model.multi_field',
  4056. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4057. ['multiple' => 'checkbox']
  4058. );
  4059. $expected = [
  4060. 'input' => [
  4061. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4062. ],
  4063. ['div' => ['class' => 'checkbox']],
  4064. ['label' => ['for' => 'model-multi-field-a+']],
  4065. ['input' => [
  4066. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4067. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4068. ]],
  4069. 'first',
  4070. '/label',
  4071. '/div',
  4072. ['div' => ['class' => 'checkbox']],
  4073. ['label' => ['for' => 'model-multi-field-a++']],
  4074. ['input' => [
  4075. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4076. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4077. ]],
  4078. 'second',
  4079. '/label',
  4080. '/div',
  4081. ['div' => ['class' => 'checkbox']],
  4082. ['label' => ['for' => 'model-multi-field-a+++']],
  4083. ['input' => [
  4084. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4085. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4086. ]],
  4087. 'third',
  4088. '/label',
  4089. '/div'
  4090. ];
  4091. $this->assertHtml($expected, $result);
  4092. $result = $this->Form->select(
  4093. 'Model.multi_field',
  4094. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4095. ['multiple' => 'checkbox']
  4096. );
  4097. $expected = [
  4098. 'input' => [
  4099. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4100. ],
  4101. ['div' => ['class' => 'checkbox']],
  4102. ['label' => ['for' => 'model-multi-field-a-b']],
  4103. ['input' => [
  4104. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4105. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4106. ]],
  4107. 'first',
  4108. '/label',
  4109. '/div',
  4110. ['div' => ['class' => 'checkbox']],
  4111. ['label' => ['for' => 'model-multi-field-a-b1']],
  4112. ['input' => [
  4113. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4114. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4115. ]],
  4116. 'second',
  4117. '/label',
  4118. '/div',
  4119. ['div' => ['class' => 'checkbox']],
  4120. ['label' => ['for' => 'model-multi-field-a-b2']],
  4121. ['input' => [
  4122. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4123. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4124. ]],
  4125. 'third',
  4126. '/label',
  4127. '/div'
  4128. ];
  4129. $this->assertHtml($expected, $result);
  4130. }
  4131. /**
  4132. * Ensure that multiCheckbox reads from the request data.
  4133. *
  4134. * @return void
  4135. */
  4136. public function testSelectMultipleCheckboxRequestData()
  4137. {
  4138. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  4139. $result = $this->Form->select(
  4140. 'Model.tags',
  4141. ['1' => 'first', 'Array' => 'Array'],
  4142. ['multiple' => 'checkbox']
  4143. );
  4144. $expected = [
  4145. 'input' => [
  4146. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4147. ],
  4148. ['div' => ['class' => 'checkbox']],
  4149. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4150. ['input' => [
  4151. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4152. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4153. ]],
  4154. 'first',
  4155. '/label',
  4156. '/div',
  4157. ['div' => ['class' => 'checkbox']],
  4158. ['label' => ['for' => 'model-tags-array']],
  4159. ['input' => [
  4160. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4161. 'value' => 'Array', 'id' => 'model-tags-array'
  4162. ]],
  4163. 'Array',
  4164. '/label',
  4165. '/div'
  4166. ];
  4167. $this->assertHtml($expected, $result);
  4168. }
  4169. /**
  4170. * Checks the security hash array generated for multiple-input checkbox elements
  4171. *
  4172. * @return void
  4173. */
  4174. public function testSelectMultipleCheckboxSecurity()
  4175. {
  4176. $this->Form->request->params['_Token'] = 'testKey';
  4177. $this->assertEquals([], $this->Form->fields);
  4178. $result = $this->Form->select(
  4179. 'Model.multi_field',
  4180. ['1' => 'first', '2' => 'second', '3' => 'third'],
  4181. ['multiple' => 'checkbox']
  4182. );
  4183. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  4184. $result = $this->Form->secure($this->Form->fields);
  4185. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4186. $this->assertRegExp('/"' . $key . '"/', $result);
  4187. }
  4188. /**
  4189. * Multiple select elements should always be secured as they always participate
  4190. * in the POST data.
  4191. *
  4192. * @return void
  4193. */
  4194. public function testSelectMultipleSecureWithNoOptions()
  4195. {
  4196. $this->assertEquals([], $this->Form->fields);
  4197. $this->Form->select(
  4198. 'Model.select',
  4199. [],
  4200. ['multiple' => true]
  4201. );
  4202. $this->assertEquals(['Model.select'], $this->Form->fields);
  4203. }
  4204. /**
  4205. * When a select box has no options it should not be added to the fields list
  4206. * as it always fail post validation.
  4207. *
  4208. * @return void
  4209. */
  4210. public function testSelectNoSecureWithNoOptions()
  4211. {
  4212. $this->Form->request->params['_Token'] = 'testkey';
  4213. $this->assertEquals([], $this->Form->fields);
  4214. $this->Form->select(
  4215. 'Model.select',
  4216. []
  4217. );
  4218. $this->assertEquals([], $this->Form->fields);
  4219. $this->Form->select(
  4220. 'Model.user_id',
  4221. [],
  4222. ['empty' => true]
  4223. );
  4224. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  4225. }
  4226. /**
  4227. * testInputMultipleCheckboxes method
  4228. *
  4229. * test input() resulting in multi select elements being generated.
  4230. *
  4231. * @return void
  4232. */
  4233. public function testInputMultipleCheckboxes()
  4234. {
  4235. $result = $this->Form->input('Model.multi_field', [
  4236. 'options' => ['first', 'second', 'third'],
  4237. 'multiple' => 'checkbox'
  4238. ]);
  4239. $expected = [
  4240. ['div' => ['class' => 'input select']],
  4241. ['label' => ['for' => 'model-multi-field']],
  4242. 'Multi Field',
  4243. '/label',
  4244. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  4245. ['div' => ['class' => 'checkbox']],
  4246. ['label' => ['for' => 'model-multi-field-0']],
  4247. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  4248. 'first',
  4249. '/label',
  4250. '/div',
  4251. ['div' => ['class' => 'checkbox']],
  4252. ['label' => ['for' => 'model-multi-field-1']],
  4253. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  4254. 'second',
  4255. '/label',
  4256. '/div',
  4257. ['div' => ['class' => 'checkbox']],
  4258. ['label' => ['for' => 'model-multi-field-2']],
  4259. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  4260. 'third',
  4261. '/label',
  4262. '/div',
  4263. '/div'
  4264. ];
  4265. $this->assertHtml($expected, $result);
  4266. $result = $this->Form->input('Model.multi_field', [
  4267. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  4268. 'multiple' => 'checkbox'
  4269. ]);
  4270. $expected = [
  4271. ['div' => ['class' => 'input select']],
  4272. ['label' => ['for' => 'model-multi-field']],
  4273. 'Multi Field',
  4274. '/label',
  4275. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  4276. ['div' => ['class' => 'checkbox']],
  4277. ['label' => ['for' => 'model-multi-field-a']],
  4278. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  4279. 'first',
  4280. '/label',
  4281. '/div',
  4282. ['div' => ['class' => 'checkbox']],
  4283. ['label' => ['for' => 'model-multi-field-b']],
  4284. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  4285. 'second',
  4286. '/label',
  4287. '/div',
  4288. ['div' => ['class' => 'checkbox']],
  4289. ['label' => ['for' => 'model-multi-field-c']],
  4290. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  4291. 'third',
  4292. '/label',
  4293. '/div',
  4294. '/div'
  4295. ];
  4296. $this->assertHtml($expected, $result);
  4297. }
  4298. /**
  4299. * testSelectHiddenFieldOmission method
  4300. *
  4301. * test that select() with 'hiddenField' => false omits the hidden field
  4302. *
  4303. * @return void
  4304. */
  4305. public function testSelectHiddenFieldOmission()
  4306. {
  4307. $result = $this->Form->select(
  4308. 'Model.multi_field',
  4309. ['first', 'second'],
  4310. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  4311. );
  4312. $this->assertNotContains('type="hidden"', $result);
  4313. }
  4314. /**
  4315. * test that select() with multiple = checkbox works with overriding name attribute.
  4316. *
  4317. * @return void
  4318. */
  4319. public function testSelectCheckboxMultipleOverrideName()
  4320. {
  4321. $result = $this->Form->select('category', ['1', '2'], [
  4322. 'multiple' => 'checkbox',
  4323. 'name' => 'fish',
  4324. ]);
  4325. $expected = [
  4326. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  4327. ['div' => ['class' => 'checkbox']],
  4328. ['label' => ['for' => 'fish-0']],
  4329. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  4330. '1',
  4331. '/label',
  4332. '/div',
  4333. ['div' => ['class' => 'checkbox']],
  4334. ['label' => ['for' => 'fish-1']],
  4335. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  4336. '2',
  4337. '/label',
  4338. '/div'
  4339. ];
  4340. $this->assertHtml($expected, $result);
  4341. $result = $this->Form->multiCheckbox(
  4342. 'category',
  4343. new Collection(['1', '2']),
  4344. ['name' => 'fish']
  4345. );
  4346. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  4347. 'name' => 'fish',
  4348. ]);
  4349. $this->assertHtml($expected, $result);
  4350. }
  4351. /**
  4352. * Test that input() works with multicheckbox.
  4353. *
  4354. * @return void
  4355. */
  4356. public function testInputMultiCheckbox()
  4357. {
  4358. $result = $this->Form->input('category', [
  4359. 'type' => 'multicheckbox',
  4360. 'options' => ['1', '2'],
  4361. ]);
  4362. $expected = [
  4363. ['div' => ['class' => 'input multicheckbox']],
  4364. '<label',
  4365. 'Category',
  4366. '/label',
  4367. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  4368. ['div' => ['class' => 'checkbox']],
  4369. ['label' => ['for' => 'category-0']],
  4370. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  4371. '1',
  4372. '/label',
  4373. '/div',
  4374. ['div' => ['class' => 'checkbox']],
  4375. ['label' => ['for' => 'category-1']],
  4376. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  4377. '2',
  4378. '/label',
  4379. '/div',
  4380. '/div',
  4381. ];
  4382. $this->assertHtml($expected, $result);
  4383. }
  4384. /**
  4385. * testCheckbox method
  4386. *
  4387. * Test generation of checkboxes
  4388. *
  4389. * @return void
  4390. */
  4391. public function testCheckbox()
  4392. {
  4393. $result = $this->Form->checkbox('Model.field');
  4394. $expected = [
  4395. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  4396. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  4397. ];
  4398. $this->assertHtml($expected, $result);
  4399. $result = $this->Form->checkbox('Model.field', [
  4400. 'id' => 'theID',
  4401. 'value' => 'myvalue',
  4402. 'form' => 'my-form',
  4403. ]);
  4404. $expected = [
  4405. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  4406. ['input' => [
  4407. 'type' => 'checkbox', 'name' => 'Model[field]',
  4408. 'value' => 'myvalue', 'id' => 'theID',
  4409. 'form' => 'my-form',
  4410. ]]
  4411. ];
  4412. $this->assertHtml($expected, $result);
  4413. }
  4414. /**
  4415. * testCheckboxDefaultValue method
  4416. *
  4417. * Test default value setting on checkbox() method
  4418. *
  4419. * @return void
  4420. */
  4421. public function testCheckboxDefaultValue()
  4422. {
  4423. $this->Form->request->data['Model']['field'] = false;
  4424. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  4425. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  4426. $this->assertHtml($expected, $result);
  4427. unset($this->Form->request->data['Model']['field']);
  4428. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  4429. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  4430. $this->assertHtml($expected, $result);
  4431. $this->Form->request->data['Model']['field'] = true;
  4432. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  4433. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  4434. $this->assertHtml($expected, $result);
  4435. unset($this->Form->request->data['Model']['field']);
  4436. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  4437. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  4438. $this->assertHtml($expected, $result);
  4439. }
  4440. /**
  4441. * Test checkbox being checked or having errors.
  4442. *
  4443. * @return void
  4444. */
  4445. public function testCheckboxCheckedAndError()
  4446. {
  4447. $this->article['errors'] = [
  4448. 'published' => true
  4449. ];
  4450. $this->Form->request->data['published'] = 'myvalue';
  4451. $this->Form->create($this->article);
  4452. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  4453. $expected = [
  4454. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  4455. ['input' => [
  4456. 'type' => 'checkbox',
  4457. 'name' => 'published',
  4458. 'value' => 'myvalue',
  4459. 'id' => 'theID',
  4460. 'checked' => 'checked',
  4461. 'class' => 'form-error'
  4462. ]]
  4463. ];
  4464. $this->assertHtml($expected, $result);
  4465. $this->Form->request->data['published'] = '';
  4466. $result = $this->Form->checkbox('published');
  4467. $expected = [
  4468. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  4469. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  4470. ];
  4471. $this->assertHtml($expected, $result);
  4472. }
  4473. /**
  4474. * test checkbox() with a custom name attribute
  4475. *
  4476. * @return void
  4477. */
  4478. public function testCheckboxCustomNameAttribute()
  4479. {
  4480. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  4481. $expected = [
  4482. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  4483. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  4484. ];
  4485. $this->assertHtml($expected, $result);
  4486. }
  4487. /**
  4488. * Test that the hidden input for checkboxes can be omitted or set to a
  4489. * specific value.
  4490. *
  4491. * @return void
  4492. */
  4493. public function testCheckboxHiddenField()
  4494. {
  4495. $result = $this->Form->checkbox('UserForm.something', [
  4496. 'hiddenField' => false
  4497. ]);
  4498. $expected = [
  4499. 'input' => [
  4500. 'type' => 'checkbox',
  4501. 'name' => 'UserForm[something]',
  4502. 'value' => '1'
  4503. ],
  4504. ];
  4505. $this->assertHtml($expected, $result);
  4506. $result = $this->Form->checkbox('UserForm.something', [
  4507. 'value' => 'Y',
  4508. 'hiddenField' => 'N',
  4509. ]);
  4510. $expected = [
  4511. ['input' => [
  4512. 'type' => 'hidden', 'name' => 'UserForm[something]',
  4513. 'value' => 'N'
  4514. ]],
  4515. ['input' => [
  4516. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  4517. 'value' => 'Y'
  4518. ]],
  4519. ];
  4520. $this->assertHtml($expected, $result);
  4521. }
  4522. /**
  4523. * Test the time type.
  4524. *
  4525. * @return void
  4526. */
  4527. public function testTime()
  4528. {
  4529. $result = $this->Form->time('start_time', [
  4530. 'timeFormat' => 12,
  4531. 'interval' => 5,
  4532. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  4533. ]);
  4534. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  4535. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4536. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  4537. $this->assertNotContains('year', $result);
  4538. $this->assertNotContains('month', $result);
  4539. $this->assertNotContains('day', $result);
  4540. $result = $this->Form->time('start_time', [
  4541. 'timeFormat' => 12,
  4542. 'interval' => 5,
  4543. 'value' => '2014-03-08 16:30:00'
  4544. ]);
  4545. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  4546. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4547. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  4548. $this->assertNotContains('year', $result);
  4549. $this->assertNotContains('month', $result);
  4550. $this->assertNotContains('day', $result);
  4551. }
  4552. /**
  4553. * Ensure that timeFormat=24 has no merdian.
  4554. *
  4555. * @return void.
  4556. */
  4557. public function testTimeFormat24NoMeridian()
  4558. {
  4559. $result = $this->Form->time('start_time', [
  4560. 'timeFormat' => 24,
  4561. 'interval' => 5,
  4562. 'value' => '2014-03-08 16:30:00'
  4563. ]);
  4564. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  4565. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4566. $this->assertNotContains('meridian', $result);
  4567. $this->assertNotContains('pm', $result);
  4568. $this->assertNotContains('year', $result);
  4569. $this->assertNotContains('month', $result);
  4570. $this->assertNotContains('day', $result);
  4571. }
  4572. /**
  4573. * Test the date type.
  4574. *
  4575. * @return void
  4576. */
  4577. public function testDate()
  4578. {
  4579. $result = $this->Form->date('start_day', [
  4580. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  4581. ]);
  4582. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  4583. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  4584. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  4585. $this->assertNotContains('hour', $result);
  4586. $this->assertNotContains('minute', $result);
  4587. $this->assertNotContains('second', $result);
  4588. $this->assertNotContains('meridian', $result);
  4589. }
  4590. /**
  4591. * testDateTime method
  4592. *
  4593. * Test generation of date/time select elements
  4594. *
  4595. * @return void
  4596. */
  4597. public function testDateTime()
  4598. {
  4599. extract($this->dateRegex);
  4600. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  4601. $now = strtotime('now');
  4602. $expected = [
  4603. ['select' => ['name' => 'Contact[date][year]']],
  4604. $yearsRegex,
  4605. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  4606. date('Y', $now),
  4607. '/option',
  4608. '*/select',
  4609. ['select' => ['name' => 'Contact[date][month]']],
  4610. $monthsRegex,
  4611. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  4612. date('F', $now),
  4613. '/option',
  4614. '*/select',
  4615. ['select' => ['name' => 'Contact[date][day]']],
  4616. $daysRegex,
  4617. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  4618. date('j', $now),
  4619. '/option',
  4620. '*/select',
  4621. ['select' => ['name' => 'Contact[date][hour]']],
  4622. $hoursRegex,
  4623. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  4624. date('G', $now),
  4625. '/option',
  4626. '*/select',
  4627. ['select' => ['name' => 'Contact[date][minute]']],
  4628. $minutesRegex,
  4629. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  4630. date('i', $now),
  4631. '/option',
  4632. '*/select',
  4633. ];
  4634. $this->assertHtml($expected, $result);
  4635. }
  4636. /**
  4637. * Test that datetime fields are added to protected fields list.
  4638. *
  4639. * @return void
  4640. */
  4641. public function testDateTimeSecured()
  4642. {
  4643. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4644. $this->Form->dateTime('Contact.date');
  4645. $expected = [
  4646. 'Contact.date.year',
  4647. 'Contact.date.month',
  4648. 'Contact.date.day',
  4649. 'Contact.date.hour',
  4650. 'Contact.date.minute',
  4651. ];
  4652. $this->assertEquals($expected, $this->Form->fields);
  4653. $this->Form->fields = [];
  4654. $this->Form->date('Contact.published');
  4655. $expected = [
  4656. 'Contact.published.year',
  4657. 'Contact.published.month',
  4658. 'Contact.published.day',
  4659. ];
  4660. $this->assertEquals($expected, $this->Form->fields);
  4661. }
  4662. /**
  4663. * Test that datetime fields are added to protected fields list.
  4664. *
  4665. * @return void
  4666. */
  4667. public function testDateTimeSecuredDisabled()
  4668. {
  4669. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4670. $this->Form->dateTime('Contact.date', ['secure' => false]);
  4671. $expected = [];
  4672. $this->assertEquals($expected, $this->Form->fields);
  4673. $this->Form->fields = [];
  4674. $this->Form->date('Contact.published', ['secure' => false]);
  4675. $expected = [];
  4676. $this->assertEquals($expected, $this->Form->fields);
  4677. }
  4678. /**
  4679. * Test empty defaulting to true for datetime.
  4680. *
  4681. * @return void
  4682. */
  4683. public function testDatetimeEmpty()
  4684. {
  4685. extract($this->dateRegex);
  4686. $now = strtotime('now');
  4687. $result = $this->Form->dateTime('Contact.date', [
  4688. 'timeFormat' => 12,
  4689. 'empty' => true,
  4690. ]);
  4691. $expected = [
  4692. ['select' => ['name' => 'Contact[date][year]']],
  4693. $yearsRegex,
  4694. ['option' => ['value' => '']],
  4695. '/option',
  4696. '*/select',
  4697. ['select' => ['name' => 'Contact[date][month]']],
  4698. $monthsRegex,
  4699. ['option' => ['value' => '']],
  4700. '/option',
  4701. '*/select',
  4702. ['select' => ['name' => 'Contact[date][day]']],
  4703. $daysRegex,
  4704. ['option' => ['value' => '']],
  4705. '/option',
  4706. '*/select',
  4707. ['select' => ['name' => 'Contact[date][hour]']],
  4708. $hoursRegex,
  4709. ['option' => ['value' => '']],
  4710. '/option',
  4711. '*/select',
  4712. ['select' => ['name' => 'Contact[date][minute]']],
  4713. $minutesRegex,
  4714. ['option' => ['value' => '']],
  4715. '/option',
  4716. '*/select',
  4717. ['select' => ['name' => 'Contact[date][meridian]']],
  4718. $meridianRegex,
  4719. ['option' => ['value' => '']],
  4720. '/option',
  4721. '*/select'
  4722. ];
  4723. $this->assertHtml($expected, $result);
  4724. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4725. }
  4726. /**
  4727. * Test datetime with interval option.
  4728. *
  4729. * @return void
  4730. */
  4731. public function testDatetimeMinuteInterval()
  4732. {
  4733. extract($this->dateRegex);
  4734. $now = strtotime('now');
  4735. $result = $this->Form->dateTime('Contact.date', [
  4736. 'interval' => 5,
  4737. 'value' => ''
  4738. ]);
  4739. $expected = [
  4740. ['select' => ['name' => 'Contact[date][year]']],
  4741. $yearsRegex,
  4742. ['option' => ['selected' => 'selected', 'value' => '']],
  4743. '/option',
  4744. '*/select',
  4745. ['select' => ['name' => 'Contact[date][month]']],
  4746. $monthsRegex,
  4747. ['option' => ['selected' => 'selected', 'value' => '']],
  4748. '/option',
  4749. '*/select',
  4750. ['select' => ['name' => 'Contact[date][day]']],
  4751. $daysRegex,
  4752. ['option' => ['selected' => 'selected', 'value' => '']],
  4753. '/option',
  4754. '*/select',
  4755. ['select' => ['name' => 'Contact[date][hour]']],
  4756. $hoursRegex,
  4757. ['option' => ['selected' => 'selected', 'value' => '']],
  4758. '/option',
  4759. '*/select',
  4760. ['select' => ['name' => 'Contact[date][minute]']],
  4761. $minutesRegex,
  4762. ['option' => ['selected' => 'selected', 'value' => '']],
  4763. '/option',
  4764. ['option' => ['value' => '00']],
  4765. '00',
  4766. '/option',
  4767. ['option' => ['value' => '05']],
  4768. '05',
  4769. '/option',
  4770. ['option' => ['value' => '10']],
  4771. '10',
  4772. '/option',
  4773. '*/select',
  4774. ];
  4775. $this->assertHtml($expected, $result);
  4776. }
  4777. /**
  4778. * Test dateTime with rounding
  4779. *
  4780. * @return void
  4781. */
  4782. public function testDateTimeRounding()
  4783. {
  4784. $this->Form->request->data['Contact'] = [
  4785. 'date' => [
  4786. 'day' => '13',
  4787. 'month' => '12',
  4788. 'year' => '2010',
  4789. 'hour' => '04',
  4790. 'minute' => '19',
  4791. 'meridian' => 'AM'
  4792. ]
  4793. ];
  4794. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  4795. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4796. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  4797. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  4798. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  4799. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4800. }
  4801. /**
  4802. * test that datetime() and default values work.
  4803. *
  4804. * @return void
  4805. */
  4806. public function testDatetimeWithDefault()
  4807. {
  4808. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  4809. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4810. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4811. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4812. $result = $this->Form->dateTime('Contact.updated', [
  4813. 'default' => '2009-06-01 11:15:30'
  4814. ]);
  4815. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4816. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4817. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4818. }
  4819. /**
  4820. * testDateTime all zeros
  4821. *
  4822. * @return void
  4823. */
  4824. public function testDateTimeAllZeros()
  4825. {
  4826. $result = $this->Form->dateTime('Contact.date', [
  4827. 'timeFormat' => false,
  4828. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  4829. 'value' => '0000-00-00'
  4830. ]);
  4831. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  4832. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  4833. }
  4834. /**
  4835. * testDateTimeEmptyAsArray
  4836. *
  4837. * @return void
  4838. */
  4839. public function testDateTimeEmptyAsArray()
  4840. {
  4841. $result = $this->Form->dateTime('Contact.date', [
  4842. 'empty' => [
  4843. 'day' => 'DAY',
  4844. 'month' => 'MONTH',
  4845. 'year' => 'YEAR',
  4846. 'hour' => 'HOUR',
  4847. 'minute' => 'MINUTE',
  4848. 'meridian' => false
  4849. ]
  4850. ]);
  4851. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4852. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4853. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4854. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4855. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4856. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4857. $result = $this->Form->dateTime('Contact.date', [
  4858. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR']
  4859. ]);
  4860. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4861. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4862. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4863. }
  4864. /**
  4865. * testFormDateTimeMulti method
  4866. *
  4867. * test multiple datetime element generation
  4868. *
  4869. * @return void
  4870. */
  4871. public function testFormDateTimeMulti()
  4872. {
  4873. extract($this->dateRegex);
  4874. $result = $this->Form->dateTime('Contact.1.updated');
  4875. $this->assertContains('Contact[1][updated][month]', $result);
  4876. $this->assertContains('Contact[1][updated][day]', $result);
  4877. $this->assertContains('Contact[1][updated][year]', $result);
  4878. $this->assertContains('Contact[1][updated][hour]', $result);
  4879. $this->assertContains('Contact[1][updated][minute]', $result);
  4880. }
  4881. /**
  4882. * When changing the date format, the label should always focus the first select box when
  4883. * clicked.
  4884. *
  4885. * @return void
  4886. */
  4887. public function testDateTimeLabelIdMatchesFirstInput()
  4888. {
  4889. $result = $this->Form->input('Model.date', ['type' => 'date']);
  4890. $this->assertContains('<label>Date</label>', $result);
  4891. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  4892. $this->assertContains('<label>Date</label>', $result);
  4893. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  4894. $this->assertContains('<label>Date</label>', $result);
  4895. }
  4896. /**
  4897. * testMonth method
  4898. *
  4899. * @return void
  4900. */
  4901. public function testMonth()
  4902. {
  4903. $result = $this->Form->month('Model.field', ['value' => '']);
  4904. $expected = [
  4905. ['select' => ['name' => 'Model[field][month]']],
  4906. ['option' => ['value' => '', 'selected' => 'selected']],
  4907. '/option',
  4908. ['option' => ['value' => '01']],
  4909. date('F', strtotime('2008-01-01 00:00:00')),
  4910. '/option',
  4911. ['option' => ['value' => '02']],
  4912. date('F', strtotime('2008-02-01 00:00:00')),
  4913. '/option',
  4914. '*/select',
  4915. ];
  4916. $this->assertHtml($expected, $result);
  4917. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  4918. $expected = [
  4919. ['select' => ['name' => 'Model[field][month]']],
  4920. ['option' => ['selected' => 'selected', 'value' => '']],
  4921. '/option',
  4922. ['option' => ['value' => '01']],
  4923. date('F', strtotime('2008-01-01 00:00:00')),
  4924. '/option',
  4925. ['option' => ['value' => '02']],
  4926. date('F', strtotime('2008-02-01 00:00:00')),
  4927. '/option',
  4928. '*/select',
  4929. ];
  4930. $this->assertHtml($expected, $result);
  4931. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  4932. $expected = [
  4933. ['select' => ['name' => 'Model[field][month]']],
  4934. ['option' => ['selected' => 'selected', 'value' => '']],
  4935. '/option',
  4936. ['option' => ['value' => '01']],
  4937. '1',
  4938. '/option',
  4939. ['option' => ['value' => '02']],
  4940. '2',
  4941. '/option',
  4942. '*/select',
  4943. ];
  4944. $this->assertHtml($expected, $result);
  4945. $monthNames = [
  4946. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4947. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  4948. ];
  4949. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  4950. $expected = [
  4951. ['select' => ['name' => 'Model[field][month]']],
  4952. ['option' => ['value' => '']],
  4953. '/option',
  4954. ['option' => ['value' => '01', 'selected' => 'selected']],
  4955. 'Jan',
  4956. '/option',
  4957. ['option' => ['value' => '02']],
  4958. 'Feb',
  4959. '/option',
  4960. '*/select',
  4961. ];
  4962. $this->assertHtml($expected, $result);
  4963. $this->Form->request->data['Project']['release'] = '2050-02-10';
  4964. $result = $this->Form->month('Project.release');
  4965. $expected = [
  4966. ['select' => ['name' => 'Project[release][month]']],
  4967. ['option' => ['value' => '']],
  4968. '/option',
  4969. ['option' => ['value' => '01']],
  4970. 'January',
  4971. '/option',
  4972. ['option' => ['value' => '02', 'selected' => 'selected']],
  4973. 'February',
  4974. '/option',
  4975. '*/select',
  4976. ];
  4977. $this->assertHtml($expected, $result);
  4978. $result = $this->Form->month('Contact.published', [
  4979. 'empty' => 'Published on',
  4980. ]);
  4981. $this->assertContains('Published on', $result);
  4982. }
  4983. /**
  4984. * testDay method
  4985. *
  4986. * @return void
  4987. */
  4988. public function testDay()
  4989. {
  4990. extract($this->dateRegex);
  4991. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  4992. $expected = [
  4993. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  4994. ['option' => ['selected' => 'selected', 'value' => '']],
  4995. '/option',
  4996. ['option' => ['value' => '01']],
  4997. '1',
  4998. '/option',
  4999. ['option' => ['value' => '02']],
  5000. '2',
  5001. '/option',
  5002. $daysRegex,
  5003. '/select',
  5004. ];
  5005. $this->assertHtml($expected, $result);
  5006. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5007. $result = $this->Form->day('Model.field');
  5008. $expected = [
  5009. ['select' => ['name' => 'Model[field][day]']],
  5010. ['option' => ['value' => '']],
  5011. '/option',
  5012. ['option' => ['value' => '01']],
  5013. '1',
  5014. '/option',
  5015. ['option' => ['value' => '02']],
  5016. '2',
  5017. '/option',
  5018. $daysRegex,
  5019. ['option' => ['value' => '10', 'selected' => 'selected']],
  5020. '10',
  5021. '/option',
  5022. $daysRegex,
  5023. '/select',
  5024. ];
  5025. $this->assertHtml($expected, $result);
  5026. $this->Form->request->data['Model']['field'] = '';
  5027. $result = $this->Form->day('Model.field', ['value' => '10']);
  5028. $expected = [
  5029. ['select' => ['name' => 'Model[field][day]']],
  5030. ['option' => ['value' => '']],
  5031. '/option',
  5032. ['option' => ['value' => '01']],
  5033. '1',
  5034. '/option',
  5035. ['option' => ['value' => '02']],
  5036. '2',
  5037. '/option',
  5038. $daysRegex,
  5039. ['option' => ['value' => '10', 'selected' => 'selected']],
  5040. '10',
  5041. '/option',
  5042. $daysRegex,
  5043. '/select',
  5044. ];
  5045. $this->assertHtml($expected, $result);
  5046. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5047. $result = $this->Form->day('Project.release');
  5048. $expected = [
  5049. ['select' => ['name' => 'Project[release][day]']],
  5050. ['option' => ['value' => '']],
  5051. '/option',
  5052. ['option' => ['value' => '01']],
  5053. '1',
  5054. '/option',
  5055. ['option' => ['value' => '02']],
  5056. '2',
  5057. '/option',
  5058. $daysRegex,
  5059. ['option' => ['value' => '10', 'selected' => 'selected']],
  5060. '10',
  5061. '/option',
  5062. $daysRegex,
  5063. '/select',
  5064. ];
  5065. $this->assertHtml($expected, $result);
  5066. $result = $this->Form->day('Contact.published', [
  5067. 'empty' => 'Published on',
  5068. ]);
  5069. $this->assertContains('Published on', $result);
  5070. }
  5071. /**
  5072. * testMinute method
  5073. *
  5074. * @return void
  5075. */
  5076. public function testMinute()
  5077. {
  5078. extract($this->dateRegex);
  5079. $result = $this->Form->minute('Model.field', ['value' => '']);
  5080. $expected = [
  5081. ['select' => ['name' => 'Model[field][minute]']],
  5082. ['option' => ['selected' => 'selected', 'value' => '']],
  5083. '/option',
  5084. ['option' => ['value' => '00']],
  5085. '00',
  5086. '/option',
  5087. ['option' => ['value' => '01']],
  5088. '01',
  5089. '/option',
  5090. ['option' => ['value' => '02']],
  5091. '02',
  5092. '/option',
  5093. $minutesRegex,
  5094. '/select',
  5095. ];
  5096. $this->assertHtml($expected, $result);
  5097. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5098. $result = $this->Form->minute('Model.field');
  5099. $expected = [
  5100. ['select' => ['name' => 'Model[field][minute]']],
  5101. ['option' => ['value' => '']],
  5102. '/option',
  5103. ['option' => ['value' => '00']],
  5104. '00',
  5105. '/option',
  5106. ['option' => ['value' => '01']],
  5107. '01',
  5108. '/option',
  5109. ['option' => ['value' => '02']],
  5110. '02',
  5111. '/option',
  5112. $minutesRegex,
  5113. ['option' => ['value' => '12', 'selected' => 'selected']],
  5114. '12',
  5115. '/option',
  5116. $minutesRegex,
  5117. '/select',
  5118. ];
  5119. $this->assertHtml($expected, $result);
  5120. $this->Form->request->data['Model']['field'] = '';
  5121. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5122. $expected = [
  5123. ['select' => ['name' => 'Model[field][minute]']],
  5124. ['option' => ['selected' => 'selected', 'value' => '']],
  5125. '/option',
  5126. ['option' => ['value' => '00']],
  5127. '00',
  5128. '/option',
  5129. ['option' => ['value' => '05']],
  5130. '05',
  5131. '/option',
  5132. ['option' => ['value' => '10']],
  5133. '10',
  5134. '/option',
  5135. $minutesRegex,
  5136. '/select',
  5137. ];
  5138. $this->assertHtml($expected, $result);
  5139. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5140. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5141. $expected = [
  5142. ['select' => ['name' => 'Model[field][minute]']],
  5143. ['option' => ['value' => '']],
  5144. '/option',
  5145. ['option' => ['value' => '00']],
  5146. '00',
  5147. '/option',
  5148. ['option' => ['value' => '05']],
  5149. '05',
  5150. '/option',
  5151. ['option' => ['value' => '10', 'selected' => 'selected']],
  5152. '10',
  5153. '/option',
  5154. $minutesRegex,
  5155. '/select',
  5156. ];
  5157. $this->assertHtml($expected, $result);
  5158. }
  5159. /**
  5160. * Test generating an input for the meridian.
  5161. *
  5162. * @return void
  5163. */
  5164. public function testMeridian()
  5165. {
  5166. extract($this->dateRegex);
  5167. $now = new \DateTime();
  5168. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  5169. $expected = [
  5170. ['select' => ['name' => 'Model[field][meridian]']],
  5171. ['option' => ['value' => '']],
  5172. '/option',
  5173. $meridianRegex,
  5174. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  5175. $now->format('a'),
  5176. '/option',
  5177. '*/select'
  5178. ];
  5179. $this->assertHtml($expected, $result);
  5180. }
  5181. /**
  5182. * testHour method
  5183. *
  5184. * @return void
  5185. */
  5186. public function testHour()
  5187. {
  5188. extract($this->dateRegex);
  5189. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  5190. $expected = [
  5191. ['select' => ['name' => 'Model[field][hour]']],
  5192. ['option' => ['selected' => 'selected', 'value' => '']],
  5193. '/option',
  5194. ['option' => ['value' => '01']],
  5195. '1',
  5196. '/option',
  5197. ['option' => ['value' => '02']],
  5198. '2',
  5199. '/option',
  5200. $hoursRegex,
  5201. '/select',
  5202. ];
  5203. $this->assertHtml($expected, $result);
  5204. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5205. $result = $this->Form->hour('Model.field', ['format' => 12]);
  5206. $expected = [
  5207. ['select' => ['name' => 'Model[field][hour]']],
  5208. ['option' => ['value' => '']],
  5209. '/option',
  5210. ['option' => ['value' => '01']],
  5211. '1',
  5212. '/option',
  5213. ['option' => ['value' => '02']],
  5214. '2',
  5215. '/option',
  5216. $hoursRegex,
  5217. ['option' => ['value' => '12', 'selected' => 'selected']],
  5218. '12',
  5219. '/option',
  5220. '/select',
  5221. ];
  5222. $this->assertHtml($expected, $result);
  5223. $this->Form->request->data['Model']['field'] = '';
  5224. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  5225. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5226. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  5227. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5228. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5229. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5230. $expected = [
  5231. ['select' => ['name' => 'Model[field][hour]']],
  5232. ['option' => ['value' => '']],
  5233. '/option',
  5234. ['option' => ['value' => '00', 'selected' => 'selected']],
  5235. '0',
  5236. '/option',
  5237. ['option' => ['value' => '01']],
  5238. '1',
  5239. '/option',
  5240. ['option' => ['value' => '02']],
  5241. '2',
  5242. '/option',
  5243. $hoursRegex,
  5244. '/select',
  5245. ];
  5246. $this->assertHtml($expected, $result);
  5247. unset($this->Form->request->data['Model']['field']);
  5248. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  5249. $thisHour = date('H');
  5250. $optValue = date('G');
  5251. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5252. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5253. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5254. $expected = [
  5255. ['select' => ['name' => 'Model[field][hour]']],
  5256. ['option' => ['value' => '']],
  5257. '/option',
  5258. ['option' => ['value' => '00']],
  5259. '0',
  5260. '/option',
  5261. ['option' => ['value' => '01', 'selected' => 'selected']],
  5262. '1',
  5263. '/option',
  5264. ['option' => ['value' => '02']],
  5265. '2',
  5266. '/option',
  5267. $hoursRegex,
  5268. '/select',
  5269. ];
  5270. $this->assertHtml($expected, $result);
  5271. }
  5272. /**
  5273. * testYear method
  5274. *
  5275. * @return void
  5276. */
  5277. public function testYear()
  5278. {
  5279. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  5280. $expected = [
  5281. ['select' => ['name' => 'Model[field][year]']],
  5282. ['option' => ['selected' => 'selected', 'value' => '']],
  5283. '/option',
  5284. ['option' => ['value' => '2007']],
  5285. '2007',
  5286. '/option',
  5287. ['option' => ['value' => '2006']],
  5288. '2006',
  5289. '/option',
  5290. '/select',
  5291. ];
  5292. $this->assertHtml($expected, $result);
  5293. $result = $this->Form->year('Model.field', [
  5294. 'value' => '',
  5295. 'minYear' => 2006,
  5296. 'maxYear' => 2007,
  5297. 'orderYear' => 'asc'
  5298. ]);
  5299. $expected = [
  5300. ['select' => ['name' => 'Model[field][year]']],
  5301. ['option' => ['selected' => 'selected', 'value' => '']],
  5302. '/option',
  5303. ['option' => ['value' => '2006']],
  5304. '2006',
  5305. '/option',
  5306. ['option' => ['value' => '2007']],
  5307. '2007',
  5308. '/option',
  5309. '/select',
  5310. ];
  5311. $this->assertHtml($expected, $result);
  5312. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5313. $result = $this->Form->year('Contact.published', [
  5314. 'empty' => false,
  5315. 'minYear' => 2006,
  5316. 'maxYear' => 2007,
  5317. ]);
  5318. $expected = [
  5319. ['select' => ['name' => 'Contact[published][year]']],
  5320. ['option' => ['value' => '2007']],
  5321. '2007',
  5322. '/option',
  5323. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5324. '2006',
  5325. '/option',
  5326. '/select',
  5327. ];
  5328. $this->assertHtml($expected, $result);
  5329. $result = $this->Form->year('Contact.published', [
  5330. 'empty' => 'Published on',
  5331. ]);
  5332. $this->assertContains('Published on', $result);
  5333. }
  5334. /**
  5335. * testYearAutoExpandRange method
  5336. *
  5337. * @return void
  5338. */
  5339. public function testYearAutoExpandRange()
  5340. {
  5341. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  5342. $result = $this->Form->year('User.birthday');
  5343. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5344. $result = $matches[1];
  5345. $expected = range(date('Y') + 5, 1930);
  5346. $this->assertEquals($expected, $result);
  5347. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5348. $result = $this->Form->year('Project.release');
  5349. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5350. $result = $matches[1];
  5351. $expected = range(2050, date('Y') - 5);
  5352. $this->assertEquals($expected, $result);
  5353. $this->Form->request->data['Project']['release'] = '1881-10-10';
  5354. $result = $this->Form->year('Project.release', [
  5355. 'minYear' => 1890,
  5356. 'maxYear' => 1900
  5357. ]);
  5358. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5359. $result = $matches[1];
  5360. $expected = range(1900, 1881);
  5361. $this->assertEquals($expected, $result);
  5362. }
  5363. /**
  5364. * Test that input() accepts the type of date and passes options in.
  5365. *
  5366. * @return void
  5367. */
  5368. public function testInputDate()
  5369. {
  5370. $this->Form->request->data = [
  5371. 'month_year' => ['month' => date('m')],
  5372. ];
  5373. $this->Form->create($this->article);
  5374. $result = $this->Form->input('month_year', [
  5375. 'label' => false,
  5376. 'type' => 'date',
  5377. 'minYear' => 2006,
  5378. 'maxYear' => 2008
  5379. ]);
  5380. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  5381. $this->assertNotContains('value="2008" selected="selected"', $result);
  5382. }
  5383. /**
  5384. * Test the label option being set to false.
  5385. *
  5386. * @return void
  5387. */
  5388. public function testInputLabelFalse()
  5389. {
  5390. $this->Form->create($this->article);
  5391. $result = $this->Form->input('title', ['label' => false]);
  5392. $expected = [
  5393. 'div' => ['class' => 'input text required'],
  5394. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  5395. '/div'
  5396. ];
  5397. $this->assertHtml($expected, $result);
  5398. }
  5399. /**
  5400. * testInputDateMaxYear method
  5401. *
  5402. * Let's say we want to only allow users born from 2006 to 2008 to register
  5403. * This being the first singup page, we still don't have any data
  5404. *
  5405. * @return void
  5406. */
  5407. public function testInputDateMaxYear()
  5408. {
  5409. $this->Form->request->data = [];
  5410. $this->Form->create($this->article);
  5411. $result = $this->Form->input('birthday', [
  5412. 'label' => false,
  5413. 'type' => 'date',
  5414. 'minYear' => 2006,
  5415. 'maxYear' => 2008
  5416. ]);
  5417. $this->assertContains('value="2008" selected="selected"', $result);
  5418. }
  5419. /**
  5420. * testTextArea method
  5421. *
  5422. * @return void
  5423. */
  5424. public function testTextArea()
  5425. {
  5426. $this->Form->request->data = ['field' => 'some test data'];
  5427. $result = $this->Form->textarea('field');
  5428. $expected = [
  5429. 'textarea' => ['name' => 'field', 'rows' => 5],
  5430. 'some test data',
  5431. '/textarea',
  5432. ];
  5433. $this->assertHtml($expected, $result);
  5434. $result = $this->Form->textarea('user.bio');
  5435. $expected = [
  5436. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5437. '/textarea',
  5438. ];
  5439. $this->assertHtml($expected, $result);
  5440. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  5441. $result = $this->Form->textarea('field');
  5442. $expected = [
  5443. 'textarea' => ['name' => 'field', 'rows' => 5],
  5444. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5445. '/textarea',
  5446. ];
  5447. $this->assertHtml($expected, $result);
  5448. $this->Form->request->data = [
  5449. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  5450. ];
  5451. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5452. $expected = [
  5453. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5454. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5455. '/textarea',
  5456. ];
  5457. $this->assertHtml($expected, $result);
  5458. $result = $this->Form->textarea('0.OtherModel.field');
  5459. $expected = [
  5460. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5461. '/textarea'
  5462. ];
  5463. $this->assertHtml($expected, $result);
  5464. }
  5465. /**
  5466. * testTextAreaWithStupidCharacters method
  5467. *
  5468. * test text area with non-ascii characters
  5469. *
  5470. * @return void
  5471. */
  5472. public function testTextAreaWithStupidCharacters()
  5473. {
  5474. $result = $this->Form->textarea('Post.content', [
  5475. 'value' => "GREAT®",
  5476. 'rows' => '15',
  5477. 'cols' => '75'
  5478. ]);
  5479. $expected = [
  5480. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5481. 'GREAT®',
  5482. '/textarea',
  5483. ];
  5484. $this->assertHtml($expected, $result);
  5485. }
  5486. /**
  5487. * Test textareas waxlength read from schema.
  5488. *
  5489. * @return void
  5490. */
  5491. public function testTextAreaMaxLength()
  5492. {
  5493. $this->Form->create([
  5494. 'schema' => [
  5495. 'stuff' => ['type' => 'string', 'length' => 10],
  5496. ]
  5497. ]);
  5498. $result = $this->Form->input('other', ['type' => 'textarea']);
  5499. $expected = [
  5500. 'div' => ['class' => 'input textarea'],
  5501. 'label' => ['for' => 'other'],
  5502. 'Other',
  5503. '/label',
  5504. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  5505. '/textarea',
  5506. '/div'
  5507. ];
  5508. $this->assertHtml($expected, $result);
  5509. $result = $this->Form->input('stuff', ['type' => 'textarea']);
  5510. $expected = [
  5511. 'div' => ['class' => 'input textarea'],
  5512. 'label' => ['for' => 'stuff'],
  5513. 'Stuff',
  5514. '/label',
  5515. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  5516. '/textarea',
  5517. '/div'
  5518. ];
  5519. $this->assertHtml($expected, $result);
  5520. }
  5521. /**
  5522. * testHiddenField method
  5523. *
  5524. * @return void
  5525. */
  5526. public function testHiddenField()
  5527. {
  5528. $this->article['errors'] = [
  5529. 'field' => true
  5530. ];
  5531. $this->Form->request->data['field'] = 'test';
  5532. $this->Form->create($this->article);
  5533. $result = $this->Form->hidden('field', ['id' => 'theID']);
  5534. $expected = [
  5535. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  5536. $this->assertHtml($expected, $result);
  5537. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5538. $expected = [
  5539. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  5540. ];
  5541. $this->assertHtml($expected, $result);
  5542. }
  5543. /**
  5544. * testFileUploadField method
  5545. *
  5546. * @return void
  5547. */
  5548. public function testFileUploadField()
  5549. {
  5550. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5551. $result = $this->Form->file('Model.upload');
  5552. $this->assertHtml($expected, $result);
  5553. $this->Form->request->data['Model']['upload'] = [
  5554. 'name' => '', 'type' => '', 'tmp_name' => '',
  5555. 'error' => 4, 'size' => 0
  5556. ];
  5557. $result = $this->Form->file('Model.upload');
  5558. $this->assertHtml($expected, $result);
  5559. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5560. $result = $this->Form->file('Model.upload');
  5561. $this->assertHtml($expected, $result);
  5562. }
  5563. /**
  5564. * test File upload input on a model not used in create();
  5565. *
  5566. * @return void
  5567. */
  5568. public function testFileUploadOnOtherModel()
  5569. {
  5570. $this->Form->create($this->article, ['type' => 'file']);
  5571. $result = $this->Form->file('ValidateProfile.city');
  5572. $expected = [
  5573. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  5574. ];
  5575. $this->assertHtml($expected, $result);
  5576. }
  5577. /**
  5578. * testButton method
  5579. *
  5580. * @return void
  5581. */
  5582. public function testButton()
  5583. {
  5584. $result = $this->Form->button('Hi');
  5585. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  5586. $this->assertHtml($expected, $result);
  5587. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  5588. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  5589. $this->assertHtml($expected, $result);
  5590. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  5591. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  5592. $this->assertHtml($expected, $result);
  5593. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  5594. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  5595. $this->assertHtml($expected, $result);
  5596. $result = $this->Form->button('No type', ['type' => false]);
  5597. $expected = ['button' => [], 'No type', '/button'];
  5598. $this->assertHtml($expected, $result);
  5599. $result = $this->Form->button('Upload Text', [
  5600. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5601. 'escape' => false
  5602. ]);
  5603. $this->assertNotRegExp('/\&039/', $result);
  5604. }
  5605. /**
  5606. * Test that button() makes unlocked fields by default.
  5607. *
  5608. * @return void
  5609. */
  5610. public function testButtonUnlockedByDefault()
  5611. {
  5612. $this->Form->request->params['_csrfToken'] = 'secured';
  5613. $this->Form->button('Save', ['name' => 'save']);
  5614. $this->Form->button('Clear');
  5615. $result = $this->Form->unlockField();
  5616. $this->assertEquals(['save'], $result);
  5617. }
  5618. /**
  5619. * testPostButton method
  5620. *
  5621. * @return void
  5622. */
  5623. public function testPostButton()
  5624. {
  5625. $result = $this->Form->postButton('Hi', '/controller/action');
  5626. $expected = [
  5627. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  5628. 'div' => ['style' => 'display:none;'],
  5629. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  5630. '/div',
  5631. 'button' => ['type' => 'submit'],
  5632. 'Hi',
  5633. '/button',
  5634. '/form'
  5635. ];
  5636. $this->assertHtml($expected, $result);
  5637. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  5638. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  5639. }
  5640. /**
  5641. * Test using postButton with N dimensional data.
  5642. *
  5643. * @return void
  5644. */
  5645. public function testPostButtonNestedData()
  5646. {
  5647. $data = [
  5648. 'one' => [
  5649. 'two' => [
  5650. 3, 4, 5
  5651. ]
  5652. ]
  5653. ];
  5654. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  5655. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5656. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5657. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5658. }
  5659. /**
  5660. * Test that postButton adds _Token fields.
  5661. *
  5662. * @return void
  5663. */
  5664. public function testSecurePostButton()
  5665. {
  5666. $this->Form->request->params['_csrfToken'] = 'testkey';
  5667. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5668. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5669. $expected = [
  5670. 'form' => [
  5671. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5672. ],
  5673. ['div' => ['style' => 'display:none;']],
  5674. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  5675. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  5676. '/div',
  5677. 'button' => ['type' => 'submit'],
  5678. 'Delete',
  5679. '/button',
  5680. ['div' => ['style' => 'display:none;']],
  5681. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  5682. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  5683. '/div',
  5684. '/form',
  5685. ];
  5686. $this->assertHtml($expected, $result);
  5687. }
  5688. /**
  5689. * testPostLink method
  5690. *
  5691. * @return void
  5692. */
  5693. public function testPostLink()
  5694. {
  5695. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5696. $expected = [
  5697. 'form' => [
  5698. 'method' => 'post', 'action' => '/posts/delete/1',
  5699. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5700. ],
  5701. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  5702. '/form',
  5703. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  5704. 'Delete',
  5705. '/a'
  5706. ];
  5707. $this->assertHtml($expected, $result);
  5708. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  5709. $expected = [
  5710. 'form' => [
  5711. 'method' => 'post', 'action' => '/posts/delete/1',
  5712. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5713. ],
  5714. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  5715. '/form',
  5716. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  5717. 'Delete',
  5718. '/a'
  5719. ];
  5720. $this->assertHtml($expected, $result);
  5721. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  5722. $expected = [
  5723. 'form' => [
  5724. 'method' => 'post', 'action' => '/posts/delete/1',
  5725. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5726. ],
  5727. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  5728. '/form',
  5729. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  5730. 'Delete',
  5731. '/a'
  5732. ];
  5733. $this->assertHtml($expected, $result);
  5734. $result = $this->Form->postLink(
  5735. 'Delete',
  5736. '/posts/delete/1',
  5737. ['escape' => false, 'confirm' => '\'Confirm\' this "deletion"?']
  5738. );
  5739. $expected = [
  5740. 'form' => [
  5741. 'method' => 'post', 'action' => '/posts/delete/1',
  5742. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5743. ],
  5744. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  5745. '/form',
  5746. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;&#039;Confirm&#039; this \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  5747. 'Delete',
  5748. '/a'
  5749. ];
  5750. $this->assertHtml($expected, $result);
  5751. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['target' => '_blank']);
  5752. $expected = [
  5753. 'form' => [
  5754. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5755. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5756. ],
  5757. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  5758. '/form',
  5759. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  5760. 'Delete',
  5761. '/a'
  5762. ];
  5763. $this->assertHtml($expected, $result);
  5764. $result = $this->Form->postLink(
  5765. '',
  5766. ['controller' => 'items', 'action' => 'delete', 10],
  5767. ['class' => 'btn btn-danger', 'escape' => false, 'confirm' => 'Confirm thing']
  5768. );
  5769. $expected = [
  5770. 'form' => [
  5771. 'method' => 'post', 'action' => '/items/delete/10',
  5772. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5773. ],
  5774. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  5775. '/form',
  5776. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  5777. '/a'
  5778. ];
  5779. $this->assertHtml($expected, $result);
  5780. }
  5781. /**
  5782. * Test postLink with additional data.
  5783. *
  5784. * @return void
  5785. */
  5786. public function testPostLinkWithData()
  5787. {
  5788. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  5789. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  5790. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  5791. $this->Form->create($entity);
  5792. $this->Form->end();
  5793. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  5794. $this->assertContains(
  5795. '<input type="hidden" name="name" value="show"',
  5796. $result,
  5797. 'should not contain entity data.'
  5798. );
  5799. }
  5800. /**
  5801. * Test that security hashes for postLink include the url.
  5802. *
  5803. * @return void
  5804. */
  5805. public function testPostLinkSecurityHash()
  5806. {
  5807. $hash = Security::hash(
  5808. '/posts/delete/1' .
  5809. serialize([]) .
  5810. '' .
  5811. Security::salt()
  5812. );
  5813. $hash .= '%3A';
  5814. $this->Form->request->params['_Token']['key'] = 'test';
  5815. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5816. $expected = [
  5817. 'form' => [
  5818. 'method' => 'post', 'action' => '/posts/delete/1',
  5819. 'name', 'style' => 'display:none;'
  5820. ],
  5821. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  5822. 'div' => ['style' => 'display:none;'],
  5823. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  5824. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  5825. '/div',
  5826. '/form',
  5827. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  5828. 'Delete',
  5829. '/a'
  5830. ];
  5831. $this->assertHtml($expected, $result);
  5832. }
  5833. /**
  5834. * Test using postLink with N dimensional data.
  5835. *
  5836. * @return void
  5837. */
  5838. public function testPostLinkNestedData()
  5839. {
  5840. $data = [
  5841. 'one' => [
  5842. 'two' => [
  5843. 3, 4, 5
  5844. ]
  5845. ]
  5846. ];
  5847. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  5848. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5849. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5850. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5851. }
  5852. /**
  5853. * test creating postLinks after a GET form.
  5854. *
  5855. * @return void
  5856. */
  5857. public function testPostLinkAfterGetForm()
  5858. {
  5859. $this->Form->request->params['_csrfToken'] = 'testkey';
  5860. $this->Form->request->params['_Token'] = 'val';
  5861. $this->Form->create($this->article, ['type' => 'get']);
  5862. $this->Form->end();
  5863. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5864. $expected = [
  5865. 'form' => [
  5866. 'method' => 'post', 'action' => '/posts/delete/1',
  5867. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5868. ],
  5869. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  5870. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  5871. 'div' => ['style' => 'display:none;'],
  5872. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  5873. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  5874. '/div',
  5875. '/form',
  5876. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  5877. 'Delete',
  5878. '/a'
  5879. ];
  5880. $this->assertHtml($expected, $result);
  5881. }
  5882. /**
  5883. * Test that postLink adds form tags to view block
  5884. *
  5885. * @return void
  5886. */
  5887. public function testPostLinkFormBuffer()
  5888. {
  5889. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  5890. $expected = [
  5891. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  5892. 'Delete',
  5893. '/a'
  5894. ];
  5895. $this->assertHtml($expected, $result);
  5896. $result = $this->View->fetch('postLink');
  5897. $expected = [
  5898. 'form' => [
  5899. 'method' => 'post', 'action' => '/posts/delete/1',
  5900. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5901. ],
  5902. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  5903. '/form'
  5904. ];
  5905. $this->assertHtml($expected, $result);
  5906. $result = $this->Form->postLink(
  5907. 'Delete',
  5908. '/posts/delete/2',
  5909. ['block' => true, 'method' => 'DELETE']
  5910. );
  5911. $expected = [
  5912. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  5913. 'Delete',
  5914. '/a'
  5915. ];
  5916. $this->assertHtml($expected, $result);
  5917. $result = $this->View->fetch('postLink');
  5918. $expected = [
  5919. 'form' => [
  5920. 'method' => 'post', 'action' => '/posts/delete/1',
  5921. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5922. ],
  5923. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  5924. '/form',
  5925. [
  5926. 'form' => [
  5927. 'method' => 'post', 'action' => '/posts/delete/2',
  5928. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5929. ],
  5930. ],
  5931. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  5932. '/form'
  5933. ];
  5934. $this->assertHtml($expected, $result);
  5935. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  5936. $expected = [
  5937. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  5938. 'Delete',
  5939. '/a'
  5940. ];
  5941. $this->assertHtml($expected, $result);
  5942. $result = $this->View->fetch('foobar');
  5943. $expected = [
  5944. 'form' => [
  5945. 'method' => 'post', 'action' => '/posts/delete/1',
  5946. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5947. ],
  5948. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  5949. '/form'
  5950. ];
  5951. $this->assertHtml($expected, $result);
  5952. }
  5953. /**
  5954. * testSubmitButton method
  5955. *
  5956. * @return void
  5957. */
  5958. public function testSubmitButton()
  5959. {
  5960. $result = $this->Form->submit('');
  5961. $expected = [
  5962. 'div' => ['class' => 'submit'],
  5963. 'input' => ['type' => 'submit', 'value' => ''],
  5964. '/div'
  5965. ];
  5966. $this->assertHtml($expected, $result);
  5967. $result = $this->Form->submit('Test Submit');
  5968. $expected = [
  5969. 'div' => ['class' => 'submit'],
  5970. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  5971. '/div'
  5972. ];
  5973. $this->assertHtml($expected, $result);
  5974. $result = $this->Form->submit('Next >');
  5975. $expected = [
  5976. 'div' => ['class' => 'submit'],
  5977. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  5978. '/div'
  5979. ];
  5980. $this->assertHtml($expected, $result);
  5981. $result = $this->Form->submit('Next >', ['escape' => false]);
  5982. $expected = [
  5983. 'div' => ['class' => 'submit'],
  5984. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  5985. '/div'
  5986. ];
  5987. $this->assertHtml($expected, $result);
  5988. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  5989. $expected = [
  5990. 'div' => ['class' => 'submit'],
  5991. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  5992. '/div'
  5993. ];
  5994. $this->assertHtml($expected, $result);
  5995. }
  5996. /**
  5997. * test image submit types.
  5998. *
  5999. * @return void
  6000. */
  6001. public function testSubmitImage()
  6002. {
  6003. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6004. $expected = [
  6005. 'div' => ['class' => 'submit'],
  6006. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6007. '/div'
  6008. ];
  6009. $this->assertHtml($expected, $result);
  6010. $result = $this->Form->submit('/relative/cake.power.gif');
  6011. $expected = [
  6012. 'div' => ['class' => 'submit'],
  6013. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6014. '/div'
  6015. ];
  6016. $this->assertHtml($expected, $result);
  6017. $result = $this->Form->submit('cake.power.gif');
  6018. $expected = [
  6019. 'div' => ['class' => 'submit'],
  6020. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6021. '/div'
  6022. ];
  6023. $this->assertHtml($expected, $result);
  6024. $result = $this->Form->submit('Not.an.image');
  6025. $expected = [
  6026. 'div' => ['class' => 'submit'],
  6027. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6028. '/div'
  6029. ];
  6030. $this->assertHtml($expected, $result);
  6031. }
  6032. /**
  6033. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6034. * be submitted at a time.
  6035. *
  6036. * @return void
  6037. */
  6038. public function testSubmitUnlockedByDefault()
  6039. {
  6040. $this->Form->request->params['_Token'] = 'secured';
  6041. $this->Form->submit('Go go');
  6042. $this->Form->submit('Save', ['name' => 'save']);
  6043. $result = $this->Form->unlockField();
  6044. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6045. }
  6046. /**
  6047. * Test submit image with timestamps.
  6048. *
  6049. * @return void
  6050. */
  6051. public function testSubmitImageTimestamp()
  6052. {
  6053. Configure::write('Asset.timestamp', 'force');
  6054. $result = $this->Form->submit('cake.power.gif');
  6055. $expected = [
  6056. 'div' => ['class' => 'submit'],
  6057. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6058. '/div'
  6059. ];
  6060. $this->assertHtml($expected, $result);
  6061. }
  6062. /**
  6063. * test that datetime() works with GET style forms.
  6064. *
  6065. * @return void
  6066. */
  6067. public function testDateTimeWithGetForms()
  6068. {
  6069. extract($this->dateRegex);
  6070. $this->Form->create($this->article, ['type' => 'get']);
  6071. $result = $this->Form->datetime('created');
  6072. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  6073. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  6074. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  6075. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  6076. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  6077. }
  6078. /**
  6079. * testForMagicInputNonExistingNorValidated method
  6080. *
  6081. * @return void
  6082. */
  6083. public function testForMagicInputNonExistingNorValidated()
  6084. {
  6085. $result = $this->Form->create($this->article);
  6086. $this->Form->templates(['inputContainer' => '{{content}}']);
  6087. $result = $this->Form->input('non_existing_nor_validated');
  6088. $expected = [
  6089. 'label' => ['for' => 'non-existing-nor-validated'],
  6090. 'Non Existing Nor Validated',
  6091. '/label',
  6092. 'input' => [
  6093. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6094. 'id' => 'non-existing-nor-validated'
  6095. ]
  6096. ];
  6097. $this->assertHtml($expected, $result);
  6098. $result = $this->Form->input('non_existing_nor_validated', [
  6099. 'val' => 'my value'
  6100. ]);
  6101. $expected = [
  6102. 'label' => ['for' => 'non-existing-nor-validated'],
  6103. 'Non Existing Nor Validated',
  6104. '/label',
  6105. 'input' => [
  6106. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6107. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  6108. ]
  6109. ];
  6110. $this->assertHtml($expected, $result);
  6111. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  6112. $result = $this->Form->input('non_existing_nor_validated');
  6113. $expected = [
  6114. 'label' => ['for' => 'non-existing-nor-validated'],
  6115. 'Non Existing Nor Validated',
  6116. '/label',
  6117. 'input' => [
  6118. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6119. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  6120. ]
  6121. ];
  6122. $this->assertHtml($expected, $result);
  6123. }
  6124. /**
  6125. * testFormMagicInputLabel method
  6126. *
  6127. * @return void
  6128. */
  6129. public function testFormMagicInputLabel()
  6130. {
  6131. TableRegistry::get('Contacts', [
  6132. 'className' => __NAMESPACE__ . '\ContactsTable'
  6133. ]);
  6134. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6135. $this->Form->templates(['inputContainer' => '{{content}}']);
  6136. $result = $this->Form->input('Contacts.name', ['label' => 'My label']);
  6137. $expected = [
  6138. 'label' => ['for' => 'contacts-name'],
  6139. 'My label',
  6140. '/label',
  6141. 'input' => [
  6142. 'type' => 'text', 'name' => 'Contacts[name]',
  6143. 'id' => 'contacts-name', 'maxlength' => '255'
  6144. ]
  6145. ];
  6146. $this->assertHtml($expected, $result);
  6147. $result = $this->Form->input('name', [
  6148. 'label' => ['class' => 'mandatory']
  6149. ]);
  6150. $expected = [
  6151. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6152. 'Name',
  6153. '/label',
  6154. 'input' => [
  6155. 'type' => 'text', 'name' => 'name',
  6156. 'id' => 'name', 'maxlength' => '255'
  6157. ]
  6158. ];
  6159. $this->assertHtml($expected, $result);
  6160. $result = $this->Form->input('name', [
  6161. 'div' => false,
  6162. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  6163. ]);
  6164. $expected = [
  6165. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6166. 'My label',
  6167. '/label',
  6168. 'input' => [
  6169. 'type' => 'text', 'name' => 'name',
  6170. 'id' => 'name', 'maxlength' => '255'
  6171. ]
  6172. ];
  6173. $this->assertHtml($expected, $result);
  6174. $result = $this->Form->input('Contact.name', [
  6175. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  6176. ]);
  6177. $expected = [
  6178. 'label' => ['for' => 'my_id'],
  6179. 'Name',
  6180. '/label',
  6181. 'input' => [
  6182. 'type' => 'text', 'name' => 'Contact[name]',
  6183. 'id' => 'my_id', 'maxlength' => '255'
  6184. ]
  6185. ];
  6186. $this->assertHtml($expected, $result);
  6187. $result = $this->Form->input('1.id');
  6188. $expected = ['input' => [
  6189. 'type' => 'hidden', 'name' => '1[id]',
  6190. 'id' => '1-id'
  6191. ]];
  6192. $this->assertHtml($expected, $result);
  6193. $result = $this->Form->input("1.name");
  6194. $expected = [
  6195. 'label' => ['for' => '1-name'],
  6196. 'Name',
  6197. '/label',
  6198. 'input' => [
  6199. 'type' => 'text', 'name' => '1[name]',
  6200. 'id' => '1-name', 'maxlength' => '255'
  6201. ]
  6202. ];
  6203. $this->assertHtml($expected, $result);
  6204. }
  6205. /**
  6206. * testFormEnd method
  6207. *
  6208. * @return void
  6209. */
  6210. public function testFormEnd()
  6211. {
  6212. $this->assertEquals('</form>', $this->Form->end());
  6213. }
  6214. /**
  6215. * Test the generation of fields for a multi record form.
  6216. *
  6217. * @return void
  6218. */
  6219. public function testMultiRecordForm()
  6220. {
  6221. $this->loadFixtures('Articles', 'Comments');
  6222. $articles = TableRegistry::get('Articles');
  6223. $articles->hasMany('Comments');
  6224. $comment = new Entity(['comment' => 'Value']);
  6225. $article = new Article(['comments' => [$comment]]);
  6226. $this->Form->create([$article]);
  6227. $result = $this->Form->input('0.comments.1.comment');
  6228. //@codingStandardsIgnoreStart
  6229. $expected = [
  6230. 'div' => ['class' => 'input textarea'],
  6231. 'label' => ['for' => '0-comments-1-comment'],
  6232. 'Comment',
  6233. '/label',
  6234. 'textarea' => [
  6235. 'name',
  6236. 'id' => '0-comments-1-comment',
  6237. 'rows' => 5
  6238. ],
  6239. '/textarea',
  6240. '/div'
  6241. ];
  6242. //@codingStandardsIgnoreEnd
  6243. $this->assertHtml($expected, $result);
  6244. $result = $this->Form->input('0.comments.0.comment');
  6245. //@codingStandardsIgnoreStart
  6246. $expected = [
  6247. 'div' => ['class' => 'input textarea'],
  6248. 'label' => ['for' => '0-comments-0-comment'],
  6249. 'Comment',
  6250. '/label',
  6251. 'textarea' => [
  6252. 'name',
  6253. 'id' => '0-comments-0-comment',
  6254. 'rows' => 5
  6255. ],
  6256. 'Value',
  6257. '/textarea',
  6258. '/div'
  6259. ];
  6260. //@codingStandardsIgnoreEnd
  6261. $this->assertHtml($expected, $result);
  6262. $comment->errors('comment', ['Not valid']);
  6263. $result = $this->Form->input('0.comments.0.comment');
  6264. //@codingStandardsIgnoreStart
  6265. $expected = [
  6266. 'div' => ['class' => 'input textarea error'],
  6267. 'label' => ['for' => '0-comments-0-comment'],
  6268. 'Comment',
  6269. '/label',
  6270. 'textarea' => [
  6271. 'name',
  6272. 'class' => 'form-error',
  6273. 'id' => '0-comments-0-comment',
  6274. 'rows' => 5
  6275. ],
  6276. 'Value',
  6277. '/textarea',
  6278. ['div' => ['class' => 'error-message']],
  6279. 'Not valid',
  6280. '/div',
  6281. '/div'
  6282. ];
  6283. //@codingStandardsIgnoreEnd
  6284. $this->assertHtml($expected, $result);
  6285. TableRegistry::get('Comments')
  6286. ->validator('default')
  6287. ->allowEmpty('comment', false);
  6288. $result = $this->Form->input('0.comments.1.comment');
  6289. //@codingStandardsIgnoreStart
  6290. $expected = [
  6291. 'div' => ['class' => 'input textarea required'],
  6292. 'label' => ['for' => '0-comments-1-comment'],
  6293. 'Comment',
  6294. '/label',
  6295. 'textarea' => [
  6296. 'name',
  6297. 'required' => 'required',
  6298. 'id' => '0-comments-1-comment',
  6299. 'rows' => 5
  6300. ],
  6301. '/textarea',
  6302. '/div'
  6303. ];
  6304. //@codingStandardsIgnoreEnd
  6305. $this->assertHtml($expected, $result);
  6306. }
  6307. /**
  6308. * test that some html5 inputs + FormHelper::__call() work
  6309. *
  6310. * @return void
  6311. */
  6312. public function testHtml5Inputs()
  6313. {
  6314. $result = $this->Form->email('User.email');
  6315. $expected = [
  6316. 'input' => ['type' => 'email', 'name' => 'User[email]']
  6317. ];
  6318. $this->assertHtml($expected, $result);
  6319. $result = $this->Form->search('User.query');
  6320. $expected = [
  6321. 'input' => ['type' => 'search', 'name' => 'User[query]']
  6322. ];
  6323. $this->assertHtml($expected, $result);
  6324. $result = $this->Form->search('User.query', ['value' => 'test']);
  6325. $expected = [
  6326. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  6327. ];
  6328. $this->assertHtml($expected, $result);
  6329. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  6330. $expected = [
  6331. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  6332. ];
  6333. $this->assertHtml($expected, $result);
  6334. }
  6335. /**
  6336. * Test accessing html5 inputs through input().
  6337. *
  6338. * @return void
  6339. */
  6340. public function testHtml5InputWithInput()
  6341. {
  6342. $this->Form->create();
  6343. $this->Form->templates(['inputContainer' => '{{content}}']);
  6344. $result = $this->Form->input('website', [
  6345. 'type' => 'url',
  6346. 'val' => 'http://domain.tld',
  6347. 'label' => false
  6348. ]);
  6349. $expected = [
  6350. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  6351. ];
  6352. $this->assertHtml($expected, $result);
  6353. }
  6354. /**
  6355. * Test errors when field name is missing.
  6356. *
  6357. * @expectedException \Cake\Core\Exception\Exception
  6358. * @return void
  6359. */
  6360. public function testHtml5InputException()
  6361. {
  6362. $this->Form->email();
  6363. }
  6364. /**
  6365. * Tests that formhelper sets required attributes.
  6366. *
  6367. * @return void
  6368. */
  6369. public function testRequiredAttribute()
  6370. {
  6371. $this->article['required'] = [
  6372. 'title' => true,
  6373. 'body' => false,
  6374. ];
  6375. $this->Form->create($this->article);
  6376. $result = $this->Form->input('title');
  6377. $expected = [
  6378. 'div' => ['class' => 'input text required'],
  6379. 'label' => ['for' => 'title'],
  6380. 'Title',
  6381. '/label',
  6382. 'input' => [
  6383. 'type' => 'text',
  6384. 'name' => 'title',
  6385. 'id' => 'title',
  6386. 'required' => 'required',
  6387. ],
  6388. '/div'
  6389. ];
  6390. $this->assertHtml($expected, $result);
  6391. $result = $this->Form->input('title', ['required' => false]);
  6392. $this->assertNotContains('required', $result);
  6393. $result = $this->Form->input('body');
  6394. $expected = [
  6395. 'div' => ['class' => 'input text'],
  6396. 'label' => ['for' => 'body'],
  6397. 'Body',
  6398. '/label',
  6399. 'input' => [
  6400. 'type' => 'text',
  6401. 'name' => 'body',
  6402. 'id' => 'body',
  6403. ],
  6404. '/div'
  6405. ];
  6406. $this->assertHtml($expected, $result);
  6407. $result = $this->Form->input('body', ['required' => true]);
  6408. $this->assertContains('required', $result);
  6409. }
  6410. /**
  6411. * Tests that it is possible to put inputs outside of the label
  6412. *
  6413. * @return void
  6414. */
  6415. public function testInputsNotNested()
  6416. {
  6417. $this->Form->templates([
  6418. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  6419. 'formGroup' => '{{input}}{{label}}',
  6420. ]);
  6421. $result = $this->Form->input('foo', ['type' => 'checkbox']);
  6422. $expected = [
  6423. 'div' => ['class' => 'input checkbox'],
  6424. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  6425. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  6426. 'label' => ['for' => 'foo'],
  6427. 'Foo',
  6428. '/label',
  6429. '/div'
  6430. ];
  6431. $this->assertHtml($expected, $result);
  6432. $result = $this->Form->input('foo', ['type' => 'checkbox', 'label' => false]);
  6433. $expected = [
  6434. 'div' => ['class' => 'input checkbox'],
  6435. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  6436. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  6437. '/div'
  6438. ];
  6439. $this->assertHtml($expected, $result);
  6440. $result = $this->Form->input('confirm', [
  6441. 'type' => 'radio',
  6442. 'options' => ['Y' => 'Yes', 'N' => 'No']
  6443. ]);
  6444. $expected = [
  6445. 'div' => ['class' => 'input radio'],
  6446. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  6447. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  6448. ['label' => ['for' => 'confirm-y']],
  6449. 'Yes',
  6450. '/label',
  6451. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  6452. ['label' => ['for' => 'confirm-n']],
  6453. 'No',
  6454. '/label',
  6455. '<label',
  6456. 'Confirm',
  6457. '/label',
  6458. '/div',
  6459. ];
  6460. $this->assertHtml($expected, $result);
  6461. $result = $this->Form->select('category', ['1', '2'], [
  6462. 'multiple' => 'checkbox',
  6463. 'name' => 'fish',
  6464. ]);
  6465. $expected = [
  6466. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  6467. ['div' => ['class' => 'checkbox']],
  6468. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  6469. ['label' => ['for' => 'fish-0']],
  6470. '1',
  6471. '/label',
  6472. '/div',
  6473. ['div' => ['class' => 'checkbox']],
  6474. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  6475. ['label' => ['for' => 'fish-1']],
  6476. '2',
  6477. '/label',
  6478. '/div'
  6479. ];
  6480. $this->assertHtml($expected, $result);
  6481. }
  6482. /**
  6483. * Test that *Container templates are used by input.
  6484. *
  6485. * @return void
  6486. */
  6487. public function testInputContainerTemplates()
  6488. {
  6489. $this->Form->templates([
  6490. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  6491. 'radioContainer' => '<div class="rad">{{content}}</div>',
  6492. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  6493. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  6494. ]);
  6495. $this->article['errors'] = [
  6496. 'Article' => ['published' => 'error message']
  6497. ];
  6498. $this->Form->create($this->article);
  6499. $result = $this->Form->input('accept', [
  6500. 'type' => 'checkbox'
  6501. ]);
  6502. $expected = [
  6503. 'div' => ['class' => 'check'],
  6504. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  6505. 'label' => ['for' => 'accept'],
  6506. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  6507. 'Accept',
  6508. '/label',
  6509. '/div'
  6510. ];
  6511. $this->assertHtml($expected, $result);
  6512. $result = $this->Form->input('accept', [
  6513. 'type' => 'radio',
  6514. 'options' => ['Y', 'N']
  6515. ]);
  6516. $this->assertContains('<div class="rad">', $result);
  6517. $result = $this->Form->input('Article.published', [
  6518. 'type' => 'radio',
  6519. 'options' => ['Y', 'N']
  6520. ]);
  6521. $this->assertContains('<div class="rad err">', $result);
  6522. $result = $this->Form->input('Article.created', [
  6523. 'type' => 'datetime',
  6524. ]);
  6525. $this->assertContains('<div class="dt">', $result);
  6526. }
  6527. /**
  6528. * Test that *Container templates are used by input.
  6529. *
  6530. * @return void
  6531. */
  6532. public function testFormGroupTemplates()
  6533. {
  6534. $this->Form->templates([
  6535. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  6536. ]);
  6537. $this->Form->create($this->article);
  6538. $result = $this->Form->input('accept', [
  6539. 'type' => 'radio',
  6540. 'options' => ['Y', 'N']
  6541. ]);
  6542. $this->assertContains('<div class="radio">', $result);
  6543. }
  6544. /**
  6545. * Test resetting templates.
  6546. *
  6547. * @return void
  6548. */
  6549. public function testResetTemplates()
  6550. {
  6551. $this->Form->templates(['input' => '<input>']);
  6552. $this->assertEquals('<input>', $this->Form->templater()->get('input'));
  6553. $this->assertNull($this->Form->resetTemplates());
  6554. $this->assertNotEquals('<input>', $this->Form->templater()->get('input'));
  6555. }
  6556. /**
  6557. * Test the context method.
  6558. *
  6559. * @return void
  6560. */
  6561. public function testContext()
  6562. {
  6563. $result = $this->Form->context();
  6564. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  6565. $mock = $this->getMock('Cake\View\Form\ContextInterface');
  6566. $this->assertSame($mock, $this->Form->context($mock));
  6567. $this->assertSame($mock, $this->Form->context());
  6568. }
  6569. }