FormHelperTest.php 166 KB

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