FormHelperTest.php 182 KB

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