FormHelperTest.php 168 KB

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