FormHelperTest.php 168 KB

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