FormHelperTest.php 231 KB

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