FormHelperTest.php 167 KB

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