FormHelperTest.php 165 KB

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