FormHelperTest.php 245 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  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://cakephp.org CakePHP(tm) Project
  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\Collection\Collection;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Plugin;
  21. use Cake\Form\Form;
  22. use Cake\Network\Request;
  23. use Cake\ORM\Entity;
  24. use Cake\ORM\Table;
  25. use Cake\ORM\TableRegistry;
  26. use Cake\Routing\Router;
  27. use Cake\TestSuite\TestCase;
  28. use Cake\Utility\Security;
  29. use Cake\View\Helper\FormHelper;
  30. use Cake\View\Helper\HtmlHelper;
  31. use Cake\View\View;
  32. /**
  33. * Test stub.
  34. */
  35. class Article extends Entity
  36. {
  37. }
  38. /**
  39. * Contact class
  40. *
  41. */
  42. class ContactsTable extends Table
  43. {
  44. /**
  45. * Default schema
  46. *
  47. * @var array
  48. */
  49. protected $_schema = [
  50. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  51. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  52. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  53. 'phone' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  54. 'password' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  55. 'published' => ['type' => 'date', 'null' => true, 'default' => null, 'length' => null],
  56. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  57. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  58. 'age' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => null],
  59. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  60. ];
  61. /**
  62. * Initializes the schema
  63. *
  64. * @return void
  65. */
  66. public function initialize(array $config)
  67. {
  68. $this->schema($this->_schema);
  69. }
  70. }
  71. /**
  72. * ValidateUser class
  73. *
  74. */
  75. class ValidateUsersTable extends Table
  76. {
  77. /**
  78. * schema method
  79. *
  80. * @var array
  81. */
  82. protected $_schema = [
  83. 'id' => ['type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'],
  84. 'name' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  85. 'email' => ['type' => 'string', 'null' => '', 'default' => '', 'length' => '255'],
  86. 'balance' => ['type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2],
  87. 'cost_decimal' => ['type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3],
  88. 'null_decimal' => ['type' => 'decimal', 'null' => false, 'length' => null, 'precision' => null],
  89. 'ratio' => ['type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6],
  90. 'population' => ['type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0],
  91. 'created' => ['type' => 'date', 'null' => '1', 'default' => '', 'length' => ''],
  92. 'updated' => ['type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null],
  93. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  94. ];
  95. /**
  96. * Initializes the schema
  97. *
  98. * @return void
  99. */
  100. public function initialize(array $config)
  101. {
  102. $this->schema($this->_schema);
  103. }
  104. }
  105. /**
  106. * FormHelperTest class
  107. *
  108. * @property FormHelper $Form
  109. */
  110. class FormHelperTest extends TestCase
  111. {
  112. /**
  113. * Fixtures to be used
  114. *
  115. * @var array
  116. */
  117. public $fixtures = ['core.articles', 'core.comments'];
  118. /**
  119. * Do not load the fixtures by default
  120. *
  121. * @var bool
  122. */
  123. public $autoFixtures = false;
  124. /**
  125. * setUp method
  126. *
  127. * @return void
  128. */
  129. public function setUp()
  130. {
  131. parent::setUp();
  132. Configure::write('Config.language', 'eng');
  133. Configure::write('App.base', '');
  134. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  135. Configure::delete('Asset');
  136. $this->View = new View();
  137. $this->Form = new FormHelper($this->View);
  138. $request = new Request('articles/add');
  139. $request->here = '/articles/add';
  140. $request['controller'] = 'articles';
  141. $request['action'] = 'add';
  142. $request->webroot = '';
  143. $request->base = '';
  144. $this->Form->Url->request = $this->Form->request = $request;
  145. $this->dateRegex = [
  146. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  147. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  148. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  149. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  150. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  151. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  152. ];
  153. $this->article = [
  154. 'schema' => [
  155. 'id' => ['type' => 'integer'],
  156. 'author_id' => ['type' => 'integer', 'null' => true],
  157. 'title' => ['type' => 'string', 'null' => true],
  158. 'body' => 'text',
  159. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  160. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  161. ],
  162. 'required' => [
  163. 'author_id' => true,
  164. 'title' => true,
  165. ]
  166. ];
  167. Security::salt('foo!');
  168. Router::connect('/:controller', ['action' => 'index']);
  169. Router::connect('/:controller/:action/*');
  170. }
  171. /**
  172. * tearDown method
  173. *
  174. * @return void
  175. */
  176. public function tearDown()
  177. {
  178. parent::tearDown();
  179. unset($this->Form, $this->Controller, $this->View);
  180. TableRegistry::clear();
  181. }
  182. /**
  183. * Test construct() with the templates option.
  184. *
  185. * @return void
  186. */
  187. public function testConstructTemplatesFile()
  188. {
  189. $helper = new FormHelper($this->View, [
  190. 'templates' => 'htmlhelper_tags'
  191. ]);
  192. $result = $helper->input('name');
  193. $this->assertContains('<input', $result);
  194. }
  195. /**
  196. * Test that when specifying custom widgets the config array for that widget
  197. * is overwritten instead of merged.
  198. *
  199. * @return void
  200. */
  201. public function testConstructWithWidgets()
  202. {
  203. $config = [
  204. 'widgets' => [
  205. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  206. ]
  207. ];
  208. $helper = new FormHelper($this->View, $config);
  209. $registry = $helper->widgetRegistry();
  210. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  211. }
  212. /**
  213. * Test that when specifying custom widgets config file and it should be
  214. * added to widgets array. WidgetRegistry will load widgets in constructor.
  215. *
  216. * @return void
  217. */
  218. public function testConstructWithWidgetsConfig()
  219. {
  220. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  221. $registry = $helper->widgetRegistry();
  222. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  223. }
  224. /**
  225. * Test registering a new widget class and rendering it.
  226. *
  227. * @return void
  228. */
  229. public function testAddWidgetAndRenderWidget()
  230. {
  231. $data = [
  232. 'val' => 1
  233. ];
  234. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  235. $this->assertNull($this->Form->addWidget('test', $mock));
  236. $mock->expects($this->once())
  237. ->method('render')
  238. ->with($data)
  239. ->will($this->returnValue('HTML'));
  240. $result = $this->Form->widget('test', $data);
  241. $this->assertEquals('HTML', $result);
  242. }
  243. /**
  244. * Test that secureFields() of widget is called after calling render(),
  245. * not before.
  246. *
  247. * @return void
  248. */
  249. public function testOrderForRenderingWidgetAndFetchingSecureFields()
  250. {
  251. $data = [
  252. 'val' => 1,
  253. 'name' => 'test'
  254. ];
  255. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  256. $this->assertNull($this->Form->addWidget('test', $mock));
  257. $mock->expects($this->at(0))
  258. ->method('render')
  259. ->with($data)
  260. ->will($this->returnValue('HTML'));
  261. $mock->expects($this->at(1))
  262. ->method('secureFields')
  263. ->with($data)
  264. ->will($this->returnValue(['test']));
  265. $result = $this->Form->widget('test', $data + ['secure' => true]);
  266. $this->assertEquals('HTML', $result);
  267. }
  268. /**
  269. * Test that empty string is not added to secure fields list when
  270. * rendering input widget without name.
  271. *
  272. * @return void
  273. */
  274. public function testRenderingWidgetWithEmptyName()
  275. {
  276. $this->assertEquals([], $this->Form->fields);
  277. $result = $this->Form->widget('select', ['secure' => true, 'name' => '']);
  278. $this->assertEquals('<select name=""></select>', $result);
  279. $this->assertEquals([], $this->Form->fields);
  280. $result = $this->Form->widget('select', ['secure' => true, 'name' => '0']);
  281. $this->assertEquals('<select name="0"></select>', $result);
  282. $this->assertEquals(['0'], $this->Form->fields);
  283. }
  284. /**
  285. * Test registering an invalid widget class.
  286. *
  287. * @expectedException \RuntimeException
  288. * @return void
  289. */
  290. public function testAddWidgetInvalid()
  291. {
  292. $mock = new \StdClass();
  293. $this->Form->addWidget('test', $mock);
  294. $this->Form->widget('test');
  295. }
  296. /**
  297. * Test adding a new context class.
  298. *
  299. * @return void
  300. */
  301. public function testAddContextProvider()
  302. {
  303. $context = 'My data';
  304. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  305. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  306. $this->assertInstanceOf('Cake\Network\Request', $request);
  307. $this->assertEquals($context, $data['entity']);
  308. return $stub;
  309. });
  310. $this->Form->create($context);
  311. $result = $this->Form->context();
  312. $this->assertSame($stub, $result);
  313. }
  314. /**
  315. * Test replacing a context class.
  316. *
  317. * @return void
  318. */
  319. public function testAddContextProviderReplace()
  320. {
  321. $entity = new Article();
  322. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  323. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  324. return $stub;
  325. });
  326. $this->Form->create($entity);
  327. $result = $this->Form->context();
  328. $this->assertSame($stub, $result);
  329. }
  330. /**
  331. * Test overriding a context class.
  332. *
  333. * @return void
  334. */
  335. public function testAddContextProviderAdd()
  336. {
  337. $entity = new Article();
  338. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  339. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  340. if ($data['entity'] instanceof Entity) {
  341. return $stub;
  342. }
  343. });
  344. $this->Form->create($entity);
  345. $result = $this->Form->context();
  346. $this->assertSame($stub, $result);
  347. }
  348. /**
  349. * Test adding an invalid context class.
  350. *
  351. * @expectedException \RuntimeException
  352. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  353. * @return void
  354. */
  355. public function testAddContextProviderInvalid()
  356. {
  357. $context = 'My data';
  358. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  359. return new \StdClass();
  360. });
  361. $this->Form->create($context);
  362. }
  363. /**
  364. * Provides context options for create().
  365. *
  366. * @return array
  367. */
  368. public function contextSelectionProvider()
  369. {
  370. $entity = new Article();
  371. $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
  372. $emptyCollection = new Collection([]);
  373. $emptyArray = [];
  374. $arrayObject = new \ArrayObject([]);
  375. $data = [
  376. 'schema' => [
  377. 'title' => ['type' => 'string']
  378. ]
  379. ];
  380. $form = new Form();
  381. return [
  382. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  383. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  384. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  385. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  386. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  387. 'form' => [$form, 'Cake\View\Form\FormContext'],
  388. 'none' => [null, 'Cake\View\Form\NullContext'],
  389. 'false' => [false, 'Cake\View\Form\NullContext'],
  390. ];
  391. }
  392. /**
  393. * Test default context selection in create()
  394. *
  395. * @dataProvider contextSelectionProvider
  396. * @return void
  397. */
  398. public function testCreateContextSelectionBuiltIn($data, $class)
  399. {
  400. $this->loadFixtures('Articles');
  401. $this->Form->create($data);
  402. $this->assertInstanceOf($class, $this->Form->context());
  403. }
  404. /**
  405. * Data provider for type option.
  406. *
  407. * @return array
  408. */
  409. public static function requestTypeProvider()
  410. {
  411. return [
  412. // type, method, override
  413. ['post', 'post', 'POST'],
  414. ['put', 'post', 'PUT'],
  415. ['patch', 'post', 'PATCH'],
  416. ['delete', 'post', 'DELETE'],
  417. ];
  418. }
  419. /**
  420. * Test creating file forms.
  421. *
  422. * @return void
  423. */
  424. public function testCreateFile()
  425. {
  426. $encoding = strtolower(Configure::read('App.encoding'));
  427. $result = $this->Form->create(false, ['type' => 'file']);
  428. $expected = [
  429. 'form' => [
  430. 'method' => 'post', 'action' => '/articles/add',
  431. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  432. ],
  433. 'div' => ['style' => 'display:none;'],
  434. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  435. '/div'
  436. ];
  437. $this->assertHtml($expected, $result);
  438. }
  439. /**
  440. * Test creating GET forms.
  441. *
  442. * @return void
  443. */
  444. public function testCreateGet()
  445. {
  446. $encoding = strtolower(Configure::read('App.encoding'));
  447. $result = $this->Form->create(false, ['type' => 'get']);
  448. $expected = ['form' => [
  449. 'method' => 'get', 'action' => '/articles/add',
  450. 'accept-charset' => $encoding
  451. ]];
  452. $this->assertHtml($expected, $result);
  453. }
  454. /**
  455. * Test create() with the templates option.
  456. *
  457. * @return void
  458. */
  459. public function testCreateTemplatesArray()
  460. {
  461. $result = $this->Form->create($this->article, [
  462. 'templates' => [
  463. 'formStart' => '<form class="form-horizontal"{{attrs}}>',
  464. ]
  465. ]);
  466. $expected = [
  467. 'form' => [
  468. 'class' => 'form-horizontal',
  469. 'method' => 'post',
  470. 'action' => '/articles/add',
  471. 'accept-charset' => 'utf-8'
  472. ]
  473. ];
  474. $this->assertHtml($expected, $result);
  475. }
  476. /**
  477. * Test create() with the templates option.
  478. *
  479. * @return void
  480. */
  481. public function testCreateTemplatesFile()
  482. {
  483. $result = $this->Form->create($this->article, [
  484. 'templates' => 'htmlhelper_tags',
  485. ]);
  486. $expected = [
  487. 'start form',
  488. 'div' => ['class' => 'hidden'],
  489. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  490. '/div'
  491. ];
  492. $this->assertHtml($expected, $result);
  493. }
  494. /**
  495. * Test that create() and end() restore templates.
  496. *
  497. * @return void
  498. */
  499. public function testCreateEndRestoreTemplates()
  500. {
  501. $this->Form->create($this->article, [
  502. 'templates' => ['input' => 'custom input element']
  503. ]);
  504. $this->Form->end();
  505. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  506. }
  507. /**
  508. * Test using template vars in various templates used by input() method.
  509. *
  510. * @return void
  511. */
  512. public function testInputTemplateVars()
  513. {
  514. $result = $this->Form->input('text', [
  515. 'templates' => [
  516. 'input' => '<input custom="{{forinput}}" type="{{type}}" name="{{name}}"{{attrs}}/>',
  517. 'label' => '<label{{attrs}}>{{text}} {{forlabel}}</label>',
  518. 'formGroup' => '{{label}}{{forgroup}}{{input}}',
  519. 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}{{forcontainer}}</div>',
  520. ],
  521. 'templateVars' => [
  522. 'forinput' => 'in-input',
  523. 'forlabel' => 'in-label',
  524. 'forgroup' => 'in-group',
  525. 'forcontainer' => 'in-container'
  526. ]
  527. ]);
  528. $expected = [
  529. 'div' => ['class'],
  530. 'label' => ['for'],
  531. 'Text in-label',
  532. '/label',
  533. 'in-group',
  534. 'input' => ['name', 'type' => 'text', 'id', 'custom' => 'in-input'],
  535. 'in-container',
  536. '/div',
  537. ];
  538. $this->assertHtml($expected, $result);
  539. }
  540. /**
  541. * Test ensuring template variables work in template files loaded
  542. * during input().
  543. *
  544. * @return void
  545. */
  546. public function testInputTemplatesFromFile()
  547. {
  548. $result = $this->Form->input('title', [
  549. 'templates' => 'test_templates',
  550. 'templateVars' => [
  551. 'forcontainer' => 'container-data'
  552. ]
  553. ]);
  554. $expected = [
  555. 'div' => ['class'],
  556. 'label' => ['for'],
  557. 'Title',
  558. '/label',
  559. 'input' => ['name', 'type' => 'text', 'id'],
  560. 'container-data',
  561. '/div',
  562. ];
  563. $this->assertHtml($expected, $result);
  564. }
  565. /**
  566. * Test using template vars in inputSubmit and submitContainer template.
  567. *
  568. * @return void
  569. */
  570. public function testSubmitTemplateVars()
  571. {
  572. $this->Form->templates([
  573. 'inputSubmit' => '<input custom="{{forinput}}" type="{{type}}"{{attrs}}/>',
  574. 'submitContainer' => '<div class="submit">{{content}}{{forcontainer}}</div>'
  575. ]);
  576. $result = $this->Form->submit('Submit', [
  577. 'templateVars' => [
  578. 'forinput' => 'in-input',
  579. 'forcontainer' => 'in-container'
  580. ]
  581. ]);
  582. $expected = [
  583. 'div' => ['class'],
  584. 'input' => ['custom' => 'in-input', 'type' => 'submit', 'value' => 'Submit'],
  585. 'in-container',
  586. '/div',
  587. ];
  588. $this->assertHtml($expected, $result);
  589. }
  590. /**
  591. * test the create() method
  592. *
  593. * @dataProvider requestTypeProvider
  594. * @return void
  595. */
  596. public function testCreateTypeOptions($type, $method, $override)
  597. {
  598. $encoding = strtolower(Configure::read('App.encoding'));
  599. $result = $this->Form->create(false, ['type' => $type]);
  600. $expected = [
  601. 'form' => [
  602. 'method' => $method, 'action' => '/articles/add',
  603. 'accept-charset' => $encoding
  604. ],
  605. 'div' => ['style' => 'display:none;'],
  606. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => $override],
  607. '/div'
  608. ];
  609. $this->assertHtml($expected, $result);
  610. }
  611. /**
  612. * Test opening a form for an update operation.
  613. *
  614. * @return void
  615. */
  616. public function testCreateUpdateForm()
  617. {
  618. $encoding = strtolower(Configure::read('App.encoding'));
  619. $this->Form->request->here = '/articles/edit/1';
  620. $this->Form->request['action'] = 'edit';
  621. $this->article['defaults']['id'] = 1;
  622. $result = $this->Form->create($this->article);
  623. $expected = [
  624. 'form' => [
  625. 'method' => 'post', 'action' => '/articles/edit/1',
  626. 'accept-charset' => $encoding
  627. ],
  628. 'div' => ['style' => 'display:none;'],
  629. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  630. '/div'
  631. ];
  632. $this->assertHtml($expected, $result);
  633. }
  634. /**
  635. * test create() with automatic url generation
  636. *
  637. * @return void
  638. */
  639. public function testCreateAutoUrl()
  640. {
  641. $encoding = strtolower(Configure::read('App.encoding'));
  642. $this->Form->request['action'] = 'delete';
  643. $this->Form->request->here = '/articles/delete/10';
  644. $this->Form->request->base = '';
  645. $result = $this->Form->create($this->article);
  646. $expected = [
  647. 'form' => [
  648. 'method' => 'post', 'action' => '/articles/delete/10',
  649. 'accept-charset' => $encoding
  650. ],
  651. 'div' => ['style' => 'display:none;'],
  652. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  653. '/div'
  654. ];
  655. $this->assertHtml($expected, $result);
  656. $this->article['defaults'] = ['id' => 1];
  657. $this->Form->request->here = '/articles/edit/1';
  658. $this->Form->request['action'] = 'delete';
  659. $result = $this->Form->create($this->article, ['url' => ['action' => 'edit']]);
  660. $expected = [
  661. 'form' => [
  662. 'method' => 'post',
  663. 'action' => '/articles/edit/1',
  664. 'accept-charset' => $encoding
  665. ],
  666. 'div' => ['style' => 'display:none;'],
  667. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  668. '/div'
  669. ];
  670. $this->assertHtml($expected, $result);
  671. $this->Form->request['action'] = 'add';
  672. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  673. $expected = [
  674. 'form' => [
  675. 'method' => 'post',
  676. 'action' => '/articles/publish/1',
  677. 'accept-charset' => $encoding
  678. ],
  679. 'div' => ['style' => 'display:none;'],
  680. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  681. '/div'
  682. ];
  683. $this->assertHtml($expected, $result);
  684. $result = $this->Form->create($this->article, ['url' => '/articles/publish']);
  685. $expected = [
  686. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  687. 'div' => ['style' => 'display:none;'],
  688. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  689. '/div'
  690. ];
  691. $this->assertHtml($expected, $result);
  692. $this->Form->request['controller'] = 'Pages';
  693. $result = $this->Form->create($this->article, ['url' => ['action' => 'signup']]);
  694. $expected = [
  695. 'form' => [
  696. 'method' => 'post', 'action' => '/Pages/signup/1',
  697. 'accept-charset' => $encoding
  698. ],
  699. 'div' => ['style' => 'display:none;'],
  700. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'PUT'],
  701. '/div'
  702. ];
  703. $this->assertHtml($expected, $result);
  704. }
  705. /**
  706. * Test create() with no URL (no "action" attribute for <form> tag)
  707. *
  708. * @return void
  709. */
  710. public function testCreateNoUrl()
  711. {
  712. $result = $this->Form->create(false, ['url' => false]);
  713. $expected = [
  714. 'form' => [
  715. 'method' => 'post',
  716. 'accept-charset' => strtolower(Configure::read('App.encoding'))
  717. ],
  718. 'div' => ['style' => 'display:none;'],
  719. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  720. '/div'
  721. ];
  722. $this->assertHtml($expected, $result);
  723. }
  724. /**
  725. * test create() with a custom route
  726. *
  727. * @return void
  728. */
  729. public function testCreateCustomRoute()
  730. {
  731. Router::connect('/login', ['controller' => 'users', 'action' => 'login']);
  732. $encoding = strtolower(Configure::read('App.encoding'));
  733. $this->Form->request['controller'] = 'users';
  734. $result = $this->Form->create(false, ['url' => ['action' => 'login']]);
  735. $expected = [
  736. 'form' => [
  737. 'method' => 'post', 'action' => '/login',
  738. 'accept-charset' => $encoding
  739. ],
  740. 'div' => ['style' => 'display:none;'],
  741. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  742. '/div'
  743. ];
  744. $this->assertHtml($expected, $result);
  745. Router::connect(
  746. '/new-article',
  747. ['controller' => 'articles', 'action' => 'myaction'],
  748. ['_name' => 'my-route']
  749. );
  750. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  751. $expected = [
  752. 'form' => [
  753. 'method' => 'post', 'action' => '/new-article',
  754. 'accept-charset' => $encoding,
  755. ],
  756. 'div' => ['style' => 'display:none;'],
  757. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  758. '/div'
  759. ];
  760. $this->assertHtml($expected, $result);
  761. }
  762. /**
  763. * test automatic accept-charset overriding
  764. *
  765. * @return void
  766. */
  767. public function testCreateWithAcceptCharset()
  768. {
  769. $result = $this->Form->create(
  770. $this->article,
  771. [
  772. 'type' => 'post', 'url' => ['action' => 'index'], 'encoding' => 'iso-8859-1'
  773. ]
  774. );
  775. $expected = [
  776. 'form' => [
  777. 'method' => 'post', 'action' => '/articles',
  778. 'accept-charset' => 'iso-8859-1'
  779. ],
  780. 'div' => ['style' => 'display:none;'],
  781. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  782. '/div'
  783. ];
  784. $this->assertHtml($expected, $result);
  785. }
  786. /**
  787. * Test base form URL when url param is passed with multiple parameters (&)
  788. *
  789. */
  790. public function testCreateQuerystringrequest()
  791. {
  792. $encoding = strtolower(Configure::read('App.encoding'));
  793. $result = $this->Form->create($this->article, [
  794. 'type' => 'post',
  795. 'escape' => false,
  796. 'url' => [
  797. 'controller' => 'controller',
  798. 'action' => 'action',
  799. '?' => ['param1' => 'value1', 'param2' => 'value2']
  800. ]
  801. ]);
  802. $expected = [
  803. 'form' => [
  804. 'method' => 'post',
  805. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  806. 'accept-charset' => $encoding
  807. ],
  808. 'div' => ['style' => 'display:none;'],
  809. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  810. '/div'
  811. ];
  812. $this->assertHtml($expected, $result);
  813. $result = $this->Form->create($this->article, [
  814. 'type' => 'post',
  815. 'url' => [
  816. 'controller' => 'controller',
  817. 'action' => 'action',
  818. '?' => ['param1' => 'value1', 'param2' => 'value2']
  819. ]
  820. ]);
  821. $this->assertHtml($expected, $result);
  822. }
  823. /**
  824. * test that create() doesn't cause errors by multiple id's being in the primary key
  825. * as could happen with multiple select or checkboxes.
  826. *
  827. * @return void
  828. */
  829. public function testCreateWithMultipleIdInData()
  830. {
  831. $encoding = strtolower(Configure::read('App.encoding'));
  832. $this->Form->request->data['Article']['id'] = [1, 2];
  833. $result = $this->Form->create($this->article);
  834. $expected = [
  835. 'form' => [
  836. 'method' => 'post',
  837. 'action' => '/articles/add',
  838. 'accept-charset' => $encoding
  839. ],
  840. 'div' => ['style' => 'display:none;'],
  841. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  842. '/div'
  843. ];
  844. $this->assertHtml($expected, $result);
  845. }
  846. /**
  847. * test that create() doesn't add in extra passed params.
  848. *
  849. * @return void
  850. */
  851. public function testCreatePassedArgs()
  852. {
  853. $encoding = strtolower(Configure::read('App.encoding'));
  854. $this->Form->request->data['Article']['id'] = 1;
  855. $result = $this->Form->create($this->article, [
  856. 'type' => 'post',
  857. 'escape' => false,
  858. 'url' => [
  859. 'action' => 'edit',
  860. 'myparam'
  861. ]
  862. ]);
  863. $expected = [
  864. 'form' => [
  865. 'method' => 'post',
  866. 'action' => '/articles/edit/myparam',
  867. 'accept-charset' => $encoding
  868. ],
  869. 'div' => ['style' => 'display:none;'],
  870. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  871. '/div'
  872. ];
  873. $this->assertHtml($expected, $result);
  874. }
  875. /**
  876. * test creating a get form, and get form inputs.
  877. *
  878. * @return void
  879. */
  880. public function testGetFormCreate()
  881. {
  882. $encoding = strtolower(Configure::read('App.encoding'));
  883. $result = $this->Form->create($this->article, ['type' => 'get']);
  884. $expected = ['form' => [
  885. 'method' => 'get', 'action' => '/articles/add',
  886. 'accept-charset' => $encoding
  887. ]];
  888. $this->assertHtml($expected, $result);
  889. $result = $this->Form->text('title');
  890. $expected = ['input' => [
  891. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  892. ]];
  893. $this->assertHtml($expected, $result);
  894. $result = $this->Form->password('password');
  895. $expected = ['input' => [
  896. 'name' => 'password', 'type' => 'password'
  897. ]];
  898. $this->assertHtml($expected, $result);
  899. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*\/>$/', $result);
  900. $result = $this->Form->text('user_form');
  901. $expected = ['input' => [
  902. 'name' => 'user_form', 'type' => 'text'
  903. ]];
  904. $this->assertHtml($expected, $result);
  905. }
  906. /**
  907. * test get form, and inputs when the model param is false
  908. *
  909. * @return void
  910. */
  911. public function testGetFormWithFalseModel()
  912. {
  913. $encoding = strtolower(Configure::read('App.encoding'));
  914. $this->Form->request['controller'] = 'contact_test';
  915. $result = $this->Form->create(false, [
  916. 'type' => 'get', 'url' => ['controller' => 'contact_test']
  917. ]);
  918. $expected = ['form' => [
  919. 'method' => 'get', 'action' => '/contact_test/add',
  920. 'accept-charset' => $encoding
  921. ]];
  922. $this->assertHtml($expected, $result);
  923. $result = $this->Form->text('reason');
  924. $expected = [
  925. 'input' => ['type' => 'text', 'name' => 'reason']
  926. ];
  927. $this->assertHtml($expected, $result);
  928. }
  929. /**
  930. * testFormCreateWithSecurity method
  931. *
  932. * Test form->create() with security key.
  933. *
  934. * @return void
  935. */
  936. public function testCreateWithSecurity()
  937. {
  938. $this->Form->request->params['_csrfToken'] = 'testKey';
  939. $encoding = strtolower(Configure::read('App.encoding'));
  940. $result = $this->Form->create($this->article, [
  941. 'url' => '/articles/publish',
  942. ]);
  943. $expected = [
  944. 'form' => ['method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding],
  945. 'div' => ['style' => 'display:none;'],
  946. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  947. ['input' => [
  948. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  949. ]],
  950. '/div'
  951. ];
  952. $this->assertHtml($expected, $result);
  953. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  954. $expected['form']['id'] = 'MyForm';
  955. $this->assertHtml($expected, $result);
  956. }
  957. /**
  958. * testFormCreateGetNoSecurity method
  959. *
  960. * Test form->create() with no security key as its a get form
  961. *
  962. * @return void
  963. */
  964. public function testCreateEndGetNoSecurity()
  965. {
  966. $this->Form->request->params['_csrfToken'] = 'testKey';
  967. $encoding = strtolower(Configure::read('App.encoding'));
  968. $article = new Article();
  969. $result = $this->Form->create($article, [
  970. 'type' => 'get',
  971. 'url' => '/contacts/add'
  972. ]);
  973. $this->assertNotContains('testKey', $result);
  974. $result = $this->Form->end();
  975. $this->assertNotContains('testKey', $result);
  976. }
  977. /**
  978. * test that create() clears the fields property so it starts fresh
  979. *
  980. * @return void
  981. */
  982. public function testCreateClearingFields()
  983. {
  984. $this->Form->fields = ['model_id'];
  985. $this->Form->create($this->article);
  986. $this->assertEquals([], $this->Form->fields);
  987. }
  988. /**
  989. * Tests form hash generation with model-less data
  990. *
  991. * @return void
  992. */
  993. public function testValidateHashNoModel()
  994. {
  995. $this->Form->request->params['_Token'] = 'foo';
  996. $result = $this->Form->secure(['anything']);
  997. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  998. }
  999. /**
  1000. * Tests that hidden fields generated for checkboxes don't get locked
  1001. *
  1002. * @return void
  1003. */
  1004. public function testNoCheckboxLocking()
  1005. {
  1006. $this->Form->request->params['_Token'] = 'foo';
  1007. $this->assertSame([], $this->Form->fields);
  1008. $this->Form->checkbox('check', ['value' => '1']);
  1009. $this->assertSame($this->Form->fields, ['check']);
  1010. }
  1011. /**
  1012. * testFormSecurityFields method
  1013. *
  1014. * Test generation of secure form hash generation.
  1015. *
  1016. * @return void
  1017. */
  1018. public function testFormSecurityFields()
  1019. {
  1020. $fields = ['Model.password', 'Model.username', 'Model.valid' => '0'];
  1021. $this->Form->request->params['_Token'] = 'testKey';
  1022. $result = $this->Form->secure($fields);
  1023. $hash = Security::hash(serialize($fields) . Security::salt());
  1024. $hash .= ':' . 'Model.valid';
  1025. $hash = urlencode($hash);
  1026. $expected = [
  1027. 'div' => ['style' => 'display:none;'],
  1028. ['input' => [
  1029. 'type' => 'hidden',
  1030. 'name' => '_Token[fields]',
  1031. 'value' => $hash
  1032. ]],
  1033. ['input' => [
  1034. 'type' => 'hidden',
  1035. 'name' => '_Token[unlocked]',
  1036. 'value' => '',
  1037. ]],
  1038. '/div'
  1039. ];
  1040. $this->assertHtml($expected, $result);
  1041. }
  1042. /**
  1043. * Tests correct generation of number fields for double and float fields
  1044. *
  1045. * @return void
  1046. */
  1047. public function testTextFieldGenerationForFloats()
  1048. {
  1049. $this->article['schema'] = [
  1050. 'foo' => [
  1051. 'type' => 'float',
  1052. 'null' => false,
  1053. 'default' => null,
  1054. 'length' => 10
  1055. ]
  1056. ];
  1057. $this->Form->create($this->article);
  1058. $result = $this->Form->input('foo');
  1059. $expected = [
  1060. 'div' => ['class' => 'input number'],
  1061. 'label' => ['for' => 'foo'],
  1062. 'Foo',
  1063. '/label',
  1064. ['input' => [
  1065. 'type' => 'number',
  1066. 'name' => 'foo',
  1067. 'id' => 'foo',
  1068. 'step' => 'any'
  1069. ]],
  1070. '/div'
  1071. ];
  1072. $this->assertHtml($expected, $result);
  1073. $result = $this->Form->input('foo', ['step' => 0.5]);
  1074. $expected = [
  1075. 'div' => ['class' => 'input number'],
  1076. 'label' => ['for' => 'foo'],
  1077. 'Foo',
  1078. '/label',
  1079. ['input' => [
  1080. 'type' => 'number',
  1081. 'name' => 'foo',
  1082. 'id' => 'foo',
  1083. 'step' => '0.5'
  1084. ]],
  1085. '/div'
  1086. ];
  1087. $this->assertHtml($expected, $result);
  1088. }
  1089. /**
  1090. * Tests correct generation of number fields for integer fields
  1091. *
  1092. * @return void
  1093. */
  1094. public function testTextFieldTypeNumberGenerationForIntegers()
  1095. {
  1096. TableRegistry::get('Contacts', [
  1097. 'className' => __NAMESPACE__ . '\ContactsTable'
  1098. ]);
  1099. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1100. $result = $this->Form->input('age');
  1101. $expected = [
  1102. 'div' => ['class' => 'input number'],
  1103. 'label' => ['for' => 'age'],
  1104. 'Age',
  1105. '/label',
  1106. ['input' => [
  1107. 'type' => 'number', 'name' => 'age',
  1108. 'id' => 'age'
  1109. ]],
  1110. '/div'
  1111. ];
  1112. $this->assertHtml($expected, $result);
  1113. }
  1114. /**
  1115. * Tests correct generation of file upload fields for binary fields
  1116. *
  1117. * @return void
  1118. */
  1119. public function testFileUploadFieldTypeGenerationForBinaries()
  1120. {
  1121. $table = TableRegistry::get('Contacts', [
  1122. 'className' => __NAMESPACE__ . '\ContactsTable'
  1123. ]);
  1124. $table->schema(['foo' => [
  1125. 'type' => 'binary',
  1126. 'null' => false,
  1127. 'default' => null,
  1128. 'length' => 1024
  1129. ]]);
  1130. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1131. $result = $this->Form->input('foo');
  1132. $expected = [
  1133. 'div' => ['class' => 'input file'],
  1134. 'label' => ['for' => 'foo'],
  1135. 'Foo',
  1136. '/label',
  1137. ['input' => [
  1138. 'type' => 'file', 'name' => 'foo',
  1139. 'id' => 'foo'
  1140. ]],
  1141. '/div'
  1142. ];
  1143. $this->assertHtml($expected, $result);
  1144. }
  1145. /**
  1146. * testFormSecurityMultipleFields method
  1147. *
  1148. * Test secure() with multiple row form. Ensure hash is correct.
  1149. *
  1150. * @return void
  1151. */
  1152. public function testFormSecurityMultipleFields()
  1153. {
  1154. $this->Form->request->params['_Token'] = 'foo';
  1155. $fields = [
  1156. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1157. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1158. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1159. ];
  1160. $result = $this->Form->secure($fields);
  1161. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1162. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1163. $expected = [
  1164. 'div' => ['style' => 'display:none;'],
  1165. ['input' => [
  1166. 'type' => 'hidden', 'name' => '_Token[fields]',
  1167. 'value' => $hash
  1168. ]],
  1169. ['input' => [
  1170. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1171. 'value' => ''
  1172. ]],
  1173. '/div'
  1174. ];
  1175. $this->assertHtml($expected, $result);
  1176. }
  1177. /**
  1178. * testFormSecurityMultipleSubmitButtons
  1179. *
  1180. * test form submit generation and ensure that _Token is only created on end()
  1181. *
  1182. * @return void
  1183. */
  1184. public function testFormSecurityMultipleSubmitButtons()
  1185. {
  1186. $this->Form->request->params['_Token'] = 'testKey';
  1187. $this->Form->create($this->article);
  1188. $this->Form->text('Address.title');
  1189. $this->Form->text('Address.first_name');
  1190. $result = $this->Form->submit('Save', ['name' => 'save']);
  1191. $expected = [
  1192. 'div' => ['class' => 'submit'],
  1193. 'input' => ['type' => 'submit', 'name' => 'save', 'value' => 'Save'],
  1194. '/div',
  1195. ];
  1196. $this->assertHtml($expected, $result);
  1197. $result = $this->Form->submit('Cancel', ['name' => 'cancel']);
  1198. $expected = [
  1199. 'div' => ['class' => 'submit'],
  1200. 'input' => ['type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'],
  1201. '/div',
  1202. ];
  1203. $this->assertHtml($expected, $result);
  1204. $result = $this->Form->end();
  1205. $expected = [
  1206. 'div' => ['style' => 'display:none;'],
  1207. ['input' => [
  1208. 'type' => 'hidden',
  1209. 'name' => '_Token[fields]',
  1210. 'value'
  1211. ]],
  1212. ['input' => [
  1213. 'type' => 'hidden',
  1214. 'name' => '_Token[unlocked]',
  1215. 'value' => 'cancel%7Csave'
  1216. ]],
  1217. '/div'
  1218. ];
  1219. $this->assertHtml($expected, $result);
  1220. }
  1221. /**
  1222. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1223. *
  1224. * @return void
  1225. */
  1226. public function testSecurityButtonNestedNamed()
  1227. {
  1228. $key = 'testKey';
  1229. $this->Form->request->params['_csrfToken'] = $key;
  1230. $this->Form->create('Addresses');
  1231. $this->Form->button('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1232. $result = $this->Form->unlockField();
  1233. $this->assertEquals(['Address.button'], $result);
  1234. }
  1235. /**
  1236. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1237. *
  1238. * @return void
  1239. */
  1240. public function testSecuritySubmitNestedNamed()
  1241. {
  1242. $this->Form->request->params['_Token'] = 'testKey';
  1243. $this->Form->create($this->article);
  1244. $this->Form->submit('Test', ['type' => 'submit', 'name' => 'Address[button]']);
  1245. $result = $this->Form->unlockField();
  1246. $this->assertEquals(['Address.button'], $result);
  1247. }
  1248. /**
  1249. * Test that the correct fields are unlocked for image submits with no names.
  1250. *
  1251. * @return void
  1252. */
  1253. public function testSecuritySubmitImageNoName()
  1254. {
  1255. $key = 'testKey';
  1256. $this->Form->request->params['_Token'] = 'testKey';
  1257. $this->Form->create(false);
  1258. $result = $this->Form->submit('save.png');
  1259. $expected = [
  1260. 'div' => ['class' => 'submit'],
  1261. 'input' => ['type' => 'image', 'src' => 'img/save.png'],
  1262. '/div'
  1263. ];
  1264. $this->assertHtml($expected, $result);
  1265. $this->assertEquals(['x', 'y'], $this->Form->unlockField());
  1266. }
  1267. /**
  1268. * Test that the correct fields are unlocked for image submits with names.
  1269. *
  1270. * @return void
  1271. */
  1272. public function testSecuritySubmitImageName()
  1273. {
  1274. $this->Form->request->params['_Token'] = 'testKey';
  1275. $this->Form->create(null);
  1276. $result = $this->Form->submit('save.png', ['name' => 'test']);
  1277. $expected = [
  1278. 'div' => ['class' => 'submit'],
  1279. 'input' => ['type' => 'image', 'name' => 'test', 'src' => 'img/save.png'],
  1280. '/div'
  1281. ];
  1282. $this->assertHtml($expected, $result);
  1283. $this->assertEquals(['test', 'test_x', 'test_y'], $this->Form->unlockField());
  1284. }
  1285. /**
  1286. * testFormSecurityMultipleInputFields method
  1287. *
  1288. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1289. *
  1290. * @return void
  1291. */
  1292. public function testFormSecurityMultipleInputFields()
  1293. {
  1294. $this->Form->request->params['_Token'] = 'testKey';
  1295. $this->Form->create();
  1296. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1297. $this->Form->input('Addresses.0.title');
  1298. $this->Form->input('Addresses.0.first_name');
  1299. $this->Form->input('Addresses.0.last_name');
  1300. $this->Form->input('Addresses.0.address');
  1301. $this->Form->input('Addresses.0.city');
  1302. $this->Form->input('Addresses.0.phone');
  1303. $this->Form->input('Addresses.0.primary', ['type' => 'checkbox']);
  1304. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1305. $this->Form->input('Addresses.1.title');
  1306. $this->Form->input('Addresses.1.first_name');
  1307. $this->Form->input('Addresses.1.last_name');
  1308. $this->Form->input('Addresses.1.address');
  1309. $this->Form->input('Addresses.1.city');
  1310. $this->Form->input('Addresses.1.phone');
  1311. $this->Form->input('Addresses.1.primary', ['type' => 'checkbox']);
  1312. $result = $this->Form->secure($this->Form->fields);
  1313. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1314. $expected = [
  1315. 'div' => ['style' => 'display:none;'],
  1316. ['input' => [
  1317. 'type' => 'hidden', 'name' => '_Token[fields]',
  1318. 'value' => $hash
  1319. ]],
  1320. ['input' => [
  1321. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1322. 'value' => ''
  1323. ]],
  1324. '/div'
  1325. ];
  1326. $this->assertHtml($expected, $result);
  1327. }
  1328. /**
  1329. * Test form security with Model.field.0 style inputs
  1330. *
  1331. * @return void
  1332. */
  1333. public function testFormSecurityArrayFields()
  1334. {
  1335. $this->Form->request->params['_Token'] = 'testKey';
  1336. $this->Form->create();
  1337. $this->Form->text('Address.primary.1');
  1338. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1339. $this->Form->text('Address.secondary.1.0');
  1340. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1341. }
  1342. /**
  1343. * testFormSecurityMultipleInputDisabledFields method
  1344. *
  1345. * test secure form generation with multiple records and disabled fields.
  1346. *
  1347. * @return void
  1348. */
  1349. public function testFormSecurityMultipleInputDisabledFields()
  1350. {
  1351. $this->Form->request->params['_Token'] = [
  1352. 'unlockedFields' => ['first_name', 'address']
  1353. ];
  1354. $this->Form->create();
  1355. $this->Form->hidden('Addresses.0.id', ['value' => '123456']);
  1356. $this->Form->text('Addresses.0.title');
  1357. $this->Form->text('Addresses.0.first_name');
  1358. $this->Form->text('Addresses.0.last_name');
  1359. $this->Form->text('Addresses.0.address');
  1360. $this->Form->text('Addresses.0.city');
  1361. $this->Form->text('Addresses.0.phone');
  1362. $this->Form->hidden('Addresses.1.id', ['value' => '654321']);
  1363. $this->Form->text('Addresses.1.title');
  1364. $this->Form->text('Addresses.1.first_name');
  1365. $this->Form->text('Addresses.1.last_name');
  1366. $this->Form->text('Addresses.1.address');
  1367. $this->Form->text('Addresses.1.city');
  1368. $this->Form->text('Addresses.1.phone');
  1369. $result = $this->Form->secure($this->Form->fields);
  1370. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1371. $expected = [
  1372. 'div' => ['style' => 'display:none;'],
  1373. ['input' => [
  1374. 'type' => 'hidden',
  1375. 'name' => '_Token[fields]',
  1376. 'value' => $hash
  1377. ]],
  1378. ['input' => [
  1379. 'type' => 'hidden',
  1380. 'name' => '_Token[unlocked]',
  1381. 'value' => 'address%7Cfirst_name',
  1382. ]],
  1383. '/div'
  1384. ];
  1385. $this->assertHtml($expected, $result);
  1386. }
  1387. /**
  1388. * testFormSecurityInputDisabledFields method
  1389. *
  1390. * Test single record form with disabled fields.
  1391. *
  1392. * @return void
  1393. */
  1394. public function testFormSecurityInputUnlockedFields()
  1395. {
  1396. $this->Form->request['_Token'] = [
  1397. 'unlockedFields' => ['first_name', 'address']
  1398. ];
  1399. $this->Form->create();
  1400. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1401. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1402. $this->Form->text('Addresses.title');
  1403. $this->Form->text('Addresses.first_name');
  1404. $this->Form->text('Addresses.last_name');
  1405. $this->Form->text('Addresses.address');
  1406. $this->Form->text('Addresses.city');
  1407. $this->Form->text('Addresses.phone');
  1408. $result = $this->Form->fields;
  1409. $expected = [
  1410. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1411. 'Addresses.city', 'Addresses.phone'
  1412. ];
  1413. $this->assertEquals($expected, $result);
  1414. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1415. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1416. $expected = [
  1417. 'div' => ['style' => 'display:none;'],
  1418. ['input' => [
  1419. 'type' => 'hidden',
  1420. 'name' => '_Token[fields]',
  1421. 'value' => $hash,
  1422. 'data-foo' => 'bar',
  1423. ]],
  1424. ['input' => [
  1425. 'type' => 'hidden',
  1426. 'name' => '_Token[unlocked]',
  1427. 'value' => 'address%7Cfirst_name',
  1428. 'data-foo' => 'bar',
  1429. ]],
  1430. '/div'
  1431. ];
  1432. $this->assertHtml($expected, $result);
  1433. }
  1434. /**
  1435. * test securing inputs with custom name attributes.
  1436. *
  1437. * @return void
  1438. */
  1439. public function testFormSecureWithCustomNameAttribute()
  1440. {
  1441. $this->Form->request->params['_Token'] = 'testKey';
  1442. $this->Form->text('UserForm.published', ['name' => 'User[custom]']);
  1443. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1444. $this->Form->text('UserForm.published', ['name' => 'User[custom][another][value]']);
  1445. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1446. }
  1447. /**
  1448. * testFormSecuredInput method
  1449. *
  1450. * Test generation of entire secure form, assertions made on input() output.
  1451. *
  1452. * @return void
  1453. */
  1454. public function testFormSecuredInput()
  1455. {
  1456. $this->Form->request->params['_csrfToken'] = 'testKey';
  1457. $this->Form->request->params['_Token'] = 'stuff';
  1458. $this->article['schema'] = [
  1459. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1460. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1461. ];
  1462. $result = $this->Form->create($this->article, ['url' => '/articles/add']);
  1463. $encoding = strtolower(Configure::read('App.encoding'));
  1464. $expected = [
  1465. 'form' => ['method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding],
  1466. 'div' => ['style' => 'display:none;'],
  1467. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  1468. ['input' => [
  1469. 'type' => 'hidden',
  1470. 'name' => '_csrfToken',
  1471. 'value' => 'testKey'
  1472. ]],
  1473. '/div'
  1474. ];
  1475. $this->assertHtml($expected, $result);
  1476. $result = $this->Form->input('ratio');
  1477. $expected = [
  1478. 'div' => ['class'],
  1479. 'label' => ['for'],
  1480. 'Ratio',
  1481. '/label',
  1482. 'input' => ['name', 'type' => 'number', 'step' => '0.000001', 'id'],
  1483. '/div',
  1484. ];
  1485. $this->assertHtml($expected, $result);
  1486. $result = $this->Form->input('population');
  1487. $expected = [
  1488. 'div' => ['class'],
  1489. 'label' => ['for'],
  1490. 'Population',
  1491. '/label',
  1492. 'input' => ['name', 'type' => 'number', 'step' => '1', 'id'],
  1493. '/div',
  1494. ];
  1495. $this->assertHtml($expected, $result);
  1496. $result = $this->Form->input('published', ['type' => 'text']);
  1497. $expected = [
  1498. 'div' => ['class' => 'input text'],
  1499. 'label' => ['for' => 'published'],
  1500. 'Published',
  1501. '/label',
  1502. ['input' => [
  1503. 'type' => 'text',
  1504. 'name' => 'published',
  1505. 'id' => 'published'
  1506. ]],
  1507. '/div'
  1508. ];
  1509. $this->assertHtml($expected, $result);
  1510. $result = $this->Form->input('other', ['type' => 'text']);
  1511. $expected = [
  1512. 'div' => ['class' => 'input text'],
  1513. 'label' => ['for' => 'other'],
  1514. 'Other',
  1515. '/label',
  1516. ['input' => [
  1517. 'type' => 'text',
  1518. 'name' => 'other',
  1519. 'id',
  1520. ]],
  1521. '/div'
  1522. ];
  1523. $this->assertHtml($expected, $result);
  1524. $result = $this->Form->hidden('stuff');
  1525. $expected = [
  1526. 'input' => [
  1527. 'type' => 'hidden',
  1528. 'name' => 'stuff',
  1529. ]];
  1530. $this->assertHtml($expected, $result);
  1531. $result = $this->Form->hidden('hidden', ['value' => '0']);
  1532. $expected = ['input' => [
  1533. 'type' => 'hidden',
  1534. 'name' => 'hidden',
  1535. 'value' => '0'
  1536. ]];
  1537. $this->assertHtml($expected, $result);
  1538. $result = $this->Form->input('something', ['type' => 'checkbox']);
  1539. $expected = [
  1540. 'div' => ['class' => 'input checkbox'],
  1541. ['input' => [
  1542. 'type' => 'hidden',
  1543. 'name' => 'something',
  1544. 'value' => '0'
  1545. ]],
  1546. 'label' => ['for' => 'something'],
  1547. ['input' => [
  1548. 'type' => 'checkbox',
  1549. 'name' => 'something',
  1550. 'value' => '1',
  1551. 'id' => 'something'
  1552. ]],
  1553. 'Something',
  1554. '/label',
  1555. '/div'
  1556. ];
  1557. $this->assertHtml($expected, $result);
  1558. $result = $this->Form->fields;
  1559. $expected = [
  1560. 'ratio',
  1561. 'population',
  1562. 'published',
  1563. 'other',
  1564. 'stuff' => '',
  1565. 'hidden' => '0',
  1566. 'something'
  1567. ];
  1568. $this->assertEquals($expected, $result);
  1569. $result = $this->Form->secure($this->Form->fields);
  1570. $expected = [
  1571. 'div' => ['style' => 'display:none;'],
  1572. ['input' => [
  1573. 'type' => 'hidden',
  1574. 'name' => '_Token[fields]',
  1575. 'value'
  1576. ]],
  1577. ['input' => [
  1578. 'type' => 'hidden',
  1579. 'name' => '_Token[unlocked]',
  1580. 'value' => ''
  1581. ]],
  1582. '/div'
  1583. ];
  1584. $this->assertHtml($expected, $result);
  1585. }
  1586. /**
  1587. * Test secured inputs with custom names.
  1588. *
  1589. * @return void
  1590. */
  1591. public function testSecuredInputCustomName()
  1592. {
  1593. $this->Form->request->params['_Token'] = 'testKey';
  1594. $this->assertEquals([], $this->Form->fields);
  1595. $this->Form->text('text_input', [
  1596. 'name' => 'Option[General.default_role]',
  1597. ]);
  1598. $expected = ['Option.General.default_role'];
  1599. $this->assertEquals($expected, $this->Form->fields);
  1600. $this->Form->select('select_box', [1, 2], [
  1601. 'name' => 'Option[General.select_role]',
  1602. ]);
  1603. $expected[] = 'Option.General.select_role';
  1604. $this->assertEquals($expected, $this->Form->fields);
  1605. $this->Form->text('other.things[]');
  1606. $expected[] = 'other.things';
  1607. $this->assertEquals($expected, $this->Form->fields);
  1608. }
  1609. /**
  1610. * Test that a hidden field followed by a visible field
  1611. * undoes the hidden field locking.
  1612. *
  1613. * @return void
  1614. */
  1615. public function testSecuredInputDuplicate()
  1616. {
  1617. $this->Form->request->params['_Token'] = ['key' => 'testKey'];
  1618. $this->assertEquals([], $this->Form->fields);
  1619. $this->Form->input('text_val', [
  1620. 'type' => 'hidden',
  1621. 'value' => 'some text',
  1622. ]);
  1623. $expected = ['text_val' => 'some text'];
  1624. $this->assertEquals($expected, $this->Form->fields);
  1625. $this->Form->input('text_val', [
  1626. 'type' => 'text',
  1627. ]);
  1628. $expected = ['text_val'];
  1629. $this->assertEquals($expected, $this->Form->fields);
  1630. }
  1631. /**
  1632. * Tests that the correct keys are added to the field hash index
  1633. *
  1634. * @return void
  1635. */
  1636. public function testFormSecuredFileInput()
  1637. {
  1638. $this->assertEquals([], $this->Form->fields);
  1639. $this->Form->file('Attachment.file');
  1640. $expected = [
  1641. 'Attachment.file.name', 'Attachment.file.type',
  1642. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1643. 'Attachment.file.size'
  1644. ];
  1645. $this->assertEquals($expected, $this->Form->fields);
  1646. }
  1647. /**
  1648. * test that multiple selects keys are added to field hash
  1649. *
  1650. * @return void
  1651. */
  1652. public function testFormSecuredMultipleSelect()
  1653. {
  1654. $this->Form->request->params['_csrfToken'] = 'testKey';
  1655. $this->assertEquals([], $this->Form->fields);
  1656. $options = ['1' => 'one', '2' => 'two'];
  1657. $this->Form->select('Model.select', $options);
  1658. $expected = ['Model.select'];
  1659. $this->assertEquals($expected, $this->Form->fields);
  1660. $this->Form->fields = [];
  1661. $this->Form->select('Model.select', $options, ['multiple' => true]);
  1662. $this->assertEquals($expected, $this->Form->fields);
  1663. }
  1664. /**
  1665. * testFormSecuredRadio method
  1666. *
  1667. * @return void
  1668. */
  1669. public function testFormSecuredRadio()
  1670. {
  1671. $this->Form->request->params['_Token'] = 'testKey';
  1672. $this->assertEquals([], $this->Form->fields);
  1673. $options = ['1' => 'option1', '2' => 'option2'];
  1674. $this->Form->radio('Test.test', $options);
  1675. $expected = ['Test.test'];
  1676. $this->assertEquals($expected, $this->Form->fields);
  1677. $this->Form->radio('Test.all', $options, [
  1678. 'disabled' => ['option1', 'option2']
  1679. ]);
  1680. $expected = ['Test.test', 'Test.all' => ''];
  1681. $this->assertEquals($expected, $this->Form->fields);
  1682. $this->Form->radio('Test.some', $options, [
  1683. 'disabled' => ['option1']
  1684. ]);
  1685. $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
  1686. $this->assertEquals($expected, $this->Form->fields);
  1687. }
  1688. /**
  1689. * Test that when disabled is in a list based attribute array it works.
  1690. *
  1691. * @return void
  1692. */
  1693. public function testFormSecuredAndDisabledNotAssoc()
  1694. {
  1695. $this->Form->request->params['_Token'] = 'testKey';
  1696. $this->Form->select('Model.select', [1, 2], ['disabled']);
  1697. $this->Form->checkbox('Model.checkbox', ['disabled']);
  1698. $this->Form->text('Model.text', ['disabled']);
  1699. $this->Form->textarea('Model.textarea', ['disabled']);
  1700. $this->Form->password('Model.password', ['disabled']);
  1701. $this->Form->radio('Model.radio', [1, 2], ['disabled']);
  1702. $expected = [
  1703. 'Model.radio' => ''
  1704. ];
  1705. $this->assertEquals($expected, $this->Form->fields);
  1706. }
  1707. /**
  1708. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1709. * hashing.
  1710. *
  1711. * @return void
  1712. */
  1713. public function testFormSecuredAndDisabled()
  1714. {
  1715. $this->Form->request->params['_Token'] = 'testKey';
  1716. $this->Form->checkbox('Model.checkbox', ['disabled' => true]);
  1717. $this->Form->text('Model.text', ['disabled' => true]);
  1718. $this->Form->password('Model.text', ['disabled' => true]);
  1719. $this->Form->textarea('Model.textarea', ['disabled' => true]);
  1720. $this->Form->select('Model.select', [1, 2], ['disabled' => true]);
  1721. $this->Form->radio('Model.radio', [1, 2], ['disabled' => [1, 2]]);
  1722. $this->Form->year('Model.year', ['disabled' => true]);
  1723. $this->Form->month('Model.month', ['disabled' => true]);
  1724. $this->Form->day('Model.day', ['disabled' => true]);
  1725. $this->Form->hour('Model.hour', ['disabled' => true]);
  1726. $this->Form->minute('Model.minute', ['disabled' => true]);
  1727. $this->Form->meridian('Model.meridian', ['disabled' => true]);
  1728. $expected = [
  1729. 'Model.radio' => ''
  1730. ];
  1731. $this->assertEquals($expected, $this->Form->fields);
  1732. }
  1733. /**
  1734. * testDisableSecurityUsingForm method
  1735. *
  1736. * @return void
  1737. */
  1738. public function testDisableSecurityUsingForm()
  1739. {
  1740. $this->Form->request['_Token'] = [
  1741. 'disabledFields' => []
  1742. ];
  1743. $this->Form->create();
  1744. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1745. $this->Form->text('Addresses.title');
  1746. $this->Form->text('Addresses.first_name', ['secure' => false]);
  1747. $this->Form->textarea('Addresses.city', ['secure' => false]);
  1748. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  1749. $result = $this->Form->fields;
  1750. $expected = [
  1751. 'Addresses.id' => '123456', 'Addresses.title',
  1752. ];
  1753. $this->assertEquals($expected, $result);
  1754. }
  1755. /**
  1756. * test disableField
  1757. *
  1758. * @return void
  1759. */
  1760. public function testUnlockFieldAddsToList()
  1761. {
  1762. $this->Form->request['_Token'] = [
  1763. 'unlockedFields' => []
  1764. ];
  1765. $this->Form->unlockField('Contact.name');
  1766. $this->Form->text('Contact.name');
  1767. $this->assertEquals(['Contact.name'], $this->Form->unlockField());
  1768. $this->assertEquals([], $this->Form->fields);
  1769. }
  1770. /**
  1771. * test unlockField removing from fields array.
  1772. *
  1773. * @return void
  1774. */
  1775. public function testUnlockFieldRemovingFromFields()
  1776. {
  1777. $this->Form->request['_Token'] = [
  1778. 'unlockedFields' => []
  1779. ];
  1780. $this->Form->create($this->article);
  1781. $this->Form->hidden('Article.id', ['value' => 1]);
  1782. $this->Form->text('Article.title');
  1783. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  1784. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  1785. $this->Form->unlockField('Article.title');
  1786. $this->Form->unlockField('Article.id');
  1787. $this->assertEquals([], $this->Form->fields);
  1788. }
  1789. /**
  1790. * Test that only the path + query elements of a form's URL show up in their hash.
  1791. *
  1792. * @return void
  1793. */
  1794. public function testSecuredFormUrlIgnoresHost()
  1795. {
  1796. $this->Form->request['_Token'] = ['key' => 'testKey'];
  1797. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  1798. $this->Form->create($this->article, [
  1799. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  1800. ]);
  1801. $result = $this->Form->secure();
  1802. $this->assertContains($expected, $result);
  1803. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  1804. $result = $this->Form->secure();
  1805. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  1806. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  1807. $result = $this->Form->secure();
  1808. $this->assertContains($expected, $result, 'URL path + query should work.');
  1809. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  1810. $result = $this->Form->secure();
  1811. $this->assertNotContains($expected, $result, 'URL is different');
  1812. }
  1813. /**
  1814. * Test that URL, HTML and identifer show up in their hashs.
  1815. *
  1816. * @return void
  1817. */
  1818. public function testSecuredFormUrlHasHtmlAndIdentifer()
  1819. {
  1820. $this->Form->request['_Token'] = ['key' => 'testKey'];
  1821. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  1822. $res = $this->Form->create($this->article, [
  1823. 'url' => [
  1824. 'controller' => 'articles',
  1825. 'action' => 'view',
  1826. '?' => [
  1827. 'page' => 1,
  1828. 'limit' => 10,
  1829. 'html' => '<>"',
  1830. ],
  1831. '#' => 'result',
  1832. ],
  1833. ]);
  1834. $result = $this->Form->secure();
  1835. $this->assertContains($expected, $result);
  1836. $this->Form->create($this->article, [
  1837. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  1838. ]);
  1839. $result = $this->Form->secure();
  1840. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  1841. $this->Form->create($this->article, [
  1842. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  1843. ]);
  1844. $result = $this->Form->secure();
  1845. $this->assertContains($expected, $result, 'URL path + query should work.');
  1846. }
  1847. /**
  1848. * test error message display
  1849. *
  1850. * @return void
  1851. */
  1852. public function testErrorMessageDisplay()
  1853. {
  1854. $this->article['errors'] = [
  1855. 'Article' => ['title' => 'error message']
  1856. ];
  1857. $this->Form->create($this->article);
  1858. $result = $this->Form->input('Article.title');
  1859. $expected = [
  1860. 'div' => ['class' => 'input text error'],
  1861. 'label' => ['for' => 'article-title'],
  1862. 'Title',
  1863. '/label',
  1864. 'input' => [
  1865. 'type' => 'text', 'name' => 'Article[title]',
  1866. 'id' => 'article-title', 'class' => 'form-error'
  1867. ],
  1868. ['div' => ['class' => 'error-message']],
  1869. 'error message',
  1870. '/div',
  1871. '/div'
  1872. ];
  1873. $this->assertHtml($expected, $result);
  1874. $result = $this->Form->input('Article.title', [
  1875. 'templates' => [
  1876. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  1877. ]
  1878. ]);
  1879. $expected = [
  1880. 'div' => ['class' => 'input text error'],
  1881. 'label' => ['for' => 'article-title'],
  1882. 'Title',
  1883. '/label',
  1884. 'input' => [
  1885. 'type' => 'text', 'name' => 'Article[title]',
  1886. 'id' => 'article-title', 'class' => 'form-error'
  1887. ],
  1888. '/div'
  1889. ];
  1890. $this->assertHtml($expected, $result);
  1891. }
  1892. /**
  1893. * Test validation errors, when validation message is an empty string.
  1894. *
  1895. * @return void
  1896. */
  1897. public function testEmptyErrorValidation()
  1898. {
  1899. $this->article['errors'] = [
  1900. 'Article' => ['title' => '']
  1901. ];
  1902. $this->Form->create($this->article);
  1903. $result = $this->Form->input('Article.title');
  1904. $expected = [
  1905. 'div' => ['class' => 'input text error'],
  1906. 'label' => ['for' => 'article-title'],
  1907. 'Title',
  1908. '/label',
  1909. 'input' => [
  1910. 'type' => 'text', 'name' => 'Article[title]',
  1911. 'id' => 'article-title', 'class' => 'form-error'
  1912. ],
  1913. ['div' => ['class' => 'error-message']],
  1914. [],
  1915. '/div',
  1916. '/div'
  1917. ];
  1918. $this->assertHtml($expected, $result);
  1919. }
  1920. /**
  1921. * Test validation errors, when calling input() overriding validation message by an empty string.
  1922. *
  1923. * @return void
  1924. */
  1925. public function testEmptyInputErrorValidation()
  1926. {
  1927. $this->article['errors'] = [
  1928. 'Article' => ['title' => 'error message']
  1929. ];
  1930. $this->Form->create($this->article);
  1931. $result = $this->Form->input('Article.title', ['error' => '']);
  1932. $expected = [
  1933. 'div' => ['class' => 'input text error'],
  1934. 'label' => ['for' => 'article-title'],
  1935. 'Title',
  1936. '/label',
  1937. 'input' => [
  1938. 'type' => 'text', 'name' => 'Article[title]',
  1939. 'id' => 'article-title', 'class' => 'form-error'
  1940. ],
  1941. ['div' => ['class' => 'error-message']],
  1942. [],
  1943. '/div',
  1944. '/div'
  1945. ];
  1946. $this->assertHtml($expected, $result);
  1947. }
  1948. /**
  1949. * Test validation errors, when calling input() overriding validation messages
  1950. *
  1951. * @return void
  1952. */
  1953. public function testInputErrorMessage()
  1954. {
  1955. $this->article['errors'] = [
  1956. 'title' => ['error message']
  1957. ];
  1958. $this->Form->create($this->article);
  1959. $result = $this->Form->input('title', [
  1960. 'error' => 'Custom error!'
  1961. ]);
  1962. $expected = [
  1963. 'div' => ['class' => 'input text required error'],
  1964. 'label' => ['for' => 'title'],
  1965. 'Title',
  1966. '/label',
  1967. 'input' => [
  1968. 'type' => 'text', 'name' => 'title',
  1969. 'id' => 'title', 'class' => 'form-error',
  1970. 'required' => 'required',
  1971. ],
  1972. ['div' => ['class' => 'error-message']],
  1973. 'Custom error!',
  1974. '/div',
  1975. '/div'
  1976. ];
  1977. $this->assertHtml($expected, $result);
  1978. $result = $this->Form->input('title', [
  1979. 'error' => ['error message' => 'Custom error!']
  1980. ]);
  1981. $expected = [
  1982. 'div' => ['class' => 'input text required error'],
  1983. 'label' => ['for' => 'title'],
  1984. 'Title',
  1985. '/label',
  1986. 'input' => [
  1987. 'type' => 'text',
  1988. 'name' => 'title',
  1989. 'id' => 'title',
  1990. 'class' => 'form-error',
  1991. 'required' => 'required'
  1992. ],
  1993. ['div' => ['class' => 'error-message']],
  1994. 'Custom error!',
  1995. '/div',
  1996. '/div'
  1997. ];
  1998. $this->assertHtml($expected, $result);
  1999. }
  2000. /**
  2001. * Tests displaying errors for nested entities
  2002. *
  2003. * @return void
  2004. */
  2005. public function testFormValidationAssociated()
  2006. {
  2007. $nested = new Entity(['foo' => 'bar']);
  2008. $nested->errors('foo', ['not a valid bar']);
  2009. $entity = new Entity(['nested' => $nested]);
  2010. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2011. $result = $this->Form->error('nested.foo');
  2012. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2013. }
  2014. /**
  2015. * testFormValidationAssociatedSecondLevel method
  2016. *
  2017. * test form error display with associated model.
  2018. *
  2019. * @return void
  2020. */
  2021. public function testFormValidationAssociatedSecondLevel()
  2022. {
  2023. $inner = new Entity(['bar' => 'baz']);
  2024. $nested = new Entity(['foo' => $inner]);
  2025. $entity = new Entity(['nested' => $nested]);
  2026. $inner->errors('bar', ['not a valid one']);
  2027. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2028. $result = $this->Form->error('nested.foo.bar');
  2029. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2030. }
  2031. /**
  2032. * testFormValidationMultiRecord method
  2033. *
  2034. * test form error display with multiple records.
  2035. *
  2036. * @return void
  2037. */
  2038. public function testFormValidationMultiRecord()
  2039. {
  2040. $one = new Entity;
  2041. $two = new Entity;
  2042. TableRegistry::get('Contacts', [
  2043. 'className' => __NAMESPACE__ . '\ContactsTable'
  2044. ]);
  2045. $one->set('email', '');
  2046. $one->errors('email', ['invalid email']);
  2047. $two->set('name', '');
  2048. $two->errors('name', ['This is wrong']);
  2049. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2050. $result = $this->Form->input('0.email');
  2051. $expected = [
  2052. 'div' => ['class' => 'input email error'],
  2053. 'label' => ['for' => '0-email'],
  2054. 'Email',
  2055. '/label',
  2056. 'input' => [
  2057. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2058. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2059. ],
  2060. ['div' => ['class' => 'error-message']],
  2061. 'invalid email',
  2062. '/div',
  2063. '/div'
  2064. ];
  2065. $this->assertHtml($expected, $result);
  2066. $result = $this->Form->input('1.name');
  2067. $expected = [
  2068. 'div' => ['class' => 'input text error'],
  2069. 'label' => ['for' => '1-name'],
  2070. 'Name',
  2071. '/label',
  2072. 'input' => [
  2073. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2074. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2075. ],
  2076. ['div' => ['class' => 'error-message']],
  2077. 'This is wrong',
  2078. '/div',
  2079. '/div'
  2080. ];
  2081. $this->assertHtml($expected, $result);
  2082. }
  2083. /**
  2084. * testInput method
  2085. *
  2086. * Test various incarnations of input().
  2087. *
  2088. * @return void
  2089. */
  2090. public function testInput()
  2091. {
  2092. TableRegistry::get('ValidateUsers', [
  2093. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2094. ]);
  2095. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2096. $result = $this->Form->input('ValidateUsers.balance');
  2097. $expected = [
  2098. 'div' => ['class'],
  2099. 'label' => ['for'],
  2100. 'Balance',
  2101. '/label',
  2102. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2103. '/div',
  2104. ];
  2105. $this->assertHtml($expected, $result);
  2106. $result = $this->Form->input('ValidateUser.cost_decimal');
  2107. $expected = [
  2108. 'div' => ['class'],
  2109. 'label' => ['for'],
  2110. 'Cost Decimal',
  2111. '/label',
  2112. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2113. '/div',
  2114. ];
  2115. $this->assertHtml($expected, $result);
  2116. $result = $this->Form->input('ValidateUser.null_decimal');
  2117. $expected = [
  2118. 'div' => ['class'],
  2119. 'label' => ['for'],
  2120. 'Null Decimal',
  2121. '/label',
  2122. 'input' => ['name', 'type' => 'number', 'id'],
  2123. '/div',
  2124. ];
  2125. $this->assertHtml($expected, $result);
  2126. }
  2127. /**
  2128. * Tests the input method and passing custom options
  2129. *
  2130. * @return void
  2131. */
  2132. public function testInputCustomization()
  2133. {
  2134. TableRegistry::get('Contacts', [
  2135. 'className' => __NAMESPACE__ . '\ContactsTable'
  2136. ]);
  2137. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2138. $result = $this->Form->input('Contact.email', ['id' => 'custom']);
  2139. $expected = [
  2140. 'div' => ['class' => 'input email'],
  2141. 'label' => ['for' => 'custom'],
  2142. 'Email',
  2143. '/label',
  2144. ['input' => [
  2145. 'type' => 'email', 'name' => 'Contact[email]',
  2146. 'id' => 'custom', 'maxlength' => 255
  2147. ]],
  2148. '/div'
  2149. ];
  2150. $this->assertHtml($expected, $result);
  2151. $result = $this->Form->input('Contact.email', [
  2152. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2153. ]);
  2154. $expected = [
  2155. '<div',
  2156. 'label' => ['for' => 'contact-email'],
  2157. 'Email',
  2158. '/label',
  2159. ['input' => [
  2160. 'type' => 'email', 'name' => 'Contact[email]',
  2161. 'id' => 'contact-email', 'maxlength' => 255
  2162. ]],
  2163. '/div'
  2164. ];
  2165. $this->assertHtml($expected, $result);
  2166. $result = $this->Form->input('Contact.email', ['type' => 'text']);
  2167. $expected = [
  2168. 'div' => ['class' => 'input text'],
  2169. 'label' => ['for' => 'contact-email'],
  2170. 'Email',
  2171. '/label',
  2172. ['input' => [
  2173. 'type' => 'text', 'name' => 'Contact[email]',
  2174. 'id' => 'contact-email', 'maxlength' => '255'
  2175. ]],
  2176. '/div'
  2177. ];
  2178. $this->assertHtml($expected, $result);
  2179. $result = $this->Form->input('Contact.5.email', ['type' => 'text']);
  2180. $expected = [
  2181. 'div' => ['class' => 'input text'],
  2182. 'label' => ['for' => 'contact-5-email'],
  2183. 'Email',
  2184. '/label',
  2185. ['input' => [
  2186. 'type' => 'text', 'name' => 'Contact[5][email]',
  2187. 'id' => 'contact-5-email', 'maxlength' => '255'
  2188. ]],
  2189. '/div'
  2190. ];
  2191. $this->assertHtml($expected, $result);
  2192. $result = $this->Form->input('Contact.password');
  2193. $expected = [
  2194. 'div' => ['class' => 'input password'],
  2195. 'label' => ['for' => 'contact-password'],
  2196. 'Password',
  2197. '/label',
  2198. ['input' => [
  2199. 'type' => 'password', 'name' => 'Contact[password]',
  2200. 'id' => 'contact-password'
  2201. ]],
  2202. '/div'
  2203. ];
  2204. $this->assertHtml($expected, $result);
  2205. $result = $this->Form->input('Contact.email', [
  2206. 'type' => 'file', 'class' => 'textbox'
  2207. ]);
  2208. $expected = [
  2209. 'div' => ['class' => 'input file'],
  2210. 'label' => ['for' => 'contact-email'],
  2211. 'Email',
  2212. '/label',
  2213. ['input' => [
  2214. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2215. 'id' => 'contact-email'
  2216. ]],
  2217. '/div'
  2218. ];
  2219. $this->assertHtml($expected, $result);
  2220. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2221. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2222. $result = $this->Form->input('phone');
  2223. $expected = [
  2224. 'div' => ['class' => 'input tel'],
  2225. 'label' => ['for' => 'phone'],
  2226. 'Phone',
  2227. '/label',
  2228. ['input' => [
  2229. 'type' => 'tel', 'name' => 'phone',
  2230. 'value' => 'Hello &amp; World &gt; weird chars',
  2231. 'id' => 'phone', 'maxlength' => 255
  2232. ]],
  2233. '/div'
  2234. ];
  2235. $this->assertHtml($expected, $result);
  2236. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2237. $this->Form->create();
  2238. $result = $this->Form->input('Model.0.OtherModel.field', ['id' => 'myId']);
  2239. $expected = [
  2240. 'div' => ['class' => 'input text'],
  2241. 'label' => ['for' => 'myId'],
  2242. 'Field',
  2243. '/label',
  2244. 'input' => [
  2245. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2246. 'value' => 'My value', 'id' => 'myId'
  2247. ],
  2248. '/div'
  2249. ];
  2250. $this->assertHtml($expected, $result);
  2251. $this->Form->request->data = [];
  2252. $entity->errors('field', 'Badness!');
  2253. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2254. $result = $this->Form->input('field');
  2255. $expected = [
  2256. 'div' => ['class' => 'input text error'],
  2257. 'label' => ['for' => 'field'],
  2258. 'Field',
  2259. '/label',
  2260. 'input' => [
  2261. 'type' => 'text', 'name' => 'field',
  2262. 'id' => 'field', 'class' => 'form-error'
  2263. ],
  2264. ['div' => ['class' => 'error-message']],
  2265. 'Badness!',
  2266. '/div',
  2267. '/div'
  2268. ];
  2269. $this->assertHtml($expected, $result);
  2270. $result = $this->Form->input('field', [
  2271. 'templates' => [
  2272. 'inputContainerError' => '{{content}}{{error}}',
  2273. 'error' => '<span class="error-message">{{content}}</span>'
  2274. ]
  2275. ]);
  2276. $expected = [
  2277. 'label' => ['for' => 'field'],
  2278. 'Field',
  2279. '/label',
  2280. 'input' => [
  2281. 'type' => 'text', 'name' => 'field',
  2282. 'id' => 'field', 'class' => 'form-error'
  2283. ],
  2284. ['span' => ['class' => 'error-message']],
  2285. 'Badness!',
  2286. '/span'
  2287. ];
  2288. $this->assertHtml($expected, $result);
  2289. $entity->errors('field', ['minLength'], true);
  2290. $result = $this->Form->input('field', [
  2291. 'error' => [
  2292. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2293. 'maxLength' => 'login too large'
  2294. ]
  2295. ]);
  2296. $expected = [
  2297. 'div' => ['class' => 'input text error'],
  2298. 'label' => ['for' => 'field'],
  2299. 'Field',
  2300. '/label',
  2301. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2302. ['div' => ['class' => 'error-message']],
  2303. 'Le login doit contenir au moins 2 caractères',
  2304. '/div',
  2305. '/div'
  2306. ];
  2307. $this->assertHtml($expected, $result);
  2308. $entity->errors('field', ['maxLength'], true);
  2309. $result = $this->Form->input('field', [
  2310. 'error' => [
  2311. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2312. 'maxLength' => 'login too large',
  2313. ]
  2314. ]);
  2315. $expected = [
  2316. 'div' => ['class' => 'input text error'],
  2317. 'label' => ['for' => 'field'],
  2318. 'Field',
  2319. '/label',
  2320. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2321. ['div' => ['class' => 'error-message']],
  2322. 'login too large',
  2323. '/div',
  2324. '/div'
  2325. ];
  2326. $this->assertHtml($expected, $result);
  2327. }
  2328. /**
  2329. * Test that input() accepts a template file.
  2330. *
  2331. * @return void
  2332. */
  2333. public function testInputWithTemplateFile()
  2334. {
  2335. $result = $this->Form->input('field', [
  2336. 'templates' => 'htmlhelper_tags'
  2337. ]);
  2338. $expected = [
  2339. 'label' => ['for' => 'field'],
  2340. 'Field',
  2341. '/label',
  2342. 'input' => [
  2343. 'type' => 'text', 'name' => 'field',
  2344. 'id' => 'field'
  2345. ],
  2346. ];
  2347. $this->assertHtml($expected, $result);
  2348. }
  2349. /**
  2350. * Test that nested inputs end with brackets
  2351. *
  2352. * @return void
  2353. */
  2354. public function testNestedInputsEndWithBrackets()
  2355. {
  2356. $result = $this->Form->text('nested.text[]');
  2357. $expected = [
  2358. 'input' => [
  2359. 'type' => 'text', 'name' => 'nested[text][]'
  2360. ],
  2361. ];
  2362. $result = $this->Form->file('nested.file[]');
  2363. $expected = [
  2364. 'input' => [
  2365. 'type' => 'file', 'name' => 'nested[file][]'
  2366. ],
  2367. ];
  2368. $this->assertHtml($expected, $result);
  2369. }
  2370. /**
  2371. * Test id prefix
  2372. *
  2373. * @return void
  2374. */
  2375. public function testCreateIdPrefix()
  2376. {
  2377. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2378. $result = $this->Form->input('field');
  2379. $expected = [
  2380. 'div' => ['class' => 'input text'],
  2381. 'label' => ['for' => 'prefix-field'],
  2382. 'Field',
  2383. '/label',
  2384. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2385. '/div'
  2386. ];
  2387. $this->assertHtml($expected, $result);
  2388. $result = $this->Form->input('field', ['id' => 'custom-id']);
  2389. $expected = [
  2390. 'div' => ['class' => 'input text'],
  2391. 'label' => ['for' => 'custom-id'],
  2392. 'Field',
  2393. '/label',
  2394. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2395. '/div'
  2396. ];
  2397. $this->assertHtml($expected, $result);
  2398. $result = $this->Form->radio('Model.field', ['option A']);
  2399. $expected = [
  2400. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2401. 'label' => ['for' => 'prefix-model-field-0'],
  2402. ['input' => [
  2403. 'type' => 'radio',
  2404. 'name' => 'Model[field]',
  2405. 'value' => '0',
  2406. 'id' => 'prefix-model-field-0'
  2407. ]],
  2408. 'option A',
  2409. '/label'
  2410. ];
  2411. $this->assertHtml($expected, $result);
  2412. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2413. $expected = [
  2414. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2415. 'label' => ['for' => 'prefix-model-field-0'],
  2416. ['input' => [
  2417. 'type' => 'radio',
  2418. 'name' => 'Model[field]',
  2419. 'value' => '0',
  2420. 'id' => 'prefix-model-field-0'
  2421. ]],
  2422. 'option A',
  2423. '/label'
  2424. ];
  2425. $this->assertHtml($expected, $result);
  2426. $result = $this->Form->select(
  2427. 'Model.multi_field',
  2428. ['first'],
  2429. ['multiple' => 'checkbox']
  2430. );
  2431. $expected = [
  2432. 'input' => [
  2433. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2434. ],
  2435. ['div' => ['class' => 'checkbox']],
  2436. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2437. ['input' => [
  2438. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2439. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2440. ]],
  2441. 'first',
  2442. '/label',
  2443. '/div',
  2444. ];
  2445. $this->assertHtml($expected, $result);
  2446. $this->Form->end();
  2447. $result = $this->Form->input('field');
  2448. $expected = [
  2449. 'div' => ['class' => 'input text'],
  2450. 'label' => ['for' => 'field'],
  2451. 'Field',
  2452. '/label',
  2453. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2454. '/div'
  2455. ];
  2456. $this->assertHtml($expected, $result);
  2457. }
  2458. /**
  2459. * Test that inputs with 0 can be created.
  2460. *
  2461. * @return void
  2462. */
  2463. public function testInputZero()
  2464. {
  2465. TableRegistry::get('Contacts', [
  2466. 'className' => __NAMESPACE__ . '\ContactsTable'
  2467. ]);
  2468. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2469. $result = $this->Form->input('0');
  2470. $expected = [
  2471. 'div' => ['class' => 'input text'],
  2472. 'label' => ['for' => '0'], '/label',
  2473. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2474. '/div'
  2475. ];
  2476. $this->assertHtml($expected, $result);
  2477. }
  2478. /**
  2479. * test input() with checkbox creation
  2480. *
  2481. * @return void
  2482. */
  2483. public function testInputCheckbox()
  2484. {
  2485. $result = $this->Form->input('User.active', ['label' => false, 'checked' => true]);
  2486. $expected = [
  2487. 'div' => ['class' => 'input checkbox'],
  2488. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2489. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2490. '/div'
  2491. ];
  2492. $this->assertHtml($expected, $result);
  2493. $result = $this->Form->input('User.active', ['label' => false, 'checked' => 1]);
  2494. $expected = [
  2495. 'div' => ['class' => 'input checkbox'],
  2496. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2497. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2498. '/div'
  2499. ];
  2500. $this->assertHtml($expected, $result);
  2501. $result = $this->Form->input('User.active', ['label' => false, 'checked' => '1']);
  2502. $expected = [
  2503. 'div' => ['class' => 'input checkbox'],
  2504. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2505. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2506. '/div'
  2507. ];
  2508. $this->assertHtml($expected, $result);
  2509. $result = $this->Form->input('User.disabled', [
  2510. 'label' => 'Disabled',
  2511. 'type' => 'checkbox',
  2512. 'data-foo' => 'disabled'
  2513. ]);
  2514. $expected = [
  2515. 'div' => ['class' => 'input checkbox'],
  2516. 'input' => ['type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'],
  2517. 'label' => ['for' => 'user-disabled'],
  2518. ['input' => [
  2519. 'type' => 'checkbox',
  2520. 'name' => 'User[disabled]',
  2521. 'value' => '1',
  2522. 'id' => 'user-disabled',
  2523. 'data-foo' => 'disabled'
  2524. ]],
  2525. 'Disabled',
  2526. '/label',
  2527. '/div'
  2528. ];
  2529. $this->assertHtml($expected, $result);
  2530. }
  2531. /**
  2532. * Test that input() does not create wrapping div and label tag for hidden fields
  2533. *
  2534. * @return void
  2535. */
  2536. public function testInputHidden()
  2537. {
  2538. TableRegistry::get('ValidateUsers', [
  2539. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2540. ]);
  2541. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2542. $result = $this->Form->input('ValidateUser.id');
  2543. $expected = [
  2544. 'input' => ['name', 'type' => 'hidden', 'id']
  2545. ];
  2546. $this->assertHtml($expected, $result);
  2547. $result = $this->Form->input('ValidateUser.custom', ['type' => 'hidden']);
  2548. $expected = [
  2549. 'input' => ['name', 'type' => 'hidden', 'id']
  2550. ];
  2551. $this->assertHtml($expected, $result);
  2552. }
  2553. /**
  2554. * test form->input() with datetime
  2555. *
  2556. * @return void
  2557. */
  2558. public function testInputDatetime()
  2559. {
  2560. $this->Form = $this->getMock(
  2561. 'Cake\View\Helper\FormHelper',
  2562. ['datetime'],
  2563. [new View()]
  2564. );
  2565. $this->Form->expects($this->once())->method('datetime')
  2566. ->with('prueba', [
  2567. 'type' => 'datetime',
  2568. 'timeFormat' => 24,
  2569. 'minYear' => 2008,
  2570. 'maxYear' => 2011,
  2571. 'interval' => 15,
  2572. 'options' => null,
  2573. 'empty' => false,
  2574. 'id' => 'prueba',
  2575. 'required' => false,
  2576. 'templateVars' => []
  2577. ])
  2578. ->will($this->returnValue('This is it!'));
  2579. $result = $this->Form->input('prueba', [
  2580. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2581. 'maxYear' => 2011, 'interval' => 15
  2582. ]);
  2583. $expected = [
  2584. 'div' => ['class' => 'input datetime'],
  2585. '<label',
  2586. 'Prueba',
  2587. '/label',
  2588. 'This is it!',
  2589. '/div'
  2590. ];
  2591. $this->assertHtml($expected, $result);
  2592. }
  2593. /**
  2594. * test form->input() with datetime with id prefix
  2595. *
  2596. * @return void
  2597. */
  2598. public function testInputDatetimeIdPrefix()
  2599. {
  2600. $this->Form = $this->getMock(
  2601. 'Cake\View\Helper\FormHelper',
  2602. ['datetime'],
  2603. [new View()]
  2604. );
  2605. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2606. $this->Form->expects($this->once())->method('datetime')
  2607. ->with('prueba', [
  2608. 'type' => 'datetime',
  2609. 'timeFormat' => 24,
  2610. 'minYear' => 2008,
  2611. 'maxYear' => 2011,
  2612. 'interval' => 15,
  2613. 'options' => null,
  2614. 'empty' => false,
  2615. 'id' => 'prefix-prueba',
  2616. 'required' => false,
  2617. 'templateVars' => []
  2618. ])
  2619. ->will($this->returnValue('This is it!'));
  2620. $result = $this->Form->input('prueba', [
  2621. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2622. 'maxYear' => 2011, 'interval' => 15
  2623. ]);
  2624. $expected = [
  2625. 'div' => ['class' => 'input datetime'],
  2626. '<label',
  2627. 'Prueba',
  2628. '/label',
  2629. 'This is it!',
  2630. '/div'
  2631. ];
  2632. $this->assertHtml($expected, $result);
  2633. }
  2634. /**
  2635. * Test generating checkboxes with disabled elements.
  2636. *
  2637. * @return void
  2638. */
  2639. public function testInputCheckboxWithDisabledElements()
  2640. {
  2641. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  2642. $result = $this->Form->input('Contact.multiple', [
  2643. 'multiple' => 'checkbox',
  2644. 'disabled' => 'disabled',
  2645. 'options' => $options
  2646. ]);
  2647. $expected = [
  2648. ['div' => ['class' => 'input select']],
  2649. ['label' => ['for' => "contact-multiple"]],
  2650. 'Multiple',
  2651. '/label',
  2652. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  2653. ['div' => ['class' => 'checkbox']],
  2654. ['label' => ['for' => "contact-multiple-1"]],
  2655. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  2656. 'One',
  2657. '/label',
  2658. '/div',
  2659. ['div' => ['class' => 'checkbox']],
  2660. ['label' => ['for' => "contact-multiple-2"]],
  2661. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  2662. 'Two',
  2663. '/label',
  2664. '/div',
  2665. ['div' => ['class' => 'checkbox']],
  2666. ['label' => ['for' => "contact-multiple-3"]],
  2667. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  2668. 'Three',
  2669. '/label',
  2670. '/div',
  2671. '/div'
  2672. ];
  2673. $this->assertHtml($expected, $result);
  2674. // make sure 50 does only disable 50, and not 50f5c0cf
  2675. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  2676. $disabled = [50];
  2677. $expected = [
  2678. ['div' => ['class' => 'input select']],
  2679. ['label' => ['for' => "contact-multiple"]],
  2680. 'Multiple',
  2681. '/label',
  2682. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  2683. ['div' => ['class' => 'checkbox']],
  2684. ['label' => ['for' => "contact-multiple-50"]],
  2685. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  2686. 'Fifty',
  2687. '/label',
  2688. '/div',
  2689. ['div' => ['class' => 'checkbox']],
  2690. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  2691. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  2692. 'Stringy',
  2693. '/label',
  2694. '/div',
  2695. '/div'
  2696. ];
  2697. $result = $this->Form->input('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  2698. $this->assertHtml($expected, $result);
  2699. }
  2700. /**
  2701. * test input name with leading integer, ensure attributes are generated correctly.
  2702. *
  2703. * @return void
  2704. */
  2705. public function testInputWithLeadingInteger()
  2706. {
  2707. $result = $this->Form->text('0.Node.title');
  2708. $expected = [
  2709. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  2710. ];
  2711. $this->assertHtml($expected, $result);
  2712. }
  2713. /**
  2714. * test form->input() with select type inputs.
  2715. *
  2716. * @return void
  2717. */
  2718. public function testInputSelectType()
  2719. {
  2720. $result = $this->Form->input(
  2721. 'email',
  2722. [
  2723. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  2724. );
  2725. $expected = [
  2726. 'div' => ['class' => 'input select'],
  2727. 'label' => ['for' => 'email'],
  2728. 'Email',
  2729. '/label',
  2730. ['select' => ['name' => 'email', 'id' => 'email']],
  2731. ['option' => ['value' => '']],
  2732. '/option',
  2733. ['option' => ['value' => 'è']],
  2734. 'Firést',
  2735. '/option',
  2736. ['option' => ['value' => 'é']],
  2737. 'Secoènd',
  2738. '/option',
  2739. '/select',
  2740. '/div'
  2741. ];
  2742. $this->assertHtml($expected, $result);
  2743. $result = $this->Form->input(
  2744. 'email',
  2745. [
  2746. 'options' => ['First', 'Second'], 'empty' => true]
  2747. );
  2748. $expected = [
  2749. 'div' => ['class' => 'input select'],
  2750. 'label' => ['for' => 'email'],
  2751. 'Email',
  2752. '/label',
  2753. ['select' => ['name' => 'email', 'id' => 'email']],
  2754. ['option' => ['value' => '']],
  2755. '/option',
  2756. ['option' => ['value' => '0']],
  2757. 'First',
  2758. '/option',
  2759. ['option' => ['value' => '1']],
  2760. 'Second',
  2761. '/option',
  2762. '/select',
  2763. '/div'
  2764. ];
  2765. $this->assertHtml($expected, $result);
  2766. $result = $this->Form->input('email', [
  2767. 'type' => 'select',
  2768. 'options' => new \ArrayObject(['First', 'Second']),
  2769. 'empty' => true
  2770. ]);
  2771. $this->assertHtml($expected, $result);
  2772. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  2773. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  2774. $result = $this->Form->input('Model.user_id', ['empty' => true]);
  2775. $expected = [
  2776. 'div' => ['class' => 'input select'],
  2777. 'label' => ['for' => 'model-user-id'],
  2778. 'User',
  2779. '/label',
  2780. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  2781. ['option' => ['value' => '']],
  2782. '/option',
  2783. ['option' => ['value' => 'value', 'selected' => 'selected']],
  2784. 'good',
  2785. '/option',
  2786. ['option' => ['value' => 'other']],
  2787. 'bad',
  2788. '/option',
  2789. '/select',
  2790. '/div'
  2791. ];
  2792. $this->assertHtml($expected, $result);
  2793. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  2794. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  2795. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  2796. $expected = [
  2797. 'div' => ['class' => 'input select'],
  2798. 'label' => ['for' => 'thing-user-id'],
  2799. 'User',
  2800. '/label',
  2801. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  2802. ['option' => ['value' => '']],
  2803. 'Some Empty',
  2804. '/option',
  2805. ['option' => ['value' => 'value']],
  2806. 'good',
  2807. '/option',
  2808. ['option' => ['value' => 'other']],
  2809. 'bad',
  2810. '/option',
  2811. '/select',
  2812. '/div'
  2813. ];
  2814. $this->assertHtml($expected, $result);
  2815. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  2816. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  2817. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  2818. $expected = [
  2819. 'div' => ['class' => 'input select'],
  2820. 'label' => ['for' => 'thing-user-id'],
  2821. 'User',
  2822. '/label',
  2823. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  2824. ['option' => ['value' => '']],
  2825. 'Some Empty',
  2826. '/option',
  2827. ['option' => ['value' => 'value', 'selected' => 'selected']],
  2828. 'good',
  2829. '/option',
  2830. ['option' => ['value' => 'other']],
  2831. 'bad',
  2832. '/option',
  2833. '/select',
  2834. '/div'
  2835. ];
  2836. $this->assertHtml($expected, $result);
  2837. $this->Form->data = [];
  2838. $result = $this->Form->input('Publisher.id', [
  2839. 'label' => 'Publisher',
  2840. 'type' => 'select',
  2841. 'multiple' => 'checkbox',
  2842. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  2843. ]);
  2844. $expected = [
  2845. ['div' => ['class' => 'input select']],
  2846. ['label' => ['for' => 'publisher-id']],
  2847. 'Publisher',
  2848. '/label',
  2849. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  2850. ['div' => ['class' => 'checkbox']],
  2851. ['label' => ['for' => 'publisher-id-value-1']],
  2852. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  2853. 'Label 1',
  2854. '/label',
  2855. '/div',
  2856. ['div' => ['class' => 'checkbox']],
  2857. ['label' => ['for' => 'publisher-id-value-2']],
  2858. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  2859. 'Label 2',
  2860. '/label',
  2861. '/div',
  2862. '/div'
  2863. ];
  2864. $this->assertHtml($expected, $result);
  2865. }
  2866. /**
  2867. * test that input() and a non standard primary key makes a hidden input by default.
  2868. *
  2869. * @return void
  2870. */
  2871. public function testInputWithNonStandardPrimaryKeyMakesHidden()
  2872. {
  2873. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  2874. $this->Form->create($this->article);
  2875. $result = $this->Form->input('title');
  2876. $expected = [
  2877. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  2878. ];
  2879. $this->assertHtml($expected, $result);
  2880. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  2881. $this->Form->create($this->article);
  2882. $result = $this->Form->input('title');
  2883. $expected = [
  2884. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  2885. ];
  2886. $this->assertHtml($expected, $result);
  2887. $result = $this->Form->input('body');
  2888. $expected = [
  2889. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  2890. ];
  2891. $this->assertHtml($expected, $result);
  2892. }
  2893. /**
  2894. * test that overriding the magic select type widget is possible
  2895. *
  2896. * @return void
  2897. */
  2898. public function testInputOverridingMagicSelectType()
  2899. {
  2900. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  2901. $result = $this->Form->input('Model.user_id', ['type' => 'text']);
  2902. $expected = [
  2903. 'div' => ['class' => 'input text'],
  2904. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  2905. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  2906. '/div'
  2907. ];
  2908. $this->assertHtml($expected, $result);
  2909. //Check that magic types still work for plural/singular vars
  2910. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  2911. $result = $this->Form->input('Model.type');
  2912. $expected = [
  2913. 'div' => ['class' => 'input select'],
  2914. 'label' => ['for' => 'model-type'], 'Type', '/label',
  2915. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  2916. ['option' => ['value' => 'value']], 'good', '/option',
  2917. ['option' => ['value' => 'other']], 'bad', '/option',
  2918. '/select',
  2919. '/div'
  2920. ];
  2921. $this->assertHtml($expected, $result);
  2922. }
  2923. /**
  2924. * Test that inferred types do not override developer input
  2925. *
  2926. * @return void
  2927. */
  2928. public function testInputMagicTypeDoesNotOverride()
  2929. {
  2930. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  2931. $result = $this->Form->input('Model.user', ['type' => 'checkbox']);
  2932. $expected = [
  2933. 'div' => ['class' => 'input checkbox'],
  2934. ['input' => [
  2935. 'type' => 'hidden',
  2936. 'name' => 'Model[user]',
  2937. 'value' => 0,
  2938. ]],
  2939. 'label' => ['for' => 'model-user'],
  2940. ['input' => [
  2941. 'name' => 'Model[user]',
  2942. 'type' => 'checkbox',
  2943. 'id' => 'model-user',
  2944. 'value' => 1
  2945. ]],
  2946. 'User',
  2947. '/label',
  2948. '/div'
  2949. ];
  2950. $this->assertHtml($expected, $result);
  2951. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  2952. $options = [
  2953. 1 => 'blue',
  2954. 2 => 'red'
  2955. ];
  2956. $result = $this->Form->input('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  2957. $expected = [
  2958. 'div' => ['class' => 'input select'],
  2959. 'label' => ['for' => 'tags-ids'],
  2960. 'Tags',
  2961. '/label',
  2962. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  2963. ['div' => ['class' => 'checkbox']],
  2964. ['label' => ['for' => 'tags-ids-1']],
  2965. ['input' => [
  2966. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  2967. 'value' => '1', 'name' => 'tags[_ids][]'
  2968. ]],
  2969. 'blue',
  2970. '/label',
  2971. '/div',
  2972. ['div' => ['class' => 'checkbox']],
  2973. ['label' => ['for' => 'tags-ids-2']],
  2974. ['input' => [
  2975. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  2976. 'value' => '2', 'name' => 'tags[_ids][]'
  2977. ]],
  2978. 'red',
  2979. '/label',
  2980. '/div',
  2981. '/div'
  2982. ];
  2983. $this->assertHtml($expected, $result);
  2984. }
  2985. /**
  2986. * Test that magic input() selects are created for type=number
  2987. *
  2988. * @return void
  2989. */
  2990. public function testInputMagicSelectForTypeNumber()
  2991. {
  2992. TableRegistry::get('ValidateUsers', [
  2993. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2994. ]);
  2995. $entity = new Entity(['balance' => 1]);
  2996. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  2997. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  2998. $result = $this->Form->input('balance');
  2999. $expected = [
  3000. 'div' => ['class' => 'input select'],
  3001. 'label' => ['for' => 'balance'],
  3002. 'Balance',
  3003. '/label',
  3004. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3005. ['option' => ['value' => '0']],
  3006. 'nothing',
  3007. '/option',
  3008. ['option' => ['value' => '1', 'selected' => 'selected']],
  3009. 'some',
  3010. '/option',
  3011. ['option' => ['value' => '100']],
  3012. 'a lot',
  3013. '/option',
  3014. '/select',
  3015. '/div'
  3016. ];
  3017. $this->assertHtml($expected, $result);
  3018. }
  3019. /**
  3020. * Test that magic input() selects can easily be converted into radio types without error.
  3021. *
  3022. * @return void
  3023. */
  3024. public function testInputMagicSelectChangeToRadio()
  3025. {
  3026. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3027. $result = $this->Form->input('Model.user_id', ['type' => 'radio']);
  3028. $this->assertContains('input type="radio"', $result);
  3029. }
  3030. /**
  3031. * testFormInputSubmit method
  3032. *
  3033. * test correct results for form::input() and type submit.
  3034. *
  3035. * @return void
  3036. */
  3037. public function testFormInputSubmit()
  3038. {
  3039. $result = $this->Form->input('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3040. $expected = [
  3041. 'div' => ['class' => 'submit'],
  3042. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3043. '/div'
  3044. ];
  3045. $this->assertHtml($expected, $result);
  3046. }
  3047. /**
  3048. * testFormInputs method
  3049. *
  3050. * test correct results from form::inputs().
  3051. *
  3052. * @return void
  3053. */
  3054. public function testFormInputsLegendFieldset()
  3055. {
  3056. $this->Form->create($this->article);
  3057. $result = $this->Form->allInputs([], ['legend' => 'The Legend']);
  3058. $expected = [
  3059. '<fieldset',
  3060. '<legend',
  3061. 'The Legend',
  3062. '/legend',
  3063. '*/fieldset',
  3064. ];
  3065. $this->assertHtml($expected, $result);
  3066. $result = $this->Form->allInputs([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3067. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3068. $this->assertContains('<fieldset>', $result);
  3069. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => false]);
  3070. $this->assertNotContains('<legend>', $result);
  3071. $this->assertNotContains('<fieldset>', $result);
  3072. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => 'Hello']);
  3073. $this->assertNotContains('<legend>', $result);
  3074. $this->assertNotContains('<fieldset>', $result);
  3075. $this->Form->create($this->article);
  3076. $this->Form->request->params['prefix'] = 'admin';
  3077. $this->Form->request->params['action'] = 'admin_edit';
  3078. $this->Form->request->params['controller'] = 'articles';
  3079. $result = $this->Form->allInputs();
  3080. $expected = [
  3081. '<fieldset',
  3082. '<legend',
  3083. 'New Article',
  3084. '/legend',
  3085. '*/fieldset',
  3086. ];
  3087. $this->assertHtml($expected, $result);
  3088. $this->Form->create($this->article);
  3089. $result = $this->Form->allInputs([], ['fieldset' => [], 'legend' => 'The Legend']);
  3090. $expected = [
  3091. '<fieldset',
  3092. '<legend',
  3093. 'The Legend',
  3094. '/legend',
  3095. '*/fieldset',
  3096. ];
  3097. $this->assertHtml($expected, $result);
  3098. $this->Form->create($this->article);
  3099. $result = $this->Form->allInputs([], [
  3100. 'fieldset' => [
  3101. 'class' => 'some-class some-other-class',
  3102. 'disabled' => true,
  3103. 'data-param' => 'a-param'
  3104. ],
  3105. 'legend' => 'The Legend'
  3106. ]);
  3107. $expected = [
  3108. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3109. '<legend',
  3110. 'The Legend',
  3111. '/legend',
  3112. '*/fieldset',
  3113. ];
  3114. $this->assertHtml($expected, $result);
  3115. }
  3116. /**
  3117. * Test the inputs() method.
  3118. *
  3119. * @return void
  3120. */
  3121. public function testFormInputs()
  3122. {
  3123. $this->Form->create($this->article);
  3124. $result = $this->Form->allInputs();
  3125. $expected = [
  3126. '<fieldset',
  3127. '<legend', 'New Article', '/legend',
  3128. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3129. ['div' => ['class' => 'input select required']],
  3130. '*/div',
  3131. ['div' => ['class' => 'input text required']],
  3132. '*/div',
  3133. ['div' => ['class' => 'input text']],
  3134. '*/div',
  3135. ['div' => ['class' => 'input text']],
  3136. '*/div',
  3137. '/fieldset',
  3138. ];
  3139. $this->assertHtml($expected, $result);
  3140. $result = $this->Form->allInputs([
  3141. 'published' => ['type' => 'boolean']
  3142. ]);
  3143. $expected = [
  3144. '<fieldset',
  3145. '<legend', 'New Article', '/legend',
  3146. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3147. ['div' => ['class' => 'input select required']],
  3148. '*/div',
  3149. ['div' => ['class' => 'input text required']],
  3150. '*/div',
  3151. ['div' => ['class' => 'input text']],
  3152. '*/div',
  3153. ['div' => ['class' => 'input boolean']],
  3154. '*/div',
  3155. '/fieldset',
  3156. ];
  3157. $this->assertHtml($expected, $result);
  3158. $this->Form->create($this->article);
  3159. $result = $this->Form->allInputs([], ['legend' => 'Hello']);
  3160. $expected = [
  3161. 'fieldset' => [],
  3162. 'legend' => [],
  3163. 'Hello',
  3164. '/legend',
  3165. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3166. ['div' => ['class' => 'input select required']],
  3167. '*/div',
  3168. ['div' => ['class' => 'input text required']],
  3169. '*/div',
  3170. ['div' => ['class' => 'input text']],
  3171. '*/div',
  3172. ['div' => ['class' => 'input text']],
  3173. '*/div',
  3174. '/fieldset'
  3175. ];
  3176. $this->assertHtml($expected, $result);
  3177. $this->Form->create(false);
  3178. $expected = [
  3179. 'fieldset' => [],
  3180. ['div' => ['class' => 'input text']],
  3181. 'label' => ['for' => 'foo'],
  3182. 'Foo',
  3183. '/label',
  3184. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3185. '*/div',
  3186. '/fieldset'
  3187. ];
  3188. $result = $this->Form->allInputs(
  3189. ['foo' => ['type' => 'text']],
  3190. ['legend' => false]
  3191. );
  3192. $this->assertHtml($expected, $result);
  3193. }
  3194. /**
  3195. * testFormInputsBlacklist
  3196. *
  3197. * @return void
  3198. */
  3199. public function testFormInputsBlacklist()
  3200. {
  3201. $this->Form->create($this->article);
  3202. $result = $this->Form->allInputs([
  3203. 'id' => false
  3204. ]);
  3205. $expected = [
  3206. '<fieldset',
  3207. '<legend', 'New Article', '/legend',
  3208. ['div' => ['class' => 'input select required']],
  3209. '*/div',
  3210. ['div' => ['class' => 'input text required']],
  3211. '*/div',
  3212. ['div' => ['class' => 'input text']],
  3213. '*/div',
  3214. ['div' => ['class' => 'input text']],
  3215. '*/div',
  3216. '/fieldset',
  3217. ];
  3218. $this->assertHtml($expected, $result);
  3219. $this->Form->create($this->article);
  3220. $result = $this->Form->allInputs([
  3221. 'id' => []
  3222. ]);
  3223. $expected = [
  3224. '<fieldset',
  3225. '<legend', 'New Article', '/legend',
  3226. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3227. ['div' => ['class' => 'input select required']],
  3228. '*/div',
  3229. ['div' => ['class' => 'input text required']],
  3230. '*/div',
  3231. ['div' => ['class' => 'input text']],
  3232. '*/div',
  3233. ['div' => ['class' => 'input text']],
  3234. '*/div',
  3235. '/fieldset',
  3236. ];
  3237. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3238. }
  3239. /**
  3240. * testSelectAsCheckbox method
  3241. *
  3242. * test multi-select widget with checkbox formatting.
  3243. *
  3244. * @return void
  3245. */
  3246. public function testSelectAsCheckbox()
  3247. {
  3248. $result = $this->Form->select(
  3249. 'Model.multi_field',
  3250. ['first', 'second', 'third'],
  3251. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3252. );
  3253. $expected = [
  3254. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3255. ['div' => ['class' => 'checkbox']],
  3256. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3257. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3258. 'first',
  3259. '/label',
  3260. '/div',
  3261. ['div' => ['class' => 'checkbox']],
  3262. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3263. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3264. 'second',
  3265. '/label',
  3266. '/div',
  3267. ['div' => ['class' => 'checkbox']],
  3268. ['label' => ['for' => 'model-multi-field-2']],
  3269. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3270. 'third',
  3271. '/label',
  3272. '/div',
  3273. ];
  3274. $this->assertHtml($expected, $result);
  3275. $result = $this->Form->select(
  3276. 'Model.multi_field',
  3277. ['1/2' => 'half'],
  3278. ['multiple' => 'checkbox']
  3279. );
  3280. $expected = [
  3281. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3282. ['div' => ['class' => 'checkbox']],
  3283. ['label' => ['for' => 'model-multi-field-1-2']],
  3284. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3285. 'half',
  3286. '/label',
  3287. '/div',
  3288. ];
  3289. $this->assertHtml($expected, $result);
  3290. }
  3291. /**
  3292. * testLabel method
  3293. *
  3294. * test label generation.
  3295. *
  3296. * @return void
  3297. */
  3298. public function testLabel()
  3299. {
  3300. $result = $this->Form->label('Person.name');
  3301. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3302. $this->assertHtml($expected, $result);
  3303. $result = $this->Form->label('Person.name');
  3304. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3305. $this->assertHtml($expected, $result);
  3306. $result = $this->Form->label('Person.first_name');
  3307. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3308. $this->assertHtml($expected, $result);
  3309. $result = $this->Form->label('Person.first_name', 'Your first name');
  3310. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3311. $this->assertHtml($expected, $result);
  3312. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3313. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3314. $this->assertHtml($expected, $result);
  3315. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3316. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3317. $this->assertHtml($expected, $result);
  3318. $result = $this->Form->label('Person.first_name', '');
  3319. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3320. $this->assertHtml($expected, $result);
  3321. $result = $this->Form->label('Person.2.name', '');
  3322. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3323. $this->assertHtml($expected, $result);
  3324. }
  3325. /**
  3326. * Test that label() can accept an input with the correct template vars.
  3327. *
  3328. * @return void
  3329. */
  3330. public function testLabelContainInput()
  3331. {
  3332. $this->Form->templates([
  3333. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3334. ]);
  3335. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3336. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3337. ]);
  3338. $expected = [
  3339. 'label' => ['for' => 'person-accept-terms'],
  3340. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3341. 'Accept',
  3342. '/label',
  3343. ];
  3344. $this->assertHtml($expected, $result);
  3345. }
  3346. /**
  3347. * testTextbox method
  3348. *
  3349. * test textbox element generation
  3350. *
  3351. * @return void
  3352. */
  3353. public function testTextbox()
  3354. {
  3355. $result = $this->Form->text('Model.field');
  3356. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3357. $this->assertHtml($expected, $result);
  3358. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3359. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3360. $this->assertHtml($expected, $result);
  3361. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3362. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3363. $this->assertHtml($expected, $result);
  3364. }
  3365. /**
  3366. * Test that text() hooks up with request data and error fields.
  3367. *
  3368. * @return void
  3369. */
  3370. public function testTextBoxDataAndError()
  3371. {
  3372. $this->article['errors'] = [
  3373. 'Contact' => ['text' => 'wrong']
  3374. ];
  3375. $this->Form->create($this->article);
  3376. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3377. $result = $this->Form->text('Model.text');
  3378. $expected = [
  3379. 'input' => [
  3380. 'type' => 'text',
  3381. 'name' => 'Model[text]',
  3382. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3383. ]
  3384. ];
  3385. $this->assertHtml($expected, $result);
  3386. $this->Form->request->data['Contact']['text'] = 'test';
  3387. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3388. $expected = [
  3389. 'input' => [
  3390. 'type' => 'text',
  3391. 'name' => 'Contact[text]',
  3392. 'value' => 'test',
  3393. 'id' => 'theID',
  3394. 'class' => 'form-error'
  3395. ]
  3396. ];
  3397. $this->assertHtml($expected, $result);
  3398. }
  3399. /**
  3400. * testDefaultValue method
  3401. *
  3402. * Test default value setting
  3403. *
  3404. * @return void
  3405. */
  3406. public function testTextDefaultValue()
  3407. {
  3408. $this->Form->request->data['Model']['field'] = 'test';
  3409. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3410. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3411. $this->assertHtml($expected, $result);
  3412. unset($this->Form->request->data['Model']['field']);
  3413. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3414. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3415. $this->assertHtml($expected, $result);
  3416. }
  3417. /**
  3418. * testError method
  3419. *
  3420. * Test field error generation
  3421. *
  3422. * @return void
  3423. */
  3424. public function testError()
  3425. {
  3426. $this->article['errors'] = [
  3427. 'Article' => ['field' => 'email']
  3428. ];
  3429. $this->Form->create($this->article);
  3430. $result = $this->Form->error('Article.field');
  3431. $expected = [
  3432. ['div' => ['class' => 'error-message']],
  3433. 'email',
  3434. '/div',
  3435. ];
  3436. $this->assertHtml($expected, $result);
  3437. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3438. $expected = [
  3439. ['div' => ['class' => 'error-message']],
  3440. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3441. '/div',
  3442. ];
  3443. $this->assertHtml($expected, $result);
  3444. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3445. $expected = [
  3446. ['div' => ['class' => 'error-message']],
  3447. '<strong', 'Badness!', '/strong',
  3448. '/div',
  3449. ];
  3450. $this->assertHtml($expected, $result);
  3451. }
  3452. /**
  3453. * Test error with nested lists.
  3454. *
  3455. * @return void
  3456. */
  3457. public function testErrorMessages()
  3458. {
  3459. $this->article['errors'] = [
  3460. 'Article' => ['field' => 'email']
  3461. ];
  3462. $this->Form->create($this->article);
  3463. $result = $this->Form->error('Article.field', [
  3464. 'email' => 'No good!'
  3465. ]);
  3466. $expected = [
  3467. 'div' => ['class' => 'error-message'],
  3468. 'No good!',
  3469. '/div'
  3470. ];
  3471. $this->assertHtml($expected, $result);
  3472. }
  3473. /**
  3474. * test error() with multiple messages.
  3475. *
  3476. * @return void
  3477. */
  3478. public function testErrorMultipleMessages()
  3479. {
  3480. $this->article['errors'] = [
  3481. 'field' => ['notBlank', 'email', 'Something else']
  3482. ];
  3483. $this->Form->create($this->article);
  3484. $result = $this->Form->error('field', [
  3485. 'notBlank' => 'Cannot be empty',
  3486. 'email' => 'No good!'
  3487. ]);
  3488. $expected = [
  3489. 'div' => ['class' => 'error-message'],
  3490. 'ul' => [],
  3491. '<li', 'Cannot be empty', '/li',
  3492. '<li', 'No good!', '/li',
  3493. '<li', 'Something else', '/li',
  3494. '/ul',
  3495. '/div'
  3496. ];
  3497. $this->assertHtml($expected, $result);
  3498. }
  3499. /**
  3500. * testPassword method
  3501. *
  3502. * Test password element generation
  3503. *
  3504. * @return void
  3505. */
  3506. public function testPassword()
  3507. {
  3508. $this->article['errors'] = [
  3509. 'Contact' => [
  3510. 'passwd' => 1
  3511. ]
  3512. ];
  3513. $this->Form->create($this->article);
  3514. $result = $this->Form->password('Contact.field');
  3515. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  3516. $this->assertHtml($expected, $result);
  3517. $this->Form->request->data['Contact']['passwd'] = 'test';
  3518. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  3519. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  3520. $this->assertHtml($expected, $result);
  3521. }
  3522. /**
  3523. * testRadio method
  3524. *
  3525. * Test radio element set generation
  3526. *
  3527. * @return void
  3528. */
  3529. public function testRadio()
  3530. {
  3531. $result = $this->Form->radio('Model.field', ['option A']);
  3532. $expected = [
  3533. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3534. 'label' => ['for' => 'model-field-0'],
  3535. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  3536. 'option A',
  3537. '/label'
  3538. ];
  3539. $this->assertHtml($expected, $result);
  3540. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  3541. $this->assertHtml($expected, $result);
  3542. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3543. $expected = [
  3544. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3545. ['label' => ['for' => 'model-field-0']],
  3546. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  3547. 'option A',
  3548. '/label',
  3549. ['label' => ['for' => 'model-field-1']],
  3550. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  3551. 'option B',
  3552. '/label',
  3553. ];
  3554. $this->assertHtml($expected, $result);
  3555. $result = $this->Form->radio(
  3556. 'Employee.gender',
  3557. ['male' => 'Male', 'female' => 'Female'],
  3558. ['form' => 'my-form']
  3559. );
  3560. $expected = [
  3561. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  3562. ['label' => ['for' => 'employee-gender-male']],
  3563. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  3564. 'Male',
  3565. '/label',
  3566. ['label' => ['for' => 'employee-gender-female']],
  3567. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  3568. 'Female',
  3569. '/label',
  3570. ];
  3571. $this->assertHtml($expected, $result);
  3572. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  3573. $expected = [
  3574. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  3575. ['label' => ['for' => 'model-custom-0']],
  3576. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  3577. 'option A',
  3578. '/label',
  3579. ['label' => ['for' => 'model-custom-1']],
  3580. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  3581. 'option B',
  3582. '/label',
  3583. ];
  3584. $this->assertHtml($expected, $result);
  3585. $result = $this->Form->radio(
  3586. 'Employee.gender',
  3587. [
  3588. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  3589. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  3590. ]
  3591. );
  3592. $expected = [
  3593. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  3594. ['label' => ['for' => 'employee-gender-male']],
  3595. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  3596. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  3597. 'Male',
  3598. '/label',
  3599. ['label' => ['for' => 'employee-gender-female']],
  3600. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  3601. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  3602. 'Female',
  3603. '/label',
  3604. ];
  3605. $this->assertHtml($expected, $result);
  3606. }
  3607. /**
  3608. * Test that input works with radio types
  3609. *
  3610. * @return void
  3611. */
  3612. public function testInputRadio()
  3613. {
  3614. $result = $this->Form->input('test', [
  3615. 'type' => 'radio',
  3616. 'options' => ['A', 'B'],
  3617. ]);
  3618. $expected = [
  3619. ['div' => ['class' => 'input radio']],
  3620. '<label',
  3621. 'Test',
  3622. '/label',
  3623. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  3624. ['label' => ['for' => 'test-0']],
  3625. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  3626. 'A',
  3627. '/label',
  3628. ['label' => ['for' => 'test-1']],
  3629. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  3630. 'B',
  3631. '/label',
  3632. '/div',
  3633. ];
  3634. $this->assertHtml($expected, $result);
  3635. $result = $this->Form->input('test', [
  3636. 'type' => 'radio',
  3637. 'options' => ['A', 'B'],
  3638. 'value' => '0'
  3639. ]);
  3640. $expected = [
  3641. ['div' => ['class' => 'input radio']],
  3642. '<label',
  3643. 'Test',
  3644. '/label',
  3645. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  3646. ['label' => ['for' => 'test-0']],
  3647. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  3648. 'A',
  3649. '/label',
  3650. ['label' => ['for' => 'test-1']],
  3651. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  3652. 'B',
  3653. '/label',
  3654. '/div',
  3655. ];
  3656. $this->assertHtml($expected, $result);
  3657. $result = $this->Form->input('test', [
  3658. 'type' => 'radio',
  3659. 'options' => ['A', 'B'],
  3660. 'label' => false
  3661. ]);
  3662. $expected = [
  3663. ['div' => ['class' => 'input radio']],
  3664. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  3665. ['label' => ['for' => 'test-0']],
  3666. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  3667. 'A',
  3668. '/label',
  3669. ['label' => ['for' => 'test-1']],
  3670. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  3671. 'B',
  3672. '/label',
  3673. '/div',
  3674. ];
  3675. $this->assertHtml($expected, $result);
  3676. }
  3677. /**
  3678. * Test that radio() works with label = false
  3679. *
  3680. * @return void
  3681. */
  3682. public function testRadioNoLabel()
  3683. {
  3684. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  3685. $expected = [
  3686. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3687. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  3688. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  3689. ];
  3690. $this->assertHtml($expected, $result);
  3691. }
  3692. /**
  3693. * test generating radio input inside label ala twitter bootstrap
  3694. *
  3695. * @return void
  3696. */
  3697. public function testRadioInputInsideLabel()
  3698. {
  3699. $this->Form->templates([
  3700. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3701. 'radioWrapper' => '{{label}}'
  3702. ]);
  3703. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3704. //@codingStandardsIgnoreStart
  3705. $expected = [
  3706. ['input' => [
  3707. 'type' => 'hidden',
  3708. 'name' => 'Model[field]',
  3709. 'value' => ''
  3710. ]],
  3711. ['label' => ['for' => 'model-field-0']],
  3712. ['input' => [
  3713. 'type' => 'radio',
  3714. 'name' => 'Model[field]',
  3715. 'value' => '0',
  3716. 'id' => 'model-field-0'
  3717. ]],
  3718. 'option A',
  3719. '/label',
  3720. ['label' => ['for' => 'model-field-1']],
  3721. ['input' => [
  3722. 'type' => 'radio',
  3723. 'name' => 'Model[field]',
  3724. 'value' => '1',
  3725. 'id' => 'model-field-1'
  3726. ]],
  3727. 'option B',
  3728. '/label',
  3729. ];
  3730. //@codingStandardsIgnoreEnd
  3731. $this->assertHtml($expected, $result);
  3732. }
  3733. /**
  3734. * test disabling the hidden input for radio buttons
  3735. *
  3736. * @return void
  3737. */
  3738. public function testRadioHiddenInputDisabling()
  3739. {
  3740. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  3741. $expected = [
  3742. 'label' => ['for' => 'model-1-field-0'],
  3743. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  3744. 'option A',
  3745. '/label'
  3746. ];
  3747. $this->assertHtml($expected, $result);
  3748. }
  3749. /**
  3750. * testRadio method
  3751. *
  3752. * Test radio element set generation
  3753. *
  3754. * @return void
  3755. */
  3756. public function testRadioOutOfRange()
  3757. {
  3758. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  3759. $expected = [
  3760. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3761. 'label' => ['for' => 'model-field-v'],
  3762. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  3763. 'value',
  3764. '/label'
  3765. ];
  3766. $this->assertHtml($expected, $result);
  3767. }
  3768. /**
  3769. * testSelect method
  3770. *
  3771. * Test select element generation.
  3772. *
  3773. * @return void
  3774. */
  3775. public function testSelect()
  3776. {
  3777. $result = $this->Form->select('Model.field', []);
  3778. $expected = [
  3779. 'select' => ['name' => 'Model[field]'],
  3780. '/select'
  3781. ];
  3782. $this->assertHtml($expected, $result);
  3783. $this->Form->request->data = ['Model' => ['field' => 'value']];
  3784. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  3785. $expected = [
  3786. 'select' => ['name' => 'Model[field]'],
  3787. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3788. 'good',
  3789. '/option',
  3790. ['option' => ['value' => 'other']],
  3791. 'bad',
  3792. '/option',
  3793. '/select'
  3794. ];
  3795. $this->assertHtml($expected, $result);
  3796. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  3797. $this->assertHtml($expected, $result);
  3798. $this->Form->request->data = [];
  3799. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  3800. $expected = [
  3801. 'select' => ['name' => 'Model[field]'],
  3802. ['option' => ['value' => 'value']],
  3803. 'good',
  3804. '/option',
  3805. ['option' => ['value' => 'other']],
  3806. 'bad',
  3807. '/option',
  3808. '/select'
  3809. ];
  3810. $this->assertHtml($expected, $result);
  3811. $options = [
  3812. ['value' => 'first', 'text' => 'First'],
  3813. ['value' => 'first', 'text' => 'Another First'],
  3814. ];
  3815. $result = $this->Form->select(
  3816. 'Model.field',
  3817. $options,
  3818. ['escape' => false, 'empty' => false]
  3819. );
  3820. $expected = [
  3821. 'select' => ['name' => 'Model[field]'],
  3822. ['option' => ['value' => 'first']],
  3823. 'First',
  3824. '/option',
  3825. ['option' => ['value' => 'first']],
  3826. 'Another First',
  3827. '/option',
  3828. '/select'
  3829. ];
  3830. $this->assertHtml($expected, $result);
  3831. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  3832. $result = $this->Form->select(
  3833. 'Model.contact_id',
  3834. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  3835. ['escape' => false, 'empty' => 'pick something']
  3836. );
  3837. $expected = [
  3838. 'select' => ['name' => 'Model[contact_id]'],
  3839. ['option' => ['value' => '']], 'pick something', '/option',
  3840. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  3841. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  3842. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  3843. '/select'
  3844. ];
  3845. $this->assertHtml($expected, $result);
  3846. $this->Form->request->data['Model']['field'] = 0;
  3847. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  3848. $expected = [
  3849. 'select' => ['name' => 'Model[field]'],
  3850. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  3851. ['option' => ['value' => '1']], 'Yes', '/option',
  3852. '/select'
  3853. ];
  3854. $this->assertHtml($expected, $result);
  3855. }
  3856. /**
  3857. * Test that select() escapes HTML.
  3858. *
  3859. * @return void
  3860. */
  3861. public function testSelectEscapeHtml()
  3862. {
  3863. $result = $this->Form->select(
  3864. 'Model.field',
  3865. ['first' => 'first "html" <chars>', 'second' => 'value'],
  3866. ['empty' => false]
  3867. );
  3868. $expected = [
  3869. 'select' => ['name' => 'Model[field]'],
  3870. ['option' => ['value' => 'first']],
  3871. 'first &quot;html&quot; &lt;chars&gt;',
  3872. '/option',
  3873. ['option' => ['value' => 'second']],
  3874. 'value',
  3875. '/option',
  3876. '/select'
  3877. ];
  3878. $this->assertHtml($expected, $result);
  3879. $result = $this->Form->select(
  3880. 'Model.field',
  3881. ['first' => 'first "html" <chars>', 'second' => 'value'],
  3882. ['escape' => false, 'empty' => false]
  3883. );
  3884. $expected = [
  3885. 'select' => ['name' => 'Model[field]'],
  3886. ['option' => ['value' => 'first']],
  3887. 'first "html" <chars>',
  3888. '/option',
  3889. ['option' => ['value' => 'second']],
  3890. 'value',
  3891. '/option',
  3892. '/select'
  3893. ];
  3894. $this->assertHtml($expected, $result);
  3895. }
  3896. /**
  3897. * test select() with required and disabled attributes.
  3898. *
  3899. * @return void
  3900. */
  3901. public function testSelectRequired()
  3902. {
  3903. $this->article['required'] = [
  3904. 'user_id' => true
  3905. ];
  3906. $this->Form->create($this->article);
  3907. $result = $this->Form->select('user_id', ['option A']);
  3908. $expected = [
  3909. 'select' => [
  3910. 'name' => 'user_id',
  3911. 'required' => 'required'
  3912. ],
  3913. ['option' => ['value' => '0']], 'option A', '/option',
  3914. '/select'
  3915. ];
  3916. $this->assertHtml($expected, $result);
  3917. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  3918. $expected = [
  3919. 'select' => [
  3920. 'name' => 'user_id',
  3921. 'disabled' => 'disabled'
  3922. ],
  3923. ['option' => ['value' => '0']], 'option A', '/option',
  3924. '/select'
  3925. ];
  3926. $this->assertHtml($expected, $result);
  3927. }
  3928. /**
  3929. * testNestedSelect method
  3930. *
  3931. * test select element generation with optgroups
  3932. *
  3933. * @return void
  3934. */
  3935. public function testNestedSelect()
  3936. {
  3937. $result = $this->Form->select(
  3938. 'Model.field',
  3939. [1 => 'One', 2 => 'Two', 'Three' => [
  3940. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3941. ]],
  3942. ['empty' => false]
  3943. );
  3944. $expected = [
  3945. 'select' => ['name' => 'Model[field]'],
  3946. ['option' => ['value' => 1]],
  3947. 'One',
  3948. '/option',
  3949. ['option' => ['value' => 2]],
  3950. 'Two',
  3951. '/option',
  3952. ['optgroup' => ['label' => 'Three']],
  3953. ['option' => ['value' => 3]],
  3954. 'Three',
  3955. '/option',
  3956. ['option' => ['value' => 4]],
  3957. 'Four',
  3958. '/option',
  3959. ['option' => ['value' => 5]],
  3960. 'Five',
  3961. '/option',
  3962. '/optgroup',
  3963. '/select'
  3964. ];
  3965. $this->assertHtml($expected, $result);
  3966. }
  3967. /**
  3968. * testSelectMultiple method
  3969. *
  3970. * test generation of multiple select elements
  3971. *
  3972. * @return void
  3973. */
  3974. public function testSelectMultiple()
  3975. {
  3976. $options = ['first', 'second', 'third'];
  3977. $result = $this->Form->select(
  3978. 'Model.multi_field',
  3979. $options,
  3980. ['form' => 'my-form', 'multiple' => true]
  3981. );
  3982. $expected = [
  3983. 'input' => [
  3984. 'type' => 'hidden',
  3985. 'name' => 'Model[multi_field]',
  3986. 'value' => '',
  3987. 'form' => 'my-form',
  3988. ],
  3989. 'select' => [
  3990. 'name' => 'Model[multi_field][]',
  3991. 'multiple' => 'multiple',
  3992. 'form' => 'my-form',
  3993. ],
  3994. ['option' => ['value' => '0']],
  3995. 'first',
  3996. '/option',
  3997. ['option' => ['value' => '1']],
  3998. 'second',
  3999. '/option',
  4000. ['option' => ['value' => '2']],
  4001. 'third',
  4002. '/option',
  4003. '/select'
  4004. ];
  4005. $this->assertHtml($expected, $result);
  4006. $result = $this->Form->select(
  4007. 'Model.multi_field',
  4008. $options,
  4009. ['multiple' => 'multiple', 'form' => 'my-form']
  4010. );
  4011. $this->assertHtml($expected, $result);
  4012. }
  4013. /**
  4014. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4015. *
  4016. * @return void
  4017. */
  4018. public function testCheckboxZeroValue()
  4019. {
  4020. $result = $this->Form->input('User.get_spam', [
  4021. 'type' => 'checkbox',
  4022. 'value' => '0',
  4023. 'hiddenField' => '1',
  4024. ]);
  4025. $expected = [
  4026. 'div' => ['class' => 'input checkbox'],
  4027. 'label' => ['for' => 'user-get-spam'],
  4028. ['input' => [
  4029. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4030. 'value' => '1'
  4031. ]],
  4032. ['input' => [
  4033. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4034. 'value' => '0', 'id' => 'user-get-spam'
  4035. ]],
  4036. 'Get Spam',
  4037. '/label',
  4038. '/div'
  4039. ];
  4040. $this->assertHtml($expected, $result);
  4041. }
  4042. /**
  4043. * test generation of habtm select boxes.
  4044. *
  4045. * @return void
  4046. */
  4047. public function testHabtmSelectBox()
  4048. {
  4049. $this->loadFixtures('Articles');
  4050. $options = [
  4051. 1 => 'blue',
  4052. 2 => 'red',
  4053. 3 => 'green'
  4054. ];
  4055. $tags = [
  4056. new Entity(['id' => 1, 'name' => 'blue']),
  4057. new Entity(['id' => 3, 'name' => 'green'])
  4058. ];
  4059. $article = new Article(['tags' => $tags]);
  4060. $this->Form->create($article);
  4061. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4062. $expected = [
  4063. 'div' => ['class' => 'input select'],
  4064. 'label' => ['for' => 'tags-ids'],
  4065. 'Tags',
  4066. '/label',
  4067. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4068. 'select' => [
  4069. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4070. 'multiple' => 'multiple'
  4071. ],
  4072. ['option' => ['value' => '1', 'selected' => 'selected']],
  4073. 'blue',
  4074. '/option',
  4075. ['option' => ['value' => '2']],
  4076. 'red',
  4077. '/option',
  4078. ['option' => ['value' => '3', 'selected' => 'selected']],
  4079. 'green',
  4080. '/option',
  4081. '/select',
  4082. '/div'
  4083. ];
  4084. $this->assertHtml($expected, $result);
  4085. // make sure only 50 is selected, and not 50f5c0cf
  4086. $options = [
  4087. '1' => 'blue',
  4088. '50f5c0cf' => 'red',
  4089. '50' => 'green'
  4090. ];
  4091. $tags = [
  4092. new Entity(['id' => 1, 'name' => 'blue']),
  4093. new Entity(['id' => 50, 'name' => 'green'])
  4094. ];
  4095. $article = new Article(['tags' => $tags]);
  4096. $this->Form->create($article);
  4097. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4098. $expected = [
  4099. 'div' => ['class' => 'input select'],
  4100. 'label' => ['for' => 'tags-ids'],
  4101. 'Tags',
  4102. '/label',
  4103. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4104. 'select' => [
  4105. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4106. 'multiple' => 'multiple'
  4107. ],
  4108. ['option' => ['value' => '1', 'selected' => 'selected']],
  4109. 'blue',
  4110. '/option',
  4111. ['option' => ['value' => '50f5c0cf']],
  4112. 'red',
  4113. '/option',
  4114. ['option' => ['value' => '50', 'selected' => 'selected']],
  4115. 'green',
  4116. '/option',
  4117. '/select',
  4118. '/div'
  4119. ];
  4120. $this->assertHtml($expected, $result);
  4121. $spacecraft = [
  4122. 1 => 'Orion',
  4123. 2 => 'Helios'
  4124. ];
  4125. $this->View->viewVars['spacecraft'] = $spacecraft;
  4126. $this->Form->create();
  4127. $result = $this->Form->input('spacecraft._ids');
  4128. $expected = [
  4129. 'div' => ['class' => 'input select'],
  4130. 'label' => ['for' => 'spacecraft-ids'],
  4131. 'Spacecraft',
  4132. '/label',
  4133. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4134. 'select' => [
  4135. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4136. 'multiple' => 'multiple'
  4137. ],
  4138. ['option' => ['value' => '1']],
  4139. 'Orion',
  4140. '/option',
  4141. ['option' => ['value' => '2']],
  4142. 'Helios',
  4143. '/option',
  4144. '/select',
  4145. '/div'
  4146. ];
  4147. $this->assertHtml($expected, $result);
  4148. }
  4149. /**
  4150. * Tests that errors for belongsToMany select fields are being
  4151. * picked up properly.
  4152. *
  4153. * @return void
  4154. */
  4155. public function testErrorsForBelongsToManySelect()
  4156. {
  4157. $spacecraft = [
  4158. 1 => 'Orion',
  4159. 2 => 'Helios'
  4160. ];
  4161. $this->View->viewVars['spacecraft'] = $spacecraft;
  4162. $article = new Article();
  4163. $article->errors('spacecraft', ['Invalid']);
  4164. $this->Form->create($article);
  4165. $result = $this->Form->input('spacecraft._ids');
  4166. $expected = [
  4167. ['div' => ['class' => 'input select error']],
  4168. 'label' => ['for' => 'spacecraft-ids'],
  4169. 'Spacecraft',
  4170. '/label',
  4171. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4172. 'select' => [
  4173. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4174. 'multiple' => 'multiple'
  4175. ],
  4176. ['option' => ['value' => '1']],
  4177. 'Orion',
  4178. '/option',
  4179. ['option' => ['value' => '2']],
  4180. 'Helios',
  4181. '/option',
  4182. '/select',
  4183. ['div' => ['class' => 'error-message']],
  4184. 'Invalid',
  4185. '/div',
  4186. '/div'
  4187. ];
  4188. $this->assertHtml($expected, $result);
  4189. }
  4190. /**
  4191. * test generation of multi select elements in checkbox format
  4192. *
  4193. * @return void
  4194. */
  4195. public function testSelectMultipleCheckboxes()
  4196. {
  4197. $result = $this->Form->select(
  4198. 'Model.multi_field',
  4199. ['first', 'second', 'third'],
  4200. ['multiple' => 'checkbox']
  4201. );
  4202. $expected = [
  4203. 'input' => [
  4204. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4205. ],
  4206. ['div' => ['class' => 'checkbox']],
  4207. ['label' => ['for' => 'model-multi-field-0']],
  4208. ['input' => [
  4209. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4210. 'value' => '0', 'id' => 'model-multi-field-0'
  4211. ]],
  4212. 'first',
  4213. '/label',
  4214. '/div',
  4215. ['div' => ['class' => 'checkbox']],
  4216. ['label' => ['for' => 'model-multi-field-1']],
  4217. ['input' => [
  4218. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4219. 'value' => '1', 'id' => 'model-multi-field-1'
  4220. ]],
  4221. 'second',
  4222. '/label',
  4223. '/div',
  4224. ['div' => ['class' => 'checkbox']],
  4225. ['label' => ['for' => 'model-multi-field-2']],
  4226. ['input' => [
  4227. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4228. 'value' => '2', 'id' => 'model-multi-field-2'
  4229. ]],
  4230. 'third',
  4231. '/label',
  4232. '/div'
  4233. ];
  4234. $this->assertHtml($expected, $result);
  4235. $result = $this->Form->select(
  4236. 'Model.multi_field',
  4237. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4238. ['multiple' => 'checkbox']
  4239. );
  4240. $expected = [
  4241. 'input' => [
  4242. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4243. ],
  4244. ['div' => ['class' => 'checkbox']],
  4245. ['label' => ['for' => 'model-multi-field-a+']],
  4246. ['input' => [
  4247. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4248. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4249. ]],
  4250. 'first',
  4251. '/label',
  4252. '/div',
  4253. ['div' => ['class' => 'checkbox']],
  4254. ['label' => ['for' => 'model-multi-field-a++']],
  4255. ['input' => [
  4256. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4257. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4258. ]],
  4259. 'second',
  4260. '/label',
  4261. '/div',
  4262. ['div' => ['class' => 'checkbox']],
  4263. ['label' => ['for' => 'model-multi-field-a+++']],
  4264. ['input' => [
  4265. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4266. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4267. ]],
  4268. 'third',
  4269. '/label',
  4270. '/div'
  4271. ];
  4272. $this->assertHtml($expected, $result);
  4273. $result = $this->Form->select(
  4274. 'Model.multi_field',
  4275. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4276. ['multiple' => 'checkbox']
  4277. );
  4278. $expected = [
  4279. 'input' => [
  4280. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4281. ],
  4282. ['div' => ['class' => 'checkbox']],
  4283. ['label' => ['for' => 'model-multi-field-a-b']],
  4284. ['input' => [
  4285. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4286. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4287. ]],
  4288. 'first',
  4289. '/label',
  4290. '/div',
  4291. ['div' => ['class' => 'checkbox']],
  4292. ['label' => ['for' => 'model-multi-field-a-b1']],
  4293. ['input' => [
  4294. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4295. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4296. ]],
  4297. 'second',
  4298. '/label',
  4299. '/div',
  4300. ['div' => ['class' => 'checkbox']],
  4301. ['label' => ['for' => 'model-multi-field-a-b2']],
  4302. ['input' => [
  4303. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4304. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4305. ]],
  4306. 'third',
  4307. '/label',
  4308. '/div'
  4309. ];
  4310. $this->assertHtml($expected, $result);
  4311. }
  4312. /**
  4313. * Ensure that multiCheckbox reads from the request data.
  4314. *
  4315. * @return void
  4316. */
  4317. public function testSelectMultipleCheckboxRequestData()
  4318. {
  4319. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  4320. $result = $this->Form->select(
  4321. 'Model.tags',
  4322. ['1' => 'first', 'Array' => 'Array'],
  4323. ['multiple' => 'checkbox']
  4324. );
  4325. $expected = [
  4326. 'input' => [
  4327. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4328. ],
  4329. ['div' => ['class' => 'checkbox']],
  4330. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4331. ['input' => [
  4332. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4333. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4334. ]],
  4335. 'first',
  4336. '/label',
  4337. '/div',
  4338. ['div' => ['class' => 'checkbox']],
  4339. ['label' => ['for' => 'model-tags-array']],
  4340. ['input' => [
  4341. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4342. 'value' => 'Array', 'id' => 'model-tags-array'
  4343. ]],
  4344. 'Array',
  4345. '/label',
  4346. '/div'
  4347. ];
  4348. $this->assertHtml($expected, $result);
  4349. }
  4350. /**
  4351. * Checks the security hash array generated for multiple-input checkbox elements
  4352. *
  4353. * @return void
  4354. */
  4355. public function testSelectMultipleCheckboxSecurity()
  4356. {
  4357. $this->Form->request->params['_Token'] = 'testKey';
  4358. $this->assertEquals([], $this->Form->fields);
  4359. $result = $this->Form->select(
  4360. 'Model.multi_field',
  4361. ['1' => 'first', '2' => 'second', '3' => 'third'],
  4362. ['multiple' => 'checkbox']
  4363. );
  4364. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  4365. $result = $this->Form->secure($this->Form->fields);
  4366. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4367. $this->assertRegExp('/"' . $key . '"/', $result);
  4368. }
  4369. /**
  4370. * Multiple select elements should always be secured as they always participate
  4371. * in the POST data.
  4372. *
  4373. * @return void
  4374. */
  4375. public function testSelectMultipleSecureWithNoOptions()
  4376. {
  4377. $this->assertEquals([], $this->Form->fields);
  4378. $this->Form->select(
  4379. 'Model.select',
  4380. [],
  4381. ['multiple' => true]
  4382. );
  4383. $this->assertEquals(['Model.select'], $this->Form->fields);
  4384. }
  4385. /**
  4386. * When a select box has no options it should not be added to the fields list
  4387. * as it always fail post validation.
  4388. *
  4389. * @return void
  4390. */
  4391. public function testSelectNoSecureWithNoOptions()
  4392. {
  4393. $this->Form->request->params['_Token'] = 'testkey';
  4394. $this->assertEquals([], $this->Form->fields);
  4395. $this->Form->select(
  4396. 'Model.select',
  4397. []
  4398. );
  4399. $this->assertEquals([], $this->Form->fields);
  4400. $this->Form->select(
  4401. 'Model.user_id',
  4402. [],
  4403. ['empty' => true]
  4404. );
  4405. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  4406. }
  4407. /**
  4408. * testInputMultipleCheckboxes method
  4409. *
  4410. * test input() resulting in multi select elements being generated.
  4411. *
  4412. * @return void
  4413. */
  4414. public function testInputMultipleCheckboxes()
  4415. {
  4416. $result = $this->Form->input('Model.multi_field', [
  4417. 'options' => ['first', 'second', 'third'],
  4418. 'multiple' => 'checkbox'
  4419. ]);
  4420. $expected = [
  4421. ['div' => ['class' => 'input select']],
  4422. ['label' => ['for' => 'model-multi-field']],
  4423. 'Multi Field',
  4424. '/label',
  4425. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  4426. ['div' => ['class' => 'checkbox']],
  4427. ['label' => ['for' => 'model-multi-field-0']],
  4428. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  4429. 'first',
  4430. '/label',
  4431. '/div',
  4432. ['div' => ['class' => 'checkbox']],
  4433. ['label' => ['for' => 'model-multi-field-1']],
  4434. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  4435. 'second',
  4436. '/label',
  4437. '/div',
  4438. ['div' => ['class' => 'checkbox']],
  4439. ['label' => ['for' => 'model-multi-field-2']],
  4440. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  4441. 'third',
  4442. '/label',
  4443. '/div',
  4444. '/div'
  4445. ];
  4446. $this->assertHtml($expected, $result);
  4447. $result = $this->Form->input('Model.multi_field', [
  4448. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  4449. 'multiple' => 'checkbox'
  4450. ]);
  4451. $expected = [
  4452. ['div' => ['class' => 'input select']],
  4453. ['label' => ['for' => 'model-multi-field']],
  4454. 'Multi Field',
  4455. '/label',
  4456. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  4457. ['div' => ['class' => 'checkbox']],
  4458. ['label' => ['for' => 'model-multi-field-a']],
  4459. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  4460. 'first',
  4461. '/label',
  4462. '/div',
  4463. ['div' => ['class' => 'checkbox']],
  4464. ['label' => ['for' => 'model-multi-field-b']],
  4465. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  4466. 'second',
  4467. '/label',
  4468. '/div',
  4469. ['div' => ['class' => 'checkbox']],
  4470. ['label' => ['for' => 'model-multi-field-c']],
  4471. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  4472. 'third',
  4473. '/label',
  4474. '/div',
  4475. '/div'
  4476. ];
  4477. $this->assertHtml($expected, $result);
  4478. }
  4479. /**
  4480. * testSelectHiddenFieldOmission method
  4481. *
  4482. * test that select() with 'hiddenField' => false omits the hidden field
  4483. *
  4484. * @return void
  4485. */
  4486. public function testSelectHiddenFieldOmission()
  4487. {
  4488. $result = $this->Form->select(
  4489. 'Model.multi_field',
  4490. ['first', 'second'],
  4491. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  4492. );
  4493. $this->assertNotContains('type="hidden"', $result);
  4494. }
  4495. /**
  4496. * test that select() with multiple = checkbox works with overriding name attribute.
  4497. *
  4498. * @return void
  4499. */
  4500. public function testSelectCheckboxMultipleOverrideName()
  4501. {
  4502. $result = $this->Form->select('category', ['1', '2'], [
  4503. 'multiple' => 'checkbox',
  4504. 'name' => 'fish',
  4505. ]);
  4506. $expected = [
  4507. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  4508. ['div' => ['class' => 'checkbox']],
  4509. ['label' => ['for' => 'fish-0']],
  4510. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  4511. '1',
  4512. '/label',
  4513. '/div',
  4514. ['div' => ['class' => 'checkbox']],
  4515. ['label' => ['for' => 'fish-1']],
  4516. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  4517. '2',
  4518. '/label',
  4519. '/div'
  4520. ];
  4521. $this->assertHtml($expected, $result);
  4522. $result = $this->Form->multiCheckbox(
  4523. 'category',
  4524. new Collection(['1', '2']),
  4525. ['name' => 'fish']
  4526. );
  4527. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  4528. 'name' => 'fish',
  4529. ]);
  4530. $this->assertHtml($expected, $result);
  4531. }
  4532. /**
  4533. * Test that input() works with multicheckbox.
  4534. *
  4535. * @return void
  4536. */
  4537. public function testInputMultiCheckbox()
  4538. {
  4539. $result = $this->Form->input('category', [
  4540. 'type' => 'multicheckbox',
  4541. 'options' => ['1', '2'],
  4542. ]);
  4543. $expected = [
  4544. ['div' => ['class' => 'input multicheckbox']],
  4545. '<label',
  4546. 'Category',
  4547. '/label',
  4548. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  4549. ['div' => ['class' => 'checkbox']],
  4550. ['label' => ['for' => 'category-0']],
  4551. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  4552. '1',
  4553. '/label',
  4554. '/div',
  4555. ['div' => ['class' => 'checkbox']],
  4556. ['label' => ['for' => 'category-1']],
  4557. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  4558. '2',
  4559. '/label',
  4560. '/div',
  4561. '/div',
  4562. ];
  4563. $this->assertHtml($expected, $result);
  4564. }
  4565. /**
  4566. * testCheckbox method
  4567. *
  4568. * Test generation of checkboxes
  4569. *
  4570. * @return void
  4571. */
  4572. public function testCheckbox()
  4573. {
  4574. $result = $this->Form->checkbox('Model.field');
  4575. $expected = [
  4576. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  4577. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  4578. ];
  4579. $this->assertHtml($expected, $result);
  4580. $result = $this->Form->checkbox('Model.field', [
  4581. 'id' => 'theID',
  4582. 'value' => 'myvalue',
  4583. 'form' => 'my-form',
  4584. ]);
  4585. $expected = [
  4586. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  4587. ['input' => [
  4588. 'type' => 'checkbox', 'name' => 'Model[field]',
  4589. 'value' => 'myvalue', 'id' => 'theID',
  4590. 'form' => 'my-form',
  4591. ]]
  4592. ];
  4593. $this->assertHtml($expected, $result);
  4594. }
  4595. /**
  4596. * testCheckboxDefaultValue method
  4597. *
  4598. * Test default value setting on checkbox() method
  4599. *
  4600. * @return void
  4601. */
  4602. public function testCheckboxDefaultValue()
  4603. {
  4604. $this->Form->request->data['Model']['field'] = false;
  4605. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  4606. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  4607. $this->assertHtml($expected, $result);
  4608. unset($this->Form->request->data['Model']['field']);
  4609. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  4610. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  4611. $this->assertHtml($expected, $result);
  4612. $this->Form->request->data['Model']['field'] = true;
  4613. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  4614. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  4615. $this->assertHtml($expected, $result);
  4616. unset($this->Form->request->data['Model']['field']);
  4617. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  4618. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  4619. $this->assertHtml($expected, $result);
  4620. }
  4621. /**
  4622. * Test checkbox being checked or having errors.
  4623. *
  4624. * @return void
  4625. */
  4626. public function testCheckboxCheckedAndError()
  4627. {
  4628. $this->article['errors'] = [
  4629. 'published' => true
  4630. ];
  4631. $this->Form->request->data['published'] = 'myvalue';
  4632. $this->Form->create($this->article);
  4633. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  4634. $expected = [
  4635. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  4636. ['input' => [
  4637. 'type' => 'checkbox',
  4638. 'name' => 'published',
  4639. 'value' => 'myvalue',
  4640. 'id' => 'theID',
  4641. 'checked' => 'checked',
  4642. 'class' => 'form-error'
  4643. ]]
  4644. ];
  4645. $this->assertHtml($expected, $result);
  4646. $this->Form->request->data['published'] = '';
  4647. $result = $this->Form->checkbox('published');
  4648. $expected = [
  4649. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  4650. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  4651. ];
  4652. $this->assertHtml($expected, $result);
  4653. }
  4654. /**
  4655. * test checkbox() with a custom name attribute
  4656. *
  4657. * @return void
  4658. */
  4659. public function testCheckboxCustomNameAttribute()
  4660. {
  4661. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  4662. $expected = [
  4663. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  4664. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  4665. ];
  4666. $this->assertHtml($expected, $result);
  4667. }
  4668. /**
  4669. * Test that the hidden input for checkboxes can be omitted or set to a
  4670. * specific value.
  4671. *
  4672. * @return void
  4673. */
  4674. public function testCheckboxHiddenField()
  4675. {
  4676. $result = $this->Form->checkbox('UserForm.something', [
  4677. 'hiddenField' => false
  4678. ]);
  4679. $expected = [
  4680. 'input' => [
  4681. 'type' => 'checkbox',
  4682. 'name' => 'UserForm[something]',
  4683. 'value' => '1'
  4684. ],
  4685. ];
  4686. $this->assertHtml($expected, $result);
  4687. $result = $this->Form->checkbox('UserForm.something', [
  4688. 'value' => 'Y',
  4689. 'hiddenField' => 'N',
  4690. ]);
  4691. $expected = [
  4692. ['input' => [
  4693. 'type' => 'hidden', 'name' => 'UserForm[something]',
  4694. 'value' => 'N'
  4695. ]],
  4696. ['input' => [
  4697. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  4698. 'value' => 'Y'
  4699. ]],
  4700. ];
  4701. $this->assertHtml($expected, $result);
  4702. }
  4703. /**
  4704. * Test the time type.
  4705. *
  4706. * @return void
  4707. */
  4708. public function testTime()
  4709. {
  4710. $result = $this->Form->time('start_time', [
  4711. 'timeFormat' => 12,
  4712. 'interval' => 5,
  4713. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  4714. ]);
  4715. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  4716. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4717. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  4718. $this->assertNotContains('year', $result);
  4719. $this->assertNotContains('month', $result);
  4720. $this->assertNotContains('day', $result);
  4721. $result = $this->Form->time('start_time', [
  4722. 'timeFormat' => 12,
  4723. 'interval' => 5,
  4724. 'value' => '2014-03-08 16:30:00'
  4725. ]);
  4726. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  4727. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4728. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  4729. $this->assertNotContains('year', $result);
  4730. $this->assertNotContains('month', $result);
  4731. $this->assertNotContains('day', $result);
  4732. }
  4733. /**
  4734. * Ensure that timeFormat=24 has no merdian.
  4735. *
  4736. * @return void.
  4737. */
  4738. public function testTimeFormat24NoMeridian()
  4739. {
  4740. $result = $this->Form->time('start_time', [
  4741. 'timeFormat' => 24,
  4742. 'interval' => 5,
  4743. 'value' => '2014-03-08 16:30:00'
  4744. ]);
  4745. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  4746. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4747. $this->assertNotContains('meridian', $result);
  4748. $this->assertNotContains('pm', $result);
  4749. $this->assertNotContains('year', $result);
  4750. $this->assertNotContains('month', $result);
  4751. $this->assertNotContains('day', $result);
  4752. }
  4753. /**
  4754. * Test the date type.
  4755. *
  4756. * @return void
  4757. */
  4758. public function testDate()
  4759. {
  4760. $result = $this->Form->date('start_day', [
  4761. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  4762. ]);
  4763. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  4764. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  4765. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  4766. $this->assertNotContains('hour', $result);
  4767. $this->assertNotContains('minute', $result);
  4768. $this->assertNotContains('second', $result);
  4769. $this->assertNotContains('meridian', $result);
  4770. }
  4771. /**
  4772. * testDateTime method
  4773. *
  4774. * Test generation of date/time select elements
  4775. *
  4776. * @return void
  4777. */
  4778. public function testDateTime()
  4779. {
  4780. extract($this->dateRegex);
  4781. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  4782. $now = strtotime('now');
  4783. $expected = [
  4784. ['select' => ['name' => 'Contact[date][year]']],
  4785. ['option' => ['value' => '']],
  4786. '/option',
  4787. $yearsRegex,
  4788. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  4789. date('Y', $now),
  4790. '/option',
  4791. '*/select',
  4792. ['select' => ['name' => 'Contact[date][month]']],
  4793. ['option' => ['value' => '']],
  4794. '/option',
  4795. $monthsRegex,
  4796. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  4797. date('F', $now),
  4798. '/option',
  4799. '*/select',
  4800. ['select' => ['name' => 'Contact[date][day]']],
  4801. ['option' => ['value' => '']],
  4802. '/option',
  4803. $daysRegex,
  4804. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  4805. date('j', $now),
  4806. '/option',
  4807. '*/select',
  4808. ['select' => ['name' => 'Contact[date][hour]']],
  4809. ['option' => ['value' => '']],
  4810. '/option',
  4811. $hoursRegex,
  4812. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  4813. date('G', $now),
  4814. '/option',
  4815. '*/select',
  4816. ['select' => ['name' => 'Contact[date][minute]']],
  4817. ['option' => ['value' => '']],
  4818. '/option',
  4819. $minutesRegex,
  4820. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  4821. date('i', $now),
  4822. '/option',
  4823. '*/select',
  4824. ];
  4825. $this->assertHtml($expected, $result);
  4826. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  4827. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  4828. $now = strtotime('now');
  4829. $expected = [
  4830. ['select' => ['name' => 'Contact[date][year]']],
  4831. $yearsRegex,
  4832. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  4833. date('Y', $now),
  4834. '/option',
  4835. '*/select',
  4836. ['select' => ['name' => 'Contact[date][month]']],
  4837. $monthsRegex,
  4838. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  4839. date('F', $now),
  4840. '/option',
  4841. '*/select',
  4842. ['select' => ['name' => 'Contact[date][day]']],
  4843. $daysRegex,
  4844. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  4845. date('j', $now),
  4846. '/option',
  4847. '*/select',
  4848. ['select' => ['name' => 'Contact[date][hour]']],
  4849. $hoursRegex,
  4850. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  4851. date('G', $now),
  4852. '/option',
  4853. '*/select',
  4854. ['select' => ['name' => 'Contact[date][minute]']],
  4855. $minutesRegex,
  4856. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  4857. date('i', $now),
  4858. '/option',
  4859. '*/select',
  4860. ];
  4861. $this->assertHtml($expected, $result);
  4862. }
  4863. /**
  4864. * Test that datetime fields are added to protected fields list.
  4865. *
  4866. * @return void
  4867. */
  4868. public function testDateTimeSecured()
  4869. {
  4870. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4871. $this->Form->dateTime('Contact.date');
  4872. $expected = [
  4873. 'Contact.date.year',
  4874. 'Contact.date.month',
  4875. 'Contact.date.day',
  4876. 'Contact.date.hour',
  4877. 'Contact.date.minute',
  4878. ];
  4879. $this->assertEquals($expected, $this->Form->fields);
  4880. $this->Form->fields = [];
  4881. $this->Form->date('Contact.published');
  4882. $expected = [
  4883. 'Contact.published.year',
  4884. 'Contact.published.month',
  4885. 'Contact.published.day',
  4886. ];
  4887. $this->assertEquals($expected, $this->Form->fields);
  4888. }
  4889. /**
  4890. * Test that datetime fields are added to protected fields list.
  4891. *
  4892. * @return void
  4893. */
  4894. public function testDateTimeSecuredDisabled()
  4895. {
  4896. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4897. $this->Form->dateTime('Contact.date', ['secure' => false]);
  4898. $expected = [];
  4899. $this->assertEquals($expected, $this->Form->fields);
  4900. $this->Form->fields = [];
  4901. $this->Form->date('Contact.published', ['secure' => false]);
  4902. $expected = [];
  4903. $this->assertEquals($expected, $this->Form->fields);
  4904. }
  4905. /**
  4906. * Test empty defaulting to true for datetime.
  4907. *
  4908. * @return void
  4909. */
  4910. public function testDatetimeEmpty()
  4911. {
  4912. extract($this->dateRegex);
  4913. $now = strtotime('now');
  4914. $result = $this->Form->dateTime('Contact.date', [
  4915. 'timeFormat' => 12,
  4916. 'empty' => true,
  4917. 'default' => true
  4918. ]);
  4919. $expected = [
  4920. ['select' => ['name' => 'Contact[date][year]']],
  4921. $yearsRegex,
  4922. ['option' => ['value' => '']],
  4923. '/option',
  4924. '*/select',
  4925. ['select' => ['name' => 'Contact[date][month]']],
  4926. $monthsRegex,
  4927. ['option' => ['value' => '']],
  4928. '/option',
  4929. '*/select',
  4930. ['select' => ['name' => 'Contact[date][day]']],
  4931. $daysRegex,
  4932. ['option' => ['value' => '']],
  4933. '/option',
  4934. '*/select',
  4935. ['select' => ['name' => 'Contact[date][hour]']],
  4936. $hoursRegex,
  4937. ['option' => ['value' => '']],
  4938. '/option',
  4939. '*/select',
  4940. ['select' => ['name' => 'Contact[date][minute]']],
  4941. $minutesRegex,
  4942. ['option' => ['value' => '']],
  4943. '/option',
  4944. '*/select',
  4945. ['select' => ['name' => 'Contact[date][meridian]']],
  4946. $meridianRegex,
  4947. ['option' => ['value' => '']],
  4948. '/option',
  4949. '*/select'
  4950. ];
  4951. $this->assertHtml($expected, $result);
  4952. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4953. }
  4954. /**
  4955. * Test datetime with interval option.
  4956. *
  4957. * @return void
  4958. */
  4959. public function testDatetimeMinuteInterval()
  4960. {
  4961. extract($this->dateRegex);
  4962. $now = strtotime('now');
  4963. $result = $this->Form->dateTime('Contact.date', [
  4964. 'interval' => 5,
  4965. 'value' => ''
  4966. ]);
  4967. $expected = [
  4968. ['select' => ['name' => 'Contact[date][year]']],
  4969. $yearsRegex,
  4970. ['option' => ['selected' => 'selected', 'value' => '']],
  4971. '/option',
  4972. '*/select',
  4973. ['select' => ['name' => 'Contact[date][month]']],
  4974. $monthsRegex,
  4975. ['option' => ['selected' => 'selected', 'value' => '']],
  4976. '/option',
  4977. '*/select',
  4978. ['select' => ['name' => 'Contact[date][day]']],
  4979. $daysRegex,
  4980. ['option' => ['selected' => 'selected', 'value' => '']],
  4981. '/option',
  4982. '*/select',
  4983. ['select' => ['name' => 'Contact[date][hour]']],
  4984. $hoursRegex,
  4985. ['option' => ['selected' => 'selected', 'value' => '']],
  4986. '/option',
  4987. '*/select',
  4988. ['select' => ['name' => 'Contact[date][minute]']],
  4989. $minutesRegex,
  4990. ['option' => ['selected' => 'selected', 'value' => '']],
  4991. '/option',
  4992. ['option' => ['value' => '00']],
  4993. '00',
  4994. '/option',
  4995. ['option' => ['value' => '05']],
  4996. '05',
  4997. '/option',
  4998. ['option' => ['value' => '10']],
  4999. '10',
  5000. '/option',
  5001. '*/select',
  5002. ];
  5003. $this->assertHtml($expected, $result);
  5004. }
  5005. /**
  5006. * Test dateTime with rounding
  5007. *
  5008. * @return void
  5009. */
  5010. public function testDateTimeRounding()
  5011. {
  5012. $this->Form->request->data['Contact'] = [
  5013. 'date' => [
  5014. 'day' => '13',
  5015. 'month' => '12',
  5016. 'year' => '2010',
  5017. 'hour' => '04',
  5018. 'minute' => '19',
  5019. 'meridian' => 'AM'
  5020. ]
  5021. ];
  5022. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5023. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5024. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5025. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5026. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5027. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5028. }
  5029. /**
  5030. * test that datetime() and default values work.
  5031. *
  5032. * @return void
  5033. */
  5034. public function testDatetimeWithDefault()
  5035. {
  5036. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5037. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5038. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5039. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5040. $result = $this->Form->dateTime('Contact.updated', [
  5041. 'default' => '2009-06-01 11:15:30'
  5042. ]);
  5043. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5044. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5045. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5046. }
  5047. /**
  5048. * testDateTime all zeros
  5049. *
  5050. * @return void
  5051. */
  5052. public function testDateTimeAllZeros()
  5053. {
  5054. $result = $this->Form->dateTime('Contact.date', [
  5055. 'timeFormat' => false,
  5056. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5057. 'value' => '0000-00-00'
  5058. ]);
  5059. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5060. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5061. }
  5062. /**
  5063. * testDateTimeEmptyAsArray
  5064. *
  5065. * @return void
  5066. */
  5067. public function testDateTimeEmptyAsArray()
  5068. {
  5069. $result = $this->Form->dateTime('Contact.date', [
  5070. 'empty' => [
  5071. 'day' => 'DAY',
  5072. 'month' => 'MONTH',
  5073. 'year' => 'YEAR',
  5074. 'hour' => 'HOUR',
  5075. 'minute' => 'MINUTE',
  5076. 'meridian' => false
  5077. ],
  5078. 'default' => true
  5079. ]);
  5080. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5081. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5082. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5083. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5084. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5085. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5086. $result = $this->Form->dateTime('Contact.date', [
  5087. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5088. 'default' => true
  5089. ]);
  5090. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5091. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5092. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5093. }
  5094. /**
  5095. * testFormDateTimeMulti method
  5096. *
  5097. * test multiple datetime element generation
  5098. *
  5099. * @return void
  5100. */
  5101. public function testFormDateTimeMulti()
  5102. {
  5103. extract($this->dateRegex);
  5104. $result = $this->Form->dateTime('Contact.1.updated');
  5105. $this->assertContains('Contact[1][updated][month]', $result);
  5106. $this->assertContains('Contact[1][updated][day]', $result);
  5107. $this->assertContains('Contact[1][updated][year]', $result);
  5108. $this->assertContains('Contact[1][updated][hour]', $result);
  5109. $this->assertContains('Contact[1][updated][minute]', $result);
  5110. }
  5111. /**
  5112. * When changing the date format, the label should always focus the first select box when
  5113. * clicked.
  5114. *
  5115. * @return void
  5116. */
  5117. public function testDateTimeLabelIdMatchesFirstInput()
  5118. {
  5119. $result = $this->Form->input('Model.date', ['type' => 'date']);
  5120. $this->assertContains('<label>Date</label>', $result);
  5121. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5122. $this->assertContains('<label>Date</label>', $result);
  5123. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5124. $this->assertContains('<label>Date</label>', $result);
  5125. }
  5126. /**
  5127. * test datetime second=true
  5128. *
  5129. * @return void
  5130. */
  5131. public function testDateTimeSecondOptions()
  5132. {
  5133. $result = $this->Form->dateTime('updated', ['second' => true]);
  5134. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5135. $result = $this->Form->dateTime('updated', ['second' => []]);
  5136. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5137. $result = $this->Form->dateTime('updated', ['second' => null]);
  5138. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5139. $result = $this->Form->dateTime('updated', ['second' => false]);
  5140. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5141. }
  5142. /**
  5143. * testMonth method
  5144. *
  5145. * @return void
  5146. */
  5147. public function testMonth()
  5148. {
  5149. $result = $this->Form->month('Model.field', ['value' => '']);
  5150. $expected = [
  5151. ['select' => ['name' => 'Model[field][month]']],
  5152. ['option' => ['value' => '', 'selected' => 'selected']],
  5153. '/option',
  5154. ['option' => ['value' => '01']],
  5155. date('F', strtotime('2008-01-01 00:00:00')),
  5156. '/option',
  5157. ['option' => ['value' => '02']],
  5158. date('F', strtotime('2008-02-01 00:00:00')),
  5159. '/option',
  5160. '*/select',
  5161. ];
  5162. $this->assertHtml($expected, $result);
  5163. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5164. $expected = [
  5165. ['select' => ['name' => 'Model[field][month]']],
  5166. ['option' => ['selected' => 'selected', 'value' => '']],
  5167. '/option',
  5168. ['option' => ['value' => '01']],
  5169. date('F', strtotime('2008-01-01 00:00:00')),
  5170. '/option',
  5171. ['option' => ['value' => '02']],
  5172. date('F', strtotime('2008-02-01 00:00:00')),
  5173. '/option',
  5174. '*/select',
  5175. ];
  5176. $this->assertHtml($expected, $result);
  5177. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5178. $expected = [
  5179. ['select' => ['name' => 'Model[field][month]']],
  5180. ['option' => ['selected' => 'selected', 'value' => '']],
  5181. '/option',
  5182. ['option' => ['value' => '01']],
  5183. '1',
  5184. '/option',
  5185. ['option' => ['value' => '02']],
  5186. '2',
  5187. '/option',
  5188. '*/select',
  5189. ];
  5190. $this->assertHtml($expected, $result);
  5191. $monthNames = [
  5192. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5193. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5194. ];
  5195. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5196. $expected = [
  5197. ['select' => ['name' => 'Model[field][month]']],
  5198. ['option' => ['value' => '']],
  5199. '/option',
  5200. ['option' => ['value' => '01', 'selected' => 'selected']],
  5201. 'Jan',
  5202. '/option',
  5203. ['option' => ['value' => '02']],
  5204. 'Feb',
  5205. '/option',
  5206. '*/select',
  5207. ];
  5208. $this->assertHtml($expected, $result);
  5209. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5210. $result = $this->Form->month('Project.release');
  5211. $expected = [
  5212. ['select' => ['name' => 'Project[release][month]']],
  5213. ['option' => ['value' => '']],
  5214. '/option',
  5215. ['option' => ['value' => '01']],
  5216. 'January',
  5217. '/option',
  5218. ['option' => ['value' => '02', 'selected' => 'selected']],
  5219. 'February',
  5220. '/option',
  5221. '*/select',
  5222. ];
  5223. $this->assertHtml($expected, $result);
  5224. $result = $this->Form->month('Contact.published', [
  5225. 'empty' => 'Published on',
  5226. ]);
  5227. $this->assertContains('Published on', $result);
  5228. }
  5229. /**
  5230. * testDay method
  5231. *
  5232. * @return void
  5233. */
  5234. public function testDay()
  5235. {
  5236. extract($this->dateRegex);
  5237. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  5238. $expected = [
  5239. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  5240. ['option' => ['selected' => 'selected', 'value' => '']],
  5241. '/option',
  5242. ['option' => ['value' => '01']],
  5243. '1',
  5244. '/option',
  5245. ['option' => ['value' => '02']],
  5246. '2',
  5247. '/option',
  5248. $daysRegex,
  5249. '/select',
  5250. ];
  5251. $this->assertHtml($expected, $result);
  5252. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5253. $result = $this->Form->day('Model.field');
  5254. $expected = [
  5255. ['select' => ['name' => 'Model[field][day]']],
  5256. ['option' => ['value' => '']],
  5257. '/option',
  5258. ['option' => ['value' => '01']],
  5259. '1',
  5260. '/option',
  5261. ['option' => ['value' => '02']],
  5262. '2',
  5263. '/option',
  5264. $daysRegex,
  5265. ['option' => ['value' => '10', 'selected' => 'selected']],
  5266. '10',
  5267. '/option',
  5268. $daysRegex,
  5269. '/select',
  5270. ];
  5271. $this->assertHtml($expected, $result);
  5272. $this->Form->request->data['Model']['field'] = '';
  5273. $result = $this->Form->day('Model.field', ['value' => '10']);
  5274. $expected = [
  5275. ['select' => ['name' => 'Model[field][day]']],
  5276. ['option' => ['value' => '']],
  5277. '/option',
  5278. ['option' => ['value' => '01']],
  5279. '1',
  5280. '/option',
  5281. ['option' => ['value' => '02']],
  5282. '2',
  5283. '/option',
  5284. $daysRegex,
  5285. ['option' => ['value' => '10', 'selected' => 'selected']],
  5286. '10',
  5287. '/option',
  5288. $daysRegex,
  5289. '/select',
  5290. ];
  5291. $this->assertHtml($expected, $result);
  5292. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5293. $result = $this->Form->day('Project.release');
  5294. $expected = [
  5295. ['select' => ['name' => 'Project[release][day]']],
  5296. ['option' => ['value' => '']],
  5297. '/option',
  5298. ['option' => ['value' => '01']],
  5299. '1',
  5300. '/option',
  5301. ['option' => ['value' => '02']],
  5302. '2',
  5303. '/option',
  5304. $daysRegex,
  5305. ['option' => ['value' => '10', 'selected' => 'selected']],
  5306. '10',
  5307. '/option',
  5308. $daysRegex,
  5309. '/select',
  5310. ];
  5311. $this->assertHtml($expected, $result);
  5312. $result = $this->Form->day('Contact.published', [
  5313. 'empty' => 'Published on',
  5314. ]);
  5315. $this->assertContains('Published on', $result);
  5316. }
  5317. /**
  5318. * testMinute method
  5319. *
  5320. * @return void
  5321. */
  5322. public function testMinute()
  5323. {
  5324. extract($this->dateRegex);
  5325. $result = $this->Form->minute('Model.field', ['value' => '']);
  5326. $expected = [
  5327. ['select' => ['name' => 'Model[field][minute]']],
  5328. ['option' => ['selected' => 'selected', 'value' => '']],
  5329. '/option',
  5330. ['option' => ['value' => '00']],
  5331. '00',
  5332. '/option',
  5333. ['option' => ['value' => '01']],
  5334. '01',
  5335. '/option',
  5336. ['option' => ['value' => '02']],
  5337. '02',
  5338. '/option',
  5339. $minutesRegex,
  5340. '/select',
  5341. ];
  5342. $this->assertHtml($expected, $result);
  5343. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5344. $result = $this->Form->minute('Model.field');
  5345. $expected = [
  5346. ['select' => ['name' => 'Model[field][minute]']],
  5347. ['option' => ['value' => '']],
  5348. '/option',
  5349. ['option' => ['value' => '00']],
  5350. '00',
  5351. '/option',
  5352. ['option' => ['value' => '01']],
  5353. '01',
  5354. '/option',
  5355. ['option' => ['value' => '02']],
  5356. '02',
  5357. '/option',
  5358. $minutesRegex,
  5359. ['option' => ['value' => '12', 'selected' => 'selected']],
  5360. '12',
  5361. '/option',
  5362. $minutesRegex,
  5363. '/select',
  5364. ];
  5365. $this->assertHtml($expected, $result);
  5366. $this->Form->request->data['Model']['field'] = '';
  5367. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5368. $expected = [
  5369. ['select' => ['name' => 'Model[field][minute]']],
  5370. ['option' => ['selected' => 'selected', 'value' => '']],
  5371. '/option',
  5372. ['option' => ['value' => '00']],
  5373. '00',
  5374. '/option',
  5375. ['option' => ['value' => '05']],
  5376. '05',
  5377. '/option',
  5378. ['option' => ['value' => '10']],
  5379. '10',
  5380. '/option',
  5381. $minutesRegex,
  5382. '/select',
  5383. ];
  5384. $this->assertHtml($expected, $result);
  5385. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5386. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5387. $expected = [
  5388. ['select' => ['name' => 'Model[field][minute]']],
  5389. ['option' => ['value' => '']],
  5390. '/option',
  5391. ['option' => ['value' => '00']],
  5392. '00',
  5393. '/option',
  5394. ['option' => ['value' => '05']],
  5395. '05',
  5396. '/option',
  5397. ['option' => ['value' => '10', 'selected' => 'selected']],
  5398. '10',
  5399. '/option',
  5400. $minutesRegex,
  5401. '/select',
  5402. ];
  5403. $this->assertHtml($expected, $result);
  5404. }
  5405. /**
  5406. * Test generating an input for the meridian.
  5407. *
  5408. * @return void
  5409. */
  5410. public function testMeridian()
  5411. {
  5412. extract($this->dateRegex);
  5413. $now = new \DateTime();
  5414. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  5415. $expected = [
  5416. ['select' => ['name' => 'Model[field][meridian]']],
  5417. ['option' => ['value' => '']],
  5418. '/option',
  5419. $meridianRegex,
  5420. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  5421. $now->format('a'),
  5422. '/option',
  5423. '*/select'
  5424. ];
  5425. $this->assertHtml($expected, $result);
  5426. }
  5427. /**
  5428. * testHour method
  5429. *
  5430. * @return void
  5431. */
  5432. public function testHour()
  5433. {
  5434. extract($this->dateRegex);
  5435. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  5436. $expected = [
  5437. ['select' => ['name' => 'Model[field][hour]']],
  5438. ['option' => ['selected' => 'selected', 'value' => '']],
  5439. '/option',
  5440. ['option' => ['value' => '01']],
  5441. '1',
  5442. '/option',
  5443. ['option' => ['value' => '02']],
  5444. '2',
  5445. '/option',
  5446. $hoursRegex,
  5447. '/select',
  5448. ];
  5449. $this->assertHtml($expected, $result);
  5450. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5451. $result = $this->Form->hour('Model.field', ['format' => 12]);
  5452. $expected = [
  5453. ['select' => ['name' => 'Model[field][hour]']],
  5454. ['option' => ['value' => '']],
  5455. '/option',
  5456. ['option' => ['value' => '01']],
  5457. '1',
  5458. '/option',
  5459. ['option' => ['value' => '02']],
  5460. '2',
  5461. '/option',
  5462. $hoursRegex,
  5463. ['option' => ['value' => '12', 'selected' => 'selected']],
  5464. '12',
  5465. '/option',
  5466. '/select',
  5467. ];
  5468. $this->assertHtml($expected, $result);
  5469. $this->Form->request->data['Model']['field'] = '';
  5470. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  5471. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5472. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  5473. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5474. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5475. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5476. $expected = [
  5477. ['select' => ['name' => 'Model[field][hour]']],
  5478. ['option' => ['value' => '']],
  5479. '/option',
  5480. ['option' => ['value' => '00', 'selected' => 'selected']],
  5481. '0',
  5482. '/option',
  5483. ['option' => ['value' => '01']],
  5484. '1',
  5485. '/option',
  5486. ['option' => ['value' => '02']],
  5487. '2',
  5488. '/option',
  5489. $hoursRegex,
  5490. '/select',
  5491. ];
  5492. $this->assertHtml($expected, $result);
  5493. unset($this->Form->request->data['Model']['field']);
  5494. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  5495. $thisHour = date('H');
  5496. $optValue = date('G');
  5497. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5498. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5499. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5500. $expected = [
  5501. ['select' => ['name' => 'Model[field][hour]']],
  5502. ['option' => ['value' => '']],
  5503. '/option',
  5504. ['option' => ['value' => '00']],
  5505. '0',
  5506. '/option',
  5507. ['option' => ['value' => '01', 'selected' => 'selected']],
  5508. '1',
  5509. '/option',
  5510. ['option' => ['value' => '02']],
  5511. '2',
  5512. '/option',
  5513. $hoursRegex,
  5514. '/select',
  5515. ];
  5516. $this->assertHtml($expected, $result);
  5517. }
  5518. /**
  5519. * testYear method
  5520. *
  5521. * @return void
  5522. */
  5523. public function testYear()
  5524. {
  5525. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  5526. $expected = [
  5527. ['select' => ['name' => 'Model[field][year]']],
  5528. ['option' => ['selected' => 'selected', 'value' => '']],
  5529. '/option',
  5530. ['option' => ['value' => '2007']],
  5531. '2007',
  5532. '/option',
  5533. ['option' => ['value' => '2006']],
  5534. '2006',
  5535. '/option',
  5536. '/select',
  5537. ];
  5538. $this->assertHtml($expected, $result);
  5539. $result = $this->Form->year('Model.field', [
  5540. 'value' => '',
  5541. 'minYear' => 2006,
  5542. 'maxYear' => 2007,
  5543. 'orderYear' => 'asc'
  5544. ]);
  5545. $expected = [
  5546. ['select' => ['name' => 'Model[field][year]']],
  5547. ['option' => ['selected' => 'selected', 'value' => '']],
  5548. '/option',
  5549. ['option' => ['value' => '2006']],
  5550. '2006',
  5551. '/option',
  5552. ['option' => ['value' => '2007']],
  5553. '2007',
  5554. '/option',
  5555. '/select',
  5556. ];
  5557. $this->assertHtml($expected, $result);
  5558. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5559. $result = $this->Form->year('Contact.published', [
  5560. 'empty' => false,
  5561. 'minYear' => 2006,
  5562. 'maxYear' => 2007,
  5563. ]);
  5564. $expected = [
  5565. ['select' => ['name' => 'Contact[published][year]']],
  5566. ['option' => ['value' => '2007']],
  5567. '2007',
  5568. '/option',
  5569. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5570. '2006',
  5571. '/option',
  5572. '/select',
  5573. ];
  5574. $this->assertHtml($expected, $result);
  5575. $result = $this->Form->year('Contact.published', [
  5576. 'empty' => 'Published on',
  5577. ]);
  5578. $this->assertContains('Published on', $result);
  5579. }
  5580. /**
  5581. * Test minYear being prior to the unix epoch
  5582. *
  5583. * @return void
  5584. */
  5585. public function testInputDatetimePreEpoch()
  5586. {
  5587. $start = date('Y') - 80;
  5588. $end = date('Y') - 18;
  5589. $result = $this->Form->input('birth_year', [
  5590. 'type' => 'date',
  5591. 'label' => 'Birth Year',
  5592. 'minYear' => $start,
  5593. 'maxYear' => $end,
  5594. 'month' => false,
  5595. 'day' => false,
  5596. ]);
  5597. $this->assertContains('value="' . $start . '">' . $start, $result);
  5598. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  5599. $this->assertNotContains('value="00">00', $result);
  5600. }
  5601. /**
  5602. * testYearAutoExpandRange method
  5603. *
  5604. * @return void
  5605. */
  5606. public function testYearAutoExpandRange()
  5607. {
  5608. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  5609. $result = $this->Form->year('User.birthday');
  5610. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5611. $result = $matches[1];
  5612. $expected = range(date('Y') + 5, 1930);
  5613. $this->assertEquals($expected, $result);
  5614. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5615. $result = $this->Form->year('Project.release');
  5616. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5617. $result = $matches[1];
  5618. $expected = range(2050, date('Y') - 5);
  5619. $this->assertEquals($expected, $result);
  5620. $this->Form->request->data['Project']['release'] = '1881-10-10';
  5621. $result = $this->Form->year('Project.release', [
  5622. 'minYear' => 1890,
  5623. 'maxYear' => 1900
  5624. ]);
  5625. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5626. $result = $matches[1];
  5627. $expected = range(1900, 1881);
  5628. $this->assertEquals($expected, $result);
  5629. }
  5630. /**
  5631. * Test that input() accepts the type of date and passes options in.
  5632. *
  5633. * @return void
  5634. */
  5635. public function testInputDate()
  5636. {
  5637. $this->Form->request->data = [
  5638. 'month_year' => ['month' => date('m')],
  5639. ];
  5640. $this->Form->create($this->article);
  5641. $result = $this->Form->input('month_year', [
  5642. 'label' => false,
  5643. 'type' => 'date',
  5644. 'minYear' => 2006,
  5645. 'maxYear' => 2008
  5646. ]);
  5647. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  5648. $this->assertNotContains('value="2008" selected="selected"', $result);
  5649. }
  5650. /**
  5651. * Test the label option being set to false.
  5652. *
  5653. * @return void
  5654. */
  5655. public function testInputLabelFalse()
  5656. {
  5657. $this->Form->create($this->article);
  5658. $result = $this->Form->input('title', ['label' => false]);
  5659. $expected = [
  5660. 'div' => ['class' => 'input text required'],
  5661. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  5662. '/div'
  5663. ];
  5664. $this->assertHtml($expected, $result);
  5665. }
  5666. /**
  5667. * testInputDateMaxYear method
  5668. *
  5669. * Let's say we want to only allow users born from 2006 to 2008 to register
  5670. * This being the first signup page, we still don't have any data
  5671. *
  5672. * @return void
  5673. */
  5674. public function testInputDateMaxYear()
  5675. {
  5676. $this->Form->request->data = [];
  5677. $this->Form->create($this->article);
  5678. $result = $this->Form->input('birthday', [
  5679. 'label' => false,
  5680. 'type' => 'date',
  5681. 'minYear' => 2006,
  5682. 'maxYear' => 2008,
  5683. 'default' => true
  5684. ]);
  5685. $this->assertContains('value="2008" selected="selected"', $result);
  5686. $this->assertContains('value="2006"', $result);
  5687. $this->assertNotContains('value="2005"', $result);
  5688. $this->assertNotContains('value="2009"', $result);
  5689. }
  5690. /**
  5691. * testTextArea method
  5692. *
  5693. * @return void
  5694. */
  5695. public function testTextArea()
  5696. {
  5697. $this->Form->request->data = ['field' => 'some test data'];
  5698. $result = $this->Form->textarea('field');
  5699. $expected = [
  5700. 'textarea' => ['name' => 'field', 'rows' => 5],
  5701. 'some test data',
  5702. '/textarea',
  5703. ];
  5704. $this->assertHtml($expected, $result);
  5705. $result = $this->Form->textarea('user.bio');
  5706. $expected = [
  5707. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  5708. '/textarea',
  5709. ];
  5710. $this->assertHtml($expected, $result);
  5711. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  5712. $result = $this->Form->textarea('field');
  5713. $expected = [
  5714. 'textarea' => ['name' => 'field', 'rows' => 5],
  5715. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5716. '/textarea',
  5717. ];
  5718. $this->assertHtml($expected, $result);
  5719. $this->Form->request->data = [
  5720. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  5721. ];
  5722. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5723. $expected = [
  5724. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  5725. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5726. '/textarea',
  5727. ];
  5728. $this->assertHtml($expected, $result);
  5729. $result = $this->Form->textarea('0.OtherModel.field');
  5730. $expected = [
  5731. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  5732. '/textarea'
  5733. ];
  5734. $this->assertHtml($expected, $result);
  5735. }
  5736. /**
  5737. * testTextAreaWithStupidCharacters method
  5738. *
  5739. * test text area with non-ascii characters
  5740. *
  5741. * @return void
  5742. */
  5743. public function testTextAreaWithStupidCharacters()
  5744. {
  5745. $result = $this->Form->textarea('Post.content', [
  5746. 'value' => "GREAT®",
  5747. 'rows' => '15',
  5748. 'cols' => '75'
  5749. ]);
  5750. $expected = [
  5751. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5752. 'GREAT®',
  5753. '/textarea',
  5754. ];
  5755. $this->assertHtml($expected, $result);
  5756. }
  5757. /**
  5758. * Test textareas waxlength read from schema.
  5759. *
  5760. * @return void
  5761. */
  5762. public function testTextAreaMaxLength()
  5763. {
  5764. $this->Form->create([
  5765. 'schema' => [
  5766. 'stuff' => ['type' => 'string', 'length' => 10],
  5767. ]
  5768. ]);
  5769. $result = $this->Form->input('other', ['type' => 'textarea']);
  5770. $expected = [
  5771. 'div' => ['class' => 'input textarea'],
  5772. 'label' => ['for' => 'other'],
  5773. 'Other',
  5774. '/label',
  5775. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  5776. '/textarea',
  5777. '/div'
  5778. ];
  5779. $this->assertHtml($expected, $result);
  5780. $result = $this->Form->input('stuff', ['type' => 'textarea']);
  5781. $expected = [
  5782. 'div' => ['class' => 'input textarea'],
  5783. 'label' => ['for' => 'stuff'],
  5784. 'Stuff',
  5785. '/label',
  5786. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  5787. '/textarea',
  5788. '/div'
  5789. ];
  5790. $this->assertHtml($expected, $result);
  5791. }
  5792. /**
  5793. * testHiddenField method
  5794. *
  5795. * @return void
  5796. */
  5797. public function testHiddenField()
  5798. {
  5799. $this->article['errors'] = [
  5800. 'field' => true
  5801. ];
  5802. $this->Form->request->data['field'] = 'test';
  5803. $this->Form->create($this->article);
  5804. $result = $this->Form->hidden('field', ['id' => 'theID']);
  5805. $expected = [
  5806. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  5807. $this->assertHtml($expected, $result);
  5808. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5809. $expected = [
  5810. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  5811. ];
  5812. $this->assertHtml($expected, $result);
  5813. }
  5814. /**
  5815. * testFileUploadField method
  5816. *
  5817. * @return void
  5818. */
  5819. public function testFileUploadField()
  5820. {
  5821. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5822. $result = $this->Form->file('Model.upload');
  5823. $this->assertHtml($expected, $result);
  5824. $this->Form->request->data['Model']['upload'] = [
  5825. 'name' => '', 'type' => '', 'tmp_name' => '',
  5826. 'error' => 4, 'size' => 0
  5827. ];
  5828. $result = $this->Form->file('Model.upload');
  5829. $this->assertHtml($expected, $result);
  5830. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5831. $result = $this->Form->file('Model.upload');
  5832. $this->assertHtml($expected, $result);
  5833. }
  5834. /**
  5835. * test File upload input on a model not used in create();
  5836. *
  5837. * @return void
  5838. */
  5839. public function testFileUploadOnOtherModel()
  5840. {
  5841. $this->Form->create($this->article, ['type' => 'file']);
  5842. $result = $this->Form->file('ValidateProfile.city');
  5843. $expected = [
  5844. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  5845. ];
  5846. $this->assertHtml($expected, $result);
  5847. }
  5848. /**
  5849. * testButton method
  5850. *
  5851. * @return void
  5852. */
  5853. public function testButton()
  5854. {
  5855. $result = $this->Form->button('Hi');
  5856. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  5857. $this->assertHtml($expected, $result);
  5858. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  5859. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  5860. $this->assertHtml($expected, $result);
  5861. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  5862. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  5863. $this->assertHtml($expected, $result);
  5864. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  5865. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  5866. $this->assertHtml($expected, $result);
  5867. $result = $this->Form->button('No type', ['type' => false]);
  5868. $expected = ['button' => [], 'No type', '/button'];
  5869. $this->assertHtml($expected, $result);
  5870. $result = $this->Form->button('Upload Text', [
  5871. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5872. 'escape' => false
  5873. ]);
  5874. $this->assertNotRegExp('/\&039/', $result);
  5875. }
  5876. /**
  5877. * Test that button() makes unlocked fields by default.
  5878. *
  5879. * @return void
  5880. */
  5881. public function testButtonUnlockedByDefault()
  5882. {
  5883. $this->Form->request->params['_csrfToken'] = 'secured';
  5884. $this->Form->button('Save', ['name' => 'save']);
  5885. $this->Form->button('Clear');
  5886. $result = $this->Form->unlockField();
  5887. $this->assertEquals(['save'], $result);
  5888. }
  5889. /**
  5890. * testPostButton method
  5891. *
  5892. * @return void
  5893. */
  5894. public function testPostButton()
  5895. {
  5896. $result = $this->Form->postButton('Hi', '/controller/action');
  5897. $expected = [
  5898. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  5899. 'div' => ['style' => 'display:none;'],
  5900. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  5901. '/div',
  5902. 'button' => ['type' => 'submit'],
  5903. 'Hi',
  5904. '/button',
  5905. '/form'
  5906. ];
  5907. $this->assertHtml($expected, $result);
  5908. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  5909. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  5910. }
  5911. /**
  5912. * Test using postButton with N dimensional data.
  5913. *
  5914. * @return void
  5915. */
  5916. public function testPostButtonNestedData()
  5917. {
  5918. $data = [
  5919. 'one' => [
  5920. 'two' => [
  5921. 3, 4, 5
  5922. ]
  5923. ]
  5924. ];
  5925. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  5926. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5927. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5928. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5929. }
  5930. /**
  5931. * Test that postButton adds _Token fields.
  5932. *
  5933. * @return void
  5934. */
  5935. public function testSecurePostButton()
  5936. {
  5937. $this->Form->request->params['_csrfToken'] = 'testkey';
  5938. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5939. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5940. $expected = [
  5941. 'form' => [
  5942. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5943. ],
  5944. ['div' => ['style' => 'display:none;']],
  5945. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  5946. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  5947. '/div',
  5948. 'button' => ['type' => 'submit'],
  5949. 'Delete',
  5950. '/button',
  5951. ['div' => ['style' => 'display:none;']],
  5952. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  5953. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  5954. '/div',
  5955. '/form',
  5956. ];
  5957. $this->assertHtml($expected, $result);
  5958. }
  5959. /**
  5960. * testPostLink method
  5961. *
  5962. * @return void
  5963. */
  5964. public function testPostLink()
  5965. {
  5966. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5967. $expected = [
  5968. 'form' => [
  5969. 'method' => 'post', 'action' => '/posts/delete/1',
  5970. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5971. ],
  5972. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  5973. '/form',
  5974. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  5975. 'Delete',
  5976. '/a'
  5977. ];
  5978. $this->assertHtml($expected, $result);
  5979. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  5980. $expected = [
  5981. 'form' => [
  5982. 'method' => 'post', 'action' => '/posts/delete/1',
  5983. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5984. ],
  5985. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  5986. '/form',
  5987. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  5988. 'Delete',
  5989. '/a'
  5990. ];
  5991. $this->assertHtml($expected, $result);
  5992. $result = $this->Form->postLink(
  5993. 'Delete',
  5994. '/posts/delete/1',
  5995. ['target' => '_blank', 'class' => 'btn btn-danger']
  5996. );
  5997. $expected = [
  5998. 'form' => [
  5999. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6000. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6001. ],
  6002. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6003. '/form',
  6004. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6005. 'Delete',
  6006. '/a'
  6007. ];
  6008. $this->assertHtml($expected, $result);
  6009. }
  6010. /**
  6011. * Test the confirm option for postLink()
  6012. *
  6013. * @return void
  6014. */
  6015. public function testPostLinkWithConfirm()
  6016. {
  6017. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6018. $expected = [
  6019. 'form' => [
  6020. 'method' => 'post', 'action' => '/posts/delete/1',
  6021. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6022. ],
  6023. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6024. '/form',
  6025. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6026. 'Delete',
  6027. '/a'
  6028. ];
  6029. $this->assertHtml($expected, $result);
  6030. $result = $this->Form->postLink(
  6031. 'Delete',
  6032. '/posts/delete/1',
  6033. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6034. );
  6035. $expected = [
  6036. 'form' => [
  6037. 'method' => 'post', 'action' => '/posts/delete/1',
  6038. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6039. ],
  6040. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6041. '/form',
  6042. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\nthis \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6043. 'Delete',
  6044. '/a'
  6045. ];
  6046. $this->assertHtml($expected, $result);
  6047. }
  6048. /**
  6049. * test postLink() with query string args.
  6050. *
  6051. * @return void
  6052. */
  6053. public function testPostLinkWithQuery()
  6054. {
  6055. $result = $this->Form->postLink(
  6056. 'Delete',
  6057. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6058. );
  6059. $expected = [
  6060. 'form' => [
  6061. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6062. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6063. ],
  6064. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6065. '/form',
  6066. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6067. 'Delete',
  6068. '/a'
  6069. ];
  6070. $this->assertHtml($expected, $result);
  6071. }
  6072. /**
  6073. * Test postLink with additional data.
  6074. *
  6075. * @return void
  6076. */
  6077. public function testPostLinkWithData()
  6078. {
  6079. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6080. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6081. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6082. $this->Form->create($entity);
  6083. $this->Form->end();
  6084. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6085. $this->assertContains(
  6086. '<input type="hidden" name="name" value="show"',
  6087. $result,
  6088. 'should not contain entity data.'
  6089. );
  6090. }
  6091. /**
  6092. * Test that security hashes for postLink include the url.
  6093. *
  6094. * @return void
  6095. */
  6096. public function testPostLinkSecurityHash()
  6097. {
  6098. $hash = Security::hash(
  6099. '/posts/delete/1' .
  6100. serialize(['id' => '1']) .
  6101. '' .
  6102. Security::salt()
  6103. );
  6104. $hash .= '%3Aid';
  6105. $this->Form->request->params['_Token']['key'] = 'test';
  6106. $result = $this->Form->postLink(
  6107. 'Delete',
  6108. '/posts/delete/1',
  6109. ['data' => ['id' => 1]]
  6110. );
  6111. $expected = [
  6112. 'form' => [
  6113. 'method' => 'post', 'action' => '/posts/delete/1',
  6114. 'name', 'style' => 'display:none;'
  6115. ],
  6116. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6117. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6118. 'div' => ['style' => 'display:none;'],
  6119. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6120. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6121. '/div',
  6122. '/form',
  6123. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6124. 'Delete',
  6125. '/a'
  6126. ];
  6127. $this->assertHtml($expected, $result);
  6128. }
  6129. /**
  6130. * Test using postLink with N dimensional data.
  6131. *
  6132. * @return void
  6133. */
  6134. public function testPostLinkNestedData()
  6135. {
  6136. $data = [
  6137. 'one' => [
  6138. 'two' => [
  6139. 3, 4, 5
  6140. ]
  6141. ]
  6142. ];
  6143. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6144. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6145. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6146. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6147. }
  6148. /**
  6149. * test creating postLinks after a GET form.
  6150. *
  6151. * @return void
  6152. */
  6153. public function testPostLinkAfterGetForm()
  6154. {
  6155. $this->Form->request->params['_csrfToken'] = 'testkey';
  6156. $this->Form->request->params['_Token'] = 'val';
  6157. $this->Form->create($this->article, ['type' => 'get']);
  6158. $this->Form->end();
  6159. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6160. $expected = [
  6161. 'form' => [
  6162. 'method' => 'post', 'action' => '/posts/delete/1',
  6163. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6164. ],
  6165. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6166. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6167. 'div' => ['style' => 'display:none;'],
  6168. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6169. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6170. '/div',
  6171. '/form',
  6172. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6173. 'Delete',
  6174. '/a'
  6175. ];
  6176. $this->assertHtml($expected, $result);
  6177. }
  6178. /**
  6179. * Test that postLink adds form tags to view block
  6180. *
  6181. * @return void
  6182. */
  6183. public function testPostLinkFormBuffer()
  6184. {
  6185. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6186. $expected = [
  6187. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6188. 'Delete',
  6189. '/a'
  6190. ];
  6191. $this->assertHtml($expected, $result);
  6192. $result = $this->View->fetch('postLink');
  6193. $expected = [
  6194. 'form' => [
  6195. 'method' => 'post', 'action' => '/posts/delete/1',
  6196. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6197. ],
  6198. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6199. '/form'
  6200. ];
  6201. $this->assertHtml($expected, $result);
  6202. $result = $this->Form->postLink(
  6203. 'Delete',
  6204. '/posts/delete/2',
  6205. ['block' => true, 'method' => 'DELETE']
  6206. );
  6207. $expected = [
  6208. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6209. 'Delete',
  6210. '/a'
  6211. ];
  6212. $this->assertHtml($expected, $result);
  6213. $result = $this->View->fetch('postLink');
  6214. $expected = [
  6215. 'form' => [
  6216. 'method' => 'post', 'action' => '/posts/delete/1',
  6217. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6218. ],
  6219. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6220. '/form',
  6221. [
  6222. 'form' => [
  6223. 'method' => 'post', 'action' => '/posts/delete/2',
  6224. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6225. ],
  6226. ],
  6227. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6228. '/form'
  6229. ];
  6230. $this->assertHtml($expected, $result);
  6231. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6232. $expected = [
  6233. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6234. 'Delete',
  6235. '/a'
  6236. ];
  6237. $this->assertHtml($expected, $result);
  6238. $result = $this->View->fetch('foobar');
  6239. $expected = [
  6240. 'form' => [
  6241. 'method' => 'post', 'action' => '/posts/delete/1',
  6242. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6243. ],
  6244. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6245. '/form'
  6246. ];
  6247. $this->assertHtml($expected, $result);
  6248. }
  6249. /**
  6250. * testSubmitButton method
  6251. *
  6252. * @return void
  6253. */
  6254. public function testSubmitButton()
  6255. {
  6256. $result = $this->Form->submit('');
  6257. $expected = [
  6258. 'div' => ['class' => 'submit'],
  6259. 'input' => ['type' => 'submit', 'value' => ''],
  6260. '/div'
  6261. ];
  6262. $this->assertHtml($expected, $result);
  6263. $result = $this->Form->submit('Test Submit');
  6264. $expected = [
  6265. 'div' => ['class' => 'submit'],
  6266. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6267. '/div'
  6268. ];
  6269. $this->assertHtml($expected, $result);
  6270. $result = $this->Form->submit('Next >');
  6271. $expected = [
  6272. 'div' => ['class' => 'submit'],
  6273. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6274. '/div'
  6275. ];
  6276. $this->assertHtml($expected, $result);
  6277. $result = $this->Form->submit('Next >', ['escape' => false]);
  6278. $expected = [
  6279. 'div' => ['class' => 'submit'],
  6280. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6281. '/div'
  6282. ];
  6283. $this->assertHtml($expected, $result);
  6284. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6285. $expected = [
  6286. 'div' => ['class' => 'submit'],
  6287. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6288. '/div'
  6289. ];
  6290. $this->assertHtml($expected, $result);
  6291. }
  6292. /**
  6293. * test image submit types.
  6294. *
  6295. * @return void
  6296. */
  6297. public function testSubmitImage()
  6298. {
  6299. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6300. $expected = [
  6301. 'div' => ['class' => 'submit'],
  6302. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6303. '/div'
  6304. ];
  6305. $this->assertHtml($expected, $result);
  6306. $result = $this->Form->submit('/relative/cake.power.gif');
  6307. $expected = [
  6308. 'div' => ['class' => 'submit'],
  6309. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6310. '/div'
  6311. ];
  6312. $this->assertHtml($expected, $result);
  6313. $result = $this->Form->submit('cake.power.gif');
  6314. $expected = [
  6315. 'div' => ['class' => 'submit'],
  6316. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6317. '/div'
  6318. ];
  6319. $this->assertHtml($expected, $result);
  6320. $result = $this->Form->submit('Not.an.image');
  6321. $expected = [
  6322. 'div' => ['class' => 'submit'],
  6323. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6324. '/div'
  6325. ];
  6326. $this->assertHtml($expected, $result);
  6327. }
  6328. /**
  6329. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6330. * be submitted at a time.
  6331. *
  6332. * @return void
  6333. */
  6334. public function testSubmitUnlockedByDefault()
  6335. {
  6336. $this->Form->request->params['_Token'] = 'secured';
  6337. $this->Form->submit('Go go');
  6338. $this->Form->submit('Save', ['name' => 'save']);
  6339. $result = $this->Form->unlockField();
  6340. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6341. }
  6342. /**
  6343. * Test submit image with timestamps.
  6344. *
  6345. * @return void
  6346. */
  6347. public function testSubmitImageTimestamp()
  6348. {
  6349. Configure::write('Asset.timestamp', 'force');
  6350. $result = $this->Form->submit('cake.power.gif');
  6351. $expected = [
  6352. 'div' => ['class' => 'submit'],
  6353. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6354. '/div'
  6355. ];
  6356. $this->assertHtml($expected, $result);
  6357. }
  6358. /**
  6359. * test that datetime() works with GET style forms.
  6360. *
  6361. * @return void
  6362. */
  6363. public function testDateTimeWithGetForms()
  6364. {
  6365. extract($this->dateRegex);
  6366. $this->Form->create($this->article, ['type' => 'get']);
  6367. $result = $this->Form->datetime('created');
  6368. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  6369. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  6370. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  6371. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  6372. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  6373. }
  6374. /**
  6375. * testForMagicInputNonExistingNorValidated method
  6376. *
  6377. * @return void
  6378. */
  6379. public function testForMagicInputNonExistingNorValidated()
  6380. {
  6381. $result = $this->Form->create($this->article);
  6382. $this->Form->templates(['inputContainer' => '{{content}}']);
  6383. $result = $this->Form->input('non_existing_nor_validated');
  6384. $expected = [
  6385. 'label' => ['for' => 'non-existing-nor-validated'],
  6386. 'Non Existing Nor Validated',
  6387. '/label',
  6388. 'input' => [
  6389. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6390. 'id' => 'non-existing-nor-validated'
  6391. ]
  6392. ];
  6393. $this->assertHtml($expected, $result);
  6394. $result = $this->Form->input('non_existing_nor_validated', [
  6395. 'val' => 'my value'
  6396. ]);
  6397. $expected = [
  6398. 'label' => ['for' => 'non-existing-nor-validated'],
  6399. 'Non Existing Nor Validated',
  6400. '/label',
  6401. 'input' => [
  6402. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6403. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  6404. ]
  6405. ];
  6406. $this->assertHtml($expected, $result);
  6407. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  6408. $result = $this->Form->input('non_existing_nor_validated');
  6409. $expected = [
  6410. 'label' => ['for' => 'non-existing-nor-validated'],
  6411. 'Non Existing Nor Validated',
  6412. '/label',
  6413. 'input' => [
  6414. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6415. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  6416. ]
  6417. ];
  6418. $this->assertHtml($expected, $result);
  6419. }
  6420. /**
  6421. * testFormMagicInputLabel method
  6422. *
  6423. * @return void
  6424. */
  6425. public function testFormMagicInputLabel()
  6426. {
  6427. TableRegistry::get('Contacts', [
  6428. 'className' => __NAMESPACE__ . '\ContactsTable'
  6429. ]);
  6430. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6431. $this->Form->templates(['inputContainer' => '{{content}}']);
  6432. $result = $this->Form->input('Contacts.name', ['label' => 'My label']);
  6433. $expected = [
  6434. 'label' => ['for' => 'contacts-name'],
  6435. 'My label',
  6436. '/label',
  6437. 'input' => [
  6438. 'type' => 'text', 'name' => 'Contacts[name]',
  6439. 'id' => 'contacts-name', 'maxlength' => '255'
  6440. ]
  6441. ];
  6442. $this->assertHtml($expected, $result);
  6443. $result = $this->Form->input('name', [
  6444. 'label' => ['class' => 'mandatory']
  6445. ]);
  6446. $expected = [
  6447. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6448. 'Name',
  6449. '/label',
  6450. 'input' => [
  6451. 'type' => 'text', 'name' => 'name',
  6452. 'id' => 'name', 'maxlength' => '255'
  6453. ]
  6454. ];
  6455. $this->assertHtml($expected, $result);
  6456. $result = $this->Form->input('name', [
  6457. 'div' => false,
  6458. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  6459. ]);
  6460. $expected = [
  6461. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6462. 'My label',
  6463. '/label',
  6464. 'input' => [
  6465. 'type' => 'text', 'name' => 'name',
  6466. 'id' => 'name', 'maxlength' => '255'
  6467. ]
  6468. ];
  6469. $this->assertHtml($expected, $result);
  6470. $result = $this->Form->input('Contact.name', [
  6471. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  6472. ]);
  6473. $expected = [
  6474. 'label' => ['for' => 'my_id'],
  6475. 'Name',
  6476. '/label',
  6477. 'input' => [
  6478. 'type' => 'text', 'name' => 'Contact[name]',
  6479. 'id' => 'my_id', 'maxlength' => '255'
  6480. ]
  6481. ];
  6482. $this->assertHtml($expected, $result);
  6483. $result = $this->Form->input('1.id');
  6484. $expected = ['input' => [
  6485. 'type' => 'hidden', 'name' => '1[id]',
  6486. 'id' => '1-id'
  6487. ]];
  6488. $this->assertHtml($expected, $result);
  6489. $result = $this->Form->input("1.name");
  6490. $expected = [
  6491. 'label' => ['for' => '1-name'],
  6492. 'Name',
  6493. '/label',
  6494. 'input' => [
  6495. 'type' => 'text', 'name' => '1[name]',
  6496. 'id' => '1-name', 'maxlength' => '255'
  6497. ]
  6498. ];
  6499. $this->assertHtml($expected, $result);
  6500. }
  6501. /**
  6502. * testFormEnd method
  6503. *
  6504. * @return void
  6505. */
  6506. public function testFormEnd()
  6507. {
  6508. $this->assertEquals('</form>', $this->Form->end());
  6509. }
  6510. /**
  6511. * Test the generation of fields for a multi record form.
  6512. *
  6513. * @return void
  6514. */
  6515. public function testMultiRecordForm()
  6516. {
  6517. $this->loadFixtures('Articles', 'Comments');
  6518. $articles = TableRegistry::get('Articles');
  6519. $articles->hasMany('Comments');
  6520. $comment = new Entity(['comment' => 'Value']);
  6521. $article = new Article(['comments' => [$comment]]);
  6522. $this->Form->create([$article]);
  6523. $result = $this->Form->input('0.comments.1.comment');
  6524. //@codingStandardsIgnoreStart
  6525. $expected = [
  6526. 'div' => ['class' => 'input textarea'],
  6527. 'label' => ['for' => '0-comments-1-comment'],
  6528. 'Comment',
  6529. '/label',
  6530. 'textarea' => [
  6531. 'name',
  6532. 'id' => '0-comments-1-comment',
  6533. 'rows' => 5
  6534. ],
  6535. '/textarea',
  6536. '/div'
  6537. ];
  6538. //@codingStandardsIgnoreEnd
  6539. $this->assertHtml($expected, $result);
  6540. $result = $this->Form->input('0.comments.0.comment');
  6541. //@codingStandardsIgnoreStart
  6542. $expected = [
  6543. 'div' => ['class' => 'input textarea'],
  6544. 'label' => ['for' => '0-comments-0-comment'],
  6545. 'Comment',
  6546. '/label',
  6547. 'textarea' => [
  6548. 'name',
  6549. 'id' => '0-comments-0-comment',
  6550. 'rows' => 5
  6551. ],
  6552. 'Value',
  6553. '/textarea',
  6554. '/div'
  6555. ];
  6556. //@codingStandardsIgnoreEnd
  6557. $this->assertHtml($expected, $result);
  6558. $comment->errors('comment', ['Not valid']);
  6559. $result = $this->Form->input('0.comments.0.comment');
  6560. //@codingStandardsIgnoreStart
  6561. $expected = [
  6562. 'div' => ['class' => 'input textarea error'],
  6563. 'label' => ['for' => '0-comments-0-comment'],
  6564. 'Comment',
  6565. '/label',
  6566. 'textarea' => [
  6567. 'name',
  6568. 'class' => 'form-error',
  6569. 'id' => '0-comments-0-comment',
  6570. 'rows' => 5
  6571. ],
  6572. 'Value',
  6573. '/textarea',
  6574. ['div' => ['class' => 'error-message']],
  6575. 'Not valid',
  6576. '/div',
  6577. '/div'
  6578. ];
  6579. //@codingStandardsIgnoreEnd
  6580. $this->assertHtml($expected, $result);
  6581. TableRegistry::get('Comments')
  6582. ->validator('default')
  6583. ->allowEmpty('comment', false);
  6584. $result = $this->Form->input('0.comments.1.comment');
  6585. //@codingStandardsIgnoreStart
  6586. $expected = [
  6587. 'div' => ['class' => 'input textarea required'],
  6588. 'label' => ['for' => '0-comments-1-comment'],
  6589. 'Comment',
  6590. '/label',
  6591. 'textarea' => [
  6592. 'name',
  6593. 'required' => 'required',
  6594. 'id' => '0-comments-1-comment',
  6595. 'rows' => 5
  6596. ],
  6597. '/textarea',
  6598. '/div'
  6599. ];
  6600. //@codingStandardsIgnoreEnd
  6601. $this->assertHtml($expected, $result);
  6602. }
  6603. /**
  6604. * test that some html5 inputs + FormHelper::__call() work
  6605. *
  6606. * @return void
  6607. */
  6608. public function testHtml5Inputs()
  6609. {
  6610. $result = $this->Form->email('User.email');
  6611. $expected = [
  6612. 'input' => ['type' => 'email', 'name' => 'User[email]']
  6613. ];
  6614. $this->assertHtml($expected, $result);
  6615. $result = $this->Form->search('User.query');
  6616. $expected = [
  6617. 'input' => ['type' => 'search', 'name' => 'User[query]']
  6618. ];
  6619. $this->assertHtml($expected, $result);
  6620. $result = $this->Form->search('User.query', ['value' => 'test']);
  6621. $expected = [
  6622. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  6623. ];
  6624. $this->assertHtml($expected, $result);
  6625. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  6626. $expected = [
  6627. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  6628. ];
  6629. $this->assertHtml($expected, $result);
  6630. }
  6631. /**
  6632. * Test accessing html5 inputs through input().
  6633. *
  6634. * @return void
  6635. */
  6636. public function testHtml5InputWithInput()
  6637. {
  6638. $this->Form->create();
  6639. $this->Form->templates(['inputContainer' => '{{content}}']);
  6640. $result = $this->Form->input('website', [
  6641. 'type' => 'url',
  6642. 'val' => 'http://domain.tld',
  6643. 'label' => false
  6644. ]);
  6645. $expected = [
  6646. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  6647. ];
  6648. $this->assertHtml($expected, $result);
  6649. }
  6650. /**
  6651. * Test errors when field name is missing.
  6652. *
  6653. * @expectedException \Cake\Core\Exception\Exception
  6654. * @return void
  6655. */
  6656. public function testHtml5InputException()
  6657. {
  6658. $this->Form->email();
  6659. }
  6660. /**
  6661. * Tests that formhelper sets required attributes.
  6662. *
  6663. * @return void
  6664. */
  6665. public function testRequiredAttribute()
  6666. {
  6667. $this->article['required'] = [
  6668. 'title' => true,
  6669. 'body' => false,
  6670. ];
  6671. $this->Form->create($this->article);
  6672. $result = $this->Form->input('title');
  6673. $expected = [
  6674. 'div' => ['class' => 'input text required'],
  6675. 'label' => ['for' => 'title'],
  6676. 'Title',
  6677. '/label',
  6678. 'input' => [
  6679. 'type' => 'text',
  6680. 'name' => 'title',
  6681. 'id' => 'title',
  6682. 'required' => 'required',
  6683. ],
  6684. '/div'
  6685. ];
  6686. $this->assertHtml($expected, $result);
  6687. $result = $this->Form->input('title', ['required' => false]);
  6688. $this->assertNotContains('required', $result);
  6689. $result = $this->Form->input('body');
  6690. $expected = [
  6691. 'div' => ['class' => 'input text'],
  6692. 'label' => ['for' => 'body'],
  6693. 'Body',
  6694. '/label',
  6695. 'input' => [
  6696. 'type' => 'text',
  6697. 'name' => 'body',
  6698. 'id' => 'body',
  6699. ],
  6700. '/div'
  6701. ];
  6702. $this->assertHtml($expected, $result);
  6703. $result = $this->Form->input('body', ['required' => true]);
  6704. $this->assertContains('required', $result);
  6705. }
  6706. /**
  6707. * Tests that it is possible to put inputs outside of the label
  6708. *
  6709. * @return void
  6710. */
  6711. public function testInputsNotNested()
  6712. {
  6713. $this->Form->templates([
  6714. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  6715. 'formGroup' => '{{input}}{{label}}',
  6716. ]);
  6717. $result = $this->Form->input('foo', ['type' => 'checkbox']);
  6718. $expected = [
  6719. 'div' => ['class' => 'input checkbox'],
  6720. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  6721. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  6722. 'label' => ['for' => 'foo'],
  6723. 'Foo',
  6724. '/label',
  6725. '/div'
  6726. ];
  6727. $this->assertHtml($expected, $result);
  6728. $result = $this->Form->input('foo', ['type' => 'checkbox', 'label' => false]);
  6729. $expected = [
  6730. 'div' => ['class' => 'input checkbox'],
  6731. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  6732. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  6733. '/div'
  6734. ];
  6735. $this->assertHtml($expected, $result);
  6736. $result = $this->Form->input('confirm', [
  6737. 'type' => 'radio',
  6738. 'options' => ['Y' => 'Yes', 'N' => 'No']
  6739. ]);
  6740. $expected = [
  6741. 'div' => ['class' => 'input radio'],
  6742. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  6743. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  6744. ['label' => ['for' => 'confirm-y']],
  6745. 'Yes',
  6746. '/label',
  6747. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  6748. ['label' => ['for' => 'confirm-n']],
  6749. 'No',
  6750. '/label',
  6751. '<label',
  6752. 'Confirm',
  6753. '/label',
  6754. '/div',
  6755. ];
  6756. $this->assertHtml($expected, $result);
  6757. $result = $this->Form->select('category', ['1', '2'], [
  6758. 'multiple' => 'checkbox',
  6759. 'name' => 'fish',
  6760. ]);
  6761. $expected = [
  6762. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  6763. ['div' => ['class' => 'checkbox']],
  6764. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  6765. ['label' => ['for' => 'fish-0']],
  6766. '1',
  6767. '/label',
  6768. '/div',
  6769. ['div' => ['class' => 'checkbox']],
  6770. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  6771. ['label' => ['for' => 'fish-1']],
  6772. '2',
  6773. '/label',
  6774. '/div'
  6775. ];
  6776. $this->assertHtml($expected, $result);
  6777. }
  6778. /**
  6779. * Test that *Container templates are used by input.
  6780. *
  6781. * @return void
  6782. */
  6783. public function testInputContainerTemplates()
  6784. {
  6785. $this->Form->templates([
  6786. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  6787. 'radioContainer' => '<div class="rad">{{content}}</div>',
  6788. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  6789. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  6790. ]);
  6791. $this->article['errors'] = [
  6792. 'Article' => ['published' => 'error message']
  6793. ];
  6794. $this->Form->create($this->article);
  6795. $result = $this->Form->input('accept', [
  6796. 'type' => 'checkbox'
  6797. ]);
  6798. $expected = [
  6799. 'div' => ['class' => 'check'],
  6800. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  6801. 'label' => ['for' => 'accept'],
  6802. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  6803. 'Accept',
  6804. '/label',
  6805. '/div'
  6806. ];
  6807. $this->assertHtml($expected, $result);
  6808. $result = $this->Form->input('accept', [
  6809. 'type' => 'radio',
  6810. 'options' => ['Y', 'N']
  6811. ]);
  6812. $this->assertContains('<div class="rad">', $result);
  6813. $result = $this->Form->input('Article.published', [
  6814. 'type' => 'radio',
  6815. 'options' => ['Y', 'N']
  6816. ]);
  6817. $this->assertContains('<div class="rad err">', $result);
  6818. $result = $this->Form->input('Article.created', [
  6819. 'type' => 'datetime',
  6820. ]);
  6821. $this->assertContains('<div class="dt">', $result);
  6822. }
  6823. /**
  6824. * Test that *Container templates are used by input.
  6825. *
  6826. * @return void
  6827. */
  6828. public function testFormGroupTemplates()
  6829. {
  6830. $this->Form->templates([
  6831. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  6832. ]);
  6833. $this->Form->create($this->article);
  6834. $result = $this->Form->input('accept', [
  6835. 'type' => 'radio',
  6836. 'options' => ['Y', 'N']
  6837. ]);
  6838. $this->assertContains('<div class="radio">', $result);
  6839. }
  6840. /**
  6841. * Test resetting templates.
  6842. *
  6843. * @return void
  6844. */
  6845. public function testResetTemplates()
  6846. {
  6847. $this->Form->templates(['input' => '<input/>']);
  6848. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  6849. $this->assertNull($this->Form->resetTemplates());
  6850. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  6851. }
  6852. /**
  6853. * Test the context method.
  6854. *
  6855. * @return void
  6856. */
  6857. public function testContext()
  6858. {
  6859. $result = $this->Form->context();
  6860. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  6861. $mock = $this->getMock('Cake\View\Form\ContextInterface');
  6862. $this->assertSame($mock, $this->Form->context($mock));
  6863. $this->assertSame($mock, $this->Form->context());
  6864. }
  6865. /**
  6866. * testAutoDomId
  6867. *
  6868. * @return void
  6869. */
  6870. public function testAutoDomId()
  6871. {
  6872. $result = $this->Form->text('field', ['id' => true]);
  6873. $expected = [
  6874. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  6875. ];
  6876. $this->assertHtml($expected, $result);
  6877. // Ensure id => doesn't cause problem when multiple inputs are generated.
  6878. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  6879. $expected = [
  6880. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  6881. ['label' => ['for' => 'field-0']],
  6882. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  6883. 'option A',
  6884. '/label',
  6885. ['label' => ['for' => 'field-1']],
  6886. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  6887. 'option B',
  6888. '/label',
  6889. ];
  6890. $this->assertHtml($expected, $result);
  6891. $result = $this->Form->select(
  6892. 'multi_field',
  6893. ['first', 'second'],
  6894. ['multiple' => 'checkbox', 'id' => true]
  6895. );
  6896. $expected = [
  6897. 'input' => [
  6898. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  6899. ],
  6900. ['div' => ['class' => 'checkbox']],
  6901. ['label' => ['for' => 'multi-field-0']],
  6902. ['input' => [
  6903. 'type' => 'checkbox', 'name' => 'multi_field[]',
  6904. 'value' => '0', 'id' => 'multi-field-0'
  6905. ]],
  6906. 'first',
  6907. '/label',
  6908. '/div',
  6909. ['div' => ['class' => 'checkbox']],
  6910. ['label' => ['for' => 'multi-field-1']],
  6911. ['input' => [
  6912. 'type' => 'checkbox', 'name' => 'multi_field[]',
  6913. 'value' => '1', 'id' => 'multi-field-1'
  6914. ]],
  6915. 'second',
  6916. '/label',
  6917. '/div',
  6918. ];
  6919. $this->assertHtml($expected, $result);
  6920. }
  6921. }