FormHelperTest.php 176 KB

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