FormHelperTest.php 180 KB

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