FormHelperTest.php 177 KB

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