FormHelperTest.php 268 KB

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