FormHelperTest.php 168 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791
  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(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2305. $expected = array(
  2306. 'fieldset' => array('class' => 'classy-stuff'),
  2307. '<legend',
  2308. 'Field of Dreams',
  2309. '/legend',
  2310. '*/fieldset'
  2311. );
  2312. $this->assertTags($result, $expected);
  2313. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2314. $this->assertTags($result, $expected);
  2315. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2316. $this->assertTags($result, $expected);
  2317. }
  2318. /**
  2319. * Test the inputs() method.
  2320. *
  2321. * @return void
  2322. */
  2323. public function testFormInputs() {
  2324. $this->Form->create($this->article);
  2325. $this->Form->request['prefix'] = 'admin';
  2326. $this->Form->request['action'] = 'admin_edit';
  2327. $result = $this->Form->inputs();
  2328. $expected = array(
  2329. '<fieldset',
  2330. '<legend',
  2331. 'Edit Article',
  2332. '/legend',
  2333. '*/fieldset',
  2334. );
  2335. $this->assertTags($result, $expected);
  2336. $this->Form->create('Contact');
  2337. $result = $this->Form->inputs(false);
  2338. $expected = array(
  2339. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2340. array('div' => array('class' => 'input text')),
  2341. '*/div',
  2342. array('div' => array('class' => 'input email')),
  2343. '*/div',
  2344. array('div' => array('class' => 'input tel')),
  2345. '*/div',
  2346. array('div' => array('class' => 'input password')),
  2347. '*/div',
  2348. array('div' => array('class' => 'input date')),
  2349. '*/div',
  2350. array('div' => array('class' => 'input date')),
  2351. '*/div',
  2352. array('div' => array('class' => 'input datetime')),
  2353. '*/div',
  2354. array('div' => array('class' => 'input number')),
  2355. '*/div',
  2356. array('div' => array('class' => 'input select')),
  2357. '*/div',
  2358. );
  2359. $this->assertTags($result, $expected);
  2360. $this->Form->create('Contact');
  2361. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2362. $expected = array(
  2363. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2364. array('div' => array('class' => 'input text')),
  2365. '*/div',
  2366. array('div' => array('class' => 'input email')),
  2367. '*/div',
  2368. array('div' => array('class' => 'input tel')),
  2369. '*/div',
  2370. array('div' => array('class' => 'input password')),
  2371. '*/div',
  2372. array('div' => array('class' => 'input date')),
  2373. '*/div',
  2374. array('div' => array('class' => 'input date')),
  2375. '*/div',
  2376. array('div' => array('class' => 'input datetime')),
  2377. '*/div',
  2378. array('div' => array('class' => 'input number')),
  2379. '*/div',
  2380. array('div' => array('class' => 'input select')),
  2381. '*/div',
  2382. );
  2383. $this->assertTags($result, $expected);
  2384. $this->Form->create('Contact');
  2385. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2386. $this->assertTags($result, $expected);
  2387. $this->Form->create('Contact');
  2388. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2389. $expected = array(
  2390. 'fieldset' => array(),
  2391. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2392. array('div' => array('class' => 'input text')),
  2393. '*/div',
  2394. array('div' => array('class' => 'input email')),
  2395. '*/div',
  2396. array('div' => array('class' => 'input tel')),
  2397. '*/div',
  2398. array('div' => array('class' => 'input password')),
  2399. '*/div',
  2400. array('div' => array('class' => 'input date')),
  2401. '*/div',
  2402. array('div' => array('class' => 'input date')),
  2403. '*/div',
  2404. array('div' => array('class' => 'input datetime')),
  2405. '*/div',
  2406. array('div' => array('class' => 'input number')),
  2407. '*/div',
  2408. array('div' => array('class' => 'input select')),
  2409. '*/div',
  2410. '/fieldset'
  2411. );
  2412. $this->assertTags($result, $expected);
  2413. $this->Form->create('Contact');
  2414. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2415. $expected = array(
  2416. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2417. array('div' => array('class' => 'input text')),
  2418. '*/div',
  2419. array('div' => array('class' => 'input email')),
  2420. '*/div',
  2421. array('div' => array('class' => 'input tel')),
  2422. '*/div',
  2423. array('div' => array('class' => 'input password')),
  2424. '*/div',
  2425. array('div' => array('class' => 'input date')),
  2426. '*/div',
  2427. array('div' => array('class' => 'input date')),
  2428. '*/div',
  2429. array('div' => array('class' => 'input datetime')),
  2430. '*/div',
  2431. array('div' => array('class' => 'input number')),
  2432. '*/div',
  2433. array('div' => array('class' => 'input select')),
  2434. '*/div',
  2435. );
  2436. $this->assertTags($result, $expected);
  2437. $this->Form->create('Contact');
  2438. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2439. $this->assertTags($result, $expected);
  2440. $this->Form->create('Contact');
  2441. $result = $this->Form->inputs('Hello');
  2442. $expected = array(
  2443. 'fieldset' => array(),
  2444. 'legend' => array(),
  2445. 'Hello',
  2446. '/legend',
  2447. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2448. array('div' => array('class' => 'input text')),
  2449. '*/div',
  2450. array('div' => array('class' => 'input email')),
  2451. '*/div',
  2452. array('div' => array('class' => 'input tel')),
  2453. '*/div',
  2454. array('div' => array('class' => 'input password')),
  2455. '*/div',
  2456. array('div' => array('class' => 'input date')),
  2457. '*/div',
  2458. array('div' => array('class' => 'input date')),
  2459. '*/div',
  2460. array('div' => array('class' => 'input datetime')),
  2461. '*/div',
  2462. array('div' => array('class' => 'input number')),
  2463. '*/div',
  2464. array('div' => array('class' => 'input select')),
  2465. '*/div',
  2466. '/fieldset'
  2467. );
  2468. $this->assertTags($result, $expected);
  2469. $this->Form->create('Contact');
  2470. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2471. $expected = array(
  2472. 'fieldset' => array(),
  2473. 'legend' => array(),
  2474. 'Hello',
  2475. '/legend',
  2476. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2477. array('div' => array('class' => 'input text')),
  2478. '*/div',
  2479. array('div' => array('class' => 'input email')),
  2480. '*/div',
  2481. array('div' => array('class' => 'input tel')),
  2482. '*/div',
  2483. array('div' => array('class' => 'input password')),
  2484. '*/div',
  2485. array('div' => array('class' => 'input date')),
  2486. '*/div',
  2487. array('div' => array('class' => 'input date')),
  2488. '*/div',
  2489. array('div' => array('class' => 'input datetime')),
  2490. '*/div',
  2491. array('div' => array('class' => 'input number')),
  2492. '*/div',
  2493. array('div' => array('class' => 'input select')),
  2494. '*/div',
  2495. '/fieldset'
  2496. );
  2497. $this->assertTags($result, $expected);
  2498. $this->Form->create('Contact');
  2499. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2500. $this->assertTags($result, $expected);
  2501. $this->Form->end();
  2502. $this->Form->create(false);
  2503. $expected = array(
  2504. 'fieldset' => array(),
  2505. array('div' => array('class' => 'input text')),
  2506. 'label' => array('for' => 'foo'),
  2507. 'Foo',
  2508. '/label',
  2509. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  2510. '*/div',
  2511. '/fieldset'
  2512. );
  2513. $result = $this->Form->inputs(
  2514. array('foo' => array('type' => 'text')),
  2515. array(),
  2516. array('legend' => false)
  2517. );
  2518. $this->assertTags($result, $expected);
  2519. }
  2520. /**
  2521. * testSelectAsCheckbox method
  2522. *
  2523. * test multi-select widget with checkbox formatting.
  2524. *
  2525. * @return void
  2526. */
  2527. public function testSelectAsCheckbox() {
  2528. $result = $this->Form->select(
  2529. 'Model.multi_field',
  2530. array('first', 'second', 'third'),
  2531. array('multiple' => 'checkbox', 'value' => array(0, 1))
  2532. );
  2533. $expected = array(
  2534. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2535. array('div' => array('class' => 'checkbox')),
  2536. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  2537. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  2538. 'first',
  2539. '/label',
  2540. '/div',
  2541. array('div' => array('class' => 'checkbox')),
  2542. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  2543. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  2544. 'second',
  2545. '/label',
  2546. '/div',
  2547. array('div' => array('class' => 'checkbox')),
  2548. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  2549. array('label' => array('for' => 'model-multi-field-2')),
  2550. 'third',
  2551. '/label',
  2552. '/div',
  2553. );
  2554. $this->assertTags($result, $expected);
  2555. $result = $this->Form->select(
  2556. 'Model.multi_field',
  2557. array('1/2' => 'half'),
  2558. array('multiple' => 'checkbox')
  2559. );
  2560. $expected = array(
  2561. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2562. array('div' => array('class' => 'checkbox')),
  2563. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  2564. array('label' => array('for' => 'model-multi-field-1-2')),
  2565. 'half',
  2566. '/label',
  2567. '/div',
  2568. );
  2569. $this->assertTags($result, $expected);
  2570. }
  2571. /**
  2572. * testLabel method
  2573. *
  2574. * test label generation.
  2575. *
  2576. * @return void
  2577. */
  2578. public function testLabel() {
  2579. $result = $this->Form->label('Person.name');
  2580. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  2581. $result = $this->Form->label('Person.name');
  2582. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  2583. $result = $this->Form->label('Person.first_name');
  2584. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
  2585. $result = $this->Form->label('Person.first_name', 'Your first name');
  2586. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
  2587. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2588. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
  2589. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2590. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2591. $result = $this->Form->label('Person.first_name', '');
  2592. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
  2593. $result = $this->Form->label('Person.2.name', '');
  2594. $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
  2595. }
  2596. /**
  2597. * testTextbox method
  2598. *
  2599. * test textbox element generation
  2600. *
  2601. * @return void
  2602. */
  2603. public function testTextbox() {
  2604. $result = $this->Form->text('Model.field');
  2605. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));
  2606. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2607. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));
  2608. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2609. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  2610. }
  2611. /**
  2612. * Test that text() hooks up with request data and error fields.
  2613. *
  2614. * @return void
  2615. */
  2616. public function testTextBoxDataAndError() {
  2617. $this->article['errors'] = [
  2618. 'Contact' => ['text' => 'wrong']
  2619. ];
  2620. $this->Form->create($this->article);
  2621. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2622. $result = $this->Form->text('Model.text');
  2623. $expected = [
  2624. 'input' => [
  2625. 'type' => 'text',
  2626. 'name' => 'Model[text]',
  2627. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  2628. ]
  2629. ];
  2630. $this->assertTags($result, $expected);
  2631. $this->Form->request->data['Contact']['text'] = 'test';
  2632. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  2633. $expected = [
  2634. 'input' => [
  2635. 'type' => 'text',
  2636. 'name' => 'Contact[text]',
  2637. 'value' => 'test',
  2638. 'id' => 'theID',
  2639. 'class' => 'form-error'
  2640. ]
  2641. ];
  2642. $this->assertTags($result, $expected);
  2643. }
  2644. /**
  2645. * testDefaultValue method
  2646. *
  2647. * Test default value setting
  2648. *
  2649. * @return void
  2650. */
  2651. public function testTextDefaultValue() {
  2652. $this->Form->request->data['Model']['field'] = 'test';
  2653. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2654. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  2655. $this->assertTags($result, $expected);
  2656. unset($this->Form->request->data['Model']['field']);
  2657. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2658. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  2659. $this->assertTags($result, $expected);
  2660. }
  2661. /**
  2662. * testError method
  2663. *
  2664. * Test field error generation
  2665. *
  2666. * @return void
  2667. */
  2668. public function testError() {
  2669. $this->article['errors'] = [
  2670. 'Article' => ['field' => 'email']
  2671. ];
  2672. $this->Form->create($this->article);
  2673. $result = $this->Form->error('Article.field');
  2674. $expected = [
  2675. ['div' => ['class' => 'error-message']],
  2676. 'email',
  2677. '/div',
  2678. ];
  2679. $this->assertTags($result, $expected);
  2680. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  2681. $expected = [
  2682. ['div' => ['class' => 'error-message']],
  2683. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  2684. '/div',
  2685. ];
  2686. $this->assertTags($result, $expected);
  2687. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  2688. $expected = [
  2689. ['div' => ['class' => 'error-message']],
  2690. '<strong', 'Badness!', '/strong',
  2691. '/div',
  2692. ];
  2693. $this->assertTags($result, $expected);
  2694. }
  2695. /**
  2696. * Test error with nested lists.
  2697. *
  2698. * @return void
  2699. */
  2700. public function testErrorMessages() {
  2701. $this->article['errors'] = [
  2702. 'Article' => ['field' => 'email']
  2703. ];
  2704. $this->Form->create($this->article);
  2705. $result = $this->Form->error('Article.field', array(
  2706. 'email' => 'No good!'
  2707. ));
  2708. $expected = array(
  2709. 'div' => array('class' => 'error-message'),
  2710. 'No good!',
  2711. '/div'
  2712. );
  2713. $this->assertTags($result, $expected);
  2714. }
  2715. /**
  2716. * test error() with multiple messages.
  2717. *
  2718. * @return void
  2719. */
  2720. public function testErrorMultipleMessages() {
  2721. $this->article['errors'] = [
  2722. 'field' => ['notEmpty', 'email', 'Something else']
  2723. ];
  2724. $this->Form->create($this->article);
  2725. $result = $this->Form->error('field', array(
  2726. 'notEmpty' => 'Cannot be empty',
  2727. 'email' => 'No good!'
  2728. ));
  2729. $expected = array(
  2730. 'div' => array('class' => 'error-message'),
  2731. 'ul' => array(),
  2732. '<li', 'Cannot be empty', '/li',
  2733. '<li', 'No good!', '/li',
  2734. '<li', 'Something else', '/li',
  2735. '/ul',
  2736. '/div'
  2737. );
  2738. $this->assertTags($result, $expected);
  2739. }
  2740. /**
  2741. * testPassword method
  2742. *
  2743. * Test password element generation
  2744. *
  2745. * @return void
  2746. */
  2747. public function testPassword() {
  2748. $this->article['errors'] = [
  2749. 'Contact' => [
  2750. 'passwd' => 1
  2751. ]
  2752. ];
  2753. $this->Form->create($this->article);
  2754. $result = $this->Form->password('Contact.field');
  2755. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));
  2756. $this->Form->request->data['Contact']['passwd'] = 'test';
  2757. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2758. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2759. }
  2760. /**
  2761. * testRadio method
  2762. *
  2763. * Test radio element set generation
  2764. *
  2765. * @return void
  2766. */
  2767. public function testRadio() {
  2768. $result = $this->Form->radio('Model.field', array('option A'));
  2769. $expected = array(
  2770. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  2771. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  2772. 'label' => array('for' => 'model-field-0'),
  2773. 'option A',
  2774. '/label'
  2775. );
  2776. $this->assertTags($result, $expected);
  2777. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2778. $expected = array(
  2779. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  2780. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  2781. array('label' => array('for' => 'model-field-0')),
  2782. 'option A',
  2783. '/label',
  2784. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  2785. array('label' => array('for' => 'model-field-1')),
  2786. 'option B',
  2787. '/label',
  2788. );
  2789. $this->assertTags($result, $expected);
  2790. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  2791. $expected = array(
  2792. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''),
  2793. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male')),
  2794. array('label' => array('for' => 'employee-gender-male')),
  2795. 'Male',
  2796. '/label',
  2797. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female')),
  2798. array('label' => array('for' => 'employee-gender-female')),
  2799. 'Female',
  2800. '/label',
  2801. );
  2802. $this->assertTags($result, $expected);
  2803. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  2804. $expected = array(
  2805. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  2806. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  2807. array('label' => array('for' => 'model-custom-0')),
  2808. 'option A',
  2809. '/label',
  2810. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  2811. array('label' => array('for' => 'model-custom-1')),
  2812. 'option B',
  2813. '/label',
  2814. );
  2815. $this->assertTags($result, $expected);
  2816. }
  2817. /**
  2818. * test generating radio input inside label ala twitter bootstrap
  2819. *
  2820. * @return void
  2821. */
  2822. public function testRadioInputInsideLabel() {
  2823. $this->Form->templates([
  2824. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  2825. 'radioContainer' => '{{label}}'
  2826. ]);
  2827. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  2828. $expected = [
  2829. ['input' => [
  2830. 'type' => 'hidden',
  2831. 'name' => 'Model[field]',
  2832. 'value' => ''
  2833. ]],
  2834. ['label' => ['for' => 'model-field-0']],
  2835. ['input' => [
  2836. 'type' => 'radio',
  2837. 'name' => 'Model[field]',
  2838. 'value' => '0',
  2839. 'id' => 'model-field-0'
  2840. ]],
  2841. 'option A',
  2842. '/label',
  2843. ['label' => ['for' => 'model-field-1']],
  2844. ['input' => [
  2845. 'type' => 'radio',
  2846. 'name' => 'Model[field]',
  2847. 'value' => '1',
  2848. 'id' => 'model-field-1'
  2849. ]],
  2850. 'option B',
  2851. '/label',
  2852. ];
  2853. $this->assertTags($result, $expected);
  2854. }
  2855. /**
  2856. * test disabling the hidden input for radio buttons
  2857. *
  2858. * @return void
  2859. */
  2860. public function testRadioHiddenInputDisabling() {
  2861. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  2862. $expected = array(
  2863. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  2864. 'label' => array('for' => 'model-1-field-0'),
  2865. 'option A',
  2866. '/label'
  2867. );
  2868. $this->assertTags($result, $expected);
  2869. }
  2870. /**
  2871. * testSelect method
  2872. *
  2873. * Test select element generation.
  2874. *
  2875. * @return void
  2876. */
  2877. public function testSelect() {
  2878. $result = $this->Form->select('Model.field', array());
  2879. $expected = array(
  2880. 'select' => array('name' => 'Model[field]'),
  2881. '/select'
  2882. );
  2883. $this->assertTags($result, $expected);
  2884. $this->Form->request->data = array('Model' => array('field' => 'value'));
  2885. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  2886. $expected = array(
  2887. 'select' => array('name' => 'Model[field]'),
  2888. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2889. 'good',
  2890. '/option',
  2891. array('option' => array('value' => 'other')),
  2892. 'bad',
  2893. '/option',
  2894. '/select'
  2895. );
  2896. $this->assertTags($result, $expected);
  2897. $this->Form->request->data = array();
  2898. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  2899. $expected = array(
  2900. 'select' => array('name' => 'Model[field]'),
  2901. array('option' => array('value' => 'value')),
  2902. 'good',
  2903. '/option',
  2904. array('option' => array('value' => 'other')),
  2905. 'bad',
  2906. '/option',
  2907. '/select'
  2908. );
  2909. $this->assertTags($result, $expected);
  2910. $result = $this->Form->select(
  2911. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  2912. array('empty' => false)
  2913. );
  2914. $expected = array(
  2915. 'select' => array('name' => 'Model[field]'),
  2916. array('option' => array('value' => 'first')),
  2917. 'first &quot;html&quot; &lt;chars&gt;',
  2918. '/option',
  2919. array('option' => array('value' => 'second')),
  2920. 'value',
  2921. '/option',
  2922. '/select'
  2923. );
  2924. $this->assertTags($result, $expected);
  2925. $result = $this->Form->select(
  2926. 'Model.field',
  2927. array('first' => 'first "html" <chars>', 'second' => 'value'),
  2928. array('escape' => false, 'empty' => false)
  2929. );
  2930. $expected = array(
  2931. 'select' => array('name' => 'Model[field]'),
  2932. array('option' => array('value' => 'first')),
  2933. 'first "html" <chars>',
  2934. '/option',
  2935. array('option' => array('value' => 'second')),
  2936. 'value',
  2937. '/option',
  2938. '/select'
  2939. );
  2940. $this->assertTags($result, $expected);
  2941. $options = array(
  2942. array('value' => 'first', 'text' => 'First'),
  2943. array('value' => 'first', 'text' => 'Another First'),
  2944. );
  2945. $result = $this->Form->select(
  2946. 'Model.field',
  2947. $options,
  2948. array('escape' => false, 'empty' => false)
  2949. );
  2950. $expected = array(
  2951. 'select' => array('name' => 'Model[field]'),
  2952. array('option' => array('value' => 'first')),
  2953. 'First',
  2954. '/option',
  2955. array('option' => array('value' => 'first')),
  2956. 'Another First',
  2957. '/option',
  2958. '/select'
  2959. );
  2960. $this->assertTags($result, $expected);
  2961. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  2962. $result = $this->Form->select(
  2963. 'Model.contact_id',
  2964. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  2965. array('escape' => false, 'empty' => 'pick something')
  2966. );
  2967. $expected = array(
  2968. 'select' => array('name' => 'Model[contact_id]'),
  2969. array('option' => array('value' => '')), 'pick something', '/option',
  2970. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  2971. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  2972. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  2973. '/select'
  2974. );
  2975. $this->assertTags($result, $expected);
  2976. $this->Form->request->data['Model']['field'] = 0;
  2977. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  2978. $expected = array(
  2979. 'select' => array('name' => 'Model[field]'),
  2980. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  2981. array('option' => array('value' => '1')), 'Yes', '/option',
  2982. '/select'
  2983. );
  2984. $this->assertTags($result, $expected);
  2985. }
  2986. /**
  2987. * test select() with required and disabled attributes.
  2988. *
  2989. * @return void
  2990. */
  2991. public function testSelectRequired() {
  2992. $this->article['required'] = [
  2993. 'user_id' => true
  2994. ];
  2995. $this->Form->create($this->article);
  2996. $result = $this->Form->select('user_id', array('option A'));
  2997. $expected = array(
  2998. 'select' => array(
  2999. 'name' => 'user_id',
  3000. 'required' => 'required'
  3001. ),
  3002. array('option' => array('value' => '0')), 'option A', '/option',
  3003. '/select'
  3004. );
  3005. $this->assertTags($result, $expected);
  3006. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  3007. $expected = array(
  3008. 'select' => array(
  3009. 'name' => 'user_id',
  3010. 'disabled' => 'disabled'
  3011. ),
  3012. array('option' => array('value' => '0')), 'option A', '/option',
  3013. '/select'
  3014. );
  3015. $this->assertTags($result, $expected);
  3016. }
  3017. /**
  3018. * testNestedSelect method
  3019. *
  3020. * test select element generation with optgroups
  3021. *
  3022. * @return void
  3023. */
  3024. public function testNestedSelect() {
  3025. $result = $this->Form->select(
  3026. 'Model.field',
  3027. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3028. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3029. )), array('empty' => false)
  3030. );
  3031. $expected = array(
  3032. 'select' => array('name' => 'Model[field]'),
  3033. array('option' => array('value' => 1)),
  3034. 'One',
  3035. '/option',
  3036. array('option' => array('value' => 2)),
  3037. 'Two',
  3038. '/option',
  3039. array('optgroup' => array('label' => 'Three')),
  3040. array('option' => array('value' => 3)),
  3041. 'Three',
  3042. '/option',
  3043. array('option' => array('value' => 4)),
  3044. 'Four',
  3045. '/option',
  3046. array('option' => array('value' => 5)),
  3047. 'Five',
  3048. '/option',
  3049. '/optgroup',
  3050. '/select'
  3051. );
  3052. $this->assertTags($result, $expected);
  3053. }
  3054. /**
  3055. * testSelectMultiple method
  3056. *
  3057. * test generation of multiple select elements
  3058. *
  3059. * @return void
  3060. */
  3061. public function testSelectMultiple() {
  3062. $options = array('first', 'second', 'third');
  3063. $result = $this->Form->select(
  3064. 'Model.multi_field',
  3065. $options,
  3066. array('multiple' => true)
  3067. );
  3068. $expected = array(
  3069. 'input' => array(
  3070. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3071. ),
  3072. 'select' => array(
  3073. 'name' => 'Model[multi_field][]',
  3074. 'multiple' => 'multiple'
  3075. ),
  3076. array('option' => array('value' => '0')),
  3077. 'first',
  3078. '/option',
  3079. array('option' => array('value' => '1')),
  3080. 'second',
  3081. '/option',
  3082. array('option' => array('value' => '2')),
  3083. 'third',
  3084. '/option',
  3085. '/select'
  3086. );
  3087. $this->assertTags($result, $expected);
  3088. $result = $this->Form->select(
  3089. 'Model.multi_field',
  3090. $options,
  3091. array('multiple' => 'multiple')
  3092. );
  3093. $this->assertTags($result, $expected);
  3094. }
  3095. /**
  3096. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  3097. *
  3098. * @return void
  3099. */
  3100. public function testCheckboxZeroValue() {
  3101. $result = $this->Form->input('User.get_spam', array(
  3102. 'type' => 'checkbox',
  3103. 'value' => '0',
  3104. 'hiddenField' => '1',
  3105. ));
  3106. $expected = array(
  3107. 'div' => array('class' => 'input checkbox'),
  3108. array('input' => array(
  3109. 'type' => 'hidden', 'name' => 'User[get_spam]',
  3110. 'value' => '1'
  3111. )),
  3112. array('input' => array(
  3113. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  3114. 'value' => '0', 'id' => 'user-get-spam'
  3115. )),
  3116. 'label' => array('for' => 'user-get-spam'),
  3117. 'Get Spam',
  3118. '/label',
  3119. '/div'
  3120. );
  3121. $this->assertTags($result, $expected);
  3122. }
  3123. /**
  3124. * test generation of habtm select boxes.
  3125. *
  3126. * @return void
  3127. */
  3128. public function testHabtmSelectBox() {
  3129. $this->loadFixtures('Article');
  3130. $options = array(
  3131. 1 => 'blue',
  3132. 2 => 'red',
  3133. 3 => 'green'
  3134. );
  3135. $tags = [
  3136. new Entity(['id' => 1, 'name' => 'blue']),
  3137. new Entity(['id' => 3, 'name' => 'green'])
  3138. ];
  3139. $article = new Article(['tags' => $tags]);
  3140. $this->Form->create($article);
  3141. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3142. $expected = array(
  3143. 'div' => array('class' => 'input select'),
  3144. 'label' => array('for' => 'tags-ids'),
  3145. 'Tags',
  3146. '/label',
  3147. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3148. 'select' => array(
  3149. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3150. 'multiple' => 'multiple'
  3151. ),
  3152. array('option' => array('value' => '1', 'selected' => 'selected')),
  3153. 'blue',
  3154. '/option',
  3155. array('option' => array('value' => '2')),
  3156. 'red',
  3157. '/option',
  3158. array('option' => array('value' => '3', 'selected' => 'selected')),
  3159. 'green',
  3160. '/option',
  3161. '/select',
  3162. '/div'
  3163. );
  3164. $this->assertTags($result, $expected);
  3165. // make sure only 50 is selected, and not 50f5c0cf
  3166. $options = array(
  3167. '1' => 'blue',
  3168. '50f5c0cf' => 'red',
  3169. '50' => 'green'
  3170. );
  3171. $tags = [
  3172. new Entity(['id' => 1, 'name' => 'blue']),
  3173. new Entity(['id' => 50, 'name' => 'green'])
  3174. ];
  3175. $article = new Article(['tags' => $tags]);
  3176. $this->Form->create($article);
  3177. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3178. $expected = array(
  3179. 'div' => array('class' => 'input select'),
  3180. 'label' => array('for' => 'tags-ids'),
  3181. 'Tags',
  3182. '/label',
  3183. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3184. 'select' => array(
  3185. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3186. 'multiple' => 'multiple'
  3187. ),
  3188. array('option' => array('value' => '1', 'selected' => 'selected')),
  3189. 'blue',
  3190. '/option',
  3191. array('option' => array('value' => '50f5c0cf')),
  3192. 'red',
  3193. '/option',
  3194. array('option' => array('value' => '50', 'selected' => 'selected')),
  3195. 'green',
  3196. '/option',
  3197. '/select',
  3198. '/div'
  3199. );
  3200. $this->assertTags($result, $expected);
  3201. }
  3202. /**
  3203. * test generation of multi select elements in checkbox format
  3204. *
  3205. * @return void
  3206. */
  3207. public function testSelectMultipleCheckboxes() {
  3208. $result = $this->Form->select(
  3209. 'Model.multi_field',
  3210. array('first', 'second', 'third'),
  3211. array('multiple' => 'checkbox')
  3212. );
  3213. $expected = array(
  3214. 'input' => array(
  3215. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3216. ),
  3217. array('div' => array('class' => 'checkbox')),
  3218. array('input' => array(
  3219. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3220. 'value' => '0', 'id' => 'model-multi-field-0'
  3221. )),
  3222. array('label' => array('for' => 'model-multi-field-0')),
  3223. 'first',
  3224. '/label',
  3225. '/div',
  3226. array('div' => array('class' => 'checkbox')),
  3227. array('input' => array(
  3228. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3229. 'value' => '1', 'id' => 'model-multi-field-1'
  3230. )),
  3231. array('label' => array('for' => 'model-multi-field-1')),
  3232. 'second',
  3233. '/label',
  3234. '/div',
  3235. array('div' => array('class' => 'checkbox')),
  3236. array('input' => array(
  3237. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3238. 'value' => '2', 'id' => 'model-multi-field-2'
  3239. )),
  3240. array('label' => array('for' => 'model-multi-field-2')),
  3241. 'third',
  3242. '/label',
  3243. '/div'
  3244. );
  3245. $this->assertTags($result, $expected);
  3246. $result = $this->Form->select(
  3247. 'Model.multi_field',
  3248. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  3249. array('multiple' => 'checkbox')
  3250. );
  3251. $expected = array(
  3252. 'input' => array(
  3253. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3254. ),
  3255. array('div' => array('class' => 'checkbox')),
  3256. array('input' => array(
  3257. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3258. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  3259. )),
  3260. array('label' => array('for' => 'model-multi-field-a+')),
  3261. 'first',
  3262. '/label',
  3263. '/div',
  3264. array('div' => array('class' => 'checkbox')),
  3265. array('input' => array(
  3266. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3267. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  3268. )),
  3269. array('label' => array('for' => 'model-multi-field-a++')),
  3270. 'second',
  3271. '/label',
  3272. '/div',
  3273. array('div' => array('class' => 'checkbox')),
  3274. array('input' => array(
  3275. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3276. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  3277. )),
  3278. array('label' => array('for' => 'model-multi-field-a+++')),
  3279. 'third',
  3280. '/label',
  3281. '/div'
  3282. );
  3283. $this->assertTags($result, $expected);
  3284. $result = $this->Form->select(
  3285. 'Model.multi_field',
  3286. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  3287. array('multiple' => 'checkbox')
  3288. );
  3289. $expected = array(
  3290. 'input' => array(
  3291. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3292. ),
  3293. array('div' => array('class' => 'checkbox')),
  3294. array('input' => array(
  3295. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3296. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  3297. )),
  3298. array('label' => array('for' => 'model-multi-field-a-b')),
  3299. 'first',
  3300. '/label',
  3301. '/div',
  3302. array('div' => array('class' => 'checkbox')),
  3303. array('input' => array(
  3304. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3305. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  3306. )),
  3307. array('label' => array('for' => 'model-multi-field-a-b1')),
  3308. 'second',
  3309. '/label',
  3310. '/div',
  3311. array('div' => array('class' => 'checkbox')),
  3312. array('input' => array(
  3313. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3314. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  3315. )),
  3316. array('label' => array('for' => 'model-multi-field-a-b2')),
  3317. 'third',
  3318. '/label',
  3319. '/div'
  3320. );
  3321. $this->assertTags($result, $expected);
  3322. }
  3323. /**
  3324. * Ensure that multiCheckbox reads from the request data.
  3325. *
  3326. * @return void
  3327. */
  3328. public function testSelectMultipleCheckboxRequestData() {
  3329. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3330. $result = $this->Form->select(
  3331. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3332. );
  3333. $expected = array(
  3334. 'input' => array(
  3335. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  3336. ),
  3337. array('div' => array('class' => 'checkbox')),
  3338. array('input' => array(
  3339. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3340. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  3341. )),
  3342. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  3343. 'first',
  3344. '/label',
  3345. '/div',
  3346. array('div' => array('class' => 'checkbox')),
  3347. array('input' => array(
  3348. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3349. 'value' => 'Array', 'id' => 'model-tags-array'
  3350. )),
  3351. array('label' => array('for' => 'model-tags-array')),
  3352. 'Array',
  3353. '/label',
  3354. '/div'
  3355. );
  3356. $this->assertTags($result, $expected);
  3357. }
  3358. /**
  3359. * Checks the security hash array generated for multiple-input checkbox elements
  3360. *
  3361. * @return void
  3362. */
  3363. public function testSelectMultipleCheckboxSecurity() {
  3364. $this->Form->request->params['_Token'] = 'testKey';
  3365. $this->assertEquals(array(), $this->Form->fields);
  3366. $result = $this->Form->select(
  3367. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3368. array('multiple' => 'checkbox')
  3369. );
  3370. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3371. $result = $this->Form->secure($this->Form->fields);
  3372. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3373. $this->assertRegExp('/"' . $key . '"/', $result);
  3374. }
  3375. /**
  3376. * Multiple select elements should always be secured as they always participate
  3377. * in the POST data.
  3378. *
  3379. * @return void
  3380. */
  3381. public function testSelectMultipleSecureWithNoOptions() {
  3382. $this->assertEquals(array(), $this->Form->fields);
  3383. $this->Form->select(
  3384. 'Model.select',
  3385. array(),
  3386. array('multiple' => true)
  3387. );
  3388. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3389. }
  3390. /**
  3391. * When a select box has no options it should not be added to the fields list
  3392. * as it always fail post validation.
  3393. *
  3394. * @return void
  3395. */
  3396. public function testSelectNoSecureWithNoOptions() {
  3397. $this->Form->request->params['_Token'] = 'testkey';
  3398. $this->assertEquals([], $this->Form->fields);
  3399. $this->Form->select(
  3400. 'Model.select',
  3401. []
  3402. );
  3403. $this->assertEquals([], $this->Form->fields);
  3404. $this->Form->select(
  3405. 'Model.user_id',
  3406. [],
  3407. ['empty' => true]
  3408. );
  3409. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  3410. }
  3411. /**
  3412. * testInputMultipleCheckboxes method
  3413. *
  3414. * test input() resulting in multi select elements being generated.
  3415. *
  3416. * @return void
  3417. */
  3418. public function testInputMultipleCheckboxes() {
  3419. $result = $this->Form->input('Model.multi_field', array(
  3420. 'options' => array('first', 'second', 'third'),
  3421. 'multiple' => 'checkbox'
  3422. ));
  3423. $expected = array(
  3424. array('div' => array('class' => 'input select')),
  3425. array('label' => array('for' => 'model-multi-field')),
  3426. 'Multi Field',
  3427. '/label',
  3428. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3429. array('div' => array('class' => 'checkbox')),
  3430. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0')),
  3431. array('label' => array('for' => 'model-multi-field-0')),
  3432. 'first',
  3433. '/label',
  3434. '/div',
  3435. array('div' => array('class' => 'checkbox')),
  3436. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1')),
  3437. array('label' => array('for' => 'model-multi-field-1')),
  3438. 'second',
  3439. '/label',
  3440. '/div',
  3441. array('div' => array('class' => 'checkbox')),
  3442. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3443. array('label' => array('for' => 'model-multi-field-2')),
  3444. 'third',
  3445. '/label',
  3446. '/div',
  3447. '/div'
  3448. );
  3449. $this->assertTags($result, $expected);
  3450. $result = $this->Form->input('Model.multi_field', array(
  3451. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3452. 'multiple' => 'checkbox'
  3453. ));
  3454. $expected = array(
  3455. array('div' => array('class' => 'input select')),
  3456. array('label' => array('for' => 'model-multi-field')),
  3457. 'Multi Field',
  3458. '/label',
  3459. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3460. array('div' => array('class' => 'checkbox')),
  3461. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a')),
  3462. array('label' => array('for' => 'model-multi-field-a')),
  3463. 'first',
  3464. '/label',
  3465. '/div',
  3466. array('div' => array('class' => 'checkbox')),
  3467. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b')),
  3468. array('label' => array('for' => 'model-multi-field-b')),
  3469. 'second',
  3470. '/label',
  3471. '/div',
  3472. array('div' => array('class' => 'checkbox')),
  3473. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c')),
  3474. array('label' => array('for' => 'model-multi-field-c')),
  3475. 'third',
  3476. '/label',
  3477. '/div',
  3478. '/div'
  3479. );
  3480. $this->assertTags($result, $expected);
  3481. }
  3482. /**
  3483. * testSelectHiddenFieldOmission method
  3484. *
  3485. * test that select() with 'hiddenField' => false omits the hidden field
  3486. *
  3487. * @return void
  3488. */
  3489. public function testSelectHiddenFieldOmission() {
  3490. $result = $this->Form->select('Model.multi_field',
  3491. array('first', 'second'),
  3492. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  3493. );
  3494. $this->assertNotContains('type="hidden"', $result);
  3495. }
  3496. /**
  3497. * test that select() with multiple = checkbox works with overriding name attribute.
  3498. *
  3499. * @return void
  3500. */
  3501. public function testSelectCheckboxMultipleOverrideName() {
  3502. $result = $this->Form->select('category', ['1', '2'], [
  3503. 'multiple' => 'checkbox',
  3504. 'name' => 'fish',
  3505. ]);
  3506. $expected = array(
  3507. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  3508. array('div' => array('class' => 'checkbox')),
  3509. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  3510. array('label' => array('for' => 'fish-0')), '1', '/label',
  3511. '/div',
  3512. array('div' => array('class' => 'checkbox')),
  3513. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  3514. array('label' => array('for' => 'fish-1')), '2', '/label',
  3515. '/div'
  3516. );
  3517. $this->assertTags($result, $expected);
  3518. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  3519. 'name' => 'fish',
  3520. ]);
  3521. $this->assertTags($result, $expected);
  3522. }
  3523. /**
  3524. * testCheckbox method
  3525. *
  3526. * Test generation of checkboxes
  3527. *
  3528. * @return void
  3529. */
  3530. public function testCheckbox() {
  3531. $result = $this->Form->checkbox('Model.field');
  3532. $expected = array(
  3533. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  3534. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  3535. );
  3536. $this->assertTags($result, $expected);
  3537. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  3538. $expected = array(
  3539. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  3540. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  3541. );
  3542. $this->assertTags($result, $expected);
  3543. }
  3544. /**
  3545. * testCheckboxDefaultValue method
  3546. *
  3547. * Test default value setting on checkbox() method
  3548. *
  3549. * @return void
  3550. */
  3551. public function testCheckboxDefaultValue() {
  3552. $this->Form->request->data['Model']['field'] = false;
  3553. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3554. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  3555. unset($this->Form->request->data['Model']['field']);
  3556. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3557. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  3558. $this->Form->request->data['Model']['field'] = true;
  3559. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3560. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  3561. unset($this->Form->request->data['Model']['field']);
  3562. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3563. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  3564. }
  3565. /**
  3566. * Test checkbox being checked or having errors.
  3567. *
  3568. * @return void
  3569. */
  3570. public function testCheckboxCheckedAndError() {
  3571. $this->article['errors'] = [
  3572. 'published' => true
  3573. ];
  3574. $this->Form->request->data['published'] = 'myvalue';
  3575. $this->Form->create($this->article);
  3576. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  3577. $expected = array(
  3578. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  3579. array('input' => array(
  3580. 'type' => 'checkbox',
  3581. 'name' => 'published',
  3582. 'value' => 'myvalue',
  3583. 'id' => 'theID',
  3584. 'checked' => 'checked',
  3585. 'class' => 'form-error'
  3586. ))
  3587. );
  3588. $this->assertTags($result, $expected);
  3589. $this->Form->request->data['published'] = '';
  3590. $result = $this->Form->checkbox('published');
  3591. $expected = array(
  3592. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  3593. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  3594. );
  3595. $this->assertTags($result, $expected);
  3596. }
  3597. /**
  3598. * test checkbox() with a custom name attribute
  3599. *
  3600. * @return void
  3601. */
  3602. public function testCheckboxCustomNameAttribute() {
  3603. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  3604. $expected = array(
  3605. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  3606. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  3607. );
  3608. $this->assertTags($result, $expected);
  3609. }
  3610. /**
  3611. * Test that the hidden input for checkboxes can be omitted or set to a
  3612. * specific value.
  3613. *
  3614. * @return void
  3615. */
  3616. public function testCheckboxHiddenField() {
  3617. $result = $this->Form->checkbox('UserForm.something', array(
  3618. 'hiddenField' => false
  3619. ));
  3620. $expected = array(
  3621. 'input' => array(
  3622. 'type' => 'checkbox',
  3623. 'name' => 'UserForm[something]',
  3624. 'value' => '1'
  3625. ),
  3626. );
  3627. $this->assertTags($result, $expected);
  3628. $result = $this->Form->checkbox('UserForm.something', array(
  3629. 'value' => 'Y',
  3630. 'hiddenField' => 'N',
  3631. ));
  3632. $expected = array(
  3633. array('input' => array(
  3634. 'type' => 'hidden', 'name' => 'UserForm[something]',
  3635. 'value' => 'N'
  3636. )),
  3637. array('input' => array(
  3638. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  3639. 'value' => 'Y'
  3640. )),
  3641. );
  3642. $this->assertTags($result, $expected);
  3643. }
  3644. /**
  3645. * testDateTime method
  3646. *
  3647. * Test generation of date/time select elements
  3648. *
  3649. * @return void
  3650. */
  3651. public function testDateTime() {
  3652. extract($this->dateRegex);
  3653. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  3654. $now = strtotime('now');
  3655. $expected = array(
  3656. array('select' => array('name' => 'Contact[date][month]')),
  3657. $monthsRegex,
  3658. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  3659. date('F', $now),
  3660. '/option',
  3661. '*/select',
  3662. array('select' => array('name' => 'Contact[date][day]')),
  3663. $daysRegex,
  3664. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  3665. date('j', $now),
  3666. '/option',
  3667. '*/select',
  3668. array('select' => array('name' => 'Contact[date][year]')),
  3669. $yearsRegex,
  3670. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  3671. date('Y', $now),
  3672. '/option',
  3673. '*/select',
  3674. array('select' => array('name' => 'Contact[date][hour]')),
  3675. $hoursRegex,
  3676. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  3677. date('g', $now),
  3678. '/option',
  3679. '*/select',
  3680. array('select' => array('name' => 'Contact[date][minute]')),
  3681. $minutesRegex,
  3682. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  3683. date('i', $now),
  3684. '/option',
  3685. '*/select',
  3686. array('select' => array('name' => 'Contact[date][meridian]')),
  3687. $meridianRegex,
  3688. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  3689. date('a', $now),
  3690. '/option',
  3691. '*/select'
  3692. );
  3693. $this->assertTags($result, $expected);
  3694. }
  3695. /**
  3696. * Test empty defaulting to true for datetime.
  3697. *
  3698. * @return void
  3699. */
  3700. public function testDatetimeEmpty() {
  3701. extract($this->dateRegex);
  3702. $now = strtotime('now');
  3703. $result = $this->Form->dateTime('Contact.date', array(
  3704. 'empty' => true,
  3705. ));
  3706. $expected = array(
  3707. array('select' => array('name' => 'Contact[date][month]')),
  3708. $monthsRegex,
  3709. array('option' => array('value' => '')),
  3710. '/option',
  3711. '*/select',
  3712. array('select' => array('name' => 'Contact[date][day]')),
  3713. $daysRegex,
  3714. array('option' => array('value' => '')),
  3715. '/option',
  3716. '*/select',
  3717. array('select' => array('name' => 'Contact[date][year]')),
  3718. $yearsRegex,
  3719. array('option' => array('value' => '')),
  3720. '/option',
  3721. '*/select',
  3722. array('select' => array('name' => 'Contact[date][hour]')),
  3723. $hoursRegex,
  3724. array('option' => array('value' => '')),
  3725. '/option',
  3726. '*/select',
  3727. array('select' => array('name' => 'Contact[date][minute]')),
  3728. $minutesRegex,
  3729. array('option' => array('value' => '')),
  3730. '/option',
  3731. '*/select',
  3732. array('select' => array('name' => 'Contact[date][meridian]')),
  3733. $meridianRegex,
  3734. array('option' => array('value' => '')),
  3735. '/option',
  3736. '*/select'
  3737. );
  3738. $this->assertTags($result, $expected);
  3739. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  3740. }
  3741. /**
  3742. * Test datetime with interval option.
  3743. *
  3744. * @return void
  3745. */
  3746. public function testDatetimeMinuteInterval() {
  3747. extract($this->dateRegex);
  3748. $now = strtotime('now');
  3749. $result = $this->Form->dateTime('Contact.date', array(
  3750. 'interval' => 5,
  3751. 'value' => ''
  3752. ));
  3753. $expected = array(
  3754. array('select' => array('name' => 'Contact[date][month]')),
  3755. $monthsRegex,
  3756. array('option' => array('selected' => 'selected', 'value' => '')),
  3757. '/option',
  3758. '*/select',
  3759. array('select' => array('name' => 'Contact[date][day]')),
  3760. $daysRegex,
  3761. array('option' => array('selected' => 'selected', 'value' => '')),
  3762. '/option',
  3763. '*/select',
  3764. array('select' => array('name' => 'Contact[date][year]')),
  3765. $yearsRegex,
  3766. array('option' => array('selected' => 'selected', 'value' => '')),
  3767. '/option',
  3768. '*/select',
  3769. array('select' => array('name' => 'Contact[date][hour]')),
  3770. $hoursRegex,
  3771. array('option' => array('selected' => 'selected', 'value' => '')),
  3772. '/option',
  3773. '*/select',
  3774. array('select' => array('name' => 'Contact[date][minute]')),
  3775. $minutesRegex,
  3776. array('option' => array('selected' => 'selected', 'value' => '')),
  3777. '/option',
  3778. array('option' => array('value' => '00')),
  3779. '00',
  3780. '/option',
  3781. array('option' => array('value' => '05')),
  3782. '05',
  3783. '/option',
  3784. array('option' => array('value' => '10')),
  3785. '10',
  3786. '/option',
  3787. '*/select',
  3788. );
  3789. $this->assertTags($result, $expected);
  3790. }
  3791. /**
  3792. * Test dateTime with rounding
  3793. *
  3794. * @return void
  3795. */
  3796. public function testDateTimeRounding() {
  3797. $this->Form->request->data['Contact'] = array(
  3798. 'date' => array(
  3799. 'day' => '13',
  3800. 'month' => '12',
  3801. 'year' => '2010',
  3802. 'hour' => '04',
  3803. 'minute' => '19',
  3804. 'meridian' => 'AM'
  3805. )
  3806. );
  3807. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  3808. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  3809. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  3810. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  3811. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  3812. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  3813. }
  3814. /**
  3815. * test that datetime() and default values work.
  3816. *
  3817. * @return void
  3818. */
  3819. public function testDatetimeWithDefault() {
  3820. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  3821. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  3822. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  3823. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  3824. $result = $this->Form->dateTime('Contact.updated', array(
  3825. 'default' => '2009-06-01 11:15:30'
  3826. ));
  3827. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  3828. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  3829. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  3830. }
  3831. /**
  3832. * testDateTime all zeros
  3833. *
  3834. * @return void
  3835. */
  3836. public function testDateTimeAllZeros() {
  3837. $result = $this->Form->dateTime('Contact.date', array(
  3838. 'timeFormat' => false,
  3839. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  3840. 'value' => '0000-00-00'
  3841. ));
  3842. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  3843. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  3844. }
  3845. /**
  3846. * testDateTimeEmptyAsArray
  3847. *
  3848. * @return void
  3849. */
  3850. public function testDateTimeEmptyAsArray() {
  3851. $result = $this->Form->dateTime('Contact.date', array(
  3852. 'empty' => array(
  3853. 'day' => 'DAY',
  3854. 'month' => 'MONTH',
  3855. 'year' => 'YEAR',
  3856. 'hour' => 'HOUR',
  3857. 'minute' => 'MINUTE',
  3858. 'meridian' => false
  3859. )
  3860. ));
  3861. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  3862. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  3863. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  3864. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  3865. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  3866. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  3867. $result = $this->Form->dateTime('Contact.date', array(
  3868. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  3869. ));
  3870. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  3871. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  3872. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  3873. }
  3874. /**
  3875. * testFormDateTimeMulti method
  3876. *
  3877. * test multiple datetime element generation
  3878. *
  3879. * @return void
  3880. */
  3881. public function testFormDateTimeMulti() {
  3882. extract($this->dateRegex);
  3883. $result = $this->Form->dateTime('Contact.1.updated');
  3884. $this->assertContains('Contact[1][updated][month]', $result);
  3885. $this->assertContains('Contact[1][updated][day]', $result);
  3886. $this->assertContains('Contact[1][updated][year]', $result);
  3887. $this->assertContains('Contact[1][updated][hour]', $result);
  3888. $this->assertContains('Contact[1][updated][minute]', $result);
  3889. $this->assertContains('Contact[1][updated][meridian]', $result);
  3890. }
  3891. /**
  3892. * When changing the date format, the label should always focus the first select box when
  3893. * clicked.
  3894. *
  3895. * @return void
  3896. */
  3897. public function testDateTimeLabelIdMatchesFirstInput() {
  3898. $this->markTestIncomplete('Need to revisit once models work again.');
  3899. $result = $this->Form->input('Model.date', array('type' => 'date'));
  3900. $this->assertContains('label for="ModelDateMonth"', $result);
  3901. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  3902. $this->assertContains('label for="ModelDateDay"', $result);
  3903. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  3904. $this->assertContains('label for="ModelDateYear"', $result);
  3905. }
  3906. /**
  3907. * testMonth method
  3908. *
  3909. * @return void
  3910. */
  3911. public function testMonth() {
  3912. $result = $this->Form->month('Model.field', ['value' => '']);
  3913. $expected = array(
  3914. array('select' => array('name' => 'Model[field][month]')),
  3915. array('option' => array('value' => '', 'selected' => 'selected')),
  3916. '/option',
  3917. array('option' => array('value' => '01')),
  3918. date('F', strtotime('2008-01-01 00:00:00')),
  3919. '/option',
  3920. array('option' => array('value' => '02')),
  3921. date('F', strtotime('2008-02-01 00:00:00')),
  3922. '/option',
  3923. '*/select',
  3924. );
  3925. $this->assertTags($result, $expected);
  3926. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  3927. $expected = array(
  3928. array('select' => array('name' => 'Model[field][month]')),
  3929. array('option' => array('selected' => 'selected', 'value' => '')),
  3930. '/option',
  3931. array('option' => array('value' => '01')),
  3932. date('F', strtotime('2008-01-01 00:00:00')),
  3933. '/option',
  3934. array('option' => array('value' => '02')),
  3935. date('F', strtotime('2008-02-01 00:00:00')),
  3936. '/option',
  3937. '*/select',
  3938. );
  3939. $this->assertTags($result, $expected);
  3940. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  3941. $expected = array(
  3942. array('select' => array('name' => 'Model[field][month]')),
  3943. array('option' => array('selected' => 'selected', 'value' => '')),
  3944. '/option',
  3945. array('option' => array('value' => '01')),
  3946. '1',
  3947. '/option',
  3948. array('option' => array('value' => '02')),
  3949. '2',
  3950. '/option',
  3951. '*/select',
  3952. );
  3953. $this->assertTags($result, $expected);
  3954. $monthNames = [
  3955. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  3956. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  3957. ];
  3958. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  3959. $expected = array(
  3960. array('select' => array('name' => 'Model[field][month]')),
  3961. array('option' => array('value' => '')),
  3962. '/option',
  3963. array('option' => array('value' => '01', 'selected' => 'selected')),
  3964. 'Jan',
  3965. '/option',
  3966. array('option' => array('value' => '02')),
  3967. 'Feb',
  3968. '/option',
  3969. '*/select',
  3970. );
  3971. $this->assertTags($result, $expected);
  3972. $this->Form->request->data['Project']['release'] = '2050-02-10';
  3973. $result = $this->Form->month('Project.release');
  3974. $expected = array(
  3975. array('select' => array('name' => 'Project[release][month]')),
  3976. array('option' => array('value' => '')),
  3977. '/option',
  3978. array('option' => array('value' => '01')),
  3979. 'January',
  3980. '/option',
  3981. array('option' => array('value' => '02', 'selected' => 'selected')),
  3982. 'February',
  3983. '/option',
  3984. '*/select',
  3985. );
  3986. $this->assertTags($result, $expected);
  3987. }
  3988. /**
  3989. * testDay method
  3990. *
  3991. * @return void
  3992. */
  3993. public function testDay() {
  3994. extract($this->dateRegex);
  3995. $result = $this->Form->day('Model.field', array('value' => false));
  3996. $expected = array(
  3997. array('select' => array('name' => 'Model[field][day]')),
  3998. array('option' => array('selected' => 'selected', 'value' => '')),
  3999. '/option',
  4000. array('option' => array('value' => '01')),
  4001. '1',
  4002. '/option',
  4003. array('option' => array('value' => '02')),
  4004. '2',
  4005. '/option',
  4006. $daysRegex,
  4007. '/select',
  4008. );
  4009. $this->assertTags($result, $expected);
  4010. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4011. $result = $this->Form->day('Model.field');
  4012. $expected = array(
  4013. array('select' => array('name' => 'Model[field][day]')),
  4014. array('option' => array('value' => '')),
  4015. '/option',
  4016. array('option' => array('value' => '01')),
  4017. '1',
  4018. '/option',
  4019. array('option' => array('value' => '02')),
  4020. '2',
  4021. '/option',
  4022. $daysRegex,
  4023. array('option' => array('value' => '10', 'selected' => 'selected')),
  4024. '10',
  4025. '/option',
  4026. $daysRegex,
  4027. '/select',
  4028. );
  4029. $this->assertTags($result, $expected);
  4030. $this->Form->request->data['Model']['field'] = '';
  4031. $result = $this->Form->day('Model.field', array('value' => '10'));
  4032. $expected = array(
  4033. array('select' => array('name' => 'Model[field][day]')),
  4034. array('option' => array('value' => '')),
  4035. '/option',
  4036. array('option' => array('value' => '01')),
  4037. '1',
  4038. '/option',
  4039. array('option' => array('value' => '02')),
  4040. '2',
  4041. '/option',
  4042. $daysRegex,
  4043. array('option' => array('value' => '10', 'selected' => 'selected')),
  4044. '10',
  4045. '/option',
  4046. $daysRegex,
  4047. '/select',
  4048. );
  4049. $this->assertTags($result, $expected);
  4050. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4051. $result = $this->Form->day('Project.release');
  4052. $expected = array(
  4053. array('select' => array('name' => 'Project[release][day]')),
  4054. array('option' => array('value' => '')),
  4055. '/option',
  4056. array('option' => array('value' => '01')),
  4057. '1',
  4058. '/option',
  4059. array('option' => array('value' => '02')),
  4060. '2',
  4061. '/option',
  4062. $daysRegex,
  4063. array('option' => array('value' => '10', 'selected' => 'selected')),
  4064. '10',
  4065. '/option',
  4066. $daysRegex,
  4067. '/select',
  4068. );
  4069. $this->assertTags($result, $expected);
  4070. }
  4071. /**
  4072. * testMinute method
  4073. *
  4074. * @return void
  4075. */
  4076. public function testMinute() {
  4077. extract($this->dateRegex);
  4078. $result = $this->Form->minute('Model.field', ['value' => '']);
  4079. $expected = array(
  4080. array('select' => array('name' => 'Model[field][minute]')),
  4081. array('option' => array('selected' => 'selected', 'value' => '')),
  4082. '/option',
  4083. array('option' => array('value' => '00')),
  4084. '00',
  4085. '/option',
  4086. array('option' => array('value' => '01')),
  4087. '01',
  4088. '/option',
  4089. array('option' => array('value' => '02')),
  4090. '02',
  4091. '/option',
  4092. $minutesRegex,
  4093. '/select',
  4094. );
  4095. $this->assertTags($result, $expected);
  4096. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4097. $result = $this->Form->minute('Model.field');
  4098. $expected = array(
  4099. array('select' => array('name' => 'Model[field][minute]')),
  4100. array('option' => array('value' => '')),
  4101. '/option',
  4102. array('option' => array('value' => '00')),
  4103. '00',
  4104. '/option',
  4105. array('option' => array('value' => '01')),
  4106. '01',
  4107. '/option',
  4108. array('option' => array('value' => '02')),
  4109. '02',
  4110. '/option',
  4111. $minutesRegex,
  4112. array('option' => array('value' => '12', 'selected' => 'selected')),
  4113. '12',
  4114. '/option',
  4115. $minutesRegex,
  4116. '/select',
  4117. );
  4118. $this->assertTags($result, $expected);
  4119. $this->Form->request->data['Model']['field'] = '';
  4120. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4121. $expected = array(
  4122. array('select' => array('name' => 'Model[field][minute]')),
  4123. array('option' => array('selected' => 'selected', 'value' => '')),
  4124. '/option',
  4125. array('option' => array('value' => '00')),
  4126. '00',
  4127. '/option',
  4128. array('option' => array('value' => '05')),
  4129. '05',
  4130. '/option',
  4131. array('option' => array('value' => '10')),
  4132. '10',
  4133. '/option',
  4134. $minutesRegex,
  4135. '/select',
  4136. );
  4137. $this->assertTags($result, $expected);
  4138. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4139. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4140. $expected = array(
  4141. array('select' => array('name' => 'Model[field][minute]')),
  4142. array('option' => array('value' => '')),
  4143. '/option',
  4144. array('option' => array('value' => '00')),
  4145. '00',
  4146. '/option',
  4147. array('option' => array('value' => '05')),
  4148. '05',
  4149. '/option',
  4150. array('option' => array('value' => '10', 'selected' => 'selected')),
  4151. '10',
  4152. '/option',
  4153. $minutesRegex,
  4154. '/select',
  4155. );
  4156. $this->assertTags($result, $expected);
  4157. }
  4158. /**
  4159. * Test generating an input for the meridian.
  4160. *
  4161. * @return void
  4162. */
  4163. public function testMeridian() {
  4164. extract($this->dateRegex);
  4165. $now = time();
  4166. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  4167. $expected = [
  4168. array('select' => array('name' => 'Model[field][meridian]')),
  4169. array('option' => array('value' => '')),
  4170. '/option',
  4171. $meridianRegex,
  4172. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4173. date('a', $now),
  4174. '/option',
  4175. '*/select'
  4176. ];
  4177. $this->assertTags($result, $expected);
  4178. }
  4179. /**
  4180. * testHour method
  4181. *
  4182. * @return void
  4183. */
  4184. public function testHour() {
  4185. extract($this->dateRegex);
  4186. $result = $this->Form->hour('Model.field', ['value' => '']);
  4187. $expected = array(
  4188. array('select' => array('name' => 'Model[field][hour]')),
  4189. array('option' => array('selected' => 'selected', 'value' => '')),
  4190. '/option',
  4191. array('option' => array('value' => '01')),
  4192. '1',
  4193. '/option',
  4194. array('option' => array('value' => '02')),
  4195. '2',
  4196. '/option',
  4197. $hoursRegex,
  4198. '/select',
  4199. );
  4200. $this->assertTags($result, $expected);
  4201. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4202. $result = $this->Form->hour('Model.field', ['format' => 12]);
  4203. $expected = array(
  4204. array('select' => array('name' => 'Model[field][hour]')),
  4205. array('option' => array('value' => '')),
  4206. '/option',
  4207. array('option' => array('value' => '01')),
  4208. '1',
  4209. '/option',
  4210. array('option' => array('value' => '02')),
  4211. '2',
  4212. '/option',
  4213. $hoursRegex,
  4214. array('option' => array('value' => '12', 'selected' => 'selected')),
  4215. '12',
  4216. '/option',
  4217. '/select',
  4218. );
  4219. $this->assertTags($result, $expected);
  4220. $this->Form->request->data['Model']['field'] = '';
  4221. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  4222. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  4223. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  4224. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  4225. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4226. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4227. $expected = array(
  4228. array('select' => array('name' => 'Model[field][hour]')),
  4229. array('option' => array('value' => '')),
  4230. '/option',
  4231. array('option' => array('value' => '00', 'selected' => 'selected')),
  4232. '0',
  4233. '/option',
  4234. array('option' => array('value' => '01')),
  4235. '1',
  4236. '/option',
  4237. array('option' => array('value' => '02')),
  4238. '2',
  4239. '/option',
  4240. $hoursRegex,
  4241. '/select',
  4242. );
  4243. $this->assertTags($result, $expected);
  4244. unset($this->Form->request->data['Model']['field']);
  4245. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  4246. $thisHour = date('H');
  4247. $optValue = date('G');
  4248. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  4249. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  4250. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4251. $expected = array(
  4252. array('select' => array('name' => 'Model[field][hour]')),
  4253. array('option' => array('value' => '')),
  4254. '/option',
  4255. array('option' => array('value' => '00')),
  4256. '0',
  4257. '/option',
  4258. array('option' => array('value' => '01', 'selected' => 'selected')),
  4259. '1',
  4260. '/option',
  4261. array('option' => array('value' => '02')),
  4262. '2',
  4263. '/option',
  4264. $hoursRegex,
  4265. '/select',
  4266. );
  4267. $this->assertTags($result, $expected);
  4268. }
  4269. /**
  4270. * testYear method
  4271. *
  4272. * @return void
  4273. */
  4274. public function testYear() {
  4275. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  4276. $expected = array(
  4277. array('select' => array('name' => 'Model[field][year]')),
  4278. array('option' => array('selected' => 'selected', 'value' => '')),
  4279. '/option',
  4280. array('option' => array('value' => '2007')),
  4281. '2007',
  4282. '/option',
  4283. array('option' => array('value' => '2006')),
  4284. '2006',
  4285. '/option',
  4286. '/select',
  4287. );
  4288. $this->assertTags($result, $expected);
  4289. $result = $this->Form->year('Model.field', [
  4290. 'value' => '',
  4291. 'minYear' => 2006,
  4292. 'maxYear' => 2007,
  4293. 'orderYear' => 'asc'
  4294. ]);
  4295. $expected = array(
  4296. array('select' => array('name' => 'Model[field][year]')),
  4297. array('option' => array('selected' => 'selected', 'value' => '')),
  4298. '/option',
  4299. array('option' => array('value' => '2006')),
  4300. '2006',
  4301. '/option',
  4302. array('option' => array('value' => '2007')),
  4303. '2007',
  4304. '/option',
  4305. '/select',
  4306. );
  4307. $this->assertTags($result, $expected);
  4308. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  4309. $result = $this->Form->year('Contact.published', [
  4310. 'empty' => false,
  4311. 'minYear' => 2006,
  4312. 'maxYear' => 2007,
  4313. ]);
  4314. $expected = array(
  4315. array('select' => array('name' => 'Contact[published][year]')),
  4316. array('option' => array('value' => '2007')),
  4317. '2007',
  4318. '/option',
  4319. array('option' => array('value' => '2006', 'selected' => 'selected')),
  4320. '2006',
  4321. '/option',
  4322. '/select',
  4323. );
  4324. $this->assertTags($result, $expected);
  4325. }
  4326. /**
  4327. * testYearAutoExpandRange method
  4328. *
  4329. * @return void
  4330. */
  4331. public function testYearAutoExpandRange() {
  4332. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  4333. $result = $this->Form->year('User.birthday');
  4334. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4335. $result = $matches[1];
  4336. $expected = range(date('Y') + 5, 1930);
  4337. $this->assertEquals($expected, $result);
  4338. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4339. $result = $this->Form->year('Project.release');
  4340. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4341. $result = $matches[1];
  4342. $expected = range(2050, date('Y') - 5);
  4343. $this->assertEquals($expected, $result);
  4344. $this->Form->request->data['Project']['release'] = '1881-10-10';
  4345. $result = $this->Form->year('Project.release', [
  4346. 'minYear' => 1890,
  4347. 'maxYear' => 1900
  4348. ]);
  4349. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4350. $result = $matches[1];
  4351. $expected = range(1900, 1881);
  4352. $this->assertEquals($expected, $result);
  4353. }
  4354. /**
  4355. * Test that input() accepts the type of date and passes options in.
  4356. *
  4357. * @return void
  4358. */
  4359. public function testInputDate() {
  4360. $this->Form->request->data = array(
  4361. 'month_year' => array('month' => date('m')),
  4362. );
  4363. $this->Form->create($this->article);
  4364. $result = $this->Form->input('month_year', array(
  4365. 'label' => false,
  4366. 'type' => 'date',
  4367. 'minYear' => 2006,
  4368. 'maxYear' => 2008
  4369. ));
  4370. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  4371. $this->assertNotContains('value="2008" selected="selected"', $result);
  4372. }
  4373. /**
  4374. * testInputDateMaxYear method
  4375. *
  4376. * Let's say we want to only allow users born from 2006 to 2008 to register
  4377. * This being the first singup page, we still don't have any data
  4378. *
  4379. * @return void
  4380. */
  4381. public function testInputDateMaxYear() {
  4382. $this->Form->request->data = [];
  4383. $this->Form->create($this->article);
  4384. $result = $this->Form->input('birthday', array(
  4385. 'label' => false,
  4386. 'type' => 'date',
  4387. 'minYear' => 2006,
  4388. 'maxYear' => 2008
  4389. ));
  4390. $this->assertContains('value="2008" selected="selected"', $result);
  4391. }
  4392. /**
  4393. * testTextArea method
  4394. *
  4395. * @return void
  4396. */
  4397. public function testTextArea() {
  4398. $this->Form->request->data = array('field' => 'some test data');
  4399. $result = $this->Form->textarea('field');
  4400. $expected = array(
  4401. 'textarea' => array('name' => 'field'),
  4402. 'some test data',
  4403. '/textarea',
  4404. );
  4405. $this->assertTags($result, $expected);
  4406. $result = $this->Form->textarea('user.bio');
  4407. $expected = array(
  4408. 'textarea' => array('name' => 'user[bio]'),
  4409. '/textarea',
  4410. );
  4411. $this->assertTags($result, $expected);
  4412. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  4413. $result = $this->Form->textarea('field');
  4414. $expected = array(
  4415. 'textarea' => array('name' => 'field'),
  4416. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  4417. '/textarea',
  4418. );
  4419. $this->assertTags($result, $expected);
  4420. $this->Form->request->data = [
  4421. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  4422. ];
  4423. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  4424. $expected = array(
  4425. 'textarea' => array('name' => 'Model[field]'),
  4426. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  4427. '/textarea',
  4428. );
  4429. $this->assertTags($result, $expected);
  4430. $result = $this->Form->textarea('0.OtherModel.field');
  4431. $expected = array(
  4432. 'textarea' => array('name' => '0[OtherModel][field]'),
  4433. '/textarea'
  4434. );
  4435. $this->assertTags($result, $expected);
  4436. }
  4437. /**
  4438. * testTextAreaWithStupidCharacters method
  4439. *
  4440. * test text area with non-ascii characters
  4441. *
  4442. * @return void
  4443. */
  4444. public function testTextAreaWithStupidCharacters() {
  4445. $result = $this->Form->textarea('Post.content', [
  4446. 'value' => "GREAT®",
  4447. 'rows' => '15',
  4448. 'cols' => '75'
  4449. ]);
  4450. $expected = [
  4451. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  4452. 'GREAT®',
  4453. '/textarea',
  4454. ];
  4455. $this->assertTags($result, $expected);
  4456. }
  4457. /**
  4458. * testHiddenField method
  4459. *
  4460. * @return void
  4461. */
  4462. public function testHiddenField() {
  4463. $this->article['errors'] = [
  4464. 'field' => true
  4465. ];
  4466. $this->Form->request->data['field'] = 'test';
  4467. $this->Form->create($this->article);
  4468. $result = $this->Form->hidden('field', array('id' => 'theID'));
  4469. $this->assertTags($result, array(
  4470. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'))
  4471. );
  4472. $result = $this->Form->hidden('field', ['value' => 'my value']);
  4473. $expected = [
  4474. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  4475. ];
  4476. $this->assertTags($result, $expected);
  4477. }
  4478. /**
  4479. * testFileUploadField method
  4480. *
  4481. * @return void
  4482. */
  4483. public function testFileUploadField() {
  4484. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  4485. $result = $this->Form->file('Model.upload');
  4486. $this->assertTags($result, $expected);
  4487. $this->Form->request->data['Model']['upload'] = [
  4488. 'name' => '', 'type' => '', 'tmp_name' => '',
  4489. 'error' => 4, 'size' => 0
  4490. ];
  4491. $result = $this->Form->file('Model.upload');
  4492. $this->assertTags($result, $expected);
  4493. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  4494. $result = $this->Form->file('Model.upload');
  4495. $this->assertTags($result, $expected);
  4496. }
  4497. /**
  4498. * test File upload input on a model not used in create();
  4499. *
  4500. * @return void
  4501. */
  4502. public function testFileUploadOnOtherModel() {
  4503. $this->Form->create($this->article, array('type' => 'file'));
  4504. $result = $this->Form->file('ValidateProfile.city');
  4505. $expected = array(
  4506. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  4507. );
  4508. $this->assertTags($result, $expected);
  4509. }
  4510. /**
  4511. * testButton method
  4512. *
  4513. * @return void
  4514. */
  4515. public function testButton() {
  4516. $result = $this->Form->button('Hi');
  4517. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  4518. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  4519. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  4520. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  4521. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  4522. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  4523. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  4524. $result = $this->Form->button('No type', array('type' => false));
  4525. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  4526. $result = $this->Form->button('Upload Text', array(
  4527. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  4528. 'escape' => false
  4529. ));
  4530. $this->assertNotRegExp('/\&039/', $result);
  4531. }
  4532. /**
  4533. * Test that button() makes unlocked fields by default.
  4534. *
  4535. * @return void
  4536. */
  4537. public function testButtonUnlockedByDefault() {
  4538. $this->Form->request->params['_csrfToken'] = 'secured';
  4539. $this->Form->button('Save', array('name' => 'save'));
  4540. $this->Form->button('Clear');
  4541. $result = $this->Form->unlockField();
  4542. $this->assertEquals(array('save'), $result);
  4543. }
  4544. /**
  4545. * testPostButton method
  4546. *
  4547. * @return void
  4548. */
  4549. public function testPostButton() {
  4550. $result = $this->Form->postButton('Hi', '/controller/action');
  4551. $this->assertTags($result, array(
  4552. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  4553. 'div' => array('style' => 'display:none;'),
  4554. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4555. '/div',
  4556. 'button' => array('type' => 'submit'),
  4557. 'Hi',
  4558. '/button',
  4559. '/form'
  4560. ));
  4561. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  4562. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  4563. }
  4564. /**
  4565. * Test using postButton with N dimensional data.
  4566. *
  4567. * @return void
  4568. */
  4569. public function testPostButtonNestedData() {
  4570. $data = array(
  4571. 'one' => array(
  4572. 'two' => array(
  4573. 3, 4, 5
  4574. )
  4575. )
  4576. );
  4577. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  4578. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  4579. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  4580. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  4581. }
  4582. /**
  4583. * Test that postButton adds _Token fields.
  4584. *
  4585. * @return void
  4586. */
  4587. public function testSecurePostButton() {
  4588. $this->Form->request->params['_csrfToken'] = 'testkey';
  4589. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4590. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  4591. $expected = array(
  4592. 'form' => array(
  4593. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  4594. ),
  4595. array('div' => array('style' => 'display:none;')),
  4596. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  4597. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  4598. '/div',
  4599. 'button' => array('type' => 'submit'),
  4600. 'Delete',
  4601. '/button',
  4602. array('div' => array('style' => 'display:none;')),
  4603. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  4604. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  4605. '/div',
  4606. '/form',
  4607. );
  4608. $this->assertTags($result, $expected);
  4609. }
  4610. /**
  4611. * testPostLink method
  4612. *
  4613. * @return void
  4614. */
  4615. public function testPostLink() {
  4616. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  4617. $this->assertTags($result, array(
  4618. 'form' => array(
  4619. 'method' => 'post', 'action' => '/posts/delete/1',
  4620. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4621. ),
  4622. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4623. '/form',
  4624. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4625. 'Delete',
  4626. '/a'
  4627. ));
  4628. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  4629. $this->assertTags($result, array(
  4630. 'form' => array(
  4631. 'method' => 'post', 'action' => '/posts/delete/1',
  4632. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4633. ),
  4634. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  4635. '/form',
  4636. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4637. 'Delete',
  4638. '/a'
  4639. ));
  4640. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  4641. $this->assertTags($result, array(
  4642. 'form' => array(
  4643. 'method' => 'post', 'action' => '/posts/delete/1',
  4644. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4645. ),
  4646. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4647. '/form',
  4648. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  4649. 'Delete',
  4650. '/a'
  4651. ));
  4652. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  4653. $this->assertTags($result, array(
  4654. 'form' => array(
  4655. 'method' => 'post', 'action' => '/posts/delete/1',
  4656. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4657. ),
  4658. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4659. '/form',
  4660. '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;/'),
  4661. 'Delete',
  4662. '/a'
  4663. ));
  4664. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  4665. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  4666. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  4667. $this->assertTags($result, array(
  4668. 'form' => array(
  4669. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  4670. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4671. ),
  4672. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4673. '/form',
  4674. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4675. 'Delete',
  4676. '/a'
  4677. ));
  4678. $result = $this->Form->postLink(
  4679. '',
  4680. array('controller' => 'items', 'action' => 'delete', 10),
  4681. array('class' => 'btn btn-danger', 'escape' => false),
  4682. 'Confirm thing'
  4683. );
  4684. $this->assertTags($result, array(
  4685. 'form' => array(
  4686. 'method' => 'post', 'action' => '/items/delete/10',
  4687. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4688. ),
  4689. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4690. '/form',
  4691. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  4692. '/a'
  4693. ));
  4694. }
  4695. /**
  4696. * Test using postLink with N dimensional data.
  4697. *
  4698. * @return void
  4699. */
  4700. public function testPostLinkNestedData() {
  4701. $data = array(
  4702. 'one' => array(
  4703. 'two' => array(
  4704. 3, 4, 5
  4705. )
  4706. )
  4707. );
  4708. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  4709. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  4710. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  4711. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  4712. }
  4713. /**
  4714. * test creating postLinks after a GET form.
  4715. *
  4716. * @return void
  4717. */
  4718. public function testPostLinkAfterGetForm() {
  4719. $this->Form->request->params['_csrfToken'] = 'testkey';
  4720. $this->Form->request->params['_Token'] = 'val';
  4721. $this->Form->create($this->article, array('type' => 'get'));
  4722. $this->Form->end();
  4723. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  4724. $this->assertTags($result, array(
  4725. 'form' => array(
  4726. 'method' => 'post', 'action' => '/posts/delete/1',
  4727. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4728. ),
  4729. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  4730. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  4731. 'div' => array('style' => 'display:none;'),
  4732. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  4733. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  4734. '/div',
  4735. '/form',
  4736. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4737. 'Delete',
  4738. '/a'
  4739. ));
  4740. }
  4741. /**
  4742. * Test that postLink adds form tags to view block
  4743. *
  4744. * @return void
  4745. */
  4746. public function testPostLinkFormBuffer() {
  4747. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  4748. $this->assertTags($result, array(
  4749. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4750. 'Delete',
  4751. '/a'
  4752. ));
  4753. $result = $this->View->fetch('postLink');
  4754. $this->assertTags($result, array(
  4755. 'form' => array(
  4756. 'method' => 'post', 'action' => '/posts/delete/1',
  4757. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4758. ),
  4759. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4760. '/form'
  4761. ));
  4762. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  4763. array('inline' => false, 'method' => 'DELETE')
  4764. );
  4765. $this->assertTags($result, array(
  4766. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4767. 'Delete',
  4768. '/a'
  4769. ));
  4770. $result = $this->View->fetch('postLink');
  4771. $this->assertTags($result, array(
  4772. 'form' => array(
  4773. 'method' => 'post', 'action' => '/posts/delete/1',
  4774. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4775. ),
  4776. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4777. '/form',
  4778. array(
  4779. 'form' => array(
  4780. 'method' => 'post', 'action' => '/posts/delete/2',
  4781. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4782. ),
  4783. ),
  4784. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  4785. '/form'
  4786. ));
  4787. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  4788. $this->assertTags($result, array(
  4789. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  4790. 'Delete',
  4791. '/a'
  4792. ));
  4793. $result = $this->View->fetch('foobar');
  4794. $this->assertTags($result, array(
  4795. 'form' => array(
  4796. 'method' => 'post', 'action' => '/posts/delete/1',
  4797. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  4798. ),
  4799. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  4800. '/form'
  4801. ));
  4802. }
  4803. /**
  4804. * testSubmitButton method
  4805. *
  4806. * @return void
  4807. */
  4808. public function testSubmitButton() {
  4809. $result = $this->Form->submit('');
  4810. $expected = array(
  4811. 'div' => array('class' => 'submit'),
  4812. 'input' => array('type' => 'submit', 'value' => ''),
  4813. '/div'
  4814. );
  4815. $this->assertTags($result, $expected);
  4816. $result = $this->Form->submit('Test Submit');
  4817. $expected = array(
  4818. 'div' => array('class' => 'submit'),
  4819. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  4820. '/div'
  4821. );
  4822. $this->assertTags($result, $expected);
  4823. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  4824. $expected = array(
  4825. 'span' => array('class' => 'submit'),
  4826. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  4827. '/span'
  4828. );
  4829. $this->assertTags($result, $expected);
  4830. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  4831. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  4832. $this->assertTags($result, $expected);
  4833. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  4834. $expected = array(
  4835. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  4836. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  4837. '/div'
  4838. );
  4839. $this->assertTags($result, $expected);
  4840. $result = $this->Form->submit('Next >');
  4841. $expected = array(
  4842. 'div' => array('class' => 'submit'),
  4843. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  4844. '/div'
  4845. );
  4846. $this->assertTags($result, $expected);
  4847. $result = $this->Form->submit('Next >', array('escape' => false));
  4848. $expected = array(
  4849. 'div' => array('class' => 'submit'),
  4850. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  4851. '/div'
  4852. );
  4853. $this->assertTags($result, $expected);
  4854. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  4855. $expected = array(
  4856. 'div' => array('class' => 'submit'),
  4857. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  4858. '/div'
  4859. );
  4860. $this->assertTags($result, $expected);
  4861. $before = '--before--';
  4862. $after = '--after--';
  4863. $result = $this->Form->submit('Test', array('before' => $before));
  4864. $expected = array(
  4865. 'div' => array('class' => 'submit'),
  4866. '--before--',
  4867. 'input' => array('type' => 'submit', 'value' => 'Test'),
  4868. '/div'
  4869. );
  4870. $this->assertTags($result, $expected);
  4871. $result = $this->Form->submit('Test', array('after' => $after));
  4872. $expected = array(
  4873. 'div' => array('class' => 'submit'),
  4874. 'input' => array('type' => 'submit', 'value' => 'Test'),
  4875. '--after--',
  4876. '/div'
  4877. );
  4878. $this->assertTags($result, $expected);
  4879. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  4880. $expected = array(
  4881. 'div' => array('class' => 'submit'),
  4882. '--before--',
  4883. 'input' => array('type' => 'submit', 'value' => 'Test'),
  4884. '--after--',
  4885. '/div'
  4886. );
  4887. $this->assertTags($result, $expected);
  4888. }
  4889. /**
  4890. * test image submit types.
  4891. *
  4892. * @return void
  4893. */
  4894. public function testSubmitImage() {
  4895. $result = $this->Form->submit('http://example.com/cake.power.gif');
  4896. $expected = array(
  4897. 'div' => array('class' => 'submit'),
  4898. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  4899. '/div'
  4900. );
  4901. $this->assertTags($result, $expected);
  4902. $result = $this->Form->submit('/relative/cake.power.gif');
  4903. $expected = array(
  4904. 'div' => array('class' => 'submit'),
  4905. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  4906. '/div'
  4907. );
  4908. $this->assertTags($result, $expected);
  4909. $result = $this->Form->submit('cake.power.gif');
  4910. $expected = array(
  4911. 'div' => array('class' => 'submit'),
  4912. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  4913. '/div'
  4914. );
  4915. $this->assertTags($result, $expected);
  4916. $result = $this->Form->submit('Not.an.image');
  4917. $expected = array(
  4918. 'div' => array('class' => 'submit'),
  4919. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  4920. '/div'
  4921. );
  4922. $this->assertTags($result, $expected);
  4923. $after = '--after--';
  4924. $before = '--before--';
  4925. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  4926. $expected = array(
  4927. 'div' => array('class' => 'submit'),
  4928. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  4929. '--after--',
  4930. '/div'
  4931. );
  4932. $this->assertTags($result, $expected);
  4933. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  4934. $expected = array(
  4935. 'div' => array('class' => 'submit'),
  4936. '--before--',
  4937. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  4938. '/div'
  4939. );
  4940. $this->assertTags($result, $expected);
  4941. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  4942. $expected = array(
  4943. 'div' => array('class' => 'submit'),
  4944. '--before--',
  4945. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  4946. '--after--',
  4947. '/div'
  4948. );
  4949. $this->assertTags($result, $expected);
  4950. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  4951. $expected = array(
  4952. 'div' => array('class' => 'submit'),
  4953. '--before--',
  4954. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  4955. '--after--',
  4956. '/div'
  4957. );
  4958. $this->assertTags($result, $expected);
  4959. }
  4960. /**
  4961. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  4962. * be submitted at a time.
  4963. *
  4964. * @return void
  4965. */
  4966. public function testSubmitUnlockedByDefault() {
  4967. $this->Form->request->params['_Token'] = 'secured';
  4968. $this->Form->submit('Go go');
  4969. $this->Form->submit('Save', array('name' => 'save'));
  4970. $result = $this->Form->unlockField();
  4971. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  4972. }
  4973. /**
  4974. * Test submit image with timestamps.
  4975. *
  4976. * @return void
  4977. */
  4978. public function testSubmitImageTimestamp() {
  4979. Configure::write('Asset.timestamp', 'force');
  4980. $result = $this->Form->submit('cake.power.gif');
  4981. $expected = array(
  4982. 'div' => array('class' => 'submit'),
  4983. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  4984. '/div'
  4985. );
  4986. $this->assertTags($result, $expected);
  4987. }
  4988. /**
  4989. * test that datetime() works with GET style forms.
  4990. *
  4991. * @return void
  4992. */
  4993. public function testDateTimeWithGetForms() {
  4994. extract($this->dateRegex);
  4995. $this->Form->create($this->article, array('type' => 'get'));
  4996. $result = $this->Form->datetime('created');
  4997. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  4998. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  4999. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  5000. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  5001. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  5002. $this->assertContains('name="created[meridian]"', $result, 'meridian name attribute is wrong.');
  5003. }
  5004. /**
  5005. * testForMagicInputNonExistingNorValidated method
  5006. *
  5007. * @return void
  5008. */
  5009. public function testForMagicInputNonExistingNorValidated() {
  5010. $result = $this->Form->create($this->article);
  5011. $this->Form->templates(['groupContainer' => '{{content}}']);
  5012. $result = $this->Form->input('non_existing_nor_validated');
  5013. $expected = array(
  5014. 'label' => array('for' => 'non-existing-nor-validated'),
  5015. 'Non Existing Nor Validated',
  5016. '/label',
  5017. 'input' => array(
  5018. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5019. 'id' => 'non-existing-nor-validated'
  5020. )
  5021. );
  5022. $this->assertTags($result, $expected);
  5023. $result = $this->Form->input('non_existing_nor_validated', array(
  5024. 'val' => 'my value'
  5025. ));
  5026. $expected = array(
  5027. 'label' => array('for' => 'non-existing-nor-validated'),
  5028. 'Non Existing Nor Validated',
  5029. '/label',
  5030. 'input' => array(
  5031. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5032. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  5033. )
  5034. );
  5035. $this->assertTags($result, $expected);
  5036. $this->Form->request->data = array('non_existing_nor_validated' => 'CakePHP magic');
  5037. $result = $this->Form->input('non_existing_nor_validated');
  5038. $expected = array(
  5039. 'label' => array('for' => 'non-existing-nor-validated'),
  5040. 'Non Existing Nor Validated',
  5041. '/label',
  5042. 'input' => array(
  5043. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5044. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  5045. )
  5046. );
  5047. $this->assertTags($result, $expected);
  5048. }
  5049. /**
  5050. * testFormMagicInputLabel method
  5051. *
  5052. * @return void
  5053. */
  5054. public function testFormMagicInputLabel() {
  5055. TableRegistry::get('Contacts', [
  5056. 'className' => __NAMESPACE__ . '\ContactsTable'
  5057. ]);
  5058. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  5059. $this->Form->templates(['groupContainer' => '{{content}}']);
  5060. $result = $this->Form->input('Contacts.name', array('label' => 'My label'));
  5061. $expected = array(
  5062. 'label' => array('for' => 'contacts-name'),
  5063. 'My label',
  5064. '/label',
  5065. 'input' => array(
  5066. 'type' => 'text', 'name' => 'Contacts[name]',
  5067. 'id' => 'contacts-name', 'maxlength' => '255'
  5068. )
  5069. );
  5070. $this->assertTags($result, $expected);
  5071. $result = $this->Form->input('name', array(
  5072. 'label' => array('class' => 'mandatory')
  5073. ));
  5074. $expected = array(
  5075. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5076. 'Name',
  5077. '/label',
  5078. 'input' => array(
  5079. 'type' => 'text', 'name' => 'name',
  5080. 'id' => 'name', 'maxlength' => '255'
  5081. )
  5082. );
  5083. $this->assertTags($result, $expected);
  5084. $result = $this->Form->input('name', array(
  5085. 'div' => false,
  5086. 'label' => array('class' => 'mandatory', 'text' => 'My label')
  5087. ));
  5088. $expected = array(
  5089. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5090. 'My label',
  5091. '/label',
  5092. 'input' => array(
  5093. 'type' => 'text', 'name' => 'name',
  5094. 'id' => 'name', 'maxlength' => '255'
  5095. )
  5096. );
  5097. $this->assertTags($result, $expected);
  5098. $result = $this->Form->input('Contact.name', array(
  5099. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  5100. ));
  5101. $expected = array(
  5102. 'label' => array('for' => 'my_id'),
  5103. 'Name',
  5104. '/label',
  5105. 'input' => array(
  5106. 'type' => 'text', 'name' => 'Contact[name]',
  5107. 'id' => 'my_id', 'maxlength' => '255'
  5108. )
  5109. );
  5110. $this->assertTags($result, $expected);
  5111. $result = $this->Form->input('1.id');
  5112. $this->assertTags($result, array('input' => array(
  5113. 'type' => 'hidden', 'name' => '1[id]',
  5114. 'id' => '1-id'
  5115. )));
  5116. $result = $this->Form->input("1.name");
  5117. $expected = array(
  5118. 'label' => array('for' => '1-name'),
  5119. 'Name',
  5120. '/label',
  5121. 'input' => array(
  5122. 'type' => 'text', 'name' => '1[name]',
  5123. 'id' => '1-name', 'maxlength' => '255'
  5124. )
  5125. );
  5126. $this->assertTags($result, $expected);
  5127. }
  5128. /**
  5129. * testFormEnd method
  5130. *
  5131. * @return void
  5132. */
  5133. public function testFormEnd() {
  5134. $this->assertEquals('</form>', $this->Form->end());
  5135. }
  5136. /**
  5137. * Test the generation of fields for a multi record form.
  5138. *
  5139. * @return void
  5140. */
  5141. public function testMultiRecordForm() {
  5142. $this->loadFixtures('Article', 'Comment');
  5143. $articles = TableRegistry::get('Articles');
  5144. $articles->hasMany('Comments');
  5145. $comment = new Entity(['comment' => 'Value']);
  5146. $article = new Article(['comments' => [$comment]]);
  5147. $this->Form->create([$article]);
  5148. $result = $this->Form->input('0.comments.1.comment');
  5149. $expected = array(
  5150. 'div' => array('class' => 'input textarea'),
  5151. 'label' => array('for' => '0-comments-1-comment'),
  5152. 'Comment',
  5153. '/label',
  5154. 'textarea' => array(
  5155. 'name',
  5156. 'id' => '0-comments-1-comment'
  5157. ),
  5158. '/textarea',
  5159. '/div'
  5160. );
  5161. $this->assertTags($result, $expected);
  5162. $result = $this->Form->input('0.comments.0.comment');
  5163. $expected = array(
  5164. 'div' => array('class' => 'input textarea'),
  5165. 'label' => array('for' => '0-comments-0-comment'),
  5166. 'Comment',
  5167. '/label',
  5168. 'textarea' => array(
  5169. 'name',
  5170. 'id' => '0-comments-0-comment'
  5171. ),
  5172. 'Value',
  5173. '/textarea',
  5174. '/div'
  5175. );
  5176. $this->assertTags($result, $expected);
  5177. $comment->errors('comment', ['Not valid']);
  5178. $result = $this->Form->input('0.comments.0.comment');
  5179. $expected = array(
  5180. 'div' => array('class' => 'input textarea error'),
  5181. 'label' => array('for' => '0-comments-0-comment'),
  5182. 'Comment',
  5183. '/label',
  5184. 'textarea' => array(
  5185. 'name',
  5186. 'class' => 'form-error',
  5187. 'id' => '0-comments-0-comment'
  5188. ),
  5189. 'Value',
  5190. '/textarea',
  5191. array('div' => array('class' => 'error-message')),
  5192. 'Not valid',
  5193. '/div',
  5194. '/div'
  5195. );
  5196. $this->assertTags($result, $expected);
  5197. TableRegistry::get('Comments')
  5198. ->validator('default')
  5199. ->allowEmpty('comment', false);
  5200. $result = $this->Form->input('0.comments.1.comment');
  5201. $expected = array(
  5202. 'div' => array('class' => 'input textarea required'),
  5203. 'label' => array('for' => '0-comments-1-comment'),
  5204. 'Comment',
  5205. '/label',
  5206. 'textarea' => array(
  5207. 'name',
  5208. 'id' => '0-comments-1-comment'
  5209. ),
  5210. '/textarea',
  5211. '/div'
  5212. );
  5213. $this->assertTags($result, $expected);
  5214. }
  5215. /**
  5216. * test that some html5 inputs + FormHelper::__call() work
  5217. *
  5218. * @return void
  5219. */
  5220. public function testHtml5Inputs() {
  5221. $result = $this->Form->email('User.email');
  5222. $expected = array(
  5223. 'input' => array('type' => 'email', 'name' => 'User[email]')
  5224. );
  5225. $this->assertTags($result, $expected);
  5226. $result = $this->Form->search('User.query');
  5227. $expected = array(
  5228. 'input' => array('type' => 'search', 'name' => 'User[query]')
  5229. );
  5230. $this->assertTags($result, $expected);
  5231. $result = $this->Form->search('User.query', array('value' => 'test'));
  5232. $expected = array(
  5233. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  5234. );
  5235. $this->assertTags($result, $expected);
  5236. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  5237. $expected = array(
  5238. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  5239. );
  5240. $this->assertTags($result, $expected);
  5241. }
  5242. /**
  5243. * Test accessing html5 inputs through input().
  5244. *
  5245. * @return void
  5246. */
  5247. public function testHtml5InputWithInput() {
  5248. $this->Form->create();
  5249. $this->Form->templates(['groupContainer' => '{{content}}']);
  5250. $result = $this->Form->input('website', array(
  5251. 'type' => 'url',
  5252. 'val' => 'http://domain.tld',
  5253. 'label' => false
  5254. ));
  5255. $expected = array(
  5256. 'input' => array('type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld')
  5257. );
  5258. $this->assertTags($result, $expected);
  5259. }
  5260. /**
  5261. * Test errors when field name is missing.
  5262. *
  5263. * @expectedException \Cake\Error\Exception
  5264. * @return void
  5265. */
  5266. public function testHtml5InputException() {
  5267. $this->Form->email();
  5268. }
  5269. /**
  5270. * Tests that formhelper sets required attributes.
  5271. *
  5272. * @return void
  5273. */
  5274. public function testRequiredAttribute() {
  5275. $this->article['required'] = [
  5276. 'title' => true,
  5277. 'body' => false,
  5278. ];
  5279. $this->Form->create($this->article);
  5280. $result = $this->Form->input('title');
  5281. $expected = array(
  5282. 'div' => array('class' => 'input text required'),
  5283. 'label' => array('for' => 'title'),
  5284. 'Title',
  5285. '/label',
  5286. 'input' => array(
  5287. 'type' => 'text',
  5288. 'name' => 'title',
  5289. 'id' => 'title',
  5290. 'required' => 'required',
  5291. ),
  5292. '/div'
  5293. );
  5294. $this->assertTags($result, $expected);
  5295. $result = $this->Form->input('title', ['required' => false]);
  5296. $this->assertNotContains('required', $result);
  5297. $result = $this->Form->input('body');
  5298. $expected = array(
  5299. 'div' => array('class' => 'input text'),
  5300. 'label' => array('for' => 'body'),
  5301. 'Body',
  5302. '/label',
  5303. 'input' => array(
  5304. 'type' => 'text',
  5305. 'name' => 'body',
  5306. 'id' => 'body',
  5307. ),
  5308. '/div'
  5309. );
  5310. $this->assertTags($result, $expected);
  5311. $result = $this->Form->input('body', ['required' => true]);
  5312. $this->assertContains('required', $result);
  5313. }
  5314. }