FormHelperTest.php 168 KB

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