FormHelperTest.php 243 KB

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