FormHelperTest.php 169 KB

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